35_request_start.t 7.08 KB
Newer Older
Francesc Guasch's avatar
Francesc Guasch committed
1
2
3
use warnings;
use strict;

4
5
use Carp qw(confess);
use Data::Dumper;
Francesc Guasch's avatar
Francesc Guasch committed
6
7
8
9
10
use Test::More;
use Test::SQL::Data;

use_ok('Ravada');
use_ok('Ravada::Request');
11
use lib 't/lib';
Francesc Guasch's avatar
Francesc Guasch committed
12

13
use Test::Ravada;
Francesc Guasch's avatar
Francesc Guasch committed
14

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

17
18
init($test->connector, 't/etc/ravada.conf');
my $RAVADA = rvd_back();
19
my $USER = create_user('foo','bar', 1);
Francesc Guasch's avatar
Francesc Guasch committed
20

21
my @ARG_CREATE_DOM = ( id_owner => $USER->id , id_iso => search_id_iso('Alpine') );
Francesc Guasch's avatar
Francesc Guasch committed
22
23

sub test_remove_domain {
24
    my $vm_name = shift;
Francesc Guasch's avatar
Francesc Guasch committed
25
26
    my $name = shift;

27
28
29
30
31
    my $vm = rvd_back->search_vm($vm_name) 
        or confess "I can't find vm $vm_name";

    diag("[$vm_name] removing domain $name");
    my $domain = $vm->search_domain($name,1);
Francesc Guasch's avatar
Francesc Guasch committed
32

33
34
    my $disks_not_removed = 0;

Francesc Guasch's avatar
Francesc Guasch committed
35
36
    if ($domain) {
        diag("Removing domain $name");
37
38
        my @disks = $domain->list_disks();
        eval { 
39
            $domain->remove(user_admin->id);
40
41
        };
        ok(!$@ , "Error removing domain $name ".ref($domain).": $@") or exit;
Francesc Guasch's avatar
Francesc Guasch committed
42

43
44
45
        for (@disks) {
            ok(!-e $_,"Disk $_ should be removed") or $disks_not_removed++;
        }
Francesc Guasch's avatar
Francesc Guasch committed
46
47

    }
48
49
    $domain = $vm->search_domain($name,1);
    ok(!$domain, "Removing old domain $name") or exit;
50
    ok(!$disks_not_removed,"$disks_not_removed disks not removed from domain $name");
Francesc Guasch's avatar
Francesc Guasch committed
51
52
53
}

sub test_new_domain {
54
    my $vm_name = shift;
Francesc Guasch's avatar
Francesc Guasch committed
55
56
    my $name = shift;

57
    my $vm = $RAVADA->search_vm($vm_name);
Francesc Guasch's avatar
Francesc Guasch committed
58

59
60
61
#    test_remove_domain($vm_name, $name);

    diag("[$vm_name] Creating domain $name");
62
    $vm->connect();
63
    my $domain = $vm->create_domain(name => $name, @ARG_CREATE_DOM, active => 0);
Francesc Guasch's avatar
Francesc Guasch committed
64
65
66
67
68
69
70
71

    ok($domain,"Domain not created");

    return $domain;
}


