summaryrefslogtreecommitdiff
path: root/test/units/modules/net_tools/nios/test_nios_srv_record.py
blob: 2c0b27388c150314baebaf7275ad4de9983be213 (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
# 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 ansible.modules.net_tools.nios import nios_srv_record
from ansible.module_utils.net_tools.nios import api
from units.compat.mock import patch, MagicMock, Mock
from .test_nios_module import TestNiosModule, load_fixture


class TestNiosSRVRecordModule(TestNiosModule):

    module = nios_srv_record

    def setUp(self):
        super(TestNiosSRVRecordModule, self).setUp()
        self.module = MagicMock(name='ansible.modules.net_tools.nios.nios_srv_record.WapiModule')
        self.module.check_mode = False
        self.module.params = {'provider': None}
        self.mock_wapi = patch('ansible.modules.net_tools.nios.nios_srv_record.WapiModule')
        self.exec_command = self.mock_wapi.start()
        self.mock_wapi_run = patch('ansible.modules.net_tools.nios.nios_srv_record.WapiModule.run')
        self.mock_wapi_run.start()
        self.load_config = self.mock_wapi_run.start()

    def tearDown(self):
        super(TestNiosSRVRecordModule, self).tearDown()
        self.mock_wapi.stop()
        self.mock_wapi_run.stop()

    def _get_wapi(self, test_object):
        wapi = api.WapiModule(self.module)
        wapi.get_object = Mock(name='get_object', return_value=test_object)
        wapi.create_object = Mock(name='create_object')
        wapi.update_object = Mock(name='update_object')
        wapi.delete_object = Mock(name='delete_object')
        return wapi

    def load_fixtures(self, commands=None):
        self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None)
        self.load_config.return_value = dict(diff=None, session='session')

    def test_nios_srv_record_create(self):
        self.module.params = {'provider': None, 'state': 'present', 'name': '_sip._tcp.service.ansible.com',
                              'port': 5080, 'target': 'service1.ansible.com', 'priority': 10, 'weight': 10,
                              'comment': None, 'extattrs': None}

        test_object = None

        test_spec = {
            "name": {"ib_req": True},
            "port": {"ib_req": True},
            "target": {"ib_req": True},
            "priority": {"ib_req": True},
            "weight": {"ib_req": True},
            "comment": {},
            "extattrs": {}
        }

        wapi = self._get_wapi(test_object)
        print("WAPI: ", wapi)
        res = wapi.run('testobject', test_spec)

        self.assertTrue(res['changed'])
        wapi.create_object.assert_called_once_with('testobject', {'name': self.module._check_type_dict().__getitem__(),
                                                                  'port': 5080, 'target': 'service1.ansible.com', 'priority': 10, 'weight': 10})

    def test_nios_srv_record_update_comment(self):
        self.module.params = {'provider': None, 'state': 'present', 'name': '_sip._tcp.service.ansible.com',
                              'port': 5080, 'target': 'service1.ansible.com', 'priority': 10, 'weight': 10,
                              'comment': None, 'extattrs': None}

        test_object = [
            {
                "comment": "test comment",
                "_ref": "srvrecord/ZG5zLm5ldHdvcmtfdmlldyQw:default/true",
                "name": "_sip._tcp.service.ansible.com",
                'port': 5080,
                "target": "mailhost.ansible.com",
                "priority": 10,
                'weight': 10,
                "extattrs": {}
            }
        ]

        test_spec = {
            "name": {"ib_req": True},
            "port": {"ib_req": True},
            "target": {"ib_req": True},
            "priority": {"ib_req": True},
            "weight": {"ib_req": True},
            "comment": {},
            "extattrs": {}
        }

        wapi = self._get_wapi(test_object)
        res = wapi.run('testobject', test_spec)

        self.assertTrue(res['changed'])

    def test_nios_srv_record_remove(self):
        self.module.params = {'provider': None, 'state': 'absent', 'name': '_sip._tcp.service.ansible.com',
                              'port': 5080, 'target': 'service1.ansible.com', 'priority': 10, 'weight': 10,
                              'comment': None, 'extattrs': None}

        ref = "srvrecord/ZG5zLm5ldHdvcmtfdmlldyQw:default/false"

        test_object = [
            {
                "comment": "test comment",
                "_ref": ref,
                "name": "_sip._tcp.service.ansible.com",
                "port": 5080,
                "target": "mailhost.ansible.com",
                "priority": 10,
                "weight": 10,
                "extattrs": {'Site': {'value': 'test'}}
            }
        ]

        test_spec = {
            "name": {"ib_req": True},
            "port": {"ib_req": True},
            "target": {"ib_req": True},
            "priority": {"ib_req": True},
            "weight": {"ib_req": True},
            "comment": {},
            "extattrs": {}
        }

        wapi = self._get_wapi(test_object)
        res = wapi.run('testobject', test_spec)

        self.assertTrue(res['changed'])
        wapi.delete_object.assert_called_once_with(ref)