Merged trunk.

This commit is contained in:
Brian Lamar
2011-07-29 17:33:55 -04:00
28 changed files with 482 additions and 448 deletions

View File

@@ -67,6 +67,7 @@ Lvov Maxim <usrleon@gmail.com>
Mark Washenberger <mark.washenberger@rackspace.com> Mark Washenberger <mark.washenberger@rackspace.com>
Masanori Itoh <itoumsn@nttdata.co.jp> Masanori Itoh <itoumsn@nttdata.co.jp>
Matt Dietz <matt.dietz@rackspace.com> Matt Dietz <matt.dietz@rackspace.com>
Matthew Hooker <matt@cloudscaling.com>
Michael Gundlach <michael.gundlach@rackspace.com> Michael Gundlach <michael.gundlach@rackspace.com>
Mike Scherbakov <mihgen@gmail.com> Mike Scherbakov <mihgen@gmail.com>
Mohammed Naser <mnaser@vexxhost.com> Mohammed Naser <mnaser@vexxhost.com>
@@ -105,3 +106,4 @@ Yoshiaki Tamura <yoshi@midokura.jp>
Youcef Laribi <Youcef.Laribi@eu.citrix.com> Youcef Laribi <Youcef.Laribi@eu.citrix.com>
Yuriy Taraday <yorik.sar@gmail.com> Yuriy Taraday <yorik.sar@gmail.com>
Zhixue Wu <Zhixue.Wu@citrix.com> Zhixue Wu <Zhixue.Wu@citrix.com>
Zed Shaw <zedshaw@zedshaw.com>

View File

@@ -518,6 +518,14 @@ class AuthManager(object):
return drv.get_user_roles(User.safe_id(user), return drv.get_user_roles(User.safe_id(user),
Project.safe_id(project)) Project.safe_id(project))
def get_active_roles(self, user, project=None):
"""Get all active roles for context"""
if project:
roles = FLAGS.allowed_roles + ['projectmanager']
else:
roles = FLAGS.global_roles
return [role for role in roles if self.has_role(user, role, project)]
def get_project(self, pid): def get_project(self, pid):
"""Get project object by id""" """Get project object by id"""
with self.driver() as drv: with self.driver() as drv:
@@ -730,10 +738,6 @@ class AuthManager(object):
with self.driver() as drv: with self.driver() as drv:
drv.modify_user(uid, access_key, secret_key, admin) drv.modify_user(uid, access_key, secret_key, admin)
@staticmethod
def get_key_pairs(context):
return db.key_pair_get_all_by_user(context.elevated(), context.user_id)
def get_credentials(self, user, project=None, use_dmz=True): def get_credentials(self, user, project=None, use_dmz=True):
"""Get credential zip for user in project""" """Get credential zip for user in project"""
if not isinstance(user, User): if not isinstance(user, User):
@@ -785,7 +789,7 @@ class AuthManager(object):
return read_buffer return read_buffer
def get_environment_rc(self, user, project=None, use_dmz=True): def get_environment_rc(self, user, project=None, use_dmz=True):
"""Get credential zip for user in project""" """Get environment rc for user in project"""
if not isinstance(user, User): if not isinstance(user, User):
user = self.get_user(user) user = self.get_user(user)
if project is None: if project is None:

View File

@@ -43,8 +43,8 @@ from nova import version
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
flags.DEFINE_string('logging_context_format_string', flags.DEFINE_string('logging_context_format_string',
'%(asctime)s %(levelname)s %(name)s ' '%(asctime)s %(levelname)s %(name)s '
'[%(request_id)s %(user)s ' '[%(request_id)s %(user_id)s '
'%(project)s] %(message)s', '%(project_id)s] %(message)s',
'format string to use for log messages with context') 'format string to use for log messages with context')
flags.DEFINE_string('logging_default_format_string', flags.DEFINE_string('logging_default_format_string',
'%(asctime)s %(levelname)s %(name)s [-] ' '%(asctime)s %(levelname)s %(name)s [-] '

66
nova/rpc/__init__.py Normal file
View File

@@ -0,0 +1,66 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# 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.
from nova.utils import import_object
from nova.rpc.common import RemoteError, LOG
from nova import flags
FLAGS = flags.FLAGS
flags.DEFINE_string('rpc_backend',
'nova.rpc.amqp',
"The messaging module to use, defaults to AMQP.")
RPCIMPL = import_object(FLAGS.rpc_backend)
def create_connection(new=True):
return RPCIMPL.Connection.instance(new=True)
def create_consumer(conn, topic, proxy, fanout=False):
if fanout:
return RPCIMPL.FanoutAdapterConsumer(
connection=conn,
topic=topic,
proxy=proxy)
else:
return RPCIMPL.TopicAdapterConsumer(
connection=conn,
topic=topic,
proxy=proxy)
def create_consumer_set(conn, consumers):
return RPCIMPL.ConsumerSet(connection=conn, consumer_list=consumers)
def call(context, topic, msg):
return RPCIMPL.call(context, topic, msg)
def cast(context, topic, msg):
return RPCIMPL.cast(context, topic, msg)
def fanout_cast(context, topic, msg):
return RPCIMPL.fanout_cast(context, topic, msg)
def multicall(context, topic, msg):
return RPCIMPL.multicall(context, topic, msg)

View File

@@ -44,9 +44,7 @@ from nova import fakerabbit
from nova import flags from nova import flags
from nova import log as logging from nova import log as logging
from nova import utils from nova import utils
from nova.rpc.common import RemoteError, LOG
LOG = logging.getLogger('nova.rpc')
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
@@ -418,25 +416,6 @@ def msg_reply(msg_id, reply=None, failure=None):
publisher.close() publisher.close()
class RemoteError(exception.Error):
"""Signifies that a remote class has raised an exception.
Containes a string representation of the type of the original exception,
the value of the original exception, and the traceback. These are
sent to the parent as a joined string so printing the exception
contains all of the relevent info.
"""
def __init__(self, exc_type, value, traceback):
self.exc_type = exc_type
self.value = value
self.traceback = traceback
super(RemoteError, self).__init__('%s %s\n%s' % (exc_type,
value,
traceback))
def _unpack_context(msg): def _unpack_context(msg):
"""Unpack context from msg.""" """Unpack context from msg."""
context_dict = {} context_dict = {}

23
nova/rpc/common.py Normal file
View File

@@ -0,0 +1,23 @@
from nova import exception
from nova import log as logging
LOG = logging.getLogger('nova.rpc')
class RemoteError(exception.Error):
"""Signifies that a remote class has raised an exception.
Containes a string representation of the type of the original exception,
the value of the original exception, and the traceback. These are
sent to the parent as a joined string so printing the exception
contains all of the relevent info.
"""
def __init__(self, exc_type, value, traceback):
self.exc_type = exc_type
self.value = value
self.traceback = traceback
super(RemoteError, self).__init__('%s %s\n%s' % (exc_type,
value,
traceback))

View File

@@ -28,6 +28,7 @@ from nova import flags
from nova import log as logging from nova import log as logging
from nova.scheduler import zone_aware_scheduler from nova.scheduler import zone_aware_scheduler
from nova import utils from nova import utils
from nova import exception
LOG = logging.getLogger('nova.scheduler.least_cost') LOG = logging.getLogger('nova.scheduler.least_cost')

View File

@@ -81,7 +81,7 @@ class ZoneAwareScheduler(driver.Scheduler):
decryptor = crypto.decryptor(FLAGS.build_plan_encryption_key) decryptor = crypto.decryptor(FLAGS.build_plan_encryption_key)
try: try:
json_entry = decryptor(blob) json_entry = decryptor(blob)
return json.dumps(entry) return json.dumps(json_entry)
except M2Crypto.EVP.EVPError: except M2Crypto.EVP.EVPError:
pass pass
return None return None

View File

@@ -149,26 +149,22 @@ class Service(object):
if 'nova-compute' == self.binary: if 'nova-compute' == self.binary:
self.manager.update_available_resource(ctxt) self.manager.update_available_resource(ctxt)
self.conn = rpc.Connection.instance(new=True) self.conn = rpc.create_connection(new=True)
logging.debug("Creating Consumer connection for Service %s" % logging.debug("Creating Consumer connection for Service %s" %
self.topic) self.topic)
# Share this same connection for these Consumers # Share this same connection for these Consumers
consumer_all = rpc.TopicAdapterConsumer( consumer_all = rpc.create_consumer(self.conn, self.topic, self,
connection=self.conn, fanout=False)
topic=self.topic,
proxy=self) node_topic = '%s.%s' % (self.topic, self.host)
consumer_node = rpc.TopicAdapterConsumer( consumer_node = rpc.create_consumer(self.conn, node_topic, self,
connection=self.conn, fanout=False)
topic='%s.%s' % (self.topic, self.host),
proxy=self) fanout = rpc.create_consumer(self.conn, self.topic, self, fanout=True)
fanout = rpc.FanoutAdapterConsumer(
connection=self.conn, consumers = [consumer_all, consumer_node, fanout]
topic=self.topic, consumer_set = rpc.create_consumer_set(self.conn, consumers)
proxy=self)
consumer_set = rpc.ConsumerSet(
connection=self.conn,
consumer_list=[consumer_all, consumer_node, fanout])
# Wait forever, processing these consumers # Wait forever, processing these consumers
def _wait(): def _wait():

View File

@@ -99,9 +99,7 @@ class TestCase(unittest.TestCase):
self.flag_overrides = {} self.flag_overrides = {}
self.injected = [] self.injected = []
self._services = [] self._services = []
self._monkey_patch_attach()
self._original_flags = FLAGS.FlagValuesDict() self._original_flags = FLAGS.FlagValuesDict()
rpc.ConnectionPool = rpc.Pool(max_size=FLAGS.rpc_conn_pool_size)
def tearDown(self): def tearDown(self):
"""Runs after each test method to tear down test environment.""" """Runs after each test method to tear down test environment."""
@@ -126,9 +124,6 @@ class TestCase(unittest.TestCase):
# Reset any overriden flags # Reset any overriden flags
self.reset_flags() self.reset_flags()
# Reset our monkey-patches
rpc.Consumer.attach_to_eventlet = self.original_attach
# Stop any timers # Stop any timers
for x in self.injected: for x in self.injected:
try: try:
@@ -172,17 +167,6 @@ class TestCase(unittest.TestCase):
self._services.append(svc) self._services.append(svc)
return svc return svc
def _monkey_patch_attach(self):
self.original_attach = rpc.Consumer.attach_to_eventlet
def _wrapped(inner_self):
rv = self.original_attach(inner_self)
self.injected.append(rv)
return rv
_wrapped.func_name = self.original_attach.func_name
rpc.Consumer.attach_to_eventlet = _wrapped
# Useful assertions # Useful assertions
def assertDictMatch(self, d1, d2, approx_equal=False, tolerance=0.001): def assertDictMatch(self, d1, d2, approx_equal=False, tolerance=0.001):
"""Assert two dicts are equivalent. """Assert two dicts are equivalent.

View File

@@ -23,7 +23,6 @@ from nova import context
from nova import db from nova import db
from nova import flags from nova import flags
from nova import test from nova import test
from nova.auth import manager
from nova.virt import hyperv from nova.virt import hyperv
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
@@ -34,11 +33,9 @@ class HyperVTestCase(test.TestCase):
"""Test cases for the Hyper-V driver""" """Test cases for the Hyper-V driver"""
def setUp(self): def setUp(self):
super(HyperVTestCase, self).setUp() super(HyperVTestCase, self).setUp()
self.manager = manager.AuthManager() self.user_id = 'fake'
self.user = self.manager.create_user('fake', 'fake', 'fake', self.project_id = 'fake'
admin=True) self.context = context.RequestContext(self.user_id, self.project_id)
self.project = self.manager.create_project('fake', 'fake', 'fake')
self.context = context.RequestContext(self.user, self.project)
def test_create_destroy(self): def test_create_destroy(self):
"""Create a VM and destroy it""" """Create a VM and destroy it"""

View File

@@ -23,7 +23,6 @@ import datetime
import mox import mox
import novaclient.exceptions import novaclient.exceptions
import stubout import stubout
import webob
from mox import IgnoreArg from mox import IgnoreArg
from nova import context from nova import context
@@ -34,12 +33,10 @@ from nova import service
from nova import test from nova import test
from nova import rpc from nova import rpc
from nova import utils from nova import utils
from nova.auth import manager as auth_manager
from nova.scheduler import api from nova.scheduler import api
from nova.scheduler import manager from nova.scheduler import manager
from nova.scheduler import driver from nova.scheduler import driver
from nova.compute import power_state from nova.compute import power_state
from nova.db.sqlalchemy import models
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
@@ -250,23 +247,17 @@ class SimpleDriverTestCase(test.TestCase):
volume_driver='nova.volume.driver.FakeISCSIDriver', volume_driver='nova.volume.driver.FakeISCSIDriver',
scheduler_driver='nova.scheduler.simple.SimpleScheduler') scheduler_driver='nova.scheduler.simple.SimpleScheduler')
self.scheduler = manager.SchedulerManager() self.scheduler = manager.SchedulerManager()
self.manager = auth_manager.AuthManager()
self.user = self.manager.create_user('fake', 'fake', 'fake')
self.project = self.manager.create_project('fake', 'fake', 'fake')
self.context = context.get_admin_context() self.context = context.get_admin_context()
self.user_id = 'fake'
def tearDown(self): self.project_id = 'fake'
self.manager.delete_user(self.user)
self.manager.delete_project(self.project)
super(SimpleDriverTestCase, self).tearDown()
def _create_instance(self, **kwargs): def _create_instance(self, **kwargs):
"""Create a test instance""" """Create a test instance"""
inst = {} inst = {}
inst['image_id'] = 1 inst['image_id'] = 1
inst['reservation_id'] = 'r-fakeres' inst['reservation_id'] = 'r-fakeres'
inst['user_id'] = self.user.id inst['user_id'] = self.user_id
inst['project_id'] = self.project.id inst['project_id'] = self.project_id
inst['instance_type_id'] = '1' inst['instance_type_id'] = '1'
inst['vcpus'] = kwargs.get('vcpus', 1) inst['vcpus'] = kwargs.get('vcpus', 1)
inst['ami_launch_index'] = 0 inst['ami_launch_index'] = 0
@@ -485,11 +476,6 @@ class SimpleDriverTestCase(test.TestCase):
self.assertEqual(host, 'host2') self.assertEqual(host, 'host2')
volume1.delete_volume(self.context, volume_id1) volume1.delete_volume(self.context, volume_id1)
db.volume_destroy(self.context, volume_id2) db.volume_destroy(self.context, volume_id2)
dic = {'service_id': s_ref['id'],
'vcpus': 16, 'memory_mb': 32, 'local_gb': 100,
'vcpus_used': 16, 'memory_mb_used': 12, 'local_gb_used': 10,
'hypervisor_type': 'qemu', 'hypervisor_version': 12003,
'cpu_info': ''}
def test_doesnt_report_disabled_hosts_as_up(self): def test_doesnt_report_disabled_hosts_as_up(self):
"""Ensures driver doesn't find hosts before they are enabled""" """Ensures driver doesn't find hosts before they are enabled"""

