Commit eb6eb82b authored by Francesc Guasch's avatar Francesc Guasch
Browse files

test(volatile): check users are removed

issue #1023
parent cec9d3de
......@@ -9,6 +9,9 @@ use Data::Dumper;
use POSIX qw(WNOHANG);
use Test::More;
no warnings "experimental::signatures";
use feature qw(signatures);
use_ok('Ravada');
use_ok('Ravada::Network');
......@@ -66,6 +69,59 @@ sub allow_anonymous {
$sth->finish;
}
sub _cleanup_info($user_id) {
my $req = Ravada::Request->cleanup( );
wait_request(debug => 0);
my $sth = connector->dbh->prepare("SELECT name,date_created FROM users where id=?");
$sth->execute($user_id);
return $sth->fetchrow;
}
sub test_volatile_cleanup ($base) {
return if $base->type ne 'KVM';
my $user = Ravada::Auth::SQL::add_user(name => "user_".new_domain_name(), is_temporary => 1);
my $user_id = $user->id;
my ($name, $date_created) = _cleanup_info($user_id);
ok($name);
my $sth_update = connector->dbh->prepare("UPDATE users set date_created=? WHERE id=?");
my $date = _date();
$sth_update->execute($date,$user_id);
($name, $date_created) = _cleanup_info($user_id);
ok($name, "date $date_created");
$sth_update->execute(_date(time - 25 * 3600),$user_id);
my $clone = $base->clone(
user => $user
, name => new_domain_name
);
$clone->start($user) if !$clone->is_active;
($name, $date_created) = _cleanup_info($user_id);
ok($name, "date $date_created");
my $sth_deldom = connector->dbh->prepare("DELETE FROM domains WHERE id=?");
$sth_deldom->execute($clone->id);
($name, $date_created) = _cleanup_info($user_id);
ok(!$name);
shutdown_domain_internal($clone);
}
sub _date($time = time) {
my @now = localtime($time);
$now[5]+=1900;
$now[4]++;
for ( 0 .. 4 ) {
$now[$_] = "0$now[$_]" if length($now[$_]) < 2;
}
return "$now[5]-$now[4]-$now[3] $now[2]:$now[1]:$now[0]";
}
sub test_volatile {
my ($vm_name, $base) = @_;
......@@ -75,59 +131,59 @@ sub test_volatile {
my $user_name = "user_".new_domain_name();
my $user_id;
{
my $user = Ravada::Auth::SQL::add_user(name => $user_name, is_temporary => 1);
is($user->is_temporary,1);
$user_id = $user->id;
my $user = Ravada::Auth::SQL::add_user(name => $user_name, is_temporary => 1);
is($user->is_temporary,1);
$user_id = $user->id;
my $clone = $base->clone(
user => $user
, name => $name
);
$clone->start($user) if !$clone->is_active;
is($clone->is_active,1,"[$vm_name] Expecting clone active");
my $clone = $base->clone(
user => $user
, name => $name
);
$clone->start($user) if !$clone->is_active;
is($clone->is_active,1,"[$vm_name] Expecting clone active");
like($clone->spice_password,qr{..+},"[$vm_name] ".$clone->name)
like($clone->spice_password,qr{..+},"[$vm_name] ".$clone->name)
if $vm_name eq 'KVM';
is($clone->is_volatile,1,"[$vm_name] Expecting is_volatile");
is($clone->is_volatile,1,"[$vm_name] Expecting is_volatile");
my $clone2 = rvd_back->search_domain($name);
is($clone2->is_volatile,1,"[$vm_name] Expecting is_volatile");
my $clone2 = rvd_back->search_domain($name);
is($clone2->is_volatile,1,"[$vm_name] Expecting is_volatile");
my $clone3 = $vm->search_domain($name);
is($clone3->is_volatile,1,"[$vm_name] Expecting is_volatile");
my $clone3 = $vm->search_domain($name);
is($clone3->is_volatile,1,"[$vm_name] Expecting is_volatile");
my @volumes = $clone->list_volumes();
my @volumes = $clone->list_volumes();
is($clone->is_active, 1);
eval { $clone->shutdown_now(user_admin) if $clone->is_active};
is(''.$@,'',"[$vm_name] Expecting no error after shutdown");
is($clone->is_active, 1);
eval { $clone->shutdown_now(user_admin) if $clone->is_active};
is(''.$@,'',"[$vm_name] Expecting no error after shutdown");
is($clone->is_active, 0);
# test out of the DB
my $sth = connector->dbh->prepare("SELECT id,name FROM domains WHERE name=?");
$sth->execute($name);
my $row = $sth->fetchrow_hashref;
ok(!$row,"Expecting no domain info in the DB, found ".Dumper($row)) or exit;
is($clone->is_active, 0);
# test out of the DB
my $sth = connector->dbh->prepare("SELECT id,name FROM domains WHERE name=?");
$sth->execute($name);
my $row = $sth->fetchrow_hashref;
ok(!$row,"Expecting no domain info in the DB, found ".Dumper($row)) or exit;
# search for the removed domain
my $domain2 = $vm->search_domain($name);
ok(!$domain2,"[$vm_name] Expecting domain $name removed after shutdown\n"
.Dumper($domain2)) or exit;
# search for the removed domain
my $domain2 = $vm->search_domain($name);
ok(!$domain2,"[$vm_name] Expecting domain $name removed after shutdown\n"
.Dumper($domain2)) or exit;
is(rvd_front->domain_exists($name),0,"[$vm_name] Expecting domain removed after shutdown")
or exit;
is(rvd_front->domain_exists($name),0,"[$vm_name] Expecting domain removed after shutdown")
or exit;
my $user2 = Ravada::Auth::SQL->new(name => $user_name);
# TODO
# ok(!$user2->id,"Expecting user '$user_name' removed");
my $domain_b = rvd_back->search_domain($name);
ok(!$domain_b,"[$vm_name] Expecting domain removed after shutdown");
my $user2 = Ravada::Auth::SQL->new(name => $user_name);
ok(!$user2->id,"Expecting user '$user_name' removed");
my $domains_f = rvd_front->list_domains();
ok(!grep({ $_->{name} eq $name } @$domains_f),"[$vm_name] Expecting $name not listed");
my $domain_b = rvd_back->search_domain($name);
ok(!$domain_b,"[$vm_name] Expecting domain removed after shutdown");
my $domains_f = rvd_front->list_domains();
ok(!grep({ $_->{name} eq $name } @$domains_f),"[$vm_name] Expecting $name not listed");
$name = undef;
$name = undef;
$vm->refresh_storage_pools();
$vm->refresh_storage();
......@@ -322,6 +378,7 @@ for my $vm_name ('Void', 'KVM') {
$base->is_public(1);
allow_anonymous($base);
test_volatile_cleanup($base);
test_volatile($vm_name, $base);
test_volatile_auto_kvm($vm_name, $base) if $vm_name eq'KVM';
......
Supports Markdown
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