n10_nodes.t 34 KB
Newer Older
1
2
3
4
5
use warnings;
use strict;

use Carp qw(confess);
use Data::Dumper;
6
use Digest::MD5;
7
8
9
10
11
12
use Test::More;
use Test::SQL::Data;

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

Francesc Guasch's avatar
Francesc Guasch committed
13
14
15
no warnings "experimental::signatures";
use feature qw(signatures);

16
17
18
19
20
21
22
23
24
25
26
my $test = Test::SQL::Data->new(config => 't/etc/sql.conf');

use_ok('Ravada');
my %ARG_CREATE_DOM = (
      KVM => [ id_iso => 1 ]
);

my @VMS = reverse keys %ARG_CREATE_DOM;
init($test->connector);
my $USER = create_user("foo","bar");

Francesc Guasch's avatar
Francesc Guasch committed
27
28
my $REMOTE_CONFIG;
##########################################################
29

Francesc Guasch's avatar
Francesc Guasch committed
30
31
32
33
34
35
sub test_node_renamed {
    my $vm_name = shift;
    my $node = shift;

    my $name = $node->name;

36
    my $name2 = "knope_".new_domain_name();
Francesc Guasch's avatar
Francesc Guasch committed
37
38
39
40
41
42
43
44

    my $sth= $test->connector->dbh->prepare(
        "UPDATE vms SET name=? WHERE name=?"
    );
    $sth->execute($name2, $name);
    $sth->finish;

    my $node2 = Ravada::VM->open($node->id);
45
46
47
    ok($node2,"Expecting a node id=".$node->id) or return;
    is($node2->name, $name2)                    or return;
    is($node2->id, $node->id)                   or return;
Francesc Guasch's avatar
Francesc Guasch committed
48
49
50
51
52

    my $rvd_back2 = Ravada->new(
        connector => $test->connector
        ,config => "t/etc/ravada.conf"
    );
53
    is(scalar(@{rvd_back->vm}), scalar(@{$rvd_back2->vm}),Dumper(rvd_back->vm)) or return;
54
    my @list_nodes2 = rvd_front->list_vms;
55

56
57
    my ($node_f) = grep { $_->{name} eq $name2} @list_nodes2;
    ok($node_f,"[$vm_name] expecting node $name2 in frontend ".Dumper(\@list_nodes2));
58
59
60
61

    $sth->execute($name,$name2);
    $sth->finish;

62
63
64
    @list_nodes2 = rvd_front->list_vms;
    ($node_f) = grep { $_->{name} eq $name} @list_nodes2;
    ok($node_f,"[$vm_name] expecting node $name in frontend ".Dumper(\@list_nodes2));
65

Francesc Guasch's avatar
Francesc Guasch committed
66
67
}

Francesc Guasch's avatar
Francesc Guasch committed
68
69
sub test_node {
    my $vm_name = shift;
70

71
72
73
    die "Error: missing host in remote config\n ".Dumper($REMOTE_CONFIG)
        if !$REMOTE_CONFIG->{host};

Francesc Guasch's avatar
Francesc Guasch committed
74
75
76
    my $vm = rvd_back->search_vm($vm_name);

    my $node;
Francesc Guasch's avatar
Francesc Guasch committed
77
78
    my @list_nodes0 = rvd_front->list_vms;

Francesc Guasch's avatar
Francesc Guasch committed
79
80
    eval { $node = $vm->new(%{$REMOTE_CONFIG}) };
    ok(!$@,"Expecting no error connecting to $vm_name at ".Dumper($REMOTE_CONFIG).", got :'"
81
        .($@ or '')."'") or return;
Francesc Guasch's avatar
Francesc Guasch committed
82
83
    ok($node) or return;

84
    is($node->type,$vm->type) or return;
85

Francesc Guasch's avatar
Francesc Guasch committed
86
    is($node->host,$REMOTE_CONFIG->{host});
87
88
89
90
    is($node->name,$REMOTE_CONFIG->{name}) or return;

    eval { $node->ping };
    is($@,'',"[$vm_name] ping ".$node->name);
Francesc Guasch's avatar
Francesc Guasch committed
91

92
93
94
95
96
97
98
99
100
101
102
103
    if ( $node->ping && !$node->_connect_ssh() ) {
        my $ssh;
        for ( 1 .. 10 ) {
            $ssh = $node->_connect_ssh();
            last if $ssh;
            sleep 1;
            diag("I can ping node ".$node->name." but I can't connect to ssh");
        }
        if (! $ssh ) {
            shutdown_node($node);
        }
    }
104
    start_node($node);
Francesc Guasch's avatar
Francesc Guasch committed
105

106
107
    clean_remote_node($node);

108
    { $node->vm };
109
    is($@,'')   or return;
Francesc Guasch's avatar
Francesc Guasch committed
110

111
112
    ok($node->id) or return;
    is($node->is_active,1) or return;
113

Francesc Guasch's avatar
Francesc Guasch committed
114
115
    ok(!$node->is_local,"[$vm_name] node remote");

116
117
118
    my $node2 = Ravada::VM->open($node->id);
    is($node2->id, $node->id);
    is($node2->name, $node->name);
Francesc Guasch's avatar
Francesc Guasch committed
119
    is($node2->public_ip, $node->public_ip);
120
    ok(!$node2->is_local,"[$vm_name] node remote") or return;
121

Francesc Guasch's avatar
Francesc Guasch committed
122
123
    my @list_nodes = $vm->list_nodes();
    is(scalar @list_nodes, 2,"[$vm_name] Expecting nodes") or return;
124

125
126
127
128
    my ($node_remote) = grep { $_->name eq $REMOTE_CONFIG->{name}} @list_nodes;
    ok($node_remote, "[$vm_name] Expecting node $REMOTE_CONFIG->{name} in back->list_nodes")
        or return;
    ok($node_remote->{type} eq $vm_name);
129
    my @list_nodes2 = rvd_front->list_vms;
130
131
132
133
    ($node_remote) = grep { $_->{name} eq $REMOTE_CONFIG->{name}} @list_nodes2;
    ok($node_remote, "[$vm_name] Expecting node $REMOTE_CONFIG->{name} in front->list_vms")
        or return;
    ok($node_remote->{type} eq $vm_name);
Francesc Guasch's avatar
Francesc Guasch committed
134
135
136
137
    return $node;
}

