10_basic.t 15.6 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
23
24
25
26
27
28
29

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

sub test_reuse_vm($node) {
    my $domain = create_domain($node->type);
    $domain->add_volume(name => 'vdb', swap => 1, size => 512 * 1024);
    $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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

    $clone1->migrate($node);
    is($clone1->_data('id_vm'), $node->id);
    $clone2->migrate($node);
    is($clone2->_data('id_vm'), $node->id);

    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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

}

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);
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
}

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

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

96
97
98
99
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
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);
}
159
160
161
162
163
164
165
166
167
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);

168
169
170
171
172
173
    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+)};
174
175
176

    my @found = search_iptable_remote(
       node => $vm
177
178
179
180
181
182
183
        ,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
184
185
186
187
188
        ,remote_ip => $remote_ip2
        ,local_port => $local_port2
    );
    is(scalar @found,1,$vm->name." $remote_ip2:$local_port2".Dumper(\@found)) or exit;

189
    $clone_local->shutdown_now(user_admin);
190
191

    @found = search_iptable_remote(
192
       node => $vm
193
194
195
196
197
198
        ,remote_ip => $remote_ip1
        ,local_port => $local_port1
    );
    is(scalar @found,0,$node->name." $remote_ip1:$local_port1".Dumper(\@found));

    @found = search_iptable_remote(
199
       node => $node
200
201
202
203
204
        ,remote_ip => $remote_ip2
        ,local_port => $local_port2
    );
    is(scalar @found,1,$vm->name." $remote_ip2:$local_port2".Dumper(\@found));

205
206
207
208
    for my $clone0 ( $domain->clones) {
        my $clone = Ravada::Domain->open($clone0->{id});
        $clone->remove(user_admin);
    }
209
210
211
    $domain->remove(user_admin);

}
212

213
214
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);
215
216
    #TODO: add a flag in start to force node to start in
    $clone_local->{_migrated} = 1;
217
218
    my $clone_remote= $base->clone(name => new_domain_name, user => user_admin);
    $clone_remote->migrate($node);
219

220
221
222
    $clone_local->start(user => user_admin, remote_ip => $ip_local);
    $clone_remote->start(user => user_admin, remote_ip => $ip_remote);

223
    for (1 .. 100 ) {
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
        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
242
243
sub test_set_vm($vm, $node) {
    my $base = create_domain($vm);
244
245
246
    my $info = $base->info(user_admin);
    is($info->{bases}->{$vm->id},0);

Francesc Guasch's avatar
Francesc Guasch committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
    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);

262
263
264
265
266
267
268
269
270
271
    $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;

    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
272
273
274
    $base->remove(user_admin);
}

275
276
277
278
279
280
281
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
282
    for ( 1 .. 10 ) {
283
        my $clone = $base->clone(user => user_admin, name => new_domain_name);
Francesc Guasch's avatar
Francesc Guasch committed
284
285
        is($clone->_vm->is_active,1);
        is($clone->is_active(),1,"Expecting clone ".$clone->name." active on ".$clone->_vm->name);
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
316
        push @clones,($clone);
        last if $clone->_vm->id == $node->id;
    }
    is($clones[-1]->_vm->id, $node->id);

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

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
317
318
        is($clone->is_active(),1,"[".$vm->type."] expecting clone ".$clone->name
            ." active on node ".$clone->_vm->name);
319
320
321
322
323
324
325
326
327
328
329
330
331
332
        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
333
    $base->is_public(1);
334
335
336
337
338
339
340
341
342
343
    $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
344
            ,id_owner => $user->id
345
346
347
348
349
350
        );
        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
351
352
        is($clone->is_active,1,"[".$node->type."] expecting ".$clone->name." active "
                                    ." in ".$clone->_vm->name) or exit;
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
        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);
    $base->prepare_base(user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
369
370
371

    my $bases_vm = $base->_bases_vm();
    is($bases_vm->{$vm->id},1) or exit;
372
    $base->set_base_vm(user => user_admin, node => $node);
Francesc Guasch's avatar
Francesc Guasch committed
373
374
375
376

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

377
378
379
380
381
382
383
    $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
384
385
386
387
388

    _test_old_base($base, $vm);
    _test_clones($base, $vm);
    $clone->remove(user_admin);
    $base->remove(user_admin);
389
}
Francesc Guasch's avatar
Francesc Guasch committed
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

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

Francesc Guasch's avatar
Francesc Guasch committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
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);

}

452
453
454
455
456
##################################################################################
clean();

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

Francesc Guasch's avatar
Francesc Guasch committed
457
for my $vm_name ( 'Void', 'KVM') {
458
459
460
461
462
463
464
465
466
467
468
469
470
471
    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);
        }

472
        if ($vm && $>) {
473
474
475
476
477
478
479
480
481
            $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;
482
        clean_remote_node($node);
483
484
485
486
487
488

        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
489

Francesc Guasch's avatar
Francesc Guasch committed
490
491
        test_set_vm($vm, $node);

Francesc Guasch's avatar
Francesc Guasch committed
492
493
        test_volatile($vm, $node);

Francesc Guasch's avatar
Francesc Guasch committed
494
495
        test_remove_req($vm, $node);

Francesc Guasch's avatar
Francesc Guasch committed
496
497
498
499
        for my $volatile (1,0) {
        test_remove_base($vm, $node, $volatile);
        }

500
501
502
        test_clone_remote($vm, $node);
        test_volatile_req($vm, $node);
        test_volatile_tmp_owner($vm, $node);
503

504
505
506
        test_iptables_close($vm, $node);

        test_reuse_vm($node);
507
508
        test_iptables($vm, $node);
        test_iptables($node, $vm);
509

510
        NEXT:
511
512
513
514
515
516
517
518
519
520
        clean_remote_node($node);
        remove_node($node);
    }

}

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