| 1 |
use warnings; |
| 2 |
use strict; |
| 3 |
use LWP::Protocol::PSGI; |
| 4 |
use Test::More; |
| 5 |
|
| 6 |
my $db = 'noexist/patroniConf.sql'; |
| 7 |
|
| 8 |
BEGIN { |
| 9 |
use_ok('Lemonldap::NG::Common::Conf'); |
| 10 |
} |
| 11 |
|
| 12 |
my $h; |
| 13 |
@ARGV = ("help=groups"); |
| 14 |
|
| 15 |
# Normal Patroni response helper |
| 16 |
my $patroni_response; |
| 17 |
|
| 18 |
sub setup_patroni_mock { |
| 19 |
LWP::Protocol::PSGI->register( |
| 20 |
sub { |
| 21 |
return [ |
| 22 |
200, |
| 23 |
[ |
| 24 |
'Content-Type' => 'application/json', |
| 25 |
'Content-Length' => length($patroni_response) |
| 26 |
], |
| 27 |
[$patroni_response], |
| 28 |
]; |
| 29 |
} |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
SKIP: { |
| 34 |
eval { require DBI; }; |
| 35 |
skip( "DBI not installed", 5 ) if ($@); |
| 36 |
eval { require DBD::SQLite }; |
| 37 |
skip( "DBD::SQLite not installed", 5 ) if ($@); |
| 38 |
|
| 39 |
subtest 'Basic Patroni functionality' => sub { |
| 40 |
plan tests => 7; |
| 41 |
|
| 42 |
$patroni_response = <<'EOT'; |
| 43 |
{ |
| 44 |
"members": [ |
| 45 |
{ |
| 46 |
"role":"leader", |
| 47 |
"host":"127.0.0.235", |
| 48 |
"port":"5432" |
| 49 |
} |
| 50 |
] |
| 51 |
} |
| 52 |
EOT |
| 53 |
setup_patroni_mock(); |
| 54 |
|
| 55 |
ok( |
| 56 |
$h = new Lemonldap::NG::Common::Conf( { |
| 57 |
type => 'Patroni', |
| 58 |
dbiChain => "DBI:SQLite:dbname=$db", |
| 59 |
dbiUser => '', |
| 60 |
dbiPassword => '', |
| 61 |
patroniUrl => 'http://db.com/', |
| 62 |
} |
| 63 |
), |
| 64 |
'Create Patroni backend object' |
| 65 |
); |
| 66 |
|
| 67 |
ok( $h->can('_dbh'), 'Driver has _dbh method' ); |
| 68 |
$h->store( { cfgNum => 1, test => 'ascii' } ); |
| 69 |
is( |
| 70 |
$h->{dbiChain}, |
| 71 |
'DBI:SQLite:dbname=noexist/patroniConf.sql;host=127.0.0.235;port=5432', |
| 72 |
'dbiChain updated with leader host/port' |
| 73 |
); |
| 74 |
|
| 75 |
ok( $h->{patroniLastLeader}, 'Leader info cached' ); |
| 76 |
is( $h->{patroniLastLeader}->{host}, |
| 77 |
'127.0.0.235', 'Cached leader host is correct' ); |
| 78 |
is( $h->{patroniLastLeader}->{port}, |
| 79 |
'5432', 'Cached leader port is correct' ); |
| 80 |
ok( $h->{patroniLastLeader}->{time}, 'Cache timestamp set' ); |
| 81 |
}; |
| 82 |
|
| 83 |
subtest 'Configuration options' => sub { |
| 84 |
plan tests => 2; |
| 85 |
|
| 86 |
ok( |
| 87 |
$h = new Lemonldap::NG::Common::Conf( { |
| 88 |
type => 'Patroni', |
| 89 |
dbiChain => "DBI:SQLite:dbname=$db", |
| 90 |
dbiUser => '', |
| 91 |
dbiPassword => '', |
| 92 |
patroniUrl => 'http://db.com/', |
| 93 |
patroniTimeout => 5, |
| 94 |
} |
| 95 |
), |
| 96 |
'Create Patroni backend with custom timeout' |
| 97 |
); |
| 98 |
|
| 99 |
ok( |
| 100 |
$h = new Lemonldap::NG::Common::Conf( { |
| 101 |
type => 'Patroni', |
| 102 |
dbiChain => "DBI:SQLite:dbname=$db", |
| 103 |
dbiUser => '', |
| 104 |
dbiPassword => '', |
| 105 |
patroniUrl => |
| 106 |
'http://db1.com/, http://db2.com/, http://db3.com/', |
| 107 |
} |
| 108 |
), |
| 109 |
'Create Patroni backend with multiple URLs' |
| 110 |
); |
| 111 |
}; |
| 112 |
|
| 113 |
subtest 'Cache fallback mechanism' => sub { |
| 114 |
plan tests => 3; |
| 115 |
|
| 116 |
$patroni_response = <<'EOT'; |
| 117 |
{ |
| 118 |
"members": [ |
| 119 |
{ |
| 120 |
"role":"leader", |
| 121 |
"host":"127.0.0.100", |
| 122 |
"port":"5433" |
| 123 |
} |
| 124 |
] |
| 125 |
} |
| 126 |
EOT |
| 127 |
setup_patroni_mock(); |
| 128 |
|
| 129 |
ok( |
| 130 |
$h = new Lemonldap::NG::Common::Conf( { |
| 131 |
type => 'Patroni', |
| 132 |
dbiChain => "DBI:SQLite:dbname=$db", |
| 133 |
dbiUser => '', |
| 134 |
dbiPassword => '', |
| 135 |
patroniUrl => 'http://db.com/', |
| 136 |
patroniCacheTTL => 120, |
| 137 |
} |
| 138 |
), |
| 139 |
'Create backend with custom cache TTL' |
| 140 |
); |
| 141 |
|
| 142 |
$h->store( { cfgNum => 5, test => 'test1' } ); |
| 143 |
ok( $h->{patroniLastLeader}, 'Leader cached after first store' ); |
| 144 |
|
| 145 |
# Simulate Patroni API failure |
| 146 |
LWP::Protocol::PSGI->register( |
| 147 |
sub { |
| 148 |
return [ |
| 149 |
500, [ 'Content-Type' => 'text/plain' ], |
| 150 |
['Server Error'] |
| 151 |
]; |
| 152 |
} |
| 153 |
); |
| 154 |
|
| 155 |
delete $h->{_dbh}; # Force reconnection |
| 156 |
eval { $h->store( { cfgNum => 6, test => 'test2' } ); }; |
| 157 |
|
| 158 |
like( |
| 159 |
$h->{dbiChain}, |
| 160 |
qr/host=127\.0\.0\.100;port=5433/, |
| 161 |
'Uses cached leader when Patroni API unavailable' |
| 162 |
); |
| 163 |
}; |
| 164 |
|
| 165 |
subtest 'Split-brain detection' => sub { |
| 166 |
plan tests => 1; |
| 167 |
|
| 168 |
$patroni_response = <<'EOT'; |
| 169 |
{ |
| 170 |
"members": [ |
| 171 |
{ |
| 172 |
"role":"leader", |
| 173 |
"host":"127.0.0.1", |
| 174 |
"port":"5432", |
| 175 |
"state":"running" |
| 176 |
}, |
| 177 |
{ |
| 178 |
"role":"leader", |
| 179 |
"host":"127.0.0.2", |
| 180 |
"port":"5432", |
| 181 |
"state":"running" |
| 182 |
} |
| 183 |
] |
| 184 |
} |
| 185 |
EOT |
| 186 |
setup_patroni_mock(); |
| 187 |
|
| 188 |
$h = new Lemonldap::NG::Common::Conf( { |
| 189 |
type => 'Patroni', |
| 190 |
dbiChain => "DBI:SQLite:dbname=$db", |
| 191 |
dbiUser => '', |
| 192 |
dbiPassword => '', |
| 193 |
patroniUrl => 'http://db.com/', |
| 194 |
} |
| 195 |
); |
| 196 |
|
| 197 |
my $stderr; |
| 198 |
{ |
| 199 |
local *STDERR; |
| 200 |
open STDERR, '>', \$stderr; |
| 201 |
eval { $h->store( { cfgNum => 7, test => 'split' } ); }; |
| 202 |
} |
| 203 |
|
| 204 |
like( |
| 205 |
$stderr, |
| 206 |
qr/Multiple leaders detected/, |
| 207 |
'Split-brain warning logged' |
| 208 |
); |
| 209 |
}; |
| 210 |
|
| 211 |
subtest 'Leader health state validation' => sub { |
| 212 |
plan tests => 3; |
| 213 |
|
| 214 |
# Non-running state should be rejected |
| 215 |
$patroni_response = <<'EOT'; |
| 216 |
{ |
| 217 |
"members": [ |
| 218 |
{ |
| 219 |
"role":"leader", |
| 220 |
"host":"127.0.0.50", |
| 221 |
"port":"5432", |
| 222 |
"state":"starting" |
| 223 |
} |
| 224 |
] |
| 225 |
} |
| 226 |
EOT |
| 227 |
setup_patroni_mock(); |
| 228 |
|
| 229 |
$h = new Lemonldap::NG::Common::Conf( { |
| 230 |
type => 'Patroni', |
| 231 |
dbiChain => "DBI:SQLite:dbname=$db", |
| 232 |
dbiUser => '', |
| 233 |
dbiPassword => '', |
| 234 |
patroniUrl => 'http://db.com/', |
| 235 |
} |
| 236 |
); |
| 237 |
|
| 238 |
my $stderr; |
| 239 |
{ |
| 240 |
local *STDERR; |
| 241 |
open STDERR, '>', \$stderr; |
| 242 |
eval { $h->store( { cfgNum => 8, test => 'starting' } ); }; |
| 243 |
} |
| 244 |
|
| 245 |
like( |
| 246 |
$stderr, |
| 247 |
qr/not in running state/, |
| 248 |
'Non-running leader state warning logged' |
| 249 |
); |
| 250 |
|
| 251 |
# Running state should be accepted |
| 252 |
$patroni_response = <<'EOT'; |
| 253 |
{ |
| 254 |
"members": [ |
| 255 |
{ |
| 256 |
"role":"leader", |
| 257 |
"host":"127.0.0.60", |
| 258 |
"port":"5432", |
| 259 |
"state":"running" |
| 260 |
} |
| 261 |
] |
| 262 |
} |
| 263 |
EOT |
| 264 |
setup_patroni_mock(); |
| 265 |
|
| 266 |
$h = new Lemonldap::NG::Common::Conf( { |
| 267 |
type => 'Patroni', |
| 268 |
dbiChain => "DBI:SQLite:dbname=$db", |
| 269 |
dbiUser => '', |
| 270 |
dbiPassword => '', |
| 271 |
patroniUrl => 'http://db.com/', |
| 272 |
} |
| 273 |
); |
| 274 |
|
| 275 |
eval { $h->store( { cfgNum => 9, test => 'running_state' } ); }; |
| 276 |
like( |
| 277 |
$h->{dbiChain}, |
| 278 |
qr/host=127\.0\.0\.60;port=5432/, |
| 279 |
'Leader with running state accepted' |
| 280 |
); |
| 281 |
|
| 282 |
# Missing state field (backward compatibility) |
| 283 |
$patroni_response = <<'EOT'; |
| 284 |
{ |
| 285 |
"members": [ |
| 286 |
{ |
| 287 |
"role":"leader", |
| 288 |
"host":"127.0.0.70", |
| 289 |
"port":"5432" |
| 290 |
} |
| 291 |
] |
| 292 |
} |
| 293 |
EOT |
| 294 |
setup_patroni_mock(); |
| 295 |
|
| 296 |
$h = new Lemonldap::NG::Common::Conf( { |
| 297 |
type => 'Patroni', |
| 298 |
dbiChain => "DBI:SQLite:dbname=$db", |
| 299 |
dbiUser => '', |
| 300 |
dbiPassword => '', |
| 301 |
patroniUrl => 'http://db.com/', |
| 302 |
} |
| 303 |
); |
| 304 |
|
| 305 |
eval { $h->store( { cfgNum => 10, test => 'no_state' } ); }; |
| 306 |
like( |
| 307 |
$h->{dbiChain}, |
| 308 |
qr/host=127\.0\.0\.70;port=5432/, |
| 309 |
'Leader without state field accepted (backward compat)' |
| 310 |
); |
| 311 |
}; |
| 312 |
|
| 313 |
} |
| 314 |
|
| 315 |
done_testing(); |