35_request_start.t 3.4 KB
Newer Older
Francesc Guasch's avatar
Francesc Guasch committed
1
2
3
4
5
6
7
8
use warnings;
use strict;

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

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

11
use Test::Ravada;
Francesc Guasch's avatar
Francesc Guasch committed
12

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

15
my $RAVADA = rvd_back($test->connector, 't/etc/ravada.conf');
Francesc Guasch's avatar
Francesc Guasch committed
16

17
18
my @ARG_CREATE_DOM;

Francesc Guasch's avatar
Francesc Guasch committed
19
20
21
22
23
24
25
sub test_request_start {
}

sub test_remove_domain {
    my $name = shift;

    my $domain = $name if ref($name);
26
    $domain = $RAVADA->search_domain($name,1);
Francesc Guasch's avatar
Francesc Guasch committed
27

28
29
    my $disks_not_removed = 0;

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

        ok(! -e $domain->file_base_img ,"Image file was not removed "
                    . $domain->file_base_img )
                if  $domain->file_base_img;
41
42
43
        for (@disks) {
            ok(!-e $_,"Disk $_ should be removed") or $disks_not_removed++;
        }
Francesc Guasch's avatar
Francesc Guasch committed
44
45

    }
46
    $domain = $RAVADA->search_domain($name,1);
Francesc Guasch's avatar
Francesc Guasch committed
47
    ok(!$domain, "I can't remove old domain $name") or exit;
48
    ok(!$disks_not_removed,"$disks_not_removed disks not removed from domain $name");
Francesc Guasch's avatar
Francesc Guasch committed
49
50
51
52
53
54
55
56
}

sub test_new_domain {
    my $name = shift;

    test_remove_domain($name);

    diag("Creating domain $name");
57
    my $domain = $RAVADA->create_domain(name => $name, @ARG_CREATE_DOM, active => 0);
Francesc Guasch's avatar
Francesc Guasch committed
58
59
60
61
62
63
64
65

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

    return $domain;
}


sub test_start {
66
    my $name = new_domain_name();
Francesc Guasch's avatar
Francesc Guasch committed
67
68
69
70
71
72
    test_remove_domain($name);


    my $req = Ravada::Request->start_domain(
        "does not exists"
    );
73
    $RAVADA->_process_requests_dont_fork();
Francesc Guasch's avatar
Francesc Guasch committed
74

75
    ok($req->status eq 'done', "Req ".$req->{id}." expecting status done, got ".$req->status);
Francesc Guasch's avatar
Francesc Guasch committed
76
    ok($req->error && $req->error =~ /unknown/i
77
78
            ,"Req ".$req->{id}." expecting unknown domain error , got "
                .($req->error or '<NULL>')) or return;
Francesc Guasch's avatar
Francesc Guasch committed
79
80
    $req = undef;

Francesc Guasch's avatar
Francesc Guasch committed
81
82
83
    #####################################################################3
    #
    # start
84
85
86
87
    test_new_domain($name);

    my $domain = $RAVADA->search_domain($name);
    ok(!$domain->is_active,"Domain $name should be inactive") or return;
Francesc Guasch's avatar
Francesc Guasch committed
88

Francesc Guasch's avatar
Francesc Guasch committed
89
    my $req2 = Ravada::Request->start_domain($name);
90
    $RAVADA->process_requests();
Francesc Guasch's avatar
Francesc Guasch committed
91
92
93
94
95

    ok($req2->status eq 'done');

    ok($domain->is_active);

96
    my $domain2 = $RAVADA->search_domain($name);
Francesc Guasch's avatar
Francesc Guasch committed
97
98
    ok($domain2->is_active);

Francesc Guasch's avatar
Francesc Guasch committed
99
100
101
102
103
104
105
    $req2 = undef;

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

    my $req3 = Ravada::Request->shutdown_domain($name);
106
    $RAVADA->process_requests();
Francesc Guasch's avatar
Francesc Guasch committed
107
108
109
110
    ok($req3->status eq 'done');

    ok(!$domain->is_active);

111
    my $domain3 = $RAVADA->search_domain($name);
Francesc Guasch's avatar
Francesc Guasch committed
112
113
    ok(!$domain3->is_active);

114
115
    return $domain3;

Francesc Guasch's avatar
Francesc Guasch committed
116
117
118
119
120
}

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

121
122
my $vmm;

Francesc Guasch's avatar
Francesc Guasch committed
123
eval { 
124
    $vmm = $RAVADA->search_vm('kvm');
125
    @ARG_CREATE_DOM = ( id_iso => 1, vm => 'kvm', id_owner => 1 )  if $vmm;
126
127
128

    if (!$vmm) {
        $vmm = $RAVADA->search_vm('lxc');
129
        @ARG_CREATE_DOM = ( id_template => 1, vm => 'LXC', id_owner => 1 );
130
131
    }

132
} if $RAVADA;
133
134
135
136
137
138

SKIP: {
    my $msg = "SKIPPED: No virtual managers found";
    diag($msg) if !$vmm;
    skip($msg,10) if !$vmm;

139
    remove_old_domains();
Francesc Guasch's avatar
Francesc Guasch committed
140
    remove_old_disks();
141
142
    my $domain = test_start();

143
    $domain->shutdown_now() if $domain;
144
    $domain->remove(user_admin())       if $domain;
145
};
Francesc Guasch's avatar
Francesc Guasch committed
146
147
done_testing();