Skip to content

Commit 5b65691

Browse files
committed
test: add test for versions
Add tests_versions.yml which will install/remove/confirm all supported versions. Signed-off-by: Rich Megginson <[email protected]>
1 parent d565502 commit 5b65691

File tree

5 files changed

+169
-123
lines changed

5 files changed

+169
-123
lines changed

tests/tasks/install_and_check.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# SPDX-License-Identifier: MIT
2+
---
3+
- name: Test default settings
4+
block:
5+
- name: Run postgresql role
6+
include_role:
7+
name: linux-system-roles.postgresql
8+
public: true
9+
10+
- name: Flush handlers
11+
meta: flush_handlers
12+
13+
- name: Test - postgresql-server running
14+
# noqa command-instead-of-module
15+
command: systemctl is-active postgresql
16+
changed_when: false
17+
18+
- name: Test - postgresql-server is enabled
19+
# noqa command-instead-of-module
20+
command: systemctl is-enabled postgresql
21+
changed_when: false
22+
23+
- name: Test - database is accessible for super user using Unix socket
24+
become: true
25+
become_user: postgres
26+
shell: |
27+
set -euo pipefail
28+
echo '\q' | psql
29+
async: 3 # in case of password prompt we need to fail
30+
changed_when: false
31+
when: __test_check_unix_socket | d(true)
32+
33+
- name: Check - server tuning is used - shared buffers
34+
become: true
35+
become_user: postgres
36+
shell: |
37+
set -euo pipefail
38+
echo "SHOW shared_buffers;" | psql
39+
register: result
40+
changed_when: false
41+
when: __test_check_unix_socket | d(true)
42+
43+
- name: Test - server tuning is used - shared buffers
44+
assert:
45+
that: >
46+
(ansible_memory_mb.real.total/4) | int | abs | string
47+
in result.stdout
48+
when: __test_check_unix_socket | d(true)
49+
50+
- name: Check - server tuning is used - effective cache size
51+
become: true
52+
become_user: postgres
53+
shell: |
54+
set -euo pipefail
55+
echo "SHOW effective_cache_size;" | psql
56+
register: result
57+
changed_when: false
58+
when: __test_check_unix_socket | d(true)
59+
60+
- name: Test - server tuning is used - effective cache size
61+
assert:
62+
that: >
63+
(ansible_memory_mb.real.total/2) | int | abs | string
64+
in result.stdout
65+
when: __test_check_unix_socket | d(true)
66+
67+
- name: Check postgresql version matches postgresql_version
68+
command: postgres --version
69+
when: __test_check_version | d(false)
70+
register: __version
71+
changed_when: false
72+
failed_when: not __version.stdout is
73+
search(" " ~ postgresql_version ~ "[^0-9]")
74+
75+
always:
76+
- name: Clean up
77+
include_tasks: clean_instance.yml
78+
when: __test_clean_instance | d(true)

tests/tests_certificate.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
postgresql_password: redhat
88
block:
99
- name: Deploy postgresql
10-
include_role:
11-
name: linux-system-roles.postgresql
12-
public: true
10+
include_tasks: tasks/install_and_check.yml
1311
vars:
12+
__test_clean_instance: false
13+
__test_check_unix_socket: false
1414
postgresql_certificates:
1515
- name: test_crt
1616
dns: www.example.com
@@ -23,9 +23,6 @@
2323
auth_method: md5
2424
address: '127.0.0.1/32'
2525

26-
- name: Flush handlers
27-
meta: flush_handlers
28-
2926
- name: Gather output of psql
3027
environment:
3128
PGPASSWORD: "{{ postgresql_password }}"
@@ -37,7 +34,7 @@
3734

3835
- name: Check output of psql
3936
assert:
40-
that: >
37+
that: >-
4138
"SSL connection" in result.stdout
4239
always:
4340
- name: Stop tracking certificate

