Skip to content

Commit ea63553

Browse files
committed
Fix the bug of "openstack console log show"
The behaviors are inconsistent while different negative line numbers specified. Change-Id: I2573f3e789f5603c896758971830ffc0b94c5e2b Closes-Bug: #1512263
1 parent 6dfa304 commit ea63553

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

openstackclient/common/parseractions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,18 @@ def __call__(self, parser, namespace, values, option_string=None):
6565
# Too many values
6666
msg = "Invalid range, too many values"
6767
raise argparse.ArgumentError(self, msg)
68+
69+
70+
class NonNegativeAction(argparse.Action):
71+
"""A custom action to check whether the value is non-negative or not
72+
73+
Ensures the value is >= 0.
74+
"""
75+
76+
def __call__(self, parser, namespace, values, option_string=None):
77+
try:
78+
assert(int(values) >= 0)
79+
setattr(namespace, self.dest, values)
80+
except Exception:
81+
msg = "%s expected a non-negative integer" % (str(option_string))
82+
raise argparse.ArgumentTypeError(self, msg)

openstackclient/compute/v2/console.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from cliff import command
2323
from cliff import show
2424

25+
from openstackclient.common import parseractions
2526
from openstackclient.common import utils
2627

2728

@@ -42,6 +43,7 @@ def get_parser(self, prog_name):
4243
metavar='<num-lines>',
4344
type=int,
4445
default=None,
46+
action=parseractions.NonNegativeAction,
4547
help='Number of lines to display from the end of the log '
4648
'(default=all)',
4749
)

openstackclient/tests/common/test_parseractions.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,58 @@ def test_error_values(self):
102102
expect = {'green': '100%'}
103103
self.assertDictEqual(expect, actual)
104104
self.assertEqual(None, failhere)
105+
106+
107+
class TestNonNegativeAction(utils.TestCase):
108+
def test_negative_values(self):
109+
parser = argparse.ArgumentParser()
110+
111+
# Set up our typical usage
112+
parser.add_argument(
113+
'--foo',
114+
metavar='<foo>',
115+
type=int,
116+
action=parseractions.NonNegativeAction,
117+
)
118+
119+
self.assertRaises(
120+
argparse.ArgumentTypeError,
121+
parser.parse_args,
122+
"--foo -1".split()
123+
)
124+
125+
def test_zero_values(self):
126+
parser = argparse.ArgumentParser()
127+
128+
# Set up our typical usage
129+
parser.add_argument(
130+
'--foo',
131+
metavar='<foo>',
132+
type=int,
133+
action=parseractions.NonNegativeAction,
134+
)
135+
136+
results = parser.parse_args(
137+
'--foo 0'.split()
138+
)
139+
140+
actual = getattr(results, 'foo', None)
141+
self.assertEqual(actual, 0)
142+
143+
def test_positive_values(self):
144+
parser = argparse.ArgumentParser()
145+
146+
# Set up our typical usage
147+
parser.add_argument(
148+
'--foo',
149+
metavar='<foo>',
150+
type=int,
151+
action=parseractions.NonNegativeAction,
152+
)
153+
154+
results = parser.parse_args(
155+
'--foo 1'.split()
156+
)
157+
158+
actual = getattr(results, 'foo', None)
159+
self.assertEqual(actual, 1)

0 commit comments

Comments
 (0)