Skip to content

Commit

Permalink
acc test for renaming downloaded versioned file
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterp committed Aug 29, 2023
1 parent 178297c commit d33578a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
1 change: 1 addition & 0 deletions cmd/storage_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Examples:
Objects: objects,
Destination: dst,
Recursive: recursive,
Versions: downloadVersions || len(versionFilters) > 0,
Overwrite: force,
DryRun: dryRun,
})
Expand Down
11 changes: 11 additions & 0 deletions internal/acctests/sos/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ func (s *SOSSuite) TestDownloadOverwrittenVersionedFile() {
"file1.txt": "new expected content",
},
},
{
Description: "check if v0 can be downloaded and renamed",
PreparedFiles: LocalFiles{},
ClearDownloadDirBeforeCommands: true,
Commands: []string{
"exo storage download -f --only-versions v0 {bucket}/file1.txt {downloadDir}/file1-v0.txt",
},
ExpectedDownloadFiles: LocalFiles{
"file1-v0.txt": "original content",
},
},
},
})
}
51 changes: 46 additions & 5 deletions internal/acctests/sos/sos_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,68 @@
package sos_test

import "os"
import (
"os"
"path/filepath"
)

type LocalFiles map[string]string

type Step struct {
Description string
PreparedFiles LocalFiles
Commands []string
ExpectedDownloadFiles LocalFiles
Description string
PreparedFiles LocalFiles
ClearDownloadDirBeforeCommands bool
Commands []string
ExpectedDownloadFiles LocalFiles
}

type SOSTest struct {
Steps []Step
}

func emptyDirectory(dirPath string) error {
// Open the directory
dir, err := os.Open(dirPath)
if err != nil {
return err
}
defer dir.Close()

// Read all directory entries
entries, err := dir.Readdir(-1)
if err != nil {
return err
}

// Loop through the entries and remove them
for _, entry := range entries {
entryPath := filepath.Join(dirPath, entry.Name())

// Remove file or directory
if entry.IsDir() {
err = os.RemoveAll(entryPath)
} else {
err = os.Remove(entryPath)
}

if err != nil {
return err
}
}

return nil
}

func (s *SOSSuite) Execute(test SOSTest) {
for stepNr, step := range test.Steps {
s.T().Logf("step number: %d %q", stepNr, step.Description)
for filename, content := range step.PreparedFiles {
s.writeFile(filename, content)
}

if step.ClearDownloadDirBeforeCommands && !s.NoError(emptyDirectory(s.DownloadDir)) {
return
}

for _, command := range step.Commands {
s.exo(command)
}
Expand Down
15 changes: 11 additions & 4 deletions pkg/storage/sos/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,31 +110,38 @@ type DownloadConfig struct {
Destination string
Objects []object.ObjectInterface
Recursive bool
Versions bool
Overwrite bool
DryRun bool
}

func (c *Client) DownloadFiles(ctx context.Context, config *DownloadConfig) error {
if len(config.Objects) > 1 && !strings.HasSuffix(config.Destination, "/") {
var (
downloadToDir = strings.HasSuffix(config.Destination, "/")
)
if len(config.Objects) > 1 && !downloadToDir {
return errors.New(`multiple files to download, destination must end with "/"`)
}

// Handle relative filesystem destination (e.g. ".", "../.." etc.)
if dstInfo, err := os.Stat(config.Destination); err == nil {
if dstInfo.IsDir() && !strings.HasSuffix(config.Destination, "/") {
if dstInfo.IsDir() && !downloadToDir {
config.Destination += "/"
downloadToDir = true
}
}

if config.DryRun {
fmt.Println("[DRY-RUN]")
}

downloadingSingleObject := len(config.Objects) == 1

for _, obj := range config.Objects {
dst := func() string {
versionIDSuffix := ""
if objectVersion, ok := obj.(object.ObjectVersionInterface); ok {
if !objectVersion.GetIsLatest() {
if !objectVersion.GetIsLatest() && !downloadingSingleObject {
versionIDSuffix = "-" + *objectVersion.GetVersionId()
}
}
Expand All @@ -143,7 +150,7 @@ func (c *Client) DownloadFiles(ctx context.Context, config *DownloadConfig) erro
return path.Join(config.Destination, strings.TrimPrefix(aws.ToString(obj.GetKey()), config.Prefix)+versionIDSuffix)
}

if strings.HasSuffix(config.Destination, "/") {
if downloadToDir {
return path.Join(config.Destination, path.Base(aws.ToString(obj.GetKey()))+versionIDSuffix)
}

Expand Down

0 comments on commit d33578a

Please sign in to comment.