def test_junos_ospf_interfaces_merged(self):
     set_module_args(
         dict(
             config=[
                 dict(
                     router_id="10.200.16.75",
                     name="ge-0/0/2.0",
                     address_family=[
                         dict(
                             afi="ipv4",
                             processes=dict(
                                 area=dict(area_id="0.0.0.2"),
                                 priority=3,
                                 metric=5,
                             ),
                         )
                     ],
                 )
             ],
             state="merged",
         ))
     commands = [
         '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><nc:ospf>'
         "<nc:area><nc:name>0.0.0.2</nc:name><nc:interface><nc:name>ge-0/0/2.0</nc:name>"
         "<nc:priority>3</nc:priority><nc:metric>5</nc:metric></nc:interface></nc:area></nc:ospf></nc:protocols>",
         '<nc:routing-options xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">'
         "<nc:router-id>10.200.16.75</nc:router-id></nc:routing-options>",
     ]
     result = self.execute_module(changed=True)
     self.assertEqual(sorted(result["commands"]), sorted(commands))
 def test_junos_routing_options_parsed_01(self):
     parsed_str = """
         <rpc-reply message-id="urn:uuid:0cadb4e8-5bba-47f4-986e-72906227007f">
             <configuration changed-seconds="1590139550" changed-localtime="2020-05-22 09:25:50 UTC">
                 <version>18.4R1-S2.4</version>
                     <routing-options>
                         <router-id>12.12.12.12</router-id>
                         <autonomous-system>
                             <as-number>2</as-number>
                             <loops>4</loops>
                             <asdot-notation/>
                         </autonomous-system>
                     </routing-options>
             </configuration>
         </rpc-reply>
     """
     set_module_args(dict(running_config=parsed_str, state="parsed"))
     result = self.execute_module(changed=False)
     parsed_dict = {
         "autonomous_system": {
             "as_number": "2",
             "asdot_notation": True,
             "loops": 4,
         },
         "router_id": "12.12.12.12",
     }
     self.assertEqual(sorted(parsed_dict), sorted(result["parsed"]))
Esempio n. 3
0
 def test_junos_config_src_json(self):
     src = load_fixture("junos_config.json", content="str")
     set_module_args(dict(src=src))
     self.execute_module(changed=True)
     args, kwargs = self.load_config.call_args
     self.assertEqual(kwargs["action"], "merge")
     self.assertEqual(kwargs["format"], "json")
 def test_junos_logging_global_gathered_14(self):
     """
     :return:
     """
     set_module_args(dict(state="gathered"))
     result = self.execute_module(changed=False)
     gather_list = {
         "hosts": [{
             "name": "host111",
             "allow_duplicates": True,
             "any": {
                 "level": "any"
             },
             "exclude_hostname": True,
             "facility_override": "ftp",
             "log_prefix": "field",
             "match": "^set*",
             "match_strings": ["^delete", "^prompt"],
             "port": 1231,
             "routing_instance": "inst11",
             "source_address": "11.1.1.11",
             "structured_data": {
                 "brief": True
             },
         }]
     }
     self.assertEqual(sorted(gather_list), sorted(result["gathered"]))
Esempio n. 5
0
 def test_junos_vlans_parsed(self):
     parsed_str = """
         <rpc-reply message-id="urn:uuid:0cadb4e8-5bba-47f4-986e-72906227007f">
             <configuration changed-seconds="1590139550" changed-localtime="2020-05-22 09:25:50 UTC">
                 <version>18.4R1-S2.4</version>
                  <vlans>
                     <vlan>
                         <name>vlan1</name>
                         <vlan-id>1</vlan-id>
                     </vlan>
                     <vlan>
                         <name>vlan2</name>
                         <vlan-id>2</vlan-id>
                         <l3-interface>irb.12</l3-interface>
                     </vlan>
                 </vlans>
             </configuration>
         </rpc-reply>
     """
     set_module_args(dict(running_config=parsed_str, state="parsed"))
     result = self.execute_module(changed=False)
     parsed_list = [
         {
             "name": "vlan1",
             "vlan_id": 1
         },
         {
             "l3_interface": "irb.12",
             "name": "vlan2",
             "vlan_id": 2
         },
     ]
     self.sort_vlans(result["parsed"])
     self.sort_vlans(parsed_list)
     self.assertEqual(result["parsed"], parsed_list)
