post event paramters fix

* make  the default value for time work
* use the argparse option to make the type argument mandatory
* add tests

Change-Id: Id3186fa90300f34fe3530109f11bd655a39d8363
This commit is contained in:
Eyal 2017-07-03 15:18:47 +03:00
parent b4687f9b10
commit e50d0f0dc2
2 changed files with 19 additions and 6 deletions

View File

@ -51,3 +51,19 @@ class EventPostTest(CliTestCase):
parser.parse_args(args=['--type', 'bla',
'--time', '-5',
'--details', 'blabla'])
@mock.patch.object(ArgumentParser, "error")
def test_parser_event_post_without_time(self, mock_parser):
mock_parser.side_effect = self._my_parser_error_func
parser = self.event_post.get_parser('vitrage event post')
parser.parse_args(args=['--type', 'bla', '--details', 'blabla'])
@mock.patch.object(ArgumentParser, "error")
def test_parser_event_post_type_required(self, mock_parser):
mock_parser.side_effect = self._my_parser_error_func
parser = self.event_post.get_parser('vitrage event post')
# noinspection PyCallByClass
with ExpectedException(ArgumentTypeError, r'.*--type'):
parser.parse_args(args=['--details', 'blabla'])

View File

@ -20,8 +20,6 @@ from datetime import datetime
from iso8601 import iso8601
from iso8601 import ParseError
from vitrageclient.common import exc
# noinspection PyAbstractClass
class EventPost(command.Command):
@ -30,6 +28,7 @@ class EventPost(command.Command):
@staticmethod
def iso8601(argument_value):
try:
if argument_value:
iso8601.parse_date(argument_value)
except ParseError:
msg = "%s must be an iso8601 date" % argument_value
@ -39,6 +38,7 @@ class EventPost(command.Command):
parser = super(EventPost, self).get_parser(prog_name)
parser.add_argument('--type',
required=True,
help='The type of the event')
parser.add_argument('--time',
@ -55,9 +55,6 @@ class EventPost(command.Command):
return parser
def take_action(self, parsed_args):
if not parsed_args.type:
raise exc.CommandException(
message='Missing event type, please add --type')
if parsed_args.time:
event_time = parsed_args.time