From 194d5081300d47f83b0a790613e9a2361b219beb Mon Sep 17 00:00:00 2001 From: Philipp Sauter Date: Fri, 22 Sep 2023 16:16:25 +0200 Subject: [PATCH] tests for downloading multiple objects --- internal/acctests/sos/download_test.go | 85 ++++++++++++++++++++++++++ internal/acctests/sos/sos_test.go | 47 ++++++++------ internal/acctests/sos/suite_test.go | 7 +++ 3 files changed, 120 insertions(+), 19 deletions(-) diff --git a/internal/acctests/sos/download_test.go b/internal/acctests/sos/download_test.go index dd395605..bdff4bdf 100644 --- a/internal/acctests/sos/download_test.go +++ b/internal/acctests/sos/download_test.go @@ -1,5 +1,7 @@ package sos_test +import "fmt" + func (s *SOSSuite) TestDownloadSingleObject() { s.Execute(SOSTest{ Steps: []Step{ @@ -118,3 +120,86 @@ func (s *SOSSuite) TestDownloadSingleVersionedObject() { }, }) } + +func (s *SOSSuite) TestDownloadMultipleObjects() { + fmt.Println(s.T().Name()) + s.Execute(SOSTest{ + Steps: []Step{ + { + Description: "check that multiple files can be downloaded", + PreparedFiles: LocalFiles{ + "file1.txt": "expected content 1", + "file2.txt": "expected content 2", + }, + Commands: []string{ + "exo storage upload {prepDir}file1.txt {bucket}", + "exo storage upload {prepDir}file2.txt {bucket}", + "exo storage download -r {bucket} {downloadDir}", + }, + ExpectedDownloadFiles: LocalFiles{ + "file1.txt": "expected content 1", + "file2.txt": "expected content 2", + }, + }, + { + Description: "check that multiple files can be uploaded", + PreparedFiles: LocalFiles{ + "file1.txt": "expected content 1", + "file2.txt": "expected content 2", + }, + Commands: []string{ + "exo storage upload -r {prepDir} {bucket}", + "exo storage download -r {bucket} {downloadDir}", + }, + ClearDownloadDirBeforeCommands: true, + ExpectedDownloadFiles: LocalFiles{ + "file1.txt": "expected content 1", + "file2.txt": "expected content 2", + }, + }, + { + Description: "check that a directory can be uploaded and downloaded", + PreparedFiles: LocalFiles{ + "dir/file1.txt": "expected content 1", + "dir/file2.txt": "expected content 2", + }, + Commands: []string{ + "exo storage upload -r {prepDir} {bucket}", + "exo storage download -r {bucket} {downloadDir}", + }, + ClearDownloadDirBeforeCommands: true, + ExpectedDownloadFiles: LocalFiles{ + "file1.txt": "expected content 1", + "file2.txt": "expected content 2", + "dir/file1.txt": "expected content 1", + "dir/file2.txt": "expected content 2", + }, + }, + { + Description: "check for error if directory download doesn't end in slash", + PreparedFiles: LocalFiles{}, + Commands: []string{ + "exo storage download -r {bucket} {downloadDir}newDir", + }, + ClearDownloadDirBeforeCommands: true, + ExpectErrorInCommandNr: 1, + ExpectedDownloadFiles: LocalFiles{}, + }, + { + Description: "check that a directory can be downloaded and renamed", + PreparedFiles: LocalFiles{}, + Commands: []string{ + "exo storage upload -r {prepDir} {bucket}", + "exo storage download -r {bucket} {downloadDir}newDir/", + }, + ClearDownloadDirBeforeCommands: true, + ExpectedDownloadFiles: LocalFiles{ + "newDir/file1.txt": "expected content 1", + "newDir/file2.txt": "expected content 2", + "newDir/dir/file1.txt": "expected content 1", + "newDir/dir/file2.txt": "expected content 2", + }, + }, + }, + }) +} diff --git a/internal/acctests/sos/sos_test.go b/internal/acctests/sos/sos_test.go index f5aab104..f7d348bc 100644 --- a/internal/acctests/sos/sos_test.go +++ b/internal/acctests/sos/sos_test.go @@ -1,8 +1,11 @@ package sos_test import ( + "fmt" + "io/fs" "os" "path/filepath" + "strings" ) type LocalFiles map[string]string @@ -55,6 +58,26 @@ func emptyDirectory(dirPath string) error { return nil } +func registerFile(s *SOSSuite, files LocalFiles, prefix string) fs.WalkDirFunc { + return func(path string, info fs.DirEntry, err error) error { + if err != nil { + fmt.Println(err) + return nil + } + + if info.IsDir() { + return nil + } + + content, err := os.ReadFile(path) + s.NoError(err) + + localPath := strings.TrimPrefix(path, prefix) + files[localPath] = string(content) + return nil + } +} + func (s *SOSSuite) Execute(test SOSTest) { for stepNr, step := range test.Steps { s.T().Logf("step number: %d %q", stepNr, step.Description) @@ -87,27 +110,13 @@ func (s *SOSSuite) Execute(test SOSTest) { } } - files, err := os.ReadDir(s.DownloadDir) - if !s.NoError(err) { - return - } - - actualFileNumberMismatches := !s.Equal(len(step.ExpectedDownloadFiles), len(files), "number of actual files doesn't match number of expected files") - downloadDir := LocalFiles{} - for _, file := range files { - if actualFileNumberMismatches { - s.T().Logf("actual file: %s", file) - } - - content, err := os.ReadFile(s.DownloadDir + file.Name()) - if !s.NoError(err) { - return - } - - downloadDir[file.Name()] = string(content) - } + err := filepath.WalkDir(s.DownloadDir, registerFile(s, downloadDir, s.DownloadDir)) + s.NoError(err) + nFiles := len(downloadDir) + fmt.Printf("downloadDir: %v\n", downloadDir) + actualFileNumberMismatches := !s.Equal(len(step.ExpectedDownloadFiles), nFiles, "number of actual files doesn't match number of expected files") if actualFileNumberMismatches { return } diff --git a/internal/acctests/sos/suite_test.go b/internal/acctests/sos/suite_test.go index c8b35936..76b42e0d 100644 --- a/internal/acctests/sos/suite_test.go +++ b/internal/acctests/sos/suite_test.go @@ -145,6 +145,13 @@ func TestSOSSuite(t *testing.T) { } func (s *SOSSuite) writeFile(filename, content string) { + slashInd := strings.LastIndex(filename, "/") + if slashInd != -1 { + folders := filename[:slashInd] + err := os.MkdirAll(s.PrepDir+folders, 0744) + s.Assert().NoError(err) + } + err := os.WriteFile(s.PrepDir+filename, []byte(content), 0644) s.Assert().NoError(err) }