Skip to content

Commit

Permalink
Refactor: filter admin machines (UPC#1710)
Browse files Browse the repository at this point in the history
refactor: improved admin machines filter

* test: fixed when mock file wasn't removed
* wip(test): clean mock iso devices
  • Loading branch information
frankiejol authored Feb 17, 2022
1 parent 4e15051 commit 9695e19
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1954,7 +1954,7 @@ sub _sql_create_tables($self) {
volumes => {
id => 'integer PRIMARY KEY AUTO_INCREMENT',
id_domain => 'integer NOT NULL references `domains` (`id`) ON DELETE CASCADE',
name => 'char(200) NOT NULL',
name => 'char(255) NOT NULL',
file => 'varchar(255) NOT NULL',
n_order => 'integer NOT NULL',
info => 'TEXT',
Expand Down
3 changes: 2 additions & 1 deletion lib/Ravada/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,8 @@ sub _search_request($self,$command,%fields) {
my $args = decode_json($args_json);
my $found=1;
for my $key (keys %fields) {
if ( $args->{$key} ne $fields{$key} ) {
if (!exists $args->{$key} || !defined $args->{$key}
|| $args->{$key} ne $fields{$key} ) {
$found = 0;
last;
}
Expand Down
4 changes: 3 additions & 1 deletion public/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ ravadaApp.directive("solShowMachine", swMach)
|| $scope.list_machines[i].date_changed != mach.date_changed
){
var show=false;
if (mach._level == 0 ) {
if (mach._level == 0 && !$scope.filter && !$scope.show_active) {
mach.show=true;
}
if ($scope.show_machine[mach.id]) {
Expand All @@ -357,6 +357,7 @@ ravadaApp.directive("solShowMachine", swMach)
}
$scope.n_active=n_active_current;
if ( $scope.show_active ) { $scope.do_show_active() };
if ( $scope.filter) { $scope.show_filter() };
});
}
}
Expand Down Expand Up @@ -591,6 +592,7 @@ ravadaApp.directive("solShowMachine", swMach)

$scope.show_filter = function() {
$scope.hide_clones = true;
$scope.show_active = false;
$scope.n_active_current = 0;
$scope.n_active_hidden = 0;
for (var [key, mach ] of Object.entries($scope.list_machines)) {
Expand Down
44 changes: 34 additions & 10 deletions t/vm/d20_disks.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use feature qw(signatures);
use lib 't/lib';
use Test::Ravada;

no warnings "experimental::signatures";
use feature qw(signatures);
my @MOCK_ISOS;

init();
#############################################################################
Expand Down Expand Up @@ -66,7 +65,6 @@ sub test_frontend_refresh {
}

sub test_remove_disk($vm, %options) {
diag(Dumper(\%options));
my $make_base = delete $options{make_base};
my $clone = delete $options{clone};
my $remove_by_file = ( delete $options{remove_by_file} or 0);
Expand All @@ -80,7 +78,6 @@ sub test_remove_disk($vm, %options) {
if keys %options;

for my $index ( 0 .. 3 ) {
diag("\tindex=$index");
my $name = new_domain_name();
my $req = Ravada::Request->create_domain(
name => $name
Expand Down Expand Up @@ -220,6 +217,10 @@ sub test_add_cd($vm, $data) {
my $n_disks0 = scalar(@{$info0->{hardware}->{disk}});
my %targets0 = map { $_->{target} => 1 } @{$info0->{hardware}->{disk}};

if ($data->{device} eq 'cdrom' && exists $data->{file} && $data->{file} =~ /tmp/) {
open my $out, ">>",$data->{file} or die "$! $data->{file}";
close $out;
}
my $req = Ravada::Request->add_hardware(
id_domain => $domain->id
,name => 'disk'
Expand All @@ -244,6 +245,10 @@ sub test_add_cd($vm, $data) {
is($new_dev->{driver_type}, 'raw');
is($new_dev->{driver}, 'ide');
is($new_dev->{file},$data->{file});
if ($data->{device} eq 'cdrom' && exists $data->{file} && $data->{file} =~ /tmp/) {
unlink $data->{file} or die "$! $data->{file}";
}

}

sub test_add_disk {
Expand Down Expand Up @@ -380,7 +385,7 @@ sub test_add_cd_kvm($vm) {
test_add_cd($vm
, { 'device' => 'cdrom'
,'driver' => 'ide'
,'file' => "/tmp/a.iso"
,'file' => "/tmp/".new_domain_name()."a.iso"
});
}

Expand All @@ -404,8 +409,6 @@ sub _list_id_isos($vm) {
next if $iso->{name} =~ /Empty/;
next if $iso->{name} =~ /Android/i;

die Dumper($iso) if !defined $iso->{id};

$sth->execute($device, $iso->{id});
push @list, ( $iso->{id} );
}
Expand Down Expand Up @@ -485,15 +488,36 @@ sub _req_remove_cd($domain) {
wait_request( debug => 0);
}

sub _create_mock_iso($vm) {

my $file = $vm->dir_img()."/".new_domain_name()."a.iso";
open my $out, ">>",$file or die "$! $file";
print $out "test\n";
close $out;

push @MOCK_ISOS,($file);

return $file;
}

sub remove_mock_isos() {
for my $file (@MOCK_ISOS) {
next if $file !~ m{/tst_};
unlink $file if -e $file;
}
}

sub _req_add_cd($domain) {
my $info = $domain->info(user_admin);
my $disks = $info->{hardware}->{disk};

my $file = _create_mock_iso($domain->_vm);
my $req = Ravada::Request->add_hardware(
uid => Ravada::Utils::user_daemon->id
,id_domain => $domain->id
,name => 'disk'
,data => { type => 'cdrom'
,file => "/var/tmp/a.iso"
,file => $file
}
);
wait_request(debug => 0);
Expand Down Expand Up @@ -557,6 +581,7 @@ sub test_cdrom($vm) {
_req_add_cd($domain);

remove_domain($domain);
remove_mock_isos();

}
}
Expand Down Expand Up @@ -588,10 +613,9 @@ for my $vm_name (vm_names() ) {
test_add_cd_kvm($vm) if $vm_name eq 'KVM';

for my $id_iso ( _list_id_isos($vm) ) {
diag("Testing id iso = ".($id_iso or '<UNDEF>'));
for my $by_file ( 1, 0 ) {
for my $by_index ( 0, 1 ) {
diag("by_file=$by_file, by_index=$by_index");
diag("Testing id_iso: $id_iso , by_file:$by_file, by_index:$by_index");
test_remove_disk($vm
,clone => 1
,id_iso => $id_iso
Expand Down

0 comments on commit 9695e19

Please sign in to comment.