diff --git a/sync/accounts.py b/sync/accounts.py index fd161eb..d0210c9 100644 --- a/sync/accounts.py +++ b/sync/accounts.py @@ -55,8 +55,10 @@ class Accounts(object): orig_storage_cnx = swiftclient.http_connection(orig_storage_url) dest_storage_cnx = swiftclient.http_connection(dest_storage_url) - orig_account_stats, orig_containers = swiftclient.get_account( - None, orig_token, http_conn=orig_storage_cnx, full_listing=True) + _, orig_containers = ( + swiftclient.get_account(None, orig_token, + http_conn=orig_storage_cnx, + full_listing=True)) for container in orig_containers: print container diff --git a/tests/fakes.py b/tests/fakes.py index 36d0c7c..c3acdb3 100644 --- a/tests/fakes.py +++ b/tests/fakes.py @@ -1,31 +1,35 @@ # -*- encoding: utf-8 -*- __author__ = "Chmouel Boudjnah " import uuid +import random + +STORAGE_ORIG = 'http://storage-orig.com' +STORAGE_DEST = 'http://storage-dest.com' 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'}} +CONTAINERS_LIST = [{'count': random.randint(1, 100), + 'name': "cont1", + 'bytes': random.randint(1, 10000)}, + {'count': random.randint(1, 100), + 'name': "cont2", + 'bytes': random.randint(1, 10000)}, + {'count': random.randint(1, 100), + 'name': "cont3", + 'bytes': random.randint(1, 10000)}] -STORAGE_DEST = 'http://storage-dest.com' +CONFIGDICT = {'auth': + {'keystone_origin': STORAGE_ORIG, + 'keystone_origin_admin_credentials': 'foo1:bar:kernel', + 'keystone_dest': STORAGE_DEST}} 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 @@ -37,6 +41,38 @@ class FakeSWConnection(object): return ('%s/v1/AUTH_%s' % (STORAGE_DEST, tenant_id), 'token') +class FakeSWContainer(object): + def __init__(self, dico): + self.dico = dico + + def __getitem__(self, key): + if key == 'name': + return self.dico['name'] + + def __repr__(self): + return str(self.dico) + + +class FakeSWClient(object): + @staticmethod + def http_connection(url): + #TODO: + return "cnx" + + @staticmethod + def get_account(*args, **kwargs): + return (random.randint(1, 9999), + [FakeSWContainer(x) for x in CONTAINERS_LIST]) + + +def fake_get_auth(auth_url, tenant, user, password): + return FakeSWConnection( + auth_url, + '%s:%s' % (tenant, user), + password, + auth_version=2).get_auth() + + class FakeKSTenant(object): def __init__(self, tenant_name): self.tenant_name = tenant_name diff --git a/tests/test_accounts.py b/tests/test_accounts.py index 2ed355d..a75921f 100644 --- a/tests/test_accounts.py +++ b/tests/test_accounts.py @@ -19,9 +19,9 @@ import swiftclient import keystoneclient import sync.accounts -from fakes import (FakeSWConnection, TENANTS_LIST, - STORAGE_DEST, FakeKS, CONFIGDICT, - fake_get_config) +from fakes import FakeSWConnection, TENANTS_LIST, STORAGE_ORIG, \ + STORAGE_DEST, FakeSWClient, FakeKS, CONFIGDICT, CONTAINERS_LIST, \ + fake_get_config class TestAccount(unittest2.TestCase): @@ -31,6 +31,8 @@ class TestAccount(unittest2.TestCase): def _monkey_patch(self): keystoneclient.v2_0.client = FakeKS + swiftclient.get_account = FakeSWClient.get_account + swiftclient.http_connection = FakeSWClient.http_connection swiftclient.client.Connection = FakeSWConnection sync.accounts.get_config = fake_get_config @@ -70,4 +72,20 @@ class TestAccount(unittest2.TestCase): [self.assertTrue(x[1].startswith(STORAGE_DEST)) for x in ret] def test_sync_account(self): - pass + ret = [] + + def sync_container(*args, **kwargs): + ret.append(args) + + sync.accounts.sync_container = sync_container + + tenant_name = TENANTS_LIST.keys()[0] + orig_storage_url = "%s/AUTH_%s" % (STORAGE_ORIG, + TENANTS_LIST[tenant_name]['id']) + dest_storage_url = "%s/AUTH_%s" % (STORAGE_DEST, + TENANTS_LIST[tenant_name]['id']) + self.accounts_cls.sync_account(orig_storage_url, "otoken", + dest_storage_url, "dtoken") + ret_container_list = sorted(x[6] for x in ret) + default_container_list = sorted(x['name'] for x in CONTAINERS_LIST) + self.assertEquals(ret_container_list, default_container_list)