diff --git a/lib/utils/docker.go b/lib/utils/docker.go index 29f540f2..42eac8f9 100644 --- a/lib/utils/docker.go +++ b/lib/utils/docker.go @@ -35,11 +35,6 @@ func dockerLogin(username string, password string) { log.Printf("Error during login: %s\n", err) return } - if err != nil { - log.Fatal(err, " :unable to read push response") - return - } - log.Println("Logged into Harbor successfully") } diff --git a/lib/utils/os_test.go b/lib/utils/os_test.go index c04e8f7a..8de4b2f6 100644 --- a/lib/utils/os_test.go +++ b/lib/utils/os_test.go @@ -1,8 +1,17 @@ package utils import ( + "archive/tar" + "bytes" + "compress/gzip" + "io" + "os" "os/exec" + "path/filepath" + "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestRunCommand(t *testing.T) { @@ -25,3 +34,40 @@ func TestRunCommand(t *testing.T) { t.Errorf("Test case failed: expected '%s', got '%s'", expectedOutput, string(output)) } } + +func TestTar(t *testing.T) { + tmpDir := t.TempDir() + sampleFileName := "sample_file.txt" + fileContents := []byte("This is a sample file to test tar function.") + err := os.WriteFile(tmpDir+string(filepath.Separator)+sampleFileName, fileContents, 0644) + assert.NoError(t, err) + + buf := new(bytes.Buffer) + + err = Tar(tmpDir, buf) + assert.NoError(t, err) + + gr, err := gzip.NewReader(buf) + assert.NoError(t, err) + defer gr.Close() + + tr := tar.NewReader(gr) + + for { + header, err := tr.Next() + + if err == io.EOF { + break + } + + assert.NoError(t, err) + + content, err := io.ReadAll(tr) + assert.NoError(t, err) + + assert.Equal(t, fileContents, content) + + fileName := strings.TrimPrefix(header.Name, "./"+tmpDir+string(filepath.Separator)) + assert.Equal(t, sampleFileName, fileName) + } +}