From 85e56123508a009b602bf0d34d89c6101a5fcf35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A9ri=20Le=20Bouder?= Date: Fri, 30 Sep 2022 16:31:05 -0400 Subject: [PATCH 1/4] import tox.ini (#1090) import tox.ini much faster than the other tests (less than 5 minutes), we don't need to build the collection first or to pull the ansible-test container works from anyway, no need to copy the collection in .ansible/collections/ansible_collections/amazon/aws first generate HTML coverage, e.g: https://c8cca5e7f93c66c3bfca-5f43057c94d0562db863016349fecb6f.ssl.cf5.rackcdn.com/1090/f1142b4b561938403637af89bfe76169da025d75/check/cloud-tox-py39/562db5d/docs/coverage/index.html test with py39 currently, but we can extend the matrix adjustment to properly reset some global variables. The problem doesn't happen with ansible-test because the tests are run in parallel and thus, there is no shared memory space. Reviewed-by: Mark Chappell (cherry picked from commit 1cac4da00f7013afdc35be25d402cdd5169292d5) --- tests/unit/module_utils/conftest.py | 9 +++++++++ .../test_minimal_versions.py | 1 + tox.ini | 20 +++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 tox.ini diff --git a/tests/unit/module_utils/conftest.py b/tests/unit/module_utils/conftest.py index 8bc13c4d55c..f90055615d1 100644 --- a/tests/unit/module_utils/conftest.py +++ b/tests/unit/module_utils/conftest.py @@ -7,10 +7,12 @@ import json import sys from io import BytesIO +import warnings import pytest import ansible.module_utils.basic +import ansible.module_utils.common from ansible.module_utils.six import PY3, string_types from ansible.module_utils._text import to_bytes from ansible.module_utils.common._collections_compat import MutableMapping @@ -23,6 +25,13 @@ def stdin(mocker, request): old_argv = sys.argv sys.argv = ['ansible_unittest'] + for var in ["_global_warnings", "_global_deprecations"]: + if hasattr(ansible.module_utils.common.warnings, var): + setattr(ansible.module_utils.common.warnings, var, []) + else: + # No need to reset the value + warnings.warn("deprecated") + if isinstance(request.param, string_types): args = request.param elif isinstance(request.param, MutableMapping): diff --git a/tests/unit/module_utils/core/ansible_aws_module/test_minimal_versions.py b/tests/unit/module_utils/core/ansible_aws_module/test_minimal_versions.py index 92373392271..8a1b07b39a9 100644 --- a/tests/unit/module_utils/core/ansible_aws_module/test_minimal_versions.py +++ b/tests/unit/module_utils/core/ansible_aws_module/test_minimal_versions.py @@ -10,6 +10,7 @@ import botocore import boto3 import json +import warnings from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule from pprint import pprint diff --git a/tox.ini b/tox.ini new file mode 100644 index 00000000000..04a0e5bf7d1 --- /dev/null +++ b/tox.ini @@ -0,0 +1,20 @@ +[tox] +skipsdist=True +envlist = clean,ansible{2.12,2.13}-py{38,39,310}-{with_constraints,without_constraints} + +[testenv] +deps = + pytest + pytest-cov + ansible2.12: ansible-core>2.12,<2.13 + ansible2.13: ansible-core>2.13,<2.14 + !ansible2.12-!ansible2.13: ansible-core + pytest-ansible-units + -rtest-requirements.txt + with_constraints: -rtests/unit/constraints.txt +commands = pytest --cov-report html --cov plugins {posargs:tests/} + +[testenv:clean] +deps = coverage +skip_install = true +commands = coverage erase From 946b457d45b11762b9303f4537bb0a82a3cf4849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A9ri=20Le=20Bouder?= Date: Fri, 23 Sep 2022 03:10:20 -0400 Subject: [PATCH 2/4] test-requirements.txt: relax the dependency on pytest (#1059) test-requirements.txt: relax the dependency on pytest pytest 6.2.0 is getting old and a recent version should work as expected. Reviewed-by: Mark Chappell --- test-requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-requirements.txt b/test-requirements.txt index 54d52b36666..77a00682cd8 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -5,7 +5,7 @@ boto coverage==4.5.4 placebo mock -pytest==6.2.0 +pytest pytest-xdist # We should avoid these two modules with py3 pytest-mock From f02d0f656bb5f450857ff34ea88a64465eaad724 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Tue, 4 Oct 2022 17:10:04 +0200 Subject: [PATCH 3/4] Fixup module path for ec2.is_outposts_arn --- tests/unit/module_utils/test_ec2.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/module_utils/test_ec2.py b/tests/unit/module_utils/test_ec2.py index fe66556bcb8..8c7b435f662 100644 --- a/tests/unit/module_utils/test_ec2.py +++ b/tests/unit/module_utils/test_ec2.py @@ -5,7 +5,8 @@ from __future__ import (absolute_import, division, print_function) -from plugins.module_utils.ec2 import is_outposts_arn +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import is_outposts_arn + __metaclass__ = type import unittest From 4384819bae27852a4b835c574e5ee498ad62e588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A9ri=20Le=20Bouder?= Date: Mon, 3 Oct 2022 13:26:19 -0400 Subject: [PATCH 4/4] unit-tests: module_utils/test_rds.py requires py37+ (#1100) unit-tests: module_utils/test_rds.py requires py37+ contextlib.nullcontext was introduced with Python 3.7 Reviewed-by: Alina Buzachis --- .../fragments/unit-tests_test_rds_py37_only.yaml | 3 +++ tests/unit/module_utils/test_rds.py | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/unit-tests_test_rds_py37_only.yaml diff --git a/changelogs/fragments/unit-tests_test_rds_py37_only.yaml b/changelogs/fragments/unit-tests_test_rds_py37_only.yaml new file mode 100644 index 00000000000..79fde89fce8 --- /dev/null +++ b/changelogs/fragments/unit-tests_test_rds_py37_only.yaml @@ -0,0 +1,3 @@ +--- +trivial: +- "module_utils/test_rds.py unit-tests only works with Python 3.7." diff --git a/tests/unit/module_utils/test_rds.py b/tests/unit/module_utils/test_rds.py index c6ad8b34284..200f74acf12 100644 --- a/tests/unit/module_utils/test_rds.py +++ b/tests/unit/module_utils/test_rds.py @@ -7,18 +7,27 @@ __metaclass__ = type +import sys +import pytest + +if sys.version_info < (3, 7): + pytest.skip("contextlib.nullcontext was introduced in Python 3.7", allow_module_level=True) + from ansible_collections.amazon.aws.plugins.module_utils import rds from ansible_collections.amazon.aws.tests.unit.compat import unittest from ansible_collections.amazon.aws.tests.unit.compat.mock import MagicMock - -from contextlib import nullcontext -import pytest +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import HAS_BOTO3 try: from botocore.exceptions import ClientError, WaiterError except ImportError: pass +from contextlib import nullcontext + +if not HAS_BOTO3: + pytestmark = pytest.mark.skip("test_rds.py requires the python modules 'boto3' and 'botocore'") + def expected(x): return x, nullcontext()