forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for scripts/compute-tags.sh (jaegertracing#5780)
## 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
1 parent
65f6cb9
commit cab1aee
Showing
3 changed files
with
124 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |