Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace test setup with molecule #10

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.vagrant

*.log
*.retry
.molecule
tests/.cache/v/cache
tests/__pycache__
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
2 changes: 2 additions & 0 deletions ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[defaults]
roles_path = .molecule:../
26 changes: 0 additions & 26 deletions examples/Vagrantfile

This file was deleted.

3 changes: 0 additions & 3 deletions examples/roles/app.papertrail/meta/main.yml

This file was deleted.

12 changes: 0 additions & 12 deletions examples/roles/app.papertrail/tasks/main.yml

This file was deleted.

3 changes: 0 additions & 3 deletions examples/roles/app.papertrail/vars/main.yml

This file was deleted.

1 change: 0 additions & 1 deletion examples/roles/azavea.papertrail

This file was deleted.

28 changes: 28 additions & 0 deletions molecule.yml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 17 additions & 1 deletion examples/site.yml → playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
- 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
Expand All @@ -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
60 changes: 60 additions & 0 deletions tests/test_papertrail.py
Original file line number Diff line number Diff line change
@@ -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