def test_add_sectional_exiting(self): hier = HConfig(host=self.host_a) bgp = hier.add_child("router bgp 64500") template = bgp.add_child("template peer-policy") hier.add_sectional_exiting() sectional_exit = template.get_child("equals", "exit-peer-policy") assert sectional_exit is not None
def test_cisco_style_text(self): hier = HConfig(host=self.host_a) interface = hier.add_child("interface Vlan2") ip_address = interface.add_child("ip address 192.168.1.1 255.255.255.0") assert ip_address.cisco_style_text() == " ip address 192.168.1.1 255.255.255.0" assert isinstance(ip_address.cisco_style_text(), str) assert not isinstance(ip_address.cisco_style_text(), list)
def difference(self, target, delta=None): """ Creates a new HConfig object with the config from self that is not in target Example usage: whats in the config.lines v.s. in running config i.e. did all my configuration changes get written to the running config :param target: HConfig - The configuration to check against :param delta: HConfig - The elements from self that are not in target :return: HConfig - missing config additions """ if delta is None: from hier_config import HConfig delta = HConfig(host=self.host) for self_child in self.children: # Not dealing with negations and defaults for now if self_child.text.startswith(('no ', 'default ')): continue target_child = target.get_child('equals', self_child.text) if target_child: delta_child = delta.add_child(self_child.text) self_child.difference(target_child, delta_child) if not delta_child.children: delta_child.delete() else: delta.add_deep_copy_of(self_child) return delta
def load_config_from(self, config_type, name, file=True): """ 1. Loads a running config or a compiled config into a Host object 2. Sets host.facts['running_config_raw'] or host.facts['compiled_config_raw'] 3. Loads the config into HConfig 4. Sets the loaded hier-config in host.facts['running_config'] or host.facts['compiled_config'] :param config_type: 'running' or 'compiled' -> type str :param name: file name or config text string to load -> type str :param file: default, True -> type boolean :return: self.running_config or self.compiled_config """ hier = HConfig(host=self) if file: config_text = self._load_from_file(name) else: config_text = name hier.load_from_string(config_text) if config_type == "running": self.facts["running_config_raw"] = config_text self._running_config = hier return self.running_config elif config_type == "compiled": self.facts["compiled_config_raw"] = config_text self._compiled_config = hier return self.compiled_config else: raise SyntaxError( "Unknown config_type. Expected 'running' or 'compiled'")
def test_add_sectional_exiting(self): hier = HConfig(host=self.host_a) bgp = hier.add_child('router bgp 64500') template = bgp.add_child('template peer-policy') hier.add_sectional_exiting() sectional_exit = template.get_child('equals', 'exit-peer-policy') self.assertIsNotNone(sectional_exit)
def test_append_tags(self): config = HConfig(host=self.host_a) interface = config.add_child('interface Vlan2') ip_address = interface.add_child('ip address 192.168.1.1/24') ip_address.append_tags('test_tag') self.assertIn('test_tag', config.tags) self.assertIn('test_tag', interface.tags) self.assertIn('test_tag', ip_address.tags)
def test_get_children(self): hier = HConfig(host=self.host_a) hier.add_child("interface Vlan2") hier.add_child("interface Vlan3") children = list(hier.get_children("startswith", "interface")) assert len(children) == 2 for child in children: assert child.text.startswith("interface Vlan")
def test_append_tags(self): config = HConfig(host=self.host_a) interface = config.add_child("interface Vlan2") ip_address = interface.add_child("ip address 192.168.1.1/24") ip_address.append_tags("test_tag") assert "test_tag" in config.tags assert "test_tag" in interface.tags assert "test_tag" in ip_address.tags
def test_get_child_deep(self): hier = HConfig(host=self.host_a) interface = hier.add_child('interface Vlan2') interface.add_child('ip address 192.168.1.1 255.255.255.0') child = hier.get_child_deep([('equals', 'interface Vlan2'), ('equals', 'ip address 192.168.1.1 255.255.255.0')]) self.assertIsNotNone(child)
def test_add_tags(self): hier = HConfig(host=self.host_a) tag_rules = [{"lineage": [{"equals": "interface Vlan2"}], "add_tags": "test"}] child = hier.add_child("interface Vlan2") hier.add_tags(tag_rules) assert {"test"} == child.tags
def test_load_from_file(self): hier = HConfig(host=self.host_a) config = 'interface Vlan2\n ip address 1.1.1.1 255.255.255.0' with tempfile.NamedTemporaryFile(mode='r+') as myfile: myfile.file.write(config) myfile.file.flush() hier.load_from_file(myfile.name) self.assertEqual(2, len(list(hier.all_children())))
def test_line_inclusion_test(self): config = HConfig(host=self.host_a) interface = config.add_child("interface Vlan2") ip_address_ab = interface.add_child("ip address 192.168.2.1/24") ip_address_ab.append_tags(["a", "b"]) assert not ip_address_ab.line_inclusion_test({"a"}, {"b"}) assert not ip_address_ab.line_inclusion_test(set(), {"a"}) assert ip_address_ab.line_inclusion_test({"a"}, set()) assert not ip_address_ab.line_inclusion_test(set(), set())
def test_line_inclusion_test(self): config = HConfig(host=self.host_a) interface = config.add_child('interface Vlan2') ip_address_ab = interface.add_child('ip address 192.168.2.1/24') ip_address_ab.append_tags(['a', 'b']) self.assertFalse(ip_address_ab.line_inclusion_test('a', 'b')) self.assertFalse(ip_address_ab.line_inclusion_test('', 'a')) self.assertTrue(ip_address_ab.line_inclusion_test('a', '')) self.assertFalse(ip_address_ab.line_inclusion_test('', ''))
def test_rebuild_children_dict(self): hier1 = HConfig(host=self.host_a) interface = hier1.add_child("interface Vlan2") interface.add_children( ["description switch-mgmt-192.168.1.0/24", "ip address 192.168.1.0/24"] ) delta_a = hier1 hier1.rebuild_children_dict() delta_b = hier1 assert list(delta_a.all_children()) == list(delta_b.all_children())
def test_tags(self): config = HConfig(host=self.host_a) interface = config.add_child('interface Vlan2') ip_address = interface.add_child('ip address 192.168.1.1/24') self.assertTrue(None in interface.tags) self.assertTrue(None in ip_address.tags) ip_address.append_tags('a') self.assertTrue('a' in interface.tags) self.assertTrue('a' in ip_address.tags) self.assertFalse('b' in interface.tags) self.assertFalse('b' in ip_address.tags)
def test_get_child_deep(self): hier = HConfig(host=self.host_a) interface = hier.add_child("interface Vlan2") interface.add_child("ip address 192.168.1.1 255.255.255.0") child = hier.get_child_deep( [ ("equals", "interface Vlan2"), ("equals", "ip address 192.168.1.1 255.255.255.0"), ] ) assert child is not None
def test_cisco_style_text(self): hier = HConfig(host=self.host_a) interface = hier.add_child('interface Vlan2') ip_address = interface.add_child( 'ip address 192.168.1.1 255.255.255.0') self.assertEqual(' ip address 192.168.1.1 255.255.255.0', ip_address.cisco_style_text()) self.assertNotEqual(' ip address 192.168.1.1 255.255.255.0', ip_address.cisco_style_text()) self.assertTrue(isinstance(ip_address.cisco_style_text(), str)) self.assertFalse(isinstance(ip_address.cisco_style_text(), list))
def test_config_to_get_to(self): running_config_hier = HConfig(self.host_a, self.os, self.options) running_config_hier.add_child('interface Vlan2') compiled_config_hier = HConfig(self.host_a, self.os, self.options) compiled_config_hier.add_child('interface Vlan3') remediation_config_hier = running_config_hier.config_to_get_to( compiled_config_hier) self.assertEqual(2, len(list(remediation_config_hier.all_children())))
def test_load_from_file(self): hier = HConfig(host=self.host_a) config = "interface Vlan2\n ip address 1.1.1.1 255.255.255.0" with tempfile.NamedTemporaryFile(mode="r+", delete=False) as myfile: myfile.file.write(config) myfile.file.flush() myfile.close() hier.load_from_file(myfile.name) os.remove(myfile.name) assert len(list(hier.all_children())) == 2
def test_del_child(self): hier1 = HConfig(host=self.host_a) hier1.add_child("interface Vlan2") assert len(list(hier1.all_children())) == 1 hier1.del_child(hier1.get_child("startswith", "interface")) assert len(list(hier1.all_children())) == 0
def test_del_child(self): hier1 = HConfig(host=self.host_a) hier1.add_child('interface Vlan2') self.assertEqual(1, len(list(hier1.all_children()))) hier1.del_child(hier1.get_child('startswith', 'interface')) self.assertEqual(0, len(list(hier1.all_children())))
def test_add_tags(self): hier = HConfig(host=self.host_a) tag_rules = [{ 'lineage': [{ 'equals': 'interface Vlan2' }], 'add_tags': 'test' }] child = hier.add_child('interface Vlan2') hier.add_tags(tag_rules) self.assertEqual({'test'}, child.tags)
def test_rebuild_children_dict(self): hier1 = HConfig(host=self.host_a) interface = hier1.add_child('interface Vlan2') interface.add_children([ 'description switch-mgmt-192.168.1.0/24', 'ip address 192.168.1.0/24' ]) delta_a = hier1 hier1.rebuild_children_dict() delta_b = hier1 self.assertEqual(list(delta_a.all_children()), list(delta_b.all_children()))
def test_add_deep_copy_of(self): hier1 = HConfig(host=self.host_a) interface1 = hier1.add_child("interface Vlan2") interface1.add_children( ["description switch-mgmt-192.168.1.0/24", "ip address 192.168.1.0/24"] ) hier2 = HConfig(host=self.host_b) hier2.add_deep_copy_of(interface1) assert len(list(hier2.all_children())) == 3 assert isinstance(hier2.all_children(), types.GeneratorType)
def test_config_to_get_to(self): running_config_hier = HConfig(host=self.host_a) interface = running_config_hier.add_child('interface Vlan2') interface.add_child('ip address 192.168.1.1/24') compiled_config_hier = HConfig(host=self.host_a) compiled_config_hier.add_child('interface Vlan3') remediation_config_hier = running_config_hier.config_to_get_to( compiled_config_hier) self.assertEqual(2, len(list(remediation_config_hier.all_children())))
def test_add_deep_copy_of(self): hier1 = HConfig(host=self.host_a) interface1 = hier1.add_child('interface Vlan2') interface1.add_children([ 'description switch-mgmt-192.168.1.0/24', 'ip address 192.168.1.0/24' ]) hier2 = HConfig(host=self.host_b) hier2.add_deep_copy_of(interface1) self.assertEqual(3, len(list(hier2.all_children()))) self.assertTrue(isinstance(hier2.all_children(), types.GeneratorType))
def with_tags(self, tags, new_instance=None): """ Returns a new instance containing only sub-objects with one of the tags in tags """ from hier_config import HConfig if new_instance is None: new_instance = HConfig(host=self.host) for child in self.children: if tags.intersection(self.tags): new_child = new_instance.add_shallow_copy_of(child) child.with_tags(tags, new_instance=new_child) return new_instance
def test_config_to_get_to(self): running_config_hier = HConfig(host=self.host_a) interface = running_config_hier.add_child("interface Vlan2") interface.add_child("ip address 192.168.1.1/24") generated_config_hier = HConfig(host=self.host_a) generated_config_hier.add_child("interface Vlan3") remediation_config_hier = running_config_hier.config_to_get_to( generated_config_hier ) assert len(list(remediation_config_hier.all_children())) == 2
def test_difference2(options_ios): host = Host(hostname="test_host", os="ios", hconfig_options=options_ios) rc = ["a", " a1", " a2", " a3", "b"] step = ["a", " a1", " a2", " a3", " a4", " a5", "b", "c", "d", " d1"] rc_hier = HConfig(host=host) rc_hier.load_from_string("\n".join(rc)) step_hier = HConfig(host=host) step_hier.load_from_string("\n".join(step)) difference = step_hier.difference(rc_hier) difference_children = list( c.cisco_style_text() for c in difference.all_children_sorted() ) assert len(difference_children) == 6
def test_difference3(options_ios): host = Host(hostname="test_host", os="ios", hconfig_options=options_ios) rc = ["ip access-list extended test", " 10 a", " 20 b"] step = ["ip access-list extended test", " 10 a", " 20 b", " 30 c"] rc_hier = HConfig(host=host) rc_hier.load_from_string("\n".join(rc)) step_hier = HConfig(host=host) step_hier.load_from_string("\n".join(step)) difference = step_hier.difference(rc_hier) difference_children = list( c.cisco_style_text() for c in difference.all_children_sorted() ) assert difference_children == ["ip access-list extended test", " 30 c"]