test_iosconfig.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Test the iosconfig Execution module.
  4. '''
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. # Import Python libs
  7. import textwrap
  8. # Import Salt Testing libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. # Import Salt modules
  12. from salt.utils.odict import OrderedDict
  13. import salt.modules.iosconfig as iosconfig
  14. class TestModulesIOSConfig(TestCase, LoaderModuleMockMixin):
  15. running_config = textwrap.dedent('''\
  16. interface GigabitEthernet1
  17. ip address dhcp
  18. negotiation auto
  19. no mop enabled
  20. !
  21. interface GigabitEthernet2
  22. ip address 172.20.0.1 255.255.255.0
  23. shutdown
  24. negotiation auto
  25. !
  26. interface GigabitEthernet3
  27. no ip address
  28. shutdown
  29. negotiation auto
  30. !''')
  31. candidate_config = textwrap.dedent('''\
  32. interface GigabitEthernet1
  33. ip address dhcp
  34. negotiation auto
  35. no mop enabled
  36. !
  37. interface GigabitEthernet2
  38. no ip address
  39. shutdown
  40. negotiation auto
  41. !
  42. interface GigabitEthernet3
  43. no ip address
  44. negotiation auto
  45. !
  46. router bgp 65000
  47. bgp log-neighbor-changes
  48. neighbor 1.1.1.1 remote-as 12345
  49. !
  50. !''')
  51. merge_config = textwrap.dedent('''\
  52. router bgp 65000
  53. bgp log-neighbor-changes
  54. neighbor 1.1.1.1 remote-as 12345
  55. !
  56. !
  57. virtual-service csr_mgmt
  58. !
  59. ip forward-protocol nd
  60. !''')
  61. def setup_loader_modules(self):
  62. return {}
  63. def test_tree(self):
  64. running_config_tree = OrderedDict([
  65. (u'interface GigabitEthernet1', OrderedDict([
  66. (u'ip address dhcp', OrderedDict()),
  67. (u'negotiation auto', OrderedDict()),
  68. (u'no mop enabled', OrderedDict())
  69. ])),
  70. (u'interface GigabitEthernet2', OrderedDict([
  71. (u'ip address 172.20.0.1 255.255.255.0', OrderedDict()),
  72. (u'shutdown', OrderedDict()),
  73. (u'negotiation auto', OrderedDict())
  74. ])),
  75. (u'interface GigabitEthernet3', OrderedDict([
  76. (u'no ip address', OrderedDict()),
  77. (u'shutdown', OrderedDict()),
  78. (u'negotiation auto', OrderedDict())
  79. ]))
  80. ])
  81. tree = iosconfig.tree(config=self.running_config)
  82. self.assertEqual(tree, running_config_tree)
  83. def test_clean(self):
  84. clean_running_config = textwrap.dedent('''\
  85. interface GigabitEthernet1
  86. ip address dhcp
  87. negotiation auto
  88. no mop enabled
  89. interface GigabitEthernet2
  90. ip address 172.20.0.1 255.255.255.0
  91. shutdown
  92. negotiation auto
  93. interface GigabitEthernet3
  94. no ip address
  95. shutdown
  96. negotiation auto
  97. ''')
  98. clean = iosconfig.clean(config=self.running_config)
  99. self.assertEqual(clean, clean_running_config)
  100. def test_merge_tree(self):
  101. expected_merge_tree = OrderedDict([
  102. (u'interface GigabitEthernet1', OrderedDict([
  103. (u'ip address dhcp', OrderedDict()),
  104. (u'negotiation auto', OrderedDict()),
  105. (u'no mop enabled', OrderedDict())
  106. ])),
  107. (u'interface GigabitEthernet2', OrderedDict([
  108. (u'ip address 172.20.0.1 255.255.255.0', OrderedDict()),
  109. (u'shutdown', OrderedDict()),
  110. (u'negotiation auto', OrderedDict())
  111. ])),
  112. (u'interface GigabitEthernet3', OrderedDict([
  113. (u'no ip address', OrderedDict()),
  114. (u'shutdown', OrderedDict()),
  115. (u'negotiation auto', OrderedDict())
  116. ])),
  117. (u'router bgp 65000', OrderedDict([
  118. (u'bgp log-neighbor-changes', OrderedDict()),
  119. (u'neighbor 1.1.1.1 remote-as 12345', OrderedDict())
  120. ])),
  121. (u'virtual-service csr_mgmt', OrderedDict()),
  122. (u'ip forward-protocol nd', OrderedDict())
  123. ])
  124. merge_tree = iosconfig.merge_tree(initial_config=self.running_config,
  125. merge_config=self.merge_config)
  126. self.assertEqual(merge_tree, expected_merge_tree)
  127. def test_merge_text(self):
  128. extected_merge_text = textwrap.dedent('''\
  129. interface GigabitEthernet1
  130. ip address dhcp
  131. negotiation auto
  132. no mop enabled
  133. interface GigabitEthernet2
  134. ip address 172.20.0.1 255.255.255.0
  135. shutdown
  136. negotiation auto
  137. interface GigabitEthernet3
  138. no ip address
  139. shutdown
  140. negotiation auto
  141. router bgp 65000
  142. bgp log-neighbor-changes
  143. neighbor 1.1.1.1 remote-as 12345
  144. virtual-service csr_mgmt
  145. ip forward-protocol nd
  146. ''')
  147. merge_text = iosconfig.merge_text(initial_config=self.running_config,
  148. merge_config=self.merge_config)
  149. self.assertEqual(merge_text, extected_merge_text)
  150. def test_merge_diff(self):
  151. expected_diff = textwrap.dedent('''\
  152. @@ -10,3 +10,8 @@
  153. no ip address
  154. shutdown
  155. negotiation auto
  156. +router bgp 65000
  157. + bgp log-neighbor-changes
  158. + neighbor 1.1.1.1 remote-as 12345
  159. +virtual-service csr_mgmt
  160. +ip forward-protocol nd
  161. ''')
  162. diff = iosconfig.merge_diff(initial_config=self.running_config,
  163. merge_config=self.merge_config)
  164. self.assertEqual(diff.splitlines()[2:], expected_diff.splitlines())
  165. def test_diff_text(self):
  166. expected_diff = textwrap.dedent('''\
  167. @@ -3,10 +3,12 @@
  168. negotiation auto
  169. no mop enabled
  170. interface GigabitEthernet2
  171. - ip address 172.20.0.1 255.255.255.0
  172. + no ip address
  173. shutdown
  174. negotiation auto
  175. interface GigabitEthernet3
  176. no ip address
  177. - shutdown
  178. negotiation auto
  179. +router bgp 65000
  180. + bgp log-neighbor-changes
  181. + neighbor 1.1.1.1 remote-as 12345
  182. ''')
  183. diff = iosconfig.diff_text(candidate_config=self.candidate_config,
  184. running_config=self.running_config)
  185. self.assertEqual(diff.splitlines()[2:], expected_diff.splitlines())