Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update file close operation to not use defer and add test case for CopyFromPath #538

Merged
merged 2 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions controllers/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,10 @@ func (s *Storage) CopyFromPath(artifact *sourcev1.Artifact, path string) (err er
if err != nil {
return err
}
defer f.Close()
return s.Copy(artifact, f)
if err = s.Copy(artifact, f); err != nil {
return err
}
return f.Close()
tomhuang12 marked this conversation as resolved.
Show resolved Hide resolved
}

// CopyToPath copies the contents in the (sub)path of the given artifact to the given path.
Expand Down
105 changes: 105 additions & 0 deletions controllers/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,108 @@ func TestStorageRemoveAllButCurrent(t *testing.T) {
}
})
}

func TestStorageCopyFromPath(t *testing.T) {
type File struct {
Name string
Content []byte
}

dir, err := createStoragePath()
if err != nil {
t.Fatal(err)
}
t.Cleanup(cleanupStoragePath(dir))

storage, err := NewStorage(dir, "hostname", time.Minute)
if err != nil {
t.Fatalf("error while bootstrapping storage: %v", err)
}

createFile := func(file *File) (absPath string, err error) {
defer func() {
if err != nil && dir != "" {
os.RemoveAll(dir)
}
}()
dir, err = os.MkdirTemp("", "test-files-")
if err != nil {
return
}
absPath = filepath.Join(dir, file.Name)
if err = os.MkdirAll(filepath.Dir(absPath), 0755); err != nil {
return
}
f, err := os.Create(absPath)
if err != nil {
return "", fmt.Errorf("could not create file %q: %w", absPath, err)
}
if n, err := f.Write(file.Content); err != nil {
f.Close()
return "", fmt.Errorf("could not write %d bytes to file %q: %w", n, f.Name(), err)
}
f.Close()
return
}

matchFile := func(t *testing.T, storage *Storage, artifact sourcev1.Artifact, file *File, wantErr bool) {
c, err := os.ReadFile(storage.LocalPath(artifact))
if err != nil {
t.Fatalf("failed reading file: %v", err)
}
if (string(c) != string(file.Content)) != wantErr {
tomhuang12 marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("artifact content does not match, got: %q, want: %q", string(c), string(file.Content))
}
}

tests := []struct {
name string
file *File
want *File
wantErr bool
}{
{
name: "content match",
file: &File{
Name: "manifest.yaml",
Content: []byte(`contents`),
},
want: &File{
Name: "manifest.yaml",
Content: []byte(`contents`),
},
},
{
name: "content not match",
file: &File{
Name: "manifest.yaml",
Content: []byte(`contents`),
},
want: &File{
Name: "manifest.yaml",
Content: []byte(`bad contents`),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
absPath, err := createFile(tt.file)
if err != nil {
t.Error(err)
return
}
defer os.RemoveAll(absPath)
artifact := sourcev1.Artifact{
Path: filepath.Join(randStringRunes(10), randStringRunes(10), randStringRunes(10)),
}
if err := storage.MkdirAll(artifact); err != nil {
t.Fatalf("artifact directory creation failed: %v", err)
}
if err := storage.CopyFromPath(&artifact, absPath); err != nil {
t.Errorf("CopyFromPath() error = %v", err)
}
matchFile(t, storage, artifact, tt.want, tt.wantErr)
})
}
}