Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/molecule: ansible role tester scripts #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.tox
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ None.
roles:
- ansible-nginx

## How to run test ansible role
Use tox(virtualenv) and molecule(ansible role tester) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put that it also required Docker installed to run test

Example run with tox
```
tox -e py37-ansible27 -- molecule test -s default
```

## License

Expand Down
Empty file added molecule/alternative/.keep
Empty file.
37 changes: 37 additions & 0 deletions molecule/default/create.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
- name: Create
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
tasks:
- name: Create molecule instance(s)
docker_container:
name: "{{ item.name }}"
docker_host: "{{ item.docker_host | default('unix://var/run/docker.sock') }}"
hostname: "{{ item.name }}"
image: "{{ item.image }}"
state: started
recreate: false
log_driver: json-file
command: "{{ item.command | default(omit) }}"
privileged: "{{ item.privileged | default(omit) }}"
volumes: "{{ item.volumes | default(omit) }}"
capabilities: "{{ item.capabilities | default(omit) }}"
exposed_ports: "{{ item.exposed_ports | default(omit) }}"
published_ports: "{{ item.published_ports | default(omit) }}"
ulimits: "{{ item.ulimits | default(omit) }}"
networks: "{{ item.networks | default(omit) }}"
dns_servers: "{{ item.dns_servers | default(omit) }}"
register: server
with_items: "{{ molecule_yml.platforms }}"
async: 7200
poll: 0

- name: Wait for instance(s) creation to complete
async_status:
jid: "{{ item.ansible_job_id }}"
register: docker_jobs
until: docker_jobs.finished
retries: 300
with_items: "{{ server.results }}"
32 changes: 32 additions & 0 deletions molecule/default/destroy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
- name: Destroy
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
tasks:
- name: Destroy molecule instance(s)
docker_container:
name: "{{ item.name }}"
docker_host: "{{ item.docker_host | default('unix://var/run/docker.sock') }}"
state: absent
force_kill: "{{ item.force_kill | default(true) }}"
register: server
with_items: "{{ molecule_yml.platforms }}"
async: 7200
poll: 0

- name: Wait for instance(s) deletion to complete
async_status:
jid: "{{ item.ansible_job_id }}"
register: docker_jobs
until: docker_jobs.finished
retries: 300
with_items: "{{ server.results }}"

- name: Delete docker network(s)
docker_network:
name: "{{ item }}"
docker_host: "{{ item.docker_host | default('unix://var/run/docker.sock') }}"
state: absent
with_items: "{{ molecule_yml.platforms | molecule_get_docker_networks }}"
47 changes: 47 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
dependency:
name: galaxy
driver:
name: docker
lint:
name: yamllint
options:
config-data:
ignore: |
../../
../../tests
platforms:
- name: bionic
image: paulfantom/ubuntu-molecule:18.04
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
- name: xenial
image: paulfantom/ubuntu-molecule:16.04
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
- name: centos7
image: paulfantom/centos-molecule:7
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
provisioner:
name: ansible
lint:
name: ansible-lint
playbooks:
create: create.yml
prepare: prepare.yml
converge: playbook.yml
destroy: destroy.yml
scenario:
name: default
verifier:
name: testinfra
lint:
name: flake8
enabled: true
6 changes: 6 additions & 0 deletions molecule/default/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: Run role
hosts: all
any_errors_fatal: true
roles:
- ansible-nginx
5 changes: 5 additions & 0 deletions molecule/default/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Prepare
hosts: all
gather_facts: false
tasks: []
Binary file not shown.
63 changes: 63 additions & 0 deletions molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import pytest
import os
import yaml
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


@pytest.fixture()
def AnsibleDefaults(Ansible):
with open("../../../defaults/main.yml", 'r') as stream:
return yaml.load(stream)


@pytest.mark.parametrize("dirs", [
"/etc/nginx",
"/etc/nginx/conf.d"
])
def test_directories(host, dirs):
d = host.file(dirs)
assert d.is_directory
assert d.exists


@pytest.mark.parametrize("files", [
"/etc/nginx/nginx.conf"
])
def test_files(host, files):
f = host.file(files)
assert f.exists
assert f.is_file


# @pytest.mark.parametrize("files", [
# "/etc/prometheus/rules/ansible_managed.rules",
# "/opt/prometheus/prometheus",
# "/opt/prometheus/promtool",
# "/opt/prometheus"
# ])
# def test_absent(host, files):
# f = host.file(files)
# assert not f.exists


def test_service(host):
s = host.service("nginx")
# assert s.is_enabled
assert s.is_running


@pytest.mark.parametrize("ports", [
"tcp://0.0.0.0:80"
])
def test_socket(host, ports):
s = host.socket(ports)
assert s.is_listening


# def test_version(host, AnsibleDefaults):
# version = os.getenv('PROMETHEUS', AnsibleDefaults['prometheus_version'])
# out = host.run("/usr/local/bin/prometheus --version").stderr
# assert "prometheus, version " + version in out
2 changes: 1 addition & 1 deletion tasks/nginx_post.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Ensure required packages is installed (CentOS/RHEL)
yum:
yum:
name: "{{ nginx_yum_required_packages }}"
state: present
when: ansible_os_family == 'RedHat'
Expand Down
11 changes: 10 additions & 1 deletion tasks/nginx_pre.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
---
- name: Ensure gpg binary is installed (Ubuntu/Debian)
apt:
name: gnupg
update_cache: yes
when:
- ansible_os_family == 'Debian'

- name: Add Nginx repository key from keyfile (Ubuntu/Debian)
apt_key:
url: "{{ nginx_apt_key_url }}"
Expand All @@ -21,7 +28,9 @@
# apt-key with proxy need to use command
# https://github.com/ansible/ansible/issues/31691#issuecomment-396973282
- name: Add Nginx repository key (proxy environment) (Ubuntu/Debian)
command: "apt-key adv --keyserver-options http-proxy={{ proxy_env.http_proxy }} --fetch-keys 'http://{{ nginx_apt_key_server }}/pks/lookup?op=get&search=0x{{ nginx_apt_key_id }}'"
command: |
apt-key adv --keyserver-options http-proxy={{ proxy_env.http_proxy }} \
--fetch-keys 'http://{{ nginx_apt_key_server }}/pks/lookup?op=get&search=0x{{ nginx_apt_key_id }}'
when:
- ansible_os_family == 'Debian'
- nginx_apt_key_use_upstream is defined
Expand Down
6 changes: 6 additions & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ansible-lint>=3.4.0
docker
jmespath
molecule>=2.15.0
pytest==3.9.3
testinfra>=1.7.0
22 changes: 22 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[tox]
minversion = 1.8
envlist = py{27,37}-ansible{25,26,27,28}
skipsdist = true

[travis:env]
ANSIBLE=
2.5: ansible25
2.6: ansible26
2.7: ansible27
2.8: ansible28

[testenv]
passenv = *
deps =
-rtest_requirements.txt
ansible25: ansible<2.6
ansible26: ansible<2.7
ansible27: ansible<2.8
ansible28: ansible>=2.8
commands =
{posargs:molecule test --all --destroy always}