Exemple #1
0
def test_textfsm_no_ntc_templates(monkeypatch):
    def _import_module(name, package):
        raise ModuleNotFoundError

    monkeypatch.setattr("importlib.import_module", _import_module)

    with pytest.warns(UserWarning) as exc:
        _textfsm_get_template("cisco_nxos", "show racecar")

    assert "Optional Extra Not Installed!" in str(exc.list[0].message)
Exemple #2
0
def test_textfsm_get_template_no_textfsm(monkeypatch):
    def mock_import_module(name, package):
        raise ModuleNotFoundError

    monkeypatch.setattr(importlib, "import_module", mock_import_module)

    with pytest.warns(UserWarning) as warning_msg:
        _textfsm_get_template(platform="blah", command="blah")
    assert (
        str(warning_msg._list[0].message) ==
        "\n***** Module 'None' not installed! ****************************************************\nTo resolve this issue, install 'None'. You can do this in one of the following ways:\n1: 'pip install -r requirements-textfsm.txt'\n2: 'pip install scrapli[textfsm]'\n***** Module 'None' not installed! ****************************************************"
    )
Exemple #3
0
    def textfsm_parse_output(
            self,
            template: Union[str, TextIO, None] = None,
            to_dict: bool = True) -> Union[Dict[str, Any], List[Any]]:
        """
        Parse results with textfsm, always return structured data

        Returns an empty list if parsing fails!

        Args:
            template: string path to textfsm template or opened textfsm template file
            to_dict: convert textfsm output from list of lists to list of dicts -- basically create
                dict from header and row data so it is easier to read/parse the output

        Returns:
            structured_result: empty list or parsed data from textfsm

        Raises:
            N/A

        """
        if template is None:
            template = _textfsm_get_template(platform=self.textfsm_platform,
                                             command=self.channel_input)

        if template is None:
            return []

        template = cast(Union[str, TextIOWrapper], template)
        return textfsm_parse(
            template=template, output=self.result, to_dict=to_dict) or []
Exemple #4
0
def test_textfsm_parse_success_string_path(parse_type):
    to_dict = parse_type[0]
    expected_result = parse_type[1]
    template = _textfsm_get_template("cisco_ios", "show ip arp")
    result = textfsm_parse(template.name, IOS_ARP, to_dict=to_dict)
    assert isinstance(result, list)
    assert result[0] == expected_result
Exemple #5
0
    def textfsm_parse_output(self,
                             to_dict: bool = True
                             ) -> Union[Dict[str, Any], List[Any]]:
        """
        Parse results with textfsm, always return structured data

        Returns an empty list if parsing fails!

        Args:
            to_dict: convert textfsm output from list of lists to list of dicts -- basically create
                dict from header and row data so it is easier to read/parse the output

        Returns:
            structured_result: empty list or parsed data from textfsm

        Raises:
            N/A

        """
        template = _textfsm_get_template(platform=self.textfsm_platform,
                                         command=self.channel_input)
        if isinstance(template, TextIOWrapper):
            structured_result = (textfsm_parse(
                template=template, output=self.result, to_dict=to_dict) or [])
        else:
            structured_result = []
        return structured_result
def test_response_parse_textfsm_string_path():
    template = _textfsm_get_template("cisco_ios", "show ip arp").name
    response = Response("localhost",
                        channel_input="show ip arp",
                        textfsm_platform="cisco_ios")
    response_bytes = b"""Protocol  Address          Age (min)  Hardware Addr   Type   Interface
Internet  172.31.254.1            -   0000.0c07.acfe  ARPA   Vlan254
Internet  172.31.254.2            -   c800.84b2.e9c2  ARPA   Vlan254
"""
    response.record_response(response_bytes)
    result = response.textfsm_parse_output(template=template)
    assert result[0]["address"] == "172.31.254.1"
Exemple #7
0
    def textfsm_parse_output(self) -> Union[Dict[str, Any], List[Any]]:
        """
        Parse results with textfsm, always return structured data

        Returns an empty list if parsing fails!

        Args:
            N/A

        Returns:
            structured_result: empty list or parsed data from textfsm

        Raises:
            N/A

        """
        template = _textfsm_get_template(self.textfsm_platform,
                                         self.channel_input)
        if isinstance(template, TextIOWrapper):
            structured_result = textfsm_parse(template, self.result) or []
        else:
            structured_result = []
        return structured_result
Exemple #8
0
def test__textfsm_get_template_invalid_template():
    template = _textfsm_get_template("cisco_nxos", "show racecar")
    assert not template
Exemple #9
0
def test__textfsm_get_template_valid_template():
    template = _textfsm_get_template("cisco_nxos", "show ip arp")
    template_dir = pkg_resources.resource_filename("ntc_templates",
                                                   "templates")
    assert isinstance(template, TextIOWrapper)
    assert template.name == f"{template_dir}/cisco_nxos_show_ip_arp.textfsm"
Exemple #10
0
def test_textfsm_parse_failure():
    template = _textfsm_get_template("cisco_ios", "show ip arp")
    result = textfsm_parse(template, "not really arp data")
    assert result == []
Exemple #11
0
def test_textfsm_parse(test_data):
    to_dict, expected_output = test_data
    template = _textfsm_get_template("cisco_ios", "show ip arp")
    result = textfsm_parse(template, IOS_ARP, to_dict=to_dict)
    assert isinstance(result, list)
    assert result[0] == expected_output
Exemple #12
0
def test_textfsm_parse_success_string_path():
    template = _textfsm_get_template("cisco_ios", "show ip arp")
    result = textfsm_parse(template.name, IOS_ARP)
    assert isinstance(result, list)
    assert result[0] == ["Internet", "172.31.254.1", "-", "0000.0c07.acfe", "ARPA", "Vlan254"]