From c9af27c2302dda74e702c1cdc50a521fe0b32431 Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Mon, 10 Dec 2012 15:17:58 -0500 Subject: xenapi: Add support for different image upload drivers This change refactors image upload code for xenapi and adds the xenapi_image_upload_handler config option. Preparation for bp nova-direct-image-upload DocImpact Change-Id: Icd4e7ef135beddca9ddcc4a880c09e5a1702a93c --- nova/tests/test_xenapi.py | 68 ++++++--------------- nova/tests/virt/xenapi/imageupload/__init__.py | 0 nova/tests/virt/xenapi/imageupload/test_glance.py | 74 +++++++++++++++++++++++ 3 files changed, 92 insertions(+), 50 deletions(-) create mode 100644 nova/tests/virt/xenapi/imageupload/__init__.py create mode 100644 nova/tests/virt/xenapi/imageupload/test_glance.py (limited to 'nova/tests') diff --git a/nova/tests/test_xenapi.py b/nova/tests/test_xenapi.py index 067e28a13..aa640810b 100644 --- a/nova/tests/test_xenapi.py +++ b/nova/tests/test_xenapi.py @@ -19,7 +19,6 @@ import ast import base64 import contextlib -import cPickle as pickle import functools import os import re @@ -48,6 +47,7 @@ from nova.virt.xenapi import agent from nova.virt.xenapi import driver as xenapi_conn from nova.virt.xenapi import fake as xenapi_fake from nova.virt.xenapi import host +from nova.virt.xenapi.imageupload import glance from nova.virt.xenapi import pool from nova.virt.xenapi import pool_states from nova.virt.xenapi import vm_utils @@ -431,15 +431,29 @@ class XenAPIVMTestCase(stubs.XenAPITestBase): {'task_state': task_states.IMAGE_UPLOADING, 'expected_state': task_states.IMAGE_PENDING_UPLOAD}}] func_call_matcher = matchers.FunctionCallMatcher(expected_calls) + image_id = "my_snapshot_id" stubs.stubout_instance_snapshot(self.stubs) stubs.stubout_is_snapshot(self.stubs) # Stubbing out firewall driver as previous stub sets alters # xml rpc result parsing stubs.stubout_firewall_driver(self.stubs, self.conn) + instance = self._create_instance() - image_id = "my_snapshot_id" + self.fake_upload_called = False + + def fake_image_upload(_self, ctx, session, inst, vdi_uuids, + img_id): + self.fake_upload_called = True + self.assertEqual(ctx, self.context) + self.assertEqual(inst, instance) + self.assertTrue(isinstance(vdi_uuids, list)) + self.assertEqual(img_id, image_id) + + self.stubs.Set(glance.GlanceStore, 'upload_image', + fake_image_upload) + self.conn.snapshot(self.context, instance, image_id, func_call_matcher.call) @@ -469,6 +483,8 @@ class XenAPIVMTestCase(stubs.XenAPITestBase): name_label = vdi_rec["name_label"] self.assert_(not name_label.endswith('snapshot')) + self.assertTrue(self.fake_upload_called) + def create_vm_record(self, conn, os_type, name): instances = conn.list_instances() self.assertEquals(instances, [name]) @@ -2574,54 +2590,6 @@ class SwapXapiHostTestCase(test.TestCase): "http://someserver", 'otherserver')) -class VmUtilsTestCase(test.TestCase): - """Unit tests for xenapi utils.""" - - def test_upload_image(self): - def fake_instance_system_metadata_get(context, uuid): - return dict(image_a=1, image_b=2, image_c='c', d='d') - - def fake_get_sr_path(session): - return "foo" - - class FakeInstance(dict): - def __init__(self): - super(FakeInstance, self).__init__({ - 'auto_disk_config': 'auto disk config', - 'os_type': 'os type'}) - - def __missing__(self, item): - return "whatever" - - class FakeSession(object): - def call_plugin(session_self, service, command, kwargs): - self.kwargs = kwargs - - def call_plugin_serialized(session_self, service, command, *args, - **kwargs): - self.kwargs = kwargs - - def fake_dumps(thing): - return thing - - self.stubs.Set(db, "instance_system_metadata_get", - fake_instance_system_metadata_get) - self.stubs.Set(vm_utils, "get_sr_path", fake_get_sr_path) - self.stubs.Set(pickle, "dumps", fake_dumps) - - ctx = context.get_admin_context() - - instance = FakeInstance() - session = FakeSession() - vm_utils.upload_image(ctx, session, instance, "vmi uuids", "image id") - - actual = self.kwargs['properties'] - # Inheritance happens in another place, now - expected = dict(auto_disk_config='auto disk config', - os_type='os type') - self.assertEquals(expected, actual) - - class XenAPILiveMigrateTestCase(stubs.XenAPITestBase): """Unit tests for live_migration.""" def setUp(self): diff --git a/nova/tests/virt/xenapi/imageupload/__init__.py b/nova/tests/virt/xenapi/imageupload/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/nova/tests/virt/xenapi/imageupload/test_glance.py b/nova/tests/virt/xenapi/imageupload/test_glance.py new file mode 100644 index 000000000..b0518228d --- /dev/null +++ b/nova/tests/virt/xenapi/imageupload/test_glance.py @@ -0,0 +1,74 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2013 OpenStack LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +import mox + +from nova import context +from nova import test +from nova.virt.xenapi.imageupload import glance +from nova.virt.xenapi import vm_utils + + +class TestGlanceStore(test.TestCase): + def setUp(self): + super(TestGlanceStore, self).setUp() + self.store = glance.GlanceStore() + self.mox = mox.Mox() + + def tearDown(self): + super(TestGlanceStore, self).tearDown() + + def test_upload_image(self): + glance_host = '0.1.2.3' + glance_port = 8143 + glance_use_ssl = False + sr_path = '/fake/sr/path' + self.flags(glance_host=glance_host) + self.flags(glance_port=glance_port) + self.flags(glance_api_insecure=glance_use_ssl) + + def fake_get_sr_path(*_args, **_kwargs): + return sr_path + + self.stubs.Set(vm_utils, 'get_sr_path', fake_get_sr_path) + + ctx = context.RequestContext('user', 'project', auth_token='foobar') + properties = { + 'auto_disk_config': True, + 'os_type': 'default', + } + image_id = 'fake_image_uuid' + vdi_uuids = ['fake_vdi_uuid'] + instance = {'uuid': 'blah'} + instance.update(properties) + + params = {'vdi_uuids': vdi_uuids, + 'image_id': image_id, + 'glance_host': glance_host, + 'glance_port': glance_port, + 'glance_use_ssl': glance_use_ssl, + 'sr_path': sr_path, + 'auth_token': 'foobar', + 'properties': properties} + session = self.mox.CreateMockAnything() + session.call_plugin_serialized('glance', 'upload_vhd', **params) + self.mox.ReplayAll() + + self.store.upload_image(ctx, session, instance, vdi_uuids, image_id) + + self.mox.VerifyAll() -- cgit