| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Copyright (c) 2011-2017 FastMail Pty Ltd. All rights reserved. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without |
| 6 |
# modification, are permitted provided that the following conditions |
| 7 |
# are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright |
| 10 |
# notice, this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
# notice, this list of conditions and the following disclaimer in |
| 14 |
# the documentation and/or other materials provided with the |
| 15 |
# distribution. |
| 16 |
# |
| 17 |
# 3. The name "Fastmail Pty Ltd" must not be used to |
| 18 |
# endorse or promote products derived from this software without |
| 19 |
# prior written permission. For permission or any legal |
| 20 |
# details, please contact |
| 21 |
# FastMail Pty Ltd |
| 22 |
# PO Box 234 |
| 23 |
# Collins St West 8007 |
| 24 |
# Victoria |
| 25 |
# Australia |
| 26 |
# |
| 27 |
# 4. Redistributions of any form whatsoever must retain the following |
| 28 |
# acknowledgment: |
| 29 |
# "This product includes software developed by Fastmail Pty. Ltd." |
| 30 |
# |
| 31 |
# FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 32 |
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 33 |
# EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT |
| 34 |
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF |
| 35 |
# USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 36 |
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 37 |
# OF THIS SOFTWARE. |
| 38 |
# |
| 39 |
|
| 40 |
package Cassandane::Cyrus::Master; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
use File::stat; |
| 44 |
use POSIX qw(getcwd); |
| 45 |
use DateTime; |
| 46 |
|
| 47 |
use lib '.'; |
| 48 |
use base qw(Cassandane::Cyrus::TestCase); |
| 49 |
use Cassandane::Util::Log; |
| 50 |
use Cassandane::Util::Wait; |
| 51 |
use Cassandane::Util::Socket; |
| 52 |
use Cassandane::Util::Sample; |
| 53 |
use Cassandane::Util::Metronome; |
| 54 |
use Cassandane::Instance; |
| 55 |
use Cassandane::Service; |
| 56 |
use Cassandane::Config; |
| 57 |
use Time::HiRes qw(sleep); |
| 58 |
|
| 59 |
my $lemming_bin = getcwd() . '/utils/lemming'; |
| 60 |
|
| 61 |
sub new |
| 62 |
{ |
| 63 |
my $class = shift; |
| 64 |
my $self = $class->SUPER::new({ instance => 0 }, @_); |
| 65 |
|
| 66 |
return $self; |
| 67 |
} |
| 68 |
|
| 69 |
sub set_up |
| 70 |
{ |
| 71 |
my ($self) = @_; |
| 72 |
die "No lemming binary $lemming_bin. Did you run \"make\" in the Cassandane directory?" |
| 73 |
unless (-f $lemming_bin); |
| 74 |
$self->SUPER::set_up(); |
| 75 |
$self->{instance} = Cassandane::Instance->new(setup_mailbox => 0, |
| 76 |
authdaemon => 0); |
| 77 |
} |
| 78 |
|
| 79 |
sub tear_down |
| 80 |
{ |
| 81 |
my ($self) = @_; |
| 82 |
$self->lemming_cull(); |
| 83 |
$self->SUPER::tear_down(); |
| 84 |
} |
| 85 |
|
| 86 |
sub lemming_connect |
| 87 |
{ |
| 88 |
my ($srv, $address_family) = @_; |
| 89 |
|
| 90 |
my $sock = create_client_socket( |
| 91 |
defined($address_family) ? $address_family : $srv->address_family(), |
| 92 |
$srv->host(), $srv->port()) |
| 93 |
or die "Cannot connect to lemming " . $srv->address() . ": $@"; |
| 94 |
|
| 95 |
# The lemming sends us his PID so we can later wait for him to die |
| 96 |
# properly. It's easiest for synchronisation purposes to encode |
| 97 |
# this as a fixed sized field. |
| 98 |
my $pid; |
| 99 |
$sock->sysread($pid, 4) |
| 100 |
or die "Cannot read from lemming: " . $srv->address() . " $!"; |
| 101 |
$pid = unpack("L", $pid); |
| 102 |
die "Cannot read from lemming: $!" |
| 103 |
unless defined $pid; |
| 104 |
|
| 105 |
return { sock => $sock, pid => $pid }; |
| 106 |
} |
| 107 |
|
| 108 |
sub lemming_push |
| 109 |
{ |
| 110 |
my ($lemming, $mode) = @_; |
| 111 |
|
| 112 |
# xlog $self, "Pushing mode=$mode to pid=$lemming->{pid}"; |
| 113 |
|
| 114 |
# Push the lemming over the metaphorical cliff. |
| 115 |
$lemming->{sock}->syswrite($mode . "\r\n"); |
| 116 |
$lemming->{sock}->close(); |
| 117 |
|
| 118 |
# Wait for the master process to wake up and reap the lemming. |
| 119 |
return timed_wait(sub { kill(0, $lemming->{pid}) == 0 }, |
| 120 |
description => "master to reap lemming $lemming->{pid}"); |
| 121 |
} |
| 122 |
|
| 123 |
sub lemming_census |
| 124 |
{ |
| 125 |
my ($self) = @_; |
| 126 |
my $coresdir = $self->{instance}->{basedir} . '/conf/cores'; |
| 127 |
|
| 128 |
my %pids; |
| 129 |
opendir LEMM,$coresdir |
| 130 |
or die "cannot open $coresdir for reading: $!"; |
| 131 |
while ($_ = readdir LEMM) |
| 132 |
{ |
| 133 |
my ($tag, $pid) = m/^lemming\.(\w+).(\d+)$/; |
| 134 |
next |
| 135 |
unless defined $pid; |
| 136 |
xlog $self, "found lemming tag=$tag pid=$pid"; |
| 137 |
$pids{$tag} = [] |
| 138 |
unless defined $pids{$tag}; |
| 139 |
push (@{$pids{$tag}}, $pid); |
| 140 |
} |
| 141 |
closedir LEMM; |
| 142 |
|
| 143 |
my %actual; |
| 144 |
foreach my $tag (keys %pids) |
| 145 |
{ |
| 146 |
my $ntotal = scalar @{$pids{$tag}}; |
| 147 |
my $nlive = kill(0, @{$pids{$tag}}); |
| 148 |
$actual{$tag} = { |
| 149 |
live => $nlive, |
| 150 |
dead => $ntotal - $nlive, |
| 151 |
}; |
| 152 |
} |
| 153 |
return \%actual; |
| 154 |
} |
| 155 |
|
| 156 |
sub lemming_cull |
| 157 |
{ |
| 158 |
my ($self) = @_; |
| 159 |
return unless defined $self->{instance}; |
| 160 |
my $coresdir = $self->{instance}->{basedir} . '/conf/cores'; |
| 161 |
|
| 162 |
my $culled; |
| 163 |
|
| 164 |
return unless -d $coresdir; |
| 165 |
opendir LEMM,$coresdir |
| 166 |
or die "cannot open $coresdir for reading: $!"; |
| 167 |
while ($_ = readdir LEMM) |
| 168 |
{ |
| 169 |
my ($tag, $pid) = m/^lemming\.(\w+).(\d+)$/; |
| 170 |
next |
| 171 |
unless defined $pid; |
| 172 |
if (kill(9, $pid)) { |
| 173 |
xlog $self, "culled lemming tag=$tag pid=$pid"; |
| 174 |
$culled++; |
| 175 |
} |
| 176 |
} |
| 177 |
closedir LEMM; |
| 178 |
|
| 179 |
return $culled; |
| 180 |
} |
| 181 |
|
| 182 |
sub _lemming_args |
| 183 |
{ |
| 184 |
my (%params) = @_; |
| 185 |
|
| 186 |
my $tag = delete $params{tag} || 'A'; |
| 187 |
my $mode = delete $params{mode} || 'serve'; |
| 188 |
my $delay = delete $params{delay}; |
| 189 |
|
| 190 |
my @argv = ( $lemming_bin, '-t', $tag, '-m', $mode ); |
| 191 |
push(@argv, '-d', $delay) if defined $delay; |
| 192 |
|
| 193 |
return (name => $tag, argv => \@argv, %params); |
| 194 |
} |
| 195 |
|
| 196 |
sub lemming_service |
| 197 |
{ |
| 198 |
my ($self, %params) = @_; |
| 199 |
return $self->{instance}->add_service(_lemming_args(%params)); |
| 200 |
} |
| 201 |
|
| 202 |
sub lemming_daemon |
| 203 |
{ |
| 204 |
my ($self, %params) = @_; |
| 205 |
return $self->{instance}->add_daemon(_lemming_args(%params)); |
| 206 |
} |
| 207 |
|
| 208 |
sub lemming_start |
| 209 |
{ |
| 210 |
my ($self, %params) = @_; |
| 211 |
return $self->{instance}->add_start(_lemming_args(%params)); |
| 212 |
} |
| 213 |
|
| 214 |
sub lemming_event |
| 215 |
{ |
| 216 |
my ($self, %params) = @_; |
| 217 |
return $self->{instance}->add_event(_lemming_args(%params)); |
| 218 |
} |
| 219 |
|
| 220 |
sub lemming_wait |
| 221 |
{ |
| 222 |
my ($self, %expected_census) = @_; |
| 223 |
|
| 224 |
timed_wait( |
| 225 |
sub |
| 226 |
{ |
| 227 |
my $census = $self->lemming_census(); |
| 228 |
map { |
| 229 |
my $service_name = $_; |
| 230 |
return 0 if !defined $census->{$service_name}; |
| 231 |
my $expected = $expected_census{$service_name}; |
| 232 |
map { |
| 233 |
return 0 if $census->{$service_name}->{$_} != $expected->{$_}; |
| 234 |
} keys %$expected; |
| 235 |
} keys %expected_census; |
| 236 |
return 1; |
| 237 |
}, |
| 238 |
description => "lemmings to reach the expected census"); |
| 239 |
} |
| 240 |
|
| 241 |
sub start |
| 242 |
{ |
| 243 |
my ($self) = @_; |
| 244 |
$self->{instance}->start(); |
| 245 |
} |
| 246 |
|
| 247 |
# |
| 248 |
# Test a single running programs in SERVICES |
| 249 |
# |
| 250 |
sub test_service |
| 251 |
{ |
| 252 |
my ($self) = @_; |
| 253 |
|
| 254 |
xlog $self, "single successful service"; |
| 255 |
my $srv = $self->lemming_service(); |
| 256 |
$self->start(); |
| 257 |
|
| 258 |
xlog $self, "not preforked, so no lemmings running yet"; |
| 259 |
$self->assert_deep_equals({}, |
| 260 |
$self->lemming_census()); |
| 261 |
|
| 262 |
my $lemm = lemming_connect($srv); |
| 263 |
|
| 264 |
xlog $self, "connected so one lemming forked"; |
| 265 |
$self->assert_deep_equals({ A => { live => 1, dead => 0 } }, |
| 266 |
$self->lemming_census()); |
| 267 |
|
| 268 |
lemming_push($lemm, 'success'); |
| 269 |
|
| 270 |
xlog $self, "no more live lemmings"; |
| 271 |
$self->assert_deep_equals({ A => { live => 0, dead => 1 } }, |
| 272 |
$self->lemming_census()); |
| 273 |
} |
| 274 |
|
| 275 |
# |
| 276 |
# Test multiple connections to a single running program in SERVICES |
| 277 |
# |
| 278 |
sub test_multi_connections |
| 279 |
{ |
| 280 |
my ($self) = @_; |
| 281 |
|
| 282 |
xlog $self, "multiple connections to a single successful service"; |
| 283 |
my $srv = $self->lemming_service(); |
| 284 |
$self->start(); |
| 285 |
|
| 286 |
xlog $self, "not preforked, so no lemmings running yet"; |
| 287 |
$self->assert_deep_equals({}, |
| 288 |
$self->lemming_census()); |
| 289 |
|
| 290 |
my $lemm1 = lemming_connect($srv); |
| 291 |
|
| 292 |
xlog $self, "connected so one lemming forked"; |
| 293 |
$self->assert_deep_equals({ A => { live => 1, dead => 0 } }, |
| 294 |
$self->lemming_census()); |
| 295 |
|
| 296 |
my $lemm2 = lemming_connect($srv); |
| 297 |
|
| 298 |
xlog $self, "two connected so two lemmings forked"; |
| 299 |
$self->assert_deep_equals({ A => { live => 2, dead => 0 } }, |
| 300 |
$self->lemming_census()); |
| 301 |
|
| 302 |
my $lemm3 = lemming_connect($srv); |
| 303 |
|
| 304 |
xlog $self, "three connected so three lemmings forked"; |
| 305 |
$self->assert_deep_equals({ A => { live => 3, dead => 0 } }, |
| 306 |
$self->lemming_census()); |
| 307 |
|
| 308 |
lemming_push($lemm1, 'success'); |
| 309 |
lemming_push($lemm2, 'success'); |
| 310 |
lemming_push($lemm3, 'success'); |
| 311 |
|
| 312 |
xlog $self, "no more live lemmings"; |
| 313 |
$self->assert_deep_equals({ A => { live => 0, dead => 3 } }, |
| 314 |
$self->lemming_census()); |
| 315 |
} |
| 316 |
|
| 317 |
# |
| 318 |
# Test multiple running programs in SERVICES |
| 319 |
# |
| 320 |
sub test_multi_services |
| 321 |
{ |
| 322 |
my ($self) = @_; |
| 323 |
|
| 324 |
xlog $self, "multiple successful services"; |
| 325 |
my $srvA = $self->lemming_service(tag => 'A'); |
| 326 |
my $srvB = $self->lemming_service(tag => 'B'); |
| 327 |
my $srvC = $self->lemming_service(tag => 'C'); |
| 328 |
$self->start(); |
| 329 |
|
| 330 |
xlog $self, "not preforked, so no lemmings running yet"; |
| 331 |
$self->assert_deep_equals({}, |
| 332 |
$self->lemming_census()); |
| 333 |
|
| 334 |
my $lemmA = lemming_connect($srvA); |
| 335 |
|
| 336 |
xlog $self, "connected so one lemming forked"; |
| 337 |
$self->assert_deep_equals({ A => { live => 1, dead => 0 } }, |
| 338 |
$self->lemming_census()); |
| 339 |
|
| 340 |
my $lemmB = lemming_connect($srvB); |
| 341 |
|
| 342 |
xlog $self, "two connected so two lemmings forked"; |
| 343 |
$self->assert_deep_equals({ |
| 344 |
A => { live => 1, dead => 0 }, |
| 345 |
B => { live => 1, dead => 0 }, |
| 346 |
}, $self->lemming_census()); |
| 347 |
|
| 348 |
my $lemmC = lemming_connect($srvC); |
| 349 |
|
| 350 |
xlog $self, "three connected so three lemmings forked"; |
| 351 |
$self->assert_deep_equals({ |
| 352 |
A => { live => 1, dead => 0 }, |
| 353 |
B => { live => 1, dead => 0 }, |
| 354 |
C => { live => 1, dead => 0 }, |
| 355 |
}, $self->lemming_census()); |
| 356 |
|
| 357 |
lemming_push($lemmA, 'success'); |
| 358 |
lemming_push($lemmB, 'success'); |
| 359 |
lemming_push($lemmC, 'success'); |
| 360 |
|
| 361 |
xlog $self, "no more live lemmings"; |
| 362 |
$self->assert_deep_equals({ |
| 363 |
A => { live => 0, dead => 1 }, |
| 364 |
B => { live => 0, dead => 1 }, |
| 365 |
C => { live => 0, dead => 1 }, |
| 366 |
}, $self->lemming_census()); |
| 367 |
} |
| 368 |
|
| 369 |
# |
| 370 |
# Test a preforked single running program in SERVICES |
| 371 |
# |
| 372 |
sub test_prefork |
| 373 |
{ |
| 374 |
my ($self) = @_; |
| 375 |
|
| 376 |
xlog $self, "single successful service"; |
| 377 |
my $srv = $self->lemming_service(prefork => 1); |
| 378 |
$self->start(); |
| 379 |
$self->lemming_wait(A => { live => 1 }); |
| 380 |
|
| 381 |
xlog $self, "preforked, so one lemming running already"; |
| 382 |
$self->assert_deep_equals({ A => { live => 1, dead => 0 } }, |
| 383 |
$self->lemming_census()); |
| 384 |
|
| 385 |
my $lemm1 = lemming_connect($srv); |
| 386 |
$self->lemming_wait(A => { live => 2 }); |
| 387 |
|
| 388 |
xlog $self, "connected so one lemming forked"; |
| 389 |
$self->assert_deep_equals({ A => { live => 2, dead => 0 } }, |
| 390 |
$self->lemming_census()); |
| 391 |
|
| 392 |
my $lemm2 = lemming_connect($srv); |
| 393 |
$self->lemming_wait(A => { live => 3 }); |
| 394 |
|
| 395 |
xlog $self, "connected again so two additional lemmings forked"; |
| 396 |
$self->assert_deep_equals({ A => { live => 3, dead => 0 } }, |
| 397 |
$self->lemming_census()); |
| 398 |
|
| 399 |
lemming_push($lemm1, 'success'); |
| 400 |
lemming_push($lemm2, 'success'); |
| 401 |
|
| 402 |
xlog $self, "always at least one live lemming"; |
| 403 |
$self->assert_deep_equals({ A => { live => 1, dead => 2 } }, |
| 404 |
$self->lemming_census()); |
| 405 |
} |
| 406 |
|
| 407 |
# |
| 408 |
# Test multiple running programs in SERVICES, some preforked. |
| 409 |
# |
| 410 |
sub test_multi_prefork |
| 411 |
{ |
| 412 |
my ($self) = @_; |
| 413 |
|
| 414 |
xlog $self, "multiple successful service some preforked"; |
| 415 |
my $srvA = $self->lemming_service(tag => 'A', prefork => 2); |
| 416 |
my $srvB = $self->lemming_service(tag => 'B'); # no preforking |
| 417 |
my $srvC = $self->lemming_service(tag => 'C', prefork => 3); |
| 418 |
$self->start(); |
| 419 |
|
| 420 |
# wait for lemmings to be preforked |
| 421 |
$self->lemming_wait(A => { live => 2 }, C => { live => 3 }); |
| 422 |
|
| 423 |
my @lemmings; |
| 424 |
my $lemm; |
| 425 |
|
| 426 |
xlog $self, "connect to A once"; |
| 427 |
$lemm = lemming_connect($srvA); |
| 428 |
$self->lemming_wait(A => { live => 3 }); |
| 429 |
push(@lemmings, $lemm); |
| 430 |
$self->assert_deep_equals({ |
| 431 |
A => { live => 3, dead => 0 }, |
| 432 |
C => { live => 3, dead => 0 }, |
| 433 |
}, $self->lemming_census()); |
| 434 |
|
| 435 |
xlog $self, "connect to A again"; |
| 436 |
$lemm = lemming_connect($srvA); |
| 437 |
$self->lemming_wait(A => { live => 4 }); |
| 438 |
push(@lemmings, $lemm); |
| 439 |
$self->assert_deep_equals({ |
| 440 |
A => { live => 4, dead => 0 }, |
| 441 |
C => { live => 3, dead => 0 }, |
| 442 |
}, $self->lemming_census()); |
| 443 |
|
| 444 |
xlog $self, "connect to A a third time"; |
| 445 |
$lemm = lemming_connect($srvA); |
| 446 |
$self->lemming_wait(A => { live => 5 }); |
| 447 |
push(@lemmings, $lemm); |
| 448 |
$self->assert_deep_equals({ |
| 449 |
A => { live => 5, dead => 0 }, |
| 450 |
C => { live => 3, dead => 0 }, |
| 451 |
}, $self->lemming_census()); |
| 452 |
|
| 453 |
xlog $self, "connect to B"; |
| 454 |
$lemm = lemming_connect($srvB); |
| 455 |
push(@lemmings, $lemm); |
| 456 |
$self->assert_deep_equals({ |
| 457 |
A => { live => 5, dead => 0 }, |
| 458 |
B => { live => 1, dead => 0 }, |
| 459 |
C => { live => 3, dead => 0 }, |
| 460 |
}, $self->lemming_census()); |
| 461 |
|
| 462 |
foreach $lemm (@lemmings) |
| 463 |
{ |
| 464 |
lemming_push($lemm, 'success'); |
| 465 |
} |
| 466 |
|
| 467 |
xlog $self, "our lemmings are gone, others have replaced them"; |
| 468 |
$self->assert_deep_equals({ |
| 469 |
A => { live => 2, dead => 3 }, |
| 470 |
B => { live => 0, dead => 1 }, |
| 471 |
C => { live => 3, dead => 0 }, |
| 472 |
}, $self->lemming_census()); |
| 473 |
} |
| 474 |
|
| 475 |
# |
| 476 |
# Test a single program in SERVICES which fails after connect |
| 477 |
# |
| 478 |
sub test_exit_after_connect |
| 479 |
{ |
| 480 |
my ($self) = @_; |
| 481 |
|
| 482 |
xlog $self, "single service will exit after connect"; |
| 483 |
my $srv = $self->lemming_service(); |
| 484 |
$self->start(); |
| 485 |
|
| 486 |
xlog $self, "not preforked, so no lemmings running yet"; |
| 487 |
$self->assert_deep_equals({}, |
| 488 |
$self->lemming_census()); |
| 489 |
|
| 490 |
my $lemm = lemming_connect($srv); |
| 491 |
|
| 492 |
xlog $self, "connected so one lemming forked"; |
| 493 |
$self->assert_deep_equals({ A => { live => 1, dead => 0 } }, |
| 494 |
$self->lemming_census()); |
| 495 |
|
| 496 |
xlog $self, "push the lemming off the cliff"; |
| 497 |
lemming_push($lemm, 'exit'); |
| 498 |
$self->assert_deep_equals({ A => { live => 0, dead => 1 } }, |
| 499 |
$self->lemming_census()); |
| 500 |
|
| 501 |
xlog $self, "can connect again"; |
| 502 |
$lemm = lemming_connect($srv); |
| 503 |
$self->assert_deep_equals({ A => { live => 1, dead => 1 } }, |
| 504 |
$self->lemming_census()); |
| 505 |
|
| 506 |
xlog $self, "push the lemming off the cliff"; |
| 507 |
lemming_push($lemm, 'exit'); |
| 508 |
$self->assert_deep_equals({ A => { live => 0, dead => 2 } }, |
| 509 |
$self->lemming_census()); |
| 510 |
} |
| 511 |
|
| 512 |
# |
| 513 |
# Test a single program in SERVICES which fails during startup |
| 514 |
# |
| 515 |
sub test_service_exit_during_start |
| 516 |
{ |
| 517 |
my ($self) = @_; |
| 518 |
my $lemm; |
| 519 |
|
| 520 |
xlog $self, "single service will exit during startup"; |
| 521 |
my $srv = $self->lemming_service(mode => 'exit', delay => 100); |
| 522 |
$self->start(); |
| 523 |
|
| 524 |
xlog $self, "not preforked, so no lemmings running yet"; |
| 525 |
$self->assert_deep_equals({}, |
| 526 |
$self->lemming_census()); |
| 527 |
|
| 528 |
xlog $self, "connection fails due to dead lemming"; |
| 529 |
eval |
| 530 |
{ |
| 531 |
$lemm = lemming_connect($srv); |
| 532 |
}; |
| 533 |
$self->assert_null($lemm); |
| 534 |
|
| 535 |
xlog $self, "expect 5 dead lemmings"; |
| 536 |
$self->assert_deep_equals({ A => { live => 0, dead => 5 } }, |
| 537 |
$self->lemming_census()); |
| 538 |
|
| 539 |
xlog $self, "connections should fail because service disabled"; |
| 540 |
eval |
| 541 |
{ |
| 542 |
$lemm = lemming_connect($srv); |
| 543 |
}; |
| 544 |
$self->assert_null($lemm); |
| 545 |
$self->assert_deep_equals({ A => { live => 0, dead => 5 } }, |
| 546 |
$self->lemming_census()); |
| 547 |
|
| 548 |
if ($self->{instance}->{have_syslog_replacement}) { |
| 549 |
xlog $self, "check that the error was syslogged"; |
| 550 |
my @lines = $self->{instance}->getsyslog(qr/too many failures for service/); |
| 551 |
$self->assert_num_equals(1, scalar @lines); |
| 552 |
$self->assert_matches(qr/disabling until next SIGHUP/, $lines[0]); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
sub test_startup |
| 557 |
{ |
| 558 |
my ($self) = @_; |
| 559 |
|
| 560 |
xlog $self, "Test a program in the START section"; |
| 561 |
$self->lemming_start(tag => 'A', delay => 100, mode => 'success'); |
| 562 |
$self->lemming_start(tag => 'B', delay => 200, mode => 'success'); |
| 563 |
# This service won't be used |
| 564 |
my $srv = $self->lemming_service(tag => 'C'); |
| 565 |
$self->start(); |
| 566 |
|
| 567 |
xlog $self, "expect 2 dead lemmings"; |
| 568 |
$self->assert_deep_equals({ |
| 569 |
A => { live => 0, dead => 1 }, |
| 570 |
B => { live => 0, dead => 1 }, |
| 571 |
}, $self->lemming_census()); |
| 572 |
} |
| 573 |
|
| 574 |
sub test_startup_exits |
| 575 |
{ |
| 576 |
my ($self) = @_; |
| 577 |
|
| 578 |
xlog $self, "Test a program in the START section which fails"; |
| 579 |
$self->lemming_start(tag => 'A', delay => 100, mode => 'exit'); |
| 580 |
$self->lemming_start(tag => 'B', delay => 200, mode => 'exit'); |
| 581 |
# This service won't be used |
| 582 |
my $srv = $self->lemming_service(tag => 'C'); |
| 583 |
eval |
| 584 |
{ |
| 585 |
$self->start(); |
| 586 |
}; |
| 587 |
xlog $self, "start failed (as expected): $@" if $@; |
| 588 |
|
| 589 |
xlog $self, "master should have exited when first startup failed"; |
| 590 |
$self->assert(!$self->{instance}->is_running()); |
| 591 |
|
| 592 |
xlog $self, "expect 1 dead lemming"; |
| 593 |
$self->assert_deep_equals({ |
| 594 |
A => { live => 0, dead => 1 }, |
| 595 |
}, $self->lemming_census()); |
| 596 |
} |
| 597 |
|
| 598 |
# TODO: test exit during startup with prefork= |
| 599 |
|
| 600 |
sub test_service_ipv6 |
| 601 |
{ |
| 602 |
my ($self) = @_; |
| 603 |
|
| 604 |
xlog $self, "single successful service on IPv6"; |
| 605 |
my $srv = $self->lemming_service(host => '::1'); |
| 606 |
$self->start(); |
| 607 |
|
| 608 |
xlog $self, "not preforked, so no lemmings running yet"; |
| 609 |
$self->assert_deep_equals({}, |
| 610 |
$self->lemming_census()); |
| 611 |
|
| 612 |
my $lemm = lemming_connect($srv); |
| 613 |
|
| 614 |
xlog $self, "connected so one lemming forked"; |
| 615 |
$self->assert_deep_equals({ A => { live => 1, dead => 0 } }, |
| 616 |
$self->lemming_census()); |
| 617 |
|
| 618 |
lemming_push($lemm, 'success'); |
| 619 |
|
| 620 |
xlog $self, "no more live lemmings"; |
| 621 |
$self->assert_deep_equals({ A => { live => 0, dead => 1 } }, |
| 622 |
$self->lemming_census()); |
| 623 |
} |
| 624 |
|
| 625 |
sub test_service_unix |
| 626 |
{ |
| 627 |
my ($self) = @_; |
| 628 |
|
| 629 |
xlog $self, "single successful service on UNIX domain socket"; |
| 630 |
my $srv = $self->lemming_service( |
| 631 |
host => undef, |
| 632 |
port => '@basedir@/conf/socket/lemming.sock'); |
| 633 |
$self->start(); |
| 634 |
|
| 635 |
xlog $self, "not preforked, so no lemmings running yet"; |
| 636 |
$self->assert_deep_equals({}, |
| 637 |
$self->lemming_census()); |
| 638 |
|
| 639 |
my $lemm = lemming_connect($srv); |
| 640 |
|
| 641 |
xlog $self, "connected so one lemming forked"; |
| 642 |
$self->assert_deep_equals({ A => { live => 1, dead => 0 } }, |
| 643 |
$self->lemming_census()); |
| 644 |
|
| 645 |
lemming_push($lemm, 'success'); |
| 646 |
|
| 647 |
xlog $self, "no more live lemmings"; |
| 648 |
$self->assert_deep_equals({ A => { live => 0, dead => 1 } }, |
| 649 |
$self->lemming_census()); |
| 650 |
} |
| 651 |
|
| 652 |
sub test_service_nohost |
| 653 |
{ |
| 654 |
my ($self) = @_; |
| 655 |
|
| 656 |
xlog $self, "single successful service with a port-only listen="; |
| 657 |
my $srv = $self->lemming_service(host => undef); |
| 658 |
$self->start(); |
| 659 |
|
| 660 |
xlog $self, "not preforked, so no lemmings running yet"; |
| 661 |
$self->assert_deep_equals({}, |
| 662 |
$self->lemming_census()); |
| 663 |
|
| 664 |
my $lemm = lemming_connect($srv); |
| 665 |
|
| 666 |
xlog $self, "connected so one lemming forked"; |
| 667 |
$self->assert_deep_equals({ A => { live => 1, dead => 0 } }, |
| 668 |
$self->lemming_census()); |
| 669 |
|
| 670 |
lemming_push($lemm, 'success'); |
| 671 |
|
| 672 |
xlog $self, "no more live lemmings"; |
| 673 |
$self->assert_deep_equals({ A => { live => 0, dead => 1 } }, |
| 674 |
$self->lemming_census()); |
| 675 |
} |
| 676 |
|
| 677 |
sub test_service_dup_port |
| 678 |
{ |
| 679 |
my ($self) = @_; |
| 680 |
|
| 681 |
xlog $self, "successful two services with listen= "; |
| 682 |
xlog $self, "parameters which reference the same IPv4 port"; |
| 683 |
my $srvA = $self->lemming_service(tag => 'A'); |
| 684 |
my $srvB = $self->lemming_service(tag => 'B', |
| 685 |
port => $srvA->port()); |
| 686 |
|
| 687 |
# master should emit a syslog message like this |
| 688 |
# |
| 689 |
# Dec 31 14:40:57 enki 0340541/master[26085]: unable to create B |
| 690 |
# listener socket: Address already in use |
| 691 |
# |
| 692 |
# and struggle on. |
| 693 |
$self->start(); |
| 694 |
|
| 695 |
if ($self->{instance}->{have_syslog_replacement}) { |
| 696 |
# check syslog for the expected error |
| 697 |
my $pat = qr/unable to create (?:A|B) listener socket:/; |
| 698 |
my @lines = $self->{instance}->getsyslog($pat); |
| 699 |
$self->assert_num_equals(1, scalar @lines); |
| 700 |
$self->assert_matches(qr/Address already in use/, $lines[0]); |
| 701 |
} |
| 702 |
|
| 703 |
xlog $self, "not preforked, so no lemmings running yet"; |
| 704 |
$self->assert_deep_equals({}, |
| 705 |
$self->lemming_census()); |
| 706 |
|
| 707 |
my $lemmA = lemming_connect($srvA); |
| 708 |
|
| 709 |
my $census = $self->lemming_census(); |
| 710 |
my ($winner) = keys %$census; # either could be the one that runs |
| 711 |
xlog $self, "connected so one lemming forked"; |
| 712 |
$self->assert_deep_equals({ $winner => { live => 1, dead => 0 } }, |
| 713 |
$self->lemming_census()); |
| 714 |
|
| 715 |
my $lemmB = lemming_connect($srvB); |
| 716 |
|
| 717 |
xlog $self, "the port is owned by service A"; |
| 718 |
$self->assert_deep_equals({ $winner => { live => 2, dead => 0 } }, |
| 719 |
$self->lemming_census()); |
| 720 |
|
| 721 |
lemming_push($lemmA, 'success'); |
| 722 |
lemming_push($lemmB, 'success'); |
| 723 |
|
| 724 |
xlog $self, "no more live lemmings"; |
| 725 |
$self->assert_deep_equals({ $winner => { live => 0, dead => 2 } }, |
| 726 |
$self->lemming_census()); |
| 727 |
} |
| 728 |
|
| 729 |
sub test_service_noexe |
| 730 |
{ |
| 731 |
my ($self) = @_; |
| 732 |
|
| 733 |
xlog $self, "single service with a non-existant executable"; |
| 734 |
my $srvA = $self->lemming_service(tag => 'A'); |
| 735 |
my $srvB = $self->{instance}->add_service( |
| 736 |
name => 'B', |
| 737 |
argv => ['/usr/bin/no-such-exe','--foo','--bar']); |
| 738 |
|
| 739 |
# master should exit while adding services, with a message |
| 740 |
# to syslog like this |
| 741 |
# |
| 742 |
# Dec 31 15:03:26 enki 0403231/master[26825]: cannot find executable |
| 743 |
# for service 'B' |
| 744 |
eval |
| 745 |
{ |
| 746 |
$self->start(); |
| 747 |
}; |
| 748 |
xlog $self, "start failed (as expected): $@" if $@; |
| 749 |
|
| 750 |
# XXX can't currently check syslog in this case because start() bailed |
| 751 |
# XXX out before we would have started reading it... |
| 752 |
|
| 753 |
xlog $self, "master should have exited when service verification failed"; |
| 754 |
$self->assert(!$self->{instance}->is_running()); |
| 755 |
} |
| 756 |
|
| 757 |
sub test_reap_rate |
| 758 |
{ |
| 759 |
my ($self) = @_; |
| 760 |
|
| 761 |
xlog $self, "Testing latency after which cyrus reaps dead children"; |
| 762 |
|
| 763 |
my $max_latency = 1.0; # seconds |
| 764 |
|
| 765 |
my $srv = $self->lemming_service(tag => 'A'); |
| 766 |
$self->start(); |
| 767 |
|
| 768 |
xlog $self, "not preforked, so no lemmings running yet"; |
| 769 |
$self->assert_deep_equals({}, |
| 770 |
$self->lemming_census()); |
| 771 |
|
| 772 |
xlog $self, "Build a vast flock of lemmings"; |
| 773 |
my @lemmings; |
| 774 |
for (1..100) |
| 775 |
{ |
| 776 |
push(@lemmings, lemming_connect($srv)); |
| 777 |
} |
| 778 |
$self->assert_deep_equals({ |
| 779 |
A => { live => 100, dead => 0 }, |
| 780 |
}, $self->lemming_census()); |
| 781 |
|
| 782 |
# This technique avoids having new connections at the |
| 783 |
# same time as we're trying to measure reaping latency, |
| 784 |
# which can hide racy bugs in the main select() loop. |
| 785 |
xlog $self, "Killing all the lemmings one by one"; |
| 786 |
my $ss = new Cassandane::Util::Sample; |
| 787 |
while (my $lemm = shift @lemmings) |
| 788 |
{ |
| 789 |
my $t = lemming_push($lemm, 'success'); |
| 790 |
$self->assert($t < $max_latency, |
| 791 |
"Child reap latency is >= $max_latency sec"); |
| 792 |
$ss->add($t); |
| 793 |
} |
| 794 |
xlog $self, "Reap times: $ss"; |
| 795 |
|
| 796 |
xlog $self, "no more live lemmings"; |
| 797 |
$self->assert_deep_equals({ |
| 798 |
A => { live => 0, dead => 100 }, |
| 799 |
}, $self->lemming_census()); |
| 800 |
} |
| 801 |
|
| 802 |
sub measure_fork_rate |
| 803 |
{ |
| 804 |
my ($self, $srv, $rate) = @_; |
| 805 |
|
| 806 |
my $metronome = Cassandane::Util::Metronome->new(rate => $rate); |
| 807 |
my @lemmings; |
| 808 |
for (1..100) |
| 809 |
{ |
| 810 |
my $lemm = lemming_connect($srv); |
| 811 |
push(@lemmings, $lemm); |
| 812 |
$metronome->tick(); |
| 813 |
} |
| 814 |
|
| 815 |
foreach my $lemm (@lemmings) |
| 816 |
{ |
| 817 |
lemming_push($lemm, 'success'); |
| 818 |
} |
| 819 |
|
| 820 |
return $metronome->actual_rate(); |
| 821 |
} |
| 822 |
|
| 823 |
sub test_maxforkrate |
| 824 |
{ |
| 825 |
my ($self) = @_; |
| 826 |
|
| 827 |
xlog $self, "Testing enforcement of the maxforkrate= parameter"; |
| 828 |
|
| 829 |
# A very loose error factor. We don't care too much if the |
| 830 |
# enforcement is slightly off, it's a rough resource limit and |
| 831 |
# fairness measure not a precise QoS issue. Also, even modest fork |
| 832 |
# rates may be difficult to achieve when running under Valgrind, and |
| 833 |
# we don't want that to cause the test to fail spuriously. |
| 834 |
my $epsilon = 0.2; |
| 835 |
my $fast = 10.0; # forks/sec |
| 836 |
my $slow = 5.0; # forks/sec |
| 837 |
|
| 838 |
my $srvA = $self->lemming_service(tag => 'A'); |
| 839 |
my $srvB = $self->lemming_service(tag => 'B', maxforkrate => int($slow)); |
| 840 |
$self->start(); |
| 841 |
|
| 842 |
xlog $self, "not preforked, so no lemmings running yet"; |
| 843 |
$self->assert_deep_equals({}, |
| 844 |
$self->lemming_census()); |
| 845 |
|
| 846 |
xlog $self, "Test that we can achieve the fast forks rate on the unlimited service"; |
| 847 |
my $r = $self->measure_fork_rate($srvA, $fast); |
| 848 |
xlog $self, "Actual rate: $r"; |
| 849 |
$self->assert($r >= (1.0-$epsilon)*$fast, |
| 850 |
"Fork rate too slow, for $r wanted $fast"); |
| 851 |
$self->assert($r <= (1.0+$epsilon)*$fast, |
| 852 |
"Fork rate too fast, for $r wanted $fast"); |
| 853 |
|
| 854 |
xlog $self, "Test that the fork rate is limited on the limited service"; |
| 855 |
$r = $self->measure_fork_rate($srvB, $fast); |
| 856 |
xlog $self, "Actual rate: $r"; |
| 857 |
$self->assert($r >= (1.0-$epsilon)*$slow, |
| 858 |
"Fork rate too slow, got $r wanted $slow"); |
| 859 |
$self->assert($r <= (1.0+$epsilon)*$slow, |
| 860 |
"Fork rate too fast, got $r wanted $slow"); |
| 861 |
|
| 862 |
xlog $self, "no more live lemmings"; |
| 863 |
$self->assert_deep_equals({ |
| 864 |
A => { live => 0, dead => 100 }, |
| 865 |
B => { live => 0, dead => 100 }, |
| 866 |
}, $self->lemming_census()); |
| 867 |
} |
| 868 |
|
| 869 |
sub test_periodic_event_slow |
| 870 |
{ |
| 871 |
my ($self) = @_; |
| 872 |
|
| 873 |
xlog $self, "Testing regular events"; |
| 874 |
|
| 875 |
my $srv = $self->lemming_service(tag => 'A'); |
| 876 |
# This is the fastest we can schedule events - every 1 minute |
| 877 |
# so in the absence of a per-process time machine our test will |
| 878 |
# need to run for several real minutes. |
| 879 |
$self->lemming_event(tag => 'B', mode => 'success', period => 1); |
| 880 |
$self->start(); |
| 881 |
|
| 882 |
xlog $self, "periodic events run immediately"; |
| 883 |
|
| 884 |
xlog $self, "waiting 5 mins for events to fire, plus some slop"; |
| 885 |
sleep(5*60 + 5); |
| 886 |
|
| 887 |
$self->assert_deep_equals({ |
| 888 |
B => { live => 0, dead => 6 }, |
| 889 |
}, $self->lemming_census()); |
| 890 |
} |
| 891 |
|
| 892 |
sub test_service_bad_name |
| 893 |
{ |
| 894 |
my ($self) = @_; |
| 895 |
|
| 896 |
xlog $self, "services with bad names (Bug 3654)"; |
| 897 |
$self->lemming_service(tag => 'foo'); |
| 898 |
$self->lemming_service(tag => 'foo_bar'); |
| 899 |
$self->lemming_service(tag => 'foo-baz'); |
| 900 |
$self->lemming_service(tag => 'foo&baz'); |
| 901 |
|
| 902 |
# master should exit while adding services, with a message |
| 903 |
# to syslog like this |
| 904 |
# |
| 905 |
# Mar 21 19:53:21 gnb-desktop 0853201/master[8789]: configuration |
| 906 |
# file /var/tmp/cass/0853201/conf/cyrus.conf: bad character '-' in |
| 907 |
# name on line 2 |
| 908 |
# |
| 909 |
eval |
| 910 |
{ |
| 911 |
$self->start(); |
| 912 |
}; |
| 913 |
xlog $self, "start failed (as expected): $@" if $@; |
| 914 |
|
| 915 |
# XXX can't currently check syslog in this case because start() bailed |
| 916 |
# XXX out before we would have started reading it... |
| 917 |
|
| 918 |
xlog $self, "master should have exited when service verification failed"; |
| 919 |
$self->assert(!$self->{instance}->is_running()); |
| 920 |
} |
| 921 |
|
| 922 |
sub test_service_associate |
| 923 |
{ |
| 924 |
my ($self) = @_; |
| 925 |
|
| 926 |
xlog $self, "sending a SIGHUP to a master process with services"; |
| 927 |
xlog $self, "whose listen= parameters give more than one result in"; |
| 928 |
xlog $self, "getaddrinfo(), such as an IPv4 and IPv6 (Bug 3771)"; |
| 929 |
|
| 930 |
my $host = 'localhost'; |
| 931 |
|
| 932 |
$self->lemming_service(tag => 'foo', host => undef); |
| 933 |
|
| 934 |
$self->{instance}->start(); |
| 935 |
$self->{instance}->send_sighup(); |
| 936 |
$self->{instance}->stop(); |
| 937 |
} |
| 938 |
|
| 939 |
sub XXX_test_service_primary_fail |
| 940 |
{ |
| 941 |
my ($self) = @_; |
| 942 |
|
| 943 |
my $host = 'localhost'; |
| 944 |
|
| 945 |
my $srv = $self->lemming_service(tag => 'foo', host => undef, mode => 'exit-ipv4/serve'); |
| 946 |
|
| 947 |
$self->start(); |
| 948 |
|
| 949 |
xlog $self, "connection fails due to dead IPv4 lemming"; |
| 950 |
my $lemm; |
| 951 |
eval |
| 952 |
{ |
| 953 |
$lemm = lemming_connect($srv, 'inet'); |
| 954 |
}; |
| 955 |
$self->assert_null($lemm); |
| 956 |
|
| 957 |
xlog $self, "expect 5 dead lemmings"; |
| 958 |
$self->assert_deep_equals({ foo => { live => 0, dead => 5 } }, |
| 959 |
$self->lemming_census()); |
| 960 |
|
| 961 |
xlog $self, "check the IPv4 service is really dead"; |
| 962 |
eval |
| 963 |
{ |
| 964 |
$lemm = lemming_connect($srv, 'inet'); |
| 965 |
}; |
| 966 |
$self->assert_null($lemm); |
| 967 |
$self->assert_deep_equals({ foo => { live => 0, dead => 5 } }, |
| 968 |
$self->lemming_census()); |
| 969 |
|
| 970 |
xlog $self, "breed one IPv6 lemming"; |
| 971 |
$lemm = lemming_connect($srv, 'inet6'); |
| 972 |
$self->assert_deep_equals({ foo => { live => 1, dead => 5 } }, |
| 973 |
$self->lemming_census()); |
| 974 |
|
| 975 |
lemming_push($lemm, 'success'); |
| 976 |
|
| 977 |
xlog $self, "no more live lemmings"; |
| 978 |
$self->assert_deep_equals({ foo => { live => 0, dead => 6 } }, |
| 979 |
$self->lemming_census()); |
| 980 |
|
| 981 |
xlog $self, "revive the dead IPv4 service"; |
| 982 |
$self->{instance}->send_sighup(); |
| 983 |
|
| 984 |
xlog $self, "connection fails again due to dead IPv4 lemming"; |
| 985 |
$lemm = undef; |
| 986 |
eval |
| 987 |
{ |
| 988 |
$lemm = lemming_connect($srv, 'inet'); |
| 989 |
}; |
| 990 |
$self->assert_null($lemm); |
| 991 |
|
| 992 |
xlog $self, "expect 5 more dead lemmings"; |
| 993 |
$self->assert_deep_equals({ foo => { live => 0, dead => 11 } }, |
| 994 |
$self->lemming_census()); |
| 995 |
} |
| 996 |
|
| 997 |
sub XXX_test_service_associate_fail |
| 998 |
{ |
| 999 |
my ($self) = @_; |
| 1000 |
|
| 1001 |
my $host = 'localhost'; |
| 1002 |
|
| 1003 |
my $srv = $self->lemming_service(tag => 'foo', host => undef, mode => 'exit-ipv6/serve'); |
| 1004 |
|
| 1005 |
$self->start(); |
| 1006 |
|
| 1007 |
xlog $self, "connection fails due to dead IPv6 lemming"; |
| 1008 |
my $lemm; |
| 1009 |
eval |
| 1010 |
{ |
| 1011 |
$lemm = lemming_connect($srv, 'inet6'); |
| 1012 |
}; |
| 1013 |
$self->assert_null($lemm); |
| 1014 |
|
| 1015 |
xlog $self, "expect 5 dead lemmings"; |
| 1016 |
$self->assert_deep_equals({ foo => { live => 0, dead => 5 } }, |
| 1017 |
$self->lemming_census()); |
| 1018 |
|
| 1019 |
xlog $self, "check the IPv6 service is really dead"; |
| 1020 |
eval |
| 1021 |
{ |
| 1022 |
$lemm = lemming_connect($srv, 'inet6'); |
| 1023 |
}; |
| 1024 |
$self->assert_null($lemm); |
| 1025 |
$self->assert_deep_equals({ foo => { live => 0, dead => 5 } }, |
| 1026 |
$self->lemming_census()); |
| 1027 |
|
| 1028 |
xlog $self, "breed one IPv4 lemming"; |
| 1029 |
$lemm = lemming_connect($srv, 'inet'); |
| 1030 |
$self->assert_deep_equals({ foo => { live => 1, dead => 5 } }, |
| 1031 |
$self->lemming_census()); |
| 1032 |
|
| 1033 |
lemming_push($lemm, 'success'); |
| 1034 |
|
| 1035 |
xlog $self, "no more live lemmings"; |
| 1036 |
$self->assert_deep_equals({ foo => { live => 0, dead => 6 } }, |
| 1037 |
$self->lemming_census()); |
| 1038 |
|
| 1039 |
xlog $self, "revive the dead IPv6 service"; |
| 1040 |
$self->{instance}->send_sighup(); |
| 1041 |
|
| 1042 |
xlog $self, "connection fails again due to dead IPv6 lemming"; |
| 1043 |
$lemm = undef; |
| 1044 |
eval |
| 1045 |
{ |
| 1046 |
$lemm = lemming_connect($srv, 'inet6'); |
| 1047 |
}; |
| 1048 |
$self->assert_null($lemm); |
| 1049 |
|
| 1050 |
xlog $self, "expect 5 dead lemmings"; |
| 1051 |
$self->assert_deep_equals({ foo => { live => 0, dead => 11 } }, |
| 1052 |
$self->lemming_census()); |
| 1053 |
} |
| 1054 |
|
| 1055 |
sub test_sighup_recycling |
| 1056 |
{ |
| 1057 |
my ($self) = @_; |
| 1058 |
|
| 1059 |
my $host = 'localhost'; |
| 1060 |
|
| 1061 |
my $srv = $self->lemming_service(tag => 'foo', prefork => 1); |
| 1062 |
$self->start(); |
| 1063 |
$self->lemming_wait(foo => { live => 1 }); |
| 1064 |
|
| 1065 |
xlog $self, "preforked, so one lemming running already"; |
| 1066 |
$self->assert_deep_equals({ foo => { live => 1, dead => 0 } }, |
| 1067 |
$self->lemming_census()); |
| 1068 |
|
| 1069 |
my $lemm = lemming_connect($srv); |
| 1070 |
$self->lemming_wait(foo => { live => 2 }); |
| 1071 |
|
| 1072 |
xlog $self, "connected so one lemming forked"; |
| 1073 |
$self->assert_deep_equals({ foo => { live => 2, dead => 0 } }, |
| 1074 |
$self->lemming_census()); |
| 1075 |
|
| 1076 |
$self->{instance}->send_sighup(); |
| 1077 |
$self->lemming_wait(foo => { live => 2, dead => 1 }); |
| 1078 |
|
| 1079 |
xlog $self, "recycled, so expect one dead lemming"; |
| 1080 |
$self->assert_deep_equals({ foo => { live => 2, dead => 1 } }, |
| 1081 |
$self->lemming_census()); |
| 1082 |
|
| 1083 |
$self->{instance}->send_sighup(); |
| 1084 |
$self->lemming_wait(foo => { live => 2, dead => 2 }); |
| 1085 |
|
| 1086 |
xlog $self, "recycled, again so expect one more dead lemming"; |
| 1087 |
$self->assert_deep_equals({ foo => { live => 2, dead => 2 } }, |
| 1088 |
$self->lemming_census()); |
| 1089 |
|
| 1090 |
lemming_push($lemm, 'success'); |
| 1091 |
|
| 1092 |
xlog $self, "always at least one live lemming"; |
| 1093 |
$self->assert_deep_equals({ foo => { live => 1, dead => 3 } }, |
| 1094 |
$self->lemming_census()); |
| 1095 |
} |
| 1096 |
|
| 1097 |
sub test_sighup_reloading |
| 1098 |
{ |
| 1099 |
my ($self) = @_; |
| 1100 |
|
| 1101 |
my $host = 'localhost'; |
| 1102 |
|
| 1103 |
my $srvA = $self->lemming_service(tag => 'A'); |
| 1104 |
$self->start(); |
| 1105 |
my $srvB = $self->lemming_service(tag => 'B'); |
| 1106 |
|
| 1107 |
|
| 1108 |
my $lemmA = lemming_connect($srvA); |
| 1109 |
|
| 1110 |
xlog $self, "connected so one lemming forked"; |
| 1111 |
$self->assert_deep_equals({ A => { live => 1, dead => 0 } }, |
| 1112 |
$self->lemming_census()); |
| 1113 |
|
| 1114 |
lemming_push($lemmA, 'success'); |
| 1115 |
|
| 1116 |
xlog $self, "no more live lemmings"; |
| 1117 |
$self->assert_deep_equals({ A => { live => 0, dead => 1 } }, |
| 1118 |
$self->lemming_census()); |
| 1119 |
|
| 1120 |
xlog $self, "connection fails due to unexisting lemming"; |
| 1121 |
my $lemmB; |
| 1122 |
eval |
| 1123 |
{ |
| 1124 |
$lemmB = lemming_connect($srvB); |
| 1125 |
}; |
| 1126 |
$self->assert_null($lemmB); |
| 1127 |
|
| 1128 |
$self->assert_deep_equals({ A => { live => 0, dead => 1 } }, |
| 1129 |
$self->lemming_census()); |
| 1130 |
|
| 1131 |
|
| 1132 |
xlog $self, "add service in cyrus.conf and reload"; |
| 1133 |
$self->{instance}->_generate_master_conf(); |
| 1134 |
$self->{instance}->send_sighup(); |
| 1135 |
|
| 1136 |
$lemmA = lemming_connect($srvA); |
| 1137 |
|
| 1138 |
xlog $self, "connected so one lemming forked"; |
| 1139 |
$self->assert_deep_equals({ A => { live => 1, dead => 1 } }, |
| 1140 |
$self->lemming_census()); |
| 1141 |
|
| 1142 |
lemming_push($lemmA, 'success'); |
| 1143 |
|
| 1144 |
xlog $self, "no more live lemmings"; |
| 1145 |
$self->assert_deep_equals({ A => { live => 0, dead => 2 } }, |
| 1146 |
$self->lemming_census()); |
| 1147 |
|
| 1148 |
$lemmB = lemming_connect($srvB); |
| 1149 |
|
| 1150 |
xlog $self, "connected so one lemming forked"; |
| 1151 |
$self->assert_deep_equals({ A => { live => 0, dead => 2 }, |
| 1152 |
B => { live => 1, dead => 0 } }, |
| 1153 |
$self->lemming_census()); |
| 1154 |
|
| 1155 |
lemming_push($lemmB, 'success'); |
| 1156 |
|
| 1157 |
xlog $self, "no more live lemmings"; |
| 1158 |
$self->assert_deep_equals({ A => { live => 0, dead => 2 }, |
| 1159 |
B => { live => 0, dead => 1 } }, |
| 1160 |
$self->lemming_census()); |
| 1161 |
|
| 1162 |
|
| 1163 |
xlog $self, "remove service in cyrus.conf and reload"; |
| 1164 |
$self->{instance}->remove_service('A'); |
| 1165 |
$self->{instance}->_generate_master_conf(); |
| 1166 |
$self->{instance}->send_sighup(); |
| 1167 |
|
| 1168 |
# wait a moment for the sighup to be processed |
| 1169 |
# XXX next test does something tricky with prefork/wait, |
| 1170 |
# XXX but i'm not sure if that can be used here. |
| 1171 |
sleep 1; |
| 1172 |
|
| 1173 |
xlog $self, "connection fails due to unexisting lemming"; |
| 1174 |
$lemmA = undef; |
| 1175 |
eval |
| 1176 |
{ |
| 1177 |
$lemmA = lemming_connect($srvA); |
| 1178 |
}; |
| 1179 |
$self->assert_null($lemmA); |
| 1180 |
|
| 1181 |
$self->assert_deep_equals({ A => { live => 0, dead => 2 }, |
| 1182 |
B => { live => 0, dead => 1 } }, |
| 1183 |
$self->lemming_census()); |
| 1184 |
|
| 1185 |
$lemmB = lemming_connect($srvB); |
| 1186 |
|
| 1187 |
xlog $self, "connected so one lemming forked"; |
| 1188 |
$self->assert_deep_equals({ A => { live => 0, dead => 2 }, |
| 1189 |
B => { live => 1, dead => 1 } }, |
| 1190 |
$self->lemming_census()); |
| 1191 |
|
| 1192 |
lemming_push($lemmB, 'success'); |
| 1193 |
|
| 1194 |
xlog $self, "no more live lemmings"; |
| 1195 |
$self->assert_deep_equals({ A => { live => 0, dead => 2 }, |
| 1196 |
B => { live => 0, dead => 2 } }, |
| 1197 |
$self->lemming_census()); |
| 1198 |
} |
| 1199 |
|
| 1200 |
sub test_sighup_reloading_listen |
| 1201 |
{ |
| 1202 |
my ($self) = @_; |
| 1203 |
|
| 1204 |
my $host = 'localhost'; |
| 1205 |
|
| 1206 |
# Note: we need to wait for SIGHUP to be processed; prefork can do the trick |
| 1207 |
# to help us check that |
| 1208 |
my $srv = $self->lemming_service(tag => 'A', prefork => 1); |
| 1209 |
$self->start(); |
| 1210 |
$self->lemming_wait(A => { live => 1 }); |
| 1211 |
|
| 1212 |
xlog $self, "preforked, so one lemming running already"; |
| 1213 |
$self->assert_deep_equals({ A => { live => 1, dead => 0 } }, |
| 1214 |
$self->lemming_census()); |
| 1215 |
|
| 1216 |
my $lemm = lemming_connect($srv); |
| 1217 |
$self->lemming_wait(A => { live => 2 }); |
| 1218 |
|
| 1219 |
xlog $self, "connected so one lemming forked"; |
| 1220 |
$self->assert_deep_equals({ A => { live => 2, dead => 0 } }, |
| 1221 |
$self->lemming_census()); |
| 1222 |
|
| 1223 |
lemming_push($lemm, 'success'); |
| 1224 |
|
| 1225 |
xlog $self, "always at least one live lemming"; |
| 1226 |
$self->assert_deep_equals({ A => { live => 1, dead => 1 } }, |
| 1227 |
$self->lemming_census()); |
| 1228 |
|
| 1229 |
|
| 1230 |
xlog $self, "change service listen port in cyrus.conf and reload"; |
| 1231 |
my $port1 = $srv->port(); |
| 1232 |
$srv->set_port(); |
| 1233 |
my $port2 = $srv->port(); |
| 1234 |
$self->assert_not_equals($port1, $port2); |
| 1235 |
$self->{instance}->_generate_master_conf(); |
| 1236 |
$self->{instance}->send_sighup(); |
| 1237 |
# Here is the trick with prefork: wait for the previously forked A instance |
| 1238 |
# to die and be replaced by a new one |
| 1239 |
$self->lemming_wait(A => { live => 1, dead => 2 }); |
| 1240 |
|
| 1241 |
$self->assert_deep_equals({ A => { live => 1, dead => 2 } }, |
| 1242 |
$self->lemming_census()); |
| 1243 |
|
| 1244 |
$lemm = lemming_connect($srv); |
| 1245 |
$self->lemming_wait(A => { live => 2 }); |
| 1246 |
|
| 1247 |
xlog $self, "connected so one lemming forked"; |
| 1248 |
$self->assert_deep_equals({ A => { live => 2, dead => 2 } }, |
| 1249 |
$self->lemming_census()); |
| 1250 |
|
| 1251 |
lemming_push($lemm, 'success'); |
| 1252 |
|
| 1253 |
xlog $self, "always at least one live lemming"; |
| 1254 |
$self->assert_deep_equals({ A => { live => 1, dead => 3 } }, |
| 1255 |
$self->lemming_census()); |
| 1256 |
} |
| 1257 |
|
| 1258 |
sub test_sighup_reloading_proto |
| 1259 |
{ |
| 1260 |
my ($self) = @_; |
| 1261 |
|
| 1262 |
my $host = 'localhost'; |
| 1263 |
|
| 1264 |
# Note: we need to wait for SIGHUP to be processed; prefork can do the trick |
| 1265 |
# to help us check that |
| 1266 |
# Note: since we are listening on IPv4 *and* IPv6, there will be 2 preforked |
| 1267 |
# instances |
| 1268 |
my $srv = $self->lemming_service(tag => 'A', host => undef, prefork => 1); |
| 1269 |
$self->start(); |
| 1270 |
$self->lemming_wait(A => { live => 2 }); |
| 1271 |
|
| 1272 |
xlog $self, "preforked, so two lemmings running already"; |
| 1273 |
$self->assert_deep_equals({ A => { live => 2, dead => 0 } }, |
| 1274 |
$self->lemming_census()); |
| 1275 |
|
| 1276 |
# check IPv4 |
| 1277 |
my $lemm = lemming_connect($srv, 'inet'); |
| 1278 |
$self->lemming_wait(A => { live => 3 }); |
| 1279 |
|
| 1280 |
xlog $self, "connected so one lemming forked"; |
| 1281 |
$self->assert_deep_equals({ A => { live => 3, dead => 0 } }, |
| 1282 |
$self->lemming_census()); |
| 1283 |
|
| 1284 |
lemming_push($lemm, 'success'); |
| 1285 |
|
| 1286 |
xlog $self, "always at least two live lemmings"; |
| 1287 |
$self->assert_deep_equals({ A => { live => 2, dead => 1 } }, |
| 1288 |
$self->lemming_census()); |
| 1289 |
|
| 1290 |
# check IPv6 |
| 1291 |
$lemm = lemming_connect($srv, 'inet6'); |
| 1292 |
$self->lemming_wait(A => { live => 3 }); |
| 1293 |
|
| 1294 |
xlog $self, "connected so one lemming forked"; |
| 1295 |
$self->assert_deep_equals({ A => { live => 3, dead => 1 } }, |
| 1296 |
$self->lemming_census()); |
| 1297 |
|
| 1298 |
lemming_push($lemm, 'success'); |
| 1299 |
|
| 1300 |
xlog $self, "always at least two live lemmings"; |
| 1301 |
$self->assert_deep_equals({ A => { live => 2, dead => 2 } }, |
| 1302 |
$self->lemming_census()); |
| 1303 |
|
| 1304 |
|
| 1305 |
xlog $self, "change service listen proto in cyrus.conf and reload"; |
| 1306 |
$srv->set_master_param('proto', 'tcp4'); |
| 1307 |
$self->{instance}->_generate_master_conf(); |
| 1308 |
$self->{instance}->send_sighup(); |
| 1309 |
# Here is the trick with prefork: wait for the previously forked A instances |
| 1310 |
# to die and be replaced by a new one |
| 1311 |
$self->lemming_wait(A => { live => 1, dead => 4 }); |
| 1312 |
|
| 1313 |
$self->assert_deep_equals({ A => { live => 1, dead => 4 } }, |
| 1314 |
$self->lemming_census()); |
| 1315 |
|
| 1316 |
# check IPv4 |
| 1317 |
$lemm = lemming_connect($srv, 'inet'); |
| 1318 |
$self->lemming_wait(A => { live => 2 }); |
| 1319 |
|
| 1320 |
xlog $self, "connected so one lemming forked"; |
| 1321 |
$self->assert_deep_equals({ A => { live => 2, dead => 4 } }, |
| 1322 |
$self->lemming_census()); |
| 1323 |
|
| 1324 |
lemming_push($lemm, 'success'); |
| 1325 |
|
| 1326 |
xlog $self, "always at least one live lemming"; |
| 1327 |
$self->assert_deep_equals({ A => { live => 1, dead => 5 } }, |
| 1328 |
$self->lemming_census()); |
| 1329 |
|
| 1330 |
# check IPv6 |
| 1331 |
xlog $self, "connection fails due to unexisting IPv6 lemming"; |
| 1332 |
$lemm = undef; |
| 1333 |
eval |
| 1334 |
{ |
| 1335 |
$lemm = lemming_connect($srv, 'inet6'); |
| 1336 |
}; |
| 1337 |
$self->assert_null($lemm); |
| 1338 |
|
| 1339 |
xlog $self, "always at least one live lemming"; |
| 1340 |
$self->assert_deep_equals({ A => { live => 1, dead => 5 } }, |
| 1341 |
$self->lemming_census()); |
| 1342 |
} |
| 1343 |
|
| 1344 |
sub test_pidfile_symlink |
| 1345 |
{ |
| 1346 |
my ($self) = @_; |
| 1347 |
|
| 1348 |
$self->{instance}->_init_basedir_and_name(); |
| 1349 |
|
| 1350 |
# XXX it would be nice to put the symlink in the basedir, but anything |
| 1351 |
# XXX we put in there before start will be blown away by _build_skeleton. |
| 1352 |
# XXX instead, set stuff up in /tmp where it won't get trampled before use |
| 1353 |
my $tmp = "/tmp/cassandane-$self->{instance}->{name}"; |
| 1354 |
my $symlink_target = "$tmp/rogue_master_pidfile"; |
| 1355 |
my $pidfile = "$tmp/master.pid"; |
| 1356 |
|
| 1357 |
mkdir $tmp or die "mkdir $tmp: $!"; |
| 1358 |
symlink $symlink_target, $pidfile |
| 1359 |
or die "symlink $symlink_target $pidfile: $!"; |
| 1360 |
|
| 1361 |
$self->{instance}->{config}->set('master_pid_file', $pidfile); |
| 1362 |
|
| 1363 |
eval { |
| 1364 |
$self->start(); |
| 1365 |
}; |
| 1366 |
my $e = $@; |
| 1367 |
$self->assert_matches(qr{exited with code 71}, $e->text()); |
| 1368 |
|
| 1369 |
$self->assert_num_equals(0, $self->{instance}->is_running()); |
| 1370 |
$self->assert_syslog_matches($self->{instance}, |
| 1371 |
qr{can't open pidfile: Too many levels}); |
| 1372 |
|
| 1373 |
unlink $pidfile; |
| 1374 |
unlink $symlink_target; |
| 1375 |
rmdir $tmp; |
| 1376 |
} |
| 1377 |
|
| 1378 |
sub test_pidfile_lock_symlink |
| 1379 |
{ |
| 1380 |
my ($self) = @_; |
| 1381 |
|
| 1382 |
$self->{instance}->_init_basedir_and_name(); |
| 1383 |
|
| 1384 |
# XXX it would be nice to put the symlink in the basedir, but anything |
| 1385 |
# XXX we put in there before start will be blown away by _build_skeleton. |
| 1386 |
# XXX instead, set stuff up in /tmp where it won't get trampled before use |
| 1387 |
my $tmp = "/tmp/cassandane-$self->{instance}->{name}"; |
| 1388 |
my $symlink_target = "$tmp/rogue_master_pidfile_lock"; |
| 1389 |
my $pidfile = "$tmp/master.pid"; |
| 1390 |
my $pidfile_lock = "$pidfile.lock"; |
| 1391 |
|
| 1392 |
mkdir $tmp or die "mkdir $tmp: $!"; |
| 1393 |
symlink $symlink_target, $pidfile_lock |
| 1394 |
or die "symlink $symlink_target $pidfile_lock: $!"; |
| 1395 |
|
| 1396 |
$self->{instance}->{config}->set('master_pid_file', $pidfile); |
| 1397 |
|
| 1398 |
eval { |
| 1399 |
$self->start(); |
| 1400 |
}; |
| 1401 |
my $e = $@; |
| 1402 |
$self->assert_matches(qr{exited with code 71}, $e->text()); |
| 1403 |
|
| 1404 |
$self->assert_num_equals(0, $self->{instance}->is_running()); |
| 1405 |
$self->assert_syslog_matches( |
| 1406 |
$self->{instance}, |
| 1407 |
qr{can't open pidfile lock: \S+ \(Too many levels} |
| 1408 |
); |
| 1409 |
|
| 1410 |
unlink $pidfile_lock; |
| 1411 |
unlink $symlink_target; |
| 1412 |
rmdir $tmp; |
| 1413 |
} |
| 1414 |
|
| 1415 |
sub test_ready_file |
| 1416 |
{ |
| 1417 |
my ($self) = @_; |
| 1418 |
|
| 1419 |
# config isn't fully initialised until start() is called, so we can't |
| 1420 |
# just read these the sensible way. instead, predict the defaults. |
| 1421 |
my $ready_file = $self->{instance}->get_basedir() . '/master.ready'; |
| 1422 |
my $pid_file = $self->{instance}->get_basedir() . '/run/master.pid'; |
| 1423 |
|
| 1424 |
# pid file should not already exist |
| 1425 |
my $pid_sb = stat($pid_file); |
| 1426 |
$self->assert_null($pid_sb); |
| 1427 |
|
| 1428 |
# ready file should not already exist |
| 1429 |
my $ready_sb = stat($ready_file); |
| 1430 |
$self->assert_null($ready_sb); |
| 1431 |
|
| 1432 |
# start cyrus |
| 1433 |
$self->start(); |
| 1434 |
|
| 1435 |
# pid file should exist now |
| 1436 |
$pid_sb = stat($pid_file); |
| 1437 |
$self->assert_not_null($pid_sb); |
| 1438 |
|
| 1439 |
# ready file should exist soon... |
| 1440 |
$ready_sb = stat($ready_file); |
| 1441 |
$self->assert_not_null($ready_sb); |
| 1442 |
|
| 1443 |
# ready file should be newer than pid file |
| 1444 |
$self->assert_num_gte($pid_sb->mtime, $ready_sb->mtime); |
| 1445 |
} |
| 1446 |
|
| 1447 |
sub test_daemon_exits |
| 1448 |
{ |
| 1449 |
my ($self) = @_; |
| 1450 |
|
| 1451 |
my $lemming_delay = 500; #ms |
| 1452 |
my $test_delay = 10; #s |
| 1453 |
|
| 1454 |
xlog $self, "Test a program in the DAEMON section which fails"; |
| 1455 |
$self->lemming_daemon(tag => 'A', delay => $lemming_delay, mode => 'exit'); |
| 1456 |
# This service won't be used |
| 1457 |
my $srv = $self->lemming_service(tag => 'C'); |
| 1458 |
|
| 1459 |
$self->{instance}->start(); |
| 1460 |
|
| 1461 |
# make sure it runs long enough for the child janitor to wake up |
| 1462 |
sleep $test_delay; |
| 1463 |
|
| 1464 |
# master had better still be running! |
| 1465 |
$self->assert($self->{instance}->is_running(), |
| 1466 |
"master is no longer running"); |
| 1467 |
|
| 1468 |
# make sure those lemmings did actually die |
| 1469 |
my $census = $self->lemming_census(); |
| 1470 |
# XXX upper limit on this should be MAX_READY_FAILS if we run for less |
| 1471 |
# XXX than MAX_READY_FAIL_INTERVAL seconds, but the delayed restart of |
| 1472 |
# XXX failed daemons doesn't seem to be actually delayed??? |
| 1473 |
$self->assert_num_gte(1, $census->{A}->{dead}); |
| 1474 |
|
| 1475 |
# there'll be syslog errors as long as this is running, so stop it early |
| 1476 |
$self->{instance}->stop(no_check_syslog => 1); |
| 1477 |
# then consume and check them |
| 1478 |
$self->assert_syslog_matches($self->{instance}, |
| 1479 |
qr{too many failures for}); |
| 1480 |
} |
| 1481 |
|
| 1482 |
sub test_babysit |
| 1483 |
:NoCheckSyslog |
| 1484 |
{ |
| 1485 |
my ($self) = @_; |
| 1486 |
|
| 1487 |
my $srv = $self->lemming_service(prefork => 1, babysit => 1); |
| 1488 |
$self->start(); |
| 1489 |
$self->lemming_wait(A => { live => 1 }); |
| 1490 |
|
| 1491 |
xlog $self, "preforked, so one lemming running already"; |
| 1492 |
$self->assert_deep_equals({ A => { live => 1, dead => 0 } }, |
| 1493 |
$self->lemming_census()); |
| 1494 |
|
| 1495 |
# Quickly kill 5 children to trigger the backoff |
| 1496 |
my $killed = 0; |
| 1497 |
|
| 1498 |
for (1..20) { |
| 1499 |
$killed++ if $self->lemming_cull(); |
| 1500 |
|
| 1501 |
last if $killed >= 5; |
| 1502 |
|
| 1503 |
sleep(.5); |
| 1504 |
}; |
| 1505 |
|
| 1506 |
# make sure it said it's going to wait |
| 1507 |
my @lines = $self->{instance}->getsyslog(qr/ERROR: too many failures .*disabling/); |
| 1508 |
$self->assert_num_equals(1, scalar @lines); |
| 1509 |
|
| 1510 |
# master should not have restarted after 5 deaths |
| 1511 |
$self->assert_deep_equals({ A => { live => 0, dead => 5 } }, |
| 1512 |
$self->lemming_census()); |
| 1513 |
|
| 1514 |
# wait 5 seconds, we still shouldn't have one |
| 1515 |
sleep 5; |
| 1516 |
|
| 1517 |
$self->assert_deep_equals({ A => { live => 0, dead => 5 } }, |
| 1518 |
$self->lemming_census()); |
| 1519 |
|
| 1520 |
# wait up to 30 seconds, there should be one |
| 1521 |
for (1..30) { |
| 1522 |
last if $self->lemming_census->{A}{live}; |
| 1523 |
|
| 1524 |
sleep 1; |
| 1525 |
} |
| 1526 |
|
| 1527 |
$self->assert_deep_equals({ A => { live => 1, dead => 5 } }, |
| 1528 |
$self->lemming_census()); |
| 1529 |
} |
| 1530 |
|
| 1531 |
1; |