Commit b6a5fb0b authored by Francesc Guasch's avatar Francesc Guasch Committed by Fernando Verdugo
Browse files

Test #1137 mojo (#1151)

* wip(frontend): return http status when failed login

issue #1137 and it will affect issue #1141 too

* test: allow test to connect to the MySQL DB

issue #1137

* test(frontend): test login and create machine

issue #1137

* wip(test): create base and clone it

issue #1137

* wip(test): show some req messages

* refactor(test): reconnect when timed out

issue #1137
parent df8735e3
......@@ -1306,6 +1306,9 @@ sub login {
." no-repeat bottom center scroll;\n\t}"];
sleep 5 if scalar(@error);
my @error_status;
@error_status = ( status => 403) if @error;
$c->render(
template => ($CONFIG_FRONT->{login_custom} or 'main/start')
,css => ['/css/main.css']
......@@ -1318,6 +1321,7 @@ sub login {
,login_message => $CONFIG_FRONT->{login_message}
,guide => $CONFIG_FRONT->{guide}
,login_hash => ''
,@error_status
);
}
......
......@@ -102,8 +102,16 @@ sub user_admin {
my $admin_name = base_domain_name();
my $admin_pass = "$$ $$";
eval {
$login = Ravada::Auth::SQL->new(name => $admin_name );
$login = Ravada::Auth::SQL->new(name => $admin_name, password => $admin_pass );
};
warn $@ if $@;
if ($@ && $@ =~ /Login failed/ ) {
$login = Ravada::Auth::SQL->new(name => $admin_name);
$login->remove();
$login = undef;
} elsif ($@) {
die $@;
}
$USER_ADMIN = $login if $login && $login->id;
$USER_ADMIN = create_user($admin_name, $admin_pass,1)
if !$USER_ADMIN;
......@@ -226,19 +234,22 @@ sub new_volume_name($domain=undef) {
return $name."_".$CONT_VOL++;
}
sub rvd_back($config=undef, $init=1) {
sub rvd_back($config=undef, $init=1, $sqlite=1) {
return $RVD_BACK if $RVD_BACK && !$config;
$RVD_BACK = 1;
init($config or $DEFAULT_CONFIG) if $init;
my @connector;
@connector = ( connector => connector() ) if $sqlite;
my $rvd = Ravada->new(
connector => connector()
@connector
, config => ( $config or $DEFAULT_CONFIG)
, warn_error => 1
);
$rvd->_install();
$CONNECTOR = $rvd->connector if !$sqlite;
user_admin();
$RVD_BACK = $rvd;
......@@ -259,7 +270,7 @@ sub rvd_front($config=undef) {
return $RVD_FRONT;
}
sub init($config=undef) {
sub init($config=undef, $sqlite = 1) {
if ($config && ! ref($config) && $config =~ /[A-Z][a-z]+$/) {
$config = { vm => [ $config ] };
......@@ -279,12 +290,17 @@ sub init($config=undef) {
DumpFile($FILE_CONFIG_TMP, $config) if $config && ref($config);
}
$Ravada::CONNECTOR = connector();
Ravada::Auth::SQL::_init_connector($CONNECTOR);
rvd_back($config, 0,$sqlite) if !$RVD_BACK;
if (!$sqlite) {
$CONNECTOR = $RVD_BACK->connector;
} else {
$Ravada::CONNECTOR = connector();
Ravada::Auth::SQL::_init_connector($CONNECTOR);
}
$Ravada::Domain::MIN_FREE_MEMORY = 512*1024;
rvd_back($config, 0) if !$RVD_BACK;
rvd_front($config) if !$RVD_FRONT;
$Ravada::VM::KVM::VERIFY_ISO = 0;
}
......@@ -636,9 +652,10 @@ sub wait_request {
$timeout = 60 if !defined $timeout && $background;
my $debug = ( delete $args{debug} or 0 );
my $skip = ( delete $args{skip} or [] );
my $skip = ( delete $args{skip} or ['enforce_limits','manage_pools','refresh_vms'] );
$skip = [ $skip ] if !ref($skip);
my %skip = map { $_ => 1 } @$skip;
%skip = ( enforce_limits => 1 ) if !keys %skip;
my $check_error = delete $args{check_error};
$check_error = 1 if !defined $check_error;
......@@ -657,7 +674,8 @@ sub wait_request {
my $req = Ravada::Request->open($req_id);
next if $skip{$req->command};
if ( $req->status ne 'done' ) {
diag("Waiting for request ".$req->id." ".$req->command) if $debug;
diag("Waiting for request ".$req->id." ".$req->command." ".$req->status
." ".($req->error or '')) if $debug && (time%5 == 0);
$done_all = 0;
} elsif (!$done{$req->id}) {
$done{$req->{id}}++;
......@@ -668,6 +686,8 @@ sub wait_request {
$post = '' if !defined $post;
if ( $done_all ) {
for my $req (@$request) {
next if $skip{$req->command};
confess if $req->command eq 'enforce_limits';
if ($req->status ne 'done') {
$done_all = 0;
diag("Waiting for request ".$req->id." ".$req->command);
......@@ -677,7 +697,7 @@ sub wait_request {
}
return if $done_all && $prev eq $post && scalar(keys %done) == $done_count;;
return if defined $timeout && time - $t0 >= $timeout;
sleep 1 if !$background;
sleep 1 if $background;
}
}
......
use warnings;
use strict;
use Data::Dumper;
use Test::More;
use Test::Mojo;
use Mojo::File 'path';
use lib 't/lib';
use Test::Ravada;
no warnings "experimental::signatures";
use feature qw(signatures);
my $SECONDS_TIMEOUT = 15;
my $t;
my $URL_LOGOUT;
my ($USERNAME, $PASSWORD);
my $SCRIPT = path(__FILE__)->dirname->sibling('../rvd_front.pl');
########################################################################################
sub remove_machines {
my $t0 = time;
for my $name ( @_ ) {
my $domain = rvd_front->search_domain($name) or next;
for my $clone ($domain->clones) {
my $req = Ravada::Request->remove_domain(
name => $clone->{name}
,uid => user_admin->id
);
}
_wait_request(debug => 1, background => 1, check_error => 1);
my $req = Ravada::Request->remove_domain(
name => $name
,uid => user_admin->id
);
}
_wait_request(debug => 1, background => 1);
if ( time - $t0 > $SECONDS_TIMEOUT ) {
login();
}
}
sub _wait_request(@args) {
my $t0 = time;
wait_request(@args);
if ( time - $t0 > $SECONDS_TIMEOUT ) {
login();
}
}
sub login( $user=$USERNAME, $pass=$PASSWORD ) {
if ($URL_LOGOUT) {
$t->get_ok('/logout');
$URL_LOGOUT = $t->tx->req->url->to_abs;
} {
$t->ua->get($URL_LOGOUT);
}
$t->post_ok('/' => form => {login => $user, password => $pass});
like($t->tx->res->code(),qr/^(200|302)$/);
# ->status_is(302);
exit if !$t->success;
}
########################################################################################
init('/etc/ravada.conf',0);
my $connector = rvd_back->connector;
like($connector->{driver} , qr/mysql/i) or BAIL_OUT;
$t = Test::Mojo->new($SCRIPT);
$t->ua->inactivity_timeout(300);
$t->get_ok('/')->status_is(200)->content_like(qr/name="login"/);
my $user_admin = user_admin();
my $pass = "$$ $$";
$USERNAME = $user_admin->name;
$PASSWORD = $pass;
login($user_admin->name, $pass);
$t->get_ok('/')->status_is(200)->content_like(qr/choose a machine/i);
my @bases;
my @clones;
for my $vm_name ( vm_names() ) {
diag("Testing new machine in $vm_name");
my $name = new_domain_name();
remove_machines($name,"$name-".user_admin->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);
my $base = rvd_front->search_domain($name);
ok($base) or next;
push @bases,($base->name);
$t->get_ok("/machine/prepare/".$base->id.".json")->status_is(200);
_wait_request(debug => 0, background => 1);
$base = rvd_front->search_domain($name);
is($base->is_base,1);
$t->get_ok("/machine/clone/".$base->id.".json")->status_is(200);
_wait_request(debug => 0, background => 1);
my $clone = rvd_front->search_domain($name."-".$user_admin->name);
ok($clone,"Expecting clone created");
is($clone->is_volatile,0) or exit;
}
ok(@bases,"Expecting some machines created");
remove_machines(@bases);
_wait_request(background => 1);
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