From e14d6eb04a6e93655d44931e4ccf7dde029d03c7 Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Wed, 26 Sep 2018 21:25:42 -0500 Subject: [PATCH] Switch tests to use Molecule. --- .gitignore | 3 +- .travis.yml | 36 ++++++++++++------- molecule/default/molecule.yml | 27 ++++++++++++++ .../default/playbook-source-install.yml | 8 +++-- .../default/playbook-standalone-nginx-aws.yml | 0 .../test.yml => molecule/default/playbook.yml | 6 ++-- {tests => molecule/default}/requirements.yml | 0 molecule/default/tests/test_default.py | 14 ++++++++ molecule/default/yaml-lint.yml | 6 ++++ tests/README.md | 11 ------ 10 files changed, 81 insertions(+), 30 deletions(-) create mode 100644 molecule/default/molecule.yml rename tests/test-source-install.yml => molecule/default/playbook-source-install.yml (80%) rename tests/test-standalone-nginx-aws.yml => molecule/default/playbook-standalone-nginx-aws.yml (100%) rename tests/test.yml => molecule/default/playbook.yml (87%) rename {tests => molecule/default}/requirements.yml (100%) create mode 100644 molecule/default/tests/test_default.py create mode 100644 molecule/default/yaml-lint.yml delete mode 100644 tests/README.md diff --git a/.gitignore b/.gitignore index c9b2377..f56f5b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.retry -tests/test.sh +*/__pycache__ +*.pyc diff --git a/.travis.yml b/.travis.yml index 47ab110..d644cb5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,23 +1,33 @@ --- +language: python services: docker env: - - distro: centos7 - - distro: centos6 - playbook: test-source-install.yml - - distro: ubuntu1604 - - distro: ubuntu1404 - playbook: test-source-install.yml - - distro: debian8 - playbook: test-source-install.yml + global: + - ROLE_NAME: certbot + matrix: + - MOLECULE_DISTRO: centos7 + MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd + - MOLECULE_DISTRO: centos6 + MOLECULE_PLAYBOOK: playbook-source-install.yml + - MOLECULE_DISTRO: ubuntu1604 + - MOLECULE_DISTRO: ubuntu1404 + MOLECULE_PLAYBOOK: playbook-source-install.yml + - MOLECULE_DISTRO: debian9 -script: - # Download test shim. - - wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/ - - chmod +x ${PWD}/tests/test.sh +install: + # Install test dependencies. + - pip install molecule docker + +before_script: + # Use actual Ansible Galaxy role name for the project directory. + - cd ../ + - mv ansible-role-$ROLE_NAME geerlingguy.$ROLE_NAME + - cd geerlingguy.$ROLE_NAME +script: # Run tests. - - ${PWD}/tests/test.sh + - molecule test notifications: webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..0339702 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,27 @@ +--- +dependency: + name: galaxy +driver: + name: docker +lint: + name: yamllint + options: + config-file: molecule/default/yaml-lint.yml +platforms: + - name: instance + image: geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible + command: ${MOLECULE_DOCKER_COMMAND:-"sleep infinity"} + privileged: true + pre_build_image: true +provisioner: + name: ansible + lint: + name: ansible-lint + playbooks: + converge: ${MOLECULE_PLAYBOOK:-playbook.yml} +scenario: + name: default +verifier: + name: testinfra + lint: + name: flake8 diff --git a/tests/test-source-install.yml b/molecule/default/playbook-source-install.yml similarity index 80% rename from tests/test-source-install.yml rename to molecule/default/playbook-source-install.yml index d2a3d73..65a374d 100644 --- a/tests/test-source-install.yml +++ b/molecule/default/playbook-source-install.yml @@ -1,8 +1,10 @@ --- -- hosts: all +- name: Converge + hosts: all + become: true vars: - certbot_install_from_source: yes + certbot_install_from_source: true pre_tasks: - name: Update apt cache. @@ -20,4 +22,4 @@ roles: - geerlingguy.git - - role_under_test + - geerlingguy.certbot \ No newline at end of file diff --git a/tests/test-standalone-nginx-aws.yml b/molecule/default/playbook-standalone-nginx-aws.yml similarity index 100% rename from tests/test-standalone-nginx-aws.yml rename to molecule/default/playbook-standalone-nginx-aws.yml diff --git a/tests/test.yml b/molecule/default/playbook.yml similarity index 87% rename from tests/test.yml rename to molecule/default/playbook.yml index a242a3e..38ff20a 100644 --- a/tests/test.yml +++ b/molecule/default/playbook.yml @@ -1,5 +1,7 @@ --- -- hosts: all +- name: Converge + hosts: all + become: true pre_tasks: - name: Update apt cache. @@ -19,4 +21,4 @@ when: ansible_os_family == 'Debian' roles: - - role_under_test + - geerlingguy.certbot \ No newline at end of file diff --git a/tests/requirements.yml b/molecule/default/requirements.yml similarity index 100% rename from tests/requirements.yml rename to molecule/default/requirements.yml diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py new file mode 100644 index 0000000..eedd64a --- /dev/null +++ b/molecule/default/tests/test_default.py @@ -0,0 +1,14 @@ +import os + +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_hosts_file(host): + f = host.file('/etc/hosts') + + assert f.exists + assert f.user == 'root' + assert f.group == 'root' diff --git a/molecule/default/yaml-lint.yml b/molecule/default/yaml-lint.yml new file mode 100644 index 0000000..a3dbc38 --- /dev/null +++ b/molecule/default/yaml-lint.yml @@ -0,0 +1,6 @@ +--- +extends: default +rules: + line-length: + max: 120 + level: warning diff --git a/tests/README.md b/tests/README.md deleted file mode 100644 index 6fb2117..0000000 --- a/tests/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# Ansible Role tests - -To run the test playbook(s) in this directory: - - 1. Install and start Docker. - 1. Download the test shim (see .travis.yml file for the URL) into `tests/test.sh`: - - `wget -O tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/` - 1. Make the test shim executable: `chmod +x tests/test.sh`. - 1. Run (from the role root directory) `distro=[distro] playbook=[playbook] ./tests/test.sh` - -If you don't want the container to be automatically deleted after the test playbook is run, add the following environment variables: `cleanup=false container_id=$(date +%s)`