addressed termies review (first round)

This commit is contained in:
Armando Migliaccio
2011-03-28 10:54:29 +01:00
parent b886d08530
commit c8f45462ac
2 changed files with 18 additions and 125 deletions

View File

@@ -1,106 +0,0 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (c) 2011 Citrix Systems, Inc.
#
# 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.
"""This modules stubs out functions in nova.utils
"""
import re
import types
from eventlet import greenthread
from nova import exception
from nova import log as logging
from nova import utils
LOG = logging.getLogger('nova.tests.fake_utils')
_fake_execute_repliers = []
_fake_execute_log = []
def fake_execute_get_log():
global _fake_execute_log
return _fake_execute_log
def fake_execute_clear_log():
global _fake_execute_log
_fake_execute_log = []
def fake_execute_set_repliers(repliers):
"""Allows the client to configure replies to commands"""
global _fake_execute_repliers
_fake_execute_repliers = repliers
def fake_execute_default_reply_handler(*ignore_args, **ignore_kwargs):
"""A reply handler for commands that haven't been added to the reply
list. Returns empty strings for stdout and stderr
"""
return '', ''
def fake_execute(*cmd, **kwargs):
"""This function stubs out execute, optionally executing
a preconfigued function to return expected data
"""
global _fake_execute_repliers
process_input = kwargs.get('process_input', None)
addl_env = kwargs.get('addl_env', None)
check_exit_code = kwargs.get('check_exit_code', 0)
cmd_map = map(str, cmd)
cmd_str = ' '.join(cmd_map)
LOG.debug(_("Faking execution of cmd (subprocess): %s"), cmd_str)
_fake_execute_log.append(cmd_str)
reply_handler = fake_execute_default_reply_handler
for fake_replier in _fake_execute_repliers:
if re.match(fake_replier[0], cmd_str):
reply_handler = fake_replier[1]
LOG.debug(_('Faked command matched %s') % fake_replier[0])
break
if isinstance(reply_handler, types.StringTypes):
# If the reply handler is a string, return it as stdout
reply = reply_handler, ''
else:
try:
# Alternative is a function, so call it
reply = reply_handler(cmd,
process_input=process_input,
addl_env=addl_env,
check_exit_code=check_exit_code)
except exception.ProcessExecutionError as e:
LOG.debug(_('Faked command raised an exception %s' % str(e)))
raise
LOG.debug(_("Reply to faked command is stdout='%(0)s' stderr='%(1)s'") %
{'0': reply[0], '1': reply[1]})
# Replicate the sleep call in the real function
greenthread.sleep(0)
return reply
def stub_out_utils_execute(stubs):
fake_execute_set_repliers([])
fake_execute_clear_log()
stubs.Set(utils, 'execute', fake_execute)

View File

