Replace Molecule's Python verifier with the Ansible verifier (#285)

This commit is contained in:
Alessandro Fael Garcia 2020-07-20 13:37:36 +02:00 committed by GitHub
parent e9b6c59733
commit 730ab15ecb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 215 additions and 288 deletions

View File

@ -63,9 +63,7 @@ before_install:
install:
- pip install ansible==2.9.9
- pip install molecule[docker]==3.0.4
- pip install testinfra
- pip install ansible-lint
- pip install flake8
script:
- molecule --version
- ansible --version

View File

@ -195,12 +195,12 @@ Example Playbooks
Working functional playbook examples can be found in the **`molecule/common`** directory in the following files:
- **[molecule/common/playbook_default.yml](https://github.com/nginxinc/ansible-role-nginx/blob/master/molecule/common/playbook_default.yml):** Install a specific version of NGINX and set up logrotate
- **[molecule/common/playbook_module.yml](https://github.com/nginxinc/ansible-role-nginx/blob/master/molecule/common/playbook_module.yml):** Install various NGINX supported modules
- **[molecule/common/playbook_source.yml](https://github.com/nginxinc/ansible-role-nginx/blob/master/molecule/common/playbook_source.yml):** Install NGINX from source
- **[molecule/common/playbook_stable_push.yml](https://github.com/nginxinc/ansible-role-nginx/blob/master/molecule/common/playbook_stable_push.yml):** Install NGINX using the stable branch and push a preexisting config from your system to your NGINX instance
- **[molecule/common/playbook_template.yml](https://github.com/nginxinc/ansible-role-nginx/blob/master/molecule/common/playbook_template.yml):** Use the NGINX configuration templating variables to create an NGINX configuration file
- **[molecule/common/playbook_unit.yml](https://github.com/nginxinc/ansible-role-nginx/blob/master/molecule/common/playbook_unit.yml):** Install NGINX Unit
- **[molecule/common/playbooks/default_converge.yml](https://github.com/nginxinc/ansible-role-nginx/blob/master/molecule/common/playbooks/default_converge.yml):** Install a specific version of NGINX and set up logrotate
- **[molecule/common/playbooks/module_converge.yml](https://github.com/nginxinc/ansible-role-nginx/blob/master/molecule/common/playbooks/module_converge.yml):** Install various NGINX supported modules
- **[molecule/common/playbooks/source_converge.yml](https://github.com/nginxinc/ansible-role-nginx/blob/master/molecule/common/playbooks/source_converge.yml):** Install NGINX from source
- **[molecule/common/playbooks/stable_push_converge.yml](https://github.com/nginxinc/ansible-role-nginx/blob/master/molecule/common/playbooks/stable_push_converge.yml):** Install NGINX using the stable branch and push a preexisting config from your system to your NGINX instance
- **[molecule/common/playbooks/template_converge.yml](https://github.com/nginxinc/ansible-role-nginx/blob/master/molecule/common/playbooks/template_converge.yml):** Use the NGINX configuration templating variables to create an NGINX configuration file
- **[molecule/common/playbooks/unit_converge.yml](https://github.com/nginxinc/ansible-role-nginx/blob/master/molecule/common/playbooks/unit_converge.yml):** Install NGINX Unit
Do note that if you install this repository via Ansible Galaxy, you will have to replace the role variable in the sample playbooks from `ansible-role-nginx` to `nginxinc.nginx`.

View File

@ -0,0 +1,24 @@
---
- name: Verify
hosts: all
tasks:
- name: Check if NGINX is installed
package:
name: nginx
check_mode: yes
register: install
failed_when: (install is changed) or (install is failed)
- name: Check if NGINX service is running
service:
name: nginx
state: started
enabled: yes
check_mode: yes
register: service
failed_when: (service is changed) or (service is failed)
- name: Verify NGINX is up and running
uri:
url: http://localhost
status_code: 200

View File

@ -0,0 +1,25 @@
---
- name: Verify
hosts: all
tasks:
- name: Check if NGINX is installed
package:
name: nginx
check_mode: yes
register: install
failed_when: (install is changed) or (install is failed)
- name: Check if NGINX service is running
service:
name: nginx
state: started
enabled: yes
check_mode: yes
register: service
failed_when: (service is changed) or (service is failed)
- name: Check default.conf does not exist
stat:
path: /etc/nginx/conf.d/default.conf
register: stat_result
failed_when: stat_result.stat.exists

View File

@ -0,0 +1,17 @@
---
- name: Verify
hosts: all
tasks:
- name: Check if NGINX service is running
service:
name: nginx
state: started
enabled: yes
check_mode: yes
register: service
failed_when: (service is changed) or (service is failed)
- name: Verify NGINX is up and running
uri:
url: http://localhost
status_code: 200

View File

@ -8,6 +8,6 @@
nginx_branch: stable
nginx_main_upload_enable: true
nginx_main_upload_src: files/nginx.conf
nginx_main_upload_src: ../files/nginx.conf
nginx_http_upload_enable: true
nginx_http_upload_src: files/http/*.conf
nginx_http_upload_src: ../files/http/*.conf

View File

@ -0,0 +1,39 @@
---
- name: Verify
hosts: all
tasks:
- name: Check if NGINX is installed
package:
name: nginx
check_mode: yes
register: install
failed_when: (install is changed) or (install is failed)
- name: Check if NGINX service is running
service:
name: nginx
state: started
enabled: yes
check_mode: yes
register: service
failed_when: (service is changed) or (service is failed)
- name: Verify NGINX is up and running
uri:
url: http://localhost
status_code: 200
- name: Check default.conf exists
stat:
path: /etc/nginx/conf.d/default.conf
register: stat_result
failed_when: not stat_result.stat.exists
- name: Ensure default.conf contains 'location /'
lineinfile:
path: /etc/nginx/conf.d/default.conf
line: " location / {"
state: present
check_mode: yes
register: conf
failed_when: (conf is changed) or (conf is failed)

View File

@ -0,0 +1,69 @@
---
- name: Verify
hosts: all
tasks:
- name: Check if NGINX is installed
package:
name: nginx
check_mode: yes
register: install
failed_when: (install is changed) or (install is failed)
- name: Check if NGINX service is running
service:
name: nginx
state: started
enabled: yes
check_mode: yes
register: service
failed_when: (service is changed) or (service is failed)
- name: Verify NGINX is up and running
uri:
url: http://localhost
status_code: 200
- name: Check default.conf exists
stat:
path: /etc/nginx/conf.d/default.conf
register: stat_result
failed_when: not stat_result.stat.exists
- name: Check frontend_default.conf exists
stat:
path: /etc/nginx/conf.d/frontend_default.conf
register: stat_result
failed_when: not stat_result.stat.exists
- name: Check backend_default.conf exists
stat:
path: /etc/nginx/conf.d/backend_default.conf
register: stat_result
failed_when: not stat_result.stat.exists
- name: Ensure default.conf contains 'location /'
lineinfile:
path: /etc/nginx/conf.d/default.conf
line: " location / {"
state: present
check_mode: yes
register: conf
failed_when: (conf is changed) or (conf is failed)
- name: Ensure default.conf contains 'location /backend'
lineinfile:
path: /etc/nginx/conf.d/default.conf
line: " location /backend {"
state: present
check_mode: yes
register: conf
failed_when: (conf is changed) or (conf is failed)
- name: Ensure default.conf contains 'client_max_body_size 512k;'
lineinfile:
path: /etc/nginx/conf.d/default.conf
line: " client_max_body_size 512k;"
state: present
check_mode: yes
register: conf
failed_when: (conf is changed) or (conf is failed)

View File

@ -1,30 +0,0 @@
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
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

View File

@ -1,24 +0,0 @@
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
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'

View File

@ -1,25 +0,0 @@
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
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

View File

@ -1,42 +0,0 @@
import nginx
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
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
def test_generated_files(host):
assert host.file('/etc/nginx/conf.d/default.conf').exists
def test_default_server(host):
f = host.file('/etc/nginx/conf.d/default.conf')
c = nginx.loads(f.content_string)
lf = c.server.filter('Location', '/')
assert len(lf) == 1

View File

@ -1,58 +0,0 @@
import nginx
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
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
def test_generated_files(host):
assert host.file('/etc/nginx/conf.d/default.conf').exists
assert host.file('/etc/nginx/conf.d/frontend_default.conf').exists
assert host.file('/etc/nginx/conf.d/backend_default.conf').exists
def test_default_server(host):
f = host.file('/etc/nginx/conf.d/default.conf')
c = nginx.loads(f.content_string)
lf = c.server.filter('Location', '/')
assert len(lf) == 1
lb = c.server.filter('Location', '/backend')
assert len(lb) == 1
def test_client_max_body_size(host):
f = host.file('/etc/nginx/conf.d/default.conf')
c = nginx.loads(f.content_string)
vs = c.server.filter('Key', 'client_max_body_size')
assert len(vs) == 1
assert vs[0].value == '512k'
lc = c.server.filter('Location', '/')
vl = lc[0].filter('Key', 'client_max_body_size')
assert len(vl) == 1
assert vl[0].value == '5m'

View File

@ -5,7 +5,6 @@ lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: debian-stretch
image: debian:stretch-slim
@ -45,7 +44,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_default.yml
verifier:
name: testinfra
directory: ../common/test_default
converge: ../common/playbooks/default_converge.yml
verify: ../common/playbooks/default_verify.yml

View File

@ -5,7 +5,6 @@ lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: alpine-3.8
image: alpine:3.8
@ -38,7 +37,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_default.yml
verifier:
name: testinfra
directory: ../common/test_default
converge: ../common/playbooks/default_converge.yml
verify: ../common/playbooks/default_verify.yml

View File

@ -5,7 +5,6 @@ lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: centos-6
image: centos:6
@ -27,7 +26,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_default.yml
verifier:
name: testinfra
directory: ../common/test_default
converge: ../common/playbooks/default_converge.yml
verify: ../common/playbooks/default_verify.yml

View File

@ -5,7 +5,6 @@ lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: debian-stretch
image: debian:stretch-slim
@ -45,7 +44,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_module.yml
verifier:
name: testinfra
directory: ../common/test_module
converge: ../common/playbooks/module_converge.yml
verify: ../common/playbooks/module_verify.yml

View File

@ -5,7 +5,6 @@ lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: alpine-3.8
image: alpine:3.8
@ -38,7 +37,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_module.yml
verifier:
name: testinfra
directory: ../common/test_module
converge: ../common/playbooks/module_converge.yml
verify: ../common/playbooks/module_verify.yml

View File

@ -5,7 +5,6 @@ lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: centos-6
image: centos:6
@ -27,7 +26,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_module.yml
verifier:
name: testinfra
directory: ../common/test_module
converge: ../common/playbooks/module_converge.yml
verify: ../common/playbooks/module_verify.yml

View File

@ -5,7 +5,6 @@ lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: debian-stretch
image: debian:stretch-slim
@ -45,7 +44,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_source.yml
verifier:
name: testinfra
directory: ../common/test_source
converge: ../common/playbooks/source_converge.yml
verify: ../common/playbooks/source_verify.yml

View File

@ -5,7 +5,6 @@ lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: alpine-3.8
image: alpine:3.8
@ -38,7 +37,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_source.yml
verifier:
name: testinfra
directory: ../common/test_source
converge: ../common/playbooks/source_converge.yml
verify: ../common/playbooks/source_verify.yml

View File

@ -5,7 +5,6 @@ lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: centos-7
image: centos:7
@ -24,7 +23,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_source.yml
verifier:
name: testinfra
directory: ../common/test_source
converge: ../common/playbooks/source_converge.yml
verify: ../common/playbooks/source_verify.yml

View File

@ -1,14 +1,10 @@
---
dependency:
name: shell
command: pip install python-nginx
driver:
name: docker
lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: debian-stretch
image: debian:stretch-slim
@ -48,7 +44,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_stable_push.yml
verifier:
name: testinfra
directory: ../common/test_stable_push
converge: ../common/playbooks/stable_push_converge.yml
verify: ../common/playbooks/stable_push_verify.yml

View File

@ -1,14 +1,10 @@
---
dependency:
name: shell
command: pip install python-nginx
driver:
name: docker
lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: alpine-3.8
image: alpine:3.8
@ -41,7 +37,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_stable_push.yml
verifier:
name: testinfra
directory: ../common/test_stable_push
converge: ../common/playbooks/stable_push_converge.yml
verify: ../common/playbooks/stable_push_verify.yml

View File

@ -1,14 +1,10 @@
---
dependency:
name: shell
command: pip install python-nginx
driver:
name: docker
lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: centos-6
image: centos:6
@ -30,7 +26,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_stable_push.yml
verifier:
name: testinfra
directory: ../common/test_stable_push
converge: ../common/playbooks/stable_push_converge.yml
verify: ../common/playbooks/stable_push_verify.yml

View File

@ -1,14 +1,10 @@
---
dependency:
name: shell
command: pip install python-nginx
driver:
name: docker
lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: debian-stretch
image: debian:stretch-slim
@ -48,7 +44,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_template.yml
verifier:
name: testinfra
directory: ../common/test_template
converge: ../common/playbooks/template_converge.yml
verify: ../common/playbooks/template_verify.yml

View File

@ -1,14 +1,10 @@
---
dependency:
name: shell
command: pip install python-nginx
driver:
name: docker
lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: alpine-3.8
image: alpine:3.8
@ -41,7 +37,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_template.yml
verifier:
name: testinfra
directory: ../common/test_template
converge: ../common/playbooks/template_converge.yml
verify: ../common/playbooks/template_verify.yml

View File

@ -1,14 +1,10 @@
---
dependency:
name: shell
command: pip install python-nginx
driver:
name: docker
lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: centos-6
image: centos:6
@ -30,7 +26,5 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_template.yml
verifier:
name: testinfra
directory: ../common/test_template
converge: ../common/playbooks/template_converge.yml
verify: ../common/playbooks/template_verify.yml

View File

@ -5,7 +5,6 @@ lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: debian-stretch
image: debian:stretch-slim
@ -45,4 +44,4 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_unit.yml
converge: ../common/playbooks/unit_converge.yml

View File

@ -5,7 +5,6 @@ lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: alpine-3.8
image: alpine:3.8
@ -38,4 +37,4 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_unit.yml
converge: ../common/playbooks/unit_converge.yml

View File

@ -5,7 +5,6 @@ lint: |
set -e
yamllint .
ansible-lint
flake8
platforms:
- name: centos-6
image: centos:6
@ -27,4 +26,4 @@ platforms:
provisioner:
name: ansible
playbooks:
converge: ../common/playbook_unit.yml
converge: ../common/playbooks/unit_converge.yml