n10_nodes.t 35.2 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
use Test::More;

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

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

15
16
use_ok('Ravada');

17
init();
18
19
my $USER = create_user("foo","bar");

Francesc Guasch's avatar
Francesc Guasch committed
20
##########################################################
21

Francesc Guasch's avatar
Francesc Guasch committed
22
23
24
25
26
27
sub test_node_renamed {
    my $vm_name = shift;
    my $node = shift;

    my $name = $node->name;

28
    my $name2 = "knope_".new_domain_name();
Francesc Guasch's avatar
Francesc Guasch committed
29

30
    my $sth= connector->dbh->prepare(
Francesc Guasch's avatar
Francesc Guasch committed
31
32
33
34
35
36
        "UPDATE vms SET name=? WHERE name=?"
    );
    $sth->execute($name2, $name);
    $sth->finish;

    my $node2 = Ravada::VM->open($node->id);
37
38
39
    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
40
41

    my $rvd_back2 = Ravada->new(
42
        connector => connector
Francesc Guasch's avatar
Francesc Guasch committed
43
44
        ,config => "t/etc/ravada.conf"
    );
45
    $rvd_back2->_install();
46
    is(scalar(@{rvd_back->vm}), scalar(@{$rvd_back2->vm}),Dumper(rvd_back->vm)) or return;
47
    my @list_nodes2 = rvd_front->list_vms;
48

49
50
    my ($node_f) = grep { $_->{name} eq $name2} @list_nodes2;
    ok($node_f,"[$vm_name] expecting node $name2 in frontend ".Dumper(\@list_nodes2));
51
52
53
54

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

55
56
57
    @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));
58

Francesc Guasch's avatar
Francesc Guasch committed
59
60
}

61
sub test_node($vm_name,$node) {
62

Francesc Guasch's avatar
Francesc Guasch committed
63
64
    my $vm = rvd_back->search_vm($vm_name);

Francesc Guasch's avatar
Francesc Guasch committed
65
66
    my @list_nodes0 = rvd_front->list_vms;

67
    is($node->type,$vm->type) or return;
68

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

72
73
74
75
76
77
78
79
80
81
82
83
    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);
        }
    }
84
    start_node($node);
Francesc Guasch's avatar
Francesc Guasch committed
85

86
87
    clean_remote_node($node);

88
    { $node->vm };
89
    is($@,'')   or return;
Francesc Guasch's avatar
Francesc Guasch committed
90

91
92
    ok($node->id) or return;
    is($node->is_active,1) or return;
93

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

96
97
98
    my $node2 = Ravada::VM->open($node->id);
    is($node2->id, $node->id);
    is($node2->name, $node->name);
Francesc Guasch's avatar
Francesc Guasch committed
99
    is($node2->public_ip, $node->public_ip);
100
    ok(!$node2->is_local,"[$vm_name] node remote") or return;
101

Francesc Guasch's avatar
Francesc Guasch committed
102
103
    my @list_nodes = $vm->list_nodes();
    is(scalar @list_nodes, 2,"[$vm_name] Expecting nodes") or return;
104
105
    ok(ref($list_nodes[0])) or exit;
    ok(ref($list_nodes[1])) or exit;
106

107
    my ($node_remote) = grep { !$_->is_local } @list_nodes;
108
    ok($node_remote->{type} eq $vm_name);
109
    my @list_nodes2 = rvd_front->list_vms;
110
111

    ($node_remote) = grep { !$_->{is_local} } @list_nodes2;
112
    ok($node_remote->{type} eq $vm_name);
Francesc Guasch's avatar
Francesc Guasch committed
113
114
115
116
    return $node;
}

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

Francesc Guasch's avatar
Francesc Guasch committed
119
    eval { $clone->rsync($node) };
Francesc Guasch's avatar
Francesc Guasch committed
120
    is(''.$@,'') or return;
Francesc Guasch's avatar
Francesc Guasch committed
121
122
    # TODO test synced files

Francesc Guasch's avatar
Francesc Guasch committed
123
124
125
126
127
    eval { $base->rsync($node) };
    is($@,'') or return;

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

130
131
132
133
134
135
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
136
137

    is($domain_ip->remote_ip, $ip);
138
139
140
141
142
143
144
145
146
147
148
    $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
