Python 3.10 mock errors

Bug #1954512 reported by Corey Bryant
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
networking-hyperv
Fix Released
Undecided
Lucian Petrut

Bug Description

'tox -e py310' results in several errors such as:

======================================================================
FAIL: networking_hyperv.tests.unit.neutron.agent.test_layer2.TestLayer2Agent.test_create_event_listeners
networking_hyperv.tests.unit.neutron.agent.test_layer2.TestLayer2Agent.test_create_event_listeners
----------------------------------------------------------------------
testtools.testresult.real._StringException: Traceback (most recent call last):
  File "/usr/lib/python3.10/unittest/mock.py", line 1369, in patched
    return func(*newargs, **newkeywargs)
  File "/<<PKGBUILDDIR>>/networking_hyperv/tests/unit/neutron/agent/test_layer2.py", line 386, in test_create_event_listeners
    self._agent._create_event_listeners()
  File "/<<PKGBUILDDIR>>/networking_hyperv/neutron/agent/layer2.py", line 239, in _create_event_listeners
    listener = self._utils.get_vnic_event_listener(event_type)
  File "/usr/lib/python3/dist-packages/oslotest/mock_fixture.py", line 80, in __getattr__
    _lazy_autospec_method(attr, original_attr, eat_self)
  File "/usr/lib/python3/dist-packages/oslotest/mock_fixture.py", line 27, in _lazy_autospec_method
    _lazy_autospec = mock.create_autospec(original_method)
  File "/usr/lib/python3.10/unittest/mock.py", line 2647, in create_autospec
    raise InvalidSpecError(f'Cannot autospec a Mock object. '
unittest.mock.InvalidSpecError: Cannot autospec a Mock object. [object=<_AutospecMagicMock name='_get_class().get_vnic_event_listener' id='140569743232640'>]

A similar issue is being fixed in ironic at https://review.opendev.org/c/openstack/ironic/+/821370

Revision history for this message
Corey Bryant (corey.bryant) wrote :

Fairly certain this is not an oslotest issue after looking at the documentation at:
https://docs.openstack.org/oslotest/latest/user/mock-autospec.html#mockautospecfixture

Revision history for this message
Corey Bryant (corey.bryant) wrote :

Removing the MockAutospecFixture() entirely works around this issue. Maybe it's not actually needed? If it is needed, I think each individual mock.Mock and mock.MagicMock needs to be inspected to ensure they are not autospec-ing Mock objects.

+--- a/networking_hyperv/tests/base.py
++++ b/networking_hyperv/tests/base.py
+@@ -50,7 +50,6 @@
+
+ def setUp(self):
+ super(BaseTestCase, self).setUp()
+- self.useFixture(mock_fixture.MockAutospecFixture())
+ self._patch_autospec_classes()
+
+ self.addCleanup(CONF.reset)

no longer affects: oslotest
Changed in networking-hyperv:
status: New → In Progress
Revision history for this message
Lucian Petrut (petrutlucian94) wrote :

Thanks for reporting this issue. It's covered by this fix: https://review.opendev.org/c/openstack/networking-hyperv/+/839755

Changed in networking-hyperv:
assignee: nobody → Lucian Petrut (petrutlucian94)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to networking-hyperv (master)

Reviewed: https://review.opendev.org/c/openstack/networking-hyperv/+/839755
Committed: https://opendev.org/openstack/networking-hyperv/commit/305dcfafc8796a0aba66b73eeb2d00445d4127fb
Submitter: "Zuul (22348)"
Branch: master

commit 305dcfafc8796a0aba66b73eeb2d00445d4127fb
Author: Lucian Petrut <email address hidden>
Date: Thu Apr 28 15:22:07 2022 +0300

    Avoid mocking the same objects twice

    Starting with Python 3.10, the (now standard) mock library
    no longer allows mocking the same object twice.

    Some utils classes are currently mocked twice. We'll avoid this,
    yet ensuring that the autospec feature is properly used.

    Closes-Bug: #1954512

    Change-Id: I7a2ba44b873ae72d675512d03cd26b69acb9383e

Changed in networking-hyperv:
status: In Progress → Fix Released
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix included in openstack/networking-hyperv 13.0.0.0rc1

This issue was fixed in the openstack/networking-hyperv 13.0.0.0rc1 release candidate.

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.