switch FLAGS.* = in tests to self.flags(...)

remove unused cases of FLAGS from tests
modified test.TestCase's flags() to allow multiple overrides
added missing license to test_rpc_amqp.py
This commit is contained in:
Chris Behrens
2011-08-03 12:22:58 -07:00
parent 37a51f83e6
commit ece2f4f15f
9 changed files with 46 additions and 46 deletions

View File

@@ -142,11 +142,9 @@ class TestCase(unittest.TestCase):
def flags(self, **kw): def flags(self, **kw):
"""Override flag variables for a test.""" """Override flag variables for a test."""
for k, v in kw.iteritems(): for k, v in kw.iteritems():
if k in self.flag_overrides: # Store original flag value if it's not been overriden yet
self.reset_flags() if k not in self.flag_overrides:
raise Exception( self.flag_overrides[k] = getattr(FLAGS, k)
'trying to override already overriden flag: %s' % k)
self.flag_overrides[k] = getattr(FLAGS, k)
setattr(FLAGS, k, v) setattr(FLAGS, k, v)
def reset_flags(self): def reset_flags(self):

View File

@@ -21,13 +21,9 @@ import random
from nova import context from nova import context
from nova import db from nova import db
from nova import flags
from nova import test from nova import test
from nova.virt import hyperv from nova.virt import hyperv
FLAGS = flags.FLAGS
FLAGS.connection_type = 'hyperv'
class HyperVTestCase(test.TestCase): class HyperVTestCase(test.TestCase):
"""Test cases for the Hyper-V driver""" """Test cases for the Hyper-V driver"""
@@ -36,6 +32,7 @@ class HyperVTestCase(test.TestCase):
self.user_id = 'fake' self.user_id = 'fake'
self.project_id = 'fake' self.project_id = 'fake'
self.context = context.RequestContext(self.user_id, self.project_id) self.context = context.RequestContext(self.user_id, self.project_id)
self.flags(connection_type='hyperv')
def test_create_destroy(self): def test_create_destroy(self):
"""Create a VM and destroy it""" """Create a VM and destroy it"""

View File

@@ -83,9 +83,9 @@ class user_and_project_generator(object):
class _AuthManagerBaseTestCase(test.TestCase): class _AuthManagerBaseTestCase(test.TestCase):
def setUp(self): def setUp(self):
FLAGS.auth_driver = self.auth_driver
super(_AuthManagerBaseTestCase, self).setUp() super(_AuthManagerBaseTestCase, self).setUp()
self.flags(connection_type='fake') self.flags(auth_driver=self.auth_driver,
connection_type='fake')
self.manager = manager.AuthManager(new=True) self.manager = manager.AuthManager(new=True)
self.manager.mc.cache = {} self.manager.mc.cache = {}

View File

