Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle testing #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/resources/workspace-template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Mi
116 changes: 116 additions & 0 deletions .github/scripts/test_tekton_tasks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/bin/bash

# This script will run task tests for all task directories
# provided either via TASK_DIRS env var, or as arguments
# when running the script.
#
# Requirements:
# - Connection to a running k8s cluster (e.g. kind)
# - Tekton installed on the cluster
# - tkn installed
#
# Examples of usage:
# export TASK_DIRS="mydir/task/apply-mapping/0.3 some/other/dir/0.1"
# ./test_tekton_tasks.sh
#
# or
#
# ./test_tekton_tasks.sh mydir/task/apply-mapping/0.3 some/other/dir/0.1

# yield empty strings for unmatched patterns
shopt -s nullglob

WORKSPACE_TEMPLATE=${BASH_SOURCE%/*/*}/resources/workspace-template.yaml

if [ $# -gt 0 ]
then
TASK_DIRS=$@
fi

if [ -z "${TASK_DIRS}" ]
then
echo Error: No task directories.
echo Usage:
echo "$0 [DIR1] [DIR2] [...]"
echo
echo or
echo
echo "export TASK_DIRS=\"DIR1 DIR2 ...\""
echo "$0"
exit 1
fi

# Check that all directories exist. If not, fail
for DIR in $TASK_DIRS
do
if [ ! -d "$DIR" ]
then
echo "Error: Directory does not exist: $DIR"
exit 1
fi
done

for DIR in $TASK_DIRS
do
echo Task dir: $DIR
TASK_NAME=${DIR%/*}
TASK_NAME=${TASK_NAME##*/}
echo " Task name: $TASK_NAME"

TASK_PATH=${DIR}/${TASK_NAME}.yaml
if [ ! -f $TASK_PATH ]
then
echo Error: Task file does not exist: $TASK_PATH
exit 1
fi

TESTS_DIR=${DIR}/tests
if [ ! -d $TESTS_DIR ]
then
echo Error: tests dir does not exist: $TESTS_DIR
exit 1
fi

