From 5e78430a1e851087110e69d61fe1d695d19f2481 Mon Sep 17 00:00:00 2001 From: xxiong2021 <89631515+xxiong2021@users.noreply.github.com> Date: Tue, 27 May 2025 21:09:40 +0800 Subject: [PATCH 1/6] Update deprovision_package.sh --- scripts/deprovision_package.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/scripts/deprovision_package.sh b/scripts/deprovision_package.sh index a0b5f0d..1dae868 100755 --- a/scripts/deprovision_package.sh +++ b/scripts/deprovision_package.sh @@ -4,6 +4,7 @@ if [ x$1 != x ];then func=$1 else echo "Must specify function: deprovision/verify/all. Exit!" + echo "Usage: $0 [username]" exit 1 fi @@ -11,6 +12,7 @@ if [ x$2 != x ];then type=$2 else echo "Must specify type: cloudinit/cloudinit_wala/wala. Exit!" + echo "Usage: $0 [username]" exit 1 fi @@ -53,6 +55,11 @@ function deprovision_wala() { rm -f /etc/ssh/sshd_config.d/50-cloud-init.conf } +function deprovision_azure_vm_utils() { + #rpm -e azure-vm-utils > /dev/null 2>&1 + return 1 +} + function deprovision_cloudinit_wala() { systemctl stop waagent systemctl enable waagent > /dev/null 2>&1 @@ -503,6 +510,19 @@ function verify_cloudinit() { exit $rflag } +function verify_azure_vm_utils() { + # rpm -q azure-vm-utils > /dev/null + # if [ $? -eq 0 ];then + # format_echo "Verify azure-vm-utils removed: FAIL"; + # ret=1 + # else + # format_echo "Verify azure-vm-utils removed: PASS" + # ret=0 + # fi + # return $ret + return 0 +} + case $type in cloudinit) function deprovision() { deprovision_cloudinit; } @@ -527,8 +547,14 @@ kernel) function verify() { verify_wala; } fi ;; +azure-vm-utils) +# azure-vm-utils + function deprovision() { deprovision_azure_vm_utils; } + function verify() { verify_azure_vm_utils; } +;; *) echo "$type: unsupported deprovision type! Exit." + echo "Usage: $0 [username]" exit 1 ;; esac From 03861cc8caa416c45cc4e499718a0bf3fd23f0c2 Mon Sep 17 00:00:00 2001 From: xxiong2021 <89631515+xxiong2021@users.noreply.github.com> Date: Tue, 27 May 2025 21:13:35 +0800 Subject: [PATCH 2/6] Create test_functional_azure-vm-utils.py --- tests/test_functional_azure-vm-utils.py | 120 ++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 tests/test_functional_azure-vm-utils.py diff --git a/tests/test_functional_azure-vm-utils.py b/tests/test_functional_azure-vm-utils.py new file mode 100644 index 0000000..48c5928 --- /dev/null +++ b/tests/test_functional_azure-vm-utils.py @@ -0,0 +1,120 @@ +import os +import re +import time +import yaml +import json +from avocado import Test +from avocado import main +from avocado_cloud.app import Setup +# from avocado_cloud.app.azure import AzureAccount +# from avocado_cloud.app.azure import AzureNIC +from avocado_cloud.app.azure import AzurePublicIP +from avocado_cloud.app.azure import AzureNicIpConfig +# from avocado_cloud.app.azure import AzureImage +from distutils.version import LooseVersion +from avocado_cloud.utils import utils_azure + +# import requests +from avocado_cloud.utils.utils_azure import command + +BASEPATH = os.path.abspath(__file__ + "/../../../") + +class Azure_vm_utilsTest(Test): + + def _postfix(self): + from datetime import datetime + return datetime.strftime(datetime.now(), "%Y%m%d%H%M%S") + + def setUp(self): + #self.casestatus = False + self.cloud = Setup(self.params, self.name) + self.vm = self.cloud.vm # Access the VM created during setup + authentication = "publickey" + self.session = self.cloud.init_vm(authentication=authentication) + if self.vm.exists(): + self.vm.delete(wait=True) + file_path = '/root/azure-vm-utils/result.txt' + if os.path.exists(file_path): + os.remove(file_path) + + key = "/root/.ssh/id_rsa.pub" + self.vm.ssh_key_value = "{}".format(key) + self.vm.authentication_type = "ssh" + #self.vm.vm_name += "-utils" + # self.vm.os_disk_name += "-new" + + #osdisk = self.vm.properties["storageProfile"]["osDisk"]["vhd"]["uri"] + #self.vm.delete() + #self.vm.image = osdisk + + self.publicip_name = self.vm.vm_name + "PublicIP" + self.vm.os_disk_name += "-utils" + # self.vm.subnet += "-utils" + + self.vm.create(wait=True) + self.session.connect(authentication="publickey") + self.assertEqual(self.vm.vm_username, + self.session.cmd_output("whoami"), + "Fail to login with publickey") + self.assertIn( + "%s ALL=(ALL) NOPASSWD:ALL" % self.vm.vm_username, + self.session.cmd_output( + "sudo cat /etc/sudoers.d/90-cloud-init-users"), + "No sudo privilege") + + @property + def _postfix(self): + from datetime import datetime + return datetime.strftime(datetime.now(), "%Y%m%d%H%M%S") + + def test_selftest_without_imds_symlink_validation(self): + """ + :avocado: tags=tier1,azure_vm_utils + """ + try: + #publicip_name = self.vm.vm_name + "PublicIP" + publicip_name = self.publicip_name + cmd = ' az network public-ip show --name {} --resource-group "{}" --query "ipAddress" --output tsv'.format(publicip_name, self.vm.resource_group) + ret = command(cmd) + public_ip = ret.stdout.strip() + self.log.info("public_ip: %s", public_ip) + + # Upload the selftest.py to the remote VM + upload_command = 'scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa /root/azure-vm-utils/selftest/selftest.py azureuser@{}:/home/azureuser'.format(public_ip) + command(upload_command) + + check_command = 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa azureuser@{} -- ls /home/azureuser/selftest.py;ls /home/azureuser;rpm -qa azure-vm-utils* '.format(public_ip) + check_res = command(check_command) + self.log.info("check_result: %s", check_res) + + # Run the selftest.py script on the VM + run_command = 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa azureuser@{} -- sudo /home/azureuser/selftest.py --skip-imds-validation --skip-symlink-validation > /root/azure-vm-utils/result.txt 2>&1'.format(public_ip) + command(run_command) + + # Get the last line of the result + result_command = "tail -n 1 /root/azure-vm-utils/result.txt | awk '{print $NF}'" + ret = command(result_command) + + + # Check if the result was successful + if ret.stdout.strip() == "success!": + self.log.info("Self-test completed successfully.") + self.vm.delete(wait=False) + #self.casestatus = True + return True + else: + self.log.error("Self-test failed: {}".format(ret.stdout)) + self.vm.delete(wait=False) + return False + + except Exception as e: + self.log.error("An error occurred: {}".format(str(e))) + return False + + def tearDown(self): + self.vm.delete(wait=False) + # del_cmd = ' az disk delete --name {} --resource-group "{}" --yes '.format(self.vm.os_disk_name, self.vm.resource_group) + # command(del_cmd) + +# if __name__ == "__main__": +# main() From 2597fc7879619159282c59318320b979daf02786 Mon Sep 17 00:00:00 2001 From: xxiong2021 <89631515+xxiong2021@users.noreply.github.com> Date: Tue, 27 May 2025 21:15:11 +0800 Subject: [PATCH 3/6] Rename tests/test_functional_azure-vm-utils.py to tests/azure/test_functional_azure-vm-utils.py --- tests/{ => azure}/test_functional_azure-vm-utils.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{ => azure}/test_functional_azure-vm-utils.py (100%) diff --git a/tests/test_functional_azure-vm-utils.py b/tests/azure/test_functional_azure-vm-utils.py similarity index 100% rename from tests/test_functional_azure-vm-utils.py rename to tests/azure/test_functional_azure-vm-utils.py From 139b6c418e4045ade8cc92154610b2181eef18f1 Mon Sep 17 00:00:00 2001 From: xxiong2021 <89631515+xxiong2021@users.noreply.github.com> Date: Tue, 27 May 2025 21:17:06 +0800 Subject: [PATCH 4/6] Update test_functional_package_preparation.py --- tests/azure/test_functional_package_preparation.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/azure/test_functional_package_preparation.py b/tests/azure/test_functional_package_preparation.py index 3c6ac41..4dc6c61 100644 --- a/tests/azure/test_functional_package_preparation.py +++ b/tests/azure/test_functional_package_preparation.py @@ -191,6 +191,8 @@ def test_package_00_preparation(self): depro_type = "wala" elif "kernel" in pkgname_list: depro_type = "kernel" + elif "azure-vm-utils" in pkgname_list: + depro_type = "azure-vm-utils" else: self.fail("Not supported package(s): {}".format(pkgname_list)) script = "deprovision_package.sh" From d0f0ba6c0bece689ab87811bb5adbe3e2df7d6cd Mon Sep 17 00:00:00 2001 From: xxiong2021 <89631515+xxiong2021@users.noreply.github.com> Date: Tue, 27 May 2025 21:18:49 +0800 Subject: [PATCH 5/6] Create azure_testcases_azure-vm-utils.yaml --- config/azure_testcases_azure-vm-utils.yaml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 config/azure_testcases_azure-vm-utils.yaml diff --git a/config/azure_testcases_azure-vm-utils.yaml b/config/azure_testcases_azure-vm-utils.yaml new file mode 100644 index 0000000..f13fcab --- /dev/null +++ b/config/azure_testcases_azure-vm-utils.yaml @@ -0,0 +1,2 @@ +cases: + test_functional_azure-vm-utils.py:Azure_vm_utilsTest.test_selftest_without_imds_symlink_validation From d73a44607aaf277316610b582d18e3c5cec3db5c Mon Sep 17 00:00:00 2001 From: xxiong2021 <89631515+xxiong2021@users.noreply.github.com> Date: Tue, 27 May 2025 21:32:32 +0800 Subject: [PATCH 6/6] Update test_functional_azure-vm-utils.py --- tests/azure/test_functional_azure-vm-utils.py | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/tests/azure/test_functional_azure-vm-utils.py b/tests/azure/test_functional_azure-vm-utils.py index 48c5928..6568202 100644 --- a/tests/azure/test_functional_azure-vm-utils.py +++ b/tests/azure/test_functional_azure-vm-utils.py @@ -6,25 +6,20 @@ from avocado import Test from avocado import main from avocado_cloud.app import Setup -# from avocado_cloud.app.azure import AzureAccount -# from avocado_cloud.app.azure import AzureNIC from avocado_cloud.app.azure import AzurePublicIP from avocado_cloud.app.azure import AzureNicIpConfig -# from avocado_cloud.app.azure import AzureImage from distutils.version import LooseVersion from avocado_cloud.utils import utils_azure -# import requests from avocado_cloud.utils.utils_azure import command BASEPATH = os.path.abspath(__file__ + "/../../../") class Azure_vm_utilsTest(Test): - def _postfix(self): from datetime import datetime return datetime.strftime(datetime.now(), "%Y%m%d%H%M%S") - + def setUp(self): #self.casestatus = False self.cloud = Setup(self.params, self.name) @@ -40,17 +35,9 @@ def setUp(self): key = "/root/.ssh/id_rsa.pub" self.vm.ssh_key_value = "{}".format(key) self.vm.authentication_type = "ssh" - #self.vm.vm_name += "-utils" - # self.vm.os_disk_name += "-new" - - #osdisk = self.vm.properties["storageProfile"]["osDisk"]["vhd"]["uri"] - #self.vm.delete() - #self.vm.image = osdisk - self.publicip_name = self.vm.vm_name + "PublicIP" self.vm.os_disk_name += "-utils" # self.vm.subnet += "-utils" - self.vm.create(wait=True) self.session.connect(authentication="publickey") self.assertEqual(self.vm.vm_username, @@ -72,7 +59,6 @@ def test_selftest_without_imds_symlink_validation(self): :avocado: tags=tier1,azure_vm_utils """ try: - #publicip_name = self.vm.vm_name + "PublicIP" publicip_name = self.publicip_name cmd = ' az network public-ip show --name {} --resource-group "{}" --query "ipAddress" --output tsv'.format(publicip_name, self.vm.resource_group) ret = command(cmd)