Debian Codegraph @

cassandane/Cassandane/Cyrus/Info.pm

raw
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::Info;
41 use strict;
42 use warnings;
43 use Cwd qw(realpath);
44 use Data::Dumper;
45 use Date::Format qw(time2str);
46 use Time::HiRes qw(usleep);
47
48 use lib '.';
49 use base qw(Cassandane::Cyrus::TestCase);
50 use Cassandane::Util::Log;
51 use Cassandane::Instance;
52
53 sub new
54 {
55 my $class = shift;
56 return $class->SUPER::new({}, @_);
57 }
58
59 sub set_up
60 {
61 my ($self) = @_;
62 $self->SUPER::set_up();
63 }
64
65 sub tear_down
66 {
67 my ($self) = @_;
68 $self->SUPER::tear_down();
69 }
70
71 sub test_conf
72 {
73 my ($self) = @_;
74
75 my %imapd_conf;
76 my $filename = $self->{instance}->_imapd_conf();
77 open my $fh, '<', $filename
78 or die "Cannot open $filename for reading: $!";
79 while (my $line = <$fh>) {
80 chomp $line;
81 my ($name, $value) = split /\s*:\s*/, $line, 2;
82 if (Cassandane::Config::is_bitfield($name)) {
83 my @values = split /\s+/, $value;
84 $imapd_conf{$name} = join q{ }, sort @values;
85 }
86 else {
87 $imapd_conf{$name} = $value;
88 }
89 }
90 close $fh;
91
92 my %cyr_info_conf;
93 foreach my $line ($self->{instance}->run_cyr_info('conf')) {
94 chomp $line;
95 my ($name, $value) = split /\s*:\s*/, $line, 2;
96 if (Cassandane::Config::is_bitfield($name)) {
97 my @values = split /\s+/, $value;
98 $cyr_info_conf{$name} = join q{ }, sort @values;
99 }
100 else {
101 $cyr_info_conf{$name} = $value;
102 }
103 }
104
105 $self->assert_deep_equals(\%imapd_conf, \%cyr_info_conf);
106 }
107
108 sub test_conf_all
109 {
110 my ($self) = @_;
111
112 my %imapd_conf;
113 my $filename = $self->{instance}->_imapd_conf();
114 open my $fh, '<', $filename
115 or die "Cannot open $filename for reading: $!";
116 while (my $line = <$fh>) {
117 chomp $line;
118 my ($name, $value) = split /\s*:\s*/, $line, 2;
119 if (Cassandane::Config::is_bitfield($name)) {
120 my @values = split /\s+/, $value;
121 $imapd_conf{$name} = join q{ }, sort @values;
122 }
123 else {
124 $imapd_conf{$name} = $value;
125 }
126 }
127 close $fh;
128
129 my %cyr_info_conf;
130 foreach my $line ($self->{instance}->run_cyr_info('conf-all')) {
131 chomp $line;
132 my ($name, $value) = split /\s*:\s*/, $line, 2;
133
134 # conf-all outputs ALL configured values (including defaults)
135 # but we can only really test for the ones we know we put there
136 next if not exists $imapd_conf{$name};
137
138 if (Cassandane::Config::is_bitfield($name)) {
139 my @values = split /\s+/, $value;
140 $cyr_info_conf{$name} = join q{ }, sort @values;
141 }
142 else {
143 $cyr_info_conf{$name} = $value;
144 }
145 }
146
147 $self->assert_deep_equals(\%imapd_conf, \%cyr_info_conf);
148 }
149
150 sub test_conf_default
151 {
152 my ($self) = @_;
153
154 # conf-default spits out all the defaults. can't do much to
155 # check the actual contents, short of duplicating lib/imapoptions
156 # in here, but we can at least make sure it runs without crashing
157 # and its output looks reasonably sane
158
159 foreach my $line ($self->{instance}->run_cyr_info('conf-default')) {
160 chomp $line;
161 my ($name, $value) = split /\s*:\s*/, $line, 2;
162
163 $self->assert_not_null($name);
164 $self->assert_not_null($value);
165
166 if (Cassandane::Config::is_bitfield($name)) {
167 foreach my $v (split /\s+/, $value) {
168 $self->assert_not_null(Cassandane::Config::is_bitfield_bit($name, $v));
169 }
170 }
171 }
172 }
173
174 sub test_lint
175 {
176 my ($self) = @_;
177
178 xlog $self, "test 'cyr_info conf-lint' in the simplest case";
179
180 my @output = $self->{instance}->run_cyr_info('conf-lint');
181 $self->assert_deep_equals([], \@output);
182 }
183
184 Cassandane::Cyrus::TestCase::magic(ConfigJunk => sub {
185 shift->config_set(trust_fund => 'street art');
186 });
187
188 sub test_lint_junk
189 :ConfigJunk
190 {
191 my ($self) = @_;
192
193 xlog $self, "test 'cyr_info conf-lint' with junk in the config";
194
195 my @output = $self->{instance}->run_cyr_info('conf-lint');
196 $self->assert_deep_equals(["trust_fund: street art\n"], \@output);
197 }
198
199 sub test_lint_channels
200 :min_version_3_2 :NoStartInstances
201 {
202 my ($self) = @_;
203
204 $self->config_set(
205 'sync_log_channels' => 'banana',
206 'banana_sync_host' => 'banana.internal',
207 'banana_sync_trust_fund' => 'street art',
208 'banana_tcp_keepalive' => 'yes',
209 'banana_sasl_mech_list' => 'PLAIN',
210 );
211
212 $self->_start_instances();
213
214 xlog $self, "test 'cyr_info conf-lint' with channel-specific sync config";
215
216 my @output = $self->{instance}->run_cyr_info('conf-lint');
217
218 $self->assert_deep_equals(
219 [ sort(
220 "banana_sync_trust_fund: street art\n",
221 "banana_tcp_keepalive: yes\n",
222 "banana_sasl_mech_list: PLAIN\n",
223 ) ],
224 [ sort @output ]
225 );
226 }
227
228 sub test_lint_partitions
229 :min_version_3_0 :NoStartInstances
230 {
231 my ($self) = @_;
232
233 $self->config_set(
234 # metapartition-, archivepartition- and searchpartition- must
235 # correspond with an extant partition-
236 'partition-good' => '/tmp/pgood',
237 'metapartition-good' => '/tmp/mgood',
238 'archivepartition-good' => '/tmp/agood',
239 'foosearchpartition-good' => '/tmp/sgood',
240
241 'metapartition-bad' => '/tmp/mbad',
242 'archivepartition-bad' => '/tmp/abad',
243 'foosearchpartition-bad' => '/tmp/sbad',
244
245 # backuppartition- was deprecated
246 'backuppartition-bad' => '/tmp/bbad',
247 );
248
249 $self->_start_instances();
250
251 xlog $self, "test 'cyr_info conf-lint' with partitions configured";
252
253 my @output = $self->{instance}->run_cyr_info('conf-lint');
254
255 $self->assert_deep_equals(
256 [ sort(
257 "archivepartition-bad: /tmp/abad\n",
258 "backuppartition-bad: /tmp/bbad\n",
259 "foosearchpartition-bad: /tmp/sbad\n",
260 "metapartition-bad: /tmp/mbad\n",
261 ) ],
262 [ sort @output ]
263 );
264 }
265
266 sub test_lint_services
267 :want_service_http :needs_component_httpd :NoStartInstances
268 {
269 my ($self) = @_;
270
271 $self->config_set(
272 'http_sasl_mech_list' => 'PLAIN',
273 'http_sasl_trust_fund' => 'street art',
274 'http_tcp_keepalive' => 'yes',
275 'http_trust_fund' => 'street art',
276 );
277
278 $self->_start_instances();
279
280 xlog $self, "test 'cyr_info conf-lint' with service-specific config";
281
282 my @output = $self->{instance}->run_cyr_info('conf-lint');
283 @output = grep { !m/_db: / } @output; # skip database types
284
285 $self->assert_deep_equals(
286 [ sort(
287 "http_trust_fund: street art\n",
288 # XXX we don't verify sasl keys, so this isn't reported
289 #"http_sasl_trust_fund: street art\n",
290 ) ],
291 [ sort @output ]
292 );
293 }
294
295 sub test_proc_services
296 {
297 my ($self) = @_;
298
299 # no clients => no service daemons => no processes
300 my @output = $self->{instance}->run_cyr_info('proc');
301 $self->assert_num_equals(0, scalar @output);
302
303 # master spawns service processes when clients connect to them
304 my $imap_svc = $self->{instance}->get_service('imap');
305 my @clients;
306 foreach (1..5) {
307 # five concurrent connections for a single user is normal,
308 # e.g. thunderbird does this
309 my $store = $imap_svc->create_store(username => 'cassandane');
310 my $imaptalk = $store->get_client();
311 push @clients, $imaptalk if $imaptalk;
312 }
313
314 # better have got some clients from that!
315 $self->assert_num_gte(1, scalar @clients);
316
317 # five clients => five service daemons => five processes
318 @output = $self->{instance}->run_cyr_info('proc');
319 $self->assert_num_equals(scalar @clients, scalar @output);
320
321 # log clients out one at a time, expect proc count to decrease
322 while (scalar @clients) {
323 my $old = shift @clients;
324 $old->logout();
325
326 @output = $self->{instance}->run_cyr_info('proc');
327 $self->assert_num_equals(scalar @clients, scalar @output);
328 }
329 }
330
331 sub test_proc_crashed_services
332 {
333 my ($self) = @_;
334
335 # no clients => no service daemons => no processes
336 my @output = $self->{instance}->run_cyr_info('proc');
337 $self->assert_num_equals(0, scalar @output);
338
339 # master spawns service processes when clients connect to them
340 my $imap_svc = $self->{instance}->get_service('imap');
341 my @clients;
342 foreach (1..5) {
343 # five concurrent connections for a single user is normal,
344 # e.g. thunderbird does this
345 my $store = $imap_svc->create_store(username => 'cassandane');
346 my $imaptalk = $store->get_client();
347 push @clients, $imaptalk if $imaptalk;
348 }
349
350 # better have got some clients from that!
351 $self->assert_num_gte(1, scalar @clients);
352
353 # five clients => five service daemons => five processes
354 @output = $self->{instance}->run_cyr_info('proc');
355 $self->assert_num_equals(scalar @clients, scalar @output);
356
357 my @pids = sort map { (split /\s+/, $_, 2)[0] } @output;
358 $self->assert_num_equals(scalar @clients, scalar @pids);
359
360 # crash service processes one at a time, expect proc count to decrease
361 while (scalar @pids) {
362 my $pid = shift @pids;
363 kill 'SEGV', $pid;
364 usleep 250_000;
365
366 my @cores = $self->{instance}->find_cores();
367 if (@cores) {
368 # if we dumped core, there'd better only be one core file
369 $self->assert_num_equals(1, scalar @cores);
370
371 # don't barf on it existing during shutdown
372 unlink $cores[0];
373 }
374
375 @output = $self->{instance}->run_cyr_info('proc');
376 $self->assert_num_equals(scalar @pids, scalar @output);
377 }
378
379 # prevent a lot of "Connection closed by other end" noise by claiming
380 # and discarding the client's socket before its DESTROY is called
381 while (scalar @clients) {
382 my $old = shift @clients;
383
384 $old->release_socket(1);
385 }
386 }
387
388 sub test_proc_starts
389 :NoStartInstances :needs_component_idled
390 {
391 my ($self) = @_;
392
393 # we used to recommend starting idled from START, and it will
394 # still work like that, so using it here saves me mocking something
395 $self->{instance}->add_start(name => 'idled',
396 argv => [ 'idled' ]);
397 $self->{instance}->start();
398
399 # entries listed in START run to completion before master fully
400 # starts up. if they fork themselves and hang around (like idled
401 # does) then that's their business, but master can't and doesn't
402 # track them
403 my @output = $self->{instance}->run_cyr_info('proc');
404
405 $self->assert_num_equals(0, scalar @output);
406 }
407
408 sub test_proc_periodic_events_slow
409 :NoStartInstances
410 {
411 my ($self) = @_;
412
413 my $sleeper_time = 10; # seconds
414
415 # periodic events first fire immediately at startup, and then every
416 # 'period' minutes thereafter. the fastest we can schedule them is
417 # every 1 minute, so this test must run for at least several real
418 # minutes
419 $self->{instance}->add_event(
420 name => 'sleeper',
421 argv => [ realpath('utils/sleeper'), $sleeper_time ],
422 period => 1,
423 );
424 $self->{instance}->start();
425
426 sleep 2; # offset our checks a little to avoid races
427
428 # observe for three cycles
429 my $observations = 3;
430 while ($observations > 0) {
431 # event should have fired and be running
432 my @output = $self->{instance}->run_cyr_info('proc');
433 $self->assert_num_equals(1, scalar @output);
434
435 # wait for it to finish and check again
436 sleep $sleeper_time;
437 @output = $self->{instance}->run_cyr_info('proc');
438 $self->assert_num_equals(0, scalar @output);
439
440 # skip final wait if we're done
441 $observations--;
442 last if $observations == 0;
443
444 # wait until next period
445 sleep 60 - $sleeper_time;
446 }
447 }
448
449 sub test_proc_scheduled_events
450 :NoStartInstances
451 {
452 my ($self) = @_;
453
454 my $sleeper_time = 10;
455
456 # schedule an event to fire at the next minute boundary that is at
457 # least ten seconds away
458 my $at = time + 70;
459 $at -= ($at % 60);
460 my $at_hm = time2str('%H%M', $at);
461 xlog $self, "scheduling event to run at $at_hm ($at)";
462 $self->{instance}->add_event(
463 name => 'sleeper',
464 argv => [ realpath('utils/sleeper'), $sleeper_time ],
465 at => $at_hm,
466 );
467 $self->{instance}->start();
468
469 # event process should not be running at startup
470 my @output = $self->{instance}->run_cyr_info('proc');
471 $self->assert_num_equals(0, scalar @output);
472
473 # should be running at the scheduled time (with a little slop)
474 sleep 2 + $at - time;
475 @output = $self->{instance}->run_cyr_info('proc');
476 $self->assert_num_equals(1, scalar @output);
477
478 # should not be running after we expect it to have finished
479 sleep $sleeper_time;
480 @output = $self->{instance}->run_cyr_info('proc');
481 $self->assert_num_equals(0, scalar @output);
482 }
483
484 sub test_proc_daemons
485 :NoStartInstances
486 {
487 my ($self) = @_;
488
489 my $sleeper_time = 10; # seconds
490 my $daemons = 3;
491
492 for my $i (1 .. $daemons) {
493 # you wouldn't usually run a daemon that exits and needs to be
494 # restarted every ten seconds, but it's useful for testing
495 # that cyr_info proc notices the pid changing
496 $self->{instance}->add_daemon(
497 name => "sleeper$i",
498 argv => [ realpath('utils/sleeper'), $sleeper_time ],
499 );
500 }
501 $self->{instance}->start();
502
503 sleep 2; # offset our checks a little to avoid races
504
505 my $observations = 3;
506 my %lastpid = map {; "sleeper$_" => 0 } (1 .. $daemons);
507 while ($observations > 0) {
508 my @output = $self->{instance}->run_cyr_info('proc');
509
510 # always exactly one process per daemon
511 $self->assert_num_equals($daemons, scalar @output);
512
513 # expect a new pid for each daemon each time
514 foreach my $line (@output) {
515 my ($pid, $servicename, $host, $user, $mailbox, $cmd)
516 = split /\s/, $line, 6;
517 $self->assert_num_not_equals($lastpid{$servicename}, $pid);
518 $lastpid{$servicename} = $pid;
519 }
520
521 # skip final wait if we're done
522 $observations--;
523 last if $observations == 0;
524
525 # wait for next restart
526 sleep $sleeper_time;
527 }
528 }
529
530 1;