Skip to content

Commit

Permalink
fix(test): load to stream tmp file counting (#13366)
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Clucas <[email protected]>
  • Loading branch information
Joibel committed Sep 20, 2024
1 parent 1892cea commit 50432f2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion workflow/artifacts/common/load_to_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
21 changes: 19 additions & 2 deletions workflow/artifacts/common/load_to_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -53,6 +54,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
Expand All @@ -78,7 +95,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)
}
Expand All @@ -90,7 +107,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)
}
Expand Down

0 comments on commit 50432f2

Please sign in to comment.