p10_password.t 12.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
use warnings;
use strict;

use Carp qw(confess);
use Data::Dumper;
use IPC::Run3;
use Test::More;

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

use_ok('Ravada');

Francesc Guasch's avatar
Francesc Guasch committed
14
init();
15
my @VMS = vm_names();
16
my $USER = create_user("foo","bar", 1);
17
18
19

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

20
21
22
23
24
25
26
27
28
sub test_domain_no_password {
    my $vm_name = shift;
    my $vm = rvd_back->search_vm($vm_name);

    my $net = Ravada::Network->new(address => '127.0.0.1/32');

    ok(!$net->requires_password);
    my $domain_name = new_domain_name();
    my $domain = $vm->create_domain( name => $domain_name
Francesc Guasch's avatar
Francesc Guasch committed
29
                , disk => 1024 * 1024
30
                , id_iso => search_id_iso('Alpine') , id_owner => $USER->id);
31
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

    $domain->start(user => $USER, remote_ip => '127.0.0.1');

    my $password = $domain->spice_password();
    is($password,undef
        ,"Expecting no password, got '".($password or '')."'");

    $domain->shutdown_now($USER);
    for ( 1 .. 10 ) {
        sleep 1;
        last if !$domain->is_active();
    }
    is($domain->is_active,0) or return;

    my $net2 = Ravada::Network->new(address => '10.0.0.1/32');
    ok(!$net2->requires_password,"Expecting net requires password ");

    $domain->start(user => $USER, remote_ip => '10.0.0.1');

    my $vm2 = rvd_back->search_vm($vm_name);
    my $domain2 = $vm2->search_domain($domain->name);
    $password = $domain2->spice_password();
    is($password,undef,"Expecting no password, got '".($password or '')."'");

    $password = $domain->spice_password();
    is($password,undef,"Expecting no password, got '".($password or '')."'")   or exit;

    my $domain_f = rvd_front()->search_domain($domain->name);
    my $password_f;
    eval { $password_f = $domain_f->spice_password() };
    is($@,'');
    is($password_f , $password,"Expecting password : '".($password or '')."'"
                                ." got : '".($password_f or '')."'");
Francesc Guasch's avatar
Francesc Guasch committed
64
    $domain->shutdown_now($USER)    if $domain->is_active();
65
66
}

67
sub test_domain_password2 {
68
69
70
71
72
73
74
75
    my $vm_name = shift;
    my $vm = rvd_back->search_vm($vm_name);

    my $net = Ravada::Network->new(address => '127.0.0.1/32');

    ok(!$net->requires_password) or return;
    my $domain_name = new_domain_name();
    my $domain = $vm->create_domain( name => $domain_name
Francesc Guasch's avatar
Francesc Guasch committed
76
                , disk => 1024 * 1024
77
                , id_iso => search_id_iso('Alpine') , id_owner => $USER->id);
78
79
80

    $domain->start(user => $USER, remote_ip => '127.0.0.1');

81
    my $password = $domain->spice_password();
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    is($password,undef
        ,"Expecting no password, got '".($password or '')."'");

    $domain->shutdown_now($USER);
    for ( 1 .. 10 ) {
        sleep 1;
        last if !$domain->is_active();
    }
    is($domain->is_active,0) or return;

    my $net2 = Ravada::Network->new(address => '10.0.0.1/32');
    ok($net2->requires_password,"Expecting net requires password ")
        or return;

    $domain->start(user => $USER, remote_ip => '10.0.0.1');

    my $vm2 = rvd_back->search_vm($vm_name);
    my $domain2 = $vm2->search_domain($domain->name);
    $password = $domain2->spice_password();
    like($password,qr/./,"Expecting a password, got '".($password or '')."'");

    $password = $domain->spice_password();
    like($password,qr/./,"Expecting a password, got '".($password or '')."'")   or exit;

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    my $domain_f = rvd_front()->search_domain($domain->name);
    my $password_f;
    eval { $password_f = $domain_f->spice_password() };
    is($@,'');
    is($password_f , $password,"Expecting password : '".($password or '')."'"
                                ." got : '".($password_f or '')."'");
    $domain->shutdown_now($USER);
    return $domain;
}

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

    my $net2 = Ravada::Network->new(address => '10.0.0.1/32');

    ok($net2->requires_password,"Expecting net requires password ")
        or return;
    my $domain = $vm->create_domain( name => new_domain_name
Francesc Guasch's avatar
Francesc Guasch committed
125
                , disk => 1024 * 1024
126
                , id_iso => search_id_iso('Alpine') , id_owner => $USER->id);
127
128
129
130
131
132

    $domain->start(user => $USER, remote_ip => '10.0.0.1');

    my $vm2 = rvd_back->search_vm($vm_name);
    my $domain2 = $vm2->search_domain($domain->name);
    my $password = $domain2->spice_password();
133
    like($password,qr/./,"Expecting a password, got '".($password or '')."'") or die $domain2->name;
