From 765b2bd783b9ba64b7ed5d8504bedbe8265aab0e Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Fri, 14 Jul 2023 17:19:33 -0700 Subject: [PATCH] ansible-test - Narrow the scope of some sanity tests (#81273) * ansible-test - Limit scope of replace-urlopen test Only ansible-core code and plugins in collections can be expected to rely on module_utils. * ansible-test - Limit scope of use-compat-six test Only ansible-core code and plugins in collections can be expected to rely on module_utils. * ansible-test - Limit scope of no-get-exception test Only ansible-core code and plugins in collections should be checked for usage of outdated module_utils functions. * Add integration tests --- changelogs/fragments/ansible-test-sanity-scope.yml | 7 +++++++ .../ansible-test-sanity-no-get-exception/aliases | 4 ++++ .../ansible_collections/ns/col/do-not-check-me.py | 5 +++++ .../ns/col/plugins/modules/check-me.py | 5 +++++ .../expected.txt | 2 ++ .../ansible-test-sanity-no-get-exception/runme.sh | 12 ++++++++++++ .../ansible-test-sanity-replace-urlopen/aliases | 4 ++++ .../ansible_collections/ns/col/do-not-check-me.py | 5 +++++ .../ns/col/plugins/modules/check-me.py | 5 +++++ .../ansible-test-sanity-replace-urlopen/expected.txt | 1 + .../ansible-test-sanity-replace-urlopen/runme.sh | 12 ++++++++++++ .../ansible-test-sanity-use-compat-six/aliases | 4 ++++ .../ansible_collections/ns/col/do-not-check-me.py | 5 +++++ .../ns/col/plugins/modules/check-me.py | 5 +++++ .../ansible-test-sanity-use-compat-six/expected.txt | 1 + .../ansible-test-sanity-use-compat-six/runme.sh | 12 ++++++++++++ .../sanity/code-smell/no-get-exception.json | 4 ++++ .../sanity/code-smell/replace-urlopen.json | 4 ++++ .../controller/sanity/code-smell/use-compat-six.json | 4 ++++ test/sanity/ignore.txt | 5 ----- 20 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/ansible-test-sanity-scope.yml create mode 100644 test/integration/targets/ansible-test-sanity-no-get-exception/aliases create mode 100644 test/integration/targets/ansible-test-sanity-no-get-exception/ansible_collections/ns/col/do-not-check-me.py create mode 100644 test/integration/targets/ansible-test-sanity-no-get-exception/ansible_collections/ns/col/plugins/modules/check-me.py create mode 100644 test/integration/targets/ansible-test-sanity-no-get-exception/expected.txt create mode 100755 test/integration/targets/ansible-test-sanity-no-get-exception/runme.sh create mode 100644 test/integration/targets/ansible-test-sanity-replace-urlopen/aliases create mode 100644 test/integration/targets/ansible-test-sanity-replace-urlopen/ansible_collections/ns/col/do-not-check-me.py create mode 100644 test/integration/targets/ansible-test-sanity-replace-urlopen/ansible_collections/ns/col/plugins/modules/check-me.py create mode 100644 test/integration/targets/ansible-test-sanity-replace-urlopen/expected.txt create mode 100755 test/integration/targets/ansible-test-sanity-replace-urlopen/runme.sh create mode 100644 test/integration/targets/ansible-test-sanity-use-compat-six/aliases create mode 100644 test/integration/targets/ansible-test-sanity-use-compat-six/ansible_collections/ns/col/do-not-check-me.py create mode 100644 test/integration/targets/ansible-test-sanity-use-compat-six/ansible_collections/ns/col/plugins/modules/check-me.py create mode 100644 test/integration/targets/ansible-test-sanity-use-compat-six/expected.txt create mode 100755 test/integration/targets/ansible-test-sanity-use-compat-six/runme.sh diff --git a/changelogs/fragments/ansible-test-sanity-scope.yml b/changelogs/fragments/ansible-test-sanity-scope.yml new file mode 100644 index 00000000000000..56e50714d6b9dc --- /dev/null +++ b/changelogs/fragments/ansible-test-sanity-scope.yml @@ -0,0 +1,7 @@ +minor_changes: + - ansible-test - The ``replace-urlopen`` sanity test is now limited to plugins in collections. + Previously any Python file in a collection was checked for ``urlopen`` usage. + - ansible-test - The ``use-compat-six`` sanity test is now limited to plugins in collections. + Previously any Python file in a collection was checked for ``six`` usage. + - ansible-test - The ``no-get-exception`` sanity test is now limited to plugins in collections. + Previously any Python file in a collection was checked for ``get_exception`` usage. diff --git a/test/integration/targets/ansible-test-sanity-no-get-exception/aliases b/test/integration/targets/ansible-test-sanity-no-get-exception/aliases new file mode 100644 index 00000000000000..7741d44451547b --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-no-get-exception/aliases @@ -0,0 +1,4 @@ +shippable/posix/group3 # runs in the distro test containers +shippable/generic/group1 # runs in the default test container +context/controller +needs/target/collection diff --git a/test/integration/targets/ansible-test-sanity-no-get-exception/ansible_collections/ns/col/do-not-check-me.py b/test/integration/targets/ansible-test-sanity-no-get-exception/ansible_collections/ns/col/do-not-check-me.py new file mode 100644 index 00000000000000..ca25269938e507 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-no-get-exception/ansible_collections/ns/col/do-not-check-me.py @@ -0,0 +1,5 @@ +from ansible.module_utils.pycompat24 import get_exception + + +def do_stuff(): + get_exception() diff --git a/test/integration/targets/ansible-test-sanity-no-get-exception/ansible_collections/ns/col/plugins/modules/check-me.py b/test/integration/targets/ansible-test-sanity-no-get-exception/ansible_collections/ns/col/plugins/modules/check-me.py new file mode 100644 index 00000000000000..ca25269938e507 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-no-get-exception/ansible_collections/ns/col/plugins/modules/check-me.py @@ -0,0 +1,5 @@ +from ansible.module_utils.pycompat24 import get_exception + + +def do_stuff(): + get_exception() diff --git a/test/integration/targets/ansible-test-sanity-no-get-exception/expected.txt b/test/integration/targets/ansible-test-sanity-no-get-exception/expected.txt new file mode 100644 index 00000000000000..4c432cb184db37 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-no-get-exception/expected.txt @@ -0,0 +1,2 @@ +plugins/modules/check-me.py:1:44: do not use `get_exception` +plugins/modules/check-me.py:5:4: do not use `get_exception` diff --git a/test/integration/targets/ansible-test-sanity-no-get-exception/runme.sh b/test/integration/targets/ansible-test-sanity-no-get-exception/runme.sh new file mode 100755 index 00000000000000..b8ec2d04d24402 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-no-get-exception/runme.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +set -eu + +source ../collection/setup.sh + +set -x + +ansible-test sanity --test no-get-exception --color --lint --failure-ok "${@}" > actual.txt + +diff -u "${TEST_DIR}/expected.txt" actual.txt +diff -u do-not-check-me.py plugins/modules/check-me.py diff --git a/test/integration/targets/ansible-test-sanity-replace-urlopen/aliases b/test/integration/targets/ansible-test-sanity-replace-urlopen/aliases new file mode 100644 index 00000000000000..7741d44451547b --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-replace-urlopen/aliases @@ -0,0 +1,4 @@ +shippable/posix/group3 # runs in the distro test containers +shippable/generic/group1 # runs in the default test container +context/controller +needs/target/collection diff --git a/test/integration/targets/ansible-test-sanity-replace-urlopen/ansible_collections/ns/col/do-not-check-me.py b/test/integration/targets/ansible-test-sanity-replace-urlopen/ansible_collections/ns/col/do-not-check-me.py new file mode 100644 index 00000000000000..9b9c7e69c3007f --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-replace-urlopen/ansible_collections/ns/col/do-not-check-me.py @@ -0,0 +1,5 @@ +import urllib.request + + +def do_stuff(): + urllib.request.urlopen('https://www.ansible.com/') diff --git a/test/integration/targets/ansible-test-sanity-replace-urlopen/ansible_collections/ns/col/plugins/modules/check-me.py b/test/integration/targets/ansible-test-sanity-replace-urlopen/ansible_collections/ns/col/plugins/modules/check-me.py new file mode 100644 index 00000000000000..9b9c7e69c3007f --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-replace-urlopen/ansible_collections/ns/col/plugins/modules/check-me.py @@ -0,0 +1,5 @@ +import urllib.request + + +def do_stuff(): + urllib.request.urlopen('https://www.ansible.com/') diff --git a/test/integration/targets/ansible-test-sanity-replace-urlopen/expected.txt b/test/integration/targets/ansible-test-sanity-replace-urlopen/expected.txt new file mode 100644 index 00000000000000..4dd1bfb06e68d4 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-replace-urlopen/expected.txt @@ -0,0 +1 @@ +plugins/modules/check-me.py:5:20: use `ansible.module_utils.urls.open_url` instead of `urlopen` diff --git a/test/integration/targets/ansible-test-sanity-replace-urlopen/runme.sh b/test/integration/targets/ansible-test-sanity-replace-urlopen/runme.sh new file mode 100755 index 00000000000000..e6637c5780e12c --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-replace-urlopen/runme.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +set -eu + +source ../collection/setup.sh + +set -x + +ansible-test sanity --test replace-urlopen --color --lint --failure-ok "${@}" > actual.txt + +diff -u "${TEST_DIR}/expected.txt" actual.txt +diff -u do-not-check-me.py plugins/modules/check-me.py diff --git a/test/integration/targets/ansible-test-sanity-use-compat-six/aliases b/test/integration/targets/ansible-test-sanity-use-compat-six/aliases new file mode 100644 index 00000000000000..7741d44451547b --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-use-compat-six/aliases @@ -0,0 +1,4 @@ +shippable/posix/group3 # runs in the distro test containers +shippable/generic/group1 # runs in the default test container +context/controller +needs/target/collection diff --git a/test/integration/targets/ansible-test-sanity-use-compat-six/ansible_collections/ns/col/do-not-check-me.py b/test/integration/targets/ansible-test-sanity-use-compat-six/ansible_collections/ns/col/do-not-check-me.py new file mode 100644 index 00000000000000..7f7f9f58195e20 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-use-compat-six/ansible_collections/ns/col/do-not-check-me.py @@ -0,0 +1,5 @@ +import six + + +def do_stuff(): + assert six.text_type diff --git a/test/integration/targets/ansible-test-sanity-use-compat-six/ansible_collections/ns/col/plugins/modules/check-me.py b/test/integration/targets/ansible-test-sanity-use-compat-six/ansible_collections/ns/col/plugins/modules/check-me.py new file mode 100644 index 00000000000000..7f7f9f58195e20 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-use-compat-six/ansible_collections/ns/col/plugins/modules/check-me.py @@ -0,0 +1,5 @@ +import six + + +def do_stuff(): + assert six.text_type diff --git a/test/integration/targets/ansible-test-sanity-use-compat-six/expected.txt b/test/integration/targets/ansible-test-sanity-use-compat-six/expected.txt new file mode 100644 index 00000000000000..42ba83ba8da073 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-use-compat-six/expected.txt @@ -0,0 +1 @@ +plugins/modules/check-me.py:1:1: use `ansible.module_utils.six` instead of `six` diff --git a/test/integration/targets/ansible-test-sanity-use-compat-six/runme.sh b/test/integration/targets/ansible-test-sanity-use-compat-six/runme.sh new file mode 100755 index 00000000000000..dbd38f9f36b884 --- /dev/null +++ b/test/integration/targets/ansible-test-sanity-use-compat-six/runme.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +set -eu + +source ../collection/setup.sh + +set -x + +ansible-test sanity --test use-compat-six --color --lint --failure-ok "${@}" > actual.txt + +diff -u "${TEST_DIR}/expected.txt" actual.txt +diff -u do-not-check-me.py plugins/modules/check-me.py diff --git a/test/lib/ansible_test/_util/controller/sanity/code-smell/no-get-exception.json b/test/lib/ansible_test/_util/controller/sanity/code-smell/no-get-exception.json index 88858aeb613d88..da4a0b10f2923c 100644 --- a/test/lib/ansible_test/_util/controller/sanity/code-smell/no-get-exception.json +++ b/test/lib/ansible_test/_util/controller/sanity/code-smell/no-get-exception.json @@ -2,6 +2,10 @@ "extensions": [ ".py" ], + "prefixes": [ + "lib/ansible/", + "plugins/" + ], "ignore_self": true, "output": "path-line-column-message" } diff --git a/test/lib/ansible_test/_util/controller/sanity/code-smell/replace-urlopen.json b/test/lib/ansible_test/_util/controller/sanity/code-smell/replace-urlopen.json index 88858aeb613d88..da4a0b10f2923c 100644 --- a/test/lib/ansible_test/_util/controller/sanity/code-smell/replace-urlopen.json +++ b/test/lib/ansible_test/_util/controller/sanity/code-smell/replace-urlopen.json @@ -2,6 +2,10 @@ "extensions": [ ".py" ], + "prefixes": [ + "lib/ansible/", + "plugins/" + ], "ignore_self": true, "output": "path-line-column-message" } diff --git a/test/lib/ansible_test/_util/controller/sanity/code-smell/use-compat-six.json b/test/lib/ansible_test/_util/controller/sanity/code-smell/use-compat-six.json index 776590b74d26ba..ccee80a2f1284e 100644 --- a/test/lib/ansible_test/_util/controller/sanity/code-smell/use-compat-six.json +++ b/test/lib/ansible_test/_util/controller/sanity/code-smell/use-compat-six.json @@ -2,5 +2,9 @@ "extensions": [ ".py" ], + "prefixes": [ + "lib/ansible/", + "plugins/" + ], "output": "path-line-column-message" } diff --git a/test/sanity/ignore.txt b/test/sanity/ignore.txt index 9c3cbf988f776c..51d25dedd3046e 100644 --- a/test/sanity/ignore.txt +++ b/test/sanity/ignore.txt @@ -1,4 +1,3 @@ -.azure-pipelines/scripts/publish-codecov.py replace-urlopen lib/ansible/cli/scripts/ansible_connection_cli_stub.py shebang lib/ansible/config/base.yml no-unwanted-files lib/ansible/executor/powershell/async_watchdog.ps1 pslint:PSCustomUseLiteralPath @@ -138,7 +137,6 @@ test/integration/targets/win_script/files/test_script_removes_file.ps1 pslint:PS test/integration/targets/win_script/files/test_script_with_args.ps1 pslint:PSAvoidUsingWriteHost # Keep test/integration/targets/win_script/files/test_script_with_splatting.ps1 pslint:PSAvoidUsingWriteHost # Keep test/lib/ansible_test/_data/requirements/sanity.pslint.ps1 pslint:PSCustomUseLiteralPath # Uses wildcards on purpose -test/lib/ansible_test/_util/target/setup/requirements.py replace-urlopen test/support/network-integration/collections/ansible_collections/ansible/netcommon/plugins/module_utils/compat/ipaddress.py no-unicode-literals test/support/network-integration/collections/ansible_collections/cisco/ios/plugins/cliconf/ios.py pylint:arguments-renamed test/support/network-integration/collections/ansible_collections/vyos/vyos/plugins/cliconf/vyos.py pylint:arguments-renamed @@ -167,9 +165,6 @@ test/units/module_utils/basic/test_deprecate_warn.py pylint:ansible-deprecated-v test/units/module_utils/common/warnings/test_deprecate.py pylint:ansible-deprecated-no-version # testing Display.deprecated call without a version or date test/units/module_utils/common/warnings/test_deprecate.py pylint:ansible-deprecated-version # testing Deprecated version found in call to Display.deprecated or AnsibleModule.deprecate test/units/module_utils/urls/fixtures/multipart.txt line-endings # Fixture for HTTP tests that use CRLF -test/units/module_utils/urls/test_fetch_url.py replace-urlopen -test/units/module_utils/urls/test_gzip.py replace-urlopen -test/units/module_utils/urls/test_Request.py replace-urlopen test/units/utils/collection_loader/fixtures/collections/ansible_collections/testns/testcoll/plugins/action/my_action.py pylint:relative-beyond-top-level test/units/utils/collection_loader/fixtures/collections/ansible_collections/testns/testcoll/plugins/modules/__init__.py empty-init # testing that collections don't need inits test/units/utils/collection_loader/fixtures/collections_masked/ansible_collections/ansible/__init__.py empty-init # testing that collections don't need inits