Commit 1e31dcae authored by Pierre Riteau's avatar Pierre Riteau

Fix lease creation when start date is not provided

Change-Id: I5fe0de436dd5e2c21ab9eb08b47c1995b5154a53
Closes-Bug: #1784038
parent f017a924
...@@ -175,6 +175,47 @@ class CreateLeaseTestCase(tests.TestCase): ...@@ -175,6 +175,47 @@ class CreateLeaseTestCase(tests.TestCase):
} }
self.assertDictEqual(self.cl.args2body(args), expected) self.assertDictEqual(self.cl.args2body(args), expected)
def test_args2body_start_now(self):
args = argparse.Namespace(
start='now',
end='2020-08-09 22:30',
before_end='2020-08-09 21:30',
events=[],
name='lease-test',
reservations=[],
physical_reservations=[
'min=1,'
'max=2,'
'hypervisor_properties='
'["and", [">=", "$vcpus", "2"], '
'[">=", "$memory_mb", "2048"]],'
'resource_properties='
'["==", "$extra_key", "extra_value"],'
'before_end=default'
]
)
expected = {
'start': 'now',
'end': '2020-08-09 22:30',
'before_end': '2020-08-09 21:30',
'events': [],
'name': 'lease-test',
'reservations': [
{
'min': 1,
'max': 2,
'hypervisor_properties':
'["and", [">=", "$vcpus", "2"], '
'[">=", "$memory_mb", "2048"]]',
'resource_properties':
'["==", "$extra_key", "extra_value"]',
'resource_type': 'physical:host',
'before_end': 'default'
}
]
}
self.assertDictEqual(self.cl.args2body(args), expected)
class UpdateLeaseTestCase(tests.TestCase): class UpdateLeaseTestCase(tests.TestCase):
......
...@@ -177,18 +177,25 @@ class CreateLease(command.CreateCommand): ...@@ -177,18 +177,25 @@ class CreateLease(command.CreateCommand):
if parsed_args.name: if parsed_args.name:
params['name'] = parsed_args.name params['name'] = parsed_args.name
if not isinstance(parsed_args.start, datetime.datetime): if not isinstance(parsed_args.start, datetime.datetime):
try: if parsed_args.start != 'now':
parsed_args.start = datetime.datetime.strptime( try:
parsed_args.start, '%Y-%m-%d %H:%M') parsed_args.start = datetime.datetime.strptime(
except ValueError: parsed_args.start, '%Y-%m-%d %H:%M')
raise exception.IncorrectLease except ValueError:
raise exception.IncorrectLease
if not isinstance(parsed_args.end, datetime.datetime): if not isinstance(parsed_args.end, datetime.datetime):
try: try:
parsed_args.end = datetime.datetime.strptime( parsed_args.end = datetime.datetime.strptime(
parsed_args.end, '%Y-%m-%d %H:%M') parsed_args.end, '%Y-%m-%d %H:%M')
except ValueError: except ValueError:
raise exception.IncorrectLease raise exception.IncorrectLease
if parsed_args.start > parsed_args.end:
if parsed_args.start == 'now':
start = datetime.datetime.utcnow()
else:
start = parsed_args.start
if start > parsed_args.end:
raise exception.IncorrectLease raise exception.IncorrectLease
if parsed_args.before_end: if parsed_args.before_end:
...@@ -197,14 +204,17 @@ class CreateLease(command.CreateCommand): ...@@ -197,14 +204,17 @@ class CreateLease(command.CreateCommand):
parsed_args.before_end, '%Y-%m-%d %H:%M') parsed_args.before_end, '%Y-%m-%d %H:%M')
except ValueError: except ValueError:
raise exception.IncorrectLease raise exception.IncorrectLease
if (parsed_args.before_end < parsed_args.start if (parsed_args.before_end < start
or parsed_args.end < parsed_args.before_end): or parsed_args.end < parsed_args.before_end):
raise exception.IncorrectLease raise exception.IncorrectLease
params['before_end'] = datetime.datetime.strftime( params['before_end'] = datetime.datetime.strftime(
parsed_args.before_end, '%Y-%m-%d %H:%M') parsed_args.before_end, '%Y-%m-%d %H:%M')
params['start'] = datetime.datetime.strftime(parsed_args.start, if parsed_args.start == 'now':
'%Y-%m-%d %H:%M') params['start'] = parsed_args.start
else:
params['start'] = datetime.datetime.strftime(parsed_args.start,
'%Y-%m-%d %H:%M')
params['end'] = datetime.datetime.strftime(parsed_args.end, params['end'] = datetime.datetime.strftime(parsed_args.end,
'%Y-%m-%d %H:%M') '%Y-%m-%d %H:%M')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment