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

Fix iso file in new machine (#1658)

fix(frontend): fix ISO file field in new machine

* test: new machine form required fields
* test: check iso file change and empty machine
* refactor: best practice not to create var in if
* refactor: raise error when creating without a CD

issue #1657
parent 0ce5fa71
......@@ -721,6 +721,7 @@ sub _update_isos {
,xml => 'empty-i386.xml'
,xml_volume => 'jessie-volume.xml'
,min_disk_size => '0'
,has_cd => 0
}
,empty_64bits => {
name => 'Empty Machine 64 bits'
......@@ -728,6 +729,7 @@ sub _update_isos {
,xml => 'empty-amd64.xml'
,xml_volume => 'jessie-volume.xml'
,min_disk_size => '0'
,has_cd => 0
}
);
$self->_scheduled_fedora_releases(\%data) if $0 !~ /\.t$/;
......@@ -2264,6 +2266,7 @@ sub _upgrade_tables {
$self->_upgrade_table('iso_images','file_re','char(64)');
$self->_upgrade_table('iso_images','device','varchar(255)');
$self->_upgrade_table('iso_images','min_disk_size','int (11) DEFAULT NULL');
$self->_upgrade_table('iso_images','has_cd','int (1) DEFAULT "1"');
$self->_upgrade_table('users','language','char(40) DEFAULT NULL');
if ( $self->_upgrade_table('users','is_external','int(11) DEFAULT 0')) {
......
......@@ -882,11 +882,12 @@ sub _domain_create_from_iso {
my $device_cdrom;
confess "Template ".$iso->{name}." has no URL, iso_file argument required."
if !$iso->{url} && !$iso_file && !$iso->{device};
if $iso->{has_cd} && !$iso->{url} && !$iso_file && !$iso->{device};
if ($iso_file) {
if ( $iso_file ne "<NONE>") {
if (defined $iso_file) {
if ( $iso_file ne "<NONE>" || $iso_file ) {
$device_cdrom = $iso_file;
}
}
......@@ -915,7 +916,7 @@ sub _domain_create_from_iso {
my $xml = $self->_define_xml($args{name} , "$DIR_XML/$iso->{xml}");
if ($device_cdrom) {
if ($device_cdrom && $device_cdrom ne '<NONE>') {
_xml_modify_cdrom($xml, $device_cdrom);
} else {
_xml_remove_cdrom($xml);
......
......@@ -116,12 +116,12 @@ ravadaApp.directive("solShowMachine", swMach)
$scope.showMinSize = false;
$scope.min_size = 1;
}
return (id.device != null) ? id.device : "<NONE>";
return (id.device != null) ? id.device : "";
};
$scope.onIdIsoSelected = function() {
$scope.iso_file = $scope.change_iso(this.id_iso)
$scope.id_file = ($scope.iso_file === "<NONE>") ? "" : $scope.iso_file;
// $scope.id_file = ($scope.iso_file === "<NONE>") ? "" : $scope.iso_file;
};
$scope.validate_new_name = function() {
......
......@@ -5,6 +5,7 @@ use Data::Dumper;
use Test::More;
use HTML::Lint;
use Mojo::DOM;
no warnings "experimental::signatures";
use feature qw(signatures);
......@@ -108,6 +109,32 @@ sub _check_html_lint($url, $content, $option = {}) {
}
sub _load_file($name) {
open my $in,"<",$name or die "$! $name";
my $string = join("",<$in>);
close $in;
return $string;
}
sub test_form_new_machine() {
my $file = "templates/ng-templates/new_machine_template.html.ep";
my $dom = Mojo::DOM->new(_load_file($file));
my $form_name = 'new_machineForm';
my $form = $dom->find('form')->grep( sub {$_->attr('name') eq $form_name});
ok($form->[0], "Expecting form name=$form_name") or return;
for my $name ('id_iso', 'name', 'iso_file', 'memory','disk'
, 'swap', 'data') {
my $inputs = $form->[0]->find("input")
->grep( sub { $_->attr('name') eq $name } );
ok($inputs->[0],"Expecting input name='$name' in $file");
}
}
##################################################################3
test_form_new_machine();
test_validate_html_local("templates/bootstrap");
test_validate_html_local("templates/main");
test_validate_html_local("templates/ng-templates");
......
......@@ -67,7 +67,7 @@ sub test_many_clones($base) {
$n_clones = 10 if !$ENV{TEST_STRESS} && ! $ENV{TEST_LONG};
$t->post_ok('/machine/copy' => json => {id_base => $base->id, copy_number => $n_clones, copy_ram => 0.128 });
like($t->tx->res->code(),qr/^(200|302)$/) or die $t->tx->res->body->to_string;
like($t->tx->res->code(),qr/^(200|302)$/) or die $t->tx->res->body;
my $response = $t->tx->res->json();
ok(exists $response->{request}) or return;
......@@ -80,7 +80,7 @@ sub test_many_clones($base) {
{ id_domain => $base->id, sequential => $sequential
}
);
like($t->tx->res->code(),qr/^(200|302)$/) or die $t->tx->res->body->to_string;
like($t->tx->res->code(),qr/^(200|302)$/) or die $t->tx->res->body;
$response = $t->tx->res->json();
if (exists $response->{request}) {
wait_request(request => $response->{request}, background => 1);
......@@ -580,6 +580,89 @@ sub _download_iso($iso_name) {
}
sub test_new_machine($t) {
$t->get_ok("/new_machine.html")->status_is(200) or return;
my $dom = Mojo::DOM->new( $t->tx->res->body );
my $form_name = 'new_machineForm';
my $form = $dom->find('form')->grep( sub {$_->attr('name') eq $form_name});
ok($form->[0], "Expecting form name=$form_name") or return;
for my $name ('id_iso', 'name', 'iso_file' ) {
my $inputs = $form->[0]->find("input")
->grep( sub { $_->attr('name') eq $name } );
ok($inputs->[0],"Expecting input name='$name'");
}
}
sub test_new_machine_empty($t, $vm_name) {
for my $iso_file ( '', '<NONE>') {
for my $iso_name ( 'Empty%32', 'Empty%64') {
my $name = new_domain_name();
$t->post_ok('/new_machine.html' => form => {
backend => $vm_name
,id_iso => search_id_iso($iso_name)
,iso_file => $iso_file
,name => $name
,disk => 1
,ram => 1
,swap => 1
,submit => 1
}
)->status_is(302);
wait_request();
my $domain = rvd_front->search_domain($name);
ok($domain);
remove_domain_and_clones_req($domain) if $domain;
}
}
}
sub test_new_machine_change_iso($t, $vm_name) {
my $iso_name = 'Alpine%32 bits';
_download_iso($iso_name);
my $iso_name2 = 'Alpine%64 bits';
_download_iso($iso_name2);
my $isos = rvd_front->list_iso_images();
my $id_iso = search_id_iso($iso_name);
my $id_iso2 = search_id_iso($iso_name2);
my ($iso2) = grep { $_->{id} == $id_iso2 } @$isos;
my $name = new_domain_name();
$t->post_ok('/new_machine.html' => form => {
backend => $vm_name
,id_iso => $id_iso
,iso_file => $iso2->{device}
,name => $name
,disk => 1
,ram => 1
,swap => 1
,submit => 1
}
)->status_is(302);
wait_request();
my $domain = rvd_front->search_domain($name);
my $xml = XML::LibXML->load_xml(string => $domain->_data_extra('xml'));
my @sources = $xml->findnodes("/domain/devices/disk/source");
my ($cd) = grep { $_->getAttribute('file') eq $iso2->{device} }
@sources;
ok($cd,"Expecting a disk device with source file=$iso2->{device}"
." in $name")
or exit;
remove_domain_and_clones_req($domain); #remove and wait
}
sub test_create_base($t, $vm_name, $name) {
my $iso_name = 'Alpine%';
_download_iso($iso_name);
......@@ -654,6 +737,11 @@ for my $vm_name ( @{rvd_front->list_vm_types} ) {
_init_mojo_client();
test_new_machine($t);
if ($vm_name eq 'KVM') {
test_new_machine_change_iso($t, $vm_name);
test_new_machine_empty($t, $vm_name);
}
my $base0 = test_create_base($t, $vm_name, $name);
push @bases,($base0->name);
test_admin_can_do_anything($t, $base0);
......
......@@ -1085,20 +1085,23 @@ sub test_fill_memory($vm, $node, $migrate) {
my $clone = rvd_back->search_domain($clone_name) or last;
ok($clone,"Expecting clone $clone_name") or exit;
$clone->migrate($node) if $migrate;
wait_request(debug => 0);
eval { $clone->start(user_admin) };
$error = $@;
like($error, qr/(^$|No free memory)/);
exit if $error && $error !~ /No free memory/;
last if $error;
$nodes{$clone->_vm->name}++;
my $clone_2 = Ravada::Domain->open($clone->id);
$nodes{$clone_2->_vm->name}++;
last if $migrate && exists $nodes{$vm->name} && $nodes{$vm->name} > 2;
if (keys(%nodes) > 1) {
if (scalar keys(%nodes) > 0) {
$memory = int($memory*1.5);
}
}
ok(exists $nodes{$vm->name},"Expecting some clones to node ".$vm->name." ".$vm->id);
ok(exists $nodes{$node->name},"Expecting some clones to node ".$node->name." ".$node->id);
ok(exists $nodes{$node->name},"Expecting some clones to node ".$node->name." ".$node->id) or exit;
_remove_clones($base);
}
......
<div class="tab-pane fade show active" id="fromtemplate" role="tabpanel">
<div class="card-body">
<div ng-show="!images">Loading... <i class="fas fa-sync-alt fa-spin"></i></div>
<form name="new_machineForm" role="form" method="post" action="/new_machine.html" novalidate ng-show="images">
<div ng-hide="isos && isos.length">
Loading ... <i class="fas fa-sync-alt fa-spin"></i>
</div>
<form name="new_machineForm" role="form" method="post"
action="/new_machine.html" novalidate ng-cloak="1"
ng-show="isos && isos.length">
<div class="form-group row">
<label for="backend" class="col-xl-3 col-form-label"><%=l 'Backend' %> <a
title="Choose the virtualization type of the Virtual Machine." ng-show="backends.length > 1"><i class="fa fa-info-circle"></i></a></label>
......@@ -16,8 +20,7 @@
></select>
</div>
</div>
<div ng-if="backend == 'KVM' || backend == 'Void'" class="form-group">
<div ng-show="backend == 'KVM' || backend == 'Void'" class="form-group">
<div class="from-group row">
<label for="id_iso" class="col-xl-3 col-form-label"><%=l 'Select Template' %> <a
title="Choose the OS you want to install."><i class="fa fa-info-circle"></i></a></label>
......@@ -42,7 +45,7 @@
</div>
</div>
</div>
<div class="from-group row" ng-show="id_iso.name">
<div class="from-group row" ng-show="id_iso.name && id_iso.has_cd">
<br/>
<label for="iso_file" class="col-xl-3 col-form-label"><%=l 'Select ISO' %> <a
title="Select the .iso file the machine will utilize when installing the OS." href="http://ravada.readthedocs.io/en/latest/docs/new_iso_image.html"><i class="fa fa-info-circle"></i></a>
......@@ -50,8 +53,8 @@
</label>
<div class="col-lg-9" ng-init="iso_file = '<NONE>'" >
<input type="text" class="form-control" placeholder="<%=l 'Type the ISO pathname' %>"
name="id_file"
ng-model="id_file"
name="iso_file"
ng-model="iso_file"
ng-hide="refresh_working || !isos"
required=""
uib-typeahead="item for item in getVisualizableObjects($viewValue, isos)"
......@@ -71,13 +74,13 @@
</div>
</div>
<div ng-if="backend == 'LXC'" class="from-group row">
<div ng-show="backend == 'LXC'" class="from-group row">
<label for="id_template" class="col-xl-3 col-form-label"><%=l 'Template' %></label>
<div class="col-lg-9">
<select name ="id_template"
ng-model="id_template"
ng-options="item.name for item in templates_lxc track by item.id"
required="">
>
</select>
</div>
</div>
......@@ -89,7 +92,7 @@
</div>
</div>
<div class="form-group row" ng-if="backend == 'KVM' || backend =='Void'">
<div class="form-group row" ng-show="backend == 'KVM' || backend =='Void'">
<label for="disk" class="col-xl-3 col-form-label"><%=l 'System Disk: (GB)' %></label>
<div class="col-lg-2">
<input class="form-control" ng-model="ddsize" type="number" min ="{{min_size}}" name="disk" required="">
......@@ -98,7 +101,7 @@
<font color="orange"><%=l 'The Minimum Disk Size needed for this ISO is' %> {{min_size}}GB.</font>
</div>
</div>
<div class="form-group row" ng-if="backend == 'KVM' || backend=='Void'">
<div class="form-group row" ng-show="backend == 'KVM' || backend=='Void'">
<label for="swap" class="col-xl-3 col-form-label">
<span ng-model="swap.label"
ng-class='{"text-muted": !swap.enabled}'><%=l 'Swap' %>
......@@ -120,7 +123,7 @@
<span>Content will be cleaned on restore and shutdown</span>
</div>
</div>
<div class="form-group row" ng-if="backend == 'KVM' || backend=='Void'">
<div class="form-group row" ng-show="backend == 'KVM' || backend=='Void'">
<label for="swap" class="col-xl-3 col-form-label">
<span ng-model="data.label"
ng-class='{"text-muted": !data.enabled}'><%=l 'Data' %>
......
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