Best Python code snippet using lisa_python
BVT-VERIFY-VHD-PREREQUISITES.py
Source:BVT-VERIFY-VHD-PREREQUISITES.py
...23 else:24 RunLog.info("Defaults targetpw is not present in /etc/sudoers")25 print(distro+"_TEST_SUDOERS_VERIFICATION_SUCCESS")26 return True27def verify_grub(distro):28 import os.path29 RunLog.info("Checking console=ttyS0 rootdelay=300..")30 if distro == "UBUNTU":31 grub_out = Run("cat /etc/default/grub")32 if distro == "SUSE":33 if os.path.exists("/boot/grub2/grub.cfg"):34 grub_out = Run("cat /boot/grub2/grub.cfg")35 elif os.path.exists("/boot/grub/grub.conf"):36 grub_out = Run("cat /boot/grub/grub.conf")37 else:38 RunLog.error("Unable to locate grub file")39 print(distro+"_TEST_GRUB_VERIFICATION_FAIL")40 return False41 if distro == "CENTOS" or distro == "ORACLELINUX" or distro == "REDHAT" or distro == "SLES" or distro == "FEDORA":42 if os.path.isfile("/boot/grub2/grub.cfg"):43 RunLog.info("Getting Contents of /boot/grub2/grub.cfg")44 grub_out = Run("cat /boot/grub2/grub.cfg")45 elif os.path.isfile("/boot/grub/menu.lst"):46 RunLog.info("Getting Contents of /boot/grub/menu.lst")47 grub_out = Run("cat /boot/grub/menu.lst")48 else:49 RunLog.error("Unable to locate grub file")50 print(distro+"_TEST_GRUB_VERIFICATION_FAIL")51 return False52 if distro == "COREOS":53 #in core os we don't have access to boot partition54 grub_out = Run("dmesg")55 if "console=ttyS0" in grub_out and "rootdelay=300" in grub_out and "libata.atapi_enabled=0" not in grub_out and "reserve=0x1f0,0x8" not in grub_out:56 if distro == "CENTOS" or distro == "ORACLELINUX" or distro == "REDHAT":57 # check numa=off in grub for CentOS 6.x and Oracle Linux 6.x58 version_release = Run("cat /etc/system-release | grep -Eo '[0-9].?[0-9]?' | head -1 | tr -d '\n'")59 if float(version_release) < 6.6:60 if "numa=off" in grub_out:61 print(distro+"_TEST_GRUB_VERIFICATION_SUCCESS")62 else:63 RunLog.error("numa=off not present in etc/default/grub")64 print(distro+"_TEST_GRUB_VERIFICATION_FAIL")65 else:66 print(distro+"_TEST_GRUB_VERIFICATION_SUCCESS")67 else:68 print(distro+"_TEST_GRUB_VERIFICATION_SUCCESS")69 return True70 else:71 print(distro+"_TEST_GRUB_VERIFICATION_FAIL")72 if "console=ttyS0" not in grub_out:73 RunLog.error("console=ttyS0 not present")74 if "rootdelay=300" not in grub_out:75 RunLog.error("rootdelay=300 not present")76 if "libata.atapi_enabled=0" in grub_out:77 RunLog.error("libata.atapi_enabled=0 is present")78 if "reserve=0x1f0,0x8" in grub_out:79 RunLog.error("reserve=0x1f0,0x8 is present")80 return False81def verify_network_manager(distro):82 RunLog.info("Verifying that network manager is not installed")83 n_out = Run ("rpm -q NetworkManager")84 if "is not installed" in n_out:85 RunLog.info("Network Manager is not installed")86 print(distro+"_TEST_NETWORK_MANAGER_NOT_INSTALLED")87 return True88 else:89 # NetworkManager package no longer conflicts with the wwagent on CentOS 7.0+ and Oracle Linux 7.0+90 if distro == "CENTOS" or distro == "ORACLELINUX" or distro == "REDHAT":91 version_release = Run("cat /etc/system-release | grep -Eo '[0-9].?[0-9]?' | head -1 | tr -d '\n'")92 if float(version_release) < 7.0:93 RunLog.error("Network Manager is installed")94 print(distro+"_TEST_NETWORK_MANAGER_INSTALLED")95 return False96 else:97 RunLog.info("Network Manager is installed but not confict with waagent.")98 print(distro+"_TEST_NETWORK_MANAGER_NOT_INSTALLED")99 return True100 else:101 RunLog.error("Network Manager is installed")102 print(distro+"_TEST_NETWORK_MANAGER_INSTALLED")103 return False104def verify_network_file_in_sysconfig(distro):105 import os.path106 RunLog.info("Checking if network file exists in /etc/sysconfig")107 if distro == "CENTOS" or distro == "ORACLELINUX" or distro == "REDHAT" or distro == "FEDORA":108 if os.path.isfile("/etc/sysconfig/network"):109 RunLog.info("File Exists.")110 n_out = Run("cat /etc/sysconfig/network")111 if "networking=yes".upper() in n_out.upper():112 RunLog.info("NETWORKING=yes present in network file")113 print(distro+"_TEST_NETWORK_FILE_SUCCESS")114 return True115 else:116 RunLog.error("NETWORKING=yes not present in network file")117 print(distro+"_TEST_NETWORK_FILE_ERROR")118 return False119 else:120 RunLog.error("File not present")121 print(distro+"_TEST_NETWORK_FILE_ERROR")122 return False123def verify_ifcfg_eth0(distro):124 RunLog.info("Verifying contents of ifcfg-eth0 file")125 if distro == "CENTOS" or distro == "ORACLELINUX" or distro == "REDHAT" or distro == "FEDORA":126 i_out = Run("cat /etc/sysconfig/network-scripts/ifcfg-eth0")127 i_out = i_out.replace('"', '')128 #if "DEVICE=eth0" in i_out and "ONBOOT=yes" in i_out and "BOOTPROTO=dhcp" in i_out and "DHCP=yes" in i_out:129 if "DEVICE=eth0" in i_out and "ONBOOT=yes" in i_out and "BOOTPROTO=dhcp" in i_out :130 RunLog.info("all required parameters exists.")131 print(distro+"_TEST_IFCFG_ETH0_FILE_SUCCESS")132 return True133 else:134 if "DEVICE=eth0" not in i_out:135 RunLog.error("DEVICE=eth0 not present in ifcfg-eth0")136 if "ONBOOT=yes" not in i_out:137 RunLog.error("ONBOOT=yes not present in ifcfg-eth0")138 if "BOOTPROTO=dhcp" not in i_out:139 RunLog.error("BOOTPROTO=dhcp not present in ifcfg-eth0")140 #if "DHCP=yes" not in i_out:141 # RunLog.error("DHCP=yes not present in ifcfg-eth0")142 print(distro+"_TEST_IFCFG_ETH0_FILE_ERROR")143 return False144def verify_udev_rules(distro):145 import os.path146 RunLog.info("Verifying if udev rules are moved to /var/lib/waagent/")147 if distro == "CENTOS" or distro == "ORACLELINUX" or distro == "REDHAT" or distro == "FEDORA":148 if not os.path.isfile("/lib/udev/rules.d/75-persistent-net-generator.rules") and not os.path.isfile("/etc/udev/rules.d/70-persistent-net.rules"):149 RunLog.info("rules are moved.")150 print(distro+"_TEST_UDEV_RULES_SUCCESS")151 return True152 else:153 if os.path.isfile("/lib/udev/rules.d/75-persistent-net-generator.rules"):154 RunLog.error("/lib/udev/rules.d/75-persistent-net-generator.rules file present")155 if os.path.isfile("/etc/udev/rules.d/70-persistent-net.rules"):156 RunLog.error("/etc/udev/rules.d/70-persistent-net.rules file present")157 print(distro+"_TEST_UDEV_RULES_ERROR")158 return False159 if distro == "COREOS":160 if not os.path.isfile("/usr/lib64/udev/rules.d/75-persistent-net-generator.rules") and not os.path.isfile("/usr/lib64/udev/rules.d/70-persistent-net.rules"):161 RunLog.info("rules are moved.")162 print(distro+"_TEST_UDEV_RULES_SUCCESS")163 return True164 else:165 if os.path.isfile("/usr/lib64/udev/rules.d/75-persistent-net-generator.rules"):166 RunLog.error("/usr/lib64/udev/rules.d/75-persistent-net-generator.rules file present")167 if os.path.isfile("/usr/lib64/udev/rules.d/70-persistent-net.rules"):168 RunLog.error("/usr/lib64/udev/rules.d/70-persistent-net.rules file present")169 print(distro+"_TEST_UDEV_RULES_ERROR")170 return False171if distro == "UBUNTU":172 RunLog.info("DISTRO PROVIDED : "+distro)173 #Test 1 : verify that hv-kvp-daemon-init is installed or not, it's optional not strict.174 RunLog.info("Checking if hv-kvp-daemon-init is installed or not..")175 #kvp_install_status = Run("dpkg -s hv-kvp-daemon-init")176 kvp_install_status = Run("pgrep -lf hv_kvp_daemon")177 matchCount = 0178 if "hv_kvp_daemon" in kvp_install_status:179 matchCount = matchCount + 1180 if matchCount == 1:181 print(distro+"_TEST_KVP_INSTALLED")182 else:183 print(distro+"_TEST_KVP_NOT_INSTALLED")184 #Test 2 : Make sure that repositories are installed.185 RunLog.info("Checking if repositories are installed or not..")186 repository_out = Run("apt-get update")187 if "security.ubuntu.com" in repository_out and "azure.archive.ubuntu.com" in repository_out and "Hit" in repository_out:188 print(distro+"_TEST_REPOSITORIES_AVAILABLE")189 else:190 print(distro+"_TEST_REPOSITORIES_ERROR")191 #Test 3 : Make sure to have console=ttyS0 rootdelay=300 in /etc/default/grub.192 result = verify_grub(distro)193 #Test 4 : Make sure that default targetpw is commented in /etc/sudoers file.194 result = verify_default_targetpw(distro)195if distro == "DEBIAN":196 RunLog.info("DISTRO PROVIDED : "+distro)197 #Test 1 : verify that hv-kvp-daemon-init is installed or not, it's optional not strict.198 RunLog.info("Checking if hv-kvp-daemon-init is installed or not..")199 kvp_install_status = Run("pgrep -lf hv_kvp_daemon")200 matchCount = 0201 if "hv_kvp_daemon" in kvp_install_status:202 matchCount = matchCount + 1203 if matchCount == 1:204 print(distro+"_TEST_KVP_INSTALLED")205 else:206 print(distro+"_TEST_KVP_NOT_INSTALLED")207 #Test 2 : Make sure that repositories are installed.208 RunLog.info("Checking if repositories are installed or not..")209 repository_out = Run("apt-get update")210 if ( "deb.debian.org" in repository_out or "debian-archive.trafficmanager.net" in repository_out ) and "Hit" in repository_out:211 print(distro+"_TEST_REPOSITORIES_AVAILABLE")212 else:213 print(distro+"_TEST_REPOSITORIES_ERROR")214 #Test 3 : Make sure that default targetpw is commented in /etc/sudoers file.215 result = verify_default_targetpw(distro)216 217if distro == "SUSE":218 #Make sure that distro contains Cloud specific repositories219 RunLog.info("Verifying Cloud specific repositories")220 Oss_repo_count = Run("zypper lr | grep -vi debug | grep -vi non | grep Oss | wc -l | tr -d '\n'")221 Update_repo_count = Run("zypper lr | grep -vi debug | grep -vi non | grep Update | wc -l | tr -d '\n'")222 Oss_repo_enable_refresh = Run("zypper lr | grep -vi debug | grep -vi non | grep Oss | grep -o Yes | wc -l | tr -d '\n'")223 Update_repo_enable_refresh = Run("zypper lr | grep -vi debug | grep -vi non | grep Update | grep -o Yes | wc -l | tr -d '\n'")224 if int(Oss_repo_count) > 0 and int(Update_repo_count) > 0:225 RunLog.info("All expected repositories are present")226 if int(Oss_repo_enable_refresh) >= 2 and int(Update_repo_enable_refresh) >= 2:227 RunLog.info("All expected repositories are enabled and refreshed")228 print(distro+"_TEST_REPOSITORIES_AVAILABLE")229 else:230 RunLog.error("One or more expected repositories are not enabled/refreshed.")231 print(distro+"_TEST_REPOSITORIES_ERROR")232 else:233 RunLog.error("One or more expected repositories are not present")234 print(distro+"_TEST_REPOSITORIES_ERROR")235 236 #Verify Grub237 result = verify_grub(distro)238 #Test : Make sure that default targetpw is commented in /etc/sudoers file.239 result = verify_default_targetpw(distro)240if distro == "CENTOS":241 #Test 1 : Make sure Network Manager is not installed242 result = verify_network_manager(distro)243 result = verify_network_file_in_sysconfig(distro)244 result = verify_ifcfg_eth0(distro)245 result = verify_udev_rules(distro)246 #Verify repositories247 r_out = Run("yum repolist")248 if "base" in r_out and "updates" in r_out:249 RunLog.info("Expected repositories are present")250 print(distro+"_TEST_REPOSITORIES_AVAILABLE")251 else:252 if "base" not in r_out:253 RunLog.error("Base repository not present")254 if "updates" not in r_out:255 RunLog.error("Updates repository not present")256 print(distro+"_TEST_REPOSITORIES_ERROR")257 #Verify etc/yum.conf258 y_out = Run("cat /etc/yum.conf")259 # check http_caching=packages in yum.conf for CentOS 6.x260 version_release = Run("cat /etc/system-release | grep -Eo '[0-9].?[0-9]?' | head -1 | tr -d '\n'")261 if float(version_release) < 6.6:262 if "http_caching=packages" in y_out:263 RunLog.info("http_caching=packages present in /etc/yum.conf")264 print(distro+"_TEST_YUM_CONF_SUCCESS")265 else:266 RunLog.error("http_caching=packages not present in /etc/yum.conf")267 print(distro+"_TEST_YUM_CONF_ERROR")268 else:269 print(distro+"_TEST_YUM_CONF_SUCCESS")270 result = verify_grub(distro)271if distro == "REDHAT" or distro == "FEDORA":272 #Test 1 : Make sure Network Manager is not installed273 result = verify_default_targetpw(distro)274 result = verify_network_manager(distro)275 result = verify_network_file_in_sysconfig(distro)276 result = verify_ifcfg_eth0(distro)277 result = verify_udev_rules(distro)278 #Verify repositories279 r_out = Run("yum repolist")280 if "base" in r_out and "updates" in r_out:281 RunLog.info("Expected repositories are present")282 print(distro+"_TEST_REPOSITORIES_AVAILABLE")283 else:284 if "base" not in r_out:285 RunLog.error("Base repository not present")286 if "updates" not in r_out:287 RunLog.error("Updates repository not present")288 print(distro+"_TEST_REPOSITORIES_ERROR")289 if distro == "REDHAT":290 ra_out = Run("yum repolist all | grep 'rhui-rhel-' | wc -l")291 if(ra_out > 5):292 RunLog.info("yum repolist all status: Success, repo count = %s", ra_out)293 print(distro+"_TEST_RHUIREPOSITORIES_AVAILABLE")294 else:295 RunLog.error("yum repolist all status: Fail, repo count = %s", ra_out)296 print(distro+"_TEST_RHUIREPOSITORIES_ERROR")297 #Verify etc/yum.conf298 version_release = Run("cat /etc/system-release | grep -Eo '[0-9].?[0-9]?' | head -1 | tr -d '\n'")299 if float(version_release) < 6.6:300 if "http_caching=packages" in y_out:301 RunLog.info("http_caching=packages present in /etc/yum.conf")302 print(distro+"_TEST_YUM_CONF_SUCCESS")303 else:304 RunLog.error("http_caching=packages not present in /etc/yum.conf")305 print(distro+"_TEST_YUM_CONF_ERROR")306 else:307 print(distro+"_TEST_YUM_CONF_SUCCESS")308 result = verify_grub(distro)309if distro == "ORACLELINUX":310 #Test 1 : Make sure Network Manager is not installed311 result = verify_network_manager(distro)312 result = verify_network_file_in_sysconfig(distro)313 result = verify_ifcfg_eth0(distro)314 result = verify_udev_rules(distro)315 #Verify repositories316 r_out = Run("yum repolist")317 if "latest" in r_out:318 RunLog.info("Expected latest repositories are present")319 print(distro+"_TEST_REPOSITORIES_AVAILABLE")320 else:321 RunLog.error("Expected latest repository not present")322 print(distro+"_TEST_REPOSITORIES_ERROR")323 # no need to verify yum.conf since http_caching is not required for Oracle Linux.324 result = verify_grub(distro)325if distro == "SLES":326 #Verify Repositories..327 r_out = Run("zypper lr")328 if "Pool" in r_out and "Updates" in r_out:329 RunLog.info("All expected repositories are present")330 RunLog.info("All expected repositories are enabled and refreshed")331 print(distro+"_TEST_REPOSITORIES_AVAILABLE")332 else:333 RunLog.error("One or more expected repositories are not present")334 print(distro+"_TEST_REPOSITORIES_ERROR")335 #Verify Grub336 result = verify_grub(distro)337 #Verify sudoers file338 result = verify_default_targetpw(distro)339 #Vefiry : It is recommended that you set /etc/sysconfig/network/dhcp or equivalent from DHCLIENT_SET_HOSTNAME="yes" to DHCLIENT_SET_HOSTNAME="no"340 RunLog.info('Checking recommended setting if DHCLIENT_SET_HOSTNAME="no" present in /etc/sysconfig/network/dhcp')341 d_out = Run("cat /etc/sysconfig/network/dhcp")342 if 'DHCLIENT_SET_HOSTNAME="no"' in d_out:343 RunLog.info('DHCLIENT_SET_HOSTNAME="no" present in /etc/sysconfig/network/dhcp')344 else:345 RunLog.info("DHCLIENT_SET_HOSTNAME='no' not present in /etc/sysconfig/network/dhcp, it's not strict.")346if distro == "COREOS":347 #"rootdelay=300" has issues with CoreOS which causes extra long boot time348 #result = verify_grub(distro)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!