Skip to contentSkip to conversation

Basic unit testing support for GRUB

RFC PATCH In review BVK 3 participants 7 messages Nov 13, 2009 – Dec 4, 2009 Kbuild

lore
Share & tools
lore

BVK

author
#0 lore raw

RFC PATCH 1/1 Basic unit testing support for GRUB

Hi,

Attached is the (experimental branch) patch for unit testing that I have in mind for GRUB. Please let me know your comments.

It provides "make check" target which would build and execute test programs from "tests" directory. We can write small programs to test specific functions in GRUB. For example, attached patch has sample program comparing grub_sprintf behavior against standard sprintf output (from glibc).

Note that it currently has few gotchas, like using TARGET_CC instead of BUILD_CC, no support for XPASS, XFAIL tests, etc.

regards,

Signature · 1 line
bvk.chaitanya

=== modified file 'Makefile.in'

Makefile.in +13 -1
@@ -125,6 +125,7 @@
 SCRIPTS = $(bin_SCRIPTS) $(sbin_SCRIPTS) $(grub-mkconfig_SCRIPTS) \
 	$(lib_SCRIPTS)
 INFOS = $(info_INFOS)
+TESTS = $(check_PROGRAMS) $(check_SCRIPTS)
 
 CLEANFILES =
 MOSTLYCLEANFILES =
@@ -151,6 +152,9 @@
 include $(srcdir)/conf/$(target_cpu)-$(platform).mk
 endif
 
