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

Add Molecule Tests #44

Open
wants to merge 21 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
tests/.vagrant
test.retry
*.pyc
__pycache__
35 changes: 28 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
---
language: python
python: "2.7"
before_install:
- sudo apt-get update -qq
- sudo apt-get install -qq python-apt python-pycurl
services: docker

env:
global:
- ROLE_NAME: logrotate
- GALAXY_AUTHOR: nickhammond
- GIT_REPO_NAME: ansible-logrotate
matrix:
- MOLECULE_DISTRO_IMAGE: ubuntu:18.04
- MOLECULE_DISTRO_IMAGE: ubuntu:16.04
- MOLECULE_DISTRO_IMAGE: ubuntu:14.04
- MOLECULE_DISTRO_IMAGE: centos:7
- MOLECULE_DISTRO_IMAGE: centos:6
- MOLECULE_DISTRO_IMAGE: fedora:29
- MOLECULE_DISTRO_IMAGE: debian:9
- MOLECULE_DISTRO_IMAGE: debian:8

install:
- pip install ansible
# Install test dependencies.
- pip install molecule docker

before_script:
# Use actual Ansible Galaxy role name for the project directory.
- cd ../
- mv $GIT_REPO_NAME $GALAXY_AUTHOR.$ROLE_NAME
- cd $GALAXY_AUTHOR.$ROLE_NAME

script:
- "printf '[defaults]\nroles_path = ../' > ansible.cfg"
- ansible-playbook -i tests/inventory --syntax-check tests/test.yml
- ansible-playbook -i tests/inventory --connection=local --become -vvvv tests/test.yml
# Run tests.
- molecule test
notifications:
email: false
13 changes: 13 additions & 0 deletions .yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extends: default

rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
line-length: disable
# NOTE(retr0h): Templates no longer fail this lint rule.
# Uncomment if running old Molecule templates.
# truthy: disable
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016-14, Nick Hammond
Copyright (c) 2016-18, Nick Hammond
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@ Setting up logrotate for additional Nginx logs, with postrotate script.

## Testing locally

This role is already configured to run on travis CI within a test playbook but it's useful to be able to run and debug a role locally which can be done via Vagrant and the `ansible_local` provisioner.
This role is already configured to run on travis CI within a test playbook but it's useful to be able to run and debug a role locally which can be done via molecule and docker provisioner.

To run the test playbook locally within a Vagrant virtual machine:
To run the test locally, [install molecule](https://molecule.readthedocs.io/en/latest/installation.html), then run

```
cd tests
vagrant up --provision
molecule test
```

## License
Expand Down
1 change: 1 addition & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
logrotate_conf_dir: "/etc/logrotate.d/"
logrotate_scripts: []
21 changes: 11 additions & 10 deletions meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ galaxy_info:
license: BSD
min_ansible_version: 1.9
platforms:
- name: Ubuntu
versions:
- lucid
- precise
- trusty
- name: EL
versions:
- 7
categories:
- system
- name: Ubuntu
versions:
- bionic
- xenial
- trusty
- name: CentOS
versions:
- 7
- 6
galaxy_tags:
- system
dependencies: []
17 changes: 17 additions & 0 deletions molecule/default/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Molecule managed

{% 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 python2-dnf bash && dnf clean all; \
elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl bash && 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 && zypper clean -a; \
elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \
elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; fi
RUN if [ $(command -v apt-get) ]; then apt-get install -y python-yaml ; \
elif [ $(command -v yum) ]; then yum install -y PyYAML ; \
fi
16 changes: 16 additions & 0 deletions molecule/default/INSTALL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*******
Docker driver installation guide
*******

Requirements
============

* General molecule dependencies (see https://molecule.readthedocs.io/en/latest/installation.html)
* Docker Engine
* docker-py
* docker

Install
=======

$ sudo pip install docker-py
25 changes: 25 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
dependency:
name: galaxy
driver:
name: docker
lint:
name: yamllint
platforms:
- name: instance
image: "${MOLECULE_DISTRO_IMAGE:-ubuntu:14.04}"
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privlidged: true
provisioner:
name: ansible
lint:
name: ansible-lint
playbook:
converge: playbook.yml
scenario:
name: default
verifier:
name: testinfra
lint:
name: flake8
43 changes: 43 additions & 0 deletions molecule/default/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
- name: Converge
hosts: all

tasks:
- name: molecule playbook | create nginx dir
file:
path: '/var/log/nginx'
state: 'directory'
become: true

- name: molecule playbook | create nginx log files
file:
path: '/var/log/nginx/{{ item }}'
state: 'touch'
changed_when: false
loop:
- 'options.log'
- 'scripts.log'

roles:
- role: nickhammond.logrotate
vars:
logrotate_scripts:
- name: nginx-options
path: /var/log/nginx/options.log
options:
- daily
- weekly
- size 25M
- rotate 7
- missingok
- compress
- delaycompress
- copytruncate
- name: nginx-scripts
path: /var/log/nginx/scripts.log
options:
- daily
- weekly
- size 25M
scripts:
postrotate: "echo test"
33 changes: 33 additions & 0 deletions molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
import yaml

import testinfra.utils.ansible_runner

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


def get_logrotate_d_log_files():
"""
return a list of log files managed by logrotate
as defined in playbook.yml
"""
playbook_path = 'playbook.yml'

with open(playbook_path) as y:
playbook_yml = yaml.safe_load(y)

log_files = []
for play in playbook_yml:
for role in play['roles']:
for script in role['vars']['logrotate_scripts']:
log_files.append(script['name'])

return log_files


def test_logrotate_conf(host):
for log_file in get_logrotate_d_log_files():
logrotate_d_path = '/etc/logrotate.d/' + log_file
cmd = host.run('logrotate -d "%s"', logrotate_d_path)
assert cmd.stderr.find('error') == -1
18 changes: 0 additions & 18 deletions tests/Vagrantfile

This file was deleted.

1 change: 0 additions & 1 deletion tests/inventory

This file was deleted.

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

This file was deleted.