# 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}}"
    dump_file1="{{tmp_dir}}/{{file2}}"
    dump_file2="{{tmp_dir}}/{{file3}}"

- name: state dump/import - create database
  mysql_db:
    name: '{{ db_name }}'
    state: present
    login_unix_socket: '{{ mysql_socket }}'

- name: create database
  mysql_db:
    name: '{{ db_name2 }}'
    state: present
    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:
    name: "{{ db_name }}"
    state: dump
    target: "{{ db_file_name }}"
    ignore_tables:
        - "{{ db_name }}.department"
    login_unix_socket: '{{ mysql_socket }}'
  register: result

- name: assert successful completion of dump operation
  assert:
    that:
       - "result.changed == true"

- 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 }}'
  register: dump_result1

- name: assert successful completion of dump operation (with multiple databases in comma separated form)
  assert:
    that:
       - "dump_result1.changed == true"

- 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:
    name: '{{ db_name }}'
    state: import
    target: '{{ db_file_name }}'
    login_unix_socket: '{{ mysql_socket }}'
  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 }}'
  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"

- 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 dump file1
  file: name={{ dump_file1 }}  state=absent

- name: remove dump file2
  file: name={{ dump_file2 }}  state=absent
