| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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) |