diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..a3a4c1f --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,11 @@ +--- +version: 2 +jobs: + build: + machine: + docker_layer_caching: true + steps: + - checkout + - run: + name: Run tests + command: make test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a3494c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +molecule/default/tests/__pycache__/ diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..3a2255e --- /dev/null +++ b/.yamllint @@ -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 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e116dfa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,2 @@ +FROM quay.io/ansible/molecule:latest +RUN sudo pip install docker-py diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8ab66bb --- /dev/null +++ b/Makefile @@ -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} . diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..1583188 --- /dev/null +++ b/molecule/default/molecule.yml @@ -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 diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml new file mode 100644 index 0000000..80f7e36 --- /dev/null +++ b/molecule/default/playbook.yml @@ -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"} diff --git a/molecule/default/requirements.yml b/molecule/default/requirements.yml new file mode 100644 index 0000000..f432b40 --- /dev/null +++ b/molecule/default/requirements.yml @@ -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 diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py new file mode 100644 index 0000000..eedd64a --- /dev/null +++ b/molecule/default/tests/test_default.py @@ -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' diff --git a/molecule/default/tests/test_default.pyc b/molecule/default/tests/test_default.pyc new file mode 100644 index 0000000..fe2021d Binary files /dev/null and b/molecule/default/tests/test_default.pyc differ diff --git a/tasks/admin.yml b/tasks/admin.yml index 14a2118..375c1e9 100644 --- a/tasks/admin.yml +++ b/tasks/admin.yml @@ -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 \ No newline at end of file + status_code: 200, 409 diff --git a/tasks/clean-up.yml b/tasks/clean-up.yml index 6cfa613..594fb70 100644 --- a/tasks/clean-up.yml +++ b/tasks/clean-up.yml @@ -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 diff --git a/tasks/endpoints.yml b/tasks/endpoints.yml index 14e386b..6b7dc9e 100644 --- a/tasks/endpoints.yml +++ b/tasks/endpoints.yml @@ -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 @@ -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 @@ -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: diff --git a/tasks/install.yml b/tasks/install.yml index fd2d647..997ec9c 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -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" diff --git a/tasks/main.yml b/tasks/main.yml index b9179d3..d42e5c2 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -18,7 +18,7 @@ uri: url: "{{ portainer_endpoint }}/settings" method: PUT - return_content: yes + return_content: true headers: Authorization: "{{ (auth_token.content|from_json).jwt }}" body_format: json @@ -26,4 +26,4 @@ when: configure_settings - include: registry.yml - when: configure_registry \ No newline at end of file + when: configure_registry diff --git a/tasks/registry.yml b/tasks/registry.yml index b7e0b9d..21dfcf8 100644 --- a/tasks/registry.yml +++ b/tasks/registry.yml @@ -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