p10_pools.t 11.3 KB
Newer Older
Francesc Guasch's avatar
Francesc Guasch committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use warnings;
use strict;

use Carp qw(confess);
use Data::Dumper;
use Test::More;
use YAML qw(DumpFile);

use Ravada::Utils;

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


no warnings "experimental::signatures";
use feature qw(signatures);

18
19
20
my $BASE_NAME = "zz-test-base-alpine";
my $BASE;

Francesc Guasch's avatar
Francesc Guasch committed
21
22
sub test_duplicate_req {
        my $req = Ravada::Request->manage_pools(uid => user_admin->id);
Francesc Guasch's avatar
Francesc Guasch committed
23
24
25
26
        my $req_dupe = Ravada::Request->manage_pools(uid => user_admin->id);
        is($req_dupe->id, $req->id);
        my $req_dupe_user =Ravada::Request->manage_pools(uid => Ravada::Utils::user_daemon->id);
        is($req_dupe_user->id, $req->id);
Francesc Guasch's avatar
Francesc Guasch committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
}

sub test_pool($domain) {
    is($domain->pools,0);
    $domain->pools(1);
    is($domain->pools,1);

    is($domain->pool_clones,0);
    wait_request(debug => 0);
}

sub test_request() {
    my $req = Ravada::Request->manage_pools(uid => user_admin->id);
    wait_request();

    is($req->status,'done');
    is($req->error, '');
}

sub test_clones($domain, $n_clones) {
    wait_request();
    $domain->pool_clones($n_clones);
    is($domain->pool_clones, $n_clones);
Francesc Guasch's avatar
Francesc Guasch committed
50
    wait_request(debug => 0);
Francesc Guasch's avatar
Francesc Guasch committed
51
52
53
54
    is($domain->is_base,1);
    is($domain->clones(), $n_clones) or exit;
    is($domain->clones(is_pool => 1), $n_clones);

Francesc Guasch's avatar
Francesc Guasch committed
55
    my $clone = $domain->clone(name => new_domain_name, user => user_admin, from_pool => 0);
Francesc Guasch's avatar
Francesc Guasch committed
56
    ok($clone);
Francesc Guasch's avatar
Francesc Guasch committed
57
    is($domain->clones(), $n_clones+1) or exit;
Francesc Guasch's avatar
Francesc Guasch committed
58
    is($domain->clones(is_pool => 1), $n_clones);
59
60
61
62

    my $clone_f = Ravada::Front::Domain->open($clone->id);
    my $info = $clone_f->info(user_admin);
    is($info->{id_base}, $domain->id,Dumper($info)) or exit;
Francesc Guasch's avatar
Francesc Guasch committed
63
64
65
66
67
68
69
}

sub test_active($domain, $n_start) {
    is($domain->pool_start,0);
    $domain->pool_start($n_start);
    is($domain->pool_start, $n_start);
    _remove_enforce_limits();
70
    wait_request(skip => ['set_time','enforce_limits']);
Francesc Guasch's avatar
Francesc Guasch committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84

    Ravada::Request->manage_pools(uid => user_admin->id);
    _remove_enforce_limits();
    wait_request();

    my @clones = $domain->clones(is_pool => 1 );
    my $n_active = grep { $_->is_active}
    map { Ravada::Domain->open($_->{id}) }
    @clones;
    is($n_active, $n_start) or exit;
}

sub _remove_enforce_limits {
    my $sth = connector->dbh->prepare("DELETE FROM requests "
85
        ."WHERE command = 'enforce_limits' OR command = 'set_time'"
Francesc Guasch's avatar
Francesc Guasch committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    );
    $sth->execute();
    $sth->finish;
}

sub _set_clones_client_status($base) {
    my $sth = connector->dbh->prepare(
        "UPDATE domains set client_status=?, client_status_time_checked=?"
        ." WHERE id_base=? AND status='active'"
    );
    $sth->execute('Disconnected',time,$base->id);
    $sth->finish;
}

Francesc Guasch's avatar
Francesc Guasch committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
sub test_user_create($base, $n_start) {
    $base->is_public(1);
    my $user = create_user('kevin','carter');
    my @clones = $base->clones();
    wait_request();
    _remove_enforce_limits();
    _set_clones_client_status($base);

    my $req = Ravada::Request->create_domain(
                id_owner => $user->id
             ,start => 1
             ,name => new_domain_name()
           ,id_base => $base->id
         ,remote_ip => '1.2.3.4'
    );
    ok($req);
116
    wait_request(debug => 0,skip => ['enforce_limits','set_time']);
Francesc Guasch's avatar
Francesc Guasch committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    _remove_enforce_limits();
    is($req->status,'done');
    is($req->error,'');

    my @clones2 = $base->clones();
    is(scalar(@clones2), scalar(@clones));

    my ($clone) = grep { $_->{id_owner} == $user->id } @clones2;
    ok($clone,"Expecting clone that belongs to ".$user->name);
    like($clone->{client_status},qr'^1.2.3.4$', $clone->{name}) or exit;
    is($clone->{is_pool},1) or exit;

    $user->remove();
}

