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

test(frontend): check web services

* list machines
* list bases user
parent 4eeb2400
......@@ -8,6 +8,7 @@ use File::Path qw(make_path);
use YAML qw(DumpFile);
use Hash::Util qw(lock_hash unlock_hash);
use IPC::Run3 qw(run3);
use Mojo::File 'path';
use Test::More;
use YAML qw(LoadFile DumpFile);
......@@ -59,6 +60,13 @@ create_domain
delete_request
remove_old_domains_req
mojo_init
mojo_clean
mojo_create_domain
mojo_login
mojo_request
remove_old_user
);
our $DEFAULT_CONFIG = "t/etc/ravada.conf";
......@@ -94,6 +102,7 @@ our %VM_VALID = ( KVM => 1
);
our @NODES;
my $URL_LOGOUT = '/logout';
sub user_admin {
......@@ -366,8 +375,12 @@ sub remote_config_nodes {
sub remove_old_domains_req() {
my $base_name = base_domain_name();
my $machines = rvd_front->list_machines(user_admin);
my @reqs;
for my $machine ( @$machines) {
my $domain = Ravada::Front::Domain->open($machine->{id});
my $domain;
eval { $domain = Ravada::Front::Domain->open($machine->{id}) };
next if $@ && $@ =~ /nknown domain/i;
die if $@;
next if $domain->name !~ /^$base_name/;
my $n_clones = scalar($domain->clones);
my $req_clone;
......@@ -379,14 +392,17 @@ sub remove_old_domains_req() {
}
wait_request(debug => 1, background => 1, check_error => 0, timeout => 60+2*$n_clones);
my @after_req = ();
@after_req = ( after_request => $req_clone->id ) if $req_clone;
my $req = Ravada::Request->remove_domain(
name => $machine->{name}
,uid => user_admin->id
);
push @reqs,($req);
}
if (!@reqs) {
push @reqs,(Ravada::Request->ping_backend);
}
wait_request(debug => 1, background => 1, timeout => 120);
wait_request(debug => 1, background => 1, timeout => 120, check_error => 0);
return $reqs[-1]->status eq 'done';
}
......@@ -521,6 +537,56 @@ sub remove_old_domains {
_remove_old_domains_kvm();
}
sub mojo_init() {
my $script = path(__FILE__)->dirname->sibling('../../rvd_front.pl');
my $t = Test::Mojo->new($script);
$t->ua->inactivity_timeout(900);
$t->ua->connect_timeout(60);
return $t;
}
sub mojo_clean {
return remove_old_domains_req();
}
sub mojo_login( $t, $user, $pass ) {
$t->ua->get($URL_LOGOUT);
$t->post_ok('/login' => form => {login => $user, password => $pass});
like($t->tx->res->code(),qr/^(200|302)$/);
# ->status_is(302);
return $t->success;
}
sub mojo_create_domain($t, $vm_name) {
my $name = new_domain_name()."-".$vm_name;
$t->post_ok('/new_machine.html' => form => {
backend => $vm_name
,id_iso => search_id_iso('Alpine%')
,name => $name
,disk => 1
,ram => 1
,swap => 1
,submit => 1
}
)->status_is(302);
wait_request(debug => 0, background => 1);
return rvd_front->search_domain($name);
}
sub mojo_request($t, $req_name, $args) {
$t->post_ok("/request/$req_name/" => json => $args);
like($t->tx->res->code(),qr/^(200|302)$/);
my $response = $t->tx->res->json();
ok(exists $response->{request}) or return;
wait_request(background => 1);
}
sub _activate_storage_pools($vm) {
for my $sp ($vm->vm->list_all_storage_pools()) {
next if $sp->is_active;
......@@ -910,11 +976,22 @@ sub _remove_old_disks {
}
}
sub remove_old_user {
$USER_ADMIN->remove if $USER_ADMIN;
sub remove_old_user($user_name=undef) {
if (!$user_name) {
if ($USER_ADMIN) {
$user_name = $USER_ADMIN->name;
$USER_ADMIN->remove;
}
}
return if !$user_name;
my $user = Ravada::Auth::SQL->new(name => $user_name);
$user->remove if $user;
confess "Undefined connector" if !defined $CONNECTOR;
my $sth = $CONNECTOR->dbh->prepare("DELETE FROM users WHERE name=?");
$sth->execute(base_domain_name());
my $sth = $CONNECTOR->dbh->prepare("DELETE FROM users WHERE name = ?");
$sth->execute($user_name);
}
sub remove_old_user_ldap {
......
use warnings;
use strict;
use Data::Dumper;
use Mojo::JSON 'decode_json';
use Test::More;
use Test::Mojo;
use lib 't/lib';
use Test::Ravada;
no warnings "experimental::signatures";
use feature qw(signatures);
my $SECONDS_TIMEOUT = 15;
my $USERNAME = user_admin->name;
my $PASSWORD = "$$ $$";
my $USER;
########################################################################################
=pod
sub _init_mojo_client {
return if $USERNAME;
$T->get_ok('/')->status_is(200)->content_like(qr/name="login"/);
my $user_admin = user_admin();
my $pass = "$$ $$";
$USERNAME = $user_admin->name;
$PASSWORD = $pass;
mojo_login($T, $user_admin->name, $pass) or exit;
$T->get_ok('/')->status_is(200)->content_like(qr/choose a machine/i);
}
=cut
sub list_machines_user($t){
$t->websocket_ok("/ws/subscribe")->send_ok("list_machines_user")->message_ok->finish_ok;
return if !$t->message || !$t->message->[1];
my $name = base_domain_name();
my @machines = grep { $_->{name} =~ /^$name/ } @{decode_json($t->message->[1])};
_clean_machines_info(\@machines);
return @machines;
}
sub _clean_machines_info($machines) {
for (@$machines) {
for my $key (keys %$_ ) {
delete $_->{$key} unless $key =~ /id|name|base|clone/;
}
}
}
sub list_machines($t) {
$t->websocket_ok("/ws/subscribe")->send_ok("list_machines")->message_ok->finish_ok;
return if !$t->message || !$t->message->[1];
my $name = base_domain_name();
my @machines = grep { $_->{name} =~ /^$name/ } @{decode_json($t->message->[1])};
_clean_machines_info(\@machines);
return @machines;
}
sub _create_bases($t, $vm_name) {
my @base;
for ( 0 .. 1 ) {
my $base = mojo_create_domain($t, $vm_name);
push @base, ($base);
}
return @base;
}
sub test_bases($t, $bases) {
my $n_bases = 0;
my $n_machines = scalar(@$bases);
for my $base ( @$bases ) {
$t->get_ok("/machine/prepare/".$base->id.".json")->status_is(200);
wait_request(debug => 0, background => 1);
$n_bases++;
my @machines_user = list_machines_user($t);
is(@machines_user, $n_bases, Dumper(\@machines_user)) or exit;
my $n_clones = 2;
mojo_request($t, "clone", { id_domain => $base->id, number => $n_clones });
$n_machines += $n_clones;
my @machines = list_machines($t);
is( scalar(@machines), $n_machines, Dumper(\@machines)) or exit;
}
}
sub test_bases_non_admin($t,$bases) {
my $user_name = base_domain_name().".doe";
remove_old_user($user_name);
$USER = create_user($user_name, $$);
mojo_login($t, $user_name,$$);
my $n_public = 0;
for my $base (@$bases) {
is(list_machines_user($t),$n_public);
$base->is_public(1);
is($base->is_public, 1);
is(list_machines_user($t),++$n_public);
}
}
sub test_list_machines_non_admin($t, $bases) {
my $url = "/machine/clone/".$bases->[0]->id.".html";
$t->get_ok($url)->status_is(200);
wait_request(background => 1);
my @list_bases = list_machines_user($t);
my ($clone) = grep { $_->{name_clone} } @list_bases;
ok($clone,Dumper(\@list_bases)) or exit;
my @list_machines = list_machines($t);
is(scalar(@list_machines),0);
Ravada::Request->prepare_base(
uid => user_admin->id
,id_domain => $clone->{id_clone}
);
wait_request(background => 1);
user_admin->grant($USER,'shutdown_clones');
@list_machines = list_machines($t);
is(scalar(@list_machines),1) or exit;
}
########################################################################################
init('/etc/ravada.conf',0);
my $connector = rvd_back->connector;
like($connector->{driver} , qr/mysql/i) or BAIL_OUT;
if (!mojo_clean()) {
diag("Backend not available");
ok(0);
done_testing();
exit;
}
my $t = mojo_init();
for my $vm_name ( @{rvd_front->list_vm_types} ) {
diag("Testing Web Services in $vm_name");
mojo_login($t, $USERNAME, $PASSWORD);
my @bases = _create_bases($t, $vm_name);
is(list_machines_user($t), 0);
is(list_machines($t), scalar(@bases)) or exit;
test_bases($t,\@bases);
test_bases_non_admin($t, \@bases);
test_list_machines_non_admin($t,\@bases);
remove_old_domains_req();
while( list_machines_user($t) ) {
remove_old_domains_req();
}
}
mojo_clean($t);
done_testing();
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