/
test-henzell.pl
executable file
·369 lines (314 loc) · 8.5 KB
/
test-henzell.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#! /usr/bin/env perl
use strict;
use warnings;
use utf8;
use Time::localtime;
use File::stat;
use lib 'lib';
use Henzell::Cmd;
use Henzell::DB;
use Henzell::CommandService;
use open qw/:std :encoding(UTF-8)/;
$ENV{LEARNDB} = 'tmp/test.learn.db';
$ENV{HENZELL_SQL_QUERIES} = 'y';
$ENV{HENZELL_TEST} = 'y';
$ENV{RUBYOPT} = '-Isrc';
$ENV{HENZELL_ALL_COMMANDS} = 'y';
$ENV{PERL_UNICODE} = 'AS';
$ENV{IRC_NICK_AUTHENTICATED} = 'y';
mkdir 'tmp';
unlink $ENV{LEARNDB};
my $DB_DIRTY;
my $DBNAME = 'sequell_test';
my $DBUSER = 'sequell';
my $DBPASS = '';
$ENV{'SEQUELL_DBNAME'} = $DBNAME;
$ENV{'SEQUELL_DBUSER'} = $DBUSER;
$ENV{'SEQUELL_DBPASS'} = $DBPASS;
my $FAILFAST;
my $TESTFILE = 'testcmd.txt';
my $TESTLOG = 'test.log';
my $TESTDIR = 'tests';
my $DATADIR = "$TESTDIR/data";
my @COMMAND_FILES = glob('config/commands-*.txt');
my @FAILED_TESTS;
my @OK_TESTS;
my $TESTNICK = 'Yermak';
my $TIMESTAMP_QUERY = <<QUERY;
SELECT last_update FROM canary;
QUERY
sub announce($) {
print STDERR "* ", @_, "\n";
}
sub test_ok($) {
my $test = shift;
push @OK_TESTS, $test;
}
sub test_failed($$) {
my ($test, $failure) = @_;
push @FAILED_TESTS, { test => $test, failure => $failure };
}
sub datafiles() {
grep(m{/test.remote[.]}, glob("$DATADIR/*"))
}
sub datafiles_newest_time() {
my $newest;
for my $file (datafiles()) {
my $mtime = stat($file)->mtime;
$newest = $mtime if !$newest || $mtime > $newest;
}
$newest = time_to_dbdate(localtime($newest)) if $newest;
$newest
}
sub with_db(&) {
my $sub = shift;
my $db = Henzell::DB::new_db_handle($DBNAME, $DBUSER, $DBPASS);
my $result = $sub->($db);
$db->disconnect;
$result
}
sub db_schema_missing() {
my $schema_missing = with_db {
my $dbh = shift;
!$dbh->selectall_arrayref($TIMESTAMP_QUERY);
};
announce "DB schema is missing or incomplete" if $schema_missing;
$schema_missing
}
sub db_canary_time() {
with_db {
my $dbh = shift;
my $ref = $dbh->selectall_arrayref($TIMESTAMP_QUERY);
return $ref && @$ref? $ref->[0][0] : undef;
}
}
sub execute_statement($$@) {
my ($dbh, $text, @values) = @_;
my $st = $dbh->prepare_cached($text)
or die "Malformed statement: $text\n";
$st->execute(@values);
$st
}
sub db_canary_set_time() {
with_db {
my $dbh = shift;
$dbh->do("DELETE FROM canary");
execute_statement($dbh, "INSERT INTO canary (last_update) VALUES (?)",
time_to_dbdate(localtime()));
}
}
sub db_timestamp_stale {
my $canary_time = db_canary_time();
my $datafiles_newest_time = datafiles_newest_time();
if ($canary_time && $datafiles_newest_time &&
$datafiles_newest_time gt $canary_time)
{
announce("DB $DBNAME needs update: last updated: $canary_time, " .
"newest datafile: $datafiles_newest_time")
}
!$canary_time || $datafiles_newest_time gt $canary_time
}
sub seqdb {
my $cmdline = shift;
print STDERR "[EXEC] seqdb $cmdline\n";
system("seqdb $cmdline")
}
sub db_reset() {
announce("Rebuilding schema for $DBNAME");
seqdb("--db $DBNAME resetdb --force")
and die "failed to reset db";
with_db {
my $dbh = shift;
my $res = $dbh->do(<<CREATE_CANARY);
create table canary (last_update timestamp);
CREATE_CANARY
$res or die "Failed to create canary table: $!";
};
}
sub db_load_data() {
announce("Loading data into $DBNAME");
seqdb("--db $DBNAME load --force-source-dir $DATADIR") and
die "Couldn't load data into $DBNAME: $!\n";
}
sub db_create_indexes() {
announce("Creating indexes for $DBNAME");
seqdb("--db $DBNAME create-indexes") and
die "Couldn't create indexes for $DBNAME: $!\n";
}
sub time_to_dbdate($) {
my $time = shift;
sprintf("%04d-%02d-%02d %02d:%02d:%02d",
$time->year() + 1900,
$time->mon() + 1,
$time->mday(),
$time->hour(),
$time->min(),
$time->sec())
}
sub dbdate_to_time($) {
my $dbdate = shift;
print "DB date: $dbdate\n";
$dbdate
}
sub build_test_db() {
$DB_DIRTY = db_schema_missing() || db_timestamp_stale();
if ($DB_DIRTY) {
announce "Database needs update";
db_reset();
db_load_data();
db_canary_set_time();
db_create_indexes();
}
}
sub trim($) {
my $text = shift;
s/^\s+//, s/\s+$// for $text;
$text
}
sub parse_test($) {
my %test;
my $text = shift;
if ($text =~ /^\$\s*(.*)/) {
$test{shell} = $1;
return \%test;
}
if ($text =~ s/^E\s+(.*)/$1/) {
$test{err} = 1;
}
die "Malformed test line\n" unless $text =~ /\S/;
my ($cmd) = $text =~ /^(\S+)/;
$test{cmd} = $cmd;
if ($text =~ /::(!?)~(.*)/) {
my $key = $1? 'regex_not_match' : 'regex_match';
$test{$key} = trim($2);
}
if ($text =~ /::(!?)=(.*)/) {
my $key = $1? 'exact_not_match' : 'exact_match';
$test{$key} = trim($2);
}
$text =~ s/\s+::!?[~=].*//;
$test{line} = $text;
\%test
}
sub read_tests() {
my $file = "tests/$TESTFILE";
open my $inf, '<', $file or die "Can't read tests from $file: $!\n";
my @tests;
while (<$inf>) {
chomp;
s/^\s+//, s/\s+$//;
next if /^#/;
next unless /\S/;
push @tests, parse_test($_);
}
@tests
}
sub execute_cmd($) {
Henzell::Cmd::load_all_commands();
Henzell::Cmd::execute_cmd($TESTNICK, shift)
}
sub execute_test($$$$) {
my ($test_index, $total_test_count, $test, $logf) = @_;
if ($$test{shell}) {
my $output = qx/$$test{shell} 2>&1/;
print $logf <<TESTREPORT;
SHELL EXEC: $$test{shell}
Output:
$output
TESTREPORT
return;
}
print STDERR "> $test_index/$total_test_count: $$test{line}\n";
my ($exitcode, $output, $cmd) = execute_cmd($$test{line});
$output = Henzell::CommandService->handle_output($output, 1) || '';
chomp $output;
$$test{cmdline} = $cmd;
print $logf <<TESTREPORT;
-----------------------------------------------------------------------
$$test{line}::
Command line: $cmd
Exit code: $exitcode
Output:
$output
TESTREPORT
my $err =
($exitcode && !$$test{err}) ? "$cmd error:\n$output\n" :
(!$exitcode && $$test{err}) ? "$cmd ok, but expected error:\n$output\n" :
$$test{regex_match} && $output !~ /$$test{regex_match}/m?
("Output '$output' does not contain expected match: " .
"$$test{regex_match}: $output") :
$$test{regex_not_match} && $output =~ /$$test{regex_not_match}/m?
("Output '$output' contains forbidden match: " .
"$$test{regex_not_match}: $output") :
$$test{exact_match} && $output ne $$test{exact_match}?
("Output '$output' does not exactly equal " .
"expected '$$test{exact_match}': $output") :
$$test{exact_not_match} && $output eq $$test{exact_not_match}?
("Output equals forbidden output '$$test{exact_not_match}" .
": $output") :
'';
if ($err) {
print $logf "Error: $err\n";
test_failed($test, $err);
return 1;
}
print $logf "TEST SUCCESS\n";
test_ok($test);
return;
}
sub test_failure_report($) {
my $failure = shift;
my $test = $failure->{test};
my $err = $failure->{failure};
my $header = "--------------------------------------------------";
return ("$header\nTest: $$test{line}\nCommand line: $$test{cmdline}"
. "\nError: $err\n$header\n");
}
sub test_summary($) {
my $logf = shift;
print $logf <<ENDBANNER;
============================================================================
ENDBANNER
my $ok = @OK_TESTS;
my $fail = @FAILED_TESTS;
my $total = $ok + $fail;
print $logf "$total tests executed, $fail failures, $ok successful.\n";
if ($fail) {
print $logf "Failing tests:\n" . join("\n",
map(test_failure_report($_),
@FAILED_TESTS)) . "\n";
}
print $logf <<ENDBANNER2;
============================================================================
ENDBANNER2
}
sub matches_argv_filter($) {
my $test = shift;
return scalar(grep($$test{line} =~ /^\Q$_/, @ARGV));
}
sub filter_tests(@) {
my @tests = @_;
return @tests unless @ARGV;
grep($$_{shell} || matches_argv_filter($_), @tests)
}
sub run_tests() {
Henzell::Cmd::load_all_commands();
my @tests = filter_tests(read_tests());
my $test_count = grep(!$$_{shell}, @tests);
announce "Running $test_count tests";
open my $logf, '>', $TESTLOG or die "Can't write $TESTLOG: $!\n";
my $test_index = 0;
for my $test (@tests) {
last if execute_test(++$test_index, scalar(@tests), $test, $logf) && $FAILFAST;
}
test_summary($logf);
test_summary(\*STDERR);
}
sub main() {
$FAILFAST = grep(/--fail-fast/, @ARGV);
@ARGV = grep(!/^--/, @ARGV);
build_test_db();
run_tests();
exit(!!scalar(@FAILED_TESTS));
}
main();