# Copyright (c) 2009 bivio Software, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: Backup3.bunit,v 1.5 2011/06/20 23:27:11 nagler Exp $ Request(); my($now) = DateTime('20081229123456'); DateTime()->set_test_now($now, req()); my($today) = DateTime()->now; my($days) = [map(Date()->to_file_name(Date()->add_days($today, $_)), 0 .. 5)]; my($logs) = []; my($log_dirs) = []; my($tmp) = IO_File()->absolute_path('Backup3'); system("chmod -R u+rxw $tmp 2>/dev/null; rm -rf $tmp"); my($mirror) = "$tmp/mirror"; my($archive) = "$tmp/archive"; foreach my $day (@$days) { push(@$logs, my $l = []); push(@$log_dirs, my $ld = []); foreach my $log ( map("$mirror/foo.bar/var/log/$_", "d1/${day}131313/f1", "d2/$day/f1", "d3/$day.gz", "d3/${day}010101.gz", ), ) { push(@$ld, IO_File()->mkdir_parent_only($log)); push(@$l, $log); system("dd if=/dev/zero of=$log bs=1024 count=1 > /dev/null 2>&1"); } } [ class() => [ compress_and_trim_log_dirs => [ [$mirror => (@$days - 1) - 1] => sub { assert_equals(undef, -e $log_dirs->[0]->[0]); assert_equals(undef, -e "$log_dirs->[0]->[0].tgz"); assert_equals(undef, -e "$log_dirs->[$#$log_dirs - 1]->[0]"); assert_equals(1, -e "$log_dirs->[$#$log_dirs - 1]->[0].tgz"); assert_equals(1, -e "$log_dirs->[$#$log_dirs]->[0]"); return [join( '', join( ' ', 'Compressed:', sort(grep( !m{/d3$}, map(@$_, @$log_dirs[0..$#$log_dirs - 1]), )), ), "\n", join( ' ', 'Deleted:', sort(grep( !m{/d3.tgz$}, map("$_.tgz", map(@$_, @$log_dirs[0])), )), ), "\n", )]; }, [$mirror => (@$days - 1) - 2] => join( ' ', 'Deleted:', sort(grep( !m{/d3.tgz$}, map("$_.tgz", map(@$_, @$log_dirs[1])), )), ) . "\n", ], archive_logs => [ [$mirror, $archive] => sub { return [[sort( map({ my($year) = $_ =~ /(200[89])/; (my $x = $_) =~ s{^\Q$mirror\E}{$archive/$year}; assert_equals(1, -r $x && ! -w $x, "$x"); $x; } map(("$_->[0].tgz", "$_->[1].tgz"), @$log_dirs[2..$#$log_dirs - 1]), grep(/gz$/, map(@$_, @$logs)), ), )]]; }, [$mirror, $archive] => [], ], ], ];