Skip to content

Commit

Permalink
Test setup + linting fixes (#4)
Browse files Browse the repository at this point in the history
* add molecule-based test-setup
* fix: linting errors
* run tests on circle ci
  • Loading branch information
rwos authored and till committed Jan 10, 2019
1 parent 82355e5 commit a90e769
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 16 deletions.
11 changes: 11 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
version: 2
jobs:
build:
machine:
docker_layer_caching: true
steps:
- checkout
- run:
name: Run tests
command: make test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
molecule/default/tests/__pycache__/
13 changes: 13 additions & 0 deletions .yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extends: default

rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
line-length: disable
# NOTE(retr0h): Templates no longer fail this lint rule.
# Uncomment if running old Molecule templates.
# truthy: disable
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM quay.io/ansible/molecule:latest
RUN sudo pip install docker-py
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SHELL=/bin/bash
TESTIMAGENAME=molecule-test

test: build-testimage
docker run --rm -it \
-v '${PWD}':/tmp/ansible-role-portainer \
-v /var/run/docker.sock:/var/run/docker.sock \
-w /tmp/ansible-role-portainer \
${TESTIMAGENAME} \
sudo molecule test
# sudo molecule init scenario -r ${ROLENAME} -s default -d docker

build-testimage:
docker build -t ${TESTIMAGENAME} .
37 changes: 37 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
dependency:
name: galaxy
driver:
name: docker
lint:
name: yamllint
platforms:
- name: instance
image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest"
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
provisioner:
name: ansible
lint:
name: ansible-lint
scenario:
name: default
test_sequence:
- lint
- destroy
- dependency
- syntax
- create
- prepare
- converge
# - idempotence
- side_effect
- verify
- destroy
verifier:
name: testinfra
lint:
name: flake8
18 changes: 18 additions & 0 deletions molecule/default/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
- name: Converge
hosts: all
roles:
- role: geerlingguy.repo-epel
- role: geerlingguy.pip
- role: geerlingguy.docker
- role: ansible-role-portainer
vars:
pip_install_packages:
- name: docker-py
persistent_data_path: /opt/portainer:/data
company_logo_url: http://example.com/foo.png
portainer_endpoint: "http://{{ inventory_hostname }}:{{ host_port }}/api"
admin_user: admin
admin_password: admin
endpoints:
- {name: portainer-mgmt, url: "unix:///var/run/docker.sock"}
7 changes: 7 additions & 0 deletions molecule/default/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- src: geerlingguy.repo-epel
version: 1.2.3
- src: geerlingguy.pip
version: 1.3.0
- src: geerlingguy.docker
version: 2.5.2
14 changes: 14 additions & 0 deletions molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os

import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


def test_hosts_file(host):
f = host.file('/etc/hosts')

assert f.exists
assert f.user == 'root'
assert f.group == 'root'
Binary file added molecule/default/tests/test_default.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions tasks/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
uri:
url: "{{ portainer_endpoint }}/users/admin/init"
method: POST
return_content: yes
return_content: true
body_format: json
body: '{ "Username":"{{ admin_user }}", "Password":"{{ admin_password }}"}'
status_code: 200, 409
status_code: 200, 409
4 changes: 2 additions & 2 deletions tasks/clean-up.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
docker_container:
name: portainer
state: absent
purge_networks: yes
force_kill: yes
purge_networks: true
force_kill: true
when: remove_existing_container

- name: Remove persistent data
Expand Down
17 changes: 9 additions & 8 deletions tasks/endpoints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
uri:
url: "{{ portainer_endpoint }}/auth"
method: POST
return_content: yes
return_content: true
body_format: json
body: '{"Username": "{{ admin_user }}", "Password": "{{ admin_password }}"}'
register: auth_token
Expand All @@ -13,7 +13,7 @@
uri:
url: "{{ portainer_endpoint }}/endpoints"
method: GET
return_content: yes
return_content: true
headers:
Authorization: Bearer {{ (auth_token.content|from_json).jwt }}
register: portainer_known_endpoints_raw
Expand All @@ -33,17 +33,18 @@
-F "URL={{ item.url }}" \
-F "EndpointType=1"
args:
warn: False
warn: false
with_items:
- "{{ endpoints | list }}"
when: item.name not in portainer_known_endpoints
register: response
tags: [skip_ansible_lint]

#- name: "Debug response"
# debug:
# msg: "Endpoint {{ item.item.name }}, Response: {{ item.stdout| default('{}')|from_json }}"
# with_items:
# - "{{ response.results }}"
# - name: "Debug response"
# debug:
# msg: "Endpoint {{ item.item.name }}, Response: {{ item.stdout| default('{}')|from_json }}"
# with_items:
# - "{{ response.results }}"

- name: Verifying calls
fail:
Expand Down
2 changes: 1 addition & 1 deletion tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
labels: "{{ container_labels | default(omit) }}"
state: started
detach: true
recreate: yes
recreate: true
restart_policy: "{{ container_restart_policy }}"
published_ports:
- "{{ host_port }}:9000"
Expand Down
4 changes: 2 additions & 2 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
uri:
url: "{{ portainer_endpoint }}/settings"
method: PUT
return_content: yes
return_content: true
headers:
Authorization: "{{ (auth_token.content|from_json).jwt }}"
body_format: json
body: "{{ lookup('template','settings.json.j2') }}"
when: configure_settings

- include: registry.yml
when: configure_registry
when: configure_registry
2 changes: 1 addition & 1 deletion tasks/registry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
uri:
url: "{{ portainer_endpoint }}/registries"
method: POST
return_content: yes
return_content: true
headers:
Authorization: "{{ (auth_token.content|from_json).jwt }}"
body_format: json
Expand Down

0 comments on commit a90e769

Please sign in to comment.