tests/tests_default.yml

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,6 @@
33
- name: Ensure that the role runs with default parameters
44
hosts: all
55
gather_facts: false
6-
76
tasks:
87
- name: Test default settings
9-
block:
10-
- name: Run postgresql role
11-
include_role:
12-
name: linux-system-roles.postgresql
13-
public: true
14-
15-
- name: Flush handlers
16-
meta: flush_handlers
17-
18-
- name: Test - postgresql-server running
19-
# noqa command-instead-of-module
20-
command: systemctl is-active postgresql
21-
changed_when: false
22-
23-
- name: Test - postgresql-server is enabled
24-
# noqa command-instead-of-module
25-
command: systemctl is-enabled postgresql
26-
changed_when: false
27-
28-
- name: Test - database is accessible for super user using Unix socket
29-
become: true
30-
become_user: postgres
31-
shell: |
32-
set -euo pipefail
33-
echo '\q' | psql
34-
async: 3 # in case of password prompt we need to fail
35-
changed_when: false
36-
37-
- name: Check - server tuning is used - shared buffers
38-
become: true
39-
become_user: postgres
40-
shell: |
41-
set -euo pipefail
42-
echo "SHOW shared_buffers;" | psql
43-
register: result
44-
changed_when: false
45-
46-
- name: Test - server tuning is used - shared buffers
47-
assert:
48-
that: >
49-
(ansible_memory_mb.real.total/4) | int | abs | string
50-
in result.stdout
51-
52-
- name: Check - server tuning is used - effective cache size
53-
become: true
54-
become_user: postgres
55-
shell: |
56-
set -euo pipefail
57-
echo "SHOW effective_cache_size;" | psql
58-
register: result
59-
changed_when: false
60-
61-
- name: Test - server tuning is used - effective cache size
62-
assert:
63-
that: >
64-
(ansible_memory_mb.real.total/2) | int | abs | string
65-
in result.stdout
66-
67-
always:
68-
- name: Clean up
69-
include_tasks: tasks/clean_instance.yml
70-
tags: tests::cleanup
8+
include_tasks: tasks/install_and_check.yml

tests/tests_include_vars_from_parent.yml

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,60 @@
33
hosts: all
44
gather_facts: true
55
tasks:
6-
- name: Create var file in caller that can override the one in called role
7-
delegate_to: localhost
8-
copy:
9-
# usually the fake file will cause the called role to crash of
10-
# overriding happens, but if not, set a variable that will
11-
# allow to detect the bug
12-
content: "__caller_override: true"
13-
# XXX ugly, self-modifying code - changes the "caller" role on
14-
# the controller
15-
dest: "{{ playbook_dir }}/roles/caller/vars/{{ item }}.yml"
16-
mode: preserve
17-
loop: "{{ varfiles | unique }}"
18-
# In case the playbook is executed against multiple hosts, use
19-
# only the first one. Otherwise the hosts would stomp on each
20-
# other since they are changing files on the controller.
21-
when: inventory_hostname == ansible_play_hosts_all[0]
22-
vars:
23-
# change to hostvars['localhost']['ansible_facts'] to use the
24-
# information for localhost
25-
facts: "{{ ansible_facts }}"
26-
versions:
27-
- "{{ facts['distribution_version'] }}"
28-
- "{{ facts['distribution_major_version'] }}"
29-
separators: ["-", "_"]
30-
# create all variants like CentOS, CentOS_8.1, CentOS-8.1,
31-
# CentOS-8, CentOS-8.1
32-
# more formally:
33-
# {{ ansible_distribution }}-{{ ansible_distribution_version }}
34-
# {{ ansible_distribution }}-{{ ansible_distribution_major_version }}
35-
# {{ ansible_distribution }}
36-
# {{ ansible_os_family }}
37-
# and the same for _ as separator.
38-
varfiles: "{{ [facts['distribution']] | product(separators) |
39-
map('join') | product(versions) | map('join') | list +
40-
[facts['distribution'], facts['os_family']] }}"
41-
register: __varfiles_created
6+
- name: Run test
7+
block:
8+
- name: Create var file in caller that can override the one in called role
9+
delegate_to: localhost
10+
copy:
11+
# usually the fake file will cause the called role to crash of
12+
# overriding happens, but if not, set a variable that will
13+
# allow to detect the bug
14+
content: "__caller_override: true"
15+
# XXX ugly, self-modifying code - changes the "caller" role on
16+
# the controller
17+
dest: "{{ playbook_dir }}/roles/caller/vars/{{ item }}.yml"
18+
mode: preserve
19+
loop: "{{ varfiles | unique }}"
20+
# In case the playbook is executed against multiple hosts, use
21+
# only the first one. Otherwise the hosts would stomp on each
22+
# other since they are changing files on the controller.
23+
when: inventory_hostname == ansible_play_hosts_all[0]
24+
vars:
25+
# change to hostvars['localhost']['ansible_facts'] to use the
26+
# information for localhost
27+
facts: "{{ ansible_facts }}"
28+
versions:
29+
- "{{ facts['distribution_version'] }}"
30+
- "{{ facts['distribution_major_version'] }}"
31+
separators: ["-", "_"]
32+
# create all variants like CentOS, CentOS_8.1, CentOS-8.1,
33+
# CentOS-8, CentOS-8.1
34+
# more formally:
35+
# {{ ansible_distribution }}-{{ ansible_distribution_version }}
36+
# {{ ansible_distribution }}-{{ ansible_distribution_major_version }}
37+
# {{ ansible_distribution }}
38+
# {{ ansible_os_family }}
39+
# and the same for _ as separator.
40+
varfiles: "{{ [facts['distribution']] | product(separators) |
41+
map('join') | product(versions) | map('join') | list +
42+
[facts['distribution'], facts['os_family']] }}"
43+
register: __varfiles_created
4244