134
135
136
137
138
139
140
141
142
143
144
145

    $password = $domain->spice_password();
    like($password,qr/./,"Expecting a password, got '".($password or '')."'");

    my $domain_f = rvd_front()->search_domain($domain->name);
    my $password_f;
    eval { $password_f = $domain_f->spice_password() };
    ok(!$@, "Expecting no error, got : '".($@ or '')."'");
    is($password_f , $password,"Expecting password : '".($password or '')."'"
                                ." got : '".($password_f or '')."'");

    $domain->shutdown_now($USER);
146
147
148
    return $domain;
}

149
150
151
152
153
154
155
156
sub test_any_network_password {
    my $vm_name = shift;
    my $vm = rvd_back->search_vm($vm_name);

    add_network_10(0);
    add_network_any(1);

    my $domain = $vm->create_domain( name => new_domain_name
Francesc Guasch's avatar
Francesc Guasch committed
157
                , disk => 1024 * 1024
158
                , id_iso => search_id_iso('Alpine') , id_owner => $USER->id);
159
160
161
162

    $domain->start(user => $USER, remote_ip => '127.0.0.1');

    my $password = $domain->spice_password();
163
    is($password, undef ,"Expecting no password, got '".($password or '')."'") or exit;
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    $domain->shutdown_now($USER);

    $domain->start(user => $USER, remote_ip => '10.0.0.1');

    $password = $domain->spice_password();
    is($password, undef ,"Expecting no password, got '".($password or '')."'");
    $domain->shutdown_now($USER);

    $domain->start(user => $USER, remote_ip => '1.2.3.4');

    $password = $domain->spice_password();
    like($password,qr/./,"Expecting a password, got '".($password or '')."'");
    $domain->shutdown_now($USER);

}

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

    add_network_10(0);
    add_network_any(1);

    my $domain = $vm->create_domain( name => new_domain_name
Francesc Guasch's avatar
Francesc Guasch committed
188
                , disk => 1024 * 1024
189
                , id_iso => search_id_iso('Alpine') , id_owner => $USER->id);
190
191
192
193
194
195

    $domain->start(user => $USER, remote_ip => '127.0.0.1');

    my $password = $domain->spice_password();
    is($password, undef ,"Expecting no password, got '".($password or '')."'");

196
    $domain->hibernate($USER);
197
    is($domain->is_active(),0);
198
    is($domain->is_hibernated(),1,"Domain should be hybernated");
199
200

    eval { $domain->start(user => $USER, remote_ip => '10.0.0.1') };
201
202
    ok(!$@,"Expecting no error start hybernated domain, got : '".($@ or '')."'");
    is($domain->is_active(),1,"Expecting domain active");
203

204
205
206
207
    my $password2 = $domain->spice_password();
    is($password2, undef ,"Expecting no password, got '".($password2 or '')."' after hybernate");

    is($password2,$password);
208

209
210
    # create another domain to start from far away
    $domain = $vm->create_domain( name => new_domain_name
Francesc Guasch's avatar
Francesc Guasch committed
211
                , disk => 1024 * 1024
212
                , id_iso => search_id_iso('Alpine') , id_owner => $USER->id);
213

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    eval {
        $domain->start($USER)   if !$domain->is_active;
        for ( 1 .. 10 ){
            last if $domain->is_active;
            sleep 1;
        }
        $domain->hybernate($USER);
    };
    ok(!$@,"Expecting no error after \$domain->hybernate, got : '".($@ or '')."'");
    is($domain->is_active(),0,"Domain should not be active, got :".$domain->is_active);
    is($domain->is_hibernated(),1,"Domain should be hybernated");

    eval { $domain->start(user => $USER, remote_ip => '1.2.3.4') };
    ok(!$@,"Expecting no error after \$domain->start, got : '".($@ or '')."'");

    eval { $password = $domain->spice_password() };
    is($@,'',"Expecting no error after \$domain->spice_password hybernate/start");
231
    is($password, undef,"Expecting password, got '".($password or '')."' after hybernate");
232
    is($domain->spice_password,$password);
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250

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

    eval { $domain->start(user => $USER, remote_ip => '1.2.3.4') };
    ok(!$@,"Expecting no error after \$domain->start, got : '".($@ or '')."'");
    eval { $password = $domain->spice_password() };
    like($password,qr/./,"Expecting a password, got '".($password2 or '')."'");

    $domain->hybernate($USER);
    is($domain->is_hibernated(),1,"Domain should be hybernated");

    eval { $password2 = $domain->spice_password() };
    is($@,'',"Expecting no error after \$domain->spice_password hybernate/start");
    like($password2,qr/./,"Expecting a password, got '".($password2 or '')."'");

    is($password2,$password);

251
252
253
254
255
256
257
258
    eval { $domain->start(user => $USER, remote_ip => '1.2.3.4') };
    ok(!$@,"Expecting no error after \$domain->start, got : '".($@ or '')."'");

    my $password3;
    eval { $password3 = $domain->spice_password() };
    like($password3,qr/./,"Expecting a password, got '".($password3 or '')."'");
    is($password3,$password2);

