Skip to content

Commit b503ad3

Browse files
committed
refactor: Consolidate package testing logic using case statement
- Replace multiple if statements with a cleaner case statement for package testing - Add support for datacenter-gpu-manager variants in testPkgDownloaded path - Improve code readability and maintainability Signed-off-by: Suraj Deshmukh <suraj.deshmukh@microsoft.com>
1 parent 60ce934 commit b503ad3

1 file changed

Lines changed: 37 additions & 31 deletions

File tree

vhdbuilder/packer/test/linux-vhd-content-test.sh

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -212,25 +212,29 @@ testPackagesInstalled() {
212212
updatePackageVersions "${p}" "${OS}" "${OS_VERSION}"
213213
PACKAGE_DOWNLOAD_URL=""
214214
updatePackageDownloadURL "${p}" "${OS}" "${OS_VERSION}"
215-
if [ "${name}" = "kubernetes-binaries" ]; then
216-
# kubernetes-binaries, namely, kubelet and kubectl are installed in a different way so we test them separately
217-
# Intentionally remove leading 'v' from each element in the array
218-
testKubeBinariesPresent "${PACKAGE_VERSIONS[@]#v}"
219-
continue
220-
fi
221-
if [ "${name}" = "azure-acr-credential-provider" ]; then
222-
# azure-acr-credential-provider is installed in a different way so we test it separately
223-
testAcrCredentialProviderInstalled "$PACKAGE_DOWNLOAD_URL" "${PACKAGE_VERSIONS[@]}"
224-
continue
225-
fi
226-
if [ "${name}" = "kubelet" ]; then
227-
testPkgDownloaded "kubelet" "${PACKAGE_VERSIONS[@]}"
228-
continue
229-
fi
230-
if [ "${name}" = "kubectl" ]; then
231-
testPkgDownloaded "kubectl" "${PACKAGE_VERSIONS[@]}"
232-
continue
233-
fi
215+
case "${name}" in
216+
"kubernetes-binaries")
217+
# kubernetes-binaries, namely, kubelet and kubectl are installed in a different way so we test them separately
218+
# Intentionally remove leading 'v' from each element in the array
219+
testKubeBinariesPresent "${PACKAGE_VERSIONS[@]#v}"
220+
continue
221+
;;
222+
"azure-acr-credential-provider")
223+
# azure-acr-credential-provider is installed in a different way so we test it separately
224+
testAcrCredentialProviderInstalled "$PACKAGE_DOWNLOAD_URL" "${PACKAGE_VERSIONS[@]}"
225+
continue
226+
;;
227+
"kubelet"|\
228+
"kubectl"|\
229+
"datacenter-gpu-manager-4-core"|\
230+
"datacenter-gpu-manager-4-cuda12"|\
231+
"datacenter-gpu-manager-4-proprietary"|\
232+
"datacenter-gpu-manager-4-proprietary-cuda12"|\
233+
"datacenter-gpu-manager-exporter")
234+
testPkgDownloaded "${name}" "${PACKAGE_VERSIONS[@]}"
235+
continue
236+
;;
237+
esac
234238

