From a44943d6925aaffa1cd1d0b2d96e65466198278c Mon Sep 17 00:00:00 2001 From: Mahendra Singh Thalor Date: Fri, 4 Apr 2025 14:36:31 +0530 Subject: [PATCH] add some more TAP-test for pg_restore and pg_dumpall for non-text mode of pg_dumpall --- src/bin/pg_dump/t/001_basic.pl | 10 ++++ src/bin/pg_dump/t/006_pg_dumpall.pl | 76 ++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 8 deletions(-) mode change 100644 => 100755 src/bin/pg_dump/t/001_basic.pl mode change 100644 => 100755 src/bin/pg_dump/t/006_pg_dumpall.pl diff --git a/src/bin/pg_dump/t/001_basic.pl b/src/bin/pg_dump/t/001_basic.pl old mode 100644 new mode 100755 index 0bbcdbe84a7..113a915bfbf --- a/src/bin/pg_dump/t/001_basic.pl +++ b/src/bin/pg_dump/t/001_basic.pl @@ -242,6 +242,16 @@ command_fails_like( qr/\Qpg_restore: error: option --exclude-database cannot be used together with -g\/--globals-only\E/, 'pg_restore: option --exclude-database cannot be used together with -g/--globals-only'); +command_fails_like( + [ 'pg_restore', '--exclude-database=foo', '-d', 'xxx', 'dumpdir' ], + qr/\Qpg_restore: error: option --exclude-database can be used only when restoring multiple databases by archive of pg_dumpall\E/, + 'When option --exclude-database is used in pg_restore with dump of pg_dump'); + +command_fails_like( + [ 'pg_restore', '--globals-only', '-d', 'xxx', 'dumpdir' ], + qr/\Qpg_restore: error: option -g\/--globals-only can be used only when restoring multiple databases by archive of pg_dumpall\E/, + 'When option --globals-only is not used in pg_restore with dump of pg_dump'); + # also fails for -r and -t, but it seems pointless to add more tests for those. command_fails_like( [ 'pg_dumpall', '--exclude-database=foo', '--globals-only' ], diff --git a/src/bin/pg_dump/t/006_pg_dumpall.pl b/src/bin/pg_dump/t/006_pg_dumpall.pl old mode 100644 new mode 100755 index fdfd1ae990b..44afdf525ff --- a/src/bin/pg_dump/t/006_pg_dumpall.pl +++ b/src/bin/pg_dump/t/006_pg_dumpall.pl @@ -115,6 +115,7 @@ my %pgdumpall_runs = ( CREATE ROLE grant8; CREATE TABLE t (id int); + INSERT INTO t VALUES (1), (2), (3), (4); GRANT SELECT ON TABLE t TO grant1; GRANT INSERT ON TABLE t TO grant2; @@ -157,27 +158,37 @@ my %pgdumpall_runs = ( setup_sql => 'CREATE DATABASE db1; \c db1 CREATE TABLE t1 (id int); + INSERT INTO t1 VALUES (1), (2), (3), (4); CREATE TABLE t2 (id int); + INSERT INTO t2 VALUES (1), (2), (3), (4); CREATE DATABASE db2; \c db2 CREATE TABLE t3 (id int); + INSERT INTO t3 VALUES (1), (2), (3), (4); CREATE TABLE t4 (id int); + INSERT INTO t4 VALUES (1), (2), (3), (4); CREATE DATABASE dbex3; \c dbex3 CREATE TABLE t5 (id int); + INSERT INTO t5 VALUES (1), (2), (3), (4); CREATE TABLE t6 (id int); + INSERT INTO t6 VALUES (1), (2), (3), (4); CREATE DATABASE dbex4; \c dbex4 CREATE TABLE t7 (id int); + INSERT INTO t7 VALUES (1), (2), (3), (4); CREATE TABLE t8 (id int); + INSERT INTO t8 VALUES (1), (2), (3), (4); CREATE DATABASE db5; \c db5 CREATE TABLE t9 (id int); + INSERT INTO t9 VALUES (1), (2), (3), (4); CREATE TABLE t10 (id int); + INSERT INTO t10 VALUES (1), (2), (3), (4); ', dump_cmd => [ 'pg_dumpall', @@ -225,8 +236,8 @@ my %pgdumpall_runs = ( }, format_directory => { - setup_sql => - 'CREATE TABLE format_directory(a int, b boolean, c text);', + setup_sql => "CREATE TABLE format_directory(a int, b boolean, c text); + INSERT INTO format_directory VALUES (1, true, 'name1'), (2, false, 'name2');", dump_cmd => [ 'pg_dumpall', '--format' => 'directory', @@ -238,11 +249,12 @@ my %pgdumpall_runs = ( '--file' => "$tempdir/format_directory.sql", "$tempdir/format_directory", ], - like => qr/^\n\QCREATE TABLE public.format_directory (/xm + like => qr/^\n\QCOPY public.format_directory (a, b, c) FROM stdin;/xm }, format_tar => { - setup_sql => 'CREATE TABLE format_tar(id int);', + setup_sql => "CREATE TABLE format_tar(a int, b boolean, c text); + INSERT INTO format_tar VALUES (1, false, 'name3'), (2, true, 'name4');", dump_cmd => [ 'pg_dumpall', '--format' => 'tar', @@ -254,11 +266,12 @@ my %pgdumpall_runs = ( '--file' => "$tempdir/format_tar.sql", "$tempdir/format_tar", ], - like => qr/^\n\QCREATE TABLE public.format_tar (/xm + like => qr/^\n\QCOPY public.format_tar (a, b, c) FROM stdin;/xm }, format_custom => { - setup_sql => 'CREATE TABLE format_custom(a int, b boolean, c text);', + setup_sql => "CREATE TABLE format_custom(a int, b boolean, c text); + INSERT INTO format_custom VALUES (1, false, 'name5'), (2, true, 'name6');", dump_cmd => [ 'pg_dumpall', '--format' => 'custom', @@ -270,9 +283,28 @@ my %pgdumpall_runs = ( '--file' => "$tempdir/format_custom.sql", "$tempdir/format_custom", ], - like => qr/^ \n\QCREATE TABLE public.format_custom (/xm - },); + like => qr/^ \n\QCOPY public.format_custom (a, b, c) FROM stdin;/xm + }, + dump_globals_only => { + setup_sql => "CREATE TABLE format_dir(a int, b boolean, c text); + INSERT INTO format_dir VALUES (1, false, 'name5'), (2, true, 'name6');", + dump_cmd => [ + 'pg_dumpall', + '--format' => 'directory', + '--globals-only', + '--file' => "$tempdir/dump_globals_only", + ], + restore_cmd => [ + 'pg_restore', '-C', '--globals-only', + '--format' => 'directory', + '--file' => "$tempdir/dump_globals_only.sql", + "$tempdir/dump_globals_only", + ], + like => qr/ + ^\s*\QCREATE ROLE dumpall;\E\s*\n + /xm + }, ); # First execute the setup_sql foreach my $run (sort keys %pgdumpall_runs) @@ -326,6 +358,34 @@ foreach my $run (sort keys %pgdumpall_runs) } } +# Some negative test case with dump of pg_dumpall and restore using pg_restore +# test case 1: when -C is not used in pg_restore with dump of pg_dumpall +$node->command_fails_like( + [ 'pg_restore', + "$tempdir/format_custom", + '--format' => 'custom', + '--file' => "$tempdir/error_test.sql", ], + qr/\Qpg_restore: error: -C\/--create option should be specified when restoring multiple databases by archive of pg_dumpall\E/, + 'When -C is not used in pg_restore with dump of pg_dumpall'); + +# test case 2: When --list option is used with dump of pg_dumpall +$node->command_fails_like( + [ 'pg_restore', + "$tempdir/format_custom", '-C', + '--format' => 'custom', '--list', + '--file' => "$tempdir/error_test.sql", ], + qr/\Qpg_restore: error: option -l\/--list cannot be used when restoring multiple databases by archive of pg_dumpall\E/, + 'When --list is used in pg_restore with dump of pg_dumpall'); + +# test case 3: When non-exist database is given with -d option +$node->command_fails_like( + [ 'pg_restore', + "$tempdir/format_custom", '-C', + '--format' => 'custom', + '-d' => 'dbpq', ], + qr/\Qpg_restore: error: could not connect to database "dbpq"\E/, + 'When non-exist datbase is given with -d option in pg_restore with dump of pg_dumpall'); + $node->stop('fast'); done_testing(); -- 2.39.3