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
|
#!/usr/bin/perl -w
#
# A simple driver for testing the subdomain parser.
# All files in $CWD named *.sd will be tested against the parser.
#
use strict;
use Getopt::Long;
use Test::More;
my %config;
$config{'parser'} = "/sbin/subdomain_parser";
$config{'profiledir'} = "./simple_tests/";
$config{'timeout'} = 120; # in seconds
my $help;
my $pwd = `pwd`;
chomp($pwd);
GetOptions(
"help|h" => \$help,
);
sub usage {
print STDERR "Usage $0 profile_directory\n";
print STDERR "\tTests the subdomain parser on the given profile directory\n";
print STDOUT "Bail out! Got the usage statement\n";
exit 0;
}
&usage if ($help);
read_config();
# Override config file profile location when passed on command line
if (@ARGV >= 1) {
$config{'profiledir'} = shift;
}
if ($config{'profiledir'} =~ /^\//) {
$config{'includedir'} = $config{'profiledir'};
} else {
$config{'includedir'} = "$pwd/$config{'profiledir'}";
}
sub read_config {
my $which;
if(open(CONF, "uservars.conf")) {
while(<CONF>) {
chomp;
next if /^\s*#/;
if (m/^\s*(\S+)\s*=\s*(.+)\s*$/) {
my ($key, $value) = ($1, $2);
$config{$key} = $value;
}
}
close(CONF);
}
}
sub test_profile {
my $profile = shift;
my $description = "no description for testcase";
my $expass = 1;
my $istodo = 0;
my $isdisabled = 0;
my $result = 0;
my $child;
eval {
local $SIG{ALRM} = sub {
$result = 1;
kill PIPE => $child;
$description = "$description - TESTCASE TIMED OUT";
};
alarm $config{'timeout'};
$child = open(PARSER, "|-");
if ($child == 0) {
# child
open(STDOUT, ">/dev/null") or die "Failed to redirect STDOUT";
open(STDERR, ">/dev/null") or die "Failed to redirect STDERR";
exec("$config{'parser'}", "-S", "-I", "$config{'includedir'}") or die "Bail out! couldn't open parser";
# noreturn
}
# parent
open(PROFILE, $profile) or die "Bail out! couldn't open profile $profile";
while (<PROFILE>) {
if (/^#=DESCRIPTION\s*(.*)/) {
$description = $1;
} elsif (/^#=EXRESULT\s*(\w+)/) {
if ($1 eq "PASS") {
$expass = 1;
} elsif ($1 eq "FAIL") {
$expass = 0;
} else {
die "Bail out! unknown expected result '$1' in $profile";
}
} elsif (/^#=TODO\s*/) {
$istodo = 1;
} elsif (/^#=DISABLED\s*/) {
$isdisabled = 1;
} else {
print PARSER if not $isdisabled;
}
}
$result = close(PARSER);
alarm 0;
};
alarm 0;
if ($isdisabled) {
TODO: {
local $TODO = "Disabled testcase.";
ok(0, "TODO: $profile: $description");
}
} elsif ($istodo) {
TODO: {
local $TODO = "Unfixed testcase.";
ok($expass ? $result : !$result, "TODO: $profile: $description");
}
} else {
ok($expass ? $result : !$result, "$profile: $description");
}
}
sub find_all_tests {
my $testdir = shift;
opendir(DIR, $testdir) or die "Bail out! can't opendir $testdir: $!";
my @files = sort grep { /\.sd$/ && -f "$testdir/$_" } readdir(DIR);
closedir(DIR);
my @profiles;
foreach my $profile (@files) {
push (@profiles, "$testdir/$profile");
}
opendir(DIR, $testdir) or die "Bail out! can't opendir $testdir: $!";
my @dirs = sort grep { /^[^\.]/ && -d "$testdir/$_" } readdir(DIR);
closedir(DIR);
foreach my $dir (@dirs) {
push(@profiles, find_all_tests("$testdir/$dir"));
}
return @profiles;
}
my @profiles = find_all_tests($config{'profiledir'});
plan tests => scalar(@profiles);
foreach my $profile (@profiles) {
test_profile ("$profile");
}
|