5
0
mirror of git://git.proxmox.com/git/pve-common.git synced 2025-01-03 09:17:36 +03:00

ProcFSTools: add kernel_version

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2019-11-23 12:15:59 +01:00
parent 510f865b5c
commit 2f98cd72a8
3 changed files with 89 additions and 1 deletions

View File

@ -80,6 +80,23 @@ sub read_proc_uptime {
return (0, 0);
}
sub kernel_version {
my $line = PVE::Tools::file_read_firstline("/proc/version");
if ($line && $line =~ m|^Linux\sversion\s((\d+(?:\.\d+)+)-?(\S+)?)|) {
my ($fullversion, $version_numbers, $extra) = ($1, $2, $3);
# variable names are the one from the Linux kernel Makefile
my ($version, $patchlevel, $sublevel) = split(/\./, $version_numbers);
return wantarray
? (int($version), int($patchlevel), int($sublevel), $extra, $fullversion)
: $fullversion;
}
return (0, 0, 0, '', '');
}
sub read_loadavg {
my $line = PVE::Tools::file_read_firstline('/proc/loadavg');

View File

@ -6,7 +6,7 @@ all:
export PERLLIB=../src
check: lock_file.test calendar_event_test.test convert_size_test.test
check: lock_file.test calendar_event_test.test convert_size_test.test procfs_tests.test
for d in $(SUBDIRS); do $(MAKE) -C $$d check; done
%.test: %.pl

71
test/procfs_tests.pl Executable file
View File

@ -0,0 +1,71 @@
#!/usr/bin/perl
use strict;
use warnings;
use lib '../src';
use Test::More;
use Test::MockModule;
use PVE::Tools;
use PVE::ProcFSTools;
# the proc "state"
my $proc = {
version => '',
};
my $pve_common_tools;
$pve_common_tools = Test::MockModule->new('PVE::Tools');
$pve_common_tools->mock(
file_read_firstline => sub {
my ($filename) = @_;
$filename =~ s!^/proc/!!;
my $res = $proc->{$filename};
if (ref($res) eq 'CODE') {
$res = $res->();
}
chomp $res;
return $res;
},
);
# version tests
my @kernel_versions = (
{
version => 'Linux version 5.3.10-1-pve (build@pve) (gcc version 8.3.0 (Debian 8.3.0-6)) #1 SMP PVE 5.3.10-1 (Thu, 14 Nov 2019 10:43:13 +0100)',
expect => [5, 3, 10, '1-pve', '5.3.10-1-pve'],
},
{
version => 'Linux version 5.0.21-5-pve (build@pve) (gcc version 8.3.0 (Debian 8.3.0-6)) #1 SMP PVE 5.0.21-10 (Wed, 13 Nov 2019 08:27:10 +0100)',
expect => [5, 0, 21, '5-pve', '5.0.21-5-pve'],
},
{
version => 'Linux version 5.0.21+ (build@pve) (gcc version 8.3.0 (Debian 8.3.0-6)) #27 SMP Tue Nov 12 10:30:36 CET 2019',
expect => [5, 0, 21, '+', '5.0.21+'],
},
{
version => 'Linu$ version 2 (build@pve) (gcc version 8.3.0 (Debian 8.3.0-6)) #27 SMP Tue Nov 12 10:30:36 CET 2019',
expect => [0, 0, 0, '', ''],
},
);
subtest 'test kernel_version parser' => sub {
for my $test (@kernel_versions) {
$proc->{version} = $test->{version};
my $res = [ PVE::ProcFSTools::kernel_version() ];
is_deeply($res, $test->{expect}, "got verison <". $res->[4] ."> same as expected");
}
};
done_testing();