10_basic.t 17.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);


use_ok('Ravada');
17
init();
18
19
20
21
22

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

sub test_reuse_vm($node) {
    my $domain = create_domain($node->type);
23
    $domain->add_volume(swap => 1, size => 512 * 1024);
24
25
26
27
28
29
    $domain->prepare_base(user_admin);
    $domain->set_base_vm(vm => $node, user => user_admin);

    my $clone1 = $domain->clone(name => new_domain_name, user => user_admin);

    my $clone2 = $domain->clone(name => new_domain_name, user => user_admin);
30
31
    is($clone1->_vm, $clone2->_vm, $clone1->_vm->name);
    is($clone1->_vm->id, $clone2->_vm->id);
32

Francesc Guasch's avatar
Francesc Guasch committed
33
34
    is($clone1->list_instances,1);

35
36
37
38
    $clone1->migrate($node);
    is($clone1->_data('id_vm'), $node->id);
    $clone2->migrate($node);
    is($clone2->_data('id_vm'), $node->id);
Francesc Guasch's avatar
Francesc Guasch committed
39
    is($clone1->list_instances,2);
40
41
42
43
44
45
46
47
48
49
50
51
52
53

    is($clone1->_vm, $clone2->_vm);
    is($clone1->_vm, $clone2->_vm);
    is($clone1->_vm->{_ssh}, $clone2->_vm->{_ssh});

    is($clone1->is_local, 0 );
    test_remove($clone1, $node);

    my $vm_local = rvd_back->search_vm($node->type,'localhost');
    is($vm_local->is_local, 1);
    $clone2->migrate($vm_local);

    is($clone2->is_local, 1 );
    test_remove($clone2, $node);
Francesc Guasch's avatar
Francesc Guasch committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

}

sub test_remove_req($vm, $node) {
    my $domain = create_domain($node->type);
    $domain->prepare_base(user_admin);
    $domain->set_base_vm(vm => $node, user => user_admin);

    my $clone1 = $domain->clone(name => new_domain_name, user => user_admin);
    $clone1->migrate($node);

    my $req = Ravada::Request->remove_domain(
               uid => user_admin->id
             ,name => $clone1->name
    );

70
    rvd_back->_process_requests_dont_fork();
Francesc Guasch's avatar
Francesc Guasch committed
71
72
73
74
75

    is($req->status, 'done');
    is($req->error, '');

    $domain->remove(user_admin);
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
}

