summaryrefslogtreecommitdiff
path: root/openstackclient
diff options
context:
space:
mode:
authorxiexs <xiexs@cn.fujitsu.com>2015-11-09 04:22:00 -0500
committerxiexs <xiexs@cn.fujitsu.com>2015-11-09 04:22:09 -0500
commitea63553925bce0432958ae4a15536f7f83b1f28d (patch)
tree0bc70fe5c474f7b76f8ab018d032029794cb4499 /openstackclient
parent6dfa304617255f8f5d3011ee92648e3c2dbbe2fa (diff)
downloadpython-openstackclient-ea63553925bce0432958ae4a15536f7f83b1f28d.tar.gz
Fix the bug of "openstack console log show"
The behaviors are inconsistent while different negative line numbers specified. Change-Id: I2573f3e789f5603c896758971830ffc0b94c5e2b Closes-Bug: #1512263
Diffstat (limited to 'openstackclient')
-rw-r--r--openstackclient/common/parseractions.py15
-rw-r--r--openstackclient/compute/v2/console.py2
-rw-r--r--openstackclient/tests/common/test_parseractions.py55
3 files changed, 72 insertions, 0 deletions
diff --git a/openstackclient/common/parseractions.py b/openstackclient/common/parseractions.py
index 8f6008e2..fd90369a 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/compute/v2/console.py b/openstackclient/compute/v2/console.py
index bb0747b1..aafa5d44 100644
--- a/openstackclient/compute/v2/console.py
+++ b/openstackclient/compute/v2/console.py
@@ -22,6 +22,7 @@ import sys
from cliff import command
from cliff import show
+from openstackclient.common import parseractions
from openstackclient.common import utils
@@ -42,6 +43,7 @@ class ShowConsoleLog(command.Command):
metavar='<num-lines>',
type=int,
default=None,
+ action=parseractions.NonNegativeAction,
help='Number of lines to display from the end of the log '
'(default=all)',
)
diff --git a/openstackclient/tests/common/test_parseractions.py b/openstackclient/tests/common/test_parseractions.py
index 8afcb632..b75c4814 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='<foo>',
+ 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='<foo>',
+ 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='<foo>',
+ type=int,
+ action=parseractions.NonNegativeAction,
+ )
+
+ results = parser.parse_args(
+ '--foo 1'.split()
+ )
+
+ actual = getattr(results, 'foo', None)
+ self.assertEqual(actual, 1)