Skip to content

Commit 7d70498

Browse files
authored
Adjust monkey_patching to work under pytest (#5689)
* pytest: adjust monkey_patching to work under pytest * pytest: monkey_patch test files that have mongoengine imported too soon When mongoengine is imported too soon, the test hangs. * add changelog entry
1 parent 6cab171 commit 7d70498

22 files changed

+136
-2
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Fixed
2727
* Fixed schema utils to more reliably handle schemas that define nested arrays (object-array-object-array-string) as discovered in some
2828
of the ansible installer RBAC tests (see #5684). This includes a test that reproduced the error so we don't hit this again. #5685
2929

30+
* Fixed eventlet monkey patching so more of the unit tests work under pytest. #5689
31+
3032
Added
3133
~~~~~
3234

conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2022 The StackStorm Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def pytest_configure(config):
17+
import sys
18+
19+
sys._called_from_test = True
20+
21+
22+
def pytest_unconfigure(config):
23+
import sys
24+
25+
del sys._called_from_test

st2api/tests/unit/controllers/v1/test_action_views.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
# pytest: make sure monkey_patching happens before importing mongoengine
17+
from st2common.util.monkey_patch import monkey_patch
18+
19+
monkey_patch()
20+
1621
import mock
1722

1823
from st2common.content import utils as content_utils

st2api/tests/unit/controllers/v1/test_executions_auth.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
# pytest: make sure monkey_patching happens before importing mongoengine
17+
from st2common.util.monkey_patch import monkey_patch
18+
19+
monkey_patch()
20+
1621
import bson
1722
import copy
1823
import datetime

st2api/tests/unit/controllers/v1/test_rule_views.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
# pytest: make sure monkey_patching happens before importing mongoengine
17+
from st2common.util.monkey_patch import monkey_patch
18+
19+
monkey_patch()
20+
1621
import six
1722

1823
from st2common.models.system.common import ResourceReference

st2api/tests/unit/controllers/v1/test_runnertypes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
# pytest: make sure monkey_patching happens before importing mongoengine
17+
from st2common.util.monkey_patch import monkey_patch
18+
19+
monkey_patch()
20+
1621
from st2api.controllers.v1.runnertypes import RunnerTypesController
1722

1823
from st2tests.api import FunctionalTest

st2api/tests/unit/controllers/v1/test_traces.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
# pytest: make sure monkey_patching happens before importing mongoengine
17+
from st2common.util.monkey_patch import monkey_patch
18+
19+
monkey_patch()
20+
1621
from st2api.controllers.v1.traces import TracesController
1722
from st2tests.fixturesloader import FixturesLoader
1823

st2api/tests/unit/controllers/v1/test_triggertypes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
# pytest: make sure monkey_patching happens before importing mongoengine
17+
from st2common.util.monkey_patch import monkey_patch
18+
19+
monkey_patch()
20+
1621
import six
1722

1823
from st2api.controllers.v1.triggers import TriggerTypeController

st2auth/tests/unit/controllers/v1/test_token.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
# pytest: make sure monkey_patching happens before importing mongoengine
17+
from st2common.util.monkey_patch import monkey_patch
18+
19+
monkey_patch()
20+
1621
import uuid
1722
import datetime
1823
import random

st2common/st2common/models/db/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@
3636
# For now, we go with option 2) since it seems to be good enough of a compromise. We detect if we
3737
# are running inside tests by checking if "nose" module is present - the same logic we already use
3838
# in a couple of other places (and something which would need to be changed if we switch to pytest).
39-
if "nose" in sys.modules.keys():
39+
# For pytest, we set sys._called_from_test in conftest.py
40+
if "nose" in sys.modules.keys() or hasattr(sys, "_called_from_test"):
41+
# pytest can load any test file first, which randomizes where the monkey_patch is needed.
42+
# thus mongoengine might already be loaded at this point under pytest!
43+
# In that case, we just add the monkey_patch to the top of that test file.
4044
from st2common.util.monkey_patch import monkey_patch
4145

4246
monkey_patch()

0 commit comments

Comments
 (0)