diff mbox series

[v4,2/2] discover/grub2: Add the '-e' test support

Message ID 20210121184437.5613-2-klaus@linux.vnet.ibm.com
State New
Headers show
Series [v4,1/2] discover: Allow for empty paths | expand

Commit Message

Klaus Heinrich Kiwi Jan. 21, 2021, 6:44 p.m. UTC
Grub2 allows a special-case of file test using the '-e' operator, where
the path can be empty, and the device existance is tested.
E.g.:
  if [ -e (md/md-boot) ]; then

Add the support for testing this condition, as well as some tests for
this scenario (also fix an existing testcase where this was wrongly
passing).

This fixes the following RH CoreOS bug:
  https://bugzilla.redhat.com/show_bug.cgi?id=1915540

Signed-off-by: Klaus Heinrich Kiwi <klaus@linux.vnet.ibm.com>
---
 discover/grub2/builtins.c                 |  7 +++++-
 test/parser/test-grub2-test-file-ops.c    | 27 +++++++++++++++++++++++
 test/parser/test-grub2-ubuntu-13_04-x86.c |  4 ++--
 3 files changed, 35 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/discover/grub2/builtins.c b/discover/grub2/builtins.c
index f32e8e5..ff28586 100644
--- a/discover/grub2/builtins.c
+++ b/discover/grub2/builtins.c
@@ -245,6 +245,11 @@  static bool builtin_test_op_file(struct grub2_script *script, char op,
 		return false;
 
 	switch (op) {
+	case 'e':
+		/* -e: for grub, a special case is testing for the device
+		 * presence itself (e.g. allows null file). */
+		result = true;
+		break;
 	case 's':
 		/* -s: return true if file exists and has non-zero size */
 		result = !path ? false : statbuf.st_size > 0;
@@ -336,7 +341,7 @@  static bool builtin_test_op(struct grub2_script *script,
 			return strlen(a1) != 0;
 		}
 
-		if (!strcmp(op, "-s") || !strcmp(op, "-f")) {
+		if (!strcmp(op, "-s") || !strcmp(op, "-f") || !(strcmp(op, "-e"))) {
 			*consumed = 2;
 			return builtin_test_op_file(script, op[1], a1);
 		}
diff --git a/test/parser/test-grub2-test-file-ops.c b/test/parser/test-grub2-test-file-ops.c
index 38214a1..94ee333 100644
--- a/test/parser/test-grub2-test-file-ops.c
+++ b/test/parser/test-grub2-test-file-ops.c
@@ -55,6 +55,28 @@  if [ ! -d / -a $status = success ]
 then status=fail_d_5
 fi
 
+if [ -e /file_that_does_not_exist -a $status = success ]
+then status=fail_e_1
+fi
+if [ ! -e /dir -a $status = success ]
+then status=fail_e_2
+fi
+if [ ! -e /empty_file -a $status = success ]
+then status=fail_e_3
+fi
+if [ -e "" -a $status = success ]
+then status=fail_e_4
+fi
+if [ ! -e / -a $status = success ]
+then status=fail_e_5
+fi
+if [ ! -e (00000000-0000-0000-0000-000000000001) -a $status = success ]
+then status=fail_e_6
+fi
+if [ -e (00000000-0000-0000-0000-000000000002) -a $status = success ]
+then status=fail_e_7
+fi
+
 menuentry $status {
   linux /vmlinux
 }
@@ -64,9 +86,14 @@  void run_test(struct parser_test *test)
 {
 	struct discover_boot_option *opt;
 	struct discover_context *ctx;
+	struct discover_device *dev;
 
 	ctx = test->ctx;
 
+	/* set local uuid */
+	dev = test->ctx->device;
+	dev->uuid = "00000000-0000-0000-0000-000000000001";
+
 	test_read_conf_embedded(test, "/grub2/grub.cfg");
 	test_add_dir(test, ctx->device, "/");
 	test_add_file_data(test, ctx->device, "/empty_file", "", 0);
diff --git a/test/parser/test-grub2-ubuntu-13_04-x86.c b/test/parser/test-grub2-ubuntu-13_04-x86.c
index 2f9aefd..785781a 100644
--- a/test/parser/test-grub2-ubuntu-13_04-x86.c
+++ b/test/parser/test-grub2-ubuntu-13_04-x86.c
@@ -19,13 +19,13 @@  void run_test(struct parser_test *test)
 	check_unresolved_resource(opt->boot_image);
 	check_unresolved_resource(opt->initrd);
 	check_name(opt, "Kubuntu GNU/Linux");
-	check_args(opt, "root=UUID=29beca39-9181-4780-bbb2-ab5d4be59aaf ro quiet splash ");
+	check_args(opt, "root=UUID=29beca39-9181-4780-bbb2-ab5d4be59aaf ro quiet splash vt.handoff=7");
 
 	opt = get_boot_option(ctx, 1);
 	check_unresolved_resource(opt->boot_image);
 	check_unresolved_resource(opt->initrd);
 	check_name(opt, "Kubuntu GNU/Linux, with Linux 3.8.0-19-generic");
-	check_args(opt, "root=UUID=29beca39-9181-4780-bbb2-ab5d4be59aaf ro quiet splash ");
+	check_args(opt, "root=UUID=29beca39-9181-4780-bbb2-ab5d4be59aaf ro quiet splash vt.handoff=7");
 
 	opt = get_boot_option(ctx, 2);
 	check_name(opt, "Kubuntu GNU/Linux, with Linux 3.8.0-19-generic (recovery mode)");