Unverified Commit 321f6179 authored by aflatto's avatar aflatto Committed by GitHub

Merge pull request #7 from Icinga/feature/testing-3

Add basic testing with Molecule and Travis-CI
parents 6fb01a63 f4fa33e8
[flake8]
max-line-length = 160
max-complexity = 10
---
sudo: required
dist: trusty
language: python
services:
- docker
before_script:
- pip install ansible==$ANSIBLE_VERSION
- pip install molecule
- pip install docker-py
script:
- molecule test
matrix:
include:
- env: ANSIBLE_VERSION="2.3.3"
- env: ANSIBLE_VERSION="2.4.4"
- env: ANSIBLE_VERSION="2.5.2"
branches:
only:
- master
- /^v\d/
notifications:
email: false
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
# Contributing
A roadmap of this project is located at https://github.com/Icinga/ansible-icinga2/milestones. Please consider
this roadmap when you start contributing to the project.
Before starting your work on this module, you should [fork the project] to your GitHub account. This allows you to
freely experiment with your changes. When your changes are complete, submit a [pull request]. All pull requests will be
reviewed and merged if they suit some general guidelines:
* Changes are located in a topic branch
* For new functionality, proper tests are written
* Changes should not solve certain problems on special environments
* Your change does not handle third party software for which dedicated Ansible role exist
* such as creating databases, installing webserver etc.
## Branches
Choosing a proper name for a branch helps us identify its purpose and possibly find an associated bug or feature.
Generally a branch name should include a topic such as `fix` or `feature` followed by a description and an issue number
if applicable. Branches should have only changes relevant to a specific issue.
```
git checkout -b fix/service-template-typo-1234
git checkout -b feature/config-handling-1235
```
## Testing
If you add new functionality, make sure to write appropriate tests as well. A complete guide on how to run tests is
described in [TESTING.md](TESTING.md).
[fork the project]: https://help.github.com/articles/fork-a-repo/
[pull request]: https://help.github.com/articles/using-pull-requests/
[![Build Status](https://api.travis-ci.org/Icinga/ansible-icinga2.svg?branch=master)](https://travis-ci.org/Icinga/ansible-icinga2)
# Icinga 2 Role for Ansible # Icinga 2 Role for Ansible
Ansible role to install and configure [Icinga 2](https://www.icinga.com/products/icinga-2/). Ansible role to install and configure [Icinga 2](https://www.icinga.com/products/icinga-2/).
...@@ -23,6 +25,14 @@ Other operating systems or versions may work but have not been tested. ...@@ -23,6 +25,14 @@ Other operating systems or versions may work but have not been tested.
A roadmap of this project is located at https://github.com/Icinga/ansible-icinga2/milestones. Please consider this A roadmap of this project is located at https://github.com/Icinga/ansible-icinga2/milestones. Please consider this
roadmap when you start contributing to the project. roadmap when you start contributing to the project.
### Contributing
When contributing several steps such as pull requests and proper testing implementations are required. Find a detailed
step by step guide in [CONTRIBUTING.md](CONTRIBUTING.md).
### Testing
Testing is essential in our workflow to ensure a good quality. We use Molecule to test all
components of this role. For a detailed description see [TESTING.md](TESTING.md).
## Release Notes ## Release Notes
When releasing new versions we refer to [SemVer 1.0.0](http://semver.org/spec/v1.0.0.html) for version numbers. All steps required when creating a new When releasing new versions we refer to [SemVer 1.0.0](http://semver.org/spec/v1.0.0.html) for version numbers. All steps required when creating a new
release are described in [RELEASE.md](RELEASE.md) release are described in [RELEASE.md](RELEASE.md)
......
...@@ -3,6 +3,9 @@ Before submitting a new release, make sure all relevant pull requests and local ...@@ -3,6 +3,9 @@ Before submitting a new release, make sure all relevant pull requests and local
branch. All tests must pass before a release is tagged. branch. All tests must pass before a release is tagged.
## Testing
Make sure tests pass before starting with a release. See [TESTING.md](TESTING.md) for more information.
## 1. AUTHORS ## 1. AUTHORS
Update the [AUTHORS](AUTHORS) and [.mailmap](.mailmap) file Update the [AUTHORS](AUTHORS) and [.mailmap](.mailmap) file
......
# TESTING
This role includes everything necessary to be tested with [Molecule](https://molecule.readthedocs.io/en/latest/). The
[installation documentation](https://molecule.readthedocs.io/en/latest/installation.html) provides
a great walk-through to get started.
## Running tests
Run all tests (all scenarios on all platforms):
``` shell
molecule test --all
```
Run specific scenario (scenario `default` is used by default)
``` shell
molecule test -s default
```
Don't destroy container after running the tests:
``` shell
molecule test --destroy never
```
Run only linter
``` shell
molecule lint
```
Run only syntax checks
``` shell
molecule syntax
```
\ No newline at end of file
# 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 upgrade -y && 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 update -y && 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 update -y && 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
*******
Install
*******
Requirements
============
* Docker Engine
* docker-py
Install
=======
.. code-block:: bash
$ sudo pip install docker-py
---
- name: Create
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
tasks:
- name: Log into a Docker registry
docker_login:
username: "{{ item.registry.credentials.username }}"
password: "{{ item.registry.credentials.password }}"
email: "{{ item.registry.credentials.email | default(omit) }}"
registry: "{{ item.registry.url }}"
docker_host: "{{ item.docker_host | default('unix://var/run/docker.sock') }}"
with_items: "{{ molecule_yml.platforms }}"
when:
- item.registry is defined
- item.registry.credentials is defined
- item.registry.credentials.username is defined
- name: Create Dockerfiles from image names
template:
src: "{{ molecule_scenario_directory }}/Dockerfile.j2"
dest: "{{ molecule_ephemeral_directory }}/Dockerfile_{{ item.image | regex_replace('[^a-zA-Z0-9_]', '_') }}"
with_items: "{{ molecule_yml.platforms }}"
register: platforms
- name: Discover local Docker images
docker_image_facts:
name: "molecule_local/{{ item.item.name }}"
docker_host: "{{ item.item.docker_host | default('unix://var/run/docker.sock') }}"
with_items: "{{ platforms.results }}"
register: docker_images
- name: Build an Ansible compatible image
docker_image:
path: "{{ molecule_ephemeral_directory }}"
name: "molecule_local/{{ item.item.image }}"
docker_host: "{{ item.item.docker_host | default('unix://var/run/docker.sock') }}"
dockerfile: "{{ item.item.dockerfile | default(item.invocation.module_args.dest) }}"
force: "{{ item.item.force | default(true) }}"
with_items: "{{ platforms.results }}"
when: platforms.changed or docker_images.results | map(attribute='images') | select('equalto', []) | list | count >= 0
- name: Create docker network(s)
docker_network:
name: "{{ item }}"
docker_host: "{{ item.docker_host | default('unix://var/run/docker.sock') }}"
state: present
with_items: "{{ molecule_yml.platforms | molecule_get_docker_networks }}"
- 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: "molecule_local/{{ item.image }}"
state: started
recreate: false
log_driver: json-file
command: "{{ item.command | default('bash -c \"while true; do sleep 10000; done\"') }}"
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 }}"
---
- 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 }}"
---
dependency:
name: galaxy
driver:
name: docker
lint:
name: yamllint
platforms:
- name: centos7
image: solita/centos-systemd:7
command: /sbin/init
capabilities:
- SYS_ADMIN
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
- name: ubuntu16
image: solita/ubuntu-systemd:16.04
command: /sbin/init
capabilities:
- SYS_ADMIN
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
provisioner:
name: ansible
lint:
name: ansible-lint
scenario:
name: default
verifier:
name: testinfra
lint:
name: flake8
enabled: true
---
- name: Converge
hosts: all
roles:
- role: ansible-icinga2
---
- name: Prepare
hosts: all
gather_facts: true
tasks: []
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
def test_icinga_repository_file(host):
if host.system_info.distribution == 'centos':
i2_repo = host.file('/etc/yum.repos.d/ICINGA-release.repo')
else:
i2_repo = host.file('/etc/apt/sources.list.d/packages_icinga_com_ubuntu.list')
assert i2_repo.exists
assert i2_repo.user == 'root'
assert i2_repo.group == 'root'
def test_icinga2_is_installed(host):
i2_package = host.package("icinga2")
assert i2_package.is_installed
def test_icinga2_running_and_enabled(host):
i2_service = host.service("icinga2")
assert i2_service.is_running
assert i2_service.is_enabled
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment