1# -*- coding: utf-8 -*-
2# Copyright 2020 Red Hat
3# GNU General Public License v3.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_l3_interfaces,
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 TestJunosL3InterfacesModule(TestJunosModule):
41    module = junos_l3_interfaces
42
43    def setUp(self):
44        super(TestJunosL3InterfacesModule, self).setUp()
45        self.mock_lock_configuration = patch(
46            "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos.lock_configuration"
47        )
48        self.lock_configuration = self.mock_lock_configuration.start()
49        self.mock_unlock_configuration = patch(
50            "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos.unlock_configuration"
51        )
52        self.unlock_configuration = self.mock_unlock_configuration.start()
53        self.mock_load_config = patch(
54            "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.config.l3_interfaces.l3_interfaces.load_config"
55        )
56        self.load_config = self.mock_load_config.start()
57
58        self.mock_validate_config = patch(
59            "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils.validate_config"
60        )
61        self.validate_config = self.mock_validate_config.start()
62
63        self.mock_commit_configuration = patch(
64            "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.config.l3_interfaces.l3_interfaces.commit_configuration"
65        )
66        self.mock_commit_configuration = self.mock_commit_configuration.start()
67
68        self.mock_get_config = patch(
69            "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.facts.l3_interfaces.l3_interfaces."
70            "L3_interfacesFacts.get_config"
71        )
72        self.get_config = self.mock_get_config.start()
73
74    def tearDown(self):
75        super(TestJunosL3InterfacesModule, self).tearDown()
76        self.mock_get_config.stop()
77        self.mock_load_config.stop()
78        self.mock_validate_config.stop()
79        self.mock_lock_configuration.stop()
80        self.mock_unlock_configuration.stop()
81        self.mock_commit_configuration.stop()
82
83    def load_fixtures(self, commands=None, format="text", changed=False):
84        self.get_config.return_value = load_fixture(
85            "junos_interfaces_config.xml"
86        )
87        if changed:
88            self.load_config.return_value = load_fixture(
89                "get_configuration_rpc_reply_diff.txt"
90            )
91        else:
92            self.load_config.return_value = None
93
94    def test_junos_l3_interfaces_merged(self):
95        set_module_args(
96            dict(
97                config=[
98                    dict(
99                        name="ge-0/0/1",
100                        ipv4=[
101                            dict(address="100.64.0.1/10"),
102                            dict(address="100.64.0.2/10"),
103                        ],
104                    )
105                ],
106                state="merged",
107            )
108        )
109        commands = [
110            '<nc:interfaces xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><nc:interface>'
111            "<nc:name>ge-0/0/1</nc:name><nc:unit><nc:name>0</nc:name>"
112            "<nc:family><nc:inet><nc:address><nc:name>100.64.0.1/10</nc:name></nc:address>"
113            "<nc:address><nc:name>100.64.0.2/10</nc:name></nc:address></nc:inet></nc:family>"
114            "</nc:unit></nc:interface></nc:interfaces>"
115        ]
116        result = self.execute_module(changed=True)
117        self.assertEqual(sorted(result["commands"]), sorted(commands))
118
119    def test_junos_l3_interfaces_merged_idempotent(self):
120        self.get_config.return_value = load_fixture(
121            "junos_interfaces_config.xml"
122        )
123        src = load_fixture("junos_l3_interfaces.cfg", content="str")
124        set_module_args(dict(src=src))
125        set_module_args(
126            dict(
127                config=[
128                    dict(
129                        name="ge-0/0/1",
130                        ipv4=[
131                            dict(address="100.64.0.1/10"),
132                            dict(address="100.64.0.2/10"),
133                        ],
134                    )
135                ],
136                state="merged",
137            )
138        )
139        self.execute_module(changed=False, commands=[])
140
141    def test_junos_l3_interfaces_replaced(self):
142        set_module_args(
143            dict(
144                config=[
145                    dict(
146                        name="ge-0/0/2",
147                        ipv4=[
148                            dict(address="100.64.0.1/10"),
149                            dict(address="100.64.0.2/10"),
150                        ],
151                    )
152                ],
153                state="replaced",
154            )
155        )
156        commands = [
157            '<nc:interfaces xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><nc:interface>'
158            "<nc:name>ge-0/0/2</nc:name><nc:unit><nc:name>0</nc:name>"
159            "<nc:family><nc:inet><nc:address><nc:name>100.64.0.1/10</nc:name></nc:address>"
160            "<nc:address><nc:name>100.64.0.2/10</nc:name></nc:address></nc:inet></nc:family>"
161            "</nc:unit></nc:interface></nc:interfaces>"
162        ]
163        result = self.execute_module(changed=True)
164
165        self.assertEqual(sorted(result["commands"]), sorted(commands))
166
167    def test_junos_l3_interfaces_replaced_idempotent(self):
168        self.get_config.return_value = load_fixture(
169            "junos_interfaces_config.xml"
170        )
171        src = load_fixture("junos_l3_interfaces.cfg", content="str")
172        set_module_args(dict(src=src))
173        set_module_args(
174            dict(
175                config=[
176                    dict(
177                        name="ge-0/0/1",
178                        ipv4=[
179                            dict(address="100.64.0.1/10"),
180                            dict(address="100.64.0.2/10"),
181                        ],
182                    )
183                ],
184                state="merged",
185            )
186        )
187        self.execute_module(changed=False, commands=[])
188
189    def test_junos_l3_interfaces_overridden(self):
190        set_module_args(
191            dict(
192                config=[
193                    dict(
194                        name="ge-0/0/1",
195                        ipv4=[
196                            dict(address="100.64.0.1/10"),
197                            dict(address="100.64.0.2/10"),
198                        ],
199                    )
200                ],
201                state="overridden",
202            )
203        )
204        commands = [
205            '<nc:interfaces xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><nc:interface>'
206            "<nc:name>ge-0/0/1</nc:name><nc:unit><nc:name>0</nc:name>"
207            "<nc:family><nc:inet><nc:address><nc:name>100.64.0.1/10</nc:name></nc:address>"
208            "<nc:address><nc:name>100.64.0.2/10</nc:name></nc:address></nc:inet></nc:family>"
209            "</nc:unit></nc:interface></nc:interfaces>"
210        ]
211        result = self.execute_module(changed=True)
212
213        self.assertEqual(sorted(result["commands"]), commands)
214
215    def test_junos_l3_interfaces_overridden_idempotent(self):
216        self.get_config.return_value = load_fixture(
217            "junos_interfaces_config.xml"
218        )
219        src = load_fixture("junos_l3_interfaces.cfg", content="str")
220        set_module_args(dict(src=src))
221        set_module_args(
222            dict(
223                config=[
224                    dict(
225                        name="ge-0/0/1",
226                        ipv4=[
227                            dict(address="100.64.0.1/10"),
228                            dict(address="100.64.0.2/10"),
229                        ],
230                    )
231                ],
232                state="overridden",
233            )
234        )
235        self.execute_module(changed=False, commands=[])
236
237    def test_junos_l3_interfaces_rendered(self):
238        set_module_args(
239            dict(
240                config=[
241                    dict(
242                        name="ge-0/0/1",
243                        ipv4=[
244                            dict(address="100.64.0.1/10"),
245                            dict(address="100.64.0.2/10"),
246                        ],
247                    )
248                ],
249                state="rendered",
250            )
251        )
252        commands = [
253            '<nc:interfaces xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><nc:interface>'
254            "<nc:name>ge-0/0/1</nc:name><nc:unit><nc:name>0</nc:name>"
255            "<nc:family><nc:inet><nc:address><nc:name>100.64.0.1/10</nc:name></nc:address>"
256            "<nc:address><nc:name>100.64.0.2/10</nc:name></nc:address></nc:inet></nc:family>"
257            "</nc:unit></nc:interface></nc:interfaces>"
258        ]
259        self.execute_module(changed=False, commands=commands)
260