| 1 |
|
| 2 |
|
| 3 |
from __future__ import absolute_import, division, print_function |
| 4 |
__metaclass__ = type |
| 5 |
from contextlib import contextmanager |
| 6 |
from ansible.module_utils.testing import patch_module_args |
| 7 |
from ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_hostgroup import NetAppESeriesHostGroup |
| 8 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 9 |
AnsibleFailJson, AnsibleExitJson, ModuleTestCase |
| 10 |
) |
| 11 |
from ansible_collections.community.internal_test_tools.tests.unit.compat import mock |
| 12 |
|
| 13 |
|
| 14 |
class HostTest(ModuleTestCase): |
| 15 |
REQUIRED_PARAMS = {"api_username": "rw", |
| 16 |
"api_password": "password", |
| 17 |
"api_url": "http://localhost", |
| 18 |
"ssid": "1"} |
| 19 |
REQ_FUNC = "ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_hostgroup.NetAppESeriesHostGroup.request" |
| 20 |
HOSTS_GET_RESPONSE = [ |
| 21 |
{"hostRef": "84000000600A098000A4B28D0030102E5C3DFC0F", |
| 22 |
"clusterRef": "85000000600A098000A4B28D0036102C5C3DFC08", "id": "84000000600A098000A4B28D0030102E5C3DFC0F", |
| 23 |
"name": "host1"}, |
| 24 |
{"hostRef": "84000000600A098000A4B28D003010315C3DFC11", |
| 25 |
"clusterRef": "85000000600A098000A4B9D100360F765C3DFC1C", "id": "84000000600A098000A4B28D003010315C3DFC11", |
| 26 |
"name": "host2"}, |
| 27 |
{"hostRef": "84000000600A098000A4B28D003010345C3DFC14", |
| 28 |
"clusterRef": "85000000600A098000A4B9D100360F765C3DFC1C", "id": "84000000600A098000A4B28D003010345C3DFC14", |
| 29 |
"name": "host3"}] |
| 30 |
HOSTGROUPS_GET_RESPONSE = [ |
| 31 |
{"clusterRef": "85000000600A098000A4B28D0036102C5C3DFC08", "id": "85000000600A098000A4B28D0036102C5C3DFC08", |
| 32 |
"name": "group1"}, |
| 33 |
{"clusterRef": "85000000600A098000A4B9D100360F765C3DFC1C", "id": "85000000600A098000A4B9D100360F765C3DFC1C", |
| 34 |
"name": "group2"}, |
| 35 |
{"clusterRef": "85000000600A098000A4B9D100360F775C3DFC1E", "id": "85000000600A098000A4B9D100360F775C3DFC1E", |
| 36 |
"name": "group3"}] |
| 37 |
|
| 38 |
@contextmanager |
| 39 |
def _set_args(self, args): |
| 40 |
self.module_args = self.REQUIRED_PARAMS.copy() |
| 41 |
self.module_args.update(args) |
| 42 |
with patch_module_args(self.module_args): |
| 43 |
yield |
| 44 |
|
| 45 |
def test_hosts_fail(self): |
| 46 |
"""Ensure that the host property method fails when self.request throws an exception.""" |
| 47 |
with self._set_args({"state": "present", "name": "hostgroup1", "hosts": ["host1", "host2"]}): |
| 48 |
hostgroup_object = NetAppESeriesHostGroup() |
| 49 |
with self.assertRaises(AnsibleFailJson): |
| 50 |
with mock.patch(self.REQ_FUNC, return_value=Exception()): |
| 51 |
hosts = hostgroup_object.hosts |
| 52 |
|
| 53 |
with self._set_args({"state": "present", "name": "hostgroup1", "hosts": ["host1", "host2"]}): |
| 54 |
hostgroup_object = NetAppESeriesHostGroup() |
| 55 |
with mock.patch(self.REQ_FUNC, return_value=(200, [])): |
| 56 |
with self.assertRaisesRegex(AnsibleFailJson, "Expected host does not exist"): |
| 57 |
hosts = hostgroup_object.hosts |
| 58 |
|
| 59 |
def test_hosts_pass(self): |
| 60 |
"""Evaluate hosts property method for valid returned data structure.""" |
| 61 |
expected_host_list = ['84000000600A098000A4B28D003010315C3DFC11', '84000000600A098000A4B28D0030102E5C3DFC0F'] |
| 62 |
for hostgroup_hosts in [["host1", "host2"], ["84000000600A098000A4B28D0030102E5C3DFC0F", |
| 63 |
"84000000600A098000A4B28D003010315C3DFC11"]]: |
| 64 |
with self._set_args({"state": "present", "name": "hostgroup1", "hosts": hostgroup_hosts}): |
| 65 |
hostgroup_object = NetAppESeriesHostGroup() |
| 66 |
|
| 67 |
with mock.patch(self.REQ_FUNC, return_value=(200, self.HOSTS_GET_RESPONSE)): |
| 68 |
for item in hostgroup_object.hosts: |
| 69 |
self.assertTrue(item in expected_host_list) |
| 70 |
|
| 71 |
|
| 72 |
with self._set_args({"state": "present", "name": "hostgroup1"}): |
| 73 |
hostgroup_object = NetAppESeriesHostGroup() |
| 74 |
with mock.patch(self.REQ_FUNC, return_value=(200, [])): |
| 75 |
self.assertEqual(hostgroup_object.hosts, []) |
| 76 |
|
| 77 |
def test_host_groups_fail(self): |
| 78 |
"""Ensure that the host_groups property method fails when self.request throws an exception.""" |
| 79 |
with self._set_args({"state": "present", "name": "hostgroup1", "hosts": ["host1", "host2"]}): |
| 80 |
hostgroup_object = NetAppESeriesHostGroup() |
| 81 |
with self.assertRaises(AnsibleFailJson): |
| 82 |
with mock.patch(self.REQ_FUNC, return_value=Exception()): |
| 83 |
host_groups = hostgroup_object.host_groups |
| 84 |
|
| 85 |
def test_host_groups_pass(self): |
| 86 |
"""Evaluate host_groups property method for valid return data structure.""" |
| 87 |
expected_groups = [ |
| 88 |
{'hosts': ['84000000600A098000A4B28D0030102E5C3DFC0F'], 'id': '85000000600A098000A4B28D0036102C5C3DFC08', |
| 89 |
'name': 'group1'}, |
| 90 |
{'hosts': ['84000000600A098000A4B28D003010315C3DFC11', '84000000600A098000A4B28D003010345C3DFC14'], |
| 91 |
'id': '85000000600A098000A4B9D100360F765C3DFC1C', 'name': 'group2'}, |
| 92 |
{'hosts': [], 'id': '85000000600A098000A4B9D100360F775C3DFC1E', 'name': 'group3'}] |
| 93 |
|
| 94 |
with self._set_args({"state": "present", "name": "hostgroup1", "hosts": ["host1", "host2"]}): |
| 95 |
hostgroup_object = NetAppESeriesHostGroup() |
| 96 |
|
| 97 |
with mock.patch(self.REQ_FUNC, |
| 98 |
side_effect=[(200, self.HOSTGROUPS_GET_RESPONSE), (200, self.HOSTS_GET_RESPONSE)]): |
| 99 |
self.assertEqual(hostgroup_object.host_groups, expected_groups) |
| 100 |
|
| 101 |
@mock.patch.object(NetAppESeriesHostGroup, "host_groups") |
| 102 |
@mock.patch.object(NetAppESeriesHostGroup, "hosts") |
| 103 |
@mock.patch.object(NetAppESeriesHostGroup, "create_host_group") |
| 104 |
@mock.patch.object(NetAppESeriesHostGroup, "update_host_group") |
| 105 |
@mock.patch.object(NetAppESeriesHostGroup, "delete_host_group") |
| 106 |
def test_apply_pass(self, fake_delete_host_group, fake_update_host_group, fake_create_host_group, fake_hosts, |
| 107 |
fake_host_groups): |
| 108 |
"""Apply desired host group state to the storage array.""" |
| 109 |
hosts_response = ['84000000600A098000A4B28D003010315C3DFC11', '84000000600A098000A4B28D0030102E5C3DFC0F'] |
| 110 |
host_groups_response = [ |
| 111 |
{'hosts': ['84000000600A098000A4B28D0030102E5C3DFC0F'], 'id': '85000000600A098000A4B28D0036102C5C3DFC08', |
| 112 |
'name': 'group1'}, |
| 113 |
{'hosts': ['84000000600A098000A4B28D003010315C3DFC11', '84000000600A098000A4B28D003010345C3DFC14'], |
| 114 |
'id': '85000000600A098000A4B9D100360F765C3DFC1C', 'name': 'group2'}, |
| 115 |
{'hosts': [], 'id': '85000000600A098000A4B9D100360F775C3DFC1E', 'name': 'group3'}] |
| 116 |
|
| 117 |
fake_host_groups.return_value = host_groups_response |
| 118 |
fake_hosts.return_value = hosts_response |
| 119 |
fake_create_host_group.return_value = lambda x: "Host group created!" |
| 120 |
fake_update_host_group.return_value = lambda x: "Host group updated!" |
| 121 |
fake_delete_host_group.return_value = lambda x: "Host group deleted!" |
| 122 |
|
| 123 |
|
| 124 |
with self._set_args({"state": "present", "name": "hostgroup1", "hosts": ["host1", "host2"]}): |
| 125 |
hostgroup_object = NetAppESeriesHostGroup() |
| 126 |
with self.assertRaises(AnsibleExitJson): |
| 127 |
hostgroup_object.apply() |
| 128 |
|
| 129 |
|
| 130 |
with self._set_args({"state": "present", "name": "group1", "hosts": ["host1"]}): |
| 131 |
hostgroup_object = NetAppESeriesHostGroup() |
| 132 |
with self.assertRaises(AnsibleExitJson): |
| 133 |
hostgroup_object.apply() |
| 134 |
|
| 135 |
|
| 136 |
with self._set_args({"state": "present", "name": "group1", "hosts": ["host1", "host2"]}): |
| 137 |
hostgroup_object = NetAppESeriesHostGroup() |
| 138 |
with self.assertRaises(AnsibleExitJson): |
| 139 |
hostgroup_object.apply() |
| 140 |
|
| 141 |
|
| 142 |
with self._set_args({"state": "absent", "name": "group1"}): |
| 143 |
hostgroup_object = NetAppESeriesHostGroup() |
| 144 |
with self.assertRaises(AnsibleExitJson): |
| 145 |
hostgroup_object.apply() |