Skip to content

Commit

Permalink
creates tests for new binary file downloader and license file downlaoder
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbrgs committed Oct 25, 2023
1 parent acd9e32 commit 64bdfd6
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func extractArchive(dst string, at io.ReaderAt, size int64) error {
klog.V(4).Infof("detected %q file type", t)
exf, ok := defaultExtractors[t]
if !ok {
return errors.Errorf("mime type %q for file is not a supported format", t)
return errors.Errorf("mime type %q for archive file is not a supported archive format", t)
}
return errors.Wrap(exf(dst, at, size), "failed to extract file")
}
Expand Down
52 changes: 52 additions & 0 deletions internal/download/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,55 @@ func zipArchiveReaderForTesting(files map[string]string) (*bytes.Reader, error)
}
return bytes.NewReader(archiveBuffer.Bytes()), nil
}

func Test_downloadBinary(t *testing.T) {
type args struct {
targetDir string
read string
size string
}
tests := []struct {
name string
args args
wantErr bool
} {
{
name: "test fail read of binary",
args: args{
targetDir: filepath.Join(testdataPath(), "null-file"),
read: "",
size: "",
},
wantErr: true,
},
{
name: "test fail write of binary",
args: args{
targetDir: filepath.Join(testdataPath(), "test/foo"),
read: "",
size: "",
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fd, err := os.Open(tt.args.targetDir)
if err != nil {
t.Errorf("failed to read file %s, err: %v", tt.args.targetDir, err)
return
}

st, err := fd.Stat()
if err != nil {
t.Errorf("failed to stat file %s, err: %v", tt.args.targetDir, err)
return
}

if err := downloadBinary(tt.args.targetDir, fd, st.Size()); (err != nil) != tt.wantErr {
t.Errorf("downloadBinary() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
1 change: 1 addition & 0 deletions internal/download/testdata/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test LICENSE file
Binary file added internal/download/testdata/binary
Binary file not shown.
70 changes: 70 additions & 0 deletions internal/installation/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,73 @@ func TestCleanupStaleKrewInstallations(t *testing.T) {
t.Fatal(diff)
}
}

func Test_downloadLicenseFile(t *testing.T) {
tmpDir := testutil.NewTempDir(t)

// start a local http server to serve the test archive from pkg/download/testdata
testdataDir := filepath.Join(testdataPath(t), "..", "..", "download", "testdata")
server := httptest.NewServer(http.FileServer(http.Dir(testdataDir)))
defer server.Close()

url := server.URL + "/LICENSE"

tests := []struct {
name string
extractDir string
uri string
wantErr bool
}{
{
name: "with valid uri",
extractDir: tmpDir.Root(),
uri: url,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := downloadLicenseFile(tt.extractDir, tt.uri); err != nil {
t.Error(err)
}
})
}
}

func Test_renameBinary(t *testing.T) {
tmpDir := testutil.NewTempDir(t)
testFile := filepath.Join(testdataPath(t), "..", "..", "download", "testdata")
tests := []struct {
name string
extractDir string
plugin string
wantErr bool
}{
{
name: "with valid plugin name",
extractDir: testFile,
plugin: "test",
wantErr: false,
},
{
name: "without valid plugin name",
extractDir: tmpDir.Root(),
plugin: "not valid",
wantErr: true,
},
{
name: "without binary",
extractDir: tmpDir.Root(),
plugin: "valid",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := renameBinary(tt.extractDir, tt.plugin); (err != nil) != tt.wantErr {
t.Error(err)
}
})
}
}

0 comments on commit 64bdfd6

Please sign in to comment.