arclog/t/02-several.t

275 lines
12 KiB
Perl
Raw Permalink Normal View History

2021-02-01 15:05:57 +08:00
#! /usr/bin/perl -w
# Test archiving several log files at once
2022-03-19 00:45:17 +08:00
# Copyright (c) 2007-2022 imacat.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2021-02-01 15:05:57 +08:00
use 5.005;
use strict;
use warnings;
use diagnostics;
use Test;
2022-03-19 00:45:17 +08:00
BEGIN { plan tests => 25 }
2021-02-01 15:05:57 +08:00
use File::Basename qw(basename);
use File::Path qw(mkpath rmtree);
use File::Spec::Functions qw(catdir catfile updir);
use FindBin;
use lib $FindBin::Bin;
use _helper;
our ($WORKDIR, $arclog, $tno);
2021-02-01 15:05:57 +08:00
$WORKDIR = catdir($FindBin::Bin, "logs");
$arclog = catfile($FindBin::Bin, updir, "blib", "script", "arclog");
$tno = 0;
2022-03-19 00:45:17 +08:00
# 1-25: Archiving several log files at once
foreach my $rt (@RESULT_TYPES) {
2021-02-01 15:05:57 +08:00
my $skip;
$skip = 0;
# 1: Source log files listed as the arguments
$_ = eval {
if ( ($$rt{"type"} eq TYPE_GZIP && has_no_gzip)
2022-03-19 00:45:17 +08:00
|| ($$rt{"type"} eq TYPE_BZIP2 && has_no_bzip2)
|| ($$rt{"type"} eq TYPE_XZ && has_no_xz)) {
2021-02-01 15:05:57 +08:00
$skip = 1;
return;
}
my ($title, $cmd, $ret_no, $out, $err, @var_dump, %logfiles);
2021-02-01 15:05:57 +08:00
my ($fr, $frb, @fle, $fle, $flr, %cef, %crf, %tef, %trf);
my ($num, @fs, @cs, @cem, @mle, %cof, $opref, $oprfb, $mt);
my (@st, $fmt);
rmtree $WORKDIR;
mkpath $WORKDIR;
$fmt = $LOG_FORMATS[int rand @LOG_FORMATS];
2021-02-01 15:05:57 +08:00
$title = join ", ", "several log files", "all listed as arguments",
$$fmt{"title"}, $$rt{"title"};
# (2-4 times available compression) log files
2022-03-19 00:45:17 +08:00
$_ = 2 + (has_no_gzip? 0: 2) + (has_no_bzip2? 0: 2)
+ (has_no_xz? 0: 2);
2021-02-01 15:05:57 +08:00
$num = $_ + int rand $_;
my %types = qw();
2021-02-01 15:05:57 +08:00
# At least 2 files for each available compression
foreach my $st (@SOURCE_TYPES) {
next if ($$st{"type"} eq TYPE_GZIP && has_no_gzip)
2022-03-19 00:45:17 +08:00
|| ($$st{"type"} eq TYPE_BZIP2 && has_no_bzip2)
|| ($$st{"type"} eq TYPE_XZ && has_no_xz);
@_ = grep !exists $types{$_}, (0...$num-1);
$types{$_[int rand @_]} = $st;
@_ = grep !exists $types{$_}, (0...$num-1);
$types{$_[int rand @_]} = $st;
2021-02-01 15:05:57 +08:00
}
# Set random compression on the rest files
foreach (grep !exists $types{$_}, (0...$num-1)) {
2021-02-01 15:05:57 +08:00
do {
$types{$_} = $SOURCE_TYPES[int rand @SOURCE_TYPES];
} until !(${$types{$_}}{"type"} eq TYPE_GZIP && has_no_gzip)
2022-03-19 00:45:17 +08:00
&& !(${$types{$_}}{"type"} eq TYPE_BZIP2 && has_no_bzip2)
&& !(${$types{$_}}{"type"} eq TYPE_XZ && has_no_xz);
2021-02-01 15:05:57 +08:00
}
@st = map $types{$_}, (0...$num-1);
2021-02-01 15:05:57 +08:00
@fs = qw();
@cs = qw();
@cem = qw();
@var_dump = qw();
2021-02-01 15:05:57 +08:00
@fle = qw();
%logfiles = qw();
for (my $k = 0; $k < $num; $k++) {
my ($logfile, $cs, $var_dump);
do { $logfile = random_word } until !exists $logfiles{$logfile};
2021-02-01 15:05:57 +08:00
$logfiles{$logfile} = 1;
$logfile .= ${$st[$k]}{"suf"};
push @fs, catfile($WORKDIR, $logfile);
($cs, $var_dump, %types) = &{$$fmt{"sub"}}($fs[$k]);
2021-02-01 15:05:57 +08:00
push @cs, $cs;
push @cem, {%types};
2021-02-01 15:05:57 +08:00
push @fle, $logfile;
push @var_dump, $var_dump;
write_raw_file(catfile($WORKDIR, "$logfile.var-dump"), $var_dump);
push @fle, "$logfile.var-dump";
2021-02-01 15:05:57 +08:00
}
%types = qw();
%types = (%types, map { $_ => 1 } keys %$_) foreach @cem;
@mle = sort keys %types;
2021-02-01 15:05:57 +08:00
$mt = pop @mle;
do { $oprfb = random_word } until !exists $logfiles{$oprfb};
2021-02-01 15:05:57 +08:00
$opref = catfile($WORKDIR, $oprfb);
%cof = make_log_file $$fmt{"sub"},
2021-02-01 15:05:57 +08:00
$WORKDIR, "$oprfb.%s" . $$rt{"suf"}, @mle;
push @fle, map "$oprfb.$_" . $$rt{"suf"}, @mle;
preserve_source $WORKDIR;
2021-02-01 15:05:57 +08:00
@_ = ($arclog, qw(-d -d -d -o a), @{$$rt{"opts"}}, @fs, $opref);
$cmd = join(" ", @_);
($ret_no, $out, $err) = run_cmd "", @_;
($fle, $flr) = (join(" ", sort @fle), list_files $WORKDIR);
2021-02-01 15:05:57 +08:00
%cef = qw(); # Expected content by file
%tef = qw(); # Expected file type by file
%crf = qw(); # Resulted content by file
%trf = qw(); # Resulted file type by file
for (my $k = 0; $k < $num; $k++) {
$fr = $fs[$k];
$frb = basename($fr);
($cef{$frb}, $tef{$frb}) = (${$cem[$k]}{$mt}, ${$st[$k]}{"type"});
($crf{$frb}, $trf{$frb}) = (read_file $fr, file_type $fr);
$fr = $fs[$k] . ".var-dump";
2021-02-01 15:05:57 +08:00
$frb = basename($fr);
($cef{$frb}, $tef{$frb}) = ($var_dump[$k], TYPE_TEXT);
($crf{$frb}, $trf{$frb}) = (read_file $fr, file_type $fr);
2021-02-01 15:05:57 +08:00
}
foreach my $m (@mle) {
$fr = "$opref.$m" . $$rt{"suf"};
$frb = basename($fr);
$cef{$frb} = $cof{$frb}
. join "", map(exists $$_{$m}? $$_{$m}: "", @cem);
$tef{$frb} = $$rt{"type"};
($crf{$frb}, $trf{$frb}) = (read_file $fr, file_type $fr);
2021-02-01 15:05:57 +08:00
}
die "$title\n$cmd\n$out$err" unless $ret_no == 0;
2021-02-01 15:05:57 +08:00
die "$title\n$cmd\nresult files incorrect.\nGot: $flr\nExpected: $fle\nOutput:\n$out$err"
unless $flr eq $fle;
foreach $fr (@fle) {
die "$title\n$cmd\n$fr: result type incorrect.\nGot: $trf{$fr}\nExpected: $tef{$fr}\nOutput:\n$out$err"
unless has_no_file || $trf{$fr} eq $tef{$fr};
2021-02-01 15:05:57 +08:00
die "$title\n$cmd\n$fr: result incorrect.\nGot:\n$crf{$fr}\nExpected:\n$cef{$fr}\nOutput:\n$out$err"
unless $crf{$fr} eq $cef{$fr};
}
1;
};
skip($skip, $_, 1, $@);
clean_up $_ || $skip, $WORKDIR, ++$tno;
2022-03-19 00:45:17 +08:00
# 2-5: One of the source log files is read from STDIN
2021-02-01 15:05:57 +08:00
# The file type at STDIN
foreach my $st_stdin (@SOURCE_TYPES) {
2021-02-01 15:05:57 +08:00
$skip = 0;
$_ = eval {
if ( ($$rt{"type"} eq TYPE_GZIP && has_no_gzip)
|| ($$rt{"type"} eq TYPE_BZIP2 && has_no_bzip2)
2022-03-19 00:45:17 +08:00
|| ($$rt{"type"} eq TYPE_XZ && has_no_xz)
|| ($$st_stdin{"type"} eq TYPE_GZIP && has_no_gzip)
2022-03-19 00:45:17 +08:00
|| ($$st_stdin{"type"} eq TYPE_BZIP2 && has_no_bzip2)
|| ($$st_stdin{"type"} eq TYPE_XZ && has_no_xz)) {
2021-02-01 15:05:57 +08:00
$skip = 1;
return;
}
my ($title, $cmd, $ret_no, $out, $err, @var_dump, %logfiles);
2021-02-01 15:05:57 +08:00
my ($fr, $frb, @fle, $fle, $flr, %cef, %crf, %tef, %trf);
my ($num, @fs, @cs, @cem, @mle, %cof, $opref, $oprfb, $stdin);
my (@st, $fmt);
rmtree $WORKDIR;
mkpath $WORKDIR;
$fmt = $LOG_FORMATS[int rand @LOG_FORMATS];
2021-02-01 15:05:57 +08:00
$title = join ", ", "several log files", "one read from STDIN",
"STDIN " . $$st_stdin{"title"}, $$rt{"title"};
2021-02-01 15:05:57 +08:00
# (2-4 times available compression) log files
2022-03-19 00:45:17 +08:00
$_ = 2 + (has_no_gzip? 0: 2) + (has_no_bzip2? 0: 2)
+ (has_no_xz? 0: 2);
2021-02-01 15:05:57 +08:00
$num = $_ + int rand $_;
my %types = qw();
2021-02-01 15:05:57 +08:00
# At least 2 files for each available compression
foreach my $st (@SOURCE_TYPES) {
next if ($$st{"type"} eq TYPE_GZIP && has_no_gzip)
2022-03-19 00:45:17 +08:00
|| ($$st{"type"} eq TYPE_BZIP2 && has_no_bzip2)
|| ($$st{"type"} eq TYPE_XZ && has_no_xz);
@_ = grep !exists $types{$_}, (0...$num-1);
$types{$_[int rand @_]} = $st;
@_ = grep !exists $types{$_}, (0...$num-1);
$types{$_[int rand @_]} = $st;
2021-02-01 15:05:57 +08:00
}
# Set random compression on the rest files
foreach (grep !exists $types{$_}, (0...$num-1)) {
2021-02-01 15:05:57 +08:00
do {
$types{$_} = $SOURCE_TYPES[int rand @SOURCE_TYPES];
} until !(${$types{$_}}{"type"} eq TYPE_GZIP && has_no_gzip)
2022-03-19 00:45:17 +08:00
&& !(${$types{$_}}{"type"} eq TYPE_BZIP2 && has_no_bzip2)
&& !(${$types{$_}}{"type"} eq TYPE_XZ && has_no_xz);
2021-02-01 15:05:57 +08:00
}
# Choose the STDIN from the matching compression
@_ = grep ${$types{$_}}{"type"} eq $$st_stdin{"type"}, (0...$num-1);
2021-02-01 15:05:57 +08:00
$stdin = $_[int rand @_];
@st = map $types{$_}, (0...$num-1);
2021-02-01 15:05:57 +08:00
@fs = qw();
@cs = qw();
@cem = qw();
@var_dump = qw();
2021-02-01 15:05:57 +08:00
@fle = qw();
%logfiles = qw();
for (my $k = 0; $k < $num; $k++) {
my ($logfile, $cs, $var_dump);
do { $logfile = random_word } until !exists $logfiles{$logfile};
2021-02-01 15:05:57 +08:00
$logfiles{$logfile} = 1;
$logfile .= ${$st[$k]}{"suf"};
push @fs, catfile($WORKDIR, $logfile);
($cs, $var_dump, %types) = &{$$fmt{"sub"}}($fs[$k]);
2021-02-01 15:05:57 +08:00
push @cs, $cs;
push @cem, {%types};
2021-02-01 15:05:57 +08:00
push @fle, $logfile;
push @var_dump, $var_dump;
write_raw_file(catfile($WORKDIR, "$logfile.var-dump"), $var_dump);
push @fle, "$logfile.var-dump";
2021-02-01 15:05:57 +08:00
}
%types = qw();
%types = (%types, map { $_ => 1 } keys %$_) foreach @cem;
@mle = sort keys %types;
do { $oprfb = random_word } until !exists $logfiles{$oprfb};
2021-02-01 15:05:57 +08:00
$opref = catfile($WORKDIR, $oprfb);
%cof = make_log_file $$fmt{"sub"},
2021-02-01 15:05:57 +08:00
$WORKDIR, "$oprfb.%s" . $$rt{"suf"}, @mle;
push @fle, map "$oprfb.$_" . $$rt{"suf"}, @mle;
preserve_source $WORKDIR;
2021-02-01 15:05:57 +08:00
@_ = @fs;
$_[$stdin] = "-";
@_ = ($arclog, qw(-d -d -d -k a -o a), @{$$rt{"opts"}}, @_, $opref);
$cmd = join(" ", @_) . " < " . $fs[$stdin];
($ret_no, $out, $err) = run_cmd read_raw_file $fs[$stdin], @_;
($fle, $flr) = (join(" ", sort @fle), list_files $WORKDIR);
2021-02-01 15:05:57 +08:00
%cef = qw(); # Expected content by file
%tef = qw(); # Expected file type by file
%crf = qw(); # Resulted content by file
%trf = qw(); # Resulted file type by file
for (my $k = 0; $k < $num; $k++) {
$fr = $fs[$k];
$frb = basename($fr);
($cef{$frb}, $tef{$frb}) = ($cs[$k], ${$st[$k]}{"type"});
($crf{$frb}, $trf{$frb}) = (read_file $fr, file_type $fr);
$fr = $fs[$k] . ".var-dump";
2021-02-01 15:05:57 +08:00
$frb = basename($fr);
($cef{$frb}, $tef{$frb}) = ($var_dump[$k], TYPE_TEXT);
($crf{$frb}, $trf{$frb}) = (read_file $fr, file_type $fr);
2021-02-01 15:05:57 +08:00
}
foreach my $m (@mle) {
$fr = "$opref.$m" . $$rt{"suf"};
$frb = basename($fr);
$cef{$frb} = $cof{$frb}
. join "", map(exists $$_{$m}? $$_{$m}: "", @cem);
$tef{$frb} = $$rt{"type"};
($crf{$frb}, $trf{$frb}) = (read_file $fr, file_type $fr);
2021-02-01 15:05:57 +08:00
}
die "$title\n$cmd\n$out$err" unless $ret_no == 0;
2021-02-01 15:05:57 +08:00
die "$title\n$cmd\nresult files incorrect.\nGot: $flr\nExpected: $fle\nOutput:\n$out$err"
unless $flr eq $fle;
foreach $fr (@fle) {
die "$title\n$cmd\n$fr: result type incorrect.\nGot: $trf{$fr}\nExpected: $tef{$fr}\nOutput:\n$out$err"
unless has_no_file || $trf{$fr} eq $tef{$fr};
2021-02-01 15:05:57 +08:00
die "$title\n$cmd\n$fr: result incorrect.\nGot:\n$crf{$fr}\nExpected:\n$cef{$fr}\nOutput:\n$out$err"
unless $crf{$fr} eq $cef{$fr};
}
1;
};
skip($skip, $_, 1, $@);
clean_up $_ || $skip, $WORKDIR, ++$tno;
2021-02-01 15:05:57 +08:00
}
}