Fix the --help flag for printing help on twistd-based services
This commit is contained in:
commit
c0f050c54f
@ -90,6 +90,12 @@ class FlagValues(gflags.FlagValues):
|
||||
self.ClearDirty()
|
||||
return args
|
||||
|
||||
def Reset(self):
|
||||
gflags.FlagValues.Reset(self)
|
||||
self.__dict__['__dirty'] = []
|
||||
self.__dict__['__was_already_parsed'] = False
|
||||
self.__dict__['__stored_argv'] = []
|
||||
|
||||
def SetDirty(self, name):
|
||||
"""Mark a flag as dirty so that accessing it will case a reparse."""
|
||||
self.__dict__['__dirty'].append(name)
|
||||
|
56
nova/test.py
56
nova/test.py
@ -22,9 +22,9 @@ Allows overriding of flags for use of fakes,
|
||||
and some black magic for inline callbacks.
|
||||
"""
|
||||
|
||||
import datetime
|
||||
import sys
|
||||
import time
|
||||
import datetime
|
||||
|
||||
import mox
|
||||
import stubout
|
||||
@ -79,31 +79,34 @@ class TrialTestCase(unittest.TestCase):
|
||||
self.stubs = stubout.StubOutForTesting()
|
||||
self.flag_overrides = {}
|
||||
self.injected = []
|
||||
self._monkeyPatchAttach()
|
||||
self._monkey_patch_attach()
|
||||
self._original_flags = FLAGS.FlagValuesDict()
|
||||
|
||||
def tearDown(self): # pylint: disable-msg=C0103
|
||||
"""Runs after each test method to finalize/tear down test environment"""
|
||||
self.reset_flags()
|
||||
self.mox.UnsetStubs()
|
||||
self.stubs.UnsetAll()
|
||||
self.stubs.SmartUnsetAll()
|
||||
self.mox.VerifyAll()
|
||||
# NOTE(vish): Clean up any ips associated during the test.
|
||||
ctxt = context.get_admin_context()
|
||||
db.fixed_ip_disassociate_all_by_timeout(ctxt, FLAGS.host, self.start)
|
||||
db.network_disassociate_all(ctxt)
|
||||
rpc.Consumer.attach_to_twisted = self.originalAttach
|
||||
for x in self.injected:
|
||||
try:
|
||||
x.stop()
|
||||
except AssertionError:
|
||||
pass
|
||||
try:
|
||||
self.mox.UnsetStubs()
|
||||
self.stubs.UnsetAll()
|
||||
self.stubs.SmartUnsetAll()
|
||||
self.mox.VerifyAll()
|
||||
# NOTE(vish): Clean up any ips associated during the test.
|
||||
ctxt = context.get_admin_context()
|
||||
db.fixed_ip_disassociate_all_by_timeout(ctxt, FLAGS.host, self.start)
|
||||
db.network_disassociate_all(ctxt)
|
||||
rpc.Consumer.attach_to_twisted = self.originalAttach
|
||||
for x in self.injected:
|
||||
try:
|
||||
x.stop()
|
||||
except AssertionError:
|
||||
pass
|
||||
|
||||
if FLAGS.fake_rabbit:
|
||||
fakerabbit.reset_all()
|
||||
db.security_group_destroy_all(ctxt)
|
||||
if FLAGS.fake_rabbit:
|
||||
fakerabbit.reset_all()
|
||||
|
||||
super(TrialTestCase, self).tearDown()
|
||||
db.security_group_destroy_all(ctxt)
|
||||
super(TrialTestCase, self).tearDown()
|
||||
finally:
|
||||
self.reset_flags()
|
||||
|
||||
def flags(self, **kw):
|
||||
"""Override flag variables for a test"""
|
||||
@ -117,7 +120,8 @@ class TrialTestCase(unittest.TestCase):
|
||||
|
||||
def reset_flags(self):
|
||||
"""Resets all flag variables for the test. Runs after each test"""
|
||||
for k, v in self.flag_overrides.iteritems():
|
||||
FLAGS.Reset()
|
||||
for k, v in self._original_flags.iteritems():
|
||||
setattr(FLAGS, k, v)
|
||||
|
||||
def run(self, result=None):
|
||||
@ -143,7 +147,7 @@ class TrialTestCase(unittest.TestCase):
|
||||
_wrapped.func_name = func.func_name
|
||||
return _wrapped
|
||||
|
||||
def _monkeyPatchAttach(self):
|
||||
def _monkey_patch_attach(self):
|
||||
self.originalAttach = rpc.Consumer.attach_to_twisted
|
||||
def _wrapped(innerSelf):
|
||||
rv = self.originalAttach(innerSelf)
|
||||
@ -171,12 +175,6 @@ class BaseTestCase(TrialTestCase):
|
||||
self._done_waiting = False
|
||||
self._timed_out = False
|
||||
|
||||
def tearDown(self):# pylint: disable-msg=C0103
|
||||
"""Runs after each test method to finalize/tear down test environment"""
|
||||
super(BaseTestCase, self).tearDown()
|
||||
if FLAGS.fake_rabbit:
|
||||
fakerabbit.reset_all()
|
||||
|
||||
def _wait_for_test(self, timeout=60):
|
||||
""" Push the ioloop along to wait for our test to complete. """
|
||||
self._waiting = self.ioloop.add_timeout(time.time() + timeout,
|
||||
|
@ -20,6 +20,8 @@ from nova import exception
|
||||
from nova import flags
|
||||
from nova import test
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
flags.DEFINE_string('flags_unittest', 'foo', 'for testing purposes only')
|
||||
|
||||
class FlagsTestCase(test.TrialTestCase):
|
||||
def setUp(self):
|
||||
@ -85,3 +87,13 @@ class FlagsTestCase(test.TrialTestCase):
|
||||
|
||||
self.assert_('runtime_answer' in self.global_FLAGS)
|
||||
self.assertEqual(self.global_FLAGS.runtime_answer, 60)
|
||||
|
||||
def test_flag_leak_left(self):
|
||||
self.assertEqual(FLAGS.flags_unittest, 'foo')
|
||||
FLAGS.flags_unittest = 'bar'
|
||||
self.assertEqual(FLAGS.flags_unittest, 'bar')
|
||||
|
||||
def test_flag_leak_right(self):
|
||||
self.assertEqual(FLAGS.flags_unittest, 'foo')
|
||||
FLAGS.flags_unittest = 'bar'
|
||||
self.assertEqual(FLAGS.flags_unittest, 'bar')
|
||||
|
53
nova/tests/twistd_unittest.py
Normal file
53
nova/tests/twistd_unittest.py
Normal file
@ -0,0 +1,53 @@
|
||||
# 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.
|
||||
|
||||
import StringIO
|
||||
import sys
|
||||
|
||||
from nova import twistd
|
||||
from nova import exception
|
||||
from nova import flags
|
||||
from nova import test
|
||||
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
|
||||
|
||||
class TwistdTestCase(test.TrialTestCase):
|
||||
def setUp(self):
|
||||
super(TwistdTestCase, self).setUp()
|
||||
self.Options = twistd.WrapTwistedOptions(twistd.TwistdServerOptions)
|
||||
sys.stdout = StringIO.StringIO()
|
||||
|
||||
def tearDown(self):
|
||||
super(TwistdTestCase, self).tearDown()
|
||||
sys.stdout = sys.__stdout__
|
||||
|
||||
def test_basic(self):
|
||||
options = self.Options()
|
||||
argv = options.parseOptions()
|
||||
|
||||
def test_logfile(self):
|
||||
options = self.Options()
|
||||
argv = options.parseOptions(['--logfile=foo'])
|
||||
self.assertEqual(FLAGS.logfile, 'foo')
|
||||
|
||||
def test_help(self):
|
||||
options = self.Options()
|
||||
self.assertRaises(SystemExit, options.parseOptions, ['--help'])
|
||||
self.assert_('pidfile' in sys.stdout.getvalue())
|
@ -51,6 +51,8 @@ class TwistdServerOptions(ServerOptions):
|
||||
|
||||
|
||||
class FlagParser(object):
|
||||
# this is a required attribute for gflags
|
||||
syntactic_help = ''
|
||||
def __init__(self, parser):
|
||||
self.parser = parser
|
||||
|
||||
@ -81,6 +83,8 @@ def WrapTwistedOptions(wrapped):
|
||||
reflect.accumulateClassList(self.__class__, 'optFlags', twistd_flags)
|
||||
for flag in twistd_flags:
|
||||
key = flag[0].replace('-', '_')
|
||||
if hasattr(FLAGS, key):
|
||||
continue
|
||||
flags.DEFINE_boolean(key, None, str(flag[-1]))
|
||||
|
||||
def _absorbParameters(self):
|
||||
@ -88,6 +92,8 @@ def WrapTwistedOptions(wrapped):
|
||||
reflect.accumulateClassList(self.__class__, 'optParameters', twistd_params)
|
||||
for param in twistd_params:
|
||||
key = param[0].replace('-', '_')
|
||||
if hasattr(FLAGS, key):
|
||||
continue
|
||||
if len(param) > 4:
|
||||
flags.DEFINE(FlagParser(param[4]),
|
||||
key, param[2], str(param[3]),
|
||||
|
@ -61,6 +61,7 @@ from nova.tests.quota_unittest import *
|
||||
from nova.tests.rpc_unittest import *
|
||||
from nova.tests.scheduler_unittest import *
|
||||
from nova.tests.service_unittest import *
|
||||
from nova.tests.twistd_unittest import *
|
||||
from nova.tests.validator_unittest import *
|
||||
from nova.tests.virt_unittest import *
|
||||
from nova.tests.volume_unittest import *
|
||||
|
Loading…
Reference in New Issue
Block a user