Debian Codegraph @

ansible_collections/infoblox/nios_modules/tests/unit/plugins/modules/test_extensible_attribute.py

raw symbols SCIP
1 # This file is part of Ansible
2 #
3 # Ansible is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # Ansible is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
15
16 # Make coding more python3-ish
17
18
19 from __future__ import (absolute_import, division, print_function)
20 __metaclass__ = type
21
22
23 from ansible_collections.infoblox.nios_modules.plugins.modules import nios_extensible_attribute
24 from ansible_collections.infoblox.nios_modules.plugins.module_utils import api
25 from ansible_collections.infoblox.nios_modules.tests.unit.compat.mock import patch, MagicMock, Mock
26 from .test_nios_module import TestNiosModule, load_fixture
27
28
29 class TestNiosExtensibleAttributeModule(TestNiosModule):
30
31 module = nios_extensible_attribute
32
33 def setUp(self):
34 super(TestNiosExtensibleAttributeModule, self).setUp()
35 self.module = MagicMock(name='ansible_collections.infoblox.nios_modules.plugins.modules.nios_extensible_attribute.WapiModule')
36 self.module.check_mode = False
37 self.module.params = {'provider': None}
38 self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_extensible_attribute.WapiModule')
39 self.exec_command = self.mock_wapi.start()
40 self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_extensible_attribute.WapiModule.run')
41 self.load_config = self.mock_wapi_run.start()
42 self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
43 self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
44
45 def tearDown(self):
46 super(TestNiosExtensibleAttributeModule, self).tearDown()
47 self.mock_wapi.stop()
48 self.mock_wapi_run.stop()
49 self.mock_check_type_dict.stop()
50
51 def _get_wapi(self, test_object):
52 wapi = api.WapiModule(self.module)
53 wapi.get_object = Mock(name='get_object', return_value=test_object)
54 wapi.create_object = Mock(name='create_object')
55 wapi.update_object = Mock(name='update_object')
56 wapi.delete_object = Mock(name='delete_object')
57 return wapi
58
59 def load_fixtures(self, commands=None):
60 self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None)
61 self.load_config.return_value = dict(diff=None, session='session')
62
63 def test_create_extensible_attribute(self):
64 self.module.params = {
65 'name': 'my_string',
66 'type': 'STRING',
67 'comment': 'Created by ansible',
68 'state': 'present',
69 'provider': None
70 }
71
72 test_object = None
73
74 test_spec = {
75 "name": {"ib_req": True},
76 "type": {"ib_req": True},
77 "comment": {},
78 }
79
80 wapi = self._get_wapi(test_object)
81 print("WAPI: ", wapi.__dict__)
82 res = wapi.run('testobject', test_spec)
83
84 self.assertEqual(res['changed'], True)
85 wapi.create_object.assert_called_once_with('testobject', {'name': 'my_string',
86 'type': 'STRING',
87 'comment': 'Created by ansible'})
88
89 def test_nios_ea_update_comment(self):
90 self.module.params = {
91 'provider': None,
92 'state': 'present',
93 'name': 'testStringEA',
94 'type': 'STRING',
95 'flag': 'I',
96 'default_value': 'test',
97 'comment': 'Update comment'
98 }
99
100 ref = "extensibleattributedef/b25lLmV4dGVuc2libGVfYXR0cmlidXRlc19kZWYkLlRlcnJhZm9ybSBJbnRlcm5hbCBJRA:testStringEA"
101
102 test_object = [
103 {
104 "comment": "test comment",
105 "_ref": ref,
106 "name": "testStringEA",
107 "flag": "I",
108 "type": "STRING",
109 "default_value": "test"
110 }
111 ]
112
113 test_spec = {
114 "name": {"ib_req": True},
115 "type": {"ib_req": True},
116 "comment": {},
117 "flag": {},
118 "default_value": {}
119 }
120
121 wapi = self._get_wapi(test_object)
122 res = wapi.run('testobject', test_spec)
123 self.assertTrue(res['changed'])
124 wapi.update_object.assert_called_once_with(
125 ref, {'comment': 'Update comment', 'type': 'STRING', 'name': 'testStringEA', 'flag': 'I', 'default_value': 'test'}
126 )
127
128 def test_remove_extensible_attribute(self):
129 self.module.params = {'provider': None, 'state': 'absent', 'name': 'testStringEA', 'type': 'STRING',
130 'flag': None, 'default_value': None, 'comment': None}
131
132 ref = "extensibleattributedef/b25lLmV4dGVuc2libGVfYXR0cmlidXRlc19kZWYkLlRlcnJhZm9ybSBJbnRlcm5hbCBJRA:testStringEA"
133
134 test_object = [
135 {
136 "comment": "test comment",
137 "_ref": ref,
138 "name": "testStringEA",
139 "flag": "I",
140 "type": "STRING",
141 "default_value": "test"
142 }
143 ]
144
145 test_spec = {
146 "name": {"ib_req": True},
147 "type": {"ib_req": True},
148 "comment": {},
149 "flag": {},
150 "default_value": {}
151 }
152
153 wapi = self._get_wapi(test_object)
154 res = wapi.run('testobject', test_spec)
155
156 self.assertTrue(res['changed'])
157 wapi.delete_object.assert_called_once_with(ref)