diff --git a/.gitignore b/.gitignore index afb1bd7..3cd1a9b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .vagrant - *.log *.retry +.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 429149c..0000000 --- a/examples/Vagrantfile +++ /dev/null @@ -1,26 +0,0 @@ -# -*- mode: ruby -*- -# vi: set ft=ruby : - -VAGRANTFILE_API_VERSION = "2" - -Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| - config.vm.define "trusty" do |trusty| - trusty.vm.box = "ubuntu/trusty64" - trusty.vm.hostname = "trusty64" - - trusty.vm.provision "ansible" do |ansible| - ansible.playbook = "site.yml" - ansible.sudo = true - end - end - - config.vm.define "xenial" do |xenial| - xenial.vm.box = "ubuntu/xenial64" - xenial.vm.hostname = "xenial64" - - xenial.vm.provision "ansible" do |ansible| - ansible.playbook = "site.yml" - ansible.sudo = true - end - 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/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/examples/site.yml b/playbook.yml similarity index 66% rename from examples/site.yml rename to playbook.yml index a405fd1..241ed19 100644 --- a/examples/site.yml +++ b/playbook.yml @@ -11,6 +11,7 @@ - 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 }}" @@ -18,6 +19,7 @@ - name: Update APT cache raw: apt-get update become: True + changed_when: false - name: Install Python raw: apt-get install -yq python @@ -29,4 +31,18 @@ setup: roles: - - { role: "app.papertrail" } + # 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