Skip to content
This repository has been archived by the owner on Jun 25, 2023. It is now read-only.

Commit

Permalink
Improve tests (#48)
Browse files Browse the repository at this point in the history
- Test distributions and versions combinations using [provision_docker](https://github.com/chrismeyersfsu/provision_docker)
- Move idempotency check to test playbook
- Add variables assertions
- Ansible 2.4+ only
  • Loading branch information
jnv authored Feb 24, 2018
1 parent 8649cb4 commit d19fe39
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 95 deletions.
32 changes: 8 additions & 24 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
---
language: python
python: "2.7"
sudo: required
dist: trusty
before_install:
- sudo apt-get update -qq
language: python
services: docker

cache: pip

install:
- pip install ansible
- "echo localhost > inventory remote_user=root"
- pip install ansible docker
- ansible-galaxy install -r tests/requirements.yml -p tests/roles/

script:
- "ansible-playbook -i inventory role.yml --connection=local --sudo"
- >
ansible-playbook -i inventory role.yml --connection=local --sudo
| grep -q 'changed=0.*failed=0'
&& (echo 'Idempotence test: pass' && exit 0)
|| (echo 'Idempotence test: fail' && exit 1)
- >
ansible-playbook -i inventory role.yml --connection=local --sudo
--tags unattended | grep -q 'changed=0.*failed=0'
&& (echo 'Idempotence test: pass' && exit 0)
|| (echo 'Idempotence test: fail' && exit 1)
- apt-config dump | grep 'Unattended-Upgrade "1"'
- sudo unattended-upgrades --dry-run

notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
- ansible --version
- tests/test.sh
34 changes: 0 additions & 34 deletions Vagrantfile

This file was deleted.

3 changes: 0 additions & 3 deletions ansible.cfg

This file was deleted.

4 changes: 0 additions & 4 deletions role.yml

This file was deleted.

30 changes: 0 additions & 30 deletions test-upgrades-on-virtual-machines.sh

This file was deleted.

3 changes: 3 additions & 0 deletions tests/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[defaults]
roles_path = ../../
retry_files_enabled = False
1 change: 1 addition & 0 deletions tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
localhost ansible_connection=local ansible_python_interpreter="/usr/bin/env python"
3 changes: 3 additions & 0 deletions tests/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- src: chrismeyersfsu.provision_docker
name: provision_docker
29 changes: 29 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Exit on any individual command failure
set -e

# Pretty colors.
red='\033[0;31m'
green='\033[0;32m'
neutral='\033[0m'

section() {
echo -e "\033[33;1m$1\033[0m"
}

fold_start() {
echo -e "travis_fold:start:$1\033[33;1m$2\033[0m"
}

fold_end() {
echo -e "\ntravis_fold:end:$1\r"
}

# Ensure we are in the tests dir
cd "$( dirname "${BASH_SOURCE[0]}" )"

section "Syntax check"
ansible-playbook -i inventory --syntax-check test.yml
section "Running role"
ansible-playbook -i inventory test.yml
65 changes: 65 additions & 0 deletions tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
- name: Bring up Docker containers
hosts: localhost
gather_facts: false
vars:
inventory:
- name: ubuntu_rolling
image: "ubuntu:rolling"
- name: ubuntu_latest
image: "ubuntu:latest"
- name: ubuntu_trusty
image: "ubuntu:trusty"
- name: debian_testing
image: "debian:testing"
- name: debian_stable
image: "debian:stable"
- name: debian_oldstable
image: "debian:oldstable"
roles:
- role: provision_docker
provision_docker_inventory: "{{ inventory }}"
provision_docker_privileged: true
provision_docker_use_docker_connection: true

- name: Test role
hosts: docker_containers
gather_facts: false
pre_tasks:
- name: Provision Python
raw: bash -c "test -e /usr/bin/python || (apt-get -y update && apt-get install -y python-simplejson)"
register: output
changed_when: output.stdout
- setup: # Gather facts
vars:
unattended_autofix_interrupted_dpkg: false
unattended_minimal_steps: true
unattended_install_on_shutdown: true
unattended_automatic_reboot: true
roles:
# Searched for in ../.. (see ansible.cfg)
- ansible-role-unattended-upgrades
tasks:
- name: Idempotency check
include_role:
name: ansible-role-unattended-upgrades
register: idempotency
- fail:
msg: Role failed idempotency check
when: idempotency.changed

- name: Get apt-config variables
shell: apt-config dump
register: aptconfig
- name: Check for registered variables
assert:
that: item in aptconfig.stdout
with_items:
- 'APT::Periodic::Unattended-Upgrade "1"'
- 'Unattended-Upgrade::AutoFixInterruptedDpkg "false"'
- 'Unattended-Upgrade::MinimalSteps "true"'
- 'Unattended-Upgrade::InstallOnShutdown "true"'
- 'Unattended-Upgrade::Automatic-Reboot "true"'

- name: Dry run unattended-upgrades
command: /usr/bin/unattended-upgrades --dry-run

0 comments on commit d19fe39

Please sign in to comment.