55_freemem.t 4.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use warnings;
use strict;

use Data::Dumper;
use IPC::Run3;
use POSIX ":sys_wait_h";
use Test::More;
use XML::LibXML;

use lib 't/lib';
use Test::Ravada;
use Sys::Statistics::Linux;

use_ok('Ravada');

Francesc Guasch's avatar
Francesc Guasch committed
16
my $RVD_BACK = rvd_back( );
17

18
my $USER = create_user('foo','bar', 1);
19

20
21
sub test_new_domain {
    my $vm = shift;
22

23
    my $name = new_domain_name();
24

25
    my $freemem = _check_free_memory();
26
    my $domain;
27
    eval { $domain = $vm->create_domain(name => $name
28
                                        , id_iso => search_id_iso('Alpine')
29
                                        ,vm => $vm->type
30
                                        ,id_owner => $USER->id
31
                                        ,memory => 4*1024*1024
32
                                        ,disk => 1 * 1024*1024
33
34
            ) 
    };
35
36
37
38
    if ($freemem < 1 ) {
        ok($@,"Expecting failed because we ran out of free RAM");
        return;
    }
39
40
41
    ok(!$@,"Domain $name not created: $@");

    ok($domain,"Domain not created") or return;
42
43
44
45
46
47
48
49
    eval { $domain->start($USER); sleep 1; };

    if ($freemem < 1 || $@ =~ /free memory/) {
        ok($@,"Expecting failed start because we ran out of free RAM ($freemem MB Free)");
        return;
    }
    ok(!$@,"Expected start domain with $freemem MB Free $@");

50
    
51
52
53
54
    #Ckeck free memory
    
    #virsh setmaxmem $name xG --config
    #virsh setmem $name xG --config
55
56
57
58

    return $domain;
}

Francesc Guasch's avatar
Francesc Guasch committed
59
60
61
62
63
64
65
66
67
sub test_new_domain_req {
    my $vm = shift;

    my $base;
    eval { $base= $vm->create_domain(name => new_domain_name()
                                        , id_iso => search_id_iso('Alpine')
                                        ,vm => $vm->type
                                        ,id_owner => $USER->id
                                        ,memory => (_check_free_memory() * 2) * 1024 * 1024
68
                                        ,disk => 1 * 1024*1024
Francesc Guasch's avatar
Francesc Guasch committed
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
95
96
97
98
99
100
101
102
103
104
            )
    };
    is(''.$@,'') or return;
    $base->prepare_base(user_admin);
    my $name = new_domain_name();
    my $req = Ravada::Request->create_domain(
        name => $name
        ,id_base => $base->id
        ,id_owner => user_admin->id
        ,remote_ip => '127.0.0.1'
        ,start => 1
    );
    ok($req) or return;
    rvd_back->_process_requests_dont_fork();

    ok($req->status eq 'done');
    like($req->error,qr(.));

    my $domain = rvd_back->search_domain($name);
    ok($domain) or return;

    my $req_start = Ravada::Request->start_domain(
        uid => user_admin->id
        ,id_domain => $domain->id
        ,remote_ip => '127.0.0.1'
    );
    rvd_back->_process_requests_dont_fork();
    ok($req_start->status, 'done');
    is($domain->is_active, 0 );
    like($req_start->error,qr(.));

    $domain->remove(user_admin);
    $base->remove(user_admin);

}

105
106
107
sub _check_free_memory{
    my $lxs  = Sys::Statistics::Linux->new( memstats => 1 );
    my $stat = $lxs->get;
108
109
    my $freemem = $stat->memstats->{realfree};
    #die "No free memory" if ( $stat->memstats->{realfree} < 500000 );
110
111
112
113
    my $free = int( $freemem / 1024 );
    $free = $free / 1024;
    $free =~ s/(\d+\.\d+)/$1/;
    return $free;
114
115
116
117
118
119
120
}



################################################################
my $vm;

121
122
remove_old_domains();
remove_old_disks();
123

Francesc Guasch's avatar
Francesc Guasch committed
124
for my $vm_name (vm_names()) {
125
SKIP: {
Francesc Guasch's avatar
Francesc Guasch committed
126
127
    my $msg = "SKIPPED test: No $vm_name backend found";
    my $vm = $RVD_BACK->search_vm($vm_name);
128
129
    #    $msg = "SKIPPED: todo review overcommitting issue #1164";
    #$vm = undef;
130

Francesc Guasch's avatar
Francesc Guasch committed
131
132
133
134
    if ($vm_name eq 'KVM' && $>) {
        $msg = "SKIPPED test: $vm_name must be run from root";
        $vm = undef;
    }
135
136
137
    diag($msg)      if !$vm;
    skip $msg,10    if !$vm;

Francesc Guasch's avatar
Francesc Guasch committed
138
    use_ok("Ravada::Domain::$vm_name");
139

140
    my $freemem = _check_free_memory();
141
    my $n_domains = int($freemem)*2+2;
142

Francesc Guasch's avatar
Francesc Guasch committed
143
    if ($n_domains > 50 ) {
144
        my $msg = "Skipped freemem check, too many memory in this host";
145
146
        diag($msg);
        skip($msg,10);
147
        next;
148
149
    }

150
151
    $freemem =~ s/(\d+\.\d)\d+/$1/;

152
    diag("Checking it won't start more than $n_domains domains with $freemem free memory");
153

154
    my @domains;
155
    for ( 0 .. $n_domains ) {
156
#        diag("Creating domain $_");
157
158
        my $domain = test_new_domain($vm) or last;
        push @domains,($domain) if $domain;
159
    }
160

Francesc Guasch's avatar
Francesc Guasch committed
161
    test_new_domain_req($vm) if $vm_name ne 'Void';
162
163
    for (@domains) {
        $_->shutdown_now($USER);
164
165
    }
    for (@domains) {
166
167
        $_->remove($USER);
    }
168
};
Francesc Guasch's avatar
Francesc Guasch committed
169
}
170

171
end();
172

173
done_testing();