From 9ce8c0b7d50b7181578693ea5fec3db652f83ceb Mon Sep 17 00:00:00 2001
From: Alexander Wellbrock <a.wellbrock@mailbox.org>
Date: Sat, 16 Jan 2021 14:23:00 +0100
Subject: [PATCH] shell-tools: add test-cases for manifests

---
 resources/shell-tools/.gitlab-ci.yml          |  5 ++-
 resources/shell-tools/test/manifest-latest.sh | 34 +++++++++++++++++
 resources/shell-tools/test/manifest-print.sh  | 29 +++++++++++++++
 resources/shell-tools/test/manifest-push.sh   | 31 ++++++++++++++++
 resources/shell-tools/test/manifest.sh        | 37 +++++++++++++++++++
 5 files changed, 135 insertions(+), 1 deletion(-)
 create mode 100755 resources/shell-tools/test/manifest-latest.sh
 create mode 100755 resources/shell-tools/test/manifest-print.sh
 create mode 100755 resources/shell-tools/test/manifest-push.sh
 create mode 100755 resources/shell-tools/test/manifest.sh

diff --git a/resources/shell-tools/.gitlab-ci.yml b/resources/shell-tools/.gitlab-ci.yml
index b298ecd..3b60c25 100644
--- a/resources/shell-tools/.gitlab-ci.yml
+++ b/resources/shell-tools/.gitlab-ci.yml
@@ -16,4 +16,7 @@ shell-tools-test:
   stage: shell-tools-test
   script:
     - ./resources/shell-tools/test/fix-dockerfile-pinning.sh
