netdevops / hier_config

Hierarchical Configuration
MIT License
128 stars 24 forks source link

[WIP] acl tests #81

Closed jtdub closed 4 years ago

jtdub commented 4 years ago
In [1]: from hier_config.host import Host

In [2]: import yaml

In [3]: options = yaml.load(open('./tests/files/test_options_ios.yml'), Loader=yaml.SafeLoader)

In [4]: host = Host('example.rtr', 'ios', options)

In [5]: host.load_config_from("running", './tests/files/running_config.conf')
Out[5]: HConfig(host=Host(hostname=example.rtr))

In [6]: host.load_config_from("compiled", './tests/files/compiled_config.conf')
Out[6]: HConfig(host=Host(hostname=example.rtr))

In [7]: host.load_tags(name="./tests/files/test_tags_ios.yml", load_file=True)
Out[7]:
[{'add_tags': 'safe',
  'lineage': [{'equals': ['no ip http secure-server',
     'no ip http server',
     'vlan',
     'no vlan']}]},
 {'add_tags': 'safe',
  'lineage': [{'startswith': 'interface Vlan'},
   {'startswith': ['description']}]},
 {'add_tags': ['manual', 'acl'],
  'lineage': [{'startswith': ['ip access-list',
     'no ip access-list',
     'access-list',
     'no access-list']}]},
 {'add_tags': 'manual',
  'lineage': [{'startswith': 'interface Vlan'},
   {'startswith': ['ip address',
     'no ip address',
     'mtu',
     'no mtu',
     'ip access-group',
     'no ip access-group',
     'shutdown',
     'no shutdown']}]}]

In [9]: host.load_remediation()
Out[9]: HConfig(host=Host(hostname=example.rtr))

In [10]: host.filter_remediation(include_tags=["acl"])
Out[10]: 'ip access-list extended TEST\n  no 30 permit tcp 10.0.0.0/29 host 11.11.11.1 range 11005 11009\n'

In [11]: rem = host.filter_remediation(include_tags=["acl"])

In [12]: print(rem)
ip access-list extended TEST
  no 30 permit tcp 10.0.0.0/29 host 11.11.11.1 range 11005 11009