Skip to content

Commit

Permalink
test: added git-target tests
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Kotzbauer <[email protected]>
  • Loading branch information
ckotzbauer committed May 8, 2022
1 parent 77f060d commit 240360b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
10 changes: 3 additions & 7 deletions internal/target/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,15 @@ func (g *GitAccount) CommitAll(path, message string) error {
return g.commitAndPush(w, r, message)
}

func (g *GitAccount) Remove(workTree, path string) {
func (g *GitAccount) Remove(workTree, path string) error {
r, w := g.openExistingRepo(workTree)

if r == nil && w == nil {
return
return nil
}

_, err := w.Remove(path)

if err != nil {
logrus.WithError(err).Error("Remove failed")
return
}
return err
}

func (g *GitAccount) CommitAndPush(path, message string) error {
Expand Down
8 changes: 4 additions & 4 deletions internal/target/git_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (g *GitTarget) Initialize() {

func (g *GitTarget) ProcessSbom(image kubernetes.ContainerImage, sbom string) error {
imageID := image.ImageID
filePath := g.imageIDToFilePath(imageID)
filePath := g.ImageIDToFilePath(imageID)

dir := filepath.Dir(filePath)
err := os.MkdirAll(dir, 0777)
Expand Down Expand Up @@ -105,13 +105,13 @@ func (g *GitTarget) Cleanup(allImages []kubernetes.ContainerImage) {
func (g *GitTarget) mapToFiles(allImages []kubernetes.ContainerImage) []string {
paths := []string{}
for _, img := range allImages {
paths = append(paths, g.imageIDToFilePath(img.ImageID))
paths = append(paths, g.ImageIDToFilePath(img.ImageID))
}

return paths
}

func (g *GitTarget) imageIDToFilePath(id string) string {
func (g *GitTarget) ImageIDToFilePath(id string) string {
fileName := syft.GetFileName(g.sbomFormat)
filePath := strings.ReplaceAll(id, "@", "/")
return strings.ReplaceAll(path.Join(g.workingTree, g.workPath, filePath, fileName), ":", "_")
Expand Down Expand Up @@ -145,7 +145,7 @@ func (g *GitTarget) deleteObsoleteFiles(fileName string, ignoreDirs, allProcesse
if !found {
rel, _ := filepath.Rel(g.workingTree, p)
dir := filepath.Dir(rel)
g.gitAccount.Remove(g.workingTree, dir)
err = g.gitAccount.Remove(g.workingTree, dir)
if err != nil {
logrus.WithError(err).Errorf("File could not be deleted %s", p)
} else {
Expand Down
55 changes: 55 additions & 0 deletions internal/target/git_target_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package target

import (
"testing"

"github.com/stretchr/testify/assert"
)

type testData struct {
input string
expected string
g *GitTarget
}

func TestImageIDToFilePath(t *testing.T) {
tests := []testData{
{
input: "alpine:latest",
expected: "alpine_latest/sbom.json",
g: NewGitTarget("", "", "", "", "", "", "", ""),
},
{
input: "alpine@sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300",
expected: "alpine/sha256_21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300/sbom.json",
g: NewGitTarget("", "", "", "", "", "", "", ""),
},
{
input: "",
expected: "sbom.json",
g: NewGitTarget("", "", "", "", "", "", "", ""),
},
{
input: "alpine:latest",
expected: "/git/dev/alpine_latest/sbom.spdx",
g: NewGitTarget("/git", "dev", "", "", "", "", "", "spdx"),
},
{
input: "alpine@sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300",
expected: "/git/sbom/prod/cluster1/alpine/sha256_21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300/sbom.spdx",
g: NewGitTarget("/git/sbom", "prod/cluster1", "", "", "", "", "", "spdx"),
},
{
input: "",
expected: "/git/sbom.json",
g: NewGitTarget("/git", "", "", "", "", "", "", ""),
},
}

for _, v := range tests {
t.Run("", func(t *testing.T) {
out := v.g.ImageIDToFilePath(v.input)
assert.Equal(t, v.expected, out)
})
}
}

0 comments on commit 240360b

Please sign in to comment.