Skip to content

Commit ae939ba

Browse files
committed
tests for tail-log-to-ndjson
1 parent 4d52aa4 commit ae939ba

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

t/tail-log-to-ndjson.t

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/perl -w
2+
# -*- cperl -*-
3+
4+
#
5+
# Author: Slaven Rezic
6+
#
7+
8+
use strict;
9+
use warnings;
10+
use autodie;
11+
use FindBin;
12+
use Test::More;
13+
14+
BEGIN {
15+
if (!eval { require IPC::Run; IPC::Run->import(qw(run)); 1 }) {
16+
plan skip_all => 'IPC::Run required';
17+
exit 0;
18+
}
19+
}
20+
plan 'no_plan';
21+
22+
use File::Temp qw(tempdir);
23+
use Getopt::Long;
24+
25+
sub slurp ($);
26+
27+
my $script = "$FindBin::RealBin/../bin/tail-log-to-ndjson.pl";
28+
29+
my $metabase_log_dir = tempdir("metabase-log-XXXXXXXX", TMPDIR => 1, CLEANUP => 1);
30+
my $ndjson_dir = "$metabase_log_dir/tail-log-as-ndjson";
31+
my $json_dir = "$metabase_log_dir/log-as-json";
32+
my $logfile = "$metabase_log_dir/ndjson-log.log";
33+
my $statusfile = "$metabase_log_dir/ndjson-statusfile";
34+
my $logtxt = "$metabase_log_dir/log.txt";
35+
36+
GetOptions(
37+
"debug" => \my $debug,
38+
"keep!" => sub {
39+
if ($_[1]) {
40+
$File::Temp::KEEP_ALL = 1;
41+
warn "Keep temporary files in $metabase_log_dir\n";
42+
}
43+
},
44+
)
45+
or die "usage?";
46+
47+
for my $dir ($ndjson_dir, $json_dir) {
48+
mkdir $dir;
49+
}
50+
51+
my @cmd = (
52+
$^X, $script,
53+
"--ndjson-dir", $ndjson_dir,
54+
"--json-dir", $json_dir,
55+
"-logfile", $logfile,
56+
"-statusfile", $statusfile,
57+
$logtxt,
58+
);
59+
60+
{
61+
open my $ofh, '>', $logtxt;
62+
print $ofh <<'EOF';
63+
The last 1000 reports as of 2025-10-18T08:00:04Z:
64+
[2025-10-18T07:59:49Z] [Carlos Guevara] [pass] [JKEENAN/IPC-System-Simple-1.30.tar.gz] [i86pc-solaris-thread-multi-64] [perl-v5.43.4] [6bad3778-abf8-11f0-8c1e-b80f595f57ba] [2025-10-18T07:59:49Z]
65+
[2025-10-18T07:59:43Z] [Carlos Guevara] [unknown] [JDHEDDEN/threads-2.21.tar.gz] [amd64-netbsd-thread-multi] [perl-v5.43.4] [67e088d4-abf8-11f0-97a1-bb460411313d] [2025-10-18T07:59:43Z]
66+
EOF
67+
close $ofh;
68+
69+
my $exptected_IPC_System_Simple_ndjson_contents = <<'EOF';
70+
{"archname":"i86pc-solaris-thread-multi-64","distribution":"IPC-System-Simple","fulldate":"202510180759","guid":"6bad3778-abf8-11f0-8c1e-b80f595f57ba","osname":"solaris","perl":"5.43.4","status":"PASS","tester":"Carlos Guevara","version":"1.30"}
71+
EOF
72+
my $exptected_threads_ndjson_contents = <<'EOF';
73+
{"archname":"amd64-netbsd-thread-multi","distribution":"threads","fulldate":"202510180759","guid":"67e088d4-abf8-11f0-97a1-bb460411313d","osname":"netbsd","perl":"5.43.4","status":"UNKNOWN","tester":"Carlos Guevara","version":"2.21"}
74+
EOF
75+
76+
for my $pass (1..2) {
77+
run(\@cmd, '2>', \my $err) or fail "@cmd failed (pass $pass)";
78+
diag "pass $pass\ncommand: @cmd\nstderr:\n$err" if $debug;
79+
if ($pass == 1) {
80+
like $err, qr{\QIPC-System-Simple.ndjson... (first-time creation) (no existing \E.*/log-as-json/IPC-System-Simple.json\Q...) (writing data...)}, "expected diagnostics for IPC-System-Simple (pass $pass)";
81+
like $err, qr{\Qthreads.ndjson... (first-time creation) (no existing \E.*/log-as-json/threads.json\Q...) (writing data...)}, "expected diagnostics for threads (pass $pass)";
82+
} else {
83+
like $err, qr{\QIPC-System-Simple.ndjson... (append to existing ndjson file...) (found last guid...) (no new data found...)}, "expected diagnostics for IPC-System-Simple (pass $pass)";
84+
like $err, qr{\Qthreads.ndjson... (append to existing ndjson file...) (found last guid...) (no new data found...)}, "expected diagnostics for threads (pass $pass)";
85+
}
86+
is slurp("$ndjson_dir/IPC-System-Simple.ndjson"), $exptected_IPC_System_Simple_ndjson_contents, 'IPC-System-Simple.ndjson contents OK';
87+
is slurp("$ndjson_dir/threads.ndjson"), $exptected_threads_ndjson_contents, 'threads.ndjson contents OK';
88+
my @ndjson_files = <$ndjson_dir/*>;
89+
is scalar(@ndjson_files), 2, 'expected two files';
90+
}
91+
}
92+
93+
# REPO BEGIN
94+
# REPO NAME slurp /home/e/eserte/src/srezic-repository
95+
# REPO MD5 241415f78355f7708eabfdb66ffcf6a1
96+
sub slurp ($) {
97+
my($file) = @_;
98+
my $fh;
99+
my $buf;
100+
open $fh, $file
101+
or die "Can't slurp file $file: $!";
102+
local $/ = undef;
103+
$buf = <$fh>;
104+
close $fh;
105+
$buf;
106+
}
107+
# REPO END
108+
109+
__END__

0 commit comments

Comments
 (0)