149
150
sub test_domain {
    my $vm_name = shift;
Francesc Guasch's avatar
Francesc Guasch committed
151
    my $node = shift or die "Missing node";
152
    my $remote_ip = shift;
Francesc Guasch's avatar
Francesc Guasch committed
153
154
155
156
157
158
159

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

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

    $base->prepare_base(user_admin);
160
    $base->migrate_base(node => $node, user => user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
161
162
163
164
    my $clone = $base->clone(name => new_domain_name
        ,user => user_admin
    );

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

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

170
171
172
173
174
    my @start_arg = ( user => user_admin );
    push @start_arg , ( remote_ip => $remote_ip )   if $remote_ip;

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

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

178
179
180
181
182
    my $local_ip = $node->ip;
    like($clone->display(user_admin),qr($local_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
183
184
    return $clone;
}
185

186
187
188
189
190
191
192
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
    );
193
    ok(scalar @line,$node->type." No iptables found $remote_ip -> $local_ip:$local_port") or confess;
194
195
    ok(scalar @line == 1,$node->type." iptables should found only 1 found $remote_ip -> $local_ip:$local_port ".Dumper(\@line));
}
196

197
sub test_domain_on_remote {
198
199
200
201
202
203
204
    my ($vm_name, $node) = @_;

    my $domain;
    eval {
        $domain = $node->create_domain(
            name => new_domain_name
            ,id_owner => user_admin->id
205
            ,id_iso => search_id_iso('Alpine')
206
207
        );
    };
208
    is($@,'',"Expecting no domain in remote node by now");
209
210
211
212

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

213
214
sub test_remove_domain_from_local {
    my ($vm_name, $node, $domain_orig) = @_;
215
    $domain_orig->shutdown_now(user_admin)   if $domain_orig->is_active;
216
217
218
219
220
221
222

    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
223
    is(''.$@,'',"Expecting no errors removing domain ".$domain_orig->name);
224
225
226
227
228

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

    my $domain3 = $node->search_domain($domain->name);
229
    ok(!$domain3,"Expecting no domain ".$domain->name." in node ".$node->name) or return;
230
231
232
233
234
235
236

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

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


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
256
257
258
259
    if ($node->type ne 'KVM') {
        diag("SKIPPING: test_remove_domain_node skipped on ".$node->type);
        return;
    }
260
261
262
263
264
265
266
267
268
269
    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) {
270
        ok(!$found{$path},$node->name." Expecting vol $path removed")
271
            or return;
272
273
274
    }

}
275

276
277
278
279
280
sub test_sync_base {
    my ($vm_name, $node) = @_;

    my $vm =rvd_back->search_vm($vm_name);
    my $base = create_domain($vm_name);
281
    $base->add_volume(swap => 1, size => 512*1024 );
282
283
284
285
286
287
288
289
    my $clone = $base->clone(
        name => new_domain_name
       ,user => user_admin
    );

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

290
    eval { $base->migrate_base(user => user_admin, vm => $node); };
291
292
    is(''.$@,'');

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

296
297
    $clone->start(user_admin);
    $clone->shutdown_now(user_admin);
298
299
300
301
302
303
304
305
306
307
308
309
    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(''.$@,'');

310
311
    is($clone2->_data('id_vm'),$node->id);

312
313
    my $clone3 = $node->search_domain($clone2->name);
    ok($clone3,"[$vm_name] expecting ".$clone2->name." found in "
314
                .$node->host) or exit;
315

316
317
318
319
    my $domains = rvd_front->list_domains();
    my ($clone_f) = grep { $_->{name} eq $clone2->name } @$domains;
    ok($clone_f);
    is($clone_f->{id}, $clone2->id);
320
    is($clone_f->{node}, $clone2->_vm->name);
321
    is($clone_f->{id_vm}, $node->id);
322

323
324
325
326
327
328
329
330
    $clone->remove(user_admin);
    $base->remove(user_admin);

}

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

331
    if ($vm_name ne 'KVM' && $vm_name ne 'Void') {
Francesc Guasch's avatar
Francesc Guasch committed
332
333
334
335
        diag("SKIPPED: start_twice not available on $vm_name");
        return;
    }

336
337
338
339
340
341
    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
    );
342
343
    $clone->shutdown_now(user_admin)    if $clone->is_active;
    is($clone->is_active,0);
344

345
346
    eval { $base->set_base_vm(vm => $node, user => user_admin); };
    is(''.$@,'') or return;
347
348

    eval { $clone->migrate($node); };
349
    is(''.$@,'')    or return;
350

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

353
    is($clone->is_active,0);
354

355
    # clone should be inactive in local node
