示例#1
0
文件: test.py 项目: Erethon/kamaki
class AstakosClient(TestCase):

    cached = False

    def assert_dicts_are_equal(self, d1, d2):
        for k, v in d1.items():
            self.assertTrue(k in d2)
            if isinstance(v, dict):
                self.assert_dicts_are_equal(v, d2[k])
            else:
                self.assertEqual(unicode(v), unicode(d2[k]))

    def setUp(self):
        self.url = 'https://astakos.example.com'
        self.token = 'ast@k0sT0k3n=='
        from kamaki.clients.astakos import AstakosClient as AC
        self.client = AC(self.url, self.token)

    def tearDown(self):
        FR.json = example

    @patch('%s.LoggedAstakosClient.__init__' % astakos_pkg, return_value=None)
    @patch(
        '%s.LoggedAstakosClient.get_endpoints' % astakos_pkg,
        return_value=example)
    def _authenticate(self, get_endpoints, sac):
        r = self.client.authenticate()
        self.assertEqual(
            sac.mock_calls[-1], call(self.token, self.url,
                logger=getLogger('astakosclient')))
        self.assertEqual(get_endpoints.mock_calls[-1], call())
        return r

    def test_authenticate(self):
        r = self._authenticate()
        self.assert_dicts_are_equal(r, example)
        uuid = example['access']['user']['id']
        self.assert_dicts_are_equal(self.client._uuids, {self.token: uuid})
        self.assert_dicts_are_equal(self.client._cache, {uuid: r})
        from astakosclient import AstakosClient as SAC
        self.assertTrue(isinstance(self.client._astakos[uuid], SAC))
        self.assert_dicts_are_equal(self.client._uuids2usernames, {uuid: {}})
        self.assert_dicts_are_equal(self.client._usernames2uuids, {uuid: {}})

    def test_get_client(self):
        if not self.cached:
            self._authenticate()
        from astakosclient import AstakosClient as SNFAC
        self.assertTrue(self.client.get_client(), SNFAC)

    def test_get_token(self):
        self._authenticate()
        uuid = self.client._uuids.values()[0]
        self.assertEqual(self.client.get_token(uuid), self.token)

    def test_get_services(self):
        if not self.cached:
            self._authenticate()
        slist = self.client.get_services()
        self.assertEqual(slist, example['access']['serviceCatalog'])

    def test_get_service_details(self):
        if not self.cached:
            self._authenticate()
        stype = '#FAIL'
        self.assertRaises(ClientError, self.client.get_service_details, stype)
        stype = 'compute'
        expected = [s for s in example['access']['serviceCatalog'] if (
            s['type'] == stype)]
        self.assert_dicts_are_equal(
            self.client.get_service_details(stype), expected[0])

    def test_get_service_endpoints(self):
        if not self.cached:
            self._authenticate()
        stype, version = 'compute', 'V2'
        self.assertRaises(
            ClientError, self.client.get_service_endpoints, stype)
        expected = [s for s in example['access']['serviceCatalog'] if (
            s['type'] == stype)]
        expected = [e for e in expected[0]['endpoints'] if (
            e['versionId'] == version.lower())]
        self.assert_dicts_are_equal(
            self.client.get_service_endpoints(stype, version), expected[0])

    def test_user_info(self):
        if not self.cached:
            self._authenticate()
        self.assertTrue(set(example['access']['user'].keys()).issubset(
            self.client.user_info().keys()))

    def test_item(self):
        if not self.cached:
            self._authenticate()
        for term, val in example['access']['user'].items():
            self.assertEqual(self.client.term(term, self.token), val)
        self.assertTrue(
            example['access']['user']['email'][0] in self.client.term('email'))

    def test_list_users(self):
        if not self.cached:
            self._authenticate()
        FR.json = example
        self._authenticate()
        r = self.client.list_users()
        self.assertTrue(len(r) == 1)
        self.assertEqual(r[0]['auth_token'], self.token)

    @patch(
        '%s.LoggedAstakosClient.get_usernames' % astakos_pkg,
        return_value={42: 'username42', 43: 'username43'})
    def test_uuids2usernames(self, get_usernames):
        from astakosclient import AstakosClientException
        self.assertRaises(
            AstakosClientException, self.client.uuids2usernames, [42, 43])
        with patch(
                '%s.LoggedAstakosClient.__init__' % astakos_pkg,
                return_value=None) as sac:
            with patch(
                    '%s.LoggedAstakosClient.get_endpoints' % astakos_pkg,
                    return_value=example) as get_endpoints:
                r = self.client.uuids2usernames([42, 43])
                self.assert_dicts_are_equal(
                    r, {42: 'username42', 43: 'username43'})
                self.assertEqual(sac.mock_calls[-1], call(
                    self.token, self.url, logger=getLogger('astakosclient')))
                self.assertEqual(get_endpoints.mock_calls[-1], call())
                self.assertEqual(get_usernames.mock_calls[-1], call([42, 43]))

    @patch(
        '%s.LoggedAstakosClient.get_uuids' % astakos_pkg,
        return_value={'username42': 42, 'username43': 43})
    def test_usernames2uuids(self, get_uuids):
        from astakosclient import AstakosClientException
        self.assertRaises(
            AstakosClientException, self.client.usernames2uuids, ['u1', 'u2'])
        with patch(
                '%s.LoggedAstakosClient.__init__' % astakos_pkg,
                return_value=None) as sac:
            with patch(
                    '%s.LoggedAstakosClient.get_endpoints' % astakos_pkg,
                    return_value=example) as get_endpoints:
                r = self.client.usernames2uuids(['u1', 'u2'])
                self.assert_dicts_are_equal(
                    r, {'username42': 42, 'username43': 43})
                self.assertEqual(sac.mock_calls[-1], call(
                    self.token, self.url, logger=getLogger('astakosclient')))
                self.assertEqual(get_endpoints.mock_calls[-1], call())
                self.assertEqual(get_uuids.mock_calls[-1], call(['u1', 'u2']))
