# -*- encoding: utf-8 -*- import os import sys import uuid import unittest2 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) import sync.accounts as sync_accounts TENANTS_LIST = {'foo1': {'id': uuid.uuid4().hex}, 'foo2': {'id': uuid.uuid4().hex}, 'foo3': {'id': uuid.uuid4().hex}} CONFIGDICT = {'auth': {'keystone_origin': 'http://keystone-origin.com', 'keystone_origin_admin_credentials': 'foo1:bar:kernel', 'keystone_dest': 'http://storage-dest.com'}} STORAGE_DEST = 'http://storage-dest.com' def fake_get_config(section, option): return CONFIGDICT[section][option] def fake_get_auth(auth_url, tenant, user, password): return FakeSWConnection( auth_url, '%s:%s' % (tenant, user), password, auth_version=2).get_auth() class FakeSWConnection(object): def __init__(self, *args, **kwargs): self.mainargs = args self.mainkwargs = kwargs def get_auth(self, *args, **kwargs): tenant, user = self.mainargs[1].split(':') tenant_id = TENANTS_LIST[tenant]['id'] return ('%s/v1/AUTH_%s' % (STORAGE_DEST, tenant_id), 'token') class FakeKSTenant(object): def __init__(self, tenant_name): self.tenant_name = tenant_name @property def id(self): return TENANTS_LIST[self.tenant_name]['id'] def __str__(self): return self.tenant_name class FakeKSClientTenant(object): def list(self): for t in list(TENANTS_LIST): yield FakeKSTenant(t) class FakeKSClient(object): def __init__(self, *args): self.args = args self.tenants = FakeKSClientTenant() def __call__(self): return self.args class FakeKS(object): @staticmethod def Client(*args, **kwargs): return FakeKSClient(args, kwargs) class TestAccount(unittest2.TestCase): def setUp(self): sync_accounts.ksclient = FakeKS sync_accounts.get_config = fake_get_config sync_accounts.get_auth = fake_get_auth def test_list_accounts(self): cnx = FakeKSClient() tenant_list = [str(x) for x in sync_accounts.list_accounts(cnx)] self.assertEquals(sorted(TENANTS_LIST), tenant_list) def test_get_ks_auth_orig(self): args, kwargs = sync_accounts.get_ks_auth_orig()() k = CONFIGDICT['auth']['keystone_origin_admin_credentials'] tenant_name, username, password = k.split(':') self.assertEquals(kwargs['tenant_name'], tenant_name) self.assertEquals(kwargs['username'], username) self.assertEquals(kwargs['password'], password) k = CONFIGDICT['auth']['keystone_origin'] self.assertEquals(k, kwargs['auth_url']) def test_sync_accounts(self): ret = [] def sync_an_account(orig_storage_url, orig_token, dest_storage_url, dest_token): ret.append((orig_storage_url, dest_storage_url)) sync_accounts.sync_an_account = sync_an_account sync_accounts.sync_accounts() tenant_list_ids = sorted(TENANTS_LIST[x]['id'] for x in TENANTS_LIST) ret_orig_storage_id = sorted( x[0][x[0].find('AUTH_') + 5:] for x in ret) self.assertEquals(tenant_list_ids, ret_orig_storage_id) [self.assertTrue(x[1].startswith(STORAGE_DEST)) for x in ret] def test_sync_an_account(self): pass if __name__ == '__main__': unittest2.main()