Skip to content

Commit

Permalink
Test: ports (#2054)
Browse files Browse the repository at this point in the history
test: refactored tests fixed false negative results
  • Loading branch information
frankiejol authored May 3, 2024
1 parent 5db1514 commit c23d577
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 51 deletions.
9 changes: 7 additions & 2 deletions lib/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4430,6 +4430,8 @@ sub _cmd_manage_pools($self, $request) {
sub _cmd_discover($self, $request) {
my $id_vm = $request->args('id_vm');
my $vm = Ravada::VM->open($id_vm);
return if !$vm;
eval { return if !$vm->vm };
my @list = $vm->discover();
$request->output(encode_json(\@list));
}
Expand Down Expand Up @@ -5611,7 +5613,7 @@ sub _check_mounted($path, $fstab, $mtab) {
sub _cmd_check_storage($self, $request) {
my $contents = "a" x 160;
for my $vm ( $self->list_vms ) {
next if !$vm->is_local;
next if !$vm || !$vm->is_local;
my %fstab = _list_mnt($vm,"s");
my %mtab = _list_mnt($vm,"m");

Expand Down Expand Up @@ -5990,6 +5992,7 @@ sub _refresh_active_vms ($self) {

my %active_vm;
for my $vm ($self->list_vms) {
next if !$vm;
if ( !$vm->enabled() || !$vm->is_active ) {
$vm->shutdown_domains();
$active_vm{$vm->id} = 0;
Expand Down Expand Up @@ -6584,7 +6587,9 @@ sub vm($self) {
warn $@;
next;
}
push @vms, ( $vm );
eval {
push @vms, ( $vm ) if $vm && $vm->vm;
};
};
return [@vms] if @vms;
return $self->_create_vm();
Expand Down
1 change: 1 addition & 0 deletions lib/Ravada/Volume.pm
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ sub _cache_volume_info($self) {
);
};
return if $@ && $@ =~ /foreign key constraint fails/i;
return if $@ && $@ =~ /Duplicate entry/i;
confess "$name / $n_order \n".$@ if $@;
return;
}
Expand Down
63 changes: 58 additions & 5 deletions t/lib/Test/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ create_domain
search_latest_machine
ping_backend
wait_ip
config_host_devices
Expand Down Expand Up @@ -163,6 +164,7 @@ my $NBD_LOADED;
my $FH_FW;
my $FH_NODE;
my %LOCKED_FH;
my $FILE_DB;

my ($MOJO_USER, $MOJO_PASSWORD);

Expand Down Expand Up @@ -685,7 +687,7 @@ sub _leftovers {
}

sub _discover() {
my $sth = connector()->dbh->prepare("SELECT id,vm_type,hostname,name FROM vms");
my $sth = connector()->dbh->prepare("SELECT id,vm_type,hostname,name,is_active,enabled FROM vms");
$sth->execute();

my $sth_instances = connector()->dbh->prepare("INSERT INTO domain_instances "
Expand All @@ -694,7 +696,9 @@ sub _discover() {

my $name = base_domain_name();

while ( my ($id_vm, $vm_type, $hostname, $vm_name) = $sth->fetchrow ) {
while ( my ($id_vm, $vm_type, $hostname, $vm_name, $is_active, $enabled) = $sth->fetchrow ) {
next if !$is_active || !$enabled;

my $req=Ravada::Request->discover(
uid => user_admin->id
,id_vm => $id_vm
Expand Down Expand Up @@ -2530,7 +2534,7 @@ sub _dir_db {
};
die $@ if $@ && $@ !~ /Permission denied/;
if ($@) {
warn "$! on mkdir $dir_db";
warn "$! on mkdir $dir_db [ $>,$< ]";
$dir_db = "t/.db";
make_path $dir_db or die "$! $dir_db";
}
Expand All @@ -2551,6 +2555,7 @@ sub _file_db {
if ( -e $file_db ) {
unlink $file_db or die("$! $file_db");
}
$FILE_DB = $file_db;
return $file_db;
}
Expand Down Expand Up @@ -2730,8 +2735,10 @@ sub end($ldap=undef) {
clean($ldap);
remove_old_users() if $CONNECTOR;
_unlock_all();
_file_db();
rmdir _dir_db();
if ($FILE_DB) {
_file_db();
rmdir _dir_db();
}
}

sub init_ldap_config($file_config='t/etc/ravada_ldap.conf'
Expand Down Expand Up @@ -3200,4 +3207,50 @@ sub create_ram_fs($dir=undef,$size=1024*1024) {
return ($dir,$size, $dev);
}

sub wait_ip($id_domain0, $seconds=60) {

my $domain;
for my $count ( 0 .. $seconds ) {
my $id_domain = $id_domain0;
if (ref($id_domain0)) {
if (ref($id_domain0) =~ /Ravada/) {
$id_domain = $id_domain0->id;
} else {
$id_domain = $id_domain0->{id};
}
}

if ($id_domain0 !~ /^\d+$/) {
$id_domain = _search_domain_by_name($id_domain);
next if !$id_domain;
}

Ravada::Request->refresh_machine(
id_domain => $id_domain
,uid => user_admin->id
);

my $info;
eval {
$domain = Ravada::Front::Domain->open($id_domain);
$info = $domain->info(user_admin);
};
warn $@ if $@ && $@ !~ /Unknown domain/;
return if $@ || ($count && !$domain->is_active);
return $info->{ip} if exists $info->{ip} && $info->{ip};
diag("Waiting for ".$domain->name. " ip") if !(time % 10);
sleep 1;
}
}

sub _search_domain_by_name($name) {
my $sth = connector->dbh->prepare("SELECT id FROM domains "
." WHERE name=?"
);
$sth->execute($name);
my ($id) = $sth->fetchrow;
return $id;
}


1;
4 changes: 2 additions & 2 deletions t/mojo/70_volatile.t
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ sub _new_network($vm_name,$id_vm) {

sub _create_network_nodes($vm_name, $net) {
my $sth = connector->dbh->prepare(
"SELECT id FROM vms WHERE vm_type=?"
"SELECT id,name FROM vms WHERE vm_type=?"
." AND is_active=1 AND enabled=1"
);
$sth->execute($vm_name);
while ( my ($id_vm) = $sth->fetchrow ) {
while ( my ($id_vm, $name) = $sth->fetchrow ) {
$net->{id_vm} = $id_vm;
Ravada::Request->create_network(
uid => user_admin->id
Expand Down
123 changes: 90 additions & 33 deletions t/mojo/80_check_resources.t
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ sub _import_base($vm_name) {

}

sub _remove_clones($time) {
sub _remove_clones($time=0) {
Ravada::Request->remove_clones(
uid => user_admin->id
,id_domain => $BASE->id
Expand All @@ -74,48 +74,100 @@ sub _free_memory() {
die;
}

sub test_ram($vm_name,$enable_check, $expected=undef) {
sub _req_clone($base, $name=new_domain_name(), $memory=2) {
Ravada::Request->clone(
uid => user_admin->id
,id_domain => $base->id
,name => $name
,memory => $memory * 1024 * 1024
);
return $name;
}

sub _wait_clone($name) {

my $new;
for ( 1 .. 90 ) {
$new = rvd_front->search_domain($name);
last if $new;
wait_request(debug => 1);
}
return if !$new;

my $req = Ravada::Request->start_domain( uid => user_admin->id
,id_domain => $new->id
);
for ( 1 .. 10 ) {
wait_request();
last if $req->status eq 'done';
}

wait_ip($new);
return $req;
}

sub test_ram($vm_name,$enable_check) {

my $free_mem = _free_memory();
my $limit = int($free_mem/1024/1024)+1 ;
_remove_clones(time+300+$limit*2);
my $count = 0;
for my $n ( 0 .. $limit*3 ) {

_set_min_free($vm_name, $limit*1024*1024);
_wait_clone(_req_clone($BASE,new_domain_name(),$limit-2));

my $name;
for my $n ( 0 .. $limit ) {
my $free = int(_free_memory()/1024/1024);
my $name = new_domain_name();
my $req=Ravada::Request->clone(
uid => user_admin->id
,id_domain => $BASE->id
,name => $name
,memory => 3 * 1024 * 1024
);
my $new;
for ( 1 .. 90 ) {
$new = rvd_front->search_domain($name);
last if $new;
wait_request();
}
last if !$new;
$req = Ravada::Request->start_domain( uid => user_admin->id
,id_domain => $new->id
);
for ( 1 .. 10 ) {
wait_request();
last if $req->status eq 'done';
}
$name = new_domain_name();
_req_clone($BASE, $name);
my $req = _wait_clone($name);
return $name if !$req;
if ($req->error) {
diag($req->error);
last;
}
$count++;
last if defined $expected && $count > $expected;
my $free2 = int(_free_memory()/1024/1024);
redo if $vm_name eq 'KVM' && ($free2>=$free);

}
_remove_clones(0);
wait_request();
return $count;
return $name;
}

sub test_start_another() {
my $found;
for my $clone ( $BASE->clones ) {
$found = $clone if $clone->{status} ne 'active';
}
if (!$found) {
_wait_clone(_req_clone($BASE));
} else {
my $req = Ravada::Request->start_domain( uid => user_admin->id
,id_domain => $found->{id}
);
for ( 1 .. 10 ) {
wait_request();
last if $req->status eq 'done';
}
is($req->error,'');
}
}

sub _set_min_free($vm_name,$min_free) {
my $sth = connector->dbh->prepare(
"select min_free_memory"
." FROM vms "
." WHERE vm_type=? AND hostname='localhost'"
);
$sth->execute($vm_name);
my ($old) = $sth->fetchrow;

$sth = connector->dbh->prepare("UPDATE vms set min_free_memory=?"
." WHERE vm_type=?"
);
$sth->execute($min_free, $vm_name);

return $old;
}

#########################################################
Expand All @@ -139,16 +191,21 @@ remove_old_domains_req();

$USERNAME = user_admin->name;
$PASSWORD = "$$ $$";
for my $vm_name (reverse @{rvd_front->list_vm_types} ) {
for my $vm_name (@{rvd_front->list_vm_types} ) {
diag("Testing RAM limit in $vm_name");

_import_base($vm_name);

rvd_back->setting("/backend/limits/startup_ram" => 1);
my $started_limit =test_ram($vm_name,1);
my $min_free=_set_min_free($vm_name,5*1024*1024);
my $domain_name = test_ram($vm_name,1);
rvd_back->setting("/backend/limits/startup_ram" => 0);
my $started_no_limit =test_ram($vm_name,0, $started_limit);
ok($started_no_limit > $started_limit);

test_start_another();

_set_min_free($vm_name, $min_free);

_remove_clones();
}

remove_old_domains_req(0); # 0=do not wait for them
Expand Down
22 changes: 19 additions & 3 deletions t/vm/20_base.t
Original file line number Diff line number Diff line change
Expand Up @@ -676,16 +676,21 @@ sub test_iptables($domain) {

my $port_rdp = $display_exp->{port};
my @iptables_rdp;
for ( 1 .. 10 ) {
for ( 1 .. 120 ) {

@iptables_rdp = grep { /^-A PREROUTING.*--dport $port_rdp -j DNAT .*3389/ } @iptables;
last if scalar(@iptables_rdp)==1;
my @prerouting = grep { /PREROUTING/} @iptables;
warn $domain->_vm->name.' waiting for /-A PREROUTING.*--dport '.$port_rdp.' -j DNAT .*3389/';
warn Dumper([$domain->_vm->name,\@iptables_rdp,\@prerouting]);

sleep 1;
wait_request();

($iptables, $err) = $domain->_vm->run_command("iptables-save");
@iptables = split /\n/,$iptables;
}
is(scalar(@iptables_rdp),1,"Expecting one entry with PRERORUTING --dport $port_rdp, got "
is(scalar(@iptables_rdp),1,"Expecting one entry with PREROUTING --dport $port_rdp, got "
.scalar(@iptables_rdp)) or do {
my @iptables_prer= grep { /^-A PREROUTING.*--dport / } @iptables;
confess Dumper(\@iptables_prer) if !scalar(@iptables_rdp);
Expand Down Expand Up @@ -1584,8 +1589,17 @@ sub test_display_conflict($vm) {
$domain->start( remote_ip => '1.1.1.1' , user => user_admin);
wait_request(debug => 0);

for ( 1 .. 3 ) {
my $display = $domain->info(user_admin)->{hardware}->{display};
last if $display->[0]->{port} ne $display->[1]->{port};
Ravada::Request->refresh_machine(uid => user_admin->id
,id_domain=> $domain->id
);
wait_request();
}

my $display = $domain->info(user_admin)->{hardware}->{display};
isnt($display->[0]->{port}, $display->[1]->{port});
isnt($display->[0]->{port}, $display->[1]->{port}) or die Dumper($display);
is($display->[0]->{is_active},1);
is($display->[1]->{is_active},1);

Expand Down Expand Up @@ -1778,6 +1792,8 @@ sub test_display_conflict_next($vm) {
}
$domain1->remove(user_admin);
$domain0->remove(user_admin);

rvd_back->setting("/backend/expose_port_min" => 60000 );
}

sub test_display_conflict_non_builtin($vm) {
Expand Down
Loading

0 comments on commit c23d577

Please sign in to comment.