mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0011-v2v-add-Var_expander.patch

da373f
From 8b7c559f6c619bab8a708358db15b2959623c833 Mon Sep 17 00:00:00 2001
3efd08
From: Pino Toscano <ptoscano@redhat.com>
3efd08
Date: Tue, 19 Feb 2019 14:54:31 +0100
3efd08
Subject: [PATCH] v2v: add Var_expander
3efd08
3efd08
This helper module provides a facility to replace %{FOO}-like variables
3efd08
in text strings with user-provided content.
3efd08
3efd08
(cherry picked from commit a27748d7000f417c16045967497208d275a09ce8)
3efd08
---
3efd08
 .gitignore                |   1 +
3efd08
 v2v/Makefile.am           |  32 ++++++++++-
3efd08
 v2v/dummy.c               |   2 +
3efd08
 v2v/var_expander.ml       |  72 ++++++++++++++++++++++++
3efd08
 v2v/var_expander.mli      |  82 +++++++++++++++++++++++++++
3efd08
 v2v/var_expander_tests.ml | 113 ++++++++++++++++++++++++++++++++++++++
3efd08
 6 files changed, 300 insertions(+), 2 deletions(-)
3efd08
 create mode 100644 v2v/dummy.c
3efd08
 create mode 100644 v2v/var_expander.ml
3efd08
 create mode 100644 v2v/var_expander.mli
3efd08
 create mode 100644 v2v/var_expander_tests.ml
3efd08
3efd08
diff --git a/.gitignore b/.gitignore
3efd08
index 637bf7765..f2efcdde2 100644
3efd08
--- a/.gitignore
3efd08
+++ b/.gitignore
3efd08
@@ -693,6 +693,7 @@ Makefile.in
3efd08
 /v2v/uefi.ml
3efd08
 /v2v/uefi.mli
3efd08
 /v2v/v2v_unit_tests
3efd08
+/v2v/var_expander_tests
3efd08
 /v2v/virt-v2v
3efd08
 /v2v/virt-v2v.1
3efd08
 /v2v/virt-v2v-copy-to-local
3efd08
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
3efd08
index 2312812fb..f196be81d 100644
3efd08
--- a/v2v/Makefile.am
3efd08
+++ b/v2v/Makefile.am
3efd08
@@ -98,6 +98,7 @@ SOURCES_MLI = \
3efd08
 	utils.mli \
3efd08
 	v2v.mli \
3efd08
 	vCenter.mli \
3efd08
+	var_expander.mli \
3efd08
 	windows.mli \
3efd08
 	windows_virtio.mli
3efd08
 
3efd08
@@ -106,6 +107,7 @@ SOURCES_ML = \
3efd08
 	types.ml \
3efd08
 	uefi.ml \
3efd08
 	utils.ml \
3efd08
+	var_expander.ml \
3efd08
 	python_script.ml \
3efd08
 	name_from_disk.ml \
3efd08
 	vCenter.ml \
3efd08
@@ -442,7 +444,7 @@ TESTS += \
3efd08
 endif
3efd08
 
3efd08
 if HAVE_OCAML_PKG_OUNIT
3efd08
-TESTS += v2v_unit_tests
3efd08
+TESTS += v2v_unit_tests var_expander_tests
3efd08
 endif
3efd08
 
3efd08
 if ENABLE_APPLIANCE
3efd08
@@ -651,7 +653,7 @@ EXTRA_DIST += \
3efd08
 # Unit tests.
3efd08
 check_PROGRAMS =
3efd08
 if HAVE_OCAML_PKG_OUNIT
3efd08
-check_PROGRAMS += v2v_unit_tests
3efd08
+check_PROGRAMS += v2v_unit_tests var_expander_tests
3efd08
 endif
3efd08
 
3efd08
 v2v_unit_tests_BOBJECTS = \
3efd08
@@ -671,13 +673,28 @@ v2v_unit_tests_SOURCES = $(virt_v2v_SOURCES)
3efd08
 v2v_unit_tests_CPPFLAGS = $(virt_v2v_CPPFLAGS)
3efd08
 v2v_unit_tests_CFLAGS = $(virt_v2v_CFLAGS)
3efd08
 
