]> git.proxmox.com Git - pve-firewall.git/blobdiff - test/fwtester.pl
do not use -s for outgoing corosync rules
[pve-firewall.git] / test / fwtester.pl
index 832d35eea66a2557a3ff684f81039029c46a107c..8faee3f977ce1ed83e99095a92d6e127cbb9a9a0 100755 (executable)
@@ -5,12 +5,26 @@ use strict;
 use warnings;
 use Data::Dumper;
 use PVE::Firewall;
+use Getopt::Long;
+use File::Basename;
+use Net::IP;
 
 my $mark;
 my $trace;
 
 my $debug = 0;
 
+sub print_usage_and_exit {
+    die "usage: $0 [--debug] [testfile [testid]]\n";
+}
+
+if (!GetOptions ('debug' => \$debug)) {
+    print_usage_and_exit();
+}
+
+my $testfilename = shift;
+my $testid = shift;
+
 sub add_trace {
     my ($text) = @_;
 
@@ -21,112 +35,195 @@ sub add_trace {
     }
 }
 
+sub nf_dev_match {
+    my ($devre, $dev) = @_;
+
+    $devre =~ s/\+$/\.\*/;
+    return  ($dev =~ m/^${devre}$/) ? 1 : 0;
+}
+
+sub ipset_match {
+    my ($ipsetname, $ipset, $ipaddr) = @_;
+
+    my $ip = Net::IP->new($ipaddr);
+
+    foreach my $entry (@$ipset) {
+       next if $entry =~ m/^create/; # simply ignore
+       if ($entry =~ m/add \S+ (\S+)$/) {
+           my $test = Net::IP->new($1);
+           if ($test->overlaps($ip)) {
+               add_trace("IPSET $ipsetname match $ipaddr\n");
+               return 1;
+           }
+       } else {
+           die "implement me";
+       }
+    }
+
+    return 0;
+}
+
 sub rule_match {
-    my ($chain, $rule, $pkg) = @_;
+    my ($ipset_ruleset, $chain, $rule, $pkg) = @_;
 
     $rule =~ s/^-A $chain // || die "got strange rule: $rule";
 
-    if ($rule =~ s/^-m conntrack\s*//) {
-       return undef; # simply ignore
-    }
+    while (length($rule)) {
 
-    if ($rule =~ s/^-m addrtype\s*//) {
-       return undef; # simply ignore
-    }
+       if ($rule =~ s/^-m conntrack --ctstate (\S+)\s*//) {
+           my $cstate = $1;
 
-    if ($rule =~ s/^-i (\S+)\s*//) {
-       die "missing iface_in" if !$pkg->{iface_in};
-       return undef if $pkg->{iface_in} ne $1; # no match
-    }
-    if ($rule =~ s/^-o (\S+)\s*//) {
-       die "missing iface_out" if !$pkg->{iface_out};
-       return undef if $pkg->{iface_out} ne $1; # no match
-    }
+           return undef if $cstate eq 'INVALID'; # no match
+           return undef if $cstate eq 'RELATED,ESTABLISHED'; # no match
+           
+           next if $cstate =~ m/NEW/;
+           
+           die "please implement cstate test '$cstate'";
+       }
 
-    if ($rule =~ s/^-p (tcp|udp)\s*//) {
-       die "missing proto" if !$pkg->{proto};
-       return undef if $pkg->{proto} ne $1; # no match
-    }
+       if ($rule =~ s/^-m addrtype\s*//) {
+           return undef; # simply ignore
+       }
 
-    if ($rule =~ s/^--dport (\d+):(\d+)\s*//) {
-       die "missing dport" if !$pkg->{dport};
-       return undef if ($pkg->{dport} < $1) || ($pkg->{dport} > $2); # no match
-    }
+       if ($rule =~ s/^-i (\S+)\s*//) {
+           my $devre = $1;
+           die "missing iface_in" if !$pkg->{iface_in};
+           return undef if !nf_dev_match($devre, $pkg->{iface_in});
+           next;
+       }
 
-    if ($rule =~ s/^--dport (\d+)\s*//) {
-       die "missing dport" if !$pkg->{dport};
-       return undef if $pkg->{dport} != $1; # no match
-    }
+       if ($rule =~ s/^-o (\S+)\s*//) {
+           my $devre = $1;
+           die "missing iface_out" if !$pkg->{iface_out};
+           return undef if !nf_dev_match($devre, $pkg->{iface_out});
+           next;
+       }
 
-    if ($rule =~ s/^-s (\S+)\s*//) {
-       die "missing source" if !$pkg->{source};
-       return undef if $pkg->{source} ne $1; # no match
-    }
+       if ($rule =~ s/^-p (tcp|udp)\s*//) {
+           die "missing proto" if !$pkg->{proto};
+           return undef if $pkg->{proto} ne $1; # no match
+           next;
+       }
+
+       if ($rule =~ s/^--dport (\d+):(\d+)\s*//) {
+           die "missing dport" if !$pkg->{dport};
+           return undef if ($pkg->{dport} < $1) || ($pkg->{dport} > $2); # no match
+           next;
+       }
+
+       if ($rule =~ s/^--dport (\d+)\s*//) {
+           die "missing dport" if !$pkg->{dport};
+           return undef if $pkg->{dport} != $1; # no match
+           next;
+       }
+
+       if ($rule =~ s/^-s (\S+)\s*//) {
+           die "missing source" if !$pkg->{source};
+           my $ip = Net::IP->new($1);
+           return undef if !$ip->overlaps(Net::IP->new($pkg->{source})); # no match
+           next;
+       }
     
-    if ($rule =~ s/^-d (\S+)\s*//) {
-       die "missing destination" if !$pkg->{dest};
-       return undef if $pkg->{dest} ne $1; # no match
-    }
+       if ($rule =~ s/^-d (\S+)\s*//) {
+           die "missing destination" if !$pkg->{dest};
+           my $ip = Net::IP->new($1);
+           return undef if !$ip->overlaps(Net::IP->new($pkg->{dest})); # no match
+           next;
+       }
 
-    if ($rule =~ s/^-m mac ! --mac-source (\S+)\s*//) {
-       die "missing source mac" if !$pkg->{mac_source};
-       return undef if $pkg->{mac_source} eq $1; # no match
-    }
+       if ($rule =~ s/^-m set --match-set (\S+) src\s*//) {
+           die "missing source" if !$pkg->{source};
+           my $ipset = $ipset_ruleset->{$1};
+           die "no such ip set '$1'" if !$ipset;
+           return undef if !ipset_match($1, $ipset, $pkg->{source});
+           next;
+       }
 
-    if ($rule =~ s/^-m physdev --physdev-is-bridged --physdev-in (\S+)\s*//) {
-       my $devre = $1;
-       $devre =~ s/\+/\.\*/;
-       return undef if !$pkg->{physdev_in};
-       return undef if $pkg->{physdev_in} !~ m/^${devre}$/;
-    }
+       if ($rule =~ s/^-m set --match-set (\S+) dst\s*//) {
+           die "missing destination" if !$pkg->{dest};
+           my $ipset = $ipset_ruleset->{$1};
+           die "no such ip set '$1'" if !$ipset;
+           return undef if !ipset_match($1, $ipset, $pkg->{dest});
+           next;
+       }
 
-    if ($rule =~ s/^-m physdev --physdev-is-bridged --physdev-out (\S+)\s*//) {
-       my $devre = $1;
-       $devre =~ s/\+/\.\*/;
-       return undef if !$pkg->{physdev_out};
-       return undef if $pkg->{physdev_out} !~ m/^${devre}$/;
-    }
+       if ($rule =~ s/^-m mac ! --mac-source (\S+)\s*//) {
+           die "missing source mac" if !$pkg->{mac_source};
+           return undef if $pkg->{mac_source} eq $1; # no match
+           next;
+       }
 
-    if ($rule =~ s/^-j MARK --set-mark (\d+)\s*$//) {
-       $mark = $1;
-       return undef;
-    }
+       if ($rule =~ s/^-m physdev --physdev-is-bridged --physdev-in (\S+)\s*//) {
+           my $devre = $1;
+           return undef if !$pkg->{physdev_in};
+           return undef if !nf_dev_match($devre, $pkg->{physdev_in});
+           next;
+       }
 
-    if ($rule =~ s/^-j (\S+)\s*$//) {
-       return (0, $1);
-    }
+       if ($rule =~ s/^-m physdev --physdev-is-bridged --physdev-out (\S+)\s*//) {
+           my $devre = $1;
+           return undef if !$pkg->{physdev_out};
+           return undef if !nf_dev_match($devre, $pkg->{physdev_out});
+           next;
+       }
 
-    if ($rule =~ s/^-g (\S+)\s*$//) {
-       return (1, $1);
+       if ($rule =~ s/^-m mark --mark (\d+)\s*//) {
+           return undef if !defined($mark) || $mark != $1;
+           next;
+       }
+
+       # final actions
+
+       if ($rule =~ s/^-j MARK --set-mark (\d+)\s*$//) {
+           $mark = $1;
+           return undef;
+       }
+
+       if ($rule =~ s/^-j (\S+)\s*$//) {
+           return (0, $1);
+       }
+
+       if ($rule =~ s/^-g (\S+)\s*$//) {
+           return (1, $1);
+       }
+
+       if ($rule =~ s/^-j NFLOG --nflog-prefix \"[^\"]+\"$//) {
+           return undef; 
+       }
+
+       last;
     }
 
     die "unable to parse rule: $rule";
 }
 
 sub ruleset_simulate_chain {
-    my ($ruleset, $chain, $pkg) = @_;
+    my ($ruleset, $ipset_ruleset, $chain, $pkg) = @_;
 
     add_trace("ENTER chain $chain\n");
     
+    my $counter = 0;
+
     if ($chain eq 'PVEFW-Drop') {
        add_trace("LEAVE chain $chain\n");
-       return 'DROP';
+       return ('DROP', $counter);
     }
     if ($chain eq 'PVEFW-reject') {
        add_trace("LEAVE chain $chain\n");
-       return 'REJECT';
+       return ('REJECT', $counter);
     }
 
     if ($chain eq 'PVEFW-tcpflags') {
        add_trace("LEAVE chain $chain\n");
-       return undef;
+       return (undef, $counter);
     }
 
     my $rules = $ruleset->{$chain} ||
        die "no such chain '$chain'";
 
     foreach my $rule (@$rules) {
-       my ($goto, $action) = rule_match($chain, $rule, $pkg);
+       $counter++;
+       my ($goto, $action) = rule_match($ipset_ruleset, $chain, $rule, $pkg);
        if (!defined($action)) {
            add_trace("SKIP: $rule\n");
            next;
@@ -135,20 +232,20 @@ sub ruleset_simulate_chain {
        
        if ($action eq 'ACCEPT' || $action eq 'DROP' || $action eq 'REJECT') {
            add_trace("TERMINATE chain $chain: $action\n");
-           return $action;
+           return ($action, $counter);
        } elsif ($action eq 'RETURN') {
            add_trace("RETURN FROM chain $chain\n");
            last;
        } else {
            if ($goto) {
                add_trace("LEAVE chain $chain - goto $action\n");
-               return ruleset_simulate_chain($ruleset, $action, $pkg)
+               return ruleset_simulate_chain($ruleset, $ipset_ruleset, $action, $pkg)
                #$chain = $action;
                #$rules = $ruleset->{$chain} || die "no such chain '$chain'";
            } else {
-               if ($action = ruleset_simulate_chain($ruleset, $action, $pkg)) {
-                   return $action;
-               }
+               my ($act, $ctr) = ruleset_simulate_chain($ruleset, $ipset_ruleset, $action, $pkg);
+               $counter += $ctr;
+               return ($act, $counter) if $act;
                add_trace("CONTINUE chain $chain\n");
            }
        }
@@ -156,10 +253,10 @@ sub ruleset_simulate_chain {
 
     add_trace("LEAVE chain $chain\n");
     if ($chain =~ m/^PVEFW-(INPUT|OUTPUT|FORWARD)$/) {
-       return 'ACCEPT'; # default policy
+       return ('ACCEPT', $counter); # default policy
     }
 
-    return undef;
+    return (undef, $counter);
 }
 
 sub copy_packet {
@@ -183,6 +280,9 @@ sub route_packet {
 
     my $physdev_in;
 
+    my $ipt_invocation_counter = 0;
+    my $rule_check_counter = 0;
+
     while ($route_state ne $target->{iface}) {
 
        my $chain;
@@ -192,9 +292,17 @@ sub route_packet {
        $pkg->{iface_in} = $pkg->{iface_out} = undef;
        $pkg->{physdev_in} = $pkg->{physdev_out} = undef;
 
-       if ($route_state eq 'host') {
+       if ($route_state eq 'from-bport') {
+           $next_route_state = $from_info->{bridge} || die 'internal error';
+           $next_physdev_in = $from_info->{iface} || die 'internal error';
+       } elsif ($route_state eq 'host') {
 
-           if ($target->{type} eq 'ct') {
+           if ($target->{type} eq 'bport') {
+               $pkg->{iface_in} = 'lo';
+               $pkg->{iface_out} = $target->{bridge} || die 'internal error';
+               $chain = 'PVEFW-OUTPUT';
+               $next_route_state = $target->{iface} || die 'internal error';
+           } elsif ($target->{type} eq 'ct') {
                $pkg->{iface_in} = 'lo';
                $pkg->{iface_out} = 'venet0';
                $chain = 'PVEFW-OUTPUT';
@@ -217,6 +325,13 @@ sub route_packet {
                $pkg->{iface_out} = 'lo';
                $next_route_state = 'host';
 
+           } elsif ($target->{type} eq 'bport') {
+               
+               $chain = 'PVEFW-FORWARD';
+               $pkg->{iface_in} = 'venet0';
+               $pkg->{iface_out} = $target->{bridge} || die 'internal error';
+               $next_route_state = $target->{iface} || die 'internal error';
+
            } elsif ($target->{type} eq 'vm') {
 
                $chain = 'PVEFW-FORWARD';
@@ -257,6 +372,7 @@ sub route_packet {
        } elsif ($route_state =~ m/^vmbr\d+$/) {
            
            die "missing physdev_in - internal error?" if !$physdev_in;
+           $pkg->{physdev_in} = $physdev_in;
 
            if ($target->{type} eq 'host') {
 
@@ -265,6 +381,17 @@ sub route_packet {
                $pkg->{iface_out} = 'lo';
                $next_route_state = 'host';
 
+           } elsif ($target->{type} eq 'bport') {
+
+               $chain = 'PVEFW-FORWARD';
+               $pkg->{iface_in} = $route_state;
+               $pkg->{iface_out} = $target->{bridge} || die 'internal error';
+               # conditionally set physdev_out (same behavior as kernel)
+               if ($route_state eq $target->{bridge}) {
+                   $pkg->{physdev_out} = $target->{iface} || die 'internal error';
+               }
+               $next_route_state = $target->{iface};
+
            } elsif ($target->{type} eq 'ct') {
 
                $chain = 'PVEFW-FORWARD';
@@ -275,16 +402,11 @@ sub route_packet {
            } elsif ($target->{type} eq 'vm') {
 
                $chain = 'PVEFW-FORWARD';
+               $pkg->{iface_in} = $route_state;
+               $pkg->{iface_out} = $target->{bridge};
+               # conditionally set physdev_out (same behavior as kernel)
                if ($route_state eq $target->{bridge}) {
-                   $pkg->{iface_in} = $route_state;
-                   $pkg->{iface_out} = $route_state;
-                   $pkg->{physdev_in} = $physdev_in;
                    $pkg->{physdev_out} = $target->{fwpr} || die 'internal error';
-               } else {
-                   $pkg->{iface_in} = $route_state;
-                   $pkg->{iface_out} = $route_state;
-                   $pkg->{physdev_in} = $physdev_in;
-                   # do not set physdev_out (same behavior as kernel)
                }
                $next_route_state = 'fwbr-in';
 
@@ -301,8 +423,10 @@ sub route_packet {
        if ($chain) {
            add_trace("IPT check at $route_state (chain $chain)\n");
            add_trace(Dumper($pkg));
-           my $res = ruleset_simulate_chain($ruleset, $chain, $pkg);
-           return $res if $res ne 'ACCEPT';
+           $ipt_invocation_counter++;
+           my ($res, $ctr) = ruleset_simulate_chain($ruleset, $ipset_ruleset, $chain, $pkg);
+           $rule_check_counter += $ctr;
+           return ($res, $ipt_invocation_counter, $rule_check_counter) if $res ne 'ACCEPT';
        } 
 
        $route_state = $next_route_state;
@@ -310,7 +434,7 @@ sub route_packet {
        $physdev_in = $next_physdev_in;
     }
 
-    return 'ACCEPT';
+    return ('ACCEPT', $ipt_invocation_counter, $rule_check_counter);
 }
 
 sub extract_ct_info {
@@ -347,21 +471,28 @@ sub extract_vm_info {
 sub simulate_firewall {
     my ($ruleset, $ipset_ruleset, $vmdata, $test) = @_;
 
-    my $from = delete $test->{from} || die "missing 'from' field";
-    my $to = delete $test->{to} || die "missing 'to' field";
-    my $action = delete $test->{action} || die "missing 'action'";
-
+    my $from = $test->{from} || die "missing 'from' field";
+    my $to = $test->{to} || die "missing 'to' field";
+    my $action = $test->{action} || die "missing 'action'";
+    
+    my $testid = $test->{id};
+    
     die "from/to needs to be different" if $from eq $to;
 
     my $pkg = {
        proto => 'tcp',
-       sport => '1234',
-       dport => '4321',
-       source => '10.11.12.13',
-       dest => '10.11.12.14',
+       sport => undef,
+       dport => undef,
+       source => undef,
+       dest => undef,
     };
 
     while (my ($k,$v) = each %$test) {
+       next if $k eq 'from';
+       next if $k eq 'to';
+       next if $k eq 'action';
+       next if $k eq 'id';
+       die "unknown attribute '$k'\n" if !exists($pkg->{$k});
        $pkg->{$k} = $v;
     }
 
@@ -369,14 +500,32 @@ sub simulate_firewall {
 
     my $start_state;
 
+    my $host_ip = '10.11.12.13';
+
     if ($from eq 'host') {
        $from_info->{type} = 'host';
        $start_state = 'host';
+       $pkg->{source} = $host_ip if !defined($pkg->{source});
+    } elsif ($from =~ m|^(vmbr\d+)/(\S+)$|) {
+       $from_info->{type} = 'bport';
+       $from_info->{bridge} = $1;
+       $from_info->{iface} = $2;
+       $start_state = 'from-bport';
+    } elsif ($from eq 'outside') {
+       $from_info->{type} = 'bport';
+       $from_info->{bridge} = 'vmbr0';
+       $from_info->{iface} = 'eth0';
+       $start_state = 'from-bport';
+    } elsif ($from eq 'nfvm') {
+       $from_info->{type} = 'bport';
+       $from_info->{bridge} = 'vmbr0';
+       $from_info->{iface} = 'tapXYZ';
+       $start_state = 'from-bport';
     } elsif ($from =~ m/^ct(\d+)$/) {
        my $vmid = $1;
        $from_info = extract_ct_info($vmdata, $vmid);
        if ($from_info->{ip_address}) {
-           $pkg->{source} = $from_info->{ip_address};
+           $pkg->{source} = $from_info->{ip_address} if !defined($pkg->{source});
            $start_state = 'venet-out';
        } else {
            die "implement me";
@@ -387,7 +536,7 @@ sub simulate_firewall {
        $start_state = 'fwbr-out'; 
        $pkg->{mac_source} = $from_info->{macaddr};
     } else {
-       die "implement me";
+       die "unable to parse \"from => '$from'\"\n";
     }
 
     my $target;
@@ -395,6 +544,19 @@ sub simulate_firewall {
     if ($to eq 'host') {
        $target->{type} = 'host';
        $target->{iface} = 'host';
+       $pkg->{dest} = $host_ip if !defined($pkg->{dest});
+    } elsif ($to =~ m|^(vmbr\d+)/(\S+)$|) {
+       $target->{type} = 'bport';
+       $target->{bridge} = $1;
+       $target->{iface} = $2;
+    } elsif ($to eq 'outside') {
+       $target->{type} = 'bport';
+       $target->{bridge} = 'vmbr0';
+       $target->{iface} = 'eth0';
+     } elsif ($to eq 'nfvm') {
+       $target->{type} = 'bport';
+       $target->{bridge} = 'vmbr0';
+       $target->{iface} = 'tapXYZ';
     } elsif ($to =~ m/^ct(\d+)$/) {
        my $vmid = $1;
        $target = extract_ct_info($vmdata, $vmid);
@@ -410,44 +572,76 @@ sub simulate_firewall {
        $target = extract_vm_info($vmdata, $vmid);
        $target->{iface} = $target->{tapdev};
     } else {
-       die "implement me";
+       die "unable to parse \"to => '$to'\"\n";
     }
 
-    my $res = route_packet($ruleset, $ipset_ruleset, $pkg, $from_info, $target, $start_state);
+    $pkg->{source} = '100.100.1.2' if !defined($pkg->{source});
+    $pkg->{dest} = '100.200.3.4' if !defined($pkg->{dest});
+
+    my ($res, $ic, $rc) = route_packet($ruleset, $ipset_ruleset, $pkg, 
+                                      $from_info, $target, $start_state);
 
+    add_trace("IPT statistics: invocation = $ic, checks = $rc\n");
     die "test failed ($res != $action)\n" if $action ne $res;
 
     return undef; 
 }
 
 sub run_tests {
-    my ($vmdata, $testdir) = @_;
+    my ($vmdata, $testdir, $testfile, $testid) = @_;
+
+    $testfile = 'tests' if !$testfile;
 
     $vmdata->{testdir} = $testdir;
 
     my ($ruleset, $ipset_ruleset) = 
        PVE::Firewall::compile(undef, undef, $vmdata);
 
-    my $testfile = "$testdir/tests";
-    my $fh = IO::File->new($testfile) ||
-       die "unable to open '$testfile' - $!\n";
+    my $filename = "$testdir/$testfile";
+    my $fh = IO::File->new($filename) ||
+       die "unable to open '$filename' - $!\n";
 
+    my $testcount = 0;
     while (defined(my $line = <$fh>)) {
        next if $line =~ m/^\s*$/;
        next if $line =~ m/^#.*$/;
        if ($line =~ m/^\{.*\}\s*$/) {
            my $test = eval $line;
            die $@ if $@;
+           next if defined($testid) && (!defined($test->{id}) || ($testid ne $test->{id}));
            $trace = '';
            print Dumper($ruleset) if $debug;
-           eval { simulate_firewall($ruleset, $ipset_ruleset, $vmdata, $test); };
+           $testcount++;
+           eval {
+               my @test_zones = qw(host outside nfvm vm100 ct200);
+               if (!defined($test->{from}) && !defined($test->{to})) {
+                   die "missing zone speification (from, to)\n";
+               } elsif (!defined($test->{to})) {
+                   foreach my $zone (@test_zones) {
+                       next if $zone eq $test->{from};
+                       $test->{to} = $zone;
+                       add_trace("Set Zone: to => '$zone'\n"); 
+                       simulate_firewall($ruleset, $ipset_ruleset, $vmdata, $test);
+                   }
+               } elsif (!defined($test->{from})) {
+                   foreach my $zone (@test_zones) {
+                       next if $zone eq $test->{to};
+                       $test->{from} = $zone;
+                       add_trace("Set Zone: from => '$zone'\n"); 
+                       simulate_firewall($ruleset, $ipset_ruleset, $vmdata, $test);
+                   }
+               } else {
+                   simulate_firewall($ruleset, $ipset_ruleset, $vmdata, $test);
+               }
+           };
            if (my $err = $@) {
 
                print Dumper($ruleset) if !$debug;
 
                print "$trace\n" if !$debug;
 
-               print "$testfile line $.: $line";
+               print "$filename line $.: $line";
 
                print "test failed: $err\n";
 
@@ -458,7 +652,9 @@ sub run_tests {
        }
     }
 
-    print "PASS: $testfile\n";
+    die "no tests found\n" if $testcount <= 0;
+
+    print "PASS: $filename\n";
 
     return undef;
 }
@@ -466,14 +662,14 @@ sub run_tests {
 my $vmdata = {
     qemu => {
        100 => {
-           net0 => "e1000=0E:0B:38:B8:B3:21,bridge=vmbr0",
+           net0 => "e1000=0E:0B:38:B8:B3:21,bridge=vmbr0,firewall=1",
        },
        101 => {
-           net0 => "e1000=0E:0B:38:B8:B3:22,bridge=vmbr0",
+           net0 => "e1000=0E:0B:38:B8:B3:22,bridge=vmbr0,firewall=1",
        },
        # on bridge vmbr1
        110 => {
-           net0 => "e1000=0E:0B:38:B8:B4:21,bridge=vmbr1",
+           net0 => "e1000=0E:0B:38:B8:B4:21,bridge=vmbr1,firewall=1",
        },
     },
     openvz => {
@@ -486,9 +682,26 @@ my $vmdata = {
     },
 };
 
-foreach my $dir (<test-*>) {
-    next if ! -d $dir;
-    run_tests($vmdata, $dir);
+if ($testfilename) {
+    my $testfile;
+    my $dir;
+
+    if (-d $testfilename) {
+       $dir = $testfilename;
+    } elsif (-f $testfilename) {
+       $dir = dirname($testfilename);
+       $testfile = basename($testfilename);
+    } else {
+       die "no such file/dir '$testfilename'\n"; 
+    }
+
+    run_tests($vmdata, $dir, $testfile, $testid);
+
+} else { 
+    foreach my $dir (<test-*>) {
+       next if ! -d $dir;
+       run_tests($vmdata, $dir);
+    }
 }
 
 print "OK - all tests passed\n";