summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2011-11-28 13:28:58 +0000
committerGerrit Code Review <review@openstack.org>2011-11-28 13:28:58 +0000
commit43bb342296936887a33deba4fb4666eadc747244 (patch)
treebc0edf023c5e9ef8b0e29dba7e332aeebff5e74c
parent356ec713068b82af3cf449815ae76d82b4f86242 (diff)
parenta0baa5c125c0a7ef5bbc8684184ebf2c130bb466 (diff)
Merge "Refactor a few things inside the xenapi unit tests."
-rw-r--r--nova/tests/xenapi/stubs.py57
-rw-r--r--nova/virt/xenapi/fake.py38
2 files changed, 55 insertions, 40 deletions
diff --git a/nova/tests/xenapi/stubs.py b/nova/tests/xenapi/stubs.py
index 51ca78708..7c5634c5a 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 '<string>%s</string>' % 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 '<string>%s</string>' % 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)
diff --git a/nova/virt/xenapi/fake.py b/nova/virt/xenapi/fake.py
index 59ea19fd0..4f40bb656 100644
--- a/nova/virt/xenapi/fake.py
+++ b/nova/virt/xenapi/fake.py
@@ -51,8 +51,10 @@ A fake XenAPI SDK.
"""
+import json
import random
import uuid
+from xml.sax.saxutils import escape
from pprint import pformat
@@ -316,6 +318,21 @@ def check_for_session_leaks():
_db_content['session'])
+def as_value(s):
+ """Helper function for simulating XenAPI plugin responses. It
+ escapes and wraps the given argument."""
+ return '<value>%s</value>' % escape(s)
+
+
+def as_json(*args, **kwargs):
+ """Helper function for simulating XenAPI plugin responses for those
+ that are returning JSON. If this function is given plain arguments,
+ then these are rendered as a JSON list. If it's given keyword
+ arguments then these are rendered as a JSON dict."""
+ arg = args or kwargs
+ return json.dumps(arg)
+
+
class Failure(Exception):
def __init__(self, details):
self.details = details
@@ -435,8 +452,20 @@ class SessionBase(object):
#Always return 12GB available
return 12 * 1024 * 1024 * 1024
- def host_call_plugin(self, *args):
- return 'herp'
+ def host_call_plugin(self, _1, _2, plugin, method, _5):
+ if (plugin, method) == ('agent', 'version'):
+ return as_json(returncode='0', message='1.0')
+ elif (plugin, method) == ('glance', 'copy_kernel_vdi'):
+ return ''
+ elif (plugin, method) == ('glance', 'upload_vhd'):
+ return ''
+ elif (plugin, method) == ('migration', 'move_vhds_into_sr'):
+ return ''
+ elif (plugin, method) == ('migration', 'transfer_vhd'):
+ return ''
+ else:
+ raise Exception('No simulation in host_call_plugin for %s,%s' %
+ (plugin, method))
def VDI_resize_online(self, *args):
return 'derp'
@@ -624,7 +653,10 @@ class SessionBase(object):
task = _db_content['task'][task_ref]
func = name[len('Async.'):]
try:
- task['result'] = self.xenapi_request(func, params[1:])
+ result = self.xenapi_request(func, params[1:])
+ if result:
+ result = as_value(result)
+ task['result'] = result
task['status'] = 'success'
except Failure, exc:
task['error_info'] = exc.details