From bebe15cef43597f92691ee121b18b9cb5a313877 Mon Sep 17 00:00:00 2001 From: Johnny Bieren Date: Wed, 30 Oct 2024 08:29:54 -0400 Subject: [PATCH] test: add create-pyxis-image test for oci artifacts (#644) This ensures the --platform flag is passed to oras properly Signed-off-by: Johnny Bieren --- tasks/create-pyxis-image/tests/mocks.sh | 7 +- .../test-create-pyxis-image-oci-artifact.yaml | 126 ++++++++++++++++++ 2 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 tasks/create-pyxis-image/tests/test-create-pyxis-image-oci-artifact.yaml diff --git a/tasks/create-pyxis-image/tests/mocks.sh b/tasks/create-pyxis-image/tests/mocks.sh index de1d7f292..086c550ce 100644 --- a/tasks/create-pyxis-image/tests/mocks.sh +++ b/tasks/create-pyxis-image/tests/mocks.sh @@ -9,7 +9,7 @@ function create_container_image() { # e.g. 0001, 0002, 0003... echo The image id is $(awk 'END{printf("%04i", NR)}' $(workspaces.data.path)/mock_create_container_image.txt) - if [[ "$*" != "--pyxis-url https://pyxis.preprod.api.redhat.com/ --certified false --tags "*" --is-latest false --verbose --oras-manifest-fetch /tmp/oras-manifest-fetch.json --name "*" --media-type my_media_type --digest "*" --architecture-digest "*" --architecture "*" --rh-push "* ]] + if [[ "$*" != "--pyxis-url https://pyxis.preprod.api.redhat.com/ --certified false --tags "*" --is-latest false --verbose --oras-manifest-fetch /tmp/oras-manifest-fetch.json --name "*" --media-type "*" --digest "*" --architecture-digest "*" --architecture "*" --rh-push "* ]] then echo Error: Unexpected call echo Mock create_container_image called with: $* @@ -30,7 +30,10 @@ function cleanup_tags() { function skopeo() { echo $* >> $(workspaces.data.path)/mock_skopeo.txt - if [[ "$*" == "inspect --raw docker://"* ]] || [[ "$*" == "inspect --no-tags --override-os linux --override-arch "*" docker://"* ]] + if [[ "$*" == "inspect --raw docker://registry.io/oci-artifact"* ]] + then + echo '{"mediaType": "application/vnd.oci.image.index.v1+json"}' + elif [[ "$*" == "inspect --raw docker://"* ]] || [[ "$*" == "inspect --no-tags --override-os linux --override-arch "*" docker://"* ]] then echo '{"mediaType": "my_media_type"}' else diff --git a/tasks/create-pyxis-image/tests/test-create-pyxis-image-oci-artifact.yaml b/tasks/create-pyxis-image/tests/test-create-pyxis-image-oci-artifact.yaml new file mode 100644 index 000000000..66d6f816e --- /dev/null +++ b/tasks/create-pyxis-image/tests/test-create-pyxis-image-oci-artifact.yaml @@ -0,0 +1,126 @@ +--- +apiVersion: tekton.dev/v1 +kind: Pipeline +metadata: + name: test-create-pyxis-image-oci-artifact +spec: + description: | + Run the create-pyxis-image task with a single oci artifact in the snapshot. + This test ensures the ORAS_ARGS are passed properly. + workspaces: + - name: tests-workspace + tasks: + - name: setup + workspaces: + - name: data + workspace: tests-workspace + taskSpec: + workspaces: + - name: data + steps: + - name: setup-values + image: quay.io/konflux-ci/release-service-utils:65d8db844c008e7736ec8dff307868525279484d + script: | + #!/usr/bin/env bash + set -eux + + cat > "$(workspaces.data.path)"/snapshot.json << EOF + { + "application": "myapp", + "components": [ + { + "name": "comp", + "containerImage": "source@sha256:mydigest", + "repository": "registry.io/oci-artifact", + "tags": [ + "testtag" + ] + } + ] + } + EOF + - name: run-task + taskRef: + name: create-pyxis-image + params: + - name: pyxisSecret + value: test-create-pyxis-image-cert + - name: server + value: stage + - name: snapshotPath + value: snapshot.json + workspaces: + - name: data + workspace: tests-workspace + runAfter: + - setup + - name: check-result + workspaces: + - name: data + workspace: tests-workspace + taskSpec: + workspaces: + - name: data + steps: + - name: check-result + image: quay.io/konflux-ci/release-service-utils:65d8db844c008e7736ec8dff307868525279484d + script: | + #!/usr/bin/env bash + set -eux + + if [ "$(wc -l < "$(workspaces.data.path)"/mock_create_container_image.txt)" != 1 ]; then + echo Error: create_container_image was expected to be called 1 time. Actual calls: + cat "$(workspaces.data.path)/mock_create_container_image.txt" + exit 1 + fi + + if [ -f "$(workspaces.data.path)"/mock_cleanup_tags.txt ]; then + echo Error: cleanup_tags was not expected to be called. Actual calls: + cat "$(workspaces.data.path)/mock_cleanup_tags.txt" + exit 1 + fi + + if ! grep -- "--tags testtag" < "$(workspaces.data.path)"/mock_create_container_image.txt 2> /dev/null + then + echo Error: create_container_image call was expected to include "--tags testtag". Actual call: + cat "$(workspaces.data.path)/mock_create_container_image.txt" + exit 1 + fi + + if ! grep -- "--rh-push false" < "$(workspaces.data.path)"/mock_create_container_image.txt 2> /dev/null + then + echo Error: create_container_image call was expected to include "--rh-push false". Actual call: + cat "$(workspaces.data.path)/mock_create_container_image.txt" + exit 1 + fi + + if [ "$(wc -l < "$(workspaces.data.path)"/mock_skopeo.txt)" != 1 ]; then + echo Error: skopeo was expected to be called 1 time. Actual calls: + cat "$(workspaces.data.path)/mock_skopeo.txt" + exit 1 + fi + + if [ "$(wc -l < "$(workspaces.data.path)"/mock_oras.txt)" != 3 ]; then + echo Error: oras was expected to be called 3 times. Actual calls: + cat "$(workspaces.data.path)/mock_oras.txt" + exit 1 + fi + + if ! grep -- "--platform linux/amd64" < "$(workspaces.data.path)"/mock_oras.txt 2> /dev/null + then + echo Error: oras call was expected to include "--platform linux/amd64". Actual call: + cat "$(workspaces.data.path)/mock_oras.txt" + exit 1 + fi + + [ "$(head -n 1 < "$(workspaces.data.path)"/mock_skopeo.txt)" \ + = "inspect --raw docker://registry.io/oci-artifact@sha256:mydigest" ] + + if [ "$(wc -l < "$(workspaces.data.path)"/mock_select-oci-auth.txt)" != 2 ]; then + echo Error: select-oci-with was expected to be called 2 times. Actual calls: + cat "$(workspaces.data.path)/mock_select-oci-auth.txt" + exit 1 + fi + + runAfter: + - run-task