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

Do not use image tag without version #5783

Merged
merged 1 commit into from
Jul 28, 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
2 changes: 0 additions & 2 deletions scripts/compute-tags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ tags() {
IMAGE_TAGS="${IMAGE_TAGS}--tag docker.io/${1} --tag quay.io/${1}"
}

tags "${BASE_BUILD_IMAGE}"

## 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
Expand Down
41 changes: 32 additions & 9 deletions scripts/compute-tags.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

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

# if running on MacOS, `brew install grep` and run with GREP=ggrep
GREP=${GREP:-grep}

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

Expand All @@ -30,53 +33,72 @@ testRequireGithubSha() {
assertContains "$err" "$err" 'expecting GITHUB_SHA env var'
}

# out is global var which is populated for every output under test
out=""

scan_list() {
local target="$1"
echo "$out" | tr ' ' '\n' | $GREP -v '^--tag$' | $GREP -Po "^($target)"'$'
}

expect_contains() {
local target="$1"
# shellcheck disable=SC2155
local found=$(scan_list "$target")
assertContains "$found" "$target"
}

expect_not_contains() {
local target="$1"
# shellcheck disable=SC2155
local found=$(scan_list "$target")
assertNotContains "$found" "$target"
}

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

expectNot() {
expect_not() {
echo ' Actual:' "$out"
while [ "$#" -gt 0 ]; do
echo ' checking excludes' "$1"
assertNotContains "actual [$out]" "$out" "--tag docker.io/$1"
assertNotContains "actual [$out]" "$out" "--tag quay.io/$1"
expect_not_contains "docker.io/$1"
expect_not_contains "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[@]}"
expect_not "foo/bar"
}

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

testSemVerBranch() {
out=$(BRANCH=v1.2.3 GITHUB_SHA=sha bash "$computeTags" foo/bar)
expected=(
"foo/bar"
"foo/bar:latest"
"foo/bar:1"
"foo/bar:1.2"
Expand All @@ -85,6 +107,7 @@ testSemVerBranch() {
"foo/bar-snapshot:latest"
)
expect "${expected[@]}"
expect_not "foo/bar"
}

# shellcheck disable=SC1091
Expand Down
Loading