tests - Python 3.12 - TypeError: Object of type _SentinelObject is not JSON serializable

Bug #2051928 reported by James Page
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
neutron
Fix Released
High
Rodolfo Alonso

Bug Description

Executing unit tests with Python 3.12 results in some test failures which I think are todo with the way the unit tests mock the __json__ method in the tools module:

neutron.tests.unit.api.v2.test_base.RegistryNotificationTest.test_networks_create_bulk_registry_publish
-------------------------------------------------------------------------------------------------------

Captured traceback:
~~~~~~~~~~~~~~~~~~~
    Traceback (most recent call last):

      File "/home/jamespage/src/upstream/openstack/neutron/neutron/tests/base.py", line 178, in func
    return f(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^

      File "/home/jamespage/src/upstream/openstack/neutron/neutron/tests/unit/api/v2/test_base.py", line 1300, in test_networks_create_bulk_registry_publish
    self._test_registry_publish('create', 'network', input)

      File "/home/jamespage/src/upstream/openstack/neutron/neutron/tests/unit/api/v2/test_base.py", line 1269, in _test_registry_publish
    res = self.api.post_json(
          ^^^^^^^^^^^^^^^^^^^

No tests were successful during the run
      File "/home/jamespage/src/upstream/openstack/neutron/.tox/py312/lib/python3.12/site-packages/webtest/utils.py", line 34, in wrapper
    return self._gen_request(method, url, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

      File "/home/jamespage/src/upstream/openstack/neutron/.tox/py312/lib/python3.12/site-packages/webtest/app.py", line 749, in _gen_request
    return self.do_request(req, status=status,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

      File "/home/jamespage/src/upstream/openstack/neutron/.tox/py312/lib/python3.12/site-packages/webtest/app.py", line 646, in do_request
    self._check_status(status, res)

      File "/home/jamespage/src/upstream/openstack/neutron/.tox/py312/lib/python3.12/site-packages/webtest/app.py", line 675, in _check_status
    raise AppError(

    webtest.app.AppError: Bad response: 500 Internal Server Error (not 200 OK or 3xx redirect for http://localhost/networks)
b'{"NeutronError": {"type": "HTTPInternalServerError", "message": "Request Failed: internal server error while processing your request.", "detail": ""}}'

Captured pythonlogging:
~~~~~~~~~~~~~~~~~~~~~~~
       ERROR [neutron.pecan_wsgi.hooks.translation] POST failed.
Traceback (most recent call last):
  File "/home/jamespage/src/upstream/openstack/neutron/.tox/py312/lib/python3.12/site-packages/pecan/core.py", line 682, in __call__
    self.invoke_controller(controller, args, kwargs, state)
  File "/home/jamespage/src/upstream/openstack/neutron/.tox/py312/lib/python3.12/site-packages/pecan/core.py", line 603, in invoke_controller
    result = self.render(template, result)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jamespage/src/upstream/openstack/neutron/.tox/py312/lib/python3.12/site-packages/pecan/core.py", line 414, in render
    return renderer.render(template, namespace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jamespage/src/upstream/openstack/neutron/.tox/py312/lib/python3.12/site-packages/pecan/templating.py", line 23, in render
    return encode(namespace)
           ^^^^^^^^^^^^^^^^^
  File "/home/jamespage/src/upstream/openstack/neutron/.tox/py312/lib/python3.12/site-packages/pecan/jsonify.py", line 154, in encode
    return _instance.encode(obj)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/home/jamespage/src/upstream/openstack/neutron/.tox/py312/lib/python3.12/site-packages/pecan/jsonify.py", line 148, in default
    return jsonify(obj)
           ^^^^^^^^^^^^
  File "/usr/lib/python3.12/functools.py", line 909, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jamespage/src/upstream/openstack/neutron/.tox/py312/lib/python3.12/site-packages/pecan/jsonify.py", line 143, in jsonify
    return _default.default(obj)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/jamespage/src/upstream/openstack/neutron/.tox/py312/lib/python3.12/site-packages/pecan/jsonify.py", line 129, in default
    return JSONEncoder.default(self, obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type _SentinelObject is not JSON serializable

Digging in I can see all of the plugin child calls being updated, however I don't see them actually called under Python 3.12.

This issue impacts the following unit tests:

FAIL: neutron.tests.unit.api.v2.test_base.RegistryNotificationTest.test_network_create_registry_publish
FAIL: neutron.tests.unit.api.v2.test_base.RegistryNotificationTest.test_network_update_registry_publish
FAIL: neutron.tests.unit.api.v2.test_base.RegistryNotificationTest.test_networks_create_bulk_registry_publish
FAIL: neutron.tests.unit.api.v2.test_base.NotificationTest.test_network_create_notifier
FAIL: neutron.tests.unit.api.v2.test_base.NotificationTest.test_network_update_notifier
FAIL: neutron.tests.unit.api.v2.test_base.QuotaTest.test_create_network_quota_without_limit
FAIL: neutron.tests.unit.extensions.test_providernet.ProvidernetExtensionTestCase.test_network_create_with_provider_attrs
FAIL: neutron.tests.unit.extensions.test_providernet.ProvidernetExtensionTestCase.test_network_update_with_provider_attrs

James Page (james-page)
description: updated
Revision history for this message
Miguel Lavalle (minsel) wrote :

@James Page,

Do you intend to fix this issue?

Changed in neutron:
importance: Undecided → High
Revision history for this message
James Page (james-page) wrote :

@minsel

I spent some time investigating and can see the same set of mocks and associated dunder methods being created between 3.11 and 3.12 but under 3.12 I don't see them being used which has me scratching my head.

I will pickup investigation again if time permits.

Revision history for this message
Miguel Lavalle (minsel) wrote :

@James Page,

In that case I'll indicate in the weekly report that this bug is in need of an owner

Changed in neutron:
status: New → In Progress
Changed in neutron:
assignee: nobody → Rodolfo Alonso (rodolfo-alonso-hernandez)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to neutron (master)

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

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

Reviewed: https://review.opendev.org/c/openstack/neutron/+/908255
Committed: https://opendev.org/openstack/neutron/commit/4df5f2f41baede43b3e87ff8b716a7e6c773fbea
Submitter: "Zuul (22348)"
Branch: master

commit 4df5f2f41baede43b3e87ff8b716a7e6c773fbea
Author: Rodolfo Alonso Hernandez <email address hidden>
Date: Wed Feb 7 08:15:03 2024 +0000

    [UT] Fix ``APIv2TestBase`` derived test classes

    The method test calling ``_post_request`` with create or update
    operations, were not properly patching the ML2 plugin
    ``create_network`` and `update_network`` methods. With
    Python3.12, the mock object passed (instead of a dictionary with a
    reduced network definition) don't have a ``__json__`` method and
    fails during the encoding.

    NOTE: this has been manually tested with Python3.12. Currently this
    patch cannot be tested in the CI.

    Closes-Bug: #2051928
    Change-Id: Ie30af0c655d2f27d9039ff7933e81603047da6f4

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

This issue was fixed in the openstack/neutron 24.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.