[Libguestfs] [PATCH 5/9] v2v: Add a binding for guestfs_int_drive_index and a unit test.

Richard W.M. Jones rjones at redhat.com
Wed Jul 1 17:54:47 UTC 2015


---
 v2v/utils-c.c         | 14 ++++++++++++++
 v2v/utils.ml          |  1 +
 v2v/v2v_unit_tests.ml | 19 +++++++++++++++++++
 3 files changed, 34 insertions(+)

diff --git a/v2v/utils-c.c b/v2v/utils-c.c
index e88620f..c0fa260 100644
--- a/v2v/utils-c.c
+++ b/v2v/utils-c.c
@@ -23,6 +23,7 @@
 #include <unistd.h>
 
 #include <caml/alloc.h>
+#include <caml/fail.h>
 #include <caml/memory.h>
 #include <caml/mlvalues.h>
 
@@ -43,3 +44,16 @@ v2v_utils_drive_name (value indexv)
 
   CAMLreturn (namev);
 }
+
+value
+v2v_utils_drive_index (value strv)
+{
+  CAMLparam1 (strv);
+  ssize_t r;
+
+  r = guestfs_int_drive_index (String_val (strv));
+  if (r == -1)
+    caml_invalid_argument ("drive_index: invalid parameter");
+
+  CAMLreturn (Val_int (r));
+}
diff --git a/v2v/utils.ml b/v2v/utils.ml
index 2aeba45..5618026 100644
--- a/v2v/utils.ml
+++ b/v2v/utils.ml
@@ -59,6 +59,7 @@ let uri_quote str =
   String.concat "" (List.rev !xs)
 
 external drive_name : int -> string = "v2v_utils_drive_name"
+external drive_index : string -> int = "v2v_utils_drive_index"
 
 (* Map guest architecture found by inspection to the architecture
  * that KVM must emulate.  Note for x86 we assume a 64 bit hypervisor.
diff --git a/v2v/v2v_unit_tests.ml b/v2v/v2v_unit_tests.ml
index 1b84ed0..00d19e0 100644
--- a/v2v/v2v_unit_tests.ml
+++ b/v2v/v2v_unit_tests.ml
@@ -94,12 +94,31 @@ let test_drive_name ctx =
   assert_equal ~printer "aaa" (Utils.drive_name 702);
   assert_equal ~printer "zzz" (Utils.drive_name 18277)
 
+let test_drive_index ctx =
+  let printer = string_of_int in
+  assert_equal ~printer 0 (Utils.drive_index "a");
+  assert_equal ~printer 25 (Utils.drive_index "z");
+  assert_equal ~printer 26 (Utils.drive_index "aa");
+  assert_equal ~printer 27 (Utils.drive_index "ab");
+  assert_equal ~printer 51 (Utils.drive_index "az");
+  assert_equal ~printer 52 (Utils.drive_index "ba");
+  assert_equal ~printer 701 (Utils.drive_index "zz");
+  assert_equal ~printer 702 (Utils.drive_index "aaa");
+  assert_equal ~printer 18277 (Utils.drive_index "zzz");
+  let exn = Invalid_argument "drive_index: invalid parameter" in
+  assert_raises exn (fun () -> Utils.drive_index "");
+  assert_raises exn (fun () -> Utils.drive_index "abc123");
+  assert_raises exn (fun () -> Utils.drive_index "123");
+  assert_raises exn (fun () -> Utils.drive_index "Z");
+  assert_raises exn (fun () -> Utils.drive_index "aB")
+
 (* Suites declaration. *)
 let suite =
   "virt-v2v" >:::
     [
       "OVF.get_ostype" >:: test_get_ostype;
       "Utils.drive_name" >:: test_drive_name;
+      "Utils.drive_index" >:: test_drive_index;
     ]
 
 let () =
-- 
2.3.1




More information about the Libguestfs mailing list