def test_svc_ep_for_region_not_found(self): svc = self.service region = utils.random_unicode().upper() good_url = utils.random_unicode() bad_url = utils.random_unicode() good_ep = fakes.FakeEndpoint({"public_url": good_url}, svc.service_type, region, self.identity) bad_ep = fakes.FakeEndpoint({"public_url": bad_url}, svc.service_type, region, self.identity) svc.endpoints = utils.DotDict({region: good_ep, "other": bad_ep}) ep = svc._ep_for_region("notthere") self.assertIsNone(ep)
def test_svc_ep_for_region(self): svc = self.service region = utils.random_unicode().upper() bad_region = utils.random_unicode().upper() good_url = utils.random_unicode() bad_url = utils.random_unicode() good_ep = fakes.FakeEndpoint({"public_url": good_url}, svc.service_type, region, self.identity) bad_ep = fakes.FakeEndpoint({"public_url": bad_url}, svc.service_type, bad_region, self.identity) svc.endpoints = utils.DotDict({region: good_ep, bad_region: bad_ep}) ep = svc._ep_for_region(region) self.assertEqual(ep, good_ep)
def test_ep_get_client_already_failed(self): svc = self.service ep_dict = {"publicURL": "http://example.com", "tenantId": "aa"} rgn = utils.random_unicode().upper() ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) ep._client = exc.NoClientForService() self.assertRaises(exc.NoClientForService, ep._get_client)
def test_ep_get_new_client(self): svc = self.service ep_dict = {"publicURL": "http://example.com", "tenantId": "aa"} rgn = utils.random_unicode().upper() ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) ep._get_client = Mock() ep.get_new_client() ep._get_client.assert_called_once_with(public=True, cached=False)
def test_ep_get_client_none(self): svc = self.service ep_dict = {"publicURL": "http://example.com", "tenantId": "aa"} rgn = utils.random_unicode().upper() ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) sav = pyos.client_class_for_service pyos.client_class_for_service = Mock(return_value=None) self.assertRaises(exc.NoClientForService, ep._get_client) pyos.client_class_for_service = sav
def test_ep_get_client_exists(self): svc = self.service ep_dict = {"publicURL": "http://example.com", "tenantId": "aa"} rgn = utils.random_unicode().upper() ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) clt = utils.random_unicode() ep._client = clt ret = ep._get_client() self.assertEqual(ret, clt)
def test_ep_client_private_prop(self): svc = self.service pub = utils.random_unicode() priv = utils.random_unicode() ep_dict = {"publicURL": pub, "privateURL": priv, "tenantId": "aa"} rgn = utils.random_unicode().upper() ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) clt = utils.random_unicode() ep._get_client = Mock(return_value=clt) ret = ep.client_private self.assertEqual(ret, clt)
def test_getattr_not_authed(self): ident = self.base_identity_class() ident.authenticated = False svc = self.service pub = utils.random_unicode() priv = utils.random_unicode() ep_dict = {"publicURL": pub, "privateURL": priv, "tenantId": "aa"} rgn = "FOO" ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) self.service.endpoints = {rgn: ep} ident.services = {"fake": self.service} self.assertRaises(exc.NotAuthenticated, getattr, ident, "BAR")
def test_ep_get(self): svc = self.service pub = utils.random_unicode() priv = utils.random_unicode() ep_dict = {"publicURL": pub, "privateURL": priv, "tenantId": "aa"} rgn = utils.random_unicode().upper() ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) ret = ep.get("public") self.assertEqual(ret, pub) ret = ep.get("private") self.assertEqual(ret, priv) self.assertRaises(ValueError, ep.get, "invalid")
def test_getattr_region(self): ident = self.base_identity_class() ident.authenticated = True svc = self.service pub = utils.random_unicode() priv = utils.random_unicode() ep_dict = {"publicURL": pub, "privateURL": priv, "tenantId": "aa"} rgn = "FOO" ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) self.service.endpoints = {rgn: ep} ident.services = {"fake": self.service} ret = ident.FOO self.assertEqual(ret, {"fake": ep})
def test_get_client_no_client(self): ident = self.identity ident.authenticated = True svc = "fake" region = utils.random_unicode() pub = utils.random_unicode() priv = utils.random_unicode() ep_dict = {"publicURL": pub, "privateURL": priv, "tenantId": "aa"} rgn = "FOO" ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) ep._get_client = Mock(return_value=None) ident.services[svc].endpoints = {region: ep} self.assertRaises(exc.NoSuchClient, ident.get_client, svc, region)
def test_ep_getattr(self): svc = self.service pub = utils.random_unicode() priv = utils.random_unicode() ep_dict = {"publicURL": pub, "privateURL": priv, "tenantId": "aa"} rgn = utils.random_unicode().upper() ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) svc_att = "exists" att_val = utils.random_unicode() setattr(svc, svc_att, att_val) ep._get_client = Mock(return_value=svc) ret = ep.exists self.assertEqual(ret, att_val) self.assertRaises(AttributeError, getattr, ep, "bogus")
def test_ep_get_client(self): svc = self.service ep_dict = {"publicURL": "http://example.com", "tenantId": "aa"} rgn = utils.random_unicode().upper() ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) sav = pyos.client_class_for_service ep.public_url = utils.random_unicode() pyos.client_class_for_service = Mock(return_value=object) fake = utils.random_unicode() ep._create_client = Mock(return_value=fake) ret = ep._get_client() self.assertEqual(ret, fake) self.assertEqual(ep._client, fake) pyos.client_class_for_service = sav
def test_get_client(self): ident = self.identity ident.authenticated = True svc = "fake" region = utils.random_unicode() pub = utils.random_unicode() priv = utils.random_unicode() ep_dict = {"publicURL": pub, "privateURL": priv, "tenantId": "aa"} rgn = "FOO" clt = fakes.FakeClient() ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) ep._get_client = Mock(return_value=clt) ident.services[svc].endpoints = {region: ep} ret = ident.get_client(svc, region) self.assertEqual(ret, clt)
def test_get_client_no_cache(self, mock_ccfs): ident = self.identity ident.authenticated = True svc = "fake" region = utils.random_unicode() pub = utils.random_unicode() priv = utils.random_unicode() ep_dict = {"publicURL": pub, "privateURL": priv, "tenantId": "aa"} rgn = "FOO" clt_class = fakes.FakeClient ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) mock_ccfs.return_value = clt_class ident.services[svc].endpoints = {region: ep} ret = ident.get_client(svc, region, cached=False) self.assertTrue(isinstance(ret, clt_class)) mock_ccfs.assert_called_once_with(svc)
def test_ep_create_client_compute(self): svc = self.service pub = utils.random_unicode() priv = utils.random_unicode() ep_dict = {"publicURL": pub, "privateURL": priv, "tenantId": "aa"} rgn = utils.random_unicode().upper() ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) vssl = random.choice((True, False)) public = random.choice((True, False)) sav_gs = pyos.get_setting pyos.get_setting = Mock(return_value=vssl) sav_conn = pyos.connect_to_cloudservers fake_client = fakes.FakeClient() fake_client.identity = self.identity pyos.connect_to_cloudservers = Mock(return_value=fake_client) ep.service = "compute" ret = ep._create_client(None, None, public) self.assertEqual(ret, fake_client) pyos.connect_to_cloudservers.assert_called_once_with(region=ep.region, context=ep.identity) pyos.connect_to_cloudservers = sav_conn pyos.get_setting = sav_gs
def test_ep_create_client_all_other(self): svc = self.service pub = utils.random_unicode() priv = utils.random_unicode() ep_dict = {"publicURL": pub, "privateURL": priv, "tenantId": "aa"} rgn = utils.random_unicode().upper() ep = fakes.FakeEndpoint(ep_dict, svc, rgn, self.identity) vssl = random.choice((True, False)) public = random.choice((True, False)) url = utils.random_unicode() sav_gs = pyos.get_setting pyos.get_setting = Mock(return_value=vssl) class FakeClientClass(object): def __init__(self, identity, region_name, management_url, verify_ssl): self.identity = identity self.region_name = region_name self.management_url = management_url self.verify_ssl = verify_ssl ret = ep._create_client(FakeClientClass, url, public) self.assertTrue(isinstance(ret, FakeClientClass)) pyos.get_setting = sav_gs