Pino Toscano
2014-Oct-22 15:30 UTC
[Libguestfs] [PATCH] tests: c-api: add $datadir and $databuilddir
Modify "tests" to expand any kind of environment variable than just $srcdir; use $datadir and $databuilddir to point to the files in $srcdir/tests/data and $builddir/tests/data, so it is easier to point at the data for the tests. Most of the work (except the $databuilddir and the integration in the Makefile.am) has been done as part of larger changes by Richard W.M. Jones <rjones@redhat.com>, see https://www.redhat.com/archives/libguestfs/2014-October/msg00044.html --- generator/actions.ml | 62 ++++++++++++++++++++++++------------------------ generator/tests_c_api.ml | 9 ++++--- tests/c-api/Makefile.am | 2 ++ tests/c-api/tests-main.c | 58 ++++++++++++++++++++++++++++++-------------- tests/c-api/tests.h | 2 +- 5 files changed, 78 insertions(+), 55 deletions(-) diff --git a/generator/actions.ml b/generator/actions.ml index 4cfba0d..370e6e0 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -2744,19 +2744,19 @@ data." }; style = RString "format", [String "filename"], []; tests = [ InitEmpty, Always, TestResultString ( - [["disk_format"; "../data/blank-disk-1s.raw"]], "raw"), []; + [["disk_format"; "$databuilddir/blank-disk-1s.raw"]], "raw"), []; InitEmpty, Always, TestResultString ( - [["disk_format"; "../data/blank-disk-1s.qcow2"]], "qcow2"), []; + [["disk_format"; "$databuilddir/blank-disk-1s.qcow2"]], "qcow2"), []; InitEmpty, Always, TestResultString ( - [["disk_format"; "../data/blank-disk-1K.raw"]], "raw"), []; + [["disk_format"; "$databuilddir/blank-disk-1K.raw"]], "raw"), []; InitEmpty, Always, TestResultString ( - [["disk_format"; "../data/blank-disk-1K.qcow2"]], "qcow2"), []; + [["disk_format"; "$databuilddir/blank-disk-1K.qcow2"]], "qcow2"), []; InitEmpty, Always, TestResultString ( - [["disk_format"; "../data/blank-disk-1M.raw"]], "raw"), []; + [["disk_format"; "$databuilddir/blank-disk-1M.raw"]], "raw"), []; InitEmpty, Always, TestResultString ( - [["disk_format"; "../data/blank-disk-1M.qcow2"]], "qcow2"), []; + [["disk_format"; "$databuilddir/blank-disk-1M.qcow2"]], "qcow2"), []; InitEmpty, Always, TestResultString ( - [["disk_format"; "../data/blank-disk-with-backing.qcow2"]], "qcow2"), []; + [["disk_format"; "$databuilddir/blank-disk-with-backing.qcow2"]], "qcow2"), []; ]; shortdesc = "detect the disk format of a disk image"; longdesc = "\ @@ -2774,19 +2774,19 @@ See also: L<guestfs(3)/DISK IMAGE FORMATS>" }; style = RInt64 "size", [String "filename"], []; tests = [ InitEmpty, Always, TestResult ( - [["disk_virtual_size"; "../data/blank-disk-1s.raw"]], "ret == 512"), []; + [["disk_virtual_size"; "$databuilddir/blank-disk-1s.raw"]], "ret == 512"), []; InitEmpty, Always, TestResult ( - [["disk_virtual_size"; "../data/blank-disk-1s.qcow2"]], "ret == 512"), []; + [["disk_virtual_size"; "$databuilddir/blank-disk-1s.qcow2"]], "ret == 512"), []; InitEmpty, Always, TestResult ( - [["disk_virtual_size"; "../data/blank-disk-1K.raw"]], "ret == 1024"), []; + [["disk_virtual_size"; "$databuilddir/blank-disk-1K.raw"]], "ret == 1024"), []; InitEmpty, Always, TestResult ( - [["disk_virtual_size"; "../data/blank-disk-1K.qcow2"]], "ret == 1024"), []; + [["disk_virtual_size"; "$databuilddir/blank-disk-1K.qcow2"]], "ret == 1024"), []; InitEmpty, Always, TestResult ( - [["disk_virtual_size"; "../data/blank-disk-1M.raw"]], "ret == 1024*1024"), []; + [["disk_virtual_size"; "$databuilddir/blank-disk-1M.raw"]], "ret == 1024*1024"), []; InitEmpty, Always, TestResult ( - [["disk_virtual_size"; "../data/blank-disk-1M.qcow2"]], "ret == 1024*1024"), []; + [["disk_virtual_size"; "$databuilddir/blank-disk-1M.qcow2"]], "ret == 1024*1024"), []; InitEmpty, Always, TestResult ( - [["disk_virtual_size"; "../data/blank-disk-with-backing.qcow2"]], "ret == 1024*1024"), []; + [["disk_virtual_size"; "$databuilddir/blank-disk-with-backing.qcow2"]], "ret == 1024*1024"), []; ]; shortdesc = "return virtual size of a disk"; longdesc = "\ @@ -2801,19 +2801,19 @@ circumstances. See L<guestfs(3)/CVE-2010-3851>." }; style = RBool "backingfile", [String "filename"], []; tests = [ InitEmpty, Always, TestResultFalse ( - [["disk_has_backing_file"; "../data/blank-disk-1s.raw"]]), []; + [["disk_has_backing_file"; "$databuilddir/blank-disk-1s.raw"]]), []; InitEmpty, Always, TestResultFalse ( - [["disk_has_backing_file"; "../data/blank-disk-1s.qcow2"]]), []; + [["disk_has_backing_file"; "$databuilddir/blank-disk-1s.qcow2"]]), []; InitEmpty, Always, TestResultFalse ( - [["disk_has_backing_file"; "../data/blank-disk-1K.raw"]]), []; + [["disk_has_backing_file"; "$databuilddir/blank-disk-1K.raw"]]), []; InitEmpty, Always, TestResultFalse ( - [["disk_has_backing_file"; "../data/blank-disk-1K.qcow2"]]), []; + [["disk_has_backing_file"; "$databuilddir/blank-disk-1K.qcow2"]]), []; InitEmpty, Always, TestResultFalse ( - [["disk_has_backing_file"; "../data/blank-disk-1M.raw"]]), []; + [["disk_has_backing_file"; "$databuilddir/blank-disk-1M.raw"]]), []; InitEmpty, Always, TestResultFalse ( - [["disk_has_backing_file"; "../data/blank-disk-1M.qcow2"]]), []; + [["disk_has_backing_file"; "$databuilddir/blank-disk-1M.qcow2"]]), []; InitEmpty, Always, TestResultTrue ( - [["disk_has_backing_file"; "../data/blank-disk-with-backing.qcow2"]]), []; + [["disk_has_backing_file"; "$databuilddir/blank-disk-with-backing.qcow2"]]), []; ]; shortdesc = "return whether disk has a backing file"; longdesc = "\ @@ -4735,15 +4735,15 @@ To get the checksums for many files, use C<guestfs_checksums_out>." }; tests = [ InitScratchFS, Always, TestResultString ( [["mkdir"; "/tar_in"]; - ["tar_in"; "$srcdir/../data/helloworld.tar"; "/tar_in"; "NOARG"]; + ["tar_in"; "$datadir/helloworld.tar"; "/tar_in"; "NOARG"]; ["cat"; "/tar_in/hello"]], "hello\n"), []; InitScratchFS, Always, TestResultString ( [["mkdir"; "/tar_in_gz"]; - ["tar_in"; "$srcdir/../data/helloworld.tar.gz"; "/tar_in_gz"; "gzip"]; + ["tar_in"; "$datadir/helloworld.tar.gz"; "/tar_in_gz"; "gzip"]; ["cat"; "/tar_in_gz/hello"]], "hello\n"), []; InitScratchFS, IfAvailable "xz", TestResultString ( [["mkdir"; "/tar_in_xz"]; - ["tar_in"; "$srcdir/../data/helloworld.tar.xz"; "/tar_in_xz"; "xz"]; + ["tar_in"; "$datadir/helloworld.tar.xz"; "/tar_in_xz"; "xz"]; ["cat"; "/tar_in_xz/hello"]], "hello\n"), [] ]; shortdesc = "unpack tarfile to directory"; @@ -4800,7 +4800,7 @@ instead of user/group names. tests = [ InitScratchFS, Always, TestResultString ( [["mkdir"; "/tgz_in"]; - ["tgz_in"; "$srcdir/../data/helloworld.tar.gz"; "/tgz_in"]; + ["tgz_in"; "$datadir/helloworld.tar.gz"; "/tgz_in"]; ["cat"; "/tgz_in/hello"]], "hello\n"), [] ]; shortdesc = "unpack compressed tarball to directory"; @@ -8130,7 +8130,7 @@ or growing unnecessarily." }; tests = [ InitScratchFS, Always, TestResultString ( [["mkdir"; "/txz_in"]; - ["txz_in"; "$srcdir/../data/helloworld.tar.xz"; "/txz_in"]; + ["txz_in"; "$datadir/helloworld.tar.xz"; "/txz_in"]; ["cat"; "/txz_in/hello"]], "hello\n"), [] ]; shortdesc = "unpack compressed tarball to directory"; @@ -8242,7 +8242,7 @@ types (see C<guestfs_part_get_parttype>)." }; tests = [ InitISOFS, Always, TestResult ( [["checksum_device"; "md5"; "/dev/sdd"]], - "check_file_md5 (ret, \"../data/test.iso\") == 0"), [] + "check_file_md5 (ret, \"$databuilddir/test.iso\") == 0"), [] ]; shortdesc = "compute MD5, SHAx or CRC checksum of the contents of a device"; longdesc = "\ @@ -8316,7 +8316,7 @@ to find out what it is for." }; cancellable = true; tests = [ InitScratchFS, Always, TestResultString ( - [["base64_in"; "../data/hello.b64"; "/base64_in"]; + [["base64_in"; "$databuilddir/hello.b64"; "/base64_in"]; ["cat"; "/base64_in"]], "hello\n"), [] ]; shortdesc = "upload base64-encoded data to file"; @@ -10708,7 +10708,7 @@ C<guestfs_xfs_growfs> calls." }; optional = Some "hivex"; tests = [ InitScratchFS, Always, TestRun ( - [["upload"; "$srcdir/../data/minimal"; "/hivex_open"]; + [["upload"; "$datadir/minimal"; "/hivex_open"]; ["hivex_open"; "/hivex_open"; ""; ""; "false"]; ["hivex_root"]; (* in this hive, it returns 0x1020 *) ["hivex_node_name"; "0x1020"]; @@ -10856,11 +10856,11 @@ See also: C<guestfs_hivex_value_utf8>." }; optional = Some "hivex"; tests = [ InitScratchFS, Always, TestRun ( - [["upload"; "$srcdir/../data/minimal"; "/hivex_commit1"]; + [["upload"; "$datadir/minimal"; "/hivex_commit1"]; ["hivex_open"; "/hivex_commit1"; ""; ""; "true"]; ["hivex_commit"; "NULL"]]), [["hivex_close"]]; InitScratchFS, Always, TestResultTrue ( - [["upload"; "$srcdir/../data/minimal"; "/hivex_commit2"]; + [["upload"; "$datadir/minimal"; "/hivex_commit2"]; ["hivex_open"; "/hivex_commit2"; ""; ""; "true"]; ["hivex_commit"; "/hivex_commit2_copy"]; ["is_file"; "/hivex_commit2_copy"; "false"]]), [["hivex_close"]] diff --git a/generator/tests_c_api.ml b/generator/tests_c_api.ml index 88aa07e..86bf08e 100644 --- a/generator/tests_c_api.ml +++ b/generator/tests_c_api.ml @@ -385,17 +385,16 @@ and generate_test_command_call ?(expect_error = false) ?test ?ret test_name cmd | String _, arg, sym | OptString _, arg, sym | Key _, arg, sym - | GUID _, arg, sym -> - pr " const char *%s = \"%s\";\n" sym (c_quote arg); + | GUID _, arg, sym + | FileIn _, arg, sym -> + pr " CLEANUP_FREE char *%s = substitute_environment (\"%s\");\n" + sym (c_quote arg) | BufferIn _, arg, sym -> pr " const char *%s = \"%s\";\n" sym (c_quote arg); pr " size_t %s_size = %d;\n" sym (String.length arg) | Int _, _, _ | Int64 _, _, _ | Bool _, _, _ -> () - | FileIn _, arg, sym -> - pr " CLEANUP_FREE char *%s = substitute_srcdir (\"%s\");\n" - sym (c_quote arg) | FileOut _, _, _ -> () | StringList _, "", sym | DeviceList _, "", sym -> diff --git a/tests/c-api/Makefile.am b/tests/c-api/Makefile.am index 6ea22e9..17c88d7 100644 --- a/tests/c-api/Makefile.am +++ b/tests/c-api/Makefile.am @@ -69,6 +69,8 @@ TESTS_ENVIRONMENT = \ SKIP_TEST_COMMAND=$(shell ldd test-command | grep -sq 'not a dynamic executable' || echo 1) \ SKIP_TEST_COMMAND_LINES=$(shell ldd test-command | grep -sq 'not a dynamic executable' || echo 1) \ SKIP_TEST_COMMAND=$(shell ldd test-pwd | grep -sq 'not a dynamic executable' || echo 1) \ + datadir=$(srcdir)/../data \ + databuilddir=$(builddir)/../data \ $(top_builddir)/run --test $(VG) #SKIP_TEST_CHECKSUM_8=$(shell if test `find ../initramfs -name squashfs.ko | wc -l` -eq 0; then echo 1; fi) diff --git a/tests/c-api/tests-main.c b/tests/c-api/tests-main.c index e81e15e..986894d 100644 --- a/tests/c-api/tests-main.c +++ b/tests/c-api/tests-main.c @@ -357,35 +357,45 @@ match_re (const char *str, const char *pattern) return r != PCRE_ERROR_NOMATCH; } -/* Used for FileIn parameters in tests. If the path starts with - * "$srcdir" then replace that with the contents of the $srcdir - * environment variable (this is set by automake and run time). The - * caller must free the returned string. +/* Used for some parameters in tests. If the string starts with + * "$variable" then replace that with the contents of the named + * environment variable. The caller must free the returned string. */ char * -substitute_srcdir (const char *path) +substitute_environment (const char *str) { char *ret; + size_t len; + CLEANUP_FREE char *name = NULL; + const char *value; + + if (STRPREFIX (str, "$")) { + len = strspn (str+1, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"); + if (len == 0) { + fprintf (stderr, "tests: invalid environment variable in string (%s)\n", + str); + exit (EXIT_FAILURE); + } + name = strndup (str+1, len); + if (name == NULL) { + perror ("strndup"); + exit (EXIT_FAILURE); + } - if (STRPREFIX (path, "$srcdir")) { - const char *srcdir; - - srcdir = getenv ("srcdir"); - if (!srcdir) { - fprintf (stderr, "tests: environment variable $srcdir is not defined.\n" - "Normally it is defined by automake. If you are running the\n" - "tests directly, set $srcdir to point to the source tests/c-api\n" - "directory.\n"); + value = getenv (name); + if (!value) { + fprintf (stderr, "tests: environment variable $%s is not defined.\n", + name); exit (EXIT_FAILURE); } - if (asprintf (&ret, "%s%s", srcdir, path + 7) == -1) { + if (asprintf (&ret, "%s%s", value, str + 1 + len) == -1) { perror ("asprintf"); exit (EXIT_FAILURE); } } else { - ret = strdup (path); + ret = strdup (str); if (!ret) { perror ("strdup"); exit (EXIT_FAILURE); @@ -424,6 +434,8 @@ static guestfs_h * create_handle (void) { guestfs_h *g; + const char *databuilddir; + CLEANUP_FREE char *test_iso = NULL; g = guestfs_create (); if (g == NULL) { @@ -446,8 +458,18 @@ create_handle (void) exit (EXIT_FAILURE); } - if (guestfs_add_drive_ro (g, "../data/test.iso") == -1) { - printf ("FAIL: guestfs_add_drive_ro ../data/test.iso\n"); + databuilddir = getenv ("databuilddir"); + if (databuilddir == NULL) { + fprintf (stderr, "environment variable $databuilddir is not defined\n"); + exit (EXIT_FAILURE); + } + if (asprintf (&test_iso, "%s/test.iso", databuilddir) == -1) { + perror ("asprintf"); + exit (EXIT_FAILURE); + } + + if (guestfs_add_drive_ro (g, test_iso) == -1) { + printf ("FAIL: guestfs_add_drive_ro $databuilddir/test.iso\n"); exit (EXIT_FAILURE); } diff --git a/tests/c-api/tests.h b/tests/c-api/tests.h index 7959570..129aee6 100644 --- a/tests/c-api/tests.h +++ b/tests/c-api/tests.h @@ -43,7 +43,7 @@ extern int check_file_md5 (const char *ret, const char *filename); extern const char *get_key (char **hash, const char *key); extern int check_hash (char **ret, const char *key, const char *expected); extern int match_re (const char *str, const char *pattern); -extern char *substitute_srcdir (const char *path); +extern char *substitute_environment (const char *str); extern void skipped (const char *test_name, const char *fs, ...) __attribute__((format (printf,2,3))); #endif /* TESTS_H_ */ -- 1.9.3
Seemingly Similar Threads
- [PATCH v5 2/7] tests/c-api: Convert the C API tests to use the test harness.
- [PATCH v4 02/17] tests/c-api: Convert the C API tests to use the test harness.
- [PATCH 1/2] generator: Simplify the handling of string parameters.
- [PATCH] generator: Add an explicit Cancellable flag
- [PATCH] generator: Allow actions to be deprecated with no replacement.