Skip to content

Commit

Permalink
testing tar function
Browse files Browse the repository at this point in the history
  • Loading branch information
rizul2108 committed Aug 25, 2023
1 parent 29838f9 commit 56f86f0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
5 changes: 0 additions & 5 deletions lib/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
46 changes: 46 additions & 0 deletions lib/utils/os_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
}
}

0 comments on commit 56f86f0

Please sign in to comment.