Debian Codegraph @

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

raw
1 # Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import unittest
8
9 from .hpe_test_utils import FactsParamsTestCase
10
11 # This import must come *after* the oneview_module_loader / hpe_test_utils import!
12 from ansible_collections.community.general.plugins.modules.oneview_enclosure_info import EnclosureInfoModule # isort: skip
13
14 ERROR_MSG = "Fake message error"
15
16 PARAMS_GET_ALL = dict(config="config.json", name=None)
17
18 PARAMS_GET_BY_NAME = dict(config="config.json", name="Test-Enclosure", options=[])
19
20 PARAMS_GET_BY_NAME_WITH_OPTIONS = dict(
21 config="config.json", name="Test-Enclosure", options=["utilization", "environmentalConfiguration", "script"]
22 )
23
24 PARAMS_GET_UTILIZATION_WITH_PARAMS = dict(
25 config="config.json",
26 name="Test-Enclosure",
27 options=[
28 dict(
29 utilization=dict(
30 fields="AveragePower",
31 filter=["startDate=2016-06-30T03:29:42.000Z", "endDate=2016-07-01T03:29:42.000Z"],
32 view="day",
33 refresh=True,
34 )
35 )
36 ],
37 )
38
39 PRESENT_ENCLOSURES = [{"name": "Test-Enclosure", "uri": "/rest/enclosures/c6bf9af9-48e7-4236-b08a-77684dc258a5"}]
40
41 ENCLOSURE_SCRIPT = "# script content"
42
43 ENCLOSURE_UTILIZATION = {"isFresh": "True"}
44
45 ENCLOSURE_ENVIRONMENTAL_CONFIG = {"calibratedMaxPower": "2500"}
46
47
48 class EnclosureInfoSpec(unittest.TestCase, FactsParamsTestCase):
49 def setUp(self):
50 self.configure_mocks(self, EnclosureInfoModule)
51 self.enclosures = self.mock_ov_client.enclosures
52 FactsParamsTestCase.configure_client_mock(self, self.enclosures)
53
54 def test_should_get_all_enclosures(self):
55 self.enclosures.get_all.return_value = PRESENT_ENCLOSURES
56 self.mock_ansible_module.params = PARAMS_GET_ALL
57
58 EnclosureInfoModule().run()
59
60 self.mock_ansible_module.exit_json.assert_called_once_with(changed=False, enclosures=(PRESENT_ENCLOSURES))
61
62 def test_should_get_enclosure_by_name(self):
63 self.enclosures.get_by.return_value = PRESENT_ENCLOSURES
64 self.mock_ansible_module.params = PARAMS_GET_BY_NAME
65
66 EnclosureInfoModule().run()
67
68 self.mock_ansible_module.exit_json.assert_called_once_with(changed=False, enclosures=(PRESENT_ENCLOSURES))
69
70 def test_should_get_enclosure_by_name_with_options(self):
71 self.enclosures.get_by.return_value = PRESENT_ENCLOSURES
72 self.enclosures.get_script.return_value = ENCLOSURE_SCRIPT
73 self.enclosures.get_utilization.return_value = ENCLOSURE_UTILIZATION
74 self.enclosures.get_environmental_configuration.return_value = ENCLOSURE_ENVIRONMENTAL_CONFIG
75
76 self.mock_ansible_module.params = PARAMS_GET_BY_NAME_WITH_OPTIONS
77
78 EnclosureInfoModule().run()
79
80 self.mock_ansible_module.exit_json.assert_called_once_with(
81 changed=False,
82 enclosures=PRESENT_ENCLOSURES,
83 enclosure_script=ENCLOSURE_SCRIPT,
84 enclosure_environmental_configuration=ENCLOSURE_ENVIRONMENTAL_CONFIG,
85 enclosure_utilization=ENCLOSURE_UTILIZATION,
86 )
87
88 def test_should_get_all_utilization_data(self):
89 self.enclosures.get_by.return_value = PRESENT_ENCLOSURES
90 self.enclosures.get_script.return_value = ENCLOSURE_SCRIPT
91 self.enclosures.get_utilization.return_value = ENCLOSURE_UTILIZATION
92 self.enclosures.get_environmental_configuration.return_value = ENCLOSURE_ENVIRONMENTAL_CONFIG
93
94 self.mock_ansible_module.params = PARAMS_GET_BY_NAME_WITH_OPTIONS
95
96 EnclosureInfoModule().run()
97
98 self.enclosures.get_utilization.assert_called_once_with(
99 PRESENT_ENCLOSURES[0]["uri"], fields="", filter="", view="", refresh=""
100 )
101
102 def test_should_get_utilization_with_parameters(self):
103 self.enclosures.get_by.return_value = PRESENT_ENCLOSURES
104 self.enclosures.get_script.return_value = ENCLOSURE_SCRIPT
105 self.enclosures.get_utilization.return_value = ENCLOSURE_UTILIZATION
106 self.enclosures.get_environmental_configuration.return_value = ENCLOSURE_ENVIRONMENTAL_CONFIG
107
108 self.mock_ansible_module.params = PARAMS_GET_UTILIZATION_WITH_PARAMS
109
110 EnclosureInfoModule().run()
111
112 date_filter = ["startDate=2016-06-30T03:29:42.000Z", "endDate=2016-07-01T03:29:42.000Z"]
113
114 self.enclosures.get_utilization.assert_called_once_with(
115 PRESENT_ENCLOSURES[0]["uri"], fields="AveragePower", filter=date_filter, view="day", refresh=True
116 )