2021-02-01 15:06:51 +08:00
|
|
|
#! /usr/bin/perl -w
|
|
|
|
# Test the errors that should be captured.
|
|
|
|
|
2022-03-19 21:13:23 +08:00
|
|
|
# Copyright (c) 2007-2022 imacat.
|
2021-02-06 19:37:28 +08:00
|
|
|
#
|
|
|
|
# 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:06:51 +08:00
|
|
|
|
|
|
|
use 5.005;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use diagnostics;
|
|
|
|
use Test;
|
|
|
|
|
2022-03-19 21:13:23 +08:00
|
|
|
BEGIN { plan tests => 9 }
|
2021-02-01 15:06:51 +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;
|
2021-02-06 19:37:28 +08:00
|
|
|
our ($WORKDIR, $reslog, $tno);
|
2021-02-01 15:06:51 +08:00
|
|
|
|
|
|
|
$WORKDIR = catdir($FindBin::Bin, "logs");
|
|
|
|
$reslog = catfile($FindBin::Bin, updir, "blib", "script", "reslog");
|
|
|
|
$tno = 0;
|
|
|
|
|
2022-03-19 21:13:23 +08:00
|
|
|
# 1-8: Trim suffix is the same as suffix
|
2021-02-06 19:37:28 +08:00
|
|
|
foreach my $st (@SOURCE_TYPES) {
|
2021-02-01 15:06:51 +08:00
|
|
|
# 1: Trim suffix is the same as suffix
|
|
|
|
$_ = eval {
|
|
|
|
return if $$st{"skip"};
|
2021-02-06 19:37:28 +08:00
|
|
|
my ($title, $cmd, $ret_no, $out, $err, $logfile);
|
2021-02-01 15:06:51 +08:00
|
|
|
my ($fr, $frb, @fle, $fle, $flr, %cef, %crf, %tef, %trf);
|
2021-02-06 19:37:28 +08:00
|
|
|
my ($fs, $cs, $csr, $suf);
|
2021-02-01 15:06:51 +08:00
|
|
|
rmtree $WORKDIR;
|
|
|
|
mkpath $WORKDIR;
|
|
|
|
$title = join ", ", "Trim suffix is the same as suffix",
|
|
|
|
$$st{"title"};
|
2021-02-06 19:37:28 +08:00
|
|
|
$logfile = random_word;
|
|
|
|
$suf = "." . random_word;
|
2021-02-01 15:06:51 +08:00
|
|
|
$fs = catfile($WORKDIR, "$logfile" . $$st{"suf"});
|
2021-02-06 19:37:28 +08:00
|
|
|
($cs, $csr) = make_log_file $fs;
|
2021-02-01 15:06:51 +08:00
|
|
|
@fle = qw();
|
|
|
|
push @fle, basename($fs);
|
2021-02-06 19:37:28 +08:00
|
|
|
preserve_source $WORKDIR;
|
2021-02-01 15:06:51 +08:00
|
|
|
@_ = ($reslog, qw(-d -d -d -n 1), "-s", $suf, "-t", $suf, $fs);
|
|
|
|
$cmd = join " ", @_;
|
2021-02-06 19:37:28 +08:00
|
|
|
($ret_no, $out, $err) = run_cmd "", @_;
|
|
|
|
($fle, $flr) = (join(" ", sort @fle), list_files $WORKDIR);
|
2021-02-01 15:06:51 +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
|
|
|
|
$fr = $fs;
|
|
|
|
$frb = basename($fr);
|
|
|
|
($cef{$frb}, $tef{$frb}) = ($cs, $$st{"type"});
|
2021-02-06 19:37:28 +08:00
|
|
|
($crf{$frb}, $trf{$frb}) = (read_file $fr, file_type $fr);
|
|
|
|
die "$title\n$cmd\n$out$err" unless $ret_no != 0;
|
2021-02-01 15:06:51 +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"
|
2021-02-06 19:37:28 +08:00
|
|
|
unless has_no_file || $trf{$fr} eq $tef{$fr}
|
2021-02-01 15:06:51 +08:00
|
|
|
|| ($tef{$fr} eq TYPE_BZIP2 && -z catfile($WORKDIR, $fr));
|
|
|
|
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($$st{"skip"}, $_, 1, $@);
|
2021-02-06 19:37:28 +08:00
|
|
|
clean_up $_ || $$st{"skip"}, $WORKDIR, ++$tno;
|
|
|
|
|
2021-02-01 15:06:51 +08:00
|
|
|
# 2: Default suffix and trim suffix is set to .resolved
|
|
|
|
$_ = eval {
|
|
|
|
return if $$st{"skip"};
|
2021-02-06 19:37:28 +08:00
|
|
|
my ($title, $cmd, $ret_no, $out, $err, $logfile);
|
2021-02-01 15:06:51 +08:00
|
|
|
my ($fr, $frb, @fle, $fle, $flr, %cef, %crf, %tef, %trf);
|
2021-02-06 19:37:28 +08:00
|
|
|
my ($fs, $cs, $csr, $suf);
|
2021-02-01 15:06:51 +08:00
|
|
|
rmtree $WORKDIR;
|
|
|
|
mkpath $WORKDIR;
|
|
|
|
$title = join ", ", "Default suffix and trim suffix is set to .resolved",
|
|
|
|
$$st{"title"};
|
2021-02-06 19:37:28 +08:00
|
|
|
$logfile = random_word;
|
2021-02-01 15:06:51 +08:00
|
|
|
$suf = ".resolved";
|
|
|
|
$fs = catfile($WORKDIR, "$logfile" . $$st{"suf"});
|
2021-02-06 19:37:28 +08:00
|
|
|
($cs, $csr) = make_log_file $fs;
|
2021-02-01 15:06:51 +08:00
|
|
|
@fle = qw();
|
|
|
|
push @fle, basename($fs);
|
2021-02-06 19:37:28 +08:00
|
|
|
preserve_source $WORKDIR;
|
2021-02-01 15:06:51 +08:00
|
|
|
@_ = ($reslog, qw(-d -d -d -n 1), "-t", $suf, $fs);
|
|
|
|
$cmd = join " ", @_;
|
2021-02-06 19:37:28 +08:00
|
|
|
($ret_no, $out, $err) = run_cmd "", @_;
|
|
|
|
($fle, $flr) = (join(" ", sort @fle), list_files $WORKDIR);
|
2021-02-01 15:06:51 +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
|
|
|
|
$fr = $fs;
|
|
|
|
$frb = basename($fr);
|
|
|
|
($cef{$frb}, $tef{$frb}) = ($cs, $$st{"type"});
|
2021-02-06 19:37:28 +08:00
|
|
|
($crf{$frb}, $trf{$frb}) = (read_file $fr, file_type $fr);
|
|
|
|
die "$title\n$cmd\n$out$err" unless $ret_no != 0;
|
2021-02-01 15:06:51 +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"
|
2021-02-06 19:37:28 +08:00
|
|
|
unless has_no_file || $trf{$fr} eq $tef{$fr}
|
2021-02-01 15:06:51 +08:00
|
|
|
|| ($tef{$fr} eq TYPE_BZIP2 && -z catfile($WORKDIR, $fr));
|
|
|
|
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($$st{"skip"}, $_, 1, $@);
|
2021-02-06 19:37:28 +08:00
|
|
|
clean_up $_ || $$st{"skip"}, $WORKDIR, ++$tno;
|
2021-02-01 15:06:51 +08:00
|
|
|
}
|
|
|
|
|
2022-03-19 21:13:23 +08:00
|
|
|
# 9: A same log file is specified more than once
|
2021-02-01 15:06:51 +08:00
|
|
|
$_ = eval {
|
2021-02-06 19:37:28 +08:00
|
|
|
my ($title, $cmd, $ret_no, $out, $err, %logfiles);
|
2021-02-01 15:06:51 +08:00
|
|
|
my ($fr, $frb, @fle, $fle, $flr, %cef, %crf, %tef, %trf);
|
2021-02-06 19:37:28 +08:00
|
|
|
my ($num, @fs, @fo, @cs, @csr, @co, @st, $suf, $trim_suf, $dup);
|
2021-02-01 15:06:51 +08:00
|
|
|
rmtree $WORKDIR;
|
|
|
|
mkpath $WORKDIR;
|
|
|
|
$title = "A same log file is specified more than once";
|
2021-02-06 19:37:28 +08:00
|
|
|
$suf = "." . random_word;
|
|
|
|
do { $trim_suf = "." . random_word; } until $trim_suf ne $suf;
|
2021-02-01 15:06:51 +08:00
|
|
|
# (2-4 times available compression) log files
|
2021-02-06 19:37:28 +08:00
|
|
|
$_ = 2 + (has_no_gzip? 0: 2) + (has_no_bzip2? 0: 2);
|
2021-02-01 15:06:51 +08:00
|
|
|
$num = $_ + int rand $_;
|
2021-02-06 19:37:28 +08:00
|
|
|
my %types = qw();
|
2021-02-01 15:06:51 +08:00
|
|
|
# At least 2 files for each available compression
|
2021-02-06 19:37:28 +08:00
|
|
|
foreach my $st (@SOURCE_TYPES) {
|
|
|
|
next if ($$st{"type"} eq TYPE_GZIP && has_no_gzip)
|
|
|
|
|| ($$st{"type"} eq TYPE_BZIP2 && has_no_bzip2);
|
|
|
|
@_ = grep !exists $types{$_}, (0...$num-1);
|
|
|
|
$types{$_[int rand @_]} = $st;
|
|
|
|
@_ = grep !exists $types{$_}, (0...$num-1);
|
|
|
|
$types{$_[int rand @_]} = $st;
|
2021-02-01 15:06:51 +08:00
|
|
|
}
|
|
|
|
# Set random compression on the rest files
|
2021-02-06 19:37:28 +08:00
|
|
|
foreach (grep !exists $types{$_}, (0...$num-1)) {
|
2021-02-01 15:06:51 +08:00
|
|
|
do {
|
2021-02-06 19:37:28 +08:00
|
|
|
$types{$_} = $SOURCE_TYPES[int rand @SOURCE_TYPES];
|
|
|
|
} until !(${$types{$_}}{"type"} eq TYPE_GZIP && has_no_gzip)
|
|
|
|
&& !(${$types{$_}}{"type"} eq TYPE_BZIP2 && has_no_bzip2);
|
2021-02-01 15:06:51 +08:00
|
|
|
}
|
2021-02-06 19:37:28 +08:00
|
|
|
@st = map $types{$_}, (0...$num-1);
|
2021-02-01 15:06:51 +08:00
|
|
|
@fs = qw();
|
|
|
|
@fo = qw();
|
|
|
|
@cs = qw();
|
|
|
|
@csr = qw();
|
|
|
|
@co = qw();
|
|
|
|
@fle = qw();
|
|
|
|
%logfiles = qw();
|
|
|
|
for (my $k = 0; $k < $num; $k++) {
|
|
|
|
my ($logfile, $cs, $csr, $co);
|
2021-02-06 19:37:28 +08:00
|
|
|
do { $logfile = random_word } until !exists $logfiles{$logfile};
|
2021-02-01 15:06:51 +08:00
|
|
|
$logfiles{$logfile} = 1;
|
2021-02-06 19:37:28 +08:00
|
|
|
push @fs, catfile($WORKDIR, "$logfile$trim_suf" . ${$st[$k]}{"suf"});
|
2021-02-01 15:06:51 +08:00
|
|
|
push @fo, catfile($WORKDIR, "$logfile$suf" . ${$st[$k]}{"suf"});
|
2021-02-06 19:37:28 +08:00
|
|
|
($cs, $csr) = make_log_file $fs[$k];
|
2021-02-01 15:06:51 +08:00
|
|
|
push @cs, $cs;
|
|
|
|
push @csr, $csr;
|
|
|
|
push @fle, basename($fs[$k]);
|
|
|
|
# 1: create existing file, 0: no existing file
|
|
|
|
if (int rand 1) {
|
2021-02-06 19:37:28 +08:00
|
|
|
$co = (make_log_file $fo[$k])[0];
|
2021-02-01 15:06:51 +08:00
|
|
|
push @co, $co;
|
|
|
|
push @fle, basename($fo[$k]);
|
|
|
|
} else {
|
|
|
|
push @co, "";
|
|
|
|
}
|
|
|
|
}
|
2021-02-06 19:37:28 +08:00
|
|
|
preserve_source $WORKDIR;
|
2021-02-01 15:06:51 +08:00
|
|
|
$dup = $fs[int rand @fs];
|
|
|
|
$_ = int rand(@fs + 1);
|
|
|
|
@_ = (@fs[0...$_-1], $dup, @fs[$_...$#fs]);
|
2021-02-06 19:37:28 +08:00
|
|
|
@_ = ($reslog, qw(-d -d -d -o a), "-s", $suf, "-t", $trim_suf, @_);
|
2021-02-01 15:06:51 +08:00
|
|
|
$cmd = join(" ", @_);
|
2021-02-06 19:37:28 +08:00
|
|
|
($ret_no, $out, $err) = run_cmd "", @_;
|
|
|
|
($fle, $flr) = (join(" ", sort @fle), list_files $WORKDIR);
|
2021-02-01 15:06:51 +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"});
|
2021-02-06 19:37:28 +08:00
|
|
|
($crf{$frb}, $trf{$frb}) = (read_file $fr, file_type $fr);
|
2021-02-01 15:06:51 +08:00
|
|
|
if ($co[$k] ne "") {
|
|
|
|
$fr = $fo[$k];
|
|
|
|
$frb = basename($fr);
|
|
|
|
($cef{$frb}, $tef{$frb}) = ($co[$k], ${$st[$k]}{"type"});
|
2021-02-06 19:37:28 +08:00
|
|
|
($crf{$frb}, $trf{$frb}) = (read_file $fr, file_type $fr);
|
2021-02-01 15:06:51 +08:00
|
|
|
}
|
|
|
|
}
|
2021-02-06 19:37:28 +08:00
|
|
|
die "$title\n$cmd\n$out$err" unless $ret_no != 0;
|
2021-02-01 15:06:51 +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"
|
2021-02-06 19:37:28 +08:00
|
|
|
unless has_no_file || $trf{$fr} eq $tef{$fr};
|
2021-02-01 15:06:51 +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;
|
|
|
|
};
|
|
|
|
ok($_, 1, $@);
|
2021-02-06 19:37:28 +08:00
|
|
|
clean_up $_, $WORKDIR, ++$tno;
|