Brackup/Test.pm

filter: Statementstmt 95.0% 174 / 183 Branchbran 63.2% 43 / 68 Conditioncond 54.5% 6 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 96.5% 28 / 29 Podpod 0.0% 0 / 9 totaltotal 82.0% 252 / 307 CC 40 SCAR 17.6
CC
40
Cov
85%
check_inventory_db
13.0
do_backup
10.3
do_restore
7.2
CRAP 5.8
« Brackup/Target/Riak.pm ↑ Summary Brackup/File.pm »
1 # support module for helping test brackup
2 package Brackup::Test;
3 12 require Exporter;
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use vars qw(@ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 @ISA = qw(Exporter);
7 12 @EXPORT = qw(do_backup do_restore ok_dirs_match ok_files_match);
8  
9 12 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use FindBin qw($Bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Brackup::Util qw(tempdir tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use File::stat ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use Brackup::DecryptedFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use Digest::SHA1 qw/sha1_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 12 ▶use Brackup;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 12 ▶my $has_diff = eval "use Text::Diff; 1;";
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 12 my @to_unlink;
23 12 my $par_pid = $$;
24 12 ▶END {
Subroutine END: called CC=3 SCAR=12.0
25 12 ▶ if ($$ == $par_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ == $par_pid) 12 0 12
26 12 ▶ my $rv = unlink @to_unlink unless $ENV{BRACKUP_TEST_NOCLEANUP};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"BRACKUP_TEST_NOCLEANUP"} 12 0 12
27 }
28 }
29  
30 # Set the gpg directory, so we don't rely on users having a ~/.gnupg
31 12 $ENV{GNUPGHOME} = tempdir( CLEANUP => 1 );
32  
33 5 ▶sub do_backup {
Subroutine do_backup: called CC=10 SCAR=23.3
Pod undocumented
34 5 my %opts = @_;
35 5 ▶ my $with_confsec = delete $opts{'with_confsec'} || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
delete $opts{'with_confsec'} || sub { }
l!l&&r!l&&!r
500
MC/DC0%0/2
delete $opts{'with_confsec'} || sub { }
delete $opts{'with_confsec'} sub { }
Truth table33%1/3
A || B
A= delete $opts{'with_confsec'}B= sub { }
ABresult
1X1
011
000
36 5 ▶ my $with_targetsec = delete $opts{'with_targetsec'} || sub {};
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition66%2/3
delete $opts{'with_targetsec'} || sub { }
l!l&&r!l&&!r
140
MC/DC0%0/2
delete $opts{'with_targetsec'} || sub { }
delete $opts{'with_targetsec'} sub { }
Truth table66%2/3
A || B
A= delete $opts{'with_targetsec'}B= sub { }
ABresult
1X1
011
000
37 5 ▶ my $with_root = delete $opts{'with_root'} || sub {};
Subroutine __ANON__: called CC=1 SCAR=1.2
Condition33%1/3
delete $opts{'with_root'} || sub { }
l!l&&r!l&&!r
050
MC/DC0%0/2
delete $opts{'with_root'} || sub { }
delete $opts{'with_root'} sub { }
Truth table33%1/3
A || B
A= delete $opts{'with_root'}B= sub { }
ABresult
1X1
011
000
38 5 my $target = delete $opts{'with_target'};
39 5 ▶ die if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 5 5
40  
41 5 my $initer = shift;
42  
43 5 my $conf = Brackup::Config->new;
44 5 my $confsec;
45  
46 5 $confsec = Brackup::ConfigSection->new("SOURCE:test_root");
47 5 $with_confsec->($confsec);
48 5 $conf->add_section($confsec);
49  
50 5 my $root = $conf->load_root("test_root");
51 5 ok($root, "have a source root");
52 5 $with_root->($root);
53  
54 5 ▶ unless ($target) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($target) 4 1 5
55 4 ▶ my $backup_dir = tempdir( CLEANUP => $ENV{BRACKUP_TEST_NOCLEANUP} ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'BRACKUP_TEST_NOCLEANUP'} ? : 0 4 4
56 4 ok_dir_empty($backup_dir);
57  
58 4 my ($inv_fh, $inv_filename) = tempfile();
59 4 close($inv_fh);
60 4 push @to_unlink, $inv_filename;
61  
62  
63 4 $confsec = Brackup::ConfigSection->new("TARGET:test_restore");
64 4 $with_targetsec->($confsec);
65 4 ▶ $confsec->add("type" => "Filesystem") unless exists $confsec->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $confsec->{"type"} 3 1 4
66 4 $confsec->add("inventorydb_file" => $inv_filename);
67 4 $confsec->add("path" => $backup_dir);
68 4 $conf->add_section($confsec);
69 4 $target = $conf->load_target("test_restore", testmode => 1);
70 }
71  
72 5 ok($target, "have a target ($target)");
73  
74 5 my $backup = Brackup::Backup->new(
75 root => $root,
76 target => $target,
77 savefiles => 1,
78 );
79 5 ok($backup, "have a backup object");
80  
81 5 my ($meta_fh, $meta_filename) = tempfile();
82 5 ok(-e $meta_filename, "metafile exists");
83 5 push @to_unlink, $meta_filename;
84  
85 5 ok(eval { $backup->backup($meta_filename) }, "backup succeeded");
86 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
87 0 warn "Died running backup: $@\n";
88 }
89 5 ok(-s $meta_filename, "backup file has size");
90  
91 5 check_inventory_db($target, [$root->gpg_args]);
92  
93 5 ▶ return wantarray ? ($meta_filename, $backup, $target) : $meta_filename;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 2 5
94 }
95  
96 6 ▶sub check_inventory_db {
Subroutine check_inventory_db: called CC=11 SCAR=25.7
Pod undocumented
97 6 my ($target, $gpg_args) = @_;
98  
99 6 my $inv_db_file;
100 6 eval {
101 6 ▶ my $inv_db = $target->inventory_db or die 'cannot open inventory db';
Branch: 1 branch
BranchTrueFalseTotal
my $inv_db = $target->inventory_db or die "cannot open inventory db" 6 0 6
102 6 ▶ $inv_db_file = $inv_db->backing_file ? (' ' . $inv_db->backing_file) : '';
Branch: 1 branch
BranchTrueFalseTotal
$inv_db->backing_file ? : 6 0 6
103  
104 6 ▶ while (my ($key, $value) = $inv_db->each) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $inv_db->each) 74 6 80
105 74 my ($raw_dig, $gpg_rcpt) = split /;/, $key;
106 74 my ($enc_dig, $enc_size, $range) = split /\s+/, $value;
107  
108 # check the stored data
109 74 ▶ my $dataref = $target->load_chunk($enc_dig) or die "cannot load chunk $enc_dig";
Branch: 1 branch
BranchTrueFalseTotal
my $dataref = $target->load_chunk($enc_dig) or die "cannot load chunk $enc_dig" 74 0 74
110 74 ▶ length $$dataref == $enc_size or die "chunk $enc_dig has wrong size, not $enc_size";
Branch: 1 branch
BranchTrueFalseTotal
length $$dataref == $enc_size or die "chunk $enc_dig has wrong size, not $enc_size" 74 0 74
111 74 ▶ $enc_dig eq "sha1:".sha1_hex($$dataref) or die "chunk $enc_dig has wrong digest";
Branch: 1 branch
BranchTrueFalseTotal
$enc_dig eq 'sha1:' . sha1_hex($$dataref) or die "chunk $enc_dig has wrong digest" 74 0 74
112
113 # if we are in a composite chunk, keep only the part we want
114 74 ▶ if($range) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range) 5 69 74
115 5 my ($from, $to) = split '-', $range;
116 5 my $part = substr $$dataref, $from, $to-$from;
117 5 $dataref = \$part;
118 }
119  
120 # decrypt if encrypted
121 74 my $dec_ref;
122 74 ▶ if($gpg_rcpt =~ /^to=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gpg_rcpt =~ /^to=(.*)$/) { } 0 74 74
123 0 my $meta = { 'GPG-Recipient' => $1 };
124 0 local @Brackup::GPG_ARGS = @$gpg_args;
125  
126 0 $dec_ref = Brackup::Decrypt::decrypt_data($dataref, meta => $meta);
127 } else {
128 74 $dec_ref = $dataref;
129 }
130  
131 # check the raw data
132 74 ▶ $raw_dig eq "sha1:".sha1_hex($$dec_ref) or die "chunk $enc_dig has wrong raw digest";
Branch: 1 branch
BranchTrueFalseTotal
$raw_dig eq 'sha1:' . sha1_hex($$dec_ref) or die "chunk $enc_dig has wrong raw digest" 74 0 74
133 }
134 };
135 6 ▶ ok(!$@, "inventory db$inv_db_file is good") or diag($@);
Branch: 1 branch
BranchTrueFalseTotal
&ok(!$@, "inventory db$inv_db_file is good") or diag($@) 6 0 6
136 }
137  
138 7 ▶sub do_restore {
Subroutine do_restore: called CC=7 SCAR=19.7
Pod undocumented
139 7 my ($backup_file, %opts) = @_;
140 7 ▶ my $prefix = delete $opts{'prefix'} || ""; # default is restore everything
Condition100%2/2
delete $opts{'prefix'} || ''
l!l
43
MC/DC100%1/1
delete $opts{'prefix'} || ''
delete $opts{'prefix'}
Truth table100%2/2
A
A= delete $opts{'prefix'}
Aresult
11
00
141 7 my $restore_should_die = delete $opts{'restore_should_die'};
142 7 ▶ die if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 7 7
143 7 ▶ my $restore_dir = tempdir( CLEANUP => $ENV{BRACKUP_TEST_NOCLEANUP} ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'BRACKUP_TEST_NOCLEANUP'} ? : 0 7 7
144 7 ok_dir_empty($restore_dir);
145  
146 7 my $restore = Brackup::Restore->new(
147 to => $restore_dir,
148 prefix => $prefix,
149 file => $backup_file,
150 );
151 7 ok($restore, "have restore object");
152 7 my $rv = eval { $restore->restore; };
153 7 ▶ if ($restore_should_die) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restore_should_die) { } 1 6 7
154 1 ▶ ok(! defined $rv, "restore died: $@")
Branch: 1 branch
BranchTrueFalseTotal
&ok(!defined($rv), "restore died: $@") or die "restore unexpectedly succeeded" 1 0 1
155 or die "restore unexpectedly succeeded";
156 1 return;
157 }
158 else {
159 6 ▶ ok($rv, "did the restore")
Branch: 1 branch
BranchTrueFalseTotal
&ok($rv, "did the restore") or die "restore failed: $@" 6 0 6
160 or die "restore failed: $@";
161 6 return $restore_dir;
162 }
163 }
164  
165 4 ▶sub ok_dirs_match {
Subroutine ok_dirs_match: called CC=2 SCAR=7.0
Pod undocumented
166 4 my ($after, $before) = @_;
167  
168 4 my $pre_ls = dir_structure($before);
169 4 my $post_ls = dir_structure($after);
170  
171 4 ▶ if ($has_diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_diff) { } 4 0 4
172 12 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 4 my $pre_dump = Dumper($pre_ls);
174 4 my $post_dump = Dumper($post_ls);
175 4 my $diff = Text::Diff::diff(\$pre_dump, \$post_dump);
176 4 is($diff, "", "dirs match");
177 } else {
178 0 is_deeply($post_ls, $pre_ls, "dirs match");
179 }
180 }
181  
182 2 ▶sub ok_files_match {
Subroutine ok_files_match: called CC=2 SCAR=7.0
Pod undocumented
183 2 my ($after, $before) = @_;
184  
185 2 my $pre_ls = file_meta($before);
186 2 my $post_ls = file_meta($after);
187  
188 2 ▶ if ($has_diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_diff) { } 2 0 2
189 12 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
190 2 my $pre_dump = Dumper($pre_ls);
191 2 my $post_dump = Dumper($post_ls);
192 2 my $diff = Text::Diff::diff(\$pre_dump, \$post_dump);
193 2 is($diff, "", "files match");
194 } else {
195 0 is_deeply($post_ls, $pre_ls, "files match");
196 }
197 }
198  
199 11 ▶sub ok_dir_empty {
Subroutine ok_dir_empty: called CC=3 SCAR=12.7
Pod undocumented
200 11 my $dir = shift;
201 0 ▶ unless (-d $dir) { ok(0, "not a dir"); return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 11 11
202 11 ▶ opendir(my $dh, $dir) or die "failed to opendir: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "failed to opendir: $!" 11 0 11
203 11 is_deeply([ sort readdir($dh) ], ['.', '..'], "dir is empty: $dir");
204 }
205  
206 92 ▶sub file_meta {
Subroutine file_meta: called CC=6 SCAR=18.0
Pod undocumented
207 92 my $path = shift;
208 92 my $st = File::stat::lstat($path);
209  
210 92 my $meta = {};
211 92 ▶ $meta->{size} = $st->size unless -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 74 18 92
212 92 ▶ $meta->{is_file} = 1 if -f $path;
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 74 18 92
213 92 ▶ $meta->{is_link} = 1 if -l $path;
Branch: 1 branch
BranchTrueFalseTotal
if -l $path 0 92 92
214 92 ▶ if ($meta->{is_link}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{'is_link'}) { } 0 92 92
215 0 $meta->{link} = readlink $path;
216 } else {
217 # we ignore these for links, since Linux doesn't let us restore anyway,
218 # as Linux as no lutimes(2) syscall, as of Linux 2.6.16 at least
219 92 $meta->{atime} = $st->atime if 0; # TODO: make tests work with atimes
220 92 $meta->{mtime} = $st->mtime;
221 92 $meta->{mode} = sprintf('%#o', $st->mode & 0777);
222 }
223  
224 # the gpg tests open/close the rings in the root, so
225 # mtimes get bumped around or something. the proper fix
226 # is too ugly for what it's worth, so let's just ignore
227 # the mtime of top-level
228 92 ▶ delete $meta->{mtime} if $path eq ".";
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "." 8 84 92
229  
230 92 return $meta;
231 }
232  
233 # given a directory, returns a hashref of its contentn
234 8 ▶sub dir_structure {
Subroutine dir_structure: called CC=3 SCAR=11.1
Pod undocumented
235 8 my $dir = shift;
236 8 my %files; # "filename" -> {metadata => ...}
237 8 my $cwd = getcwd;
238 8 ▶ chdir($dir) or die "Failed to chdir to $dir";
Branch: 1 branch
BranchTrueFalseTotal
chdir $dir or die "Failed to chdir to $dir" 8 0 8
239  
240 find({
241 no_chdir => 1,
242 18 ▶ preprocess => sub { return sort @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
243 88 ▶ wanted => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
244 88 my $path = $_;
245 88 $files{$path} = file_meta($path);
246 },
247 8 }, ".");
248  
249 8 ▶ chdir($cwd) or die "Failed to chdir back to $cwd";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or die "Failed to chdir back to $cwd" 8 0 8
250 8 return \%files;
251 }
252  
253 # add a random number of orphan chunks to $target
254 1 ▶sub add_orphan_chunks {
Subroutine add_orphan_chunks: called CC=2 SCAR=6.9
Pod undocumented
255 1 my ($root, $target, $orphan_chunks_count) = @_;
256  
257 1 for (1..$orphan_chunks_count) {
258 # HACK: to avoid worse hacks, we need a pchunk to store an orphan chunk.
259 # We use small segments of 'pubring-test.gpg' so that they are different
260 # than all other chunks
261 9 my $pchunk = Brackup::PositionedChunk->new(
262 file => Brackup::File->new(root => $root,
263 path => 'pubring-test.gpg'),
264 offset => $_ * 10,
265 length => 10,
266 );
267  
268 # no encryption, copy raw data and store schunk
269 9 my $schunk = Brackup::StoredChunk->new($pchunk);
270 # $schunk->copy_raw_data;
271 9 $target->store_chunk($schunk);
272 }
273 }
274  
275  
276 12 1;
277  
278 # vim:et:sw=4
« Brackup/Target/Riak.pm ↑ Summary Brackup/File.pm »