356
357
    my $clone2 = $vm->search_domain($clone->name);
    is($clone2->_vm->host, $vm->host);
358
    is($clone2->is_active,0);
359

360
361
362
363
364
    is($clone->_vm->id, $node->id);
    start_domain_internal($clone);
    is($clone->_vm->id, $node->id);

    is($clone->_vm->id, $node->id) or exit;
365
366

    eval { $clone->start(user => user_admin ) };
367
    is($clone->_vm->id, $node->id);
368
    is($clone->is_active,1);
369
370
371
    is($clone2->is_active,0);
    $clone2 = $vm->search_domain($clone->name);
    is($clone2->_vm->host, $vm->host,"[$vm_name] Expecting ".$clone->name." in ".$vm->ip)
372
        or return;
373
374
375
376
377

    my $clone_generic = Ravada::Domain->open($clone->id);
    is($clone_generic->is_active,1);
    is($clone_generic->_vm->host, $node->host,"[$vm_name] Expecting ".$clone->name
                                                        ." in ".$node->ip);
378
379
380
381
382
383

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

}

384
385
386
387
388
389
390
391
392
393
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);
394
395
396
    my $ip_local = $vm->ip;
    $clone_local->_set_spice_ip(1,$vm->ip);# if $clone_local->type eq 'KVM';
    like($clone_local->display(user_admin),qr($ip_local)) or exit;
397
398
399
400
401
402
403
404
405
406
407

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

408
409
    is($clone2->list_requests,2);
    for ( 1 .. 6 ) {
410
411
        for ( 1 .. 10 ) {
            last if !$clone->is_active
412
            && !$clone_local->is_active;
413
            sleep 1;
414
            rvd_back->_process_all_requests_dont_fork();
415
        }
416
417
418
    }
    rvd_back->_process_all_requests_dont_fork();

419
420
    is($clone->is_active, 0,"[".$node->type."] expecting remote clone ".$clone->name." down") or exit;
    is($clone_local->is_active, 0,"[".$node->type."] expecting local clone down") or exit;
421
422
423
424
425
426
427
428
429
430
431
432
433
434

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

435
    $clone->_set_spice_ip(1,$node->ip) if $clone_local->type eq 'KVM';
436
    start_domain_internal($clone);
437
    $clone_local->_set_spice_ip(1,$vm->ip) if $clone_local->type eq 'KVM';
438
    hibernate_domain_internal($clone_local);
439
    $clone->_timeout_shutdown(4);
440
441
442
443
444
445
446
447

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

448
    rvd_back->_process_all_requests_dont_fork(1);
449
    for ( 1 .. 120 ) {
450
451
452
        last if $clone->is_active
                && !$clone_local->is_active
                && !$clone_local->is_hibernated;
453
454
        sleep 1;
    }
455
    wait_request( debug => 1 );
456

457
458
    is($clone->is_active, 0,"[$vm_name] expected ".$clone->name." down in "
        .$clone->_vm->name." ".$clone->_vm->id) or exit;
459
    is($clone_local->is_active, 0,"[$vm_name] expected ".$clone->name." down") or exit;
460
461
462
463
464

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

465
466
467
468
469
470
471
472
473
474
475
476
477
# 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);
478
    is($clone->status,'active');
479
480

    shutdown_domain_internal($clone);
481
    is($clone->status,'active');
482
483
484
485
    _write_in_volumes($clone);

    Ravada::Request->refresh_vms();

486
487
    rvd_back->_process_all_requests_dont_fork();
    is($clone->is_active,0);
488
489
490
491
492
493
494
495

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

496
497
    $clone->remove(user_admin);
    $base->remove(user_admin);
498
}
499

