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

Refactor: iso download before create machine (#1680)

refactor: add a CDROM after ISO download

* test: create from ISO
parent 3102d34c
......@@ -60,3 +60,5 @@ jobs:
run: prove -lr t/17_templates.t
- name: Test mock VM
run: prove -lr t/vm/60_new_args.t t/30_request.t
- name: Test create from ISO
run: prove -lr t/request/25_create_from_iso.t
......@@ -794,6 +794,21 @@ sub _check_downloading($self) {
} else {
$self->after_request($req_download->id);
}
$sth = $$CONNECTOR->dbh->prepare("SELECT args FROM requests"
." WHERE id=?"
);
$sth->execute($self->id);
my $args_json = $sth->fetchrow();
my $args = decode_json($args_json);
if (exists $args->{iso_file} && !$args->{iso_file}) {
delete $args->{iso_file};
$sth = $$CONNECTOR->dbh->prepare("UPDATE requests set args=?"
." WHERE id=?"
);
$sth->execute(encode_json($args), $self->id);
}
}
sub _mark_iso_downloaded($id_iso) {
......@@ -1099,12 +1114,26 @@ Sets or gets de value of an argument of a Request
=cut
sub arg($self, $name, $value) {
sub arg($self, $name, $value=undef) {
confess "Unknown argument $name ".Dumper($self->{args})
if !exists $self->{args}->{$name} && !defined $value;
$self->{args}->{$name} = $value if defined $value;
if (defined $value) {
$self->{args}->{$name} = $value;
my $sth = $$CONNECTOR->dbh->prepare("SELECT args FROM requests"
." WHERE id=?"
);
$sth->execute($self->id);
my $args_json = $sth->fetchrow();
my $args = decode_json($args_json);
$args->{$name} = $value;
$sth = $$CONNECTOR->dbh->prepare("UPDATE requests set args=?"
." WHERE id=?"
);
$sth->execute(encode_json($args),$self->id);
}
return $self->{args}->{$name};
}
......
......@@ -1139,6 +1139,7 @@ sub _set_iso_downloading($self, $iso,$value) {
sub _iso_name($self, $iso, $req=undef, $verbose=1) {
return '' if !$iso->{has_cd};
my $iso_name;
if ($iso->{rename_file}) {
$iso_name = $iso->{rename_file};
......
......@@ -176,6 +176,7 @@ sub _add_cdrom($self, $domain, %args) {
my $sth = $$CONNECTOR->dbh->prepare("SELECT * FROM iso_images WHERE id=?");
$sth->execute($id_iso);
my $row = $sth->fetchrow_hashref();
return if !$row->{has_cd};
$iso_file = $row->{device};
if (!$iso_file) {
$iso_file = $row->{name};
......@@ -437,10 +438,14 @@ sub _search_iso($self, $id, $device = undef) {
}
sub _iso_name($self, $iso, $request=undef, $verbose=0) {
return '' if !$iso->{has_cd};
my $name = ($iso->{device} or $iso->{rename_file} or $iso->{file_re});
confess Dumper($iso) if !$name;
$name =~ s/(.*)\.\*(.*)/$1$2/;
$name =~ s/(.*)\.\+(.*)/$1.$2/;
$name =~ s/(.*)\[\\d.*?\]\+(.*)/${1}1$2/;
confess $name if $name =~ m{[*+\\]};
$name = $self->dir_img."/".$name unless $name =~ m{^/};
......
use warnings;
use strict;
use Carp qw(confess);
use Data::Dumper qw(Dumper);
use Mojo::JSON qw(encode_json decode_json);
use Test::More;
use lib 't/lib';
use Test::Ravada;
no warnings "experimental::signatures";
use feature qw(signatures);
########################################################################
sub test_create($vm, $with_iso_file=1, $no_cd=0) {
my $iso = _load_iso($vm, 'Debian%');
$iso = _load_iso($vm, 'Empty Machine 32 bits') if $no_cd;
my $name = new_domain_name();
my @args = (
id_owner => user_admin->id
,name => $name
,vm => $vm->type
,id_iso => $iso->{id}
);
push @args,(iso_file => "") if $with_iso_file;
my $req = Ravada::Request->create_domain(@args);
unless ($no_cd) {
my $req_download = Ravada::Request::_search_request('download');
ok($req_download,"Expecting a download request") or die;
$req_download->arg(test => 1);
}
wait_request(debug => 0);
my $domain = rvd_front->search_domain($name);
ok($domain);
my $disks= $domain->info(user_admin)->{hardware}->{disk};
my ($cdrom) = grep { $_->{file} =~ m{/.*iso$} } @$disks;
if ($no_cd) {
ok(!$cdrom,"Expecting no CDROM in ".Dumper($disks));
} else {
ok($cdrom,"Expecting a CDROM in ".Dumper($disks));
}
}
sub _load_iso($vm, $name) {
# my $name = 'debian%';
my $sth = connector->dbh->prepare("SELECT * FROM iso_images "
." WHERE name like ?"
);
$sth->execute($name);
my $iso = $sth->fetchrow_hashref;
die "No $name found in iso_images ".Dumper($iso) if !$iso->{id};
_remove_device($vm, $iso);
return $iso;
}
sub _remove_device($vm, $iso ) {
my $device = $iso->{device};
return if !$device;
# unlink $device or die "$! $device" if $device;
$iso->{device} =undef;
my $sth = connector->dbh->prepare("UPDATE iso_images "
." SET device=NULL "
." WHERE id=?"
);
$sth->execute($iso->{id});
$sth->finish;
}
########################################################################
init();
for my $vm_name ( vm_names() ) {
SKIP: {
my $vm = rvd_back->search_vm($vm_name);
my $msg = "SKIPPED test: No $vm_name VM found ";
if ($vm && $vm_name eq 'KVM' && $>) {
$msg = "SKIPPED: Test must run as root";
$vm = undef;
}
diag($msg) if !$vm;
skip $msg,10 if !$vm;
diag("Testing create from ISO in $vm_name");
test_create($vm,1);
test_create($vm);
test_create($vm,0,1);
}
}
end();
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