forked from UPC/ravada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
55_freemem.t
125 lines (96 loc) · 2.97 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
use warnings;
use strict;
use Data::Dumper;
use IPC::Run3;
use POSIX ":sys_wait_h";
use Test::More;
use Test::SQL::Data;
use XML::LibXML;
use lib 't/lib';
use Test::Ravada;
use Sys::Statistics::Linux;
use_ok('Ravada');
my $test = Test::SQL::Data->new( config => 't/etc/sql.conf');
my $RVD_BACK = rvd_back( $test->connector , 't/etc/ravada.conf');
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 => 1.5*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 _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);
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;
if ($n_domains > 5 ) {
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;
}
for (@domains) {
$_->shutdown_now($USER);
}
for (@domains) {
$_->remove($USER);
}
};
}
remove_old_domains();
remove_old_disks();
done_testing();