3efd08
+var_expander_tests_BOBJECTS = \
3efd08
+	var_expander.cmo \
3efd08
+	var_expander_tests.cmo
3efd08
+var_expander_tests_XOBJECTS = $(var_expander_tests_BOBJECTS:.cmo=.cmx)
3efd08
+
3efd08
+var_expander_tests_SOURCES = dummy.c
3efd08
+var_expander_tests_CPPFLAGS = $(virt_v2v_CPPFLAGS)
3efd08
+var_expander_tests_CFLAGS = $(virt_v2v_CFLAGS)
3efd08
+
3efd08
 if !HAVE_OCAMLOPT
3efd08
 # Can't call this v2v_unit_tests_OBJECTS because automake gets confused.
3efd08
 v2v_unit_tests_THEOBJECTS = $(v2v_unit_tests_BOBJECTS)
3efd08
 v2v_unit_tests.cmo: OCAMLPACKAGES += -package oUnit
3efd08
+
3efd08
+var_expander_tests_THEOBJECTS = $(var_expander_tests_BOBJECTS)
3efd08
+var_expander_tests.cmo: OCAMLPACKAGES += -package oUnit
3efd08
 else
3efd08
 v2v_unit_tests_THEOBJECTS = $(v2v_unit_tests_XOBJECTS)
3efd08
 v2v_unit_tests.cmx: OCAMLPACKAGES += -package oUnit
3efd08
+
3efd08
+var_expander_tests_THEOBJECTS = $(var_expander_tests_XOBJECTS)
3efd08
+var_expander_tests.cmx: OCAMLPACKAGES += -package oUnit
3efd08
 endif
3efd08
 
3efd08
 v2v_unit_tests_DEPENDENCIES = \
3efd08
@@ -696,6 +713,17 @@ v2v_unit_tests_LINK = \
3efd08
 	  $(OCAMLLINKFLAGS) \
3efd08
 	  $(v2v_unit_tests_THEOBJECTS) -o $@
3efd08
 
3efd08
+var_expander_tests_DEPENDENCIES = \
3efd08
+	$(var_expander_tests_THEOBJECTS) \
3efd08
+	../common/mlpcre/mlpcre.$(MLARCHIVE) \
3efd08
+	$(top_srcdir)/ocaml-link.sh
3efd08
+var_expander_tests_LINK = \
3efd08
+	$(top_srcdir)/ocaml-link.sh -cclib '$(OCAMLCLIBS)' -- \
3efd08
+	  $(OCAMLFIND) $(BEST) $(OCAMLFLAGS) \
3efd08
+	  $(OCAMLPACKAGES) -package oUnit \
3efd08
+	  $(OCAMLLINKFLAGS) \
3efd08
+	  $(var_expander_tests_THEOBJECTS) -o $@
3efd08
+
3efd08
 # Dependencies.
3efd08
 .depend: \
