Reducing size of each test, using Test2 package

This commit is contained in:
Jed Reynolds
2019-11-25 20:35:05 -08:00
parent 50c859d487
commit 392c6ca6c0
2 changed files with 188 additions and 87 deletions

View File

@@ -4,7 +4,12 @@ use strict;
use warnings; use warnings;
use diagnostics; use diagnostics;
use Carp; use Carp;
use Test2::V0;
# Ubuntu: libtest2-suite-perl
use Test2::V0 qw(ok fail done_testing);
use Test2::Tools::Basic qw(plan);
$SIG{ __DIE__ } = sub { Carp::confess( @_ ) }; $SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
$SIG{ __WARN__ } = sub { Carp::confess( @_ ) }; $SIG{ __WARN__ } = sub { Carp::confess( @_ ) };
@@ -22,8 +27,10 @@ if (defined $ENV{'DEBUG'}) {
require Exporter; require Exporter;
our @EXPORT_OK=qw(new test); our @EXPORT_OK=qw(new test);
our $FAIL = 0; #our $FAIL = 'fail';
our $OK = 1; #our $OK = 'pass';
#our $PASS = 'pass';
our @test_errors = ();
sub new { sub new {
my $class = shift; my $class = shift;
@@ -44,18 +51,27 @@ sub new {
return $self; return $self;
} }
sub run {
my $self = shift;
print "Run $self->{Name}\n";
my $result = shift;
ok($result, $self->{'Name'}) || fail($self->{'Name'});
}
sub test { sub test {
my $self = shift; my $self = shift;
if (! (defined $self->{'Test'})) { if (! (defined $self->{'Test'})) {
print "LANforge::test lacks self->Test, please rewrite your script.\n";
return $::FAIL; return $::FAIL;
} }
return &{$self->{'Test'}}(); return $self->{'Test'}();
} }
sub test_err { sub test_err {
my $self = shift; my $self = shift;
for my $e (@_) { for my $e (@_) {
my $ref = "".(caller(1))[3].":".(caller(1))[2].""; my $ref = "".(caller(1))[3].":".(caller(1))[2]."";
push (@{$self->{'Errors'}}, "$ref: $e"); push (@test_errors, "$ref: $e");
} }
} }
1; 1;

View File

@@ -12,7 +12,8 @@ $| = 1;
use Net::Telnet; use Net::Telnet;
use lib '.'; use lib '.';
use lib './LANforge'; use lib './LANforge';
# Ubuntu: libtry-tiny-smartcatch-perlq
use Try::Tiny;
use Getopt::Long; use Getopt::Long;
use JSON::XS; use JSON::XS;
use HTTP::Request; use HTTP::Request;
@@ -20,14 +21,19 @@ use LWP;
use LWP::UserAgent; use LWP::UserAgent;
use JSON; use JSON;
use Data::Dumper; use Data::Dumper;
# Ubuntu: libtest2-suite-perl # Ubuntu: libtest2-suite-perl
use Test2::V0 qw(ok fail done_testing);
use Test2::Tools::Basic qw(plan);
use constant NA => "NA";
use LANforge::Utils; use LANforge::Utils;
use LANforge::Port; use LANforge::Port;
use LANforge::Endpoint; use LANforge::Endpoint;
use LANforge::JsonUtils qw(err logg xpand json_request get_links_from get_thru json_post get_port_names flatten_list); use LANforge::JsonUtils qw(err logg xpand json_request get_links_from get_thru json_post get_port_names flatten_list);
use LANforge::Test qw(new test OK FAIL); use LANforge::Test qw(new test run);
#our $PASS = $LANforge::Test::PASS;
#our $FAIL = $LANforge::Test::FAIL;
package main; package main;
our $LFUtils; our $LFUtils;
our $lfmgr_host = "ct524-debbie"; our $lfmgr_host = "ct524-debbie";
@@ -94,17 +100,13 @@ else {
$::LFUtils->cli_send_silent(0); # Show input to telnet $::LFUtils->cli_send_silent(0); # Show input to telnet
$::LFUtils->cli_rcv_silent(0); # Show output from telnet $::LFUtils->cli_rcv_silent(0); # Show output from telnet
} }
our $port_ip = "";
#---------------------------------------------------------------------- #----------------------------------------------------------------------
# Tests # Tests
#---------------------------------------------------------------------- #----------------------------------------------------------------------
$tests{'t_create_telnet'} = LANforge::Test->new(Name=>"t_create_telnet",
Desc=>"Create telnet connection",
Test=>sub {
my $rv = 0;
$rv = 1;
});
#---------------------------------------------------------------------- #----------------------------------------------------------------------
# multiple ways of querying a port: # multiple ways of querying a port:
@@ -117,59 +119,133 @@ $tests{'query_port_cli'} = LANforge::Test->new(Name=>'query_port_cli',
Desc=>'query port using cli', Test => sub{ Desc=>'query port using cli', Test => sub{
my $self = pop; my $self = pop;
my $cmd = $::LFUtils->fmt_cmd("nc_show_port", 1, $::resource, "eth0"); my $cmd = $::LFUtils->fmt_cmd("nc_show_port", 1, $::resource, "eth0");
my $res = $::LFUtils->doAsyncCmd($cmd); my $resp = $::LFUtils->doAsyncCmd($cmd);
($::port_ip) = $resp =~ / IP:\s+([^ ]+) /;
my ($port_ip) = $res =~ / IP:\s+([^ ]+) /; fail($self->{Name}) if (!(defined $::port_ip));
return $::OK if ((defined $port_ip) && (length($port_ip) >= 7)); $self->run((length($::port_ip) >= 7));
# || fail($self->{Name}."port_ip [$::port_ip] incorrect\n");
$self->test_err("port_ip [$port_ip] incorrect\n"); });
return $::FAIL;
}
);
## test LANforge::Port ## test LANforge::Port
$tests{'query_port_class_port'} = LANforge::Test->new(Name=>'query_port_class_port', $tests{'query_port_class_port'} = LANforge::Test->new(Name=>'query_port_class_port',
Desc=>'query port using class Port', Test=>sub { Desc=>'query port using class Port', Test=>sub {
my $self = pop;
my $cmd = $::LFUtils->fmt_cmd("nc_show_port", 1, $::resource, "eth0");
my $resp = $::LFUtils->doAsyncCmd($cmd);
my $lf_port = LANforge::Port->new; my $lf_port = LANforge::Port->new;
$lf_port->decode($res); $lf_port->decode($resp);
return $::OK if ($lf_port->ip_addr() eq $port_ip); $self->run($lf_port->ip_addr() eq $::port_ip);
$self->test_err( "port_ip ".$lf_port->ip_addr()." doesn't match above $port_ip");
return $::FAIL; #$self->test_err( "port_ip ".$lf_port->ip_addr()." doesn't match above $::port_ip");
#return $LANforge::Test::FAIL;
}); });
## test JsonUtils/port ## test JsonUtils/port
$tests{'query_port_jsonutils'} = LANforge::Test->new(Name=>'query_port_jsonutils', $tests{'query_port_jsonutils'} = LANforge::Test->new(Name=>'query_port_jsonutils',
Desc=>'query port using jsonutils', Test=>sub { Desc=>'query port using jsonutils', Test=>sub {
print "http://".$::lf_mgr.":8080/port/1/1/eth0 \n"; my $self = pop;
my $port_json = json_request("http://".$::lf_mgr.":8080/port/1/1/eth0"); my $url = "http://".$::lf_mgr.":8080/port/1/1/eth0";
return $::OK if ($port_json->{IP} eq $port_ip); my $port_json = json_request($url);
return $::FAIL; #print Dumper($port_json);
$self->run($port_json->{interface}->{ip} eq $::port_ip);
}); });
## test lf_portmod.pl ## test lf_portmod.pl
$tests{'query_port_lfportmod'} = LANforge::Test->new(Name=>'query_port_lfportmod', $tests{'query_port_lfportmod'} = LANforge::Test->new(Name=>'query_port_lfportmod',
Desc=>'query port using lfportmod', Test=>sub { Desc=>'query port using lfportmod', Test=>sub {
my $self = pop;
print "Trying: ./lf_portmod.pl --manager $::lf_mgr --card $::resource --port_name eth0 --show_port\n"; print "Trying: ./lf_portmod.pl --manager $::lf_mgr --card $::resource --port_name eth0 --show_port\n";
$res = `./lf_portmod.pl --manager $::lf_mgr --card $::resource --port_name eth0 --show_port`; fail("lf_portmod.pl not found in ".cwd()) if (-f "./lf_portmod.pl");
if (!(defined $res)) { my $resp = `./lf_portmod.pl --manager $::lf_mgr --card $::resource --port_name eth0 --show_port`;
$self->test_err("Insufficient output from lf_portmod.pl.\n"); fail(!(defined $resp));
return $::FAIL; #$self->test_err("Insufficient output from lf_portmod.pl.\n");
}
my ($port_ip2) = $res =~ / IP:\s+([^ ]+) /; my ($port_ip2) = $resp =~ / IP:\s+([^ ]+) /;
return $::OK if ((defined $port_ip2) && length($port_ip2) >= 7); $self->run((defined $port_ip2) && length($port_ip2) >= 7);
$self->test_err("port_ip [$port_ip] incorrect\n"); #$self->test_err("port_ip [$port_ip2] incorrect\n");
return $::FAIL; #return $::FAIL;
}); });
$tests{'port_down_up_down_cli'} = LANforge::Test->new(Name=>'t_set_port_up',
$tests{'port_up_cli'} = LANforge::Test->new(Name=>'t_set_port_up', Desc=>'port_down_up_down, cli', Test=>sub {
Desc=>'set port up, cli', Test=>sub { my $self = pop;
my $cmd = $::LFUtils->fmt_cmd("set_port", 1, $::resource, "eth1"); my $up = 1;
my $res = $::LFUtils->doAsyncCmd($cmd); my $down = 0;
my $report_timer = 1;
my $status = -1;
my $cmd = $::LFUtils->fmt_cmd("set_port", 1, $::resource, "eth1",
NA, NA, NA, NA, $down, NA, NA, NA, NA, 8421378, $report_timer);
my $resp = $::LFUtils->doAsyncCmd($cmd);
my $begin = time();
until ($status == $down) {
sleep 1;
$resp = $::LFUtils->doAsyncCmd("nc_show_port 1 $resource eth1");
my @lines = split("\n", $resp);
my @matching = grep { /^\s+Current:\s+/ } @lines;
fail("eth1 has multiple lines starting with Current")
if (@matching > 1);
my ($updown) = $matching[0] =~ /Current:\s+(\w+)\s+/;
$status = ($updown eq "UP") ? 1: (($updown eq "DOWN") ? 0 : -1);
print $matching[0]
if ($status == -1);
if ((time() - $begin) > 15) {
fail("port does not report down in 15 seconds");
die("stupid thing wont admin down");
}
}
print "Port is down\n";
ok(1);
sleep 1;
$cmd = $::LFUtils->fmt_cmd("set_port", 1, $::resource, "eth1",
NA, NA, NA, NA, $up, NA, NA, NA, NA, 8421378, $report_timer);
$resp = $::LFUtils->doAsyncCmd($cmd);
$begin = time();
until ($status == $up) {
sleep 1;
$resp = $::LFUtils->doAsyncCmd("nc_show_port 1 $resource eth1");
my @lines = split("\n", $resp);
my @matching = grep { /^\s+Current:\s+/ } @lines;
fail("eth1 has multiple lines starting with Current")
if (@matching > 1);
my ($updown) = $matching[0] =~ /Current:\s+(\w+)\s+/;
$status = ($updown eq "UP") ? 1: (($updown eq "DOWN") ? 0 : -1);
print $matching[0]
if ($status == -1);
if ((time() - $begin) > 15) {
fail("port does not report up in 15 seconds");
die("stupid thing wont admin up");
}
}
print "Port is up\n";
ok(1);
$cmd = $::LFUtils->fmt_cmd("set_port", 1, $::resource, "eth1",
NA, NA, NA, NA, $down, NA, NA, NA, NA, 8421378, $report_timer);
$resp = $::LFUtils->doAsyncCmd($cmd);
$begin = time();
until ($status == $down) {
sleep 1;
$resp = $::LFUtils->doAsyncCmd("nc_show_port 1 $resource eth1");
my @lines = split("\n", $resp);
my @matching = grep { /^\s+Current:\s+/ } @lines;
fail("eth1 has multiple lines starting with Current")
if (@matching > 1);
my ($updown) = $matching[0] =~ /Current:\s+(\w+)\s+/;
$status = ($updown eq "UP") ? 1: (($updown eq "DOWN") ? 0 : -1);
print $matching[0]
if ($status == -1);
if ((time() - $begin) > 15) {
fail("port does not report down in 15 seconds");
die("stupid thing wont admin down");
}
}
print "Port is down\n";
ok(1);
}); });
$tests{'port_up_class_port'} = LANforge::Test->new(Name=>'t_set_port_up', $tests{'port_up_class_port'} = LANforge::Test->new(Name=>'t_set_port_up',
Desc=>'set port up, cli', Test=>sub { Desc=>'set port up, cli', Test=>sub {
my $self = pop;
#my $cmd =
## test LANforge::Port ## test LANforge::Port
## test JsonUtils/port ## test JsonUtils/port
## test lf_portmod.pl ## test lf_portmod.pl
@@ -268,23 +344,25 @@ sub t_rm_sta_L3 {
#---------------------------------------------------------------------- #----------------------------------------------------------------------
#---------------------------------------------------------------------- #----------------------------------------------------------------------
%test_subs = ( our @test_list = (
'00_create_telnet' => \&{'t_create_telnet'}, 'query_port_cli',
'01_query_port' => \&{'t_query_port'}, 'query_port_class_port',
'02_set_port_up' => 0, 'query_port_jsonutils',
'03_set_port_down' => 0, 'query_port_lfportmod',
'04_create_mvlan' => 0,
'05_destroy_mvlan' => 0, #'03_set_port_down' => 0,
'06_query_radio' => 0, #'04_create_mvlan' => 0,
'07_del_all_stations' => 0, #'05_destroy_mvlan' => 0,
'08_add_station_to_radio' => 0, #'06_query_radio' => 0,
'09_station_up' => 0, #'07_del_all_stations' => 0,
'10_station_down' => 0, #'08_add_station_to_radio' => 0,
'11_remove_radio' => 0, #'09_station_up' => 0,
'12_add_sta_L3_udp' => 0, #'10_station_down' => 0,
'13_sta_L3_start' => 0, #'11_remove_radio' => 0,
'14_sta_L3_stop' => 0, #'12_add_sta_L3_udp' => 0,
'15_rm_sta_L3' => 0, #'13_sta_L3_start' => 0,
#'14_sta_L3_stop' => 0,
#'15_rm_sta_L3' => 0,
); );
@@ -302,13 +380,19 @@ sub RunTests {
# } # }
#} #}
#else { #else {
for my $test_name (sort keys %::test_subs) { plan(0+@::test_list);
if (defined &{$::test_subs{$test_name}}) { for my $test_name (@::test_list) {
test_err("Failed on $test_name") if (! (defined $::tests{$test_name})) {
unless &{$::test_subs{$test_name}}(); die("test $test_name not found");
} }
else { my $r_test = $::tests{$test_name};
test_err("test $test_name not found"); try {
print "$test_name...";
my $rv = $r_test->test();
print "$rv\n";
}
catch {
$r_test->test_err($_);
} }
} }
#} #}
@@ -321,9 +405,9 @@ sub RunTests {
if ($list) { if ($list) {
my $av=""; my $av="";
print "Test names:\n"; print "Test names:\n";
for my $test_name (sort keys %::test_subs) { for my $test_name (@::test_list) {
$av=" "; $av=" ";
if (defined &{$::test_subs{$test_name}}) { if (defined &{$::tests{$test_name}}) {
$av='*'; $av='*';
} }
print " ${av} ${test_name}\n"; print " ${av} ${test_name}\n";
@@ -333,6 +417,7 @@ if ($list) {
else { else {
RunTests(); RunTests();
} }
done_testing();
#if (@test_errs > 1) { #if (@test_errs > 1) {
# print "Test errors:\n"; # print "Test errors:\n";
# print join("\n", @::test_errs); # print join("\n", @::test_errs);