def test_garbage_collector_for_expired_sessions_when_raises_exception( self, time_mock, logging_mock, uuid_mock): time_mock.sleep.side_effect = [None, InterruptedError] uuid_mock.uuid4.return_value = 'session_id_1' client = mock.Mock() client.connection.do_http.side_effect = Exception('Some error message') client_session._set_new_client_by_token('abc', {'10.0.0.11': client}) expected_map_client = OrderedDict() expected_map_client['abc'] = { 'client_ov_by_ip': { '10.0.0.11': client }, 'session_id': 'session_id_1' } try: client_session._gc_for_expired_sessions() except InterruptedError: pass logging_mock.exception.assert_called_with( 'Unexpected error: Some error message') self.assertEqual(expected_map_client, client_session._get_map_clients())
def test_garbage_collector_for_expired_sessions(self, threading_mock, time_mock, uuid_mock): thread_obj_mock = mock.Mock() threading_mock.Thread.return_value = thread_obj_mock # using time.sleep to exit from loop raising an InterruptedError time_mock.sleep.side_effect = [None, InterruptedError] # Client 1 will represents the success request, must be in the cache # Client 2 will represents the fail request, must be removed from # the cache client_1 = mock.Mock() client_2 = mock.Mock() resp_1 = mock.Mock(status=200) resp_2 = mock.Mock(status=404) uuid_mock.uuid4.side_effect = ['session_id_1', 'session_id_2'] client_1.connection.get_session_id.return_value = 'ov_session_abc' client_2.connection.get_session_id.return_value = 'ov_session_def' client_1.connection.do_http.return_value = (resp_1, None) client_2.connection.do_http.return_value = (resp_2, None) client_session._set_new_client_by_token('abc', {'10.0.0.11': client_1}) client_session._set_new_client_by_token('def', {'10.0.0.12': client_2}) expected_map_client = OrderedDict() expected_map_client['abc'] = { 'client_ov_by_ip': {'10.0.0.11': client_1}, 'session_id': 'session_id_1' } client_session.init_gc_for_expired_sessions() try: client_session._gc_for_expired_sessions() except InterruptedError: pass self.assertEqual(expected_map_client, client_session._get_map_clients()) threading_mock.Thread.assert_called_with( target=client_session._gc_for_expired_sessions, daemon=True) thread_obj_mock.start.assert_called_with() time_mock.sleep.assert_called_with(client_session.GC_FREQUENCY_IN_SEC) client_1.connection.do_http.assert_called_with('GET', '/rest/sessions/', '', {'Session-Id': 'ov_session_abc'}) client_2.connection.do_http.assert_called_with('GET', '/rest/sessions/', '', {'Session-Id': 'ov_session_def'})
def test_search_resource_not_found(self, _, get_oneview_client, oneview_client_mockup, request, get_config): self.config_obj.set('oneview_config', 'ip', '10.0.0.1, 10.0.0.2, 10.0.0.3') self.config_obj.set('redfish', 'authentication_mode', 'session') get_config.return_value = self.config_obj client_session._set_new_client_by_token(self.redfish_token, self.ov_tokens) # Mocking redfish token on request property request.headers.get.return_value = self.redfish_token oneview_client_mockup.enclosures.get.side_effect = [ self.not_found_error, # 10.0.0.1 self.not_found_error, # 10.0.0.2 self.not_found_error, # 10.0.0.3 ] get_oneview_client.return_value = oneview_client_mockup handler_multiple_ov = \ handler_multiple_oneview.MultipleOneViewResource() with self.assertRaises(HPOneViewException): handler_multiple_ov.enclosures.get('UUID_1') get_oneview_client.assert_has_calls( [call("10.0.0.1"), call("10.0.0.2"), call("10.0.0.3")])
def test_searching_again_in_other_ov_when_resource_cached_is_not_found( self, _, get_oneview_client, oneview_client_mockup, request, get_config): self.config_obj.set('oneview_config', 'ip', '10.0.0.1, 10.0.0.2, 10.0.0.3') self.config_obj.set('redfish', 'authentication_mode', 'session') get_config.return_value = self.config_obj client_session._set_new_client_by_token(self.redfish_token, self.ov_tokens) # Mocking redfish token on request property request.headers.get.return_value = self.redfish_token with open( 'oneview_redfish_toolkit/mockups/oneview/Enclosure.json') as f: enclosure = json.load(f) oneview_client_mockup.enclosures.get.side_effect = [ self.not_found_error, # 10.0.0.1 enclosure, # 10.0.0.2 found the resource here self.not_found_error, # 10.0.0.2 self.not_found_error, # 10.0.0.1 enclosure, # 10.0.0.3 found the resource here (2th time) self.not_found_error, # 10.0.0.3 self.not_found_error, # 10.0.0.1 self.not_found_error # 10.0.0.2 ] get_oneview_client.return_value = oneview_client_mockup handler_multiple_ov = \ handler_multiple_oneview.MultipleOneViewResource() handler_multiple_ov.enclosures.get('UUID_1') mapped_ov_ip = multiple_oneview.get_ov_ip_by_resource('UUID_1') # Check OneView IP matchs the mapped one self.assertEqual(mapped_ov_ip, '10.0.0.2') get_oneview_client.assert_has_calls( [call("10.0.0.1"), call("10.0.0.2")]) handler_multiple_ov.enclosures.get('UUID_1') mapped_ov_ip = multiple_oneview.get_ov_ip_by_resource('UUID_1') self.assertEqual(mapped_ov_ip, '10.0.0.3') get_oneview_client.assert_has_calls( [call("10.0.0.2"), call("10.0.0.1"), call("10.0.0.3")]) with self.assertRaises(HPOneViewException): handler_multiple_ov.enclosures.get('UUID_1') get_oneview_client.assert_has_calls( [call("10.0.0.3"), call("10.0.0.1"), call("10.0.0.2")])
def test_get_session_when_is_not_cached(self, _, uuid_mock): """Tests get a specific Session when session id is not present""" uuid_mock.uuid4.return_value = '709cf49b-f367-417e-af70-ede782e0fa3c' client_session.init_map_clients() client_session._set_new_client_by_token('abc', '10.0.0.1') response = self.client.get( "/redfish/v1/SessionService/Sessions/" "e2807c0b-87d6-4304-a773-6ec33521fb1c", content_type='application/json') self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code) self.assertEqual("application/json", response.mimetype)
def test_garbage_collector_when_ov_request_status_is_not_200_neither_404( self, time_mock, logging_mock, uuid_mock): time_mock.sleep.side_effect = [None, None, None, None, InterruptedError] client = mock.Mock() client.connection.do_http.side_effect = [ (mock.Mock(status=400), 'request body 1'), (mock.Mock(status=401), 'request body 2'), (mock.Mock(status=403), 'request body 3'), (mock.Mock(status=500), 'request body 4') ] uuid_mock.uuid4.return_value = 'session_id_1' client_session._set_new_client_by_token('abc', {'10.0.0.11': client}) expected_map_client = OrderedDict() expected_map_client['abc'] = { 'client_ov_by_ip': {'10.0.0.11': client}, 'session_id': 'session_id_1' } try: client_session._gc_for_expired_sessions() except InterruptedError: pass logging_mock.error.assert_has_calls([ call( 'Unexpected response with status 400 of Oneview sessions ' 'endpoint: request body 1' ), call( 'Unexpected response with status 401 of Oneview sessions ' 'endpoint: request body 2' ), call( 'Unexpected response with status 403 of Oneview sessions ' 'endpoint: request body 3' ), call( 'Unexpected response with status 500 of Oneview sessions ' 'endpoint: request body 4' ) ]) self.assertEqual(expected_map_client, client_session._get_map_clients())
def test_search_in_all_ov_found_on_second(self, req_context, get_oneview_client, oneview_client_mockup, request, get_config): # Mocking configuration read from config file self.config_obj.set('oneview_config', 'ip', '10.0.0.1, 10.0.0.2') self.config_obj.set('redfish', 'authentication_mode', 'session') # Mocking globals['config'] of config file get_config.return_value = self.config_obj # Mocking redfish->Oneview tokens client_session._set_new_client_by_token(self.redfish_token, self.ov_tokens) # Mocking redfish token on request property request.headers.get.return_value = self.redfish_token # Mocking OneView client call returning resource for the second # one, ip: 10.0.0.2 oneview_client_mockup.server_profiles.get.side_effect = [ self.not_found_server_profile, self.server_profile, ] # Mocking connection.get_oneview_client() to return mocked # OneView Client get_oneview_client.return_value = oneview_client_mockup # Create new handler for multiple OneView support handler_multiple_ov = \ handler_multiple_oneview.MultipleOneViewResource() # Query resource handler_multiple_ov.server_profiles.get(self.sp_uuid) # Check if resource was queried on two of three OneViews oneview_client_mockup.server_profiles.get.assert_has_calls( [call(self.sp_uuid), call(self.sp_uuid)]) # Check if resource was queried on first and the second OneViews get_oneview_client.assert_has_calls( [call("10.0.0.1"), call("10.0.0.2")])
def test_get_session(self, _, uuid_mock): """Tests get a specific Session""" with open('oneview_redfish_toolkit/mockups/redfish/Session.json') as f: expected_session = json.load(f) uuid_mock.uuid4.side_effect = self.session_ids client_session.init_map_clients() client_session._set_new_client_by_token('abc', '10.0.0.1') client_session._set_new_client_by_token('def', '10.0.0.2') response = self.client.get("/redfish/v1/SessionService/Sessions/" + expected_session["Id"], content_type='application/json') result = json.loads(response.data.decode("utf-8")) self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual("application/json", response.mimetype) self.assertEqualMockup(expected_session, result)
def _build_common_sessions(self, uuid_mock): uuid_mock.uuid4.side_effect = self.session_ids client_session.init_map_clients() client_session._set_new_client_by_token('abc', '10.0.0.1') client_session._set_new_client_by_token('def', '10.0.0.2') client_session._set_new_client_by_token('ghi', '10.0.0.3')
def test_search_mapped_after_search_in_all(self, req_context, get_oneview_client, oneview_client_mockup, request, get_config): # Mocking configuration read from config file self.config_obj.set('oneview_config', 'ip', '10.0.0.1, 10.0.0.2, 10.0.0.3') self.config_obj.set('redfish', 'authentication_mode', 'session') # Mocking globals['config'] of config file get_config.return_value = self.config_obj # Mocking redfish->Oneview tokens client_session._set_new_client_by_token(self.redfish_token, self.ov_tokens) # Mocking redfish token on request property request.headers.get.return_value = self.redfish_token # Mocking OneView client call returning resource just for the last # one, ip: 10.0.0.3 oneview_client_mockup.server_profiles.get.side_effect = [ self.not_found_server_profile, self.not_found_server_profile, self.server_profile, ] # Mocking connection.get_oneview_client() to return mocked # OneView Client get_oneview_client.return_value = oneview_client_mockup # Create new handler for multiple OneView support handler_multiple_ov = \ handler_multiple_oneview.MultipleOneViewResource() # Query resource handler_multiple_ov.server_profiles.get(self.sp_uuid) # Check if resource was queried for all OneViews before found on # last one oneview_client_mockup.server_profiles.get.assert_has_calls( [call(self.sp_uuid), call(self.sp_uuid), call(self.sp_uuid)]) # Check if resource was queried on each one for all OneViews get_oneview_client.assert_has_calls( [call("10.0.0.1"), call("10.0.0.2"), call("10.0.0.3")]) # Mocking OneView client call returning resource oneview_client_mockup.server_profiles.get.side_effect = [ self.server_profile, ] # Query resource again, now already mapped on cache handler_multiple_ov.server_profiles.get(self.sp_uuid) # Check if resource was queried on just one OneView, the mapped one # for the resource oneview_client_mockup.server_profiles.get.assert_has_calls( [call(self.sp_uuid)]) # Check if resource was queried on just one OneViews that was mapped # for the resource get_oneview_client.assert_has_calls([call("10.0.0.3")]) # Get the OneView IP mapped for the resource mapped_ov_ip = multiple_oneview.get_ov_ip_by_resource(self.sp_uuid) # Check OneView IP matchs the mapped one self.assertEqual(mapped_ov_ip, "10.0.0.3")