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