From e0531368b205abdc21774da7340b991ee703cd59 Mon Sep 17 00:00:00 2001 From: Jake Yip Date: Mon, 27 Oct 2025 18:40:25 +1100 Subject: [PATCH] Remove unused utcnow mock Mock utcnow is useful for testing operations [1] that should not change the `updated_at` value. For tests that do not validate `updated_at`, there is really no need for such a mock. Clean up unused mock to simplify the code. [1] https://opendev.org/openstack/magnum/src/tag/21.0.0/magnum/tests/unit/api/controllers/v1/test_cluster.py#L324 Change-Id: Ibbbe92a4e27a46438ee231fe4a314122dcabc57a Signed-off-by: Jake Yip --- .../unit/api/controllers/v1/test_cluster.py | 26 ++------ .../api/controllers/v1/test_federation.py | 6 +- .../unit/api/controllers/v1/test_nodegroup.py | 62 ++++--------------- 3 files changed, 18 insertions(+), 76 deletions(-) diff --git a/magnum/tests/unit/api/controllers/v1/test_cluster.py b/magnum/tests/unit/api/controllers/v1/test_cluster.py index 9c37ff40e3..503758488b 100644 --- a/magnum/tests/unit/api/controllers/v1/test_cluster.py +++ b/magnum/tests/unit/api/controllers/v1/test_cluster.py @@ -386,11 +386,8 @@ class TestPatch(api_base.FunctionalTest): self.assertEqual(self.cluster_obj.cluster_template_id, response['cluster_template_id']) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_replace_ok_by_name_not_found(self, mock_utcnow): + def test_replace_ok_by_name_not_found(self): name = 'not_found' - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.patch_json('/clusters/%s' % name, [{'path': '/name', 'value': name, @@ -399,11 +396,8 @@ class TestPatch(api_base.FunctionalTest): self.assertEqual('application/json', response.content_type) self.assertEqual(404, response.status_code) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_replace_ok_by_uuid_not_found(self, mock_utcnow): + def test_replace_ok_by_uuid_not_found(self): uuid = uuidutils.generate_uuid() - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.patch_json('/clusters/%s' % uuid, [{'path': '/cluster_id', 'value': uuid, @@ -425,11 +419,7 @@ class TestPatch(api_base.FunctionalTest): self.assertEqual(400, response.status_code) self.assertTrue(response.json['errors']) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_replace_ok_by_name_multiple_cluster(self, mock_utcnow): - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time - + def test_replace_ok_by_name_multiple_cluster(self): obj_utils.create_test_cluster(self.context, name='test_cluster', uuid=uuidutils.generate_uuid()) obj_utils.create_test_cluster(self.context, name='test_cluster', @@ -626,25 +616,19 @@ class TestPost(api_base.FunctionalTest): cluster.create() return cluster - @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_cluster(self, mock_utcnow): + def test_create_cluster(self): bdict = apiutils.cluster_post_data() - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.post_json('/clusters', bdict) self.assertEqual('application/json', response.content_type) self.assertEqual(202, response.status_int) self.assertTrue(uuidutils.is_uuid_like(response.json['uuid'])) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_cluster_resource_limit_reached(self, mock_utcnow): + def test_create_cluster_resource_limit_reached(self): # override max_cluster_per_project to 1 CONF.set_override('max_clusters_per_project', 1, group='quotas') bdict = apiutils.cluster_post_data() - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time # create first cluster response = self.post_json('/clusters', bdict) diff --git a/magnum/tests/unit/api/controllers/v1/test_federation.py b/magnum/tests/unit/api/controllers/v1/test_federation.py index c321e3ef81..97bd65586c 100644 --- a/magnum/tests/unit/api/controllers/v1/test_federation.py +++ b/magnum/tests/unit/api/controllers/v1/test_federation.py @@ -10,7 +10,6 @@ # License for the specific language governing permissions and limitations # under the License. -import datetime from unittest import mock from oslo_config import cfg @@ -285,13 +284,10 @@ class TestPost(api_base.FunctionalTest): federation.create() return federation - @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_federation(self, mock_utcnow): + def test_create_federation(self): bdict = apiutils.federation_post_data( uuid=uuidutils.generate_uuid(), hostcluster_id=self.hostcluster.uuid) - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.post_json('/federations', bdict) self.assertEqual('application/json', response.content_type) diff --git a/magnum/tests/unit/api/controllers/v1/test_nodegroup.py b/magnum/tests/unit/api/controllers/v1/test_nodegroup.py index a2b757c974..de6ab0eb37 100644 --- a/magnum/tests/unit/api/controllers/v1/test_nodegroup.py +++ b/magnum/tests/unit/api/controllers/v1/test_nodegroup.py @@ -293,11 +293,8 @@ class TestPost(NodeGroupControllerTest): nodegroup.create() return nodegroup - @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_nodegroup(self, mock_utcnow): + def test_create_nodegroup(self): ng_dict = apiutils.nodegroup_post_data() - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.post_json(self.url, ng_dict) self.assertEqual('application/json', response.content_type) @@ -305,12 +302,9 @@ class TestPost(NodeGroupControllerTest): self.assertTrue(uuidutils.is_uuid_like(response.json['uuid'])) self.assertFalse(response.json['is_default']) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_nodegroup_without_node_count(self, mock_utcnow): + def test_create_nodegroup_without_node_count(self): ng_dict = apiutils.nodegroup_post_data() del ng_dict['node_count'] - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.post_json(self.url, ng_dict) self.assertEqual('application/json', response.content_type) @@ -318,13 +312,10 @@ class TestPost(NodeGroupControllerTest): # Verify node_count defaults to 1 self.assertEqual(1, response.json['node_count']) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_nodegroup_with_zero_nodes(self, mock_utcnow): + def test_create_nodegroup_with_zero_nodes(self): ng_dict = apiutils.nodegroup_post_data() ng_dict['node_count'] = 0 ng_dict['min_node_count'] = 0 - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.post_json(self.url, ng_dict) self.assertEqual('application/json', response.content_type) @@ -332,67 +323,49 @@ class TestPost(NodeGroupControllerTest): # Verify node_count is set to zero self.assertEqual(0, response.json['node_count']) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_nodegroup_with_max_node_count(self, mock_utcnow): + def test_create_nodegroup_with_max_node_count(self): ng_dict = apiutils.nodegroup_post_data(max_node_count=5) - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.post_json(self.url, ng_dict) self.assertEqual('application/json', response.content_type) self.assertEqual(202, response.status_int) self.assertEqual(5, response.json['max_node_count']) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_nodegroup_with_role(self, mock_utcnow): + def test_create_nodegroup_with_role(self): ng_dict = apiutils.nodegroup_post_data(role='test-role') - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.post_json(self.url, ng_dict) self.assertEqual('application/json', response.content_type) self.assertEqual(202, response.status_int) self.assertEqual('test-role', response.json['role']) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_nodegroup_with_labels(self, mock_utcnow): + def test_create_nodegroup_with_labels(self): labels = {'label1': 'value1'} ng_dict = apiutils.nodegroup_post_data(labels=labels) - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.post_json(self.url, ng_dict) self.assertEqual('application/json', response.content_type) self.assertEqual(202, response.status_int) self.assertEqual(labels, response.json['labels']) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_nodegroup_with_image_id(self, mock_utcnow): + def test_create_nodegroup_with_image_id(self): ng_dict = apiutils.nodegroup_post_data(image_id='test_image') - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.post_json(self.url, ng_dict) self.assertEqual('application/json', response.content_type) self.assertEqual(202, response.status_int) self.assertEqual('test_image', response.json['image_id']) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_nodegroup_with_flavor(self, mock_utcnow): + def test_create_nodegroup_with_flavor(self): ng_dict = apiutils.nodegroup_post_data(flavor_id='test_flavor') - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.post_json(self.url, ng_dict) self.assertEqual('application/json', response.content_type) self.assertEqual(202, response.status_int) self.assertEqual('test_flavor', response.json['flavor_id']) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_nodegroup_only_name(self, mock_utcnow): + def test_create_nodegroup_only_name(self): ng_dict = {'name': 'test_ng'} - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.post_json(self.url, ng_dict) self.assertEqual('application/json', response.content_type) @@ -409,11 +382,8 @@ class TestPost(NodeGroupControllerTest): self.assertEqual(1, response.json['node_count']) self.assertIsNone(response.json['max_node_count']) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_nodegroup_invalid_node_count(self, mock_utcnow): + def test_create_nodegroup_invalid_node_count(self): ng_dict = apiutils.nodegroup_post_data(node_count=7, max_node_count=5) - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time response = self.post_json(self.url, ng_dict, expect_errors=True) self.assertEqual('application/json', response.content_type) @@ -710,11 +680,7 @@ class TestPatch(NodeGroupControllerTest): response['updated_at']).replace(tzinfo=None) self.assertEqual(test_time, return_updated_at) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_remove_internal_attr(self, mock_utcnow): - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time - + def test_remove_internal_attr(self): response = self.patch_json(self.url + self.nodegroup.name, [{'path': '/node_count', 'op': 'remove'}], expect_errors=True) @@ -722,11 +688,7 @@ class TestPatch(NodeGroupControllerTest): self.assertEqual(400, response.status_code) self.assertIsNotNone(response.json['errors']) - @mock.patch('oslo_utils.timeutils.utcnow') - def test_remove_non_existent_property(self, mock_utcnow): - test_time = datetime.datetime(2000, 1, 1, 0, 0) - mock_utcnow.return_value = test_time - + def test_remove_non_existent_property(self): response = self.patch_json(self.url + self.nodegroup.name, [{'path': '/not_there', 'op': 'remove'}], expect_errors=True)