2021-01-13 19:48:57 +00:00
|
|
|
package confwatcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func writeTempFile(byts []byte) (string, error) {
|
2022-09-17 19:19:45 +00:00
|
|
|
tmpf, err := os.CreateTemp(os.TempDir(), "confwatcher-")
|
2021-01-13 19:48:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer tmpf.Close()
|
|
|
|
|
|
|
|
_, err = tmpf.Write(byts)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpf.Name(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoFile(t *testing.T) {
|
|
|
|
_, err := New("/nonexistent")
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-13 20:47:23 +00:00
|
|
|
func TestWrite(t *testing.T) {
|
2021-01-13 19:48:57 +00:00
|
|
|
fpath, err := writeTempFile([]byte("{}"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
w, err := New(fpath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
func() {
|
|
|
|
f, err := os.Create(fpath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.Write([]byte("{}"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-w.Watch():
|
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
t.Errorf("timed out")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 20:47:23 +00:00
|
|
|
func TestWriteMultipleTimes(t *testing.T) {
|
2021-01-13 19:48:57 +00:00
|
|
|
fpath, err := writeTempFile([]byte("{}"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
w, err := New(fpath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
func() {
|
|
|
|
f, err := os.Create(fpath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.Write([]byte("{}"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
func() {
|
|
|
|
f, err := os.Create(fpath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.Write([]byte("{}"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-w.Watch():
|
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
t.Errorf("timed out")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
case <-w.Watch():
|
|
|
|
t.Errorf("should not happen")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 20:47:23 +00:00
|
|
|
func TestDeleteCreate(t *testing.T) {
|
2021-01-13 19:48:57 +00:00
|
|
|
fpath, err := writeTempFile([]byte("{}"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
w, err := New(fpath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer w.Close()
|
|
|
|
|
2021-01-13 20:47:23 +00:00
|
|
|
os.Remove(fpath)
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
func() {
|
|
|
|
f, err := os.Create(fpath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.Write([]byte("{}"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-w.Watch():
|
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
t.Errorf("timed out")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSymlinkDeleteCreate(t *testing.T) {
|
|
|
|
fpath, err := writeTempFile([]byte("{}"))
|
|
|
|
require.NoError(t, err)
|
2021-01-13 19:48:57 +00:00
|
|
|
|
2021-01-13 20:47:23 +00:00
|
|
|
err = os.Symlink(fpath, fpath+"-sym")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
w, err := New(fpath + "-sym")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer w.Close()
|
2021-01-13 19:48:57 +00:00
|
|
|
|
2021-01-13 20:47:23 +00:00
|
|
|
os.Remove(fpath)
|
|
|
|
|
|
|
|
func() {
|
|
|
|
f, err := os.Create(fpath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.Write([]byte("{}"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
2021-01-13 19:48:57 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-w.Watch():
|
2021-01-13 20:47:23 +00:00
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
t.Errorf("timed out")
|
|
|
|
return
|
2021-01-13 19:48:57 +00:00
|
|
|
}
|
2021-01-13 20:47:23 +00:00
|
|
|
}
|