From fa4f7c2154643ea9146803e65fc852d2c875b6c8 Mon Sep 17 00:00:00 2001 From: mengze zhu Date: Mon, 6 Jan 2025 07:58:46 +0000 Subject: [PATCH] test: add UT for filewatcher --- pkg/filewatcher/filewatcher.go | 16 +++++++-- pkg/filewatcher/filewatcher_test.go | 50 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 pkg/filewatcher/filewatcher_test.go diff --git a/pkg/filewatcher/filewatcher.go b/pkg/filewatcher/filewatcher.go index 52a8335ee0..605a321934 100644 --- a/pkg/filewatcher/filewatcher.go +++ b/pkg/filewatcher/filewatcher.go @@ -25,6 +25,11 @@ import ( "k8s.io/klog/v2" ) +// exit is a separate function to handle program termination +var exit = func(code int) { + os.Exit(code) +} + var watchCertificateFileOnce sync.Once // WatchFileForChanges watches the file, fileToWatch, for changes. If the file contents have changed, the pod this @@ -44,9 +49,16 @@ func WatchFileForChanges(fileToWatch string) error { klog.V(2).Infof("Watching file, %s", fileToWatch) // Start the file watcher to monitor file changes + errChan := make(chan error, 1) go func() { - err = checkForFileChanges(fileToWatch) + if err := checkForFileChanges(fileToWatch); err != nil { + errChan <- err + } }() + select { + case err = <-errChan: + default: + } }) return err } @@ -64,7 +76,7 @@ func checkForFileChanges(path string) error { case event, ok := <-watcher.Events: if ok && (event.Has(fsnotify.Write) || event.Has(fsnotify.Chmod) || event.Has(fsnotify.Remove)) { klog.V(2).Infof("file, %s, was modified, exiting...", event.Name) - os.Exit(0) + exit(0) } case err, ok := <-watcher.Errors: if ok { diff --git a/pkg/filewatcher/filewatcher_test.go b/pkg/filewatcher/filewatcher_test.go new file mode 100644 index 0000000000..84c97a5d95 --- /dev/null +++ b/pkg/filewatcher/filewatcher_test.go @@ -0,0 +1,50 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package filewatcher + +import ( + "os" + "testing" + "time" +) + +func TestWatchFileForChanges(t *testing.T) { + // Set mock exit once. + exit = func(_ int) { + // 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()) + + // 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) + } + + // Let the watcher see the change + time.Sleep(1 * time.Second) +}