Skip to content

Commit

Permalink
Switched to use Molecule to test role (#5)
Browse files Browse the repository at this point in the history
Makes it easy to test locally during development.

Enhancement: resolves #2
  • Loading branch information
freemanjp committed Aug 19, 2016
1 parent 5dbca70 commit 353fc2f
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 53 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,12 @@ local.properties

### Ansible ###
*.retry

####################
### Custom rules ###
####################

### Molecule ###

.cache
.molecule
44 changes: 20 additions & 24 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---
language: python
python: '2.7'

# Require the standard build environment
sudo: required

Expand All @@ -9,36 +12,29 @@ dist: trusty
services:
- docker

before_install:
# Create the Docker image
- sudo docker build --rm --file=tests/Dockerfile --tag=test_img tests

# Verify test user
- sudo docker run --rm --user=test_usr test_img sudo ansible --version
# Cache Ansible and Molecule to speed things up
cache:
- pip

script:
# Assign the name of the current directory as the repo_name
- 'repo_name=${PWD##*/}'

# Create the Docker container
- sudo docker run --detach --volume=$(pwd):/ansible/$repo_name --name=test_dkr test_img /sbin/init
before_install:
- sudo apt-get -qq update
- sudo apt-get install -o Dpkg::Options::='--force-confold' --force-yes -y docker-engine

# Save Docker exec command to an environment variable
- 'exec_dkr="sudo docker exec --user=test_usr test_dkr"'
install:
# Install Ansible
- pip install ansible

# Basic role syntax check
- '$exec_dkr bash -c "cd /ansible/$repo_name && ansible-playbook -i tests/inventory --syntax-check tests/test.yml"'
# Install Molecule
- pip install molecule

# Run the playbook with ansible-playbook
- '$exec_dkr bash -c "cd /ansible/$repo_name && ansible-playbook -i tests/inventory tests/test.yml"'
# Check Ansible version
- ansible --version

# Check the Java is installed
- '$exec_dkr bash -c "source /etc/profile && java -version"'
- '$exec_dkr bash -c "source /etc/profile && javac -version"'
# Check Molecule version
- molecule --version

# Clean up
- sudo docker stop test_dkr
- sudo docker rm test_dkr
script:
- molecule test

notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ Example Playbook
- { role: gantsign.java }
```

Development & Testing
---------------------

This project uses [Molecule](http://molecule.readthedocs.io/) to aid in the
development and testing; the role is unit tested using
[Testinfra](http://testinfra.readthedocs.io/) and
[pytest](http://docs.pytest.org/).

To develop or test you'll need to have installed the following:
* Linux (e.g. [Ubuntu](http://www.ubuntu.com/))
* [Docker](https://www.docker.com/)
* [Python](https://www.python.org/) (including python-pip)
* [Ansible](https://www.ansible.com/)
* [Molecule](http://molecule.readthedocs.io/)

To run the role (i.e. the `tests/test.yml` playbook), and test the results
(`tests/test_role.py`), execute the following command from the project root
(i.e. the directory with `molecule.yml` in it):

```bash
molecule test
```

License
-------

Expand Down
4 changes: 0 additions & 4 deletions ansible.cfg

This file was deleted.

9 changes: 9 additions & 0 deletions molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
ansible:
playbook: tests/test.yml

docker:
containers:
- name: ansible-role-java-01
image: ubuntu
image_version: '16.04'
16 changes: 14 additions & 2 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
mode: 'u=rwx,go=rx'
dest: "{{ local_ansible_data_path }}"

# Ensure CA certificates installed (so we can download the JDK)
- name: ensure ca-certificates installed
become: yes
apt:
name: ca-certificates
state: present
when: ansible_os_family == "Debian"

# Download install files
- name: download JDK
get_url:
Expand Down Expand Up @@ -67,7 +75,8 @@
unarchive:
src: "{{ local_ansible_data_path }}/{{ java_redis_filename }}"
dest: "{{ java_install_dir }}"
creates: "{{ java_home }}/bin/java"
creates: "{{ java_home }}/bin/java"
copy: no
owner: root
group: root
mode: 'go-w'
Expand All @@ -82,13 +91,15 @@
unarchive:
src: "{{ local_ansible_data_path }}/{{ java_jce_redis_filename }}"
dest: "{{ local_ansible_data_path }}"
creates: "{{ local_ansible_data_path }}/{{ java_jce_redis_folder }}/local_policy.jar"
creates: "{{ local_ansible_data_path }}/{{ java_jce_redis_folder }}/local_policy.jar"
copy: no
mode: 'go-w'

- name: install local_policy.jar
become: yes
copy:
src: "{{ local_ansible_data_path }}/{{ java_jce_redis_folder }}/local_policy.jar"
remote_src: yes
dest: "{{ java_home }}/jre/lib/security/local_policy.jar"
owner: root
group: root
Expand All @@ -97,6 +108,7 @@
become: yes
copy:
src: "{{ local_ansible_data_path }}/{{ java_jce_redis_folder }}/US_export_policy.jar"
remote_src: yes
dest: "{{ java_home }}/jre/lib/security/US_export_policy.jar"
owner: root
group: root
Expand Down
20 changes: 0 additions & 20 deletions tests/Dockerfile

This file was deleted.

1 change: 0 additions & 1 deletion tests/inventory

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- hosts: localhost
- hosts: all
remote_user: root
roles:
- ansible-role-java
9 changes: 9 additions & 0 deletions tests/test_role.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest

@pytest.mark.parametrize('command', [
('java'),
('javac')
])
def test_java_tools(Command, command):
cmd = Command('. /etc/profile && ' + command + ' -version')
assert cmd.rc == 0
2 changes: 1 addition & 1 deletion vars/jce-versions/8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ java_jce_redis_mirror: "{{ java_mirror_base }}/jce/8"
# The JCE redistributable file name
java_jce_redis_filename: jce_policy-8.zip

# The root folder name inside the JCE redistributable
# The root folder name inside the JCE redistributable
java_jce_redis_folder: UnlimitedJCEPolicyJDK8

0 comments on commit 353fc2f

Please sign in to comment.