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 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 diff --git a/tests/azure/test_functional_azure-vm-utils.py b/tests/azure/test_functional_azure-vm-utils.py new file mode 100644 index 0000000..6568202 --- /dev/null +++ b/tests/azure/test_functional_azure-vm-utils.py @@ -0,0 +1,106 @@ +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 AzurePublicIP +from avocado_cloud.app.azure import AzureNicIpConfig +from distutils.version import LooseVersion +from avocado_cloud.utils import utils_azure + +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.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.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() 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"