示例#2
0
文件: astakos.py 项目: Erethon/kamaki
class Astakos(livetest.Generic):
    def setUp(self):
        self.cloud = 'cloud.%s' % self['testcloud']
        self.client = AstakosClient(
            self[self.cloud, 'url'], self[self.cloud, 'token'])
        with open(self['astakos', 'details']) as f:
            self._astakos_details = eval(f.read())

    def test_authenticate(self):
        self._test_0010_authenticate()

    def _test_0010_authenticate(self):
        r = self.client.authenticate()
        self.assert_dicts_are_equal(r, self._astakos_details)

    def test_get_services(self):
        self._test_0020_get_services()

    def _test_0020_get_services(self):
        for args in (tuple(), (self[self.cloud, 'token'],)):
            r = self.client.get_services(*args)
            services = self._astakos_details['access']['serviceCatalog']
            self.assertEqual(len(services), len(r))
            for i, service in enumerate(services):
                self.assert_dicts_are_equal(r[i], service)
        self.assertRaises(ClientError, self.client.get_services, 'wrong_token')

    def test_get_service_details(self):
        self._test_0020_get_service_details()

    def _test_0020_get_service_details(self):
        parsed_services = dict()
        for args in product(
                self._astakos_details['access']['serviceCatalog'],
                ([tuple(), (self[self.cloud, 'token'],)])):
            service = args[0]
            if service['type'] in parsed_services:
                continue
            r = self.client.get_service_details(service['type'], *args[1])
            self.assert_dicts_are_equal(r, service)
            parsed_services[service['type']] = True
        self.assertRaises(
            ClientError, self.client.get_service_details, 'wrong_token')

    def test_get_service_endpoints(self):
        self._test_0020_get_service_endpoints()

    def _test_0020_get_service_endpoints(self):
        parsed_services = dict()
        for args in product(
                self._astakos_details['access']['serviceCatalog'],
                ([], [self[self.cloud, 'token']])):
            service = args[0]
            if service['type'] in parsed_services:
                continue
            for endpoint, with_id in product(
                    service['endpoints'], (True, False)):
                vid = endpoint['versionId'] if (
                    with_id and endpoint['versionId']) else None
                end_args = [service['type'], vid] + args[1]
                r = self.client.get_service_endpoints(*end_args)
                self.assert_dicts_are_equal(r, endpoint)
            parsed_services[service['type']] = True
        self.assertRaises(
            ClientError, self.client.get_service_endpoints, 'wrong_token')

    def test_user_info(self):
        self._test_0020_user_info()

    def _test_0020_user_info(self):
        self.assertTrue(set([
            'roles',
            'name',
            'id']).issubset(self.client.user_info().keys()))

    def test_get(self):
        self._test_0020_get()

    def _test_0020_get(self):
        for term in ('id', 'name'):
            self.assertEqual(
                self.client.term(term, self[self.cloud, 'token']),
                self['astakos', term] or '')

    def test_list_users(self):
        self.client.authenticate()
        self._test_0020_list_users()

    def _test_0020_list_users(self):
        terms = set(['name', 'id'])
        uuid = 0
        for r in self.client.list_users():
            self.assertTrue(terms.issubset(r.keys()))
            self.assertTrue(uuid != r['id'] if uuid else True)
            uuid = r['id']