259
    $domain->shutdown_now($USER)    if $domain->is_active;
260
261
262

}

263
sub add_network_10 {
264
265
266
    my $requires_password = shift;
    $requires_password = 1 if !defined $requires_password;

Francesc Guasch's avatar
Francesc Guasch committed
267
    my $sth = connector->dbh->prepare(
268
269
270
        "DELETE FROM networks where address='10.0.0.0/24'"
    );
    $sth->execute;
Francesc Guasch's avatar
Francesc Guasch committed
271
    $sth = connector->dbh->prepare(
272
273
274
275
276
277
278
279
280
281
        "INSERT INTO networks (name,address,all_domains,requires_password)"
        ."VALUES('10','10.0.0.0/24',1,?)"
    );
    $sth->execute($requires_password);
}

sub add_network_any {
    my $requires_password = shift;
    $requires_password = 1 if !defined $requires_password;

Francesc Guasch's avatar
Francesc Guasch committed
282
    my $sth = connector->dbh->prepare(
283
284
285
286
        "DELETE FROM networks where address='0.0.0.0/0'"
    );
    $sth->execute;

Francesc Guasch's avatar
Francesc Guasch committed
287
    $sth = connector->dbh->prepare(
288
289
        "INSERT INTO networks (name,address,all_domains,requires_password,n_order)"
        ."VALUES('any','0.0.0.0/0',1,?,999)"
290
291
292
293
294
    );
    $sth->execute($requires_password);
}

sub remove_network_10 {
Francesc Guasch's avatar
Francesc Guasch committed
295
    my $sth = connector->dbh->prepare(
296
        "DELETE FROM networks where name='10'"
297
298
    );
    $sth->execute();
299

300
301
}

302
sub remove_network_default {
Francesc Guasch's avatar
Francesc Guasch committed
303
    my $sth = connector->dbh->prepare(
304
305
306
307
308
309
        "DELETE FROM networks where name='default'"
    );
    $sth->execute();

}

310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
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
sub _remove_network {
    my $name = shift;
    my $sth = connector->dbh->prepare(
        "DELETE FROM networks where name=?"
    );
    $sth->execute($name);

}
# When a domain is started in network that requires password, then
# the password won't show in password-less networks.
sub test_reopen {
    my $vm_name = shift;
    my $domain1 = create_domain($vm_name);

    add_network_any(1); # with password
    $domain1->start(user => user_admin
        ,remote_ip => '8.8.8.8'
    );
    my $password1;
    eval { $password1 = $domain1->spice_password };
    is($@,'');
    like($password1,qr'.+');

    my $domain2 = create_domain($vm_name);
    $domain2->start(user => user_admin
        ,remote_ip => '127.0.0.1'
    );
    my $password2;
    eval { $password2 = $domain2->spice_password };
    is($@,'');
    is($password2,undef);

    $domain1->start(user => user_admin
        , remote_ip => '127.0.0.1'
    );
    my $password1b;
    eval { $password1b = $domain1->spice_password };
    is($@,'');
    like($password1b,qr'.+');
    is($password1, $password1b);

    $domain1->remove(user_admin);
    $domain2->remove(user_admin);

    _remove_network('any');
}
356

357
358
#######################################################

359
360
361
362
363
364
365
if ($>)  {
    diag("SKIPPED: Test must run as root");
    done_testing();
    exit;
}


366
367
368
369
370
clean();

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

371

372
373
374
375
376
377
378
379
380
381
SKIP: {

    my $msg = "SKIPPED: No virtual managers found";
    if ($vm && $vm_name =~ /kvm/i && $>) {
        $msg = "SKIPPED: Test must run as root";
        $vm = undef;
    }

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

382
383
384
385
386
387
388
    add_network_10();
    my $domain1 = test_domain_password1($vm_name);
    my $domain2 = test_domain_password2($vm_name);
    remove_network_10();

    $domain1->start(user => $USER, remote_ip => '10.0.0.1');
    my $password = $domain1->spice_password();
389
390
391
392
393
394
395
    ok($password,"Expecting password, got : '".($password or '')."'");

    remove_network_default();
    $domain1->shutdown_now($USER);
    $domain1->start(user => $USER, remote_ip => '10.0.0.1');
    $password = $domain1->spice_password();

396
    is($password,undef,"Expecting no password, got : '".($password or '')."'");
397
    $domain1->shutdown_now($USER)   if $domain1->is_active;
398
399
400
401

    $domain2->start(user => $USER, remote_ip => '10.0.0.1');
    $password = $domain2->spice_password();
    is($password,undef,"Expecting no password, got : '".($password or '')."'");
402
    $domain2->shutdown_now($USER)   if $domain2->is_active;
403

404
    test_reopen($vm_name);
405
406
407
408
    test_domain_no_password($vm_name);

    test_any_network_password($vm_name);
    test_any_network_password_hybernate($vm_name);
409
410
}

411
end();
412
413

done_testing();