-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrl-test.pl
executable file
·170 lines (129 loc) · 3.86 KB
/
krl-test.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
#!/usr/bin/perl -w
use lib '.';
use strict;
use utf8;
use Getopt::Std;
# use Data::Dumper;
use YAML::XS;
use POSIX qw(strftime);
use Kinetic::Raise;
use constant DEFAULT_CONFIG_FILE => './test_config.yml';
use constant DEFAULT_RULES_ENGINE => 'kibdev.kobj.net';
# global options
use vars qw/ %clopt /;
my $opt_string = 'c:?hvdt:';
getopts( "$opt_string", \%clopt ) or usage();
usage() if $clopt{'h'} || $clopt{'?'};
print "No test file specified. Using " . DEFAULT_CONFIG_FILE . "\n" unless $clopt{'c'};
my $config = read_config($clopt{'c'});
my $eci = $config->{'eci'};
my $server = $config->{'rules_engine'} || DEFAULT_RULES_ENGINE;
# find tests
my @config_keys = keys %{ $config };
my @tests = sort(grep(/^test_/, @config_keys));
my %spec_tests;
$spec_tests{$_} = 1 for split(/,/, $clopt{"t"});
my $global_succ = 0;
my $global_fail = 0;
my $global_diag = 0;
foreach my $test_key (@tests) {
my $test = $config->{$test_key};
if ( %spec_tests &&
! $spec_tests{$test_key}) {
print "Skipping $test_key because it's not in the test list\n" if $clopt{"v"};
next;
}
my $options = {'eci' => $eci,
'host' => $server,
};
$options->{"rids"} = $config->{"rids"} if $config->{'rids'};
$options->{'rids'} = $test->{'rids'} if $test->{'rids'};
# print Dump $test;
# print Dump $options;
my $event = Kinetic::Raise->new($test->{'domain'},
$test->{'type'},
$options
);
my $eid = $test_key."_".time;
my $response = $event->raise($test->{'attributes'}, {"eid" => $eid, "esl" => 1});
my $succ = 0;
my $fail = 0;
my $diag = 0;
my $ran = 0;
if ($clopt{"v"}) {
print "\nTest ESL: ", $response->{"_esl"}, "\n\n";
}
foreach my $d (@{$response->{'directives'}}) {
my $opt = $d->{'options'};
if ($opt->{'status'} eq 'success') {
$succ++;
$ran++;
} elsif (($opt->{'status'} eq 'failure')) {
$fail++;
$ran++;
} else {
$diag++;
}
if ($clopt{'v'}) {
my $content_str = "";
if ( $opt->{'status'} eq 'success' ||
$opt->{'status'} eq 'failure' ||
$clopt{'d'}
) {
print ">> " if $opt->{'status'} eq 'failure';
print $opt->{'status'}, " ";
print "<< " if $opt->{'status'} eq 'failure';
print $opt->{'rule'}, ": " if $opt->{'rule'};
print $d->{'name'};
print $opt->{"msg"} if $opt->{"msg"};
print "\n";
}
if ($clopt{'d'}) { # print diagnositcs too
my $content = JSON::XS->new->decode($d->{'options'}->{'details'});
$content_str = JSON::XS->new->ascii->pretty->encode($content) . "\n";
}
print $content_str;
}
}
$global_succ += $succ;
$global_fail += $fail;
$global_diag += $diag;
if ($ran != $test->{'expect'}) {
print ">> WARNING << Expected ", $test->{"expect"}, " test but ran $ran\n";
}
print $test_key, " ", $test->{'desc'}, " ($eid): ", $succ , " succeeded, ", $fail, " failed, ", $diag, " diagnostic messages\n---------------------------------- $eid ---------------------------------\n\n";
}
print "Summary: ", $global_succ , " succeeded, ", $global_fail, " failed, ", $global_diag, " diagnostic messages\n";
if ($global_fail) {
exit 1
} else {
exit 0
}
1;
sub read_config {
my ($filename) = @_;
$filename ||= DEFAULT_CONFIG_FILE;
# print "File ", $filename;
my $config;
if ( -e $filename ) {
$config = YAML::XS::LoadFile($filename) ||
warn "Can't open configuration file $filename: $!";
}
return $config;
}
#
# Message about this program and how to use it
#
sub usage {
print STDERR << "EOF";
Test harness for running KRL tests
usage: $0 [-h?] -c config_file.yml
-h|? : this (help) message
-c file : configuration file
-v : vebose, print diagnostic messges
-d : print details accompanying diagnostics (no effect wthout -v switch).
-t : test to run as a comma separated list of names
example: $0 -c test_AWS.yml -vd
EOF
exit;
}