Header Ads Widget

Responsive Advertisement

Network Config Testing with Pytest Framework

Pytest is  open-source testing  framework for python . it used to write the test cases to verify the functionality, reliability, and performance of network-based applications or services.

 it is perfect for network testing as well , in terms of building structured tests and also integrating with tools such as Nornir, Scrapli and few others. 

Here is one of the use case to  login into the network switch , parse the vlan data using textfsm module from the device and check whether VLAN 201 does exits in switch or not using the Pytest framework  before VLAN is added into the device.

Topology :


Sample Code : 
-------------------

@pytest.fixture
def device_vlans():
    print("\nFIXTURE SETUP")

    ## Defines the connection settings for the devices wherein test need to be done ##

    device_data= {
        "host": "host_ip_address",
        "auth_username": "username",
        "auth_password": "password",
        "auth_strict_key": False,
        "platform": "cisco_iosxe"}


    with Scrapli(**device_data) as conn:

        ## Run command against the device ##
        command = conn.send_command("show vlan brief")
        print(command) ## To view vlans info on terminal ##

        ## yeild  response back

        yield [vlan["vlan_id"] for vlan in command.textfsm_parse_output()]

    # Once outside of context block, close connection
    print("\nFIXTURE TEARDOWN")


@pytest.fixture
def expected_vlan():
    return "201"


def test_vlan(expected_vlan, device_vlans):
    assert expected_vlan in device_vlans

Execute the code to test the config :
##  pytest confg_test.py 
Execution output :


Result : Test result is failed , it meas VLAN 201 does not exists into the device and it can be configured if needed.

Use cases :

-It can be used in API Testing
-It can be used in connectivity tests
-It can be used in performance testing 
-It can be used in  vulnerabilities testing such as open ports or weak SSL configurations.



Post a Comment

0 Comments