Skip to content

Commit

Permalink
Merge pull request #59 from jmcgill298/py2_bool
Browse files Browse the repository at this point in the history
Bug Fix: Python2 does not accurately compare running and compiled configs
  • Loading branch information
jtdub authored Jun 18, 2018
2 parents b1fa03b + 8dce88e commit 4f5c0a5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion hier_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import re

__version__ = '1.4.1'
__version__ = '1.4.2'


class HConfig(HConfigChild):
Expand Down
5 changes: 4 additions & 1 deletion hier_config/hc_child.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def __len__(self):
def __bool__(self):
return True

def __nonzero__(self):
return self.__bool__()

def __contains__(self, item):
return str(item) in self.children_dict

Expand Down Expand Up @@ -590,7 +593,7 @@ def _config_to_get_to_right(self, target, delta):
for target_child in target.children:
# if the child exist, recurse into its children
self_child = self.get_child('equals', target_child.text)
if self_child:
if self_child is not None:
# This creates a new HConfigChild object just in case there are some delta children
# Not very efficient, think of a way to not do this
subtree = delta.add_child(target_child.text)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_hier_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def setUpClass(cls):
cls.host_a = Host('example1.rtr', cls.os, cls.options)
cls.host_b = Host('example2.rtr', cls.os, cls.options)

def test_bool(self):
self.assertTrue(HConfig(host=self.host_a))

def test_merge(self):
hier1 = HConfig(host=self.host_a)
hier1.add_child('interface Vlan2')
Expand Down Expand Up @@ -332,6 +335,17 @@ def test_config_to_get_to(self):
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_config_to_get_to_right(self):
running_config_hier = HConfig(host=self.host_a)
running_config_hier.add_child('do not add me')
compiled_config_hier = HConfig(host=self.host_a)
compiled_config_hier.add_child('do not add me')
compiled_config_hier.add_child('add me')
delta = HConfig(host=self.host_a)
running_config_hier._config_to_get_to_right(compiled_config_hier, delta)
self.assertNotIn('do not add me', delta)
self.assertIn('add me', delta)

def test_is_idempotent_command(self):
pass

Expand Down

0 comments on commit 4f5c0a5

Please sign in to comment.