Skip to content

Commit

Permalink
test fixes attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-prindle committed Dec 14, 2023
1 parent 331740b commit e71bef9
Showing 1 changed file with 94 additions and 20 deletions.
114 changes: 94 additions & 20 deletions test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package test

import (
"bytes"
"context"
"crypto"
"crypto/ecdsa"
"encoding/base64"
Expand All @@ -46,6 +47,8 @@ import (
"github.com/tektoncd/chains/pkg/chains/provenance"
"github.com/tektoncd/chains/pkg/test/tekton"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
logtesting "knative.dev/pkg/logging/testing"
)
Expand Down Expand Up @@ -236,15 +239,15 @@ func TestOCISigning(t *testing.T) {

// Let's fetch the signature and body:
t.Log(obj.GetAnnotations())
if _, ok := obj.GetAnnotations()["chains.tekton.dev/signature-586789aa031f"]; !ok {
t.Fatal("TaskRun missing expected signature annotation: chains.tekton.dev/signature-586789aa031f")
if _, ok := obj.GetAnnotations()["chains.tekton.dev/signature-05f95b26ed10"]; !ok {
t.Fatal("TaskRun missing expected signature annotation: chains.tekton.dev/signature-05f95b26ed10")
}
if _, ok := obj.GetAnnotations()["chains.tekton.dev/payload-586789aa031f"]; !ok {
t.Fatal("TaskRun missing expected payload annotation: chains.tekton.dev/signature-586789aa031f")
if _, ok := obj.GetAnnotations()["chains.tekton.dev/payload-05f95b26ed10"]; !ok {
t.Fatal("TaskRun missing expected payload annotation: chains.tekton.dev/signature-05f95b26ed10")
}

sig, body := obj.GetAnnotations()["chains.tekton.dev/signature-586789aa031f"],
obj.GetAnnotations()["chains.tekton.dev/payload-586789aa031f"]
sig, body := obj.GetAnnotations()["chains.tekton.dev/signature-05f95b26ed10"],
obj.GetAnnotations()["chains.tekton.dev/payload-05f95b26ed10"]
// base64 decode them
sigBytes, err := base64.StdEncoding.DecodeString(sig)
if err != nil {
Expand Down Expand Up @@ -586,28 +589,99 @@ func TestRetryFailed(t *testing.T) {
}
}

func taskRunFromFile(f string) (*v1.TaskRun, error) {
contents, err := os.ReadFile(f)
if err != nil {
return nil, err
}
var tr v1.TaskRun
if err := json.Unmarshal(contents, &tr); err != nil {
return nil, err
}
return &tr, nil
var imageTaskSpec = v1beta1.TaskSpec{
Steps: []v1beta1.Step{
{
Image: "busybox",
Script: `set -e
cat <<EOF > $(outputs.resources.image.path)/index.json
{
"schemaVersion": 2,
"manifests": [
{
"mediaType": "application/vnd.oci.image.index.v1+json",
"size": 314,
"digest": "sha256:05f95b26ed10668b7183c1e2da98610e91372fa9f510046d4ce5812addad86b5"
}
]
}
`,
},
},
Resources: &v1beta1.TaskResources{
Outputs: []v1beta1.TaskResource{
{
ResourceDeclaration: v1alpha1.ResourceDeclaration{
Name: "image",
Type: "image",
},
},
},
},
}

var imageTaskRun = v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "image-task",
Annotations: map[string]string{chains.RekorAnnotation: "true"},
},
Spec: v1beta1.TaskRunSpec{
TaskSpec: &imageTaskSpec,
Resources: &v1beta1.TaskRunResources{
Outputs: []v1beta1.TaskResourceBinding{{
PipelineResourceBinding: v1beta1.PipelineResourceBinding{
Name: "image",
ResourceSpec: &v1alpha1.PipelineResourceSpec{
Type: "image",
Params: []v1alpha1.ResourceParam{
{
Name: "url",
Value: "gcr.io/foo/bar",
},
},
},
},
}},
},
},
}

func getTaskRunObject(ns string) objects.TektonObject {
tr, _ := taskRunFromFile("testdata/type-hinting/taskrun.json")
o := objects.NewTaskRunObjectV1(tr)
trV1 := &v1.TaskRun{}
if err := imageTaskRun.ConvertTo(context.Background(), trV1); err == nil {
if imageTaskRun.Spec.Resources != nil { //nolint:staticcheck
jsonData, err := json.Marshal(imageTaskRun.Spec.Resources) //nolint:staticcheck
if err != nil {
// TODO(aaron-prindle) do something here
// t.Errorf("Error serializing to JSON: %v", err)
}
trV1.Annotations["tekton.dev/v1beta1-spec-resources"] = string(jsonData)
}
}
// trV1.Annotations["tekton.dev/v1beta1Resources"] = "{\"outputs\":[{\"name\":\"image\",\"resourceSpec\":{\"type\":\"image\",\"params\":[{\"name\":\"url\",\"value\":\"gcr.io/foo/bar\"}]}}]}"
// trV1.Annotations["tekton.dev/v1beta1-status-taskrunstatusfields-taskspec-resources"] = "{\"outputs\":[{\"name\":\"image\",\"resourceSpec\":{\"type\":\"image\",\"params\":[{\"name\":\"url\",\"value\":\"gcr.io/foo/bar\"}]}}]}"
// trV1.Annotations["tekton.dev/v1beta1ResourcesResult"] = "[{\"key\":\"digest\",\"value\":\"sha256:05f95b26ed10668b7183c1e2da98610e91372fa9f510046d4ce5812addad86b5\",\"resourceName\":\"image\"},{\"key\":\"url\",\"value\":\"gcr.io/foo/bar\",\"resourceName\":\"image\"}]"
trV1.Annotations["tekton.dev/v1beta1Digest"] = "gcr.io/foo/bar@sha256:05f95b26ed10668b7183c1e2da98610e91372fa9f510046d4ce5812addad86b5"
o := objects.NewTaskRunObjectV1(trV1)
o.Namespace = ns
return o
}

func getTaskRunObjectWithParams(ns string, params []v1.Param) objects.TektonObject {
tr, _ := taskRunFromFile("testdata/type-hinting/taskrun.json")
o := objects.NewTaskRunObjectV1(tr)
trV1 := &v1.TaskRun{}
if err := imageTaskRun.ConvertTo(context.Background(), trV1); err == nil {
if imageTaskRun.Spec.Resources != nil { //nolint:staticcheck
jsonData, err := json.Marshal(imageTaskRun.Spec.Resources) //nolint:staticcheck
if err != nil {
// TODO(aaron-prindle) do something here
// t.Errorf("Error serializing to JSON: %v", err)
}
trV1.Annotations["tekton.dev/v1beta1-spec-resources"] = string(jsonData)
}
}
trV1.Annotations["tekton.dev/v1beta1Resources"] = "{\"outputs\":[{\"name\":\"image\",\"resourceSpec\":{\"type\":\"image\",\"params\":[{\"name\":\"url\",\"value\":\"gcr.io/foo/bar\"}]}}]}"
trV1.Annotations["tekton.dev/v1beta1ResourcesResult"] = "[{\"key\":\"digest\",\"value\":\"sha256:05f95b26ed10668b7183c1e2da98610e91372fa9f510046d4ce5812addad86b5\",\"resourceName\":\"image\"},{\"key\":\"url\",\"value\":\"gcr.io/foo/bar\",\"resourceName\":\"image\"}]"
o := objects.NewTaskRunObjectV1(trV1)
o.Namespace = ns
o.Spec.Params = params
return o
Expand Down

0 comments on commit e71bef9

Please sign in to comment.