diff --git a/controller/nucleus/initialise_test.go b/controller/nucleus/initialise_test.go index cf17d49d60ff3a2aedf77a7a446b29bc820c549b..ed1d2b16cdaea11009dc59c33a68edf61ec980a8 100644 --- a/controller/nucleus/initialise_test.go +++ b/controller/nucleus/initialise_test.go @@ -2,8 +2,12 @@ package nucleus import ( "context" + "io/fs" + "io/ioutil" "os" + "path/filepath" "testing" + "time" "code.fbi.h-da.de/danet/gosdn/controller/interfaces/device" "code.fbi.h-da.de/danet/gosdn/controller/store" @@ -166,3 +170,42 @@ func newPnd() pndImplementation { Id: defaultPndID, } } + +// removeTestPlugins removes the plugins created during running the test in the current directory based on their name consisting of a uuid +// and the time since they`ve been created +func removeTestPlugins() { + currentDirectory := "./" + // pattern to match plugin uuid used as dir name + pattern := "*-*-*-*-*" + // max time passed since creation in seconds + var seconds int64 = 10 + + // get all available files + dirs, err := ioutil.ReadDir(currentDirectory) + if err != nil { + log.Info(err) + } + + for _, dir := range dirs { + if !dir.IsDir() { + continue + } else if found, _ := filepath.Match(pattern, dir.Name()); found && isDirYoungerThanSeconds(dir.Name(), seconds) { + log.Infof("removing: %v", dir.Name()) + err = os.RemoveAll(dir.Name()) + if err != nil { + log.Info(err) + } + } + } +} + +// isDirYoungerThanSeconds returns true if the provided dir is younger than the given amount in seconds +// and therefore was created as part of the test suite +func isDirYoungerThanSeconds(dirName string, seconds int64) bool { + fileInfo, err := os.Stat(dirName) + if err != nil { + log.Info(err) + } + + return fs.FileInfo.ModTime(fileInfo).Unix() > (time.Now().Unix() - seconds) +} diff --git a/controller/nucleus/principalNetworkDomain_test.go b/controller/nucleus/principalNetworkDomain_test.go index 62da66887596e8a658476e1898d35ebd043b9cdc..907e027ab78ee1740d262247d985d76bd11778e5 100644 --- a/controller/nucleus/principalNetworkDomain_test.go +++ b/controller/nucleus/principalNetworkDomain_test.go @@ -1095,6 +1095,8 @@ func Test_pndImplementation_ConfirmedChanges(t *testing.T) { } func Test_pndImplementation_saveGoStructsToFile(t *testing.T) { + defer removeTestPlugins() + type genericGoStructClientArg struct { fn string rtrn []interface{}