Update a few doc strings.

Address a few pep8 issues.
Add nova.tests.utils which provides a couple of handy methods for testing stuff.
This commit is contained in:
Soren Hansen 2011-08-22 23:26:12 +02:00
parent b24a05dbc1
commit 150098011d
4 changed files with 132 additions and 21 deletions

View File

@ -0,0 +1,41 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2010 OpenStack LLC
#
# 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 import db
from nova import test
from nova.tests import utils as test_utils
class TestUtilsTestCase(test.TestCase):
def test_get_test_admin_context(self):
"""get_test_admin_context's return value behaves like admin context"""
ctxt = test_utils.get_test_admin_context()
# TODO(soren): This should verify the full interface context
# objects expose.
self.assertTrue(ctxt.is_admin)
def test_get_test_instance(self):
"""get_test_instance's return value looks like an instance_ref"""
instance_ref = test_utils.get_test_instance()
ctxt = test_utils.get_test_admin_context()
db.instance_get(ctxt, instance_ref['id'])
def _test_get_test_network_info(self):
"""Does the return value match a real network_info structure"""
# The challenge here is to define what exactly such a structure
# must look like.
pass

View File

@ -31,6 +31,7 @@ FLAGS = flags.FLAGS
LOG = logging.getLogger('nova.tests.test_virt_drivers')
def catch_notimplementederror(f):
"""Decorator to simplify catching drivers raising NotImplementedError
@ -50,6 +51,7 @@ def catch_notimplementederror(f):
wrapped_func.__doc__ = f.__doc__
return wrapped_func
class _VirtDriverTestCase(test.TestCase):
def setUp(self):
super(_VirtDriverTestCase, self).setUp()
@ -151,14 +153,14 @@ class _VirtDriverTestCase(test.TestCase):
network_info = test_utils.get_test_network_info()
self.connection.spawn(self.ctxt, instance_ref, network_info)
self.connection.rescue(self.ctxt, instance_ref,
lambda x:None, network_info)
lambda x: None, network_info)
@catch_notimplementederror
def test_unrescue_unrescued_instance(self):
instance_ref = test_utils.get_test_instance()
network_info = test_utils.get_test_network_info()
self.connection.spawn(self.ctxt, instance_ref, network_info)
self.connection.unrescue(instance_ref, lambda x:None, network_info)
self.connection.unrescue(instance_ref, lambda x: None, network_info)
@catch_notimplementederror
def test_unrescue_rescued_instance(self):
@ -166,8 +168,8 @@ class _VirtDriverTestCase(test.TestCase):
network_info = test_utils.get_test_network_info()
self.connection.spawn(self.ctxt, instance_ref, network_info)
self.connection.rescue(self.ctxt, instance_ref,
lambda x:None, network_info)
self.connection.unrescue(instance_ref, lambda x:None, network_info)
lambda x: None, network_info)
self.connection.unrescue(instance_ref, lambda x: None, network_info)
@catch_notimplementederror
def test_poll_rescued_instances(self):
@ -226,7 +228,7 @@ class _VirtDriverTestCase(test.TestCase):
@catch_notimplementederror
def test_destroy_instance_nonexistant(self):
fake_instance = { 'id': 42, 'name': 'I just made this up!' }
fake_instance = {'id': 42, 'name': 'I just made this up!'}
network_info = test_utils.get_test_network_info()
self.connection.destroy(fake_instance, network_info)
@ -410,7 +412,6 @@ class _VirtDriverTestCase(test.TestCase):
network_info = test_utils.get_test_network_info()
self.connection.unfilter_instance(instance_ref, network_info)
@catch_notimplementederror
def test_live_migration(self):
network_info = test_utils.get_test_network_info()
@ -448,29 +449,34 @@ class _VirtDriverTestCase(test.TestCase):
@catch_notimplementederror
def test_set_host_enabled(self):
self.connection.set_host_enabled('Am I a useless argument?', True)
self.connection.set_host_enabled('a useless argument?', True)
@catch_notimplementederror
def test_host_power_action_reboot(self):
self.connection.host_power_action('Am I a useless argument?', 'reboot')
self.connection.host_power_action('a useless argument?', 'reboot')
@catch_notimplementederror
def test_host_power_action_shutdown(self):
self.connection.host_power_action('Am I a useless argument?', 'shutdown')
self.connection.host_power_action('a useless argument?', 'shutdown')
@catch_notimplementederror
def test_host_power_action_startup(self):
self.connection.host_power_action('Am I a useless argument?', 'startup')
self.connection.host_power_action('a useless argument?', 'startup')
class AbstractDriverTestCase(_VirtDriverTestCase):
def setUp(self):
import nova.virt.driver
self.driver_module = nova.virt.driver
def get_driver_connection(_):
return nova.virt.driver.ComputeDriver()
self.driver_module.get_connection = get_driver_connection
super(AbstractDriverTestCase, self).setUp()
class FakeConnectionTestCase(_VirtDriverTestCase):
def setUp(self):
import nova.virt.fake

68
nova/tests/utils.py Normal file
View File

@ -0,0 +1,68 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2011 OpenStack LLC
#
# 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
#
import nova.context
import nova.db
import nova.flags
FLAGS = nova.flags.FLAGS
def get_test_admin_context():
return nova.context.get_admin_context()
def get_test_instance(context=None):
if not context:
context = get_test_admin_context()
test_instance = {'memory_kb': '1024000',
'basepath': '/some/path',
'bridge_name': 'br100',
'vcpus': 2,
'project_id': 'fake',
'bridge': 'br101',
'image_ref': '1',
'instance_type_id': '5'} # m1.small
instance_ref = nova.db.instance_create(context, test_instance)
return instance_ref
def get_test_network_info(count=1):
ipv6 = FLAGS.use_ipv6
fake = 'fake'
fake_ip = '0.0.0.0/0'
fake_ip_2 = '0.0.0.1/0'
fake_ip_3 = '0.0.0.1/0'
fake_vlan = 100
fake_bridge_interface = 'eth0'
network = {'bridge': fake,
'cidr': fake_ip,
'cidr_v6': fake_ip,
'vlan': fake_vlan,
'bridge_interface': fake_bridge_interface,
'injected': False}
mapping = {'mac': fake,
'dhcp_server': fake,
'gateway': fake,
'gateway6': fake,
'ips': [{'ip': fake_ip}, {'ip': fake_ip}]}
if ipv6:
mapping['ip6s'] = [{'ip': fake_ip},
{'ip': fake_ip_2},
{'ip': fake_ip_3}]
return [(network, mapping) for x in xrange(0, count)]

View File

@ -140,7 +140,7 @@ class ComputeDriver(object):
that it was before this call began.
:param context: security context
:param instance: Instance of {nova.compute.service.Instance}.
:param instance: Instance object as returned by DB layer.
This function should use the data there to guide
the creation of the new instance.
:param network_info:
@ -152,14 +152,11 @@ class ComputeDriver(object):
def destroy(self, instance, network_info, cleanup=True):
"""Destroy (shutdown and delete) the specified instance.
The given parameter is an instance of nova.compute.service.Instance,
If the instance is not found (for example if networking failed), this
function should still succeed. It's probably a good idea to log a
warning in that case.
:param instance: Instance of {nova.compute.service.Instance} and so
the instance is being specified as instance.name.
:param instance: Instance object as returned by DB layer.
:param network_info:
:py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
:param cleanup:
@ -171,8 +168,7 @@ class ComputeDriver(object):
def reboot(self, instance, network_info):
"""Reboot the specified instance.
:param instance: Instance of {nova.compute.service.Instance} and so
the instance is being specified as instance.name.
:param instance: Instance object as returned by DB layer.
:param network_info:
:py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
"""
@ -240,10 +236,10 @@ class ComputeDriver(object):
"""
Snapshots the specified instance.
The given parameter is an instance of nova.compute.service.Instance,
and so the instance is being specified as instance.name.
The second parameter is the name of the snapshot.
:param context: security context
:param instance: Instance object as returned by DB layer.
:param image_id: Reference to a pre-created image that will
hold the snapshot.
"""
raise NotImplementedError()