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_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"]
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_difference(self): rc = ['a', ' a1', ' a2', ' a3', 'b'] step = ['a', ' a1', ' a2', ' a3', ' a4', ' a5', 'b', 'c', 'd', ' d1'] rc_hier = HConfig(host=self.host_a) rc_hier.load_from_string("\n".join(rc)) step_hier = HConfig(host=self.host_a) 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()) self.assertEqual(len(difference_children), 6) self.assertIn('c', difference) self.assertIn('d', difference) self.assertIn('a4', difference.get_child('equals', 'a')) self.assertIn('a5', difference.get_child('equals', 'a')) self.assertIn('d1', difference.get_child('equals', 'd'))
def test_difference1(self): rc = ["a", " a1", " a2", " a3", "b"] step = ["a", " a1", " a2", " a3", " a4", " a5", "b", "c", "d", " d1"] rc_hier = HConfig(host=self.host_a) rc_hier.load_from_string("\n".join(rc)) step_hier = HConfig(host=self.host_a) 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 assert "c" in difference assert "d" in difference assert "a4" in difference.get_child("equals", "a") assert "a5" in difference.get_child("equals", "a") assert "d1" in difference.get_child("equals", "d")
def test_issue104() -> None: running_config_raw = ("tacacs-server deadtime 3\n" "tacacs-server host 192.168.1.99 key 7 Test12345\n") generated_config_raw = ( "tacacs-server host 192.168.1.98 key 0 Test135 timeout 3\n" "tacacs-server host 192.168.100.98 key 0 test135 timeout 3\n") host = Host(hostname="test", os="nxos") running_config = HConfig(host=host) running_config.load_from_string(running_config_raw) generated_config = HConfig(host=host) generated_config.load_from_string(generated_config_raw) rem = running_config.config_to_get_to(generated_config) expected_rem_lines = { "no tacacs-server deadtime 3", "no tacacs-server host 192.168.1.99 key 7 Test12345", "tacacs-server host 192.168.1.98 key 0 Test135 timeout 3", "tacacs-server host 192.168.100.98 key 0 test135 timeout 3", } rem_lines = {line.cisco_style_text() for line in rem.all_children()} assert expected_rem_lines == rem_lines
def test_load_from_config_text(self): hier = HConfig(host=self.host_a) config = 'interface Vlan2\n ip address 1.1.1.1 255.255.255.0' hier.load_from_string(config) self.assertEqual(2, len(list(hier.all_children())))