Switch tests to use Molecule.

pull/129/head^2
Jeff Geerling 6 years ago
parent 57ef9e3855
commit a3e664b069
  1. 3
      .gitignore
  2. 40
      .travis.yml
  3. 25
      molecule/default/molecule.yml
  4. 8
      molecule/default/playbook.yml
  5. 14
      molecule/default/tests/test_default.py
  6. 11
      tests/README.md

3
.gitignore vendored

@ -1,2 +1,3 @@
*.retry *.retry
tests/test.sh */__pycache__
*.pyc

@ -1,31 +1,31 @@
--- ---
language: python
services: docker services: docker
env: env:
- distro: centos7 global:
- distro: ubuntu1604 - ROLE_NAME: nginx
- distro: ubuntu1404 matrix:
- distro: debian9 - MOLECULE_DISTRO: centos7
- distro: debian8 MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd
- MOLECULE_DISTRO: ubuntu1604
- MOLECULE_DISTRO: ubuntu1404
- MOLECULE_DISTRO: debian9
- MOLECULE_DISTRO: debian8
script: install:
# Configure test script so we can run extra tests after playbook is run. # Install test dependencies.
- export container_id=$(date +%s) - pip install molecule docker
- export cleanup=false
# Download test shim. before_script:
- wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/ # Use actual Ansible Galaxy role name for the project directory.
- chmod +x ${PWD}/tests/test.sh - cd ../
- mv ansible-role-$ROLE_NAME geerlingguy.$ROLE_NAME
- cd geerlingguy.$ROLE_NAME
script:
# Run tests. # Run tests.
- ${PWD}/tests/test.sh - molecule test
# Setup test site.
- 'docker exec ${container_id} mkdir -p /var/www/test'
- 'docker exec ${container_id} bash -c "echo Success >| /var/www/test/index.html"'
# Make sure virtualhost exists.
- 'docker exec --tty ${container_id} env TERM=xterm curl http://localhost/ | grep "Success"'
notifications: notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/ webhooks: https://galaxy.ansible.com/api/v1/notifications/

@ -0,0 +1,25 @@
---
dependency:
name: galaxy
driver:
name: docker
lint:
name: yamllint
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

@ -1,5 +1,6 @@
--- ---
- hosts: all - name: Converge
hosts: all
vars: vars:
nginx_use_ppa: true nginx_use_ppa: true
@ -14,8 +15,5 @@
when: ansible_os_family == 'Debian' when: ansible_os_family == 'Debian'
changed_when: false changed_when: false
- name: Install dependencies.
package: name=curl state=present
roles: roles:
- role_under_test - role: geerlingguy.nginx

@ -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'

@ -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)`