500
501
502
sub _create_clone($node) {

    my $vm =rvd_back->search_vm($node->type);
503
    is($vm->is_local,1);
504
505
    is($node->is_local,0);

506
    my $base = create_domain($vm->type);
507
508
509
510
511
512
513
    $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();
514
    my $clone = $base->clone(
515
        name => $clone_name
516
517
518
519
520
521
522
       ,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;
523
524
525
    for my $volume ( $clone->list_volumes ) {
        ok(-e $volume,"Expecting volume $volume of machine ".$clone->name);
    }
526
527

    eval { $clone->migrate($node); };
528
    is(''.$@,'')    or BAIL_OUT();
529
530
531
532
533
534

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

    return($base, $clone);
}

535
536
537
sub test_rsync_newer {
    my ($vm_name, $node) = @_;

538
    if ($vm_name ne 'KVM') {
539
        diag("Skipping: Volumes not implemented for $vm_name");
540
541
        return;
    }
542
    my $domain = test_domain($vm_name, $node) or return;
543
544
545
546
547
548
549
    $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);

550
551
    my $capacity;
    { # vols equal, then resize
552
    my $vol = $vm->search_volume($vol_name);
553
    ok($vol,"[$vm_name] expecting volume $vol_name")    or return;
554
    ok($vol->get_info,"[$vm_name] No info for remote vol "
555
        .Dumper($vol)) or return;
556

557
    my $vol_remote = $node->search_volume($vol_name);
558
    ok($vol_remote->get_info,"[$vm_name] No info for remote vol "
559
        .Dumper($vol_remote)) or return;
560
    is($vol_remote->get_info->{capacity}, $vol->get_info->{capacity});
561

562
    $capacity = int ($vol->get_info->{capacity} *1.5 );
563
564
    $vol->resize($capacity);
    }
565

566
    { # vols different
567
568
569
    my $vol2 = $vm->search_volume($vol_name);
    my $vol2_remote = $node->search_volume($vol_name);

570
571
572
573
574
    is($vol2->get_info->{capacity}, $capacity);
    isnt($vol2_remote->get_info->{capacity}, $capacity);
    isnt($vol2_remote->get_info->{capacity}, $vol2->get_info->{capacity});
    }

575
    # on starting it should sync
576
577
    is($domain->_vm->host, $node->host);
    $domain->start(user => user_admin);
578
    is($domain->_vm->host, $node->host);
579
580
581
582
583
584
585

    { # 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});
    }

586
    $domain->remove(user_admin);
587
588
}

589
590
591
592
593
594
595
sub test_bases_node {
    my ($vm_name, $node) = @_;

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

    my $domain = create_domain($vm_name);

596
597
598
599
600
601
    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');
602
603
604
605
606
607
608
609

    $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);
    is($domain->base_in_vm($node->id), 1);

610
611
612
613
614
615
    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;
    }

616
617
618
619
620
    $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);
621
622
623
624
    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');
625

626
    user_admin->mark_all_messages_read();
627
628
629
630
631
    my $req = Ravada::Request->set_base_vm(
                uid => user_admin->id
             ,id_vm => $vm->id
         ,id_domain => $domain->id
    );
632
633
    rvd_back->_process_all_requests_dont_fork();
    is($req->status,'done') or die Dumper($req);
634
635
636
    is($req->error,'');
    is($domain->base_in_vm($vm->id), 1);

637
638
    is(scalar user_admin->unread_messages , 2, Dumper(user_admin->unread_messages));

639
640
641
642
643
    $req = Ravada::Request->remove_base_vm(
                uid => user_admin->id
             ,id_vm => $vm->id
         ,id_domain => $domain->id
    );
644
    rvd_back->_process_all_requests_dont_fork();
645
646
    eval { $domain->base_in_vm($vm->id)};
    like($@,qr'is not a base');
647
648
649
650

    $domain->remove(user_admin);
}

651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
sub test_clone_make_base {
    my ($vm_name, $node) = @_;

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

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

    $clone->migrate($node);
    eval { $clone->base_in_vm($node->id) };
    like($@,qr(is not a base));

    eval { $clone->base_in_vm($vm->id) };
    like($@,qr(is not a base));

    $clone->prepare_base(user_admin);
    is($clone->base_in_vm($vm->id),1);
680
    is($clone->base_in_vm($node->id),0);
681
682
683
684
685

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

686
687
688
689
690
691
692
sub _disable_storage_pools {
    my $node = shift;
    for my $sp ($node->vm->list_all_storage_pools()) {
        $sp->destroy();
    }
}

693

694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
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) };
717
    like($@, qr'storage pool.*not found'i);
718
719
720
721
722
723
724
    is($domain->base_in_vm($node->id), undef);

    _enable_storage_pools($node);

    $domain->remove(user_admin);
}

725
726
727
sub test_clone_not_in_node {
    my ($vm_name, $node) = @_;

728
729
    diag("[$vm_name] Checking some clones go to other nodes");

730
731
732
733
734
    my $vm = rvd_back->search_vm($vm_name);

    my $domain = create_domain($vm_name);

    $domain->prepare_base(user_admin);
735
    is($domain->base_in_vm($vm->id), 1);
736
737
738
739
    $domain->set_base_vm(vm => $node, user => user_admin);

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

740
741
742
743
744
745
    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) };
