def test__parse_version(self): sot = session.Session(None) self.assertEqual(sot._parse_version("2"), (2, -1)) self.assertEqual(sot._parse_version("v2"), (2, -1)) self.assertEqual(sot._parse_version("v2.1"), (2, 1)) self.assertRaises(ValueError, sot._parse_version, "lol")
def test_init_with_single_api_request(self): prof = profile.Profile() prof.set_api_version('storage', '1.2') sot = session.Session(prof) # The assertion acutally tests the property assigned in parent class self.assertEqual({'ecl-api-version': 'storage 1.2'}, sot.additional_headers)
def test_get_endpoint_cached(self): sot = session.Session(None) service_type = "compute" interface = "public" endpoint = "the world wide web" sot.endpoint_cache[(service_type, interface)] = endpoint rv = sot.get_endpoint(service_type=service_type, interface=interface) self.assertEqual(rv, endpoint)
def test__get_version_match_fragment(self): root = "http://cloud.net" match = "/v2/" versions = [{"id": "v2.0", "links": [{"href": match, "rel": "self"}]}] sot = session.Session(None) rv = sot._get_version_match(versions, session.Version(2, 0), "service", root, False) self.assertEqual(rv, root + match)
def test__get_endpoint_versions_exception(self): sot = session.Session(None) fake_response = mock.Mock() fake_response.json = mock.Mock(return_value={}) sot.get = mock.Mock(return_value=fake_response) self.assertRaises(exceptions.EndpointNotFound, sot._get_endpoint_versions, "service", "endpoint")
def test__get_version_match_project_id(self): match = "http://devstack/v2/" project_id = "asdf123" versions = [{"id": "v2.0", "links": [{"href": match, "rel": "self"}]}] sot = session.Session(None) sot.get_project_id = mock.Mock(return_value=project_id) rv = sot._get_version_match(versions, session.Version(2, 0), "service", "root", True) self.assertEqual(rv, match + project_id)
def test_init_with_multi_api_requests(self): prof = profile.Profile() prof.set_api_version('storage', '1.2') prof.set_api_version('compute', '2.15') sot = session.Session(prof) versions = sot.additional_headers['ecl-api-version'] requests = [req.strip() for req in versions.split(',')] self.assertIn('storage 1.2', requests) self.assertIn('compute 2.15', requests)
def _test__get_endpoint_versions(self, body, versions): sot = session.Session(None) fake_response = mock.Mock() fake_response.json = mock.Mock(return_value=body) sot.get = mock.Mock(return_value=fake_response) scheme = "https" netloc = "devstack" root = scheme + "://" + netloc rv = sot._get_endpoint_versions( "compute", "%s://%s/v2.1/projectidblahblah" % (scheme, netloc)) sot.get.assert_called_with(root) self.assertEqual(rv[0], root) self.assertEqual(rv[1], versions)
def test__get_version_match_exact(self): match = "http://devstack/v2/" versions = [{ "id": "v2.0", "links": [{ "href": match, "rel": "self" }] }, { "id": "v2.1", "links": [{ "href": "http://devstack/v2.1/", "rel": "self" }] }] sot = session.Session(None) rv = sot._get_version_match(versions, session.Version(2, 0), "service", "root", False) self.assertEqual(rv, match)
def test__get_version_match_fuzzy(self): match = "http://devstack/v2.1/" versions = [{ "id": "v2.0", "links": [{ "href": "http://devstack/v2/", "rel": "self" }] }, { "id": "v2.1", "links": [{ "href": match, "rel": "self" }] }] sot = session.Session(None) # Look for a v2 match, which we internally denote as a minor # version of -1 so we can find the highest matching minor. rv = sot._get_version_match(versions, session.Version(2, -1), "service", "root", False) self.assertEqual(rv, match)
def __init__(self, session=None, authenticator=None, profile=None, verify=True, cert=None, user_agent=None, auth_plugin="password", timeout=None, **auth_args): """Create a context for a connection to a cloud provider. A connection needs a transport and an authenticator. The user may pass in a transport and authenticator they want to use or they may pass in the parameters to create a transport and authenticator. The connection creates a :class:`~ecl.session.Session` which uses the profile and authenticator to perform HTTP requests. :param session: A session object compatible with :class:`~ecl.session.Session`. :type session: :class:`~ecl.session.Session` :param authenticator: An authenticator derived from the base authenticator plugin that was previously created. Two common authentication identity plugins are :class:`identity_v2 <ecl.auth.identity.v2.Auth>` and :class:`identity_v3 <ecl.auth.identity.v3.Auth>`. If this parameter is not passed in, the connection will create an authenticator. :type authenticator: :class:`~ecl.auth.base.BaseAuthPlugin` :param profile: If the user has any special profiles such as the service name, region, version or interface, they may be provided in the profile object. If no profiles are provided, the services that appear first in the service catalog will be used. :type profile: :class:`~ecl.profile.Profile` :param bool verify: If a transport is not provided to the connection, this parameter will be used to create a transport. If ``verify`` is set to true, which is the default, the SSL cert will be verified. It can also be set to a CA_BUNDLE path. :param cert: If a transport is not provided to the connection then this parameter will be used to create a transport. `cert` allows to provide a client certificate file path or a tuple with client certificate and key paths. :type cert: str or tuple :param str user_agent: If a transport is not provided to the connection, this parameter will be used when creating a transport. The value given here will be prepended to the default, which is specified in :attr:`~ecl.transport.USER_AGENT`. The resulting ``user_agent`` value is used for the ``User-Agent`` HTTP header. :param str auth_plugin: The name of authentication plugin to use. The default value is ``password``. :param float timeout: A timeout to pass to requests. This should be a numerical value indicating some amount (or fraction) of seconds or 0 for no timeout. (optional, defaults to 0) :param auth_args: The rest of the parameters provided are assumed to be authentication arguments that are used by the authentication plugin. """ self.authenticator = self._create_authenticator( authenticator, auth_plugin, **auth_args) self.profile = profile if profile else _profile.Profile() self.session = session if session else _session.Session( self.profile, auth=self.authenticator, verify=verify, cert=cert, user_agent=user_agent, timeout=timeout) self._open()
def test_init_with_no_api_requests(self): prof = profile.Profile() sot = session.Session(prof) self.assertEqual({}, sot.additional_headers)
def test_init_user_agent_set(self): sot = session.Session(None, user_agent="testing/123") self.assertTrue(sot.user_agent.startswith("testing/123 eclsdk"))
def test_init_user_agent_none(self): sot = session.Session(None) self.assertTrue(sot.user_agent.startswith("eclsdk"))
def test__get_version_match_none(self): sot = session.Session(None) self.assertRaises(exceptions.EndpointNotFound, sot._get_version_match, [], None, "service", "root", False)