60_hardware.t 7.13 KB
Newer Older
Francesc Guasch's avatar
Francesc Guasch committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
use warnings;
use strict;

use Carp qw(confess);
use Data::Dumper;
use Digest::MD5;
use Test::More;

use lib 't/lib';
use Test::Ravada;

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

15
16
my %HAS_NOT_VALUE = map { $_ => 1 } qw(image jpeg zlib playback streaming);

Francesc Guasch's avatar
Francesc Guasch committed
17
##################################################################################
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#

sub test_graphics($vm, $node) {
    my $domain = create_domain($vm->type);
    for my $driver_name ( qw( image jpeg zlib playback streaming )) {
        my $driver = $domain->drivers($driver_name) or do {
            diag("No driver for $driver_name in ".$domain->type);
            next;
        };
        test_driver_migrate($vm, $node, $domain, $driver_name);
        for my $option ($driver->get_options) {
            next if $domain->get_driver($driver_name)
                && $domain->get_driver($driver_name) eq $option->{value};

            diag("Testing $driver_name $option->{value} in ".$vm->type);

            test_driver_clone($vm, $node, $domain, $driver_name, $option);

Francesc Guasch's avatar
Francesc Guasch committed
36
            last unless $ENV{TEST_LONG};
37
38
39
40
41
42
        }
    }
    $domain->remove(user_admin);
}

sub test_driver_clone($vm, $node, $domain, $driver_name, $option) {
Francesc Guasch's avatar
Francesc Guasch committed
43
    $domain->remove_base(user_admin) if $domain->is_base;
44
45
46
47
48
49
50
    my $req = Ravada::Request->set_driver(uid => user_admin->id
        , id_domain => $domain->id
        , id_option => $option->{id}
    );
    wait_request();
    is($req->status,'done');
    is($req->error,'');
Francesc Guasch's avatar
Francesc Guasch committed
51
52
53
    is($domain->get_driver($driver_name), $option->{value}
        , $driver_name);
    $domain->prepare_base(user_admin);
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    $domain->set_base_vm(node => $node, user => user_admin);

    my $clone = $domain->clone(name => new_domain_name, user => user_admin);
    $clone->migrate($node);
    my $clone2 = Ravada::Domain->open($clone->id);
    is($clone2->_vm->id,$node->id);
    is($clone2->get_driver($driver_name), $option->{value}
        , $driver_name);

    $clone->remove(user_admin);

    $domain->remove_base(user_admin);
}

