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

Add unit tests for scripts/compute-tags.sh #5780

Merged
merged 5 commits into from
Jul 27, 2024
Merged
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
12 changes: 11 additions & 1 deletion .github/workflows/ci-lint-shell-scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@ jobs:
with:
egress-policy: audit

- name: check out code
- name: Check out code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

- name: Install shellcheck
run: sudo apt-get install shellcheck

- name: Run shellcheck
run: shellcheck scripts/*.sh

- name: Install shunit2
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: kward/shunit2
path: .tools/shunit2

- name: Run unit tests for scripts
run: |
SHUNIT2=.tools/shunit2 bash scripts/compute-tags.test.sh
47 changes: 31 additions & 16 deletions scripts/compute-tags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@

# Compute major/minor/etc image tags based on the current branch

set -exu
set -ef -o pipefail

BASE_BUILD_IMAGE=$1
if [[ -z $QUIET ]]; then
set -x
fi

set -u

BASE_BUILD_IMAGE=${1:?'expecting Docker image name, such as jaegertracing/jaeger'}
BRANCH=${BRANCH:?'expecting BRANCH env var'}
GITHUB_SHA=${GITHUB_SHA:?'expecting GITHUB_SHA env var'}
# allow substituting for ggrep on Mac, since its default grep doesn't grok -P flag.
GREP=${GREP:-"grep"}

## if we are on a release tag, let's extract the version number
## the other possible value, currently, is 'main' (or another branch name)
## If we are on a release tag, let's extract the version number.
## The other possible values are 'main' or another branch name.
if [[ $BRANCH =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
MAJOR_MINOR_PATCH=$(echo "${BRANCH}" | grep -Po "([\d\.]+)")
MAJOR_MINOR_PATCH=$(echo "${BRANCH}" | ${GREP} -Po "([\d\.]+)")
MAJOR_MINOR=$(echo "${MAJOR_MINOR_PATCH}" | awk -F. '{print $1"."$2}')
MAJOR=$(echo "${MAJOR_MINOR_PATCH}" | awk -F. '{print $1}')
else
Expand All @@ -19,23 +28,29 @@ else
MAJOR=""
fi

# for docker.io and quay.io
BUILD_IMAGE=${BUILD_IMAGE:-"${BASE_BUILD_IMAGE}:${MAJOR_MINOR_PATCH}"}
IMAGE_TAGS="--tag docker.io/${BASE_BUILD_IMAGE} --tag docker.io/${BUILD_IMAGE} --tag quay.io/${BASE_BUILD_IMAGE} --tag quay.io/${BUILD_IMAGE}"
IMAGE_TAGS=""

# append given tag for docker.io and quay.io
tags() {
if [[ -n "$IMAGE_TAGS" ]]; then
# append space
IMAGE_TAGS="${IMAGE_TAGS} "
fi
IMAGE_TAGS="${IMAGE_TAGS}--tag docker.io/${1} --tag quay.io/${1}"
}

tags "${BASE_BUILD_IMAGE}"
tags "${BASE_BUILD_IMAGE}:${MAJOR_MINOR_PATCH}"

if [ "${MAJOR_MINOR}x" != "x" ]; then
MAJOR_MINOR_IMAGE="${BASE_BUILD_IMAGE}:${MAJOR_MINOR}"
IMAGE_TAGS="${IMAGE_TAGS} --tag docker.io/${MAJOR_MINOR_IMAGE} --tag quay.io/${MAJOR_MINOR_IMAGE}"
tags "${BASE_BUILD_IMAGE}:${MAJOR_MINOR}"
fi

if [ "${MAJOR}x" != "x" ]; then
MAJOR_IMAGE="${BASE_BUILD_IMAGE}:${MAJOR}"
IMAGE_TAGS="${IMAGE_TAGS} --tag docker.io/${MAJOR_IMAGE} --tag quay.io/${MAJOR_IMAGE}"
tags "${BASE_BUILD_IMAGE}:${MAJOR}"
fi

SNAPSHOT_TAG1="${BASE_BUILD_IMAGE}-snapshot:${GITHUB_SHA}"
IMAGE_TAGS="${IMAGE_TAGS} --tag docker.io/${SNAPSHOT_TAG1} --tag quay.io/${SNAPSHOT_TAG1}"
SNAPSHOT_TAG2="${BASE_BUILD_IMAGE}-snapshot:latest"
IMAGE_TAGS="${IMAGE_TAGS} --tag docker.io/${SNAPSHOT_TAG2} --tag quay.io/${SNAPSHOT_TAG2}"
tags "${BASE_BUILD_IMAGE}-snapshot:${GITHUB_SHA}"
tags "${BASE_BUILD_IMAGE}-snapshot:latest"

echo "${IMAGE_TAGS}"
82 changes: 82 additions & 0 deletions scripts/compute-tags.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash

# This script uses https://github.com/kward/shunit2 to run unit tests.
# The path to this repo must be provided via SHUNIT2 env var.

SHUNIT2="${SHUNIT2:?'expecting SHUNIT2 env var pointing to a dir with https://github.com/kward/shunit2 clone'}"

# shellcheck disable=SC2086
computeTags="$(dirname $0)/compute-tags.sh"

# suppress command echoing by compute-tags.sh
export QUIET=1

# unset env vars that were possibly set by the caller, since we test against them
unset BRANCH
unset GITHUB_SHA

testRequireImageName() {
err=$(bash "$computeTags" 2>&1)
assertContains "$err" 'expecting Docker image name'
}

testRequireBranch() {
err=$(GITHUB_SHA=sha bash "$computeTags" foo/bar 2>&1)
assertContains "$err" "$err" 'expecting BRANCH env var'
}

testRequireGithubSha() {
err=$(BRANCH=abcd bash "$computeTags" foo/bar 2>&1)
assertContains "$err" "$err" 'expecting GITHUB_SHA env var'
}

out=""
expect() {
echo ' Actual:' "$out"
while [ "$#" -gt 0 ]; do
echo ' checking' "$1"
assertContains "actual !!$out!!" "$out" "--tag docker.io/$1"
assertContains "actual !!$out!!" "$out" "--tag quay.io/$1"
shift
done
}

testRandomBranch() {
out=$(BRANCH=branch GITHUB_SHA=sha bash "$computeTags" foo/bar)
expected=(
"foo/bar"
"foo/bar:latest"
"foo/bar-snapshot:sha"
"foo/bar-snapshot:latest"
)
expect "${expected[@]}"
}

testMainBranch() {
out=$(BRANCH=main GITHUB_SHA=sha bash "$computeTags" foo/bar)
# TODO we do not want :latest tag in this scenario for non-snapshot images
expected=(
"foo/bar"
"foo/bar:latest"
"foo/bar-snapshot:sha"
"foo/bar-snapshot:latest"
)
expect "${expected[@]}"
}

testSemVerBranch() {
out=$(BRANCH=v1.2.3 GITHUB_SHA=sha bash "$computeTags" foo/bar)
# TODO we want :latest tag in this scenario, it's currently not produced
expected=(
"foo/bar"
"foo/bar:1"
"foo/bar:1.2"
"foo/bar:1.2.3"
"foo/bar-snapshot:sha"
"foo/bar-snapshot:latest"
)
expect "${expected[@]}"
}

# shellcheck disable=SC1091
source "${SHUNIT2}/shunit2"
Loading