Debian Codegraph @

cassandane/Cassandane/Cyrus/Archive.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::Archive;
41 use strict;
42 use warnings;
43 use DateTime;
44 use Data::Dumper;
45
46 use lib '.';
47 use base qw(Cassandane::Cyrus::TestCase);
48 use Cassandane::Util::Log;
49 use Cassandane::Util::Words;
50
51 sub new
52 {
53 my ($class, @args) = @_;
54 return $class->SUPER::new({ adminstore => 1 }, @args);
55 }
56
57 sub set_up
58 {
59 my ($self) = @_;
60 $self->SUPER::set_up();
61 }
62
63 sub tear_down
64 {
65 my ($self) = @_;
66 $self->SUPER::tear_down();
67 }
68
69 #
70 # Test that
71 # - cyr_expire archives messages
72 # - once archived, messages are in the new path
73 # - the message is gone from the old path
74 # - XXX: hard to test - that there's no possible race in which the message
75 # isn't available to clients during the archive operation
76 #
77 sub test_archive_messages
78 :ArchivePartition :min_version_3_0
79 {
80 my ($self) = @_;
81
82 my $talk = $self->{store}->get_client();
83 $self->{store}->_select();
84 $self->assert_num_equals(1, $talk->uid());
85 $self->{store}->set_fetch_attributes(qw(uid flags));
86
87 xlog $self, "Append 3 messages";
88 my %msg;
89 $msg{A} = $self->make_message('Message A');
90 $msg{A}->set_attributes(id => 1,
91 uid => 1,
92 flags => []);
93 $msg{B} = $self->make_message('Message B');
94 $msg{B}->set_attributes(id => 2,
95 uid => 2,
96 flags => []);
97 $msg{C} = $self->make_message('Message C');
98 $msg{C}->set_attributes(id => 3,
99 uid => 3,
100 flags => []);
101 $self->check_messages(\%msg);
102
103 my $data = $self->{instance}->run_mbpath("-u", 'cassandane');
104 my $datadir = $data->{data};
105 my $archivedir = $data->{archive};
106
107 $self->assert_file_test("$datadir/1.", "-f");
108 $self->assert_file_test("$datadir/2.", "-f");
109 $self->assert_file_test("$datadir/3.", "-f");
110
111 $self->assert_not_file_test("$archivedir/1.", "-f");
112 $self->assert_not_file_test("$archivedir/2.", "-f");
113 $self->assert_not_file_test("$archivedir/3.", "-f");
114
115 xlog $self, "Run cyr_expire but no messages should move";
116 $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-A' => '7d' );
117
118 $self->assert_file_test("$datadir/1.", "-f");
119 $self->assert_file_test("$datadir/2.", "-f");
120 $self->assert_file_test("$datadir/3.", "-f");
121
122 $self->assert_not_file_test("$archivedir/1.", "-f");
123 $self->assert_not_file_test("$archivedir/2.", "-f");
124 $self->assert_not_file_test("$archivedir/3.", "-f");
125
126 xlog $self, "Run cyr_expire to archive now";
127 $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-A' => '0' );
128
129 $self->assert_not_file_test("$datadir/1.", "-f");
130 $self->assert_not_file_test("$datadir/2.", "-f");
131 $self->assert_not_file_test("$datadir/3.", "-f");
132
133 $self->assert_file_test("$archivedir/1.", "-f");
134 $self->assert_file_test("$archivedir/2.", "-f");
135 $self->assert_file_test("$archivedir/3.", "-f");
136 }
137
138 sub test_archivenow_messages
139 :ArchiveNow :min_version_3_0
140 {
141 my ($self) = @_;
142
143 my $talk = $self->{store}->get_client();
144 $self->{store}->_select();
145 $self->assert_num_equals(1, $talk->uid());
146 $self->{store}->set_fetch_attributes(qw(uid flags));
147
148 xlog $self, "Append 3 messages";
149 my %msg;
150 $msg{A} = $self->make_message('Message A');
151 $msg{A}->set_attributes(id => 1,
152 uid => 1,
153 flags => []);
154 $msg{B} = $self->make_message('Message B');
155 $msg{B}->set_attributes(id => 2,
156 uid => 2,
157 flags => []);
158 $msg{C} = $self->make_message('Message C');
159 $msg{C}->set_attributes(id => 3,
160 uid => 3,
161 flags => []);
162 $self->check_messages(\%msg);
163
164 my $data = $self->{instance}->run_mbpath("-u", 'cassandane');
165 my $datadir = $data->{data};
166 my $archivedir = $data->{archive};
167
168 # already archived
169 $self->assert_not_file_test("$datadir/1.", "-f");
170 $self->assert_not_file_test("$datadir/2.", "-f");
171 $self->assert_not_file_test("$datadir/3.", "-f");
172
173 $self->assert_file_test("$archivedir/1.", "-f");
174 $self->assert_file_test("$archivedir/2.", "-f");
175 $self->assert_file_test("$archivedir/3.", "-f");
176
177 xlog $self, "Run cyr_expire with old and messages stay archived";
178 $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-A' => '7d' );
179
180 $self->assert_not_file_test("$datadir/1.", "-f");
181 $self->assert_not_file_test("$datadir/2.", "-f");
182 $self->assert_not_file_test("$datadir/3.", "-f");
183
184 $self->assert_file_test("$archivedir/1.", "-f");
185 $self->assert_file_test("$archivedir/2.", "-f");
186 $self->assert_file_test("$archivedir/3.", "-f");
187
188 xlog $self, "Run cyr_expire to archive now and messages stay archived";
189 $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-A' => '0' );
190
191 $self->assert_not_file_test("$datadir/1.", "-f");
192 $self->assert_not_file_test("$datadir/2.", "-f");
193 $self->assert_not_file_test("$datadir/3.", "-f");
194
195 $self->assert_file_test("$archivedir/1.", "-f");
196 $self->assert_file_test("$archivedir/2.", "-f");
197 $self->assert_file_test("$archivedir/3.", "-f");
198 }
199
200 1;
201
202 sub test_archive_messages_archive_annotation
203 :ArchivePartition :min_version_3_1
204 {
205 my ($self) = @_;
206
207 my $talk = $self->{store}->get_client();
208 my $admintalk = $self->{adminstore}->get_client();
209
210 $self->{store}->_select();
211 $self->assert_num_equals(1, $talk->uid());
212 $self->{store}->set_fetch_attributes(qw(uid flags));
213
214 xlog $self, "Append 3 messages";
215 my %msg;
216 $msg{A} = $self->make_message('Message A');
217 $msg{A}->set_attributes(id => 1,
218 uid => 1,
219 flags => []);
220 $msg{B} = $self->make_message('Message B');
221 $msg{B}->set_attributes(id => 2,
222 uid => 2,
223 flags => []);
224 $msg{C} = $self->make_message('Message C');
225 $msg{C}->set_attributes(id => 3,
226 uid => 3,
227 flags => []);
228 $self->check_messages(\%msg);
229
230 my $data = $self->{instance}->run_mbpath("-u", 'cassandane');
231 my $datadir = $data->{data};
232 my $archivedir = $data->{archive};
233
234 $self->assert_file_test("$datadir/1.", "-f");
235 $self->assert_file_test("$datadir/2.", "-f");
236 $self->assert_file_test("$datadir/3.", "-f");
237
238 $self->assert_not_file_test("$archivedir/1.", "-f");
239 $self->assert_not_file_test("$archivedir/2.", "-f");
240 $self->assert_not_file_test("$archivedir/3.", "-f");
241
242 xlog $self, "Run cyr_expire but no messages should move";
243 $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-A' => '7d' );
244
245 $self->assert_file_test("$datadir/1.", "-f");
246 $self->assert_file_test("$datadir/2.", "-f");
247 $self->assert_file_test("$datadir/3.", "-f");
248
249 $self->assert_not_file_test("$archivedir/1.", "-f");
250 $self->assert_not_file_test("$archivedir/2.", "-f");
251 $self->assert_not_file_test("$archivedir/3.", "-f");
252
253 $admintalk->setmetadata('user.cassandane',
254 "/shared/vendor/cmu/cyrus-imapd/archive",
255 '3');
256
257 xlog $self, "Run cyr_expire asking to archive now, but it shouldn't";
258 $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-A' => '0' );
259
260 $self->assert_file_test("$datadir/1.", "-f");
261 $self->assert_file_test("$datadir/2.", "-f");
262 $self->assert_file_test("$datadir/3.", "-f");
263
264 $self->assert_not_file_test("$archivedir/1.", "-f");
265 $self->assert_not_file_test("$archivedir/2.", "-f");
266 $self->assert_not_file_test("$archivedir/3.", "-f");
267
268 xlog $self, "Run cyr_expire asking to archive now, with skip annotation";
269 $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-A' => '0' , '-a');
270
271 $self->assert_not_file_test("$datadir/1.", "-f");
272 $self->assert_not_file_test("$datadir/2.", "-f");
273 $self->assert_not_file_test("$datadir/3.", "-f");
274
275 $self->assert_file_test("$archivedir/1.", "-f");
276 $self->assert_file_test("$archivedir/2.", "-f");
277 $self->assert_file_test("$archivedir/3.", "-f");
278 }
279
280 sub test_archivenow_reconstruct
281 :ArchiveNow :min_version_3_0
282 {
283 my ($self) = @_;
284
285 my $talk = $self->{store}->get_client();
286 $self->{store}->_select();
287 $self->assert_num_equals(1, $talk->uid());
288 $self->{store}->set_fetch_attributes(qw(uid flags));
289
290 xlog $self, "Append 3 messages";
291 my %msg;
292 $msg{A} = $self->make_message('Message A');
293 $msg{A}->set_attributes(id => 1,
294 uid => 1,
295 flags => []);
296 $msg{B} = $self->make_message('Message B');
297 $msg{B}->set_attributes(id => 2,
298 uid => 2,
299 flags => []);
300 $msg{C} = $self->make_message('Message C');
301 $msg{C}->set_attributes(id => 3,
302 uid => 3,
303 flags => []);
304 $self->check_messages(\%msg);
305
306 my $data = $self->{instance}->run_mbpath("-u", 'cassandane');
307 my $datadir = $data->{data};
308 my $archivedir = $data->{archive};
309
310 # already archived
311 $self->assert_not_file_test("$datadir/1.", "-f");
312 $self->assert_not_file_test("$datadir/2.", "-f");
313 $self->assert_not_file_test("$datadir/3.", "-f");
314
315 $self->assert_file_test("$archivedir/1.", "-f");
316 $self->assert_file_test("$archivedir/2.", "-f");
317 $self->assert_file_test("$archivedir/3.", "-f");
318
319 xlog $self, "Run cyr_expire with old and messages stay archived";
320 $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-A' => '7d' );
321
322 $self->assert_not_file_test("$datadir/1.", "-f");
323 $self->assert_not_file_test("$datadir/2.", "-f");
324 $self->assert_not_file_test("$datadir/3.", "-f");
325
326 $self->assert_file_test("$archivedir/1.", "-f");
327 $self->assert_file_test("$archivedir/2.", "-f");
328 $self->assert_file_test("$archivedir/3.", "-f");
329
330 xlog $self, "Run cyr_expire to archive now and messages stay archived";
331 $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-A' => '0' );
332
333 $self->assert_not_file_test("$datadir/1.", "-f");
334 $self->assert_not_file_test("$datadir/2.", "-f");
335 $self->assert_not_file_test("$datadir/3.", "-f");
336
337 $self->assert_file_test("$archivedir/1.", "-f");
338 $self->assert_file_test("$archivedir/2.", "-f");
339 $self->assert_file_test("$archivedir/3.", "-f");
340
341 xlog $self, "Reconstruct doesn't lose files";
342
343 $self->{instance}->run_command({ cyrus => 1 }, 'reconstruct', '-s');
344
345 $self->assert_not_file_test("$datadir/1.", "-f");
346 $self->assert_not_file_test("$datadir/2.", "-f");
347 $self->assert_not_file_test("$datadir/3.", "-f");
348
349 $self->assert_file_test("$archivedir/1.", "-f");
350 $self->assert_file_test("$archivedir/2.", "-f");
351 $self->assert_file_test("$archivedir/3.", "-f");
352 }
353
354 1;