mirror of https://github.com/ansible/ansible.git
postgres_db: add dump and restore support (#20627)
* Feature #2731: added postgres import and dump * Feature #2731: be more permissive of arguments ``` hacking/test-module -m ./ppostgresql_db.py -a "db=example state=dump target=/tmp/out"` ``` failed previously since host, user, and port were required as keywords in the pg_dump / pg_import methods. * Feature #2731: fixed doc string for validate-modules ``` $ ansible-validate-modules database/postgresql/ ``` now passes. * Feature #2731: disable 'password' for dump/restore * Feature #2731: bump added version to 2.3 * Feature #2731: replace db_import with db_restore * Feature #2731: add missing version description * Feature #2731: fix 'state' description * Feature #2731: fix pep8 issues * Feature #2731: put state documentation in a single string * Bump added version from 2.3 to 2.4 * Fix pep8 and pylint errors * Attempt yaml formatting of documentation string * Add integration tests for postgres_db:dump/restore * Update dump/restore logic to support new kw-args Also attempt to support password; integration tests are still failing. * Revert to postgres user for dump/restore Passing PGPASSWORD is not working for subprocesses. For the moment, reverting to the strategy of failing if login_password is set and using `postgres` for all testing of dump/restore. * Various cleanups to have tests passing * Working tests for {sql,tar} x {,bz2,gz,xz} * Use pg_user to support FreeBSD * Revert login_ prefixes and re-enable password support All `login_` keywords are mapped to their non-prefix versions so the previous changes were effectively using `postgres` for all actions. With the proper keywords, PGPASSWORD-passing to the subprocess is now working. * Optionally add password environ_update doesn't handle None values in the dictionary to be added to the environment. Adding check. * Quick fixes * Refactor login arguments after fixes from pchauncey The fixes introduced by pchaunchy pointed to further issues (like no --dbname on PG<=9.2) with the login parameters. This refactors them and adds further tests. Note: this will still not pass integration tests due to a further issue with pg_dump as a non-admin user: pg_restore: [archiver (db)] Error while PROCESSING TOC: pg_restore: [archiver (db)] Error from TOC entry 1925; 0 0 COMMENT EXTENSION plpgsql pg_restore: [archiver (db)] could not execute query: ERROR: must be owner of extension plpgsql * Introduce target_opts for passing limiting dumped/restored schemas The current integration tests (PG version and template DBs) don't permit a regular user (`{{ db_user1 }}`) access to plpgsql causing restores to fail. By adding an option for passing arbitrary args to pg_dump and pg_restore, testing is made easier. This also paves the way for `-j` usage, once the PG version is bumped.pull/26584/head
parent
5c99850232
commit
d5ae6cc585
@ -0,0 +1,113 @@
|
|||||||
|
# test code for state dump and restore for postgresql_db module
|
||||||
|
# copied from mysql_db/tasks/state_dump_import.yml
|
||||||
|
# (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}}"
|
||||||
|
|
||||||
|
- set_fact:
|
||||||
|
admin_str: "psql -U {{ pg_user }}"
|
||||||
|
admin_map:
|
||||||
|
name: "{{ db_name }}"
|
||||||
|
owner: "{{ db_user1 }}"
|
||||||
|
login_user: "{{ pg_user }}"
|
||||||
|
|
||||||
|
- set_fact:
|
||||||
|
user_str: "env PGPASSWORD=password psql -h localhost -U {{ db_user1 }} {{ db_name }}"
|
||||||
|
user_map:
|
||||||
|
name: "{{ db_name }}"
|
||||||
|
target: "{{ db_file_name }}"
|
||||||
|
target_opts: "-n public"
|
||||||
|
owner: "{{ db_user1 }}"
|
||||||
|
login_host: "localhost"
|
||||||
|
login_user: "{{ db_user1 }}"
|
||||||
|
login_password: "password"
|
||||||
|
when: test_fixture == "user"
|
||||||
|
# "-n public" is required to work around pg_restore issues with plpgsql
|
||||||
|
|
||||||
|
- set_fact:
|
||||||
|
user_str: "psql -U {{ pg_user }} {{ db_name }}"
|
||||||
|
user_map:
|
||||||
|
name: "{{ db_name }}"
|
||||||
|
target: "{{ db_file_name }}"
|
||||||
|
owner: "{{ db_user1 }}"
|
||||||
|
login_user: "{{ pg_user }}"
|
||||||
|
when: test_fixture == "admin"
|
||||||
|
|
||||||
|
- set_fact:
|
||||||
|
sql_create: "create table employee(id int, name varchar(100));"
|
||||||
|
sql_insert: "insert into employee values (47,'Joe Smith');"
|
||||||
|
sql_select: "select * from employee;"
|
||||||
|
|
||||||
|
- name: state dump/restore - create database
|
||||||
|
postgresql_db: "{{ admin_map | combine({'state': 'present'}) }}"
|
||||||
|
|
||||||
|
- name: state dump/restore - create table employee
|
||||||
|
command: '{{ user_str }} -c "{{ sql_create }}"'
|
||||||
|
|
||||||
|
- name: state dump/restore - insert data into table employee
|
||||||
|
command: '{{ user_str }} -c "{{ sql_insert }}"'
|
||||||
|
|
||||||
|
- name: state dump/restore - file name should not exist
|
||||||
|
file: name={{ db_file_name }} state=absent
|
||||||
|
|
||||||
|
- name: test state=dump to backup the database (expect changed=true)
|
||||||
|
postgresql_db: "{{ user_map | combine({'state': 'dump'}) }}"
|
||||||
|
register: result
|
||||||
|
become_user: "{{ pg_user }}"
|
||||||
|
become: True
|
||||||
|
|
||||||
|
- name: assert output message backup the database
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- "result.changed == true"
|
||||||
|
|
||||||
|
- name: assert database was backed up successfully
|
||||||
|
command: file {{ db_file_name }}
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- name: state dump/restore - remove database for restore
|
||||||
|
postgresql_db: "{{ user_map | combine({'state': 'absent'}) }}"
|
||||||
|
|
||||||
|
- name: state dump/restore - re-create database
|
||||||
|
postgresql_db: "{{ admin_map | combine({'state': 'present'}) }}"
|
||||||
|
|
||||||
|
- name: test state=restore to restore the database (expect changed=true)
|
||||||
|
postgresql_db: "{{ user_map | combine({'state': 'restore'}) }}"
|
||||||
|
register: result
|
||||||
|
become_user: "{{ pg_user }}"
|
||||||
|
become: True
|
||||||
|
|
||||||
|
- name: assert output message restore the database
|
||||||
|
assert: { that: "result.changed == true" }
|
||||||
|
|
||||||
|
- name: select data from table employee
|
||||||
|
command: '{{ user_str }} -c "{{ sql_select }}"'
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- name: assert data in database is from the restore database
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- "'47' in result.stdout"
|
||||||
|
- "'Joe Smith' in result.stdout"
|
||||||
|
|
||||||
|
- name: state dump/restore - remove database name
|
||||||
|
postgresql_db: "{{ user_map | combine({'state': 'absent'}) }}"
|
||||||
|
|
||||||
|
- name: remove file name
|
||||||
|
file: name={{ db_file_name }} state=absent
|
Loading…
Reference in New Issue