예제 #1
0
def test_netmiko_gc_exec_command(netmiko_connection_handler: Mock):
    ec_kwargs = {
        "library": "netmiko",
        "command": list(NETMIKO_COMMANDS.keys()),
        "connection_args": NETMIKO_C_ARGS
    }
    result = exec_command(**ec_kwargs)

    netmiko_connection_handler.assert_called_once_with(**NETMIKO_C_ARGS)
    for command, value in NETMIKO_COMMANDS.items():
        assert result[command] == value.splitlines()
    assert netmiko_connection_handler.session.disconnect.called
예제 #2
0
def test_netmiko_gc_exec_command_post_checks(netmiko_connection_handler: Mock,
                                             rq_job):
    netmiko_command_list = list(NETMIKO_COMMANDS.keys())

    command, post_check_command = netmiko_command_list[
        0], netmiko_command_list[-1]

    good_post_check = {
        "get_config_args": {
            "command": post_check_command
        },
        "match_str": ["wubba"],
        "match_type": "include"
    }

    bad_post_check = {
        "get_config_args": {
            "command": post_check_command
        },
        "match_str": ["wubba"],
        "match_type": "exclude"
    }

    result = exec_command(library="netmiko",
                          command=command,
                          connection_args=NETMIKO_C_ARGS,
                          post_checks=[good_post_check])

    netmiko_connection_handler.assert_called_once_with(**NETMIKO_C_ARGS)
    for command, value in list(NETMIKO_COMMANDS.items())[:1]:
        assert result[command] == value.splitlines()

    with pytest.raises(Exception):
        result = exec_command(library="netmiko",
                              command=command,
                              connection_args=NETMIKO_C_ARGS,
                              post_checks=[bad_post_check])
예제 #3
0
def test_napalm_gc_exec_command_post_checks(napalm_get_network_driver: Mock,
                                            rq_job):

    command, post_check_command = "get_config", "show run"

    good_post_check = {
        "get_config_args": {
            "command": post_check_command
        },
        "match_str": [post_check_command],
        "match_type": "include"
    }

    bad_post_check = {
        "get_config_args": {
            "command": post_check_command
        },
        "match_str": [post_check_command],
        "match_type": "exclude"
    }

    _ = exec_command(library="napalm",
                     command=command,
                     connection_args=NAPALM_C_ARGS.copy(),
                     post_checks=[good_post_check])

    napalm_get_network_driver.assert_called_once_with("ios")
    napalm_get_network_driver.driver.assert_called_once_with(
        hostname=NAPALM_C_ARGS["host"],
        username=NAPALM_C_ARGS["username"],
        password=NAPALM_C_ARGS["password"])

    with pytest.raises(NetpalmMetaProcessedException):
        _ = exec_command(library="napalm",
                         command=command,
                         connection_args=NAPALM_C_ARGS.copy(),
                         post_checks=[bad_post_check])
예제 #4
0
def test_netmiko_gc_exec_command_ttp(netmiko_connection_handler: Mock, rq_job):
    netmiko_command_list = list(NETMIKO_COMMANDS.keys())

    command = netmiko_command_list[0]

    netmiko_kwarg = {"ttp_template": "asdf"}
    result = exec_command(library="netmiko",
                          command=command,
                          connection_args=NETMIKO_C_ARGS,
                          args=netmiko_kwarg.copy())

    with pytest.raises(AssertionError):
        netmiko_connection_handler.session.send_command.assert_called_once_with(
            command, **netmiko_kwarg)

    netmiko_kwarg[
        "ttp_template"] = "netpalm/backend/plugins/extensibles/ttp_templates/asdf.ttp"
    netmiko_connection_handler.session.send_command.assert_called_once_with(
        command, **netmiko_kwarg)
예제 #5
0
def test_napalm_gc_exec_command(napalm_get_network_driver: Mock):
    ec_kwargs = {
        "library": "napalm",
        "command": ["get_config", "show run"],
        "connection_args": NAPALM_C_ARGS.copy()
    }
    mock_session = napalm_get_network_driver.session

    result = exec_command(**ec_kwargs)

    napalm_get_network_driver.assert_called_once_with("ios")
    napalm_get_network_driver.driver.assert_called_once_with(
        hostname=NAPALM_C_ARGS["host"],
        username=NAPALM_C_ARGS["username"],
        password=NAPALM_C_ARGS["password"])

    assert result["get_config"] == ["my config"]
    assert result["show run"] == ["ran show run"]
    assert napalm_get_network_driver.session.close.called
예제 #6
0
def test_ncclient_gc_exec_command(ncclient_manager: Mock, rq_job):
    args = {
        "source":
        "running",
        "filter":
        "<filter type='subtree'>"
        "<System xmlns='http://cisco.com/ns/yang/cisco-nx-os-device'></System>"
        "</filter>",
    }
    ec_kwargs = {
        "library": "ncclient",
        "connection_args": NCCLIENT_C_ARGS.copy(),
        "args": args
    }
    mocked_session = ncclient_manager.mocked_session

    result = exec_command(**ec_kwargs)

    mocked_session.get_config.assert_called_once_with(**args)

    assert result["get_config"] == mocked_session.get_config().data_xml
    assert mocked_session.close_session.called