@@ -19,12 +19,9 @@ Tests For Scheduler Host Filters.
import json import json
from nova import exception from nova import exception
from nova import flags
from nova import test from nova import test
from nova.scheduler import host_filter from nova.scheduler import host_filter
FLAGS = flags.FLAGS
class FakeZoneManager: class FakeZoneManager:
pass pass
@@ -57,9 +54,9 @@ class HostFilterTestCase(test.TestCase):
'host_name-label': 'xs-%s' % multiplier} 'host_name-label': 'xs-%s' % multiplier}
def setUp(self): def setUp(self):
self.old_flag = FLAGS.default_host_filter super(HostFilterTestCase, self).setUp()
FLAGS.default_host_filter = \ default_host_filter = 'nova.scheduler.host_filter.AllHostsFilter'
'nova.scheduler.host_filter.AllHostsFilter' self.flags(default_host_filter=default_host_filter)
self.instance_type = dict(name='tiny', self.instance_type = dict(name='tiny',
memory_mb=50, memory_mb=50,
vcpus=10, vcpus=10,
@@ -76,9 +73,6 @@ class HostFilterTestCase(test.TestCase):
states['host%02d' % (x + 1)] = {'compute': self._host_caps(x)} states['host%02d' % (x + 1)] = {'compute': self._host_caps(x)}
self.zone_manager.service_states = states self.zone_manager.service_states = states
def tearDown(self):
FLAGS.default_host_filter = self.old_flag
def test_choose_filter(self): def test_choose_filter(self):
# Test default filter ... # Test default filter ...
hf = host_filter.choose_host_filter() hf = host_filter.choose_host_filter()

View File

@@ -38,7 +38,6 @@ from nova.virt.libvirt import firewall
libvirt = None libvirt = None
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
flags.DECLARE('instances_path', 'nova.compute.manager')
def _concurrency(wait, done, target): def _concurrency(wait, done, target):
@@ -93,6 +92,7 @@ def _setup_networking(instance_id, ip='1.2.3.4'):
class CacheConcurrencyTestCase(test.TestCase): class CacheConcurrencyTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(CacheConcurrencyTestCase, self).setUp() super(CacheConcurrencyTestCase, self).setUp()
self.flags(instances_path='nova.compute.manager')
def fake_exists(fname): def fake_exists(fname):
basedir = os.path.join(FLAGS.instances_path, '_base') basedir = os.path.join(FLAGS.instances_path, '_base')
@@ -158,7 +158,7 @@ class LibvirtConnTestCase(test.TestCase):
self.context = context.RequestContext(self.user_id, self.project_id) self.context = context.RequestContext(self.user_id, self.project_id)
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 = '' self.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'
@@ -322,7 +322,7 @@ class LibvirtConnTestCase(test.TestCase):
if not self.lazy_load_library_exists(): if not self.lazy_load_library_exists():
return return
FLAGS.image_service = 'nova.image.fake.FakeImageService' self.flags(image_service='nova.image.fake.FakeImageService')
# Start test # Start test
image_service = utils.import_object(FLAGS.image_service) image_service = utils.import_object(FLAGS.image_service)
@@ -357,7 +357,7 @@ class LibvirtConnTestCase(test.TestCase):
if not self.lazy_load_library_exists(): if not self.lazy_load_library_exists():
return return
FLAGS.image_service = 'nova.image.fake.FakeImageService' self.flags(image_service='nova.image.fake.FakeImageService')
# Start test # Start test
image_service = utils.import_object(FLAGS.image_service) image_service = utils.import_object(FLAGS.image_service)
@@ -521,7 +521,7 @@ class LibvirtConnTestCase(test.TestCase):
'disk.local')] 'disk.local')]
for (libvirt_type, (expected_uri, checks)) in type_uri_map.iteritems(): for (libvirt_type, (expected_uri, checks)) in type_uri_map.iteritems():
FLAGS.libvirt_type = libvirt_type self.flags(libvirt_type=libvirt_type)
conn = connection.LibvirtConnection(True) conn = connection.LibvirtConnection(True)
uri = conn.get_uri() uri = conn.get_uri()
@@ -546,9 +546,9 @@ class LibvirtConnTestCase(test.TestCase):
# checking against that later on. This way we make sure the # checking against that later on. This way we make sure the
# implementation doesn't fiddle around with the FLAGS. # implementation doesn't fiddle around with the FLAGS.
testuri = 'something completely different' testuri = 'something completely different'
FLAGS.libvirt_uri = testuri self.flags(libvirt_uri=testuri)
for (libvirt_type, (expected_uri, checks)) in type_uri_map.iteritems(): for (libvirt_type, (expected_uri, checks)) in type_uri_map.iteritems():
FLAGS.libvirt_type = libvirt_type self.flags(libvirt_type=libvirt_type)
conn = connection.LibvirtConnection(True) conn = connection.LibvirtConnection(True)
uri = conn.get_uri() uri = conn.get_uri()
self.assertEquals(uri, testuri) self.assertEquals(uri, testuri)
@@ -556,8 +556,7 @@ class LibvirtConnTestCase(test.TestCase):
def test_update_available_resource_works_correctly(self): def test_update_available_resource_works_correctly(self):
"""Confirm compute_node table is updated successfully.""" """Confirm compute_node table is updated successfully."""
org_path = FLAGS.instances_path = '' self.flags(instances_path='.')
FLAGS.instances_path = '.'
# Prepare mocks # Prepare mocks
def getVersion(): def getVersion():
@@ -604,12 +603,10 @@ class LibvirtConnTestCase(test.TestCase):
self.assertTrue(compute_node['hypervisor_version'] > 0) self.assertTrue(compute_node['hypervisor_version'] > 0)
db.service_destroy(self.context, service_ref['id']) db.service_destroy(self.context, service_ref['id'])
FLAGS.instances_path = org_path
def test_update_resource_info_no_compute_record_found(self): def test_update_resource_info_no_compute_record_found(self):
"""Raise exception if no recorde found on services table.""" """Raise exception if no recorde found on services table."""
org_path = FLAGS.instances_path = '' self.flags(instances_path='.')
FLAGS.instances_path = '.'
self.create_fake_libvirt_mock() self.create_fake_libvirt_mock()
self.mox.ReplayAll() self.mox.ReplayAll()
@@ -618,8 +615,6 @@ class LibvirtConnTestCase(test.TestCase):
conn.update_available_resource, conn.update_available_resource,
self.context, 'dummy') self.context, 'dummy')
FLAGS.instances_path = org_path
def test_ensure_filtering_rules_for_instance_timeout(self): def test_ensure_filtering_rules_for_instance_timeout(self):
"""ensure_filtering_fules_for_instance() finishes with timeout.""" """ensure_filtering_fules_for_instance() finishes with timeout."""
# Skip if non-libvirt environment # Skip if non-libvirt environment