+# for tests
+include $(srcdir)/conf/tests.mk
+
 # For external modules.
 -include $(wildcard $(GRUB_CONTRIB)/*/conf/common.mk)
 
@@ -428,7 +432,15 @@
 	@echo "$(distdir).tar.gz is ready for distribution" | \
 	  sed 'h;s/./=/g;p;x;p;x'
 
-check:
+check: $(TESTS)
+	@list='$(TESTS)'; \
+	for file in $$list; do \
+	  if $(builddir)/$$file; then \
+	    echo "$$file: PASS"; \
+	  else \
+	    echo "$$file: FAIL"; \
+	  fi; \
+	done
 
 .SUFFIX:
 .SUFFIX: .c .o .S .d

=== added file 'conf/tests.rmk'

conf/tests.rmk +6 -0
@@ -0,0 +1,6 @@
+# -*- makefile -*-
+
+check_PROGRAMS += grub_sprintf
+grub_sprintf_SOURCES = tests/grub_sprintf.c tests/misc.c kern/misc.c
+grub_sprintf_CFLAGS = -Wno-error -Wno-format
+

=== added directory 'tests'
=== added file 'tests/grub_sprintf.c'

tests/grub_sprintf.c +116 -0

116 lines not on this page · raw message

=== added file 'tests/misc.c'

tests/misc.c +56 -0

56 lines not on this page · raw message

=== added file 'tests/test.h'

tests/test.h +22 -0
@@ -0,0 +1,22 @@
+#ifndef GRUB_TEST_HEADER
+#define GRUB_TEST_HEADER
+
+#include <grub/err.h>
+#include <grub/types.h>
+#include <grub/handler.h>
+
+// Below functions are necessary for successfully linking tests.  We
+// define dummy versions to keep the linker from complaining.
+
+char *     grub_env_get (const char *name __attribute__ ((unused)));
+grub_err_t grub_error   (grub_err_t n, const char *fmt, ...);
+void *     grub_malloc  (grub_size_t size);
+void       grub_refresh (void);
+void       grub_putchar (int c);
+int        grub_getkey  (void);
+void       grub_exit    (void);
+
+extern struct grub_handler_class grub_term_input_class;
+extern struct grub_handler_class grub_term_output_class;
+
+#endif /* ! GRUB_TEST_HEADER */
#1 lore raw
Quoted 6 lines · BVK

Hi,

Attached is the (experimental branch) patch for unit testing that I have in mind for GRUB. Please let me know your comments.

Quoted 8 lines

It provides "make check" target which would build and execute test programs from "tests" directory. We can write small programs to test specific functions in GRUB. For example, attached patch has sample program comparing grub_sprintf behavior against standard sprintf output (from glibc).

Note that it currently has few gotchas, like using TARGET_CC instead of BUILD_CC, no support for XPASS, XFAIL tests, etc.

Some parts don't follow our coding style; please check with indent(1) utility, it should produce the right indentation. Also, we use C-style /**/ syntax for comments.

Ah, and could you include a ChangeLog entry?

Thank you!

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
author
#2 lore raw

Hi,

I have been busy last week, so I couldn't reply :-(

But I am able to complete most of the framework. It has support for logging multiple testcase failures and reporting them at the end of test execution. Please go through the attached patch and let me know your suggestions.

Attached framework has support for two kinds of tests.

1. Unit Tests
2. Functional Tests

Unit tests are test programs that doesn't need an active GRUB instance for their operation. These programs target specific code of GRUB, like grub_printf logic, etc. These are run during the build time by "make check" command. Since these are normal programs, they can link with standard C library and others.

Functional tests are for testing GRUB functionality that needs an active instance of GRUB (for example, testing serial console code, partition tables logic, biosdisk access, etc.). Every functional test is a GRUB module that registers itself when its loaded. These are run using special GRUB command "functional_test" in GRUB shell.

Attached patch has example tests for both. An additional grub_sprintf unit test is also available which tests grub_sprintf function logic and gets executed as part of "make check".

Since functional tests are grub modules, we either need to boot into GRUB to run them or can make use of i386-qemu port of GRUB. Below is how I currently tested functional tests using i386-port

$ ./autogen.sh
$ ./configure --prefix=/usr --with-platform=qemu
$ make && make check
$ ./grub-mkimage -d . -o grub.bios at_keyboard normal help halt
functional_test example_functional_test
$ qemu -bios ./grub.bios -hda /dev/zero

rescue:grub> functional_test example_functional_test
...
rescue:grub> halt

As for the future framework related enhancements, I am thinking of developing a grub-test script which would run functional tests directly inside a qemu using serial console -- as part of the build process. Then we can have test scripts using grub-test as its interpreter, which would remove the difference between functional tests and unit tests.

thanks,

Signature · 1 line
bvk.chaitanya

=== modified file 'ChangeLog'

ChangeLog +13 -0
@@ -1,3 +1,16 @@
+2009-11-23  BVK Chaitanya  <[email protected]>
+
+	Regression testing framework for GRUB.
+
+	* Makefile.in: Added rules for make check target.
+	* include/grub/test.h: New file.  Header for testing framework.
+	* tests/test.c: Common definitions for the framework.
+	* tests/functional_test.c: Definitions for functional tests.
+	* tests/unit_test.c: Definitions for unit tests.
+	* tests/example_unit_test.c: Example test.
+	* tests/example_functional_test.c: Example test.
+	* tests/grub_sprintf.c: Unit test for grub_sprintf.
+
 2009-11-09  Robert Millan  <[email protected]>
 
 	Import from Gnulib.

=== modified file 'Makefile.in'

Makefile.in +13 -1
@@ -125,6 +125,7 @@
 SCRIPTS = $(bin_SCRIPTS) $(sbin_SCRIPTS) $(grub-mkconfig_SCRIPTS) \
 	$(lib_SCRIPTS)
 INFOS = $(info_INFOS)
+TESTS = $(check_PROGRAMS) $(check_SCRIPTS) $(check_MODULES)
 
 CLEANFILES =
 MOSTLYCLEANFILES =
@@ -151,6 +152,9 @@
 include $(srcdir)/conf/$(target_cpu)-$(platform).mk
 endif
 
+# for tests
+include $(srcdir)/conf/tests.mk
+
 # For external modules.
 -include $(wildcard $(GRUB_CONTRIB)/*/conf/common.mk)
 
@@ -428,7 +432,15 @@
 	@echo "$(distdir).tar.gz is ready for distribution" | \
 	  sed 'h;s/./=/g;p;x;p;x'
 
-check:
+check: $(TESTS)
+	@list="$(check_PROGRAMS) $(check_SCRIPTS)"; \
+	for file in $$list; do \
+	  if $(builddir)/$$file; then \
+	    echo "$$file: PASS"; \
+	  else \
+	    echo "$$file: FAIL"; \
+	  fi; \
+	done
 
 .SUFFIX:
 .SUFFIX: .c .o .S .d

=== added file 'conf/tests.rmk'

tests.rmk +26 -0
@@ -0,0 +1,26 @@
+# -*- makefile -*-
+
+check_MODULES += functional_test.mod
+functional_test_mod_SOURCES = tests/functional_test.c tests/test.c
+functional_test_mod_CFLAGS  = $(COMMON_CFLAGS)
+functional_test_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
+# Unit tests
+
+check_PROGRAMS += example_unit_test
+example_unit_test_SOURCES = tests/example_unit_test.c kern/list.c kern/misc.c tests/test.c tests/unit_test.c
+example_unit_test_CFLAGS  = -Wno-format
+
+check_PROGRAMS += grub_sprintf
+grub_sprintf_SOURCES = tests/grub_sprintf.c kern/misc.c kern/list.c tests/test.c tests/unit_test.c
+grub_sprintf_CFLAGS  = -Wno-error -Wno-format
+
+# Functional tests
+
+check_MODULES += example_functional_test.mod
+example_functional_test_mod_SOURCES = tests/example_functional_test.c
+example_functional_test_mod_CFLAGS  = -Wno-format $(COMMON_CFLAGS)
+example_functional_test_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
+
+

=== added file 'include/grub/test.h'

grub/test.h +84 -0

84 lines not on this page · raw message

=== added directory 'tests'
=== added file 'tests/example_functional_test.c'

example_functional_test.c +17 -0
@@ -0,0 +1,17 @@
+/* All tests need to include test.h for GRUB testing framework.  */
+#include <grub/test.h>
+
+/* Functional test main method.  */
+static void
+example_test (void)
+{
+  /* Check if 1st argument is true and report with default error message.  */
+  grub_test_assert (1 == 1);
+
+  /* Check if 1st argument is true and report with custom error message.  */
+  grub_test_assert (2 == 2, "2 equal 2 expected");
+  grub_test_assert (2 == 3, "2 is not equal to %d", 3);
+}
+
+/* Register example_test method as a functional test.  */
+GRUB_FUNCTIONAL_TEST ("example_functional_test", example_test);

=== added file 'tests/example_unit_test.c'

example_unit_test.c +20 -0
@@ -0,0 +1,20 @@
+/* Unit tests are normal programs, so they can include C library.  */
+#include <string.h>
+
+/* All tests need to include test.h for GRUB testing framework.  */
+#include <grub/test.h>
+
+/* Unit test main method.  */
+static void
+example_test (void)
+{
+  /* Check if 1st argument is true and report with default error message.  */
+  grub_test_assert (1 == 1);
+
+  /* Check if 1st argument is true and report with custom error message.  */
+  grub_test_assert (2 == 2, "2 equal 2 expected");
+  grub_test_assert (2 == 3, "2 is not equal to %d", 3);
+}
+
+/* Register example_test method as a unit test.  */
+GRUB_UNIT_TEST ("example_unit_test", example_test);

=== added file 'tests/functional_test.c'

functional_test.c +89 -0

89 lines not on this page · raw message

=== added file 'tests/grub_sprintf.c'

grub_sprintf.c +162 -0

162 lines not on this page · raw message

=== added file 'tests/test.c'

test.c +150 -0

150 lines not on this page · raw message

=== added file 'tests/unit_test.c'

unit_test.c +122 -0

122 lines not on this page · raw message

author
#3 lore raw

Earlier patch had ChangeLog entry missing for conf/tests.rmk. Attached version has it fixed now.

Signature · 1 line
bvk.chaitanya

=== modified file 'ChangeLog'

ChangeLog +14 -0
@@ -1,3 +1,17 @@
+2009-11-23  BVK Chaitanya  <[email protected]>
+
+	Regression testing framework for GRUB.
+
+	* Makefile.in: Added rules for make check target.
+	* conf/tests.rmk: New file.  Rules for tests.
+	* include/grub/test.h: New file.  Header for testing framework.
+	* tests/test.c: Common definitions for the framework.
+	* tests/functional_test.c: Definitions for functional tests.
+	* tests/unit_test.c: Definitions for unit tests.
+	* tests/example_unit_test.c: Example test.
+	* tests/example_functional_test.c: Example test.
+	* tests/grub_sprintf.c: Unit test for grub_sprintf.
+
 2009-11-09  Robert Millan  <[email protected]>
 
 	Import from Gnulib.

=== modified file 'Makefile.in'

Makefile.in +13 -1
@@ -125,6 +125,7 @@
 SCRIPTS = $(bin_SCRIPTS) $(sbin_SCRIPTS) $(grub-mkconfig_SCRIPTS) \
 	$(lib_SCRIPTS)
 INFOS = $(info_INFOS)
+TESTS = $(check_PROGRAMS) $(check_SCRIPTS) $(check_MODULES)
 
 CLEANFILES =
 MOSTLYCLEANFILES =
@@ -151,6 +152,9 @@
 include $(srcdir)/conf/$(target_cpu)-$(platform).mk
 endif
 
+# for tests
+include $(srcdir)/conf/tests.mk
+
 # For external modules.
 -include $(wildcard $(GRUB_CONTRIB)/*/conf/common.mk)
 
@@ -428,7 +432,15 @@
 	@echo "$(distdir).tar.gz is ready for distribution" | \
 	  sed 'h;s/./=/g;p;x;p;x'
 
-check:
+check: $(TESTS)
+	@list="$(check_PROGRAMS) $(check_SCRIPTS)"; \
+	for file in $$list; do \
+	  if $(builddir)/$$file; then \
+	    echo "$$file: PASS"; \
+	  else \
+	    echo "$$file: FAIL"; \
+	  fi; \
+	done
 
 .SUFFIX:
 .SUFFIX: .c .o .S .d

=== added file 'conf/tests.rmk'

tests.rmk +26 -0
@@ -0,0 +1,26 @@
+# -*- makefile -*-
+
+check_MODULES += functional_test.mod
+functional_test_mod_SOURCES = tests/functional_test.c tests/test.c
+functional_test_mod_CFLAGS  = $(COMMON_CFLAGS)
+functional_test_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
+# Unit tests
+
+check_PROGRAMS += example_unit_test
+example_unit_test_SOURCES = tests/example_unit_test.c kern/list.c kern/misc.c tests/test.c tests/unit_test.c
+example_unit_test_CFLAGS  = -Wno-format
+
+check_PROGRAMS += grub_sprintf
+grub_sprintf_SOURCES = tests/grub_sprintf.c kern/misc.c kern/list.c tests/test.c tests/unit_test.c
+grub_sprintf_CFLAGS  = -Wno-error -Wno-format
+
+# Functional tests
+
+check_MODULES += example_functional_test.mod
+example_functional_test_mod_SOURCES = tests/example_functional_test.c
+example_functional_test_mod_CFLAGS  = -Wno-format $(COMMON_CFLAGS)
+example_functional_test_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
+
+

=== added file 'include/grub/test.h'

grub/test.h +84 -0

84 lines not on this page · raw message

=== added directory 'tests'
=== added file 'tests/example_functional_test.c'

example_functional_test.c +17 -0
@@ -0,0 +1,17 @@
+/* All tests need to include test.h for GRUB testing framework.  */
+#include <grub/test.h>
+
+/* Functional test main method.  */
+static void
+example_test (void)
+{
+  /* Check if 1st argument is true and report with default error message.  */
+  grub_test_assert (1 == 1);
+
+  /* Check if 1st argument is true and report with custom error message.  */
+  grub_test_assert (2 == 2, "2 equal 2 expected");
+  grub_test_assert (2 == 3, "2 is not equal to %d", 3);
+}
+
+/* Register example_test method as a functional test.  */
+GRUB_FUNCTIONAL_TEST ("example_functional_test", example_test);

=== added file 'tests/example_unit_test.c'

example_unit_test.c +20 -0
@@ -0,0 +1,20 @@
+/* Unit tests are normal programs, so they can include C library.  */
+#include <string.h>
+
+/* All tests need to include test.h for GRUB testing framework.  */
+#include <grub/test.h>
+
+/* Unit test main method.  */
+static void
+example_test (void)
+{
+  /* Check if 1st argument is true and report with default error message.  */
+  grub_test_assert (1 == 1);
+
+  /* Check if 1st argument is true and report with custom error message.  */
+  grub_test_assert (2 == 2, "2 equal 2 expected");
+  grub_test_assert (2 == 3, "2 is not equal to %d", 3);
+}
+
+/* Register example_test method as a unit test.  */
+GRUB_UNIT_TEST ("example_unit_test", example_test);

=== added file 'tests/functional_test.c'

functional_test.c +89 -0

89 lines not on this page · raw message

=== added file 'tests/grub_sprintf.c'

grub_sprintf.c +162 -0

162 lines not on this page · raw message

=== added file 'tests/test.c'

test.c +150 -0

150 lines not on this page · raw message

=== added file 'tests/unit_test.c'

unit_test.c +122 -0

122 lines not on this page · raw message

#4 lore raw
In reply to BVK

Am Freitag, den 13.11.2009, 23:15 +0530 schrieb BVK:

Quoted 6 lines

Hi,

Attached is the (experimental branch) patch for unit testing that I have in mind for GRUB. Please let me know your comments.

Quoted 8 lines

It provides "make check" target which would build and execute test programs from "tests" directory. We can write small programs to test specific functions in GRUB. For example, attached patch has sample program comparing grub_sprintf behavior against standard sprintf output (from glibc).

Note that it currently has few gotchas, like using TARGET_CC instead of BUILD_CC, no support for XPASS, XFAIL tests, etc.

Thanks for your work.
A really useful test and hopefully not complicated to implement would be if the config generated by grub-mkconfig (with e.g. 1 fake linux kernel) can be parsed by sh.mod and only uses commands listed in commands.lst

Reasoning:
With current trunk grub-mkconfig this line ends up in grub.cfg:
menuentry "Debian, with Linux GNU/Linux" {menuentry "2.6.31-1-amd64, with Linux " { insmod ext2
and of course this totally breaks the parser.

Signature · 2 lines
Felix Zielcke
Proud Debian Maintainer and GNU GRUB developer
author
#5 lore raw

Hi,

I have been traveling a lot, so I couldn't reply :-(

Quoted 5 lines · Felix Zielcke

Thanks for your work.
A really useful test and hopefully not complicated to implement would be if the config generated by grub-mkconfig (with e.g. 1 fake linux kernel) can be parsed by sh.mod and only uses commands listed in commands.lst

IMO this should also be done as part of update-grub process, because parts of grub.cfg file generated includes changes done by users too. Having a test case would only ensure that config snippets from GRUB source tree are parseable. So what we need is a grub util that checks for correctness of grub.cfg at user site.

I am not sure how we can achieve this with current grub tools :-( Maybe a grub-emu variant can do this parsing?

Signature · 1 line
bvk.chaitanya
author
#6 lore raw

Please see the other thread "Syntax checker for GRUB script configuration file" for this tool.

Signature · 1 line
bvk.chaitanya