3efd08
 	$(srcdir)/*.mli \
3efd08
diff --git a/v2v/dummy.c b/v2v/dummy.c
3efd08
new file mode 100644
3efd08
index 000000000..ebab6198c
3efd08
--- /dev/null
3efd08
+++ b/v2v/dummy.c
3efd08
@@ -0,0 +1,2 @@
3efd08
+/* Dummy source, to be used for OCaml-based tools with no C sources. */
3efd08
+enum { foo = 1 };
3efd08
diff --git a/v2v/var_expander.ml b/v2v/var_expander.ml
3efd08
new file mode 100644
3efd08
index 000000000..24b9bafe3
3efd08
--- /dev/null
3efd08
+++ b/v2v/var_expander.ml
3efd08
@@ -0,0 +1,72 @@
3efd08
+(* virt-v2v
3efd08
+ * Copyright (C) 2019 Red Hat Inc.
3efd08
+ *
3efd08
+ * This program is free software; you can redistribute it and/or modify
3efd08
+ * it under the terms of the GNU General Public License as published by
3efd08
+ * the Free Software Foundation; either version 2 of the License, or
3efd08
+ * (at your option) any later version.
3efd08
+ *
3efd08
+ * This program is distributed in the hope that it will be useful,
3efd08
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3efd08
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3efd08
+ * GNU General Public License for more details.
3efd08
+ *
3efd08
+ * You should have received a copy of the GNU General Public License along
3efd08
+ * with this program; if not, write to the Free Software Foundation, Inc.,
3efd08
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3efd08
+ *)
3efd08
+
3efd08
+open Std_utils
3efd08
+
3efd08
+exception Invalid_variable of string
3efd08
+
3efd08
+let var_re = PCRE.compile "(^|[^%])%{([^}]+)}"
3efd08
+
3efd08
+let check_variable var =
3efd08
+  String.iter (
3efd08
+    function
3efd08
+    | '0'..'9'
3efd08
+    | 'a'..'z'
3efd08
+    | 'A'..'Z'
3efd08
+    | '_'
3efd08
+    | '-' -> ()
3efd08
+    | _ -> raise (Invalid_variable var)
3efd08
+  ) var
3efd08
+
3efd08
+let scan_variables str =
3efd08
+  let res = ref [] in
3efd08
+  let offset = ref 0 in
3efd08
+  while PCRE.matches ~offset:!offset var_re str; do
3efd08
+    let var = PCRE.sub 2 in
3efd08
+    check_variable var;
3efd08
+    let _, end_ = PCRE.subi 0 in
3efd08
+    List.push_back res var;
3efd08
+    offset := end_
3efd08
+  done;
3efd08
+  List.remove_duplicates !res
3efd08
+
3efd08
+let replace_fn str fn =
3efd08
+  let res = ref str in
3efd08
+  let offset = ref 0 in
3efd08
+  while PCRE.matches ~offset:!offset var_re !res; do
3efd08
+    let var = PCRE.sub 2 in
3efd08
+    check_variable var;
3efd08
+    let start_, end_ = PCRE.subi 0 in
3efd08
+    match fn var with
3efd08
+    | None ->
3efd08
+      offset := end_
3efd08
+    | Some text ->
3efd08
+      let prefix_len =
3efd08
+        let prefix_start, prefix_end = PCRE.subi 1 in
3efd08
+        prefix_end - prefix_start in
3efd08
+      res := (String.sub !res 0 (start_ + prefix_len)) ^ text ^ (String.sub !res end_ (String.length !res - end_));
3efd08
+      offset := start_ + prefix_len + String.length text
3efd08
+  done;
3efd08
+  !res
3efd08
+
3efd08
+let replace_list str lst =
3efd08
+  let fn var =
3efd08
+    try Some (List.assoc var lst)
3efd08
+    with Not_found -> None
3efd08
+  in
3efd08
+  replace_fn str fn
3efd08
diff --git a/v2v/var_expander.mli b/v2v/var_expander.mli
3efd08
new file mode 100644
3efd08
index 000000000..80aa33c2c
3efd08
--- /dev/null
3efd08
+++ b/v2v/var_expander.mli
3efd08
@@ -0,0 +1,82 @@
3efd08
+(* virt-v2v
3efd08
+ * Copyright (C) 2019 Red Hat Inc.
3efd08
+ *
3efd08
+ * This program is free software; you can redistribute it and/or modify
3efd08
+ * it under the terms of the GNU General Public License as published by
3efd08
+ * the Free Software Foundation; either version 2 of the License, or
3efd08
+ * (at your option) any later version.
3efd08
+ *
3efd08
+ * This program is distributed in the hope that it will be useful,
3efd08
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3efd08
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3efd08
+ * GNU General Public License for more details.
3efd08
+ *
3efd08
+ * You should have received a copy of the GNU General Public License along
3efd08
+ * with this program; if not, write to the Free Software Foundation, Inc.,
3efd08
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3efd08
+ *)
3efd08
+
3efd08
+(** Simple variable expander.
3efd08
+
3efd08
+    This module provides the support to expand variables in strings,
3efd08
+    specified in the form of [%{name}].
3efd08
+
3efd08
+    For example:
3efd08
+
3efd08
+{v
3efd08
+let str = "variable-%{INDEX} in %{INDEX} replaced %{INDEX} times"
3efd08
+let index = ref 0
3efd08
+let fn = function
3efd08
+  | "INDEX" ->
3efd08
+    incr index;
3efd08
+    Some (string_of_int !index)
3efd08
+  | _ -> None
3efd08
+in
3efd08
+let str = Var_expander.replace_fn str fn
3efd08
+(* now str is "variable-1 in 2 replaced 3 times" *)
3efd08
+v}
3efd08
+
3efd08
+    The names of variables can contain only ASCII letters (uppercase,
3efd08
+    and lowercase), digits, underscores, and dashes.
3efd08
+
3efd08
+    The replacement is done in a single pass: this means that if a
3efd08
+    variable is replaced with the text of a variable, that new text
3efd08
+    is kept as is in the final output.  In practice:
3efd08
+
3efd08
+{v
3efd08
+let str = "%{VAR}"
3efd08
+let str = Var_expander.replace_list str [("VAR", "%{VAR}")]
3efd08
+(* now str is "%{VAR}" *)
3efd08
+v}
3efd08
+*)
3efd08
+
3efd08
+exception Invalid_variable of string
3efd08
+(** Invalid variable name error.
3efd08
+
3efd08
+    In case a variable contains characters not allowed, then this
3efd08
+    exception with the actual unacceptable variable. *)
3efd08
+
3efd08
+val scan_variables : string -> string list
3efd08
+(** Scan the pattern string for all the variables available.
3efd08
+
3efd08
+    This can raise {!Invalid_variable} in case there are invalid
3efd08
+    variable names. *)
3efd08
+
3efd08
+val replace_fn : string -> (string -> string option) -> string
3efd08
+(** Replaces a string expanding all the variables.
3efd08
+
3efd08
+    The replacement function specify how a variable is replaced;
3efd08
+    if [None] is returned, then that variable is not replaced.
3efd08
+
3efd08
+    This can raise {!Invalid_variable} in case there are invalid
3efd08
+    variable names. *)
3efd08
+
3efd08
+val replace_list : string -> (string * string) list -> string
3efd08
+(** Replaces a string expanding all the variables.
3efd08
+
3efd08
+    The replacement list specify how a variable is replaced;
3efd08
+    if it is not specified in the list, then that variable is not
3efd08
+    replaced.
3efd08
+
3efd08
+    This can raise {!Invalid_variable} in case there are invalid
3efd08
+    variable names. *)
3efd08
diff --git a/v2v/var_expander_tests.ml b/v2v/var_expander_tests.ml
3efd08
new file mode 100644
3efd08
index 000000000..35b628369
3efd08
--- /dev/null
3efd08
+++ b/v2v/var_expander_tests.ml
3efd08
@@ -0,0 +1,113 @@
3efd08
+(* virt-v2v
3efd08
+ * Copyright (C) 2019 Red Hat Inc.
3efd08
+ *
3efd08
+ * This program is free software; you can redistribute it and/or modify
3efd08
+ * it under the terms of the GNU General Public License as published by
3efd08
+ * the Free Software Foundation; either version 2 of the License, or
3efd08
+ * (at your option) any later version.
3efd08
+ *
3efd08
+ * This program is distributed in the hope that it will be useful,
3efd08
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3efd08
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3efd08
+ * GNU General Public License for more details.
3efd08
+ *
3efd08
+ * You should have received a copy of the GNU General Public License along
3efd08
+ * with this program; if not, write to the Free Software Foundation, Inc.,
3efd08
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3efd08
+ *)
3efd08
+
3efd08
+open Printf
3efd08
+open OUnit
3efd08
+
3efd08
+open Std_utils
3efd08
+
3efd08
+let assert_equal_string = assert_equal ~printer:identity
3efd08
+let assert_equal_stringlist = assert_equal ~printer:(fun x -> "(" ^ (String.escaped (String.concat "," x)) ^ ")")
3efd08
+
3efd08
+let replace_none_fn _ = None
3efd08
+let replace_empty_fn _ = Some ""
3efd08
+
3efd08
+let test_no_replacement () =
3efd08
+  assert_equal_string "" (Var_expander.replace_fn "" replace_none_fn);
3efd08
+  assert_equal_string "x" (Var_expander.replace_fn "x" replace_none_fn);
3efd08
+  assert_equal_string "%{}" (Var_expander.replace_fn "%{}" replace_none_fn);
3efd08
+  assert_equal_string "%{EMPTY}" (Var_expander.replace_fn "%{EMPTY}" replace_none_fn);
3efd08
+  assert_equal_string "%{EMPTY} %{no}" (Var_expander.replace_fn "%{EMPTY} %{no}" replace_none_fn);
3efd08
+  assert_equal_string "a %{EMPTY} b" (Var_expander.replace_fn "a %{EMPTY} b" replace_none_fn);
3efd08
+  ()
3efd08
+
3efd08
+let test_replacements () =
3efd08
+  assert_equal_string "" (Var_expander.replace_fn "%{EMPTY}" replace_empty_fn);
3efd08
+  assert_equal_string "x " (Var_expander.replace_fn "x %{EMPTY}" replace_empty_fn);
3efd08
+  assert_equal_string "xy" (Var_expander.replace_fn "x%{EMPTY}y" replace_empty_fn);
3efd08
+  assert_equal_string "x<->y" (Var_expander.replace_fn "x%{FOO}y" (function | "FOO" -> Some "<->" | _ -> None));
3efd08
+  assert_equal_string "a x b" (Var_expander.replace_fn "a %{FOO} b" (function | "FOO" -> Some "x" | _ -> None));
3efd08
+  assert_equal_string "%{FOO} x" (Var_expander.replace_fn "%{FOO} %{BAR}" (function | "BAR" -> Some "x" | _ -> None));
3efd08
+  assert_equal_string "%{FOO}" (Var_expander.replace_fn "%{BAR}" (function | "BAR" -> Some "%{FOO}" | _ -> None));
3efd08
+  assert_equal_string "%{FOO} x" (Var_expander.replace_fn "%{BAR} %{FOO}" (function | "BAR" -> Some "%{FOO}" | "FOO" -> Some "x" | _ -> None));
3efd08
+  begin
3efd08
+    let str = "%{INDEX}, %{INDEX}, %{INDEX}" in
3efd08
+    let index = ref 0 in
3efd08
+    let fn = function
3efd08
+      | "INDEX" ->
3efd08
+        incr index;
3efd08
+        Some (string_of_int !index)
3efd08
+      | _ -> None
3efd08
+    in
3efd08
+    assert_equal_string "1, 2, 3" (Var_expander.replace_fn str fn)
3efd08
+  end;
3efd08
+  ()
3efd08
+
3efd08
+let test_escape () =
3efd08
+  assert_equal_string "%%{FOO}" (Var_expander.replace_fn "%%{FOO}" replace_empty_fn);
3efd08
+  assert_equal_string "x %%{FOO} x" (Var_expander.replace_fn "%{FOO} %%{FOO} %{FOO}" (function | "FOO" -> Some "x" | _ -> None));
3efd08
+  ()
3efd08
+
3efd08
+let test_list () =
3efd08
+  assert_equal_string "x %{NONE}" (Var_expander.replace_list "%{FOO} %{NONE}" [("FOO", "x")]);
3efd08
+  ()
3efd08
+
3efd08
+let test_scan_variables () =
3efd08
+  let assert_invalid_variable var =
3efd08
+    let str = "%{" ^ var ^ "}" in
3efd08
+    assert_raises (Var_expander.Invalid_variable var)
3efd08
+                  (fun () -> Var_expander.scan_variables str)
3efd08
+  in
3efd08
+  assert_equal_stringlist [] (Var_expander.scan_variables "");
3efd08
+  assert_equal_stringlist [] (Var_expander.scan_variables "foo");
3efd08
+  assert_equal_stringlist ["FOO"] (Var_expander.scan_variables "%{FOO}");
3efd08
+  assert_equal_stringlist ["FOO"; "BAR"] (Var_expander.scan_variables "%{FOO} %{BAR}");
3efd08
+  assert_equal_stringlist ["FOO"; "BAR"] (Var_expander.scan_variables "%{FOO} %{BAR} %{FOO}");
3efd08
+  assert_equal_stringlist ["FOO"; "BAR"] (Var_expander.scan_variables "%{FOO} %%{ESCAPED} %{BAR}");
3efd08
+  assert_invalid_variable "FOO/BAR";
3efd08
+  ()
3efd08
+
3efd08
+let test_errors () =
3efd08
+  let assert_invalid_variable var =
3efd08
+    let str = "%{" ^ var ^ "}" in
3efd08
+    assert_raises (Var_expander.Invalid_variable var)
3efd08
+                  (fun () -> Var_expander.replace_fn str replace_none_fn)
3efd08
+  in
3efd08
+  assert_invalid_variable "FOO/BAR";
3efd08
+  assert_invalid_variable "FOO:BAR";
3efd08
+  assert_invalid_variable "FOO(BAR";
3efd08
+  assert_invalid_variable "FOO)BAR";
3efd08
+  assert_invalid_variable "FOO@BAR";
3efd08
+  ()
3efd08
+
3efd08
+(* Suites declaration. *)
3efd08
+let suite =
3efd08
+  TestList ([
3efd08
+    "basic" >::: [
3efd08
+      "no_replacement" >:: test_no_replacement;
3efd08
+      "replacements" >:: test_replacements;
3efd08
+      "escape" >:: test_escape;
3efd08
+      "list" >:: test_list;
3efd08
+      "scan_variables" >:: test_scan_variables;
3efd08
+      "errors" >:: test_errors;
3efd08
+    ];
3efd08
+  ])
3efd08
+
3efd08
+let () =
3efd08
+  ignore (run_test_tt_main suite);
3efd08
+  Printf.fprintf stderr "\n"
3efd08
-- 
da373f
2.18.4
3efd08