Skip to content

Commit

Permalink
test: add test for versions
Browse files Browse the repository at this point in the history
Add tests_versions.yml which will install/remove/confirm all supported
versions.

Signed-off-by: Rich Megginson <[email protected]>
  • Loading branch information
richm committed Jan 22, 2024
1 parent d565502 commit f51fc38
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 123 deletions.
78 changes: 78 additions & 0 deletions tests/tasks/install_and_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# SPDX-License-Identifier: MIT
---
- name: Test default settings
block:
- name: Run postgresql role
include_role:
name: linux-system-roles.postgresql
public: true

- name: Flush handlers
meta: flush_handlers

- name: Test - postgresql-server running
# noqa command-instead-of-module
command: systemctl is-active postgresql
changed_when: false

- name: Test - postgresql-server is enabled
# noqa command-instead-of-module
command: systemctl is-enabled postgresql
changed_when: false

- name: Test - database is accessible for super user using Unix socket
become: true
become_user: postgres
shell: |
set -euo pipefail
echo '\q' | psql
async: 3 # in case of password prompt we need to fail
changed_when: false
when: __test_check_unix_socket | d(true)

- name: Check - server tuning is used - shared buffers
become: true
become_user: postgres
shell: |
set -euo pipefail
echo "SHOW shared_buffers;" | psql
register: result
changed_when: false
when: __test_check_unix_socket | d(true)

- name: Test - server tuning is used - shared buffers
assert:
that: >
(ansible_memory_mb.real.total/4) | int | abs | string
in result.stdout
when: __test_check_unix_socket | d(true)

- name: Check - server tuning is used - effective cache size
become: true
become_user: postgres
shell: |
set -euo pipefail
echo "SHOW effective_cache_size;" | psql
register: result
changed_when: false
when: __test_check_unix_socket | d(true)

- name: Test - server tuning is used - effective cache size
assert:
that: >
(ansible_memory_mb.real.total/2) | int | abs | string
in result.stdout
when: __test_check_unix_socket | d(true)

- name: Check postgresql version matches postgresql_version
command: postgres --version
when: __test_check_version | d(false)
register: __version
changed_when: false
failed_when: not __version.stdout is
search(" " ~ postgresql_version ~ "[^0-9]")

always:
- name: Clean up
include_tasks: clean_instance.yml
when: __test_clean_instance | d(true)
11 changes: 4 additions & 7 deletions tests/tests_certificate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
postgresql_password: redhat
block:
- name: Deploy postgresql
include_role:
name: linux-system-roles.postgresql
public: true
include_tasks: tasks/install_and_check.yml
vars:
__test_clean_instance: false
__test_check_unix_socket: false
postgresql_certificates:
- name: test_crt
dns: www.example.com
Expand All @@ -23,9 +23,6 @@
auth_method: md5
address: '127.0.0.1/32'

- name: Flush handlers
meta: flush_handlers

- name: Gather output of psql
environment:
PGPASSWORD: "{{ postgresql_password }}"
Expand All @@ -37,7 +34,7 @@

- name: Check output of psql
assert:
that: >
that: >-
"SSL connection" in result.stdout
always:
- name: Stop tracking certificate
Expand Down
64 changes: 1 addition & 63 deletions tests/tests_default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,6 @@
- name: Ensure that the role runs with default parameters
hosts: all
gather_facts: false

tasks:
- name: Test default settings
block:
- name: Run postgresql role
include_role:
name: linux-system-roles.postgresql
public: true

- name: Flush handlers
meta: flush_handlers

- name: Test - postgresql-server running
# noqa command-instead-of-module
command: systemctl is-active postgresql
changed_when: false

- name: Test - postgresql-server is enabled
# noqa command-instead-of-module
command: systemctl is-enabled postgresql
changed_when: false

- name: Test - database is accessible for super user using Unix socket
become: true
become_user: postgres
shell: |
set -euo pipefail
echo '\q' | psql
async: 3 # in case of password prompt we need to fail
changed_when: false

- name: Check - server tuning is used - shared buffers
become: true
become_user: postgres
shell: |
set -euo pipefail
echo "SHOW shared_buffers;" | psql
register: result
changed_when: false

- name: Test - server tuning is used - shared buffers
assert:
that: >
(ansible_memory_mb.real.total/4) | int | abs | string
in result.stdout
- name: Check - server tuning is used - effective cache size
become: true
become_user: postgres
shell: |
set -euo pipefail
echo "SHOW effective_cache_size;" | psql
register: result
changed_when: false

