Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add UT negative case for filewatcher #2810

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/filewatcher/filewatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ var exit = func(code int) {

var watchCertificateFileOnce sync.Once

// resetWatchCertificateFileOnce resets the watchCertificateFileOnce variable. This is used for testing purposes.
func resetWatchCertificateFileOnce() {
watchCertificateFileOnce = sync.Once{}
}

// WatchFileForChanges watches the file, fileToWatch, for changes. If the file contents have changed, the pod this
// function is running on will be restarted.
func WatchFileForChanges(fileToWatch string) error {
Expand Down
46 changes: 30 additions & 16 deletions pkg/filewatcher/filewatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package filewatcher

import (
"os"
"strings"
"testing"
"time"
)
Expand All @@ -28,23 +29,36 @@ func TestWatchFileForChanges(t *testing.T) {
// Mock, do nothing
}

// Create a temporary file to watch
tmpfile, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
t.Run("ExistingFile", func(t *testing.T) {
// Create a temporary file to watch
tmpfile, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())

// Now call the watcher
if err = WatchFileForChanges(tmpfile.Name()); err != nil {
t.Errorf("Failed to watch file: %v", err)
}
// Now call the watcher
if err = WatchFileForChanges(tmpfile.Name()); err != nil {
t.Errorf("Failed to watch file: %v", err)
}

// Trigger the watcher
if err = os.WriteFile(tmpfile.Name(), []byte("new content"), 0644); err != nil {
t.Fatal(err)
}
// Trigger the watcher
if err = os.WriteFile(tmpfile.Name(), []byte("new content"), 0644); err != nil {
t.Fatal(err)
}

// Let the watcher see the change
time.Sleep(100 * time.Millisecond)
})

t.Run("NonExistentFile", func(t *testing.T) {
// Reset the watcher once before the test
resetWatchCertificateFileOnce()

// Let the watcher see the change
time.Sleep(1 * time.Second)
err := WatchFileForChanges("nonexistentfile")
if err == nil || (!strings.Contains(err.Error(), "no such file or directory") &&
!strings.Contains(err.Error(), "The system cannot find the file specified")) {
t.Errorf("expected error to contain 'no such file or directory' or 'The system cannot find the file specified', got %v", err)
}
})
}
Loading