Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzzzzzzz committed Apr 22, 2024
1 parent 16f854d commit d39a81f
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 11 deletions.
4 changes: 2 additions & 2 deletions examples/v1/taskruns/alpha/produce-consume-artifacts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
"name":"input-artifacts",
"values":[
{
"uri":"git:jjjsss",
"uri":"pkg:example.github.com/inputs",
"digest":{
"sha256":"b35cacccfdb1e24dc497d15d553891345fd155713ffe647c281c583269eaaae0"
}
Expand All @@ -30,7 +30,7 @@ spec:
"name":"image",
"values":[
{
"uri":"pkg:balba",
"uri":"pkg:github/package-url/purl-spec@244fd47e07d1004f0aed9c",
"digest":{
"sha256":"df85b9e3983fe2ce20ef76ad675ecf435cc99fc9350adc54fa230bae8c32ce48",
"sha1":"95588b8f34c31eb7d62c92aaa4e6506639b06ef2"
Expand Down
7 changes: 5 additions & 2 deletions internal/sidecarlogartifacts/sidecarlogartifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -120,15 +123,15 @@ 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 {
continue
}
subRes, err := extractArtifactsFromFile(p)
if err != nil {
return artifacts, err
return SidecarArtifacts{}, err
}
artifacts[name] = v1.Artifacts{Inputs: subRes.Inputs, Outputs: subRes.Outputs}
}
Expand Down
132 changes: 125 additions & 7 deletions internal/sidecarlogartifacts/sidecarlogartifacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
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
}

0 comments on commit d39a81f

Please sign in to comment.