Debian Codegraph @

cassandane/Cassandane/Cyrus/Prometheus.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 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" 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 # Level 1, 91 William St
23 # Melbourne 3000
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
32 # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
33 # AND FITNESS, IN NO EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE
34 # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
36 # AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
37 # OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 #
39
40 package Cassandane::Cyrus::Prometheus;
41 use strict;
42 use warnings;
43 use Data::Dumper;
44 use File::Slurp;
45 use HTTP::Tiny;
46
47 use lib '.';
48 use base qw(Cassandane::Cyrus::TestCase);
49 use Cassandane::Util::Log;
50 use Cassandane::Instance;
51
52 $Data::Dumper::Sortkeys = 1;
53
54 sub new
55 {
56 my $class = shift;
57
58 my $config = Cassandane::Config->default()->clone();
59 $config->set(prometheus_enabled => "yes");
60 $config->set(httpmodules => "prometheus");
61 $config->set(prometheus_need_auth => "none");
62 $config->set(prometheus_service_update_freq => 2);
63 $config->set(prometheus_master_update_freq => 2);
64 $config->set(prometheus_usage_update_freq => 2);
65
66 my $self = $class->SUPER::new(
67 { adminstore => 1,
68 config => $config,
69 services => ['imap', 'http'] },
70 @_);
71
72 $self->needs('component', 'httpd');
73 return $self;
74 }
75
76 sub set_up
77 {
78 my ($self) = @_;
79 $self->SUPER::set_up();
80 }
81
82 sub _create_instances
83 {
84 my ($self) = @_;
85
86 $self->SUPER::_create_instances();
87 # XXX This should really run from the DAEMON section,
88 # XXX but Cassandane doesn't know about that.
89 $self->{instance}->add_start(name => 'promstatsd',
90 argv => [ 'promstatsd' ]);
91 }
92
93 sub tear_down
94 {
95 my ($self) = @_;
96 $self->SUPER::tear_down();
97 }
98
99 sub http_report
100 {
101 my ($self) = @_;
102
103 my $service = $self->{instance}->get_service("http");
104 my $url = join(q{},
105 q{http://}, $service->host(),
106 q{:}, $service->port(),
107 q{/metrics});
108
109 return HTTP::Tiny->new()->get($url);
110 }
111
112 sub parse_report
113 {
114 my ($content) = @_;
115
116 my $report = {};
117
118 foreach my $line (split /\n/, $content) {
119 next if $line =~ /^\#/;
120 my ($key, $val, $ts) = split /\s+/, $line;
121 if ($key =~ m/^([^\{]+)\{([^\}]+)}$/) {
122 $report->{$1}->{$2} = { value => $val, timestamp => $ts };
123 }
124 else {
125 $report->{$key} = { value => $val, timestamp => $ts };
126 }
127 }
128
129 return $report;
130 }
131
132 sub test_aaasetup
133 :min_version_3_1
134 {
135 my ($self) = @_;
136
137 # does everything set up and tear down cleanly?
138 $self->assert(1);
139 }
140
141 sub test_service_reportfile_exists
142 :min_version_3_1
143 {
144 my ($self) = @_;
145
146 # do something that'll get counted
147 my $imaptalk = $self->{store}->get_client();
148 $imaptalk->select("INBOX");
149 # and wait for a fresh report
150 sleep 3;
151
152 my $fname = "$self->{instance}->{basedir}/conf/stats/service.txt";
153
154 $self->assert_file_test($fname, '-f');
155
156 my $report = parse_report(scalar read_file $fname);
157
158 $self->assert(scalar keys %{$report});
159 $self->assert(exists $report->{cyrus_imap_connections_total});
160 }
161
162 sub test_httpreport
163 :min_version_3_1
164 {
165 my ($self) = @_;
166
167 # do something that'll get counted
168 my $imaptalk = $self->{store}->get_client();
169 $imaptalk->select("INBOX");
170 # and wait for a fresh report
171 sleep 3;
172
173 my $response = $self->http_report();
174
175 $self->assert($response->{success});
176 $self->assert(length $response->{content});
177
178 my $report = parse_report($response->{content});
179
180 $self->assert(scalar keys %{$report});
181 $self->assert(exists $report->{cyrus_imap_connections_total});
182 }
183
184 sub test_disabled
185 :min_version_3_1 :NoStartInstances
186 {
187 my ($self) = @_;
188
189 my $instance = $self->{instance};
190 $instance->{starts} = [ grep { $_->{name} ne 'promstatsd' } @{$instance->{starts}} ];
191 $instance->{config}->set(prometheus_enabled => 'no');
192
193 $self->_start_instances();
194
195 # no stats directory
196 my $stats_dir = "$self->{instance}->{basedir}/conf/stats";
197 $self->assert_not_file_test($stats_dir, '-d');
198
199 # no http report
200 my $response = $self->http_report();
201 $self->assert_equals(404, $response->{status});
202 }
203
204 # tests for pathological quotaroot/partition subdivisions
205 sub test_quota_commitments
206 :min_version_3_1 :Partition2
207 {
208 my ($self) = @_;
209
210 my $admintalk = $self->{adminstore}->get_client();
211
212 my $inbox = 'user.cassandane'; # allocate top level quota here
213 my $child = "$inbox.child";
214 my $gchild1 = "$child.cat"; # we'll stick this one on a sep part
215 my $gchild2 = "$child.dog"; # give this one its own quota
216 my $gchild3 = "$child.sheep"; # normal, but sorts after weird ones
217 my $ggchild1 = "$gchild1.manx"; # and give this one its own quota
218 my $ggchild2 = "$gchild1.siamese"; # and this one back on def part
219 my $interm = "$inbox.foo.bar.baz"; # contains intermediate folders
220 my $inbox2 = 'user.cassandane-child'; # hyphen! own quota
221
222 # make some folders
223 foreach my $f ($child, $gchild1, $gchild2, $gchild3, $ggchild1,
224 $ggchild2, $interm, $inbox2) {
225 $admintalk->create($f);
226 $self->assert_str_equals('ok',
227 $admintalk->get_last_completion_response());
228 }
229
230 # stick one of them on a different partition
231 $admintalk->rename($gchild1, $gchild1, 'p2');
232 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
233
234 # but not one of its children
235 $admintalk->rename($ggchild2, $ggchild2, 'default');
236 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
237
238 # create a mess of quotas
239 $admintalk->setquota($inbox, '(STORAGE 8000)');
240 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
241 $admintalk->setquota($gchild2, '(STORAGE 4000)');
242 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
243 $admintalk->setquota($ggchild1, '(STORAGE 2000)');
244 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
245 $admintalk->setquota($inbox2, '(STORAGE 1000)');
246 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
247
248 $admintalk->logout();
249
250 sleep 3;
251
252 my $response = $self->http_report();
253 $self->assert($response->{success});
254
255 my $report = parse_report($response->{content});
256 $self->assert(scalar keys %{$report});
257
258 # now we expect default partition to have 8000 + 4000 + 1000 committed
259 $self->assert_equals(13000, $report->{'cyrus_usage_quota_commitment'}->{'partition="default",resource="STORAGE"'}->{value});
260
261 # and p2 partition to have 8000 + 2000 committed
262 $self->assert_equals(10000, $report->{'cyrus_usage_quota_commitment'}->{'partition="p2",resource="STORAGE"'}->{value});
263 }
264
265 sub test_shared_mailbox_namespaces
266 :min_version_3_1
267 {
268 my ($self) = @_;
269
270 my $admintalk = $self->{adminstore}->get_client();
271
272 my $ns1 = 'foo';
273 my $ns2 = 'bar';
274 my @folders = map { ("$ns1.$_", "$ns2.$_" ) }
275 qw(cat sheep dog interm.interm.rabbit);
276
277 foreach my $f (@folders) {
278 $admintalk->create($f);
279 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
280 }
281
282 sleep 3;
283
284 my $response = $self->http_report();
285 $self->assert($response->{success});
286
287 my $report = parse_report($response->{content});
288 $self->assert(scalar keys %{$report});
289
290 my $num_folders = 4;
291 my ($maj, $min) = Cassandane::Instance->get_version();
292 if ($maj > 3 || ($maj == 3 && $min > 4)) {
293 $num_folders = 7;
294 }
295
296 # expect to find $num_folders folders on each of 'foo' and 'bar' namespaces
297 $self->assert_equals($num_folders, $report->{'cyrus_usage_shared_mailboxes'}->{'partition="default",namespace="bar"'}->{value});
298
299 $self->assert_equals($num_folders, $report->{'cyrus_usage_shared_mailboxes'}->{'partition="default",namespace="foo"'}->{value});
300 }
301
302 sub slowtest_50000_users
303 :min_version_3_1
304 {
305 my ($self) = @_;
306
307 my $nusers = 50000;
308 my @subfolders = qw(Drafts Sent Spam Trash);
309 my $storage = 8000;
310
311 my $admintalk = $self->{adminstore}->get_client();
312
313 foreach my $n (1..$nusers) {
314 # reconnect every so often so stuff can flush
315 if ($n % 5000 == 0) {
316 $admintalk->logout();
317 $self->{adminstore}->disconnect();
318 $admintalk = $self->{adminstore}->get_client();
319 }
320
321 my $folder = sprintf("user.a%08d", $n);
322 $admintalk->create($folder);
323 $self->assert_str_equals('ok',
324 $admintalk->get_last_completion_response());
325
326 $admintalk->setquota($folder, "(STORAGE $storage)");
327 $self->assert_str_equals('ok',
328 $admintalk->get_last_completion_response());
329
330 foreach my $subfolder (@subfolders) {
331 $admintalk->create("$folder.$subfolder");
332 $self->assert_str_equals('ok',
333 $admintalk->get_last_completion_response());
334 }
335 }
336
337 # XXX may not be long enough!
338 sleep 3;
339
340 my $response = $self->http_report();
341 $self->assert($response->{success});
342
343 my $report = parse_report($response->{content});
344 $self->assert(scalar keys %{$report});
345
346 # n.b. user/mailbox counts are +1 cause of user.cassandane!
347 $self->assert_num_equals(1 + $nusers,
348 $report->{'cyrus_usage_users'}->{'partition="default"'}->{value});
349 $self->assert_num_equals(1 + $nusers + ($nusers * scalar @subfolders),
350 $report->{'cyrus_usage_mailboxes'}->{'partition="default"'}->{value});
351 $self->assert_num_equals($nusers * $storage,
352 $report->{'cyrus_usage_quota_commitment'}->{'partition="default",resource="STORAGE"'}->{value});
353 }
354
355 sub test_connection_setup_failure_imapd
356 :min_version_3_2 :TLS
357 {
358 my ($self) = @_;
359
360 my $instance = $self->{instance};
361
362 my $svc = $instance->get_service('imaps');
363 $self->assert_not_null($svc);
364
365 # we're gonna try to connect to it unencrypted, so it fails
366 my $store = $svc->create_store('type' => 'imap');
367 $self->assert_not_null($store);
368
369 my $badconns = 2 + int(rand(4)); # between 2-5 tries
370 for (1 .. $badconns) {
371 # try to connect to it using a plain text client
372 # and expect the server to drop the connection
373 eval {
374 $store->get_client();
375 };
376 my $error = $@;
377 $self->assert_not_null($error);
378 }
379
380 # wait a bit for the prometheus report to refresh
381 sleep 3;
382
383 # check the prom report
384 my $response = $self->http_report();
385 $self->assert($response->{success});
386
387 my $report = parse_report($response->{content});
388 $self->assert(scalar keys %{$report});
389
390 my $active = $report->{'cyrus_imap_active_connections'};
391 $self->assert_not_null($active);
392 my $ready = $report->{'cyrus_imap_ready_listeners'};
393 $self->assert_not_null($ready);
394 my $total = $report->{'cyrus_imap_connections_total'};
395 $self->assert_not_null($total);
396 my $shutdown = $report->{'cyrus_imap_shutdown_total'};
397 $self->assert_not_null($shutdown);
398
399 my $service_label = 'service="imaps"';
400
401 # number of active connections should definitely not be negative
402 $self->assert_num_gte(0, $active->{$service_label}->{value});
403
404 # number of active connections should in fact be zero
405 $self->assert_num_equals(0, $active->{$service_label}->{value});
406
407 # number of ready listeners should be zero or one, depending on
408 # whether it felt like preforking
409 $self->assert_num_gte(0, $ready->{$service_label}->{value});
410 $self->assert_num_lte(1, $ready->{$service_label}->{value});
411
412 # should not have had any successful connections to imaps
413 $self->assert(not exists $total->{$service_label});
414
415 # should be $badconns error shutdowns counted
416 $self->assert_num_equals(
417 $badconns,
418 $shutdown->{"$service_label,status=\"error\""}->{value}
419 );
420
421 # XXX someday: expect to find $badconns setup failures counted
422 }
423
424 1;