unit tests are redefining datetime.datetime.now and should just use mock instead

Bug #2045588 reported by Corey Bryant
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
OpenStack Swift Storage Charm
Fix Committed
Undecided
Unassigned
2023.1
Fix Committed
Undecided
Unassigned
2023.2
Fix Committed
Undecided
Unassigned
Xena
Fix Committed
Undecided
Unassigned
Yoga
Fix Committed
Undecided
Unassigned
Zed
Fix Committed
Undecided
Unassigned

Bug Description

As seen in the failures here:
https://review.opendev.org/c/openstack/charm-swift-storage/+/899610

charm-swift-storage is redefining the standard library's datetime.datetime.now() method:
https://opendev.org/openstack/charm-swift-storage/src/branch/master/unit_tests/test_check_swift_storage.py#L45

and it is causing unintended side-effects.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to charm-swift-storage (master)
Changed in charm-swift-storage:
status: New → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to charm-swift-storage (master)

Reviewed: https://review.opendev.org/c/openstack/charm-swift-storage/+/902594
Committed: https://opendev.org/openstack/charm-swift-storage/commit/72e22a94ed4966ff6fb2fba41bac1b204dbd4c14
Submitter: "Zuul (22348)"
Branch: master

commit 72e22a94ed4966ff6fb2fba41bac1b204dbd4c14
Author: Corey Bryant <email address hidden>
Date: Mon Dec 4 15:47:20 2023 -0500

    Mock datetime instead of redefining standard library

    This fixes up the test to mock the datetime calls specifically in
    the test that needs to mock them, rather than the prior approach
    which was redefining the behavior of the standard library function
    globally.

    Closes-Bug: #2045588
    Change-Id: I9c585c91c5527a61e4a8dbcba0a8a01108cd9b2e

Changed in charm-swift-storage:
status: In Progress → Fix Committed
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to charm-swift-storage (stable/2023.2)

Fix proposed to branch: stable/2023.2
Review: https://review.opendev.org/c/openstack/charm-swift-storage/+/902540

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to charm-swift-storage (stable/2023.1)

Fix proposed to branch: stable/2023.1
Review: https://review.opendev.org/c/openstack/charm-swift-storage/+/902661

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to charm-swift-storage (stable/zed)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to charm-swift-storage (stable/yoga)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to charm-swift-storage (stable/xena)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to charm-swift-storage (stable/2023.2)

Reviewed: https://review.opendev.org/c/openstack/charm-swift-storage/+/902540
Committed: https://opendev.org/openstack/charm-swift-storage/commit/f8cfe9338735c3f0342dffb14a623370300d3ade
Submitter: "Zuul (22348)"
Branch: stable/2023.2

commit f8cfe9338735c3f0342dffb14a623370300d3ade
Author: Corey Bryant <email address hidden>
Date: Mon Dec 4 15:47:20 2023 -0500

    Mock datetime instead of redefining standard library

    This fixes up the test to mock the datetime calls specifically in
    the test that needs to mock them, rather than the prior approach
    which was redefining the behavior of the standard library function
    globally.

    Closes-Bug: #2045588
    Change-Id: I9c585c91c5527a61e4a8dbcba0a8a01108cd9b2e
    (cherry picked from commit 72e22a94ed4966ff6fb2fba41bac1b204dbd4c14)

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to charm-swift-storage (stable/2023.1)

Reviewed: https://review.opendev.org/c/openstack/charm-swift-storage/+/902661
Committed: https://opendev.org/openstack/charm-swift-storage/commit/a9d5d3b1864835224b21a885b7a172d8f5901d1c
Submitter: "Zuul (22348)"
Branch: stable/2023.1

commit a9d5d3b1864835224b21a885b7a172d8f5901d1c
Author: Corey Bryant <email address hidden>
Date: Mon Dec 4 15:47:20 2023 -0500

    Mock datetime instead of redefining standard library

    This fixes up the test to mock the datetime calls specifically in
    the test that needs to mock them, rather than the prior approach
    which was redefining the behavior of the standard library function
    globally.

    Closes-Bug: #2045588
    Change-Id: I9c585c91c5527a61e4a8dbcba0a8a01108cd9b2e
    (cherry picked from commit 72e22a94ed4966ff6fb2fba41bac1b204dbd4c14)

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to charm-swift-storage (stable/zed)