View File

@@ -16,6 +16,8 @@
Tests For Zone Aware Scheduler. Tests For Zone Aware Scheduler.
""" """
import json
import nova.db import nova.db
from nova import exception from nova import exception
@@ -327,3 +329,19 @@ class ZoneAwareSchedulerTestCase(test.TestCase):
sched._provision_resource_from_blob(None, request_spec, 1, sched._provision_resource_from_blob(None, request_spec, 1,
request_spec, {}) request_spec, {})
self.assertTrue(was_called) self.assertTrue(was_called)
def test_decrypt_blob(self):
"""Test that the decrypt method works."""
fixture = FakeZoneAwareScheduler()
test_data = {"foo": "bar"}
class StubDecryptor(object):
def decryptor(self, key):
return lambda blob: blob
self.stubs.Set(zone_aware_scheduler, 'crypto',
StubDecryptor())
self.assertEqual(fixture._decrypt_blob(test_data),
json.dumps(test_data))

View File

@@ -16,7 +16,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import unittest
import webob import webob
from nova import context from nova import context
@@ -41,7 +40,7 @@ class FakeApiRequest(object):
class AccessTestCase(test.TestCase): class AccessTestCase(test.TestCase):
def _env_for(self, ctxt, action): def _env_for(self, ctxt, action):
env = {} env = {}
env['ec2.context'] = ctxt env['nova.context'] = ctxt
env['ec2.request'] = FakeApiRequest(action) env['ec2.request'] = FakeApiRequest(action)
return env return env
@@ -93,7 +92,11 @@ class AccessTestCase(test.TestCase):
super(AccessTestCase, self).tearDown() super(AccessTestCase, self).tearDown()
def response_status(self, user, methodName): def response_status(self, user, methodName):
ctxt = context.RequestContext(user, self.project) roles = manager.AuthManager().get_active_roles(user, self.project)
ctxt = context.RequestContext(user.id,
self.project.id,
is_admin=user.is_admin(),
roles=roles)
environ = self._env_for(ctxt, methodName) environ = self._env_for(ctxt, methodName)
req = webob.Request.blank('/', environ) req = webob.Request.blank('/', environ)
resp = req.get_response(self.mw) resp = req.get_response(self.mw)
@@ -105,30 +108,26 @@ class AccessTestCase(test.TestCase):
def shouldDeny(self, user, methodName): def shouldDeny(self, user, methodName):
self.assertEqual(401, self.response_status(user, methodName)) self.assertEqual(401, self.response_status(user, methodName))
def test_001_allow_all(self): def test_allow_all(self):
users = [self.testadmin, self.testpmsys, self.testnet, self.testsys] users = [self.testadmin, self.testpmsys, self.testnet, self.testsys]
for user in users: for user in users:
self.shouldAllow(user, '_allow_all') self.shouldAllow(user, '_allow_all')
def test_002_allow_none(self): def test_allow_none(self):
self.shouldAllow(self.testadmin, '_allow_none') self.shouldAllow(self.testadmin, '_allow_none')
users = [self.testpmsys, self.testnet, self.testsys] users = [self.testpmsys, self.testnet, self.testsys]
for user in users: for user in users:
self.shouldDeny(user, '_allow_none') self.shouldDeny(user, '_allow_none')
def test_003_allow_project_manager(self): def test_allow_project_manager(self):
for user in [self.testadmin, self.testpmsys]: for user in [self.testadmin, self.testpmsys]:
self.shouldAllow(user, '_allow_project_manager') self.shouldAllow(user, '_allow_project_manager')
for user in [self.testnet, self.testsys]: for user in [self.testnet, self.testsys]:
self.shouldDeny(user, '_allow_project_manager') self.shouldDeny(user, '_allow_project_manager')
def test_004_allow_sys_and_net(self): def test_allow_sys_and_net(self):
for user in [self.testadmin, self.testnet, self.testsys]: for user in [self.testadmin, self.testnet, self.testsys]:
self.shouldAllow(user, '_allow_sys_and_net') self.shouldAllow(user, '_allow_sys_and_net')
# denied because it doesn't have the per project sysadmin # denied because it doesn't have the per project sysadmin
for user in [self.testpmsys]: for user in [self.testpmsys]:
self.shouldDeny(user, '_allow_sys_and_net') self.shouldDeny(user, '_allow_sys_and_net')
if __name__ == "__main__":
# TODO: Implement use_fake as an option
unittest.main()

View File

