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
tests/test.sh
*/__pycache__
*.pyc

View File

@ -1,48 +1,32 @@
---
language: python
services: docker
env:
- distro: centos7
postgresql_bin_dir: /usr/bin
postgresql_data_dir: /var/lib/pgsql/data
- distro: centos6
postgresql_bin_dir: /usr/bin
postgresql_data_dir: /var/lib/pgsql/data
- distro: ubuntu1804
postgresql_bin_dir: /usr/lib/postgresql/10/bin
postgresql_data_dir: /var/lib/postgresql/10/main
- distro: ubuntu1604
postgresql_bin_dir: /usr/lib/postgresql/9.5/bin
postgresql_data_dir: /var/lib/postgresql/9.5/main
- distro: ubuntu1404
postgresql_bin_dir: /usr/lib/postgresql/9.3/bin
postgresql_data_dir: /var/lib/postgresql/9.3/main
- distro: debian8
postgresql_bin_dir: /usr/lib/postgresql/9.4/bin
postgresql_data_dir: /var/lib/postgresql/9.4/main
- distro: debian9
postgresql_bin_dir: /usr/lib/postgresql/9.6/bin
postgresql_data_dir: /var/lib/postgresql/9.6/main
global:
- ROLE_NAME: postgresql
matrix:
- MOLECULE_DISTRO: centos7
MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd
- MOLECULE_DISTRO: ubuntu1804
- MOLECULE_DISTRO: ubuntu1604
- MOLECULE_DISTRO: ubuntu1404
- MOLECULE_DISTRO: debian9
- MOLECULE_DISTRO: debian8
install:
# Install test dependencies.
- pip install molecule docker
before_script:
# Use actual Ansible Galaxy role name for the project directory.
- cd ../
- mv ansible-role-$ROLE_NAME geerlingguy.$ROLE_NAME
- cd geerlingguy.$ROLE_NAME
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.
- ${PWD}/tests/test.sh
# 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'
- molecule test
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.
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:
- option: unix_socket_directories
value: '{{ postgresql_unix_socket_directories | join(",") }}'

View File

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

View File

@ -10,14 +10,14 @@ galaxy_info:
platforms:
- name: EL
versions:
- 6
- 7
- 6
- 7
- name: Ubuntu
versions:
- all
- all
- name: Debian
versions:
- all
- all
galaxy_tags:
- database
- 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:
postgresql_databases:
@ -9,7 +11,7 @@
pre_tasks:
- 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'
- name: Set custom variables for old CentOS 6 PostgreSQL install.
@ -18,7 +20,14 @@
postgresql_global_config_options:
- option: unix_socket_directory
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:
- 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) }}"
state: "{{ item.state | default('present') }}"
with_items: "{{ postgresql_databases }}"
become: yes
become: true
become_user: "{{ postgresql_user }}"
# See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509
vars:

View File

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

View File

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

View File

@ -15,7 +15,7 @@
state: "{{ item.state | default('present') }}"
with_items: "{{ postgresql_users }}"
no_log: true
become: yes
become: true
become_user: "{{ postgresql_user }}"
# See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509
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
- postgresql-contrib
- libpq-dev
- libpq-dev