mirror of
https://github.com/ansible-collections/community.mysql.git
synced 2025-04-06 10:40:36 -07:00
375 lines
11 KiB
YAML
375 lines
11 KiB
YAML
# test code for state dump and import for mysql_db module
|
|
# (c) 2014, Wayne Rosario <wrosario@ansible.com>
|
|
|
|
# This file is part of Ansible
|
|
#
|
|
# Ansible 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.
|
|
#
|
|
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
# ============================================================
|
|
- set_fact:
|
|
db_file_name="{{tmp_dir}}/{{file}}"
|
|
wrong_sql_file="{{tmp_dir}}/wrong.sql"
|
|
dump_file1="{{tmp_dir}}/{{file2}}"
|
|
dump_file2="{{tmp_dir}}/{{file3}}"
|
|
db_user="test"
|
|
db_user_unsafe_password="pass!word"
|
|
config_file="/root/.my.cnf"
|
|
|
|
- name: create custom config file
|
|
shell: 'echo "[client]" > {{ config_file }}'
|
|
|
|
- name: create user for test unsafe_login_password parameter
|
|
mysql_user:
|
|
name: '{{ db_user }}'
|
|
password: '{{ db_user_unsafe_password }}'
|
|
priv: '*.*:ALL'
|
|
state: present
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
|
|
- name: state dump/import - create database
|
|
mysql_db:
|
|
name: '{{ db_name }}'
|
|
state: present
|
|
check_implicit_admin: yes
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
|
|
- name: create database
|
|
mysql_db:
|
|
name: '{{ db_name2 }}'
|
|
state: present
|
|
check_implicit_admin: no
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
|
|
- name: state dump/import - create table department
|
|
command: mysql {{ db_name }} '-e create table department(id int, name varchar(100));'
|
|
|
|
- name: state dump/import - create table employee
|
|
command: mysql {{ db_name }} '-e create table employee(id int, name varchar(100));'
|
|
|
|
- name: state dump/import - insert data into table employee
|
|
command: mysql {{ db_name }} "-e insert into employee value(47,'Joe Smith');"
|
|
|
|
- name: state dump/import - insert data into table department
|
|
command: mysql {{ db_name }} "-e insert into department value(2,'Engineering');"
|
|
|
|
- name: state dump/import - file name should not exist
|
|
file: name={{ db_file_name }} state=absent
|
|
|
|
- name: database dump file1 should not exist
|
|
file: name={{ dump_file1 }} state=absent
|
|
|
|
- name: database dump file2 should not exist
|
|
file: name={{ dump_file2 }} state=absent
|
|
|
|
- name: state dump without department table.
|
|
mysql_db:
|
|
login_user: '{{ db_user }}'
|
|
login_password: '{{ db_user_unsafe_password }}'
|
|
unsafe_login_password: yes
|
|
name: "{{ db_name }}"
|
|
state: dump
|
|
target: "{{ db_file_name }}"
|
|
ignore_tables:
|
|
- "{{ db_name }}.department"
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
force: yes
|
|
master_data: 1
|
|
skip_lock_tables: yes
|
|
dump_extra_args: --skip-triggers
|
|
config_file: '{{ config_file }}'
|
|
restrict_config_file: yes
|
|
check_implicit_admin: no
|
|
register: result
|
|
|
|
- name: assert successful completion of dump operation
|
|
assert:
|
|
that:
|
|
- result is changed
|
|
- result.executed_commands[0] is search("mysqldump --defaults-file={{ config_file }} --user={{ db_user }} --password=\*\*\*\*\*\*\*\* --force --socket={{ mysql_socket }} {{ db_name }} --skip-lock-tables --quick --ignore-table={{ db_name }}.department --master-data=1 --skip-triggers")
|
|
|
|
- name: state dump/import - file name should exist
|
|
file: name={{ db_file_name }} state=file
|
|
|
|
- name: state dump with multiple databases in comma separated form.
|
|
mysql_db:
|
|
name: "{{ db_name }},{{ db_name2 }}"
|
|
state: dump
|
|
target: "{{ dump_file1 }}"
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
check_implicit_admin: yes
|
|
register: dump_result1
|
|
|
|
- name: assert successful completion of dump operation (with multiple databases in comma separated form)
|
|
assert:
|
|
that:
|
|
- dump_result1 is changed
|
|
- dump_result1.executed_commands[0] is search(" --user=root --password=''")
|
|
|
|
- name: state dump - dump file1 should exist
|
|
file: name={{ dump_file1 }} state=file
|
|
|
|
- name: state dump with multiple databases in list form via check_mode
|
|
mysql_db:
|
|
name:
|
|
- "{{ db_name }}"
|
|
- "{{ db_name2 }}"
|
|
state: dump
|
|
target: "{{ dump_file2 }}"
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
register: dump_result
|
|
check_mode: yes
|
|
|
|
- name: assert successful completion of dump operation (with multiple databases in list form) via check mode
|
|
assert:
|
|
that:
|
|
- "dump_result.changed == true"
|
|
|
|
- name: database dump file2 should not exist
|
|
stat:
|
|
path: "{{ dump_file2 }}"
|
|
register: stat_result
|
|
|
|
- name: assert that check_mode does not create dump file for databases
|
|
assert:
|
|
that:
|
|
- stat_result.stat.exists is defined and not stat_result.stat.exists
|
|
|
|
- name: state dump with multiple databases in list form.
|
|
mysql_db:
|
|
name:
|
|
- "{{ db_name }}"
|
|
- "{{ db_name2 }}"
|
|
state: dump
|
|
target: "{{ dump_file2 }}"
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
register: dump_result2
|
|
|
|
- name: assert successful completion of dump operation (with multiple databases in list form)
|
|
assert:
|
|
that:
|
|
- "dump_result2.changed == true"
|
|
|
|
- name: state dump - dump file2 should exist
|
|
file: name={{ dump_file2 }} state=file
|
|
|
|
- name: state dump/import - remove database
|
|
mysql_db:
|
|
name: '{{ db_name }}'
|
|
state: absent
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
|
|
- name: remove database
|
|
mysql_db:
|
|
name: '{{ db_name2 }}'
|
|
state: absent
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
|
|
- name: test state=import to restore the database of type {{ format_type }} (expect changed=true)
|
|
mysql_db:
|
|
login_user: '{{ db_user }}'
|
|
login_password: '{{ db_user_unsafe_password }}'
|
|
unsafe_login_password: yes
|
|
name: '{{ db_name }}'
|
|
state: import
|
|
target: '{{ db_file_name }}'
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
use_shell: yes
|
|
register: result
|
|
|
|
- name: show the tables
|
|
command: mysql {{ db_name }} "-e show tables;"
|
|
register: result
|
|
|
|
- name: assert that the department table is absent.
|
|
assert:
|
|
that:
|
|
- "'department' not in result.stdout"
|
|
|
|
- name: test state=import to restore a database from multiple database dumped file1
|
|
mysql_db:
|
|
name: '{{ db_name2 }}'
|
|
state: import
|
|
target: '{{ dump_file1 }}'
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
use_shell: no
|
|
register: import_result
|
|
|
|
- name: assert output message restored a database from dump file1
|
|
assert: { that: "import_result.changed == true" }
|
|
|
|
- name: remove database
|
|
mysql_db:
|
|
name: '{{ db_name2 }}'
|
|
state: absent
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
|
|
- name: run command to list databases
|
|
command: mysql "-e show databases like 'data%';"
|
|
register: mysql_result
|
|
|
|
- name: assert that db_name2 database does not exist
|
|
assert:
|
|
that:
|
|
- "'{{ db_name2 }}' not in mysql_result.stdout"
|
|
|
|
- name: test state=import to restore a database from dumped file2 (check mode)
|
|
mysql_db:
|
|
name: '{{ db_name2 }}'
|
|
state: import
|
|
target: '{{ dump_file2 }}'
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
register: check_import_result
|
|
check_mode: yes
|
|
|
|
- name: assert output message restored a database from dump file2 (check mode)
|
|
assert: { that: "check_import_result.changed == true" }
|
|
|
|
- name: run command to list databases
|
|
command: mysql "-e show databases like 'data%';"
|
|
register: mysql_result
|
|
|
|
- name: assert that db_name2 database does not exist (check mode)
|
|
assert:
|
|
that:
|
|
- "'{{ db_name2 }}' not in mysql_result.stdout"
|
|
|
|
- name: test state=import to restore a database from multiple database dumped file2
|
|
mysql_db:
|
|
name: '{{ db_name2 }}'
|
|
state: import
|
|
target: '{{ dump_file2 }}'
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
register: import_result2
|
|
|
|
- name: assert output message restored a database from dump file2
|
|
assert:
|
|
that:
|
|
- import_result2.changed == true
|
|
- import_result2.db_list == ['{{ db_name2 }}']
|
|
|
|
- name: run command to list databases
|
|
command: mysql "-e show databases like 'data%';"
|
|
register: mysql_result
|
|
|
|
- name: assert that db_name2 database does exist after import
|
|
assert:
|
|
that:
|
|
- "'{{ db_name2 }}' in mysql_result.stdout"
|
|
|
|
- name: test state=dump to backup the database of type {{ format_type }} (expect changed=true)
|
|
mysql_db:
|
|
name: '{{ db_name }}'
|
|
state: dump
|
|
target: '{{ db_file_name }}'
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
register: result
|
|
|
|
- name: assert output message backup the database
|
|
assert:
|
|
that:
|
|
- "result.changed == true"
|
|
- "result.db =='{{ db_name }}'"
|
|
|
|
- name: assert database was backed up successfully
|
|
command: file {{ db_file_name }}
|
|
register: result
|
|
|
|
- name: assert file format type
|
|
assert: { that: "'{{format_msg_type}}' in result.stdout" }
|
|
|
|
- name: update database table employee
|
|
command: mysql {{ db_name }} "-e update employee set name='John Doe' where id=47;"
|
|
|
|
- name: test state=import to restore the database of type {{ format_type }} (expect changed=true)
|
|
mysql_db:
|
|
name: '{{ db_name }}'
|
|
state: import
|
|
target: '{{ db_file_name }}'
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
register: result
|
|
|
|
- name: assert output message restore the database
|
|
assert: { that: "result.changed == true" }
|
|
|
|
- name: select data from table employee
|
|
command: mysql {{ db_name }} "-e select * from employee;"
|
|
register: result
|
|
|
|
- name: assert data in database is from the restore database
|
|
assert:
|
|
that:
|
|
- "'47' in result.stdout"
|
|
- "'Joe Smith' in result.stdout"
|
|
|
|
##########################
|
|
# Test ``force`` parameter
|
|
##########################
|
|
|
|
- name: create wrong sql file
|
|
shell: echo 'CREATE TABLE hello (id int); CREATE ELBAT ehlo (int id);' >> '{{ wrong_sql_file }}'
|
|
|
|
- name: try to import without force parameter, must fail
|
|
mysql_db:
|
|
name: '{{ db_name }}'
|
|
state: import
|
|
target: '{{ wrong_sql_file }}'
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
force: no
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.failed == true
|
|
|
|
- name: try to import with force parameter
|
|
mysql_db:
|
|
name: '{{ db_name }}'
|
|
state: import
|
|
target: '{{ wrong_sql_file }}'
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
force: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result is changed
|
|
|
|
##########
|
|
# Clean up
|
|
##########
|
|
|
|
- name: remove database name
|
|
mysql_db:
|
|
name: '{{ db_name }}'
|
|
state: absent
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
|
|
- name: remove database
|
|
mysql_db:
|
|
name: '{{ db_name2 }}'
|
|
state: absent
|
|
login_unix_socket: '{{ mysql_socket }}'
|
|
|
|
- name: remove file name
|
|
file: name={{ db_file_name }} state=absent
|
|
|
|
- name: remove file name
|
|
file: name={{ wrong_sql_file }} state=absent
|
|
|
|
- name: remove dump file1
|
|
file: name={{ dump_file1 }} state=absent
|
|
|
|
- name: remove dump file2
|
|
file: name={{ dump_file2 }} state=absent
|