Skip to content

Commit

Permalink
Switch tests to use Molecule.
Browse files Browse the repository at this point in the history
  • Loading branch information
geerlingguy committed Oct 2, 2018
1 parent c79ca2a commit 4945fda
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 114 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.retry
tests/test.sh
*/__pycache__
*.pyc
60 changes: 26 additions & 34 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
---
language: python
services: docker

env:
- distro: centos7
- distro: centos6
- distro: ubuntu1604
- distro: ubuntu1404
- distro: debian9
- distro: debian8

# Also test installing older versions.
- distro: centos7
playbook: test-41.yml
varnish_version: "4.1"
- distro: ubuntu1604
varnish_version: "4.1"
playbook: test-41.yml
global:
- ROLE_NAME: varnish
matrix:
- MOLECULE_DISTRO: centos7
MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd
- MOLECULE_DISTRO: centos6
- MOLECULE_DISTRO: ubuntu1604
- MOLECULE_DISTRO: ubuntu1404
- MOLECULE_DISTRO: debian9
- MOLECULE_DISTRO: debian8

- MOLECULE_DISTRO: centos7
MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd
MOLECULE_PLAYBOOK: playbook-41.yml

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:
# Configure test script so we can run extra tests after playbook is run.
- export container_id=$(date +%s)
- export cleanup=false

# Download test shim.
- wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/
- chmod +x ${PWD}/tests/test.sh

# Run tests.
- ${PWD}/tests/test.sh

# Make sure Varnish is running.
- 'docker exec --tty ${container_id} env TERM=xterm varnishd -V'

# Test the version of Varnish installed (default to the main.yml default).
- |
varnish_version="${varnish_version:-5.1}"
docker exec --tty ${container_id} env TERM=xterm varnishd -V | grep "${varnish_version}"
# Test port 80.
- 'docker exec --tty ${container_id} env TERM=xterm curl -sI localhost:80 | grep -q "Via: .* varnish"'
- molecule test

notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
2 changes: 1 addition & 1 deletion handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: reload systemd
command: systemctl daemon-reload
systemd: daemon-reload=true

- name: restart varnish
service: name=varnish state=restarted
20 changes: 14 additions & 6 deletions meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ galaxy_info:
platforms:
- name: EL
versions:
- 6
- 7
- 6
- 7
- name: Ubuntu
versions:
- precise
- trusty
- xenial
- precise
- trusty
- xenial
- bionic
- name: Debian
versions:
- all
- all
galaxy_tags:
- web
- varnish
- cache
- proxy
- reverse-proxy
- performance
- loadbalancer
- balancer
27 changes: 27 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions molecule/default/playbook-41.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
- name: Converge
hosts: all
become: true

vars:
varnish_version: "4.1"

pre_tasks:
- import_tasks: playbook-setup.yml

roles:
- role: geerlingguy.varnish

post_tasks:
- name: Check the installed Ansible version.
command: varnishd -V
register: varnish_version
failed_when: "'4.1' not in varnish_version.stdout"

- name: Verify Varnish is running on port 80.
shell: 'curl -sI localhost:80 | grep -q "Via: .* varnish"'
args:
warn: false
25 changes: 25 additions & 0 deletions molecule/default/playbook-setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
- name: Update apt cache.
apt: update_cache=true cache_valid_time=600
when: ansible_os_family == 'Debian'

- name: Ensure build dependencies are installed (RedHat 7+).
yum:
name:
- logrotate
- systemd-sysv
state: present
when:
- ansible_os_family == 'RedHat'
- ansible_distribution_major_version >= '7'

- name: Ensure build dependencies are installed (RedHat < 7).
yum:
name: logrotate
state: present
when:
- ansible_os_family == 'RedHat'
- ansible_distribution_major_version < '7'

- name: Ensure curl is installed.
package: name=curl state=present
23 changes: 23 additions & 0 deletions molecule/default/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
- name: Converge
hosts: all
become: true

pre_tasks:
- import_tasks: playbook-setup.yml

roles:
- role: geerlingguy.varnish

post_tasks:
- name: Check the installed Ansible version.
command: varnishd -V
register: varnish_version
failed_when: "'5.1' not in varnish_version.stdout"
tags: ['skip_ansible_lint']

- name: Verify Varnish is running on port 80.
shell: 'curl -sI localhost:80 | grep -q "Via: .* varnish"'
args:
warn: false
tags: ['skip_ansible_lint']
14 changes: 14 additions & 0 deletions molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -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'
6 changes: 6 additions & 0 deletions molecule/default/yaml-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
extends: default
rules:
line-length:
max: 160
level: warning
4 changes: 2 additions & 2 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
service:
name: "{{ item }}"
state: started
enabled: yes
enabled: true
with_items: "{{ varnish_enabled_services | default([]) }}"
when: >
varnish_enabled_services and
Expand All @@ -87,7 +87,7 @@
service:
name: "{{ item }}"
state: started
enabled: yes
enabled: true
use: "service"
with_items: "{{ varnish_enabled_services | default([]) }}"
when:
Expand Down
12 changes: 6 additions & 6 deletions tasks/setup-RedHat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
name: varnishcache_{{ varnish_packagecloud_repo }}
description: Varnish Cache packagecloud.io repository.
baseurl: https://packagecloud.io/varnishcache/{{ varnish_packagecloud_repo }}/el/{{ ansible_distribution_major_version|int }}/$basearch
repo_gpgcheck: no
gpgcheck: no
enabled: yes
repo_gpgcheck: false
gpgcheck: false
enabled: true
gpgkey: https://packagecloud.io/varnishcache/{{ varnish_packagecloud_repo }}/gpgkey
sslverify: 1
sslcacert: /etc/pki/tls/certs/ca-bundle.crt
priority: "{{ varnish_packagecloud_repo_yum_repository_priority }}"
register: varnish_packagecloud_repo_addition

- name: Refresh yum metadata cache if repo changed.
command: >
yum clean metadata
warn=no
command: yum clean metadata
args:
warn: false
when: varnish_packagecloud_repo_addition.changed

- name: Ensure Varnish is installed.
Expand Down
11 changes: 0 additions & 11 deletions tests/README.md

This file was deleted.

9 changes: 0 additions & 9 deletions tests/debug.yml

This file was deleted.

20 changes: 0 additions & 20 deletions tests/setup.yml

This file was deleted.

14 changes: 0 additions & 14 deletions tests/test-41.yml

This file was deleted.

10 changes: 0 additions & 10 deletions tests/test.yml

This file was deleted.

0 comments on commit 4945fda

Please sign in to comment.