forked from UPC/ravada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path55_freemem.t
169 lines (133 loc) · 4.25 KB
/
55_freemem.t
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
45
46
47
48
49
50
51
52
53
54
55
56
57
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
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
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
162
163
164
165
166
167
168
169
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');
my $RVD_BACK = rvd_back( );
my $USER = create_user('foo','bar', 1);
sub test_new_domain {
my $vm = shift;
my $name = new_domain_name();
my $freemem = _check_free_memory();
my $domain;
eval { $domain = $vm->create_domain(name => $name
, id_iso => search_id_iso('Alpine')
,vm => $vm->type
,id_owner => $USER->id
,memory => 4*1024*1024
,disk => 1 * 1024*1024
)
};
if ($freemem < 1 ) {
ok($@,"Expecting failed because we ran out of free RAM");
return;
}
ok(!$@,"Domain $name not created: $@");
ok($domain,"Domain not created") or return;
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 $@");
#Ckeck free memory
#virsh setmaxmem $name xG --config
#virsh setmem $name xG --config
return $domain;
}
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
,disk => 1 * 1024*1024
)
};
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'
);
ok($req) or return;
wait_request($req);
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);
}
sub _check_free_memory{
my $lxs = Sys::Statistics::Linux->new( memstats => 1 );
my $stat = $lxs->get;
my $freemem = $stat->memstats->{realfree};
#die "No free memory" if ( $stat->memstats->{realfree} < 500000 );
my $free = int( $freemem / 1024 );
$free = $free / 1024;
$free =~ s/(\d+\.\d+)/$1/;
return $free;
}
################################################################
my $vm;
remove_old_domains();
remove_old_disks();
for my $vm_name (vm_names()) {
SKIP: {
my $msg = "SKIPPED test: No $vm_name backend found";
my $vm = $RVD_BACK->search_vm($vm_name);
# $msg = "SKIPPED: todo review overcommitting issue #1164";
#$vm = undef;
if ($vm_name eq 'KVM' && $>) {
$msg = "SKIPPED test: $vm_name must be run from root";
$vm = undef;
}
diag($msg) if !$vm;
skip $msg,10 if !$vm;
use_ok("Ravada::Domain::$vm_name");
my $freemem = _check_free_memory();
my $n_domains = int($freemem)*2+2;
if ($n_domains > 50 ) {
my $msg = "Skipped freemem check, too many memory in this host";
diag($msg);
skip($msg,10);
next;
}
$freemem =~ s/(\d+\.\d)\d+/$1/;
diag("Checking it won't start more than $n_domains domains with $freemem free memory");
my @domains;
for ( 0 .. $n_domains ) {
# diag("Creating domain $_");
my $domain = test_new_domain($vm) or last;
push @domains,($domain) if $domain;
}
test_new_domain_req($vm) if $vm_name ne 'Void';
for (@domains) {
$_->shutdown_now($USER);
}
for (@domains) {
$_->remove($USER);
}
};
}
end();
done_testing();