75f3dd141c
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CI and Release / lint-frontend (push) Has been cancelled
CI and Release / dockerfile-scan (push) Has been cancelled
CI and Release / test-frontend (push) Has been cancelled
CI and Release / lint-verification-agent (push) Has been cancelled
CI and Release / test-verification-agent (push) Has been cancelled
CI and Release / e2e-verification-agent (push) Has been cancelled
CI and Release / test-backend (push) Has been cancelled
CI and Release / build-dev-image (push) Has been cancelled
CI and Release / push-dev-image (push) Has been cancelled
CI and Release / build-image (push) Has been cancelled
CI and Release / build-verification-image (push) Has been cancelled
CI and Release / determine-version (push) Has been cancelled
CI and Release / push-image (push) Has been cancelled
CI and Release / push-verification-image (12) (push) Has been cancelled
CI and Release / lint-backend (push) Has been cancelled
CI and Release / push-verification-image (17) (push) Has been cancelled
CI and Release / push-verification-image (18) (push) Has been cancelled
CI and Release / release (push) Has been cancelled
CI and Release / publish-helm-chart (push) Has been cancelled
CI and Release / push-verification-image (13) (push) Has been cancelled
CI and Release / push-verification-image (14) (push) Has been cancelled
CI and Release / push-verification-image (15) (push) Has been cancelled
CI and Release / push-verification-image (16) (push) Has been cancelled
111 lines
2.2 KiB
Go
111 lines
2.2 KiB
Go
//go:build !windows
|
|
|
|
package start
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"databasus-verification-agent/internal/logger"
|
|
)
|
|
|
|
func Test_NewLockWatcher_CapturesInode(t *testing.T) {
|
|
setupTempDir(t)
|
|
log := logger.GetLogger()
|
|
|
|
lockFile, err := AcquireLock(log)
|
|
require.NoError(t, err)
|
|
defer ReleaseLock(lockFile)
|
|
|
|
_, cancel := context.WithCancel(t.Context())
|
|
defer cancel()
|
|
|
|
watcher, err := NewLockWatcher(lockFile, cancel, log)
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, watcher.originalInode)
|
|
}
|
|
|
|
func Test_LockWatcher_FileUnchanged_ContextNotCancelled(t *testing.T) {
|
|
setupTempDir(t)
|
|
log := logger.GetLogger()
|
|
|
|
lockFile, err := AcquireLock(log)
|
|
require.NoError(t, err)
|
|
defer ReleaseLock(lockFile)
|
|
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
defer cancel()
|
|
|
|
watcher, err := NewLockWatcher(lockFile, cancel, log)
|
|
require.NoError(t, err)
|
|
|
|
watcher.check()
|
|
watcher.check()
|
|
watcher.check()
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
t.Fatal("context should not be cancelled when lock file is unchanged")
|
|
default:
|
|
}
|
|
}
|
|
|
|
func Test_LockWatcher_FileDeleted_CancelsContext(t *testing.T) {
|
|
setupTempDir(t)
|
|
log := logger.GetLogger()
|
|
|
|
lockFile, err := AcquireLock(log)
|
|
require.NoError(t, err)
|
|
defer ReleaseLock(lockFile)
|
|
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
defer cancel()
|
|
|
|
watcher, err := NewLockWatcher(lockFile, cancel, log)
|
|
require.NoError(t, err)
|
|
|
|
err = os.Remove(lockFileName)
|
|
require.NoError(t, err)
|
|
|
|
watcher.check()
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
default:
|
|
t.Fatal("context should be cancelled when lock file is deleted")
|
|
}
|
|
}
|
|
|
|
func Test_LockWatcher_FileReplacedWithDifferentInode_CancelsContext(t *testing.T) {
|
|
setupTempDir(t)
|
|
log := logger.GetLogger()
|
|
|
|
lockFile, err := AcquireLock(log)
|
|
require.NoError(t, err)
|
|
defer ReleaseLock(lockFile)
|
|
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
defer cancel()
|
|
|
|
watcher, err := NewLockWatcher(lockFile, cancel, log)
|
|
require.NoError(t, err)
|
|
|
|
err = os.Remove(lockFileName)
|
|
require.NoError(t, err)
|
|
|
|
err = os.WriteFile(lockFileName, []byte("99999\n"), 0o644)
|
|
require.NoError(t, err)
|
|
|
|
watcher.check()
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
default:
|
|
t.Fatal("context should be cancelled when lock file inode changes")
|
|
}
|
|
}
|