sub test_sync {
Francesc Guasch's avatar
Francesc Guasch committed
138
    my ($vm_name, $node, $base, $clone) = @_;
Francesc Guasch's avatar
Francesc Guasch committed
139

Francesc Guasch's avatar
Francesc Guasch committed
140
    eval { $clone->rsync($node) };
Francesc Guasch's avatar
Francesc Guasch committed
141
    is(''.$@,'') or return;
Francesc Guasch's avatar
Francesc Guasch committed
142
143
    # TODO test synced files

Francesc Guasch's avatar
Francesc Guasch committed
144
145
146
147
148
    eval { $base->rsync($node) };
    is($@,'') or return;

    eval { $clone->rsync($node) };
    is($@,'') or return;
Francesc Guasch's avatar
Francesc Guasch committed
149
150
}

151
152
153
154
155
156
sub test_domain_ip($vm_name, $node) {
    my $ip = '1.2.4.5';
    my $domain_ip = test_domain($vm_name, $node, $ip);

    my ($local_ip,$local_port)
        = $domain_ip->display(user_admin) =~ m{(\d+.\d+\.\d+\.\d+):(\d+)};
Francesc Guasch's avatar
Francesc Guasch committed
157
158

    is($domain_ip->remote_ip, $ip);
159
160
161
162
163
164
165
166
167
168
169
    $domain_ip->remove(user_admin);
    my @line = search_iptable_remote(
        node => $node
        , remote_ip => $ip
        , local_ip => $local_ip
        , local_port => $local_port
    );
    ok(scalar @line == 0,$node->type." There should be no iptables found $ip -> $local_ip:$local_port ".Dumper(\@line));

}

