123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- # -*- coding: utf-8 -*-
- """
- Test the iosconfig Execution module.
- """
- from __future__ import absolute_import, print_function, unicode_literals
- # Import Python libs
- import textwrap
- import salt.modules.iosconfig as iosconfig
- # Import Salt modules
- from salt.utils.odict import OrderedDict
- # Import Salt Testing libs
- from tests.support.mixins import LoaderModuleMockMixin
- from tests.support.unit import TestCase
- class TestModulesIOSConfig(TestCase, LoaderModuleMockMixin):
- running_config = textwrap.dedent(
- """\
- interface GigabitEthernet1
- ip address dhcp
- negotiation auto
- no mop enabled
- !
- interface GigabitEthernet2
- ip address 172.20.0.1 255.255.255.0
- shutdown
- negotiation auto
- !
- interface GigabitEthernet3
- no ip address
- shutdown
- negotiation auto
- !"""
- )
- candidate_config = textwrap.dedent(
- """\
- interface GigabitEthernet1
- ip address dhcp
- negotiation auto
- no mop enabled
- !
- interface GigabitEthernet2
- no ip address
- shutdown
- negotiation auto
- !
- interface GigabitEthernet3
- no ip address
- negotiation auto
- !
- router bgp 65000
- bgp log-neighbor-changes
- neighbor 1.1.1.1 remote-as 12345
- !
- !"""
- )
- merge_config = textwrap.dedent(
- """\
- router bgp 65000
- bgp log-neighbor-changes
- neighbor 1.1.1.1 remote-as 12345
- !
- !
- virtual-service csr_mgmt
- !
- ip forward-protocol nd
- !"""
- )
- def setup_loader_modules(self):
- return {}
- def test_tree(self):
- running_config_tree = OrderedDict(
- [
- (
- "interface GigabitEthernet1",
- OrderedDict(
- [
- ("ip address dhcp", OrderedDict()),
- ("negotiation auto", OrderedDict()),
- ("no mop enabled", OrderedDict()),
- ]
- ),
- ),
- (
- "interface GigabitEthernet2",
- OrderedDict(
- [
- ("ip address 172.20.0.1 255.255.255.0", OrderedDict()),
- ("shutdown", OrderedDict()),
- ("negotiation auto", OrderedDict()),
- ]
- ),
- ),
- (
- "interface GigabitEthernet3",
- OrderedDict(
- [
- ("no ip address", OrderedDict()),
- ("shutdown", OrderedDict()),
- ("negotiation auto", OrderedDict()),
- ]
- ),
- ),
- ]
- )
- tree = iosconfig.tree(config=self.running_config)
- self.assertEqual(tree, running_config_tree)
- def test_clean(self):
- clean_running_config = textwrap.dedent(
- """\
- interface GigabitEthernet1
- ip address dhcp
- negotiation auto
- no mop enabled
- interface GigabitEthernet2
- ip address 172.20.0.1 255.255.255.0
- shutdown
- negotiation auto
- interface GigabitEthernet3
- no ip address
- shutdown
- negotiation auto
- """
- )
- clean = iosconfig.clean(config=self.running_config)
- self.assertEqual(clean, clean_running_config)
- def test_merge_tree(self):
- expected_merge_tree = OrderedDict(
- [
- (
- "interface GigabitEthernet1",
- OrderedDict(
- [
- ("ip address dhcp", OrderedDict()),
- ("negotiation auto", OrderedDict()),
- ("no mop enabled", OrderedDict()),
- ]
- ),
- ),
- (
- "interface GigabitEthernet2",
- OrderedDict(
- [
- ("ip address 172.20.0.1 255.255.255.0", OrderedDict()),
- ("shutdown", OrderedDict()),
- ("negotiation auto", OrderedDict()),
- ]
- ),
- ),
- (
- "interface GigabitEthernet3",
- OrderedDict(
- [
- ("no ip address", OrderedDict()),
- ("shutdown", OrderedDict()),
- ("negotiation auto", OrderedDict()),
- ]
- ),
- ),
- (
- "router bgp 65000",
- OrderedDict(
- [
- ("bgp log-neighbor-changes", OrderedDict()),
- ("neighbor 1.1.1.1 remote-as 12345", OrderedDict()),
- ]
- ),
- ),
- ("virtual-service csr_mgmt", OrderedDict()),
- ("ip forward-protocol nd", OrderedDict()),
- ]
- )
- merge_tree = iosconfig.merge_tree(
- initial_config=self.running_config, merge_config=self.merge_config
- )
- self.assertEqual(merge_tree, expected_merge_tree)
- def test_merge_text(self):
- extected_merge_text = textwrap.dedent(
- """\
- interface GigabitEthernet1
- ip address dhcp
- negotiation auto
- no mop enabled
- interface GigabitEthernet2
- ip address 172.20.0.1 255.255.255.0
- shutdown
- negotiation auto
- interface GigabitEthernet3
- no ip address
- shutdown
- negotiation auto
- router bgp 65000
- bgp log-neighbor-changes
- neighbor 1.1.1.1 remote-as 12345
- virtual-service csr_mgmt
- ip forward-protocol nd
- """
- )
- merge_text = iosconfig.merge_text(
- initial_config=self.running_config, merge_config=self.merge_config
- )
- self.assertEqual(merge_text, extected_merge_text)
- def test_merge_diff(self):
- expected_diff = textwrap.dedent(
- """\
- @@ -10,3 +10,8 @@
- no ip address
- shutdown
- negotiation auto
- +router bgp 65000
- + bgp log-neighbor-changes
- + neighbor 1.1.1.1 remote-as 12345
- +virtual-service csr_mgmt
- +ip forward-protocol nd
- """
- )
- diff = iosconfig.merge_diff(
- initial_config=self.running_config, merge_config=self.merge_config
- )
- self.assertEqual(diff.splitlines()[2:], expected_diff.splitlines())
- def test_diff_text(self):
- expected_diff = textwrap.dedent(
- """\
- @@ -3,10 +3,12 @@
- negotiation auto
- no mop enabled
- interface GigabitEthernet2
- - ip address 172.20.0.1 255.255.255.0
- + no ip address
- shutdown
- negotiation auto
- interface GigabitEthernet3
- no ip address
- - shutdown
- negotiation auto
- +router bgp 65000
- + bgp log-neighbor-changes
- + neighbor 1.1.1.1 remote-as 12345
- """
- )
- diff = iosconfig.diff_text(
- candidate_config=self.candidate_config, running_config=self.running_config
- )
- self.assertEqual(diff.splitlines()[2:], expected_diff.splitlines())
|