diff --git a/examples/v1/taskruns/alpha/produce-consume-artifacts.yaml b/examples/v1/taskruns/alpha/produce-consume-artifacts.yaml index e270b1b85ff..0079fe930cf 100644 --- a/examples/v1/taskruns/alpha/produce-consume-artifacts.yaml +++ b/examples/v1/taskruns/alpha/produce-consume-artifacts.yaml @@ -17,7 +17,7 @@ spec: "name":"input-artifacts", "values":[ { - "uri":"git:jjjsss", + "uri":"pkg:example.github.com/inputs", "digest":{ "sha256":"b35cacccfdb1e24dc497d15d553891345fd155713ffe647c281c583269eaaae0" } @@ -30,7 +30,7 @@ spec: "name":"image", "values":[ { - "uri":"pkg:balba", + "uri":"pkg:github/package-url/purl-spec@244fd47e07d1004f0aed9c", "digest":{ "sha256":"df85b9e3983fe2ce20ef76ad675ecf435cc99fc9350adc54fa230bae8c32ce48", "sha1":"95588b8f34c31eb7d62c92aaa4e6506639b06ef2" diff --git a/internal/sidecarlogartifacts/sidecarlogartifacts.go b/internal/sidecarlogartifacts/sidecarlogartifacts.go index 65825c8d21f..d1c3d76e7a7 100644 --- a/internal/sidecarlogartifacts/sidecarlogartifacts.go +++ b/internal/sidecarlogartifacts/sidecarlogartifacts.go @@ -28,6 +28,9 @@ import ( "k8s.io/client-go/kubernetes" ) +// for testing +var stepDir = pipeline.StepsDir + func fileExists(filename string) (bool, error) { info, err := os.Stat(filename) if os.IsNotExist(err) { @@ -120,7 +123,7 @@ func LookForArtifacts(names []string, runDir string) (SidecarArtifacts, error) { } artifacts := SidecarArtifacts{} for _, name := range names { - p := filepath.Join(pipeline.StepsDir, name, "artifacts", "provenance.json") + p := filepath.Join(stepDir, name, "artifacts", "provenance.json") if exist, err := fileExists(p); err != nil { return artifacts, err } else if !exist { @@ -128,7 +131,7 @@ func LookForArtifacts(names []string, runDir string) (SidecarArtifacts, error) { } subRes, err := extractArtifactsFromFile(p) if err != nil { - return artifacts, err + return SidecarArtifacts{}, err } artifacts[name] = v1.Artifacts{Inputs: subRes.Inputs, Outputs: subRes.Outputs} } diff --git a/internal/sidecarlogartifacts/sidecarlogartifacts_test.go b/internal/sidecarlogartifacts/sidecarlogartifacts_test.go index 9cff4e249dc..91be7b46e78 100644 --- a/internal/sidecarlogartifacts/sidecarlogartifacts_test.go +++ b/internal/sidecarlogartifacts/sidecarlogartifacts_test.go @@ -17,13 +17,17 @@ limitations under the License. package sidecarlogartifacts import ( + "encoding/json" + "fmt" + "github.com/google/go-cmp/cmp" + v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" + "github.com/tektoncd/pipeline/test/diff" "os" "path/filepath" "testing" ) func TestLookForArtifacts_WaitForFiles(t *testing.T) { - tests := []struct { desc string wantErr bool @@ -72,14 +76,128 @@ func TestLookForArtifacts_WaitForFiles(t *testing.T) { } func TestLookForArtifacts(t *testing.T) { - + base := basicArtifacts() + var modified = base.DeepCopy() + modified.Outputs[0].Name = "tests" + type Arg struct { + stepName string + artifacts *v1.Artifacts + customContent []byte + } tests := []struct { - desc string - wantErr bool - runDirMode os.FileMode - }{{}} + desc string + wantErr bool + args []Arg + expected SidecarArtifacts + }{ + { + desc: "one step produces artifacts, read success", + args: []Arg{{stepName: "first", artifacts: &base}}, + expected: map[string]v1.Artifacts{"first": base}, + }, { + desc: "two step produce artifacts, read success", + args: []Arg{{stepName: "first", artifacts: &base}, {stepName: "second", artifacts: modified}}, + expected: map[string]v1.Artifacts{"first": base, "second": *modified}, + }, + { + desc: "one step produces artifacts, one step does not, read success", + args: []Arg{{stepName: "first", artifacts: &base}, {stepName: "second"}}, + expected: map[string]v1.Artifacts{"first": base}, + }, + { + desc: "two step produces, one read success, one not, error out and result is empty.", + args: []Arg{{stepName: "first", artifacts: &base}, {stepName: "second", artifacts: modified, customContent: []byte("this is to break json")}}, + expected: map[string]v1.Artifacts{}, + wantErr: true, + }, + } for _, tc := range tests { + t.Run(tc.desc, func(t *testing.T) { + dir := t.TempDir() + curStepDir := stepDir + stepDir = dir + t.Cleanup(func() { + stepDir = curStepDir + }) + var names []string + for _, arg := range tc.args { + names = append(names, arg.stepName) + if err := os.MkdirAll(filepath.Join(dir, arg.stepName, "artifacts"), os.ModePerm); err != nil { + t.Errorf("failed to create artifacts folder, err: %v", err) + } + if _, err := os.Create(filepath.Join(dir, arg.stepName, "out")); err != nil { + t.Errorf("failed to file, err: %v", err) + } + if arg.artifacts != nil { + if err := writeArtifacts(filepath.Join(dir, arg.stepName, "artifacts", "provenance.json"), arg.artifacts); err != nil { + t.Errorf("failed to write artifacts to provenance.json, err: %v", err) + } + } + if arg.customContent != nil { + if err := os.WriteFile(filepath.Join(dir, arg.stepName, "artifacts", "provenance.json"), arg.customContent, os.ModePerm); err != nil { + t.Errorf("failed to write customContent to provenance.json, err: %v", err) + } + } + } + got, err := LookForArtifacts(names, dir) + fmt.Println(err) + if (err != nil) != tc.wantErr { + t.Errorf("error checking failed, wantErr: %v, got: %v", tc.wantErr, err) + } + if d := cmp.Diff(tc.expected, got); d != "" { + t.Errorf(diff.PrintWantGot(d)) + } + }) + } +} + +func writeArtifacts(path string, artifacts *v1.Artifacts) error { + f, err := os.Create(path) + if err != nil { + return err } -} \ No newline at end of file + defer f.Close() + res := json.NewEncoder(f).Encode(artifacts) + return res +} + +func basicArtifacts() v1.Artifacts { + data := `{ + "inputs":[ + { + "name":"inputs", + "values":[ + { + "uri":"pkg:example.github.com/inputs", + "digest":{ + "sha256":"b35cacccfdb1e24dc497d15d553891345fd155713ffe647c281c583269eaaae0" + } + } + ] + } + ], + "outputs":[ + { + "name":"image", + "values":[ + { + "uri":"pkg:github/package-url/purl-spec@244fd47e07d1004f0aed9c", + "digest":{ + "sha256":"df85b9e3983fe2ce20ef76ad675ecf435cc99fc9350adc54fa230bae8c32ce48", + "sha1":"95588b8f34c31eb7d62c92aaa4e6506639b06ef2" + } + } + ] + } + ] + } +` + var ars v1.Artifacts + err := json.Unmarshal([]byte(data), &ars) + if err != nil { + panic(err) + } + return ars +}