diff --git a/blazarclient/tests/v1/shell_commands/test_leases.py b/blazarclient/tests/v1/shell_commands/test_leases.py index 554dfab..8f16b37 100644 --- a/blazarclient/tests/v1/shell_commands/test_leases.py +++ b/blazarclient/tests/v1/shell_commands/test_leases.py @@ -14,6 +14,7 @@ # limitations under the License. import argparse +from datetime import datetime import mock from blazarclient import exception @@ -21,11 +22,13 @@ from blazarclient import shell from blazarclient import tests from blazarclient.v1.shell_commands import leases +mock_time = mock.Mock(return_value=datetime(2020, 6, 8)) FIRST_LEASE = 'd1e43d6d-8f6f-4c2e-b0a9-2982b39dc698' SECOND_LEASE = '424d21c3-45a2-448a-81ad-32eddc888375' +@mock.patch('blazarclient.v1.shell_commands.leases._utc_now', mock_time) class CreateLeaseTestCase(tests.TestCase): def setUp(self): diff --git a/blazarclient/v1/shell_commands/leases.py b/blazarclient/v1/shell_commands/leases.py index fdd3013..b46626d 100644 --- a/blazarclient/v1/shell_commands/leases.py +++ b/blazarclient/v1/shell_commands/leases.py @@ -58,6 +58,16 @@ CREATE_RESERVATION_KEYS = { } +def _utc_now(): + """Wrap datetime.datetime.utcnow so it can be mocked in unit tests. + + This is required because some of the tests require understanding the + 'current time'; simply mocking utcnow() is made very difficult by + the many different ways the datetime package is used in this module. + """ + return datetime.datetime.utcnow() + + class ListLeases(command.ListCommand): """Print a list of leases.""" resource = 'lease' @@ -87,7 +97,7 @@ class CreateLease(command.CreateCommand): json_indent = 4 log = logging.getLogger(__name__ + '.CreateLease') default_start = 'now' - default_end = datetime.datetime.utcnow() + datetime.timedelta(days=1) + default_end = _utc_now() + datetime.timedelta(days=1) def get_parser(self, prog_name): parser = super(CreateLease, self).get_parser(prog_name) @@ -203,7 +213,7 @@ class CreateLease(command.CreateCommand): raise exception.IncorrectLease if parsed_args.start == 'now': - start = datetime.datetime.utcnow() + start = _utc_now() else: start = parsed_args.start