test_iosconfig.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.modules.iosconfig as iosconfig
  9. # Import Salt modules
  10. from salt.utils.odict import OrderedDict
  11. # Import Salt Testing libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.unit import TestCase
  14. class TestModulesIOSConfig(TestCase, LoaderModuleMockMixin):
  15. running_config = textwrap.dedent(
  16. """\
  17. interface GigabitEthernet1
  18. ip address dhcp
  19. negotiation auto
  20. no mop enabled
  21. !
  22. interface GigabitEthernet2
  23. ip address 172.20.0.1 255.255.255.0
  24. shutdown
  25. negotiation auto
  26. !
  27. interface GigabitEthernet3
  28. no ip address
  29. shutdown
  30. negotiation auto
  31. !"""
  32. )
  33. candidate_config = textwrap.dedent(
  34. """\
  35. interface GigabitEthernet1
  36. ip address dhcp
  37. negotiation auto
  38. no mop enabled
  39. !
  40. interface GigabitEthernet2
  41. no ip address
  42. shutdown
  43. negotiation auto
  44. !
  45. interface GigabitEthernet3
  46. no ip address
  47. negotiation auto
  48. !
  49. router bgp 65000
  50. bgp log-neighbor-changes
  51. neighbor 1.1.1.1 remote-as 12345
  52. !
  53. !"""
  54. )
  55. merge_config = textwrap.dedent(
  56. """\
  57. router bgp 65000
  58. bgp log-neighbor-changes
  59. neighbor 1.1.1.1 remote-as 12345
  60. !
  61. !
  62. virtual-service csr_mgmt
  63. !
  64. ip forward-protocol nd
  65. !"""
  66. )
  67. def setup_loader_modules(self):
  68. return {}
  69. def test_tree(self):
  70. running_config_tree = OrderedDict(
  71. [
  72. (
  73. "interface GigabitEthernet1",
  74. OrderedDict(
  75. [
  76. ("ip address dhcp", OrderedDict()),
  77. ("negotiation auto", OrderedDict()),
  78. ("no mop enabled", OrderedDict()),
  79. ]
  80. ),
  81. ),
  82. (
  83. "interface GigabitEthernet2",
  84. OrderedDict(
  85. [
  86. ("ip address 172.20.0.1 255.255.255.0", OrderedDict()),
  87. ("shutdown", OrderedDict()),
  88. ("negotiation auto", OrderedDict()),
  89. ]
  90. ),
  91. ),
  92. (
  93. "interface GigabitEthernet3",
  94. OrderedDict(
  95. [
  96. ("no ip address", OrderedDict()),
  97. ("shutdown", OrderedDict()),
  98. ("negotiation auto", OrderedDict()),
  99. ]
  100. ),
  101. ),
  102. ]
  103. )
  104. tree = iosconfig.tree(config=self.running_config)
  105. self.assertEqual(tree, running_config_tree)
  106. def test_clean(self):
  107. clean_running_config = textwrap.dedent(
  108. """\
  109. interface GigabitEthernet1
  110. ip address dhcp
  111. negotiation auto
  112. no mop enabled
  113. interface GigabitEthernet2
  114. ip address 172.20.0.1 255.255.255.0
  115. shutdown
  116. negotiation auto
  117. interface GigabitEthernet3
  118. no ip address
  119. shutdown
  120. negotiation auto
  121. """
  122. )
  123. clean = iosconfig.clean(config=self.running_config)
  124. self.assertEqual(clean, clean_running_config)
  125. def test_merge_tree(self):
  126. expected_merge_tree = OrderedDict(
  127. [
  128. (
  129. "interface GigabitEthernet1",
  130. OrderedDict(
  131. [
  132. ("ip address dhcp", OrderedDict()),
  133. ("negotiation auto", OrderedDict()),
  134. ("no mop enabled", OrderedDict()),
  135. ]
  136. ),
  137. ),
  138. (
  139. "interface GigabitEthernet2",
  140. OrderedDict(
  141. [
  142. ("ip address 172.20.0.1 255.255.255.0", OrderedDict()),
  143. ("shutdown", OrderedDict()),
  144. ("negotiation auto", OrderedDict()),
  145. ]
  146. ),
  147. ),
  148. (
  149. "interface GigabitEthernet3",
  150. OrderedDict(
  151. [
  152. ("no ip address", OrderedDict()),
  153. ("shutdown", OrderedDict()),
  154. ("negotiation auto", OrderedDict()),
  155. ]
  156. ),
  157. ),
  158. (
  159. "router bgp 65000",
  160. OrderedDict(
  161. [
  162. ("bgp log-neighbor-changes", OrderedDict()),
  163. ("neighbor 1.1.1.1 remote-as 12345", OrderedDict()),
  164. ]
  165. ),
  166. ),
  167. ("virtual-service csr_mgmt", OrderedDict()),
  168. ("ip forward-protocol nd", OrderedDict()),
  169. ]
  170. )
  171. merge_tree = iosconfig.merge_tree(
  172. initial_config=self.running_config, merge_config=self.merge_config
  173. )
  174. self.assertEqual(merge_tree, expected_merge_tree)
  175. def test_merge_text(self):
  176. extected_merge_text = textwrap.dedent(
  177. """\
  178. interface GigabitEthernet1
  179. ip address dhcp
  180. negotiation auto
  181. no mop enabled
  182. interface GigabitEthernet2
  183. ip address 172.20.0.1 255.255.255.0
  184. shutdown
  185. negotiation auto
  186. interface GigabitEthernet3
  187. no ip address
  188. shutdown
  189. negotiation auto
  190. router bgp 65000
  191. bgp log-neighbor-changes
  192. neighbor 1.1.1.1 remote-as 12345
  193. virtual-service csr_mgmt
  194. ip forward-protocol nd
  195. """
  196. )
  197. merge_text = iosconfig.merge_text(
  198. initial_config=self.running_config, merge_config=self.merge_config
  199. )
  200. self.assertEqual(merge_text, extected_merge_text)
  201. def test_merge_diff(self):
  202. expected_diff = textwrap.dedent(
  203. """\
  204. @@ -10,3 +10,8 @@
  205. no ip address
  206. shutdown
  207. negotiation auto
  208. +router bgp 65000
  209. + bgp log-neighbor-changes
  210. + neighbor 1.1.1.1 remote-as 12345
  211. +virtual-service csr_mgmt
  212. +ip forward-protocol nd
  213. """
  214. )
  215. diff = iosconfig.merge_diff(
  216. initial_config=self.running_config, merge_config=self.merge_config
  217. )
  218. self.assertEqual(diff.splitlines()[2:], expected_diff.splitlines())
  219. def test_diff_text(self):
  220. expected_diff = textwrap.dedent(
  221. """\
  222. @@ -3,10 +3,12 @@
  223. negotiation auto
  224. no mop enabled
  225. interface GigabitEthernet2
  226. - ip address 172.20.0.1 255.255.255.0
  227. + no ip address
  228. shutdown
  229. negotiation auto
  230. interface GigabitEthernet3
  231. no ip address
  232. - shutdown
  233. negotiation auto
  234. +router bgp 65000
  235. + bgp log-neighbor-changes
  236. + neighbor 1.1.1.1 remote-as 12345
  237. """
  238. )
  239. diff = iosconfig.diff_text(
  240. candidate_config=self.candidate_config, running_config=self.running_config
  241. )
  242. self.assertEqual(diff.splitlines()[2:], expected_diff.splitlines())