summaryrefslogtreecommitdiff
path: root/test/units/modules/network/eos/test_eos_eapi.py
blob: f002689e0c3ad7da4f849e8119384c8b2b455259 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# (c) 2016 Red Hat Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from units.compat.mock import patch
from ansible.modules.network.eos import eos_eapi
from units.modules.utils import set_module_args
from .eos_module import TestEosModule, load_fixture


class TestEosEapiModule(TestEosModule):

    module = eos_eapi

    def setUp(self):
        super(TestEosEapiModule, self).setUp()

        self.mock_run_commands = patch('ansible.modules.network.eos.eos_eapi.run_commands')
        self.run_commands = self.mock_run_commands.start()

        self.mock_load_config = patch('ansible.modules.network.eos.eos_eapi.load_config')
        self.load_config = self.mock_load_config.start()

        self.mock_verify_state = patch('ansible.modules.network.eos.eos_eapi.verify_state')
        self.verify_state = self.mock_verify_state.start()

        self.command_fixtures = {}

    def tearDown(self):
        super(TestEosEapiModule, self).tearDown()

        self.mock_run_commands.stop()
        self.mock_load_config.stop()

        # hack for older version of mock
        # should be using patch.stopall() but CI is still failing
        try:
            self.mock_verify_state.stop()
        except RuntimeError:
            pass

    def load_fixtures(self, commands=None, transport='eapi'):
        def run_commands(module, commands, **kwargs):
            output = list()
            for cmd in commands:
                output.append(load_fixture(self.command_fixtures[cmd]))
            return output

        self.run_commands.side_effect = run_commands
        self.load_config.return_value = dict(diff=None, session='session')

    def start_configured(self, *args, **kwargs):
        self.command_fixtures = {
            'show vrf': 'eos_eapi_show_vrf.text',
            'show management api http-commands | json': 'eos_eapi_show_mgmt.json'
        }
        return self.execute_module(*args, **kwargs)

    def start_unconfigured(self, *args, **kwargs):
        self.command_fixtures = {
            'show vrf': 'eos_eapi_show_vrf.text',
            'show management api http-commands | json': 'eos_eapi_show_mgmt_unconfigured.json'
        }
        return self.execute_module(*args, **kwargs)

    def test_eos_eapi_http_enable(self):
        set_module_args(dict(http=True))
        commands = ['management api http-commands', 'protocol http port 80',
                    'no shutdown']
        self.start_unconfigured(changed=True, commands=commands)

    def test_eos_eapi_http_disable(self):
        set_module_args(dict(http=False))
        commands = ['management api http-commands', 'no protocol http']
        self.start_configured(changed=True, commands=commands)

    def test_eos_eapi_http_port(self):
        set_module_args(dict(http_port=81))
        commands = ['management api http-commands', 'protocol http port 81']
        self.start_configured(changed=True, commands=commands)

    def test_eos_eapi_http_invalid(self):
        set_module_args(dict(http_port=80000))
        self.start_unconfigured(failed=True)

    def test_eos_eapi_https_enable(self):
        set_module_args(dict(https=True))
        commands = ['management api http-commands', 'protocol https port 443',
                    'no shutdown']
        self.start_unconfigured(changed=True, commands=commands)

    def test_eos_eapi_https_disable(self):
        set_module_args(dict(https=False))
        commands = ['management api http-commands', 'no protocol https']
        self.start_configured(changed=True, commands=commands)

    def test_eos_eapi_https_port(self):
        set_module_args(dict(https_port=8443))
        commands = ['management api http-commands', 'protocol https port 8443']
        self.start_configured(changed=True, commands=commands)

    def test_eos_eapi_local_http_enable(self):
        set_module_args(dict(local_http=True))
        commands = ['management api http-commands', 'protocol http localhost port 8080',
                    'no shutdown']
        self.start_unconfigured(changed=True, commands=commands)

    def test_eos_eapi_local_http_disable(self):
        set_module_args(dict(local_http=False))
        commands = ['management api http-commands', 'no protocol http localhost']
        self.start_configured(changed=True, commands=commands)

    def test_eos_eapi_local_http_port(self):
        set_module_args(dict(local_http_port=81))
        commands = ['management api http-commands', 'protocol http localhost port 81']
        self.start_configured(changed=True, commands=commands)

    def test_eos_eapi_vrf(self):
        set_module_args(dict(vrf='test'))
        commands = ['management api http-commands', 'no shutdown', 'vrf test', 'no shutdown']
        self.start_unconfigured(changed=True, commands=commands)

    def test_eos_eapi_change_from_default_vrf(self):
        set_module_args(dict(vrf='test'))
        commands = ['management api http-commands', 'vrf test', 'no shutdown']
        self.start_configured(changed=True, commands=commands)

    def test_eos_eapi_default(self):
        set_module_args(dict())
        self.start_configured(changed=False, commands=[])

    def test_eos_eapi_vrf_missing(self):
        set_module_args(dict(vrf='missing'))
        self.start_unconfigured(failed=True)

    def test_eos_eapi_state_absent(self):
        set_module_args(dict(state='stopped'))
        commands = ['management api http-commands', 'shutdown']
        self.start_configured(changed=True, commands=commands)

    def test_eos_eapi_state_failed(self):
        self.mock_verify_state.stop()
        set_module_args(dict(state='stopped', timeout=1))
        result = self.start_configured(failed=True)
        'timeout expired before eapi running state changed' in result['msg']

    def test_eos_eapi_state_failed(self):
        self.mock_verify_state.stop()
        set_module_args(dict(state='stopped', timeout=1))
        result = self.start_configured(failed=True)
        'timeout expired before eapi running state changed' in result['msg']