20_base.t 15.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use warnings;
use strict;

use Data::Dumper;
use Test::More;
use Test::SQL::Data;

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

my $test = Test::SQL::Data->new(config => 't/etc/sql.conf');

use_ok('Ravada');

my $FILE_CONFIG = 't/etc/ravada.conf';

my $RVD_BACK = rvd_back($test->connector, $FILE_CONFIG);
18
my $RVD_FRONT= rvd_front($test->connector, $FILE_CONFIG);
19
20
21
22
23
24
25
26

my %ARG_CREATE_DOM = (
      KVM => [ id_iso => 1 ]
    ,Void => [ ]
);

my @ARG_RVD = ( config => $FILE_CONFIG,  connector => $test->connector);

27
my @VMS = reverse keys %ARG_CREATE_DOM;
28
29
my $USER = create_user("foo","bar");

30
31
my $DISPLAY_IP = '99.1.99.1';

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#######################################################################33

sub test_create_domain {
    my $vm_name = shift;

    my $ravada = Ravada->new(@ARG_RVD);
    my $vm = $ravada->search_vm($vm_name);
    ok($vm,"I can't find VM $vm_name") or return;

    my $name = new_domain_name();

    if (!$ARG_CREATE_DOM{$vm_name}) {
        diag("VM $vm_name should be defined at \%ARG_CREATE_DOM");
        return;
    }
    my @arg_create = @{$ARG_CREATE_DOM{$vm_name}};

    my $domain;
    eval { $domain = $vm->create_domain(name => $name
                    , id_owner => $USER->id
                    , @{$ARG_CREATE_DOM{$vm_name}})
    };

    ok($domain,"No domain $name created with ".ref($vm)." ".($@ or '')) or exit;
    ok($domain->name
        && $domain->name eq $name,"Expecting domain name '$name' , got "
        .($domain->name or '<UNDEF>')
        ." for VM $vm_name"
    );

    return $domain;
}

65
66
67
68
69
70
71
72
73
74
75
sub test_files_base {
    my $domain = shift;
    my $n_expected = shift;

    my @files = $domain->list_files_base();

    ok(scalar @files == $n_expected,"Expecting $n_expected files base , got "
            .scalar @files);
    return;
}