43-
- name: Import role
44-
import_role:
45-
name: caller
46-
vars:
47-
roletoinclude: linux-system-roles.postgresql
45+
- name: Import role
46+
import_role:
47+
name: caller
48+
vars:
49+
roletoinclude: linux-system-roles.postgresql
50+
always:
51+
- name: Clean up db instance
52+
include_tasks: tasks/clean_instance.yml
53+
tags: tests::cleanup
4854

49-
- name: Clean up db instance
50-
include_tasks: tasks/clean_instance.yml
51-
tags: tests::cleanup
52-
53-
- name: Cleanup
54-
file:
55-
path: "{{ item.dest }}"
56-
state: absent
57-
loop: "{{ __varfiles_created.results }}"
58-
delegate_to: localhost
59-
when: inventory_hostname == ansible_play_hosts_all[0]
60-
tags: tests::cleanup
55+
- name: Cleanup files created by test
56+
file:
57+
path: "{{ item.dest }}"
58+
state: absent
59+
loop: "{{ __varfiles_created.results }}"
60+
delegate_to: localhost
61+
when: inventory_hostname == ansible_play_hosts_all[0]
62+
tags: tests::cleanup

tests/tests_versions.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# SPDX-License-Identifier: MIT
2+
---
3+
- name: Test installing and uninstalling all supported versions
4+
hosts: all
5+
tasks:
6+
- name: Skip test if distro does not support multiple versions
7+
meta: end_host
8+
when: ansible_facts['distribution'] not in ['CentOS', 'RedHat'] or
9+
ansible_facts['distribution_major_version'] not in ["8", "9"]
10+
11+
- name: Install and cleanup default version
12+
include_tasks: tasks/install_and_check.yml
13+
vars:
14+
__test_check_version: true
15+
16+
- name: Save default postgresql_version
17+
set_fact:
18+
__default_version: "{{ postgresql_version }}"
19+
20+
- name: Install and cleanup the other supported versions
21+
include_tasks: tasks/install_and_check.yml
22+
loop: "{{ __versions }}"
23+
when: item != __default_version # we already installed it
24+
vars:
25+
__test_check_version: true
26+
postgresql_version: "{{ item }}"
27+
__versions: "{{ __postgresql_versions_el8
28+
if ansible_facts['distribution_major_version'] == '8'
29+
else __postgresql_versions_el9
30+
if ansible_facts['distribution_major_version'] == '9'
31+
else [] }}"

0 commit comments

Comments
 (0)