sub test_remove($clone, $node) {

    diag("Testing remove is_local = ".$clone->is_local);

    my @volumes = $clone->list_volumes();
    ok(scalar @volumes,"Expecting volumes in ".$clone->name);

    for my $file ( @volumes ) {
        ok( -e $file, "Expecting file '$file' in localhost");
        my ($out, $err) = $node->run_command("ls $file");
        ok($out, "Expecting file '$file' in ".$node->name) or exit;
    }

Francesc Guasch's avatar
Francesc Guasch committed
91
    is($clone->list_instances,2) or confess;
92
93
94
95
96
97
98
99
    $clone->remove(user_admin);
    for my $file ( @volumes ) {
        ok(! -e $file, "Expecting no file '$file' in localhost") or exit;
        my ($out, $err) = $node->run_command("ls $file");
        ok(!$out, "Expecting no file '$file' in ".$node->name) or exit;
    }
}

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
150
151
152
153
154
155
156
157
158
159
160
161
162
sub test_iptables($node, $node2) {
    my $domain = create_domain($node->type);
    $domain->prepare_base(user_admin);
    $domain->set_base_vm(vm => $node, user => user_admin) if !$domain->base_in_vm($node->id);

    my $clone1 = $domain->clone(name => new_domain_name, user => user_admin);

    flush_rules_node($node);

    $clone1->migrate($node) if $clone1->_vm->id != $node->id;

    my $remote_ip1 = '1.1.1.1';
    $clone1->start(user => user_admin, remote_ip => $remote_ip1);
    is($clone1->is_active,1,"[".$node->type."] expecting ".$clone1->name." active "
                                    ." in ".$node->name) or exit;

    my ($local_ip, $local_port)
        = $clone1->display(user_admin) =~ m{(\d+\.\d+\.\d+\.\d+)\:(\d+)};

    # check iptabled added on node
    my @found = search_iptable_remote(
        node => $node
        ,remote_ip => $remote_ip1
        ,local_port => $local_port
    );
    is(scalar @found,1,$node->name." $remote_ip1:$local_port".Dumper(\@found)) or exit;
    @found = search_iptable_remote(
        node => $node
        ,local_port => $local_port
        ,jump => 'DROP'
    );
    is(scalar @found,1,Dumper(\@found));
    #
    # check iptabled NOT added on node2
    @found = search_iptable_remote(
        node => $node2
        ,remote_ip => $remote_ip1
        ,local_port => $local_port
    );
    is(scalar @found,0,$node2->name." $remote_ip1:$local_port".Dumper(\@found)) or exit;
    @found = search_iptable_remote(
        node => $node2
        ,local_port => $local_port
        ,jump => 'DROP'
    );
    is(scalar @found,0,Dumper(\@found));
    #    warn Dumper($list->{filter});

    $clone1->remove(user_admin);

    @found = search_iptable_remote(
        node => $node
        ,remote_ip => $remote_ip1
        ,local_port => $local_port
    );
    is(scalar @found,0,$node->name." $remote_ip1:$local_port".Dumper(\@found)) or exit;
    @found = search_iptable_remote( node => $node
        ,local_port => $local_port
        ,jump => 'DROP'
    );
    is(scalar @found,0,$node->name." ".Dumper(\@found)) or exit;
    $domain->remove(user_admin);
}
163
164
165
166
167
168
169
170
171
sub test_iptables_close($vm, $node) {
    flush_rules_node($vm);
    flush_rules_node($node);

    my $domain = create_domain($node->type);

    $domain->prepare_base(user_admin);
    $domain->set_base_vm(vm => $node, user => user_admin) if !$domain->base_in_vm($node->id);

172
173
174
175
176
177
    my ($remote_ip1, $remote_ip2) = ('1.1.1.1','2.2.2.2');
    my ($clone_local, $clone_remote) = _create_2_clones_same_port($vm, $node, $domain
                                , $remote_ip1, $remote_ip2);
    isnt($clone_local->_vm->id, $clone_remote->_vm->id);
    my ( $local_port1 ) = $clone_local->display(user_admin)=~ m{://.*:(\d+)};
    my ( $local_port2 ) = $clone_remote->display(user_admin)=~ m{://.*:(\d+)};
178
179
180

    my @found = search_iptable_remote(
       node => $vm
181
182
183
184
185
186
187
        ,remote_ip => $remote_ip1
        ,local_port => $local_port2
    );
    is(scalar @found,1,$vm->name." $remote_ip2:$local_port2".Dumper(\@found)) or exit;

    @found = search_iptable_remote(
       node => $node
188
189
190
191
192
        ,remote_ip => $remote_ip2
        ,local_port => $local_port2
    );
    is(scalar @found,1,$vm->name." $remote_ip2:$local_port2".Dumper(\@found)) or exit;

193
    $clone_local->shutdown_now(user_admin);
194
195

    @found = search_iptable_remote(
196
       node => $vm
197
198
199
200
201
202
        ,remote_ip => $remote_ip1
        ,local_port => $local_port1
    );
    is(scalar @found,0,$node->name." $remote_ip1:$local_port1".Dumper(\@found));

    @found = search_iptable_remote(
203
       node => $node
204
205
206
207
208
        ,remote_ip => $remote_ip2
        ,local_port => $local_port2
    );
    is(scalar @found,1,$vm->name." $remote_ip2:$local_port2".Dumper(\@found));

209
210
211
212
    for my $clone0 ( $domain->clones) {
        my $clone = Ravada::Domain->open($clone0->{id});
        $clone->remove(user_admin);
    }
213
214
215
    $domain->remove(user_admin);

}
216

217
218
sub _create_2_clones_same_port($vm, $node, $base, $ip_local, $ip_remote) {
    my $clone_local = $base->clone(name => new_domain_name, user => user_admin);
219
220
    #TODO: add a flag in start to force node to start in
    $clone_local->{_migrated} = 1;
221
222
    my $clone_remote= $base->clone(name => new_domain_name, user => user_admin);
    $clone_remote->migrate($node);
223

224
225
226
    $clone_local->start(user => user_admin, remote_ip => $ip_local);
    $clone_remote->start(user => user_admin, remote_ip => $ip_remote);

227
    for (1 .. 100 ) {
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
        my ($port_local) = $clone_local->display(user_admin) =~ m{://.*:(\d+)};
        my ($port_remote) = $clone_remote->display(user_admin) =~ m{://.*:(\d+)};

        return($clone_local, $clone_remote) if $port_local == $port_remote;

        my $clone3 = $base->clone(name => new_domain_name, user => user_admin);
        if ($port_local < $port_remote) {
            $clone3->migrate($vm) if $clone3->_vm->id != $vm->id;
            $clone_local = $clone3;
            $clone_local->start(user => user_admin, remote_ip => $ip_local);
        } else {
            $clone3->migrate($node) if $clone3->_vm->id != $node->id;
            $clone_remote = $clone3;
            $clone_remote->start(user => user_admin, remote_ip => $ip_remote);
        }
    }
}

Francesc Guasch's avatar
Francesc Guasch committed
246
247
sub test_set_vm($vm, $node) {
    my $base = create_domain($vm);
248
249
250
    my $info = $base->info(user_admin);
    is($info->{bases}->{$vm->id},0);

Francesc Guasch's avatar
Francesc Guasch committed
251
252
253
254
255
256
    my $req = Ravada::Request->set_base_vm(
        id_domain => $base->id
        , id_vm => $node->id
        , value => 1
        , uid => user_admin->id
    );
257
    rvd_back->_process_requests_dont_fork();
Francesc Guasch's avatar
Francesc Guasch committed
258
259
260
261
262
263
264
265
    is($req->status, 'done');
    is($req->error, '');

    is($base->_vm->id, $vm->id);

    my $base2 = Ravada::Domain->open($base->id);
    is($base2->_vm->id, $vm->id);

266
267
268
269
270
    $info = $base2->info(user_admin);
    is($info->{bases}->{$vm->id},1,Dumper($info->{bases})) or exit;
    is($info->{bases}->{$node->id},1,$node->id." "
        .Dumper($info->{bases})) or exit;

Francesc Guasch's avatar
Francesc Guasch committed
271
272
    is($base->list_instances,2) or exit;

273
274
275
276
277
    my $base_f = Ravada::Front::Domain->open($base->id);
    $info = $base_f->info(user_admin);
    is($info->{bases}->{$vm->id},1) or exit;
    is($info->{bases}->{$node->id},1) or exit;

Francesc Guasch's avatar
Francesc Guasch committed
278
279
    is($base_f->list_instances,2) or exit;

280
    test_bind_ip($node, $base,'1.2.3.4',1);
281
282
    test_bind_ip($node, $base,'1.2.3.4');
    test_bind_ip($node, $base);
Francesc Guasch's avatar
Francesc Guasch committed
283
    $base->remove(user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
284
285
286
    is(scalar($base->list_instances),undef);
}

287
288
289
290
sub test_bind_ip($node, $base, $remote_ip=undef, $config=undef) {
    if ($config) {
        rvd_back->display_ip("127.0.0.1");
    }
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
    my @clone;
    my $clone_2;
    my @remote_ip;
    @remote_ip = ( remote_ip => $remote_ip ) if $remote_ip;
    for (1 .. 20) {
        my $clone= $base->clone( user => user_admin, name => new_domain_name);
        if ($clone->type eq 'KVM') {
            $clone->_set_spice_ip(undef,'2.3.4.5');
            my $cloneb = Ravada::Domain->open($clone->id);
        }
        my $req = Ravada::Request->start_domain(uid => user_admin->id
            ,id_domain => $clone->id
            ,@remote_ip
        );
        wait_request();
306
307
308
309
310
311
312
313
314
315
316
317
318
        is($req->error, '');
        my $clone_v = Ravada::Domain->open($clone->id);
        if ($clone_v->is_local) {
            if (!$config) {
                my $vm_ip = $clone_v->_vm->ip;
                like($clone_v->display(user_admin),qr($vm_ip)) or confess $clone_v->name;
            } else {
                like($clone_v->display(user_admin),qr(127.0.0.1)) or die $clone_v->name;
            }
        } else {
            my $node_ip = $node->ip;
            like($clone_v->display(user_admin), qr($node_ip));
        }
319
320
321
322
323
324
325
326
327
328
329
330
        is($req->status,'done');
        is($req->error, '');
        push @clone,($clone);
        $clone_2 = Ravada::Domain->open($clone->id);
        last if $clone_2->_vm->id == $node->id;
    }
    my $node_ip = $node->ip;
    is($clone_2->_vm->id, $node->id) or exit;
    like($clone_2->display(user_admin),qr($node_ip)) or exit;
    for (@clone) {
        $_->remove(user_admin);
    }
331
    rvd_back->display_ip("") if $config;
Francesc Guasch's avatar
Francesc Guasch committed
332
333
}

334
335
336
337
338
339
340
sub test_volatile($vm, $node) {
    my $base = create_domain($vm);
    $base->prepare_base(user_admin);
    $base->set_base_vm(user => user_admin, node => $node);
    $base->volatile_clones(1);

    my @clones;
Francesc Guasch's avatar
Francesc Guasch committed
341
    for ( 1 .. 10 ) {
342
        my $clone = $base->clone(user => user_admin, name => new_domain_name);
Francesc Guasch's avatar
Francesc Guasch committed
343
344
        is($clone->_vm->is_active,1);
        is($clone->is_active(),1,"Expecting clone ".$clone->name." active on ".$clone->_vm->name);
345
346
347
348
349
        push @clones,($clone);
        last if $clone->_vm->id == $node->id;
    }
    is($clones[-1]->_vm->id, $node->id);

Francesc Guasch's avatar
Francesc Guasch committed
350
351
    is($clones[-1]->list_instances,1,Dumper([$clones[-1]->list_instances])) or exit;

352
353
354
355
    for (@clones) {
        $_->remove(user_admin);
    }
    $base->remove(user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
356
    is(scalar($base->list_instances),undef,Dumper([$base->list_instances])) or exit;
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
}

sub test_volatile_req($vm, $node) {
    my $base = create_domain($vm);
    $base->prepare_base(user_admin);
    $base->set_base_vm(user => user_admin, node => $node);
    $base->volatile_clones(1);
    ok($base->base_in_vm($node->id));

    my @clones;
    for ( 1 .. 4 ) {
        my $clone_name = new_domain_name;
        my $req = Ravada::Request->create_domain(
           id_base => $base->id
             ,name => $clone_name
            ,id_owner => user_admin->id
        );
        rvd_back->_process_all_requests_dont_fork();
        is($req->status, 'done');
        is($req->error,'');

        my $clone = rvd_back->search_domain($clone_name);
Francesc Guasch's avatar
Francesc Guasch committed
379
380
        is($clone->is_active(),1,"[".$vm->type."] expecting clone ".$clone->name
            ." active on node ".$clone->_vm->name);
381
382
383
384
385
386
387
388
389
390
391
392
393
394
        push @clones,($clone);
        last if $clone->_vm->id == $node->id;
    }
    is($clones[-1]->_vm->id, $node->id) or exit;

    for (@clones) {
        $_->remove(user_admin);
    }
    $base->remove(user_admin);
}

sub test_volatile_tmp_owner($vm, $node) {
    my $base = create_domain($vm);
    $base->prepare_base(user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
395
    $base->is_public(1);
396
397
398
399
400
401
402
403
404
405
    $base->set_base_vm(user => user_admin, node => $node);

    my $user = Ravada::Auth::SQL::add_user(name => 'mcnulty', is_temporary => 1);

    my @clones;
    for ( 1 .. 4 ) {
        my $clone_name = new_domain_name;
        my $req = Ravada::Request->create_domain(
           id_base => $base->id
             ,name => $clone_name
Francesc Guasch's avatar
Francesc Guasch committed
406
            ,id_owner => $user->id
407
408
409
410
411
412
        );
        rvd_back->_process_all_requests_dont_fork();
        is($req->status, 'done');
        is($req->error,'');

        my $clone = rvd_back->search_domain($clone_name);
Francesc Guasch's avatar
Francesc Guasch committed
413
414
        is($clone->is_active,1,"[".$node->type."] expecting ".$clone->name." active "
                                    ." in ".$clone->_vm->name) or exit;
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
        push @clones,($clone);
        last if $clone->_vm->id == $node->id;
    }
    is($clones[-1]->_vm->id, $node->id);

    for (@clones) {
        $_->shutdown_now(user_admin);
        $_->remove(user_admin);
    }
    $base->remove(user_admin);
    $user->remove();
}

sub test_clone_remote($vm, $node) {
    my $base = create_domain($vm);
Francesc Guasch's avatar
Francesc Guasch committed
430
    is($base->list_instances,1);
431
    $base->prepare_base(user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
432
433
434

    my $bases_vm = $base->_bases_vm();
    is($bases_vm->{$vm->id},1) or exit;
435
    $base->set_base_vm(user => user_admin, node => $node);
Francesc Guasch's avatar
Francesc Guasch committed
436
    is($base->list_instances,2);
Francesc Guasch's avatar
Francesc Guasch committed
437
438
439
440

    $bases_vm = $base->_bases_vm();
    is($bases_vm->{$node->id},1) or exit;

441
442
443
444
445
446
447
    $base->migrate($node);

    my $clone = $base->clone(
        name => new_domain_name
        ,user => user_admin
    );
    ok($clone->_vm->name, $node->name);
Francesc Guasch's avatar
Francesc Guasch committed
448

Francesc Guasch's avatar
Francesc Guasch committed
449
450
    is($clone->list_instances,1);

Francesc Guasch's avatar
Francesc Guasch committed
451
452
453
    _test_old_base($base, $vm);
    _test_clones($base, $vm);
    $clone->remove(user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
454
    is($clone->list_instances,undef);
Francesc Guasch's avatar
Francesc Guasch committed
455
    $base->remove(user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
456
    is($base->list_instances,undef);
457
}
Francesc Guasch's avatar
Francesc Guasch committed
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476

sub _test_old_base($base, $vm) {
    my $sth = connector->dbh->prepare(
        "DELETE FROM bases_vm "
        ." WHERE id_domain=? AND id_vm=?"
    );
    $sth->execute($base->id, $vm->id);

    my $base_f = Ravada::Front::Domain->open($base->id);

    my $info = $base_f->info(user_admin);
    is($info->{bases}->{$vm->id},1) ;

    is(scalar keys %{$info->{bases}}, 2);
}

sub _test_clones($base, $vm) {
    my $info = $base->info(user_admin);
    ok($info->{clones}) or return;
Francesc Guasch's avatar
Francesc Guasch committed
477
    ok($info->{clones}->{$vm->id}) or confess;
Francesc Guasch's avatar
Francesc Guasch committed
478
479
480
    is(scalar @{$info->{clones}->{$vm->id}},1 );
}

Francesc Guasch's avatar
Francesc Guasch committed
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
sub test_remove_base($vm, $node, $volatile) {
    my $base = create_domain($vm);
    $base->volatile_clones($volatile);
    my @volumes0 = $base->list_volumes( device => 'disk');
    ok(!grep(/iso$/,@volumes0),"Expecting no iso files on device list ".Dumper(\@volumes0))
        or exit;
    $base->prepare_base(user_admin);

    my @volumes = $base->list_files_base();
    $base->set_base_vm(node => $node, user => user_admin);
    for my $file ( @volumes ) {
        my ($out, $err) = $node->run_command("ls $file");
        ok($out, "Expecting file '$file' in ".$node->name) or exit;
    }

    $base->remove_base_vm(node => $node, user => user_admin);
    for my $file ( @volumes ) {
        my ($out, $err) = $node->run_command("ls $file");
        ok(!$out, "Expecting no file '$file' in ".$node->name) or exit;
    }
    isnt($base->_data('id_vm'), $node->id);

    for my $file ( @volumes0 ) {
        my ($out, $err) = $node->run_command("ls $file");
        ok($out, "Expecting file '$file' in ".$node->name) or exit;
    }
    $base->set_base_vm(node => $node, user => user_admin);
    $base->remove_base(user_admin);

    for my $file ( @volumes ) {
        ok(!-e $file, "Expecting no file '$file' in local") or exit;
        my ($out, $err) = $node->run_command("ls $file");
        ok(!$out, "Expecting no file '$file' in ".$node->name) or exit;
    }

    $base->remove(user_admin);

}

520
521
522
523
524
##################################################################################
clean();

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

Francesc Guasch's avatar
Francesc Guasch committed
525
for my $vm_name ( 'Void', 'KVM') {
526
527
528
529
530
531
532
533
534
535
536
537
538
539
    my $vm;
    eval { $vm = rvd_back->search_vm($vm_name) };

    SKIP: {

        my $msg = "SKIPPED: $vm_name virtual manager not found ".($@ or '');
        my $REMOTE_CONFIG = remote_config($vm_name);
        if (!keys %$REMOTE_CONFIG) {
            my $msg = "skipped, missing the remote configuration for $vm_name in the file "
                .$Test::Ravada::FILE_CONFIG_REMOTE;
            diag($msg);
            skip($msg,10);
        }

540
        if ($vm && $>) {
541
542
543
544
545
546
547
548
549
            $msg = "SKIPPED: Test must run as root";
            $vm = undef;
        }

        diag($msg)      if !$vm;
        skip($msg,10)   if !$vm;

        diag("Testing remote node in $vm_name");
        my $node = remote_node($vm_name)  or next;
550
        clean_remote_node($node);
551
552
553
554
555
556

        ok($node->vm,"[$vm_name] expecting a VM inside the node") or do {
            remove_node($node);
            next;
        };
        is($node->is_local,0,"Expecting ".$node->name." ".$node->ip." is remote" ) or BAIL_OUT();
Francesc Guasch's avatar
Francesc Guasch committed
557

Francesc Guasch's avatar
Francesc Guasch committed
558
559
        test_set_vm($vm, $node);

Francesc Guasch's avatar
Francesc Guasch committed
560
561
        test_volatile($vm, $node);

Francesc Guasch's avatar
Francesc Guasch committed
562
563
        test_remove_req($vm, $node);

Francesc Guasch's avatar
Francesc Guasch committed
564
565
566
567
        for my $volatile (1,0) {
        test_remove_base($vm, $node, $volatile);
        }

568
569
570
        test_clone_remote($vm, $node);
        test_volatile_req($vm, $node);
        test_volatile_tmp_owner($vm, $node);
571

572
573
574
        test_iptables_close($vm, $node);

        test_reuse_vm($node);
575
576
        test_iptables($vm, $node);
        test_iptables($node, $vm);
577

578
        NEXT:
579
580
581
582
583
584
585
586
587
588
        clean_remote_node($node);
        remove_node($node);
    }

}

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