From 496b26423e917ce7efca5e4466f5ff75cc7f038d Mon Sep 17 00:00:00 2001 From: Emerson Rocha Luiz Date: Fri, 27 Dec 2019 01:04:50 -0300 Subject: [PATCH] testinfra (#44): mananged components now create simple file at /opt/alb/info/ to allow tests know what to check --- handlers/main.yml | 59 +++++++++++++++++++ molecule/default/tests/test_default.py | 55 ++++++++--------- tasks/apps/alb/openresty-conf.yml | 5 +- tasks/haproxy/management/bsd-init.yml | 5 +- tasks/haproxy/management/systemd.yml | 1 + tasks/openresty/install/default-files.yml | 3 +- .../management/nginx-conf-global.yml | 3 +- tasks/sysapps/sysapps.yml | 4 +- tasks/ufw/install-ufw.yml | 2 + tasks/ufw/rules.yml | 4 +- tests/test_alb-standard-node.py | 45 ++++++++------ 11 files changed, 134 insertions(+), 52 deletions(-) diff --git a/handlers/main.yml b/handlers/main.yml index e802ebe..6c54a2f 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -25,3 +25,62 @@ - name: restart ufw service: name=ufw state=restarted + +## About /opt/alb/info/ files +# This folder may contain some very basic information about enabled components +# on each ALB node. The existence means a service was installed and (eventually) +# it's contents (INI format) could be used to describe more about the service. +# +# One main initial reason is allow testinfra (and later other scripts) know +# what state the node was expected to be. At this moment it just create empty +# file. + +- name: "info alb component openresty" + file: + path: "/opt/alb/info/openresty" + state: touch + modification_time: preserve + access_time: preserve + owner: "{{ alb_internal_root_user }}" + group: "{{ alb_internal_root_group }}" + mode: "0640" + +- name: "info alb component haproxy" + file: + path: "/opt/alb/info/haproxy" + state: touch + modification_time: preserve + access_time: preserve + owner: "{{ alb_internal_root_user }}" + group: "{{ alb_internal_root_group }}" + mode: "0640" + +- name: "info alb component ufw" + file: + path: "/opt/alb/info/ufw" + state: touch + modification_time: preserve + access_time: preserve + owner: "{{ alb_internal_root_user }}" + group: "{{ alb_internal_root_group }}" + mode: "0640" + +- name: "info alb component apps" + file: + path: "/opt/alb/info/apps" + state: touch + modification_time: preserve + access_time: preserve + owner: "{{ alb_internal_root_user }}" + group: "{{ alb_internal_root_group }}" + mode: "0640" + +- name: "info alb component sysapps" + file: + path: "/opt/alb/info/sysapps" + state: touch + modification_time: preserve + access_time: preserve + owner: "{{ alb_internal_root_user }}" + group: "{{ alb_internal_root_group }}" + mode: "0640" diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py index 7045e9b..1e6ee55 100644 --- a/molecule/default/tests/test_default.py +++ b/molecule/default/tests/test_default.py @@ -7,45 +7,46 @@ ).get_hosts('all') -def test_hosts_file(host): - f = host.file('/etc/hosts') +def test_required_haproxy_is_installed(host): + assert (not host.file("/opt/alb/info/haproxy").exists) \ + or host.package("haproxy").is_installed - assert f.exists - assert f.user == 'root' - assert f.group == 'root' or f.group == 'wheel' +def test_required_haproxy_is_enabled(host): + assert (not host.file("/opt/alb/info/haproxy").exists) \ + or host.service("haproxy").is_enabled -def test_haproxy_is_installed(host): - assert host.package("haproxy").is_installed +def test_required_haproxy_is_running(host): + assert (not host.file("/opt/alb/info/haproxy").exists) \ + or host.service("haproxy").is_running -def test_haproxy_is_enabled(host): - assert host.service("haproxy").is_enabled +def test_required_openresty_is_installed(host): + assert (not host.file("/opt/alb/info/openresty").exists) \ + or host.package("openresty").is_installed -def test_haproxy_is_running(host): - assert host.service("haproxy").is_running +def test_required_openresty_is_enabled(host): + assert (not host.file("/opt/alb/info/openresty").exists) \ + or host.service("openresty").is_enabled -def test_openresty_is_installed(host): - assert host.package("openresty").is_installed +def test_required_openresty_is_running(host): + assert (not host.file("/opt/alb/info/openresty").exists) \ + or host.service("openresty").is_running -def test_openresty_is_enabled(host): - assert host.service("openresty").is_enabled +def test_required_ufw_is_installed(host): + assert (not host.file("/opt/alb/info/ufw").exists) \ + or host.package("ufw").is_installed -def test_openresty_is_running(host): - assert host.service("openresty").is_running +def test_required_ufw_is_enabled(host): + assert (not host.file("/opt/alb/info/ufw").exists) \ + or host.service("ufw").is_enabled -#def test_ufw_is_installed(host): -# assert host.package("ufw").is_installed -# -# -#def test_ufw_is_enabled(host): -# assert host.service("ufw").is_enabled -# -# -#def test_ufw_is_running(host): -# assert host.service("ufw").is_running + +def test_required_ufw_is_running(host): + assert (not host.file("/opt/alb/info/ufw").exists) \ + or host.service("ufw").is_running diff --git a/tasks/apps/alb/openresty-conf.yml b/tasks/apps/alb/openresty-conf.yml index d42589a..d45ff29 100644 --- a/tasks/apps/alb/openresty-conf.yml +++ b/tasks/apps/alb/openresty-conf.yml @@ -24,4 +24,7 @@ - "(alb_run_only_app_uid is not defined) or (alb_run_only_app_uid == item.app_uid)" - "(alb_apps_facts_deployed is changed) or (alb_forceredeploy|bool)" register: result_apps_rules - notify: reload openresty + notify: + - "reload openresty" + - "info alb component apps" + diff --git a/tasks/haproxy/management/bsd-init.yml b/tasks/haproxy/management/bsd-init.yml index 6b0e8eb..36e89dc 100644 --- a/tasks/haproxy/management/bsd-init.yml +++ b/tasks/haproxy/management/bsd-init.yml @@ -12,12 +12,13 @@ mode: '0644' backup: yes notify: - - reload haproxy + - "reload haproxy" + - "info alb component haproxy" # Enabled/Started must be AFTER moving new /etc/haproxy/haproxy.cfg # configurations or Ansible will not be able to update one new valid # configuration if old one was already with error. By moving this step -# after we avoid user being forced to solve manually on the server +# after we avoid user being forced to solve manually on the server - name: "haproxy | management | bsd-init.yml: enable haproxy.service" service: name: haproxy diff --git a/tasks/haproxy/management/systemd.yml b/tasks/haproxy/management/systemd.yml index 3f76609..243a92a 100644 --- a/tasks/haproxy/management/systemd.yml +++ b/tasks/haproxy/management/systemd.yml @@ -13,6 +13,7 @@ backup: yes notify: - reload haproxy + - info alb component haproxy # Enabled/Started must be AFTER moving new /etc/haproxy/haproxy.cfg # configurations or Ansible will not be able to update one new valid diff --git a/tasks/openresty/install/default-files.yml b/tasks/openresty/install/default-files.yml index 9304337..cdcfb3f 100644 --- a/tasks/openresty/install/default-files.yml +++ b/tasks/openresty/install/default-files.yml @@ -19,4 +19,5 @@ mode: '0644' backup: yes notify: - - reload openresty + - "reload openresty" + - "info alb component openresty" diff --git a/tasks/openresty/management/nginx-conf-global.yml b/tasks/openresty/management/nginx-conf-global.yml index d09ae75..e9562cc 100644 --- a/tasks/openresty/management/nginx-conf-global.yml +++ b/tasks/openresty/management/nginx-conf-global.yml @@ -11,4 +11,5 @@ mode: '0644' backup: yes notify: - - reload openresty + - "reload openresty" + - "info alb component openresty" diff --git a/tasks/sysapps/sysapps.yml b/tasks/sysapps/sysapps.yml index b7b262a..95eefa6 100644 --- a/tasks/sysapps/sysapps.yml +++ b/tasks/sysapps/sysapps.yml @@ -69,7 +69,9 @@ when: - alb_internal_sysapps is defined and alb_internal_sysapps[0] is defined - "(alb_sysapps_facts_deployed is changed) or (alb_forceredeploy|bool)" - notify: reload openresty + notify: + - "reload openresty" + - "info alb component sysapps" - name: "sysapps | mkdir /var/log/sysapp/[app_uid]" file: diff --git a/tasks/ufw/install-ufw.yml b/tasks/ufw/install-ufw.yml index 6d5e19b..5c803b6 100644 --- a/tasks/ufw/install-ufw.yml +++ b/tasks/ufw/install-ufw.yml @@ -6,3 +6,5 @@ state: present when: - "alb_manange_ufw_install is defined and alb_manange_ufw_install|bool" + notify: + - "info alb component ufw" diff --git a/tasks/ufw/rules.yml b/tasks/ufw/rules.yml index 930b815..e238899 100644 --- a/tasks/ufw/rules.yml +++ b/tasks/ufw/rules.yml @@ -109,5 +109,7 @@ loop: "{{ ufw_rules }}" loop_control: extended: yes - notify: reload ufw + notify: + - "reload ufw" + - "info alb component ufw" when: "ufw_rules is defined" \ No newline at end of file diff --git a/tests/test_alb-standard-node.py b/tests/test_alb-standard-node.py index 7e1a229..1b70477 100644 --- a/tests/test_alb-standard-node.py +++ b/tests/test_alb-standard-node.py @@ -1,36 +1,45 @@ # FILE: /opt/alb/bin/tests/test_alb-standard-node.py -def test_haproxy_is_installed(host): - assert host.package("haproxy").is_installed +def test_required_haproxy_is_installed(host): + assert (not host.file("/opt/alb/info/haproxy").exists) \ + or host.package("haproxy").is_installed -def test_haproxy_is_enabled(host): - assert host.service("haproxy").is_enabled +def test_required_haproxy_is_enabled(host): + assert (not host.file("/opt/alb/info/haproxy").exists) \ + or host.service("haproxy").is_enabled -def test_haproxy_is_running(host): - assert host.service("haproxy").is_running +def test_required_haproxy_is_running(host): + assert (not host.file("/opt/alb/info/haproxy").exists) \ + or host.service("haproxy").is_running -def test_openresty_is_installed(host): - assert host.package("openresty").is_installed +def test_required_openresty_is_installed(host): + assert (not host.file("/opt/alb/info/openresty").exists) \ + or host.package("openresty").is_installed -def test_openresty_is_enabled(host): - assert host.service("openresty").is_enabled +def test_required_openresty_is_enabled(host): + assert (not host.file("/opt/alb/info/openresty").exists) \ + or host.service("openresty").is_enabled -def test_openresty_is_running(host): - assert host.service("openresty").is_running +def test_required_openresty_is_running(host): + assert (not host.file("/opt/alb/info/openresty").exists) \ + or host.service("openresty").is_running -def test_ufw_is_installed(host): - assert host.package("ufw").is_installed +def test_required_ufw_is_installed(host): + assert (not host.file("/opt/alb/info/ufw").exists) \ + or host.package("ufw").is_installed -def test_ufw_is_enabled(host): - assert host.service("ufw").is_enabled +def test_required_ufw_is_enabled(host): + assert (not host.file("/opt/alb/info/ufw").exists) \ + or host.service("ufw").is_enabled -def test_ufw_is_running(host): - assert host.service("ufw").is_running \ No newline at end of file +def test_required_ufw_is_running(host): + assert (not host.file("/opt/alb/info/ufw").exists) \ + or host.service("ufw").is_running