Best Python code snippet using lisa_python
dpdksuite.py
Source:dpdksuite.py  
...115        self, node: Node, log: Logger, variables: Dict[str, Any]116    ) -> None:117        # initialize DPDK first, OVS requires it built from source before configuring.118        self._force_dpdk_default_source(variables)119        test_kit = initialize_node_resources(node, log, variables, "failsafe")120        # checkout OpenVirtualSwitch121        ovs = node.tools[DpdkOvs]122        # provide ovs build with DPDK tool info and build123        ovs.build_with_dpdk(test_kit.testpmd)124        # enable hugepages needed for dpdk EAL125        init_hugepages(node)126        try:127            # run OVS tests, providing OVS with the NIC info needed for DPDK init128            ovs.setup_ovs(node.nics.get_nic_by_index().pci_slot)129            # validate if OVS was able to initialize DPDK130            node.execute(131                "ovs-vsctl get Open_vSwitch . dpdk_initialized",132                sudo=True,133                expected_exit_code=0,134                expected_exit_code_failure_message=(135                    "OVS repoted that DPDK EAL failed to initialize."136                ),137            )138        finally:139            ovs.stop_ovs()140    @TestCaseMetadata(141        description="""142           Install and run ci test for NFF-Go on ubuntu143        """,144        priority=4,145        requirement=simple_requirement(146            min_core_count=8,147            min_nic_count=2,148            network_interface=Sriov(),149            unsupported_features=[Gpu, Infiniband],150            supported_features=[IsolatedResource],151        ),152    )153    def verify_dpdk_nff_go(154        self, node: Node, log: Logger, variables: Dict[str, Any]155    ) -> None:156        try:157            nff_go = node.tools[DpdkNffGo]158        except UnsupportedDistroException as err:159            raise SkippedException(err)160        # hugepages needed for dpdk tests161        init_hugepages(node)162        # run the nff-go tests163        nff_go.run_test()164    @TestCaseMetadata(165        description="""166           Build and run DPDK multiprocess client/server sample application.167           Requires 3 nics since client/server needs two ports + 1 nic for LISA168        """,169        priority=4,170        requirement=simple_requirement(171            min_nic_count=3,172            network_interface=Sriov(),173            unsupported_features=[Gpu, Infiniband],174            supported_features=[IsolatedResource],175        ),176    )177    def verify_dpdk_multiprocess(178        self, node: Node, log: Logger, variables: Dict[str, Any]179    ) -> None:180        # multiprocess test requires dpdk source.181        self._force_dpdk_default_source(variables)182        kill = node.tools[Kill]183        pmd = "failsafe"184        server_app_name = "dpdk-mp_server"185        client_app_name = "dpdk-mp_client"186        # initialize DPDK with sample applications selected for build187        test_kit = initialize_node_resources(188            node,189            log,190            variables,191            pmd,192            sample_apps=[193                "multi_process/client_server_mp/mp_server",194                "multi_process/client_server_mp/mp_client",195            ],196        )197        if test_kit.testpmd.is_connect_x3:198            raise SkippedException(199                "Unsupported Hardware: ConnectX3 does not support secondary process RX"200            )201        # enable hugepages needed for dpdk EAL202        init_hugepages(node)203        # setup and run mp_server application204        examples_path = test_kit.testpmd.dpdk_build_path.joinpath("examples")205        server_app_path = examples_path.joinpath(server_app_name)206        client_app_path = examples_path.joinpath(client_app_name)207        # EAL -l: start server on cores 1-2,208        # EAL -n: use 4 memory channels209        # APP: -p : set port bitmask to port 0 and 1210        # APP: -n : allow one client to connect211        server_proc = node.execute_async(212            (213                f"{server_app_path} -l 1-2 -n 4 "214                f"-b {node.nics.get_nic_by_index(0).pci_slot} -- -p 3 -n 1"215            ),216            sudo=True,217            shell=True,218        )219        # Wait for server to finish init220        server_proc.wait_output("APP: Finished Process Init.", timeout=5)221        # EAL -l: start client on core 3,222        # EAL --proc-type: client runs as secondary process.223        # APP: -n : client index is 0224        client_result = node.execute(225            (226                f"timeout -s INT 2 {client_app_path} --proc-type=secondary -l 3 -n 4"227                f" -b {node.nics.get_nic_by_index(0).pci_slot} -- -n 0"228            ),229            sudo=True,230            shell=True,231        )232        # client blocks and returns, kill server once client is finished.233        kill.by_name(str(server_app_name), signum=SIGINT)234        server_result = server_proc.wait_result()235        # perform the checks from v2236        assert_that(client_result.stdout).described_as(237            "Secondary process did not finish initialization"238        ).contains("APP: Finished Process Init")239        assert_that(client_result.stdout).described_as(240            "Secondary process did not start accepting packets from server"241        ).contains("Client process 0 handling packets")242        # mp_client returns a nonstandard positive number when killed w signal.243        # one would expect either 0 or 130 (killed by signal w sigint).244        # check that the nonsense number is at least the expected one.245        assert_that(client_result.exit_code).described_as(246            "dpdk-mp client exit code was unexpected"247        ).is_equal_to(124)248        assert_that(server_result.exit_code).is_equal_to(0)249    @TestCaseMetadata(250        description="""251            test sriov failsafe during vf revoke (receive side)252        """,253        priority=2,254        requirement=simple_requirement(255            min_core_count=8,256            min_nic_count=2,257            network_interface=Sriov(),258            min_count=2,259            unsupported_features=[Gpu, Infiniband],260            supported_features=[IsolatedResource],261        ),262    )263    def verify_dpdk_sriov_rescind_failover_receiver(264        self, environment: Environment, log: Logger, variables: Dict[str, Any]265    ) -> None:266        test_kits = init_nodes_concurrent(environment, log, variables, "failsafe")267        try:268            check_send_receive_compatibility(test_kits)269        except UnsupportedPackageVersionException as err:270            raise SkippedException(err)271        sender, receiver = test_kits272        # Want to only switch receiver sriov to avoid timing weirdness273        receiver.switch_sriov = True274        sender.switch_sriov = False275        kit_cmd_pairs = generate_send_receive_run_info("failsafe", sender, receiver)276        run_testpmd_concurrent(277            kit_cmd_pairs, DPDK_VF_REMOVAL_MAX_TEST_TIME, log, rescind_sriov=True278        )279        rescind_tx_pps_set = receiver.testpmd.get_mean_rx_pps_sriov_rescind()280        self._check_rx_or_tx_pps_sriov_rescind("RX", rescind_tx_pps_set)281    @TestCaseMetadata(282        description="""283            test sriov failsafe during vf revoke (send only version)284        """,285        priority=2,286        requirement=simple_requirement(287            min_core_count=8,288            min_nic_count=2,289            network_interface=Sriov(),290            unsupported_features=[Gpu, Infiniband],291            supported_features=[IsolatedResource],292        ),293    )294    def verify_dpdk_sriov_rescind_failover_send_only(295        self, node: Node, log: Logger, variables: Dict[str, Any]296    ) -> None:297        test_kit = initialize_node_resources(node, log, variables, "failsafe")298        testpmd = test_kit.testpmd299        test_nic = node.nics.get_nic_by_index()300        testpmd_cmd = testpmd.generate_testpmd_command(301            test_nic, 0, "txonly", "failsafe"302        )303        kit_cmd_pairs = {304            test_kit: testpmd_cmd,305        }306        run_testpmd_concurrent(307            kit_cmd_pairs, DPDK_VF_REMOVAL_MAX_TEST_TIME, log, rescind_sriov=True308        )309        rescind_tx_pps_set = testpmd.get_mean_tx_pps_sriov_rescind()310        self._check_rx_or_tx_pps_sriov_rescind("TX", rescind_tx_pps_set)311    def _check_rx_or_tx_pps_sriov_rescind(312        self, tx_or_rx: str, pps: Tuple[int, int, int]313    ) -> None:314        before_rescind, during_rescind, after_reenable = pps315        self._check_rx_or_tx_pps(tx_or_rx, before_rescind, sriov_enabled=True)316        self._check_rx_or_tx_pps(tx_or_rx, during_rescind, sriov_enabled=False)317        self._check_rx_or_tx_pps(tx_or_rx, after_reenable, sriov_enabled=True)318    def _check_rx_or_tx_pps(319        self, tx_or_rx: str, pps: int, sriov_enabled: bool = True320    ) -> None:321        if sriov_enabled:322            assert_that(pps).described_as(323                f"{tx_or_rx}-PPS ({pps}) should have been greater "324                "than 2^20 (~1m) PPS before sriov disable."325            ).is_greater_than(2**20)326        else:327            assert_that(pps).described_as(328                f"{tx_or_rx}-PPS ({pps}) should have been less "329                "than 2^20 (~1m) PPS after sriov disable."330            ).is_less_than(2**20)331    @TestCaseMetadata(332        description="""333            verify vpp is able to detect azure network interfaces334            1. run fd.io vpp install scripts335            2. install vpp from their repositories336            3. start vpp service337            4. check that azure interfaces are detected by vpp338        """,339        priority=4,340        requirement=simple_requirement(341            min_core_count=8,342            min_nic_count=2,343            network_interface=Sriov(),344            unsupported_features=[Gpu, Infiniband],345            supported_features=[IsolatedResource],346        ),347    )348    def verify_dpdk_vpp(349        self, node: Node, log: Logger, variables: Dict[str, Any]350    ) -> None:351        vpp = node.tools[DpdkVpp]352        vpp.install()353        net = node.nics354        nic = net.get_nic_by_index()355        # set devices to down and restart vpp service356        ip = node.tools[Ip]357        for dev in [nic.lower, nic.upper]:358            ip.down(dev)359        for dev in [nic.lower, nic.upper]:360            ip.addr_flush(dev)361        vpp.start()362        vpp.run_test()363    @TestCaseMetadata(364        description="""365            This test runs the dpdk ring ping utility from:366            https://github.com/shemminger/dpdk-ring-ping367            to measure the maximum latency for 99.999 percent of packets during368            the test run. The maximum should be under 200000 nanoseconds369            (.2 milliseconds).370            Not dependent on any specific PMD.371        """,372        priority=4,373        requirement=simple_requirement(374            min_core_count=8,375            network_interface=Sriov(),376            unsupported_features=[Gpu, Infiniband],377            supported_features=[IsolatedResource],378        ),379    )380    def verify_dpdk_ring_ping(381        self, node: Node, log: Logger, variables: Dict[str, Any]382    ) -> None:383        # ring ping requires dpdk source to run, since default is package_manager384        # we special case here to use to dpdk-stable as the default.385        self._force_dpdk_default_source(variables)386        # setup and unwrap the resources for this test387        test_kit = initialize_node_resources(node, log, variables, "failsafe")388        testpmd = test_kit.testpmd389        # grab a nic and run testpmd390        git = node.tools[Git]391        make = node.tools[Make]392        echo = node.tools[Echo]393        rping_build_env_vars = [394            "export RTE_TARGET=build",395            f"export RTE_SDK={str(testpmd.dpdk_path)}",396        ]397        echo.write_to_file(398            ";".join(rping_build_env_vars), node.get_pure_path("~/.bashrc"), append=True399        )400        git_path = git.clone(401            "https://github.com/shemminger/dpdk-ring-ping.git", cwd=node.working_path...dpdkutil.py
Source:dpdkutil.py  
...192            hv_uio_generic_uuid,193            node.get_pure_path("/sys/bus/vmbus/drivers/uio_hv_generic/new_id"),194            sudo=True,195        )196def initialize_node_resources(197    node: Node,198    log: Logger,199    variables: Dict[str, Any],200    pmd: str,201    sample_apps: Union[List[str], None] = None,202) -> DpdkTestResources:203    _set_forced_source_by_distro(node, variables)204    dpdk_source = variables.get("dpdk_source", PACKAGE_MANAGER_SOURCE)205    dpdk_branch = variables.get("dpdk_branch", "")206    log.info(207        "Dpdk initialize_node_resources running"208        f"found dpdk_source '{dpdk_source}' and dpdk_branch '{dpdk_branch}'"209    )210    network_interface_feature = node.features[NetworkInterface]211    sriov_is_enabled = network_interface_feature.is_enabled_sriov()212    log.info(f"Node[{node.name}] Verify SRIOV is enabled: {sriov_is_enabled}")213    assert_that(sriov_is_enabled).described_as(214        f"SRIOV was not enabled for this test node ({node.name})"215    ).is_true()216    # dump some info about the pci devices before we start217    lspci = node.tools[Lspci]218    log.info(f"Node[{node.name}] LSPCI Info:\n{lspci.run().stdout}\n")219    # check compatibility first.220    try:221        check_dpdk_support(node)222    except UnsupportedDistroException as err:223        # forward message from distro exception224        raise SkippedException(err)225    # verify SRIOV is setup as-expected on the node after compat check226    node.nics.wait_for_sriov_enabled()227    # create tool, initialize testpmd tool (installs dpdk)228    testpmd: DpdkTestpmd = node.tools.get(229        DpdkTestpmd,230        dpdk_source=dpdk_source,231        dpdk_branch=dpdk_branch,232        sample_apps=sample_apps,233    )234    # init and enable hugepages (required by dpdk)235    init_hugepages(node)236    assert_that(len(node.nics)).described_as(237        "Test needs at least 1 NIC on the test node."238    ).is_greater_than_or_equal_to(1)239    test_nic = node.nics.get_nic_by_index()240    # check an assumption that our nics are bound to hv_netvsc241    # at test start.242    assert_that(test_nic.bound_driver).described_as(243        f"Error: Expected test nic {test_nic.upper} to be "244        f"bound to hv_netvsc. Found {test_nic.bound_driver}."245    ).is_equal_to("hv_netvsc")246    # netvsc pmd requires uio_hv_generic to be loaded before use247    if pmd == "netvsc":248        # this code makes changes to interfaces that will cause later tests to fail.249        # Therefore we mark the node dirty to prevent future testing on this environment250        node.mark_dirty()251        enable_uio_hv_generic_for_nic(node, test_nic)252        # if this device is paired, set the upper device 'down'253        if test_nic.lower:254            node.nics.unbind(test_nic)255            node.nics.bind(test_nic, UIO_HV_GENERIC_SYSFS_PATH)256    return DpdkTestResources(node, testpmd)257def check_send_receive_compatibility(test_kits: List[DpdkTestResources]) -> None:258    for kit in test_kits:259        if not kit.testpmd.has_tx_ip_flag():260            raise UnsupportedPackageVersionException(261                kit.node.os,262                "dpdk",263                kit.testpmd.get_dpdk_version(),264                "-tx-ip flag for ip forwarding",265            )266def run_testpmd_concurrent(267    node_cmd_pairs: Dict[DpdkTestResources, str],268    seconds: int,269    log: Logger,270    rescind_sriov: bool = False,271) -> Dict[DpdkTestResources, str]:272    output: Dict[DpdkTestResources, str] = dict()273    task_manager = start_testpmd_concurrent(node_cmd_pairs, seconds, log, output)274    if rescind_sriov:275        time.sleep(10)  # run testpmd for a bit before disabling sriov276        test_kits = node_cmd_pairs.keys()277        # disable sriov (and wait for change to apply)278        for node_resources in [x for x in test_kits if x.switch_sriov]:279            node_resources.nic_controller.switch_sriov(280                enable=False, wait=True, reset_connections=False281            )282        # let run for a bit with SRIOV disabled283        time.sleep(10)284        # re-enable sriov285        for node_resources in [x for x in test_kits if x.switch_sriov]:286            node_resources.nic_controller.switch_sriov(287                enable=True, wait=True, reset_connections=False288            )289        # run for a bit with SRIOV re-enabled290        time.sleep(10)291        # kill the commands to collect the output early and terminate before timeout292        for node_resources in test_kits:293            node_resources.testpmd.kill_previous_testpmd_command()294    task_manager.wait_for_all_workers()295    return output296def start_testpmd_concurrent(297    node_cmd_pairs: Dict[DpdkTestResources, str],298    seconds: int,299    log: Logger,300    output: Dict[DpdkTestResources, str],301) -> TaskManager[Tuple[DpdkTestResources, str]]:302    cmd_pairs_as_tuples = deque(node_cmd_pairs.items())303    def _collect_dict_result(result: Tuple[DpdkTestResources, str]) -> None:304        output[result[0]] = result[1]305    def _run_command_with_testkit(306        run_kit: Tuple[DpdkTestResources, str]307    ) -> Tuple[DpdkTestResources, str]:308        testkit, cmd = run_kit309        return (testkit, testkit.testpmd.run_for_n_seconds(cmd, seconds))310    task_manager = run_in_parallel_async(311        [partial(_run_command_with_testkit, x) for x in cmd_pairs_as_tuples],312        _collect_dict_result,313    )314    return task_manager315def init_nodes_concurrent(316    environment: Environment, log: Logger, variables: Dict[str, Any], pmd: str317) -> List[DpdkTestResources]:318    # Use threading module to parallelize the IO-bound node init.319    test_kits = run_in_parallel(320        [321            partial(initialize_node_resources, node, log, variables, pmd)322            for node in environment.nodes.list()323        ],324        log,325    )326    return test_kits327def verify_dpdk_build(328    node: Node,329    log: Logger,330    variables: Dict[str, Any],331    pmd: str,332) -> None:333    # setup and unwrap the resources for this test334    test_kit = initialize_node_resources(node, log, variables, pmd)335    testpmd = test_kit.testpmd336    # grab a nic and run testpmd337    test_nic = node.nics.get_nic_by_index()338    testpmd_cmd = testpmd.generate_testpmd_command(339        test_nic,340        0,341        "txonly",342        pmd,343    )344    testpmd.run_for_n_seconds(testpmd_cmd, 10)345    tx_pps = testpmd.get_mean_tx_pps()346    log.info(347        f"TX-PPS:{tx_pps} from {test_nic.upper}/{test_nic.lower}:"348        + f"{test_nic.pci_slot}"...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!!
