From 9671fa85e06c9572111246151f459b2b0624a34f Mon Sep 17 00:00:00 2001 From: Corey Bryant Date: Mon, 4 Dec 2023 15:47:20 -0500 Subject: [PATCH] Mock datetime instead of redefining standard library This fixes up the test to mock the datetime calls specifically in the test that needs to mock them, rather than the prior approach which was redefining the behavior of the standard library function globally. This change also pins openstacksdk to fix python 3.6 unit tests. Commit b34de32a in openstacksdk replaced the appdirs library with the platformdirs library. The platformdirs library is not python 3.6 compatible and causes some challenges. Pin the openstacksdk to a lower version. Thanks to Billy Olsen for this patch. Taken from: https://review.opendev.org/c/openstack/charm-neutron-api/+/902607 Closes-Bug: #2045588 Change-Id: I9c585c91c5527a61e4a8dbcba0a8a01108cd9b2e (cherry picked from commit 72e22a94ed4966ff6fb2fba41bac1b204dbd4c14) --- test-requirements.txt | 2 ++ unit_tests/test_check_swift_storage.py | 28 +++++++++----------------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/test-requirements.txt b/test-requirements.txt index 78b3c7d..aebd192 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -47,3 +47,5 @@ tempest<31.0.0;python_version<'3.8' and python_version>='3.6' tempest<24.0.0;python_version<'3.6' croniter # needed for charm-rabbitmq-server unit tests + +openstacksdk<=1.5.0; # Pin to a lower version of openstacksdk due to introduced libraries diff --git a/unit_tests/test_check_swift_storage.py b/unit_tests/test_check_swift_storage.py index 940f6ca..29fd353 100644 --- a/unit_tests/test_check_swift_storage.py +++ b/unit_tests/test_check_swift_storage.py @@ -40,24 +40,6 @@ STATUS_CRIT = 2 STATUS_UNKNOWN = 3 -class NewDate(datetime.datetime): - @classmethod - def now(cls): - """ - Mock for builtin datetime.datetime.now(), to test repl_last_timestamp() - Non-defined methods are inherited from real class - """ - return cls(2017, 4, 27, - 13, 25, 46, 629282) - - @classmethod - def fromtimestamp(cls, timestamp): - return cls.utcfromtimestamp(timestamp) - - -datetime.datetime = NewDate - - class CheckSwiftStorageTestCase(unittest.TestCase): def test_generate_md5(self): """ @@ -398,12 +380,20 @@ class CheckSwiftStorageTestCase(unittest.TestCase): result = check_replication(base_url, [4, 10, 4, 10]) self.assertEqual(result, [(STATUS_OK, 'OK')]) - def test_repl_last_timestamp(self): + @patch('check_swift_storage.datetime') + def test_repl_last_timestamp(self, mock_datetime): """ Calculates delta between NOW and last replication date Also gathers the number of failures """ # 1493299546.629282 + mock_datetime.datetime.now.return_value = ( + datetime.datetime(2017, 4, 27, 13, 25, 46, 629282) + ) + mock_datetime.datetime.fromtimestamp.side_effect = ( + datetime.datetime.utcfromtimestamp + ) + jdata = {u'replication_last': 1493299546.629282, u'replication_stats': {u'no_change': 0, u'rsync': 0, u'success': 0, u'start': 1493299546.621624, u'attempted': 0, u'ts_repl': 0, u'remove':