forked from crawl/sequell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-henzell.pl
executable file
·381 lines (326 loc) · 8.81 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
370
371
372
373
374
375
376
377
378
379
380
381
#! /usr/bin/env perl
use strict;
use warnings;
use Time::localtime;
use File::stat;
use Henzell::Cmd;
$ENV{HENZELL_SQL_QUERIES} = 'y';
$ENV{HENZELL_TEST} = 'y';
require 'sqllog.pl';
my $DB_DIRTY;
my $DBNAME = 'henzell_test';
my $DBUSER = 'henzell';
my $DBPASS = '';
$ENV{'HENZELL_DBNAME'} = $DBNAME;
$ENV{'HENZELL_DBUSER'} = $DBUSER;
$ENV{'HENZELL_DBPASS'} = $DBPASS;
new_db_handle($DBNAME, $DBUSER, $DBPASS) && initialize_sqllog();
my $SCHEMAFILE = 'henzell.sql';
my $TESTFILE = 'testcmd.txt';
my $TESTLOG = 'test.log';
my $TESTDIR = 'tests';
my $DATADIR = "$TESTDIR/data";
my @LOGFILES = qw/remote.cdo-logfile-svn remote.cdo-logfile-spr
remote.cdo-logfile-zd/;
my @MILEFILES = qw/remote.cdo-milestones-svn
remote.cdo-milestones-spr
remote.cdo-milestones-zd/;
my @COMMAND_FILES = ('commands/commands-henzell.txt',
'commands/commands-sequell.txt');
my @FAILED_TESTS;
my @OK_TESTS;
my $TESTNICK = 'hyperbolic';
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 datafile_logs() {
@LOGFILES
}
sub datafile_milestones() {
@MILEFILES
}
sub datafiles() {
(datafile_logs(), datafile_milestones())
}
sub datafile_path($) {
"$DATADIR/" . shift
}
sub datafile_hashrefs(@) {
map({ file => datafile_path($_),
path => datafile_path($_),
src => 'cdo' },
@_)
}
sub check_datafiles_exist() {
for my $file (map(datafile_path($_), datafiles())) {
die "Cannot find data file \"$file\"\n" unless -r $file;
}
}
sub datafiles_newest_time() {
my $newest;
for my $file (map(datafile_path($_), 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 = 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 db_load_schema() {
announce "Rebuilding schema from $SCHEMAFILE";
with_db {
my $dbh = shift;
my $sql_ddl = do { local (@ARGV, $/) = $SCHEMAFILE; <> };
my @ddl_statements = split(/;/, $sql_ddl);
for my $statement (@ddl_statements) {
if ($statement =~ /\S/) {
$dbh->do($statement)
or die "Failed to load schema: error $! on $statement\n";
}
}
};
}
sub db_load_data() {
with_db {
my $dbh = shift;
my $timestamp = localtime();
for my $logfile (open_handles(datafile_hashrefs(datafile_logs()))) {
announce "Loading logfile data from $$logfile{file}";
sql_register_logfiles($logfile->{file});
cat_logfile($logfile);
}
for my $milestone (open_handles(datafile_hashrefs(datafile_milestones()))) {
announce "Loading milestone data from $$milestone{file}";
sql_register_milestones($milestone->{file});
cat_stonefile($milestone);
}
};
}
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_load_schema();
db_load_data();
db_canary_set_time();
}
}
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+)/;
die "Unknown command $cmd in test '$text'\n"
unless Henzell::Cmd::command_exists($cmd);
$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::execute_cmd($TESTNICK, shift)
}
sub execute_test($$) {
my ($test, $logf) = @_;
if ($$test{shell}) {
my $output = qx/$$test{shell} 2>&1/;
print $logf <<TESTREPORT;
SHELL EXEC: $$test{shell}
Output:
$output
TESTREPORT
return;
}
my ($exitcode, $output, $cmd) = execute_cmd($$test{line});
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" :
$$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;
}
print $logf "TEST SUCCESS\n";
test_ok($test);
}
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());
announce "Running " . scalar(@tests) . " tests";
open my $logf, '>', $TESTLOG or die "Can't write $TESTLOG: $!\n";
for my $test (@tests) {
execute_test($test, $logf);
}
test_summary($logf);
test_summary(\*STDERR);
}
sub main() {
check_datafiles_exist();
build_test_db();
run_tests();
exit(!!scalar(@FAILED_TESTS));
}
main();