lintian doesn't like orphan packages with uploaders...
[debian/amanda] / installcheck / amgtar.pl
1 # Copyright (c) 2009-2012 Zmanda, Inc.  All Rights Reserved.
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11 # for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19
20 use Test::More tests => 21;
21
22 use lib "@amperldir@";
23 use strict;
24 use warnings;
25 use Installcheck;
26 use Amanda::Constants;
27 use Amanda::Paths;
28 use File::Path;
29 use Installcheck::Application;
30 use IO::File;
31
32 unless ($Amanda::Constants::GNUTAR and -x $Amanda::Constants::GNUTAR) {
33     SKIP: {
34         skip("GNU tar is not available", Test::More->builder->expected_tests);
35     }
36     exit 0;
37 }
38
39 my $app = Installcheck::Application->new('amgtar');
40
41 my $support = $app->support();
42 is($support->{'INDEX-LINE'}, 'YES', "supports indexing");
43 is($support->{'MESSAGE-LINE'}, 'YES', "supports messages");
44 is($support->{'CALCSIZE'}, 'YES', "supports calcsize");
45
46 my $root_dir = "$Installcheck::TMP/installcheck-amgtar";
47 my $back_dir = "$root_dir/to_backup";
48 my $rest_dir = "$root_dir/restore";
49 my $list_dir = "$root_dir/list";
50
51 sub ok_foreach {
52     my $code = shift @_;
53     my $stringify = shift @_;
54     my $name = shift @_;
55     my @list = @_;
56
57     my @errors;
58     foreach my $elm (@list) {
59         my $elm_str = $stringify? $stringify->($elm) : "$elm";
60         push @errors, "on element $elm_str: $@" unless eval {$code->($elm); 1;};
61     }
62     unless (ok(!@errors, $name)) {
63         foreach my $err (@errors) {
64             diag($err);
65         }
66     }
67 }
68
69 ok_foreach(
70     sub {
71         my $dir = shift @_;
72         rmtree($dir);
73     },
74     undef,
75     "emptied directories",
76     $back_dir, $rest_dir, $list_dir);
77
78 ok_foreach(
79     sub {
80         my $dir = shift @_;
81         mkpath($dir);
82     },
83     undef,
84     "create directories",
85     $back_dir, $rest_dir, $list_dir);
86
87
88 my @dir_struct = (
89     {'type' => 'f', 'name' => 'foo'},
90     {'type' => 'd', 'name' => 'bar/baz/bat/'},
91     {'type' => 'h', 'name' => 'hard', 'to' => 'foo'},
92     {'type' => 's', 'name' => 'sym', 'to' => 'bar'},
93     {'type' => 's', 'name' => 'a', 'to' => 'b'},
94     {'type' => 's', 'name' => 'b', 'to' => 'a'},
95 );
96
97 ok_foreach(
98     sub {
99         my $obj = shift @_;
100
101         if ($obj->{'type'} eq 'f') {
102             my $fh = new IO::File("$back_dir/$obj->{'name'}", '>');
103             ok($fh, "created file $obj->{'name'}");
104             undef $fh;
105         } elsif ($obj->{'type'} eq 'd') {
106             mkpath("$back_dir/$obj->{'name'}");
107         } elsif ($obj->{'type'} eq 'h') {
108             link("$back_dir/$obj->{'to'}", "$back_dir/$obj->{'name'}") or die "$!";
109         } elsif ($obj->{'type'} eq 's') {
110             symlink("$obj->{'to'}", "$back_dir/$obj->{'name'}") or die "$!";
111         } else {
112             die "unknown object type $obj->{'type'} for $obj->{'name'}";
113         }
114     },
115     sub {shift(@_)->{'name'}},
116     "create directory structure",
117     @dir_struct);
118
119 $app->add_property('gnutar-listdir', $list_dir);
120 # GNU tar on Solaris doesn't support this, so avoid it
121 $app->add_property('atime-preserve', 'no');
122
123 my $selfcheck = $app->selfcheck('device' => $back_dir, 'level' => 0, 'index' => 'line');
124 is($selfcheck->{'exit_status'}, 0, "error status ok");
125 ok(!@{$selfcheck->{'errors'}}, "no errors during selfcheck");
126
127 my $backup = $app->backup('device' => $back_dir, 'level' => 0, 'index' => 'line');
128 is($backup->{'exit_status'}, 0, "error status ok");
129 ok(!@{$backup->{'errors'}}, "no errors during backup")
130     or diag(@{$backup->{'errors'}});
131
132 is(length($backup->{'data'}), $backup->{'size'}, "reported and actual size match");
133
134 ok(@{$backup->{'index'}}, "index is not empty");
135 ok_foreach(
136     sub {
137         my $obj = shift @_;
138         my $name = $obj->{'name'};
139         die "missing $name" unless
140             grep {"/$name" eq $_} @{$backup->{'index'}};
141     },
142     sub {shift(@_)->{'name'}},
143     "index contains all names/paths",
144     @dir_struct);
145
146 my $orig_cur_dir = POSIX::getcwd();
147 ok($orig_cur_dir, "got current directory");
148
149 ok(chdir($rest_dir), "changed working directory (for restore)");
150
151 my $restore = $app->restore('objects' => ['./foo', './bar'], 'data' => $backup->{'data'});
152 is($restore->{'exit_status'}, 0, "error status ok");
153
154 ok(chdir($orig_cur_dir), "changed working directory (back to original)");
155
156 ok(-f "$rest_dir/foo", "foo restored");
157 ok(-d "$rest_dir/bar", "bar/ restored");
158 ok(-d "$rest_dir/bar", "bar/baz/bat/ restored");
159
160 # cleanup
161 rmtree($root_dir);