From 5712965fd3b33d88cc564f03da5f8523bf0b905e Mon Sep 17 00:00:00 2001 From: Taylor Nation Date: Fri, 18 Aug 2017 15:51:18 -0400 Subject: [PATCH] Replace test setup with molecule --- .gitignore | 3 + README.md | 25 +++++++- ansible.cfg | 2 + examples/Vagrantfile | 13 ---- examples/roles/app.papertrail/meta/main.yml | 3 - examples/roles/app.papertrail/tasks/main.yml | 12 ---- examples/roles/app.papertrail/vars/main.yml | 3 - examples/roles/azavea.papertrail | 1 - examples/site.yml | 4 -- molecule.yml | 28 +++++++++ playbook.yml | 48 +++++++++++++++ .../templates => templates}/log_files.yml.j2 | 0 tests/test_papertrail.py | 60 +++++++++++++++++++ 13 files changed, 164 insertions(+), 38 deletions(-) create mode 100644 ansible.cfg delete mode 100644 examples/Vagrantfile delete mode 100644 examples/roles/app.papertrail/meta/main.yml delete mode 100644 examples/roles/app.papertrail/tasks/main.yml delete mode 100644 examples/roles/app.papertrail/vars/main.yml delete mode 120000 examples/roles/azavea.papertrail delete mode 100644 examples/site.yml create mode 100644 molecule.yml create mode 100644 playbook.yml rename {examples/roles/app.papertrail/templates => templates}/log_files.yml.j2 (100%) create mode 100644 tests/test_papertrail.py diff --git a/.gitignore b/.gitignore index 86a37fb..9792d5a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .vagrant +.molecule +tests/.cache/v/cache +tests/__pycache__ diff --git a/README.md b/README.md index ceca6c6..458fbae 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,27 @@ destination: new_file_check_interval: "10" # Check every 10 seconds ``` -## Example Playbook +## Testing +Tests are done using [molecule](http://molecule.readthedocs.io/). To run the test suite, install molecule and its dependencies and run ` molecule test` from the folder containing molecule.yml. To add additional tests, add a [testinfra](http://testinfra.readthedocs.org/) python script in the [tests](./tests/) directory, or add a function to [test_papertrail.py](./tests/test_papertrail.py). Information about available Testinfra modules is available [here](http://testinfra.readthedocs.io/en/latest/modules.html). -See the [examples](./examples/) directory. +### Example +``` +# Download molecule, dependencies +$ pip install molecule + +# Change to the top-level project directory, which contains molecule.yml +$ cd /path/to/ansible-papertrail + +# Ensure that molecule.yml is present +$ ls +CHANGELOG.md molecule.yml +LICENSE playbook.retry +README.md playbook.yml +ansible.cfg tasks +defaults templates +handlers tests +meta + +# We're in the right directory, so let's run tests! +$ molecule test +``` \ No newline at end of file diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..8369dbc --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,2 @@ +[defaults] +roles_path = .molecule:../ diff --git a/examples/Vagrantfile b/examples/Vagrantfile deleted file mode 100644 index 8b30eb2..0000000 --- a/examples/Vagrantfile +++ /dev/null @@ -1,13 +0,0 @@ -# -*- mode: ruby -*- -# vi: set ft=ruby : - -VAGRANTFILE_API_VERSION = "2" - -Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| - config.vm.box = "ubuntu/trusty64" - - config.vm.provision "ansible" do |ansible| - ansible.playbook = "site.yml" - ansible.sudo = true - end -end diff --git a/examples/roles/app.papertrail/meta/main.yml b/examples/roles/app.papertrail/meta/main.yml deleted file mode 100644 index 6817e73..0000000 --- a/examples/roles/app.papertrail/meta/main.yml +++ /dev/null @@ -1,3 +0,0 @@ ---- -dependencies: - - { role: "azavea.papertrail" } diff --git a/examples/roles/app.papertrail/tasks/main.yml b/examples/roles/app.papertrail/tasks/main.yml deleted file mode 100644 index bcf4198..0000000 --- a/examples/roles/app.papertrail/tasks/main.yml +++ /dev/null @@ -1,12 +0,0 @@ ---- -- name: Configure remote_syslog - template: src="log_files.yml.j2" - dest="{{ papertrail_conf }}" - notify: - - Restart remote_syslog - -- name: Add Papertrail user to service group - user: name=papertrail - append=yes - groups=adm - state=present diff --git a/examples/roles/app.papertrail/vars/main.yml b/examples/roles/app.papertrail/vars/main.yml deleted file mode 100644 index b4174f1..0000000 --- a/examples/roles/app.papertrail/vars/main.yml +++ /dev/null @@ -1,3 +0,0 @@ ---- -papertrail_host: "logs3.papertrailapp.com" -papertrail_port: 45551 diff --git a/examples/roles/azavea.papertrail b/examples/roles/azavea.papertrail deleted file mode 120000 index 6581736..0000000 --- a/examples/roles/azavea.papertrail +++ /dev/null @@ -1 +0,0 @@ -../../ \ No newline at end of file diff --git a/examples/site.yml b/examples/site.yml deleted file mode 100644 index 05de15b..0000000 --- a/examples/site.yml +++ /dev/null @@ -1,4 +0,0 @@ ---- -- hosts: all - roles: - - { role: "app.papertrail" } diff --git a/molecule.yml b/molecule.yml new file mode 100644 index 0000000..7d1b0d8 --- /dev/null +++ b/molecule.yml @@ -0,0 +1,28 @@ +--- +ansible: + config_file: ansible.cfg + become: True + +molecule: + test: + sequence: + - destroy + - syntax + - create + - converge + - verify + +vagrant: + platforms: + - name: trusty64 + box: ubuntu/trusty64 + + - name: xenial64 + box: ubuntu/xenial64 + + providers: + - name: virtualbox + type: virtualbox + + instances: + - name: ansible-papertrail diff --git a/playbook.yml b/playbook.yml new file mode 100644 index 0000000..241ed19 --- /dev/null +++ b/playbook.yml @@ -0,0 +1,48 @@ +--- +- hosts: all + gather_facts: False + + pre_tasks: + + # Check Ubuntu release version to determine if we need to install Python 2, + # an ansible dependency that isn't included by default in Ubuntu 16.04 and + # up. + + - name: Check Ubuntu release + raw: cat /etc/lsb-release | grep DISTRIB_RELEASE | cut -d "=" -f2 + register: ubuntu_release + changed_when: false + + - debug: msg="Running ubuntu version {{ ubuntu_release.stdout }}" + + # Update apt cache and install python 2 for Ubuntu versions greater than 16.04 + - name: Update APT cache + raw: apt-get update + become: True + changed_when: false + + - name: Install Python + raw: apt-get install -yq python + become: True + when: ubuntu_release.stdout | version_compare('16.04', '>=') + + # Gather facts once ansible dependencies are installed + - name: Gather facts + setup: + + roles: + # Papertrail + - { role: "ansible-papertrail" } + + tasks: + - name: Configure remote_syslog + template: src="log_files.yml.j2" + dest="{{ papertrail_conf }}" + notify: + - Restart remote_syslog + + - name: Add Papertrail user to service group + user: name=papertrail + append=yes + groups=adm + state=present diff --git a/examples/roles/app.papertrail/templates/log_files.yml.j2 b/templates/log_files.yml.j2 similarity index 100% rename from examples/roles/app.papertrail/templates/log_files.yml.j2 rename to templates/log_files.yml.j2 diff --git a/tests/test_papertrail.py b/tests/test_papertrail.py new file mode 100644 index 0000000..85c4700 --- /dev/null +++ b/tests/test_papertrail.py @@ -0,0 +1,60 @@ +import pytest +import re + + +@pytest.fixture() +def AnsibleDefaults(Ansible): + """ Load default variables into dictionary. + Args: + Ansible - Requires the ansible connection backend. + """ + return Ansible("include_vars", "./defaults/main.yml")["ansible_facts"] + + +def test_papertrail_user(User): + """Check that the Papertrail user exists with the right configuration""" + papertrail = User('papertrail') + assert papertrail.exists + assert papertrail.home == "/var/lib/papertrail" + assert papertrail.shell == "/bin/false" + assert papertrail.name == "papertrail" + + +def test_papertrail_bundle(File): + """Check that the Papertrail certificate bundle exists""" + assert File('/etc/papertrail-bundle.pem').exists + + +def test_rsyslog_gnutils(Package): + """Check that the rsyslog TLS support is installed""" + assert Package("rsyslog-gnutls").is_installed + + +def test_services(Service): + """Check that syslog/remote syslog are running""" + assert Service("rsyslog").is_enabled + assert Service("remote_syslog").is_enabled + + +def test_papertrail_configuration(File, AnsibleDefaults): + """Check make sure the proper Papertrail configuration exists""" + papertrail_config = File(AnsibleDefaults["papertrail_conf"]) + papertrail_host = AnsibleDefaults["papertrail_host"] + papertrail_port = AnsibleDefaults["papertrail_port"] + + assert papertrail_config.exists + assert re.search("host: {}".format(papertrail_host), + papertrail_config.content_string) is not None + assert re.search("port: {}".format(papertrail_port), + papertrail_config.content_string) is not None + + +def test_rsyslog_configuration(File, AnsibleDefaults): + """Check make sure the proper rsyslog configuration exists""" + rsyslog_config = File("/etc/rsyslog.d/90-papertrail.conf") + papertrail_host = AnsibleDefaults["papertrail_host"] + papertrail_port = AnsibleDefaults["papertrail_port"] + + assert rsyslog_config.exists + assert re.search("\*\.\* @@{}:{}".format(papertrail_host, papertrail_port), + rsyslog_config.content_string) is not None