1#
2# (c) 2018 Red Hat Inc.
3# Copyright (C) 2017 Lenovo.
4#
5# This file is part of Ansible
6#
7# Ansible is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# Ansible is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
19#
20from __future__ import (absolute_import, division, print_function)
21__metaclass__ = type
22
23from ansible_collections.community.network.tests.unit.compat.mock import patch
24from ansible_collections.community.network.plugins.modules.network.cnos import cnos_interface
25from ansible_collections.community.network.tests.unit.plugins.modules.utils import set_module_args
26from .cnos_module import TestCnosModule, load_fixture
27
28
29class TestCnosInterfaceModule(TestCnosModule):
30    module = cnos_interface
31
32    def setUp(self):
33        super(TestCnosInterfaceModule, self).setUp()
34        self._patch_get_config = patch(
35            'ansible_collections.community.network.plugins.modules.network.cnos.cnos_interface.get_config'
36        )
37        self._patch_load_config = patch(
38            'ansible_collections.community.network.plugins.modules.network.cnos.cnos_interface.load_config'
39        )
40        self._patch_exec_command = patch(
41            'ansible_collections.community.network.plugins.modules.network.cnos.cnos_interface.exec_command'
42        )
43
44        self._get_config = self._patch_get_config.start()
45        self._load_config = self._patch_load_config.start()
46        self._exec_command = self._patch_exec_command.start()
47
48    def tearDown(self):
49        super(TestCnosInterfaceModule, self).tearDown()
50        self._patch_get_config.stop()
51        self._patch_load_config.stop()
52        self._patch_exec_command.stop()
53
54    def load_fixtures(self, commands=None):
55        config_file = 'cnos_config_config.cfg'
56        self._get_config.return_value = load_fixture(config_file)
57        self._load_config.return_value = None
58
59    def test_cnos_interface_description(self, *args, **kwargs):
60        set_module_args(dict(
61            name='Ethernet1/2',
62            description='show version'
63        ))
64        result = self.execute_module(changed=True)
65        self.assertEqual(
66            result,
67            {
68                'commands': [
69                    'interface Ethernet1/2',
70                    'description show version',
71                    'duplex auto'
72                ],
73                'changed': True
74            }
75        )
76
77    def test_cnos_interface_speed(self, *args, **kwargs):
78        set_module_args(dict(
79            name='Ethernet1/2',
80            speed=1000
81        ))
82        result = self.execute_module(changed=True)
83        self.assertEqual(
84            result,
85            {
86                'commands': [
87                    'interface Ethernet1/2',
88                    'speed 1000',
89                    'duplex auto'
90                ],
91                'changed': True
92            }
93        )
94
95    def test_cnos_interface_mtu(self, *args, **kwargs):
96        set_module_args(dict(
97            name='Ethernet1/2',
98            mtu=1548
99        ))
100        result = self.execute_module(changed=True)
101        self.assertEqual(
102            result,
103            {
104                'commands': [
105                    'interface Ethernet1/2',
106                    'duplex auto',
107                    'mtu 1548'
108                ],
109                'changed': True
110            }
111        )
112
113    def test_cnos_interface_mtu_out_of_range(self, *args, **kwargs):
114        set_module_args(dict(
115            name='Ethernet0/2',
116            mtu=15000
117        ))
118        result = self.execute_module(failed=True)
119        self.assertEqual(
120            result,
121            {
122                'msg': 'mtu must be between 64 and 9216',
123                'failed': True
124            }
125        )
126
127    def test_cnos_interface_enabled(self, *args, **kwargs):
128        set_module_args(dict(
129            name='Ethernet1/44',
130            enabled=True
131        ))
132        result = self.execute_module(changed=True)
133        self.assertEqual(
134            result,
135            {
136                'commands': [
137                    'interface Ethernet1/44',
138                    'duplex auto',
139                    'no shutdown'
140                ],
141                'changed': True
142            }
143        )
144