Skip to content

Commit e053136

Browse files
committed
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 <jake.yip@ardc.edu.au>
1 parent a950ce7 commit e053136

3 files changed

Lines changed: 18 additions & 76 deletions

File tree

magnum/tests/unit/api/controllers/v1/test_cluster.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,8 @@ def test_replace_ok_by_name(self, mock_utcnow):
386386
self.assertEqual(self.cluster_obj.cluster_template_id,
387387
response['cluster_template_id'])
388388

389-
@mock.patch('oslo_utils.timeutils.utcnow')
390-
def test_replace_ok_by_name_not_found(self, mock_utcnow):
389+
def test_replace_ok_by_name_not_found(self):
391390
name = 'not_found'
392-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
393-
mock_utcnow.return_value = test_time
394391

395392
response = self.patch_json('/clusters/%s' % name,
396393
[{'path': '/name', 'value': name,
@@ -399,11 +396,8 @@ def test_replace_ok_by_name_not_found(self, mock_utcnow):
399396
self.assertEqual('application/json', response.content_type)
400397
self.assertEqual(404, response.status_code)
401398

402-
@mock.patch('oslo_utils.timeutils.utcnow')
403-
def test_replace_ok_by_uuid_not_found(self, mock_utcnow):
399+
def test_replace_ok_by_uuid_not_found(self):
404400
uuid = uuidutils.generate_uuid()
405-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
406-
mock_utcnow.return_value = test_time
407401

408402
response = self.patch_json('/clusters/%s' % uuid,
409403
[{'path': '/cluster_id', 'value': uuid,
@@ -425,11 +419,7 @@ def test_replace_cluster_template_id_failed(self):
425419
self.assertEqual(400, response.status_code)
426420
self.assertTrue(response.json['errors'])
427421

428-
@mock.patch('oslo_utils.timeutils.utcnow')
429-
def test_replace_ok_by_name_multiple_cluster(self, mock_utcnow):
430-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
431-
mock_utcnow.return_value = test_time
432-
422+
def test_replace_ok_by_name_multiple_cluster(self):
433423
obj_utils.create_test_cluster(self.context, name='test_cluster',
434424
uuid=uuidutils.generate_uuid())
435425
obj_utils.create_test_cluster(self.context, name='test_cluster',
@@ -626,25 +616,19 @@ def _simulate_cluster_create(self, cluster, master_count, node_count,
626616
cluster.create()
627617
return cluster
628618

629-
@mock.patch('oslo_utils.timeutils.utcnow')
630-
def test_create_cluster(self, mock_utcnow):
619+
def test_create_cluster(self):
631620
bdict = apiutils.cluster_post_data()
632-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
633-
mock_utcnow.return_value = test_time
634621

635622
response = self.post_json('/clusters', bdict)
636623
self.assertEqual('application/json', response.content_type)
637624
self.assertEqual(202, response.status_int)
638625
self.assertTrue(uuidutils.is_uuid_like(response.json['uuid']))
639626

640-
@mock.patch('oslo_utils.timeutils.utcnow')
641-
def test_create_cluster_resource_limit_reached(self, mock_utcnow):
627+
def test_create_cluster_resource_limit_reached(self):
642628
# override max_cluster_per_project to 1
643629
CONF.set_override('max_clusters_per_project', 1, group='quotas')
644630

645631
bdict = apiutils.cluster_post_data()
646-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
647-
mock_utcnow.return_value = test_time
648632

649633
# create first cluster
650634
response = self.post_json('/clusters', bdict)

magnum/tests/unit/api/controllers/v1/test_federation.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# License for the specific language governing permissions and limitations
1111
# under the License.
1212

13-
import datetime
1413
from unittest import mock
1514

1615
from oslo_config import cfg
@@ -285,13 +284,10 @@ def _simulate_federation_create(self, federation, create_timeout):
285284
federation.create()
286285
return federation
287286

288-
@mock.patch('oslo_utils.timeutils.utcnow')
289-
def test_create_federation(self, mock_utcnow):
287+
def test_create_federation(self):
290288
bdict = apiutils.federation_post_data(
291289
uuid=uuidutils.generate_uuid(),
292290
hostcluster_id=self.hostcluster.uuid)
293-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
294-
mock_utcnow.return_value = test_time
295291

296292
response = self.post_json('/federations', bdict)
297293
self.assertEqual('application/json', response.content_type)

magnum/tests/unit/api/controllers/v1/test_nodegroup.py

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -293,106 +293,79 @@ def _simulate_nodegroup_create(self, cluster, nodegroup):
293293
nodegroup.create()
294294
return nodegroup
295295

296-
@mock.patch('oslo_utils.timeutils.utcnow')
297-
def test_create_nodegroup(self, mock_utcnow):
296+
def test_create_nodegroup(self):
298297
ng_dict = apiutils.nodegroup_post_data()
299-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
300-
mock_utcnow.return_value = test_time
301298

302299
response = self.post_json(self.url, ng_dict)
303300
self.assertEqual('application/json', response.content_type)
304301
self.assertEqual(202, response.status_int)
305302
self.assertTrue(uuidutils.is_uuid_like(response.json['uuid']))
306303
self.assertFalse(response.json['is_default'])
307304

308-
@mock.patch('oslo_utils.timeutils.utcnow')
309-
def test_create_nodegroup_without_node_count(self, mock_utcnow):
305+
def test_create_nodegroup_without_node_count(self):
310306
ng_dict = apiutils.nodegroup_post_data()
311307
del ng_dict['node_count']
312-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
313-
mock_utcnow.return_value = test_time
314308

315309
response = self.post_json(self.url, ng_dict)
316310
self.assertEqual('application/json', response.content_type)
317311
self.assertEqual(202, response.status_int)
318312
# Verify node_count defaults to 1
319313
self.assertEqual(1, response.json['node_count'])
320314

321-
@mock.patch('oslo_utils.timeutils.utcnow')
322-
def test_create_nodegroup_with_zero_nodes(self, mock_utcnow):
315+
def test_create_nodegroup_with_zero_nodes(self):
323316
ng_dict = apiutils.nodegroup_post_data()
324317
ng_dict['node_count'] = 0
325318
ng_dict['min_node_count'] = 0
326-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
327-
mock_utcnow.return_value = test_time
328319

329320
response = self.post_json(self.url, ng_dict)
330321
self.assertEqual('application/json', response.content_type)
331322
self.assertEqual(202, response.status_int)
332323
# Verify node_count is set to zero
333324
self.assertEqual(0, response.json['node_count'])
334325

335-
@mock.patch('oslo_utils.timeutils.utcnow')
336-
def test_create_nodegroup_with_max_node_count(self, mock_utcnow):
326+
def test_create_nodegroup_with_max_node_count(self):
337327
ng_dict = apiutils.nodegroup_post_data(max_node_count=5)
338-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
339-
mock_utcnow.return_value = test_time
340328

341329
response = self.post_json(self.url, ng_dict)
342330
self.assertEqual('application/json', response.content_type)
343331
self.assertEqual(202, response.status_int)
344332
self.assertEqual(5, response.json['max_node_count'])
345333

346-
@mock.patch('oslo_utils.timeutils.utcnow')
347-
def test_create_nodegroup_with_role(self, mock_utcnow):
334+
def test_create_nodegroup_with_role(self):
348335
ng_dict = apiutils.nodegroup_post_data(role='test-role')
349-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
350-
mock_utcnow.return_value = test_time
351336

352337
response = self.post_json(self.url, ng_dict)
353338
self.assertEqual('application/json', response.content_type)
354339
self.assertEqual(202, response.status_int)
355340
self.assertEqual('test-role', response.json['role'])
356341

357-
@mock.patch('oslo_utils.timeutils.utcnow')
358-
def test_create_nodegroup_with_labels(self, mock_utcnow):
342+
def test_create_nodegroup_with_labels(self):
359343
labels = {'label1': 'value1'}
360344
ng_dict = apiutils.nodegroup_post_data(labels=labels)
361-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
362-
mock_utcnow.return_value = test_time
363345

364346
response = self.post_json(self.url, ng_dict)
365347
self.assertEqual('application/json', response.content_type)
366348
self.assertEqual(202, response.status_int)
367349
self.assertEqual(labels, response.json['labels'])
368350

369-
@mock.patch('oslo_utils.timeutils.utcnow')
370-
def test_create_nodegroup_with_image_id(self, mock_utcnow):
351+
def test_create_nodegroup_with_image_id(self):
371352
ng_dict = apiutils.nodegroup_post_data(image_id='test_image')
372-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
373-
mock_utcnow.return_value = test_time
374353

375354
response = self.post_json(self.url, ng_dict)
376355
self.assertEqual('application/json', response.content_type)
377356
self.assertEqual(202, response.status_int)
378357
self.assertEqual('test_image', response.json['image_id'])
379358

380-
@mock.patch('oslo_utils.timeutils.utcnow')
381-
def test_create_nodegroup_with_flavor(self, mock_utcnow):
359+
def test_create_nodegroup_with_flavor(self):
382360
ng_dict = apiutils.nodegroup_post_data(flavor_id='test_flavor')
383-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
384-
mock_utcnow.return_value = test_time
385361

386362
response = self.post_json(self.url, ng_dict)
387363
self.assertEqual('application/json', response.content_type)
388364
self.assertEqual(202, response.status_int)
389365
self.assertEqual('test_flavor', response.json['flavor_id'])
390366

391-
@mock.patch('oslo_utils.timeutils.utcnow')
392-
def test_create_nodegroup_only_name(self, mock_utcnow):
367+
def test_create_nodegroup_only_name(self):
393368
ng_dict = {'name': 'test_ng'}
394-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
395-
mock_utcnow.return_value = test_time
396369

397370
response = self.post_json(self.url, ng_dict)
398371
self.assertEqual('application/json', response.content_type)
@@ -409,11 +382,8 @@ def test_create_nodegroup_only_name(self, mock_utcnow):
409382
self.assertEqual(1, response.json['node_count'])
410383
self.assertIsNone(response.json['max_node_count'])
411384

412-
@mock.patch('oslo_utils.timeutils.utcnow')
413-
def test_create_nodegroup_invalid_node_count(self, mock_utcnow):
385+
def test_create_nodegroup_invalid_node_count(self):
414386
ng_dict = apiutils.nodegroup_post_data(node_count=7, max_node_count=5)
415-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
416-
mock_utcnow.return_value = test_time
417387

418388
response = self.post_json(self.url, ng_dict, expect_errors=True)
419389
self.assertEqual('application/json', response.content_type)
@@ -710,23 +680,15 @@ def test_remove_min_node_count(self, mock_utcnow):
710680
response['updated_at']).replace(tzinfo=None)
711681
self.assertEqual(test_time, return_updated_at)
712682

713-
@mock.patch('oslo_utils.timeutils.utcnow')
714-
def test_remove_internal_attr(self, mock_utcnow):
715-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
716-
mock_utcnow.return_value = test_time
717-
683+
def test_remove_internal_attr(self):
718684
response = self.patch_json(self.url + self.nodegroup.name,
719685
[{'path': '/node_count',
720686
'op': 'remove'}], expect_errors=True)
721687
self.assertEqual('application/json', response.content_type)
722688
self.assertEqual(400, response.status_code)
723689
self.assertIsNotNone(response.json['errors'])
724690

725-
@mock.patch('oslo_utils.timeutils.utcnow')
726-
def test_remove_non_existent_property(self, mock_utcnow):
727-
test_time = datetime.datetime(2000, 1, 1, 0, 0)
728-
mock_utcnow.return_value = test_time
729-
691+
def test_remove_non_existent_property(self):
730692
response = self.patch_json(self.url + self.nodegroup.name,
731693
[{'path': '/not_there',
732694
'op': 'remove'}], expect_errors=True)

0 commit comments

Comments
 (0)