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
def test_write_facts(tmpdir): """Test that writing facts writes nodes' facts to individual files.""" nodes = {"node1": "foo", "node2": "bar"} version = "version" facts = _encapsulate_nodes_facts(nodes, version) write_facts(str(tmpdir), facts) for node in nodes: filename = node + ".yml" file_path = str(tmpdir.join(filename)) assert os.path.isfile(file_path) with open(file_path) as f: node_facts_raw = yaml.safe_load(f.read()) node_facts, node_version = _unencapsulate_facts(node_facts_raw) assert version == node_version, "Each file has the correct version" assert (node_facts.get(node) == nodes[node] ), "Each file has the correct facts"
def test_write_facts(tmpdir): """Test that writing facts writes nodes' facts to individual files.""" nodes = {'node1': 'foo', 'node2': 'bar'} version = 'version' facts = _encapsulate_nodes_facts(nodes, version) write_facts(str(tmpdir), facts) for node in nodes: filename = node + '.yml' file_path = str(tmpdir.join(filename)) assert os.path.isfile(file_path) with open(file_path) as f: node_facts_raw = yaml.safe_load(f.read()) node_facts, node_version = _unencapsulate_facts(node_facts_raw) assert version == node_version, 'Each file has the correct version' assert node_facts.get(node) == nodes[ node], 'Each file has the correct facts'