Skip to contentSkip to conversation

Unit testing framework adjusted over trunk

PATC BVK Chaitanya 1 participant 4 messages Jan 8, 2010 – Jan 14, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi,

Attached is the patch that adds unit-testing framework support to GRUB. Its been adjusted to work over trunk branch, and has few changes from what is available in my people/bvk/unit-testing-support branch.

Since my all of my other works has one-or-two unit tests, this is a pre-requisite and needs through code review. Please let me know your comments.

thanks,

Signature · 1 line
bvk.chaitanya

=== added file 'ChangeLog.unit-testing-framework'

ChangeLog.unit-testing-framework +26 -0
@@ -0,0 +1,26 @@
+2010-01-08  BVK Chaitanya  <[email protected]>
+
+	Unit testing framework for GRUB.
+
+	* Makefile.in: Test framework build rules for 'make check'.
+	* conf/tests.rmk: Build rules for individual tests and framework.
+
+	* include/grub/test.h: Header file for whitebox tests.
+	* tests/lib/functional_test.c: Framework support for whitebox
+	functional tests.
+	* tests/lib/test.c: Common whitebox testing code for unit and
+	functional tests.
+	* tests/lib/unit_test.c: Framework support for whitebox unit
+	tests.
+
+	* tests/util/grub-shell-tester.in: Support utility for grub-script
+	tests.
+	* tests/util/grub-shell.in: Utility to execute grub-script
+	commands in a Qemu instance.
+
+	* tests/example_functional_test.c: Example whitebox functional
+	test.
+	* tests/example_grub_script_test.in: Example grub-script test.
+	* tests/example_scripted_test.in: Example scripted test.
+	* tests/example_unit_test.c: Example whitebox unit test.
+

=== modified file 'Makefile.in'

Makefile.in +25 -1
@@ -167,6 +167,8 @@
 include $(srcdir)/conf/any-emu.mk
 else
 include $(srcdir)/conf/$(target_cpu)-$(platform).mk
+# For tests.
+include $(srcdir)/conf/tests.mk
 # For external modules.
 -include $(wildcard $(GRUB_CONTRIB)/*/conf/common.mk)
 endif
@@ -458,7 +460,29 @@
 	@echo "$(distdir).tar.gz is ready for distribution" | \
 	  sed 'h;s/./=/g;p;x;p;x'
 
-check:
+TESTS = $(check_UTILITIES) $(check_SCRIPTS) $(check_MODULES)
+$(TESTS): $(test_framework_SCRIPTS) $(test_framework_MODULES)
+
+check: all $(TESTS)
+	@list="$(check_UTILITIES)"; \
+	for file in $$list; do \
+	  $(builddir)/$$file; \
+	done
+	@list="$(check_SCRIPTS)"; \
+	for file in $$list; do \
+	  echo "$$file:"; \
+	  if $(builddir)/$$file; then \
+	    echo "$$file: PASS"; \
+	  else \
+	    echo "$$file: FAIL"; \
+	  fi; \
+	done
+	@list="$(check_MODULES)"; \
+	for file in $$list; do \
+	  mod=`basename $$file .mod`; \
+	  echo "insmod functional_test; insmod $$mod; functional_test" \
+	    | $(builddir)/grub-shell; \
+	done
 
 .SUFFIX:
 .SUFFIX: .c .o .S .d

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

tests.rmk +41 -0

41 lines not on this page · raw message

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

grub/test.h +76 -0

76 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_grub_script_test.in'

example_grub_script_test.in +3 -0
@@ -0,0 +1,3 @@
+#! @builddir@/grub-shell-tester --modules=echo
+
+echo "hello world"

=== added file 'tests/example_scripted_test.in'

example_scripted_test.in +3 -0
@@ -0,0 +1,3 @@
+#!/bin/sh -e
+
+true

=== 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 directory 'tests/lib'
=== added file 'tests/lib/functional_test.c'

lib/functional_test.c +72 -0

72 lines not on this page · raw message

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

lib/test.c +146 -0

146 lines not on this page · raw message

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

lib/unit_test.c +122 -0

122 lines not on this page · raw message

=== added directory 'tests/util'
=== added file 'tests/util/grub-shell-tester.in'

util/grub-shell-tester.in +109 -0

109 lines not on this page · raw message

=== added file 'tests/util/grub-shell.in'

util/grub-shell.in +125 -0

125 lines not on this page · raw message

#1 lore raw

Some minor comments:

+
+if ! diff -q ${outfile1} ${outfile2} >/dev/null
+then
+ echo "$1: GRUB and BASH outputs (${outfile1}, ${outfile2}) did not match"
+ status=1
+else
+ rm -f ${outfile1} ${outfile2}
+fi
+

1. In the above echo statement, $1 doesn't resolve to the testcase file name, use ${source} instead.
2. Change the "(${outfile1}, ${outfile2})" to "(see diff -u ${outfile1} ${outfile2})" so that developer can select-n-paste the command more easily.

Signature · 1 line
bvk.chaitanya
#2 lore raw

An updated patch is attached with some fixes mentioned below:

1. Added --boot=[fd|hd|cd] option support to grub-shell script. By default grub-shell script now starts Qemu with disk image, but with --boot=fd or --boot=cd options, Qemu will be booted with floppy or
cdrom image respectively. This feature is requested by Robert and
Jordan_U (???).

2. Removed using EXPORT_VAR and EXPORT_FUNC macros from include/grub/test.h header file. Vladimir explained that these are not necessary (in IRC).

3. Fixed error message in grub-shell-tester as in my earlier reply.

4. All functional tests are now built as part of make and are installed on make install.

5. Introduced new variables, UNIT_TESTS, FUNCTIONAL_TESTS and SCRIPTED_TESTS, to simplify changes between tests.rmk and Makefile.in files.

Signature · 1 line
bvk.chaitanya

=== added file 'ChangeLog.unit-testing-framework'

ChangeLog.unit-testing-framework +37 -0
@@ -0,0 +1,37 @@
+2010-01-12  BVK Chaitanya  <bvk@dbook>
+
+	* tests/util/grub-shell.in: New --boot option.
+
+	* conf/tests.rmk: Build functional tests on make.
+	* Makefile.in (check): Use new variables.
+
+	* include/grub/test.h: Remove EXPORT_* usage.
+
+	* tests/util/grub-shell-tester.in: Fix error message.
+
+2010-01-08  BVK Chaitanya  <[email protected]>
+
+	Unit testing framework for GRUB.
+
+	* Makefile.in: Test framework build rules for 'make check'.
+	* conf/tests.rmk: Build rules for individual tests and framework.
+
+	* include/grub/test.h: Header file for whitebox tests.
+	* tests/lib/functional_test.c: Framework support for whitebox
+	functional tests.
+	* tests/lib/test.c: Common whitebox testing code for unit and
+	functional tests.
+	* tests/lib/unit_test.c: Framework support for whitebox unit
+	tests.
+
+	* tests/util/grub-shell-tester.in: Support utility for grub-script
+	tests.
+	* tests/util/grub-shell.in: Utility to execute grub-script
+	commands in a Qemu instance.
+
+	* tests/example_functional_test.c: Example whitebox functional
+	test.
+	* tests/example_grub_script_test.in: Example grub-script test.
+	* tests/example_scripted_test.in: Example scripted test.
+	* tests/example_unit_test.c: Example whitebox unit test.
+

=== modified file 'Makefile.in'

Makefile.in +22 -1
@@ -167,6 +167,8 @@
 include $(srcdir)/conf/any-emu.mk
 else
 include $(srcdir)/conf/$(target_cpu)-$(platform).mk
+# For tests.
+include $(srcdir)/conf/tests.mk
 # For external modules.
 -include $(wildcard $(GRUB_CONTRIB)/*/conf/common.mk)
 endif
