Replace plain Mock() calls with autospec=True or Mock(spec)

Bug #2004174 reported by Sofia Enriquez
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Cinder
In Progress
Low
Khadija Kamran

Bug Description

Cinder has some tests with assertion typo's or plain calls to Mock() or MagicMock() resulting in those tests not testing code correctly. One example of that is: https://review.opendev.org/c/openstack/cinder/+/831600

The refactored code would replace plain Mock() calls with Mock(spec) calls
or you can either pass autospec=True to patch() / patch.object() or use the create_autospec() function to create a mock with a spec. We want stronger mocks by using specs, do the right thing where needed to get this.

Using autospec/spec limits the API of mocks to the API of an original object (the spec). This ensures that any assertion typo's or incorrect calls will be caught by raising a TypeError. This makes it easier to identify and fix any errors in the tests, ensuring that they are testing code correctly.

Mock(spec) example:

diff --git a/cinder/tests/unit/volume/test_volume_manager.py b/cinder/tests/unit/volume/test_volume_manager.py
index 9a7a50ff6..5666f7442 100644
--- a/cinder/tests/unit/volume/test_volume_manager.py
+++ b/cinder/tests/unit/volume/test_volume_manager.py
@@ -20,6 +20,7 @@ import ddt

 from cinder.common import constants
 from cinder import exception
+ from cinder import context
 from cinder.message import message_field
 from cinder.tests.unit import fake_constants as fake
 from cinder.tests.unit import fake_volume
@@ -231,7 +232,7 @@ class VolumeManagerTestCase(base.BaseVolumeTestCase):
                                    'volume_encryption_metadata_get')
         mock_attach_encryptor.side_effect = ValueError

- ctxt = mock.Mock()
+ ctxt = mock.Mock(spec=context.RequestContext)
         vol = fake_volume.fake_volume_obj(ctxt)

The refactored code replaces plain calls to Mock() with Mock(spec) calls, which specify the object class that the mock should be based on (in this case, context.RequestContext). This allows the tests to more accurately simulate how the code will behave when it is run in production, as it will be using objects of those classes rather than generic mocks that don't have any of the methods or attributes of those classes available to them.

Documentation unittest.mock#autospeccing: https://docs.python.org/3/library/unittest.mock.html#autospeccing

summary: - Replace plain Mock() calls with Mock(spec)
+ Replace plain Mock() calls with autospec=True or Mock(spec)
tags: added: tests
description: updated
Changed in cinder:
status: Confirmed → Triaged
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to cinder (master)

Fix proposed to branch: master
Review: https://review.opendev.org/c/openstack/cinder/+/873905

Changed in cinder:
status: Triaged → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Fix proposed to branch: master
Review: https://review.opendev.org/c/openstack/cinder/+/874569

Revision history for this message
Khadija Kamran (deeja) wrote :

Hey Sofia! Can you please assign this bug to me?

Revision history for this message
Sofia Enriquez (lsofia-enriquez) wrote :

Yes Khadija Kamran, feel free to work on them!

Changed in cinder:
assignee: nobody → Khadija Kamran (deeja)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Fix proposed to branch: master
Review: https://review.opendev.org/c/openstack/cinder/+/877185

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Fix proposed to branch: master
Review: https://review.opendev.org/c/openstack/cinder/+/877425

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Change abandoned on cinder (master)

Change abandoned by "Khadija Kamran <email address hidden>" on branch: master
Review: https://review.opendev.org/c/openstack/cinder/+/877425

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Change abandoned by "Khadija Kamran <email address hidden>" on branch: master
Review: https://review.opendev.org/c/openstack/cinder/+/877425
Reason: Submitted by mistake

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Change abandoned by "Sofia Enriquez <email address hidden>" on branch: master
Review: https://review.opendev.org/c/openstack/cinder/+/877425
Reason: I've reactive by mistake

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to cinder (master)

Reviewed: https://review.opendev.org/c/openstack/cinder/+/877185
Committed: https://opendev.org/openstack/cinder/commit/41da45ddd3b9677b4ee7f82f1a59e3bca9dee615
Submitter: "Zuul (22348)"
Branch: master

commit 41da45ddd3b9677b4ee7f82f1a59e3bca9dee615
Author: Khadija Kamran <email address hidden>
Date: Mon Mar 13 01:14:01 2023 +0500

    Improve test_execute_root_and_helper

    Pass autospec=True while patching the function.
    Create the mock object for mock_helper using mock.sentinel. This gives
    us some extra protection because sentinel object is not callable. If the
    code tries to use it like a function, it will raise a TypeError.
    This patch also changes assertFalse() to assert_not_called().

    Partial-Bug: #2004174
    Change-Id: I209ff5d0fd7e3eaa0b50dcbf71ff4b0960403f96
    Signed-off-by: Khadija Kamran <email address hidden>

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Change abandoned on cinder (master)

Change abandoned by "Sofia Enriquez <email address hidden>" on branch: master
Review: https://review.opendev.org/c/openstack/cinder/+/873905
Reason: I've fixed all the file in the next patch https://review.opendev.org/c/openstack/cinder/+/874569

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.