From c5955b9c043de02eec87e156f42dbd6aa5dc9c80 Mon Sep 17 00:00:00 2001 From: Ewan Mellor Date: Sat, 26 Nov 2011 11:40:31 -0800 Subject: [PATCH] Refactor a few things inside the xenapi unit tests. There were a couple of places where basically the same code was being used to create a simulated VDI record -- these have been brought together into _make_fake_vdi. fake_fetch_image was stubbing out parse_xmlrpc_value, which looks like it was a workaround for the fact that the task result wasn't being populated properly. I've fixed the latter, and removed the former. This moved the JSON handling into xenapi.fake. There were a couple of implementations of host_call_plugin, which contained a lot of duplicated code. In particular, they both made a simulated VDI record, even when the plugin function being called didn't require that. I have brought the two implementations together into fake.SessionBase, with overrides in the subclasses for those things that are specific to a given test. I have also made the baseclass strict about flagging unsimulated plugin calls, and added explicit handling for agent.version, and a couple of methods from glance and migration. Change-Id: Idc3a872870ae15165747a04ecd1b48e889bd90fd --- nova/tests/xenapi/stubs.py | 57 +++++++++++++------------------------- 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/nova/tests/xenapi/stubs.py b/nova/tests/xenapi/stubs.py index 51ca7870..7c5634c5 100644 --- a/nova/tests/xenapi/stubs.py +++ b/nova/tests/xenapi/stubs.py @@ -17,7 +17,6 @@ """Stubouts, mocks and fixtures for the test suite""" import eventlet -import json import random from nova.virt import xenapi_conn @@ -32,23 +31,10 @@ def stubout_instance_snapshot(stubs): @classmethod def fake_fetch_image(cls, context, session, instance, image, user, project, type): - from nova.virt.xenapi.fake import create_vdi - name_label = "instance-%s" % instance.id - #TODO: create fake SR record - sr_ref = "fakesr" - vdi_ref = create_vdi(name_label=name_label, read_only=False, - sr_ref=sr_ref, sharable=False) - vdi_rec = session.call_xenapi("VDI.get_record", vdi_ref) - vdi_uuid = vdi_rec['uuid'] - return [dict(vdi_type='os', vdi_uuid=vdi_uuid)] + return [dict(vdi_type='os', vdi_uuid=_make_fake_vdi())] stubs.Set(vm_utils.VMHelper, 'fetch_image', fake_fetch_image) - def fake_parse_xmlrpc_value(val): - return val - - stubs.Set(xenapi_conn, '_parse_xmlrpc_value', fake_parse_xmlrpc_value) - def fake_wait_for_vhd_coalesce(session, instance_id, sr_ref, vdi_ref, original_parent_uuid): #TODO(sirp): Should we actually fake out the data here @@ -156,38 +142,35 @@ def stubout_loopingcall_delay(stubs): stubs.Set(utils.LoopingCall, 'start', fake_start) +def _make_fake_vdi(): + sr_ref = fake.get_all('SR')[0] + vdi_ref = fake.create_vdi('', False, sr_ref, False) + vdi_rec = fake.get_record('VDI', vdi_ref) + return vdi_rec['uuid'] + + class FakeSessionForVMTests(fake.SessionBase): """ Stubs out a XenAPISession for VM tests """ def __init__(self, uri): super(FakeSessionForVMTests, self).__init__(uri) def host_call_plugin(self, _1, _2, plugin, method, _5): - # If the call is for 'copy_kernel_vdi' return None. - if method == 'copy_kernel_vdi': - return - sr_ref = fake.get_all('SR')[0] - vdi_ref = fake.create_vdi('', False, sr_ref, False) - vdi_rec = fake.get_record('VDI', vdi_ref) - if plugin == "glance" and method == "download_vhd": - ret_str = json.dumps([dict(vdi_type='os', - vdi_uuid=vdi_rec['uuid'])]) + if (plugin, method) == ('glance', 'download_vhd'): + return fake.as_json(dict(vdi_type='os', + vdi_uuid=_make_fake_vdi())) else: - ret_str = vdi_rec['uuid'] - return '%s' % ret_str + return (super(FakeSessionForVMTests, self). + host_call_plugin(_1, _2, plugin, method, _5)) def host_call_plugin_swap(self, _1, _2, plugin, method, _5): - sr_ref = fake.get_all('SR')[0] - vdi_ref = fake.create_vdi('', False, sr_ref, False) - vdi_rec = fake.get_record('VDI', vdi_ref) - if plugin == "glance" and method == "download_vhd": - swap_vdi_ref = fake.create_vdi('', False, sr_ref, False) - swap_vdi_rec = fake.get_record('VDI', swap_vdi_ref) - ret_str = json.dumps( - [dict(vdi_type='os', vdi_uuid=vdi_rec['uuid']), - dict(vdi_type='swap', vdi_uuid=swap_vdi_rec['uuid'])]) + if (plugin, method) == ('glance', 'download_vhd'): + return fake.as_json(dict(vdi_type='os', + vdi_uuid=_make_fake_vdi()), + dict(vdi_type='swap', + vdi_uuid=_make_fake_vdi())) else: - ret_str = vdi_rec['uuid'] - return '%s' % ret_str + return (super(FakeSessionForVMTests, self). + host_call_plugin(_1, _2, plugin, method, _5)) def VM_start(self, _1, ref, _2, _3): vm = fake.get_record('VM', ref)