@@ -461,7 +463,26 @@
 	@echo "$(distdir).tar.gz is ready for distribution" | \
 	  sed 'h;s/./=/g;p;x;p;x'
 
-check:
+check: all $(UNIT_TESTS) $(FUNCTIONAL_TESTS) $(SCRIPTED_TESTS)
+	@list="$(UNIT_TESTS)"; \
+	for file in $$list; do \
+	  $(builddir)/$$file; \
+	done
+	@list="$(FUNCTIONAL_TESTS)"; \
+	for file in $$list; do \
+	  mod=`basename $$file .mod`; \
+	  echo "insmod functional_test; insmod $$mod; functional_test" \
+	    | $(builddir)/grub-shell; \
+	done
+	@list="$(SCRIPTED_TESTS)"; \
+	for file in $$list; do \
+	  echo "$$file:"; \
+	  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 +50 -0

50 lines not on this page · raw message

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

grub/test.h +75 -0

75 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_grub_script_test.in'

example_grub_script_test.in +3 -0
@@ -0,0 +1,3 @@
+#! @builddir@/grub-shell-tester --modules=echo
+
+echo "hello world"

=== added file 'tests/example_scripted_test.in'

example_scripted_test.in +3 -0
@@ -0,0 +1,3 @@
+#!/bin/sh -e
+
+true

=== 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 directory 'tests/lib'
=== added file 'tests/lib/functional_test.c'

lib/functional_test.c +72 -0

72 lines not on this page · raw message

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

lib/test.c +146 -0

146 lines not on this page · raw message

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

lib/unit_test.c +122 -0

122 lines not on this page · raw message

=== added directory 'tests/util'
=== added file 'tests/util/grub-shell-tester.in'

util/grub-shell-tester.in +109 -0

109 lines not on this page · raw message

=== added file 'tests/util/grub-shell.in'

util/grub-shell.in +145 -0

145 lines not on this page · raw message

#3 lore raw

Hi,

This has been committed to trunk with some minor changes. Its been reviewed by Vladimir (over IRC).

thanks,

Signature · 1 line
bvk.chaitanya