Skip to contentSkip to conversation

Unittests

DISCUSSION Carles Pina i Estany 2 participants 7 messages Dec 14, 2009 – Dec 18, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

Hello,

I remember that BVK was interested to add some unit tests in Grub2.

Soon, when gettext will be a bit more far I' could write a unit test for gettext module (habit from work :-) -actually I wanted to do even if I don't publish it).

Writing a unittest for gettext it's very easy: it just needs a couple of .mo and a script executing gettext "string" and checking that the result is "string translated". Will get more interesting when ngettext will come too :-)

So, BVK, once you have the infrastructure if you ping me gettext could be your guinea pig.

Cheers,

Signature · 2 lines
Carles Pina i Estany
	http://pinux.info
#1 lore raw

Hi Carles,

Yes, I wrote unit testing framework and is available as a patch here:
http://lists.gnu.org/archive/html/grub-devel/2009-11/msg00349.html It is not reviewed and is very basic as of now. It has no example for scripted tests, but can be added using check_SCRIPTS make/rmk variable, similar to check_PROGRAMS.

Currently I am working on rewriting grub script lexer using flex, which also needs serious unit tests to ensure that nothing breaks. So we will see some action in this front soon :-)

BTW, I am yet to send my copyright papers (i will send in couple of days), so my patches are not committed.

Signature · 1 line
bvk.chaitanya
#2 lore raw
In reply to Carles Pina i Estany
Quoted 6 lines · Carles Pina i Estany

Writing a unittest for gettext it's very easy: it just needs a couple of .mo and a script executing gettext "string" and checking that the result is "string translated". Will get more interesting when ngettext will come too :-)

Hi Carles,