sub test_start {
72
    my $vm_name = shift;
73
    my $fork = shift;
74

75
    my $name = new_domain_name();
76
#    test_remove_domain($vm_name, $name);
Francesc Guasch's avatar
Francesc Guasch committed
77
78


79
80
    my $remote_ip = '99.88.77.66';

Francesc Guasch's avatar
Francesc Guasch committed
81
    my $req = Ravada::Request->start_domain(
82
83
        name => "does not exists"
        ,uid => $USER->id
84
        ,remote_ip => $remote_ip
Francesc Guasch's avatar
Francesc Guasch committed
85
    );
86
87
88
89
90
    if ($fork) {
        $RAVADA->process_requests(0);
    } else {
        $RAVADA->_process_all_requests_dont_fork(0);
    }
Francesc Guasch's avatar
Francesc Guasch committed
91

92
93
94
    wait_request($req);

    ok($req->status eq 'done', "[$vm_name] Req ".$req->{id}." expecting status done, got ".$req->status);
95
    like($req->error , qr/unknown/i
96
            ,"[$vm_name] Req ".$req->{id}." expecting unknown domain error , got "
97
                .($req->error or '<NULL>')) or exit;
Francesc Guasch's avatar
Francesc Guasch committed
98
99
    $req = undef;

Francesc Guasch's avatar
Francesc Guasch committed
100
101
102
    #####################################################################3
    #
    # start
103
    test_new_domain($vm_name, $name);
104

105
106
107
108
109
    {
        my $vm = $RAVADA->search_vm($vm_name);
        my $domain = $vm->search_domain($name);
        ok(!$domain->is_active,"Domain $name should be inactive") or return;
    }
110
    my $req2 = Ravada::Request->start_domain(name => $name, uid => $USER->id
111
        ,remote_ip => $remote_ip
112
    );
113
    $RAVADA->process_requests();
Francesc Guasch's avatar
Francesc Guasch committed
114

115
116
117
    wait_request($req2);
    ok($req2->status eq 'done',"Expecting request status 'done' , got "
                                .$req2->status);
Francesc Guasch's avatar
Francesc Guasch committed
118

119
    my $id_domain;
120
121
    {
        my $domain = $RAVADA->search_domain($name);
122
        $id_domain = $domain->id;
123
124
        $domain->start($USER)    if !$domain->is_active();
        ok($domain->is_active);
125
        is($domain->is_volatile,0);
Francesc Guasch's avatar
Francesc Guasch committed
126

127
128
129
        my $vm = $RAVADA->search_vm($vm_name);
        my $domain2 = $vm->search_domain($name);
        ok($domain2->is_active);
130
        is($domain2->is_volatile,0);
131
    }
Francesc Guasch's avatar
Francesc Guasch committed
132

Francesc Guasch's avatar
Francesc Guasch committed
133
134
135
136
137
138
    $req2 = undef;

    #####################################################################3
    #
    # stop

139
    my $req3 = Ravada::Request->force_shutdown_domain(id_domain => $id_domain, uid => $USER->id);
Francesc Guasch's avatar
Francesc Guasch committed
140
    $RAVADA->_process_all_requests_dont_fork(0);
141
142
143
    wait_request($req3);
    ok($req3->status eq 'done',"[$vm_name] expecting request done , got "
                            .$req3->status);
144
145
    ok(!$req3->error,"Error shutting down domain $name , expecting ''
                        . Got '".($req3->error or ''));
Francesc Guasch's avatar
Francesc Guasch committed
146

147
    my $vm = $RAVADA->search_vm($vm_name);
148
    my $domain3 = $vm->search_domain($name);
149
    ok($domain3,"[$vm_name] Searching for domain $name") or exit;
150
    for ( 1 .. 60 ) {
151
        last if !$domain3 || !$domain3->is_active;
152
153
        sleep 1;
    }
154
    ok(!$domain3->is_active,"Domain $name should not be active");
Francesc Guasch's avatar
Francesc Guasch committed
155

156
157
    return $domain3;

Francesc Guasch's avatar
Francesc Guasch committed
158
159
}

160
161
sub test_screenshot {
    my $vm_name = shift;
162
    my $domain_name = shift;
163

164
    my $domain = $RAVADA->search_domain($domain_name);
165
166
167
168
169
170
171
172
173
    $domain->start($USER) if !$domain->is_active();
    return if !$domain->can_screenshot();

    unlink $domain->_file_screenshot or die "$! ".$domain->_file_screenshot
        if -e $domain->_file_screenshot;

    ok(!-e $domain->_file_screenshot,"File screenshot ".$domain->_file_screenshot
                                    ." should not exist");

174
175
176
177
178
    my $file_screenshot = $domain->_file_screenshot();
    my $domain_id = $domain->id;
    $domain = undef;

    my $req = Ravada::Request->screenshot_domain(id_domain => $domain_id );
179
    ok($req);
180
181
182

    my $dont_fork = 1;
    rvd_back->process_all_requests(0,$dont_fork);
183
184
185
186
    wait_request($req);
    ok($req->status('done'),"Request should be done, it is ".$req->status);
    ok(!$req->error(''),"Error should be '' , it is ".$req->error);

187
    ok(-e $file_screenshot,"File screenshot ".$file_screenshot
188
189
190
191
192
                                    ." should exist");
}

sub test_screenshot_file {
    my $vm_name = shift;
193
194
195
    my $domain_name = shift;

    my $domain = $RAVADA->search_domain($domain_name);
196
197
198
199
200
201
202
203
204
205

    $domain->start($USER) if !$domain->is_active();
    return if !$domain->can_screenshot();

    unlink $domain->_file_screenshot or die "$! ".$domain->_file_screenshot
        if -e $domain->_file_screenshot;

    ok(!-e $domain->_file_screenshot,"File screenshot should not exist");

    my $file = "/var/tmp/screenshot.$$.png";
206
207
208
    my $domain_id = $domain->id;
    $domain = undef;

209
    my $req = Ravada::Request->screenshot_domain(
210
        id_domain => $domain_id
211
212
213
        ,filename => $file);
    ok($req);

214
215
    my $dont_fork = 1;
    rvd_back->process_all_requests(0,$dont_fork);
216
217
218
    wait_request($req);

    ok($req->status('done'),"Request should be done, it is ".$req->status);
219
    ok(!$req->error(),"Error should be '' , it is ".($req->error or ''));
220
221
222
223
224
225

    ok(-e $file,"File '$file' screenshot should exist");

}


Francesc Guasch's avatar
Francesc Guasch committed
226
227
228
###############################################################
#

Francesc Guasch's avatar
Francesc Guasch committed
229
230
231
remove_old_domains();
remove_old_disks();

232
for my $vm_name (qw(KVM Void)) {
233
    my $vmm = $RAVADA->search_vm($vm_name);
234

235
236
    SKIP: {
        my $msg = "SKIPPED: Virtual manager $vm_name not found";
237
238
239
240
241
        if ($vmm && $>) {
            $msg = "SKIPPED: Test must run as root";
            $vmm = undef;
        }

242
243
        diag($msg) if !$vmm;
        skip($msg,10) if !$vmm;
244

245
#        $vmm->disconnect() if $vmm;
246
        diag("Testing VM $vm_name");
247
248
        my $domain = test_start($vm_name,0);
        $domain = test_start($vm_name,1);
249
#        $domain->_vm->disconnect;
250
        next if !$domain;
251
252
        my $domain_name = $domain->name;
        $domain = undef;
253

254
255
        test_screenshot($vm_name, $domain_name);
        test_screenshot_file($vm_name, $domain_name);
256
257
258
259
    };
}
remove_old_domains();
remove_old_disks();
260

Francesc Guasch's avatar
Francesc Guasch committed
261
262
done_testing();