From c81314dcb9c6cf719cada0ad9965b2cdbc59bafa Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Sat, 7 Oct 2017 15:55:52 -0700 Subject: [PATCH] Add a hack to test whether type=path should be used in a module --- .../testing/sanity/use-argspec-type-path.rst | 11 +++++ .../code-smell/use-argspec-type-path.sh | 42 +++++++++++++++++++ test/sanity/pep8/legacy-files.txt | 2 - 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 docs/docsite/rst/dev_guide/testing/sanity/use-argspec-type-path.rst create mode 100755 test/sanity/code-smell/use-argspec-type-path.sh diff --git a/docs/docsite/rst/dev_guide/testing/sanity/use-argspec-type-path.rst b/docs/docsite/rst/dev_guide/testing/sanity/use-argspec-type-path.rst new file mode 100644 index 00000000000..7a77b2161e3 --- /dev/null +++ b/docs/docsite/rst/dev_guide/testing/sanity/use-argspec-type-path.rst @@ -0,0 +1,11 @@ +Sanity Tests ยป use-argspec-type-path +==================================== + +The AnsibleModule argument_spec knows of several types beyond the standard python types. One of +these is ``path``. When used, type ``path`` ensures that an argument is a string and expands any +shell variables and tilde characters. + +This test looks for use of :meth:`os.path.expanduser` in modules. When found, it tells the user to +replace it with ``type='path'`` in the module's argument_spec or list it as a false positive in the +test. + diff --git a/test/sanity/code-smell/use-argspec-type-path.sh b/test/sanity/code-smell/use-argspec-type-path.sh new file mode 100755 index 00000000000..19efd1cf0bb --- /dev/null +++ b/test/sanity/code-smell/use-argspec-type-path.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +# Add valid uses of expanduser to this list +WHITELIST='cloud/lxc/lxc_container.py +cloud/rackspace/rax_files_objects.py +database/mongodb/mongodb_parameter.py +database/mongodb/mongodb_user.py +database/postgresql/postgresql_db.py +files/synchronize.py +source_control/git.py +system/puppet.py +utilities/logic/async_status.py +utilities/logic/async_wrapper.py +web_infrastructure/ansible_tower/tower_host.py +web_infrastructure/ansible_tower/tower_group.py +web_infrastructure/jenkins_plugin.py' + +# Modules which need to be ported to get rid of expanduser. See: https://github.com/ansible/ansible/projects/12 +GRANDFATHERED_NEED_PORTING='cloud/rackspace/rax.py +cloud/rackspace/rax_scaling_group.py +files/find.py +files/archive.py' + +for FILE in $WHITELIST ; do + GREP_FORMAT_WHITELIST="$GREP_FORMAT_WHITELIST -e $FILE" +done + +for FILE in $GRANDFATHERED_NEED_PORTING ; do + GREP_FORMAT_WHITELIST="$GREP_FORMAT_WHITELIST -e $FILE" +done + +# GREP_FORMAT_WHITELIST has been formatted so that wordsplitting is wanted. Therefore no double quotes around the var +# shellcheck disable=SC2086 +egrep -r 'expanduser' lib/ansible/modules | egrep -v $GREP_FORMAT_WHITELIST + +if [ $? -ne 1 ]; then + printf 'The module(s) listed above use expanduser.\n' + printf 'This may indicate the module should be using an argpsec type="path" instead of type="str"\n' + printf 'If this is a false positive, add to the whitelist in:\n' + printf ' test/sanity/code-smell/use-argspec-type-path.sh\n' + exit 1 +fi diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index 8bee91249ae..9ca280f7096 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -59,7 +59,6 @@ lib/ansible/modules/cloud/azure/azure_rm_networkinterface.py lib/ansible/modules/cloud/azure/azure_rm_publicipaddress.py lib/ansible/modules/cloud/azure/azure_rm_publicipaddress_facts.py lib/ansible/modules/cloud/azure/azure_rm_storageaccount.py -lib/ansible/modules/cloud/azure/azure_rm_storageblob.py lib/ansible/modules/cloud/azure/azure_rm_subnet.py lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork.py @@ -76,7 +75,6 @@ lib/ansible/modules/cloud/docker/_docker.py lib/ansible/modules/cloud/docker/docker_container.py lib/ansible/modules/cloud/docker/docker_image.py lib/ansible/modules/cloud/docker/docker_image_facts.py -lib/ansible/modules/cloud/docker/docker_login.py lib/ansible/modules/cloud/docker/docker_network.py lib/ansible/modules/cloud/google/gc_storage.py lib/ansible/modules/cloud/google/gcdns_record.py