Esempio n. 6
0
    def test_junos_scp_remote_src(self):
        set_module_args(dict(src="test.txt", remote_src=True))
        self.execute_module(changed=True)

        self.scp_mock.get.assert_called_once_with(
            "test.txt", local_path=".", recursive=False
        )
 def test_junos_logging_global_merged_user_08(self):
     set_module_args(
         dict(
             config=dict(users=[
                 dict(name="user1", allow_duplicates=True),
                 dict(
                     name="user2",
                     allow_duplicates=True,
                     any=dict(level="any"),
                     user=dict(level="info"),
                 ),
             ]),
             state="merged",
         ))
     result = self.execute_module(changed=True)
     self.assertIn(
         "<nc:user><nc:name>user1</nc:name><nc:allow-duplicates/>",
         str(result["commands"]),
     )
     self.assertIn(
         "<nc:user><nc:name>user2</nc:name><nc:allow-duplicates/>",
         str(result["commands"]),
     )
     self.assertIn(
         "<nc:contents><nc:name>any</nc:name><nc:any/></nc:contents>",
         str(result["commands"]),
     )
    def test_junos_scp_src(self):
        set_module_args(dict(src='test.txt'))
        self.execute_module(changed=True)

        self.scp_mock.put.assert_called_once_with('test.txt',
                                                  remote_path='.',
                                                  recursive=False)
 def test_junos_routing_instances_merged(self):
     set_module_args(
         dict(
             config=[
                 dict(
                     name="test",
                     type="vrf",
                     route_distinguisher="10.58.255.1:37",
                     vrf_imports=["test-policy"],
                     vrf_exports=["test-policy", "test-policy-1"],
                     interfaces=[
                         dict(name="sp-0/0/0.0"),
                         dict(name="gr-0/0/0.0"),
                     ],
                     connector_id_advertise=True,
                 )
             ],
             state="merged",
         ))
     commands = [
         '<nc:routing-instances xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">'
         "<nc:instance><nc:name>test</nc:name><nc:connector-id-advertise/>"
         "<nc:instance-type>vrf</nc:instance-type><nc:interface><nc:name>sp-0/0/0.0</nc:name>"
         "</nc:interface><nc:interface><nc:name>gr-0/0/0.0</nc:name>"
         "</nc:interface><nc:route-distinguisher><nc:rd-type>10.58.255.1:37</nc:rd-type>"
         "</nc:route-distinguisher><nc:vrf-import>test-policy</nc:vrf-import>"
         "<nc:vrf-export>test-policy</nc:vrf-export><nc:vrf-export>test-policy-1</nc:vrf-export>"
         "</nc:instance></nc:routing-instances>"
     ]
     result = self.execute_module(changed=True, commands=commands)
     self.assertEqual(sorted(result["commands"]), sorted(commands))
Esempio n. 10
0
 def test_junos_config_src_json(self):
     src = load_fixture('junos_config.json', content='str')
     set_module_args(dict(src=src))
     self.execute_module(changed=True)
     args, kwargs = self.load_config.call_args
     self.assertEqual(kwargs['action'], 'merge')
     self.assertEqual(kwargs['format'], 'json')
Esempio n. 11
0
 def test_junos_config_update_replace(self):
     src = load_fixture('junos_config.json', content='str')
     set_module_args(dict(src=src, update='replace'))
     self.execute_module()
     args, kwargs = self.load_config.call_args
     self.assertEqual(kwargs['action'], 'replace')
     self.assertEqual(kwargs['format'], 'json')
 def test_junos_rpc_attrs(self):
     set_module_args(
         dict(rpc='load-configuration',
              output='xml',
              attrs={'url': '/var/tmp/config.conf'}))
     result = self.execute_module(format='xml')
     self.assertTrue(result['xml'].find('<load-success/>'))
 def test_junos_command_match_any(self):
     wait_for = [
         'result[0] contains "Junos:"', 'result[0] contains "test string"'
     ]
     set_module_args(
         dict(commands=['show version'], wait_for=wait_for, match='any'))
     self.execute_module()
 def test_junos_hostname_rendered(self):
     set_module_args(dict(config=dict(hostname="vsrx10"), state="rendered"))
     rendered = (
         '<nc:system xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">'
         "<nc:host-name>vsrx10</nc:host-name></nc:system>")
     result = self.execute_module(changed=False)
     self.assertEqual(sorted(result["rendered"]), sorted(rendered))
Esempio n. 15
0
 def test_junos_ospfv2_rendered(self):
     set_module_args(
         dict(
             config=[
                 dict(
                     router_id="10.200.16.75",
                     areas=[
                         dict(
                             area_id="0.0.0.100",
                             stub=dict(default_metric=200, set=True),
                             interfaces=[
                                 dict(
                                     name="so-0/0/0.0", priority=3, metric=5
                                 )
                             ],
                         )
                     ],
                 )
             ],
             state="rendered",
         )
     )
     commands = (
         '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><nc:ospf>'
         "<nc:area><nc:name>0.0.0.100</nc:name><nc:interface><nc:name>so-0/0/0.0</nc:name>"
         "<nc:priority>3</nc:priority><nc:metric>5</nc:metric></nc:interface><nc:stub>"
         "<nc:default-metric>200</nc:default-metric></nc:stub></nc:area></nc:ospf></nc:protocols>"
     )
     self.execute_module(changed=False, commands=commands)
