10_basic.t 18.4 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

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);
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
70
71
72
73
74
75

}

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
    );

    rvd_back->_process_requests_dont_fork(1);

    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;
    }

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
257
258
259
260
261
262
263
264
265
    my $req = Ravada::Request->set_base_vm(
        id_domain => $base->id
        , id_vm => $node->id
        , value => 1
        , uid => user_admin->id
    );
    rvd_back->_process_requests_dont_fork(1);
    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;

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;

278
279
    is($base_f->list_instances,2) or exit;

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

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
sub test_bind_ip($node, $base, $remote_ip=undef) {
    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();
        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);
    }
}

316
317
318
319
sub test_instances($clone, $expected) {
    confess;
    my @instances = $clone->list_instances();
    is(@instances,$expected,Dumper(\@instances)) or exit;
Francesc Guasch's avatar
Francesc Guasch committed
320
321
}

322
323
324
325
326
327
328
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
329
    for ( 1 .. 10 ) {
330
        my $clone = $base->clone(user => user_admin, name => new_domain_name);
Francesc Guasch's avatar
Francesc Guasch committed
331
332
        is($clone->_vm->is_active,1);
        is($clone->is_active(),1,"Expecting clone ".$clone->name." active on ".$clone->_vm->name);
333
334
335
336
337
        push @clones,($clone);
        last if $clone->_vm->id == $node->id;
    }
    is($clones[-1]->_vm->id, $node->id);

338
339
    is($clones[-1]->list_instances,1,Dumper([$clones[-1]->list_instances])) or exit;

340
341
342
343
    for (@clones) {
        $_->remove(user_admin);
    }
    $base->remove(user_admin);
344
    is(scalar($base->list_instances),undef,Dumper([$base->list_instances])) or exit;
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
}

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
367
368
        is($clone->is_active(),1,"[".$vm->type."] expecting clone ".$clone->name
            ." active on node ".$clone->_vm->name);
369
370
371
372
373
374
375
376
377
378
379
380
381
382
        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
383
    $base->is_public(1);
384
385
386
387
388
389
390
391
392
393
    $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
394
            ,id_owner => $user->id
395
396
397
398
399
400
        );
        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
401
402
        is($clone->is_active,1,"[".$node->type."] expecting ".$clone->name." active "
                                    ." in ".$clone->_vm->name) or exit;
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
        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);
418
    is($base->list_instances,1);
419
    $base->prepare_base(user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
420
421
422

    my $bases_vm = $base->_bases_vm();
    is($bases_vm->{$vm->id},1) or exit;
423
    $base->set_base_vm(user => user_admin, node => $node);
424
    is($base->list_instances,2);
Francesc Guasch's avatar
Francesc Guasch committed
425
426
427
428

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

429
430
431
432
433
434
435
    $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
436

437
438
    is($clone->list_instances,1);

Francesc Guasch's avatar
Francesc Guasch committed
439
440
441
    _test_old_base($base, $vm);
    _test_clones($base, $vm);
    $clone->remove(user_admin);
442
    is($clone->list_instances,undef);
Francesc Guasch's avatar
Francesc Guasch committed
443
    $base->remove(user_admin);
444
    is($base->list_instances,undef);
445
}
Francesc Guasch's avatar
Francesc Guasch committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464

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
465
    ok($info->{clones}->{$vm->id}) or confess;
Francesc Guasch's avatar
Francesc Guasch committed
466
467
468
    is(scalar @{$info->{clones}->{$vm->id}},1 );
}

Francesc Guasch's avatar
Francesc Guasch committed
469
470
471
472
473
474
475
476
477
478
479
480
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
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);

}

508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
sub _check_internal_autostart($domain, $expected) {
    if ($domain->type eq 'KVM') {
        ok($domain->domain->get_autostart)  if $expected;
        ok(!$domain->domain->get_autostart) if !$expected;
    } elsif ($domain->type eq 'Void') {
        ok($domain->_value('autostart'))    if $expected;
        ok(!$domain->_value('autostart'),$domain->name) or exit   if !$expected;
    } else {
        diag("WARNING: I don't know how to check ".$domain->type." internal autostart");
    }
}

# check autostart is managed by Ravada when nodes
sub test_autostart($vm, $node) {
    my $base = create_domain($vm);
    $base->prepare_base(user_admin);
    my $domain = $base->clone(name => new_domain_name , user => user_admin);
    $domain->autostart(1,user_admin);
    is($domain->autostart,1);
    _check_internal_autostart($domain,1);

    $base->set_base_vm(node => $node, user => user_admin);
    is($domain->autostart,1) or exit;
    _check_internal_autostart($domain,0);

    $domain->remove(user_admin);
    $base->remove(user_admin);
}

537
538
539
540
541
##################################################################################
clean();

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

Francesc Guasch's avatar
Francesc Guasch committed
542
for my $vm_name ( 'Void', 'KVM') {
543
544
545
546
547
548
549
550
551
552
553
554
555
556
    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);
        }

557
        if ($vm && $>) {
558
559
560
561
562
563
564
565
566
            $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;
567
        clean_remote_node($node);
568
569
570
571
572
573

        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
574

Francesc Guasch's avatar
Francesc Guasch committed
575
576
        test_set_vm($vm, $node);

577
        test_autostart($vm, $node);
Francesc Guasch's avatar
Francesc Guasch committed
578
579
        test_volatile($vm, $node);

Francesc Guasch's avatar
Francesc Guasch committed
580
581
        test_remove_req($vm, $node);

Francesc Guasch's avatar
Francesc Guasch committed
582
583
584
585
        for my $volatile (1,0) {
        test_remove_base($vm, $node, $volatile);
        }

586
587
588
        test_clone_remote($vm, $node);
        test_volatile_req($vm, $node);
        test_volatile_tmp_owner($vm, $node);
589

590
591
592
        test_iptables_close($vm, $node);

        test_reuse_vm($node);
593
594
        test_iptables($vm, $node);
        test_iptables($node, $vm);
595

596
        NEXT:
597
598
599
600
601
602
603
        clean_remote_node($node);
        remove_node($node);
    }

}

END: {
604
    end();
605
606
    done_testing();
}