diff options
| author | Andy Smith <code@term.ie> | 2010-10-15 22:04:14 +0000 |
|---|---|---|
| committer | Tarmac <> | 2010-10-15 22:04:14 +0000 |
| commit | c0f050c54fad7fd714402b592b97cdc1caf8a8b7 (patch) | |
| tree | fd2817c3bf103b7b620b31ab1dcce191f9f88f51 | |
| parent | 3b34b9f64c2f27cdc6203f8d5571246a92aa4386 (diff) | |
| parent | 8b2d1143f53bb029941a770e2611bb58a064c492 (diff) | |
| download | nova-c0f050c54fad7fd714402b592b97cdc1caf8a8b7.tar.gz nova-c0f050c54fad7fd714402b592b97cdc1caf8a8b7.tar.xz nova-c0f050c54fad7fd714402b592b97cdc1caf8a8b7.zip | |
Fix the --help flag for printing help on twistd-based services
| -rw-r--r-- | nova/flags.py | 6 | ||||
| -rw-r--r-- | nova/test.py | 60 | ||||
| -rw-r--r-- | nova/tests/flags_unittest.py | 12 | ||||
| -rw-r--r-- | nova/tests/twistd_unittest.py | 53 | ||||
| -rw-r--r-- | nova/twistd.py | 6 | ||||
| -rw-r--r-- | run_tests.py | 1 |
6 files changed, 107 insertions, 31 deletions
diff --git a/nova/flags.py b/nova/flags.py index ab80e83fb..3b473488f 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -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) diff --git a/nova/test.py b/nova/test.py index b9ea36e1d..8e89eb43e 100644 --- a/nova/test.py +++ b/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 - - if FLAGS.fake_rabbit: - fakerabbit.reset_all() - db.security_group_destroy_all(ctxt) - - super(TrialTestCase, self).tearDown() + 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) + 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, diff --git a/nova/tests/flags_unittest.py b/nova/tests/flags_unittest.py index d49d5dc43..714170e5e 100644 --- a/nova/tests/flags_unittest.py +++ b/nova/tests/flags_unittest.py @@ -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') diff --git a/nova/tests/twistd_unittest.py b/nova/tests/twistd_unittest.py new file mode 100644 index 000000000..75007b9c8 --- /dev/null +++ b/nova/tests/twistd_unittest.py @@ -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()) diff --git a/nova/twistd.py b/nova/twistd.py index 9511c231c..df75b603e 100644 --- a/nova/twistd.py +++ b/nova/twistd.py @@ -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]), diff --git a/run_tests.py b/run_tests.py index b1a3f1d66..9a2f40dc9 100644 --- a/run_tests.py +++ b/run_tests.py @@ -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 * |
