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 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_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))