349a0dccee
* New module postgresql_table - create postgresql table on remote host * New module postgresql_table - fix tests * New module postgresql_table - fix tests * New module postgresql_table - fix tests * New module postgresql_table - fix a typo * New module postgresql_table - change columns and params to type str * New module postgresql_table - fix columns type * New module postgresql_table - added pg_quote_identifier * New module postgresql_table - removed unused lib * New module postgresql_table - query -> queries * New module postgresql_table - test improvement * New module postgresql_table - fixed tests for PG 9.0 * New module postgresql_table - fixes * New module postgresql_table - fixes
693 lines
17 KiB
YAML
693 lines
17 KiB
YAML
# Test code for the postgresql_set module
|
|
|
|
# Copyright: (c) 2019, Andrew Klychkov (@Andersson007) <aaklychkov@mail.ru>
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# Create a role for tests:
|
|
- name: postgresql_table - create a role for tests
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_user:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: alice
|
|
|
|
#
|
|
# Check table creation
|
|
#
|
|
|
|
# Create a simple table in check_mode:
|
|
- name: postgresql_table - create table in check_mode
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_table:
|
|
login_db: postgres
|
|
login_port: 5432
|
|
login_user: "{{ pg_user }}"
|
|
name: test1
|
|
owner: alice
|
|
columns: id int
|
|
register: result
|
|
ignore_errors: yes
|
|
check_mode: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.table == 'test1'
|
|
- result.queries == ['CREATE TABLE "test1" (id int)', 'ALTER TABLE "test1" OWNER TO "alice"']
|
|
- result.state == 'absent'
|
|
|
|
# Check that the table doesn't exist after the previous step, rowcount must be 0
|
|
- name: postgresql_table - check that table doesn't exist after the previous step
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_stat_all_tables WHERE relname ='test1'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 0
|
|
|
|
# Create a simple table:
|
|
- name: postgresql_table - create table
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_table:
|
|
login_db: postgres
|
|
login_port: 5432
|
|
login_user: "{{ pg_user }}"
|
|
name: test1
|
|
owner: alice
|
|
columns: id int
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.table == 'test1'
|
|
- result.queries == ['CREATE TABLE "test1" (id int)', 'ALTER TABLE "test1" OWNER TO "alice"']
|
|
- result.state == 'present'
|
|
- result.storage_params == []
|
|
- result.tablespace == ""
|
|
- result.owner == "alice"
|
|
|
|
# Check that the table exists after the previous step, rowcount must be 1
|
|
- name: postgresql_table - check that table exists after the previous step
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_stat_all_tables WHERE relname ='test1'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 1
|
|
|
|
# Check that the tableowner is alice
|
|
- name: postgresql_table - check that table owner is alice
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_tables WHERE tablename = 'test1' AND tableowner = 'alice'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 1
|
|
|
|
#
|
|
# Check create table like another table
|
|
#
|
|
|
|
# Create a table LIKE another table without any additional parameters in check_mode:
|
|
- name: postgresql_table - create table like in check_mode
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test2
|
|
like: test1
|
|
register: result
|
|
ignore_errors: yes
|
|
check_mode: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.table == 'test2'
|
|
- result.queries == ['CREATE TABLE "test2" (LIKE "test1")']
|
|
- result.state == 'absent'
|
|
|
|
# Check that the table doesn't exist after the previous step, rowcount must be 0
|
|
- name: postgresql_table - check that table doesn't exist after the previous step
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_stat_all_tables WHERE relname ='test2'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 0
|
|
|
|
# Create a table LIKE another table without any additional parameters:
|
|
- name: postgresql_table - create table like
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test2
|
|
like: test1
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.table == 'test2'
|
|
- result.queries == ['CREATE TABLE "test2" (LIKE "test1")']
|
|
- result.state == 'present'
|
|
- result.storage_params == []
|
|
- result.tablespace == ""
|
|
- result.owner == "{{ pg_user }}"
|
|
|
|
# Check that the table exists after the previous step, rowcount must be 1
|
|
- name: postgresql_table - check that table exists after the previous step
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_stat_all_tables WHERE relname ='test2'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 1
|
|
|
|
#
|
|
# Check drop table
|
|
#
|
|
|
|
# Drop a table in check_mode:
|
|
- name: postgresql_table - drop table in check_mode
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test2
|
|
state: absent
|
|
register: result
|
|
ignore_errors: yes
|
|
check_mode: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.queries == ['DROP TABLE "test2"']
|
|
- result.state == 'present'
|
|
- result.storage_params == []
|
|
- result.tablespace == ""
|
|
- result.owner == "{{ pg_user }}"
|
|
|
|
# Check that the table exists after the previous step, rowcount must be 1
|
|
- name: postgresql_table - check that table exists after the previous step
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_stat_all_tables WHERE relname ='test2'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 1
|
|
|
|
# Drop a table:
|
|
- name: postgresql_table - drop table
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test2
|
|
state: absent
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.queries == ['DROP TABLE "test2"']
|
|
- result.state == 'absent'
|
|
|
|
# Check that the table doesn't exist after the previous step, rowcount must be 0
|
|
- name: postgresql_table - check that table doesn't exist after the previous step
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_stat_all_tables WHERE relname ='test2'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 0
|
|
|
|
# Create a table like another table including:
|
|
- name: postgresql_table - create table like with including indexes
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test2
|
|
like: test1
|
|
including: indexes
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.queries == ['CREATE TABLE "test2" (LIKE "test1" INCLUDING indexes)']
|
|
- result.state == 'present'
|
|
- result.storage_params == []
|
|
- result.tablespace == ""
|
|
- result.owner == "{{ pg_user }}"
|
|
|
|
# Check to create table if it exists:
|
|
- name: postgresql_table - try to create existing table again
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test2
|
|
like: test1
|
|
including: indexes
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == false
|
|
|
|
# Drop the table to prepare for the next step:
|
|
- name: postgresql_table - drop table
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test2
|
|
state: absent
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
# Try to drop non existing table:
|
|
- name: postgresql_table - try drop dropped table again
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test2
|
|
state: absent
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == false
|
|
|
|
#
|
|
# Change ownership
|
|
#
|
|
|
|
# Create user to prepare for the next step:
|
|
- name: postgresql_table - create the new user test_user
|
|
become: yes
|
|
become_user: "{{ pg_user }}"
|
|
postgresql_user:
|
|
login_user: "{{ pg_user }}"
|
|
db: postgres
|
|
name: test_user
|
|
state: present
|
|
ignore_errors: yes
|
|
|
|
# Try to change owner to test_user in check_mode
|
|
- name: postgresql_table - change table ownership to test_user in check_mode
|
|
become: yes
|
|
become_user: "{{ pg_user }}"
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test1
|
|
owner: test_user
|
|
register: result
|
|
ignore_errors: yes
|
|
check_mode: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.owner == 'alice'
|
|
- result.queries == ['ALTER TABLE "test1" OWNER TO "test_user"']
|
|
- result.state == 'present'
|
|
- result.changed == true
|
|
|
|
# Check that the tableowner was not changed to test_user
|
|
- name: postgresql_table - check that table owner was not changed
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_tables WHERE tablename = 'test1' AND tableowner = 'test_user'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == 0
|
|
|
|
# Try to change owner to test_user
|
|
- name: postgresql_table - change table ownership to test_user
|
|
become: yes
|
|
become_user: "{{ pg_user }}"
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test1
|
|
owner: test_user
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.owner == 'test_user'
|
|
- result.queries == ['ALTER TABLE "test1" OWNER TO "test_user"']
|
|
- result.state == 'present'
|
|
- result.changed == true
|
|
|
|
# Check that the tableowner was changed to test_user
|
|
- name: postgresql_table - check that table owner was changed
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_tables WHERE tablename = 'test1' AND tableowner = 'test_user'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 1
|
|
|
|
#
|
|
# Additional storage parameters
|
|
#
|
|
|
|
# Create a table with additional storage parameters:
|
|
- name: postgresql_table - create table with storage_params
|
|
become: yes
|
|
become_user: "{{ pg_user }}"
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test3
|
|
columns:
|
|
- id int
|
|
- name text
|
|
storage_params:
|
|
- fillfactor=10
|
|
- autovacuum_analyze_threshold=1
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.state == 'present'
|
|
- result.queries == ['CREATE TABLE "test3" (id int,name text) WITH (fillfactor=10,autovacuum_analyze_threshold=1)']
|
|
- result.storage_params == [ "fillfactor=10", "autovacuum_analyze_threshold=1" ]
|
|
|
|
# Check storage parameters
|
|
- name: postgresql_table - check storage parameters
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT reloptions FROM pg_class WHERE relname = 'test3'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.query_result[0].reloptions == ["fillfactor=10", "autovacuum_analyze_threshold=1"]
|
|
#
|
|
# Check truncate table
|
|
#
|
|
|
|
# Insert a row to test table:
|
|
- name: postgresql_table - insert a row
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "INSERT INTO test3 (id, name) VALUES (1, 'first')"
|
|
|
|
# Truncate a table in check_mode:
|
|
- name: postgresql_table - truncate table
|
|
become: yes
|
|
become_user: "{{ pg_user }}"
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test3
|
|
truncate: yes
|
|
register: result
|
|
ignore_errors: yes
|
|
check_mode: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.queries == ['TRUNCATE TABLE "test3"']
|
|
- result.state == "present"
|
|
|
|
# Check the row exists:
|
|
- name: postgresql_table - check that row exists after the previous step
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT * FROM test3 WHERE id = '1'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 1
|
|
|
|
# Truncate a table. It always returns changed == true
|
|
# because it always creates a new table with the same schema and drop the old table:
|
|
- name: postgresql_table - truncate table
|
|
become: yes
|
|
become_user: "{{ pg_user }}"
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test3
|
|
truncate: yes
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.queries == ['TRUNCATE TABLE "test3"']
|
|
- result.state == "present"
|
|
|
|
# Check the row exists:
|
|
- name: postgresql_table - check that row doesn't exist after the previous step
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT * FROM test3 WHERE id = '1'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 0
|
|
|
|
#
|
|
# Check rename table
|
|
#
|
|
|
|
# Rename a table in check_mode.
|
|
# In check_mode test4 won't be exist after the following playbook,
|
|
# so result.changed == 'absent' for the table with this name
|
|
- name: postgresql_table - rename table in check_mode
|
|
become: yes
|
|
become_user: "{{ pg_user }}"
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test3
|
|
rename: test4
|
|
register: result
|
|
ignore_errors: yes
|
|
check_mode: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.queries == ['ALTER TABLE "test3" RENAME TO "test4"']
|
|
- result.state == "absent"
|
|
|
|
# Check that the table exists after the previous step, rowcount must be 1
|
|
- name: postgresql_table - check that table exists after the previous step
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_stat_all_tables WHERE relname = 'test3'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 1
|
|
|
|
# Rename a table:
|
|
- name: postgresql_table - rename table
|
|
become: yes
|
|
become_user: "{{ pg_user }}"
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test3
|
|
rename: test4
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.queries == ['ALTER TABLE "test3" RENAME TO "test4"']
|
|
- result.state == "present"
|
|
|
|
# Check that the table test 3 doesn't exist after the previous step, rowcount must be - 0
|
|
- name: postgresql_table - check that table exists after the previous step
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_stat_all_tables WHERE relname = 'test3'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 0
|
|
|
|
# Check that the table test 4 exists after the previous step, rowcount must be - 1
|
|
- name: postgresql_table - check that table exists after the previous step
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_stat_all_tables WHERE relname = 'test4'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 1
|
|
|
|
#
|
|
# Check create unlogged table
|
|
#
|
|
|
|
# Create unlogged table in check_mode:
|
|
- name: postgresql_table - create unlogged table in check_mode
|
|
become: yes
|
|
become_user: "{{ pg_user }}"
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test5
|
|
unlogged: yes
|
|
register: result
|
|
ignore_errors: yes
|
|
check_mode: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.queries == ['CREATE UNLOGGED TABLE "test5" ()']
|
|
when: postgres_version_resp.stdout is version('9.1', '>=')
|
|
|
|
# Check that the table doesn't exist after the previous step, rowcount must be - 0
|
|
- name: postgresql_table - check that table doesn't exist after the previous step
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_stat_all_tables WHERE relname = 'test5'"
|
|
ignore_errors: yes
|
|
register: result
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 0
|
|
|
|
# Create unlogged table:
|
|
- name: postgresql_table - create unlogged table
|
|
become: yes
|
|
become_user: "{{ pg_user }}"
|
|
postgresql_table:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
name: test5
|
|
unlogged: yes
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
- assert:
|
|
that:
|
|
- result.changed == true
|
|
- result.queries == ['CREATE UNLOGGED TABLE "test5" ()']
|
|
when: postgres_version_resp.stdout is version('9.1', '>=')
|
|
|
|
# Check that the table exists after the previous step, rowcount must be - 1
|
|
- name: postgresql_table - check that table exists after the previous step
|
|
become_user: "{{ pg_user }}"
|
|
become: yes
|
|
postgresql_query:
|
|
db: postgres
|
|
login_user: "{{ pg_user }}"
|
|
query: "SELECT 1 FROM pg_stat_all_tables WHERE relname = 'test5'"
|
|
ignore_errors: yes
|
|
register: result
|
|
when: postgres_version_resp.stdout is version('9.1', '>=')
|
|
|
|
- assert:
|
|
that:
|
|
- result.rowcount == 1
|
|
when: postgres_version_resp.stdout is version('9.1', '>=')
|