Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Fixed
* Fixed schema utils to more reliably handle schemas that define nested arrays (object-array-object-array-string) as discovered in some
of the ansible installer RBAC tests (see #5684). This includes a test that reproduced the error so we don't hit this again. #5685

* Fixed eventlet monkey patching so more of the unit tests work under pytest. #5689

Added
~~~~~

Expand Down
25 changes: 25 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2022 The StackStorm Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def pytest_configure(config):
import sys

sys._called_from_test = True


def pytest_unconfigure(config):
import sys

del sys._called_from_test
5 changes: 5 additions & 0 deletions st2api/tests/unit/controllers/v1/test_action_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import mock

from st2common.content import utils as content_utils
Expand Down
5 changes: 5 additions & 0 deletions st2api/tests/unit/controllers/v1/test_executions_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import bson
import copy
import datetime
Expand Down
5 changes: 5 additions & 0 deletions st2api/tests/unit/controllers/v1/test_rule_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import six

from st2common.models.system.common import ResourceReference
Expand Down
5 changes: 5 additions & 0 deletions st2api/tests/unit/controllers/v1/test_runnertypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

from st2api.controllers.v1.runnertypes import RunnerTypesController

from st2tests.api import FunctionalTest
Expand Down
5 changes: 5 additions & 0 deletions st2api/tests/unit/controllers/v1/test_traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

from st2api.controllers.v1.traces import TracesController
from st2tests.fixturesloader import FixturesLoader

Expand Down
5 changes: 5 additions & 0 deletions st2api/tests/unit/controllers/v1/test_triggertypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import six

from st2api.controllers.v1.triggers import TriggerTypeController
Expand Down
5 changes: 5 additions & 0 deletions st2auth/tests/unit/controllers/v1/test_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

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

monkey_patch()
Expand Down
6 changes: 5 additions & 1 deletion st2common/st2common/util/monkey_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ def use_select_poll_workaround(nose_only=True):
import eventlet

# Work around to get tests to pass with eventlet >= 0.20.0
if not nose_only or (nose_only and "nose" in sys.modules.keys()):
if not nose_only or (
nose_only
# sys._called_from_test set in conftest.py for pytest runs
and ("nose" in sys.modules.keys() or hasattr(sys, "_called_from_test"))
):
# Add back blocking poll() to eventlet monkeypatched select
original_poll = eventlet.patcher.original("select").poll
select.poll = original_poll
Expand Down
6 changes: 6 additions & 0 deletions st2common/tests/unit/test_action_param_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
# limitations under the License.

from __future__ import absolute_import

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import copy
import six

Expand Down
6 changes: 6 additions & 0 deletions st2common/tests/unit/test_db_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
# limitations under the License.

from __future__ import absolute_import

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import mongoengine

from st2common.models.db import stormbase
Expand Down
6 changes: 6 additions & 0 deletions st2common/tests/unit/test_db_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
import unittest2
import orjson
import zstandard

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import mongoengine as me

from st2common.fields import ComplexDateTimeField
Expand Down
5 changes: 5 additions & 0 deletions st2common/tests/unit/test_param_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

from __future__ import absolute_import

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import six
import mock

Expand Down
6 changes: 6 additions & 0 deletions st2common/tests/unit/test_purge_executions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
# limitations under the License.

from __future__ import absolute_import

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import copy
from datetime import timedelta

Expand Down
6 changes: 6 additions & 0 deletions st2common/tests/unit/test_purge_trigger_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
# limitations under the License.

from __future__ import absolute_import

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

from datetime import timedelta

from st2common import log as logging
Expand Down
6 changes: 6 additions & 0 deletions st2common/tests/unit/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
# limitations under the License.

from __future__ import absolute_import

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import copy
import mock
import mongoengine
Expand Down
6 changes: 6 additions & 0 deletions st2common/tests/unit/test_runners_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
# limitations under the License.

from __future__ import absolute_import

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import mock

from st2common.runners import utils
Expand Down
6 changes: 6 additions & 0 deletions st2common/tests/unit/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
# limitations under the License.

from __future__ import absolute_import

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import random
import string

Expand Down
5 changes: 5 additions & 0 deletions st2reactor/tests/unit/test_enforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

from __future__ import absolute_import

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import mock

from st2common.constants import action as action_constants
Expand Down
6 changes: 6 additions & 0 deletions st2reactor/tests/unit/test_rule_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
# limitations under the License.

from __future__ import absolute_import

# pytest: make sure monkey_patching happens before importing mongoengine
from st2common.util.monkey_patch import monkey_patch

monkey_patch()

import mock
from mongoengine import NotUniqueError

Expand Down