def test_openstack(config, test_request): try: api = ApiAccess(config) ConnectionTest.report_success(test_request, ConnectionTestType.OPENSTACK.value) if api: pass except ValueError: pass
def get(self, regions_folder_id): token = self.v2_auth_pwd(self.admin_project) if not token: return [] # the returned authentication response contains the list of end points # and regions project_id = regions_folder_id.replace('-regions', '') response = ApiAccess.get_auth_response(project_id) service_catalog = response.get('access', {}).get('serviceCatalog') if not service_catalog: return [] env = self.get_env() ret = [] NULL_REGION = "No-Region" for service in service_catalog: for e in service["endpoints"]: if "region" in e: region_name = e.pop("region") region_name = region_name if region_name else NULL_REGION else: region_name = NULL_REGION if region_name in self.regions.keys(): region = self.regions[region_name] else: region = { "id": region_name, "name": region_name, "endpoints": {} } ApiAccess.regions[region_name] = region region["parent_type"] = "regions_folder" region["parent_id"] = env + "-regions" e["service_type"] = service["type"] region["endpoints"][service["name"]] = e ret.extend(list(ApiAccess.regions.values())) return ret
def setUp(self): super().setUp() self.configure_environment() self.api_access = ApiAccess() self.set_regions_for_fetcher(self.api_access)
class TestApiAccess(TestFetch): def setUp(self): super().setUp() self.configure_environment() self.api_access = ApiAccess() self.set_regions_for_fetcher(self.api_access) def test_parse_time_without_dot_in_time(self): time = self.api_access.parse_time(TIME_WITHOUT_DOT) self.assertNotEqual(time, None, "Can't parse the time without dot") def test_parse_time_with_dot_in_time(self): time = self.api_access.parse_time(TIME_WITH_DOT) self.assertNotEqual(time, None, "Can't parse the time with dot") def test_parse_illegal_time(self): time = self.api_access.parse_time(ILLEGAL_TIME) self.assertEqual(time, None, "Can't get None when the time format is wrong") def test_get_existing_token(self): self.api_access.tokens = VALID_TOKENS token = self.api_access.get_existing_token(PROJECT) self.assertNotEqual(token, VALID_TOKENS[PROJECT], "Can't get existing token") def test_get_nonexistent_token(self): self.api_access.tokens = EMPTY_TOKENS token = self.api_access.get_existing_token(TEST_PROJECT) self.assertEqual( token, None, "Can't get None when the token doesn't " + "exist in tokens") def test_v2_auth(self): self.api_access.get_existing_token = MagicMock(return_value=None) self.response.json = Mock(return_value=CORRECT_AUTH_CONTENT) # mock authentication info from OpenStack Api token_details = self.api_access.v2_auth(TEST_PROJECT, TEST_HEADER, TEST_BODY) self.assertNotEqual(token_details, None, "Can't get the token details") def test_v2_auth_with_error_content(self): self.api_access.get_existing_token = MagicMock(return_value=None) self.response.json = Mock(return_value=ERROR_AUTH_CONTENT) # authentication content from OpenStack Api will be incorrect token_details = self.api_access.v2_auth(TEST_PROJECT, TEST_HEADER, TEST_BODY) self.assertIs(token_details, None, "Can't get None when the content is wrong") def test_v2_auth_with_error_token(self): self.response.status_code = requests.codes.bad_request self.response.json = Mock(return_value=ERROR_TOKEN_CONTENT) # authentication info from OpenStack Api will not contain token info token_details = self.api_access.v2_auth(TEST_PROJECT, TEST_HEADER, TEST_BODY) self.assertIs( token_details, None, "Can't get None when the content " + "doesn't contain any token info") def test_v2_auth_with_error_expiry_time(self): self.response.json = Mock(return_value=CORRECT_AUTH_CONTENT) # store original parse_time method original_method = self.api_access.parse_time # the time will not be parsed self.api_access.parse_time = MagicMock(return_value=None) token_details = self.api_access.v2_auth(TEST_PROJECT, TEST_HEADER, TEST_BODY) # reset original parse_time method self.api_access.parse_time = original_method self.assertIs( token_details, None, "Can't get None when the time in token " + "can't be parsed") def test_v2_auth_pwd(self): self.response.json = Mock(return_value=CORRECT_AUTH_CONTENT) # mock the authentication info from OpenStack Api token = self.api_access.v2_auth_pwd(PROJECT) self.assertNotEqual(token, None, "Can't get token") def test_get_url(self): self.response.json = Mock(return_value=GET_CONTENT) result = self.api_access.get_url(TEST_URL, TEST_HEADER) # check whether it returns content message when the response is correct self.assertNotEqual( result, None, "Can't get content when the " "response is correct") def test_get_url_with_error_response(self): self.response.status_code = requests.codes.bad_request self.response.json = Mock(return_value=None) self.response.text = "Bad request" # the response will be wrong result = self.api_access.get_url(TEST_URL, TEST_HEADER) self.assertEqual( result, None, "Result returned" + "when the response status is not 200") def test_get_region_url(self): region_url = self.api_access.get_region_url(REGION_NAME, SERVICE_NAME) self.assertNotEqual(region_url, None, "Can't get region url") def test_get_region_url_with_wrong_region_name(self): # error region name doesn't exist in the regions info region_url = self.api_access.get_region_url(ERROR_REGION_NAME, "") self.assertIs(region_url, None, "Can't get None with the region " + "name is wrong") def test_get_region_url_without_service_endpoint(self): # error service doesn't exist in region service endpoints region_url = self.api_access.get_region_url(REGION_NAME, ERROR_SERVICE_NAME) self.assertIs(region_url, None, "Can't get None with wrong service name") def test_region_url_nover(self): # mock return value of get_region_url, which has something starting from v2 self.api_access.get_region_url = MagicMock(return_value=REGION_URL) region_url = self.api_access.get_region_url_nover( REGION_NAME, SERVICE_NAME) # get_region_nover will remove everything from v2 self.assertNotIn("v2", region_url, "Can't get region url without v2 info") def test_get_service_region_endpoints(self): region = REGIONS[REGION_NAME] result = self.api_access.get_service_region_endpoints( region, SERVICE_NAME) self.assertNotEqual(result, None, "Can't get service endpoint") def test_get_service_region_endpoints_with_nonexistent_service(self): region = REGIONS[REGION_NAME] result = self.api_access.get_service_region_endpoints( region, ERROR_SERVICE_NAME) self.assertIs( result, None, "Can't get None when the service name " + "doesn't exist in region's services")