From 4ed4aecf7cb5ab231736908e1829a9ce4869b0c7 Mon Sep 17 00:00:00 2001 From: xiexs Date: Mon, 9 Nov 2015 04:22:00 -0500 Subject: [PATCH] Fix the bug of "openstack console log show" The behaviors are inconsistent while different negative line numbers specified. Change-Id: I2573f3e789f5603c896758971830ffc0b94c5e2b Closes-Bug: #1512263 --- openstackclient/common/parseractions.py | 15 +++++ .../tests/common/test_parseractions.py | 55 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/openstackclient/common/parseractions.py b/openstackclient/common/parseractions.py index 8f6008e..fd90369 100644 --- a/openstackclient/common/parseractions.py +++ b/openstackclient/common/parseractions.py @@ -65,3 +65,18 @@ class RangeAction(argparse.Action): # Too many values msg = "Invalid range, too many values" raise argparse.ArgumentError(self, msg) + + +class NonNegativeAction(argparse.Action): + """A custom action to check whether the value is non-negative or not + + Ensures the value is >= 0. + """ + + def __call__(self, parser, namespace, values, option_string=None): + try: + assert(int(values) >= 0) + setattr(namespace, self.dest, values) + except Exception: + msg = "%s expected a non-negative integer" % (str(option_string)) + raise argparse.ArgumentTypeError(self, msg) diff --git a/openstackclient/tests/common/test_parseractions.py b/openstackclient/tests/common/test_parseractions.py index 8afcb63..b75c481 100644 --- a/openstackclient/tests/common/test_parseractions.py +++ b/openstackclient/tests/common/test_parseractions.py @@ -102,3 +102,58 @@ class TestKeyValueAction(utils.TestCase): expect = {'green': '100%'} self.assertDictEqual(expect, actual) self.assertEqual(None, failhere) + + +class TestNonNegativeAction(utils.TestCase): + def test_negative_values(self): + parser = argparse.ArgumentParser() + + # Set up our typical usage + parser.add_argument( + '--foo', + metavar='', + type=int, + action=parseractions.NonNegativeAction, + ) + + self.assertRaises( + argparse.ArgumentTypeError, + parser.parse_args, + "--foo -1".split() + ) + + def test_zero_values(self): + parser = argparse.ArgumentParser() + + # Set up our typical usage + parser.add_argument( + '--foo', + metavar='', + type=int, + action=parseractions.NonNegativeAction, + ) + + results = parser.parse_args( + '--foo 0'.split() + ) + + actual = getattr(results, 'foo', None) + self.assertEqual(actual, 0) + + def test_positive_values(self): + parser = argparse.ArgumentParser() + + # Set up our typical usage + parser.add_argument( + '--foo', + metavar='', + type=int, + action=parseractions.NonNegativeAction, + ) + + results = parser.parse_args( + '--foo 1'.split() + ) + + actual = getattr(results, 'foo', None) + self.assertEqual(actual, 1)