Best Python code snippet using lisa_python
nestedperf.py
Source:nestedperf.py  
...139        result: TestResult,140        variables: Dict[str, Any],141        log: Logger,142    ) -> None:143        self._storage_perf_hyperv(node, result, variables, log)144    @TestCaseMetadata(145        description="""146        This test case is to validate performance of nested VM using fio tool with raid0147        configuration of 6 l1 data disk attached to the l2 VM.148        """,149        priority=3,150        timeout=_TIME_OUT,151        requirement=simple_requirement(152            supported_os=[Windows],153            supported_platform_type=[AZURE, READY],154            disk=schema.DiskOptionSettings(155                disk_type=schema.DiskType.StandardSSDLRS,156                data_disk_iops=search_space.IntRange(min=5000),157                data_disk_count=search_space.IntRange(min=6),158            ),159        ),160    )161    def perf_nested_hyperv_storage_multidisk(162        self,163        node: RemoteNode,164        result: TestResult,165        variables: Dict[str, Any],166        log: Logger,167    ) -> None:168        self._storage_perf_hyperv(node, result, variables, log, setup_raid=True)169    @TestCaseMetadata(170        description="""171        This test case runs ntttcp test on two nested VMs on same L1 guest172        connected with private bridge173        """,174        priority=3,175        timeout=_TIME_OUT,176        requirement=simple_requirement(177            min_core_count=16,178            disk=schema.DiskOptionSettings(179                data_disk_count=search_space.IntRange(min=1),180                data_disk_size=search_space.IntRange(min=12),181            ),182        ),183    )184    def perf_nested_kvm_ntttcp_private_bridge(185        self,186        node: RemoteNode,187        result: TestResult,188        variables: Dict[str, Any],189        log: Logger,190    ) -> None:191        (192            nested_image_username,193            nested_image_password,194            _,195            nested_image_url,196        ) = parse_nested_image_variables(variables)197        # get cpu count198        cpu_count = node.tools[Lscpu].get_core_count()199        try:200            # setup bridge201            node.tools[Ip].setup_bridge(202                self._BR_NAME, f"{self._BR_GATEWAY}/{self._BR_CIDR}"203            )204            # setup server and client205            server = qemu_connect_nested_vm(206                node,207                nested_image_username,208                nested_image_password,209                self._SERVER_HOST_FWD_PORT,210                nested_image_url,211                image_name=self._SERVER_IMAGE,212                nic_model="virtio-net-pci",213                taps=1,214                cores=cpu_count,215                bridge=self._BR_NAME,216                name="server",217                log=log,218            )219            server.tools[Ip].add_ipv4_address(220                self._NIC_NAME, f"{self._SERVER_IP_ADDR}/{self._BR_CIDR}", persist=True221            )222            server.tools[Ip].up(self._NIC_NAME, persist=True)223            server.internal_address = self._SERVER_IP_ADDR224            server.capability.network_interface = Synthetic()225            client = qemu_connect_nested_vm(226                node,227                nested_image_username,228                nested_image_password,229                self._CLIENT_HOST_FWD_PORT,230                nested_image_url,231                image_name=self._CLIENT_IMAGE,232                nic_model="virtio-net-pci",233                taps=1,234                cores=cpu_count,235                bridge=self._BR_NAME,236                name="client",237                stop_existing_vm=False,238                log=log,239            )240            client.tools[Ip].add_ipv4_address(241                self._NIC_NAME, f"{self._CLIENT_IP_ADDR}/{self._BR_CIDR}", persist=True242            )243            client.tools[Ip].up(self._NIC_NAME, persist=True)244            client.capability.network_interface = Synthetic()245            # run ntttcp test246            perf_ntttcp(247                result,248                server,249                client,250                server_nic_name=self._NIC_NAME,251                client_nic_name=self._NIC_NAME,252                test_case_name=inspect.stack()[1][3],253            )254        finally:255            try:256                # stop running QEMU instances257                node.tools[Qemu].delete_vm()258                # clear bridge and taps259                node.tools[Ip].delete_interface(self._BR_NAME)260            except Exception as e:261                log.debug(f"Failed to clean up vm: {e}")262                node.mark_dirty()263    @TestCaseMetadata(264        description="""265        This script runs ntttcp test on two nested VMs on different L1 guests266        connected with NAT267        """,268        priority=3,269        timeout=_TIME_OUT,270        requirement=simple_requirement(271            min_count=2,272            min_core_count=16,273            network_interface=schema.NetworkInterfaceOptionSettings(274                nic_count=search_space.IntRange(min=2),275            ),276            disk=schema.DiskOptionSettings(277                data_disk_count=search_space.IntRange(min=1),278                data_disk_size=search_space.IntRange(min=12),279            ),280        ),281    )282    def perf_nested_kvm_ntttcp_different_l1_nat(283        self,284        result: TestResult,285        variables: Dict[str, Any],286        log: Logger,287    ) -> None:288        environment = result.environment289        assert environment, "fail to get environment from testresult"290        server_l1 = cast(RemoteNode, environment.nodes[0])291        client_l1 = cast(RemoteNode, environment.nodes[1])292        # parse nested image variables293        (294            nested_image_username,295            nested_image_password,296            _,297            nested_image_url,298        ) = parse_nested_image_variables(variables)299        try:300            # setup nested vm on server in NAT configuration301            server_l2 = self._linux_setup_nat(302                node=server_l1,303                nested_vm_name="server_l2",304                guest_username=nested_image_username,305                guest_password=nested_image_password,306                guest_port=self._SERVER_HOST_FWD_PORT,307                guest_image_url=nested_image_url,308                guest_internal_ip=self._SERVER_IP_ADDR,309                guest_default_nic=self._NIC_NAME,310                bridge_name=self._BR_NAME,311                bridge_network=self._BR_NETWORK,312                bridge_cidr=self._BR_CIDR,313                bridge_gateway=self._BR_GATEWAY,314            )315            # setup nested vm on client in NAT configuration316            client_l2 = self._linux_setup_nat(317                node=client_l1,318                nested_vm_name="client_l2",319                guest_username=nested_image_username,320                guest_password=nested_image_password,321                guest_port=self._CLIENT_HOST_FWD_PORT,322                guest_image_url=nested_image_url,323                guest_internal_ip=self._CLIENT_IP_ADDR,324                guest_default_nic=self._NIC_NAME,325                bridge_name=self._BR_NAME,326                bridge_network=self._BR_NETWORK,327                bridge_cidr=self._BR_CIDR,328                bridge_gateway=self._BR_GATEWAY,329            )330            # run ntttcp test331            perf_ntttcp(332                result,333                server_l2,334                client_l2,335                server_nic_name=self._NIC_NAME,336                client_nic_name=self._NIC_NAME,337                test_case_name=inspect.stack()[1][3],338            )339        finally:340            self._linux_cleanup_nat(server_l1, self._BR_NAME, log)341            self._linux_cleanup_nat(client_l1, self._BR_NAME, log)342    @TestCaseMetadata(343        description="""344        This script runs ntttcp test on two nested VMs on different L1 guests345        connected with NAT346        """,347        priority=3,348        timeout=_TIME_OUT,349        requirement=simple_requirement(350            min_count=2,351            supported_os=[Windows],352            supported_platform_type=[AZURE, READY],353        ),354    )355    def perf_nested_hyperv_ntttcp_different_l1_nat(356        self,357        result: TestResult,358        variables: Dict[str, Any],359        log: Logger,360    ) -> None:361        environment = result.environment362        assert environment, "fail to get environment from testresult"363        server_l1 = cast(RemoteNode, environment.nodes[0])364        client_l1 = cast(RemoteNode, environment.nodes[1])365        # parse nested image variables366        (367            nested_image_username,368            nested_image_password,369            _,370            nested_image_url,371        ) = parse_nested_image_variables(variables)372        try:373            # setup nested vm on server in NAT configuration374            server_l2 = self._windows_setup_nat(375                node=server_l1,376                nested_vm_name="server_l2",377                guest_username=nested_image_username,378                guest_password=nested_image_password,379                guest_port=self._SERVER_HOST_FWD_PORT,380                guest_image_url=nested_image_url,381            )382            # setup nested vm on client in NAT configuration383            client_l2 = self._windows_setup_nat(384                node=client_l1,385                nested_vm_name="client_l2",386                guest_username=nested_image_username,387                guest_password=nested_image_password,388                guest_port=self._CLIENT_HOST_FWD_PORT,389                guest_image_url=nested_image_url,390            )391            # run ntttcp test392            perf_ntttcp(393                result,394                server_l2,395                client_l2,396                test_case_name=inspect.stack()[1][3],397            )398        finally:399            # cleanup server400            try:401                hyperv_remove_nested_vm(server_l1, "server_l2")402            except Exception as e:403                log.debug(f"Failed to clean up server vm: {e}")404                server_l1.mark_dirty()405            # cleanup client406            try:407                hyperv_remove_nested_vm(client_l1, "client_l2")408            except Exception as e:409                log.debug(f"Failed to clean up client vm: {e}")410                client_l1.mark_dirty()411    @TestCaseMetadata(412        description="""413        This script runs netperf test on two nested VMs on different L1 guests414        connected with NAT415        """,416        priority=3,417        timeout=_TIME_OUT,418        requirement=simple_requirement(419            min_count=2,420            network_interface=schema.NetworkInterfaceOptionSettings(421                nic_count=search_space.IntRange(min=2),422            ),423            disk=schema.DiskOptionSettings(424                data_disk_count=search_space.IntRange(min=1),425                data_disk_size=search_space.IntRange(min=12),426            ),427        ),428    )429    def perf_nested_kvm_netperf_pps_nat(430        self,431        result: TestResult,432        variables: Dict[str, Any],433        log: Logger,434    ) -> None:435        environment = result.environment436        assert environment, "fail to get environment from testresult"437        server_l1 = cast(RemoteNode, environment.nodes[0])438        client_l1 = cast(RemoteNode, environment.nodes[1])439        # parse nested image variables440        (441            nested_image_username,442            nested_image_password,443            _,444            nested_image_url,445        ) = parse_nested_image_variables(variables)446        try:447            # setup nested vm on server in NAT configuration448            server_l2 = self._linux_setup_nat(449                node=server_l1,450                nested_vm_name="server_l2",451                guest_username=nested_image_username,452                guest_password=nested_image_password,453                guest_port=self._SERVER_HOST_FWD_PORT,454                guest_image_url=nested_image_url,455                guest_internal_ip=self._SERVER_IP_ADDR,456                guest_default_nic=self._NIC_NAME,457                bridge_name=self._BR_NAME,458                bridge_network=self._BR_NETWORK,459                bridge_cidr=self._BR_CIDR,460                bridge_gateway=self._BR_GATEWAY,461            )462            # setup nested vm on client in NAT configuration463            client_l2 = self._linux_setup_nat(464                node=client_l1,465                nested_vm_name="client_l2",466                guest_username=nested_image_username,467                guest_password=nested_image_password,468                guest_port=self._CLIENT_HOST_FWD_PORT,469                guest_image_url=nested_image_url,470                guest_internal_ip=self._CLIENT_IP_ADDR,471                guest_default_nic=self._NIC_NAME,472                bridge_name=self._BR_NAME,473                bridge_network=self._BR_NETWORK,474                bridge_cidr=self._BR_CIDR,475                bridge_gateway=self._BR_GATEWAY,476            )477            # run netperf test478            perf_tcp_pps(result, "singlepps", server_l2, client_l2)479        finally:480            self._linux_cleanup_nat(server_l1, self._BR_NAME, log)481            self._linux_cleanup_nat(client_l1, self._BR_NAME, log)482    def _linux_setup_nat(483        self,484        node: RemoteNode,485        nested_vm_name: str,486        guest_username: str,487        guest_password: str,488        guest_port: int,489        guest_image_url: str,490        guest_internal_ip: str,491        guest_default_nic: str,492        bridge_name: str,493        bridge_network: str,494        bridge_cidr: str,495        bridge_gateway: str,496    ) -> RemoteNode:497        """498        Setup NAT on the node with following configurations:499        1. Forward traffic on node's eth0 interface and port `guest_port`500        to the nested VM's port 22.501        2. Forward all traffic on node's eth1 interface to the nested VM.502        """503        # get core count504        core_count = node.tools[Lscpu].get_core_count()505        node_eth1_ip = node.nics.get_nic("eth1").ip_addr506        bridge_dhcp_range = f"{guest_internal_ip},{guest_internal_ip}"507        # enable ip forwarding508        node.tools[Sysctl].write("net.ipv4.ip_forward", "1")509        # setup bridge510        node.tools[Ip].setup_bridge(bridge_name, f"{bridge_gateway}/{bridge_cidr}")511        node.tools[Ip].set_bridge_configuration(bridge_name, "stp_state", "0")512        node.tools[Ip].set_bridge_configuration(bridge_name, "forward_delay", "0")513        # reset bridge lease file to remove old dns leases514        node.execute(515            f"cp /dev/null /var/run/qemu-dnsmasq-{bridge_name}.leases", sudo=True516        )517        # start dnsmasq518        node.tools[Dnsmasq].start(bridge_name, bridge_gateway, bridge_dhcp_range)519        # reset filter table to accept all traffic520        node.tools[Iptables].reset_table()521        # reset nat table and setup nat forwarding522        node.tools[Iptables].reset_table("nat")523        node.tools[Iptables].run(524            f"-t nat -A POSTROUTING -s {bridge_network}/{bridge_cidr} -j MASQUERADE",525            sudo=True,526            force_run=True,527        )528        # start nested vm529        nested_vm = qemu_connect_nested_vm(530            node,531            guest_username,532            guest_password,533            guest_port,534            guest_image_url,535            taps=1,536            cores=core_count,537            bridge=bridge_name,538            stop_existing_vm=True,539            name=nested_vm_name,540        )541        # configure rc.local to run dhclient on reboot542        nested_vm.tools[StartConfiguration].add_command("ip link set dev ens4 up")543        nested_vm.tools[StartConfiguration].add_command("dhclient ens4")544        # reboot nested vm and close ssh connection545        nested_vm.execute("reboot", sudo=True)546        nested_vm.close()547        # route traffic on `eth0` and port `guest_port` on l1 vm to548        # port 22 on l2 vm549        node.tools[Iptables].run(550            f"-t nat -A PREROUTING -i eth0 -p tcp --dport {guest_port} "551            f"-j DNAT --to {guest_internal_ip}:22",552            sudo=True,553            force_run=True,554        )555        # route all tcp traffic on `eth1` port on l1 vm to l2 vm556        node.tools[Iptables].run(557            f"-t nat -A PREROUTING -i eth1 -d {node_eth1_ip} "558            f"-p tcp -j DNAT --to {guest_internal_ip}",559            sudo=True,560            force_run=True,561        )562        # wait till nested vm is up563        try_connect(564            schema.ConnectionInfo(565                address=node.public_address,566                port=guest_port,567                username=guest_username,568                password=guest_password,569            )570        )571        # set default nic interfaces on l2 vm572        nested_vm.internal_address = node_eth1_ip573        nested_vm.capability.network_interface = Synthetic()574        return nested_vm575    def _linux_cleanup_nat(576        self,577        node: RemoteNode,578        bridge_name: str,579        log: Logger,580    ) -> None:581        try:582            # stop running QEMU instances583            node.tools[Qemu].delete_vm()584            # clear bridge and taps585            node.tools[Ip].delete_interface(bridge_name)586            # flush ip tables587            node.tools[Iptables].reset_table()588            node.tools[Iptables].reset_table("nat")589        except Exception as e:590            log.debug(f"Failed to clean up NAT configuration: {e}")591            node.mark_dirty()592    def _storage_perf_qemu(593        self,594        node: RemoteNode,595        result: TestResult,596        variables: Dict[str, Any],597        log: Logger,598        filename: str = "/dev/sdb",599        start_iodepth: int = 1,600        max_iodepth: int = 1024,601        setup_raid: bool = True,602    ) -> None:603        (604            nested_image_username,605            nested_image_password,606            nested_image_port,607            nested_image_url,608        ) = parse_nested_image_variables(variables)609        # get data disks and remove disk we will use for downloading610        # nested image611        l1_data_disks = node.features[Disk].get_raw_data_disks()612        log.debug(f"l1_data_disks: {l1_data_disks}")613        image_download_location = node.find_partition_with_freespace(614            NESTED_VM_REQUIRED_DISK_SIZE_IN_GB615        )616        image_download_disk = (617            node.tools[Lsblk]618            .find_disk_by_mountpoint(image_download_location, force_run=True)619            .device_name620        )621        log.debug(f"image_download_disk: {image_download_disk}")622        if image_download_disk in l1_data_disks:623            l1_data_disks.remove(image_download_disk)624        l1_data_disk_count = len(l1_data_disks)625        try:626            # setup raid on l1 data disks627            if setup_raid:628                disks = ["/dev/md0"]629                l1_partition_disks = reset_partitions(node, l1_data_disks)630                stop_raid(node)631                reset_raid(node, l1_partition_disks)632            else:633                disks = [l1_data_disks[0]]634            # get l2 vm635            l2_vm = qemu_connect_nested_vm(636                node,637                nested_image_username,638                nested_image_password,639                nested_image_port,640                nested_image_url,641                disks=disks,642            )643            l2_vm.capability.network_interface = Synthetic()644            # Qemu command exits immediately but the VM requires some time to boot up.645            l2_vm.tools[Lscpu].get_core_count()646            # Each fio process start jobs equal to the iodepth to read/write from647            # the disks. The max number of jobs can be equal to the core count of648            # the node.649            # Examples:650            # iodepth = 4, core count = 8 => max_jobs = 4651            # iodepth = 16, core count = 8 => max_jobs = 8652            num_jobs = []653            iodepth_iter = start_iodepth654            core_count = node.tools[Lscpu].get_core_count()655            while iodepth_iter <= max_iodepth:656                num_jobs.append(min(iodepth_iter, core_count))657                iodepth_iter = iodepth_iter * 2658            # Run fio test659            # The added disks appear as /dev/sdb on the nested vm660            perf_disk(661                l2_vm,662                start_iodepth,663                max_iodepth,664                filename,665                test_name=inspect.stack()[1][3],666                core_count=core_count,667                disk_count=l1_data_disk_count,668                disk_setup_type=DiskSetupType.raid0,669                disk_type=DiskType.premiumssd,670                test_result=result,671                num_jobs=num_jobs,672                size_mb=8192,673                overwrite=True,674            )675        finally:676            try:677                node.tools[Qemu].delete_vm()678                stop_raid(node)679            except Exception as e:680                log.debug(f"Failed to cleanup Qemu VM: {e}")681                node.mark_dirty()682    def _storage_perf_hyperv(683        self,684        node: RemoteNode,685        test_result: TestResult,686        variables: Dict[str, Any],687        log: Logger,688        filename: str = "/dev/sdb",689        start_iodepth: int = 1,690        max_iodepth: int = 1024,691        setup_raid: bool = False,692    ) -> None:693        (694            nested_image_username,695            nested_image_password,696            nested_image_port,...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!!
