summaryrefslogtreecommitdiff
path: root/test/units/modules/network/ironware/test_ironware_config.py
blob: ad228db05a8c7ab4ccea013e251b6be39cea6f3b (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
#
# (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

import json

from ansible.compat.tests.mock import patch
from ansible.modules.network.ironware import ironware_config
from .ironware_module import TestIronwareModule, load_fixture
from units.modules.utils import set_module_args


class TestIronwareConfigModule(TestIronwareModule):

    module = ironware_config

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

        self.mock_get_config = patch('ansible.modules.network.ironware.ironware_config.get_config')
        self.get_config = self.mock_get_config.start()

        self.mock_load_config = patch('ansible.modules.network.ironware.ironware_config.load_config')
        self.load_config = self.mock_load_config.start()

        self.mock_run_commands = patch('ansible.modules.network.ironware.ironware_config.run_commands')
        self.run_commands = self.mock_run_commands.start()

    def tearDown(self):
        super(TestIronwareConfigModule, self).tearDown()
        self.mock_get_config.stop()
        self.mock_load_config.stop()
        self.mock_run_commands.stop()

    def load_fixtures(self, commands=None):
        config_file = 'ironware_config_config.cfg'
        self.get_config.return_value = load_fixture(config_file)
        self.load_config.return_value = None

    def execute_module(self, failed=False, changed=False, updates=None, sort=True, defaults=False):

        self.load_fixtures(updates)

        if failed:
            result = self.failed()
            self.assertTrue(result['failed'], result)
        else:
            result = self.changed(changed)
            self.assertEqual(result['changed'], changed, result)

        if updates is not None:
            if sort:
                self.assertEqual(sorted(updates), sorted(result['updates']), result['updates'])
            else:
                self.assertEqual(updates, result['updates'], result['updates'])

        return result

    def test_ironware_config_unchanged(self):
        src = load_fixture('ironware_config_config.cfg')
        set_module_args(dict(src=src))
        self.execute_module()

    def test_ironware_config_src(self):
        src = load_fixture('ironware_config_src.cfg')
        set_module_args(dict(src=src))
        updates = ['hostname foo', 'interface ethernet 1/1',
                   'no ip address']
        self.execute_module(changed=True, updates=updates)

    def test_ironware_config_backup(self):
        set_module_args(dict(backup=True))
        result = self.execute_module()
        self.assertIn('__backup__', result)

    def test_ironware_config_save_always(self):
        self.run_commands.return_value = "hostname foobar"
        set_module_args(dict(save_when='always'))
        self.execute_module(changed=True)
        self.assertEqual(self.run_commands.call_count, 1)
        self.assertEqual(self.get_config.call_count, 1)
        self.assertEqual(self.load_config.call_count, 0)

    def test_ironware_config_lines_wo_parents(self):
        set_module_args(dict(lines=['hostname foobar']))
        updates = ['hostname foobar']
        self.execute_module(changed=True, updates=updates)

    def test_ironware_config_lines_w_parents(self):
        set_module_args(dict(lines=['disable'], parents=['interface ethernet 1/1']))
        updates = ['interface ethernet 1/1', 'disable']
        self.execute_module(changed=True, updates=updates)

    def test_ironware_config_before(self):
        set_module_args(dict(lines=['hostname foo'], before=['test1', 'test2']))
        updates = ['test1', 'test2', 'hostname foo']
        self.execute_module(changed=True, updates=updates, sort=False)

    def test_ironware_config_after(self):
        set_module_args(dict(lines=['hostname foo'], after=['test1', 'test2']))
        updates = ['hostname foo', 'test1', 'test2']
        self.execute_module(changed=True, updates=updates, sort=False)

    def test_ironware_config_before_after_no_change(self):
        set_module_args(dict(lines=['hostname router'],
                             before=['test1', 'test2'],
                             after=['test3', 'test4']))
        self.execute_module()

    def test_ironware_config_config(self):
        config = 'hostname localhost'
        set_module_args(dict(lines=['hostname router'], config=config))
        updates = ['hostname router']
        self.execute_module(changed=True, updates=updates)

    def test_ironware_config_replace_block(self):
        lines = ['port-name test string', 'test string']
        parents = ['interface ethernet 1/1']
        set_module_args(dict(lines=lines, replace='block', parents=parents))
        updates = parents + lines
        self.execute_module(changed=True, updates=updates)

    def test_ironware_config_match_none(self):
        lines = ['hostname router']
        set_module_args(dict(lines=lines, match='none'))
        self.execute_module(changed=True, updates=lines)

    def test_ironware_config_match_none(self):
        lines = ['ip address 1.2.3.4 255.255.255.0', 'port-name test string']
        parents = ['interface ethernet 1/1']
        set_module_args(dict(lines=lines, parents=parents, match='none'))
        updates = parents + lines
        self.execute_module(changed=True, updates=updates, sort=False)

    def test_ironware_config_match_strict(self):
        lines = ['ip address 1.2.3.4 255.255.255.0', 'port-name test string',
                 'disable']
        parents = ['interface ethernet 1/1']
        set_module_args(dict(lines=lines, parents=parents, match='strict'))
        updates = parents + ['disable']
        self.execute_module(changed=True, updates=updates, sort=False)

    def test_ironware_config_match_exact(self):
        lines = ['ip address 1.2.3.4 255.255.255.0', 'port-name test string',
                 'disable']
        parents = ['interface ethernet 1/1']
        set_module_args(dict(lines=lines, parents=parents, match='exact'))
        updates = parents + lines
        self.execute_module(changed=True, updates=updates, sort=False)