class VersionsTest(utils.TestCase): def setUp(self): super(VersionsTest, self).setUp() self.cs = fakes.FakeClient(api_versions.APIVersion("2.0")) self.service_type = versions.Version def test_list_services(self): vl = self.cs.versions.list() self.assert_request_id(vl, fakes.FAKE_REQUEST_ID_LIST) self.cs.assert_called('GET', 'http://nova-api:8774') def test_get_current(self): self.cs.callback = [] v = self.cs.versions.get_current() self.assert_request_id(v, fakes.FAKE_REQUEST_ID_LIST) self.cs.assert_called('GET', 'http://nova-api:8774/v2.1/') @mock.patch.object(versions.VersionManager, '_get', side_effect=exc.Unauthorized("401 RAX")) def test_get_current_with_rax_workaround(self, get): self.cs.callback = [] self.assertIsNone(self.cs.versions.get_current()) def test_get_endpoint_without_project_id(self): # create a fake client such that get_endpoint() # doesn't return uuid in url endpoint_type = 'v2.1' expected_endpoint = 'http://nova-api:8774/v2.1/' cs_2_1 = fakes.FakeClient(api_versions.APIVersion("2.0"), endpoint_type=endpoint_type) result = cs_2_1.versions.get_current() self.assert_request_id(result, fakes.FAKE_REQUEST_ID_LIST) self.assertEqual(result.manager.api.client.endpoint_type, endpoint_type, "Check endpoint_type was set") # check that the full request works as expected cs_2_1.assert_called('GET', expected_endpoint) def test_v2_get_endpoint_without_project_id(self): # create a fake client such that get_endpoint() # doesn't return uuid in url endpoint_type = 'v2' expected_endpoint = 'http://nova-api:8774/v2/' cs_2 = fakes.FakeClient(api_versions.APIVersion("2.0"), endpoint_type=endpoint_type) result = cs_2.versions.get_current() self.assert_request_id(result, fakes.FAKE_REQUEST_ID_LIST) self.assertEqual(result.manager.api.client.endpoint_type, endpoint_type, "Check v2 endpoint_type was set") # check that the full request works as expected cs_2.assert_called('GET', expected_endpoint) def test_list_versions(self): fapi = mock.Mock() version_mgr = versions.VersionManager(fapi) version_mgr._list = mock.Mock() data = [ ("https://example.com:777/v2", "https://example.com:777"), ("https://example.com/v2", "https://example.com"), ("http://example.com/compute/v2", "http://example.com/compute"), ("https://example.com/v2/prrrooojeect-uuid", "https://example.com"), ("https://example.com:777/v2.1", "https://example.com:777"), ("https://example.com/v2.1", "https://example.com"), ("http://example.com/compute/v2.1", "http://example.com/compute"), ("https://example.com/v2.1/prrrooojeect-uuid", "https://example.com"), ("http://example.com/compute", "http://example.com/compute"), ("http://compute.example.com", "http://compute.example.com"), ] for endpoint, expected in data: version_mgr._list.reset_mock() fapi.client.get_endpoint.return_value = endpoint version_mgr.list() version_mgr._list.assert_called_once_with(expected, "versions")
class VersionsTest(utils.TestCase): def setUp(self): super(VersionsTest, self).setUp() self.cs = fakes.FakeClient() self.service_type = versions.Version @mock.patch.object(versions.VersionManager, '_is_session_client', return_value=False) def test_list_services_with_http_client(self, mock_is_session_client): self.cs.versions.list() self.cs.assert_called('GET', None) @mock.patch.object(versions.VersionManager, '_is_session_client', return_value=True) def test_list_services_with_session_client(self, mock_is_session_client): self.cs.versions.list() self.cs.assert_called('GET', 'http://*****:*****@mock.patch.object(versions.VersionManager, '_is_session_client', return_value=False) @mock.patch.object(versions.VersionManager, 'list') def test_get_current_with_http_client(self, mock_list, mock_is_session_client): current_version = versions.Version( None, {"links": [{"href": "http://*****:*****@mock.patch.object(versions.VersionManager, '_is_session_client', return_value=True) def test_get_current_with_session_client(self, mock_is_session_client): self.cs.callback = [] self.cs.versions.get_current() self.cs.assert_called('GET', 'http://*****:*****@mock.patch.object(versions.VersionManager, '_is_session_client', return_value=True) @mock.patch.object(versions.VersionManager, '_get', side_effect=exc.Unauthorized("401 RAX")) def test_get_current_with_rax_workaround(self, session, get): self.cs.callback = [] self.assertIsNone(self.cs.versions.get_current()) @mock.patch.object(versions.VersionManager, '_is_session_client', return_value=False) @mock.patch.object(versions.VersionManager, '_list', side_effect=exc.Unauthorized("401 RAX")) def test_get_current_with_rax_auth_plugin_workaround(self, session, _list): self.cs.callback = [] self.assertIsNone(self.cs.versions.get_current()) @mock.patch.object(versions.VersionManager, '_is_session_client', return_value=True) def test_get_endpoint_without_project_id(self, mock_is_session_client): # create a fake client such that get_endpoint() # doesn't return uuid in url endpoint_type = 'v2.1' expected_endpoint = 'http://*****:*****@mock.patch.object(versions.VersionManager, '_is_session_client', return_value=True) def test_v2_get_endpoint_without_project_id(self, mock_is_session_client): # create a fake client such that get_endpoint() # doesn't return uuid in url endpoint_type = 'v2' expected_endpoint = 'http://nova-api:8774/v2/' cs_2 = fakes.FakeClient(endpoint_type=endpoint_type) result = cs_2.versions.get_current() self.assertEqual(result.manager.api.client.endpoint_type, endpoint_type, "Check v2 endpoint_type was set") self.assertEqual(result.manager.api.client.management_url, expected_endpoint, "Check v2 endpoint without uuid") # check that the full request works as expected cs_2.assert_called('GET', 'http://nova-api:8774/v2/')
def authenticate(self): magic_tuple = urlutils.urlsplit(self.auth_url) scheme, netloc, path, query, frag = magic_tuple port = magic_tuple.port if port is None: port = 80 path_parts = path.split('/') for part in path_parts: if len(part) > 0 and part[0] == 'v': self.version = part break # TODO(sandy): Assume admin endpoint is 35357 for now. # Ideally this is going to have to be provided by the service catalog. new_netloc = netloc.replace(':%d' % port, ':%d' % (35357, )) admin_url = urlutils.urlunsplit( (scheme, new_netloc, path, query, frag)) auth_url = self.auth_url if self.version == "v2.0": # FIXME(chris): This should be better. while auth_url: if not self.auth_system or self.auth_system == 'keystone': auth_url = self._v2_auth(auth_url) else: auth_url = self._plugin_auth(auth_url) # Are we acting on behalf of another user via an # existing token? If so, our actual endpoints may # be different than that of the admin token. if self.proxy_token: if self.bypass_url: self.set_management_url(self.bypass_url) else: self._fetch_endpoints_from_auth(admin_url) # Since keystone no longer returns the user token # with the endpoints any more, we need to replace # our service account token with the user token. self.auth_token = self.proxy_token else: try: while auth_url: auth_url = self._v1_auth(auth_url) # In some configurations nova makes redirection to # v2.0 keystone endpoint. Also, new location does not contain # real endpoint, only hostname and port. except exceptions.AuthorizationFailure: if auth_url.find('v2.0') < 0: auth_url = auth_url + '/v2.0' self._v2_auth(auth_url) if self.bypass_url: self.set_management_url(self.bypass_url) elif not self.management_url: raise exceptions.Unauthorized('Nova Client') # Store the token/mgmt url in the keyring for later requests. if self.keyring_saver and self.os_cache and not self.keyring_saved: self.keyring_saver.save(self.auth_token, self.management_url, self.tenant_id) # Don't save it again self.keyring_saved = True
def authenticate(self): if not self.auth_url: msg = _("Authentication requires 'auth_url', which should be " "specified in '%s'") % self.__class__.__name__ raise exceptions.AuthorizationFailure(msg) magic_tuple = netutils.urlsplit(self.auth_url) scheme, netloc, path, query, frag = magic_tuple port = magic_tuple.port if port is None: port = 80 path_parts = path.split('/') for part in path_parts: if len(part) > 0 and part[0] == 'v': self.version = part break if self.auth_token and self.management_url: self._save_keys() return # TODO(sandy): Assume admin endpoint is 35357 for now. # Ideally this is going to have to be provided by the service catalog. new_netloc = netloc.replace(':%d' % port, ':%d' % (35357,)) admin_url = parse.urlunsplit( (scheme, new_netloc, path, query, frag)) auth_url = self.auth_url if self.version == "v2.0": # FIXME(chris): This should be better. while auth_url: auth_url = self._v2_auth(auth_url) # Are we acting on behalf of another user via an # existing token? If so, our actual endpoints may # be different than that of the admin token. if self.proxy_token: if self.bypass_url: self.set_management_url(self.bypass_url) else: self._fetch_endpoints_from_auth(admin_url) # Since keystone no longer returns the user token # with the endpoints any more, we need to replace # our service account token with the user token. self.auth_token = self.proxy_token else: try: while auth_url: auth_url = self._v1_auth(auth_url) # In some configurations nova makes redirection to # v2.0 keystone endpoint. Also, new location does not contain # real endpoint, only hostname and port. except exceptions.AuthorizationFailure: if auth_url.find('v2.0') < 0: auth_url = auth_url + '/v2.0' self._v2_auth(auth_url) if self.bypass_url: self.set_management_url(self.bypass_url) elif not self.management_url: raise exceptions.Unauthorized('Nova Client') self._save_keys()