1#
2# (c) 2018 Lenovo.
3#
4# This file is part of Ansible
5#
6# Ansible is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# Ansible is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
18#
19from __future__ import (absolute_import, division, print_function)
20__metaclass__ = type
21
22from ansible_collections.community.network.tests.unit.compat.mock import patch
23from ansible_collections.community.network.plugins.modules.network.cnos import cnos_l3_interface
24from ansible_collections.community.network.tests.unit.plugins.modules.utils import set_module_args
25from .cnos_module import TestCnosModule, load_fixture
26
27
28class TestCnosL3InterfaceModule(TestCnosModule):
29    module = cnos_l3_interface
30
31    def setUp(self):
32        super(TestCnosL3InterfaceModule, self).setUp()
33        self._patch_get_config = patch(
34            'ansible_collections.community.network.plugins.modules.network.cnos.cnos_l3_interface.get_config'
35        )
36        self._patch_load_config = patch(
37            'ansible_collections.community.network.plugins.modules.network.cnos.cnos_l3_interface.load_config'
38        )
39        self._patch_is_switchport = patch(
40            'ansible_collections.community.network.plugins.modules.network.cnos.cnos_l3_interface.is_switchport'
41        )
42
43        self._get_config = self._patch_get_config.start()
44        self._load_config = self._patch_load_config.start()
45        self._is_switchport = self._patch_is_switchport.start()
46
47    def tearDown(self):
48        super(TestCnosL3InterfaceModule, self).tearDown()
49        self._patch_get_config.stop()
50        self._patch_load_config.stop()
51
52    def load_fixtures(self, commands=None):
53        config_file = 'l3_interface_config.cfg'
54        self._get_config.return_value = load_fixture(config_file)
55        self._load_config.return_value = None
56        self._is_switchport.return_value = False
57
58    def test_cnos_l3_interface_ipv4_address(self, *args, **kwargs):
59        set_module_args(dict(
60            name='Ethernet 1/35',
61            ipv4='192.168.4.1/24'
62        ))
63        commands = [
64            'interface Ethernet 1/35',
65            'ip address 192.168.4.1 255.255.255.0'
66        ]
67        result = self.execute_module(changed=True, commands=commands)
68
69    def test_cnos_l3_interface_absent(self, *args, **kwargs):
70        set_module_args(dict(
71            name='Ethernet1/9',
72            state='absent'
73        ))
74        commands = [
75            'interface Ethernet1/9',
76            'no ip address',
77            'no ipv6 address'
78        ]
79        result = self.execute_module(changed=True, commands=commands)
80