summaryrefslogtreecommitdiffstats
path: root/tests/parser.tcl
blob: 1adeac255b6e994a63d335753c4dee27c2207cce (plain)
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
# rsyslog parser tests
# This is a first version, and can be extended and improved for
# sure. But it is far better than nothing. Please note that this
# script works together with the config file AND easily extensible
# test case files (*.parse1) to run a number of checks. All test
# cases are executed, even if there is a failure early in the
# process. When finished, the numberof failed tests will be given.
#
# Note: a lot of things are not elegant, but at least they work...
# Even simple things seem to be somewhat non-simple if you are
# not sufficiently involved with tcl/expect ;) -- rgerhards
#
# call: tclsh parser.tcl /director/with/testcases
# 
# Copyright (C) 2009 by Rainer Gerhards and Adiscon GmbH
#
# This file is part of rsyslog.

package require Expect
package require udp 1.0
log_user 0; # comment this out if you would like to see rsyslog output for testing

if {$argc > 1} {
	puts "invalid number of parameters, usage: tclsh parser.tcl /directory/with/testcases";
	exit 1;
}
if {$argc == 0 } {
	set srcdir ".";
} else {
	set srcdir "$argv";
}

set rsyslogdPID [spawn "../tools/rsyslogd" "-c4" "-f$srcdir/testruns/parser.conf" "-u2" "-n" "-irsyslog.pid" "-M../runtime/.libs"];
#interact;
expect "}}"; # eat startup message
set udpSock [udp_open];
udp_conf $udpSock 127.0.0.1 12514
set files [glob "$srcdir/testruns/*.parse1"]
set failed 0;
puts "\nExecuting parser test suite...";

set i 0;

foreach testcase $files {
	puts "testing $testcase";
	set fp [open "$testcase" r];
	fconfigure $fp -buffering line
	gets $fp input
	gets $fp expected
	# assemble "expected" to match the template we use
	close $fp;

	# send to daemon
	puts $udpSock $input;
	flush $udpSock;

	# get response and compare
	expect -re "{{.*}}";

	set result $expect_out(buffer);
	set result [string trimleft $result "\{\{"];
	set result [string trimright $result "\}\}"];

	if { $result != $expected } {
		puts "failed!";
		puts "expected: '$expected'\n";
		puts "returned: '$result'\n";
		puts "\n";
		set failed [expr {$failed + 1}];
	};
	set i [expr {$i + 1}];
}

exec kill $rsyslogdPID;
close $udpSock;

puts "Total number of tests: $i, number of failed tests: $failed.\n";
if { $failed != 0 } { exit 1 };