1#
2# (c) 2018 Extreme Networks Inc.
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
22import re
23
24from units.compat.mock import patch
25from units.modules.utils import set_module_args
26from ansible.modules.network.slxos import slxos_l3_interface
27from .slxos_module import TestSlxosModule, load_fixture
28
29
30class TestSlxosL3InterfaceModule(TestSlxosModule):
31    module = slxos_l3_interface
32
33    def setUp(self):
34        super(TestSlxosL3InterfaceModule, self).setUp()
35        self._patch_get_config = patch(
36            'ansible.modules.network.slxos.slxos_l3_interface.get_config'
37        )
38        self._patch_load_config = patch(
39            'ansible.modules.network.slxos.slxos_l3_interface.load_config'
40        )
41
42        self._get_config = self._patch_get_config.start()
43        self._load_config = self._patch_load_config.start()
44
45    def tearDown(self):
46        super(TestSlxosL3InterfaceModule, self).tearDown()
47        self._patch_get_config.stop()
48        self._patch_load_config.stop()
49
50    def load_fixtures(self, commands=None):
51        config_file = 'slxos_config_config.cfg'
52        self._get_config.return_value = load_fixture(config_file)
53        self._load_config.return_value = None
54
55    def test_slxos_l3_interface_ipv4_address(self, *args, **kwargs):
56        set_module_args(dict(
57            name='Ethernet 0/2',
58            ipv4='192.168.4.1/24'
59        ))
60        result = self.execute_module(changed=True)
61        self.assertEqual(
62            result,
63            {
64                'commands': [
65                    'interface Ethernet 0/2',
66                    'ip address 192.168.4.1/24'
67                ],
68                'changed': True
69            }
70        )
71
72    def test_slxos_l3_interface_absent(self, *args, **kwargs):
73        set_module_args(dict(
74            name='Ethernet 0/9',
75            state='absent'
76        ))
77        result = self.execute_module(changed=True)
78        self.assertEqual(
79            result,
80            {
81                'commands': [
82                    'interface Ethernet 0/9',
83                    'no ip address',
84                    'no ipv6 address'
85                ],
86                'changed': True
87            }
88        )
89
90    def test_slxos_l3_interface_invalid_argument(self, *args, **kwargs):
91        set_module_args(dict(
92            name='Ethernet 0/1',
93            shawshank='Redemption'
94        ))
95        result = self.execute_module(failed=True)
96        self.assertEqual(result['failed'], True)
97        self.assertTrue(re.match(
98            r'Unsupported parameters for \((basic.py|basic.pyc)\) module: '
99            'shawshank Supported parameters include: aggregate, ipv4, ipv6, '
100            'name, state',
101            result['msg']
102        ))
103