@@ -15,7 +15,7 @@
# under the License. # under the License.
""" """
Test suite for XenAPI Test suite for XenAPI.
""" """
import functools import functools
@@ -66,7 +66,7 @@ def stub_vm_utils_with_vdi_attached_here(function, should_return=True):
class XenAPIVolumeTestCase(test.TestCase): class XenAPIVolumeTestCase(test.TestCase):
""" """
Unit tests for Volume operations Unit tests for Volume operations.
""" """
def setUp(self): def setUp(self):
super(XenAPIVolumeTestCase, self).setUp() super(XenAPIVolumeTestCase, self).setUp()
@@ -76,7 +76,6 @@ class XenAPIVolumeTestCase(test.TestCase):
FLAGS.xenapi_connection_url = 'test_url' FLAGS.xenapi_connection_url = 'test_url'
FLAGS.xenapi_connection_password = 'test_pass' FLAGS.xenapi_connection_password = 'test_pass'
db_fakes.stub_out_db_instance_api(self.stubs) db_fakes.stub_out_db_instance_api(self.stubs)
#db_fakes.stub_out_db_network_api(self.stubs)
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,
@@ -102,7 +101,7 @@ class XenAPIVolumeTestCase(test.TestCase):
return db.volume_create(self.context, vol) return db.volume_create(self.context, vol)
def test_create_iscsi_storage(self): def test_create_iscsi_storage(self):
""" This shows how to test helper classes' methods """ """This shows how to test helper classes' methods."""
stubs.stubout_session(self.stubs, stubs.FakeSessionForVolumeTests) stubs.stubout_session(self.stubs, stubs.FakeSessionForVolumeTests)
session = xenapi_conn.XenAPISession('test_url', 'root', 'test_pass') session = xenapi_conn.XenAPISession('test_url', 'root', 'test_pass')
helper = volume_utils.VolumeHelper helper = volume_utils.VolumeHelper
@@ -117,7 +116,7 @@ class XenAPIVolumeTestCase(test.TestCase):
db.volume_destroy(context.get_admin_context(), vol['id']) db.volume_destroy(context.get_admin_context(), vol['id'])
def test_parse_volume_info_raise_exception(self): def test_parse_volume_info_raise_exception(self):
""" This shows how to test helper classes' methods """ """This shows how to test helper classes' methods."""
stubs.stubout_session(self.stubs, stubs.FakeSessionForVolumeTests) stubs.stubout_session(self.stubs, stubs.FakeSessionForVolumeTests)
session = xenapi_conn.XenAPISession('test_url', 'root', 'test_pass') session = xenapi_conn.XenAPISession('test_url', 'root', 'test_pass')
helper = volume_utils.VolumeHelper helper = volume_utils.VolumeHelper
@@ -131,7 +130,7 @@ class XenAPIVolumeTestCase(test.TestCase):
db.volume_destroy(context.get_admin_context(), vol['id']) db.volume_destroy(context.get_admin_context(), vol['id'])
def test_attach_volume(self): def test_attach_volume(self):
""" This shows how to test Ops classes' methods """ """This shows how to test Ops classes' methods."""
stubs.stubout_session(self.stubs, stubs.FakeSessionForVolumeTests) stubs.stubout_session(self.stubs, stubs.FakeSessionForVolumeTests)
conn = xenapi_conn.get_connection(False) conn = xenapi_conn.get_connection(False)
volume = self._create_volume() volume = self._create_volume()
@@ -150,7 +149,7 @@ class XenAPIVolumeTestCase(test.TestCase):
check() check()
def test_attach_volume_raise_exception(self): def test_attach_volume_raise_exception(self):
""" This shows how to test when exceptions are raised """ """This shows how to test when exceptions are raised."""
stubs.stubout_session(self.stubs, stubs.stubout_session(self.stubs,
stubs.FakeSessionForVolumeFailedTests) stubs.FakeSessionForVolumeFailedTests)
conn = xenapi_conn.get_connection(False) conn = xenapi_conn.get_connection(False)
@@ -174,7 +173,7 @@ def reset_network(*args):
class XenAPIVMTestCase(test.TestCase): 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()
@@ -475,21 +474,21 @@ class XenAPIVMTestCase(test.TestCase):
network_manager='nova.network.manager.VlanManager', network_manager='nova.network.manager.VlanManager',
network_driver='nova.network.xenapi_net', network_driver='nova.network.xenapi_net',
vlan_interface='fake0') vlan_interface='fake0')
#reset network table # Reset network table
xenapi_fake.reset_table('network') xenapi_fake.reset_table('network')
#instance id = 2 will use vlan network (see db/fakes.py) # Instance id = 2 will use vlan network (see db/fakes.py)
fake_instance_id = 2 fake_instance_id = 2
network_bk = self.network network_bk = self.network
#ensure we use xenapi_net driver # Ensure we use xenapi_net driver
self.network = utils.import_object(FLAGS.network_manager) self.network = utils.import_object(FLAGS.network_manager)
self.network.setup_compute_network(None, fake_instance_id) self.network.setup_compute_network(None, fake_instance_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,
instance_id=fake_instance_id) instance_id=fake_instance_id)
#TODO(salvatore-orlando): a complete test here would require # TODO(salvatore-orlando): a complete test here would require
#a check for making sure the bridge for the VM's VIF is # A check for making sure the bridge for the VM's VIF is
#consistent with bridge specified in nova db # consistent with bridge specified in nova db
self.network = network_bk self.network = network_bk
def test_spawn_with_network_qos(self): def test_spawn_with_network_qos(self):
@@ -521,7 +520,7 @@ class XenAPIVMTestCase(test.TestCase):
self.stubs.UnsetAll() self.stubs.UnsetAll()
def _create_instance(self): def _create_instance(self):
"""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': 1, 'id': 1,
@@ -540,7 +539,7 @@ class XenAPIVMTestCase(test.TestCase):
class XenAPIDiffieHellmanTestCase(test.TestCase): class XenAPIDiffieHellmanTestCase(test.TestCase):
""" """
Unit tests for Diffie-Hellman code Unit tests for Diffie-Hellman code.
""" """
def setUp(self): def setUp(self):
super(XenAPIDiffieHellmanTestCase, self).setUp() super(XenAPIDiffieHellmanTestCase, self).setUp()
@@ -566,7 +565,7 @@ class XenAPIDiffieHellmanTestCase(test.TestCase):
class XenAPIMigrateInstance(test.TestCase): class XenAPIMigrateInstance(test.TestCase):
""" """
Unit test for verifying migration-related actions Unit test for verifying migration-related actions.
""" """
def setUp(self): def setUp(self):
@@ -623,7 +622,7 @@ class XenAPIMigrateInstance(test.TestCase):
class XenAPIDetermineDiskImageTestCase(test.TestCase): class XenAPIDetermineDiskImageTestCase(test.TestCase):
""" """
Unit tests for code that detects the ImageType Unit tests for code that detects the ImageType.
""" """
def setUp(self): def setUp(self):
super(XenAPIDetermineDiskImageTestCase, self).setUp() super(XenAPIDetermineDiskImageTestCase, self).setUp()
@@ -644,7 +643,7 @@ class XenAPIDetermineDiskImageTestCase(test.TestCase):
def test_instance_disk(self): def test_instance_disk(self):
""" """
If a kernel is specified then the image type is DISK (aka machine) If a kernel is specified then the image type is DISK (aka machine).
""" """
FLAGS.xenapi_image_service = 'objectstore' FLAGS.xenapi_image_service = 'objectstore'
self.fake_instance.image_id = glance_stubs.FakeGlance.IMAGE_MACHINE self.fake_instance.image_id = glance_stubs.FakeGlance.IMAGE_MACHINE