76
77
78
sub test_display {
    my ($vm_name, $domain) = @_;

79
80
81
82
83
    my $display;
    $domain->start($USER) if !$domain->is_active;
    eval { $display = $domain->display($USER)};
    is($@,'');
    ok($display,"Expecting a display URI, got '".($display or '')."'") or return;
84

85
86
87
    my ($ip) = $display =~ m{^\w+://(.*):\d+} if defined $display;

    ok($ip,"Expecting an IP , got ''") or return;
88

89
    ok($ip ne '127.0.0.1', "[$vm_name] Expecting IP no '127.0.0.1', got '$ip'") or exit;
90

91
92
93
94
95

    # only test this for Void, it will fail on real VMs
    return if $vm_name ne 'Void';

    $Ravada::CONFIG->{display_ip} = $DISPLAY_IP;
96
97
    eval { $display = $domain->display($USER) };
    is($@,'');
98
99
100
101
102
    ($ip) = $display =~ m{^\w+://(.*):\d+};

    my $expected_ip =  Ravada::display_ip();
    ok($expected_ip,"[$vm_name] Expecting display_ip '$DISPLAY_IP' , got none in config "
        .Dumper($Ravada::CONFIG)) or exit;
103
104
105
106
107

    ok($ip eq $expected_ip,"Expecting display IP '$expected_ip', got '$ip'");

}

108
109
110
111
sub test_prepare_base {
    my $vm_name = shift;
    my $domain = shift;

112
    test_files_base($domain,0);
113
    $domain->shutdown_now($USER)    if $domain->is_active();
114

115
116
117
    eval { $domain->prepare_base( $USER) };
    ok(!$@, $@);
    ok($domain->is_base);
118
    is($domain->is_active(),0);
119
    $domain->is_public(1);
120

121
122
123
124
125
126
127
128
129
130
131
    my $front_domains = rvd_front->list_domains();
    my ($dom_front) = grep { $_->{name} eq $domain->name }
        @$front_domains;

    ok($dom_front,"Expecting the domain ".$domain->name
                    ." in list domains");

    if ($dom_front) {
        ok($dom_front->{is_base});
    }

132
    ok($domain->is_base);
133
    $domain->is_public(1);
134

135
136
    test_files_base($domain,1);

137
    my @disk = $domain->disk_device();
138
    $domain->shutdown(user => $USER)    if $domain->is_active;
139

140
    touch_mtime(@disk);
141
142

    eval { $domain->prepare_base( $USER) };
143
    is($@,'');
144
145
146
147
    ok($domain->is_base);

    my $name_clone = new_domain_name();

148
149
    my $domain_clone;
    eval { $domain_clone = $RVD_BACK->create_domain(
150
151
152
153
        name => $name_clone
        ,id_owner => $USER->id
        ,id_base => $domain->id
        ,vm => $vm_name
154
155
156
157
        );
    };
    ok(!$@,"Clone domain, expecting error='' , got='".($@ or '')."'") or exit;
    ok($domain_clone,"Trying to clone from ".$domain->name." to $name_clone");
158
    test_devices_clone($vm_name, $domain_clone);
159
160
    test_display($vm_name, $domain_clone);

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    ok($domain_clone->id_base && $domain_clone->id_base == $domain->id
        ,"[$vm_name] Expecting id_base=".$domain->id." got ".($domain_clone->id_base or '<UNDEF>')) or exit;

    my $domain_clone2 = $RVD_FRONT->search_clone(
         id_base => $domain->id,
        id_owner => $USER->id
    );
    ok($domain_clone2,"Searching for clone id_base=".$domain->id." user=".$USER->id
        ." expecting domain , got nothing "
        ." ".Dumper($domain)) or exit;

    if ($domain_clone2) {
        ok( $domain_clone2->name eq $domain_clone->name
        ,"Expecting clone name ".$domain_clone->name." , got:".$domain_clone2->name
        );

        ok($domain_clone2->id eq $domain_clone->id
        ,"Expecting clone id ".$domain_clone->id." , got:".$domain_clone2->id
        );
    }

182

183
    touch_mtime(@disk);
184
185
186
187
188
189
190
    eval { $domain->prepare_base($USER) };
    ok($@ && $@ =~ /has \d+ clones/i
        ,"[$vm_name] Don't prepare if there are clones ".($@ or '<UNDEF>'));
    ok($domain->is_base);

    $domain_clone->remove($USER);

191
    touch_mtime(@disk);
192
    eval { $domain->prepare_base($USER) };
Francesc Guasch's avatar
Francesc Guasch committed
193
    ok($domain->is_base,"[$vm_name] Expecting domain is_base=1 , got :".$domain->is_base);
194
195
196
197
198
    ok(!$@,"[$vm_name] Error preparing base after clone removed :'".($@ or '')."'");

    eval { $domain->start($USER)};
    like($@,qr/bases.*started/i);
    is($domain->is_active,0,"Expecting base domains can't be run");
Francesc Guasch's avatar
Francesc Guasch committed
199
200
201
202
203
204
205

    $domain->is_base(0);
    ok(!$domain->is_base,"[$vm_name] Expecting domain is_base=0 , got :".$domain->is_base);

    $domain->is_base(1);
    ok($domain->is_base,"[$vm_name] Expecting domain is_base=1 , got :".$domain->is_base);

206
207
}

208
209
210
211
212
213
214
sub test_prepare_base_active {
    my $vm_name = shift;

    my $domain = test_create_domain($vm_name);

    ok(!$domain->is_base,"Domain ".$domain->name." should not be base") or return;
    eval { $domain->start($USER) if !$domain->is_active() };
215
    ok(!$@,$@) or exit;
216
217
218
219
    eval { $domain->resume($USER)  if $domain->is_paused()  };
    ok(!$@,$@);

    ok($domain->is_active,"[$vm_name] Domain ".$domain->name." should be active") or return;
220
    ok(!$domain->is_paused,"[$vm_name] Domain ".$domain->name." should not be paused") or return;
221

222
223
    eval{ $domain->prepare_base($USER) };
    ok(!$@,"[$vm_name] Prepare base, expecting error='', got '$@'") or exit;
224

225
226
    ok(!$domain->is_active,"[$vm_name] Domain ".$domain->name." should not be active")
            or return;
227
228
}

229
sub touch_mtime {
230
    for my $disk (@_) {
231

232
        my @stat0 = stat($disk);
233

234
235
236
        sleep 2;
        utime(undef, undef, $disk) or die "$! $disk";
        my @stat1 = stat($disk);
237

238
239
        die "$stat0[9] not before $stat1[9] for $disk" if $stat0[0] && $stat0[9] >= $stat1[9];
    }
240
241
242

}

243
244
245
246
247
sub test_devices_clone {
    my $vm_name = shift;
    my $domain = shift;

    my @volumes = $domain->list_volumes();
248
249
250
    ok(scalar(@volumes),"[$vm_name] domain ".$domain->name
        ." Expecting at least 1 volume cloned "
        ." got ".scalar(@volumes)) or exit;
251
    for my $disk (@volumes ) {
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
        ok(-e $disk,"Checking volume ".Dumper($disk)." exists") or exit;
    }
}

sub test_remove_base {
    my $vm_name = shift;

    my $domain = test_create_domain($vm_name);
    ok($domain,"Expecting domain, got NONE") or return;

    my @files0 = $domain->list_files_base();
    ok(!scalar @files0,"Expecting no files base, got ".Dumper(\@files0)) or return;

    $domain->prepare_base($USER);
    ok($domain->is_base,"Domain ".$domain->name." should be base") or return;

    my @files = $domain->list_files_base();
    ok(scalar @files,"Expecting files base, got ".Dumper(\@files)) or return;

    $domain->remove_base($USER);
    ok(!$domain->is_base,"Domain ".$domain->name." should be base") or return;

    for my $file (@files) {
        ok(!-e $file,"Expecting file base '$file' removed" );
276
    }
277

278
279
    my @files_deleted = $domain->list_files_base();
    is(scalar @files_deleted,0);
280
281
282
283
284
285
286
287
288
289
290

    my $sth = $test->dbh->prepare(
        "SELECT count(*) FROM file_base_images"
        ." WHERE id_domain = ?"
    );
    $sth->execute($domain->id);
    my ($count) = $sth->fetchrow;
    $sth->finish;

    is($count,0,"[$vm_name] Count files base after remove base domain");

291
292
}

293
294
295
296
297
298
299
300
301
302
303
304
sub test_dont_remove_base_cloned {
    my $vm_name = shift;

    my $domain = test_create_domain($vm_name);
    $domain->prepare_base($USER);
    ok($domain->is_base,"[$vm_name] expecting domain is base, got "
                        .$domain->is_base);

    my @files = $domain->list_files_base();

    my $name_clone = new_domain_name();

305
    $domain->is_public(1);
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
    my $clone = rvd_back()->create_domain( name => $name_clone
            ,id_owner => $USER->id
            ,id_base => $domain->id
            ,vm => $vm_name
    );
    eval {$domain->remove_base($USER)};
    ok($@,"Expecting error removing base with clones, got '$@'");
    ok($domain->is_base,"[$vm_name] expecting domain is base, got "
                        .$domain->is_base);
    for my $file (@files) {
        ok(-e $file,"[$vm_name] Expecting file base '$file' not removed" );
    }

    ##################################################################3
    # now we remove the clone, it should work

    $clone->remove($USER);

    eval {$domain->remove_base($USER)};
    ok(!$@,"Expecting not error removing base with clones, got '$@'");
    ok(!$domain->is_base,"[$vm_name] expecting domain is base, got "
                        .$domain->is_base);
    for my $file (@files) {
        ok(!-e $file,"[$vm_name] Expecting file base '$file' removed" );
    }

}

334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
sub test_spinned_off_base {
    my $vm_name = shift;

    my $base= test_create_domain($vm_name);
    $base->prepare_base($USER);
    ok($base->is_base,"[$vm_name] expecting domain is base, got "
                        .$base->is_base);

    my $name_clone = new_domain_name();

    $base->is_public(1);
    my $clone = rvd_back()->create_domain( name => $name_clone
            ,id_owner => $USER->id
            ,id_base => $base->id
            ,vm => $vm_name
    );

    # Base can't started, it has clones
    eval { $base->start(user => $USER) };
    like($@,qr'.');
    is($base->is_active,0);

    $clone->prepare_base(user_admin);

    $base->remove_base(user_admin());
    # Base can get started now the clones are released
    eval { $base->start(user => $USER) };
    is($@,'');
    is($base->is_active,1);

    $base->shutdown_now($USER);
    is($base->is_active,0);

    $clone->remove_base(user_admin);

    # Base can get started now the clones are released even though they are not base
    eval { $base->start(user => $USER) };
    is($@,'');
    is($base->is_active,1);

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


Francesc Guasch's avatar
Francesc Guasch committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
sub test_private_base {
    my $vm_name = shift;

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

    my $domain = test_create_domain($vm_name);
    $domain->prepare_base($USER);

    my $clone_name = new_domain_name();

    my $clone;
    eval { $clone = $domain->clone(user => $USER, name => $clone_name); };
    like($@,qr(.));

    my $clone2 = $vm->search_domain($clone_name);
    ok(!$clone2,"Expecting no clone");

    # admin can clone
    eval { $clone = $domain->clone(user => user_admin, name => $clone_name); };
    is($@,'');

    $clone2 = $vm->search_domain($clone_name);
    ok($clone2,"Expecting a clone");
    $clone->remove(user_admin)  if $clone;

    # when is public, any can clone
    $domain->is_public(1);
    eval { $clone = $domain->clone(user => $USER, name => $clone_name); };
    is($@,'');

    $clone2 = $vm->search_domain($clone_name);
    ok($clone2,"Expecting a clone");
    $clone->remove(user_admin)  if $clone;

    # hide it again
    $domain->is_public(0);
    eval { $clone = $domain->clone(user => $USER, name => $clone_name); };
    like($@,qr(.));

    $clone2 = $vm->search_domain($clone_name);
    ok(!$clone2,"Expecting no clone");
}
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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
sub test_domain_limit {
    my $vm_name = shift;

    for my $domain ( rvd_back->list_domains()) {
        $domain->shutdown_now(user_admin);
    }
    my $domain = create_domain($vm_name, $USER);
    ok($domain,"Expecting a new domain created") or exit;
    $domain->shutdown_now($USER)    if $domain->is_active;

    is(rvd_back->list_domains(user => $USER, active => 1),0
        ,Dumper(rvd_back->list_domains())) or exit;

    $domain->start($USER);
    is($domain->is_active,1);

    ok($domain->start_time <= time,"Expecting start time <= ".time
                                    ." got ".time);

    sleep 1;
    is(rvd_back->list_domains(user => $USER, active => 1),1);

    my $domain2 = create_domain($vm_name, $USER);
    $domain2->shutdown_now($USER)   if $domain2->is_active;
    is(rvd_back->list_domains(user => $USER, active => 1),1);

    $domain2->start($USER);
    rvd_back->enforce_limits(timeout => 2);
    sleep 2;
    rvd_back->_process_requests_dont_fork();
    my @list = rvd_back->list_domains(user => $USER, active => 1);
    is(scalar @list,1) or die Dumper(\@list);
    is($list[0]->name, $domain2->name) if $list[0];
}

sub test_domain_limit_already_requested {
    my $vm_name = shift;

    for my $domain ( rvd_back->list_domains()) {
        $domain->shutdown_now(user_admin);
    }
    my $domain = create_domain($vm_name, $USER);
    ok($domain,"Expecting a new domain created") or return;
    $domain->shutdown_now($USER)    if $domain->is_active;

    is(rvd_back->list_domains(user => $USER, active => 1),0
        ,Dumper(rvd_back->list_domains())) or return;

    $domain->start($USER);
    is($domain->is_active,1);

    ok($domain->start_time <= time,"Expecting start time <= ".time
                                    ." got ".time);

    sleep 1;
    is(rvd_back->list_domains(user => $USER, active => 1),1);

    my $domain2 = create_domain($vm_name, $USER);
    $domain2->shutdown_now($USER)   if $domain2->is_active;
    is(rvd_back->list_domains(user => $USER, active => 1),1);

    $domain2->start($USER);
    my @list_requests = $domain->list_requests;
    is(scalar @list_requests,0,"Expecting 0 requests ".Dumper(\@list_requests));

    rvd_back->enforce_limits(timeout => 2);

    if (!$domain->can_hybernate) {
        @list_requests = $domain->list_all_requests();
        is(scalar @list_requests,1,"Expecting 1 request ".Dumper(\@list_requests));
        rvd_back->enforce_limits(timeout => 2);

        @list_requests = $domain->list_all_requests();
        is(scalar @list_requests,1,"Expecting 1 request ".Dumper(\@list_requests));

        sleep 3;

        rvd_back->_process_requests_dont_fork();
    }
    @list_requests = $domain->list_requests;
    is(scalar @list_requests,0,"Expecting 0 request ".Dumper(\@list_requests)) or exit;

    my @list = rvd_back->list_domains(user => $USER, active => 1);
    is(scalar @list,1) or die Dumper(\@list);
    is($list[0]->name, $domain2->name) if $list[0];
}

509
510
511
512
513
514
#######################################################################33


remove_old_domains();
remove_old_disks();

515
for my $vm_name (reverse sort @VMS) {
516
517
518
519
520
521
522
523
524
525
526
527
528
529

    diag("Testing $vm_name VM");
    my $CLASS= "Ravada::VM::$vm_name";


    my $RAVADA;
    eval { $RAVADA = Ravada->new(@ARG_RVD) };

    my $vm;

    eval { $vm = $RAVADA->search_vm($vm_name) } if $RAVADA;

    SKIP: {
        my $msg = "SKIPPED test: No $vm_name VM found ";
530
531
532
533
534
        if ($vm && $vm_name =~ /kvm/i && $>) {
            $msg = "SKIPPED: Test must run as root";
            $vm = undef;
        }

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

538
539
        use_ok($CLASS);

540
541
        my $domain = test_create_domain($vm_name);
        test_prepare_base($vm_name, $domain);
542
        test_prepare_base_active($vm_name);
543
        test_remove_base($vm_name);
544
        test_dont_remove_base_cloned($vm_name);
Francesc Guasch's avatar
Francesc Guasch committed
545
546

        test_private_base($vm_name);
547
548

        test_spinned_off_base($vm_name);
549
550
551
552
553
554
555
    }
}

remove_old_domains();
remove_old_disks();

done_testing();