From c83310ae644a8f57c81b1b238d8b4fe50d3aaae2 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Thu, 1 Dec 2016 21:13:25 +0200
Subject: [PATCH] - Adding Travis CI tests using Docker containers and an
 Ansible playbook to run through the modules.

---
 .pre-commit-config.yaml | 13 ++++++--
 .travis.yml             | 16 ++++++++-
 ansible.cfg             |  3 ++
 playbook.yml            | 73 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 101 insertions(+), 4 deletions(-)
 create mode 100644 ansible.cfg
 create mode 100644 playbook.yml

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index e91a45c..40017a3 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -2,13 +2,20 @@
     sha: 97b88d9610bcc03982ddac33caba98bb2b751f5f
     hooks:
     -   id: check-added-large-files
-    -   id: check-json
-    -   id: check-xml
     -   id: check-yaml
     -   id: check-merge-conflict
     -   id: flake8
 -   repo: https://github.com/adarnimrod/shell-pre-commit
-    sha: e48c7fbdadf14a548dcbda32895b67f90fa0f12b
+    sha: v0.1.0
     hooks:
     -   id: shell-lint
         files: collectd/collectd_facts|nginx/nginx_facts|ssl/dhparams
+-   repo: https://github.com/adarnimrod/ansible-pre-commit.git
+    sha: v0.4.0
+    hooks:
+    -   id: ansible-syntax-check
+-   repo: https://github.com/willthames/ansible-lint
+    sha: 959ab0f525e9abb19cf75f34381015cf33695f61
+    hooks:
+    -   id: ansible-lint
+        files: playbook.yml
diff --git a/.travis.yml b/.travis.yml
index a304f5e..d467350 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -3,9 +3,23 @@ language: python
 python: "2.7"
 dist: trusty
 sudo: false
+services: [docker]
+cache:
+  - pip
+  - directories:
+      - $HOME/.pre-commit
+
+env:
+  - DOCKER=ubuntu:trusty
+  - DOCKER=ubuntu:xenial
+  - DOCKER=debian:jessie
 
 install:
-  - pip install pre_commit
+  - pip install pre_commit ansible
+
+before_script:
+  - docker run --name $(echo $DOCKER | sed 's/:/_/g') $DOCKER
 
 script:
   - pre-commit run --all-files
+  - ansible-playbook -i $(echo $DOCKER | sed 's/:/_/g'), playbook.yml
diff --git a/ansible.cfg b/ansible.cfg
new file mode 100644
index 0000000..b6d3a7e
--- /dev/null
+++ b/ansible.cfg
@@ -0,0 +1,3 @@
+[defaults]
+library = ./
+host_key_checking = False
diff --git a/playbook.yml b/playbook.yml
new file mode 100644
index 0000000..790dddd
--- /dev/null
+++ b/playbook.yml
@@ -0,0 +1,73 @@
+---
+- hosts: all
+  tasks:
+  - name: APT install
+    apt:
+      name:
+      - collectd
+      - nginx
+      - openssl
+      state: present
+      update_cache: yes
+
+  - name: Collectd facts
+    collectd_facts:
+    register: collectd_facts
+
+  - name: Assertions
+    assert:
+      that:
+      - collectd_facts is defined
+      - major in collectd_facts
+      - collectd_facts.major is number
+      - collectd_facts.changed == False
+
+  - name: Nginx facts
+    nginx_facts:
+    register: nginx_facts
+
+  - name: Assertions
+    assert:
+      that:
+      - nginx_facts is defined
+      - version in nginx_facts
+      - major in nginx_facts
+      - nginx_facts.major is number
+      - nginx_facts.changed == False
+
+  - name: DH params for missing file
+    ignore_errors: True
+    dhparams:
+      path: /etc/ssl/dhparams.pem
+    register: missing_dhparams
+
+  - name: Assertions
+    assert:
+      that:
+      - missing_dhparams is defined
+      - bits in missing_dhparams
+      - missing_dhparams.bits == 0
+      - failed in missing_dhparams
+      - missing_dhparams.failed == True
+      - path in missing_dhparams
+      - missing_dhparams.path == '/etc/ssl/dhparams.pem'
+
+  - name: Generate DH params
+    command: openssl dhparam -out /etc/ssl/dhparams.pem 2048
+    changed_when: True
+
+  - name: DH params for existing file
+    dhparams:
+      path: /etc/ssl/dhparams.pem
+    register: existing_dhparams
+
+  - name: Assertions
+    assert:
+      that:
+      - existing_dhparams is defined
+      - bits in existing_dhparams
+      - existing_dhparams.bits == 2048
+      - failed in existing_dhparams
+      - existing_dhparams.failed == False
+      - path in existing_dhparams
+      - existing_dhparams.path == '/etc/ssl/dhparams.pem'
-- 
GitLab