From 9555da8d4870de5ac12939af833eb1e203ba3812 Mon Sep 17 00:00:00 2001 From: matoval Date: Wed, 13 Nov 2024 16:04:43 -0800 Subject: [PATCH 1/9] Add debug log for fsnotify remove and rename --- pkg/workceptor/workunitbase.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/workceptor/workunitbase.go b/pkg/workceptor/workunitbase.go index 72de02527..0c531d004 100644 --- a/pkg/workceptor/workunitbase.go +++ b/pkg/workceptor/workunitbase.go @@ -439,6 +439,16 @@ loop: if err != nil { bwu.w.nc.GetLogger().Error("Watcher Events Error reading %s: %s", statusFile, err) } + } else if event.Op&fsnotify.Remove == fsnotify.Remove { + err = bwu.Load() + if err != nil { + bwu.w.nc.GetLogger().Error("Watcher Events Remove reading %s: %s", statusFile, err) + } + } else if event.Op&fsnotify.Rename == fsnotify.Rename { + err = bwu.Load() + if err != nil { + bwu.w.nc.GetLogger().Error("Watcher Events Rename reading %s: %s", statusFile, err) + } } case <-time.After(time.Second): newFi, err := bwu.fs.Stat(statusFile) From 34016b798e6aeaddbd667bfba4cccaaffe8425e3 Mon Sep 17 00:00:00 2001 From: matoval Date: Wed, 13 Nov 2024 16:37:17 -0800 Subject: [PATCH 2/9] Replace if-else with switch --- pkg/workceptor/workunitbase.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/workceptor/workunitbase.go b/pkg/workceptor/workunitbase.go index 0c531d004..3cb8e9fe3 100644 --- a/pkg/workceptor/workunitbase.go +++ b/pkg/workceptor/workunitbase.go @@ -432,19 +432,20 @@ loop: case <-bwu.ctx.Done(): break loop case event := <-watcherEvents: - if event.Has(fsnotify.Create) { + switch { + case event.Has(fsnotify.Create): bwu.w.nc.GetLogger().Debug("Watcher Event create of %s", statusFile) - } else if event.Op&fsnotify.Write == fsnotify.Write { + case event.Op&fsnotify.Write == fsnotify.Write: err = bwu.Load() if err != nil { bwu.w.nc.GetLogger().Error("Watcher Events Error reading %s: %s", statusFile, err) } - } else if event.Op&fsnotify.Remove == fsnotify.Remove { + case event.Op&fsnotify.Remove == fsnotify.Remove: err = bwu.Load() if err != nil { bwu.w.nc.GetLogger().Error("Watcher Events Remove reading %s: %s", statusFile, err) } - } else if event.Op&fsnotify.Rename == fsnotify.Rename { + case event.Op&fsnotify.Rename == fsnotify.Rename: err = bwu.Load() if err != nil { bwu.w.nc.GetLogger().Error("Watcher Events Rename reading %s: %s", statusFile, err) From 4f567833c0366991f24d6fd0f52d42eb0c200608 Mon Sep 17 00:00:00 2001 From: matoval Date: Thu, 14 Nov 2024 15:43:28 -0800 Subject: [PATCH 3/9] Add unit test for remove and rename --- pkg/workceptor/workunitbase_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/workceptor/workunitbase_test.go b/pkg/workceptor/workunitbase_test.go index b21c7dfb7..6463972d6 100644 --- a/pkg/workceptor/workunitbase_test.go +++ b/pkg/workceptor/workunitbase_test.go @@ -343,6 +343,21 @@ func TestMonitorLocalStatus(t *testing.T) { fsNotifyEvent: &fsnotify.Event{Op: fsnotify.Write}, sleepDuration: 500 * time.Millisecond, }, + { + name: "Handle Remove Event", + statObj: NewInfo("test", 1, 0, time.Now()), + addWatcherErr: nil, + statErr: nil, + fsNotifyEvent: &fsnotify.Event{Op: fsnotify.Remove}, + sleepDuration: 100 * time.Millisecond, + },{ + name: "Handle Rename Event", + statObj: NewInfo("test", 1, 0, time.Now()), + addWatcherErr: nil, + statErr: nil, + fsNotifyEvent: &fsnotify.Event{Op: fsnotify.Rename}, + sleepDuration: 100 * time.Millisecond, + }, } for _, tc := range tests { From 0eab20b95138a92b46e41b816898eacecda09325 Mon Sep 17 00:00:00 2001 From: matoval Date: Thu, 14 Nov 2024 15:50:12 -0800 Subject: [PATCH 4/9] Fix lint --- pkg/workceptor/workunitbase_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/workceptor/workunitbase_test.go b/pkg/workceptor/workunitbase_test.go index 6463972d6..40b335b8a 100644 --- a/pkg/workceptor/workunitbase_test.go +++ b/pkg/workceptor/workunitbase_test.go @@ -350,7 +350,8 @@ func TestMonitorLocalStatus(t *testing.T) { statErr: nil, fsNotifyEvent: &fsnotify.Event{Op: fsnotify.Remove}, sleepDuration: 100 * time.Millisecond, - },{ + }, + { name: "Handle Rename Event", statObj: NewInfo("test", 1, 0, time.Now()), addWatcherErr: nil, From 5f8d3c5ca4466635fd27da163a3429d42dd9e5ac Mon Sep 17 00:00:00 2001 From: matoval Date: Thu, 14 Nov 2024 16:56:53 -0800 Subject: [PATCH 5/9] change remove and rename to debug and update tests --- pkg/workceptor/workunitbase.go | 4 +- pkg/workceptor/workunitbase_test.go | 77 +++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 22 deletions(-) diff --git a/pkg/workceptor/workunitbase.go b/pkg/workceptor/workunitbase.go index 3cb8e9fe3..476b7ba0d 100644 --- a/pkg/workceptor/workunitbase.go +++ b/pkg/workceptor/workunitbase.go @@ -443,12 +443,12 @@ loop: case event.Op&fsnotify.Remove == fsnotify.Remove: err = bwu.Load() if err != nil { - bwu.w.nc.GetLogger().Error("Watcher Events Remove reading %s: %s", statusFile, err) + bwu.w.nc.GetLogger().Debug("Watcher Events Remove reading %s: %s", statusFile, err) } case event.Op&fsnotify.Rename == fsnotify.Rename: err = bwu.Load() if err != nil { - bwu.w.nc.GetLogger().Error("Watcher Events Rename reading %s: %s", statusFile, err) + bwu.w.nc.GetLogger().Debug("Watcher Events Rename reading %s: %s", statusFile, err) } } case <-time.After(time.Second): diff --git a/pkg/workceptor/workunitbase_test.go b/pkg/workceptor/workunitbase_test.go index 40b335b8a..a6a361cf2 100644 --- a/pkg/workceptor/workunitbase_test.go +++ b/pkg/workceptor/workunitbase_test.go @@ -1,9 +1,11 @@ package workceptor_test import ( + "bytes" "context" "errors" "fmt" + "os" "path" "strings" "testing" @@ -81,12 +83,13 @@ func TestIsPending(t *testing.T) { } } -func setUp(t *testing.T) (*gomock.Controller, workceptor.BaseWorkUnit, *workceptor.Workceptor, *mock_workceptor.MockNetceptorForWorkceptor) { +func setUp(t *testing.T) (*gomock.Controller, workceptor.BaseWorkUnit, *workceptor.Workceptor, *mock_workceptor.MockNetceptorForWorkceptor, *logger.ReceptorLogger) { ctrl := gomock.NewController(t) mockNetceptor := mock_workceptor.NewMockNetceptorForWorkceptor(ctrl) // attach logger to the mock netceptor and return any number of times + logger.SetGlobalLogLevel(4) logger := logger.NewReceptorLogger("") mockNetceptor.EXPECT().GetLogger().AnyTimes().Return(logger) mockNetceptor.EXPECT().NodeID().Return("NodeID") @@ -98,45 +101,45 @@ func setUp(t *testing.T) (*gomock.Controller, workceptor.BaseWorkUnit, *workcept bwu := workceptor.BaseWorkUnit{} - return ctrl, bwu, w, mockNetceptor + return ctrl, bwu, w, mockNetceptor, logger } func TestInit(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "test", workceptor.FileSystem{}, nil) ctrl.Finish() } func TestErrorLog(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "test", workceptor.FileSystem{}, &workceptor.RealWatcher{}) bwu.Error("test error") ctrl.Finish() } func TestWarningLog(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "test", workceptor.FileSystem{}, &workceptor.RealWatcher{}) bwu.Warning("test warning") ctrl.Finish() } func TestInfoLog(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "test", workceptor.FileSystem{}, &workceptor.RealWatcher{}) bwu.Info("test info") ctrl.Finish() } func TestDebugLog(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "test", workceptor.FileSystem{}, &workceptor.RealWatcher{}) bwu.Error("test debug") ctrl.Finish() } func TestSetFromParams(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "test", workceptor.FileSystem{}, &workceptor.RealWatcher{}) err := bwu.SetFromParams(nil) if err != nil { @@ -152,7 +155,7 @@ const ( ) func TestUnitDir(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "test", workceptor.FileSystem{}, &workceptor.RealWatcher{}) expectedUnitDir := path.Join(rootDir, testDir) if unitDir := bwu.UnitDir(); unitDir != expectedUnitDir { @@ -162,7 +165,7 @@ func TestUnitDir(t *testing.T) { } func TestID(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "test", workceptor.FileSystem{}, &workceptor.RealWatcher{}) if id := bwu.ID(); id != "test" { t.Errorf("ID returned wrong value: got %s, want %s", id, "test") @@ -171,7 +174,7 @@ func TestID(t *testing.T) { } func TestStatusFileName(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "", workceptor.FileSystem{}, &workceptor.RealWatcher{}) expectedUnitDir := path.Join(rootDir, testDir) expectedStatusFileName := path.Join(expectedUnitDir, "status") @@ -182,7 +185,7 @@ func TestStatusFileName(t *testing.T) { } func TestStdoutFileName(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "", workceptor.FileSystem{}, &workceptor.RealWatcher{}) expectedUnitDir := path.Join(rootDir, testDir) expectedStdoutFileName := path.Join(expectedUnitDir, "stdout") @@ -193,7 +196,7 @@ func TestStdoutFileName(t *testing.T) { } func TestBaseSave(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "", workceptor.FileSystem{}, &workceptor.RealWatcher{}) err := bwu.Save() if !strings.Contains(err.Error(), dirError) { @@ -203,7 +206,7 @@ func TestBaseSave(t *testing.T) { } func TestBaseLoad(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "", workceptor.FileSystem{}, &workceptor.RealWatcher{}) err := bwu.Load() if !strings.Contains(err.Error(), dirError) { @@ -213,7 +216,7 @@ func TestBaseLoad(t *testing.T) { } func TestBaseUpdateFullStatus(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "", workceptor.FileSystem{}, &workceptor.RealWatcher{}) sf := func(sfd *workceptor.StatusFileData) { // Do nothing @@ -227,7 +230,7 @@ func TestBaseUpdateFullStatus(t *testing.T) { } func TestBaseUpdateBasicStatus(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "", workceptor.FileSystem{}, &workceptor.RealWatcher{}) bwu.UpdateBasicStatus(1, "Details", 0) err := bwu.LastUpdateError() @@ -238,7 +241,7 @@ func TestBaseUpdateBasicStatus(t *testing.T) { } func TestBaseStatus(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) bwu.Init(w, "test", "", workceptor.FileSystem{}, &workceptor.RealWatcher{}) status := bwu.Status() if status.State != workceptor.WorkStatePending { @@ -248,7 +251,7 @@ func TestBaseStatus(t *testing.T) { } func TestBaseRelease(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, _ := setUp(t) mockFileSystem := mock_workceptor.NewMockFileSystemer(ctrl) bwu.Init(w, "test", "", mockFileSystem, &workceptor.RealWatcher{}) @@ -300,6 +303,7 @@ func TestMonitorLocalStatus(t *testing.T) { addWatcherErr error statErr error fsNotifyEvent *fsnotify.Event // using pointer to allow nil + logOutput string sleepDuration time.Duration }{ { @@ -308,6 +312,7 @@ func TestMonitorLocalStatus(t *testing.T) { addWatcherErr: nil, statErr: nil, fsNotifyEvent: &fsnotify.Event{Op: fsnotify.Write}, + logOutput: "Watcher Events Error reading", sleepDuration: 100 * time.Millisecond, }, { @@ -316,6 +321,7 @@ func TestMonitorLocalStatus(t *testing.T) { addWatcherErr: fmt.Errorf("error adding watcher"), statErr: nil, fsNotifyEvent: nil, + logOutput: "", sleepDuration: 100 * time.Millisecond, }, { @@ -324,6 +330,7 @@ func TestMonitorLocalStatus(t *testing.T) { addWatcherErr: fmt.Errorf("error adding watcher"), statErr: fmt.Errorf("stat error"), fsNotifyEvent: nil, + logOutput: "", sleepDuration: 100 * time.Millisecond, }, { @@ -332,6 +339,7 @@ func TestMonitorLocalStatus(t *testing.T) { addWatcherErr: nil, statErr: nil, fsNotifyEvent: &fsnotify.Event{Op: fsnotify.Write}, + logOutput: "Watcher Events Error reading", sleepDuration: 100 * time.Millisecond, }, { @@ -341,6 +349,7 @@ func TestMonitorLocalStatus(t *testing.T) { addWatcherErr: nil, statErr: nil, fsNotifyEvent: &fsnotify.Event{Op: fsnotify.Write}, + logOutput: "Watcher Events Error reading", sleepDuration: 500 * time.Millisecond, }, { @@ -349,6 +358,7 @@ func TestMonitorLocalStatus(t *testing.T) { addWatcherErr: nil, statErr: nil, fsNotifyEvent: &fsnotify.Event{Op: fsnotify.Remove}, + logOutput: "Watcher Events Remove reading", sleepDuration: 100 * time.Millisecond, }, { @@ -357,14 +367,21 @@ func TestMonitorLocalStatus(t *testing.T) { addWatcherErr: nil, statErr: nil, fsNotifyEvent: &fsnotify.Event{Op: fsnotify.Rename}, + logOutput: "Watcher Events Rename reading", sleepDuration: 100 * time.Millisecond, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - ctrl, bwu, w, _ := setUp(t) + ctrl, bwu, w, _, l := setUp(t) defer ctrl.Finish() + logFilePath := "/tmp/monitorLocalStatusLog" + logFile, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600) + if err != nil { + t.Error("error creating monitorLocalStatusLog file") + } + l.SetOutput(logFile) mockWatcher := mock_workceptor.NewMockWatcherWrapper(ctrl) mockFileSystem := mock_workceptor.NewMockFileSystemer(ctrl) @@ -378,13 +395,33 @@ func TestMonitorLocalStatus(t *testing.T) { mockWatcher.EXPECT().Close().AnyTimes() if tc.fsNotifyEvent != nil { + logOutput, err := os.ReadFile(logFilePath) + if err != nil { + t.Error("error reading monitorLocalStatusLog file") + } eventCh := make(chan fsnotify.Event, 1) mockWatcher.EXPECT().EventChannel().Return(eventCh).AnyTimes() go func() { eventCh <- *tc.fsNotifyEvent }() errorCh := make(chan error, 1) mockWatcher.EXPECT().ErrorChannel().Return(errorCh).AnyTimes() - // go func() { errorCh <- nil }() + switch { + // write event + case tc.fsNotifyEvent.Op == 2: + if !bytes.Contains(logOutput, []byte(tc.logOutput)) { + t.Errorf("failed to log fsnotify event: %s", tc.logOutput) + } + // remove event + case tc.fsNotifyEvent.Op == 4: + if !bytes.Contains(logOutput, []byte(tc.logOutput)) { + t.Errorf("failed to log fsnotify event: %s", tc.logOutput) + } + // rename event + case tc.fsNotifyEvent.Op == 8: + if !bytes.Contains(logOutput, []byte(tc.logOutput)) { + t.Errorf("failed to log fsnotify event: %s", tc.logOutput) + } + } } go bwu.MonitorLocalStatus() From 83c5bb9d3a52e44e8f5af46fbad8ec84523eb88f Mon Sep 17 00:00:00 2001 From: matoval Date: Thu, 14 Nov 2024 17:50:18 -0800 Subject: [PATCH 6/9] Update test --- pkg/workceptor/workunitbase_test.go | 52 +++++++++++++---------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/pkg/workceptor/workunitbase_test.go b/pkg/workceptor/workunitbase_test.go index a6a361cf2..a454ca565 100644 --- a/pkg/workceptor/workunitbase_test.go +++ b/pkg/workceptor/workunitbase_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/ansible/receptor/pkg/logger" + "github.com/ansible/receptor/pkg/randstr" "github.com/ansible/receptor/pkg/workceptor" "github.com/ansible/receptor/pkg/workceptor/mock_workceptor" "github.com/fsnotify/fsnotify" @@ -376,12 +377,16 @@ func TestMonitorLocalStatus(t *testing.T) { t.Run(tc.name, func(t *testing.T) { ctrl, bwu, w, _, l := setUp(t) defer ctrl.Finish() - logFilePath := "/tmp/monitorLocalStatusLog" - logFile, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600) - if err != nil { - t.Error("error creating monitorLocalStatusLog file") + randstring := randstr.RandomString(4) + logFilePath := fmt.Sprintf("/tmp/monitorLocalStatusLog%s", randstring) + + if tc.fsNotifyEvent != nil { + logFile, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600) + if err != nil { + t.Error("error creating monitorLocalStatusLog file") + } + l.SetOutput(logFile) } - l.SetOutput(logFile) mockWatcher := mock_workceptor.NewMockWatcherWrapper(ctrl) mockFileSystem := mock_workceptor.NewMockFileSystemer(ctrl) @@ -395,38 +400,27 @@ func TestMonitorLocalStatus(t *testing.T) { mockWatcher.EXPECT().Close().AnyTimes() if tc.fsNotifyEvent != nil { - logOutput, err := os.ReadFile(logFilePath) - if err != nil { - t.Error("error reading monitorLocalStatusLog file") - } eventCh := make(chan fsnotify.Event, 1) mockWatcher.EXPECT().EventChannel().Return(eventCh).AnyTimes() go func() { eventCh <- *tc.fsNotifyEvent }() - + errorCh := make(chan error, 1) mockWatcher.EXPECT().ErrorChannel().Return(errorCh).AnyTimes() - switch { - // write event - case tc.fsNotifyEvent.Op == 2: - if !bytes.Contains(logOutput, []byte(tc.logOutput)) { - t.Errorf("failed to log fsnotify event: %s", tc.logOutput) - } - // remove event - case tc.fsNotifyEvent.Op == 4: - if !bytes.Contains(logOutput, []byte(tc.logOutput)) { - t.Errorf("failed to log fsnotify event: %s", tc.logOutput) - } - // rename event - case tc.fsNotifyEvent.Op == 8: - if !bytes.Contains(logOutput, []byte(tc.logOutput)) { - t.Errorf("failed to log fsnotify event: %s", tc.logOutput) - } - } } - + go bwu.MonitorLocalStatus() - time.Sleep(tc.sleepDuration) + + if tc.fsNotifyEvent != nil { + logOutput, err := os.ReadFile(logFilePath) + if err != nil && len(logOutput) == 0 { + t.Errorf("error reading %s file", logFilePath) + } + if !bytes.Contains(logOutput, []byte(tc.logOutput)) { + t.Errorf("expected log to be: %s, got %s", tc.logOutput, string(logOutput)) + } + } + bwu.CancelContext() }) } From a4aa1956d5bd3cdc39884c3cba4ab83c9a1924fd Mon Sep 17 00:00:00 2001 From: matoval Date: Thu, 14 Nov 2024 17:52:13 -0800 Subject: [PATCH 7/9] fix lint --- pkg/workceptor/workunitbase_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/workceptor/workunitbase_test.go b/pkg/workceptor/workunitbase_test.go index a454ca565..84f8a616c 100644 --- a/pkg/workceptor/workunitbase_test.go +++ b/pkg/workceptor/workunitbase_test.go @@ -403,14 +403,14 @@ func TestMonitorLocalStatus(t *testing.T) { eventCh := make(chan fsnotify.Event, 1) mockWatcher.EXPECT().EventChannel().Return(eventCh).AnyTimes() go func() { eventCh <- *tc.fsNotifyEvent }() - + errorCh := make(chan error, 1) mockWatcher.EXPECT().ErrorChannel().Return(errorCh).AnyTimes() } - + go bwu.MonitorLocalStatus() time.Sleep(tc.sleepDuration) - + if tc.fsNotifyEvent != nil { logOutput, err := os.ReadFile(logFilePath) if err != nil && len(logOutput) == 0 { From 1ec72fc5ac6d3638d075b85dec1092a6935947e3 Mon Sep 17 00:00:00 2001 From: matoval Date: Thu, 14 Nov 2024 17:58:00 -0800 Subject: [PATCH 8/9] lint --- pkg/workceptor/workunitbase_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/workceptor/workunitbase_test.go b/pkg/workceptor/workunitbase_test.go index 84f8a616c..0e134b42b 100644 --- a/pkg/workceptor/workunitbase_test.go +++ b/pkg/workceptor/workunitbase_test.go @@ -420,7 +420,7 @@ func TestMonitorLocalStatus(t *testing.T) { t.Errorf("expected log to be: %s, got %s", tc.logOutput, string(logOutput)) } } - + bwu.CancelContext() }) } From 066681b54b750ca16a27ced22970a751b9100065 Mon Sep 17 00:00:00 2001 From: matoval Date: Thu, 14 Nov 2024 18:13:06 -0800 Subject: [PATCH 9/9] refactor test --- pkg/workceptor/workunitbase_test.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pkg/workceptor/workunitbase_test.go b/pkg/workceptor/workunitbase_test.go index 0e134b42b..d59c31692 100644 --- a/pkg/workceptor/workunitbase_test.go +++ b/pkg/workceptor/workunitbase_test.go @@ -380,14 +380,6 @@ func TestMonitorLocalStatus(t *testing.T) { randstring := randstr.RandomString(4) logFilePath := fmt.Sprintf("/tmp/monitorLocalStatusLog%s", randstring) - if tc.fsNotifyEvent != nil { - logFile, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600) - if err != nil { - t.Error("error creating monitorLocalStatusLog file") - } - l.SetOutput(logFile) - } - mockWatcher := mock_workceptor.NewMockWatcherWrapper(ctrl) mockFileSystem := mock_workceptor.NewMockFileSystemer(ctrl) bwu.Init(w, "test", "", mockFileSystem, mockWatcher) @@ -400,6 +392,11 @@ func TestMonitorLocalStatus(t *testing.T) { mockWatcher.EXPECT().Close().AnyTimes() if tc.fsNotifyEvent != nil { + logFile, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600) + if err != nil { + t.Error("error creating monitorLocalStatusLog file") + } + l.SetOutput(logFile) eventCh := make(chan fsnotify.Event, 1) mockWatcher.EXPECT().EventChannel().Return(eventCh).AnyTimes() go func() { eventCh <- *tc.fsNotifyEvent }()