Skip to content
Open
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
41 changes: 32 additions & 9 deletions test/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -938,22 +938,18 @@ get_image_labels() {
exit 2
fi

local image_labels
# Ensure that we don't have a tag and digest for skopeo
image=$(get_image_registry_repository_digest "$image")

# Fetch first arch using --raw as skopeo may fail when
# the image does not have build for arch same as base system.
local raw_output

if ! raw_output=$(retry skopeo inspect --raw "docker://${image}"); then
echo "get_image_labels: raw inspect failed after retries" >&2
first_arch=$(get_first_arch "${image}")
if [ -z "${first_arch}" ] || [ "${first_arch}" == "null" ]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just curios. about these lines.
Am I right when I think that these same lines are inside the get_first_arch function and they end with exit 1. Which means the script will be terminated and nothing returned. Meaning if that fails inside get_first_arch function, it will never reach this part of code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, so either we can remove these lines or modify the get_first_arch to return empty string if no arch is found. What do you suggest ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I see these kind of tests and error failing is done directly in the functions. Lets keep those lines in get_first_arch so you do not have to test it everywhere.

echo "get_image_labels: architecture could not be determined for ${image}" >&2
exit 1
fi

first_arch=$(echo "${raw_output}" | jq -r '.manifests[].platform.architecture' | head -n 1)
echo "get_image_labels: First architecture found: ${first_arch}"


local image_labels
if ! image_labels=$(retry skopeo inspect --override-arch="${first_arch}" --no-tags docker://"${image}"); then
echo "get_image_labels: failed to inspect the image" >&2
exit 1
Expand Down Expand Up @@ -1924,3 +1920,30 @@ get_image_mirror_list() {

printf "%s\n" "${mirrors[@]}" | sort -u
}

# Fetch first architecture for the image
get_first_arch() {
local image="$1"
local raw_output
local arch

if ! raw_output=$(retry skopeo inspect --raw "docker://${image}"); then
echo "get_first_arch: Error fetching raw manifest for ${image}" >&2
exit 1
fi

# Fetch the architecture from the first entry in the Manifest List
arch=$(echo "${raw_output}" | jq -r '.manifests[0].platform.architecture // empty')

# If arch is empty try fetching arch for Signle Arch Manifest
if [[ -z "${arch}" ]]; then
arch=$(echo "${raw_output}" | jq -r '.Architecture // empty')
fi

if [[ -z "$arch" || "$arch" == "null" ]]; then
echo "get_first_arch: No architecture found in .manifests[0] for ${image}" >&2
exit 1
fi

echo "$arch"
}
58 changes: 50 additions & 8 deletions unittests_bash/test_utils.bats
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ EOF

return 0

elif [[ $1 == "inspect" && $2 == "--raw" && $3 == "docker://registry/image@valid-url" ]]; then
echo '{"manifests":[{"platform":{"architecture":"arm64"}},{"platform":{"architecture":"amd64"}}]}'
return 0

# Mock for single-arch manifest
elif [[ $1 == "inspect" && $3 == "docker://registry/image-manifest@valid" ]]; then
echo '{"Name": "quay.io/ashwkuma/ashwkuma-rhoai","Architecture": "ppc64le","Os": "linux"}'
return 0

# Some skopeo commands fail
else
echo 'Unrecognized call to mock skopeo'
Expand Down Expand Up @@ -827,14 +836,12 @@ EOF
@test "Get Image Labels: registry/image-manifest:tag@invalid" {
run get_image_labels registry/image-manifest:tag@invalid

local EXPECTED_LINE_1="get_image_labels: First architecture found:"
local EXPECTED_LINE_2="get_image_labels: failed to inspect the image"
local EXPECTED_ERROR_LINE="Invalid numeric literal at line 1, column 13"
local EXPECTED_LINE_1="get_first_arch: Error fetching raw manifest for registry/image-manifest@invalid"
local EXPECTED_LINE_2="get_image_labels: architecture could not be determined for registry/image-manifest@invalid"

[[ "$status" -eq 1 \
&& "${output}" == *"${EXPECTED_LINE_1}"* \
&& "${output}" == *"${EXPECTED_LINE_2}"* \
&& "${output}" == *"${EXPECTED_ERROR_LINE}"* ]]
&& "${output}" == *"${EXPECTED_LINE_2}"* ]]
}

@test "Get relatedImages from operator bundle: valid-operator-bundle-1" {
Expand Down Expand Up @@ -1505,12 +1512,12 @@ EOF

}

@test "Retry Get Image Labels: registry/image:tag@invalid-url" {
@test "Retry Get Image Labels: registry/image-manifest@valid" {
RETRY_COUNT=1
RETRY_INTERVAL=1
retry_output=$(get_retry_expected_output)
run get_image_labels registry/image:tag@invalid-url
EXPECTED_RESPONSE=$(echo -e -n "get_image_labels: First architecture found: \n${retry_output}get_image_labels: failed to inspect the image")
run get_image_labels registry/image-manifest@valid
EXPECTED_RESPONSE=$(echo -e -n "get_image_labels: First architecture found: ppc64le\n${retry_output}get_image_labels: failed to inspect the image")

[[ "${output}" == *"${EXPECTED_RESPONSE}"* && "$status" -eq 1 ]]
}
Expand Down Expand Up @@ -2150,3 +2157,38 @@ EOF
EXPECTED_RESPONSE=$(echo 'brew.registry.redhat.io/rh-osbs/salami-operator-bundle*quay.io/salami/operator-bundle'| tr '*' '\n')
[[ "${EXPECTED_RESPONSE}" = "${output}" && "${status}" -eq 0 ]]
}

@test "Get first arch: multi-arch index (registry/image@valid-url)" {
run get_first_arch "registry/image@valid-url"

[ "$status" -eq 0 ]|| { echo "Status was $status. Output: $output"; return 1; }
[[ "${output}" == *"arm64"* ]]
}

@test "Get first arch: single-arch manifest (registry/image-manifest@valid)" {
run get_first_arch "registry/image-manifest@valid"

[ "$status" -eq 0 ]
[[ "${output}" == *"ppc64le"* ]]
}

@test "Get first arch: invalid image" {
run get_first_arch "registry/non-existent@image"

[ "$status" -eq 1 ]
[[ "$output" == *"get_first_arch: Error fetching raw manifest"* ]]
}
@test "Retry get_first_arch: registry/invalid-image:lates" {
RETRY_COUNT=1
RETRY_INTERVAL=1

retry_output=$(get_retry_expected_output)

run get_first_arch "registry/invalid-image:latest"

local EXPECTED_FINAL_ERROR="get_first_arch: Error fetching raw manifest for ${image}"
EXPECTED_RESPONSE=$(echo -e "${retry_output}\n${FINAL_ERROR}")
echo "$EXPECTED_RESPONSE"
[[ "${output}" == *"${EXPECTED_RESPONSE}"* && "$status" -eq 1 ]]

}