Skip to content

Commit

Permalink
Add unit tests for scripts/compute-tags.sh (jaegertracing#5780)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
- To address jaegertracing#5721, add unit tests first

## Description of the changes
- Add unit tests and call them from scripts linting workflow
- Minor refactoring of `scripts/compute-tags.sh` to make it easier to
read

## How was this change tested?
```
$ GREP=ggrep SHUNIT2=/Users/ysh/dev/shunit2 bash scripts/compute-tags.test.sh
testRequireImageName
testRequireBranch
testRequireGithubSha
testRandomBranch
   Actual: --tag docker.io/foo/bar --tag quay.io/foo/bar --tag docker.io/foo/bar:latest --tag quay.io/foo/bar:latest --tag docker.io/foo/bar-snapshot:sha --tag quay.io/foo/bar-snapshot:sha --tag docker.io/foo/bar-snapshot:latest --tag quay.io/foo/bar-snapshot:latest
   checking foo/bar
   checking foo/bar:latest
   checking foo/bar-snapshot:sha
   checking foo/bar-snapshot:latest
testMainBranch
   Actual: --tag docker.io/foo/bar --tag quay.io/foo/bar --tag docker.io/foo/bar:latest --tag quay.io/foo/bar:latest --tag docker.io/foo/bar-snapshot:sha --tag quay.io/foo/bar-snapshot:sha --tag docker.io/foo/bar-snapshot:latest --tag quay.io/foo/bar-snapshot:latest
   checking foo/bar
   checking foo/bar:latest
   checking foo/bar-snapshot:sha
   checking foo/bar-snapshot:latest
testSemVerBranch
   Actual: --tag docker.io/foo/bar --tag quay.io/foo/bar --tag docker.io/foo/bar:1.2.3 --tag quay.io/foo/bar:1.2.3 --tag docker.io/foo/bar:1.2 --tag quay.io/foo/bar:1.2 --tag docker.io/foo/bar:1 --tag quay.io/foo/bar:1 --tag docker.io/foo/bar-snapshot:sha --tag quay.io/foo/bar-snapshot:sha --tag docker.io/foo/bar-snapshot:latest --tag quay.io/foo/bar-snapshot:latest
   checking foo/bar
   checking foo/bar:1
   checking foo/bar:1.2
   checking foo/bar:1.2.3
   checking foo/bar-snapshot:sha
   checking foo/bar-snapshot:latest

Ran 6 tests.

OK
```

---------

Signed-off-by: Yuri Shkuro <github@ysh.us>
  • Loading branch information
yurishkuro authored Jul 27, 2024
1 parent 65f6cb9 commit cab1aee
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 17 deletions.
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"

0 comments on commit cab1aee

Please sign in to comment.