From 16f21c5c7c51314eea1d023a46bcaad8c889cd46 Mon Sep 17 00:00:00 2001 From: Alessandro Fael Garcia Date: Thu, 21 Nov 2019 19:53:30 +0100 Subject: [PATCH] Optimise testing and fix CentOS GeoIP module (#185) * Use slim Debian images for testing * Update `volume_mounts` to `volumes` * Update and move default Dockerfile * Use normal centos image * Add epel-release prerequisite to CentOS modules * Separate module and template tests --- .travis.yml | 3 +- molecule/common/Dockerfile.j2 | 42 +++++++++++++++++++ molecule/default/Dockerfile.j2 | 14 ------- molecule/default/molecule.yml | 15 +++++-- molecule/default/playbook.yml | 4 +- molecule/module/molecule.yml | 37 ++++++++++++++++ molecule/module/playbook.yml | 16 +++++++ molecule/module/tests/test_default.py | 29 +++++++++++++ molecule/stable_push/Dockerfile.j2 | 14 ------- molecule/stable_push/molecule.yml | 15 +++++-- .../molecule.yml | 15 +++++-- .../playbook.yml | 9 ---- .../tests/test_default.py | 0 molecule/template_module/Dockerfile.j2 | 14 ------- molecule/unit/Dockerfile.j2 | 14 ------- molecule/unit/molecule.yml | 15 +++++-- tasks/modules/install-geoip.yml | 6 +++ 17 files changed, 178 insertions(+), 84 deletions(-) create mode 100644 molecule/common/Dockerfile.j2 delete mode 100644 molecule/default/Dockerfile.j2 create mode 100644 molecule/module/molecule.yml create mode 100644 molecule/module/playbook.yml create mode 100644 molecule/module/tests/test_default.py delete mode 100644 molecule/stable_push/Dockerfile.j2 rename molecule/{template_module => template}/molecule.yml (60%) rename molecule/{template_module => template}/playbook.yml (98%) rename molecule/{template_module => template}/tests/test_default.py (100%) delete mode 100644 molecule/template_module/Dockerfile.j2 delete mode 100644 molecule/unit/Dockerfile.j2 diff --git a/.travis.yml b/.travis.yml index 598f88f..bbf83dd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,8 +5,9 @@ services: - docker env: - scenario: default + - scenario: module - scenario: stable_push - - scenario: template_module + - scenario: template - scenario: unit before_install: - sudo apt-get -qq update diff --git a/molecule/common/Dockerfile.j2 b/molecule/common/Dockerfile.j2 new file mode 100644 index 0000000..bb20dd7 --- /dev/null +++ b/molecule/common/Dockerfile.j2 @@ -0,0 +1,42 @@ +# Molecule managed + +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +{% if item.env is defined %} +{% for var, value in item.env.items() %} +{% if value %} +ENV {{ var }} {{ value }} +{% endif %} +{% endfor %} +{% endif %} + +RUN \ + if [ $(command -v apt-get) ]; then \ + apt-get update \ + && apt-get install -y python sudo bash ca-certificates iproute2 curl \ + && apt-get clean; \ + elif [ $(command -v dnf) ]; then \ + dnf makecache \ + && dnf --assumeyes install python sudo python-devel python*-dnf bash iproute \ + && dnf clean all; \ + elif [ $(command -v yum) ]; then \ + yum makecache fast \ + && yum install -y python sudo yum-plugin-ovl bash iproute \ + && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf \ + && yum clean all; \ + elif [ $(command -v zypper) ]; then \ + zypper refresh \ + && zypper install -y python sudo bash python-xml iproute2 \ + && zypper clean -a; \ + elif [ $(command -v apk) ]; then \ + apk update \ + && apk add --no-cache python sudo bash ca-certificates; \ + elif [ $(command -v xbps-install) ]; then \ + xbps-install -Syu \ + && xbps-install -y python sudo bash ca-certificates iproute2 \ + && xbps-remove -O; \ + fi diff --git a/molecule/default/Dockerfile.j2 b/molecule/default/Dockerfile.j2 deleted file mode 100644 index b3519e9..0000000 --- a/molecule/default/Dockerfile.j2 +++ /dev/null @@ -1,14 +0,0 @@ -# Molecule managed - -{% if item.registry is defined %} -FROM {{ item.registry.url }}/{{ item.image }} -{% else %} -FROM {{ item.image }} -{% endif %} - -RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y curl python sudo bash ca-certificates && apt-get clean; \ - elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash && dnf clean all; \ - elif [ $(command -v yum) ]; then yum makecache fast && yum install -y epel-release python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ - elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml && zypper clean -a; \ - elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \ - elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; fi diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index a061ac1..f66127b 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -6,20 +6,27 @@ lint: platforms: - name: centos-6 image: centos:6 + dockerfile: ../common/Dockerfile.j2 - name: centos-7 - image: centos/systemd + image: centos:7 + dockerfile: ../common/Dockerfile.j2 privileged: True - volume_mounts: + volumes: - "/sys/fs/cgroup:/sys/fs/cgroup:rw" command: "/usr/sbin/init" + tty: True - name: debian-stretch - image: debian:stretch + image: debian:stretch-slim + dockerfile: ../common/Dockerfile.j2 - name: debian-buster - image: debian:buster + image: debian:buster-slim + dockerfile: ../common/Dockerfile.j2 - name: ubuntu-xenial image: ubuntu:xenial + dockerfile: ../common/Dockerfile.j2 - name: ubuntu-bionic image: ubuntu:bionic + dockerfile: ../common/Dockerfile.j2 provisioner: name: ansible lint: diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml index 2b94a12..a434e9c 100644 --- a/molecule/default/playbook.yml +++ b/molecule/default/playbook.yml @@ -4,11 +4,11 @@ pre_tasks: - name: "Set repo if Debian" set_fact: - version: "=1.17.4-1~{{ ansible_distribution_release }}" + version: "=1.17.6-1~{{ ansible_distribution_release }}" when: ansible_os_family == "Debian" - name: "Set repo if RedHat" set_fact: - version: "-1.17.4-1.el{{ ansible_distribution_major_version }}.ngx" + version: "-1.17.6-1.el{{ ansible_distribution_major_version }}.ngx" when: ansible_os_family == "RedHat" roles: - role: ansible-role-nginx diff --git a/molecule/module/molecule.yml b/molecule/module/molecule.yml new file mode 100644 index 0000000..f66127b --- /dev/null +++ b/molecule/module/molecule.yml @@ -0,0 +1,37 @@ +--- +driver: + name: docker +lint: + name: yamllint +platforms: + - name: centos-6 + image: centos:6 + dockerfile: ../common/Dockerfile.j2 + - name: centos-7 + image: centos:7 + dockerfile: ../common/Dockerfile.j2 + privileged: True + volumes: + - "/sys/fs/cgroup:/sys/fs/cgroup:rw" + command: "/usr/sbin/init" + tty: True + - name: debian-stretch + image: debian:stretch-slim + dockerfile: ../common/Dockerfile.j2 + - name: debian-buster + image: debian:buster-slim + dockerfile: ../common/Dockerfile.j2 + - name: ubuntu-xenial + image: ubuntu:xenial + dockerfile: ../common/Dockerfile.j2 + - name: ubuntu-bionic + image: ubuntu:bionic + dockerfile: ../common/Dockerfile.j2 +provisioner: + name: ansible + lint: + name: ansible-lint +verifier: + name: testinfra + lint: + name: flake8 diff --git a/molecule/module/playbook.yml b/molecule/module/playbook.yml new file mode 100644 index 0000000..3f42b42 --- /dev/null +++ b/molecule/module/playbook.yml @@ -0,0 +1,16 @@ +--- +- name: Converge + hosts: all + roles: + - role: ansible-role-nginx + vars: + nginx_debug_output: true + + nginx_modules: + njs: true + perl: true + waf: false + geoip: true + image_filter: true + rtmp: true + xslt: true diff --git a/molecule/module/tests/test_default.py b/molecule/module/tests/test_default.py new file mode 100644 index 0000000..4b2d109 --- /dev/null +++ b/molecule/module/tests/test_default.py @@ -0,0 +1,29 @@ +import os + +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') # noqa E501 + + +def test_nginx_is_installed(host): + ngx = host.package("nginx") + assert ngx.is_installed + + +def test_nginx_running_and_enabled(host): + ngx = host.service("nginx") + assert ngx.is_running + assert ngx.is_enabled + + +def test_hosts_file(host): + ngx = host.file('/etc/hosts') + assert ngx.exists + assert ngx.user == 'root' + assert ngx.group == 'root' + + +def test_endpoint(host): + command = """curl -I http://localhost/""" + cmd = host.run(command) + assert '200 OK' in cmd.stdout diff --git a/molecule/stable_push/Dockerfile.j2 b/molecule/stable_push/Dockerfile.j2 deleted file mode 100644 index b3519e9..0000000 --- a/molecule/stable_push/Dockerfile.j2 +++ /dev/null @@ -1,14 +0,0 @@ -# Molecule managed - -{% if item.registry is defined %} -FROM {{ item.registry.url }}/{{ item.image }} -{% else %} -FROM {{ item.image }} -{% endif %} - -RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y curl python sudo bash ca-certificates && apt-get clean; \ - elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash && dnf clean all; \ - elif [ $(command -v yum) ]; then yum makecache fast && yum install -y epel-release python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ - elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml && zypper clean -a; \ - elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \ - elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; fi diff --git a/molecule/stable_push/molecule.yml b/molecule/stable_push/molecule.yml index aa431a7..85f927f 100644 --- a/molecule/stable_push/molecule.yml +++ b/molecule/stable_push/molecule.yml @@ -9,20 +9,27 @@ lint: platforms: - name: centos-6 image: centos:6 + dockerfile: ../common/Dockerfile.j2 - name: centos-7 - image: centos/systemd + image: centos:7 + dockerfile: ../common/Dockerfile.j2 privileged: True - volume_mounts: + volumes: - "/sys/fs/cgroup:/sys/fs/cgroup:rw" command: "/usr/sbin/init" + tty: True - name: debian-stretch - image: debian:stretch + image: debian:stretch-slim + dockerfile: ../common/Dockerfile.j2 - name: debian-buster - image: debian:buster + image: debian:buster-slim + dockerfile: ../common/Dockerfile.j2 - name: ubuntu-xenial image: ubuntu:xenial + dockerfile: ../common/Dockerfile.j2 - name: ubuntu-bionic image: ubuntu:bionic + dockerfile: ../common/Dockerfile.j2 provisioner: name: ansible lint: diff --git a/molecule/template_module/molecule.yml b/molecule/template/molecule.yml similarity index 60% rename from molecule/template_module/molecule.yml rename to molecule/template/molecule.yml index aa431a7..85f927f 100644 --- a/molecule/template_module/molecule.yml +++ b/molecule/template/molecule.yml @@ -9,20 +9,27 @@ lint: platforms: - name: centos-6 image: centos:6 + dockerfile: ../common/Dockerfile.j2 - name: centos-7 - image: centos/systemd + image: centos:7 + dockerfile: ../common/Dockerfile.j2 privileged: True - volume_mounts: + volumes: - "/sys/fs/cgroup:/sys/fs/cgroup:rw" command: "/usr/sbin/init" + tty: True - name: debian-stretch - image: debian:stretch + image: debian:stretch-slim + dockerfile: ../common/Dockerfile.j2 - name: debian-buster - image: debian:buster + image: debian:buster-slim + dockerfile: ../common/Dockerfile.j2 - name: ubuntu-xenial image: ubuntu:xenial + dockerfile: ../common/Dockerfile.j2 - name: ubuntu-bionic image: ubuntu:bionic + dockerfile: ../common/Dockerfile.j2 provisioner: name: ansible lint: diff --git a/molecule/template_module/playbook.yml b/molecule/template/playbook.yml similarity index 98% rename from molecule/template_module/playbook.yml rename to molecule/template/playbook.yml index 89e2a3b..2dd67e3 100644 --- a/molecule/template_module/playbook.yml +++ b/molecule/template/playbook.yml @@ -281,12 +281,3 @@ port: 8091 weight: 1 health_check: max_fails=1 fail_timeout=10s - - nginx_modules: - njs: true - perl: true - waf: false - geoip: true - image_filter: true - rtmp: true - xslt: true diff --git a/molecule/template_module/tests/test_default.py b/molecule/template/tests/test_default.py similarity index 100% rename from molecule/template_module/tests/test_default.py rename to molecule/template/tests/test_default.py diff --git a/molecule/template_module/Dockerfile.j2 b/molecule/template_module/Dockerfile.j2 deleted file mode 100644 index b3519e9..0000000 --- a/molecule/template_module/Dockerfile.j2 +++ /dev/null @@ -1,14 +0,0 @@ -# Molecule managed - -{% if item.registry is defined %} -FROM {{ item.registry.url }}/{{ item.image }} -{% else %} -FROM {{ item.image }} -{% endif %} - -RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y curl python sudo bash ca-certificates && apt-get clean; \ - elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash && dnf clean all; \ - elif [ $(command -v yum) ]; then yum makecache fast && yum install -y epel-release python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ - elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml && zypper clean -a; \ - elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \ - elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; fi diff --git a/molecule/unit/Dockerfile.j2 b/molecule/unit/Dockerfile.j2 deleted file mode 100644 index b3519e9..0000000 --- a/molecule/unit/Dockerfile.j2 +++ /dev/null @@ -1,14 +0,0 @@ -# Molecule managed - -{% if item.registry is defined %} -FROM {{ item.registry.url }}/{{ item.image }} -{% else %} -FROM {{ item.image }} -{% endif %} - -RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y curl python sudo bash ca-certificates && apt-get clean; \ - elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash && dnf clean all; \ - elif [ $(command -v yum) ]; then yum makecache fast && yum install -y epel-release python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ - elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml && zypper clean -a; \ - elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \ - elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; fi diff --git a/molecule/unit/molecule.yml b/molecule/unit/molecule.yml index b46145a..0f43ce8 100644 --- a/molecule/unit/molecule.yml +++ b/molecule/unit/molecule.yml @@ -6,20 +6,27 @@ lint: platforms: - name: centos-6 image: centos:6 + dockerfile: ../common/Dockerfile.j2 - name: centos-7 - image: centos/systemd + image: centos:7 + dockerfile: ../common/Dockerfile.j2 privileged: True - volume_mounts: + volumes: - "/sys/fs/cgroup:/sys/fs/cgroup:rw" command: "/usr/sbin/init" + tty: True - name: debian-stretch - image: debian:stretch + image: debian:stretch-slim + dockerfile: ../common/Dockerfile.j2 - name: debian-buster - image: debian:buster + image: debian:buster-slim + dockerfile: ../common/Dockerfile.j2 - name: ubuntu-xenial image: ubuntu:xenial + dockerfile: ../common/Dockerfile.j2 - name: ubuntu-bionic image: ubuntu:bionic + dockerfile: ../common/Dockerfile.j2 provisioner: name: ansible lint: diff --git a/tasks/modules/install-geoip.yml b/tasks/modules/install-geoip.yml index 739fc5d..96b7aa2 100644 --- a/tasks/modules/install-geoip.yml +++ b/tasks/modules/install-geoip.yml @@ -1,4 +1,10 @@ --- +- name: "(Install: CentOS) Install GeoIP Required CentOS Dependencies" + yum: + name: + - epel-release + when: ansible_distribution == "CentOS" + - name: "(Install: All OSs) Install NGINX Open Source GeoIP Module" package: name: nginx-module-geoip