Esempio n. 16
0
 def test_junos_snmp_server_merged_client_02(self):
     set_module_args(
         dict(
             config=dict(client_lists=[
                 dict(
                     name="cl2",
                     addresses=[dict(address="192.16.4.0/24")],
                 )
             ]),
             state="merged",
         ))
     result = self.execute_module(changed=True)
     self.assertIn(
         '<nc:snmp xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">',
         str(result["commands"]),
     )
     self.assertIn(
         "<nc:client-list><nc:name>cl2</nc:name><nc:client-address-list>",
         str(result["commands"]),
     )
     self.assertIn("<nc:name>192.16.4.0/24</nc:name>",
                   str(result["commands"]))
     self.assertIn(
         "</nc:client-address-list></nc:client-list></nc:snmp>",
         str(result["commands"]),
     )
Esempio n. 17
0
    def test_junos_scp_src_expand_tilde(self):
        set_module_args(dict(src="~/test.txt"))
        self.execute_module(changed=True)

        self.scp_mock.put.assert_called_once_with(
            os.path.expanduser("~/test.txt"), remote_path=".", recursive=False
        )
Esempio n. 18
0
 def test_junos_snmp_server_merged_clients_03(self):
     set_module_args(
         dict(
             config=dict(client_lists=[
                 dict(
                     name="cl3",
                     addresses=[
                         dict(address="172.16.1.0/24"),
                         dict(address="10.11.11.11", restrict=True),
                     ],
                 ),
                 dict(
                     name="cl4",
                     addresses=[dict(address="172.16.4.0/24")],
                 ),
             ]),
             state="merged",
         ))
     result = self.execute_module(changed=True)
     commands = [
         '<nc:snmp xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">',
         "<nc:client-list><nc:name>cl3</nc:name>",
         "<nc:client-address-list><nc:name>172.16.1.0/24</nc:name>",
         "</nc:client-address-list><nc:client-address-list>",
         "<nc:name>10.11.11.11</nc:name><nc:restrict/></nc:client-address-list>",
         "</nc:client-list><nc:client-list><nc:name>cl4</nc:name>",
         "<nc:client-address-list><nc:name>172.16.4.0/24</nc:name>",
         "</nc:client-address-list></nc:client-list></nc:snmp>",
     ]
     for command in commands:
         self.assertIn(command, str(result["commands"]))
 def test_junos_logging_global_merged_console_02(self):
     set_module_args(
         dict(
             config=dict(console=dict(
                 any=dict(level="info"),
                 authorization=dict(level="any"),
                 change_log=dict(level="critical"),
                 ftp=dict(level="none"),
             )),
             state="merged",
         ))
     result = self.execute_module(changed=True)
     self.assertIn(
         "<nc:console><nc:name>any</nc:name><nc:info/>",
         str(result["commands"]),
     )
     self.assertIn(
         "<nc:console><nc:name>authorization</nc:name><nc:any/>",
         str(result["commands"]),
     )
     self.assertIn(
         "<nc:console><nc:name>change-log</nc:name><nc:critical/>",
         str(result["commands"]),
     )
     self.assertIn(
         "<nc:console><nc:name>ftp</nc:name><nc:none/>",
         str(result["commands"]),
     )
