Best Python code snippet using lisa_python
BVT-VERIFY-VHD-PREREQUISITES.py
Source:BVT-VERIFY-VHD-PREREQUISITES.py  
...6parser = argparse.ArgumentParser()7parser.add_argument('-d', '--distro', help='Please mention which distro you are testing', required=True, type = str)8args = parser.parse_args()9distro = args.distro10def verify_default_targetpw(distro):11    RunLog.info("Checking Defaults targetpw is commented or not..")12    if distro == "FreeBSD":13        sudoers_out = Run("cat /usr/local/etc/sudoers")14    else:15        sudoers_out = Run("cat /etc/sudoers")16    if "Defaults targetpw" in sudoers_out:17        if "#Defaults targetpw" in sudoers_out or "# Defaults targetpw" in sudoers_out:18            print(distro+"_TEST_SUDOERS_VERIFICATION_SUCCESS")19            RunLog.info("Defaults targetpw is commented")20            return True21        else:22            RunLog.error("Defaults targetpw is present in /etc sudoers but it is not commented.")23            print(distro+"_TEST_SUDOERS_VERIFICATION_FAIL")24            return False25    else:26        RunLog.info("Defaults targetpw is not present in /etc/sudoers")27        print(distro+"_TEST_SUDOERS_VERIFICATION_SUCCESS")28        return True29def verify_grub(distro):30    import os.path31    RunLog.info("Checking console=ttyS0 rootdelay=300..")32    if distro == "UBUNTU":33        grub_out = Run("cat /etc/default/grub")34    if distro == "SUSE":35        if os.path.exists("/boot/grub2/grub.cfg"):36            grub_out = Run("cat /boot/grub2/grub.cfg")37        elif os.path.exists("/boot/grub/grub.conf"):38            grub_out = Run("cat /boot/grub/grub.conf")39        else:40            RunLog.error("Unable to locate grub file")41            print(distro+"_TEST_GRUB_VERIFICATION_FAIL")42            return False43    if distro == "CENTOS" or distro == "ORACLELINUX" or distro == "REDHAT" or distro == "SLES" or distro == "FEDORA":44        if os.path.isfile("/boot/grub2/grub.cfg"):45            RunLog.info("Getting Contents of /boot/grub2/grub.cfg")46            grub_out = Run("cat /boot/grub2/grub.cfg")47        elif os.path.isfile("/boot/grub/menu.lst"):48            RunLog.info("Getting Contents of /boot/grub/menu.lst")49            grub_out = Run("cat /boot/grub/menu.lst")50        else:51            RunLog.error("Unable to locate grub file")52            print(distro+"_TEST_GRUB_VERIFICATION_FAIL")53            return False54    if distro == "COREOS":55        #in core os we don't have access to boot partition56        grub_out = Run("dmesg")57    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:58        if distro == "CENTOS" or distro == "ORACLELINUX" or distro == "REDHAT":59            # check numa=off in grub for CentOS 6.x and Oracle Linux 6.x60            version_release = Run("cat /etc/system-release | grep -Eo '[0-9].?[0-9]?' | head -1 | tr -d '\n'")61            if float(version_release) < 7.0:62                if "numa=off" in grub_out:63                    print(distro+"_TEST_GRUB_VERIFICATION_SUCCESS")64                else : 65                    RunLog.error("numa=off not present in etc/default/grub")66                    print(distro+"_TEST_GRUB_VERIFICATION_FAIL")67            else:68                print(distro+"_TEST_GRUB_VERIFICATION_SUCCESS")69        else:70            print(distro+"_TEST_GRUB_VERIFICATION_SUCCESS")71            return True72    else:73        print(distro+"_TEST_GRUB_VERIFICATION_FAIL")74        if "console=ttyS0" not in grub_out:75            RunLog.error("console=ttyS0 not present")76        if "rootdelay=300" not in grub_out:77            RunLog.error("rootdelay=300 not present")78        if "libata.atapi_enabled=0" in grub_out:79            RunLog.error("libata.atapi_enabled=0 is present")80        if "reserve=0x1f0,0x8" in grub_out:81            RunLog.error("reserve=0x1f0,0x8 is present")82        return False83def verify_network_manager(distro):84    RunLog.info("Verifying that network manager is not installed")85    n_out = Run ("rpm -q NetworkManager")86    if "is not installed" in n_out:87        RunLog.info("Network Manager is not installed")88        print(distro+"_TEST_NETWORK_MANAGER_NOT_INSTALLED")89        return True90    else:91        # NetworkManager package no longer conflicts with the wwagent on CentOS 7.0+ and Oracle Linux 7.0+92        if distro == "CENTOS" or distro == "ORACLELINUX" or distro == "REDHAT":93            version_release = Run("cat /etc/system-release | grep -Eo '[0-9].?[0-9]?' | head -1 | tr -d '\n'")94            if float(version_release) < 7.0:95                RunLog.error("Network Manager is installed")96                print(distro+"_TEST_NETWORK_MANAGER_INSTALLED")97                return False98            else:99                RunLog.info("Network Manager is installed but not confict with waagent.")100                print(distro+"_TEST_NETWORK_MANAGER_NOT_INSTALLED")101                return True102        else:103            RunLog.error("Network Manager is installed")104            print(distro+"_TEST_NETWORK_MANAGER_INSTALLED")105            return False106def verify_network_file_in_sysconfig(distro):107    import os.path108    RunLog.info("Checking if network file exists in /etc/sysconfig")109    if distro == "CENTOS" or distro == "ORACLELINUX" or distro == "REDHAT" or distro == "FEDORA":110        if os.path.isfile("/etc/sysconfig/network"):111            RunLog.info("File Exists.")112            n_out = Run("cat /etc/sysconfig/network")113            if "networking=yes".upper() in n_out.upper():114                RunLog.info("NETWORKING=yes present in network file")115                print(distro+"_TEST_NETWORK_FILE_SUCCESS")116                return True117            else:118                RunLog.error("NETWORKING=yes not present in network file")119                print(distro+"_TEST_NETWORK_FILE_ERROR")120                return False121        else:122            RunLog.error("File not present")123            print(distro+"_TEST_NETWORK_FILE_ERROR")124            return False125def verify_ifcfg_eth0(distro):126    RunLog.info("Verifying contents of ifcfg-eth0 file")127    if distro == "CENTOS" or distro == "ORACLELINUX" or distro == "REDHAT" or distro == "FEDORA":128        i_out = Run("cat /etc/sysconfig/network-scripts/ifcfg-eth0")129        i_out = i_out.replace('"','')130        #if "DEVICE=eth0" in i_out and "ONBOOT=yes" in i_out and "BOOTPROTO=dhcp" in i_out and "DHCP=yes" in i_out:131        if "DEVICE=eth0" in i_out and "ONBOOT=yes" in i_out and "BOOTPROTO=dhcp" in i_out  :132            RunLog.info("all required parameters exists.")133            print(distro+"_TEST_IFCFG_ETH0_FILE_SUCCESS")134            return True135        else:136            if "DEVICE=eth0" not in i_out:137                RunLog.error("DEVICE=eth0 not present in ifcfg-eth0")138            if "ONBOOT=yes" not in i_out:139                RunLog.error("ONBOOT=yes not present in ifcfg-eth0")140            if "BOOTPROTO=dhcp" not in i_out:141                RunLog.error("BOOTPROTO=dhcp not present in ifcfg-eth0")142            #if "DHCP=yes" not in i_out:143            #    RunLog.error("DHCP=yes not present in ifcfg-eth0")144            print(distro+"_TEST_IFCFG_ETH0_FILE_ERROR")145            return False146def verify_udev_rules(distro):147    import os.path148    RunLog.info("Verifying if udev rules are moved to /var/lib/waagent/")149    if distro == "CENTOS" or distro == "ORACLELINUX" or distro == "REDHAT" or distro == "FEDORA":150        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"):151            RunLog.info("rules are moved.")152            print(distro+"_TEST_UDEV_RULES_SUCCESS")153            return True154        else:155            if os.path.isfile("/lib/udev/rules.d/75-persistent-net-generator.rules"):156                RunLog.error("/lib/udev/rules.d/75-persistent-net-generator.rules file present")157            if os.path.isfile("/etc/udev/rules.d/70-persistent-net.rules"):158                RunLog.error("/etc/udev/rules.d/70-persistent-net.rules file present")159            print(distro+"_TEST_UDEV_RULES_ERROR")160            return False161        if distro == "COREOS":162            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"):163                RunLog.info("rules are moved.")164                print(distro+"_TEST_UDEV_RULES_SUCCESS")165                return True166            else:167                if os.path.isfile("/usr/lib64/udev/rules.d/75-persistent-net-generator.rules"):168                    RunLog.error("/usr/lib64/udev/rules.d/75-persistent-net-generator.rules file present")169                if os.path.isfile("/usr/lib64/udev/rules.d/70-persistent-net.rules"):170                    RunLog.error("/usr/lib64/udev/rules.d/70-persistent-net.rules file present")171                print(distro+"_TEST_UDEV_RULES_ERROR")172                return False173if distro == "UBUNTU":174    RunLog.info("DISTRO PROVIDED : "+distro)175    #Test 1 : verify that hv-kvp-daemon-init is installed or not, it's optional not strict.176    RunLog.info("Checking if hv-kvp-daemon-init is installed or not..")177    #kvp_install_status = Run("dpkg -s hv-kvp-daemon-init")178    kvp_install_status = Run("pgrep -lf hv_kvp_daemon")179    matchCount = 0180    if "hv_kvp_daemon" in kvp_install_status:181        matchCount = matchCount + 1182    if matchCount == 1:183        print(distro+"_TEST_KVP_INSTALLED")184    else:185        print(distro+"_TEST_KVP_NOT_INSTALLED")186    #Test 2 : Make sure that repositories are installed.187    RunLog.info("Checking if repositories are installed or not..")188    repository_out = Run("apt-get update")189    if "security.ubuntu.com" in repository_out and "azure.archive.ubuntu.com" in repository_out and "Hit" in repository_out:190        print(distro+"_TEST_REPOSITORIES_AVAILABLE")191    else:192        print(distro+"_TEST_REPOSITORIES_ERROR")193    #Test 3 : Make sure to have console=ttyS0 rootdelay=300 in /etc/default/grub.194    result = verify_grub(distro)195    #Test 4 : Make sure that default targetpw is commented in /etc/sudoers file.196    result = verify_default_targetpw(distro)197if distro == "DEBIAN":198    RunLog.info("DISTRO PROVIDED : "+distro)199    #Test 1 : verify that hv-kvp-daemon-init is installed or not, it's optional not strict.200    RunLog.info("Checking if hv-kvp-daemon-init is installed or not..")201    kvp_install_status = Run("pgrep -lf hv_kvp_daemon")202    matchCount = 0203    if "hv_kvp_daemon" in kvp_install_status:204        matchCount = matchCount + 1205    if matchCount == 1:206        print(distro+"_TEST_KVP_INSTALLED")207    else:208        print(distro+"_TEST_KVP_NOT_INSTALLED")209    #Test 2 : Make sure that repositories are installed.210    RunLog.info("Checking if repositories are installed or not..")211    repository_out = Run("apt-get update")212    if "security.debian.org" in repository_out and "debian-archive.trafficmanager.net" in repository_out and "Hit" in repository_out:213        print(distro+"_TEST_REPOSITORIES_AVAILABLE")214    else:215        print(distro+"_TEST_REPOSITORIES_ERROR")216    #Test 3 : Make sure that default targetpw is commented in /etc/sudoers file.217    result = verify_default_targetpw(distro)218    219if distro == "SUSE":220    #Make sure that distro contains Cloud specific repositories221    RunLog.info("Verifying Cloud specific repositories")222    Oss_repo_count = Run("zypper lr | grep -vi debug | grep -vi non | grep Oss | wc -l | tr -d '\n'")223    Update_repo_count = Run("zypper lr | grep -vi debug | grep -vi non | grep Update | wc -l | tr -d '\n'")224    Oss_repo_enable_refresh = Run("zypper lr | grep -vi debug | grep -vi non | grep Oss  | grep -o Yes | wc -l | tr -d '\n'")225    Update_repo_enable_refresh = Run("zypper lr | grep -vi debug | grep -vi non | grep Update | grep -o Yes | wc -l | tr -d '\n'")226    if int(Oss_repo_count) > 0 and int(Update_repo_count) > 0:227        RunLog.info("All expected repositories are present")228        if int(Oss_repo_enable_refresh) >= 2 and int(Update_repo_enable_refresh) >= 2:229            RunLog.info("All expected repositories are enabled and refreshed")230            print(distro+"_TEST_REPOSITORIES_AVAILABLE")231        else:232            RunLog.error("One or more expected repositories are not enabled/refreshed.")233            print(distro+"_TEST_REPOSITORIES_ERROR")234    else:235        RunLog.error("One or more expected repositories are not present")236        print(distro+"_TEST_REPOSITORIES_ERROR")237    238    #Verify Grub239    result = verify_grub(distro)240    #Test : Make sure that default targetpw is commented in /etc/sudoers file.241    result = verify_default_targetpw(distro)242if distro == "CENTOS":243    #Test 1 : Make sure Network Manager is not installed244    result = verify_network_manager(distro)245    result = verify_network_file_in_sysconfig(distro)246    result = verify_ifcfg_eth0(distro)247    result = verify_udev_rules(distro)248    #Verify repositories249    r_out = Run("yum repolist")250    if "base" in r_out and "updates" in r_out:251        RunLog.info("Expected repositories are present")252        print(distro+"_TEST_REPOSITORIES_AVAILABLE")253    else:254        if "base" not in r_out:255            RunLog.error("Base repository not present")256        if "updates" not in r_out:257            RunLog.error("Updates repository not present") 258        print(distro+"_TEST_REPOSITORIES_ERROR")259    #Verify etc/yum.conf260    y_out = Run("cat /etc/yum.conf")261    # check http_caching=packages in yum.conf for CentOS 6.x262    version_release = Run("cat /etc/system-release | grep -Eo '[0-9].?[0-9]?' | head -1 | tr -d '\n'")263    if float(version_release) < 7.0:264        if "http_caching=packages" in y_out:265            RunLog.info("http_caching=packages present in /etc/yum.conf")266            print(distro+"_TEST_YUM_CONF_SUCCESS")267        else:268            RunLog.error("http_caching=packages not present in /etc/yum.conf")269            print(distro+"_TEST_YUM_CONF_ERROR")270    else:271        print(distro+"_TEST_YUM_CONF_SUCCESS")272    result = verify_grub(distro)273if distro == "REDHAT" or distro == "FEDORA":274    #Test 1 : Make sure Network Manager is not installed275    result = verify_default_targetpw(distro)276    result = verify_network_manager(distro)277    result = verify_network_file_in_sysconfig(distro)278    result = verify_ifcfg_eth0(distro)279    result = verify_udev_rules(distro)280    #Verify repositories281    r_out = Run("yum repolist")282    if "base" in r_out and "updates" in r_out:283        RunLog.info("Expected repositories are present")284        print(distro+"_TEST_REPOSITORIES_AVAILABLE")285    else:286        if "base" not in r_out:287            RunLog.error("Base repository not present")288        if "updates" not in r_out:289            RunLog.error("Updates repository not present")290            print(distro+"_TEST_REPOSITORIES_ERROR")291    if distro == "REDHAT":292            ra_out = Run("yum repolist all | grep 'rhui-rhel-' | wc -l")293            if(ra_out > 5):294                RunLog.info("yum repolist all status: Success, repo count = %s", ra_out)295                print(distro+"_TEST_RHUIREPOSITORIES_AVAILABLE")296            else:297                RunLog.error("yum repolist all status: Fail, repo count = %s", ra_out)298                print(distro+"_TEST_RHUIREPOSITORIES_ERROR")299    #Verify etc/yum.conf300    y_out = Run("cat /etc/yum.conf")301    if "http_caching=packages" in y_out:302        RunLog.info("http_caching=packages present in /etc/yum.conf")303        print(distro+"_TEST_YUM_CONF_SUCCESS")304    else:305        RunLog.error("http_caching=packages not present in /etc/yum.conf")306        print(distro+"_TEST_YUM_CONF_ERROR")307    result = verify_grub(distro)308if distro == "ORACLELINUX":309    #Test 1 : Make sure Network Manager is not installed310    result = verify_network_manager(distro)311    result = verify_network_file_in_sysconfig(distro)312    result = verify_ifcfg_eth0(distro)313    result = verify_udev_rules(distro)314    #Verify repositories315    r_out = Run("yum repolist")316    if "latest" in r_out:317        RunLog.info("Expected latest repositories are present")318        print(distro+"_TEST_REPOSITORIES_AVAILABLE")319    else:320        RunLog.error("Expected latest repository not present")321        print(distro+"_TEST_REPOSITORIES_ERROR")322    # no need to verify yum.conf since http_caching is not required for Oracle Linux.323    result = verify_grub(distro)324if distro == "SLES":325    #Verify Repositories..326    r_out = Run("zypper lr")327    if "Pool" in r_out and "Updates" in r_out:328        RunLog.info("All expected repositories are present")329        RunLog.info("All expected repositories are enabled and refreshed")330        print(distro+"_TEST_REPOSITORIES_AVAILABLE")331    else:332        RunLog.error("One or more expected repositories are not present")333        print(distro+"_TEST_REPOSITORIES_ERROR")334    #Verify Grub335    result = verify_grub(distro)336    #Verify sudoers file337    result = verify_default_targetpw(distro)338    #Vefiry : It is recommended that you set /etc/sysconfig/network/dhcp or equivalent from DHCLIENT_SET_HOSTNAME="yes" to DHCLIENT_SET_HOSTNAME="no"339    RunLog.info('Checking recommended setting if DHCLIENT_SET_HOSTNAME="no" present in /etc/sysconfig/network/dhcp')340    d_out = Run("cat /etc/sysconfig/network/dhcp")341    if 'DHCLIENT_SET_HOSTNAME="no"' in d_out:342        RunLog.info('DHCLIENT_SET_HOSTNAME="no" present in /etc/sysconfig/network/dhcp')343    else:344        RunLog.info("DHCLIENT_SET_HOSTNAME='no' not present in /etc/sysconfig/network/dhcp, it's not strict.")345if distro == "COREOS":346    #"rootdelay=300" has issues with CoreOS which causes extra long boot time347    #result = verify_grub(distro)348    result = verify_udev_rules(distro)349if distro == "FreeBSD":350    RunLog.info("DISTRO PROVIDED : "+distro)351    #Test 1 : verify that hv-kvp-daemon-init is installed or not, it's optional not strict.352    RunLog.info("Checking if hv-kvp-daemon-init is installed or not..")353    kvp_install_status = Run("pgrep -lf hv_kvp_daemon")354    matchCount = 0355    if "hv_kvp_daemon" in kvp_install_status:356        matchCount = matchCount + 1357    if matchCount == 1:358        print(distro+"_TEST_KVP_INSTALLED")359    else:360        print(distro+"_TEST_KVP_NOT_INSTALLED")361    #Verify sudoers file...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!!
