ara/tests/with_postgresql.yaml

107 lines
4.1 KiB
YAML

# Copyright (c) 2019 Red Hat, Inc.
#
# This file is part of ARA Records Ansible.
#
# ARA is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ARA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
- name: Test the ARA API with postgresql
hosts: all
gather_facts: yes
vars:
ara_tests_cleanup: true
ara_api_root_dir: "{{ ansible_user_dir }}/.ara-tests"
ara_api_secret_key: testing
ara_api_debug: true
ara_api_log_level: DEBUG
ara_api_database_engine: django.db.backends.postgresql
ara_api_database_name: ara
ara_api_database_user: ara
ara_api_database_password: password
ara_api_database_host: 127.0.0.1
ara_api_database_port: 5432
pre_tasks:
# TODO: This fails if the docker python library isn't installed but we can
# recover by running the command manually. The Ansible provided by Zuul
# does not have the module installed.
- name: Start a postgresql container
docker_container:
name: ara_tests_postgresql
image: postgres:10
state: started
ports:
- "{{ ara_api_database_port }}:{{ ara_api_database_port }}"
env:
POSTGRES_DB: "{{ ara_api_database_name }}"
POSTGRES_USER: "{{ ara_api_database_user }}"
POSTGRES_PASSWORD: "{{ ara_api_database_password }}"
ignore_errors: yes
register: _docker_run
- name: Start a postgresql container without docker python library
command: |
docker run -d \
--name ara_tests_postgresql \
-p {{ ara_api_database_port }}:{{ ara_api_database_port }} \
-e POSTGRES_DB={{ ara_api_database_name }} \
-e POSTGRES_USER={{ ara_api_database_user }} \
-e POSTGRES_PASSWORD={{ ara_api_database_password }} \
postgres:10
when:
- _docker_run is failed
- "'Failed to import docker or docker-py' in _docker_run.msg or 'Failed to import the required Python library' in _docker_run.msg"
tasks:
- block:
- name: Set up the API with the ara_api role
include_role:
name: ara_api
public: yes
# These are tasks rather than a standalone playbook to give us an easy
# access to all the variables within the same play.
- include_tasks: test_tasks.yaml
- name: Set up postgre password file for passwordless commands
lineinfile:
path: "{{ ansible_user_dir }}/.pgpass"
create: yes
line: "{{ ara_api_database_host }}:{{ ara_api_database_port }}:{{ ara_api_database_name }}:{{ ara_api_database_user }}:{{ ara_api_database_password }}"
mode: 0600
# Dump is suffixed by .txt so we don't need magic mimetypes when
# viewing on the web.
- name: Dump database file
command: |
pg_dump -w \
--host={{ ara_api_database_host }} \
--username={{ ara_api_database_user }} \
--dbname={{ ara_api_database_name }} \
--file={{ ara_api_base_dir }}/pg_dump.sql.txt
always:
- when: ara_tests_cleanup | bool
block:
- name: Clean up the postgresql container
docker_container:
name: ara_tests_postgresql
state: absent
ignore_errors: yes
register: _docker_rm
- name: Remove the postgresql container without the docker python library
command: docker rm -f ara_tests_postgresql
ignore_errors: yes
when:
- _docker_rm is failed
- "'Failed to import docker or docker-py' in _docker_rm.msg"