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()
|
body = response.read()
|
||||||
return headers, body
|
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>
|
"""%(prog)s size <server:port>
|
||||||
|
|
||||||
Determine the size of a glance instance if dumped to disk.
|
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
|
total_size = 0
|
||||||
count = 0
|
count = 0
|
||||||
|
|
||||||
if not imageservice:
|
imageservice = get_image_service()
|
||||||
imageservice = ImageService
|
|
||||||
client = imageservice(httplib.HTTPConnection(server, port),
|
client = imageservice(httplib.HTTPConnection(server, port),
|
||||||
options.slavetoken)
|
options.slavetoken)
|
||||||
for image in client.get_images():
|
for image in client.get_images():
|
||||||
|
@ -295,7 +301,8 @@ def replication_dump(options, args):
|
||||||
server_port = args.pop()
|
server_port = args.pop()
|
||||||
server, port = server_port.split(':')
|
server, port = server_port.split(':')
|
||||||
|
|
||||||
client = ImageService(httplib.HTTPConnection(server, port),
|
imageservice = get_image_service()
|
||||||
|
client = imageservice(httplib.HTTPConnection(server, port),
|
||||||
options.mastertoken)
|
options.mastertoken)
|
||||||
for image in client.get_images():
|
for image in client.get_images():
|
||||||
logging.info(_('Considering: %s' % image['id']))
|
logging.info(_('Considering: %s' % image['id']))
|
||||||
|
|
|
@ -18,8 +18,10 @@ import copy
|
||||||
import imp
|
import imp
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
import StringIO
|
import StringIO
|
||||||
import sys
|
import sys
|
||||||
|
import tempfile
|
||||||
import UserDict
|
import UserDict
|
||||||
|
|
||||||
from glance.tests import utils as test_utils
|
from glance.tests import utils as test_utils
|
||||||
|
@ -249,9 +251,16 @@ class FakeImageService(object):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def get_images(self):
|
def get_images(self):
|
||||||
return [{'status': 'active', 'size': 100, 'id': 123},
|
return [{'status': 'active', 'size': 100, 'id': '123'},
|
||||||
{'status': 'deleted', 'size': 200, 'id': 456},
|
{'status': 'deleted', 'size': 200, 'id': '456'},
|
||||||
{'status': 'active', 'size': 300, 'id': 789}]
|
{'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):
|
class ReplicationCommandsTestCase(test_utils.BaseTestCase):
|
||||||
|
@ -261,14 +270,56 @@ class ReplicationCommandsTestCase(test_utils.BaseTestCase):
|
||||||
args = ['localhost:9292']
|
args = ['localhost:9292']
|
||||||
|
|
||||||
stdout = sys.stdout
|
stdout = sys.stdout
|
||||||
|
orig_img_service = glance_replicator.get_image_service
|
||||||
sys.stdout = StringIO.StringIO()
|
sys.stdout = StringIO.StringIO()
|
||||||
try:
|
try:
|
||||||
glance_replicator.replication_size(options, args,
|
glance_replicator.get_image_service = get_image_service
|
||||||
imageservice=FakeImageService)
|
glance_replicator.replication_size(options, args)
|
||||||
sys.stdout.seek(0)
|
sys.stdout.seek(0)
|
||||||
output = sys.stdout.read()
|
output = sys.stdout.read()
|
||||||
finally:
|
finally:
|
||||||
sys.stdout = stdout
|
sys.stdout = stdout
|
||||||
|
glance_replicator.get_image_service = orig_img_service
|
||||||
|
|
||||||
output = output.rstrip()
|
output = output.rstrip()
|
||||||
self.assertEqual(output, 'Total size is 400 bytes across 2 images')
|
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):
|
def getheader(self, name, default=None):
|
||||||
return self.headers.get(name.lower(), default)
|
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