I don't have much knowledge about how gettext works, but I tried to add unit test today (in my local branch) and I couldn't get it working :-( Below is what I did, with some questions:

1. I downloaded zh_CN.po from translationproject.org and placed it in $(srcdir)/po/ directory.
2. I tried to build grub.pot, but build failed.

    Build rule for grub.pot assumes builddir == srcdir, which is not

true in my case. Also, it looks like that command is trying to create grub.po in srcdir instead of builddir; is it normal? if we need grub.pot in srcdir, can we move this step into autogen.sh?

3. After fixing the grub.pot rule, I was able to get .mo files in $(builddir)/po/
4. Now I wanted to see if gettext could find the translations without doing a make install, as below in $(builddir)

    $ LANG="zh_CN" TEXTDOMAINDIR=$(pwd) gettext grub "the core image
is too small"

It displayed only english version, even though zh_CN translation is
available :-( Since the directory structure in /usr/share/locale is different from $(builddir), I did "make install" and tried the gettext command without setting TEXTDOMAINDIR, but still no success :-(

What is wrong? Can you provide any pointers?

thanks,

Signature · 1 line
bvk.chaitanya
#3 lore raw

Hi,

Quoted 6 lines · BVK Chaitanya
Quoted 5 lines · Carles Pina i Estany

Writing a unittest for gettext it's very easy: it just needs a couple of .mo and a script executing gettext "string" and checking that the result is "string translated". Will get more interesting when ngettext will come too :-)

when I said "it's very easy" I was thinking more like "you explain to me how the unittest infrastructure works and I write the test" more than the other way. But no problem at all if you want to write it!

Quoted 6 lines

I don't have much knowledge about how gettext works, but I tried to add unit test today (in my local branch) and I couldn't get it working :-( Below is what I did, with some questions:

1. I downloaded zh_CN.po from translationproject.org and placed it in $(srcdir)/po/ directory.

ok!

Quoted 1 line

2. I tried to build grub.pot, but build failed.

ops...

Quoted 4 lines

Build rule for grub.pot assumes builddir == srcdir, which is not true in my case. Also, it looks like that command is trying to create grub.po in srcdir instead of builddir; is it normal? if we need grub.pot in srcdir, can we move this step into autogen.sh?

I'm not familiar with this part, Robert prepared the building system. Of course I can take a look next days or you can propose a patch.

Quoted 8 lines

3. After fixing the grub.pot rule, I was able to get .mo files in $(builddir)/po/
4. Now I wanted to see if gettext could find the translations without doing a make install, as below in $(builddir)

    $ LANG="zh_CN" TEXTDOMAINDIR=$(pwd) gettext grub "the core image
    is too small"
Quoted 6 lines

It displayed only english version, even though zh_CN translation is
available :-( Since the directory structure in /usr/share/locale is different from $(builddir), I did "make install" and tried the gettext command without setting TEXTDOMAINDIR, but still no success :-(

What is wrong? Can you provide any pointers?

This is not what I wanted to test using the unittest, but I can show a shell script that I did last june testing for these things.

carles@pinux:~/grub2/oldgrub/grubprova$ cat test.sh 
#!/bin/bash

export TEXTDOMAIN="testscript"
export TEXTDOMAINDIR="./locale"
echo $(gettext "hello world")

And then:

carles@pinux:~/grub2/oldgrub/grubprova$ find
.
./locale
./locale/ca
./locale/ca/LC_MESSAGES
./locale/ca/LC_MESSAGES/testscript.mo
./locale/[email protected]
./locale/test.pot
./test.sh
carles@pinux:~/grub2/oldgrub/grubprova$ 

and it works:

carles@pinux:~/grub2/oldgrub/grubprova$ ./test.sh 
hola món
carles@pinux:~/grub2/oldgrub/grubprova$ 

I'm not expert in this area, but $TEXTDOMAINDIR in my case is pointing to locale directory that needs to have the above hierarchy. Plus I think that you need to setup TEXTDOMAINDIR even if the files are correctly places in /usr/share/locale.

But what I was thinking to test with the unittest is the gettext.mod, not outside. I could do semi-manually doing:
a) Different .mo files with some strings (inside /boot/grub/locale/[.mo files])
b) A Grub2 disk image with the script to check the gettext module. Mainly would do (I'll write in pseudo-Bash, would be in Grub script):
-------
set lang=ca
TESTFAILED=false
if [ $(gettext "hello world") -ne "hola món" ]
then
echo "Test failed 01!"
TESTFAILED=true
fi

if [ ... ]
then
echo "Test failed 02!"
TESTFAILED=true
fi

set lang=es

if [ ... ]
then

fi

if [ $TESTFAILED -eq "true" ]
then
echo "TESTS FAILEDS"
fi
-------

So when I do changes in gettext module (like optimizse something, add support for plurals, etc. etc.) I would be sure that everything is all right.

Was you thinking something like this?

Thanks,

Signature · 2 lines
Carles Pina i Estany
	http://pinux.info
#4 lore raw

Hi,

Quoted 5 lines · Carles Pina i Estany

when I said "it's very easy" I was thinking more like "you explain to me how the unittest infrastructure works and I write the test" more than the other way. But no problem at all if you want to write it!

For scripted tests, unit test framework simply executes the script and reports PASS/FAIL based on its exit status. Attached patch has an example_scripted_test.in

Basically, add a testcase.in (.in would make it run through autoconf stuff, so you can use @builddir@, @srcdir@, etc. stuff in your testcase) file into tests/ subdir and modify conf/tests.rmk file to include your testcase, as

check_SCRIPTS += testcase
testcase_SOURCES = tests/testcase.in

and run configure and "make check". Let me know if you face any problems.

Quoted 11 lines

This is not what I wanted to test using the unittest, but I can show a shell script that I did last june testing for these things.

carles@pinux:~/grub2/oldgrub/grubprova$ cat test.sh
#!/bin/bash

export TEXTDOMAIN="testscript"
export TEXTDOMAINDIR="./locale"
echo $(gettext "hello world")

And then:

Quoted 11 lines
carles@pinux:~/grub2/oldgrub/grubprova$ find
.
./locale
./locale/ca
./locale/ca/LC_MESSAGES
./locale/ca/LC_MESSAGES/testscript.mo
./locale/[email protected]
./locale/test.pot
./test.sh
carles@pinux:~/grub2/oldgrub/grubprova$

Currently, GRUB-2 build system doesn't provide this directory structure, we need to fix it :-(

Quoted 34 lines

But what I was thinking to test with the unittest is the gettext.mod, not outside. I could do semi-manually doing:
a) Different .mo files with some strings (inside /boot/grub/locale/[.mo files])
b) A Grub2 disk image with the script to check the gettext module. Mainly would do (I'll write in pseudo-Bash, would be in Grub script):
-------
set lang=ca
TESTFAILED=false
if [ $(gettext "hello world") -ne "hola món" ]
then
       echo "Test failed 01!"
       TESTFAILED=true
fi

if [ ... ]
then
       echo "Test failed 02!"
       TESTFAILED=true
fi

set lang=es

if [ ... ]
then

fi

if [ $TESTFAILED -eq "true" ]
then
       echo "TESTS FAILEDS"
fi
-------

Quoted 6 lines

So when I do changes in gettext module (like optimizse something, add support for plurals, etc. etc.) I would be sure that everything is all right.

Was you thinking something like this?

Well, not exactly! Since $(gettext "hello world") output must be same in grub-shell environment and regular bash-shell, we can automate most of the test case failures. For example, for below script, we expect the output to be same both on grub-shell and bash-shell

#! grub-tester
gettext "hello world"
LANG="zh_CN" gettext "helloworld"

so *if* we can write an interpreter (like, grub-tester above) that executes given script in both shells and compares the output automatically, our unit tests would be a lot easier. A testcase failure should be raised by grub-tester if it finds that grub-shell output is different than bash-shell output for that script :-) Of course, testcase should use only those commands that are valid in both grub and bash.

For this to work we need a script to start grub image inside qemu with user specified script and capture its output.

Earlier I thought we could use grub-qemu port, but i am now
convinced that grub-mkrescue tool is sufficient. I am half way in doing this already. I hit these below problems, but i have workarounds in mind:

  a.  Option for not to display grub prompt (add PS1 environment variable ???)
  b.  Option to set noecho flag to tty.
  c.  Option to disable NCURSES escape sequences for serial terminal

(add TERM=dumb support ???)

Currently I am trying to cleanup escape sequences, grub> prompt and echoed characters from serial port output captured from grub; hopefully it should do for some time.

regards,

Signature · 1 line
bvk.chaitanya

=== modified file 'Makefile.in'

Makefile.in +13 -1
@@ -138,6 +138,7 @@
 SCRIPTS = $(bin_SCRIPTS) $(sbin_SCRIPTS) $(grub-mkconfig_SCRIPTS) \
 	$(lib_SCRIPTS)
 INFOS = $(info_INFOS)
+TESTS = $(check_TESTS) $(check_SCRIPTS) $(check_MODULES)
 
 CLEANFILES =
 MOSTLYCLEANFILES =
@@ -168,6 +169,9 @@
 -include $(wildcard $(GRUB_CONTRIB)/*/conf/common.mk)
 endif
 
+# For tests.
+include $(srcdir)/conf/tests.mk
+
 ### General targets.
 
 CLEANFILES += $(pkglib_DATA) $(pkgdata_DATA) po/*.mo
@@ -452,7 +456,15 @@
 	@echo "$(distdir).tar.gz is ready for distribution" | \
 	  sed 'h;s/./=/g;p;x;p;x'
 
-check:
+check: all $(TESTS)
+	@list="$(check_TESTS) $(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'

conf/tests.rmk +30 -0
@@ -0,0 +1,30 @@
+# -*- 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_TESTS += 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_TESTS += 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)
+
+# Scripted tests
+
+check_SCRIPTS += example_scripted_test
+example_scripted_test_SOURCES = tests/example_scripted_test.in
+
+

=== modified file 'genmk.rb'

genmk.rb +1 -1
@@ -413,7 +413,7 @@
 	    PModule.new(prefix, pmod)
 	  end
 
-	when 'UTILITIES'
+	when 'UTILITIES', 'TESTS'
 	  utils += args.split(/\s+/).collect do |util|
 	    Utility.new(prefix, util)
 	  end

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

include/grub/test.h +84 -0

84 lines not on this page · raw message

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

tests/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_scripted_test.in'

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

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

tests/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'

tests/functional_test.c +89 -0

89 lines not on this page · raw message

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

tests/grub_sprintf.c +162 -0

162 lines not on this page · raw message

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

tests/test.c +150 -0

150 lines not on this page · raw message

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

tests/unit_test.c +122 -0

122 lines not on this page · raw message

#5 lore raw

Hi,

an update is

Quoted 8 lines

doing this already.  I hit these below problems, but i have workarounds in mind:

 a.  Option for not to display grub prompt (add PS1 environment variable ???) b.  Option to set noecho flag to tty.
 c.  Option to disable NCURSES escape sequences for serial terminal (add TERM=dumb support ???)

I found a way to capture grub script output without above problems :-)

I attached a new patch, which has an example gettext_1.in testcase -- it doesn't yet do what you wanted, but is the starter.

Let me know your comments.

regards,

Signature · 1 line
bvk.chaitanya

=== modified file 'Makefile.in'

Makefile.in +13 -1
@@ -138,6 +138,7 @@
 SCRIPTS = $(bin_SCRIPTS) $(sbin_SCRIPTS) $(grub-mkconfig_SCRIPTS) \
 	$(lib_SCRIPTS)
 INFOS = $(info_INFOS)
+TESTS = $(check_TESTS) $(check_SCRIPTS) $(check_MODULES)
 
 CLEANFILES =
 MOSTLYCLEANFILES =
@@ -168,6 +169,9 @@
 -include $(wildcard $(GRUB_CONTRIB)/*/conf/common.mk)
 endif
 
+# For tests.
+include $(srcdir)/conf/tests.mk
+
 ### General targets.
 
 CLEANFILES += $(pkglib_DATA) $(pkgdata_DATA) po/*.mo
@@ -452,7 +456,15 @@
 	@echo "$(distdir).tar.gz is ready for distribution" | \
 	  sed 'h;s/./=/g;p;x;p;x'
 
-check:
+check: all $(TESTS)
+	@list="$(check_TESTS) $(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'

conf/tests.rmk +41 -0

41 lines not on this page · raw message

=== modified file 'genmk.rb'

genmk.rb +1 -1
@@ -413,7 +413,7 @@
 	    PModule.new(prefix, pmod)
 	  end
 
-	when 'UTILITIES'
+	when 'UTILITIES', 'TESTS'
 	  utils += args.split(/\s+/).collect do |util|
 	    Utility.new(prefix, util)
 	  end

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

include/grub/test.h +84 -0

84 lines not on this page · raw message

=== added directory 'tests'
=== added file 'tests/echo_keywords.in'

tests/echo_keywords.in +3 -0
@@ -0,0 +1,3 @@
+#! @builddir@/grub-shell-tester
+
+echo if then else fi for do done

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

tests/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_scripted_test.in'

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

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

tests/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'

tests/functional_test.c +89 -0

89 lines not on this page · raw message

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

tests/gettext_1.in +4 -0
@@ -0,0 +1,4 @@
+#! @builddir@/grub-shell-tester --modules=gettext
+
+gettext "hello world"
+

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

tests/grub_sprintf.c +162 -0

162 lines not on this page · raw message

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

tests/test.c +150 -0

150 lines not on this page · raw message

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

tests/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'

tests/util/grub-shell-tester.in +128 -0

128 lines not on this page · raw message

#6 lore raw

Hi,

Quoted 4 lines · BVK Chaitanya

I attached a new patch, which has an example gettext_1.in testcase -- it doesn't yet do what you wanted, but is the starter.

Let me know your comments.

When I will check I will tell you something :-) (I will do it in some days/during holidays...)

Thanks,

Signature · 2 lines
Carles Pina i Estany
	http://pinux.info