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
|
use strict;
use warnings;
use Test::More;
# Mocking variables from mysqltuner.pl
our %opt;
our $physical_memory;
our $swap_memory;
sub infoprint { } # Silence output
sub badprint { }
sub debugprint { }
sub hr_bytes {
my $num = shift;
return "0B" unless defined($num);
if ( $num >= ( 1024**3 ) ) { return sprintf( "%.1f", ( $num / ( 1024**3 ) ) ) . "G"; }
elsif ( $num >= ( 1024**2 ) ) { return sprintf( "%.1f", ( $num / ( 1024**2 ) ) ) . "M"; }
elsif ( $num >= 1024 ) { return sprintf( "%.1f", ( $num / 1024 ) ) . "K"; }
else { return $num . "B"; }
}
sub os_setup_logic {
my ($os, $opt_forcemem, $opt_forceswap) = @_;
# Logic from mysqltuner.pl
if ( $opt_forcemem > 0 ) {
$physical_memory = $opt_forcemem * 1048576;
if ( $opt_forceswap > 0 ) {
$swap_memory = $opt_forceswap * 1048576;
} else {
$swap_memory = 0;
}
} else {
# Mocked system detection
$physical_memory = 2048 * 1048576; # 2GB
$swap_memory = 1024 * 1048576; # 1GB
}
# Regression check for v2.6.1 bug:
# $physical_memory = $opt_forcemem if ($opt_forcemem > 0);
# This line IS DELETED in current version.
return ($physical_memory, $swap_memory);
}
# Test cases
my ($mem, $swap);
# 32GB requested via forcemem
($mem, $swap) = os_setup_logic("Linux", 32768, 0);
is($mem, 34359738368, "32768 MB should be 34359738368 bytes");
is(hr_bytes($mem), "32.0G", "32768 MB should be displayed as 32.0G");
# 1GB requested via forcemem
($mem, $swap) = os_setup_logic("Linux", 1024, 0);
is($mem, 1073741824, "1024 MB should be 1073741824 bytes");
is(hr_bytes($mem), "1.0G", "1024 MB should be displayed as 1.0G");
# 2000 MB requested (issue #780)
($mem, $swap) = os_setup_logic("Linux", 2000, 0);
is($mem, 2097152000, "2000 MB should be 2097152000 bytes");
is(hr_bytes($mem), "2.0G", "2000 MB should be displayed as 2.0G (not 2.0K)");
done_testing();
|