746
        is(''.$@,'',"[$vm_name] Clone of ".$domain->name." failed ".$clone1->name) or return;
747
        is($clone1->is_active,1);
748
749

    # search the domain in the underlying VM
Francesc Guasch's avatar
Francesc Guasch committed
750
751
752
753
754
755
756
757
        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);
        }
758
        last if $clone1->_vm->host ne $clones[0]->_vm->host;
759
760
761
    }


762
763
    isnt($clones[-1]->_vm->host, $clones[0]->_vm->host,"[$vm_name] "
        .$clones[-1]->name
764
        ." - ".$clones[0]->name.Dumper({map {$_->name => $_->_vm->host} @clones})) or exit;
765
766
    for (@clones) {
        $_->remove(user_admin);
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
    }
    $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
786
    eval { $clone->migrate($node) };
787
    is(''.$@,'')                        or return;
788
    is($clone->_vm->host, $node->host);
789
    is($clone->_vm->id, $node->id) or return;
Francesc Guasch's avatar
Francesc Guasch committed
790

791
    is($clone->_data('id_vm'), $node->id) or return;
Francesc Guasch's avatar
Francesc Guasch committed
792
793
794
795
796

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

    eval { $clone->start(user_admin) };
801
    is(''.$@,'',$clone->name) or return;
802
    is($clone->is_active,1);
803
804
    is($clone->_vm->id, $node->id)  or return;
    is($clone->_vm->host, $node->host)  or return;
805
806
807
808
809
810
811

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

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

816
    { # clone is active, it should be found in node
817
    my $clone3 = rvd_back->search_domain($clone->name);
818
    $clone3->check_status();
819
    is($clone3->id, $clone->id);
820
821
    is($clone3->_vm->host , $node->host,"Expecting ".$clone3->name
        ." in ".$node->host) or exit;
822
    }
823
824
825

    $clone->remove(user_admin);
    $domain->remove(user_admin);
826
}
827
828
829
830
831
832

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

    my $domain = create_domain($vm_name);
833
    eval { $domain->base_in_vm($vm->id) };
834
    like($@,qr'is not a base');
835
836
837
838
839

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

    $domain->remove_base(user_admin);
840
841
    eval { $domain->base_in_vm($vm->id) };
    like($@,qr'is not a base');
842
843
844

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

846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
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);
}

Francesc Guasch's avatar
Francesc Guasch committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
sub test_remove_base_main($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(user_admin);
    $domain->prepare_base(user_admin);

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

    $domain->remove(user_admin);
}


897
898
899
sub test_node_inactive($vm_name, $node) {

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

901
    hibernate_node($node);
902
903
904
905
    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
906

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

910
911
912
    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
913

914
    start_node($node);
Francesc Guasch's avatar
Francesc Guasch committed
915
916

    for ( 1 .. 10 ) {
917
        last if $node->_do_is_active;
Francesc Guasch's avatar
Francesc Guasch committed
918
919
920
921
922
923
924
        sleep 1;
        diag("[$vm_name] waiting for node ".$node->name);
    }
    is($node->is_active,1,"[$vm_name] node ".$node->name." active");

}

925
sub test_sync_back($node) {
926
    diag("Testing sync back on remote non shared storage node");
927
928
929
930
931
932
933
934
935
936
937
    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);
938
939
    eval { $clone->start(user_admin) };
    is(''.$@,'',"[".$node->type."] expecting no error starting ".$clone->name) or exit;
940
941
    is($clone->_vm->host, $node->host);

942
    _write_in_volumes($clone);
943
944
945
946
947
948
949
    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);
950
    is ( $clone->is_active, 0 );
951
952
953
    for my $file ($clone->list_volumes) {
        my $md5 = _md5($file, $vm);
        my $md5_remote = _md5($file, $node);
954
955
        is( $md5_remote, $md5, "[".$node->type."] ".$clone->name." $file" )
                or exit;
956
    }
957
958
    $clone->remove(user_admin);
    $domain->remove(user_admin);
959
960
}

961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
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) };
Francesc Guasch's avatar
Francesc Guasch committed
983
    is(''.$@, '');
984
985
986
987
988
989
990
991
992
993
994
995

    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
996
997
998
999
1000
sub test_shutdown($node) {

    my $vm = rvd_back->search_vm($node->type, 'localhost');
    my $domain = create_domain($vm);
    $domain->prepare_base(user_admin);
For faster browsing, not all history is shown. View entire blame