diff --git a/Makefile b/Makefile index 3ee9f82..cd6c6ea 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,8 @@ BATS_FLAGS ?= --print-output-on-failure --show-output-of-passing-tests --verbose E2E_TESTS ?= ./test/e2e/*.bats E2E_OPENSHIFT_PARAMS_SCRIPT ?= oc version +E2E_TKN_PARAMS_SCRIPT ?= tkn version + # generic arguments employed on most of the targets ARGS ?= @@ -47,10 +49,15 @@ test-e2e: install .PHONY: prepare-e2e-openshift prepare-e2e-openshift: ./hack/install-osp.sh $(OSP_VERSION) + .PHONY: test-e2e-openshift test-e2e-openshift: prepare-e2e-openshift test-e2e-openshift: test-e2e +.PHONY: test-e2e-tkn +test-e2e-tkn: prepare-e2e-openshift +test-e2e-tkn: test-e2e + # act runs the github actions workflows, so by default only running the test workflow (integration # and end-to-end) to avoid running the release workflow accidently act: ARGS = --workflows=./.github/workflows/test.yaml diff --git a/scripts/tkn-client.sh b/scripts/tkn-client.sh new file mode 100644 index 0000000..dc52ee8 --- /dev/null +++ b/scripts/tkn-client.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +shopt -s inherit_errexit +set -eu -o pipefail + +source "$(dirname ${BASH_SOURCE[0]})/common.sh" +source "$(dirname ${BASH_SOURCE[0]})/tkn-common.sh" + +[[ "${WORKSPACES_KUBECONFIG_DIR_BOUND}" == "true" ]] && \ +[[ -f ${WORKSPACES_KUBECONFIG_DIR_PATH}/kubeconfig ]] && \ +export KUBECONFIG=${WORKSPACES_KUBECONFIG_DIR_PATH}/kubeconfig + +${PARAMS_SCRIPT} diff --git a/scripts/tkn-common.sh b/scripts/tkn-common.sh new file mode 100644 index 0000000..c5475bd --- /dev/null +++ b/scripts/tkn-common.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +declare -rx PARAMS_SCRIPT="${PARAMS_SCRIPT:-}" + +declare -rx WORKSPACES_KUBECONFIG_DIR_PATH="${WORKSPACES_KUBECONFIG_DIR_PATH:-}" +declare -rx WORKSPACES_KUBECONFIG_DIR_BOUND="${WORKSPACES_KUBECONFIG_DIR_BOUND:-}" + +# +# Asserting Environment +# + +exported_or_fail \ + WORKSPACES_KUBECONFIG_DIR_BOUND \ + PARAMS_SCRIPT \ + diff --git a/templates/task-tkn.yaml b/templates/task-tkn.yaml new file mode 100644 index 0000000..8601b78 --- /dev/null +++ b/templates/task-tkn.yaml @@ -0,0 +1,63 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: tkn + labels: + app.kubernetes.io/version: {{ .Chart.Version }} +{{- if .Values.annotations }} + annotations: + {{- .Values.annotations | toYaml | nindent 4 }} +{{- end }} +spec: + description: >- + This task performs operations on Tekton resources using tkn. + + workspaces: + - name: kubeconfig_dir + optional: true + description: >- + An optional workspace that allows you to provide a .kube/config + file for tkn to access the cluster. The file should be placed at + the root of the Workspace with name kubeconfig. + + params: + - name: SCRIPT + description: tkn CLI script to execute + type: string + default: "tkn $@" + - name: ARGS + type: array + description: tkn CLI arguments to run + default: ["--help"] + + stepTemplate: + env: +{{- $variables := list + "params.SCRIPT" + "workspaces.kubeconfig_dir.bound" + "workspaces.kubeconfig_dir.path" +}} +{{- include "environment" ( list $variables ) | nindent 6 }} + + steps: +{{- include "load_scripts" ( list . "tkn-" ) | nindent 4 }} + + - name: tkn + image: {{ .Values.images.tkn }} + env: + - name: HOME + value: /tekton/home + command: + - /scripts/tkn-client.sh + args: ["$(params.ARGS)"] + securityContext: + runAsNonRoot: true + runAsUser: 65532 + volumeMounts: + - name: scripts-dir + mountPath: /scripts + + volumes: + - name: scripts-dir + emptyDir: {} diff --git a/test/e2e/resources/tkn-pipeline.yaml b/test/e2e/resources/tkn-pipeline.yaml new file mode 100644 index 0000000..9125dd9 --- /dev/null +++ b/test/e2e/resources/tkn-pipeline.yaml @@ -0,0 +1,34 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: Pipeline +metadata: + labels: + name: task-tkn + name: task-tkn +spec: + workspaces: + - name: kubeconfig_dir + optional: true + + params: + - name: SCRIPT + type: string + default: "tkn $@" + - name: ARGS + type: array + default: ["--help"] + + tasks: + - name: task-tkn + taskRef: + name: task-tkn + workspaces: + - name: kubeconfig_dir + workspace: kubeconfig_dir + params: + - name: SCRIPT + value: "$(params.SCRIPT)" + - name: ARGS + value: "$(params.ARGS)" + + \ No newline at end of file diff --git a/test/e2e/resources/tkn-pvc.yaml b/test/e2e/resources/tkn-pvc.yaml new file mode 100644 index 0000000..c15b5d3 --- /dev/null +++ b/test/e2e/resources/tkn-pvc.yaml @@ -0,0 +1,13 @@ +--- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + labels: + name: task-tkn + name: task-tkn +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 250Mi \ No newline at end of file diff --git a/test/e2e/tkn-e2e.bats b/test/e2e/tkn-e2e.bats new file mode 100644 index 0000000..d484e5f --- /dev/null +++ b/test/e2e/tkn-e2e.bats @@ -0,0 +1,43 @@ +#!/usr/bin/env bats + +source ./test/helper/helper.sh + +# E2E tests parameters for the test pipeline + +# Testing the tkn task, +@test "[e2e] tkn task" { + [ -n "${E2E_TKN_PARAMS_SCRIPT}" ] + + run kubectl delete taskrun --all + assert_success + + run tkn task start tkn \ + --param="SCRIPT=${E2E_TKN_PARAMS_SCRIPT}" \ + --use-param-defaults \ + --skip-optional-workspace \ + --showlog >&3 + assert_success + + # waiting a few seconds before asserting results + sleep 15 + + # assering the taskrun status, making sure all steps have been successful + declare tmpl_file="${BASE_DIR}/go-template.tpl" + # the following template is able to extract information from TaskRun and PipelineRun resources, + # and as well supports the current Tekton Pipeline version using a different `.task.results` + # attribute + cat >${tmpl_file} <