ara/tests/with_mysql.yaml

104 lines
4.0 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 mysql
hosts: all
gather_facts: yes
vars:
_mysql_container_name: "ara_tests_mariadb"
_mysql_image_name: "mariadb:10"
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.mysql
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: 3306
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 mariadb container
docker_container:
name: "{{ _mysql_container_name }}"
image: "{{ _mysql_image_name }}"
state: started
ports:
- "{{ ara_api_database_port }}:{{ ara_api_database_port }}"
env:
MYSQL_RANDOM_ROOT_PASSWORD: yes
MYSQL_DATABASE: "{{ ara_api_database_name }}"
MYSQL_USER: "{{ ara_api_database_user }}"
MYSQL_PASSWORD: "{{ ara_api_database_password }}"
ignore_errors: yes
register: _docker_run
- name: Start a mysql container without docker python library
command: |
docker run -d \
--name {{ _mysql_container_name }} \
-p {{ ara_api_database_port }}:{{ ara_api_database_port }} \
-e MYSQL_RANDOM_ROOT_PASSWORD=yes \
-e MYSQL_DATABASE={{ ara_api_database_name }} \
-e MYSQL_USER={{ ara_api_database_user }} \
-e MYSQL_PASSWORD={{ ara_api_database_password }} \
{{ _mysql_image_name }}
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
# Dump is suffixed by .txt so we don't need magic mimetypes when
# viewing on the web.
- name: Dump database file
shell: |
mysqldump \
--host={{ ara_api_database_host }} \
--port={{ ara_api_database_port }} \
--user={{ ara_api_database_user }} \
--password={{ ara_api_database_password }} \
{{ ara_api_database_name }} > {{ ara_api_base_dir }}/mysqldump.sql.txt
always:
- when: ara_tests_cleanup | bool
block:
- name: Clean up the mysql container
docker_container:
name: "{{ _mysql_container_name }}"
state: absent
ignore_errors: yes
register: _docker_rm
- name: Remove the mysql container without the docker python library
command: "docker rm -f {{ _mysql_container_name }}"
ignore_errors: yes
when:
- _docker_rm is failed
- "'Failed to import docker or docker-py' in _docker_rm.msg"