diff options
| author | Johannes Erdfelt <johannes.erdfelt@rackspace.com> | 2012-08-13 18:07:28 +0000 |
|---|---|---|
| committer | Johannes Erdfelt <johannes.erdfelt@rackspace.com> | 2012-08-15 15:33:57 +0000 |
| commit | 35c12feb8c9467b9b4a7eac7a3f3d63bd620a35b (patch) | |
| tree | 944a9046f85de2ea4d50839e9053856340cdc9cd | |
| parent | 2ba710af4f619a9aa3d4d4e377af01f5c6fe39e9 (diff) | |
xenapi: remove unnecessary json decoding of injected_files
The API is responsible for decoding JSON an XML content. Also, the
code decoded results weren't used since the code below it would use
the original data anyway.
Also, add a unit test for injected_files in the xenapi driver.
Change-Id: Ie96b301b0948f92dd90060eacba0db9ae940b952
| -rw-r--r-- | nova/tests/test_xenapi.py | 29 | ||||
| -rw-r--r-- | nova/virt/xenapi/fake.py | 80 | ||||
| -rw-r--r-- | nova/virt/xenapi/vmops.py | 8 |
3 files changed, 73 insertions, 44 deletions
diff --git a/nova/tests/test_xenapi.py b/nova/tests/test_xenapi.py index 92d3000b3..04049ae70 100644 --- a/nova/tests/test_xenapi.py +++ b/nova/tests/test_xenapi.py @@ -17,6 +17,7 @@ """Test suite for XenAPI.""" import ast +import base64 import contextlib import cPickle as pickle import functools @@ -522,8 +523,11 @@ class XenAPIVMTestCase(stubs.XenAPITestBase): def _test_spawn(self, image_ref, kernel_id, ramdisk_id, instance_type_id="3", os_type="linux", hostname="test", architecture="x86-64", instance_id=1, - check_injection=False, + injected_files=None, check_injection=False, create_record=True, empty_dns=False): + if injected_files is None: + injected_files = [] + # Fake out inject_instance_metadata def fake_inject_instance_metadata(self, instance, vm): pass @@ -554,8 +558,8 @@ class XenAPIVMTestCase(stubs.XenAPITestBase): image_meta = {'id': IMAGE_VHD, 'disk_format': 'vhd'} - self.conn.spawn(self.context, instance, image_meta, [], 'herp', - network_info) + self.conn.spawn(self.context, instance, image_meta, injected_files, + 'herp', network_info) self.create_vm_record(self.conn, os_type, instance['name']) self.check_vm_record(self.conn, check_injection) self.assertTrue(instance.os_type) @@ -774,6 +778,25 @@ class XenAPIVMTestCase(stubs.XenAPITestBase): self.assertEquals(vif_rec['qos_algorithm_params']['kbps'], str(3 * 10 * 1024)) + def test_spawn_injected_files(self): + """Test spawning with injected_files""" + actual_injected_files = [] + + def fake_inject_file(self, method, args): + path = base64.b64decode(args['b64_path']) + contents = base64.b64decode(args['b64_contents']) + actual_injected_files.append((path, contents)) + return jsonutils.dumps({'returncode': '0', 'message': 'success'}) + self.stubs.Set(stubs.FakeSessionForVMTests, + '_plugin_agent_inject_file', fake_inject_file) + + injected_files = [('/tmp/foo', 'foobar')] + self._test_spawn(IMAGE_VHD, None, None, + os_type="linux", architecture="x86-64", + injected_files=injected_files) + self.check_vm_params_for_linux() + self.assertEquals(actual_injected_files, injected_files) + def test_rescue(self): instance = self._create_instance() session = xenapi_conn.XenAPISession('test_url', 'root', 'test_pass') diff --git a/nova/virt/xenapi/fake.py b/nova/virt/xenapi/fake.py index 0fcd30ebe..1f8e400c8 100644 --- a/nova/virt/xenapi/fake.py +++ b/nova/virt/xenapi/fake.py @@ -530,45 +530,59 @@ class SessionBase(object): #Always return 12GB available return 12 * 1024 * 1024 * 1024 - 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) == ('agent', 'key_init'): - return as_json(returncode='D0', message='1') - elif (plugin, method) == ('agent', 'password'): - return as_json(returncode='0', message='success') - elif (plugin, method) == ('agent', 'resetnetwork'): - return as_json(returncode='0', message='success') - elif (plugin, method) == ('glance', 'upload_vhd'): - return '' - elif (plugin, method) == ('kernel', 'copy_vdi'): - return '' - elif (plugin, method) == ('kernel', 'create_kernel_ramdisk'): - return '' - elif (plugin, method) == ('kernel', 'remove_kernel_ramdisk'): - return '' - elif (plugin, method) == ('migration', 'move_vhds_into_sr'): - return '' - elif (plugin, method) == ('migration', 'transfer_vhd'): - return '' - elif (plugin, method) == ('xenhost', 'host_data'): + def _plugin_agent_version(self, method, args): + return as_json(returncode='0', message='1.0') + + def _plugin_agent_key_init(self, method, args): + return as_json(returncode='D0', message='1') + + def _plugin_agent_password(self, method, args): + return as_json(returncode='0', message='success') + + def _plugin_agent_inject_file(self, method, args): + return as_json(returncode='0', message='success') + + def _plugin_agent_resetnetwork(self, method, args): + return as_json(returncode='0', message='success') + + def _plugin_noop(self, method, args): + return '' + + _plugin_glance_upload_vhd = _plugin_noop + _plugin_kernel_copy_vdi = _plugin_noop + _plugin_kernel_create_kernel_ramdisk = _plugin_noop + _plugin_kernel_remove_kernel_ramdisk = _plugin_noop + _plugin_migration_move_vhds_into_sr = _plugin_noop + _plugin_migration_transfer_vhd = _plugin_noop + + def _plugin_xenhost_host_data(self, method, args): return jsonutils.dumps({'host_memory': {'total': 10, 'overhead': 20, 'free': 30, - 'free-computed': 40}, }) - elif (plugin == 'xenhost' and method in ['host_reboot', - 'host_startup', - 'host_shutdown']): - return jsonutils.dumps({"power_action": method[5:]}) - elif (plugin, method) == ('xenhost', 'set_host_enabled'): - enabled = 'enabled' if _5.get('enabled') == 'true' else 'disabled' - return jsonutils.dumps({"status": enabled}) - elif (plugin, method) == ('xenhost', 'host_uptime'): - return jsonutils.dumps({"uptime": "fake uptime"}) - else: + 'free-computed': 40}}) + + def _plugin_poweraction(self, method, args): + return jsonutils.dumps({"power_action": method[5:]}) + + _plugin_xenhost_host_reboot = _plugin_poweraction + _plugin_xenhost_host_startup = _plugin_poweraction + _plugin_xenhost_host_shutdown = _plugin_poweraction + + def _plugin_xenhost_set_host_enabled(self, method, args): + enabled = 'enabled' if args.get('enabled') == 'true' else 'disabled' + return jsonutils.dumps({"status": enabled}) + + def _plugin_xenhost_host_uptime(self, method, args): + return jsonutils.dumps({"uptime": "fake uptime"}) + + def host_call_plugin(self, _1, _2, plugin, method, args): + func = getattr(self, '_plugin_%s_%s' % (plugin, method), None) + if not func: raise Exception('No simulation in host_call_plugin for %s,%s' % (plugin, method)) + return func(method, args) + def VDI_get_virtual_size(self, *args): return 1 * 1024 * 1024 * 1024 diff --git a/nova/virt/xenapi/vmops.py b/nova/virt/xenapi/vmops.py index c74c19773..10cca68f4 100644 --- a/nova/virt/xenapi/vmops.py +++ b/nova/virt/xenapi/vmops.py @@ -512,14 +512,6 @@ class VMOps(object): # Inject files, if necessary if injected_files: - # Check if this is a JSON-encoded string and convert if needed. - if isinstance(injected_files, basestring): - try: - injected_files = jsonutils.loads(injected_files) - except ValueError: - LOG.exception(_("Invalid value for injected_files: %r"), - injected_files, instance=instance) - injected_files = [] # Inject any files, if specified for path, contents in injected_files: agent.inject_file(self._session, instance, vm_ref, |
