Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/hpe_test_utils.py

raw
1 #
2 # Copyright (2016-2017) Hewlett Packard Enterprise Development LP
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 import re
9 from unittest.mock import Mock, patch
10
11 import pytest
12
13 from .oneview_module_loader import ONEVIEW_MODULE_UTILS_PATH
14
15 # This import must come *after* the oneview_module_loader import!
16 from hpOneView.oneview_client import OneViewClient # isort: skip
17
18
19 class OneViewBaseTest:
20 @pytest.fixture(autouse=True)
21 def setUp(self, mock_ansible_module, mock_ov_client, request):
22 marker = request.node.get_marker("resource")
23 self.resource = getattr(mock_ov_client, f"{marker.args}")
24 self.mock_ov_client = mock_ov_client
25 self.mock_ansible_module = mock_ansible_module
26
27 @pytest.fixture
28 def testing_module(self):
29 resource_name = type(self).__name__.replace("Test", "")
30 resource_module_path_name = resource_name.replace("Module", "")
31 resource_module_path_name = re.findall("[A-Z][^A-Z]*", resource_module_path_name)
32 resource_module_path_name = f"oneview_{str.join('_', resource_module_path_name).lower()}"
33
34 ansible_collections = __import__("ansible_collections")
35 oneview_module = ansible_collections.community.general.plugins.modules
36 resource_module = getattr(oneview_module, resource_module_path_name)
37 self.testing_class = getattr(resource_module, resource_name)
38 testing_module_name = self.testing_class.__module__.split(".")[-1]
39 testing_module = getattr(oneview_module, testing_module_name)
40 return testing_module
41
42 def test_main_function_should_call_run_method(self, testing_module, mock_ansible_module):
43 mock_ansible_module.params = {"config": "config.json"}
44
45 main_func = testing_module.main
46
47 with patch.object(self.testing_class, "run") as mock_run:
48 main_func()
49 mock_run.assert_called_once()
50
51
52 class FactsParamsTest(OneViewBaseTest):
53 def test_should_get_all_using_filters(self, testing_module):
54 self.resource.get_all.return_value = []
55
56 params_get_all_with_filters = dict(
57 config="config.json",
58 name=None,
59 params={
60 "start": 1,
61 "count": 3,
62 "sort": "name:descending",
63 "filter": "purpose=General",
64 "query": "imported eq true",
65 },
66 )
67 self.mock_ansible_module.params = params_get_all_with_filters
68
69 self.testing_class().run()
70
71 self.resource.get_all.assert_called_once_with(
72 start=1, count=3, sort="name:descending", filter="purpose=General", query="imported eq true"
73 )
74
75 def test_should_get_all_without_params(self, testing_module):
76 self.resource.get_all.return_value = []
77
78 params_get_all_with_filters = dict(config="config.json", name=None)
79 self.mock_ansible_module.params = params_get_all_with_filters
80
81 self.testing_class().run()
82
83 self.resource.get_all.assert_called_once_with()
84
85
86 class OneViewBaseTestCase:
87 mock_ov_client_from_json_file = None
88 testing_class = None
89 mock_ansible_module = None
90 mock_ov_client = None
91 testing_module = None
92
93 def configure_mocks(self, test_case, testing_class):
94 """
95 Preload mocked OneViewClient instance and AnsibleModule
96 Args:
97 test_case (object): class instance (self) that are inheriting from OneViewBaseTestCase
98 testing_class (object): class being tested
99 """
100 self.testing_class = testing_class
101
102 # Define OneView Client Mock (FILE)
103 patcher_json_file = patch.object(OneViewClient, "from_json_file")
104 test_case.addCleanup(patcher_json_file.stop)
105 self.mock_ov_client_from_json_file = patcher_json_file.start()
106
107 # Define OneView Client Mock
108 self.mock_ov_client = self.mock_ov_client_from_json_file.return_value
109
110 # Define Ansible Module Mock
111 patcher_ansible = patch(f"{ONEVIEW_MODULE_UTILS_PATH}.AnsibleModule")
112 test_case.addCleanup(patcher_ansible.stop)
113 mock_ansible_module = patcher_ansible.start()
114 self.mock_ansible_module = Mock()
115 mock_ansible_module.return_value = self.mock_ansible_module
116
117 ansible_collections = __import__("ansible_collections")
118 testing_module_name = self.testing_class.__module__.split(".")[-1]
119 self.testing_module = getattr(ansible_collections.community.general.plugins.modules, testing_module_name)
120
121 def test_main_function_should_call_run_method(self):
122 self.mock_ansible_module.params = {"config": "config.json"}
123
124 main_func = self.testing_module.main
125
126 with patch.object(self.testing_class, "run") as mock_run:
127 main_func()
128 mock_run.assert_called_once()
129
130
131 class FactsParamsTestCase(OneViewBaseTestCase):
132 """
133 FactsParamsTestCase has common test for classes that support pass additional
134 parameters when retrieving all resources.
135 """
136
137 def configure_client_mock(self, resorce_client):
138 """
139 Args:
140 resorce_client: Resource client that is being called
141 """
142 self.resource_client = resorce_client
143
144 def __validations(self):
145 if not self.testing_class:
146 raise Exception("Mocks are not configured, you must call 'configure_mocks' before running this test.")
147
148 if not self.resource_client:
149 raise Exception(
150 "Mock for the client not configured, you must call 'configure_client_mock' before running this test."
151 )
152
153 def test_should_get_all_using_filters(self):
154 self.__validations()
155 self.resource_client.get_all.return_value = []
156
157 params_get_all_with_filters = dict(
158 config="config.json",
159 name=None,
160 params={
161 "start": 1,
162 "count": 3,
163 "sort": "name:descending",
164 "filter": "purpose=General",
165 "query": "imported eq true",
166 },
167 )
168 self.mock_ansible_module.params = params_get_all_with_filters
169
170 self.testing_class().run()
171
172 self.resource_client.get_all.assert_called_once_with(
173 start=1, count=3, sort="name:descending", filter="purpose=General", query="imported eq true"
174 )
175
176 def test_should_get_all_without_params(self):
177 self.__validations()
178 self.resource_client.get_all.return_value = []
179
180 params_get_all_with_filters = dict(config="config.json", name=None)
181 self.mock_ansible_module.params = params_get_all_with_filters
182
183 self.testing_class().run()
184
185 self.resource_client.get_all.assert_called_once_with()