From 126a07061f5b25f446493abd353a433bcbb4dd61 Mon Sep 17 00:00:00 2001 From: Senjuti De Date: Wed, 13 Mar 2024 18:14:00 +0530 Subject: [PATCH] OPC task working fine --- Makefile | 6 +++ scripts/opc-client.sh | 13 ++++++ scripts/opc-common.sh | 15 +++++++ templates/task-opc.yaml | 63 ++++++++++++++++++++++++++++ test/e2e/opc-e2e.bats | 43 +++++++++++++++++++ test/e2e/resources/opc-pipeline.yaml | 34 +++++++++++++++ test/e2e/resources/opc-pvc.yaml | 13 ++++++ values.yaml | 1 + 8 files changed, 188 insertions(+) create mode 100644 scripts/opc-client.sh create mode 100644 scripts/opc-common.sh create mode 100644 templates/task-opc.yaml create mode 100644 test/e2e/opc-e2e.bats create mode 100644 test/e2e/resources/opc-pipeline.yaml create mode 100644 test/e2e/resources/opc-pvc.yaml diff --git a/Makefile b/Makefile index a83f4c6..c5de278 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,8 @@ E2E_OPENSHIFT_PARAMS_SCRIPT ?= oc version E2E_TKN_PARAMS_SCRIPT ?= tkn version +E2E_OPC_PARAMS_SCRIPT ?= opc version + E2E_KN_PARAMS_ARGS ?= help E2E_KN_APPLY_PARAMS_SERVICE ?= hello @@ -109,6 +111,10 @@ test-e2e-openshift: test-e2e test-e2e-tkn: prepare-e2e-openshift test-e2e-tkn: test-e2e +.PHONY: test-e2e-opc +test-e2e-opc: prepare-e2e-openshift +test-e2e-opc: 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/opc-client.sh b/scripts/opc-client.sh new file mode 100644 index 0000000..6c41705 --- /dev/null +++ b/scripts/opc-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]})/opc-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/opc-common.sh b/scripts/opc-common.sh new file mode 100644 index 0000000..c5475bd --- /dev/null +++ b/scripts/opc-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-opc.yaml b/templates/task-opc.yaml new file mode 100644 index 0000000..7d101ac --- /dev/null +++ b/templates/task-opc.yaml @@ -0,0 +1,63 @@ +--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: opc + labels: + app.kubernetes.io/version: {{ .Chart.Version }} +{{- if .Values.annotations }} + annotations: + {{- .Values.annotations | toYaml | nindent 4 }} +{{- end }} +spec: + description: >- + The opc task makes it easy to work with Tekton resources in OpenShift Pipelines. + + workspaces: + - name: kubeconfig_dir + optional: true + description: >- + An optional workspace that allows you to provide a .kube/config + file for opc to access the cluster. The file should be placed at + the root of the Workspace with name kubeconfig. + + params: + - name: SCRIPT + description: opc CLI script to execute + type: string + default: "opc $@" + - name: ARGS + type: array + description: opc 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 . "opc-" ) | nindent 4 }} + + - name: opc + image: {{ .Values.images.opc }} + env: + - name: HOME + value: /tekton/home + command: + - /scripts/opc-client.sh + args: ["$(params.ARGS)"] + securityContext: + runAsNonRoot: true + runAsUser: 65532 + volumeMounts: + - name: scripts-dir + mountPath: /scripts + + volumes: + - name: scripts-dir + emptyDir: {} \ No newline at end of file diff --git a/test/e2e/opc-e2e.bats b/test/e2e/opc-e2e.bats new file mode 100644 index 0000000..a218f75 --- /dev/null +++ b/test/e2e/opc-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] opc task" { + [ -n "${E2E_OPC_PARAMS_SCRIPT}" ] + + run kubectl delete taskrun --all + assert_success + + run tkn task start opc \ + --param="SCRIPT=${E2E_OPC_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} <