diff --git a/prometheus_hardware_exporter/__main__.py b/prometheus_hardware_exporter/__main__.py index 0fb43cc..e6006f2 100644 --- a/prometheus_hardware_exporter/__main__.py +++ b/prometheus_hardware_exporter/__main__.py @@ -121,7 +121,7 @@ def parse_command_line() -> argparse.Namespace: ) parser.add_argument( "--redfish-client-timeout", - help="Redfish client timeout in seconds for initial connection (default: 3) ", + help="Redfish client timeout in seconds for initial connection (default: 15) ", default=DEFAULT_REDFISH_CLIENT_TIMEOUT, type=int, ) diff --git a/prometheus_hardware_exporter/collector.py b/prometheus_hardware_exporter/collector.py index fe93c79..02f4231 100644 --- a/prometheus_hardware_exporter/collector.py +++ b/prometheus_hardware_exporter/collector.py @@ -995,41 +995,48 @@ def fetch(self) -> List[Payload]: if not service_status: return payloads + redfish_helper = None try: - with RedfishHelper(self.config) as redfish_helper: - payloads.append(Payload(name="redfish_call_success", value=1.0)) - - processor_count: Dict[str, int] - processor_data: Dict[str, List] - storage_controller_count: Dict[str, int] - storage_controller_data: Dict[str, List] - storage_drive_count: Dict[str, int] - storage_drive_data: Dict[str, List] - memory_dimm_count: Dict[str, int] - memory_dimm_data: Dict[str, List] - - sensor_data: Dict[str, List] = redfish_helper.get_sensor_data() - processor_count, processor_data = redfish_helper.get_processor_data() - ( - storage_controller_count, - storage_controller_data, - ) = redfish_helper.get_storage_controller_data() - network_adapter_count: Dict[str, Any] = redfish_helper.get_network_adapter_data() - chassis_data: Dict[str, Dict] = redfish_helper.get_chassis_data() - storage_drive_count, storage_drive_data = redfish_helper.get_storage_drive_data() - memory_dimm_count, memory_dimm_data = redfish_helper.get_memory_dimm_data() - smart_storage_health_data: Dict[str, Any] = ( - redfish_helper.get_smart_storage_health_data() - ) - except ( + redfish_helper = RedfishHelper(self.config) + redfish_helper.login() + payloads.append(Payload(name="redfish_call_success", value=1.0)) + + processor_count: Dict[str, int] + processor_data: Dict[str, List] + storage_controller_count: Dict[str, int] + storage_controller_data: Dict[str, List] + storage_drive_count: Dict[str, int] + storage_drive_data: Dict[str, List] + memory_dimm_count: Dict[str, int] + memory_dimm_data: Dict[str, List] + + sensor_data: Dict[str, List] = redfish_helper.get_sensor_data() + processor_count, processor_data = redfish_helper.get_processor_data() + ( + storage_controller_count, + storage_controller_data, + ) = redfish_helper.get_storage_controller_data() + network_adapter_count: Dict[str, Any] = redfish_helper.get_network_adapter_data() + chassis_data: Dict[str, Dict] = redfish_helper.get_chassis_data() + storage_drive_count, storage_drive_data = redfish_helper.get_storage_drive_data() + memory_dimm_count, memory_dimm_data = redfish_helper.get_memory_dimm_data() + smart_storage_health_data: Dict[str, Any] = ( + redfish_helper.get_smart_storage_health_data() + ) + except ( # pylint: disable=W0718 ConnectionError, InvalidCredentialsError, RetriesExhaustedError, SessionCreationError, + Exception, ) as err: - logger.exception("Exception occurred while getting redfish object: %s", err) + logger.exception("Exception occurred while using redfish object: %s", err) payloads.append(Payload(name="redfish_call_success", value=0.0)) return payloads + finally: + # Guarding against `RedfishHelper` crashes, and `redfish_helper` remains `None` + if redfish_helper: + redfish_helper.logout() metrics: Dict[str, Any] = { "sensor_data": sensor_data, diff --git a/prometheus_hardware_exporter/collectors/redfish.py b/prometheus_hardware_exporter/collectors/redfish.py index 46fab2d..26da384 100644 --- a/prometheus_hardware_exporter/collectors/redfish.py +++ b/prometheus_hardware_exporter/collectors/redfish.py @@ -7,7 +7,6 @@ from cachetools.func import ttl_cache from redfish import redfish_client from redfish.rest.v1 import ( - HttpClient, InvalidCredentialsError, RestResponse, RetriesExhaustedError, @@ -76,11 +75,6 @@ def __init__(self, config: Config) -> None: self.password = config.redfish_password self.timeout = config.redfish_client_timeout self.max_retry = config.redfish_client_max_retry - self.redfish_obj: HttpClient - - def __enter__(self) -> Self: - """Login to redfish while entering context manager.""" - logger.debug("(service) Trying to login to redfish service ...") self.redfish_obj = redfish_client( base_url=self.host, username=self.username, @@ -88,18 +82,25 @@ def __enter__(self) -> Self: timeout=self.timeout, max_retry=self.max_retry, ) - self.redfish_obj.login(auth="session") + + def __enter__(self) -> Self: + """Login as a context manager.""" + self.login() return self def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: - """Logout from redfish while exiting context manager.""" + """Logout as a context manager.""" self.logout() + def login(self) -> None: + """Login to redfish.""" + logger.debug("(service) Trying to login to redfish service ...") + self.redfish_obj.login(auth="session") + def logout(self) -> None: """Logout from redfish.""" - if self.redfish_obj is not None: - self.redfish_obj.logout() - logger.debug("(service) Logged out from redfish service ...") + self.redfish_obj.logout() + logger.debug("(service) Logged out from redfish service ...") def get_sensor_data(self) -> Dict[str, List]: """Get sensor data. @@ -136,12 +137,12 @@ def _retrieve_redfish_sensor_data(self) -> List[Any]: sensors = redfish_utilities.get_sensors(self.redfish_obj) return sensors - def _verify_redfish_call(self, redfish_obj: HttpClient, uri: str) -> Optional[Dict[str, Any]]: + def _verify_redfish_call(self, uri: str) -> Optional[Dict[str, Any]]: """Return REST response for GET request API call on the URI. Returns None if URI isn't present. """ - resp: RestResponse = redfish_obj.get(uri) + resp: RestResponse = self.redfish_obj.get(uri) if resp.status == 200: return resp.dict logger.debug("Not able to query from URI: %s.", uri) @@ -343,7 +344,7 @@ def get_network_adapter_data(self) -> Dict[str, int]: # eg: /redfish/v1/Chassis/1/NetworkAdapters network_adapters_root_uri = network_adapters_root_uri_pattern.format(chassis_id) network_adapters: Optional[Dict[str, Any]] = self._verify_redfish_call( - self.redfish_obj, network_adapters_root_uri + network_adapters_root_uri ) if not network_adapters: logger.debug("No network adapters could be found on chassis id: %s", chassis_id) @@ -579,7 +580,7 @@ def get_smart_storage_health_data(self) -> Dict[str, Any]: for chassis_id in chassis_ids: smart_storage_uri = smart_storage_root_uri_pattern.format(chassis_id) smart_storage_data: Optional[Dict[str, Any]] = self._verify_redfish_call( - self.redfish_obj, smart_storage_uri + smart_storage_uri ) if not smart_storage_data: logger.debug("Smart Storage URI endpoint not found for chassis ID: %s", chassis_id) diff --git a/prometheus_hardware_exporter/config.py b/prometheus_hardware_exporter/config.py index 2db1f6f..c74fd03 100644 --- a/prometheus_hardware_exporter/config.py +++ b/prometheus_hardware_exporter/config.py @@ -12,7 +12,7 @@ DEFAULT_CONFIG = os.path.join(os.environ.get("SNAP_DATA", "./"), "config.yaml") DEFAULT_IPMI_SEL_INTERVAL = 86400 -DEFAULT_REDFISH_CLIENT_TIMEOUT = 3 +DEFAULT_REDFISH_CLIENT_TIMEOUT = 15 DEFAULT_REDFISH_CLIENT_MAX_RETRY = 1 DEFAULT_REDFISH_DISCOVER_CACHE_TTL = 86400 diff --git a/tests/unit/test_collector.py b/tests/unit/test_collector.py index 50daadf..4cfb1e4 100644 --- a/tests/unit/test_collector.py +++ b/tests/unit/test_collector.py @@ -635,12 +635,17 @@ def _discover(*args, **kwargs): mock_redfish_client.assert_not_called() @patch("prometheus_hardware_exporter.collector.logger") + @patch("prometheus_hardware_exporter.collector.RedfishHelper") @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch( "prometheus_hardware_exporter.collectors.redfish.RedfishHelper.get_cached_discover_method" ) def test_redfish_no_login( - self, mock_get_cached_discover_method, mock_redfish_client, mock_logger + self, + mock_get_cached_discover_method, + mock_redfish_client, + mock_redfish_helper, + mock_logger, ): """Test redfish collector when redfish login doesn't work.""" @@ -652,15 +657,18 @@ def _discover(*args, **kwargs): mock_get_cached_discover_method.side_effect = cached_discover redfish_collector = RedfishCollector(Mock()) - redfish_collector.redfish_helper = Mock() + + mock_helper = Mock() + mock_redfish_helper.return_value = mock_helper for err in [ + Exception(), ConnectionError(), InvalidCredentialsError(), SessionCreationError(), RetriesExhaustedError(), ]: - mock_redfish_client.side_effect = err + mock_helper.login.side_effect = err payloads = redfish_collector.collect() # Check whether these 2 payloads are present @@ -668,13 +676,14 @@ def _discover(*args, **kwargs): # redfish_call_success which is set to value 0 self.assertEqual(len(list(payloads)), 2) mock_logger.exception.assert_called_with( - "Exception occurred while getting redfish object: %s", err + "Exception occurred while using redfish object: %s", err ) - mock_redfish_client.assert_called() + mock_redfish_helper.assert_called() + mock_helper.login.assert_called() + mock_helper.logout.assert_called() @patch("prometheus_hardware_exporter.collector.logger") - @patch("prometheus_hardware_exporter.collectors.redfish.RedfishHelper.__exit__") - @patch("prometheus_hardware_exporter.collectors.redfish.RedfishHelper.__enter__") + @patch("prometheus_hardware_exporter.collector.RedfishHelper") @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch( "prometheus_hardware_exporter.collectors.redfish.RedfishHelper.get_cached_discover_method" @@ -683,8 +692,7 @@ def test_redfish_installed_and_okay( self, mock_get_cached_discover_method, mock_redfish_client, - mock_redfish_helper_enter, - mock_redfish_helper_exit, + mock_redfish_helper, mock_logger, ): """Test redfish collector and check if all metrics are present in payload.""" @@ -700,7 +708,7 @@ def _discover(*args, **kwargs): redfish_collector = RedfishCollector(Mock()) mock_helper = Mock() - mock_redfish_helper_enter.return_value = mock_helper + mock_redfish_helper.return_value = mock_helper mock_get_sensor_data = Mock() mock_get_processor_data = Mock() @@ -861,6 +869,8 @@ def _discover(*args, **kwargs): for payload in payloads: self.assertIn(payload.name, available_metrics) + mock_helper.login.assert_called() + mock_helper.logout.assert_called() mock_logger.warning.assert_called_with( "Failed to get %s via redfish", "network_adapter_count" ) diff --git a/tests/unit/test_redfish.py b/tests/unit/test_redfish.py index bbe085d..ccea2a1 100644 --- a/tests/unit/test_redfish.py +++ b/tests/unit/test_redfish.py @@ -11,88 +11,49 @@ ) from prometheus_hardware_exporter.collectors.redfish import RedfishHelper -from prometheus_hardware_exporter.config import Config class TestRedfishMetrics(unittest.TestCase): """Test metrics methods in RedfishHelper.""" - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") - def test_redfish_helper_context_manager_success(self, mock_redfish_client): - mock_redfish_login = Mock() - mock_redfish_logout = Mock() - mock_redfish_client.return_value.login = mock_redfish_login - mock_redfish_client.return_value.logout = mock_redfish_logout - mock_config = Config( - redfish_host="", - redfish_username="", - redfish_password="", - redfish_client_timeout=10, - redfish_client_max_retry=5, - redfish_discover_cache_ttl=5, - ) - with RedfishHelper(mock_config): - mock_redfish_client.assert_called_once_with( - base_url="", - username="", - password="", - timeout=10, - max_retry=5, - ) - mock_redfish_login.assert_called_once_with(auth="session") - mock_redfish_logout.assert_called_once() - - @parameterized.expand( - [InvalidCredentialsError, ConnectionError, SessionCreationError, RetriesExhaustedError] - ) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") - def test_redfish_helper_context_manager_fail(self, err, mock_redfish_client): - mock_config = Config( - redfish_host="", - redfish_username="", - redfish_password="", - redfish_client_timeout=10, - redfish_client_max_retry=5, - redfish_discover_cache_ttl=5, + def setUp(self): + redfish_client_patcher = patch( + "prometheus_hardware_exporter.collectors.redfish.redfish_client" ) - mock_redfish_client.side_effect = err - with self.assertRaises(err): - with RedfishHelper(mock_config): - pass + self.mock_redfish_client = redfish_client_patcher.start() + self.addCleanup(redfish_client_patcher.stop) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") - def test_verify_redfish_call_success(self, mock_redfish_client): + def test_verify_redfish_call_success(self): uri = "/some/test/uri" - mock_redfish_obj = Mock() mock_response = Mock() - - mock_redfish_obj.get.return_value = mock_response mock_response.status = 200 mock_response.dict = {"foo": 1, "bar": 2} + mock_redfish_obj = Mock() + mock_redfish_obj.get.return_value = mock_response + self.mock_redfish_client.return_value = mock_redfish_obj - with RedfishHelper(Mock()) as helper: - resp_dict = helper._verify_redfish_call(mock_redfish_obj, uri) + redfish_helper = RedfishHelper(Mock()) + with RedfishHelper(Mock()) as redfish_helper: + resp_dict = redfish_helper._verify_redfish_call(uri) self.assertEqual(resp_dict, {"foo": 1, "bar": 2}) mock_redfish_obj.get.assert_called_with(uri) @patch("prometheus_hardware_exporter.collectors.redfish.logger") - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") - def test_verify_redfish_call_fail(self, mock_redfish_client, mock_logger): + def test_verify_redfish_call_fail(self, mock_logger): uri = "/some/test/uri" - mock_redfish_obj = Mock() mock_response = Mock() - - mock_redfish_obj.get.return_value = mock_response mock_response.status = 401 + mock_redfish_obj = Mock() + mock_redfish_obj.get.return_value = mock_response + self.mock_redfish_client.return_value = mock_redfish_obj - with RedfishHelper(Mock()) as helper: - resp_dict = helper._verify_redfish_call(mock_redfish_obj, uri) + with RedfishHelper(Mock()) as redfish_helper: + resp_dict = redfish_helper._verify_redfish_call(uri) mock_redfish_obj.get.assert_called_with(uri) self.assertIsNone(resp_dict) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch.object( RedfishHelper, "_retrieve_redfish_sensor_data", @@ -156,9 +117,9 @@ def test_verify_redfish_call_fail(self, mock_redfish_client, mock_logger): } ], ) - def test_get_sensor_data_success(self, mock_sensor_data, mock_redfish_client): - with RedfishHelper(Mock()) as helper: - data = helper.get_sensor_data() + def test_get_sensor_data_success(self, mock_sensor_data): + with RedfishHelper(Mock()) as redfish_helper: + data = redfish_helper.get_sensor_data() self.assertEqual( data, { @@ -179,7 +140,6 @@ def test_get_sensor_data_success(self, mock_sensor_data, mock_redfish_client): }, ) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch.object( RedfishHelper, "_retrieve_redfish_sensor_data", @@ -248,9 +208,9 @@ def test_get_sensor_data_success(self, mock_sensor_data, mock_redfish_client): }, ], ) - def test_get_multiple_chassis_sensor_data_success(self, mock_sensor_data, mock_redfish_client): - with RedfishHelper(Mock()) as helper: - data = helper.get_sensor_data() + def test_get_multiple_chassis_sensor_data_success(self, mock_sensor_data): + with RedfishHelper(Mock()) as redfish_helper: + data = redfish_helper.get_sensor_data() self.assertEqual( data, { @@ -273,15 +233,13 @@ def test_get_multiple_chassis_sensor_data_success(self, mock_sensor_data, mock_r }, ) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch.object(RedfishHelper, "_retrieve_redfish_sensor_data", return_value=[]) - def test_get_sensor_data_fail(self, mock_sensor_data, mock_redfish_client): - with RedfishHelper(Mock()) as helper: - data = helper.get_sensor_data() + def test_get_sensor_data_fail(self, mock_sensor_data): + with RedfishHelper(Mock()) as redfish_helper: + data = redfish_helper.get_sensor_data() self.assertEqual(data, {}) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") - def test_map_sensor_data_to_chassis(self, mock_redfish_client): + def test_map_sensor_data_to_chassis(self): mock_data = [ { "ChassisName": 1, @@ -347,8 +305,8 @@ def test_map_sensor_data_to_chassis(self, mock_redfish_client): }, ] - with RedfishHelper(Mock()) as helper: - output = helper._map_sensor_data_to_chassis(mock_data) + with RedfishHelper(Mock()) as redfish_helper: + output = redfish_helper._map_sensor_data_to_chassis(mock_data) self.assertEqual( output, { @@ -372,25 +330,23 @@ def test_map_sensor_data_to_chassis(self, mock_redfish_client): ) @patch("prometheus_hardware_exporter.collectors.redfish.redfish_utilities.get_sensors") - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") - def test_retrieve_redfish_sensor_data_success(self, mock_redfish_client, mock_get_sensors): + def test_retrieve_redfish_sensor_data_success(self, mock_get_sensors): mock_get_sensors.return_value = ["return_data"] mock_redfish_obj = Mock() - mock_redfish_client.return_value = mock_redfish_obj - with RedfishHelper(Mock()) as helper: - data = helper._retrieve_redfish_sensor_data() + self.mock_redfish_client.return_value = mock_redfish_obj + with RedfishHelper(Mock()) as redfish_helper: + data = redfish_helper._retrieve_redfish_sensor_data() self.assertEqual(data, ["return_data"]) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch( "prometheus_hardware_exporter.collectors.redfish.redfish_utilities.systems.get_system_ids" ) - def test_get_processor_data_success(self, mock_get_system_ids, mock_redfish_client): + def test_get_processor_data_success(self, mock_get_system_ids): mock_redfish_obj = Mock() mock_system_ids = ["s1", "s2"] mock_get_system_ids.return_value = mock_system_ids - mock_redfish_client.return_value = mock_redfish_obj + self.mock_redfish_client.return_value = mock_redfish_obj def mock_get_response(uri): response = Mock() @@ -429,8 +385,8 @@ def mock_get_response(uri): mock_redfish_obj.get.side_effect = mock_get_response - with RedfishHelper(Mock()) as helper: - processor_count, processor_data = helper.get_processor_data() + with RedfishHelper(Mock()) as redfish_helper: + processor_count, processor_data = redfish_helper.get_processor_data() self.assertEqual(processor_count, {"s1": 2, "s2": 1}) self.assertEqual( @@ -469,7 +425,6 @@ def test__storage_root_uri(self): assert uri == expected_uri @parameterized.expand(["Storage", "Storages"]) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch( "prometheus_hardware_exporter.collectors.redfish.redfish_utilities.collections.get_collection_ids" # noqa: E501 ) @@ -477,14 +432,12 @@ def test__storage_root_uri(self): "prometheus_hardware_exporter.collectors.redfish.redfish_utilities.systems.get_system_ids" ) def test_get_storage_controller_data_success( - self, storage_name, mock_get_system_ids, mock_get_collection_ids, mock_redfish_client + self, storage_name, mock_get_system_ids, mock_get_collection_ids ): - mock_redfish_obj = Mock() mock_system_ids = ["s1"] mock_storage_ids = ["STOR1", "STOR2"] mock_get_system_ids.return_value = mock_system_ids - mock_redfish_client.return_value = mock_redfish_obj mock_get_collection_ids.return_value = mock_storage_ids def mock_get_response(uri): @@ -515,13 +468,15 @@ def mock_get_response(uri): } return response + mock_redfish_obj = Mock() mock_redfish_obj.get.side_effect = mock_get_response + self.mock_redfish_client.return_value = mock_redfish_obj - with RedfishHelper(Mock()) as helper: + with RedfishHelper(Mock()) as redfish_helper: ( storage_controller_count, storage_controller_data, - ) = helper.get_storage_controller_data() + ) = redfish_helper.get_storage_controller_data() self.assertEqual(storage_controller_count, {"s1": 2}) self.assertEqual( @@ -544,15 +499,12 @@ def mock_get_response(uri): }, ) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch( "prometheus_hardware_exporter.collectors.redfish.redfish_utilities.inventory.get_chassis_ids" # noqa: E501 ) - def test_get_network_adapter_data_success(self, mock_get_chassis_ids, mock_redfish_client): - mock_redfish_obj = Mock() + def test_get_network_adapter_data_success(self, mock_get_chassis_ids): mock_chassis_ids = ["c1", "c2"] mock_get_chassis_ids.return_value = mock_chassis_ids - mock_redfish_client.return_value = mock_redfish_obj def mock_get_response(uri): response = Mock() @@ -574,7 +526,9 @@ def mock_get_response(uri): return response + mock_redfish_obj = Mock() mock_redfish_obj.get.side_effect = mock_get_response + self.mock_redfish_client.return_value = mock_redfish_obj with RedfishHelper(Mock()) as helper: network_adapter_count = helper.get_network_adapter_data() @@ -582,39 +536,33 @@ def mock_get_response(uri): self.assertEqual(network_adapter_count, {"c1": 2, "c2": 1}) @patch("prometheus_hardware_exporter.collectors.redfish.logger") - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch( "prometheus_hardware_exporter.collectors.redfish.redfish_utilities.inventory.get_chassis_ids" # noqa: E501 ) - def test_get_network_adapter_data_fail( - self, mock_get_chassis_ids, mock_redfish_client, mock_logger - ): + def test_get_network_adapter_data_fail(self, mock_get_chassis_ids, mock_logger): mock_redfish_obj = Mock() mock_chassis_ids = ["c1"] mock_get_chassis_ids.return_value = mock_chassis_ids - mock_redfish_client.return_value = mock_redfish_obj + self.mock_redfish_client.return_value = mock_redfish_obj response = Mock() response.status = 401 mock_redfish_obj.get.return_value = response - with RedfishHelper(Mock()) as helper: - network_adapter_count = helper.get_network_adapter_data() + with RedfishHelper(Mock()) as redfish_helper: + network_adapter_count = redfish_helper.get_network_adapter_data() self.assertEqual(network_adapter_count, {}) mock_logger.debug.assert_any_call( "No network adapters could be found on chassis id: %s", "c1" ) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch( "prometheus_hardware_exporter.collectors.redfish.redfish_utilities.inventory.get_chassis_ids" # noqa: E501 ) - def test_get_chassis_data_success(self, mock_get_chassis_ids, mock_redfish_client): + def test_get_chassis_data_success(self, mock_get_chassis_ids): mock_chassis_ids = ["c1", "c2"] - mock_redfish_obj = Mock() mock_get_chassis_ids.return_value = mock_chassis_ids - mock_redfish_client.return_value = mock_redfish_obj def mock_get_response(uri): response = Mock() @@ -634,10 +582,12 @@ def mock_get_response(uri): } return response + mock_redfish_obj = Mock() mock_redfish_obj.get.side_effect = mock_get_response + self.mock_redfish_client.return_value = mock_redfish_obj - with RedfishHelper(Mock()) as helper: - chassis_data = helper.get_chassis_data() + with RedfishHelper(Mock()) as redfish_helper: + chassis_data = redfish_helper.get_chassis_data() self.assertEqual( chassis_data, @@ -660,7 +610,6 @@ def mock_get_response(uri): ) @parameterized.expand(["Storage", "Storages"]) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch( "prometheus_hardware_exporter.collectors.redfish.redfish_utilities.collections.get_collection_ids" # noqa: E501 ) @@ -668,14 +617,12 @@ def mock_get_response(uri): "prometheus_hardware_exporter.collectors.redfish.redfish_utilities.systems.get_system_ids" ) def test_get_storage_drive_data_success( - self, storage_name, mock_get_system_ids, mock_get_collection_ids, mock_redfish_client + self, storage_name, mock_get_system_ids, mock_get_collection_ids ): - mock_redfish_obj = Mock() mock_system_ids = ["s1"] mock_storage_ids = ["STOR1", "STOR2"] mock_get_system_ids.return_value = mock_system_ids - mock_redfish_client.return_value = mock_redfish_obj mock_get_collection_ids.return_value = mock_storage_ids def mock_get_response(uri): @@ -718,10 +665,12 @@ def mock_get_response(uri): } return response + mock_redfish_obj = Mock() mock_redfish_obj.get.side_effect = mock_get_response + self.mock_redfish_client.return_value = mock_redfish_obj - with RedfishHelper(Mock()) as helper: - storage_drive_count, storage_drive_data = helper.get_storage_drive_data() + with RedfishHelper(Mock()) as redfish_helper: + storage_drive_count, storage_drive_data = redfish_helper.get_storage_drive_data() self.assertEqual(storage_drive_count, {"s1": 3}) self.assertEqual( @@ -750,15 +699,12 @@ def mock_get_response(uri): }, ) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch( "prometheus_hardware_exporter.collectors.redfish.redfish_utilities.systems.get_system_ids" ) - def test_get_memory_dimm_data_success(self, mock_get_system_ids, mock_redfish_client): - mock_redfish_obj = Mock() + def test_get_memory_dimm_data_success(self, mock_get_system_ids): mock_system_ids = ["s1", "s2"] mock_get_system_ids.return_value = mock_system_ids - mock_redfish_client.return_value = mock_redfish_obj def mock_get_response(uri): response = Mock() @@ -786,10 +732,12 @@ def mock_get_response(uri): } return response + mock_redfish_obj = Mock() mock_redfish_obj.get.side_effect = mock_get_response + self.mock_redfish_client.return_value = mock_redfish_obj - with RedfishHelper(Mock()) as helper: - memory_dimm_count, memory_dimm_data = helper.get_memory_dimm_data() + with RedfishHelper(Mock()) as redfish_helper: + memory_dimm_count, memory_dimm_data = redfish_helper.get_memory_dimm_data() self.assertEqual(memory_dimm_count, {"s1": 1, "s2": 1}) self.assertEqual( @@ -812,15 +760,12 @@ def mock_get_response(uri): }, ) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch( "prometheus_hardware_exporter.collectors.redfish.redfish_utilities.inventory.get_chassis_ids" # noqa: E501 ) - def test_smart_storage_health_data_success(self, mock_get_chassis_ids, mock_redfish_client): + def test_smart_storage_health_data_success(self, mock_get_chassis_ids): mock_chassis_ids = ["c1"] - mock_redfish_obj = Mock() mock_get_chassis_ids.return_value = mock_chassis_ids - mock_redfish_client.return_value = mock_redfish_obj def mock_get_response(uri): response = Mock() @@ -831,7 +776,9 @@ def mock_get_response(uri): } return response + mock_redfish_obj = Mock() mock_redfish_obj.get.side_effect = mock_get_response + self.mock_redfish_client.return_value = mock_redfish_obj with RedfishHelper(Mock()) as helper: smart_storage_health_data = helper.get_smart_storage_health_data() @@ -846,24 +793,21 @@ def mock_get_response(uri): ) @patch("prometheus_hardware_exporter.collectors.redfish.logger") - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch( "prometheus_hardware_exporter.collectors.redfish.redfish_utilities.inventory.get_chassis_ids" # noqa: E501 ) - def test_smart_storage_health_data_fail( - self, mock_get_chassis_ids, mock_redfish_client, mock_logger - ): + def test_smart_storage_health_data_fail(self, mock_get_chassis_ids, mock_logger): mock_chassis_ids = ["c1"] - mock_redfish_obj = Mock() mock_get_chassis_ids.return_value = mock_chassis_ids - mock_redfish_client.return_value = mock_redfish_obj response = Mock() response.status = 401 + mock_redfish_obj = Mock() mock_redfish_obj.get.return_value = response + self.mock_redfish_client.return_value = mock_redfish_obj - with RedfishHelper(Mock()) as helper: - smart_storage_health_data = helper.get_smart_storage_health_data() + with RedfishHelper(Mock()) as redfish_helper: + smart_storage_health_data = redfish_helper.get_smart_storage_health_data() self.assertEqual( smart_storage_health_data, @@ -874,23 +818,22 @@ def test_smart_storage_health_data_fail( "Smart Storage URI endpoint not found for chassis ID: %s", "c1" ) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch( "prometheus_hardware_exporter.collectors.redfish.redfish_utilities.systems.get_system_ids" ) - def test_get_system_id_fail(self, mock_get_system_ids, mock_redfish_client): - mock_redfish_obj = Mock() + def test_get_system_id_fail(self, mock_get_system_ids): mock_get_system_ids.side_effect = redfish_utilities.systems.RedfishSystemNotFoundError - mock_redfish_client.return_value = mock_redfish_obj - with RedfishHelper(Mock()) as helper: - processor_count, processor_data = helper.get_processor_data() + mock_redfish_obj = Mock() + self.mock_redfish_client.return_value = mock_redfish_obj + with RedfishHelper(Mock()) as redfish_helper: + processor_count, processor_data = redfish_helper.get_processor_data() ( storage_controller_count, storage_controller_data, - ) = helper.get_storage_controller_data() - storage_drive_count, storage_drive_data = helper.get_storage_drive_data() - memory_dimm_count, memory_dimm_data = helper.get_memory_dimm_data() + ) = redfish_helper.get_storage_controller_data() + storage_drive_count, storage_drive_data = redfish_helper.get_storage_drive_data() + memory_dimm_count, memory_dimm_data = redfish_helper.get_memory_dimm_data() self.assertEqual(memory_dimm_count, {}) self.assertEqual(memory_dimm_data, {}) @@ -901,19 +844,18 @@ def test_get_system_id_fail(self, mock_get_system_ids, mock_redfish_client): self.assertEqual(storage_drive_count, {}) self.assertEqual(storage_drive_data, {}) - @patch("prometheus_hardware_exporter.collectors.redfish.redfish_client") @patch( "prometheus_hardware_exporter.collectors.redfish.redfish_utilities.inventory.get_chassis_ids" # noqa: E501 ) - def test_get_chassis_id_fail(self, mock_get_chassis_ids, mock_redfish_client): - mock_redfish_obj = Mock() + def test_get_chassis_id_fail(self, mock_get_chassis_ids): mock_get_chassis_ids.side_effect = redfish_utilities.inventory.RedfishChassisNotFoundError - mock_redfish_client.return_value = mock_redfish_obj - with RedfishHelper(Mock()) as helper: - network_adapter_count = helper.get_network_adapter_data() - chassis_data = helper.get_chassis_data() - smart_storage_health_data = helper.get_smart_storage_health_data() + mock_redfish_obj = Mock() + self.mock_redfish_client.return_value = mock_redfish_obj + with RedfishHelper(Mock()) as redfish_helper: + network_adapter_count = redfish_helper.get_network_adapter_data() + chassis_data = redfish_helper.get_chassis_data() + smart_storage_health_data = redfish_helper.get_smart_storage_health_data() self.assertEqual(network_adapter_count, {}) self.assertEqual(chassis_data, {})