2023-01-28 17:43:04 +00:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWatcherCheckHash(t *testing.T) {
|
|
|
|
testDir, err := os.MkdirTemp("", "")
|
|
|
|
require.NoError(t, err, "creating test-tempdir")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if err := os.RemoveAll(testDir); err != nil {
|
|
|
|
t.Logf("failed to clean tempdir %q: %s", testDir, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
testFile := path.Join(testDir, "test.txt")
|
|
|
|
|
|
|
|
w, err := newWatcher(testFile, time.Second, WatcherCheckHash(sha256.New))
|
|
|
|
require.NoError(t, err, "initial check should not error on non existing file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err := w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on non existing file")
|
|
|
|
assert.Equal(t, WatcherEventInvalid, evt, "expect invalid as file is still missing")
|
|
|
|
|
|
|
|
err = os.WriteFile(testFile, []byte("test"), 0o644)
|
|
|
|
require.NoError(t, err, "creating test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventFileModified, evt, "expect change as file now exists and has hash change")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventNoChange, evt, "expect no change as the file has the same hash")
|
|
|
|
|
|
|
|
err = os.WriteFile(testFile, []byte("hello world"), 0o644)
|
|
|
|
require.NoError(t, err, "updating test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventFileModified, evt, "expect change as file was modified")
|
|
|
|
|
|
|
|
err = os.Remove(testFile)
|
|
|
|
require.NoError(t, err, "deleting test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on non existing file")
|
|
|
|
assert.Equal(t, WatcherEventInvalid, evt, "expect check to be invalid as file is no longer there")
|
|
|
|
|
|
|
|
err = os.WriteFile(testFile, []byte("hello world"), 0o644)
|
|
|
|
require.NoError(t, err, "updating test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventNoChange, evt, "expect change as file has same hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatcherCheckMtime(t *testing.T) {
|
|
|
|
testDir, err := os.MkdirTemp("", "")
|
|
|
|
require.NoError(t, err, "creating test-tempdir")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if err := os.RemoveAll(testDir); err != nil {
|
|
|
|
t.Logf("failed to clean tempdir %q: %s", testDir, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
testFile := path.Join(testDir, "test.txt")
|
|
|
|
|
|
|
|
w, err := newWatcher(testFile, time.Second, WatcherCheckMtime)
|
|
|
|
require.NoError(t, err, "initial check should not error on non existing file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err := w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on non existing file")
|
|
|
|
assert.Equal(t, WatcherEventInvalid, evt, "expect invalid as file is still missing")
|
|
|
|
|
|
|
|
err = os.WriteFile(testFile, []byte("test"), 0o644)
|
|
|
|
require.NoError(t, err, "creating test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventFileModified, evt, "expect change as file now exists and has mtime change")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventNoChange, evt, "expect no change as the file has the same mtime")
|
|
|
|
|
|
|
|
time.Sleep(time.Second) // Unix mtime is second-based, wait a moment
|
|
|
|
|
|
|
|
err = os.WriteFile(testFile, []byte("hello world"), 0o644)
|
|
|
|
require.NoError(t, err, "updating test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventFileModified, evt, "expect change as file was modified")
|
|
|
|
|
|
|
|
err = os.Remove(testFile)
|
|
|
|
require.NoError(t, err, "deleting test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on non existing file")
|
|
|
|
assert.Equal(t, WatcherEventInvalid, evt, "expect check to be invalid as file is no longer there")
|
|
|
|
|
|
|
|
time.Sleep(time.Second) // Unix mtime is second-based, wait a moment
|
|
|
|
|
|
|
|
err = os.WriteFile(testFile, []byte("hello world"), 0o644)
|
|
|
|
require.NoError(t, err, "updating test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventFileModified, evt, "expect change as file is newer")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatcherCheckPresence(t *testing.T) {
|
|
|
|
testDir, err := os.MkdirTemp("", "")
|
|
|
|
require.NoError(t, err, "creating test-tempdir")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if err := os.RemoveAll(testDir); err != nil {
|
|
|
|
t.Logf("failed to clean tempdir %q: %s", testDir, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
testFile := path.Join(testDir, "test.txt")
|
|
|
|
|
|
|
|
w, err := newWatcher(testFile, time.Second, WatcherCheckPresence)
|
|
|
|
require.NoError(t, err, "initial check should not error on non existing file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err := w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on non existing file")
|
|
|
|
assert.Equal(t, WatcherEventNoChange, evt, "expect no change as file is still missing")
|
|
|
|
|
|
|
|
err = os.WriteFile(testFile, []byte("test"), 0o644)
|
|
|
|
require.NoError(t, err, "creating test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventFileAppeared, evt, "expect check to state file is now there")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventNoChange, evt, "expect check to state nothing changed")
|
|
|
|
|
|
|
|
err = os.Remove(testFile)
|
|
|
|
require.NoError(t, err, "deleting test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on non existing file")
|
|
|
|
assert.Equal(t, WatcherEventFileVanished, evt, "expect check to state file vanished again")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatcherCheckSize(t *testing.T) {
|
|
|
|
testDir, err := os.MkdirTemp("", "")
|
|
|
|
require.NoError(t, err, "creating test-tempdir")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if err := os.RemoveAll(testDir); err != nil {
|
|
|
|
t.Logf("failed to clean tempdir %q: %s", testDir, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
testFile := path.Join(testDir, "test.txt")
|
|
|
|
|
|
|
|
w, err := newWatcher(testFile, time.Second, WatcherCheckSize)
|
|
|
|
require.NoError(t, err, "initial check should not error on non existing file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err := w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on non existing file")
|
|
|
|
assert.Equal(t, WatcherEventInvalid, evt, "expect invalid as file is still missing")
|
|
|
|
|
|
|
|
err = os.WriteFile(testFile, []byte("test"), 0o644)
|
|
|
|
require.NoError(t, err, "creating test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventFileModified, evt, "expect change as file has now 4 instead of 0 bytes")
|
|
|
|
|
|
|
|
err = os.WriteFile(testFile, []byte("tset"), 0o644)
|
|
|
|
require.NoError(t, err, "updating test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventNoChange, evt, "expect no change as the file has the same size")
|
|
|
|
|
|
|
|
err = os.WriteFile(testFile, []byte("hello world"), 0o644)
|
|
|
|
require.NoError(t, err, "updating test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventFileModified, evt, "expect change as we went from 4 to 11 chars")
|
|
|
|
|
|
|
|
err = os.Remove(testFile)
|
|
|
|
require.NoError(t, err, "deleting test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on non existing file")
|
|
|
|
assert.Equal(t, WatcherEventInvalid, evt, "expect check to be invalid as file is no longer there")
|
|
|
|
|
|
|
|
err = os.WriteFile(testFile, []byte("hello world"), 0o644)
|
|
|
|
require.NoError(t, err, "updating test file")
|
|
|
|
|
2023-03-18 14:37:42 +00:00
|
|
|
evt, err = w.runStateChecks()
|
2023-01-28 17:43:04 +00:00
|
|
|
require.NoError(t, err, "check should not error on existing file")
|
|
|
|
assert.Equal(t, WatcherEventNoChange, evt, "expect no change as we restored the file with same content")
|
|
|
|
}
|