Esempio n. 20
0
 def test_junos_snmp_server_merged_communities_05(self):
     set_module_args(
         dict(
             config=dict(communities=[
                 dict(
                     name="comm1",
                     clients=[dict(address="24.0.0.0/32", restrict=True)],
                     routing_instances=[
                         dict(
                             name="clv1",
                             clients=[
                                 dict(
                                     address="13.13.13.13/24",
                                     restrict=True,
                                 )
                             ],
                         )
                     ],
                 )
             ]),
             state="merged",
         ))
     result = self.execute_module(changed=True)
     commands = [
         '<nc:snmp xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">'
         "<nc:community><nc:name>comm1</nc:name><nc:clients>",
         "<nc:name>24.0.0.0/32</nc:name><nc:restrict/></nc:clients>",
         "<nc:routing-instance><nc:name>clv1</nc:name><nc:clients>",
         "<nc:name>13.13.13.13/24</nc:name><nc:restrict/></nc:clients>",
         "</nc:routing-instance></nc:community></nc:snmp>",
     ]
     for command in commands:
         self.assertIn(command, str(result["commands"]))
 def test_junos_logging_global_overridden_user_10(self):
     set_module_args(
         dict(
             config=dict(users=[
                 dict(name="user1", allow_duplicates=True),
                 dict(
                     name="user2",
                     allow_duplicates=True,
                     any=dict(level="any"),
                     user=dict(level="info"),
                 ),
             ]),
             state="overridden",
         ))
     result = self.execute_module(changed=True)
     self.assertIn(
         '<nc:system xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">',
         str(result["commands"]),
     )
     self.assertIn(
         '<nc:syslog delete="delete"/><nc:syslog><nc:user><nc:name>user1</nc:name>',
         str(result["commands"]),
     )
     self.assertIn(
         "<nc:allow-duplicates/></nc:user><nc:user><nc:name>user2</nc:name>",
         str(result["commands"]),
     )
    def test_junos_bgp_address_family_rep_idempotent(self):
        self.get_connection.return_value = load_fixture(
            "junos_bgp_address_family_config.cfg"
        )
        src = load_fixture("junos_bgp_address_family.cfg", content="str")
        set_module_args(dict(src=src))
        set_module_args(
            dict(
                config=dict(
                    groups=[
                        dict(
                            name="external",
                            address_family=[
                                dict(
                                    afi="evpn",
                                    af_type=[dict(type="flow", damping=True)],
                                )
                            ],
                        )
                    ],
                    address_family=[
                        dict(
                            afi="evpn",
                            af_type=[dict(type="flow", damping=True)],
                        )
                    ],
                ),
                state="replaced",
            )
        )

        self.execute_module(changed=False, commands=[])
 def test_junos_bgp_global_rendered(self):
     set_module_args(
         dict(
             config=dict(
                 damping=True,
                 description="This is configured with Junos_bgp resource module",
                 preference="2",
                 bfd_liveness_detection=dict(
                     minimum_receive_interval=4,
                     multiplier=10,
                     no_adaptation=True,
                     version="automatic",
                 ),
             ),
             state="merged",
         )
     )
     commands = [
         '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><nc:bgp>'
         "<nc:damping/><nc:description>This is configured with Junos_bgp resource module</nc:description>"
         "<nc:preference>2</nc:preference><nc:bfd-liveness-detection>"
         "<nc:minimum-receive-interval>4</nc:minimum-receive-interval>"
         "<nc:multiplier>10</nc:multiplier><nc:no-adaptation/><nc:version>automatic</nc:version>"
         "</nc:bfd-liveness-detection>"
         "</nc:bgp></nc:protocols>"
     ]
     self.execute_module(changed=False, commands=commands)
Esempio n. 24
0
    def test_junos_l2_interfaces_replaced(self):
        self.get_res_config.return_value = load_fixture(
            "junos_interfaces_config.xml"
        )
        set_module_args(
            dict(
                config=[dict(name="ge-0/0/2", access=dict(vlan="vlan200"))],
                state="replaced",
            )
        )
        commands = [
            '<nc:interfaces xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><nc:interface>'
            "<nc:name>ge-0/0/2</nc:name><nc:unit><nc:name>0</nc:name>"
            "<nc:family><nc:ethernet-switching>"
            '<nc:interface-mode delete="delete"/><nc:vlan delete="delete"/>'
            '</nc:ethernet-switching></nc:family></nc:unit><nc:native-vlan-id delete="delete"/>'
            "</nc:interface><nc:interface><nc:name>ge-0/0/2</nc:name>"
            "<nc:unit><nc:name>0</nc:name><nc:family>"
            "<nc:ethernet-switching><nc:interface-mode>access</nc:interface-mode>"
            "<nc:vlan><nc:members>vlan200</nc:members></nc:vlan>"
            "</nc:ethernet-switching></nc:family></nc:unit></nc:interface></nc:interfaces>"
        ]
        result = self.execute_module(changed=True)

        self.assertEqual(sorted(result["commands"]), sorted(commands))
    def test_junos_l3_interfaces_overridden(self):
        set_module_args(
            dict(
                config=[
                    dict(
                        name="ge-0/0/1",
                        ipv4=[
                            dict(address="100.64.0.1/10"),
                            dict(address="100.64.0.2/10"),
                        ],
                    )
                ],
                state="overridden",
            )
        )
        commands = [
            '<nc:interfaces xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><nc:interface>'
            "<nc:name>ge-0/0/1</nc:name><nc:unit><nc:name>0</nc:name>"
            "<nc:family><nc:inet><nc:address><nc:name>100.64.0.1/10</nc:name></nc:address>"
            "<nc:address><nc:name>100.64.0.2/10</nc:name></nc:address></nc:inet></nc:family>"
            "</nc:unit></nc:interface></nc:interfaces>"
        ]
        result = self.execute_module(changed=True)

        self.assertEqual(sorted(result["commands"]), commands)
