Skip to content

Commit

Permalink
Merge pull request #371 from asergienk/get_unreleased_bundles_packages
Browse files Browse the repository at this point in the history
feat(CVP-4397): add function to group bundle images by package
  • Loading branch information
yashvardhannanavati authored Feb 25, 2025
2 parents 21e5732 + d3bd4b8 commit 9ecc95a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
30 changes: 30 additions & 0 deletions test/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1008,3 +1008,33 @@ get_bundle_arches() {

echo "$arches"
}

# This function will be used by tasks in tekton-integration-catalog
# Given the output of 'opm render $fbc_fragment, and bundle images, this function groups bundle images by package
# It returns a map in the format {"package1":["bundleImage1","bundleImage2"],"package2":["bundleImage3"]}
group_bundle_images_by_package() {
local RENDER_OUT_FBC="$1"
local BUNDLE_IMAGES="$2"
local package_image_map

# Validate input parameters
if [[ -z "$RENDER_OUT_FBC" || -z "$BUNDLE_IMAGES" ]]; then
echo "group_bundle_images_by_package: Invalid input. Usage: group_bundle_images_by_package <RENDER_OUT_FBC> <BUNDLE_IMAGES>" >&2
exit 2
fi

# Group bundle images by package
package_image_map=$(echo "$RENDER_OUT_FBC" | tr -d '\000-\031' | jq -cs \
--argjson bundles "$(printf '%s\n' "${BUNDLE_IMAGES[@]}" | jq -R . | jq -s .)" \
'[.[] | select(.schema == "olm.bundle" and (.image as $img | $bundles | index($img) != null))] |
group_by(.package) |
map({(.[0].package): [.[].image]}) | add')

# Check if package_image_map is empty or null
if [[ -z "$package_image_map" || "$package_image_map" == "null" || "$package_image_map" == "{}" ]]; then
echo "group_bundle_images_by_package: No matching packages found for the provided bundle images." >&2
exit 1
fi

echo "$package_image_map"
}
72 changes: 72 additions & 0 deletions unittests_bash/test_utils.bats
Original file line number Diff line number Diff line change
Expand Up @@ -810,3 +810,75 @@ EOF
EXPECTED_RESPONSE="get_bundle_arches: Error: No architectures found for bundle image."
[[ "${EXPECTED_RESPONSE}" = "${output}" && "$status" -eq 1 ]]
}

@test "Group bundle images by package: two packages" {
BUNDLE_IMAGES=$(echo "registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:4f100135ccbfc726f4b1887703ef7a08453b48c202ba04c0fb7382f0fec637db registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:5a75810bdebb97c63cad1d25fe0399ed189b558b50ee6dc1cb61f75f9116aa89" | tr ' ' '\n')
RENDER_OUT_FBC=$(cat <<EOF
{
"schema": "olm.bundle",
"name": "kubevirt-hyperconverged-operator.v4.17.5",
"package": "kubevirt-hyperconverged-v1",
"image": "registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:4f100135ccbfc726f4b1887703ef7a08453b48c202ba04c0fb7382f0fec637db",
"properties": []
}
{
"schema": "olm.bundle",
"name": "kubevirt-hyperconverged-operator.v4.99.0",
"package": "kubevirt-hyperconverged-v2",
"image": "registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:5a75810bdebb97c63cad1d25fe0399ed189b558b50ee6dc1cb61f75f9116aa89",
"properties": []
}
EOF
)
run group_bundle_images_by_package "${RENDER_OUT_FBC}" "${BUNDLE_IMAGES}"
EXPECTED_RESPONSE='{"kubevirt-hyperconverged-v1":["registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:4f100135ccbfc726f4b1887703ef7a08453b48c202ba04c0fb7382f0fec637db"],"kubevirt-hyperconverged-v2":["registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:5a75810bdebb97c63cad1d25fe0399ed189b558b50ee6dc1cb61f75f9116aa89"]}'
[[ "${EXPECTED_RESPONSE}" = "${output}" && "$status" -eq 0 ]]
}

@test "Group bundle images by package: one package" {
BUNDLE_IMAGES=$(echo "registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:4f100135ccbfc726f4b1887703ef7a08453b48c202ba04c0fb7382f0fec637db registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:5a75810bdebb97c63cad1d25fe0399ed189b558b50ee6dc1cb61f75f9116aa89" | tr ' ' '\n')
RENDER_OUT_FBC=$(cat <<EOF
{
"schema": "olm.bundle",
"name": "kubevirt-hyperconverged-operator.v4.17.5",
"package": "kubevirt-hyperconverged-v1",
"image": "registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:4f100135ccbfc726f4b1887703ef7a08453b48c202ba04c0fb7382f0fec637db",
"properties": []
}
{
"schema": "olm.bundle",
"name": "kubevirt-hyperconverged-operator.v4.99.0",
"package": "kubevirt-hyperconverged-v1",
"image": "registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:5a75810bdebb97c63cad1d25fe0399ed189b558b50ee6dc1cb61f75f9116aa89",
"properties": []
}
EOF
)
run group_bundle_images_by_package "${RENDER_OUT_FBC}" "${BUNDLE_IMAGES}"
EXPECTED_RESPONSE='{"kubevirt-hyperconverged-v1":["registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:4f100135ccbfc726f4b1887703ef7a08453b48c202ba04c0fb7382f0fec637db","registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:5a75810bdebb97c63cad1d25fe0399ed189b558b50ee6dc1cb61f75f9116aa89"]}'
[[ "${EXPECTED_RESPONSE}" = "${output}" && "$status" -eq 0 ]]
}

@test "Group bundle images by package: no packages found" {
BUNDLE_IMAGES=$(echo "registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:4f100135ccbfc726f4b1887703ef7a08453b48c202ba04c0fb7382f0fec637db registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:5a75810bdebb97c63cad1d25fe0399ed189b558b50ee6dc1cb61f75f9116aa89" | tr ' ' '\n')
RENDER_OUT_FBC=$(cat <<EOF
{
"schema": "olm.bundle",
"name": "kubevirt-hyperconverged-operator.v4.17.5",
"package": "kubevirt-hyperconverged-v1",
"image": "registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:4f100135ccbfc726f4b1887703ef7a08453b48c202ba04c0fb7382f0feczxcvb",
"properties": []
}
{
"schema": "olm.bundle",
"name": "kubevirt-hyperconverged-operator.v4.99.0",
"package": "kubevirt-hyperconverged-v1",
"image": "registry.redhat.io/container-native-virtualization/hco-bundle-registry@sha256:5a75810bdebb97c63cad1d25fe0399ed189b558b50ee6dc1cb61f75f911asdfg",
"properties": []
}
EOF
)
run group_bundle_images_by_package "${RENDER_OUT_FBC}" "${BUNDLE_IMAGES}"
EXPECTED_RESPONSE="group_bundle_images_by_package: No matching packages found for the provided bundle images."
[[ "${EXPECTED_RESPONSE}" = "${output}" && "$status" -eq 1 ]]
}

0 comments on commit 9ecc95a

Please sign in to comment.