Skip to content

Commit

Permalink
Add e2e test for GCS
Browse files Browse the repository at this point in the history
This adds an e2e test for GCS using storage emulator by passing env
STORAGE_EMULATOR_HOST. Reference:
https://cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest
  • Loading branch information
khrm committed Oct 10, 2024
1 parent 6ad954d commit efaabd9
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ GOFUMPT=gofumpt
e2e-coverage: ## run e2e tests with coverage
@go test -failfast -count=1 -tags=$(E2E_TAG) ./tests -coverpkg=./... -coverprofile /tmp/coverage.out
@go tool cover -func /tmp/coverage.out
tests/e2e.sh

e2e: e2e-coverage

e2e-docker: ## run e2e tests with a docker registry started
Expand Down
39 changes: 39 additions & 0 deletions tests/e2e.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

ROOT="$(git rev-parse --show-toplevel)"

# Apply the GCS emulator configuration
kubectl apply -f "${ROOT}/tests/gcs-emulator.yaml"

# Wait for the deployment to be ready
echo "Waiting for GCS emulator deployment to be ready..."
kubectl wait --for=condition=available --timeout=300s deployment/gcs-emulator

# Check the deployment status
if [ $? -eq 0 ]; then
echo "GCS emulator deployment is ready"
else
echo "Error: GCS emulator deployment failed to become ready within the timeout period"
exit 1
fi

ko apply -R -f ${ROOT}/dev/step-action/

ko create -f ${ROOT}/tests/test-pipeline-gcs.yaml

# Check the pipelinerun status
echo "Waiting for pipelinerun to complete..."
kubectl wait --for=condition=succeeded --timeout=600s pipelinerun --all

if [ $? -eq 0 ]; then
echo "Pipelinerun completed successfully"
exit 0
else
echo "Error: Pipelinerun failed or timed out"
kubectl get pipelinerun
kubectl describe pipelinerun
kubectl get taskrun
kubectl describe taskrun
exit 1
fi

48 changes: 48 additions & 0 deletions tests/gcs-emulator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: gcs-emulator
spec:
selector:
matchLabels:
run: gcs-emulator
replicas: 1
template:
metadata:
labels:
run: gcs-emulator
spec:
containers:
- name: gcs-emulator
image: quay.io/khrm/gcse-emulator:latest
volumeMounts:
- mountPath: /data
name: data
ports:
- containerPort: 9000
securityContext:
allowPrivilegeEscalation: false
capabilities:
add:
- NET_BIND_SERVICE
drop:
- ALL
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
volumes:
- name: data
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: gcs-emulator
labels:
run: gcs-emulator
spec:
ports:
- port: 9000
protocol: TCP
selector:
run: gcs-emulator
177 changes: 177 additions & 0 deletions tests/test-pipeline-gcs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
generateName: pipelinerun-gcs-
spec:
pipelineSpec:
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
- name: test
tasks:
- displayName: Build go application
name: build-task
workspaces:
- name: source
workspace: source
- name: test
workspace: test
taskSpec:
workspaces:
- name: source
- name: cred
- name: test
params:
- name: buildCommand
default: $(params.buildCommand)
- name: cachePatterns
default: $(params.cachePatterns)
- name: image
default: $(params.image)
stepTemplate:
env:
- name: "STORAGE_EMULATOR_HOST"
value: "gcs-emulator.default.svc.cluster.local:9000"
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: 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-test
ref:
name: cache-fetch
params:
- name: patterns
value: $(params.cachePatterns)
- name: source
value: $(params.registry)/cache-go:{{hash}}
- name: cachePath
value: $(workspaces.test.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: validate-cache
workingDir: $(workspaces.test.path)/cache
image: $(params.image)
env:
- name: GOCACHE
value: $(workspaces.source.path)/cache/gocache
- name: GOMODCACHE
value: $(workspaces.source.path)/cache/gomodcache
script: |
set -x
echo "Cache size is $(du -sh $(workspaces.test.path)/cache)"
if [ $(du -shb $(workspaces.test.path)/cache | cut -f1) -lt 1000 ] ;then
echo "cache fetch failed"
exit 1
fi
exit 0
params:
- name: repo_url
value: https://github.com/chmouel/go-helloworld
- name: revision
value: main
- name: registry
value: gs://tekton-cache
- name: buildCommand
value: go build -v ./
- name: image
value: golang:1.21
workspaces:
- name: cred
secret:
secretName: aws-cred
- name: source
emptyDir: {}
- name: test
emptyDir: {}

0 comments on commit efaabd9

Please sign in to comment.