netdevops / hier_config

Hierarchical Configuration
MIT License
126 stars 24 forks source link

Bug Fix: Python2 does not accurately compare running and compiled configs #59

Closed jmcgill298 closed 6 years ago

jmcgill298 commented 6 years ago

Python2 does not accurately recognize compiled configs being present in running config.

I identified the issue in the truth test in HConfigChild._config_to_get_to_right().

  1. I believe the test is meant to identify whether an HConfig.child object or None is returned
  2. Based on above, change test to only test for None and not all truth tests
  3. HConfig.Child implements __bool__() magic method, which replaces Python2's __nonzero__() method.
  4. Add __nonzero__() magic method which returns __bool__()
  5. Add test that verifies HConfigChild objects evaluate to True
  6. Add test that validates HConfigChild._config_to_get_to_right() doesn't include a config line that is present in both running and compiled configs, and that a config line in compiled, but absent in running, is added.
jtdub commented 6 years ago

I incremented __version__.

I used hconfig-remediation-ansible to test the patch with python v2 and v3, noting differences:

(env) jamess-mbp:example jtdub$ md5 router1-remediation*.conf
MD5 (router1-remediation-post-patch-py2.conf) = 6081233c0dcfddd877c76a0fe7655649
MD5 (router1-remediation-post-patch-py3.conf) = 6081233c0dcfddd877c76a0fe7655649
MD5 (router1-remediation-pre-patch-py2.conf) = a90f0cc073927746947548b5564f8b56
MD5 (router1-remediation-pre-patch-py3.conf) = 6081233c0dcfddd877c76a0fe7655649

MD5 (router1-remediation-with-exclude-tags-post-patch-py2.conf) = 180b41f9c2f0a7113762f523794cdc7a
MD5 (router1-remediation-with-exclude-tags-post-patch-py3.conf) = 180b41f9c2f0a7113762f523794cdc7a
MD5 (router1-remediation-with-exclude-tags-pre-patch-py2.conf) = 4bf3c7486bc3c4bc85e5648cb6517d2c
MD5 (router1-remediation-with-exclude-tags-pre-patch-py3.conf) = 180b41f9c2f0a7113762f523794cdc7a

MD5 (router1-remediation-with-include-tags-post-patch-py2.conf) = 75cfa0df9b7167bc2d30218cc9a78a43
MD5 (router1-remediation-with-include-tags-post-patch-py3.conf) = 75cfa0df9b7167bc2d30218cc9a78a43
MD5 (router1-remediation-with-include-tags-pre-patch-py2.conf) = d4648409931fe52b2594d41e653b1ae7
MD5 (router1-remediation-with-include-tags-pre-patch-py3.conf) = 75cfa0df9b7167bc2d30218cc9a78a43

In all instances, the pre-patched python v2 run has a different checksum than all runs. Post patch, all checksums are the same. Nice catch.