@@ -25,7 +25,6 @@ from nova import log as logging
from nova import rpc from nova import rpc
from nova import test from nova import test
from nova import utils from nova import utils
from nova.auth import manager
from nova.api.ec2 import admin from nova.api.ec2 import admin
from nova.image import fake from nova.image import fake
@@ -39,7 +38,7 @@ class AdminApiTestCase(test.TestCase):
super(AdminApiTestCase, self).setUp() super(AdminApiTestCase, self).setUp()
self.flags(connection_type='fake') self.flags(connection_type='fake')
self.conn = rpc.Connection.instance() self.conn = rpc.create_connection()
# set up our cloud # set up our cloud
self.api = admin.AdminController() self.api = admin.AdminController()
@@ -51,11 +50,11 @@ class AdminApiTestCase(test.TestCase):
self.volume = self.start_service('volume') self.volume = self.start_service('volume')
self.image_service = utils.import_object(FLAGS.image_service) self.image_service = utils.import_object(FLAGS.image_service)
self.manager = manager.AuthManager() self.user_id = 'admin'
self.user = self.manager.create_user('admin', 'admin', 'admin', True) self.project_id = 'admin'
self.project = self.manager.create_project('proj', 'admin', 'proj') self.context = context.RequestContext(self.user_id,
self.context = context.RequestContext(user=self.user, self.project_id,
project=self.project) True)
def fake_show(meh, context, id): def fake_show(meh, context, id):
return {'id': 1, 'properties': {'kernel_id': 1, 'ramdisk_id': 1, return {'id': 1, 'properties': {'kernel_id': 1, 'ramdisk_id': 1,
@@ -73,11 +72,6 @@ class AdminApiTestCase(test.TestCase):
self.stubs.Set(rpc, 'cast', finish_cast) self.stubs.Set(rpc, 'cast', finish_cast)
def tearDown(self):
self.manager.delete_project(self.project)
self.manager.delete_user(self.user)
super(AdminApiTestCase, self).tearDown()
def test_block_external_ips(self): def test_block_external_ips(self):
"""Make sure provider firewall rules are created.""" """Make sure provider firewall rules are created."""
result = self.api.block_external_addresses(self.context, '1.1.1.1/32') result = self.api.block_external_addresses(self.context, '1.1.1.1/32')

View File

@@ -30,11 +30,11 @@ import webob
from nova import context from nova import context
from nova import exception from nova import exception
from nova import test from nova import test
from nova import wsgi
from nova.api import ec2 from nova.api import ec2
from nova.api.ec2 import apirequest from nova.api.ec2 import apirequest
from nova.api.ec2 import cloud from nova.api.ec2 import cloud
from nova.api.ec2 import ec2utils from nova.api.ec2 import ec2utils
from nova.auth import manager
class FakeHttplibSocket(object): class FakeHttplibSocket(object):
@@ -192,10 +192,13 @@ class ApiEc2TestCase(test.TestCase):
"""Unit test for the cloud controller on an EC2 API""" """Unit test for the cloud controller on an EC2 API"""
def setUp(self): def setUp(self):
super(ApiEc2TestCase, self).setUp() super(ApiEc2TestCase, self).setUp()
self.manager = manager.AuthManager()
self.host = '127.0.0.1' self.host = '127.0.0.1'
self.app = ec2.Authenticate(ec2.Requestify(ec2.Executor(), # NOTE(vish): skipping the Authorizer
'nova.api.ec2.cloud.CloudController')) roles = ['sysadmin', 'netadmin']
ctxt = context.RequestContext('fake', 'fake', roles=roles)
self.app = wsgi.InjectContext(ctxt,
ec2.Requestify(ec2.Authorizer(ec2.Executor()),
'nova.api.ec2.cloud.CloudController'))
def expect_http(self, host=None, is_secure=False, api_version=None): def expect_http(self, host=None, is_secure=False, api_version=None):
"""Returns a new EC2 connection""" """Returns a new EC2 connection"""
@@ -246,39 +249,25 @@ class ApiEc2TestCase(test.TestCase):
self.expect_http(api_version='2010-10-30') self.expect_http(api_version='2010-10-30')
self.mox.ReplayAll() self.mox.ReplayAll()
user = self.manager.create_user('fake', 'fake', 'fake')
project = self.manager.create_project('fake', 'fake', 'fake')
# Any request should be fine # Any request should be fine
self.ec2.get_all_instances() self.ec2.get_all_instances()
self.assertTrue(self.ec2.APIVersion in self.http.getresponsebody(), self.assertTrue(self.ec2.APIVersion in self.http.getresponsebody(),
'The version in the xmlns of the response does ' 'The version in the xmlns of the response does '
'not match the API version given in the request.') 'not match the API version given in the request.')
self.manager.delete_project(project)
self.manager.delete_user(user)
def test_describe_instances(self): def test_describe_instances(self):
"""Test that, after creating a user and a project, the describe """Test that, after creating a user and a project, the describe
instances call to the API works properly""" instances call to the API works properly"""
self.expect_http() self.expect_http()
self.mox.ReplayAll() self.mox.ReplayAll()
user = self.manager.create_user('fake', 'fake', 'fake')
project = self.manager.create_project('fake', 'fake', 'fake')
self.assertEqual(self.ec2.get_all_instances(), []) self.assertEqual(self.ec2.get_all_instances(), [])
self.manager.delete_project(project)
self.manager.delete_user(user)
def test_terminate_invalid_instance(self): def test_terminate_invalid_instance(self):
"""Attempt to terminate an invalid instance""" """Attempt to terminate an invalid instance"""
self.expect_http() self.expect_http()
self.mox.ReplayAll() self.mox.ReplayAll()
user = self.manager.create_user('fake', 'fake', 'fake')
project = self.manager.create_project('fake', 'fake', 'fake')
self.assertRaises(EC2ResponseError, self.ec2.terminate_instances, self.assertRaises(EC2ResponseError, self.ec2.terminate_instances,
"i-00000005") "i-00000005")
self.manager.delete_project(project)
self.manager.delete_user(user)
def test_get_all_key_pairs(self): def test_get_all_key_pairs(self):
"""Test that, after creating a user and project and generating """Test that, after creating a user and project and generating
@@ -287,16 +276,12 @@ class ApiEc2TestCase(test.TestCase):
self.mox.ReplayAll() self.mox.ReplayAll()
keyname = "".join(random.choice("sdiuisudfsdcnpaqwertasd") \ keyname = "".join(random.choice("sdiuisudfsdcnpaqwertasd") \
for x in range(random.randint(4, 8))) for x in range(random.randint(4, 8)))
user = self.manager.create_user('fake', 'fake', 'fake')
project = self.manager.create_project('fake', 'fake', 'fake')
# NOTE(vish): create depends on pool, so call helper directly # NOTE(vish): create depends on pool, so call helper directly
cloud._gen_key(context.get_admin_context(), user.id, keyname) cloud._gen_key(context.get_admin_context(), 'fake', keyname)
rv = self.ec2.get_all_key_pairs() rv = self.ec2.get_all_key_pairs()
results = [k for k in rv if k.name == keyname] results = [k for k in rv if k.name == keyname]
self.assertEquals(len(results), 1) self.assertEquals(len(results), 1)
self.manager.delete_project(project)
self.manager.delete_user(user)
def test_create_duplicate_key_pair(self): def test_create_duplicate_key_pair(self):
"""Test that, after successfully generating a keypair, """Test that, after successfully generating a keypair,
@@ -305,8 +290,6 @@ class ApiEc2TestCase(test.TestCase):
self.mox.ReplayAll() self.mox.ReplayAll()
keyname = "".join(random.choice("sdiuisudfsdcnpaqwertasd") \ keyname = "".join(random.choice("sdiuisudfsdcnpaqwertasd") \
for x in range(random.randint(4, 8))) for x in range(random.randint(4, 8)))
user = self.manager.create_user('fake', 'fake', 'fake')
project = self.manager.create_project('fake', 'fake', 'fake')
# NOTE(vish): create depends on pool, so call helper directly # NOTE(vish): create depends on pool, so call helper directly
self.ec2.create_key_pair('test') self.ec2.create_key_pair('test')
@@ -325,27 +308,16 @@ class ApiEc2TestCase(test.TestCase):
"""Test that we can retrieve security groups""" """Test that we can retrieve security groups"""
self.expect_http() self.expect_http()
self.mox.ReplayAll() self.mox.ReplayAll()
user = self.manager.create_user('fake', 'fake', 'fake', admin=True)
project = self.manager.create_project('fake', 'fake', 'fake')
rv = self.ec2.get_all_security_groups() rv = self.ec2.get_all_security_groups()
self.assertEquals(len(rv), 1) self.assertEquals(len(rv), 1)
self.assertEquals(rv[0].name, 'default') self.assertEquals(rv[0].name, 'default')
self.manager.delete_project(project)
self.manager.delete_user(user)
def test_create_delete_security_group(self): def test_create_delete_security_group(self):
"""Test that we can create a security group""" """Test that we can create a security group"""
self.expect_http() self.expect_http()
self.mox.ReplayAll() self.mox.ReplayAll()
user = self.manager.create_user('fake', 'fake', 'fake', admin=True)
project = self.manager.create_project('fake', 'fake', 'fake')
# At the moment, you need both of these to actually be netadmin
self.manager.add_role('fake', 'netadmin')
project.add_role('fake', 'netadmin')
security_group_name = "".join(random.choice("sdiuisudfsdcnpaqwertasd") security_group_name = "".join(random.choice("sdiuisudfsdcnpaqwertasd")
for x in range(random.randint(4, 8))) for x in range(random.randint(4, 8)))
@@ -364,9 +336,6 @@ class ApiEc2TestCase(test.TestCase):
self.ec2.delete_security_group(security_group_name) self.ec2.delete_security_group(security_group_name)
self.manager.delete_project(project)
self.manager.delete_user(user)
def test_authorize_revoke_security_group_cidr(self): def test_authorize_revoke_security_group_cidr(self):
""" """
Test that we can add and remove CIDR based rules Test that we can add and remove CIDR based rules
@@ -374,12 +343,6 @@ class ApiEc2TestCase(test.TestCase):
""" """
self.expect_http() self.expect_http()
self.mox.ReplayAll() self.mox.ReplayAll()
user = self.manager.create_user('fake', 'fake', 'fake')
project = self.manager.create_project('fake', 'fake', 'fake')
# At the moment, you need both of these to actually be netadmin
self.manager.add_role('fake', 'netadmin')
project.add_role('fake', 'netadmin')
security_group_name = "".join(random.choice("sdiuisudfsdcnpaqwertasd") security_group_name = "".join(random.choice("sdiuisudfsdcnpaqwertasd")
for x in range(random.randint(4, 8))) for x in range(random.randint(4, 8)))
@@ -426,9 +389,6 @@ class ApiEc2TestCase(test.TestCase):
self.assertEqual(len(rv), 1) self.assertEqual(len(rv), 1)
self.assertEqual(rv[0].name, 'default') self.assertEqual(rv[0].name, 'default')
self.manager.delete_project(project)
self.manager.delete_user(user)
return return
def test_authorize_revoke_security_group_cidr_v6(self): def test_authorize_revoke_security_group_cidr_v6(self):
@@ -438,12 +398,6 @@ class ApiEc2TestCase(test.TestCase):
""" """
self.expect_http() self.expect_http()
self.mox.ReplayAll() self.mox.ReplayAll()
user = self.manager.create_user('fake', 'fake', 'fake')
project = self.manager.create_project('fake', 'fake', 'fake')
# At the moment, you need both of these to actually be netadmin
self.manager.add_role('fake', 'netadmin')
project.add_role('fake', 'netadmin')
security_group_name = "".join(random.choice("sdiuisudfsdcnpaqwertasd") security_group_name = "".join(random.choice("sdiuisudfsdcnpaqwertasd")
for x in range(random.randint(4, 8))) for x in range(random.randint(4, 8)))
@@ -489,9 +443,6 @@ class ApiEc2TestCase(test.TestCase):
self.assertEqual(len(rv), 1) self.assertEqual(len(rv), 1)
self.assertEqual(rv[0].name, 'default') self.assertEqual(rv[0].name, 'default')
self.manager.delete_project(project)
self.manager.delete_user(user)
return return
def test_authorize_revoke_security_group_foreign_group(self): def test_authorize_revoke_security_group_foreign_group(self):
@@ -501,12 +452,6 @@ class ApiEc2TestCase(test.TestCase):
""" """
self.expect_http() self.expect_http()
self.mox.ReplayAll() self.mox.ReplayAll()
user = self.manager.create_user('fake', 'fake', 'fake', admin=True)
project = self.manager.create_project('fake', 'fake', 'fake')
# At the moment, you need both of these to actually be netadmin
self.manager.add_role('fake', 'netadmin')
project.add_role('fake', 'netadmin')
rand_string = 'sdiuisudfsdcnpaqwertasd' rand_string = 'sdiuisudfsdcnpaqwertasd'
security_group_name = "".join(random.choice(rand_string) security_group_name = "".join(random.choice(rand_string)
@@ -560,8 +505,3 @@ class ApiEc2TestCase(test.TestCase):
self.mox.ReplayAll() self.mox.ReplayAll()
self.ec2.delete_security_group(security_group_name) self.ec2.delete_security_group(security_group_name)
self.manager.delete_project(project)
self.manager.delete_user(user)
return

View File

@@ -102,7 +102,7 @@ class _AuthManagerBaseTestCase(test.TestCase):
self.assertEqual('classified', u.secret) self.assertEqual('classified', u.secret)
self.assertEqual('private-party', u.access) self.assertEqual('private-party', u.access)
def test_004_signature_is_valid(self): def test_signature_is_valid(self):
with user_generator(self.manager, name='admin', secret='admin', with user_generator(self.manager, name='admin', secret='admin',
access='admin'): access='admin'):
with project_generator(self.manager, name="admin", with project_generator(self.manager, name="admin",
@@ -141,15 +141,14 @@ class _AuthManagerBaseTestCase(test.TestCase):
'127.0.0.1', '127.0.0.1',
'/services/Cloud')) '/services/Cloud'))
def test_005_can_get_credentials(self): def test_can_get_credentials(self):
return st = {'access': 'access', 'secret': 'secret'}
credentials = self.manager.get_user('test1').get_credentials() with user_and_project_generator(self.manager, user_state=st) as (u, p):
self.assertEqual(credentials, credentials = self.manager.get_environment_rc(u, p)
'export EC2_ACCESS_KEY="access"\n' + LOG.debug(credentials)
'export EC2_SECRET_KEY="secret"\n' + self.assertTrue('export EC2_ACCESS_KEY="access:testproj"\n'
'export EC2_URL="http://127.0.0.1:8773/services/Cloud"\n' + in credentials)
'export S3_URL="http://127.0.0.1:3333/"\n' + self.assertTrue('export EC2_SECRET_KEY="secret"\n' in credentials)
'export EC2_USER_ID="test1"\n')
def test_can_list_users(self): def test_can_list_users(self):
with user_generator(self.manager): with user_generator(self.manager):

View File

@@ -34,7 +34,6 @@ from nova import network
from nova import rpc from nova import rpc
from nova import test from nova import test
from nova import utils from nova import utils
from nova.auth import manager
from nova.api.ec2 import cloud from nova.api.ec2 import cloud
from nova.api.ec2 import ec2utils from nova.api.ec2 import ec2utils
from nova.image import fake from nova.image import fake
@@ -50,7 +49,7 @@ class CloudTestCase(test.TestCase):
self.flags(connection_type='fake', self.flags(connection_type='fake',
stub_network=True) stub_network=True)
self.conn = rpc.Connection.instance() self.conn = rpc.create_connection()
# set up our cloud # set up our cloud
self.cloud = cloud.CloudController() self.cloud = cloud.CloudController()
@@ -62,12 +61,11 @@ class CloudTestCase(test.TestCase):
self.volume = self.start_service('volume') self.volume = self.start_service('volume')
self.image_service = utils.import_object(FLAGS.image_service) self.image_service = utils.import_object(FLAGS.image_service)
self.manager = manager.AuthManager() self.user_id = 'fake'
self.user = self.manager.create_user('admin', 'admin', 'admin', True) self.project_id = 'fake'
self.project = self.manager.create_project('proj', 'admin', 'proj') self.context = context.RequestContext(self.user_id,
self.context = context.RequestContext(user=self.user, self.project_id,
project=self.project) True)
host = self.network.host
def fake_show(meh, context, id): def fake_show(meh, context, id):
return {'id': 1, 'container_format': 'ami', return {'id': 1, 'container_format': 'ami',
@@ -87,17 +85,15 @@ class CloudTestCase(test.TestCase):
self.stubs.Set(rpc, 'cast', finish_cast) self.stubs.Set(rpc, 'cast', finish_cast)
def tearDown(self): def tearDown(self):
networks = db.project_get_networks(self.context, self.project.id, networks = db.project_get_networks(self.context, self.project_id,
associate=False) associate=False)
for network in networks: for network in networks:
db.network_disassociate(self.context, network['id']) db.network_disassociate(self.context, network['id'])
self.manager.delete_project(self.project)
self.manager.delete_user(self.user)
super(CloudTestCase, self).tearDown() super(CloudTestCase, self).tearDown()
def _create_key(self, name): def _create_key(self, name):
# NOTE(vish): create depends on pool, so just call helper directly # NOTE(vish): create depends on pool, so just call helper directly
return cloud._gen_key(self.context, self.context.user.id, name) return cloud._gen_key(self.context, self.context.user_id, name)
def test_describe_regions(self): def test_describe_regions(self):
"""Makes sure describe regions runs without raising an exception""" """Makes sure describe regions runs without raising an exception"""
@@ -326,22 +322,15 @@ class CloudTestCase(test.TestCase):
revoke = self.cloud.revoke_security_group_ingress revoke = self.cloud.revoke_security_group_ingress
self.assertTrue(revoke(self.context, group_name=sec['name'], **kwargs)) self.assertTrue(revoke(self.context, group_name=sec['name'], **kwargs))
def test_revoke_security_group_ingress_by_id(self): def test_authorize_revoke_security_group_ingress_by_id(self):
kwargs = {'project_id': self.context.project_id, 'name': 'test'}
sec = db.security_group_create(self.context, kwargs)
authz = self.cloud.authorize_security_group_ingress
kwargs = {'to_port': '999', 'from_port': '999', 'ip_protocol': 'tcp'}
authz(self.context, group_id=sec['id'], **kwargs)
revoke = self.cloud.revoke_security_group_ingress
self.assertTrue(revoke(self.context, group_id=sec['id'], **kwargs))
def test_authorize_security_group_ingress_by_id(self):
sec = db.security_group_create(self.context, sec = db.security_group_create(self.context,
{'project_id': self.context.project_id, {'project_id': self.context.project_id,
'name': 'test'}) 'name': 'test'})
authz = self.cloud.authorize_security_group_ingress authz = self.cloud.authorize_security_group_ingress
kwargs = {'to_port': '999', 'from_port': '999', 'ip_protocol': 'tcp'} kwargs = {'to_port': '999', 'from_port': '999', 'ip_protocol': 'tcp'}
self.assertTrue(authz(self.context, group_id=sec['id'], **kwargs)) authz(self.context, group_id=sec['id'], **kwargs)
revoke = self.cloud.revoke_security_group_ingress
self.assertTrue(revoke(self.context, group_id=sec['id'], **kwargs))
def test_authorize_security_group_ingress_missing_protocol_params(self): def test_authorize_security_group_ingress_missing_protocol_params(self):
sec = db.security_group_create(self.context, sec = db.security_group_create(self.context,
@@ -961,21 +950,6 @@ class CloudTestCase(test.TestCase):
self._wait_for_running(ec2_instance_id) self._wait_for_running(ec2_instance_id)
return ec2_instance_id return ec2_instance_id
def test_rescue_unrescue_instance(self):
instance_id = self._run_instance(
image_id='ami-1',
instance_type=FLAGS.default_instance_type,
max_count=1)
self.cloud.rescue_instance(context=self.context,
instance_id=instance_id)
# NOTE(vish): This currently does no validation, it simply makes sure
# that the code path doesn't throw an exception.
self.cloud.unrescue_instance(context=self.context,
instance_id=instance_id)
# TODO(soren): We need this until we can stop polling in the rpc code
# for unit tests.
self.cloud.terminate_instances(self.context, [instance_id])
def test_console_output(self): def test_console_output(self):
instance_id = self._run_instance( instance_id = self._run_instance(
image_id='ami-1', image_id='ami-1',
@@ -1004,7 +978,7 @@ class CloudTestCase(test.TestCase):
key = RSA.load_key_string(private_key, callback=lambda: None) key = RSA.load_key_string(private_key, callback=lambda: None)
bio = BIO.MemoryBuffer() bio = BIO.MemoryBuffer()
public_key = db.key_pair_get(self.context, public_key = db.key_pair_get(self.context,
self.context.user.id, self.context.user_id,
'test')['public_key'] 'test')['public_key']
key.save_pub_key_bio(bio) key.save_pub_key_bio(bio)
converted = crypto.ssl_pub_to_ssh_pub(bio.read()) converted = crypto.ssl_pub_to_ssh_pub(bio.read())
@@ -1028,7 +1002,7 @@ class CloudTestCase(test.TestCase):
'mytestfprint') 'mytestfprint')
self.assertTrue(result1) self.assertTrue(result1)
keydata = db.key_pair_get(self.context, keydata = db.key_pair_get(self.context,
self.context.user.id, self.context.user_id,
'testimportkey1') 'testimportkey1')
self.assertEqual('mytestpubkey', keydata['public_key']) self.assertEqual('mytestpubkey', keydata['public_key'])
self.assertEqual('mytestfprint', keydata['fingerprint']) self.assertEqual('mytestfprint', keydata['fingerprint'])
@@ -1045,7 +1019,7 @@ class CloudTestCase(test.TestCase):
dummypub) dummypub)
self.assertTrue(result2) self.assertTrue(result2)
keydata = db.key_pair_get(self.context, keydata = db.key_pair_get(self.context,
self.context.user.id, self.context.user_id,
'testimportkey2') 'testimportkey2')
self.assertEqual(dummypub, keydata['public_key']) self.assertEqual(dummypub, keydata['public_key'])
self.assertEqual(dummyfprint, keydata['fingerprint']) self.assertEqual(dummyfprint, keydata['fingerprint'])

View File

@@ -19,10 +19,6 @@
Tests For Compute Tests For Compute
""" """
import mox
import stubout
from nova.auth import manager
from nova import compute from nova import compute
from nova.compute import instance_types from nova.compute import instance_types
from nova.compute import manager as compute_manager from nova.compute import manager as compute_manager
@@ -67,10 +63,9 @@ class ComputeTestCase(test.TestCase):
network_manager='nova.network.manager.FlatManager') network_manager='nova.network.manager.FlatManager')
self.compute = utils.import_object(FLAGS.compute_manager) self.compute = utils.import_object(FLAGS.compute_manager)
self.compute_api = compute.API() self.compute_api = compute.API()
self.manager = manager.AuthManager() self.user_id = 'fake'
self.user = self.manager.create_user('fake', 'fake', 'fake') self.project_id = 'fake'
self.project = self.manager.create_project('fake', 'fake', 'fake') self.context = context.RequestContext(self.user_id, self.project_id)
self.context = context.RequestContext('fake', 'fake', False)
test_notifier.NOTIFICATIONS = [] test_notifier.NOTIFICATIONS = []
def fake_show(meh, context, id): def fake_show(meh, context, id):
@@ -78,19 +73,14 @@ class ComputeTestCase(test.TestCase):
self.stubs.Set(nova.image.fake._FakeImageService, 'show', fake_show) self.stubs.Set(nova.image.fake._FakeImageService, 'show', fake_show)
def tearDown(self):
self.manager.delete_user(self.user)
self.manager.delete_project(self.project)
super(ComputeTestCase, self).tearDown()
def _create_instance(self, params={}): def _create_instance(self, params={}):
"""Create a test instance""" """Create a test instance"""
inst = {} inst = {}
inst['image_ref'] = 1 inst['image_ref'] = 1
inst['reservation_id'] = 'r-fakeres' inst['reservation_id'] = 'r-fakeres'
inst['launch_time'] = '10' inst['launch_time'] = '10'
inst['user_id'] = self.user.id inst['user_id'] = self.user_id
inst['project_id'] = self.project.id inst['project_id'] = self.project_id
type_id = instance_types.get_instance_type_by_name('m1.tiny')['id'] type_id = instance_types.get_instance_type_by_name('m1.tiny')['id']
inst['instance_type_id'] = type_id inst['instance_type_id'] = type_id
inst['ami_launch_index'] = 0 inst['ami_launch_index'] = 0
@@ -115,8 +105,8 @@ class ComputeTestCase(test.TestCase):
def _create_group(self): def _create_group(self):
values = {'name': 'testgroup', values = {'name': 'testgroup',
'description': 'testgroup', 'description': 'testgroup',
'user_id': self.user.id, 'user_id': self.user_id,
'project_id': self.project.id} 'project_id': self.project_id}
return db.security_group_create(self.context, values) return db.security_group_create(self.context, values)
def _get_dummy_instance(self): def _get_dummy_instance(self):
@@ -350,8 +340,8 @@ class ComputeTestCase(test.TestCase):
self.assertEquals(msg['priority'], 'INFO') self.assertEquals(msg['priority'], 'INFO')
self.assertEquals(msg['event_type'], 'compute.instance.create') self.assertEquals(msg['event_type'], 'compute.instance.create')
payload = msg['payload'] payload = msg['payload']
self.assertEquals(payload['tenant_id'], self.project.id) self.assertEquals(payload['tenant_id'], self.project_id)
self.assertEquals(payload['user_id'], self.user.id) self.assertEquals(payload['user_id'], self.user_id)
self.assertEquals(payload['instance_id'], instance_id) self.assertEquals(payload['instance_id'], instance_id)
self.assertEquals(payload['instance_type'], 'm1.tiny') self.assertEquals(payload['instance_type'], 'm1.tiny')
type_id = instance_types.get_instance_type_by_name('m1.tiny')['id'] type_id = instance_types.get_instance_type_by_name('m1.tiny')['id']
@@ -374,8 +364,8 @@ class ComputeTestCase(test.TestCase):
self.assertEquals(msg['priority'], 'INFO') self.assertEquals(msg['priority'], 'INFO')
self.assertEquals(msg['event_type'], 'compute.instance.delete') self.assertEquals(msg['event_type'], 'compute.instance.delete')
payload = msg['payload'] payload = msg['payload']
self.assertEquals(payload['tenant_id'], self.project.id) self.assertEquals(payload['tenant_id'], self.project_id)
self.assertEquals(payload['user_id'], self.user.id) self.assertEquals(payload['user_id'], self.user_id)
self.assertEquals(payload['instance_id'], instance_id) self.assertEquals(payload['instance_id'], instance_id)
self.assertEquals(payload['instance_type'], 'm1.tiny') self.assertEquals(payload['instance_type'], 'm1.tiny')
type_id = instance_types.get_instance_type_by_name('m1.tiny')['id'] type_id = instance_types.get_instance_type_by_name('m1.tiny')['id']
@@ -420,7 +410,7 @@ class ComputeTestCase(test.TestCase):
def fake(*args, **kwargs): def fake(*args, **kwargs):
pass pass
self.stubs.Set(self.compute.driver, 'finish_resize', fake) self.stubs.Set(self.compute.driver, 'finish_migration', fake)
self.stubs.Set(self.compute.network_api, 'get_instance_nw_info', fake) self.stubs.Set(self.compute.network_api, 'get_instance_nw_info', fake)
context = self.context.elevated() context = self.context.elevated()
instance_id = self._create_instance() instance_id = self._create_instance()
@@ -457,8 +447,8 @@ class ComputeTestCase(test.TestCase):
self.assertEquals(msg['priority'], 'INFO') self.assertEquals(msg['priority'], 'INFO')
self.assertEquals(msg['event_type'], 'compute.instance.resize.prep') self.assertEquals(msg['event_type'], 'compute.instance.resize.prep')
payload = msg['payload'] payload = msg['payload']
self.assertEquals(payload['tenant_id'], self.project.id) self.assertEquals(payload['tenant_id'], self.project_id)
self.assertEquals(payload['user_id'], self.user.id) self.assertEquals(payload['user_id'], self.user_id)
self.assertEquals(payload['instance_id'], instance_id) self.assertEquals(payload['instance_id'], instance_id)
self.assertEquals(payload['instance_type'], 'm1.tiny') self.assertEquals(payload['instance_type'], 'm1.tiny')
type_id = instance_types.get_instance_type_by_name('m1.tiny')['id'] type_id = instance_types.get_instance_type_by_name('m1.tiny')['id']
@@ -531,8 +521,8 @@ class ComputeTestCase(test.TestCase):
def fake(*args, **kwargs): def fake(*args, **kwargs):
pass pass
self.stubs.Set(self.compute.driver, 'finish_resize', fake) self.stubs.Set(self.compute.driver, 'finish_migration', fake)
self.stubs.Set(self.compute.driver, 'revert_resize', fake) self.stubs.Set(self.compute.driver, 'revert_migration', fake)
self.stubs.Set(self.compute.network_api, 'get_instance_nw_info', fake) self.stubs.Set(self.compute.network_api, 'get_instance_nw_info', fake)
self.compute.run_instance(self.context, instance_id) self.compute.run_instance(self.context, instance_id)
@@ -849,7 +839,6 @@ class ComputeTestCase(test.TestCase):
def test_run_kill_vm(self): def test_run_kill_vm(self):
"""Detect when a vm is terminated behind the scenes""" """Detect when a vm is terminated behind the scenes"""
self.stubs = stubout.StubOutForTesting()
self.stubs.Set(compute_manager.ComputeManager, self.stubs.Set(compute_manager.ComputeManager,
'_report_driver_status', nop_report_driver_status) '_report_driver_status', nop_report_driver_status)

View File

@@ -26,10 +26,9 @@ from nova import exception
from nova import flags from nova import flags
from nova import test from nova import test
from nova import utils from nova import utils
from nova.auth import manager
from nova.console import manager as console_manager
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
flags.DECLARE('console_driver', 'nova.console.manager')
class ConsoleTestCase(test.TestCase): class ConsoleTestCase(test.TestCase):
@@ -39,17 +38,11 @@ class ConsoleTestCase(test.TestCase):
self.flags(console_driver='nova.console.fake.FakeConsoleProxy', self.flags(console_driver='nova.console.fake.FakeConsoleProxy',
stub_compute=True) stub_compute=True)
self.console = utils.import_object(FLAGS.console_manager) self.console = utils.import_object(FLAGS.console_manager)
self.manager = manager.AuthManager() self.user_id = 'fake'
self.user = self.manager.create_user('fake', 'fake', 'fake') self.project_id = 'fake'
self.project = self.manager.create_project('fake', 'fake', 'fake') self.context = context.RequestContext(self.user_id, self.project_id)
self.context = context.get_admin_context()
self.host = 'test_compute_host' self.host = 'test_compute_host'
def tearDown(self):
self.manager.delete_user(self.user)
self.manager.delete_project(self.project)
super(ConsoleTestCase, self).tearDown()
def _create_instance(self): def _create_instance(self):
"""Create a test instance""" """Create a test instance"""
inst = {} inst = {}
@@ -58,8 +51,8 @@ class ConsoleTestCase(test.TestCase):
inst['image_id'] = 1 inst['image_id'] = 1
inst['reservation_id'] = 'r-fakeres' inst['reservation_id'] = 'r-fakeres'
inst['launch_time'] = '10' inst['launch_time'] = '10'
inst['user_id'] = self.user.id inst['user_id'] = self.user_id
inst['project_id'] = self.project.id inst['project_id'] = self.project_id
inst['instance_type_id'] = 1 inst['instance_type_id'] = 1
inst['ami_launch_index'] = 0 inst['ami_launch_index'] = 0
return db.instance_create(self.context, inst)['id'] return db.instance_create(self.context, inst)['id']

View File

@@ -22,7 +22,6 @@ from nova import test
from nova import context from nova import context
from nova import db from nova import db
from nova import flags from nova import flags
from nova.auth import manager
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
@@ -45,42 +44,35 @@ def _setup_networking(instance_id, ip='1.2.3.4', flo_addr='1.2.1.2'):
db.fixed_ip_create(ctxt, fixed_ip) db.fixed_ip_create(ctxt, fixed_ip)
fix_ref = db.fixed_ip_get_by_address(ctxt, ip) fix_ref = db.fixed_ip_get_by_address(ctxt, ip)
db.floating_ip_create(ctxt, {'address': flo_addr, db.floating_ip_create(ctxt, {'address': flo_addr,
'fixed_ip_id': fix_ref.id}) 'fixed_ip_id': fix_ref['id']})
class DbApiTestCase(test.TestCase): class DbApiTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(DbApiTestCase, self).setUp() super(DbApiTestCase, self).setUp()
self.manager = manager.AuthManager() self.user_id = 'fake'
self.user = self.manager.create_user('admin', 'admin', 'admin', True) self.project_id = 'fake'
self.project = self.manager.create_project('proj', 'admin', 'proj') self.context = context.RequestContext(self.user_id, self.project_id)
self.context = context.RequestContext(user=self.user,
project=self.project)
def tearDown(self):
self.manager.delete_project(self.project)
self.manager.delete_user(self.user)
super(DbApiTestCase, self).tearDown()
def test_instance_get_project_vpn(self): def test_instance_get_project_vpn(self):
result = db.fixed_ip_get_all(self.context)
values = {'instance_type_id': FLAGS.default_instance_type, values = {'instance_type_id': FLAGS.default_instance_type,
'image_ref': FLAGS.vpn_image_id, 'image_ref': FLAGS.vpn_image_id,
'project_id': self.project.id 'project_id': self.project_id
} }
instance = db.instance_create(self.context, values) instance = db.instance_create(self.context, values)
result = db.instance_get_project_vpn(self.context, self.project.id) result = db.instance_get_project_vpn(self.context.elevated(),
self.assertEqual(instance.id, result.id) self.project_id)
self.assertEqual(instance['id'], result['id'])
def test_instance_get_project_vpn_joins(self): def test_instance_get_project_vpn_joins(self):
result = db.fixed_ip_get_all(self.context)
values = {'instance_type_id': FLAGS.default_instance_type, values = {'instance_type_id': FLAGS.default_instance_type,
'image_ref': FLAGS.vpn_image_id, 'image_ref': FLAGS.vpn_image_id,
'project_id': self.project.id 'project_id': self.project_id
} }
instance = db.instance_create(self.context, values) instance = db.instance_create(self.context, values)
_setup_networking(instance.id) _setup_networking(instance['id'])
result = db.instance_get_project_vpn(self.context, self.project.id) result = db.instance_get_project_vpn(self.context.elevated(),
self.assertEqual(instance.id, result.id) self.project_id)
self.assertEqual(instance['id'], result['id'])
self.assertEqual(result['fixed_ips'][0]['floating_ips'][0].address, self.assertEqual(result['fixed_ips'][0]['floating_ips'][0].address,
'1.2.1.2') '1.2.1.2')

View File

@@ -32,7 +32,6 @@ from nova import flags
from nova import test from nova import test
from nova import utils from nova import utils
from nova.api.ec2 import cloud from nova.api.ec2 import cloud
from nova.auth import manager
from nova.compute import power_state from nova.compute import power_state
from nova.virt.libvirt import connection from nova.virt.libvirt import connection
from nova.virt.libvirt import firewall from nova.virt.libvirt import firewall
@@ -154,36 +153,15 @@ class LibvirtConnTestCase(test.TestCase):
super(LibvirtConnTestCase, self).setUp() super(LibvirtConnTestCase, self).setUp()
connection._late_load_cheetah() connection._late_load_cheetah()
self.flags(fake_call=True) self.flags(fake_call=True)
self.manager = manager.AuthManager() self.user_id = 'fake'
self.project_id = 'fake'
try: self.context = context.RequestContext(self.user_id, self.project_id)
pjs = self.manager.get_projects()
pjs = [p for p in pjs if p.name == 'fake']
if 0 != len(pjs):
self.manager.delete_project(pjs[0])
users = self.manager.get_users()
users = [u for u in users if u.name == 'fake']
if 0 != len(users):
self.manager.delete_user(users[0])
except Exception, e:
pass
users = self.manager.get_users()
self.user = self.manager.create_user('fake', 'fake', 'fake',
admin=True)
self.project = self.manager.create_project('fake', 'fake', 'fake')
self.network = utils.import_object(FLAGS.network_manager) self.network = utils.import_object(FLAGS.network_manager)
self.context = context.get_admin_context() self.context = context.get_admin_context()
FLAGS.instances_path = '' FLAGS.instances_path = ''
self.call_libvirt_dependant_setup = False self.call_libvirt_dependant_setup = False
self.test_ip = '10.11.12.13' self.test_ip = '10.11.12.13'
def tearDown(self):
self.manager.delete_project(self.project)
self.manager.delete_user(self.user)
super(LibvirtConnTestCase, self).tearDown()
test_instance = {'memory_kb': '1024000', test_instance = {'memory_kb': '1024000',
'basepath': '/some/path', 'basepath': '/some/path',
'bridge_name': 'br100', 'bridge_name': 'br100',
@@ -239,7 +217,7 @@ class LibvirtConnTestCase(test.TestCase):
'mac_address': 'fake', 'mac_address': 'fake',
'ip_address': 'fake', 'ip_address': 'fake',
'dhcp_server': 'fake', 'dhcp_server': 'fake',
'extra_params': 'fake' 'extra_params': 'fake',
} }
# Creating mocks # Creating mocks
@@ -441,8 +419,8 @@ class LibvirtConnTestCase(test.TestCase):
self.assertEquals(parameters[1].get('value'), 'fake') self.assertEquals(parameters[1].get('value'), 'fake')
def _check_xml_and_container(self, instance): def _check_xml_and_container(self, instance):
user_context = context.RequestContext(project=self.project, user_context = context.RequestContext(self.user_id,
user=self.user) self.project_id)
instance_ref = db.instance_create(user_context, instance) instance_ref = db.instance_create(user_context, instance)
_setup_networking(instance_ref['id'], self.test_ip) _setup_networking(instance_ref['id'], self.test_ip)
@@ -470,11 +448,10 @@ class LibvirtConnTestCase(test.TestCase):
def _check_xml_and_uri(self, instance, expect_ramdisk, expect_kernel, def _check_xml_and_uri(self, instance, expect_ramdisk, expect_kernel,
rescue=False): rescue=False):
user_context = context.RequestContext(project=self.project, user_context = context.RequestContext(self.user_id, self.project_id)
user=self.user)
instance_ref = db.instance_create(user_context, instance) instance_ref = db.instance_create(user_context, instance)
network_ref = db.project_get_networks(context.get_admin_context(), network_ref = db.project_get_networks(context.get_admin_context(),
self.project.id)[0] self.project_id)[0]
_setup_networking(instance_ref['id'], self.test_ip) _setup_networking(instance_ref['id'], self.test_ip)
@@ -802,11 +779,9 @@ class IptablesFirewallTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(IptablesFirewallTestCase, self).setUp() super(IptablesFirewallTestCase, self).setUp()
self.manager = manager.AuthManager() self.user_id = 'fake'
self.user = self.manager.create_user('fake', 'fake', 'fake', self.project_id = 'fake'
admin=True) self.context = context.RequestContext(self.user_id, self.project_id)
self.project = self.manager.create_project('fake', 'fake', 'fake')
self.context = context.RequestContext('fake', 'fake')
self.network = utils.import_object(FLAGS.network_manager) self.network = utils.import_object(FLAGS.network_manager)
class FakeLibvirtConnection(object): class FakeLibvirtConnection(object):
@@ -832,11 +807,6 @@ class IptablesFirewallTestCase(test.TestCase):
connection.libxml2 = __import__('libxml2') connection.libxml2 = __import__('libxml2')
return True return True
def tearDown(self):
self.manager.delete_project(self.project)
self.manager.delete_user(self.user)
super(IptablesFirewallTestCase, self).tearDown()
in_nat_rules = [ in_nat_rules = [
'# Generated by iptables-save v1.4.10 on Sat Feb 19 00:03:19 2011', '# Generated by iptables-save v1.4.10 on Sat Feb 19 00:03:19 2011',
'*nat', '*nat',
@@ -1119,11 +1089,9 @@ class NWFilterTestCase(test.TestCase):
class Mock(object): class Mock(object):
pass pass
self.manager = manager.AuthManager() self.user_id = 'fake'
self.user = self.manager.create_user('fake', 'fake', 'fake', self.project_id = 'fake'
admin=True) self.context = context.RequestContext(self.user_id, self.project_id)
self.project = self.manager.create_project('fake', 'fake', 'fake')
self.context = context.RequestContext(self.user, self.project)
self.fake_libvirt_connection = Mock() self.fake_libvirt_connection = Mock()
@@ -1131,11 +1099,6 @@ class NWFilterTestCase(test.TestCase):
self.fw = firewall.NWFilterFirewall( self.fw = firewall.NWFilterFirewall(
lambda: self.fake_libvirt_connection) lambda: self.fake_libvirt_connection)
def tearDown(self):
self.manager.delete_project(self.project)
self.manager.delete_user(self.user)
super(NWFilterTestCase, self).tearDown()
def test_cidr_rule_nwfilter_xml(self): def test_cidr_rule_nwfilter_xml(self):
cloud_controller = cloud.CloudController() cloud_controller = cloud.CloudController()
cloud_controller.create_security_group(self.context, cloud_controller.create_security_group(self.context,

View File

@@ -33,11 +33,12 @@ LOG = logging.getLogger('nova.tests.rpc')
class RpcTestCase(test.TestCase): class RpcTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(RpcTestCase, self).setUp() super(RpcTestCase, self).setUp()
self.conn = rpc.Connection.instance(True) self.conn = rpc.create_connection(True)
self.receiver = TestReceiver() self.receiver = TestReceiver()
self.consumer = rpc.TopicAdapterConsumer(connection=self.conn, self.consumer = rpc.create_consumer(self.conn,
topic='test', 'test',
proxy=self.receiver) self.receiver,
False)
self.consumer.attach_to_eventlet() self.consumer.attach_to_eventlet()
self.context = context.get_admin_context() self.context = context.get_admin_context()
@@ -129,6 +130,8 @@ class RpcTestCase(test.TestCase):
"""Calls echo in the passed queue""" """Calls echo in the passed queue"""
LOG.debug(_("Nested received %(queue)s, %(value)s") LOG.debug(_("Nested received %(queue)s, %(value)s")
% locals()) % locals())
# TODO: so, it will replay the context and use the same REQID?
# that's bizarre.
ret = rpc.call(context, ret = rpc.call(context,
queue, queue,
{"method": "echo", {"method": "echo",
@@ -137,10 +140,11 @@ class RpcTestCase(test.TestCase):
return value return value
nested = Nested() nested = Nested()
conn = rpc.Connection.instance(True) conn = rpc.create_connection(True)
consumer = rpc.TopicAdapterConsumer(connection=conn, consumer = rpc.create_consumer(conn,
topic='nested', 'nested',
proxy=nested) nested,
False)
consumer.attach_to_eventlet() consumer.attach_to_eventlet()
value = 42 value = 42
result = rpc.call(self.context, result = rpc.call(self.context,
@@ -149,47 +153,6 @@ class RpcTestCase(test.TestCase):
"value": value}}) "value": value}})
self.assertEqual(value, result) self.assertEqual(value, result)
def test_connectionpool_single(self):
"""Test that ConnectionPool recycles a single connection."""
conn1 = rpc.ConnectionPool.get()
rpc.ConnectionPool.put(conn1)
conn2 = rpc.ConnectionPool.get()
rpc.ConnectionPool.put(conn2)
self.assertEqual(conn1, conn2)
def test_connectionpool_double(self):
"""Test that ConnectionPool returns and reuses separate connections.
When called consecutively we should get separate connections and upon
returning them those connections should be reused for future calls
before generating a new connection.
"""
conn1 = rpc.ConnectionPool.get()
conn2 = rpc.ConnectionPool.get()
self.assertNotEqual(conn1, conn2)
rpc.ConnectionPool.put(conn1)
rpc.ConnectionPool.put(conn2)
conn3 = rpc.ConnectionPool.get()
conn4 = rpc.ConnectionPool.get()
self.assertEqual(conn1, conn3)
self.assertEqual(conn2, conn4)
def test_connectionpool_limit(self):
"""Test connection pool limit and connection uniqueness."""
max_size = FLAGS.rpc_conn_pool_size
conns = []
for i in xrange(max_size):
conns.append(rpc.ConnectionPool.get())
self.assertFalse(rpc.ConnectionPool.free_items)
self.assertEqual(rpc.ConnectionPool.current_size,
rpc.ConnectionPool.max_size)
self.assertEqual(len(set(conns)), max_size)
class TestReceiver(object): class TestReceiver(object):
"""Simple Proxy class so the consumer has methods to call. """Simple Proxy class so the consumer has methods to call.

View File

@@ -0,0 +1,68 @@
from nova import context
from nova import flags
from nova import log as logging
from nova import rpc
from nova.rpc import amqp
from nova import test
FLAGS = flags.FLAGS
LOG = logging.getLogger('nova.tests.rpc')
class RpcAMQPTestCase(test.TestCase):
def setUp(self):
super(RpcAMQPTestCase, self).setUp()
self.conn = rpc.create_connection(True)
self.receiver = TestReceiver()
self.consumer = rpc.create_consumer(self.conn,
'test',
self.receiver,
False)
self.consumer.attach_to_eventlet()
self.context = context.get_admin_context()
def test_connectionpool_single(self):
"""Test that ConnectionPool recycles a single connection."""
conn1 = amqp.ConnectionPool.get()
amqp.ConnectionPool.put(conn1)
conn2 = amqp.ConnectionPool.get()
amqp.ConnectionPool.put(conn2)
self.assertEqual(conn1, conn2)
class TestReceiver(object):
"""Simple Proxy class so the consumer has methods to call.
Uses static methods because we aren't actually storing any state.
"""
@staticmethod
def echo(context, value):
"""Simply returns whatever value is sent in."""
LOG.debug(_("Received %s"), value)
return value
@staticmethod
def context(context, value):
"""Returns dictionary version of context."""
LOG.debug(_("Received %s"), context)
return context.to_dict()
@staticmethod
def echo_three_times(context, value):
context.reply(value)
context.reply(value + 1)
context.reply(value + 2)
@staticmethod
def echo_three_times_yield(context, value):
yield value
yield value + 1
yield value + 2
@staticmethod
def fail(context, value):
"""Raises an exception with the value sent in."""
raise Exception(value)

View File

@@ -19,11 +19,11 @@
Test suite for VMWareAPI. Test suite for VMWareAPI.
""" """
from nova import context
from nova import db from nova import db
from nova import flags from nova import flags
from nova import test from nova import test
from nova import utils from nova import utils
from nova.auth import manager
from nova.compute import power_state from nova.compute import power_state
from nova.tests.glance import stubs as glance_stubs from nova.tests.glance import stubs as glance_stubs
from nova.tests.vmwareapi import db_fakes from nova.tests.vmwareapi import db_fakes
@@ -43,10 +43,9 @@ class VMWareAPIVMTestCase(test.TestCase):
self.flags(vmwareapi_host_ip='test_url', self.flags(vmwareapi_host_ip='test_url',
vmwareapi_host_username='test_username', vmwareapi_host_username='test_username',
vmwareapi_host_password='test_pass') vmwareapi_host_password='test_pass')
self.manager = manager.AuthManager() self.user_id = 'fake'
self.user = self.manager.create_user('fake', 'fake', 'fake', self.project_id = 'fake'
admin=True) self.context = context.RequestContext(self.user_id, self.project_id)
self.project = self.manager.create_project('fake', 'fake', 'fake')
self.network = utils.import_object(FLAGS.network_manager) self.network = utils.import_object(FLAGS.network_manager)
vmwareapi_fake.reset() vmwareapi_fake.reset()
db_fakes.stub_out_db_instance_api(self.stubs) db_fakes.stub_out_db_instance_api(self.stubs)
@@ -77,14 +76,12 @@ class VMWareAPIVMTestCase(test.TestCase):
def tearDown(self): def tearDown(self):
super(VMWareAPIVMTestCase, self).tearDown() super(VMWareAPIVMTestCase, self).tearDown()
vmwareapi_fake.cleanup() vmwareapi_fake.cleanup()
self.manager.delete_project(self.project)
self.manager.delete_user(self.user)
def _create_instance_in_the_db(self): def _create_instance_in_the_db(self):
values = {'name': 1, values = {'name': 1,
'id': 1, 'id': 1,
'project_id': self.project.id, 'project_id': self.project_id,
'user_id': self.user.id, 'user_id': self.user_id,
'image_ref': "1", 'image_ref': "1",
'kernel_id': "1", 'kernel_id': "1",
'ramdisk_id': "1", 'ramdisk_id': "1",

View File

@@ -30,7 +30,6 @@ from nova import flags
from nova import log as logging from nova import log as logging
from nova import test from nova import test
from nova import utils from nova import utils
from nova.auth import manager
from nova.compute import instance_types from nova.compute import instance_types
from nova.compute import power_state from nova.compute import power_state
from nova import exception from nova import exception
@@ -69,7 +68,9 @@ class XenAPIVolumeTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(XenAPIVolumeTestCase, self).setUp() super(XenAPIVolumeTestCase, self).setUp()
self.stubs = stubout.StubOutForTesting() self.stubs = stubout.StubOutForTesting()
self.context = context.RequestContext('fake', 'fake', False) self.user_id = 'fake'
self.project_id = 'fake'
self.context = context.RequestContext(self.user_id, self.project_id)
FLAGS.target_host = '127.0.0.1' FLAGS.target_host = '127.0.0.1'
FLAGS.xenapi_connection_url = 'test_url' FLAGS.xenapi_connection_url = 'test_url'
FLAGS.xenapi_connection_password = 'test_pass' FLAGS.xenapi_connection_password = 'test_pass'
@@ -77,7 +78,7 @@ class XenAPIVolumeTestCase(test.TestCase):
stubs.stub_out_get_target(self.stubs) stubs.stub_out_get_target(self.stubs)
xenapi_fake.reset() xenapi_fake.reset()
self.values = {'id': 1, self.values = {'id': 1,
'project_id': 'fake', 'project_id': self.user_id,
'user_id': 'fake', 'user_id': 'fake',
'image_ref': 1, 'image_ref': 1,
'kernel_id': 2, 'kernel_id': 2,
@@ -173,10 +174,6 @@ class XenAPIVMTestCase(test.TestCase):
"""Unit tests for VM operations.""" """Unit tests for VM operations."""
def setUp(self): def setUp(self):
super(XenAPIVMTestCase, self).setUp() super(XenAPIVMTestCase, self).setUp()
self.manager = manager.AuthManager()
self.user = self.manager.create_user('fake', 'fake', 'fake',
admin=True)
self.project = self.manager.create_project('fake', 'fake', 'fake')
self.network = utils.import_object(FLAGS.network_manager) self.network = utils.import_object(FLAGS.network_manager)
self.stubs = stubout.StubOutForTesting() self.stubs = stubout.StubOutForTesting()
self.flags(xenapi_connection_url='test_url', self.flags(xenapi_connection_url='test_url',
@@ -195,7 +192,9 @@ class XenAPIVMTestCase(test.TestCase):
stubs.stub_out_vm_methods(self.stubs) stubs.stub_out_vm_methods(self.stubs)
glance_stubs.stubout_glance_client(self.stubs) glance_stubs.stubout_glance_client(self.stubs)
fake_utils.stub_out_utils_execute(self.stubs) fake_utils.stub_out_utils_execute(self.stubs)
self.context = context.RequestContext('fake', 'fake', False) self.user_id = 'fake'
self.project_id = 'fake'
self.context = context.RequestContext(self.user_id, self.project_id)
self.conn = xenapi_conn.get_connection(False) self.conn = xenapi_conn.get_connection(False)
def test_parallel_builds(self): def test_parallel_builds(self):
@@ -229,8 +228,8 @@ class XenAPIVMTestCase(test.TestCase):
instance = db.instance_create(self.context, values) instance = db.instance_create(self.context, values)
self.conn.spawn(instance, network_info) self.conn.spawn(instance, network_info)
gt1 = eventlet.spawn(_do_build, 1, self.project.id, self.user.id) gt1 = eventlet.spawn(_do_build, 1, self.project_id, self.user_id)
gt2 = eventlet.spawn(_do_build, 2, self.project.id, self.user.id) gt2 = eventlet.spawn(_do_build, 2, self.project_id, self.user_id)
gt1.wait() gt1.wait()
gt2.wait() gt2.wait()
@@ -401,8 +400,8 @@ class XenAPIVMTestCase(test.TestCase):
stubs.stubout_loopingcall_start(self.stubs) stubs.stubout_loopingcall_start(self.stubs)
if create_record: if create_record:
values = {'id': instance_id, values = {'id': instance_id,
'project_id': self.project.id, 'project_id': self.project_id,
'user_id': self.user.id, 'user_id': self.user_id,
'image_ref': image_ref, 'image_ref': image_ref,
'kernel_id': kernel_id, 'kernel_id': kernel_id,
'ramdisk_id': ramdisk_id, 'ramdisk_id': ramdisk_id,
@@ -469,12 +468,30 @@ class XenAPIVMTestCase(test.TestCase):
self._check_vdis(vdi_recs_start, vdi_recs_end) self._check_vdis(vdi_recs_start, vdi_recs_end)
def test_spawn_raw_objectstore(self): def test_spawn_raw_objectstore(self):
FLAGS.xenapi_image_service = 'objectstore' # TODO(vish): deprecated
self._test_spawn(1, None, None) from nova.auth import manager
authman = manager.AuthManager()
authman.create_user('fake', 'fake')
authman.create_project('fake', 'fake')
try:
FLAGS.xenapi_image_service = 'objectstore'
self._test_spawn(1, None, None)
finally:
authman.delete_project('fake')
authman.delete_user('fake')
def test_spawn_objectstore(self): def test_spawn_objectstore(self):
FLAGS.xenapi_image_service = 'objectstore' # TODO(vish): deprecated
self._test_spawn(1, 2, 3) from nova.auth import manager
authman = manager.AuthManager()
authman.create_user('fake', 'fake')
authman.create_project('fake', 'fake')
try:
FLAGS.xenapi_image_service = 'objectstore'
self._test_spawn(1, 2, 3)
finally:
authman.delete_project('fake')
authman.delete_user('fake')
@stub_vm_utils_with_vdi_attached_here @stub_vm_utils_with_vdi_attached_here
def test_spawn_raw_glance(self): def test_spawn_raw_glance(self):
@@ -626,7 +643,7 @@ class XenAPIVMTestCase(test.TestCase):
host=FLAGS.host, host=FLAGS.host,
vpn=None, vpn=None,
instance_type_id=1, instance_type_id=1,
project_id=self.project.id) project_id=self.project_id)
self._test_spawn(glance_stubs.FakeGlance.IMAGE_MACHINE, self._test_spawn(glance_stubs.FakeGlance.IMAGE_MACHINE,
glance_stubs.FakeGlance.IMAGE_KERNEL, glance_stubs.FakeGlance.IMAGE_KERNEL,
glance_stubs.FakeGlance.IMAGE_RAMDISK, glance_stubs.FakeGlance.IMAGE_RAMDISK,
@@ -656,21 +673,13 @@ class XenAPIVMTestCase(test.TestCase):
# Ensure that it will not unrescue a non-rescued instance. # Ensure that it will not unrescue a non-rescued instance.
self.assertRaises(Exception, conn.unrescue, instance, None) self.assertRaises(Exception, conn.unrescue, instance, None)
def tearDown(self):
super(XenAPIVMTestCase, self).tearDown()
self.manager.delete_project(self.project)
self.manager.delete_user(self.user)
self.vm_info = None
self.vm = None
self.stubs.UnsetAll()
def _create_instance(self, instance_id=1, spawn=True): def _create_instance(self, instance_id=1, spawn=True):
"""Creates and spawns a test instance.""" """Creates and spawns a test instance."""
stubs.stubout_loopingcall_start(self.stubs) stubs.stubout_loopingcall_start(self.stubs)
values = { values = {
'id': instance_id, 'id': instance_id,
'project_id': self.project.id, 'project_id': self.project_id,
'user_id': self.user.id, 'user_id': self.user_id,
'image_ref': 1, 'image_ref': 1,
'kernel_id': 2, 'kernel_id': 2,
'ramdisk_id': 3, 'ramdisk_id': 3,
@@ -752,14 +761,12 @@ class XenAPIMigrateInstance(test.TestCase):
stubs.stub_out_get_target(self.stubs) stubs.stub_out_get_target(self.stubs)
xenapi_fake.reset() xenapi_fake.reset()
xenapi_fake.create_network('fake', FLAGS.flat_network_bridge) xenapi_fake.create_network('fake', FLAGS.flat_network_bridge)
self.manager = manager.AuthManager() self.user_id = 'fake'
self.user = self.manager.create_user('fake', 'fake', 'fake', self.project_id = 'fake'
admin=True) self.context = context.RequestContext(self.user_id, self.project_id)
self.project = self.manager.create_project('fake', 'fake', 'fake')
self.context = context.RequestContext('fake', 'fake', False)
self.values = {'id': 1, self.values = {'id': 1,
'project_id': self.project.id, 'project_id': self.project_id,
'user_id': self.user.id, 'user_id': self.user_id,
'image_ref': 1, 'image_ref': 1,
'kernel_id': None, 'kernel_id': None,
'ramdisk_id': None, 'ramdisk_id': None,
@@ -773,20 +780,21 @@ class XenAPIMigrateInstance(test.TestCase):
stubs.stubout_get_this_vm_uuid(self.stubs) stubs.stubout_get_this_vm_uuid(self.stubs)
glance_stubs.stubout_glance_client(self.stubs) glance_stubs.stubout_glance_client(self.stubs)
def tearDown(self):
super(XenAPIMigrateInstance, self).tearDown()
self.manager.delete_project(self.project)
self.manager.delete_user(self.user)
self.stubs.UnsetAll()
def test_migrate_disk_and_power_off(self): def test_migrate_disk_and_power_off(self):
instance = db.instance_create(self.context, self.values) instance = db.instance_create(self.context, self.values)
stubs.stubout_session(self.stubs, stubs.FakeSessionForMigrationTests) stubs.stubout_session(self.stubs, stubs.FakeSessionForMigrationTests)
conn = xenapi_conn.get_connection(False) conn = xenapi_conn.get_connection(False)
conn.migrate_disk_and_power_off(instance, '127.0.0.1') conn.migrate_disk_and_power_off(instance, '127.0.0.1')
def test_finish_resize(self): def test_finish_migrate(self):
instance = db.instance_create(self.context, self.values) instance = db.instance_create(self.context, self.values)
self.called = False
def fake_vdi_resize(*args, **kwargs):
self.called = True
self.stubs.Set(stubs.FakeSessionForMigrationTests,
"VDI_resize_online", fake_vdi_resize)
stubs.stubout_session(self.stubs, stubs.FakeSessionForMigrationTests) stubs.stubout_session(self.stubs, stubs.FakeSessionForMigrationTests)
stubs.stubout_loopingcall_start(self.stubs) stubs.stubout_loopingcall_start(self.stubs)
conn = xenapi_conn.get_connection(False) conn = xenapi_conn.get_connection(False)
@@ -804,8 +812,86 @@ class XenAPIMigrateInstance(test.TestCase):
'label': 'fake', 'label': 'fake',
'mac': 'DE:AD:BE:EF:00:00', 'mac': 'DE:AD:BE:EF:00:00',
'rxtx_cap': 3})] 'rxtx_cap': 3})]
conn.finish_resize(instance, dict(base_copy='hurr', cow='durr'), conn.finish_migration(instance, dict(base_copy='hurr', cow='durr'),
network_info) network_info, resize_instance=True)
self.assertEqual(self.called, True)
def test_finish_migrate_no_local_storage(self):
tiny_type_id = \
instance_types.get_instance_type_by_name('m1.tiny')['id']
self.values.update({'instance_type_id': tiny_type_id, 'local_gb': 0})
instance = db.instance_create(self.context, self.values)
def fake_vdi_resize(*args, **kwargs):
raise Exception("This shouldn't be called")
self.stubs.Set(stubs.FakeSessionForMigrationTests,
"VDI_resize_online", fake_vdi_resize)
stubs.stubout_session(self.stubs, stubs.FakeSessionForMigrationTests)
stubs.stubout_loopingcall_start(self.stubs)
conn = xenapi_conn.get_connection(False)
network_info = [({'bridge': 'fa0', 'id': 0, 'injected': False},
{'broadcast': '192.168.0.255',
'dns': ['192.168.0.1'],
'gateway': '192.168.0.1',
'gateway6': 'dead:beef::1',
'ip6s': [{'enabled': '1',
'ip': 'dead:beef::dcad:beff:feef:0',
'netmask': '64'}],
'ips': [{'enabled': '1',
'ip': '192.168.0.100',
'netmask': '255.255.255.0'}],
'label': 'fake',
'mac': 'DE:AD:BE:EF:00:00',
'rxtx_cap': 3})]
conn.finish_migration(instance, dict(base_copy='hurr', cow='durr'),
network_info, resize_instance=True)
def test_finish_migrate_no_resize_vdi(self):
instance = db.instance_create(self.context, self.values)
def fake_vdi_resize(*args, **kwargs):
raise Exception("This shouldn't be called")
self.stubs.Set(stubs.FakeSessionForMigrationTests,
"VDI_resize_online", fake_vdi_resize)
stubs.stubout_session(self.stubs, stubs.FakeSessionForMigrationTests)
stubs.stubout_loopingcall_start(self.stubs)
conn = xenapi_conn.get_connection(False)
network_info = [({'bridge': 'fa0', 'id': 0, 'injected': False},
{'broadcast': '192.168.0.255',
'dns': ['192.168.0.1'],
'gateway': '192.168.0.1',
'gateway6': 'dead:beef::1',
'ip6s': [{'enabled': '1',
'ip': 'dead:beef::dcad:beff:feef:0',
'netmask': '64'}],
'ips': [{'enabled': '1',
'ip': '192.168.0.100',
'netmask': '255.255.255.0'}],
'label': 'fake',
'mac': 'DE:AD:BE:EF:00:00',
'rxtx_cap': 3})]
# Resize instance would be determined by the compute call
conn.finish_migration(instance, dict(base_copy='hurr', cow='durr'),
network_info, resize_instance=False)
class XenAPIImageTypeTestCase(test.TestCase):
"""Test ImageType class."""
def test_to_string(self):
"""Can convert from type id to type string."""
self.assertEquals(
vm_utils.ImageType.to_string(vm_utils.ImageType.KERNEL),
vm_utils.ImageType.KERNEL_STR)
def test_from_string(self):
"""Can convert from string to type id."""
self.assertEquals(
vm_utils.ImageType.from_string(vm_utils.ImageType.KERNEL_STR),
vm_utils.ImageType.KERNEL)
class XenAPIDetermineDiskImageTestCase(test.TestCase): class XenAPIDetermineDiskImageTestCase(test.TestCase):

View File

@@ -19,7 +19,6 @@
"""Utilities and helper functions.""" """Utilities and helper functions."""
import base64
import datetime import datetime
import functools import functools
import inspect import inspect
@@ -30,7 +29,6 @@ import os
import random import random
import re import re
import socket import socket
import string
import struct import struct
import sys import sys
import time import time
@@ -50,7 +48,8 @@ from nova import version
LOG = logging.getLogger("nova.utils") LOG = logging.getLogger("nova.utils")
TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" ISO_TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
PERFECT_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%f"
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
@@ -361,16 +360,26 @@ def clear_time_override():
utcnow.override_time = None utcnow.override_time = None
def isotime(at=None): def strtime(at=None, fmt=PERFECT_TIME_FORMAT):
"""Returns iso formatted utcnow.""" """Returns formatted utcnow."""
if not at: if not at:
at = utcnow() at = utcnow()
return at.strftime(TIME_FORMAT) return at.strftime(fmt)
def parse_strtime(timestr, fmt=PERFECT_TIME_FORMAT):
"""Turn a formatted time back into a datetime."""
return datetime.datetime.strptime(timestr, fmt)
def isotime(at=None):
"""Returns iso formatted utcnow."""
return strtime(at, ISO_TIME_FORMAT)
def parse_isotime(timestr): def parse_isotime(timestr):
"""Turn an iso formatted time back into a datetime.""" """Turn an iso formatted time back into a datetime."""
return datetime.datetime.strptime(timestr, TIME_FORMAT) return parse_strtime(timestr, ISO_TIME_FORMAT)
def parse_mailmap(mailmap='.mailmap'): def parse_mailmap(mailmap='.mailmap'):

View File

@@ -274,6 +274,18 @@ class Middleware(Application):
return self.process_response(response) return self.process_response(response)
class InjectContext(Middleware):
"""Add a 'nova.context' to WSGI environ."""
def __init__(self, context, *args, **kwargs):
self.context = context
super(InjectContext, self).__init__(*args, **kwargs)
@webob.dec.wsgify(RequestClass=Request)
def __call__(self, req):
req.environ['nova.context'] = self.context
return self.application
class Debug(Middleware): class Debug(Middleware):
"""Helper class for debugging a WSGI application. """Helper class for debugging a WSGI application.