Unit test replication_dump.
Extend unit test coverage to glance-replicator's replication_dump method. Change-Id: I91928c2f73e5394782652af58ab4790b1ecc6844
This commit is contained in:
parent
cdc40f6623
commit
6b2300fb0c
|
@ -254,8 +254,15 @@ class ImageService(object):
|
|||
body = response.read()
|
||||
return headers, body
|
||||
|
||||
def get_image_service():
|
||||
"""Get a copy of the image service.
|
||||
|
||||
def replication_size(options, args, imageservice=None):
|
||||
This is done like this to make it easier to mock out ImageService.
|
||||
"""
|
||||
return ImageService
|
||||
|
||||
|
||||
def replication_size(options, args):
|
||||
"""%(prog)s size <server:port>
|
||||
|
||||
Determine the size of a glance instance if dumped to disk.
|
||||
|
@ -269,8 +276,7 @@ def replication_size(options, args, imageservice=None):
|
|||
total_size = 0
|
||||
count = 0
|
||||
|
||||
if not imageservice:
|
||||
imageservice = ImageService
|
||||
imageservice = get_image_service()
|
||||
client = imageservice(httplib.HTTPConnection(server, port),
|
||||
options.slavetoken)
|
||||
for image in client.get_images():
|
||||
|
@ -295,7 +301,8 @@ def replication_dump(options, args):
|
|||
server_port = args.pop()
|
||||
server, port = server_port.split(':')
|
||||
|
||||
client = ImageService(httplib.HTTPConnection(server, port),
|
||||
imageservice = get_image_service()
|
||||
client = imageservice(httplib.HTTPConnection(server, port),
|
||||
options.mastertoken)
|
||||
for image in client.get_images():
|
||||
logging.info(_('Considering: %s' % image['id']))
|
||||
|
|
|
@ -18,8 +18,10 @@ import copy
|
|||
import imp
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import StringIO
|
||||
import sys
|
||||
import tempfile
|
||||
import UserDict
|
||||
|
||||
from glance.tests import utils as test_utils
|
||||
|
@ -249,9 +251,16 @@ class FakeImageService(object):
|
|||
pass
|
||||
|
||||
def get_images(self):
|
||||
return [{'status': 'active', 'size': 100, 'id': 123},
|
||||
{'status': 'deleted', 'size': 200, 'id': 456},
|
||||
{'status': 'active', 'size': 300, 'id': 789}]
|
||||
return [{'status': 'active', 'size': 100, 'id': '123'},
|
||||
{'status': 'deleted', 'size': 200, 'id': '456'},
|
||||
{'status': 'active', 'size': 300, 'id': '789'}]
|
||||
|
||||
def get_image(self, id):
|
||||
return test_utils.FakeHTTPResponse()
|
||||
|
||||
|
||||
def get_image_service():
|
||||
return FakeImageService
|
||||
|
||||
|
||||
class ReplicationCommandsTestCase(test_utils.BaseTestCase):
|
||||
|
@ -261,14 +270,56 @@ class ReplicationCommandsTestCase(test_utils.BaseTestCase):
|
|||
args = ['localhost:9292']
|
||||
|
||||
stdout = sys.stdout
|
||||
orig_img_service = glance_replicator.get_image_service
|
||||
sys.stdout = StringIO.StringIO()
|
||||
try:
|
||||
glance_replicator.replication_size(options, args,
|
||||
imageservice=FakeImageService)
|
||||
glance_replicator.get_image_service = get_image_service
|
||||
glance_replicator.replication_size(options, args)
|
||||
sys.stdout.seek(0)
|
||||
output = sys.stdout.read()
|
||||
finally:
|
||||
sys.stdout = stdout
|
||||
glance_replicator.get_image_service = orig_img_service
|
||||
|
||||
output = output.rstrip()
|
||||
self.assertEqual(output, 'Total size is 400 bytes across 2 images')
|
||||
|
||||
def test_replication_dump(self):
|
||||
tempdir = tempfile.mkdtemp()
|
||||
|
||||
orig_img_service = glance_replicator.get_image_service
|
||||
try:
|
||||
options = UserDict.UserDict()
|
||||
options.chunksize = 4096
|
||||
options.mastertoken = 'mastertoken'
|
||||
options.metaonly = False
|
||||
args = ['localhost:9292', tempdir]
|
||||
|
||||
glance_replicator.get_image_service = get_image_service
|
||||
glance_replicator.replication_dump(options, args)
|
||||
|
||||
for active in ['123', '789']:
|
||||
imgfile = os.path.join(tempdir, active)
|
||||
self.assertTrue(os.path.exists(imgfile))
|
||||
self.assertTrue(os.path.exists('%s.img' % imgfile))
|
||||
|
||||
with open(imgfile) as f:
|
||||
d = json.loads(f.read())
|
||||
self.assertTrue('status' in d)
|
||||
self.assertTrue('id' in d)
|
||||
self.assertTrue('size' in d)
|
||||
|
||||
for inactive in ['456']:
|
||||
imgfile = os.path.join(tempdir, inactive)
|
||||
self.assertTrue(os.path.exists(imgfile))
|
||||
self.assertFalse(os.path.exists('%s.img' % imgfile))
|
||||
|
||||
with open(imgfile) as f:
|
||||
d = json.loads(f.read())
|
||||
self.assertTrue('status' in d)
|
||||
self.assertTrue('id' in d)
|
||||
self.assertTrue('size' in d)
|
||||
|
||||
finally:
|
||||
glance_replicator.get_image_service = orig_img_service
|
||||
shutil.rmtree(tempdir)
|
||||
|
|
|
@ -387,3 +387,9 @@ class FakeHTTPResponse(object):
|
|||
|
||||
def getheader(self, name, default=None):
|
||||
return self.headers.get(name.lower(), default)
|
||||
|
||||
def getheaders(self):
|
||||
return self.headers or {}
|
||||
|
||||
def read(self, amt):
|
||||
self.data.read(amt)
|
||||
|
|
Loading…
Reference in New Issue