-    - ./resources/shell-tools/test/multiarch.sh
+    - ./resources/shell-tools/test/manifest.sh
+    - ./resources/shell-tools/test/manifest-latest.sh
+    - ./resources/shell-tools/test/manifest-print.sh
+    - ./resources/shell-tools/test/manifest-push.sh
diff --git a/resources/shell-tools/test/manifest-latest.sh b/resources/shell-tools/test/manifest-latest.sh
new file mode 100755
index 0000000..c44e0ad
--- /dev/null
+++ b/resources/shell-tools/test/manifest-latest.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+
+set -o pipefail
+set -u
+
+BASEDIR=$(dirname "$0")
+
+podman build --format docker -t mytest-latest:test-amd64 -f- <<EOF
+FROM scratch
+EOF
+podman build --format docker -t mytest-latest:test-arm64 -f- <<EOF
+FROM scratch
+EOF
+
+"$BASEDIR"/../bin/manifest.sh --local --latest mytest-latest:test registry.example.com/latest-test 1.2.3
+
+ERRORS=0
+podman manifest inspect registry.example.com/latest-test:latest
+ERRORS=$((ERRORS + "$?"))
+
+if [ "$ERRORS" = "0" ]; then
+  EXIT_CODE=0
+else
+  EXIT_CODE=1
+fi
+
+podman rmi -f registry.example.com/latest-test:1 \
+  registry.example.com/latest-test:1.2 \
+  registry.example.com/latest-test:1.2.3 \
+  registry.example.com/latest-test:latest
+podman rmi -f "$(podman images -q mytest-latest:test-amd64)"
+podman rmi -f "$(podman images -q mytest-latest:test-arm64)"
+
+exit $EXIT_CODE
diff --git a/resources/shell-tools/test/manifest-print.sh b/resources/shell-tools/test/manifest-print.sh
new file mode 100755
index 0000000..d8b6d43
--- /dev/null
+++ b/resources/shell-tools/test/manifest-print.sh
@@ -0,0 +1,29 @@
+#!/bin/bash
+
+set -o pipefail
+set -u
+
+BASEDIR=$(dirname "$0")
+
+podman build --format docker -t mytest-latest:test-amd64 -f- <<EOF
+FROM scratch
+EOF
+podman build --format docker -t mytest-latest:test-arm64 -f- <<EOF
+FROM scratch
+EOF
+
+MANIFESTS_TO_PUSH=$("$BASEDIR"/../bin/manifest.sh --local --print mytest-latest:test registry.example.com/latest-test 1.2.3)
+
+EXIT_CODE=1
+
+if [ "$MANIFESTS_TO_PUSH" == "registry.example.com/latest-test:1 registry.example.com/latest-test:1.2 registry.example.com/latest-test:1.2.3" ]; then
+  EXIT_CODE=0
+fi
+
+podman rmi -f registry.example.com/latest-test:1 \
+  registry.example.com/latest-test:1.2 \
+  registry.example.com/latest-test:1.2.3
+podman rmi -f "$(podman images -q mytest-latest:test-amd64)"
+podman rmi -f "$(podman images -q mytest-latest:test-arm64)"
+
+exit $EXIT_CODE
diff --git a/resources/shell-tools/test/manifest-push.sh b/resources/shell-tools/test/manifest-push.sh
new file mode 100755
index 0000000..24560fe
--- /dev/null
+++ b/resources/shell-tools/test/manifest-push.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+
+set -o pipefail
+set -u
+
+BASEDIR=$(dirname "$0")
+
+podman build --format docker -t mytest-latest:test-amd64 -f- <<EOF
+FROM scratch
+EOF
+podman build --format docker -t mytest-latest:test-arm64 -f- <<EOF
+FROM scratch
+EOF
+
+MANIFESTS_TO_PUSH=$("$BASEDIR"/../bin/manifest.sh --archs="amd64:arm64" --local --print mytest-latest:test registry.example.com/latest-test 1.2.3)
+
+EXIT_CODE=1
+
+PUSHED=$("$BASEDIR"/../bin/manifest-push.sh --dry "$MANIFESTS_TO_PUSH")
+
+if [ "$PUSHED" == "registry.example.com/latest-test:1 registry.example.com/latest-test:1.2 registry.example.com/latest-test:1.2.3" ]; then
+  EXIT_CODE=0
+fi
+
+podman rmi -f registry.example.com/latest-test:1 \
+  registry.example.com/latest-test:1.2 \
+  registry.example.com/latest-test:1.2.3
+podman rmi -f "$(podman images -q mytest-latest:test-amd64)"
+podman rmi -f "$(podman images -q mytest-latest:test-arm64)"
+
+exit $EXIT_CODE
diff --git a/resources/shell-tools/test/manifest.sh b/resources/shell-tools/test/manifest.sh
new file mode 100755
index 0000000..f74de37
--- /dev/null
+++ b/resources/shell-tools/test/manifest.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+
+set -o pipefail
+set -u
+
+BASEDIR=$(dirname "$0")
+
+podman build --format docker -t mytest-latest:test-amd64 -f- <<EOF
+FROM scratch
+EOF
+podman build --format docker -t mytest-latest:test-arm64 -f- <<EOF
+FROM scratch
+EOF
+
+"$BASEDIR"/../bin/manifest.sh --archs="amd64:arm64" --local mytest-latest:test registry.example.com/latest-test 1.2.3
+
+ERRORS=0
+podman manifest inspect registry.example.com/latest-test:1
+ERRORS=$((ERRORS + "$?"))
+podman manifest inspect registry.example.com/latest-test:1.2
+ERRORS=$((ERRORS + "$?"))
+podman manifest inspect registry.example.com/latest-test:1.2.3
+ERRORS=$((ERRORS + "$?"))
+
+if [ "$ERRORS" = "0" ]; then
+  EXIT_CODE=0
+else
+  EXIT_CODE=1
+fi
+
+podman rmi -f registry.example.com/latest-test:1 \
+  registry.example.com/latest-test:1.2 \
+  registry.example.com/latest-test:1.2.3
+podman rmi -f "$(podman images -q mytest-latest:test-amd64)"
+podman rmi -f "$(podman images -q mytest-latest:test-arm64)"
+
+exit $EXIT_CODE
-- 
GitLab