Exemple #1
0
def test_get_facts_questions():
    """Test that get facts calls the right questions, passing through the right args."""
    bf = Session(load_questions=False)
    nodes = "foo"
    with patch.object(
            bf.q, "nodeProperties", create=True) as mock_node, patch.object(
                bf.q, "interfaceProperties",
                create=True) as mock_iface, patch.object(
                    bf.q, "bgpPeerConfiguration",
                    create=True) as mock_peers, patch.object(
                        bf.q, "bgpProcessConfiguration",
                        create=True) as mock_proc, patch.object(
                            bf.q, "ospfProcessConfiguration",
                            create=True) as mock_ospf_proc, patch.object(
                                bf.q, "ospfAreaConfiguration",
                                create=True) as mock_ospf_area, patch.object(
                                    bf.q,
                                    "ospfInterfaceConfiguration",
                                    create=True) as mock_ospf_iface:
        mock_node.return_value = MockQuestion(MockTableAnswer)
        mock_iface.return_value = MockQuestion(MockTableAnswer)
        mock_proc.return_value = MockQuestion(MockTableAnswer)
        mock_peers.return_value = MockQuestion(MockTableAnswer)
        mock_ospf_proc.return_value = MockQuestion(MockTableAnswer)
        mock_ospf_area.return_value = MockQuestion(MockTableAnswer)
        mock_ospf_iface.return_value = MockQuestion(MockTableAnswer)
        get_facts(bf, nodes)

        mock_node.assert_called_with(nodes=nodes)
        mock_iface.assert_called_with(nodes=nodes)
        mock_proc.assert_called_with(nodes=nodes)
        mock_peers.assert_called_with(nodes=nodes)
        mock_ospf_proc.assert_called_with(nodes=nodes)
        mock_ospf_area.assert_called_with(nodes=nodes)
        mock_ospf_iface.assert_called_with(nodes=nodes)
Exemple #2
0
def test_get_facts_questions():
    """Test that get facts calls the right questions, passing through the right args."""
    bf = Session(load_questions=False)
    nodes = 'foo'
    with patch.object(bf.q,
                      'nodeProperties',
                      create=True) as mock_node, \
            patch.object(bf.q,
                         'interfaceProperties',
                         create=True) as mock_iface, \
            patch.object(bf.q,
                         'bgpPeerConfiguration',
                         create=True) as mock_peers, \
            patch.object(bf.q,
                         'bgpProcessConfiguration',
                         create=True) as mock_proc:
        mock_node.return_value = MockQuestion(MockTableAnswer())
        mock_iface.return_value = MockQuestion(MockTableAnswer())
        mock_proc.return_value = MockQuestion(MockTableAnswer())
        mock_peers.return_value = MockQuestion(MockTableAnswer())
        get_facts(bf, nodes)

        mock_node.assert_called_with(nodes=nodes)
        mock_iface.assert_called_with(nodes=nodes)
        mock_proc.assert_called_with(nodes=nodes)
        mock_peers.assert_called_with(nodes=nodes)
