Switch tests to use Molecule.

This commit is contained in:
Jeff Geerling 2018-09-30 20:23:32 -05:00
parent 66e8af0adf
commit bf0fe8d531
15 changed files with 130 additions and 92 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.retry *.retry
tests/test.sh */__pycache__
*.pyc

View File

@ -1,48 +1,32 @@
--- ---
language: python
services: docker services: docker
env: env:
- distro: centos7 global:
postgresql_bin_dir: /usr/bin - ROLE_NAME: postgresql
postgresql_data_dir: /var/lib/pgsql/data matrix:
- distro: centos6 - MOLECULE_DISTRO: centos7
postgresql_bin_dir: /usr/bin MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd
postgresql_data_dir: /var/lib/pgsql/data - MOLECULE_DISTRO: ubuntu1804
- distro: ubuntu1804 - MOLECULE_DISTRO: ubuntu1604
postgresql_bin_dir: /usr/lib/postgresql/10/bin - MOLECULE_DISTRO: ubuntu1404
postgresql_data_dir: /var/lib/postgresql/10/main - MOLECULE_DISTRO: debian9
- distro: ubuntu1604 - MOLECULE_DISTRO: debian8
postgresql_bin_dir: /usr/lib/postgresql/9.5/bin
postgresql_data_dir: /var/lib/postgresql/9.5/main install:
- distro: ubuntu1404 # Install test dependencies.
postgresql_bin_dir: /usr/lib/postgresql/9.3/bin - pip install molecule docker
postgresql_data_dir: /var/lib/postgresql/9.3/main
- distro: debian8 before_script:
postgresql_bin_dir: /usr/lib/postgresql/9.4/bin # Use actual Ansible Galaxy role name for the project directory.
postgresql_data_dir: /var/lib/postgresql/9.4/main - cd ../
- distro: debian9 - mv ansible-role-$ROLE_NAME geerlingguy.$ROLE_NAME
postgresql_bin_dir: /usr/lib/postgresql/9.6/bin - cd geerlingguy.$ROLE_NAME
postgresql_data_dir: /var/lib/postgresql/9.6/main
script: script:
# Configure test script so we can run extra tests after playbook is run.
- export container_id=$(date +%s)
- export cleanup=false
# Download test shim.
- wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/
- chmod +x ${PWD}/tests/test.sh
# Run tests. # Run tests.
- ${PWD}/tests/test.sh - molecule test
# Check PostgreSQL status.
- 'docker exec -u postgres ${container_id} ${postgresql_bin_dir}/pg_ctl -D ${postgresql_data_dir} status'
after_failure:
# Check what happened on systemd systems.
- 'docker exec --tty ${container_id} env TERM=xterm systemctl -l status postgresql.service'
- 'docker exec --tty ${container_id} env TERM=xterm journalctl -xe --no-pager'
notifications: notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/ webhooks: https://galaxy.ansible.com/api/v1/notifications/

View File

@ -39,6 +39,11 @@ The user and group under which PostgreSQL will run.
The directories (usually one, but can be multiple) where PostgreSQL's socket will be created. The directories (usually one, but can be multiple) where PostgreSQL's socket will be created.
postgresql_service_state: started
postgresql_service_enabled: true
Control the state of the postgresql service and whether it should start at boot time.
postgresql_global_config_options: postgresql_global_config_options:
- option: unix_socket_directories - option: unix_socket_directories
value: '{{ postgresql_unix_socket_directories | join(",") }}' value: '{{ postgresql_unix_socket_directories | join(",") }}'

View File

