1# -*- coding: utf-8 -*- 2# Copyright 2020 Red Hat 3# GNU General Public License v2.0+ 4# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 5 6############################################# 7# WARNING # 8############################################# 9# 10# This file is auto generated by the resource 11# module builder playbook. 12# 13# Do not edit this file manually. 14# 15# Changes to this file will be over written 16# by the resource module builder. 17# 18# Changes should be made in the model used to 19# generate this file or in the resource module 20# builder template. 21# 22############################################# 23 24from __future__ import absolute_import, division, print_function 25 26__metaclass__ = type 27 28from ansible_collections.junipernetworks.junos.tests.unit.compat.mock import ( 29 patch, 30) 31from ansible_collections.junipernetworks.junos.plugins.modules import ( 32 junos_ospfv2, 33) 34from ansible_collections.junipernetworks.junos.tests.unit.modules.utils import ( 35 set_module_args, 36) 37from .junos_module import TestJunosModule, load_fixture 38 39 40class TestJunosOspfv2Module(TestJunosModule): 41 module = junos_ospfv2 42 43 def setUp(self): 44 super(TestJunosOspfv2Module, self).setUp() 45 46 self.mock_lock_configuration = patch( 47 "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos.lock_configuration" 48 ) 49 self.lock_configuration = self.mock_lock_configuration.start() 50 51 self.mock_unlock_configuration = patch( 52 "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos.unlock_configuration" 53 ) 54 self.unlock_configuration = self.mock_unlock_configuration.start() 55 56 self.mock_load_config = patch( 57 "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.config.ospfv2.ospfv2.load_config" 58 ) 59 self.load_config = self.mock_load_config.start() 60 61 self.mock_commit_configuration = patch( 62 "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.config.ospfv2.ospfv2.commit_configuration" 63 ) 64 self.mock_commit_configuration = self.mock_commit_configuration.start() 65 66 self.mock_execute_show_command = patch( 67 "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.facts.ospfv2.ospfv2." 68 "Ospfv2Facts.get_connection" 69 ) 70 self.execute_show_command = self.mock_execute_show_command.start() 71 72 def tearDown(self): 73 super(TestJunosOspfv2Module, self).tearDown() 74 self.mock_load_config.stop() 75 self.mock_lock_configuration.stop() 76 self.mock_unlock_configuration.stop() 77 self.mock_commit_configuration.stop() 78 self.mock_execute_show_command.stop() 79 80 def load_fixtures( 81 self, commands=None, format="text", changed=False, filename=None 82 ): 83 def load_from_file(*args, **kwargs): 84 output = load_fixture("junos_ospfv2_config.cfg") 85 return output 86 87 self.execute_show_command.side_effect = load_from_file 88 89 def sort_ospf(self, entry_list): 90 entry_list.sort(key=lambda i: i.get("name")) 91 92 def test_junos_ospfv2_merged(self): 93 set_module_args( 94 dict( 95 config=[ 96 dict( 97 router_id="10.200.16.75", 98 areas=[ 99 dict( 100 area_id="0.0.0.100", 101 stub=dict(default_metric=200, set=True), 102 interfaces=[ 103 dict( 104 name="so-0/0/0.0", priority=3, metric=5 105 ) 106 ], 107 ) 108 ], 109 ) 110 ], 111 state="merged", 112 ) 113 ) 114 commands = [ 115 '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><nc:ospf>' 116 "<nc:area><nc:name>0.0.0.100</nc:name><nc:interface><nc:name>so-0/0/0.0</nc:name>" 117 "<nc:priority>3</nc:priority><nc:metric>5</nc:metric></nc:interface><nc:stub>" 118 "<nc:default-metric>200</nc:default-metric></nc:stub></nc:area></nc:ospf></nc:protocols>", 119 '<nc:routing-options xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">' 120 "<nc:router-id>10.200.16.75</nc:router-id></nc:routing-options>", 121 ] 122 result = self.execute_module(changed=True, commands=commands) 123 self.assertEqual(sorted(result["commands"]), sorted(commands)) 124 125 def test_junos_ospfv2_merged_bandwith(self): 126 set_module_args( 127 dict( 128 config=[ 129 dict( 130 router_id="10.200.16.77", 131 areas=[ 132 dict( 133 area_id="0.0.0.10", 134 interfaces=[ 135 dict( 136 name="so-0/0/0.0", 137 metric=5, 138 bandwidth_based_metrics=[ 139 dict(bandwidth="10g", metric=5) 140 ], 141 ) 142 ], 143 ) 144 ], 145 ) 146 ], 147 state="merged", 148 ) 149 ) 150 result = self.execute_module(changed=True) 151 self.assertIn( 152 '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">', 153 str(result["commands"]), 154 ) 155 self.assertIn( 156 '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">', 157 str(result["commands"]), 158 ) 159 self.assertIn("<nc:name>0.0.0.10</nc:name>", str(result["commands"])) 160 self.assertIn( 161 "<nc:interface><nc:name>so-0/0/0.0</nc:name>", 162 str(result["commands"]), 163 ) 164 self.assertIn( 165 "<nc:bandwidth-based-metrics><nc:bandwidth><nc:name>10g</nc:name>", 166 str(result["commands"]), 167 ) 168 self.assertIn("<nc:metric>5</nc:metric>", str(result["commands"])) 169 170 def test_junos_ospfv2_merged_02(self): 171 set_module_args( 172 dict( 173 config=[ 174 dict( 175 router_id="10.200.16.75", 176 areas=[ 177 dict( 178 area_id="0.0.0.100", 179 area_range="3:3::/64", 180 stub=dict(set=True), 181 interfaces=[ 182 dict( 183 name="so-0/0/0.0", 184 priority=3, 185 metric=5, 186 flood_reduction=True, 187 passive=True, 188 timers=dict( 189 dead_interval=100, 190 hello_interval=80, 191 retransmit_interval=90, 192 transit_delay=True, 193 ), 194 ) 195 ], 196 ) 197 ], 198 ) 199 ], 200 state="merged", 201 ) 202 ) 203 result = self.execute_module(changed=True) 204 self.assertIn( 205 '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">', 206 str(result["commands"]), 207 ) 208 self.assertIn( 209 "<nc:ospf><nc:area><nc:name>0.0.0.100</nc:name>", 210 str(result["commands"]), 211 ) 212 self.assertIn( 213 "<nc:interface><nc:name>so-0/0/0.0</nc:name>", 214 str(result["commands"]), 215 ) 216 self.assertIn("<nc:priority>3</nc:priority>", str(result["commands"])) 217 self.assertIn("<nc:flood-reduction/>", str(result["commands"])) 218 self.assertIn("<nc:metric>5</nc:metric>", str(result["commands"])) 219 self.assertIn("<nc:passive/>", str(result["commands"])) 220 self.assertIn( 221 "<nc:dead-interval>100</nc:dead-interval>", str(result["commands"]) 222 ) 223 self.assertIn( 224 "<nc:hello-interval>80</nc:hello-interval>", 225 str(result["commands"]), 226 ) 227 self.assertIn( 228 "<nc:retransmit-interval>90</nc:retransmit-interval>", 229 str(result["commands"]), 230 ) 231 self.assertIn("</nc:interface>", str(result["commands"])) 232 self.assertIn( 233 "<nc:stub/></nc:area></nc:ospf></nc:protocols>", 234 str(result["commands"]), 235 ) 236 self.assertIn("</nc:interface>", str(result["commands"])) 237 self.assertIn( 238 '<nc:routing-options xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">', 239 str(result["commands"]), 240 ) 241 self.assertIn( 242 "<nc:router-id>10.200.16.75</nc:router-id></nc:routing-options>", 243 str(result["commands"]), 244 ) 245 246 def test_junos_ospfv2_merged_03(self): 247 set_module_args( 248 dict( 249 config=[ 250 dict( 251 router_id="10.200.16.75", 252 external_preference=2, 253 overload=dict(timeout=80), 254 preference=1, 255 prefix_export_limit=20000, 256 reference_bandwidth="10g", 257 rfc1583compatibility=False, 258 spf_options=dict( 259 delay=1000, holddown=15000, rapid_runs=9 260 ), 261 ) 262 ], 263 state="merged", 264 ) 265 ) 266 result = self.execute_module(changed=True) 267 self.assertIn( 268 '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">', 269 str(result["commands"]), 270 ) 271 self.assertIn( 272 "<nc:spf-options><nc:delay>1000</nc:delay>", 273 str(result["commands"]), 274 ) 275 self.assertIn( 276 "<nc:overload><nc:timeout>80</nc:timeout></nc:overload>", 277 str(result["commands"]), 278 ) 279 self.assertIn( 280 "<nc:external-preference>2</nc:external-preference>", 281 str(result["commands"]), 282 ) 283 self.assertIn( 284 "<nc:preference>1</nc:preference>", str(result["commands"]) 285 ) 286 self.assertIn( 287 "<nc:prefix-export-limit>20000</nc:prefix-export-limit>", 288 str(result["commands"]), 289 ) 290 self.assertIn( 291 "<nc:reference-bandwidth>10g</nc:reference-bandwidth>", 292 str(result["commands"]), 293 ) 294 self.assertIn("<nc:no-rfc-1583/>", str(result["commands"])) 295 self.assertIn( 296 "<nc:router-id>10.200.16.75</nc:router-id></nc:routing-options>", 297 str(result["commands"]), 298 ) 299 300 def test_junos_ospfv2_replaced_01(self): 301 set_module_args( 302 dict( 303 config=[ 304 dict( 305 router_id="10.200.16.77", 306 areas=[ 307 dict( 308 area_id="0.0.0.10", 309 stub=dict(default_metric=200, set=True), 310 interfaces=[ 311 dict( 312 name="so-0/0/0.0", 313 priority=3, 314 metric=2, 315 passive=True, 316 ), 317 dict( 318 name="so-0/0/0.1", priority=4, metric=4 319 ), 320 ], 321 ), 322 dict( 323 area_id="0.0.0.1=30", 324 interfaces=[ 325 dict(name="ge-1/1/1.0"), 326 dict(name="ge-2/2/2.0"), 327 ], 328 ), 329 ], 330 ) 331 ], 332 state="replaced", 333 ) 334 ) 335 result = self.execute_module(changed=True) 336 self.assertIn( 337 '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">', 338 str(result["commands"]), 339 ) 340 self.assertIn( 341 '<nc:ospf><nc:area delete="delete">0.0.0.20</nc:area></nc:ospf>', 342 str(result["commands"]), 343 ) 344 self.assertIn( 345 '<nc:ospf><nc:area delete="delete">0.0.0.10</nc:area></nc:ospf>', 346 str(result["commands"]), 347 ) 348 self.assertIn( 349 '<nc:routing-options xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">', 350 str(result["commands"]), 351 ) 352 self.assertIn("<nc:metric>2</nc:metric>", str(result["commands"])) 353 self.assertIn("<nc:priority>3</nc:priority>", str(result["commands"])) 354 self.assertIn( 355 "<nc:interface><nc:name>so-0/0/0.1</nc:name>", 356 str(result["commands"]), 357 ) 358 self.assertIn( 359 "<nc:stub><nc:default-metric>200</nc:default-metric></nc:stub>", 360 str(result["commands"]), 361 ) 362 self.assertIn( 363 "<nc:router-id>10.200.16.77</nc:router-id></nc:routing-options>", 364 str(result["commands"]), 365 ) 366 367 def test_junos_ospfv2_merged_01(self): 368 set_module_args( 369 dict( 370 config=[ 371 dict( 372 router_id="10.200.16.77", 373 areas=[ 374 dict( 375 area_id="0.0.0.10", 376 stub=dict(default_metric=200, set=True), 377 interfaces=[ 378 dict( 379 name="so-0/0/0.0", 380 priority=3, 381 metric=2, 382 passive=True, 383 ), 384 dict( 385 name="so-0/0/0.1", priority=4, metric=4 386 ), 387 ], 388 ), 389 dict( 390 area_id="0.0.0.1=30", 391 interfaces=[ 392 dict(name="ge-1/1/1.0"), 393 dict(name="ge-2/2/2.0"), 394 ], 395 ), 396 ], 397 ) 398 ], 399 state="overridden", 400 ) 401 ) 402 result = self.execute_module(changed=True) 403 self.assertIn( 404 '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">', 405 str(result["commands"]), 406 ) 407 self.assertIn( 408 '<nc:ospf><nc:area delete="delete">0.0.0.20</nc:area></nc:ospf>', 409 str(result["commands"]), 410 ) 411 self.assertIn( 412 '<nc:ospf><nc:area delete="delete">0.0.0.10</nc:area></nc:ospf>', 413 str(result["commands"]), 414 ) 415 self.assertIn( 416 '<nc:routing-options xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">', 417 str(result["commands"]), 418 ) 419 self.assertIn("<nc:metric>2</nc:metric>", str(result["commands"])) 420 self.assertIn("<nc:priority>3</nc:priority>", str(result["commands"])) 421 self.assertIn( 422 "<nc:interface><nc:name>so-0/0/0.1</nc:name>", 423 str(result["commands"]), 424 ) 425 self.assertIn( 426 "<nc:stub><nc:default-metric>200</nc:default-metric></nc:stub>", 427 str(result["commands"]), 428 ) 429 self.assertIn( 430 "<nc:router-id>10.200.16.77</nc:router-id></nc:routing-options>", 431 str(result["commands"]), 432 ) 433 434 def test_junos_ospfv2_deleted(self): 435 """ 436 :return: 437 """ 438 set_module_args(dict(config=[], state="deleted")) 439 440 commands = [ 441 '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">' 442 '<nc:ospf><nc:area delete="delete">0.0.0.10</nc:area></nc:ospf>' 443 '<nc:ospf><nc:area delete="delete">0.0.0.20</nc:area></nc:ospf>' 444 "</nc:protocols>" 445 ] 446 result = self.execute_module(changed=True) 447 self.assertEqual(sorted(result["commands"]), sorted(commands)) 448 449 def test_junos_ospfv2_rendered(self): 450 set_module_args( 451 dict( 452 config=[ 453 dict( 454 router_id="10.200.16.75", 455 areas=[ 456 dict( 457 area_id="0.0.0.100", 458 stub=dict(default_metric=200, set=True), 459 interfaces=[ 460 dict( 461 name="so-0/0/0.0", priority=3, metric=5 462 ) 463 ], 464 ) 465 ], 466 ) 467 ], 468 state="rendered", 469 ) 470 ) 471 rendered = ( 472 '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">' 473 "<nc:ospf><nc:area><nc:name>0.0.0.100</nc:name><nc:interface>" 474 "<nc:name>so-0/0/0.0</nc:name><nc:priority>3</nc:priority>" 475 "<nc:metric>5</nc:metric></nc:interface>" 476 "<nc:stub><nc:default-metric>200</nc:default-metric></nc:stub></nc:area></nc:ospf></nc:protocols>" 477 ) 478 result = self.execute_module(changed=False) 479 self.assertEqual(sorted(result["rendered"]), sorted(rendered)) 480 481 def test_junos_vlans_gathered(self): 482 """ 483 :return: 484 """ 485 set_module_args(dict(state="gathered")) 486 result = self.execute_module(changed=False) 487 gather_list = [ 488 dict( 489 router_id="10.200.16.77", 490 areas=[ 491 dict( 492 area_id="0.0.0.10", 493 stub=dict(default_metric=200, set=True), 494 interfaces=[ 495 dict( 496 name="so-0/0/0.0", 497 priority=3, 498 metric=5, 499 passive=True, 500 flood_reduction=True, 501 ) 502 ], 503 ), 504 dict( 505 area_id="0.0.0.20", 506 interfaces=[ 507 dict(name="ge-1/1/0.0"), 508 dict(name="ge-2/2/0.0"), 509 ], 510 ), 511 ], 512 ) 513 ] 514 self.assertEqual(sorted(gather_list), sorted(result["gathered"])) 515 516 def test_junos_vlans_parsed(self): 517 parsed_str = """ 518 <rpc-reply message-id="urn:uuid:0cadb4e8-5bba-47f4-986e-72906227007f"> 519 <configuration changed-seconds="1590139550" changed-localtime="2020-05-22 09:25:50 UTC"> 520 <version>18.4R1-S2.4</version> 521 <protocols> 522 <ospf> 523 <area> 524 <name>0.0.0.200</name> 525 <interface> 526 <name>so-0/0/0.1</name> 527 <metric>3</metric> 528 <priority>5</priority> 529 </interface> 530 </area> 531 <area> 532 <name>0.0.0.100</name> 533 <stub> 534 <default-metric>200</default-metric> 535 </stub> 536 <interface> 537 <name>so-0/0/0.0</name> 538 <metric>5</metric> 539 <priority>3</priority> 540 <bandwidth-based-metrics> 541 <bandwidth> 542 <name>10g</name> 543 <metric>5</metric> 544 </bandwidth> 545 </bandwidth-based-metrics> 546 </interface> 547 </area> 548 </ospf> 549 </protocols> 550 <routing-options> 551 <router-id>10.200.16.7</router-id> 552 </routing-options> 553 </configuration> 554 </rpc-reply> 555 """ 556 set_module_args(dict(running_config=parsed_str, state="parsed")) 557 result = self.execute_module(changed=False) 558 parsed_list = [ 559 { 560 "areas": [ 561 { 562 "area_id": "0.0.0.200", 563 "interfaces": [ 564 {"metric": 3, "name": "so-0/0/0.1", "priority": 5} 565 ], 566 }, 567 { 568 "area_id": "0.0.0.100", 569 "interfaces": [ 570 { 571 "bandwidth_based_metrics": [ 572 {"metric": 5, "bandwidth": "10g"} 573 ], 574 "metric": 5, 575 "name": "so-0/0/0.0", 576 "priority": 3, 577 } 578 ], 579 "stub": {"default_metric": 200, "set": True}, 580 }, 581 ], 582 "router_id": "10.200.16.7", 583 } 584 ] 585 self.assertEqual(result["parsed"], parsed_list) 586 587 def test_junos_vlans_parsed_02(self): 588 parsed_str = """ 589 <rpc-reply message-id="urn:uuid:0cadb4e8-5bba-47f4-986e-72906227007f"> 590 <configuration changed-seconds="1590139550" changed-localtime="2020-05-22 09:25:50 UTC"> 591 <version>18.4R1-S2.4</version> 592 <protocols> 593 <ospf> 594 <area> 595 <name>0.0.0.200</name> 596 <interface> 597 <name>so-0/0/0.1</name> 598 <metric>3</metric> 599 <priority>5</priority> 600 </interface> 601 </area> 602 <area> 603 <name>0.0.0.100</name> 604 <stub> 605 <default-metric>200</default-metric> 606 </stub> 607 <interface> 608 <name>so-0/0/0.0</name> 609 <metric>5</metric> 610 <priority>3</priority> 611 <bandwidth-based-metrics> 612 <bandwidth> 613 <name>10g</name> 614 <metric>5</metric> 615 </bandwidth> 616 <bandwidth> 617 <name>1g</name> 618 <metric>5</metric> 619 </bandwidth> 620 </bandwidth-based-metrics> 621 </interface> 622 </area> 623 </ospf> 624 </protocols> 625 <routing-options> 626 <router-id>10.200.16.7</router-id> 627 </routing-options> 628 </configuration> 629 </rpc-reply> 630 """ 631 set_module_args(dict(running_config=parsed_str, state="parsed")) 632 result = self.execute_module(changed=False) 633 parsed_list = [ 634 { 635 "areas": [ 636 { 637 "area_id": "0.0.0.200", 638 "interfaces": [ 639 {"metric": 3, "name": "so-0/0/0.1", "priority": 5} 640 ], 641 }, 642 { 643 "area_id": "0.0.0.100", 644 "interfaces": [ 645 { 646 "bandwidth_based_metrics": [ 647 {"metric": 5, "bandwidth": "10g"}, 648 {"metric": 5, "bandwidth": "1g"}, 649 ], 650 "metric": 5, 651 "name": "so-0/0/0.0", 652 "priority": 3, 653 } 654 ], 655 "stub": {"default_metric": 200, "set": True}, 656 }, 657 ], 658 "router_id": "10.200.16.7", 659 } 660 ] 661 self.assertEqual(result["parsed"], parsed_list) 662