Francesc Guasch's avatar
Francesc Guasch committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
sub test_user($base, $n_start) {
    # TODO : test $domain->_data('comment');
    $base->is_public(1);
    my $user = create_user('kevin','carter');
    my @clones = $base->clones();
    wait_request();
    _remove_enforce_limits();
    _set_clones_client_status($base);

    my $req = Ravada::Request->clone(
                uid => $user->id
             ,start => 1
         ,id_domain => $base->id
         ,remote_ip => '1.2.3.4'
    );
    ok($req);
Francesc Guasch's avatar
Francesc Guasch committed
148
    wait_request(debug => 0,skip => ['enforce_limits','set_time']);
Francesc Guasch's avatar
Francesc Guasch committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    _remove_enforce_limits();
    is($req->status,'done');
    is($req->error,'');

    my @clones2 = $base->clones();
    is(scalar(@clones2), scalar(@clones));

    my ($clone) = grep { $_->{id_owner} == $user->id } @clones2;
    ok($clone,"Expecting clone that belongs to ".$user->name);
    like($clone->{client_status},qr'^connect', $clone->{name}) or exit;
    is($clone->{is_pool},1) or exit;

    my $clone2 = $base->_search_pool_clone($user);
    ok($clone2);
    is($clone2->id, $clone->{id});
    is($clone2->is_pool,1) or exit;

    my $clone_f = Ravada::Front::Domain->open($clone2->id);
167
168
169
170
    my $info = $clone_f->info(user_admin);
    is($info->{pools},0);
    is($info->{is_pool},1) or die Dumper($clone_f);
    is($info->{comment},$user->name);
Francesc Guasch's avatar
Francesc Guasch committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

    # now we should have another active
    wait_request(debug => 0);
    wait_request(debug => 0);
    @clones = $base->clones(is_pool => 1 );
    my $n_active = grep { $_->is_active}
    map { Ravada::Domain->open($_->{id}) }
    @clones;
    ok($n_active >= $n_start+1,"Expecting active > $n_start, got $n_active ") or exit;

    is($n_active, $n_start+1,"Expecting active == ".(1+ $n_start)
        .", got $n_active ") or exit;

    my $user_r = create_user('paul','robeson');
    $req = Ravada::Request->clone(
                uid => $user_r->id
             ,start => 1
         ,id_domain => $base->id
         ,remote_ip => '2.2.2.5'
    );
    ok($req);
    _remove_enforce_limits();
193
    wait_request( skip => ['enforce_limits','set_time']);
Francesc Guasch's avatar
Francesc Guasch committed
194
195
196
197
198
199
200
201
202
203
    is($req->status,'done');
    is($req->error,'');

    my $clone_r = $base->_search_pool_clone($user_r);
    isnt($clone_r->id,$clone2->id);

    $user->remove();
    $user_r->remove();
}

