|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | +use PostgreSQL::Test::Cluster; |
| 4 | +use PostgreSQL::Test::Utils; |
| 5 | +use Test::More; |
| 6 | + |
| 7 | +# Tests the slot manipulation in the single-user mode |
| 8 | + |
| 9 | +# Skip the whole thing on the windows platform |
| 10 | +if ($windows_os) |
| 11 | +{ |
| 12 | + plan skip_all => 'this test is not supported by this platform'; |
| 13 | +} |
| 14 | + |
| 15 | +# Run passed commands in single-user mode. The return value from the command is |
| 16 | +# passed through. |
| 17 | +sub run_test_in_single_user_mode |
| 18 | +{ |
| 19 | + my ($node, $commands, $testname) = @_; |
| 20 | + |
| 21 | + my $result = run_log( |
| 22 | + [ |
| 23 | + 'postgres', '--single', '-F', |
| 24 | + '-c' => 'exit_on_error=true', |
| 25 | + '-D' => $node->data_dir, |
| 26 | + 'postgres' |
| 27 | + ], |
| 28 | + \$commands); |
| 29 | + |
| 30 | + return $result; |
| 31 | +} |
| 32 | + |
| 33 | +# Wrapper function for run_test_in_single_user_mode. This would be used when |
| 34 | +# the input command succeeds. |
| 35 | +sub run_test_in_single_user_mode_success |
| 36 | +{ |
| 37 | + my ($node, $commands, $testname) = @_; |
| 38 | + |
| 39 | + my $result = run_test_in_single_user_mode($node, $commands, $testname); |
| 40 | + |
| 41 | + ok($result, $testname); |
| 42 | +} |
| 43 | + |
| 44 | +# Wrapper function for run_test_in_single_user_mode. This would be used when |
| 45 | +# the input command fails. |
| 46 | +sub run_test_in_single_user_mode_fail |
| 47 | +{ |
| 48 | + my ($node, $commands, $testname) = @_; |
| 49 | + |
| 50 | + my $result = run_test_in_single_user_mode($node, $commands, $testname); |
| 51 | + |
| 52 | + ok(!$result, $testname); |
| 53 | +} |
| 54 | + |
| 55 | +my $slotname = 'test_slot'; |
| 56 | + |
| 57 | +# Initialize a node |
| 58 | +my $node = PostgreSQL::Test::Cluster->new('node'); |
| 59 | +$node->init(allows_streaming => "logical"); |
| 60 | +$node->start; |
| 61 | + |
| 62 | +# Define initial table |
| 63 | +$node->safe_psql( |
| 64 | + 'postgres', qq( |
| 65 | +CREATE TABLE foo (id int); |
| 66 | +SELECT pg_create_logical_replication_slot('$slotname', 'test_decoding'); |
| 67 | +)); |
| 68 | + |
| 69 | +# Stop the node to run and test in single-user mode |
| 70 | +$node->stop; |
| 71 | + |
| 72 | +run_test_in_single_user_mode_fail( |
| 73 | + $node, |
| 74 | + "SELECT pg_create_logical_replication_slot('another_slot', 'test_decoding')", |
| 75 | + "replication slot cannot be created in single-user mode"); |
| 76 | + |
| 77 | +run_test_in_single_user_mode_fail( |
| 78 | + $node, qq( |
| 79 | +INSERT INTO foo VALUES (1); |
| 80 | +SELECT count(1) FROM pg_logical_slot_get_changes('$slotname', NULL, NULL); |
| 81 | +), |
| 82 | + "logical decoding cannot be done in single-user mode"); |
| 83 | + |
| 84 | +run_test_in_single_user_mode_fail( |
| 85 | + $node, |
| 86 | + "SELECT pg_replication_slot_advance('$slotname', pg_current_wal_lsn())", |
| 87 | + "replication slot cannot be advanced in single-user mode"); |
| 88 | + |
| 89 | +run_test_in_single_user_mode_fail( |
| 90 | + $node, |
| 91 | + "SELECT pg_copy_logical_replication_slot('$slotname', 'dest_slot')", |
| 92 | + "replication slot cannot be copied in single-user mode"); |
| 93 | + |
| 94 | +run_test_in_single_user_mode_success( |
| 95 | + $node, |
| 96 | + "SELECT pg_drop_replication_slot('$slotname')", |
| 97 | + "replication slot can be dropped in single-user mode"); |
| 98 | + |
| 99 | +done_testing(); |
0 commit comments