Reviewed: https://review.opendev.org/c/openstack/charm-swift-storage/+/902662
Committed: https://opendev.org/openstack/charm-swift-storage/commit/897e34f1dfa79cc2826d40adf18e929476bf9def
Submitter: "Zuul (22348)"
Branch: stable/zed

commit 897e34f1dfa79cc2826d40adf18e929476bf9def
Author: Corey Bryant <email address hidden>
Date: Mon Dec 4 15:47:20 2023 -0500

    Mock datetime instead of redefining standard library

    This fixes up the test to mock the datetime calls specifically in
    the test that needs to mock them, rather than the prior approach
    which was redefining the behavior of the standard library function
    globally.

    Additionally, to fix the py310 unit tests, jsonschema is pinned to
    the latest version that doesn't depend (indirectly) on Rust. This
    is not part of the original cherry-pick but is needed in this change
    in order to fix py310 unit tests. This is not needed for stable/2023.1
    and above, where commit da0f33c95bbf93d09011990847d2ff10ba3358f1 (Fix
    charm for tox4 compatibility) fixed this issue. Stable branches
    prior to stable/2023.1 pinned tox to < 4.0.0.

    Closes-Bug: #2045588
    Change-Id: I9c585c91c5527a61e4a8dbcba0a8a01108cd9b2e
    (cherry picked from commit 72e22a94ed4966ff6fb2fba41bac1b204dbd4c14)

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to charm-swift-storage (stable/yoga)

Reviewed: https://review.opendev.org/c/openstack/charm-swift-storage/+/902663
Committed: https://opendev.org/openstack/charm-swift-storage/commit/468cd574c14ff19ad2b21196612e7ae28dff9e51
Submitter: "Zuul (22348)"
Branch: stable/yoga

commit 468cd574c14ff19ad2b21196612e7ae28dff9e51
Author: Corey Bryant <email address hidden>
Date: Mon Dec 4 15:47:20 2023 -0500

    Mock datetime instead of redefining standard library

    This fixes up the test to mock the datetime calls specifically in
    the test that needs to mock them, rather than the prior approach
    which was redefining the behavior of the standard library function
    globally.

    Closes-Bug: #2045588
    Change-Id: I9c585c91c5527a61e4a8dbcba0a8a01108cd9b2e
    (cherry picked from commit 72e22a94ed4966ff6fb2fba41bac1b204dbd4c14)

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to charm-swift-storage (stable/xena)

Reviewed: https://review.opendev.org/c/openstack/charm-swift-storage/+/902664
Committed: https://opendev.org/openstack/charm-swift-storage/commit/9671fa85e06c9572111246151f459b2b0624a34f
Submitter: "Zuul (22348)"
Branch: stable/xena

commit 9671fa85e06c9572111246151f459b2b0624a34f
Author: Corey Bryant <email address hidden>
Date: Mon Dec 4 15:47:20 2023 -0500

    Mock datetime instead of redefining standard library

    This fixes up the test to mock the datetime calls specifically in
    the test that needs to mock them, rather than the prior approach
    which was redefining the behavior of the standard library function
    globally.

    This change also pins openstacksdk to fix python 3.6 unit tests.
    Commit b34de32a in openstacksdk replaced the appdirs library with
    the platformdirs library. The platformdirs library is not python 3.6
    compatible and causes some challenges. Pin the openstacksdk to a
    lower version. Thanks to Billy Olsen for this patch. Taken from:
    https://review.opendev.org/c/openstack/charm-neutron-api/+/902607

    Closes-Bug: #2045588
    Change-Id: I9c585c91c5527a61e4a8dbcba0a8a01108cd9b2e
    (cherry picked from commit 72e22a94ed4966ff6fb2fba41bac1b204dbd4c14)

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.