@ -13,6 +13,9 @@ postgresql_group: postgres
postgresql_unix_socket_directories: postgresql_unix_socket_directories:
- /var/run/postgresql - /var/run/postgresql
postgresql_service_state: started
postgresql_service_enabled: true
# Global configuration options that will be set in postgresql.conf. # Global configuration options that will be set in postgresql.conf.
postgresql_global_config_options: postgresql_global_config_options:
- option: unix_socket_directories - option: unix_socket_directories
@ -21,10 +24,10 @@ postgresql_global_config_options:
# Host based authentication (hba) entries to be added to the pg_hba.conf. This # Host based authentication (hba) entries to be added to the pg_hba.conf. This
# variable's defaults reflect the defaults that come with a fresh installation. # variable's defaults reflect the defaults that come with a fresh installation.
postgresql_hba_entries: postgresql_hba_entries:
- { type: local, database: all, user: postgres, auth_method: peer } - {type: local, database: all, user: postgres, auth_method: peer}
- { type: local, database: all, user: all, auth_method: peer } - {type: local, database: all, user: all, auth_method: peer}
- { type: host, database: all, user: all, address: '127.0.0.1/32', auth_method: md5 } - {type: host, database: all, user: all, address: '127.0.0.1/32', auth_method: md5}
- { type: host, database: all, user: all, address: '::1/128', auth_method: md5 } - {type: host, database: all, user: all, address: '::1/128', auth_method: md5}
# Debian only. Used to generate the locales used by PostgreSQL databases. # Debian only. Used to generate the locales used by PostgreSQL databases.
postgresql_locales: postgresql_locales:
@ -32,30 +35,30 @@ postgresql_locales:
# Databases to ensure exist. # Databases to ensure exist.
postgresql_databases: [] postgresql_databases: []
# - name: exampledb # required; the rest are optional # - name: exampledb # required; the rest are optional
# lc_collate: # defaults to 'en_US.UTF-8' # lc_collate: # defaults to 'en_US.UTF-8'
# lc_ctype: # defaults to 'en_US.UTF-8' # lc_ctype: # defaults to 'en_US.UTF-8'
# encoding: # defaults to 'UTF-8' # encoding: # defaults to 'UTF-8'
# template: # defaults to 'template0' # template: # defaults to 'template0'
# login_host: # defaults to 'localhost' # login_host: # defaults to 'localhost'
# login_password: # defaults to not set # login_password: # defaults to not set
# login_user: # defaults to '{{ postgresql_user }}' # login_user: # defaults to '{{ postgresql_user }}'
# login_unix_socket: # defaults to 1st of postgresql_unix_socket_directories # login_unix_socket: # defaults to 1st of postgresql_unix_socket_directories
# port: # defaults to not set # port: # defaults to not set
# owner: # defaults to postgresql_user # owner: # defaults to postgresql_user
# state: # defaults to 'present' # state: # defaults to 'present'
# Users to ensure exist. # Users to ensure exist.
postgresql_users: [] postgresql_users: []
# - name: jdoe #required; the rest are optional # - name: jdoe #required; the rest are optional
# password: # defaults to not set # password: # defaults to not set
# encrypted: # defaults to not set # encrypted: # defaults to not set
# priv: # defaults to not set # priv: # defaults to not set
# role_attr_flags: # defaults to not set # role_attr_flags: # defaults to not set
# db: # defaults to not set # db: # defaults to not set
# login_host: # defaults to 'localhost' # login_host: # defaults to 'localhost'
# login_password: # defaults to not set # login_password: # defaults to not set
# login_user: # defaults to '{{ postgresql_user }}' # login_user: # defaults to '{{ postgresql_user }}'
# login_unix_socket: # defaults to 1st of postgresql_unix_socket_directories # login_unix_socket: # defaults to 1st of postgresql_unix_socket_directories
# port: # defaults to not set # port: # defaults to not set
# state: # defaults to 'present' # state: # defaults to 'present'

View File

@ -10,14 +10,14 @@ galaxy_info:
platforms: platforms:
- name: EL - name: EL
versions: versions:
- 6 - 6
- 7 - 7
- name: Ubuntu - name: Ubuntu
versions: versions:
- all - all
- name: Debian - name: Debian
versions: versions:
- all - all
galaxy_tags: galaxy_tags:
- database - database
- postgresql - postgresql

View File

@ -0,0 +1,27 @@
---
dependency:
name: galaxy
driver:
name: docker
lint:
name: yamllint
options:
config-file: molecule/default/yaml-lint.yml
platforms:
- name: instance
image: geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible
command: ${MOLECULE_DOCKER_COMMAND:-"sleep infinity"}
privileged: true
pre_build_image: true
provisioner:
name: ansible
lint:
name: ansible-lint
playbooks:
converge: ${MOLECULE_PLAYBOOK:-playbook.yml}
scenario:
name: default
verifier:
name: testinfra
lint:
name: flake8

View File