Francesc Guasch's avatar
Francesc Guasch committed
170
171
sub test_domain {
    my $vm_name = shift;
Francesc Guasch's avatar
Francesc Guasch committed
172
    my $node = shift or die "Missing node";
173
    my $remote_ip = shift;
Francesc Guasch's avatar
Francesc Guasch committed
174
175
176
177
178
179
180

    my $vm = rvd_back->search_vm($vm_name);

    my $base = create_domain($vm_name);
    is($base->_vm->host, 'localhost');

    $base->prepare_base(user_admin);
181
    $base->migrate_base(node => $node, user => user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
182
183
184
185
    my $clone = $base->clone(name => new_domain_name
        ,user => user_admin
    );

Francesc Guasch's avatar
Francesc Guasch committed
186
    test_sync($vm_name, $node, $base, $clone);
Francesc Guasch's avatar
Francesc Guasch committed
187

188
    eval { $clone->migrate($node) };
189
    is(''.$@ , '') or return;
Francesc Guasch's avatar
Francesc Guasch committed
190

191
192
193
194
195
    my @start_arg = ( user => user_admin );
    push @start_arg , ( remote_ip => $remote_ip )   if $remote_ip;

    eval { $clone->start(@start_arg) };

196
    ok(!$@,$node->name." Expecting no error, got ".($@ or ''));
197
    is($clone->is_active,1) or return;
Francesc Guasch's avatar
Francesc Guasch committed
198

199
    my $local_ip = $node->ip;
200

201
202
203
204
205
206
207
208
    $local_ip = $REMOTE_CONFIG->{public_ip}
        if $REMOTE_CONFIG->{public_ip};
    like($clone->display(user_admin),qr($local_ip));

    diag("SKIPPED: Add public_ip to remote_vm.conf to test nodes with 2 IPs")
        if !exists $REMOTE_CONFIG->{public_ip};
    my ($local_port) = $clone->display(user_admin) =~ m{:(\d+)};
    test_iptables($node, $remote_ip, $local_ip, $local_port)  if $remote_ip;
Francesc Guasch's avatar
Francesc Guasch committed
209
210
    return $clone;
}
211

212
213
214
215
216
217
218
219
220
221
sub test_iptables($node, $remote_ip, $local_ip, $local_port) {
    my @line = search_iptable_remote(
        node => $node
        , remote_ip => $remote_ip
        , local_ip => $local_ip
        , local_port => $local_port
    );
    ok(scalar @line,$node->type." No iptables found $remote_ip -> $local_ip:$local_port");
    ok(scalar @line == 1,$node->type." iptables should found only 1 found $remote_ip -> $local_ip:$local_port ".Dumper(\@line));
}
222
223
224
225
226
227
228
229
230
231
232
233

sub test_domain_no_remote {
    my ($vm_name, $node) = @_;

    my $domain;
    eval {
        $domain = $node->create_domain(
            name => new_domain_name
            ,id_owner => user_admin->id
            ,id_iso => 1
        );
    };
Francesc Guasch's avatar
Francesc Guasch committed
234
    like($@,qr'.',"Expecting no domain in remote node by now");
235
236
237
238

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

239
240
sub test_remove_domain_from_local {
    my ($vm_name, $node, $domain_orig) = @_;
241
    $domain_orig->shutdown_now(user_admin)   if $domain_orig->is_active;
242
243
244
245
246
247
248

    my $vm = rvd_back->search_vm($vm_name);
    my $domain = $vm->search_domain($domain_orig->name);

    my @volumes = $domain->list_volumes();

    eval {$domain->remove(user_admin); };
Francesc Guasch's avatar
Francesc Guasch committed
249
    is(''.$@,'',"Expecting no errors removing domain ".$domain_orig->name);
250
251
252
253
254

    my $domain2 = $vm->search_domain($domain->name);
    ok(!$domain2,"Expecting no domain in local");

    my $domain3 = $node->search_domain($domain->name);
255
    ok(!$domain3,"Expecting no domain ".$domain->name." in node ".$node->name) or return;
256
257
258
259
260
261
262

    test_remove_domain_node($node, $domain, \@volumes);

    test_remove_domain_node($vm, $domain, \@volumes);
}


263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
sub test_remove_domain {
    my ($vm_name, $node, $domain) = @_;

    my @volumes = $domain->list_volumes();

    eval {$domain->remove(user_admin); };
    is($@,'');

    test_remove_domain_node($node, $domain, \@volumes);

    my $vm = rvd_back->search_vm($vm_name);
    isnt($vm->name, $node->name) or return;

    test_remove_domain_node($vm, $domain, \@volumes);
}

sub test_remove_domain_node {
    my ($node, $domain, $volumes) = @_;

Francesc Guasch's avatar
Francesc Guasch committed
282
283
284
285
    if ($node->type ne 'KVM') {
        diag("SKIPPING: test_remove_domain_node skipped on ".$node->type);
        return;
    }
286
287
288
289
290
291
292
293
294
295
    my %found = map { $_ => 0 } @$volumes;

    $node->_refresh_storage_pools();
    for my $pool ($node->vm->list_all_storage_pools()) {
        for my $vol ($pool->list_all_volumes()) {
            my $path = $vol->get_path();
            $found{$path}++ if exists $found{$path};
        }
    }
    for my $path (keys %found) {
296
        ok(!$found{$path},$node->name." Expecting vol $path removed")
297
            or return;
298
299
300
    }

}
301
302

sub test_domain_starts_in_same_vm {
303
304
305
306
307
308
    my ($vm_name, $node) = @_;

    my $domain = test_domain($vm_name, $node);

    my $display = $domain->display(user_admin);
    $domain->shutdown_now(user_admin)   if $domain->is_active;
309
310
311
312
313
314
315

    unlike($domain->_vm->host, qr/localhost/)   or return;
    is($domain->_vm->host, $node->host)         or return;

    my $domain2 = rvd_back->search_domain($domain->name);
    ok($domain2,"Expecting a domain called ".$domain->name) or return;

316
    $domain2->start(user => user_admin);
317
    is($domain2->_vm->host, $node->host);
318
319
320
    is($domain2->display(user_admin), $display);

    $domain->remove(user_admin);
321
}
322

323
324
325
326
327
328
329
330
331
332
333
334
335
sub test_sync_base {
    my ($vm_name, $node) = @_;

    my $vm =rvd_back->search_vm($vm_name);
    my $base = create_domain($vm_name);
    my $clone = $base->clone(
        name => new_domain_name
       ,user => user_admin
    );

    eval { $clone->migrate($node); };
    like($@, qr'.');

336
    eval { $base->migrate_base(user => user_admin, vm => $node); };
337
338
    is(''.$@,'');

Francesc Guasch's avatar
Francesc Guasch committed
339
    is($base->base_in_vm($node->id),1,"Expecting domain ".$base->id
340
        ." base in node ".$node->id ) or return;
Francesc Guasch's avatar
Francesc Guasch committed
341

342
343
344
345
346
347
348
349
350
351
352
353
    eval { $clone->migrate($node); };
    is(''.$@,'');

    is($clone->_vm->host, $node->host);
    $clone->shutdown_now(user_admin);

    my $clone2 = $vm->search_domain($clone->name);
    is($clone2->_vm->host, $vm->host);

    eval { $clone2->migrate($node); };
    is(''.$@,'');

354
355
    is($clone2->_data('id_vm'),$node->id);

356
357
    my $clone3 = $node->search_domain($clone2->name);
    ok($clone3,"[$vm_name] expecting ".$clone2->name." found in "
358
                .$node->host) or exit;
359

360
361
362
363
    my $domains = rvd_front->list_domains();
    my ($clone_f) = grep { $_->{name} eq $clone2->name } @$domains;
    ok($clone_f);
    is($clone_f->{id}, $clone2->id);
364
    is($clone_f->{node}, $clone2->_vm->name);
365
    is($clone_f->{id_vm}, $node->id);
366

367
368
369
370
371
372
373
374
    $clone->remove(user_admin);
    $base->remove(user_admin);

}

sub test_start_twice {
    my ($vm_name, $node) = @_;

375
    if ($vm_name ne 'KVM' && $vm_name ne 'Void') {
Francesc Guasch's avatar
Francesc Guasch committed
376
377
378
379
        diag("SKIPPED: start_twice not available on $vm_name");
        return;
    }

380
381
382
383
384
385
    my $vm =rvd_back->search_vm($vm_name);
    my $base = create_domain($vm_name);
    my $clone = $base->clone(
        name => new_domain_name
       ,user => user_admin
    );
386
387
    $clone->shutdown_now(user_admin)    if $clone->is_active;
    is($clone->is_active,0);
388

389
390
    eval { $base->set_base_vm(vm => $node, user => user_admin); };
    is(''.$@,'') or return;
391

392
393
    my $display0 = $clone->display(user_admin);

394
    eval { $clone->migrate($node); };
395
    is(''.$@,'')    or return;
396

397
398
399
    is($clone->_vm->host, $node->host) or exit;
    isnt($clone->display(user_admin), $display0, $clone->name) or exit;

400
    is($clone->is_active,0);
401

402
    # clone should be inactive in local node
403
404
    my $clone2 = $vm->search_domain($clone->name);
    is($clone2->_vm->host, $vm->host);
405
    is($clone2->is_active,0);
406

407
    start_domain_internal($clone2);
408
409

    eval { $clone->start(user => user_admin ) };
410
411
    like(''.$@,qr'libvirt error code: 55,',$clone->name) or exit
        if $vm_name eq 'KVM';
412
413
    is($clone->_vm->host, $vm->host,"[$vm_name] Expecting ".$clone->name." in ".$vm->ip)
        or return;
414
415
416
417
418
419
420
    is($clone->display(user_admin), $clone2->display(user_admin));

    $clone->remove(user_admin);
    $base->remove(user_admin);

}

421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
sub test_already_started_twice($vm_name, $node) {
    my ($base, $clone) = _create_clone($node);
    my $vm = rvd_back->search_vm($vm_name);

    diag("test start twice both already started");

    is($vm->is_local, 1);

    my $clone_local = $vm->search_domain($clone->name);
    is($clone_local->_vm->is_local, 1);

    start_domain_internal($clone);
    start_domain_internal($clone_local);

    is($clone->is_active, 1,"expecting clone active on remote");
    is($clone_local->is_active, 1, "expecting clone active on local");

    my $clone2 = rvd_back->search_domain($clone->name);
    eval { $clone2->start(user => user_admin) };
    like($@,qr/already running/)    if $@;

442
443
444
445
    for ( 1 .. 3 ) {
        rvd_back->_process_all_requests_dont_fork();
        for ( 1 .. 10 ) {
            last if !$clone->is_active
446
            && !$clone_local->is_active;
447
448
            sleep 1;
        }
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
    }
    rvd_back->_process_all_requests_dont_fork();

    is($clone->is_active, 0,"[".$node->type."] expecting remote clone ".$clone->name." down");
    is($clone_local->is_active, 0,"[".$node->type."] expecting local clone down");

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

sub test_already_started_hibernated($vm_name, $node) {
    my ($base, $clone) = _create_clone($node);
    my $vm = rvd_back->search_vm($vm_name);

    is($vm->is_local, 1);

    my $clone_local = $vm->search_domain($clone->name);
    is($clone_local->_vm->is_local, 1);

    start_domain_internal($clone);
    hibernate_domain_internal($clone_local);

    is($clone->is_active, 1,"expecting clone active on remote");
    is($clone_local->is_hibernated, 1, "expecting clone hibernated on local");

    my $clone2 = rvd_back->search_domain($clone->name);
    eval { $clone2->start(user => user_admin) };
    like($@,qr/already running/)    if $@;

    rvd_back->_process_all_requests_dont_fork();
    for ( 1 .. 10 ) {
        last if $clone->is_active
                && !$clone_local->is_active
                && !$clone_local->is_hibernated;
483
484
        sleep 1;
    }
485
    rvd_back->_process_all_requests_dont_fork();
486
487
488
489
490
491
492
493

    is($clone->is_active, 0);
    is($clone_local->is_active, 0);

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

494
495
496
497
498
499
500
501
502
503
504
505
506
# Test it shuts down and syncs back
sub test_shutdown_internal( $node ) {
    my ($base, $clone) = _create_clone($node);
    my $vm = rvd_back->search_vm($node->type);

    is($vm->is_local, 1);

    my $clone_local = $vm->search_domain($clone->name);
    is($clone_local->_vm->is_local, 1);
    is($clone_local->is_active, 0);

    start_domain_internal($clone);
    is($clone->is_active,1);
507
    is($clone->status,'active');
508
509

    shutdown_domain_internal($clone);
510
    is($clone->status,'active');
511
512
513
514
    _write_in_volumes($clone);

    Ravada::Request->refresh_vms();

515
516
    rvd_back->_process_all_requests_dont_fork();
    is($clone->is_active,0);
517
518
519
520
521
522
523
524
525

    for my $file ($clone_local->list_volumes) {
        my $md5 = _md5($file, $vm);
        my $md5_remote = _md5($file, $node);
        is( $md5_remote, $md5, "[".$node->type."] ".$clone->name." $file" )
                or exit;
    }

}
526

527
528
529
sub _create_clone($node) {

    my $vm =rvd_back->search_vm($node->type);
530
    is($vm->is_local,1);
531
532
    is($node->is_local,0);

533
    my $base = create_domain($vm->type);
534
535
536
537
538
539
540
    $base->shutdown_now(user_admin) if $base->is_active;

    my $clone_name = new_domain_name();

    my $clone_remote = $node->search_domain($clone_name);
    ok(!$clone_remote,"[".$node->type."] expectin cleaned domain in node ".$node->name)
        or BAIL_OUT();
541
    my $clone = $base->clone(
542
        name => $clone_name
543
544
545
546
547
548
549
       ,user => user_admin
    );
    $clone->shutdown_now(user_admin)    if $clone->is_active;
    is($clone->is_active,0);

    eval { $base->set_base_vm(vm => $node, user => user_admin); };
    is(''.$@,'')    or return;
550
551
552
    for my $volume ( $clone->list_volumes ) {
        ok(-e $volume,"Expecting volume $volume of machine ".$clone->name);
    }
553
554

    eval { $clone->migrate($node); };
555
    is(''.$@,'')    or BAIL_OUT();
556
557
558
559
560
561

    is($clone->_vm->host, $node->host) or exit;

    return($base, $clone);
}

562
563
564
sub test_rsync_newer {
    my ($vm_name, $node) = @_;

565
    if ($vm_name ne 'KVM') {
566
        diag("Skipping: Volumes not implemented for $vm_name");
567
568
        return;
    }
569
    my $domain = test_domain($vm_name, $node) or return;
570
571
572
573
574
575
576
    $domain->shutdown_now(user_admin)   if $domain->is_active;

    my ($volume) = $domain->list_volumes();
    my ($vol_name) = $volume =~ m{.*/(.*)};

    my $vm = rvd_back->search_vm($vm_name);

577
578
    my $capacity;
    { # vols equal, then resize
579
    my $vol = $vm->search_volume($vol_name);
580
    ok($vol,"[$vm_name] expecting volume $vol_name")    or return;
581
    ok($vol->get_info,"[$vm_name] No info for remote vol "
582
        .Dumper($vol)) or return;
583

584
    my $vol_remote = $node->search_volume($vol_name);
585
    ok($vol_remote->get_info,"[$vm_name] No info for remote vol "
586
        .Dumper($vol_remote)) or return;
587
    is($vol_remote->get_info->{capacity}, $vol->get_info->{capacity});
588

589
    $capacity = int ($vol->get_info->{capacity} *1.5 );
590
591
    $vol->resize($capacity);
    }
592

593
    { # vols different
594
595
596
    my $vol2 = $vm->search_volume($vol_name);
    my $vol2_remote = $node->search_volume($vol_name);

597
598
599
600
601
    is($vol2->get_info->{capacity}, $capacity);
    isnt($vol2_remote->get_info->{capacity}, $capacity);
    isnt($vol2_remote->get_info->{capacity}, $vol2->get_info->{capacity});
    }

602
    # on starting it should sync
603
604
    is($domain->_vm->host, $node->host);
    $domain->start(user => user_admin);
605
    is($domain->_vm->host, $node->host);
606
607
608
609
610
611
612

    { # syncs for start, so vols should be equal
    my $vol3 = $vm->search_volume($vol_name);
    my $vol3_remote = $node->search_volume($vol_name);
    is($vol3_remote->get_info->{capacity}, $vol3->get_info->{capacity});
    }

613
    $domain->remove(user_admin);
614
615
}

616
617
618
619
620
621
sub test_bases_node {
    my ($vm_name, $node) = @_;

    my $vm = rvd_back->search_vm($vm_name);

    my $domain = create_domain($vm_name);
622
    my $local_display = $domain->display(user_admin);
623

624
625
626
627
628
629
    eval { $domain->base_in_vm($domain->_vm->id)};
    like($@,qr'is not a base');

#    is($domain->base_in_vm($domain->_vm->id),1);
    eval { $domain->base_in_vm($node->id) };
    like($@,qr'is not a base');
630
631
632
633
634
635

    $domain->prepare_base(user_admin);
    is($domain->base_in_vm($domain->_vm->id), 1);
    is($domain->base_in_vm($node->id), undef);

    $domain->migrate($node);
636
    isnt($domain->display(user_admin), $local_display) or exit;
637
638
    is($domain->base_in_vm($node->id), 1);

639
640
641
642
643
644
    for my $file ( $domain->list_files_base ) {
        my ($name) = $file =~ m{.*/(.*)};
        my $vol_path = $node->search_volume_path($name);
        ok($vol_path,"[$vm_name] Expecting file '$name' in node ".$node->name) or exit;
    }

645
646
647
648
649
    $domain->set_base_vm(vm => $node, value => 0, user => user_admin);
    is($domain->base_in_vm($node->id), 0);

    $domain->set_base_vm(vm => $vm, value => 0, user => user_admin);
    is($domain->is_base(),0);
650
651
652
653
    eval { is($domain->base_in_vm($vm->id), 0) };
    like($@,qr'is not a base');
    eval { is($domain->base_in_vm($node->id), 0) };
    like($@,qr'is not a base');
654

655
    user_admin->mark_all_messages_read();
656
657
658
659
660
    my $req = Ravada::Request->set_base_vm(
                uid => user_admin->id
             ,id_vm => $vm->id
         ,id_domain => $domain->id
    );
661
662
    rvd_back->_process_all_requests_dont_fork();
    is($req->status,'done') or die Dumper($req);
663
664
665
    is($req->error,'');
    is($domain->base_in_vm($vm->id), 1);

666
667
    is(scalar user_admin->unread_messages , 2, Dumper(user_admin->unread_messages));

668
669
670
671
672
    $req = Ravada::Request->remove_base_vm(
                uid => user_admin->id
             ,id_vm => $vm->id
         ,id_domain => $domain->id
    );
673
    rvd_back->_process_all_requests_dont_fork();
674
675
    eval { $domain->base_in_vm($vm->id)};
    like($@,qr'is not a base');
676
677
678
679

    $domain->remove(user_admin);
}

680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
sub _disable_storage_pools {
    my $node = shift;
    for my $sp ($node->vm->list_all_storage_pools()) {
        $sp->destroy();
    }
}

sub _enable_storage_pools {
    my $node = shift;
    for my $sp ($node->vm->list_all_storage_pools()) {
        $sp->create();
    }
}

sub test_bases_different_storage_pools {
    my ($vm_name, $node) = @_;
    return if $vm_name ne 'KVM';

    _disable_storage_pools($node);
    is(scalar($node->list_storage_pools),0);

    my $vm = rvd_back->search_vm($vm_name);

    my $domain = create_domain($vm_name);

    $domain->prepare_base(user_admin);
    is($domain->base_in_vm($domain->_vm->id), 1);
    is($domain->base_in_vm($node->id), undef);

    eval {$domain->migrate($node) };
    like($@, qr'storage pools');
    is($domain->base_in_vm($node->id), undef);

    _enable_storage_pools($node);

    $domain->remove(user_admin);
}

718
719
720
721
722
723
724
725
726
727
728
729
sub test_clone_not_in_node {
    my ($vm_name, $node) = @_;

    my $vm = rvd_back->search_vm($vm_name);

    my $domain = create_domain($vm_name);

    $domain->prepare_base(user_admin);
    $domain->set_base_vm(vm => $node, user => user_admin);

    is($domain->base_in_vm($node->id), 1);

730
731
732
733
734
735
    my @clones;
    for ( 1 .. 4 ) {
        my $clone1 = $domain->clone(name => new_domain_name, user => user_admin);
        push @clones,($clone1);
        is($clone1->_vm->host, 'localhost');
        eval { $clone1->start(user_admin) };
736
        is(''.$@,'',"[$vm_name] Clone of ".$domain->name." failed ".$clone1->name) or return;
737
        is($clone1->is_active,1);
738
739

    # search the domain in the underlying VM
Francesc Guasch's avatar
Francesc Guasch committed
740
741
742
743
744
745
746
747
        if ($vm_name eq 'KVM') {
            my $virt_domain;
            eval { $virt_domain = $clone1->_vm->vm
                                ->get_domain_by_name($clone1->name) };
            is(''.$@,'');
            ok($virt_domain,"Expecting ".$clone1->name." in "
                .$clone1->_vm->host);
        }
748
        last if $clone1->_vm->host ne $clones[0]->_vm->host;
749
750
751
    }


752
753
    isnt($clones[-1]->_vm->host, $clones[0]->_vm->host,"[$vm_name] "
        .$clones[-1]->name
754
        ." - ".$clones[0]->name) or return;
755
756
    for (@clones) {
        $_->remove(user_admin);
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
    }
    $domain->remove(user_admin);
}

sub test_domain_already_started {
    my ($vm_name, $node) = @_;

    my $vm = rvd_back->search_vm($vm_name);

    my $domain = create_domain($vm_name);

    $domain->prepare_base(user_admin);
    $domain->set_base_vm(vm => $node, user => user_admin);

    is($domain->base_in_vm($node->id), 1);

    my $clone = $domain->clone(name => new_domain_name, user => user_admin);
    is($clone->_vm->host, 'localhost');

Francesc Guasch's avatar
Francesc Guasch committed
776
    eval { $clone->migrate($node) };
777
    is(''.$@,'')                        or return;
778
    is($clone->_vm->host, $node->host);
779
    is($clone->_vm->id, $node->id) or return;
Francesc Guasch's avatar
Francesc Guasch committed
780

781
    is($clone->_data('id_vm'), $node->id) or return;
Francesc Guasch's avatar
Francesc Guasch committed
782
783
784
785
786

    {
        my $clone_copy = $node->search_domain($clone->name);
        ok($clone_copy,"[$vm_name] expecting domain ".$clone->name
                        ." in node ".$node->host
787
        ) or exit;
Francesc Guasch's avatar
Francesc Guasch committed
788
    }
789
790

    eval { $clone->start(user_admin) };
791
    is(''.$@,'',$clone->name) or return;
792
    is($clone->is_active,1);
793
794
    is($clone->_vm->id, $node->id)  or return;
    is($clone->_vm->host, $node->host)  or return;
795
796
797
798
799
800
801
802
803
804
805

    {
    my $clone2 = rvd_back->search_domain($clone->name);
    is($clone2->id, $clone->id);
    is($clone2->_vm->host , $clone->_vm->host);
    }

    my $sth = $test->connector->dbh->prepare("UPDATE domains set id_vm=NULL WHERE id=?");
    $sth->execute($clone->id);
    $sth->finish;

806
    { # clone is active, it should be found in node
807
808
    my $clone3 = rvd_back->search_domain($clone->name);
    is($clone3->id, $clone->id);
809
810
    is($clone3->_vm->host , $node->host,"Expecting ".$clone3->name
        ." in ".$node->host) or exit;
811
    }
812
813
814

    $clone->remove(user_admin);
    $domain->remove(user_admin);
815
}
816
817
818
819
820
821

sub test_prepare_sets_vm {
    my $vm_name = shift;
    my $vm = rvd_back->search_vm($vm_name);

    my $domain = create_domain($vm_name);
822
    eval { $domain->base_in_vm($vm->id) };
823
    like($@,qr'is not a base');
824
825
826
827
828

    $domain->prepare_base(user_admin);
    is($domain->base_in_vm($vm->id),1);

    $domain->remove_base(user_admin);
829
830
    eval { $domain->base_in_vm($vm->id) };
    like($@,qr'is not a base');
831
832
833

    $domain->remove(user_admin);
}
Francesc Guasch's avatar
Francesc Guasch committed
834

835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
sub test_remove_base($node) {

    my $vm = rvd_back->search_vm($node->type, 'localhost');
    my $domain = create_domain($vm);
    $domain->prepare_base(user_admin);

    $domain->set_base_vm(vm => $node, user => user_admin);

    is($domain->base_in_vm($vm->id),1);
    is($domain->base_in_vm($node->id),1);

    $domain->remove_base_vm(vm => $node, user => user_admin);

    is($domain->base_in_vm($vm->id),1);
    is($domain->base_in_vm($node->id),0);

    $domain->set_base_vm(vm => $node, user => user_admin);
    is($domain->base_in_vm($node->id),1);

    $domain->remove_base_vm(user => user_admin, vm => $vm);

    is($domain->is_base,0);

    $domain->prepare_base(user_admin);
    is($domain->base_in_vm($vm->id),1);
    is($domain->base_in_vm($node->id),0);

    $domain->remove(user_admin);
}

865
866
867
sub test_node_inactive($vm_name, $node) {

    start_node($node);
Francesc Guasch's avatar
Francesc Guasch committed
868

869
    hibernate_node($node);
870
871
872
873
    is($node->ping, 0);
    is($node->_do_is_active,0);
    is($node->_data('is_active'), 0);
    is($node->is_active,0) or exit;
Francesc Guasch's avatar
Francesc Guasch committed
874

875
876
    my @list_nodes = rvd_front->list_vms;
    my ($node2) = grep { $_->{name} eq $node->name} @list_nodes;
Francesc Guasch's avatar
Francesc Guasch committed
877

878
879
880
    ok($node2,"[$vm_name] Expecting node called ".$node->name." in frontend")
        or return;
    is($node2->{is_active},0,Dumper($node2)) or return;
Francesc Guasch's avatar
Francesc Guasch committed
881

882
    start_node($node);
Francesc Guasch's avatar
Francesc Guasch committed
883
884

    for ( 1 .. 10 ) {
885
        last if $node->_do_is_active;
Francesc Guasch's avatar
Francesc Guasch committed
886
887
888
889
890
891
892
        sleep 1;
        diag("[$vm_name] waiting for node ".$node->name);
    }
    is($node->is_active,1,"[$vm_name] node ".$node->name." active");

}

893
sub test_sync_back($node) {
894
    diag("Testing sync back on remote non shared storage node");
895
896
897
898
899
900
901
902
903
904
905
    my $vm = rvd_back->search_vm($node->type, 'localhost');
    my $domain = create_domain($vm);
    $domain->prepare_base(user_admin);

    $domain->set_base_vm(vm => $node, user => user_admin);

    is($domain->base_in_vm($vm->id),1);
    is($domain->base_in_vm($node->id),1);

    my $clone = $domain->clone( name => new_domain_name(), user => user_admin );
    $clone->migrate($node);
906
907
    eval { $clone->start(user_admin) };
    is(''.$@,'',"[".$node->type."] expecting no error starting ".$clone->name) or exit;
908
909
    is($clone->_vm->host, $node->host);

910
    _write_in_volumes($clone);
911
912
913
914
915
916
917
    for my $file ($clone->list_volumes) {
        my $md5 = _md5($file, $vm);
        my $md5_remote = _md5($file, $node);
        isnt( $md5_remote, $md5, "[".$node->type."] ".$clone->name." $file" ) or exit;
    }

    _shutdown_nicely($clone);
918
    is ( $clone->is_active, 0 );
919
920
921
    for my $file ($clone->list_volumes) {
        my $md5 = _md5($file, $vm);
        my $md5_remote = _md5($file, $node);
922
923
        is( $md5_remote, $md5, "[".$node->type."] ".$clone->name." $file" )
                or exit;
924
    }
925
926
    $clone->remove(user_admin);
    $domain->remove(user_admin);
927
928
}

929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
sub test_migrate_back($node) {
    diag("Testing migrate back from remote non shared storage node");
    my $vm = rvd_back->search_vm($node->type, 'localhost');
    my $domain = create_domain($vm);
    $domain->prepare_base(user_admin);

    $domain->set_base_vm(vm => $node, user => user_admin);

    is($domain->base_in_vm($vm->id),1);
    is($domain->base_in_vm($node->id),1);

    my $clone = $domain->clone( name => new_domain_name(), user => user_admin );
    $clone->migrate($node);
    eval { $clone->start(user_admin) };
    is(''.$@,'',"[".$node->type."] expecting no error starting ".$clone->name) or exit;
    is($clone->_vm->host, $node->host);

    _write_in_volumes($clone);

    shutdown_domain_internal($clone);

    eval { $clone->migrate($vm) };
    is($@, '');

    for my $file ($clone->list_volumes) {
        my $md5 = _md5($file, $vm);
        my $md5_remote = _md5($file, $node);
        is( $md5_remote, $md5, "[".$node->type."] ".$clone->name." $file" ) or exit;
    }


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

Francesc Guasch's avatar
Francesc Guasch committed
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
sub test_shutdown($node) {

    my $vm = rvd_back->search_vm($node->type, 'localhost');
    my $domain = create_domain($vm);
    $domain->prepare_base(user_admin);

    $domain->set_base_vm(vm => $node, user => user_admin);

    is($domain->base_in_vm($vm->id),1);
    is($domain->base_in_vm($node->id),1);

    my $clone = $domain->clone( name => new_domain_name(), user => user_admin );
    $clone->migrate($node);
    my $remote_ip = '1.2.4.5';
    $clone->start(user => user_admin, remote_ip => $remote_ip);
    my ($local_ip,$local_port)
        = $clone->display(user_admin) =~ m{(\d+.\d+\.\d+\.\d+):(\d+)};

    is($clone->remote_ip, $remote_ip);

    if ($clone->type eq 'KVM') {
        $clone->domain->destroy();
    } elsif ($clone->type eq 'Void') {
        $clone->_store(is_active => 0);
    } else {
        diag("SKIPPED: I can't test shutdown of ".$node->type." nodes");
    }
    is($clone->is_active,0,"[".$clone->type."] Expecting clone ".$clone->name." inactive") or return;
992
    is($clone->_data('status'),'shutdown',"[".$clone->type."] Expecting clone ".$clone->name." data active") or return;
Francesc Guasch's avatar
Francesc Guasch committed
993
994
995
996
997
998
999
1000

    my $clone2 = Ravada::Domain->open($clone->id); #open will clean internal shutdown

    for my $file ($clone->list_volumes) {
        my $md5 = _md5($file, $vm);
        my $md5_remote = _md5($file, $node);
        is($md5_remote, $md5);
    }
For faster browsing, not all history is shown. View entire blame