Unverified Commit f8494e49 authored by Francesc Guasch's avatar Francesc Guasch Committed by GitHub
Browse files

Test: start limit (#1570)

test: check start limit
parent 6372717d
......@@ -69,6 +69,7 @@ create_domain
remove_old_domains_req
remove_domain_and_clones_req
remove_domain
mojo_init
mojo_clean
mojo_create_domain
......@@ -499,6 +500,18 @@ sub remove_old_domains_req($wait=1, $run_request=0) {
}
}
sub remove_domain(@bases) {
for my $base (@bases) {
for my $clone ($base->clones) {
my $d_clone = Ravada::Domain->open($clone->{id});
remove_domain($d_clone);
}
$base->remove(user_admin);
}
}
sub remove_domain_and_clones_req($domain_data, $wait=1, $run_request=0) {
my $domain;
if (ref($domain_data) =~ /Ravada.*Domain/) {
......@@ -1976,7 +1989,7 @@ sub _load_sql_file {
my $connector = shift;
my $file_sql = shift;
open my $h_sql,'<',$file_sql or die "$! $file_sql";
open my $h_sql,'<',$file_sql or confess "$! $file_sql";
my $sql = '';
while (my $line = <$h_sql>) {
$sql .= $line;
......
......@@ -1196,132 +1196,6 @@ sub test_private_base {
$clone->remove(user_admin) if $clone;
}
sub test_domain_limit_admin {
my $vm_name = shift;
for my $domain ( rvd_back->list_domains()) {
$domain->shutdown_now(user_admin);
}
my $domain = create_domain($vm_name, user_admin );
ok($domain,"Expecting a new domain created") or exit;
$domain->shutdown_now(user_admin) if $domain->is_active;
is(rvd_back->list_domains(user => user_admin , active => 1),0
,Dumper(rvd_back->list_domains())) or exit;
$domain->start( user_admin );
is($domain->is_active,1);
ok($domain->start_time <= time,"Expecting start time <= ".time
." got ".time);
sleep 1;
is(rvd_back->list_domains(user => user_admin , active => 1),1);
my $domain2 = create_domain($vm_name, user_admin );
$domain2->shutdown_now( user_admin ) if $domain2->is_active;
is(rvd_back->list_domains(user => user_admin , active => 1),1);
$domain2->start( user_admin );
my $req = Ravada::Request->enforce_limits(timeout => 60);
wait_request();
my @list = rvd_back->list_domains(user => user_admin, active => 1);
is(scalar @list,2) or die Dumper([map { $_->name } @list]);
$domain2->remove(user_admin);
$domain->remove(user_admin);
}
sub test_domain_limit_noadmin {
my $vm_name = shift;
my $user = $USER;
user_admin->grant($user,'create_machine');
is($user->is_admin,0);
for my $domain ( rvd_back->list_domains()) {
$domain->shutdown_now(user_admin);
}
my $domain = create_domain($vm_name, $user);
ok($domain,"Expecting a new domain created") or exit;
$domain->shutdown_now($USER) if $domain->is_active;
is(rvd_back->list_domains(user => $user, active => 1),0
,Dumper(rvd_back->list_domains())) or exit;
$domain->start( $user);
is($domain->is_active,1);
ok($domain->start_time <= time,"Expecting start time <= ".time
." got ".time);
sleep 1;
is(rvd_back->list_domains(user => $user, active => 1),1);
my $domain2 = create_domain($vm_name, $user);
$domain2->shutdown_now( $user ) if $domain2->is_active;
is(rvd_back->list_domains(user => $user, active => 1),1);
$domain2->start( $user );
test_enforce_limits($domain2,1, $user);
my @list = rvd_back->list_domains(user => $user, active => 1);
is(scalar @list,1) or die Dumper([ map { $_->id." ".$_->name } @list]);
is($list[0]->name, $domain2->name) if $list[0];
$domain->remove(user_admin);
$domain2->remove(user_admin);
}
sub test_domain_limit_allowed {
my $vm_name = shift;
my $user = $USER;
user_admin->grant($user,'create_machine');
user_admin->grant($user,'start_many');
is($user->is_admin,0);
for my $domain ( rvd_back->list_domains()) {
$domain->shutdown_now(user_admin);
}
my $domain = create_domain($vm_name, $user);
ok($domain,"Expecting a new domain created") or exit;
$domain->shutdown_now($USER) if $domain->is_active;
is(rvd_back->list_domains(user => $user, active => 1),0
,Dumper(rvd_back->list_domains())) or exit;
$domain->start( $user);
is($domain->is_active,1);
ok($domain->start_time <= time,"Expecting start time <= ".time
." got ".time);
sleep 1;
is(rvd_back->list_domains(user => $user, active => 1),1);
my $domain2 = create_domain($vm_name, $user);
$domain2->shutdown_now( $user ) if $domain2->is_active;
is(rvd_back->list_domains(user => $user, active => 1),1);
$domain2->start( $user );
my $req = Ravada::Request->enforce_limits(timeout => 60);
wait_request();
my @list = rvd_back->list_domains(user => $user, active => 1);
is(scalar @list,2) or die Dumper([ map { $_->name } @list]);
user_admin->revoke($user,'start_many');
is($user->can_start_many,0) or exit;
test_enforce_limits($domain2, 1, $user);
@list = rvd_back->list_domains(user => $user, active => 1);
is(scalar @list,1,"[$vm_name] expecting 1 active domain")
or die Dumper([ map { $_->name } @list]);
$domain->remove(user_admin);
$domain2->remove(user_admin);
}
sub test_enforce_limits($domain, $n_expected, $user=user_admin) {
# let's try for a while to check if one machine is shutdown because
......@@ -1356,51 +1230,6 @@ sub test_enforce_limits($domain, $n_expected, $user=user_admin) {
}
}
sub test_domain_limit_already_requested {
my $vm_name = shift;
for my $domain ( rvd_back->list_domains()) {
$domain->shutdown_now(user_admin);
}
my $user = create_user("limit$$","bar");
user_admin->grant($user, 'create_machine');
my $domain = create_domain($vm_name, $user);
ok($domain,"Expecting a new domain created") or return;
$domain->shutdown_now($user) if $domain->is_active;
is(rvd_back->list_domains(user => $USER, active => 1),0
,Dumper(rvd_back->list_domains())) or return;
$domain->start( $user );
is($domain->is_active,1);
ok($domain->start_time <= time,"Expecting start time <= ".time
." got ".time);
sleep 1;
is(rvd_back->list_domains(user => $user, active => 1),1);
my $domain2 = create_domain($vm_name, $user);
$domain2->shutdown_now($USER) if $domain2->is_active;
is(rvd_back->list_domains(user => $user, active => 1),1);
$domain2->start( $user );
my @list_requests = grep { $_->command ne 'set_time'} $domain->list_requests;
is(scalar @list_requests,0,"Expecting 0 requests ".Dumper(\@list_requests));
test_enforce_limits($domain2,1, $user);
my @list=rvd_back->list_domains(user => $user, active => 1);
is(scalar(@list),1);
ok(!$domain->is_active || !$domain2->is_active ) or die Dumper([ map { $_->name } @list]);
is($list[0]->name, $domain2->name) if $list[0];
$domain2->remove($user);
$domain->remove($user);
$user->remove();
}
sub test_prepare_fail($vm) {
my $domain = create_domain($vm,user_admin,'Alpine',1);
my @volumes = $domain->list_volumes_info();
......@@ -2018,9 +1847,6 @@ for my $vm_name ( vm_names() ) {
}
flush_rules() if !$<;
test_domain_limit_noadmin($vm_name);
test_domain_limit_already_requested($vm_name);
test_change_display_settings($vm);
test_display_drivers($vm,0);
test_display_drivers($vm,1); #remove after testing display type
......@@ -2067,9 +1893,6 @@ for my $vm_name ( vm_names() ) {
test_private_base($vm_name);
test_spinned_off_base($vm_name);
test_domain_limit_admin($vm_name);
test_domain_limit_noadmin($vm_name);
test_domain_limit_allowed($vm_name);
$domain->remove( user_admin );
......
use warnings;
use strict;
use Carp qw(confess);
use Data::Dumper;
use Test::More;
use lib 't/lib';
use Test::Ravada;
no warnings "experimental::signatures";
use feature qw(signatures);
my $USER;
################################################################
sub test_domain_limit_admin {
my $vm_name = shift;
for my $domain ( rvd_back->list_domains()) {
$domain->shutdown_now(user_admin);
}
my $domain = create_domain($vm_name, user_admin );
ok($domain,"Expecting a new domain created") or exit;
$domain->shutdown_now(user_admin) if $domain->is_active;
is(rvd_back->list_domains(user => user_admin , active => 1),0
,Dumper(rvd_back->list_domains())) or exit;
$domain->start( user_admin );
is($domain->is_active,1);
ok($domain->start_time <= time,"Expecting start time <= ".time
." got ".time);
sleep 1;
is(rvd_back->list_domains(user => user_admin , active => 1),1);
my $domain2 = create_domain($vm_name, user_admin );
$domain2->shutdown_now( user_admin ) if $domain2->is_active;
is(rvd_back->list_domains(user => user_admin , active => 1),1);
$domain2->start( user_admin );
my $req = Ravada::Request->enforce_limits(timeout => 1);
rvd_back->_process_all_requests_dont_fork();
sleep 1;
rvd_back->_process_all_requests_dont_fork();
my @list = rvd_back->list_domains(user => user_admin, active => 1);
is(scalar @list,2) or die Dumper([map { $_->name } @list]);
$domain2->remove(user_admin);
$domain->remove(user_admin);
}
sub test_domain_limit_noadmin {
my $vm_name = shift;
my $user = $USER;
user_admin->grant($user,'create_machine');
is($user->is_admin,0);
for my $domain ( rvd_back->list_domains()) {
$domain->shutdown_now(user_admin);
}
my $domain = create_domain($vm_name, $user);
ok($domain,"Expecting a new domain created") or exit;
$domain->shutdown_now($USER) if $domain->is_active;
is(rvd_back->list_domains(user => $user, active => 1),0
,Dumper(rvd_back->list_domains())) or exit;
$domain->start( $user);
is($domain->is_active,1);
ok($domain->start_time <= time,"Expecting start time <= ".time
." got ".time);
sleep 1;
is(rvd_back->list_domains(user => $user, active => 1),1);
my $domain2 = create_domain($vm_name, $user);
$domain2->shutdown_now( $user ) if $domain2->is_active;
is(rvd_back->list_domains(user => $user, active => 1),1);
$domain2->start( $user );
my $req = Ravada::Request->enforce_limits(timeout => 1, _force => 1);
rvd_back->_process_all_requests_dont_fork();
sleep 1;
rvd_back->_process_all_requests_dont_fork();
my @list = rvd_back->list_domains(user => $user, active => 1);
is(scalar @list,1) or die Dumper(\@list);
is($list[0]->name, $domain2->name) if $list[0];
$domain->remove(user_admin);
$domain2->remove(user_admin);
}
sub test_domain_limit_allowed {
my $vm_name = shift;
my $user = $USER;
user_admin->grant($user,'create_machine');
user_admin->grant($user,'start_many');
is($user->is_admin,0);
for my $domain ( rvd_back->list_domains()) {
$domain->shutdown_now(user_admin);
}
my $domain = create_domain($vm_name, $user);
ok($domain,"Expecting a new domain created") or exit;
$domain->shutdown_now($USER) if $domain->is_active;
is(rvd_back->list_domains(user => $user, active => 1),0
,Dumper(rvd_back->list_domains())) or exit;
$domain->start( $user);
is($domain->is_active,1);
ok($domain->start_time <= time,"Expecting start time <= ".time
." got ".time);
sleep 1;
is(rvd_back->list_domains(user => $user, active => 1),1);
my $domain2 = create_domain($vm_name, $user);
$domain2->shutdown_now( $user ) if $domain2->is_active;
is(rvd_back->list_domains(user => $user, active => 1),1);
$domain2->start( $user );
my $req = Ravada::Request->enforce_limits(timeout => 1);
rvd_back->_process_all_requests_dont_fork();
sleep 1;
rvd_back->_process_all_requests_dont_fork();
my @list = rvd_back->list_domains(user => $user, active => 1);
is(scalar @list,2) or die Dumper([ map { $_->name } @list]);
user_admin->revoke($user,'start_many');
is($user->can_start_many,0) or exit;
$req = Ravada::Request->enforce_limits(timeout => 1,_force => 1);
rvd_back->_process_all_requests_dont_fork();
sleep 1;
rvd_back->_process_all_requests_dont_fork();
@list = rvd_back->list_domains(user => $user, active => 1);
is(scalar @list,1,"[$vm_name] expecting 1 active domain")
or die Dumper([ map { $_->name } @list]);
$domain->remove(user_admin);
$domain2->remove(user_admin);
}
sub test_domain_limit_already_requested {
my $vm_name = shift;
for my $domain ( rvd_back->list_domains()) {
$domain->shutdown_now(user_admin);
}
my $user = create_user("limit$$","bar");
user_admin->grant($user, 'create_machine');
my $domain = create_domain($vm_name, $user);
ok($domain,"Expecting a new domain created") or return;
$domain->shutdown_now($user) if $domain->is_active;
is(rvd_back->list_domains(user => $USER, active => 1),0
,Dumper(rvd_back->list_domains())) or return;
$domain->start( $user );
is($domain->is_active,1);
ok($domain->start_time <= time,"Expecting start time <= ".time
." got ".time);
sleep 1;
is(rvd_back->list_domains(user => $user, active => 1),1);
my $domain2 = create_domain($vm_name, $user);
$domain2->shutdown_now($USER) if $domain2->is_active;
is(rvd_back->list_domains(user => $user, active => 1),1);
$domain2->start( $user );
my @list_requests = grep { $_->command ne 'set_time'} $domain->list_requests;
is(scalar @list_requests,0,"Expecting 0 requests ".Dumper(\@list_requests));
is(rvd_back->list_domains(user => $user, active => 1),2);
my $req = Ravada::Request->enforce_limits(timeout => 1, _force => 1);
rvd_back->_process_all_requests_dont_fork();
is($req->status,'done');
is($req->error, '');
my @list = rvd_back->list_domains(user => $user, active => 1);
is(scalar @list,1) or die Dumper([ map { $_->name } @list]);
is($list[0]->name, $domain2->name) if $list[0];
$domain2->remove($user);
$domain->remove($user);
$user->remove();
}
sub test_limit_change($vm, $limit) {
my $user = create_user(new_domain_name(),$$);
my $base1 = create_domain($vm);
$base1->prepare_base(user_admin);
$base1->is_public(1);
my $base2 = create_domain($vm);
$base2->prepare_base(user_admin);
$base2->is_public(1);
my $clone1=$base1->clone(name => new_domain_name(), user => $user);
my $clone2=$base2->clone(name => new_domain_name(), user => $user);
# user_admin->grant($user, 'start_limit', 1);
$clone1->start(user_admin);
$clone2->start(user_admin);
my @list = rvd_back->list_domains(user => $user, active => 1);
is(scalar @list,2) or die Dumper([map { $_->name } @list]);
my $req = Ravada::Request->enforce_limits(timeout => 1, _force => 1);
wait_request( debug => 0);
is($req->status,'done');
is($req->error,'');
@list = rvd_back->list_domains(user => $user, active => 1);
is(scalar @list,1) or die Dumper([map { $_->name } @list]);
my $base3 = create_domain($vm);
$base3->prepare_base(user_admin);
$base3->is_public(1);
my $clone3=$base3->clone(name => new_domain_name(), user => $user);
$clone1->start(user_admin);
$clone2->start(user_admin);
$clone3->start(user_admin);
@list = rvd_back->list_domains(user => $user, active => 1);
is(scalar @list,3) or die Dumper([map { $_->name } @list]);
$req = Ravada::Request->enforce_limits(timeout => 1, _force => 1);
wait_request( debug => 0);
is($req->status,'done');
is($req->error,'');
@list = rvd_back->list_domains(user => $user, active => 1);
is(scalar @list,1) or warn Dumper([map { $_->name } @list]);
user_admin->grant($user, 'start_limit', 2);
$clone1->start(user_admin);
$clone2->start(user_admin);
$clone3->start(user_admin);
@list = rvd_back->list_domains(user => $user, active => 1);
is(scalar @list,3) or warn Dumper([map { $_->name } @list]);
$req = Ravada::Request->enforce_limits(timeout => 1, _force => 1);
wait_request( debug => 0);
is($req->status,'done');
is($req->error,'');
@list = rvd_back->list_domains(user => $user, active => 1);
is(scalar @list,2) or die Dumper([map { $_->name } @list]);
remove_domain($base1, $base2, $base3);
}
################################################################
clean();
init();
for my $vm_name ( vm_names() ) {
diag("Testing limits on $vm_name VM");
my $vm;
eval { $vm = rvd_back->search_vm($vm_name) };
SKIP: {
my $msg = "SKIPPED test: No $vm_name VM found ".($@ or '');
if ($vm && $vm_name =~ /kvm/i && $>) {
$msg = "SKIPPED: Test must run as root";
$vm = undef;
}
diag($msg) if !$vm;
skip $msg,10 if !$vm;
$USER = create_user("foo_${vm_name}_".new_domain_name(),"bar");
test_limit_change($vm, 1);
test_limit_change($vm, 2);
test_domain_limit_admin($vm_name);
test_domain_limit_noadmin($vm_name);
test_domain_limit_allowed($vm_name);
test_domain_limit_already_requested($vm_name);
};
}
end();
done_testing();
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment