Skip to content

Commit

Permalink
tests for downloading multiple objects
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterp committed Sep 22, 2023
1 parent 5749a78 commit 194d508
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 19 deletions.
85 changes: 85 additions & 0 deletions internal/acctests/sos/download_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sos_test

import "fmt"

func (s *SOSSuite) TestDownloadSingleObject() {
s.Execute(SOSTest{
Steps: []Step{
Expand Down Expand Up @@ -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",
},
},
},
})
}
47 changes: 28 additions & 19 deletions internal/acctests/sos/sos_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package sos_test

import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)

type LocalFiles map[string]string
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions internal/acctests/sos/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 194d508

Please sign in to comment.