|
| 1 | + |
| 2 | +# Copyright (c) 2024-2024, MDB, Mother Russia |
| 3 | + |
| 4 | +# Minimal test testing streaming replication |
| 5 | +use strict; |
| 6 | +use warnings; |
| 7 | +use PostgreSQL::Test::Cluster; |
| 8 | +use PostgreSQL::Test::Utils; |
| 9 | +use Test::More; |
| 10 | + |
| 11 | +# Initialize primary node |
| 12 | +my $node_primary = PostgreSQL::Test::Cluster->new('primary'); |
| 13 | +$node_primary->init(); |
| 14 | +$node_primary->start; |
| 15 | + |
| 16 | +# Create some content on primary and check its presence in standby nodes |
| 17 | +$node_primary->safe_psql('postgres', |
| 18 | + " |
| 19 | + CREATE DATABASE regress; |
| 20 | + CREATE ROLE mdb_admin; |
| 21 | + CREATE ROLE mdb_reg_lh_app_name; |
| 22 | + GRANT mdb_admin to mdb_reg_lh_app_name; |
| 23 | + GRANT pg_signal_backend to mdb_reg_lh_app_name; |
| 24 | + CREATE TABLE mdb_app_name_t(i int); |
| 25 | +"); |
| 26 | + |
| 27 | +my $main_sess = $node_primary->background_psql('postgres'); |
| 28 | + |
| 29 | +$main_sess->query_safe( |
| 30 | + q( |
| 31 | +SET application_name TO 'SU'; |
| 32 | +BEGIN; |
| 33 | +INSERT INTO mdb_app_name_t VALUES(0); |
| 34 | +)); |
| 35 | + |
| 36 | + |
| 37 | +my $res_pid = $node_primary->safe_psql('regress', |
| 38 | + " |
| 39 | + SELECT pid FROM pg_stat_activity WHERE application_name = 'SU'; |
| 40 | +"); |
| 41 | + |
| 42 | +print "pid is $res_pid\n"; |
| 43 | + |
| 44 | +ok(1); |
| 45 | + |
| 46 | + |
| 47 | +my ($res_reg_lh_1, $stdout_reg_lh_1, $stderr_reg_lh_1) = $node_primary->psql('regress', |
| 48 | + " |
| 49 | + SET ROLE mdb_reg_lh_app_name; |
| 50 | + SELECT pg_terminate_backend($res_pid); |
| 51 | +"); |
| 52 | + |
| 53 | +# print ($res_reg_lh_1, $stdout_reg_lh_1, $stderr_reg_lh_1, "\n"); |
| 54 | + |
| 55 | +ok($res_reg_lh_1 != 0, "should fail for non-MDB"); |
| 56 | +like($stderr_reg_lh_1, qr/Only roles with the SUPERUSER attribute may terminate processes of roles with the SUPERUSER attribute./, "matches"); |
| 57 | + |
| 58 | +# should succeed |
| 59 | +$main_sess->query_safe(qq[COMMIT]); |
| 60 | + |
| 61 | +$main_sess->query_safe( |
| 62 | + q( |
| 63 | +SET application_name TO 'MDB'; |
| 64 | +BEGIN; |
| 65 | +INSERT INTO mdb_app_name_t VALUES(1); |
| 66 | +)); |
| 67 | + |
| 68 | + |
| 69 | +my ($res_reg_lh_2, $stdout_reg_lh_2, $stderr_reg_lh_2) = $node_primary->psql('regress', |
| 70 | + " |
| 71 | + SET ROLE mdb_reg_lh_app_name; |
| 72 | + SELECT pg_terminate_backend($res_pid); |
| 73 | +"); |
| 74 | + |
| 75 | +ok($res_reg_lh_2 == 0, "should success for MDB"); |
| 76 | + |
| 77 | +done_testing(); |
0 commit comments