sub test_driver_migrate($vm, $node, $domain, $driver_name) {
    my $option;
    my $driver = $domain->drivers($driver_name) or do {
            diag("No driver for $driver_name in ".$domain->type);
            next;
    };
Francesc Guasch's avatar
Francesc Guasch committed
74
    $domain->prepare_base(user_admin);
75
    $domain->set_base_vm(node => $node, user => user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
76
77
78
    for my $option ($driver->get_options) {
        next if defined $domain->get_driver($driver_name)
        && $domain->get_driver($driver_name) eq $option->{value};
79

Francesc Guasch's avatar
Francesc Guasch committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        diag("Testing $driver_name $option->{value} then migrate");
        my $clone = $domain->clone(name => new_domain_name, user => user_admin);
        my $req = Ravada::Request->set_driver(uid => user_admin->id
            , id_domain => $clone->id
            , id_option => $option->{id}
        );
        wait_request();
        is($req->status,'done');
        is($req->error,'');

        $clone->migrate($node);
        my $clone2 = Ravada::Domain->open($clone->id);
        is($clone2->_vm->id,$node->id);
        is($clone2->get_driver($driver_name), $option->{value}
            , $driver_name) or exit;

        $clone->remove(user_admin);
        last unless $ENV{TEST_LONG};
    }
    $domain->remove_base(user_admin);
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
}

sub test_drivers_type($type, $vm, $node) {

    my $domain = create_domain($vm->type);
    my $driver_type = $domain->drivers($type);

    if (!$HAS_NOT_VALUE{$type}) {
        my $value = $driver_type->get_value();
        ok($value,"Expecting value for driver type: $type ".ref($driver_type)."->get_value")
            or exit;
    }

    my @options = $driver_type->get_options();
    isa_ok(\@options,'ARRAY');
    ok(scalar @options > 1,"Expecting more than 1 options , got ".scalar(@options));

    for my $option (@options) {
        die "No value for driver ".Dumper($option)  if !$option->{value};

        diag("Testing $type $option->{value}");

        eval { $domain->set_driver($type => $option->{value}) };
        ok(!$@,"Expecting no error, got : ".($@ or ''));

        is($domain->get_driver($type), $option->{value}, $type);
        $domain->prepare_base(user_admin);
        $domain->set_base_vm(node => $node, user => user_admin);

        my $clone = $domain->clone(name => new_domain_name, user => user_admin);
        is($clone->get_driver($type), $option->{value}, $type);
        $clone->migrate($node);
        my $clone2 = Ravada::Domain->open($clone->id);
        is($clone2->_vm->id,$node->id);
        is($clone2->get_driver($type), $option->{value}, $type);

        $clone->remove(user_admin);
        $domain->remove_base(user_admin);

    }
    $domain->remove(user_admin);
}

sub test_drivers($vm, $node) {
    my @drivers = $vm->list_drivers();
     for my $driver ( @drivers ) {
         test_drivers_type($driver->name, $vm, $node);
     }

}
Francesc Guasch's avatar
Francesc Guasch committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

sub test_change_hardware($vm, @nodes) {
    diag("[".$vm->type."] testing remove with ".scalar(@nodes)." node ".join(",",map { $_->name } @nodes));
    my $domain = create_domain($vm);
    my $clone = $domain->clone(name => new_domain_name, user => user_admin);
    my @volumes = $clone->list_volumes();

    for my $node (@nodes) {
        $domain->set_base_vm( vm => $node, user => user_admin);
        my $clone2 = $node->search_domain($clone->name);
        ok(!$clone2);
        $clone->migrate($node);
        $clone2 = $node->search_domain($clone->name);
        ok($clone2);
    }

    my $info = $domain->info(user_admin);
    my ($hardware) = grep { !/disk|volume/ } keys %{$info->{hardware}};
    $clone->remove_controller($hardware,0);

    for my $node (@nodes) {
        my $clone2 = $node->search_domain($clone->name);
        ok(!$clone2,"Expecting no clone ".$clone->name." in remote node ".$node->name) or exit;
    }

    is($clone->_vm->is_local,1) or exit;
    for (@volumes) {
        ok(-e $_,$_) or exit;
    }
    $clone->remove(user_admin);
    $domain->remove(user_admin);
}

##################################################################################

clean();

$Ravada::Domain::MIN_FREE_MEMORY = 256 * 1024;

my @nodes;

for my $vm_name ( 'Void', 'KVM') {
    my $vm;
    eval { $vm = rvd_back->search_vm($vm_name) };

    SKIP: {

        my $msg = "SKIPPED: $vm_name virtual manager not found ".($@ or '');

        if ($vm && $>) {
            $msg = "SKIPPED: Test must run as root";
            $vm = undef;
        }

204
205
206
207
208
209
210
        my ($node1,$node2);
        if ($vm) {
            ($node1,$node2) = remote_node_2($vm_name);
            if (!$node2) {
                $vm = undef;
                $msg = "Expecting at least 2 nodes configured to test";
            }
211
212
213
        }
        diag($msg)      if !$vm;
        skip($msg,10)   if !$vm;
214

215
        diag("Testing remote node in $vm_name");
216

Francesc Guasch's avatar
Francesc Guasch committed
217
        clean_remote_node($node1);
218
        clean_remote_node($node2)   if $node2;
Francesc Guasch's avatar
Francesc Guasch committed
219

220
221
222
        test_graphics($vm, $node1);
        test_drivers($vm, $node1);

Francesc Guasch's avatar
Francesc Guasch committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
        test_change_hardware($vm);
        test_change_hardware($vm, $node1);
        test_change_hardware($vm, $node2);
        test_change_hardware($vm, $node1, $node2);

        NEXT:
        clean_remote_node($node1);
        remove_node($node1);
        clean_remote_node($node2);
        remove_node($node2);
    }

}

END: {
    clean();
    done_testing();
}