Skip to content

Commit

Permalink
Adds e2e tests for oci tekton cache
Browse files Browse the repository at this point in the history
Signed-off-by: PuneetPunamiya <[email protected]>
  • Loading branch information
PuneetPunamiya committed Oct 17, 2024
1 parent 7409b33 commit 4483b77
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 6 deletions.
19 changes: 13 additions & 6 deletions .github/workflows/latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions tests/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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")
}
}
151 changes: 151 additions & 0 deletions tests/test-pipelineruns/test-pipeline-oci.yaml
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions tests/test-pipelineruns/test-pipelinerun-oci.yaml
Original file line number Diff line number Diff line change
@@ -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: {}

0 comments on commit 4483b77

Please sign in to comment.