From 44fb5362874cfbae3587d868b732a9410a41ccc5 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 20 Aug 2021 12:29:39 +0200 Subject: [PATCH] pkg/entrypoint: clean after running tests TestEntrypointer_OnError leaves some created file behind : `termination`. This change does 2 things: - Use a temporary file in order to not create anything in the current directory - Make sure we remove the file once the test is done This also uses a temporary file for TestEntrypointer Signed-off-by: Vincent Demeester --- pkg/entrypoint/entrypointer_test.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/pkg/entrypoint/entrypointer_test.go b/pkg/entrypoint/entrypointer_test.go index 908422f747c..048c7e572f8 100644 --- a/pkg/entrypoint/entrypointer_test.go +++ b/pkg/entrypoint/entrypointer_test.go @@ -167,6 +167,13 @@ func TestEntrypointer(t *testing.T) { t.Run(c.desc, func(t *testing.T) { fw, fr, fpw := &fakeWaiter{}, &fakeRunner{}, &fakePostWriter{} timeout := time.Duration(0) + terminationPath := "termination" + if terminationFile, err := ioutil.TempFile("", "termination"); err != nil { + t.Fatalf("unexpected error creating temporary termination file: %v", err) + } else { + terminationPath = terminationFile.Name() + defer os.Remove(terminationFile.Name()) + } err := Entrypointer{ Entrypoint: c.entrypoint, WaitFiles: c.waitFiles, @@ -175,7 +182,7 @@ func TestEntrypointer(t *testing.T) { Waiter: fw, Runner: fr, PostWriter: fpw, - TerminationPath: "termination", + TerminationPath: terminationPath, Timeout: &timeout, BreakpointOnFailure: c.breakpointOnFailure, StepMetadataDir: c.stepDir, @@ -221,7 +228,7 @@ func TestEntrypointer(t *testing.T) { if c.postFile == "" && fpw.wrote != nil { t.Errorf("Wrote post file when not required") } - fileContents, err := ioutil.ReadFile("termination") + fileContents, err := ioutil.ReadFile(terminationPath) if err == nil { var entries []v1alpha1.PipelineResourceResult if err := json.Unmarshal([]byte(fileContents), &entries); err == nil { @@ -239,7 +246,7 @@ func TestEntrypointer(t *testing.T) { } else if !os.IsNotExist(err) { t.Error("Wanted termination file written, got nil") } - if err := os.Remove("termination"); err != nil { + if err := os.Remove(terminationPath); err != nil { t.Errorf("Could not remove termination path: %s", err) } @@ -309,6 +316,13 @@ func TestEntrypointer_OnError(t *testing.T) { }} { t.Run(c.desc, func(t *testing.T) { fpw := &fakePostWriter{} + terminationPath := "termination" + if terminationFile, err := ioutil.TempFile("", "termination"); err != nil { + t.Fatalf("unexpected error creating temporary termination file: %v", err) + } else { + terminationPath = terminationFile.Name() + defer os.Remove(terminationFile.Name()) + } err := Entrypointer{ Entrypoint: "echo", WaitFiles: []string{}, @@ -317,7 +331,7 @@ func TestEntrypointer_OnError(t *testing.T) { Waiter: &fakeWaiter{}, Runner: c.runner, PostWriter: fpw, - TerminationPath: "termination", + TerminationPath: terminationPath, OnError: c.onError, }.Go()