235239
resolve_packages_source_url
236240
for version in "${PACKAGE_VERSIONS[@]}"; do
@@ -247,9 +251,9 @@ testPackagesInstalled() {
247251
;;
248252
esac
249253
break
250-
254+
251255
fi
252-
# A downloadURL from a package in components.json will look like this:
256+
# A downloadURL from a package in components.json will look like this:
253257
# "https://acs-mirror.azureedge.net/cni-plugins/v${version}/binaries/cni-plugins-linux-${CPU_ARCH}-v${version}.tgz"
254258
# After eval(resolved), downloadURL will look like "https://acs-mirror.azureedge.net/cni-plugins/v0.8.7/binaries/cni-plugins-linux-arm64-v0.8.7.tgz"
255259
eval "downloadURL=${PACKAGE_DOWNLOAD_URL}"
@@ -306,7 +310,7 @@ testPackagesInstalled() {
306310
# Azure China Cloud uses a different proxy but the same path, and we want to verify the package URL
307311
# if defined in control plane, is accessible and has the same file size as the one in the public cloud.
308312
testPackageInAzureChinaCloud() {
309-
# In Azure China Cloud, the proxy server proxies download URL to the storage account URL according to the root path, for example,
313+
# In Azure China Cloud, the proxy server proxies download URL to the storage account URL according to the root path, for example,
310314
# location /kubernetes/ {
311315
# proxy_pass https://kubernetesartifacts.blob.core.chinacloudapi.cn/kubernetes/;
312316
# }
@@ -389,7 +393,7 @@ testImagesPulled() {
389393
downloadURL=$(echo "${imageToBePulled}" | jq .downloadURL -r)
390394
if [ $(echo "${imageToBePulled}" | jq -r '.amd64OnlyVersions // empty') = "null" ]; then
391395
amd64OnlyVersionsStr=""
392-
else
396+
else
393397
amd64OnlyVersionsStr=$(echo "${imageToBePulled}" | jq -r '.amd64OnlyVersions // empty')
394398
fi
395399
declare -a MULTI_ARCH_VERSIONS=()
@@ -660,7 +664,7 @@ testLSMBPF() {
660664
if [ -f /sys/kernel/security/lsm ]; then
661665
current_lsm=$(cat /sys/kernel/security/lsm)
662666
echo "$test: Current LSM modules: $current_lsm"
663-
667+
664668
if echo "$current_lsm" | grep -q "bpf"; then
665669
echo "$test: BPF is present in LSM modules"
666670
else
@@ -767,6 +771,8 @@ testPkgDownloaded() {
767771
for packageVersion in "${packageVersions[@]}"; do
768772
echo "checking package version: $packageVersion ..."
769773
if [ $OS = $UBUNTU_OS_NAME ]; then
774+
# Strip epoch (e.g., 1:4.4.1-1 -> 4.4.1-1)
775+
packageVersion="${packageVersion#*:}"
770776
debFile=$(find "${downloadLocation}" -maxdepth 1 -name "${packageName}_${packageVersion}*" -print -quit 2>/dev/null) || debFile=""
771777
if [ -z "${debFile}" ]; then
772778
err $test "Package ${packageName}_${packageVersion} does not exist, content of downloads dir is $(ls -al ${downloadLocation})"
@@ -1366,7 +1372,7 @@ testCriCtl() {
13661372
# the expectedVersion looks like this, "1.32.0-ubuntu18.04u3", need to extract the version number.
13671373
expectedVersion=$(echo $expectedVersion | cut -d'-' -f1)
13681374
# use command `crictl --version` to get the version
1369-
1375+
13701376
local crictl_version=$(crictl --version)
13711377
# the output of crictl_version looks like this "crictl version 1.32.0", need to extract the version number.
13721378
crictl_version=$(echo $crictl_version | cut -d' ' -f3)
@@ -1394,7 +1400,7 @@ testContainerd() {
13941400
# use command `containerd --version` to get the version
13951401
local containerd_version=$(containerd --version)
13961402
# the output of containerd_version looks like the followings. We need to extract the major.minor.patch version only.
1397-
# For containerd (v1): containerd github.com/containerd/containerd 1.6.26
1403+
# For containerd (v1): containerd github.com/containerd/containerd 1.6.26
13981404
# For containerd (v2): containerd github.com/containerd/containerd/v2 2.0.0
13991405
containerd_version=$(echo $containerd_version | cut -d' ' -f3)
14001406
# The version could be in the format "1.6.24-11-ubuntu1~18.04.1" or "2.0.0-6.azl3" or just "2.0.0", we need to extract the major.minor.patch version only.
@@ -1523,7 +1529,7 @@ testPackageDownloadURLFallbackLogic() {
15231529
echo "PACKAGE_DOWNLOAD_BASE_URL was not set to packages.aks.azure.com"
15241530
err "$test: failed to set PACKAGE_DOWNLOAD_BASE_URL to packages.aks.azure.com"
15251531
fi
1526-
1532+
15271533
# Block the IP on local vm to simulate cluster firewall blocking packages.aks.azure.com and retry test to see output
15281534
echo "127.0.0.1 packages.aks.azure.com" | sudo tee /etc/hosts > /dev/null
15291535

@@ -1538,28 +1544,28 @@ testPackageDownloadURLFallbackLogic() {
15381544

15391545
checkLocaldnsScriptsAndConfigs() {
15401546
local test="checkLocaldnsScriptsAndConfigs"
1541-
1547+
15421548
declare -A localdnsfiles=(
15431549
["/opt/azure/containers/localdns/localdns.sh"]=755
15441550
["/etc/systemd/system/localdns.service"]=644
15451551
["/etc/systemd/system/localdns.service.d/delegate.conf"]=644
15461552
)
1547-
1553+
15481554
for file in "${!localdnsfiles[@]}"; do
15491555
echo "$test: Checking existence of ${file}"
15501556
if [ ! -f "${file}" ]; then
15511557
echo "$test: Localdnsfile - ${file} not found"
15521558
return 1
15531559
fi
1554-
1560+
15551561
echo "$test: Checking permissions of ${file}"
15561562
permissions=$(stat -c "%a" "$file")
15571563
if [ "$permissions" != "${localdnsfiles[$file]}" ]; then
15581564
echo "$test: Localdnsfile $file has incorrect permission. Expected ${localdnsfiles[$file]}, got $permissions"
15591565
return 1
15601566
fi
15611567
done
1562-
1568+
15631569
echo "$test: All localdnsfiles exist with correct permissions"
15641570
return 0
15651571
}

0 commit comments

Comments
 (0)