Fix the --help flag for printing help on twistd-based services
This commit is contained in:
@@ -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)
|
||||
|
@@ -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 *
|
||||
|
Reference in New Issue
Block a user