@ -1,5 +1,7 @@
--- ---
- hosts: all - name: Converge
hosts: all
become: true
vars: vars:
postgresql_databases: postgresql_databases:
@ -9,7 +11,7 @@
pre_tasks: pre_tasks:
- name: Update apt cache. - name: Update apt cache.
apt: update_cache=yes cache_valid_time=600 apt: update_cache=true cache_valid_time=600
when: ansible_os_family == 'Debian' when: ansible_os_family == 'Debian'
- name: Set custom variables for old CentOS 6 PostgreSQL install. - name: Set custom variables for old CentOS 6 PostgreSQL install.
@ -18,7 +20,14 @@
postgresql_global_config_options: postgresql_global_config_options:
- option: unix_socket_directory - option: unix_socket_directory
value: '{{ postgresql_unix_socket_directories[0] }}' value: '{{ postgresql_unix_socket_directories[0] }}'
when: ansible_os_family == 'RedHat' and ansible_distribution_version.split('.')[0] == '6' when:
- ansible_os_family == 'RedHat'
- ansible_distribution_version.split('.')[0] == '6'
roles: roles:
- role_under_test - role: geerlingguy.postgresql
post_tasks:
- name: Verify postgres is running.
command: "{{ postgresql_bin_path }}/pg_ctl -D {{ postgresql_data_dir }} status"
changed_when: false

View File

@ -0,0 +1,14 @@
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
def test_hosts_file(host):
f = host.file('/etc/hosts')
assert f.exists
assert f.user == 'root'
assert f.group == 'root'

View File

@ -0,0 +1,6 @@
---
extends: default
rules:
line-length:
max: 120
level: warning

View File

@ -14,7 +14,7 @@
owner: "{{ item.owner | default(postgresql_user) }}" owner: "{{ item.owner | default(postgresql_user) }}"
state: "{{ item.state | default('present') }}" state: "{{ item.state | default('present') }}"
with_items: "{{ postgresql_databases }}" with_items: "{{ postgresql_databases }}"
become: yes become: true
become_user: "{{ postgresql_user }}" become_user: "{{ postgresql_user }}"
# See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509 # See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509
vars: vars:

View File

@ -22,7 +22,7 @@
- name: Ensure PostgreSQL database is initialized. - name: Ensure PostgreSQL database is initialized.
command: "{{ postgresql_bin_path }}/initdb -D {{ postgresql_data_dir }}" command: "{{ postgresql_bin_path }}/initdb -D {{ postgresql_data_dir }}"
when: not pgdata_dir_version.stat.exists when: not pgdata_dir_version.stat.exists
become: yes become: true
become_user: "{{ postgresql_user }}" become_user: "{{ postgresql_user }}"
# See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509 # See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509
vars: vars:

View File

@ -15,8 +15,8 @@
- name: Ensure PostgreSQL is started and enabled on boot. - name: Ensure PostgreSQL is started and enabled on boot.
service: service:
name: "{{ postgresql_daemon }}" name: "{{ postgresql_daemon }}"
state: started state: "{{ postgresql_service_state }}"
enabled: yes enabled: "{{ postgresql_service_enabled }}"
# Configure PostgreSQL. # Configure PostgreSQL.
- import_tasks: databases.yml - import_tasks: databases.yml

View File

@ -15,7 +15,7 @@
state: "{{ item.state | default('present') }}" state: "{{ item.state | default('present') }}"
with_items: "{{ postgresql_users }}" with_items: "{{ postgresql_users }}"
no_log: true no_log: true
become: yes become: true
become_user: "{{ postgresql_user }}" become_user: "{{ postgresql_user }}"
# See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509 # See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509
vars: vars:

View File

@ -1,11 +0,0 @@
# Ansible Role tests
To run the test playbook(s) in this directory:
1. Install and start Docker.
1. Download the test shim (see .travis.yml file for the URL) into `tests/test.sh`:
- `wget -O tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/`
1. Make the test shim executable: `chmod +x tests/test.sh`.
1. Run (from the role root directory) `distro=[distro] playbook=[playbook] ./tests/test.sh`
If you don't want the container to be automatically deleted after the test playbook is run, add the following environment variables: `cleanup=false container_id=$(date +%s)`

View File

@ -7,4 +7,4 @@ __postgresql_daemon: postgresql
__postgresql_packages: __postgresql_packages:
- postgresql - postgresql
- postgresql-contrib - postgresql-contrib
- libpq-dev - libpq-dev