p10_pools.t 8.64 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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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);

sub test_duplicate_req {
        my $req = Ravada::Request->manage_pools(uid => user_admin->id);
        ok(! Ravada::Request->manage_pools(uid => user_admin->id));
        ok(! Ravada::Request->manage_pools(uid => Ravada::Utils::user_daemon->id));
}

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
45
    wait_request(debug => 1);
Francesc Guasch's avatar
Francesc Guasch committed
46
47
48
49
    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
50
    my $clone = $domain->clone(name => new_domain_name, user => user_admin, from_pool => 0);
Francesc Guasch's avatar
Francesc Guasch committed
51
    ok($clone);
Francesc Guasch's avatar
Francesc Guasch committed
52
    is($domain->clones(), $n_clones+1) or exit;
Francesc Guasch's avatar
Francesc Guasch committed
53
    is($domain->clones(is_pool => 1), $n_clones);
54
55
56
57

    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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
}

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();
    wait_request(skip => 'enforce_limits');

    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 "
        ."WHERE command = 'enforce_limits'"
    );
    $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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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);
    wait_request(debug => 0,skip => 'enforce_limits');
    _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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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);
    wait_request(debug => 0,skip => 'enforce_limits');
    _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);
162
163
164
165
    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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

    # 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();
    wait_request( skip => 'enforce_limits');
    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
199
200
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
201
202
203
204
205
    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
206
207
        ,add_to_pool => $add_to_pool
        ,from_pool => 0
Francesc Guasch's avatar
Francesc Guasch committed
208
209
210
    );
    ok($req) or exit;

Francesc Guasch's avatar
Francesc Guasch committed
211
    wait_request(debug => 0);
Francesc Guasch's avatar
Francesc Guasch committed
212
213
214
215
    is($req->status,'done');
    is($req->error,'');

    my $domain = rvd_back->search_domain($name);
Francesc Guasch's avatar
Francesc Guasch committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    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
238
    $domain->remove(user_admin);
Francesc Guasch's avatar
Francesc Guasch committed
239
240
241
242
243
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


}

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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
}

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 ***");

        test_duplicate_req();

Francesc Guasch's avatar
Francesc Guasch committed
306
307
        test_no_pool($vm);

Francesc Guasch's avatar
Francesc Guasch committed
308
309
310
311
312
313
314
315
316
317
318
        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
319
320
321
322
323
324
        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
325
326
327
328
329
330
331

        _remove_enforce_limits();
   }
}

clean();
done_testing();