Exemple #3
0
def test_get_facts_questions_specific_snapshot():
    """Test that get facts calls the right questions, passing through the right args when a snapshot is specified."""
    bf = Session(load_questions=False)
    nodes = "foo"
    with patch.object(
            bf.q, "nodeProperties", create=True) as mock_node, patch.object(
                bf.q, "interfaceProperties",
                create=True) as mock_iface, patch.object(
                    bf.q, "bgpPeerConfiguration",
                    create=True) as mock_peers, patch.object(
                        bf.q, "bgpProcessConfiguration",
                        create=True) as mock_proc, patch.object(
                            bf.q, "ospfProcessConfiguration",
                            create=True) as mock_ospf_proc, patch.object(
                                bf.q, "ospfAreaConfiguration",
                                create=True) as mock_ospf_area, patch.object(
                                    bf.q,
                                    "ospfInterfaceConfiguration",
                                    create=True) as mock_ospf_iface:
        # Setup mock answers for each underlying question
        mock_node_a = Mock(return_value=MockTableAnswer())
        mock_iface_a = Mock(return_value=MockTableAnswer())
        mock_proc_a = Mock(return_value=MockTableAnswer())
        mock_peers_a = Mock(return_value=MockTableAnswer())
        mock_ospf_proc_a = Mock(return_value=MockTableAnswer())
        mock_ospf_area_a = Mock(return_value=MockTableAnswer())
        mock_ospf_iface_a = Mock(return_value=MockTableAnswer())

        # Setup mock questions for all underlying questions
        mock_node.return_value = MockQuestion(mock_node_a)
        mock_iface.return_value = MockQuestion(mock_iface_a)
        mock_proc.return_value = MockQuestion(mock_proc_a)
        mock_peers.return_value = MockQuestion(mock_peers_a)
        mock_ospf_proc.return_value = MockQuestion(mock_ospf_proc_a)
        mock_ospf_area.return_value = MockQuestion(mock_ospf_area_a)
        mock_ospf_iface.return_value = MockQuestion(mock_ospf_iface_a)

        get_facts(bf, nodes, snapshot="snapshot")

        # Make sure questions were all called with expected params
        mock_node.assert_called_with(nodes=nodes)
        mock_iface.assert_called_with(nodes=nodes)
        mock_proc.assert_called_with(nodes=nodes)
        mock_peers.assert_called_with(nodes=nodes)
        mock_ospf_proc.assert_called_with(nodes=nodes)
        mock_ospf_area.assert_called_with(nodes=nodes)
        mock_ospf_iface.assert_called_with(nodes=nodes)

        # Make sure answer functions were all called with expected params
        mock_node_a.assert_called_with(snapshot="snapshot")
        mock_iface_a.assert_called_with(snapshot="snapshot")
        mock_proc_a.assert_called_with(snapshot="snapshot")
        mock_peers_a.assert_called_with(snapshot="snapshot")
        mock_ospf_proc_a.assert_called_with(snapshot="snapshot")
        mock_ospf_area_a.assert_called_with(snapshot="snapshot")
        mock_ospf_iface_a.assert_called_with(snapshot="snapshot")
Exemple #4
0
    def extract_facts(self, nodes="/.*/", output_directory=None, snapshot=None):
        # type: (Text, Optional[Text], Optional[Text]) -> Dict[Text, Any]
        """
        Extract and return a dictionary of facts about the specified nodes on a network snapshot.

        If a snapshot is specified, facts are collected for that snapshot, otherwise facts are collected for the current snapshot.

        If an output directory is specified, facts for each node will be written to a separate YAML file in that directory.

        :param nodes: `NodeSpecifier <https://github.com/batfish/batfish/blob/master/questions/Parameters.md#node-specifier>`_, specifying which nodes to extract facts for.
        :type nodes: Text
        :param output_directory: path to directory to write facts to
        :type output_directory: Text
        :param snapshot: name of the snapshot to extract facts for, defaults to the current snapshot
        :type snapshot: Text
        :return: facts about the specified nodes on the current network snapshot
        :rtype: dict
        """
        facts = get_facts(self, nodes, snapshot=snapshot)
        if output_directory:
            if not os.path.exists(output_directory):
                os.makedirs(output_directory)
            if os.path.isfile(output_directory):
                raise ValueError(
                    "Cannot write facts to file, must be a directory: {}".format(
                        output_directory
                    )
                )
            write_facts(output_directory, facts)
        return facts
Exemple #5
0
    def validate_facts(self, expected_facts):
        # type: (Text) -> Dict[Text, Any]
        """
        Return a dictionary of mismatched facts between the loaded expected facts and the actual facts.

        :param expected_facts: path to directory to read expected fact YAML files from
        :type expected_facts: Text
        :return: facts about the specified nodes on the current network snapshot
        :rtype: dict
        """
        actual_facts = get_facts(self)
        expected_facts_ = load_facts(expected_facts)
        return validate_facts(expected_facts_, actual_facts)
Exemple #6
0
    def validate_facts(self, expected_facts, snapshot=None):
        # type: (Text, Optional[Text]) -> Dict[Text, Any]
        """
        Return a dictionary of mismatched facts between the loaded expected facts and the actual facts.

        :param expected_facts: path to directory to read expected fact YAML files from
        :type expected_facts: Text
        :param snapshot: name of the snapshot to validate facts for, defaults to the current snapshot
        :type snapshot: Text
        :return: facts about the specified nodes on the current network snapshot
        :rtype: dict
        """
        actual_facts = get_facts(self, snapshot=snapshot)
        expected_facts_ = load_facts(expected_facts)
        return validate_facts(expected_facts_, actual_facts)