Francesc Guasch's avatar
Francesc Guasch committed
204
205
sub test_clone_regular($base, $add_to_pool) {
    is($base->pools,1) or confess "Base ".$base->name." has no pools";
Francesc Guasch's avatar
Francesc Guasch committed
206
207
208
209
210
    my $name = new_domain_name();
    my $req = Ravada::Request->clone(
        name => $name
        ,id_domain => $base->id
        ,uid => user_admin->id
Francesc Guasch's avatar
Francesc Guasch committed
211
212
        ,add_to_pool => $add_to_pool
        ,from_pool => 0
Francesc Guasch's avatar
Francesc Guasch committed
213
214
215
    );
    ok($req) or exit;

Francesc Guasch's avatar
Francesc Guasch committed
216
    wait_request(debug => 0);
Francesc Guasch's avatar
Francesc Guasch committed
217
218
219
220
    is($req->status,'done');
    is($req->error,'');

    my $domain = rvd_back->search_domain($name);
Francesc Guasch's avatar
Francesc Guasch committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    ok($domain,"Expecting clone from ".$base->name) or exit;
    is($domain->is_pool, $add_to_pool);

    $domain->pools(1);
    # copy the clone
    my $name_clone = new_domain_name();
    $req = Ravada::Request->clone(
        name => $name_clone
        ,id_domain => $domain->id
        ,uid => user_admin->id
        ,add_to_pool => $add_to_pool
        ,from_pool => 0
    );
    ok($req) or exit;
    wait_request(debug => 0);
    is($req->status,'done');
    is($req->error,'') or exit;

    my $clone = rvd_back->search_domain($name_clone);
    ok($clone,"Expecting clone from ".$name) or exit;
    is($clone->is_pool, $add_to_pool) or exit;

Francesc Guasch's avatar
Francesc Guasch committed
243
    $domain->remove(user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286


}

sub test_no_pool($vm) {
    my $base = create_domain($vm);
    $base->prepare_base(user_admin);

    my $clone_name = new_domain_name();

    my $clone;
    eval {
        $clone = rvd_back->create_domain(
            id_base => $base->id
            ,name => $clone_name
            ,add_to_pool => 1
            ,id_owner => user_admin->id
        );
    };
    like($@,qr(this base has no pools)i);
    ok(!$clone);

    my $clone2 = rvd_back->search_domain($clone_name);
    ok(!$clone2);

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

    $clone_name = new_domain_name();
    eval {
        $clone = $base->clone(
             name => $clone_name
            ,user => user_admin
            ,add_to_pool => 1
        );
    };
    like($@,qr(this base has no pools)i);
    $clone2 = rvd_back->search_domain($clone_name);
    ok(!$clone);
    ok(!$clone2);

    $clone->remove(user_admin) if $clone;
    $base->remove(user_admin);
    wait_request( debug => 0);
Francesc Guasch's avatar
Francesc Guasch committed
287
288
}

289
sub import_base($vm) {
290
    if ($vm->type eq 'KVM') {
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
317
318
319
320
321
322
        $BASE = import_domain($vm->type, $BASE_NAME, 1);
        confess "Error: domain $BASE_NAME is not base" unless $BASE->is_base;

        confess "Error: domain $BASE_NAME has exported ports that conflict with the tests"
        if $BASE->list_ports;
    } else {
        $BASE = create_domain($vm);
    }
}

sub test_exposed_port($vm) {
    my $base = $BASE->clone(name => new_domain_name, user => user_admin);

    $base->pools(1);
    $base->volatile_clones(1);
    my $n = 3;
    $base->pool_clones($n);
    $base->expose(22);

    my $req = Ravada::Request->manage_pools(uid => user_admin->id , _no_duplicate => 1);
    wait_request( debug => 1, skip => 'set_time' );
    is($req->status, 'done');

    my $req_refresh = Ravada::Request->refresh_vms( _no_duplicate => 1);
    wait_request( debug => 1 ,skip => 'set_time' );
    is($req_refresh->status, 'done');
    is(scalar($base->clones), $n);

    my $clone = $base->clone(name => new_domain_name(), user => user_admin);

    for my $clone ( $base->clones ) {
        Ravada::Domain->open($clone->{id})->remove(user_admin);
323
    }
324
325
326
327
328
329
    $base->remove(user_admin);

}

sub test_remove_clone($vm) {
    my $base = $BASE->clone(name => new_domain_name, user => user_admin);
330
331
332
333
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

    $base->pools(1);
    $base->volatile_clones(1);

    my $n = 5;
    $base->pool_clones($n);
    $base->pool_start($n);
    my $req = Ravada::Request->manage_pools(uid => user_admin->id , _no_duplicate => 1);
    wait_request( debug => 0);
    is($req->status, 'done');

    my $req_refresh = Ravada::Request->refresh_vms( _no_duplicate => 1);
    wait_request( debug => 0);
    is($req_refresh->status, 'done');

    my @clones = $base->clones();
    is(scalar @clones, $n);
    Ravada::Domain->open($clones[0]->{id})->remove(user_admin);
    is(scalar($base->clones()),$n-1);

    warn Dumper([map { $_->{name}." ".$_->{status} } $base->clones]);
    $req = Ravada::Request->manage_pools(uid => user_admin->id, _no_duplicate => 1);
    wait_request();
    is($req->status, 'done');
    ok(Dumper([map { $_->{name} } $base->clones]));
    for my $clone ( $base->clones ) {
        like($clone->{name},qr/-\d+$/);
        Ravada::Domain->open($clone->{id})->remove(user_admin);
    }
    $base->remove(user_admin);

}

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

Francesc Guasch's avatar
Francesc Guasch committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
init();
clean();

for my $vm_name (reverse vm_names() ) {
    my $vm;
    eval {
        $vm = rvd_back->search_vm($vm_name);
    };
    SKIP: {
        my $msg = "SKIPPED: $vm_name virtual manager not found ".($@ or '');
        if ($vm && $vm_name =~ /kvm/i && $>) {
            $msg = "SKIPPED: Test must run as root";
            $vm = undef;
        }

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

        diag("*** Testing pools in $vm_name ***");
384
385
386
        import_base($vm);

        test_exposed_port($vm);
Francesc Guasch's avatar
Francesc Guasch committed
387

388
        test_remove_clone($vm);
Francesc Guasch's avatar
Francesc Guasch committed
389
390
        test_duplicate_req();

Francesc Guasch's avatar
Francesc Guasch committed
391
392
        test_no_pool($vm);

Francesc Guasch's avatar
Francesc Guasch committed
393
394
395
396
397
398
399
400
401
402
403
        my $domain = create_domain($vm);

        my $n_clones = 6;
        my $n_start = 2;

        test_pool($domain);
        test_request();
        test_clones($domain, $n_clones);
        test_active($domain, $n_start);

        test_user($domain, $n_start);
Francesc Guasch's avatar
Francesc Guasch committed
404
405
406
407
408
409
        test_user_create($domain, $n_start);

        # add the clone to the pool => 1 <=
        test_clone_regular($domain   , 1);
        # do not add the clone to the pool
        test_clone_regular($domain, 0);
Francesc Guasch's avatar
Francesc Guasch committed
410
411
412
413
414

        _remove_enforce_limits();
   }
}

415
end();
Francesc Guasch's avatar
Francesc Guasch committed
416
done_testing();