- name: Test - server tuning is used - effective cache size
assert:
that: >
(ansible_memory_mb.real.total/2) | int | abs | string
in result.stdout
always:
- name: Clean up
include_tasks: tasks/clean_instance.yml
tags: tests::cleanup
include_tasks: tasks/install_and_check.yml
108 changes: 55 additions & 53 deletions tests/tests_include_vars_from_parent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,60 @@
hosts: all
gather_facts: true
tasks:
- name: Create var file in caller that can override the one in called role
delegate_to: localhost
copy:
# usually the fake file will cause the called role to crash of
# overriding happens, but if not, set a variable that will
# allow to detect the bug
content: "__caller_override: true"
# XXX ugly, self-modifying code - changes the "caller" role on
# the controller
dest: "{{ playbook_dir }}/roles/caller/vars/{{ item }}.yml"
mode: preserve
loop: "{{ varfiles | unique }}"
# In case the playbook is executed against multiple hosts, use
# only the first one. Otherwise the hosts would stomp on each
# other since they are changing files on the controller.
when: inventory_hostname == ansible_play_hosts_all[0]
vars:
# change to hostvars['localhost']['ansible_facts'] to use the
# information for localhost
facts: "{{ ansible_facts }}"
versions:
- "{{ facts['distribution_version'] }}"
- "{{ facts['distribution_major_version'] }}"
separators: ["-", "_"]
# create all variants like CentOS, CentOS_8.1, CentOS-8.1,
# CentOS-8, CentOS-8.1
# more formally:
# {{ ansible_distribution }}-{{ ansible_distribution_version }}
# {{ ansible_distribution }}-{{ ansible_distribution_major_version }}
# {{ ansible_distribution }}
# {{ ansible_os_family }}
# and the same for _ as separator.
varfiles: "{{ [facts['distribution']] | product(separators) |
map('join') | product(versions) | map('join') | list +
[facts['distribution'], facts['os_family']] }}"
register: __varfiles_created
- name: Run test
block:
- name: Create var file in caller that can override the one in called role
delegate_to: localhost
copy:
# usually the fake file will cause the called role to crash of
# overriding happens, but if not, set a variable that will
# allow to detect the bug
content: "__caller_override: true"
# XXX ugly, self-modifying code - changes the "caller" role on
# the controller
dest: "{{ playbook_dir }}/roles/caller/vars/{{ item }}.yml"
mode: preserve
loop: "{{ varfiles | unique }}"
# In case the playbook is executed against multiple hosts, use
# only the first one. Otherwise the hosts would stomp on each
# other since they are changing files on the controller.
when: inventory_hostname == ansible_play_hosts_all[0]
vars:
# change to hostvars['localhost']['ansible_facts'] to use the
# information for localhost
facts: "{{ ansible_facts }}"
versions:
- "{{ facts['distribution_version'] }}"
- "{{ facts['distribution_major_version'] }}"
separators: ["-", "_"]
# create all variants like CentOS, CentOS_8.1, CentOS-8.1,
# CentOS-8, CentOS-8.1
# more formally:
# {{ ansible_distribution }}-{{ ansible_distribution_version }}
# {{ ansible_distribution }}-{{ ansible_distribution_major_version }}
# {{ ansible_distribution }}
# {{ ansible_os_family }}
# and the same for _ as separator.
varfiles: "{{ [facts['distribution']] | product(separators) |
map('join') | product(versions) | map('join') | list +
[facts['distribution'], facts['os_family']] }}"
register: __varfiles_created

- name: Import role
import_role:
name: caller
vars:
roletoinclude: linux-system-roles.postgresql
- name: Import role
import_role:
name: caller
vars:
roletoinclude: linux-system-roles.postgresql
always:
- name: Clean up db instance
include_tasks: tasks/clean_instance.yml
tags: tests::cleanup

- name: Clean up db instance
include_tasks: tasks/clean_instance.yml
tags: tests::cleanup

- name: Cleanup
file:
path: "{{ item.dest }}"
state: absent
loop: "{{ __varfiles_created.results }}"
delegate_to: localhost
when: inventory_hostname == ansible_play_hosts_all[0]
tags: tests::cleanup
- name: Cleanup files created by test
file:
path: "{{ item.dest }}"
state: absent
loop: "{{ __varfiles_created.results }}"
delegate_to: localhost
when: inventory_hostname == ansible_play_hosts_all[0]
tags: tests::cleanup
31 changes: 31 additions & 0 deletions tests/tests_versions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SPDX-License-Identifier: MIT
---
- name: Test installing and uninstalling all supported versions
hosts: all
tasks:
- name: Skip test if distro does not support multiple versions
meta: end_host
when: ansible_facts['distribution'] not in ['CentOS', 'RedHat'] or
ansible_facts['distribution_major_version'] not in ["8", "9"]

- name: Install and cleanup default version
include_tasks: tasks/install_and_check.yml
vars:
__test_check_version: true

- name: Save default postgresql_version
set_fact:
__default_version: "{{ postgresql_version }}"

- name: Install and cleanup the other supported versions
include_tasks: tasks/install_and_check.yml
loop: "{{ __versions }}"
when: item != __default_version # we already installed it
vars:
__test_check_version: true
postgresql_version: "{{ item }}"
__versions: "{{ __postgresql_versions_el8
if ansible_facts['distribution_major_version'] == '8'
else __postgresql_versions_el9
if ansible_facts['distribution_major_version'] == '9'
else [] }}"

0 comments on commit f51fc38

Please sign in to comment.