View File

@@ -17,7 +17,6 @@
from nova import db from nova import db
from nova import exception from nova import exception
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.network import manager as network_manager from nova.network import manager as network_manager
@@ -26,7 +25,6 @@ from nova.network import manager as network_manager
import mox import mox
FLAGS = flags.FLAGS
LOG = logging.getLogger('nova.tests.network') LOG = logging.getLogger('nova.tests.network')

View File

@@ -20,13 +20,11 @@ Unit Tests for remote procedure calls using queue
""" """
from nova import context from nova import context
from nova import flags
from nova import log as logging from nova import log as logging
from nova import rpc from nova import rpc
from nova import test from nova import test
FLAGS = flags.FLAGS
LOG = logging.getLogger('nova.tests.rpc') LOG = logging.getLogger('nova.tests.rpc')

View File

@@ -1,12 +1,32 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (c) 2010 Openstack, LLC.
# 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.
"""
Tests For RPC AMQP.
"""
from nova import context from nova import context
from nova import flags
from nova import log as logging from nova import log as logging
from nova import rpc from nova import rpc
from nova.rpc import amqp from nova.rpc import amqp
from nova import test from nova import test
FLAGS = flags.FLAGS
LOG = logging.getLogger('nova.tests.rpc') LOG = logging.getLogger('nova.tests.rpc')

View File

@@ -71,9 +71,9 @@ class XenAPIVolumeTestCase(test.TestCase):
self.user_id = 'fake' self.user_id = 'fake'
self.project_id = 'fake' self.project_id = 'fake'
self.context = context.RequestContext(self.user_id, self.project_id) self.context = context.RequestContext(self.user_id, self.project_id)
FLAGS.target_host = '127.0.0.1' self.flags(target_host='127.0.0.1',
FLAGS.xenapi_connection_url = 'test_url' xenapi_connection_url='test_url',
FLAGS.xenapi_connection_password = 'test_pass' xenapi_connection_password='test_pass')
db_fakes.stub_out_db_instance_api(self.stubs) db_fakes.stub_out_db_instance_api(self.stubs)
stubs.stub_out_get_target(self.stubs) stubs.stub_out_get_target(self.stubs)
xenapi_fake.reset() xenapi_fake.reset()
@@ -719,9 +719,9 @@ class XenAPIMigrateInstance(test.TestCase):
def setUp(self): def setUp(self):
super(XenAPIMigrateInstance, self).setUp() super(XenAPIMigrateInstance, self).setUp()
self.stubs = stubout.StubOutForTesting() self.stubs = stubout.StubOutForTesting()
FLAGS.target_host = '127.0.0.1' self.flags(target_host='127.0.0.1',
FLAGS.xenapi_connection_url = 'test_url' xenapi_connection_url='test_url',
FLAGS.xenapi_connection_password = 'test_pass' xenapi_connection_password='test_pass')
db_fakes.stub_out_db_instance_api(self.stubs) db_fakes.stub_out_db_instance_api(self.stubs)
stubs.stub_out_get_target(self.stubs) stubs.stub_out_get_target(self.stubs)
xenapi_fake.reset() xenapi_fake.reset()