From 7f545acc78462b0c6513f18ecae268cb8ac09a97 Mon Sep 17 00:00:00 2001 From: Alan Clucas Date: Fri, 19 Jul 2024 10:12:30 +0100 Subject: [PATCH] fix: load to steam test The current test counts files in /tmp and hopes there are the same number before and after the test, which is fragile Add a prefix to the stream temporary files. Only count files in /tmp which have this prefix. Signed-off-by: Alan Clucas --- workflow/artifacts/common/load_to_stream.go | 4 +++- .../artifacts/common/load_to_stream_test.go | 21 +++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/workflow/artifacts/common/load_to_stream.go b/workflow/artifacts/common/load_to_stream.go index 18a165b33d11..0e8b4fbf3fb8 100644 --- a/workflow/artifacts/common/load_to_stream.go +++ b/workflow/artifacts/common/load_to_stream.go @@ -12,6 +12,8 @@ import ( wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" ) +const loadToStreamPrefix = `wfstream-` + // wrapper around os.File enables us to remove the file when it gets closed type selfDestructingFile struct { os.File @@ -28,7 +30,7 @@ func (w selfDestructingFile) Close() error { func LoadToStream(a *wfv1.Artifact, g ArtifactDriver) (io.ReadCloser, error) { log.Infof("Efficient artifact streaming is not supported for type %v: see https://github.com/argoproj/argo-workflows/issues/8489", reflect.TypeOf(g)) - filename := "/tmp/" + rand.String(32) + filename := "/tmp/" + loadToStreamPrefix + rand.String(32) if err := g.Load(a, filename); err != nil { return nil, err } diff --git a/workflow/artifacts/common/load_to_stream_test.go b/workflow/artifacts/common/load_to_stream_test.go index c05dbc9e06e5..a36b8e87d1c8 100644 --- a/workflow/artifacts/common/load_to_stream_test.go +++ b/workflow/artifacts/common/load_to_stream_test.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "os" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -55,6 +56,22 @@ func (a *fakeArtifactDriver) ListObjects(artifact *wfv1.Artifact) ([]string, err return nil, fmt.Errorf("not implemented") } +func filteredFiles(t *testing.T) ([]os.DirEntry, error) { + t.Helper() + + filtered := make([]os.DirEntry, 0) + entries, err := os.ReadDir("/tmp/") + if err != nil { + return filtered, err + } + for _, entry := range entries { + if strings.HasPrefix(entry.Name(), loadToStreamPrefix) { + filtered = append(filtered, entry) + } + } + return filtered, err +} + func TestLoadToStream(t *testing.T) { tests := map[string]struct { artifactDriver ArtifactDriver @@ -80,7 +97,7 @@ func TestLoadToStream(t *testing.T) { t.Run(name, func(t *testing.T) { // need to verify that a new file doesn't get written so check the /tmp directory ahead of time - filesBefore, err := os.ReadDir("/tmp/") + filesBefore, err := filteredFiles(t) if err != nil { panic(err) } @@ -92,7 +109,7 @@ func TestLoadToStream(t *testing.T) { stream.Close() // make sure the new file got deleted when we called stream.Close() above - filesAfter, err := os.ReadDir("/tmp/") + filesAfter, err := filteredFiles(t) if err != nil { panic(err) }