diff mbox series

[v2,1/2] selftests: Introduce script to validate required configs

Message ID 20241220193536.13781-2-simeddon@gmail.com (mailing list archive)
State New
Headers show
Series update kselftest framework to check for required configs | expand

Commit Message

Siddharth Menon Dec. 20, 2024, 7:35 p.m. UTC
This patch adds a script to validate that the current kernel configuration
satisfies the requirements for selftests. The script compares the current
kernel configs against the required selftest configs.

A config mismatch exits with error value 1 while matching configs or missing
config files exit with value 0.In order to get debug output, set the
environment variable LOCALMODCONFIG_DEBUG=1.

The code for extracting the current kernel configs is adapted from
scripts/kconfig/streamline_config.pl.

Suggested-by: Petr Mladek <pmladek@suse.com>
Suggested-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Siddharth Menon <simeddon@gmail.com>
---
 v1->v2: created this perl script instead of checking directly in lib.mk
 tools/testing/selftests/mktest.pl | 138 ++++++++++++++++++++++++++++++
 1 file changed, 138 insertions(+)
 create mode 100755 tools/testing/selftests/mktest.pl
diff mbox series

Patch

diff --git a/tools/testing/selftests/mktest.pl b/tools/testing/selftests/mktest.pl
new file mode 100755
index 000000000000..60462f323bde
--- /dev/null
+++ b/tools/testing/selftests/mktest.pl
@@ -0,0 +1,138 @@ 
+#!/usr/bin/env perl
+# SPDX-License-Identifier: GPL-2.0
+use warnings;
+use strict;
+use Getopt::Long;
+use File::Spec;
+
+# set the environment variable LOCALMODCONFIG_DEBUG to get
+# debug output.
+my $debugprint = 0;
+$debugprint = 1 if (defined($ENV{LOCALMODCONFIG_DEBUG}));
+
+sub dprint {
+    return if (!$debugprint);
+    print STDERR @_;
+}
+
+my $uname = `uname -r`;
+chomp $uname;
+
+my @searchconfigs = (
+    {
+        "file" => ".config",
+        "exec" => "cat",
+    },
+    {
+        "file" => "/proc/config.gz",
+        "exec" => "zcat",
+    },
+    {
+        "file" => "/boot/config-$uname",
+        "exec" => "cat",
+    },
+    {
+        "file" => "/boot/vmlinuz-$uname",
+        "exec" => "scripts/extract-ikconfig",
+        "test" => "scripts/extract-ikconfig",
+    },
+    {
+        "file" => "vmlinux",
+        "exec" => "scripts/extract-ikconfig",
+        "test" => "scripts/extract-ikconfig",
+    },
+    {
+        "file" => "/lib/modules/$uname/kernel/kernel/configs.ko",
+        "exec" => "scripts/extract-ikconfig",
+        "test" => "scripts/extract-ikconfig",
+    },
+    {
+        "file" => "/lib/modules/$uname/build/.config",
+        "exec" => "cat",
+    },
+    {
+        "file" => "kernel/configs.ko",
+        "exec" => "scripts/extract-ikconfig",
+        "test" => "scripts/extract-ikconfig",
+    },
+    {
+        "file" => "kernel/configs.o",
+        "exec" => "scripts/extract-ikconfig",
+        "test" => "scripts/extract-ikconfig",
+    },
+);
+
+sub read_config {
+    foreach my $conf (@searchconfigs) {
+    my $file = $conf->{"file"};
+
+    next if (! -f "$file");
+    
+    if (defined($conf->{"test"})) {
+        `$conf->{"test"} $conf->{"file"} 2>/dev/null`;
+        next if ($?);
+    }
+    
+    my $exec = $conf->{"exec"};
+    
+    dprint "Kernel config: '$file'\n";
+    
+    open(my $infile, '-|', "$exec $file") || die "Failed to run $exec $file";
+    my @x = <$infile>;
+    close $infile;
+    return @x;
+    }
+    dprint "Unable to find kernel config file, skipping check\n";
+    exit 0;
+}
+
+# Check if selftest path is provided
+die "Usage: $0 <selftest_path>\n" unless @ARGV == 1;
+
+my $file_path = $ARGV[0];
+
+my @config_file = read_config();
+
+my %kern_configs;
+my $valid = "A-Za-z_0-9";
+foreach my $line (@config_file) {
+    chomp $line;
+    next if $line =~ /^\s*$/ || $line =~ /^#/;
+    
+    if ($line =~ /^(CONFIG_\w+)=(.+)$/) {
+        $kern_configs{$1} = $2;
+    }
+}
+
+my %test_configs;
+# Continue as normal if /config file does not exist 
+open(my $fh, '<', $file_path) or exit 0;
+
+while (my $line = <$fh>) {
+    chomp $line;
+    next if $line =~ /^\s*$/ || $line =~ /^#/;
+    
+    if ($line =~ /^(CONFIG_\w+)=(.+)$/) {
+        $test_configs{$1} = $2;
+    }
+}
+close $fh;
+
+# Check if all selftest configs match kernel config
+my $all_match = 1;
+my @missing_or_mismatched;
+
+foreach my $key (keys %test_configs) {
+    if (!exists $kern_configs{$key} || $kern_configs{$key} ne $test_configs{$key}) {
+        push @missing_or_mismatched, "Required: $key=$test_configs{$key}";
+        $all_match = 0;
+    }
+}
+
+
+if ($all_match) {
+    exit 0;
+} else {
+    dprint "$_\n" for @missing_or_mismatched;
+    exit 1;
+}