From 12d9d8dd61e09b70914c77a010bc61d327827220 Mon Sep 17 00:00:00 2001 From: Kevin Carter Date: Wed, 12 Jun 2019 13:35:13 -0500 Subject: [PATCH] Import action plugin from tripleo-common The package action plugin has been imported from tripleo common. This plugin acts as a shim allowing tripleo to control package installations within ansible using a global variable. The action plugin will now have tests to ensure that it is functioning as expected within ansible. * The default scenario will run the test role normally ensuring that the plugin does not interfer with normal operations. * The negative scenario will run the test role with the global option `tripleo_enable_package_install` set to false. This will ensure that no package is installed. * The positive scenario will run the test role with the global option `tripleo_enable_package_install` set to true, which will ensure the expect package is installed. All three scenarios have been added as jobs which run when this role or plugin has been changed. Because this job will run three different molecule scenarios a change has been made to the pytest setup which will allow us to pass in command line arguments to the job function. This update will allow us to now set the command line option `--scenario` when running tests. This new argument has been documented in the `contributing.rst` file. Change-Id: I096d6332f8231620e39ead557e7c9598adad66dd Signed-off-by: Kevin Carter --- doc/source/contributing.rst | 14 ++ doc/source/roles/role-test_package_action.rst | 23 +++ tests/conftest.py | 19 +++ tests/test_molecule.py | 17 ++- tox.ini | 25 +++- .../ansible_plugins/action/.gitkeep | 0 .../ansible_plugins/action/package.py | 131 ++++++++++++++++++ .../roles/test_package_action/README.md | 6 + .../test_package_action/defaults/main.yml | 21 +++ .../roles/test_package_action/meta/main.yml | 44 ++++++ .../molecule/default/Dockerfile | 37 +++++ .../molecule/default/molecule.yml | 48 +++++++ .../molecule/default/playbook.yml | 21 +++ .../molecule/default/prepare.yml | 21 +++ .../molecule/default/tests/test_default.py | 27 ++++ .../molecule/default/verify.yml | 23 +++ .../molecule/negative/Dockerfile | 37 +++++ .../molecule/negative/molecule.yml | 48 +++++++ .../molecule/negative/playbook.yml | 23 +++ .../molecule/negative/prepare.yml | 21 +++ .../molecule/negative/tests/test_negative.py | 27 ++++ .../molecule/negative/verify.yml | 23 +++ .../molecule/positive/Dockerfile | 37 +++++ .../molecule/positive/molecule.yml | 48 +++++++ .../molecule/positive/playbook.yml | 23 +++ .../molecule/positive/prepare.yml | 21 +++ .../molecule/positive/tests/test_positive.py | 27 ++++ .../molecule/positive/verify.yml | 23 +++ .../roles/test_package_action/tasks/main.yml | 37 +++++ .../roles/test_package_action/vars/redhat.yml | 20 +++ zuul.d/jobs.yaml | 9 ++ zuul.d/layout.yaml | 1 + 32 files changed, 894 insertions(+), 8 deletions(-) create mode 100644 doc/source/roles/role-test_package_action.rst create mode 100644 tests/conftest.py delete mode 100644 tripleo_ansible/ansible_plugins/action/.gitkeep create mode 100644 tripleo_ansible/ansible_plugins/action/package.py create mode 100644 tripleo_ansible/roles/test_package_action/README.md create mode 100644 tripleo_ansible/roles/test_package_action/defaults/main.yml create mode 100644 tripleo_ansible/roles/test_package_action/meta/main.yml create mode 100644 tripleo_ansible/roles/test_package_action/molecule/default/Dockerfile create mode 100644 tripleo_ansible/roles/test_package_action/molecule/default/molecule.yml create mode 100644 tripleo_ansible/roles/test_package_action/molecule/default/playbook.yml create mode 100644 tripleo_ansible/roles/test_package_action/molecule/default/prepare.yml create mode 100644 tripleo_ansible/roles/test_package_action/molecule/default/tests/test_default.py create mode 100644 tripleo_ansible/roles/test_package_action/molecule/default/verify.yml create mode 100644 tripleo_ansible/roles/test_package_action/molecule/negative/Dockerfile create mode 100644 tripleo_ansible/roles/test_package_action/molecule/negative/molecule.yml create mode 100644 tripleo_ansible/roles/test_package_action/molecule/negative/playbook.yml create mode 100644 tripleo_ansible/roles/test_package_action/molecule/negative/prepare.yml create mode 100644 tripleo_ansible/roles/test_package_action/molecule/negative/tests/test_negative.py create mode 100644 tripleo_ansible/roles/test_package_action/molecule/negative/verify.yml create mode 100644 tripleo_ansible/roles/test_package_action/molecule/positive/Dockerfile create mode 100644 tripleo_ansible/roles/test_package_action/molecule/positive/molecule.yml create mode 100644 tripleo_ansible/roles/test_package_action/molecule/positive/playbook.yml create mode 100644 tripleo_ansible/roles/test_package_action/molecule/positive/prepare.yml create mode 100644 tripleo_ansible/roles/test_package_action/molecule/positive/tests/test_positive.py create mode 100644 tripleo_ansible/roles/test_package_action/molecule/positive/verify.yml create mode 100644 tripleo_ansible/roles/test_package_action/tasks/main.yml create mode 100644 tripleo_ansible/roles/test_package_action/vars/redhat.yml diff --git a/doc/source/contributing.rst b/doc/source/contributing.rst index d11d72677..3fe239ef3 100644 --- a/doc/source/contributing.rst +++ b/doc/source/contributing.rst @@ -26,6 +26,20 @@ the `tox.ini` file as an test scenario. python -m pytest --color=yes --html={envlogdir}/reports.html --self-contained-html {tty:-s} {toxinidir}/tests/test_molecule.py +If a given role has more than one scenario to test, the `--scenario` argument +can be used to set the scenario accordingly. + +.. code-block:: ini + + [testenv:mol-${NEWROLENAME}-${SCENARIO_2}] + basepython={[testenv:mol-${NEWROLENAME}]basepython} + deps={[testenv:mol-${NEWROLENAME}]deps} + changedir = {[testenv:mol-${NEWROLENAME}]changedir} + envdir = {[testenv:mol-${NEWROLENAME}]envdir} + commands = + python -m pytest --color=yes --html={envlogdir}/reports.html --self-contained-html {tty:-s} {toxinidir}/tests/test_molecule.py --scenario=${SCENARIO_2} + + When the role is ready for CI add a jobs entry into the `zuul.d/jobs.yaml`. .. code-block:: yaml diff --git a/doc/source/roles/role-test_package_action.rst b/doc/source/roles/role-test_package_action.rst new file mode 100644 index 000000000..b282da309 --- /dev/null +++ b/doc/source/roles/role-test_package_action.rst @@ -0,0 +1,23 @@ +========================================== +TripleO-Ansible Role - test_package_action +========================================== + +This role provides for the following services: + + * test_package_action + + +Default variables +~~~~~~~~~~~~~~~~~ + +.. literalinclude:: ../../../tripleo_ansible/roles/test_package_action/defaults/main.yml + :language: yaml + :start-after: under the License. + + +Example playbook +~~~~~~~~~~~~~~~~ + +.. literalinclude:: ../../../tripleo_ansible/roles/test_package_action/molecule/default/playbook.yml + :language: yaml + :start-after: under the License. diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..ee1b37f9c --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,19 @@ +# 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. + + +import os + + +def pytest_addoption(parser): + parser.addoption('--scenario', help='scenario setting') diff --git a/tests/test_molecule.py b/tests/test_molecule.py index 45edc2c93..3a493c084 100644 --- a/tests/test_molecule.py +++ b/tests/test_molecule.py @@ -12,14 +12,17 @@ # limitations under the License. -import os import subprocess +import pytest + + +def test_molecule(pytestconfig): + cmd = ['python', '-m', 'molecule', 'test'] + scenario = pytestconfig.getoption("scenario") + if scenario: + cmd.extend(['--scenario-name', scenario]) + else: + cmd.append('--all') -def test_molecule(): - cmd = ['python', '-m', 'molecule', 'test', '-s', os.getenv('MOL_SCENARIO', 'default')] assert subprocess.call(cmd) == 0 - - -if __name__ == '__main__': - test_molecule() diff --git a/tox.ini b/tox.ini index 16dfd17bb..294f37001 100644 --- a/tox.ini +++ b/tox.ini @@ -127,9 +127,32 @@ envdir = {toxworkdir}/mol commands = python -m pytest --color=yes --html={envlogdir}/reports.html --self-contained-html {tty:-s} {toxinidir}/tests/test_molecule.py +[testenv:mol-test_package_action] +basepython = {[testenv:mol]basepython} +deps = {[testenv:mol]deps} +changedir = {toxinidir}/tripleo_ansible/roles/test_package_action +commands = python -m pytest --color=yes --html={envlogdir}/reports.html --self-contained-html {tty:-s} {toxinidir}/tests/test_molecule.py + +[testenv:mol-test_package_action-default] +basepython = {[testenv:mol-test_package_action]basepython} +deps = {[testenv:mol-test_package_action]deps} +changedir = {[testenv:mol-test_package_action]changedir} +commands = python -m pytest --color=yes --html={envlogdir}/reports.html --self-contained-html {tty:-s} {toxinidir}/tests/test_molecule.py --scenario=default + +[testenv:mol-test_package_action-negative] +basepython = {[testenv:mol-test_package_action]basepython} +deps = {[testenv:mol-test_package_action]deps} +changedir = {[testenv:mol-test_package_action]changedir} +commands = python -m pytest --color=yes --html={envlogdir}/reports.html --self-contained-html {tty:-s} {toxinidir}/tests/test_molecule.py --scenario=negative + +[testenv:mol-test_package_action-positive] +basepython = {[testenv:mol-test_package_action]basepython} +deps = {[testenv:mol-test_package_action]deps} +changedir = {[testenv:mol-test_package_action]changedir} +commands = python -m pytest --color=yes --html={envlogdir}/reports.html --self-contained-html {tty:-s} {toxinidir}/tests/test_molecule.py --scenario=positive + [testenv:mol-tuned] basepython = {[testenv:mol]basepython} deps = {[testenv:mol]deps} changedir = {toxinidir}/tripleo_ansible/roles/tuned commands = python -m pytest --color=yes --html={envlogdir}/reports.html --self-contained-html {tty:-s} {toxinidir}/tests/test_molecule.py - python -m pytest --color=yes --html={envlogdir}/reports.html --self-contained-html {tty:-s} {toxinidir}/tests/test_molecule.py diff --git a/tripleo_ansible/ansible_plugins/action/.gitkeep b/tripleo_ansible/ansible_plugins/action/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/tripleo_ansible/ansible_plugins/action/package.py b/tripleo_ansible/ansible_plugins/action/package.py new file mode 100644 index 000000000..6cb3647d0 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/action/package.py @@ -0,0 +1,131 @@ +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +import imp +import os + +import ansible.plugins.action as action + + +# NOTE(cloudnull): imp is being used because core action plugins are not +# importable in py27. Once we get to the point where we +# no longer support py27 these lines should be converted +# to a straight python import. +# +# >>> from ansible.plugins.action import package +# +PKG = imp.load_source( + 'package', + os.path.join( + os.path.dirname( + action.__file__ + ), + 'package.py' + ) +) + + +def _bool_set(bool_opt): + """Check if option is a bool and return its type. + + returns: `bool` || `None` + """ + true_opts = ('true', 'yes', '1') + false_opts = ('false', 'no', '0') + if bool_opt is None: + return None + elif bool_opt is True: + return True + elif bool_opt is False: + return False + else: + bool_opt = str(bool_opt).lower() + if bool_opt in (true_opts + false_opts): + if bool_opt in true_opts: + return True + else: + return False + else: + return None + + +class ActionModule(PKG.ActionModule): + """Tripleo action plugin made to evaluate package installations. + + This is an action plugin shim that will intercept the use of + the standard package module. The intention of this shim is to ensure the + package module respects the option `tripleo_enable_package_install` + which is used to control the installation of packages through a deployment. + + This plugin will do nothing if the option `tripleo_enable_package_install` + is unset thereby allowing ansible to function normally. When the global + option is present the plugin will evaluate its truthiness and react + accordingly. + + * False: No action taken, task will be marked as skipped. + * True: Package installation happens normally. + + If this module encounters an error while processesing the module will + proceed as if the option `tripleo_enable_package_install` is unset which + ensures ansible tasks are handled correctly no matter the context in which + they are executed. + + Anytime this module results in a "skip" a message will be made available + which indicates why it was skipped. Messages will only be visualized when + debug mode has been enabled or through registering a variable and using it + a task which can print messages; e.g. `debug` or `fail`. + """ + + def run(self, tmp=None, task_vars=None): + """Shim for tripleo package operations. + + This shim will intercept the package module and if the hostvar + `tripleo_enable_package_install` is set to false all package + operations will be no-op. If this option is set to true, then the + normal package module will be executed. + + * This shim allows for the package module to be used with and without + delegation. + * In the event of ANY exception the module will hand off back to the + normal package module. + """ + try: + if self._task.delegate_to: + tripleo_pkg = self._templar.template( + "{{ hostvars['%s']['tripleo_enable_package_install'] }}" + % self._task.delegate_to + ) + else: + tripleo_pkg = self._templar.template( + "{{ tripleo_enable_package_install }}" + ) + except Exception: # If any exception run the normal pkg module + tripleo_pkg = None + else: + tripleo_pkg = _bool_set(bool_opt=tripleo_pkg) + finally: + if (tripleo_pkg is not None) and (tripleo_pkg is False): + return { + 'failed': False, + 'skipped': True, + 'msg': 'package installations are currently disabled,' + ' via "tripleo_enable_package_install" being' + ' set to "{}". please check the deployment' + ' settings.'.format(tripleo_pkg), + 'bool_param': tripleo_pkg + } + else: + return super(ActionModule, self).run(tmp, task_vars) diff --git a/tripleo_ansible/roles/test_package_action/README.md b/tripleo_ansible/roles/test_package_action/README.md new file mode 100644 index 000000000..1a8726d4e --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/README.md @@ -0,0 +1,6 @@ +test_package_action +=================== + +This role tests the package action plugin shim used within tripleo. + +The role tests will run through a default, negative, and positive scenario. diff --git a/tripleo_ansible/roles/test_package_action/defaults/main.yml b/tripleo_ansible/roles/test_package_action/defaults/main.yml new file mode 100644 index 000000000..072fdc3d9 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/defaults/main.yml @@ -0,0 +1,21 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +# All variables intended for modification should place placed in this file. + +# All variables within this role should have a prefix of "test_package_action" +test_package_action_debug: false diff --git a/tripleo_ansible/roles/test_package_action/meta/main.yml b/tripleo_ansible/roles/test_package_action/meta/main.yml new file mode 100644 index 000000000..4545d58c2 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/meta/main.yml @@ -0,0 +1,44 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +galaxy_info: + author: OpenStack + description: TripleO OpenStack Role -- test_package_action + company: Red Hat + license: Apache-2.0 + min_ansible_version: 2.7 + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + platforms: + - name: Fedora + versions: + - 28 + - name: CentOS + versions: + - 7 + + galaxy_tags: + - tripleo + + +# List your role dependencies here, one per line. Be sure to remove the '[]' above, +# if you add dependencies to this list. +dependencies: [] diff --git a/tripleo_ansible/roles/test_package_action/molecule/default/Dockerfile b/tripleo_ansible/roles/test_package_action/molecule/default/Dockerfile new file mode 100644 index 000000000..fcdbd2990 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/default/Dockerfile @@ -0,0 +1,37 @@ +# Molecule managed +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates && apt-get clean; \ + elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash {{ item.pkg_extras | default('') }} && dnf clean all; \ + elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl python-setuptools bash {{ item.pkg_extras | default('') }} && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ + elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml {{ item.pkg_extras | default('') }} && zypper clean -a; \ + elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates {{ item.pkg_extras | default('') }}; \ + elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates {{ item.pkg_extras | default('') }} && xbps-remove -O; fi + +{% for pkg in item.easy_install | default([]) %} +# install pip for centos where there is no python-pip rpm in default repos +RUN easy_install {{ pkg }} +{% endfor %} + + +CMD ["sh", "-c", "while true; do sleep 10000; done"] \ No newline at end of file diff --git a/tripleo_ansible/roles/test_package_action/molecule/default/molecule.yml b/tripleo_ansible/roles/test_package_action/molecule/default/molecule.yml new file mode 100644 index 000000000..3a32890d4 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/default/molecule.yml @@ -0,0 +1,48 @@ +--- +driver: + name: docker + +log: true + +platforms: + - name: centos7 + hostname: centos7 + image: centos:7 + dockerfile: Dockerfile + pkg_extras: python-setuptools + easy_install: + - pip + environment: &env + http_proxy: "{{ lookup('env', 'http_proxy') }}" + https_proxy: "{{ lookup('env', 'https_proxy') }}" + + - name: fedora28 + hostname: fedora28 + image: fedora:28 + dockerfile: Dockerfile + pkg_extras: python*-setuptools + environment: + <<: *env + +provisioner: + name: ansible + log: true + env: + ANSIBLE_STDOUT_CALLBACK: yaml + +scenario: + test_sequence: + - destroy + - create + - prepare + - converge + - verify + - destroy + +lint: + enabled: false + +verifier: + name: testinfra + lint: + name: flake8 diff --git a/tripleo_ansible/roles/test_package_action/molecule/default/playbook.yml b/tripleo_ansible/roles/test_package_action/molecule/default/playbook.yml new file mode 100644 index 000000000..1b3d9a3d9 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/default/playbook.yml @@ -0,0 +1,21 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +- name: Converge + hosts: all + roles: + - role: "test_package_action" diff --git a/tripleo_ansible/roles/test_package_action/molecule/default/prepare.yml b/tripleo_ansible/roles/test_package_action/molecule/default/prepare.yml new file mode 100644 index 000000000..ef85c3128 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/default/prepare.yml @@ -0,0 +1,21 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +- name: Prepare + hosts: all + roles: + - role: test_deps diff --git a/tripleo_ansible/roles/test_package_action/molecule/default/tests/test_default.py b/tripleo_ansible/roles/test_package_action/molecule/default/tests/test_default.py new file mode 100644 index 000000000..3af5a6563 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/default/tests/test_default.py @@ -0,0 +1,27 @@ +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +import os + +import testinfra.utils.ansible_runner + + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_package_installed(host): + assert host.package("bison").is_installed diff --git a/tripleo_ansible/roles/test_package_action/molecule/default/verify.yml b/tripleo_ansible/roles/test_package_action/molecule/default/verify.yml new file mode 100644 index 000000000..8a7b2cc09 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/default/verify.yml @@ -0,0 +1,23 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + +- name: Verify + hosts: all + tasks: + - name: Check for tripleo-ansible package + debug: + msg: >- + test message diff --git a/tripleo_ansible/roles/test_package_action/molecule/negative/Dockerfile b/tripleo_ansible/roles/test_package_action/molecule/negative/Dockerfile new file mode 100644 index 000000000..fcdbd2990 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/negative/Dockerfile @@ -0,0 +1,37 @@ +# Molecule managed +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates && apt-get clean; \ + elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash {{ item.pkg_extras | default('') }} && dnf clean all; \ + elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl python-setuptools bash {{ item.pkg_extras | default('') }} && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ + elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml {{ item.pkg_extras | default('') }} && zypper clean -a; \ + elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates {{ item.pkg_extras | default('') }}; \ + elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates {{ item.pkg_extras | default('') }} && xbps-remove -O; fi + +{% for pkg in item.easy_install | default([]) %} +# install pip for centos where there is no python-pip rpm in default repos +RUN easy_install {{ pkg }} +{% endfor %} + + +CMD ["sh", "-c", "while true; do sleep 10000; done"] \ No newline at end of file diff --git a/tripleo_ansible/roles/test_package_action/molecule/negative/molecule.yml b/tripleo_ansible/roles/test_package_action/molecule/negative/molecule.yml new file mode 100644 index 000000000..3a32890d4 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/negative/molecule.yml @@ -0,0 +1,48 @@ +--- +driver: + name: docker + +log: true + +platforms: + - name: centos7 + hostname: centos7 + image: centos:7 + dockerfile: Dockerfile + pkg_extras: python-setuptools + easy_install: + - pip + environment: &env + http_proxy: "{{ lookup('env', 'http_proxy') }}" + https_proxy: "{{ lookup('env', 'https_proxy') }}" + + - name: fedora28 + hostname: fedora28 + image: fedora:28 + dockerfile: Dockerfile + pkg_extras: python*-setuptools + environment: + <<: *env + +provisioner: + name: ansible + log: true + env: + ANSIBLE_STDOUT_CALLBACK: yaml + +scenario: + test_sequence: + - destroy + - create + - prepare + - converge + - verify + - destroy + +lint: + enabled: false + +verifier: + name: testinfra + lint: + name: flake8 diff --git a/tripleo_ansible/roles/test_package_action/molecule/negative/playbook.yml b/tripleo_ansible/roles/test_package_action/molecule/negative/playbook.yml new file mode 100644 index 000000000..0f90a461e --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/negative/playbook.yml @@ -0,0 +1,23 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +- name: Converge + hosts: all + roles: + - role: "test_package_action" + # Role variable used to interact with the package module shim + tripleo_enable_package_install: false diff --git a/tripleo_ansible/roles/test_package_action/molecule/negative/prepare.yml b/tripleo_ansible/roles/test_package_action/molecule/negative/prepare.yml new file mode 100644 index 000000000..ef85c3128 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/negative/prepare.yml @@ -0,0 +1,21 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +- name: Prepare + hosts: all + roles: + - role: test_deps diff --git a/tripleo_ansible/roles/test_package_action/molecule/negative/tests/test_negative.py b/tripleo_ansible/roles/test_package_action/molecule/negative/tests/test_negative.py new file mode 100644 index 000000000..c56417baa --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/negative/tests/test_negative.py @@ -0,0 +1,27 @@ +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +import os + +import testinfra.utils.ansible_runner + + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_package_installed(host): + assert host.package("bison").is_installed is False diff --git a/tripleo_ansible/roles/test_package_action/molecule/negative/verify.yml b/tripleo_ansible/roles/test_package_action/molecule/negative/verify.yml new file mode 100644 index 000000000..87beedd74 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/negative/verify.yml @@ -0,0 +1,23 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + +- name: Verify + hosts: all + tasks: + - name: Check for tripleo-ansible package + debug: + msg: >- + Negative test message diff --git a/tripleo_ansible/roles/test_package_action/molecule/positive/Dockerfile b/tripleo_ansible/roles/test_package_action/molecule/positive/Dockerfile new file mode 100644 index 000000000..fcdbd2990 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/positive/Dockerfile @@ -0,0 +1,37 @@ +# Molecule managed +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates && apt-get clean; \ + elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash {{ item.pkg_extras | default('') }} && dnf clean all; \ + elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl python-setuptools bash {{ item.pkg_extras | default('') }} && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ + elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml {{ item.pkg_extras | default('') }} && zypper clean -a; \ + elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates {{ item.pkg_extras | default('') }}; \ + elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates {{ item.pkg_extras | default('') }} && xbps-remove -O; fi + +{% for pkg in item.easy_install | default([]) %} +# install pip for centos where there is no python-pip rpm in default repos +RUN easy_install {{ pkg }} +{% endfor %} + + +CMD ["sh", "-c", "while true; do sleep 10000; done"] \ No newline at end of file diff --git a/tripleo_ansible/roles/test_package_action/molecule/positive/molecule.yml b/tripleo_ansible/roles/test_package_action/molecule/positive/molecule.yml new file mode 100644 index 000000000..3a32890d4 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/positive/molecule.yml @@ -0,0 +1,48 @@ +--- +driver: + name: docker + +log: true + +platforms: + - name: centos7 + hostname: centos7 + image: centos:7 + dockerfile: Dockerfile + pkg_extras: python-setuptools + easy_install: + - pip + environment: &env + http_proxy: "{{ lookup('env', 'http_proxy') }}" + https_proxy: "{{ lookup('env', 'https_proxy') }}" + + - name: fedora28 + hostname: fedora28 + image: fedora:28 + dockerfile: Dockerfile + pkg_extras: python*-setuptools + environment: + <<: *env + +provisioner: + name: ansible + log: true + env: + ANSIBLE_STDOUT_CALLBACK: yaml + +scenario: + test_sequence: + - destroy + - create + - prepare + - converge + - verify + - destroy + +lint: + enabled: false + +verifier: + name: testinfra + lint: + name: flake8 diff --git a/tripleo_ansible/roles/test_package_action/molecule/positive/playbook.yml b/tripleo_ansible/roles/test_package_action/molecule/positive/playbook.yml new file mode 100644 index 000000000..69a59e87a --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/positive/playbook.yml @@ -0,0 +1,23 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +- name: Converge + hosts: all + roles: + - role: "test_package_action" + # Role variable used to interact with the package module shim + tripleo_enable_package_install: true diff --git a/tripleo_ansible/roles/test_package_action/molecule/positive/prepare.yml b/tripleo_ansible/roles/test_package_action/molecule/positive/prepare.yml new file mode 100644 index 000000000..ef85c3128 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/positive/prepare.yml @@ -0,0 +1,21 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +- name: Prepare + hosts: all + roles: + - role: test_deps diff --git a/tripleo_ansible/roles/test_package_action/molecule/positive/tests/test_positive.py b/tripleo_ansible/roles/test_package_action/molecule/positive/tests/test_positive.py new file mode 100644 index 000000000..3af5a6563 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/positive/tests/test_positive.py @@ -0,0 +1,27 @@ +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +import os + +import testinfra.utils.ansible_runner + + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_package_installed(host): + assert host.package("bison").is_installed diff --git a/tripleo_ansible/roles/test_package_action/molecule/positive/verify.yml b/tripleo_ansible/roles/test_package_action/molecule/positive/verify.yml new file mode 100644 index 000000000..87beedd74 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/molecule/positive/verify.yml @@ -0,0 +1,23 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + +- name: Verify + hosts: all + tasks: + - name: Check for tripleo-ansible package + debug: + msg: >- + Negative test message diff --git a/tripleo_ansible/roles/test_package_action/tasks/main.yml b/tripleo_ansible/roles/test_package_action/tasks/main.yml new file mode 100644 index 000000000..a21a3cd68 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/tasks/main.yml @@ -0,0 +1,37 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +# "{{ role_name }}" will search for and load any operating system variable file +# found within the "vars/" path. If no OS files are found the task will skip. +- name: Gather variables for each operating system + include_vars: "{{ item }}" + with_first_found: + - skip: true + files: + - "{{ ansible_distribution | lower }}-{{ ansible_distribution_version | lower }}.yml" + - "{{ ansible_distribution | lower }}-{{ ansible_distribution_major_version | lower }}.yml" + - "{{ ansible_os_family | lower }}-{{ ansible_distribution_major_version | lower }}.yml" + - "{{ ansible_distribution | lower }}.yml" + - "{{ ansible_os_family | lower }}-{{ ansible_distribution_version.split('.')[0] }}.yml" + - "{{ ansible_os_family | lower }}.yml" + tags: + - always + +- name: Install test packages + package: + name: "{{ test_install_packages }}" + state: present diff --git a/tripleo_ansible/roles/test_package_action/vars/redhat.yml b/tripleo_ansible/roles/test_package_action/vars/redhat.yml new file mode 100644 index 000000000..2a4674e00 --- /dev/null +++ b/tripleo_ansible/roles/test_package_action/vars/redhat.yml @@ -0,0 +1,20 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + + +# This is a random package which is likely to not be installed by default. +test_install_packages: + - bison diff --git a/zuul.d/jobs.yaml b/zuul.d/jobs.yaml index dcf3621ce..09a30e968 100644 --- a/zuul.d/jobs.yaml +++ b/zuul.d/jobs.yaml @@ -22,6 +22,15 @@ vars: tox_envlist: mol-test_deps +- job: + name: tripleo-ansible-centos-7-molecule-test_package_action + parent: tripleo-ansible-centos + files: + - ^tripleo_ansible/roles/test_package_action/.* + - ^tripleo-ansible/tripleo_ansible/ansible_plugins/action/package.py + vars: + tox_envlist: mol-test_package_action + - job: name: tripleo-ansible-centos-7-molecule-tuned parent: tripleo-ansible-centos diff --git a/zuul.d/layout.yaml b/zuul.d/layout.yaml index 2b2a2bf15..de152a5b2 100644 --- a/zuul.d/layout.yaml +++ b/zuul.d/layout.yaml @@ -4,6 +4,7 @@ jobs: - openstack-tox-linters - tripleo-ansible-centos-7-molecule-test_deps + - tripleo-ansible-centos-7-molecule-test_package_action - tripleo-ansible-centos-7-molecule-tuned - tripleo-ansible-docs gate: