diff --git a/.github/workflows/latest.yaml b/.github/workflows/latest.yaml index 8bc7c7441..84bd7a5bb 100644 --- a/.github/workflows/latest.yaml +++ b/.github/workflows/latest.yaml @@ -57,12 +57,12 @@ jobs: with: go-version: "1.22" - - name: Setup tmate session - uses: mxschmitt/action-tmate@v3 - if: ${{ github.event_name == 'workflow_dispatch' && inputs.debug_enabled }} - with: - limit-access-to-actor: true - detached: true + # - name: Setup tmate session + # uses: mxschmitt/action-tmate@v3 + # if: ${{ github.event_name == 'workflow_dispatch' && inputs.debug_enabled }} + # with: + # limit-access-to-actor: true + # detached: true - name: Install kind run: | @@ -101,6 +101,13 @@ jobs: tkn taskrun list kubectl get taskrun -o yaml + - name: Setup tmate session + uses: mxschmitt/action-tmate@v3 + if: ${{ github.event_name == 'workflow_dispatch' && inputs.debug_enabled }} + with: + limit-access-to-actor: true + detached: true + publish: name: publish latest runs-on: ubuntu-latest diff --git a/tests/oci_test.go b/tests/oci_test.go index 1f234d55d..10419c5c8 100644 --- a/tests/oci_test.go +++ b/tests/oci_test.go @@ -12,9 +12,12 @@ import ( "github.com/openshift-pipelines/tekton-caches/internal/fetch" "github.com/openshift-pipelines/tekton-caches/internal/hash" "github.com/openshift-pipelines/tekton-caches/internal/upload" + tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" "gotest.tools/v3/assert" "gotest.tools/v3/env" "gotest.tools/v3/fs" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" ) const ( @@ -41,3 +44,68 @@ func TestOCIUpload(t *testing.T) { assert.NilError(t, fetch.Fetch(ctx, hash, regTarget, tmpdir.Path(), false)) assert.NilError(t, fetch.Fetch(ctx, "unknown", regTarget, tmpdir.Path(), false)) // should not error on unknown hash } + +func TestCacheOCI(t *testing.T) { + ctx := context.Background() + p := new(tektonv1.Pipeline) + pr := new(tektonv1.PipelineRun) + + // Get the pipeline yaml + pipeline, err := os.ReadFile("test-pipelineruns/test-pipeline-oci.yaml") + if err != nil { + t.Fatalf("Error reading file: %v", err) + } + if err := yaml.UnmarshalStrict(pipeline, p); err != nil { + t.Fatalf("Error unmarshalling: %v", err) + } + + // Get the pipelineRun yaml + pipelineRun, err := os.ReadFile("test-pipelineruns/test-pipelinerun-oci.yaml") + if err != nil { + t.Fatalf("Error reading file: %v", err) + } + if err := yaml.UnmarshalStrict(pipelineRun, pr); err != nil { + t.Fatalf("Error unmarshalling: %v", err) + } + + tc := tektonClient(t) + + // Install the pipeline example + if _, err := tc.Pipelines(defaultNamespace).Create(ctx, p, metav1.CreateOptions{}); err != nil { + t.Fatalf("Error creating Pipeline: %v", err) + } + + _ = tc.PipelineRuns(defaultNamespace).Delete(ctx, pr.GetName(), metav1.DeleteOptions{ + PropagationPolicy: &deletePolicy, + }) + + // Install the pipelineRun example + if _, err = tc.PipelineRuns(defaultNamespace).Create(ctx, pr, metav1.CreateOptions{}); err != nil { + t.Fatalf("Error creating PipelineRun: %v", err) + } + + if err := waitForPipelineRun(ctx, tc, pr, Succeed(pr.Name), waitInterval); err != nil { + t.Fatalf("Error waiting for PipelineRun to complete: %v", err) + } + + // Get the taskrun + tr, err := tc.TaskRuns(defaultNamespace).Get(ctx, "pipelinerun-oci-test-build-task", metav1.GetOptions{}) + if err != nil { + t.Fatalf("Error creating PipelineRun: %v", err) + } + + // Assert the result which was produced by stepAction and stored as result in taskrun + assert.Equal(t, tr.Status.Results[0].Value.StringVal, "true") + + // Delete the pipelinerun + err = tc.PipelineRuns(defaultNamespace).Delete(ctx, pr.GetName(), metav1.DeleteOptions{}) + if err != nil { + t.Fatalf("Error deleting pipelinerun") + } + + // Delete the pipeline + err = tc.Pipelines(defaultNamespace).Delete(ctx, p.GetName(), metav1.DeleteOptions{}) + if err != nil { + t.Fatalf("Error deleting pipelinerun") + } +} diff --git a/tests/test-pipelineruns/test-pipeline-oci.yaml b/tests/test-pipelineruns/test-pipeline-oci.yaml new file mode 100644 index 000000000..95f7270bf --- /dev/null +++ b/tests/test-pipelineruns/test-pipeline-oci.yaml @@ -0,0 +1,151 @@ +--- +apiVersion: tekton.dev/v1 +kind: Pipeline +metadata: + name: pipeline +spec: + params: + - name: repo_url + type: string + - name: revision + type: string + - name: registry + type: string + - name: buildCommand + type: string + default: go build -v . + - name: cachePatterns + type: array + default: [ "**go.mod", "**go.sum" ] + - name: image + type: string + default: golang:latest + - name: force-cache-upload + type: string + default: "false" + workspaces: + - name: source + - name: cred + tasks: + - displayName: Build go application + name: build-task + workspaces: + - name: source + workspace: source + taskSpec: + results: + - name: test-fetched + workspaces: + - name: source + - name: cred + params: + - name: buildCommand + default: $(params.buildCommand) + - name: cachePatterns + default: $(params.cachePatterns) + - name: image + default: $(params.image) + steps: + - name: create-repo + image: $(params.image) + script: | + mkdir -p $(workspaces.source.path)/repo + chmod 777 $(workspaces.source.path)/repo + - name: fetch-repo + ref: + resolver: http + params: + - name: url + value: https://raw.githubusercontent.com/tektoncd/catalog/main/stepaction/git-clone/0.1/git-clone.yaml + params: + - name: output-path + value: $(workspaces.source.path)/repo + - name: url + value: $(params.repo_url) + - name: revision + value: $(params.revision) + - name: cache-fetch + ref: + name: cache-fetch + params: + - name: patterns + value: $(params.cachePatterns) + - name: source + value: $(params.registry)/cache-go:{{hash}} + - name: cachePath + value: $(workspaces.source.path)/cache + - name: workingdir + value: $(workspaces.source.path)/repo + - name: awsCredentialFile + value: $(workspaces.cred.path)/credentials + - name: awsConfigFile + value: $(workspaces.cred.path)/config + - name: googleCredentialsPath + value: $(workspaces.cred.path)/creds.json + + - name: run-go-build + workingDir: $(workspaces.source.path)/repo + image: $(params.image) + env: + - name: GOCACHE + value: $(workspaces.source.path)/cache/gocache + - name: GOMODCACHE + value: $(workspaces.source.path)/cache/gomodcache + script: | + set -x + git config --global --add safe.directory $(workspaces.source.path)/repo + $(params.buildCommand) + echo "Cache size is $(du -sh $(workspaces.source.path)/cache)" + - name: cache-upload + ref: + name: cache-upload + params: + - name: patterns + value: $(params.cachePatterns) + - name: target + value: $(params.registry)/cache-go:{{hash}} + - name: cachePath + value: $(workspaces.source.path)/cache + - name: workingdir + value: $(workspaces.source.path)/repo + - name: dockerConfig + value: $(workspaces.cred.path) + - name: awsCredentialFile + value: $(workspaces.cred.path)/credentials + - name: awsConfigFile + value: $(workspaces.cred.path)/config + - name: force-cache-upload + value: $(params.force-cache-upload) + - name: googleCredentialsPath + value: $(workspaces.cred.path)/creds.json + + - name: cache-fetch-2 + ref: + name: cache-fetch + params: + - name: patterns + value: $(params.cachePatterns) + - name: source + value: $(params.registry)/cache-go:{{hash}} + - name: cachePath + value: $(workspaces.source.path)/cache + - name: workingdir + value: $(workspaces.source.path)/repo + - name: awsCredentialFile + value: $(workspaces.cred.path)/credentials + - name: awsConfigFile + value: $(workspaces.cred.path)/config + - name: googleCredentialsPath + value: $(workspaces.cred.path)/creds.json + + - name: verify + image: bash:latest + env: + - name: test + value: $(steps.cache-fetch-2.results.fetched) + script: | + #!/usr/bin/env bash + res=${test} + echo ${res} + echo -n ${res} > $(results.test-fetched.path) + diff --git a/tests/test-pipelineruns/test-pipelinerun-oci.yaml b/tests/test-pipelineruns/test-pipelinerun-oci.yaml new file mode 100644 index 000000000..41efdf23b --- /dev/null +++ b/tests/test-pipelineruns/test-pipelinerun-oci.yaml @@ -0,0 +1,25 @@ +--- +apiVersion: tekton.dev/v1 +kind: PipelineRun +metadata: + name: pipelinerun-oci-test +spec: + pipelineRef: + name: pipeline + params: + - name: repo_url + value: https://github.com/chmouel/go-helloworld + - name: revision + value: main + # This uses S3 bucket to upload Caches + - name: registry + value: oci://127.0.0.1:5000/cache/go + - name: buildCommand + value: go build -v ./ + - name: image + value: golang:1.21 + workspaces: + - name: cred + emptyDir: {} + - name: source + emptyDir: {}