diff --git a/check/base.go b/check/base.go index 4b5cfbc4..e66213fd 100644 --- a/check/base.go +++ b/check/base.go @@ -111,3 +111,7 @@ func (c *FTWCheck) SetStartMarker(marker []byte) { func (c *FTWCheck) SetEndMarker(marker []byte) { c.log.WithEndMarker(marker) } + +func (c *FTWCheck) Close() error { + return c.log.Cleanup() +} diff --git a/check/logs_test.go b/check/logs_test.go index aa2a8999..c69b6174 100644 --- a/check/logs_test.go +++ b/check/logs_test.go @@ -4,7 +4,6 @@ package check import ( - "os" "testing" "github.com/google/uuid" @@ -50,7 +49,7 @@ func (s *checkLogsTestSuite) SetupTest() { } func (s *checkLogsTestSuite) TearDownTest() { - err := os.Remove(s.logName) + err := s.check.Close() s.Require().NoError(err) } diff --git a/cmd/check_test.go b/cmd/check_test.go index 3a6b868e..05011c9e 100644 --- a/cmd/check_test.go +++ b/cmd/check_test.go @@ -51,6 +51,8 @@ func (s *checkCmdTestSuite) SetupTest() { s.Require().NoError(err) n, err := testFileContents.WriteString(checkFileContents) s.Require().NoError(err) + err = testFileContents.Close() + s.Require().NoError(err) s.Equal(len(checkFileContents), n) s.rootCmd = NewRootCommand() diff --git a/cmd/run_test.go b/cmd/run_test.go index 2e22ffa3..a53d3918 100644 --- a/cmd/run_test.go +++ b/cmd/run_test.go @@ -84,6 +84,8 @@ func (s *runCmdTestSuite) SetupTest() { s.Require().NoError(err) err = tmpl.Execute(testFileContents, vars) s.Require().NoError(err) + err = testFileContents.Close() + s.Require().NoError(err) s.rootCmd = NewRootCommand() s.rootCmd.AddCommand(NewRunCommand()) diff --git a/config/config_test.go b/config/config_test.go index 93b360b3..8fb4226c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -256,6 +256,7 @@ func (s *baseTestSuite) TestLoadPlatformOverrides() { tempDir := s.T().TempDir() overridesFile, err := os.CreateTemp(tempDir, "overrides.yaml") s.Require().NoError(err) + defer overridesFile.Close() _, err = overridesFile.WriteString(`--- version: "v0.0.0" meta: diff --git a/runner/run.go b/runner/run.go index c2157658..30495edf 100644 --- a/runner/run.go +++ b/runner/run.go @@ -104,6 +104,7 @@ func RunTest(runContext *TestRunContext, ftwTest *test.FTWTest) error { if err != nil { return err } + defer ftwCheck.Close() if err := RunStage(runContext, ftwCheck, testCase, stage); err != nil { if err.Error() == "retry-once" { log.Info().Msgf("Retrying test once: %s", testCase.IdString()) diff --git a/runner/run_cloud_test.go b/runner/run_cloud_test.go index 892d139e..cea5a46c 100644 --- a/runner/run_cloud_test.go +++ b/runner/run_cloud_test.go @@ -72,10 +72,14 @@ func (s *runCloudTestSuite) BeforeTest(_ string, name string) { tmpl, err := template.ParseFiles(fmt.Sprintf("testdata/%s.yaml", name)) s.Require().NoError(err) // create a temporary file to hold the test - testFileContents, err := os.CreateTemp("testdata", "mock-test-*.yaml") + testdataDir, err := os.MkdirTemp(s.Suite.T().TempDir(), "testdata") + s.Require().NoError(err) + testFileContents, err := os.CreateTemp(testdataDir, "mock-test-*.yaml") s.Require().NoError(err, "cannot create temporary file") err = tmpl.Execute(testFileContents, vars) s.Require().NoError(err, "cannot execute template") + err = testFileContents.Close() + s.Require().NoError(err) // get tests from file s.ftwTests, err = test.GetTestsFromFiles(testFileContents.Name()) s.Require().NoError(err, "cannot get tests from file") diff --git a/runner/run_test.go b/runner/run_test.go index 19e4052d..b0b3879e 100644 --- a/runner/run_test.go +++ b/runner/run_test.go @@ -143,7 +143,9 @@ func (s *runTestSuite) setUpLogFileForTestServer() { } // if no file has been configured, create one and handle cleanup if s.logFilePath == "" { - file, err := os.CreateTemp("", "go-ftw-test-*.log") + file, err := os.CreateTemp(s.T().TempDir(), "go-ftw-test-*.log") + s.Require().NoError(err) + err = file.Close() s.Require().NoError(err) s.logFilePath = file.Name() } @@ -239,6 +241,8 @@ func (s *runTestSuite) BeforeTest(_ string, name string) { s.Require().NoError(err, "cannot create temporary file") err = tmpl.Execute(testFileContents, vars) s.Require().NoError(err, "cannot execute template") + err = testFileContents.Close() + s.Require().NoError(err) // get tests from file s.ftwTests, err = test.GetTestsFromFiles(testFileContents.Name()) s.Require().NoError(err, "cannot get tests from file")