From 2f98cd72a8ba9350296cfe202b4bd82dd9af0077 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Sat, 23 Nov 2019 12:15:59 +0100 Subject: [PATCH] ProcFSTools: add kernel_version Signed-off-by: Thomas Lamprecht --- src/PVE/ProcFSTools.pm | 17 ++++++++++ test/Makefile | 2 +- test/procfs_tests.pl | 71 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100755 test/procfs_tests.pl diff --git a/src/PVE/ProcFSTools.pm b/src/PVE/ProcFSTools.pm index 14c1d6e..94a2d54 100644 --- a/src/PVE/ProcFSTools.pm +++ b/src/PVE/ProcFSTools.pm @@ -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'); diff --git a/test/Makefile b/test/Makefile index 4467f5b..b8118c7 100644 --- a/test/Makefile +++ b/test/Makefile @@ -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 diff --git a/test/procfs_tests.pl b/test/procfs_tests.pl new file mode 100755 index 0000000..de094ab --- /dev/null +++ b/test/procfs_tests.pl @@ -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(); -- 2.39.2