diff --git a/internal/target/git/git.go b/internal/target/git/git.go index f7e45cd8..3ee07c9b 100644 --- a/internal/target/git/git.go +++ b/internal/target/git/git.go @@ -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 { diff --git a/internal/target/git_target.go b/internal/target/git_target.go index efba015e..ecf9d525 100644 --- a/internal/target/git_target.go +++ b/internal/target/git_target.go @@ -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) @@ -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), ":", "_") @@ -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 { diff --git a/internal/target/git_target_test.go b/internal/target/git_target_test.go new file mode 100644 index 00000000..b242e9e9 --- /dev/null +++ b/internal/target/git_target_test.go @@ -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) + }) + } +}