TEST_PATHS=($TESTS_DIR/test*.yaml)
if [ ${#TEST_PATHS[@]} -eq 0 ]
then
echo " Warning: No tests. Skipping..."
continue
fi

echo " Installing task"
kubectl apply -f $TASK_PATH

for TEST_PATH in ${TEST_PATHS[@]}
do
echo " Installing test pipeline: $TEST_PATH"
kubectl apply -f $TEST_PATH
TEST_NAME=${TEST_PATH##*/}
TEST_NAME=${TEST_NAME%.*}

# Sometimes the pipeline is not available immediately
while ! kubectl get pipeline $TEST_NAME > /dev/null 2>&1
do
echo " Pipeline $TEST_NAME not ready. Waiting 5s..."
sleep 5
done

PIPELINERUN=$(tkn p start $TEST_NAME -w name=tests-workspace,volumeClaimTemplateFile=$WORKSPACE_TEMPLATE -o json | jq -r '.metadata.name')
echo " Started pipelinerun $PIPELINERUN"
tkn pr logs $PIPELINERUN -f
while [ "$(kubectl get pr $PIPELINERUN -o=jsonpath='{.status.conditions[0].status}')" == "Unknown" ]
do
echo " PipelineRun $PIPELINERUN status Unknown. Waiting for update..."
sleep 5
done

if [ "$(kubectl get pr $PIPELINERUN -o=jsonpath='{.status.conditions[0].status}')" == "True" ]
then
echo " Pipelinerun succeeded"
else
echo " Pipelinerun failed"
exit 1
fi
done

done
56 changes: 56 additions & 0 deletions .github/workflows/tekton_task_tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
name: Tekton Task Tests
on:
pull_request:
branches: ['main']
paths:
- 'catalog/task/**'
jobs:
run-tekton-task-tests:
name: Run Tekton Task tests
runs-on: ubuntu-latest
steps:
- name: Create k8s Kind Cluster
uses: helm/[email protected]
- name: Check cluster info
run: |
kubectl cluster-info
- name: Install Tekton
timeout-minutes: 10
run: |
kubectl apply --filename https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml

echo -n Waiting for Tekton pods to appear..
while [ $(kubectl get pods --namespace tekton-pipelines -o name | wc -l) -lt 2 ]
do
sleep 1
echo -n .
done
echo " done"

echo Waiting for Tekton pods to be ready
for POD in $(kubectl get pods --namespace tekton-pipelines -o name)
do
kubectl wait --timeout=120s --for=condition=Ready $POD --namespace tekton-pipelines
done

kubectl get pods --namespace tekton-pipelines
- name: Checkout code
uses: actions/checkout@v3
- name: Install tkn
uses: ./.github/actions/install-tkn
- name: Get changed dirs
id: changed-dirs
uses: tj-actions/changed-files@v35
with:
files: |
catalog/task/*/*
dir_names: "true"
dir_names_max_depth: "4"
- name: Show changed dirs
run: |
echo ${{ steps.changed-dirs.outputs.all_changed_files }}
- name: Test Tekton tasks
run: .github/scripts/test_tekton_tasks.sh
env:
TASK_DIRS: ${{ steps.changed-dirs.outputs.all_changed_files }}
65 changes: 62 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Contributions of all kinds are welcome. In particular pull requests are apprecia

## Code of Conduct

Our [company values](https://www.redhat.com/en/about/brand/standards/culture) guide us in our day-to-day interactions and decision-making. Our open source projects are no exception and they will define the standards for how to engage with the project through a [code of conduct](CODE_OF_CONDUCT.md).
Our [company values](https://www.redhat.com/en/about/brand/standards/culture) guide us in our day-to-day interactions and decision-making. Our open source projects are no exception and they will define the standards for how to engage with the project through a [code of conduct](CODE_OF_CONDUCT.md).

Please, make sure you read both of them before contributing, so you can help us to maintain a healthy community.

Expand Down Expand Up @@ -57,9 +57,9 @@ A well formatted commit would look something like this:

```
feat(issue-id): what this commit does

Overall explanation of what this commit is achieving and the motivation behind it.

Signed-off-by: Your Name <[email protected]>
```

Expand All @@ -82,3 +82,62 @@ Before a pull request can be merged:
* The feature branch must be rebased so it contains the latest changes from the target branch
* The CI has to pass successfully
* Every comment has to be addressed and resolved

### Tekton Task Testing

When a pull request is opened, Tekton Task tests are run for all the task version
directories that are modified.

The Github workflow is defined in
[.github/workflows/tekton_task_tests.yaml](.github/workflows/tekton_task_tests.yaml)

#### Adding new Tekton Task tests

Tests are defined as Tekton Pipelines inside the `tests` subdirectory of the task version
directory. Their filenames must match `test*.yaml` and the Pipeline name must be
the same as the filename (sans `.yaml`).

E.g. to add a test pipeline for `catalog/task/apply-mapping/0.3`, you can add a pipeline
such as `catalog/task/apply-mapping/0.3/tests/test-apply-mapping.yaml`.

To reference the task under test in a test pipeline, use just the name - the test
script will install the task CR locally. For example:

```
- name: run-task
taskRef:
name: apply-mapping
```

Currently task tests are not required, so if a task version directory is modified
in a PR and there are no tests, that directory will be skipped.

##### Workspaces

Some tasks require one or multiple workspaces. This means that the test pipeline will also
have to declare a workspace and bind it to the workspace(s) required by the task under test.

Currently, the test script will pass a single workspace named `tests-workspace` mapping
to a 10Mi volume when starting the pipelinerun. This workspace can be used in the test pipeline.

#### Running Tekton Task tests manually

Requirements:

* A k8s cluster running and kubectl default context pointing to it (e.g. [kind](https://kind.sigs.k8s.io/docs/user/quick-start/#installation))
* Tekton installed in the cluster ([docs](https://tekton.dev/docs/pipelines/install/))

```kubectl apply --filename https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml```

* tkn cli installed ([docs](https://tekton.dev/docs/cli/))

* jq installed

Once you have everything ready, you can run the test script and pass task version directories
as arguments, e.g.

```
./.github/scripts/test_tekton_tasks.sh catalog/task/apply-mapping/0.3
```

This will install the task and run all test pipelines matching `tests/test*.yaml`.
19 changes: 0 additions & 19 deletions catalog/task/apply-mapping/0.3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,6 @@ meant to inform whether a mapped Snapshot is being returned or the original one.
| snapshot | The Snapshot in JSON format to apply the mapping to | No | - |
| extraConfigPath | The path to the config file containing the mapping | Yes | - |

## Example usage

This is an example usage of the `apply-mapping` task:

```
---
tasks:
- name: apply-mapping
taskRef:
name: apply-mapping
params:
- name: snapshot
value: '{"components":[{"name":"component1","containerImage":"quay.io/repo/component1:digest"}}]}'
- name: extraConfigPath
value: "path/to/file"
workspaces:
- name: config
workspace: config_workspace
```
## Changes since 0.2

* Base image was changed from `release-utils` to `release-base-image`
Expand Down
18 changes: 0 additions & 18 deletions catalog/task/apply-mapping/0.3/tests/run.yaml

This file was deleted.

85 changes: 85 additions & 0 deletions catalog/task/apply-mapping/0.3/tests/test-apply-mapping-empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: test-apply-mapping-empty
spec:
description: |
Run the apply-mapping with a basic snapshot.spec json without any extra config
and verify that the returned json is the same as the one on the input.
workspaces:
- name: tests-workspace
tasks:
- name: setup
taskSpec:
results:
- name: snapshot
steps:
- name: setup-values
image: quay.io/hacbs-release/release-utils:5b1a1cd9fd68625cab5573ce62e0d87e6f93f341
script: |
#!/usr/bin/env sh
set -eux

cat > $(results.snapshot.path) << EOF
{
"application": "myapp",
"components": [
{
"name": "comp1",
"containerImage": "imageurl1",
"source": {
"git": {
"revision": "myrev",
"url": "myurl"
}
}
},
{
"name": "comp3",
"repository": "repo3"
},
{
"name": "comp4",
"repository": "repo4"
}
]
}
EOF
- name: run-task
taskRef:
name: apply-mapping
params:
- name: snapshot
value: $(tasks.setup.results.snapshot)
- name: extraConfigPath
value: ""
workspaces:
- name: config
workspace: tests-workspace
- name: check-result
params:
- name: origSnapshot
value: $(tasks.setup.results.snapshot)
- name: mappedSnapshot
value: $(tasks.run-task.results.snapshot)
taskSpec:
params:
- name: origSnapshot
type: string
- name: mappedSnapshot
type: string
steps:
- name: check-result
image: quay.io/hacbs-release/release-utils:5b1a1cd9fd68625cab5573ce62e0d87e6f93f341
env:
- name: "ORIG"
value: '$(params.origSnapshot)'
- name: "MAPPED"
value: '$(params.mappedSnapshot)'
script: |
#!/usr/bin/env sh
set -eux

# the resulting json is exactly the same as the original one (since no mapping file was used)
test "$(echo $ORIG|jq --sort-keys .)" == "$(echo $MAPPED|jq --sort-keys .)"
Loading