1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/tests/unit/modules/net_tools/nios/test_nios_nsgroup.py
Felix Fontein c012d0fba7
Fix unit test relative imports, and permissions for included collection requirements (#29)
* Fix Hetzner firewall unit test imports.

* Make sure tests can actually access collections.

* Fix more relative imports.

* Fix more relative imports.

* Fix more includes.

* Fix more tests.

* One more.

* Fix syntax error in sanity import tests (invalid escape sequence "\$" caused by non-raw docs block)

* Fix permissions of ansible-test parts for sanity tests.

* Revert "Fix permissions of ansible-test parts for sanity tests."

This reverts commit c2713f0a12.
2020-03-24 08:27:28 +00:00

125 lines
4.6 KiB
Python

# 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_collections.community.general.plugins.modules.net_tools.nios import nios_nsgroup
from ansible_collections.community.general.plugins.module_utils.net_tools.nios import api
from ansible_collections.community.general.tests.unit.compat.mock import patch, MagicMock, Mock
from .test_nios_module import TestNiosModule, load_fixture
class TestNiosNSGroupModule(TestNiosModule):
module = nios_nsgroup
def setUp(self):
super(TestNiosNSGroupModule, self).setUp()
self.module = MagicMock(name='ansible_collections.community.general.plugins.modules.net_tools.nios.nios_nsgroup.WapiModule')
self.module.check_mode = False
self.module.params = {'provider': None}
self.mock_wapi = patch('ansible_collections.community.general.plugins.modules.net_tools.nios.nios_nsgroup.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.community.general.plugins.modules.net_tools.nios.nios_nsgroup.WapiModule.run')
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()
def tearDown(self):
super(TestNiosNSGroupModule, self).tearDown()
self.mock_wapi.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_nsgroup_create(self):
self.module.params = {'provider': None, 'state': 'present', 'name': 'my-simple-group',
'comment': None, 'grid_primary': None}
test_object = None
test_spec = {
"name": {"ib_req": True},
"comment": {},
"grid_primary": {}
}
wapi = self._get_wapi(test_object)
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__()})
def test_nios_nsgroup_remove(self):
self.module.params = {'provider': None, 'state': 'absent', 'name': 'my-simple-group',
'comment': None, 'grid_primary': None}
ref = "nsgroup/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/false"
test_object = [{
"comment": "test comment",
"_ref": ref,
"name": "my-simple-group",
"grid_primary": {'name': 'infoblox-test.example.com'}
}]
test_spec = {
"name": {"ib_req": True},
"comment": {},
"grid_primary": {}
}
wapi = self._get_wapi(test_object)
res = wapi.run('testobject', test_spec)
self.assertTrue(res['changed'])
wapi.delete_object.assert_called_once_with(ref)
def test_nios_nsgroup_update_comment(self):
self.module.params = {'provider': None, 'state': 'present', 'name': 'default',
'comment': 'updated comment', 'grid_primary': None}
test_object = [
{
"comment": "test comment",
"_ref": "nsgroup/ZG5zLm5ldHdvcmtfdmlldyQw:default/true",
"name": "default",
"grid_primary": {}
}
]
test_spec = {
"name": {"ib_req": True},
"comment": {},
"grid_primary": {}
}
wapi = self._get_wapi(test_object)
res = wapi.run('testobject', test_spec)
self.assertTrue(res['changed'])
wapi.update_object.called_once_with(test_object)