Richard W.M. Jones
2015-May-11 10:11 UTC
[Libguestfs] [PATCH 1/2] mllib: Require OUnit2 for tests.
OUnit2 has an OUnit (v1) compatibility module. Unfortunately it
is rather gravely broken:
https://forge.ocamlcore.org/tracker/?func=detail&aid=1392&group_id=162&atid=730
Since there is no new release fixing this, it's easier to switch to
using OUnit2 for unit tests.
---
.gitignore | 2 +-
README | 2 +-
mllib/JSON_tests.ml | 56 ++++++++++++++++++++-------------------------
mllib/Makefile.am | 2 ++
mllib/common_utils_tests.ml | 50 +++++++++++++++++-----------------------
5 files changed, 50 insertions(+), 62 deletions(-)
diff --git a/.gitignore b/.gitignore
index d5c6742..2a3cc81 100644
--- a/.gitignore
+++ b/.gitignore
@@ -312,7 +312,7 @@ Makefile.in
/mllib/JSON_tests
/mllib/libdir.ml
/mllib/link.sh
-/mllib/oUnit-anon.cache
+/mllib/oUnit-*
/ocaml/bindtests.bc
/ocaml/bindtests.opt
/ocaml/bindtests.ml
diff --git a/README b/README
index ec08c01..4a5786c 100644
--- a/README
+++ b/README
@@ -242,7 +242,7 @@ The full requirements are described below.
+--------------+-------------+---+-----------------------------------------+
| bash-completion | O | For tab-completion of commands in bash. |
+--------------+-------------+---+-----------------------------------------+
-| ocaml-ounit | | O | For the tests of the common OCaml |
+| ocaml-ounit | 2.0.0 | O | For the tests of the common OCaml |
| | | | modules. |
+--------------+-------------+---+-----------------------------------------+
| ocaml-libvirt| 0.6.1.5 | O | For building the virt-v2v test harness. |
diff --git a/mllib/JSON_tests.ml b/mllib/JSON_tests.ml
index fa37171..281d38e 100644
--- a/mllib/JSON_tests.ml
+++ b/mllib/JSON_tests.ml
@@ -18,19 +18,19 @@
(* This file tests the JSON module. *)
-open OUnit
+open OUnit2
(* Utils. *)
let assert_equal_string = assert_equal ~printer:(fun x -> x)
(* "basic" suite. *)
-let test_empty () +let test_empty ctx let doc = [] in
assert_equal_string "{}" (JSON.string_of_doc doc);
assert_equal_string "{
}" (JSON.string_of_doc ~fmt:JSON.Indented doc)
-let test_string () +let test_string ctx let doc = [ "test_string",
JSON.String "foo"; ] in
assert_equal_string "{ \"test_string\": \"foo\"
}"
(JSON.string_of_doc doc);
@@ -39,7 +39,7 @@ let test_string () }"
(JSON.string_of_doc ~fmt:JSON.Indented doc)
-let test_bool () +let test_bool ctx let doc = [ "test_true",
JSON.Bool true;
"test_false", JSON.Bool false ] in
assert_equal_string
@@ -52,7 +52,7 @@ let test_bool () }"
(JSON.string_of_doc ~fmt:JSON.Indented doc)
-let test_int () +let test_int ctx let doc = [ "test_zero",
JSON.Int 0;
"test_pos", JSON.Int 5;
"test_neg", JSON.Int (-5);
@@ -71,7 +71,7 @@ let test_int () }"
(JSON.string_of_doc ~fmt:JSON.Indented doc)
-let test_list () +let test_list ctx let doc = [ "item", JSON.List
[ JSON.String "foo"; JSON.Int 10; JSON.Bool true ] ] in
assert_equal_string
"{ \"item\": [ \"foo\", 10, true ] }"
@@ -86,7 +86,7 @@ let test_list () }"
(JSON.string_of_doc ~fmt:JSON.Indented doc)
-let test_nested_dict () +let test_nested_dict ctx let doc = [
"item", JSON.Dict [ "int", JSON.Int 5;
"string", JSON.String "foo"; ];
"last", JSON.Int 10;
@@ -104,7 +104,7 @@ let test_nested_dict () }"
(JSON.string_of_doc ~fmt:JSON.Indented doc)
-let test_nested_nested_dict () +let test_nested_nested_dict ctx let doc = [
"item", JSON.Dict [ "int", JSON.Int 5;
"item2", JSON.Dict [ "int", JSON.Int 0; ];
@@ -126,7 +126,7 @@ let test_nested_nested_dict () }"
(JSON.string_of_doc ~fmt:JSON.Indented doc)
-let test_escape () +let test_escape ctx let doc = [ "test_string",
JSON.String "test \" ' \n \b \r \t"; ] in
assert_equal_string "{ \"test_string\": \"test \\\"
' \\n \\b \\r \\t\" }"
(JSON.string_of_doc doc);
@@ -136,7 +136,7 @@ let test_escape () (JSON.string_of_doc
~fmt:JSON.Indented doc)
(* "examples" suite. *)
-let test_qemu () +let test_qemu ctx let doc = [
"file.driver", JSON.String "https";
"file.url", JSON.String "https://libguestfs.org";
@@ -155,7 +155,7 @@ let test_qemu () }"
(JSON.string_of_doc ~fmt:JSON.Indented doc)
-let test_builder () +let test_builder ctx let doc = [
"version", JSON.Int 1;
"sources", JSON.List [
@@ -221,25 +221,19 @@ let test_builder ()
(* Suites declaration. *)
let suite - TestList ([
- "basic" >::: [
- "empty" >:: test_empty;
- "string" >:: test_string;
- "bool" >:: test_bool;
- "int" >:: test_int;
- "list" >:: test_list;
- "nested dict" >:: test_nested_dict;
- "nested nested dict" >:: test_nested_nested_dict;
- "escape" >:: test_escape;
- ];
- "examples" >::: [
- "qemu" >:: test_qemu;
- "virt-builder" >:: test_builder;
- ];
- ])
-
-let _ - run_test_tt_main suite
+ "mllib JSON" >:::
+ [
+ "basic.empty" >:: test_empty;
+ "basic.string" >:: test_string;
+ "basic.bool" >:: test_bool;
+ "basic.int" >:: test_int;
+ "basic.list" >:: test_list;
+ "basic.nested_dict" >:: test_nested_dict;
+ "basic.nested_nested dict" >:: test_nested_nested_dict;
+ "basic.escape" >:: test_escape;
+ "examples.qemu" >:: test_qemu;
+ "examples.virt-builder" >:: test_builder;
+ ]
let () - Printf.fprintf stderr "\n"
+ run_test_tt_main suite
diff --git a/mllib/Makefile.am b/mllib/Makefile.am
index d5faf19..0b43684 100644
--- a/mllib/Makefile.am
+++ b/mllib/Makefile.am
@@ -165,6 +165,8 @@ if HAVE_OCAML_PKG_OUNIT
TESTS += common_utils_tests JSON_tests
endif
+CLEANFILES += oUnit-*
+
check-valgrind:
$(MAKE) VG="$(top_builddir)/run @VG@" check
diff --git a/mllib/common_utils_tests.ml b/mllib/common_utils_tests.ml
index 283e9a1..a06476b 100644
--- a/mllib/common_utils_tests.ml
+++ b/mllib/common_utils_tests.ml
@@ -1,5 +1,5 @@
-(* virt-resize
- * Copyright (C) 2011 Red Hat Inc.
+(* Common utilities for OCaml tools in libguestfs.
+ * Copyright (C) 2011-2015 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -18,7 +18,7 @@
(* This file tests the Common_utils module. *)
-open OUnit
+open OUnit2
open Common_utils
let prog = "common_utils_tests"
@@ -30,12 +30,12 @@ let assert_equal_int64 = assert_equal ~printer:(fun x ->
Int64.to_string x)
let assert_equal_stringlist = assert_equal ~printer:(fun x -> "("
^ (String.escaped (String.concat "," x)) ^ ")")
(* Test Common_utils.int_of_le32 and Common_utils.le32_of_int. *)
-let test_le32 () +let test_le32 ctx assert_equal_int64 0x20406080L
(int_of_le32 "\x80\x60\x40\x20");
assert_equal_string "\x80\x60\x40\x20" (le32_of_int 0x20406080L)
(* Test Common_utils.parse_size. *)
-let test_parse_resize () +let test_parse_resize ctx (* For absolute sizes,
oldsize is ignored. *)
assert_equal_int64 100_L (parse_resize ~prog 100_L "100b");
assert_equal_int64 100_L (parse_resize ~prog 1000_L "100b");
@@ -74,7 +74,7 @@ let test_parse_resize () assert_equal_int64 101100_L
(parse_resize ~prog 100000_L "+1.12%")
(* Test Common_utils.human_size. *)
-let test_human_size () +let test_human_size ctx assert_equal_string
"100" (human_size 100_L);
assert_equal_string "-100" (human_size (-100_L));
assert_equal_string "1.0K" (human_size 1024_L);
@@ -87,7 +87,7 @@ let test_human_size () assert_equal_string
"-3.4G" (human_size (-3650722201_L))
(* Test Common_utils.string_prefix. *)
-let test_string_prefix () +let test_string_prefix ctx assert_bool
"string_prefix,," (string_prefix "" "");
assert_bool "string_prefix,foo," (string_prefix "foo"
"");
assert_bool "string_prefix,foo,foo" (string_prefix "foo"
"foo");
@@ -95,7 +95,7 @@ let test_string_prefix () assert_bool "not
(string_prefix,,foo" (not (string_prefix "" "foo"))
(* Test Common_utils.string_suffix. *)
-let test_string_suffix () +let test_string_suffix ctx assert_bool
"string_suffix,," (string_suffix "" "");
assert_bool "string_suffix,foo," (string_suffix "foo"
"");
assert_bool "string_suffix,foo,foo" (string_suffix "foo"
"foo");
@@ -103,7 +103,7 @@ let test_string_suffix () assert_bool "not
string_suffix,,foo" (not (string_suffix "" "foo"))
(* Test Common_utils.string_find. *)
-let test_string_find () +let test_string_find ctx assert_equal_int 0
(string_find "" "");
assert_equal_int 0 (string_find "foo" "");
assert_equal_int 1 (string_find "foo" "o");
@@ -112,7 +112,7 @@ let test_string_find () assert_equal_int (-1)
(string_find "foobar" "baz")
(* Test Common_utils.string_lines_split. *)
-let test_string_lines_split () +let test_string_lines_split ctx
assert_equal_stringlist [""] (string_lines_split "");
assert_equal_stringlist ["A"] (string_lines_split "A");
assert_equal_stringlist ["A"; ""] (string_lines_split
"A\n");
@@ -129,24 +129,16 @@ let test_string_lines_split ()
(* Suites declaration. *)
let suite - TestList ([
- "numeric" >::: [
- "le32" >:: test_le32;
- ];
- "sizes" >::: [
- "parse_resize" >:: test_parse_resize;
- "human_size" >:: test_human_size;
- ];
- "strings" >::: [
- "prefix" >:: test_string_prefix;
- "suffix" >:: test_string_suffix;
- "find" >:: test_string_find;
- "string_lines_split" >:: test_string_lines_split;
- ];
- ])
-
-let _ - run_test_tt_main suite
+ "mllib Common_utils" >:::
+ [
+ "numeric.le32" >:: test_le32;
+ "sizes.parse_resize" >:: test_parse_resize;
+ "sizes.human_size" >:: test_human_size;
+ "strings.prefix" >:: test_string_prefix;
+ "strings.suffix" >:: test_string_suffix;
+ "strings.find" >:: test_string_find;
+ "strings.string_lines_split" >:: test_string_lines_split;
+ ]
let () - Printf.fprintf stderr "\n"
+ run_test_tt_main suite
--
2.3.1
Richard W.M. Jones
2015-May-11 10:11 UTC
[Libguestfs] [PATCH 2/2] v2v: OVF: Add unit tests for modified OVF.get_ostype function.
This adds unit tests for commit 6f9d5dce47f8502bee5d7285faf7695b22b5300b.
---
.gitignore | 2 ++
po/POTFILES-ml | 1 +
v2v/Makefile.am | 45 ++++++++++++++++++++++++
v2v/OVF.mli | 5 +++
v2v/v2v_unit_tests.ml | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 147 insertions(+)
create mode 100644 v2v/v2v_unit_tests.ml
diff --git a/.gitignore b/.gitignore
index 2a3cc81..318cfa0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -567,6 +567,7 @@ Makefile.in
/v2v/centos-7.0.img
/v2v/fedora-20.img
/v2v/link.sh
+/v2v/oUnit-*
/v2v/rhel-5.10.img
/v2v/rhel-6.5.img
/v2v/rhel-7.0.img
@@ -576,5 +577,6 @@ Makefile.in
/v2v/test-harness/dllv2v_test_harness.so
/v2v/test-harness/stamp-virt-v2v-test-harness.pod
/v2v/test-harness/virt-v2v-test-harness.1
+/v2v/v2v_unit_tests
/v2v/virt-v2v
/v2v/virt-v2v.1
diff --git a/po/POTFILES-ml b/po/POTFILES-ml
index 552fff3..7f4e096 100644
--- a/po/POTFILES-ml
+++ b/po/POTFILES-ml
@@ -115,4 +115,5 @@ v2v/test-harness/v2v_test_harness.ml
v2v/types.ml
v2v/utils.ml
v2v/v2v.ml
+v2v/v2v_unit_tests.ml
v2v/xml.ml
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index f07860e..8ba5fde 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -231,6 +231,10 @@ TESTS = \
test-v2v-i-ova-gz.sh \
test-v2v-i-ova-two-disks.sh
+if HAVE_OCAML_PKG_OUNIT
+TESTS += v2v_unit_tests
+endif
+
if ENABLE_APPLIANCE
TESTS += \
test-v2v-i-ova.sh \
@@ -283,6 +287,47 @@ $(real_guests:%=%.img):
CLEANFILES += $(real_guests:%=%.img)
+# Unit tests.
+check_PROGRAMS +if HAVE_OCAML_PKG_OUNIT
+check_PROGRAMS += v2v_unit_tests
+endif
+
+v2v_unit_tests_BOBJECTS = \
+ $(top_builddir)/mllib/config.cmo \
+ $(top_builddir)/mllib/common_gettext.cmo \
+ $(top_builddir)/mllib/common_utils.cmo \
+ stringMap.cmo \
+ types.cmo \
+ utils.cmo \
+ DOM.cmo \
+ OVF.cmo \
+ v2v_unit_tests.cmo
+v2v_unit_tests_XOBJECTS = $(v2v_unit_tests_BOBJECTS:.cmo=.cmx)
+
+v2v_unit_tests_SOURCES = $(virt_v2v_SOURCES)
+v2v_unit_tests_CPPFLAGS = $(virt_v2v_CPPFLAGS)
+v2v_unit_tests_CFLAGS = $(virt_v2v_CFLAGS)
+
+if !HAVE_OCAMLOPT
+# Can't call this v2v_unit_tests_OBJECTS because automake gets confused.
+v2v_unit_tests_THEOBJECTS = $(v2v_unit_tests_BOBJECTS)
+v2v_unit_tests.cmo: OCAMLPACKAGES += -package oUnit
+else
+v2v_unit_tests_THEOBJECTS = $(v2v_unit_tests_XOBJECTS)
+v2v_unit_tests.cmx: OCAMLPACKAGES += -package oUnit
+endif
+
+v2v_unit_tests_DEPENDENCIES = $(v2v_unit_tests_THEOBJECTS)
+v2v_unit_tests_LINK = \
+ ./link.sh \
+ $(OCAMLFIND) $(BEST) $(OCAMLFLAGS) \
+ $(OCAMLPACKAGES) -package oUnit \
+ $(OCAMLLINKFLAGS) \
+ $(v2v_unit_tests_THEOBJECTS) -o $@
+
+CLEANFILES += oUnit-*
+
# Dependencies.
depend: .depend
diff --git a/v2v/OVF.mli b/v2v/OVF.mli
index d894991..0a354e7 100644
--- a/v2v/OVF.mli
+++ b/v2v/OVF.mli
@@ -28,3 +28,8 @@ val create_meta_files : bool -> Types.output_allocation
-> string -> string list
val create_ovf : bool -> Types.source -> Types.target list ->
Types.guestcaps -> Types.inspect -> Types.output_allocation ->
Types.vmtype option -> string -> string list -> string list ->
string -> DOM.doc
(** Create the OVF file. *)
+
+(**/**)
+
+(* For use by v2v_unit_tests only. *)
+val get_ostype : Types.inspect -> string
diff --git a/v2v/v2v_unit_tests.ml b/v2v/v2v_unit_tests.ml
new file mode 100644
index 0000000..5c3b63a
--- /dev/null
+++ b/v2v/v2v_unit_tests.ml
@@ -0,0 +1,94 @@
+(* virt-resize
+ * Copyright (C) 2011 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(* This file tests individual virt-v2v functions. *)
+
+open OUnit2
+open Types
+
+let prog = "v2v_unit_tests"
+
+external identity : 'a -> 'a = "%identity"
+
+let test_get_ostype ctx + let i = { i_type = ""; i_distro =
""; i_arch = "";
+ i_major_version = 0; i_minor_version = 0;
+ i_root = ""; i_package_format = "";
i_package_management = "";
+ i_product_name = ""; i_product_variant = "";
i_mountpoints = [];
+ i_apps = []; i_apps_map = StringMap.empty; i_uefi = false } in
+ assert_equal ~printer:identity "RHEL6"
+ (OVF.get_ostype { i with i_type = "linux"; i_distro =
"rhel";
+ i_major_version = 6;
+ i_minor_version = 0;
+ i_arch = "i386" });
+ assert_equal ~printer:identity "RHEL6x64"
+ (OVF.get_ostype { i with i_type = "linux"; i_distro =
"rhel";
+ i_major_version = 6;
+ i_minor_version = 0;
+ i_arch = "x86_64" });
+ assert_equal ~printer:identity "rhel_7x64"
+ (OVF.get_ostype { i with i_type = "linux"; i_distro =
"rhel";
+ i_major_version = 7;
+ i_minor_version = 0;
+ i_arch = "x86_64" });
+ assert_equal ~printer:identity "Windows7"
+ (OVF.get_ostype { i with i_type = "windows";
+ i_major_version = 6;
+ i_minor_version = 1;
+ i_product_variant = "Client";
+ i_arch = "i386" });
+ assert_equal ~printer:identity "Windows7x64"
+ (OVF.get_ostype { i with i_type = "windows";
+ i_major_version = 6;
+ i_minor_version = 1;
+ i_product_variant = "Client";
+ i_arch = "x86_64" });
+ assert_equal ~printer:identity "windows_8"
+ (OVF.get_ostype { i with i_type = "windows";
+ i_major_version = 6;
+ i_minor_version = 2;
+ i_product_variant = "Client";
+ i_arch = "i386" });
+ assert_equal ~printer:identity "windows_8x64"
+ (OVF.get_ostype { i with i_type = "windows";
+ i_major_version = 6;
+ i_minor_version = 2;
+ i_product_variant = "Client";
+ i_arch = "x86_64" });
+ assert_equal ~printer:identity "windows_2012x64"
+ (OVF.get_ostype { i with i_type = "windows";
+ i_major_version = 6;
+ i_minor_version = 2;
+ i_product_variant = "Server";
+ i_arch = "x86_64" });
+ assert_equal ~printer:identity "windows_2012R2x64"
+ (OVF.get_ostype { i with i_type = "windows";
+ i_major_version = 6;
+ i_minor_version = 3;
+ i_product_variant = "Server";
+ i_arch = "x86_64" })
+
+(* Suites declaration. *)
+let suite + "virt-v2v" >:::
+ [
+ "OVF.get_ostype" >:: test_get_ostype;
+ ]
+
+let () + run_test_tt_main suite
--
2.3.1
Pino Toscano
2015-May-11 17:22 UTC
Re: [Libguestfs] [PATCH 1/2] mllib: Require OUnit2 for tests.
On Monday 11 May 2015 11:11:50 Richard W.M. Jones wrote:> OUnit2 has an OUnit (v1) compatibility module. Unfortunately it > is rather gravely broken: > https://forge.ocamlcore.org/tracker/?func=detail&aid=1392&group_id=162&atid=730 > > Since there is no new release fixing this, it's easier to switch to > using OUnit2 for unit tests.A pity, for the bug and the fact that there's no oUnit v2 for Fedora 21... It is not a problem for me anyway, just ...> --- > .gitignore | 2 +- > README | 2 +- > mllib/JSON_tests.ml | 56 ++++++++++++++++++++------------------------- > mllib/Makefile.am | 2 ++ > mllib/common_utils_tests.ml | 50 +++++++++++++++++----------------------- > 5 files changed, 50 insertions(+), 62 deletions(-)... please adjust configure.ac to look for oUnit2 instead of oUnit. -- Pino Toscano
Richard W.M. Jones
2015-May-11 19:45 UTC
Re: [Libguestfs] [PATCH 1/2] mllib: Require OUnit2 for tests.
On Mon, May 11, 2015 at 07:22:58PM +0200, Pino Toscano wrote:> On Monday 11 May 2015 11:11:50 Richard W.M. Jones wrote: > > OUnit2 has an OUnit (v1) compatibility module. Unfortunately it > > is rather gravely broken: > > https://forge.ocamlcore.org/tracker/?func=detail&aid=1392&group_id=162&atid=730 > > > > Since there is no new release fixing this, it's easier to switch to > > using OUnit2 for unit tests. > > A pity, for the bug and the fact that there's no oUnit v2 for > Fedora 21... > It is not a problem for me anyway, just ... > > > --- > > .gitignore | 2 +- > > README | 2 +- > > mllib/JSON_tests.ml | 56 ++++++++++++++++++++------------------------- > > mllib/Makefile.am | 2 ++ > > mllib/common_utils_tests.ml | 50 +++++++++++++++++----------------------- > > 5 files changed, 50 insertions(+), 62 deletions(-) > > ... please adjust configure.ac to look for oUnit2 instead of oUnit.This should do it: https://github.com/libguestfs/libguestfs/commit/46bc79109bb51cfb656863da3635f1c8da5f6412 Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
Reasonably Related Threads
- Re: [PATCH 1/2] mllib: Require OUnit2 for tests.
- [PATCH 1/2] mllib: Require OUnit2 for tests.
- [PATCH 2/2] mllib: add simple tests for the JSON module
- [PATCH 1/2] mllib: remove spurious check_SCRIPTS from Makefile.am
- [PATCH 2/2] mllib: link tests with automake