Esempio n. 26
0
    def test_junos_ospfv2_overridden_idempotent(self):
        self.get_connection.return_value = load_fixture(
            "junos_ospfv2_config.cfg"
        )
        src = load_fixture("junos_ospfv2.cfg", content="str")
        set_module_args(dict(src=src))
        set_module_args(
            dict(
                config=[
                    dict(
                        router_id="30",
                        areas=[
                            dict(
                                area_id="100",
                                stub=dict(default_metric=10, set=True),
                                interfaces=[
                                    dict(
                                        name="so-0/0/0.0", priority=3, metric=5
                                    )
                                ],
                            )
                        ],
                    )
                ],
                state="overridden",
            )
        )

        self.execute_module(changed=False, commands=[])
 def test_junos_interfaces_overridden(self):
     set_module_args(
         dict(
             config=[
                 dict(
                     name="ge-0/0/2",
                     description="This is configured with ansible",
                     mtu=1024,
                     speed="100m",
                 )
             ],
             state="overridden",
         ))
     commands = [
         '<nc:interfaces xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">'
         "<nc:interface><nc:name>ge-0/0/2</nc:name>"
         '<nc:description delete="delete"/><nc:speed delete="delete"/>'
         '<nc:mtu delete="delete"/><nc:link-mode delete="delete"/>'
         '<nc:disable delete="delete"/><nc:hold-time><nc:up delete="delete"/>'
         '<nc:down delete="delete"/></nc:hold-time></nc:interface>'
         "<nc:interface><nc:name>ge-0/0/2</nc:name>"
         "<nc:description>This is configured with ansible</nc:description>"
         "<nc:speed>100m</nc:speed><nc:mtu>1024</nc:mtu>"
         "</nc:interface></nc:interfaces>"
     ]
     result = self.execute_module(changed=True)
     self.assertEqual(sorted(result["commands"]), commands)
Esempio n. 28
0
    def test_junos_ospfv2_delete(self):
        set_module_args(
            dict(
                config=[
                    dict(
                        router_id="10.200.16.75",
                        areas=[
                            dict(
                                area_id="0.0.0.100",
                                stub=dict(default_metric=200, set=True),
                                interfaces=[dict(name="so-0/0/0.0")],
                            )
                        ],
                    )
                ],
                state="deleted",
            )
        )

        commands = [
            '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><nc:ospf>'
            "<nc:area><nc:name>0.0.0.100</nc:name><nc:interface><nc:name>so-0/0/0.0</nc:name>"
            "</nc:interface><nc:stub><nc:default-metric>200</nc:default-metric></nc:stub></nc:area></nc:ospf></nc:protocols>",
            '<nc:routing-options xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">'
            "<nc:router-id>10.200.16.75</nc:router-id></nc:routing-options>",
        ]

        result = self.execute_module(changed=True)
        self.assertEqual(sorted(result["commands"]), commands)
Esempio n. 29
0
 def test_junos_config_update_replace(self):
     src = load_fixture("junos_config.json", content="str")
     set_module_args(dict(src=src, update="replace"))
     self.execute_module()
     args, kwargs = self.load_config.call_args
     self.assertEqual(kwargs["action"], "replace")
     self.assertEqual(kwargs["format"], "json")
 def test_junos_ospf_interfaces_rendered(self):
     set_module_args(
         dict(
             config=[
                 dict(
                     router_id="10.200.16.75",
                     name="ge-0/0/2.0",
                     address_family=[
                         dict(
                             afi="ipv4",
                             processes=dict(
                                 area=dict(area_id="0.0.0.1"),
                                 priority=6,
                                 metric=7,
                             ),
                         )
                     ],
                 )
             ],
             state="rendered",
         ))
     commands = [
         '<nc:protocols xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><nc:ospf/><nc:ospf>'
         "<nc:area><nc:name>0.0.0.1</nc:name><nc:interface><nc:name>ge-0/0/2.0</nc:name>"
         "<nc:priority>6</nc:priority><nc:metric>7</nc:metric></nc:interface></nc:area></nc:ospf></nc:protocols>",
         '<nc:routing-options xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">'
         "<nc:router-id>10.200.16.75</nc:router-id></nc:routing-options>",
     ]
     self.execute_module(changed=False, commands=commands)