[libvirt] [PATCH v2 0/8] tests: Mostly fix 'make check' on FreeBSD

Changes from [v1]: * drop the virnetsockettest changes, since the relationship between the errors seen on FreeBSD and IPv4 clients connecting to IPv6 sockets has turned out to be just a red herring; * fix the mocking instead of dropping test cases that don't work without it. [v1] https://www.redhat.com/archives/libvir-list/2018-April/msg02611.html Andrea Bolognani (8): test: Introduce VIR_MOCK_REAL_INIT_VERSIONED() tests: Mock realpath() all: Use realpath() instead of canonicalize_file_name() tests: Stop mocking canonicalize_file_name() gnulib: Drop canonicalize-lgpl module syntax-check: Prohibit canonicalize_file_name() tests: Fix mode_t usage with va_arg() tests: Build virpcimock on non-Linux bootstrap.conf | 1 - cfg.mk | 10 ++++ src/storage/storage_backend_fs.c | 2 +- src/util/virfile.c | 2 +- src/util/virpci.c | 2 +- tests/virmock.h | 13 +++++ tests/virpcimock.c | 118 +++++++++++++++++++++++---------------- tests/virstoragetest.c | 10 ++-- tests/virtestmock.c | 13 ++--- 9 files changed, 107 insertions(+), 64 deletions(-) -- 2.14.3

This will be used later on to choose the modern implementation of the functions we need to mock instead of the default, and broken, one. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/virmock.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/virmock.h b/tests/virmock.h index 914d5141c9..d35b1176fc 100644 --- a/tests/virmock.h +++ b/tests/virmock.h @@ -302,4 +302,17 @@ } \ } while (0) +# define VIR_MOCK_REAL_INIT_VERSIONED(name, version) \ + do { \ + if (real_##name == NULL && \ + !(real_##name = dlvsym(RTLD_NEXT, \ + #name, \ + version))) { \ + fprintf(stderr, "Missing symbol '%s@%s'\n", \ + #name, \ + version); \ + abort(); \ + } \ + } while (0) + #endif /* __VIR_MOCK_H__ */ -- 2.14.3

We need to use VIR_MOCK_REAL_INIT_VERSIONED() and ask for a specific version of the symbol here, because the implementation dlsym() returns by default on Linux is unsuitable for our use. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/virpcimock.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/virpcimock.c b/tests/virpcimock.c index 176c64d654..9a5e6b4cec 100644 --- a/tests/virpcimock.c +++ b/tests/virpcimock.c @@ -43,6 +43,7 @@ static char *(*real_canonicalize_file_name)(const char *path); static int (*real_open)(const char *path, int flags, ...); static int (*real_close)(int fd); static DIR * (*real_opendir)(const char *name); +static char *(*real_realpath)(const char *path, char *resolved); /* Don't make static, since it causes problems with clang * when passed as an arg to virAsprintf() @@ -814,6 +815,20 @@ init_syms(void) VIR_MOCK_REAL_INIT(open); VIR_MOCK_REAL_INIT(close); VIR_MOCK_REAL_INIT(opendir); + + /* When linking on Linux, the default implementation of realpath() is + * realpath@GLIBC_2.3; when using dlsym(), however, we get the older + * realpath@GLIBC_2.2.5 instead, which unfortunately doesn't support + * passing NULL as the second parameter. + * + * Ask for a versioned symbol to make sure we get a working realpath() + * on Linux; other operating systems such as FreeBSD don't suffer from + * the same limitation and can rely on the default dlsym() behavior */ +# ifdef __linux__ + VIR_MOCK_REAL_INIT_VERSIONED(realpath, "GLIBC_2.3"); +# else + VIR_MOCK_REAL_INIT(realpath); +# endif } static void @@ -1046,6 +1061,25 @@ close(int fd) return -1; return real_close(fd); } + +char * +realpath(const char *path, char *resolved) +{ + char *ret; + + init_syms(); + + if (STRPREFIX(path, SYSFS_PCI_PREFIX)) { + char *newpath; + if (getrealpath(&newpath, path) < 0) + return NULL; + ret = real_realpath(newpath, resolved); + VIR_FREE(newpath); + } else { + ret = real_realpath(path, resolved); + } + return ret; +} #else /* Nothing to override on non-__linux__ platforms */ #endif -- 2.14.3

The latter is a glibc extension that's not available on other operating systems, notably FreeBSD. So far we have worked around the issue through gnulib, but that makes it difficult to use mocking in our test suite, so just drop it in favor of the portable alternative. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- src/storage/storage_backend_fs.c | 2 +- src/util/virfile.c | 2 +- src/util/virpci.c | 2 +- tests/virstoragetest.c | 10 +++++----- tests/virtestmock.c | 13 ++++++------- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c index 9b0fcf92c5..37f02f0712 100644 --- a/src/storage/storage_backend_fs.c +++ b/src/storage/storage_backend_fs.c @@ -823,7 +823,7 @@ virStorageFileBackendFileGetUniqueIdentifier(virStorageSourcePtr src) virStorageFileBackendFsPrivPtr priv = src->drv->priv; if (!priv->canonpath) { - if (!(priv->canonpath = canonicalize_file_name(src->path))) { + if (!(priv->canonpath = realpath(src->path, NULL))) { virReportSystemError(errno, _("can't canonicalize path '%s'"), src->path); return NULL; diff --git a/src/util/virfile.c b/src/util/virfile.c index e12a584ca1..270906a009 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -1597,7 +1597,7 @@ virFileResolveLinkHelper(const char *linkpath, return VIR_STRDUP_QUIET(*resultpath, linkpath) < 0 ? -1 : 0; } - *resultpath = canonicalize_file_name(linkpath); + *resultpath = realpath(linkpath, NULL); return *resultpath == NULL ? -1 : 0; } diff --git a/src/util/virpci.c b/src/util/virpci.c index 23932558e9..1108d2a7c1 100644 --- a/src/util/virpci.c +++ b/src/util/virpci.c @@ -2622,7 +2622,7 @@ virPCIGetDeviceAddressFromSysfsLink(const char *device_link) return NULL; } - device_path = canonicalize_file_name(device_link); + device_path = realpath(device_link, NULL); if (device_path == NULL) { virReportSystemError(errno, _("Failed to resolve device link '%s'"), diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c index b032d8b93f..ff95292698 100644 --- a/tests/virstoragetest.c +++ b/tests/virstoragetest.c @@ -171,7 +171,7 @@ testPrepImages(void) fprintf(stderr, "unable to create directory %s\n", datadir "/dir"); goto cleanup; } - if (!(canondir = canonicalize_file_name(absdir))) { + if (!(canondir = realpath(absdir, NULL))) { virReportOOMError(); goto cleanup; } @@ -186,7 +186,7 @@ testPrepImages(void) fprintf(stderr, "unable to create raw file\n"); goto cleanup; } - if (!(canonraw = canonicalize_file_name(absraw))) { + if (!(canonraw = realpath(absraw, NULL))) { virReportOOMError(); goto cleanup; } @@ -206,7 +206,7 @@ testPrepImages(void) "-F", "raw", "-b", "raw", "qcow2", NULL); if (virCommandRun(cmd, NULL) < 0) goto skip; - if (!(canonqcow2 = canonicalize_file_name(absqcow2))) { + if (!(canonqcow2 = realpath(absqcow2, NULL))) { virReportOOMError(); goto cleanup; } @@ -220,7 +220,7 @@ testPrepImages(void) virCommandAddArg(cmd, "wrap"); if (virCommandRun(cmd, NULL) < 0) goto skip; - if (!(canonwrap = canonicalize_file_name(abswrap))) { + if (!(canonwrap = realpath(abswrap, NULL))) { virReportOOMError(); goto cleanup; } @@ -233,7 +233,7 @@ testPrepImages(void) virCommandAddArg(cmd, "qed"); if (virCommandRun(cmd, NULL) < 0) goto skip; - if (!(canonqed = canonicalize_file_name(absqed))) { + if (!(canonqed = realpath(absqed, NULL))) { virReportOOMError(); goto cleanup; } diff --git a/tests/virtestmock.c b/tests/virtestmock.c index 688945d805..9dcff1bd75 100644 --- a/tests/virtestmock.c +++ b/tests/virtestmock.c @@ -133,12 +133,11 @@ checkPath(const char *path) virAsprintfQuiet(&relPath, "./%s", path) < 0) goto error; - /* Le sigh. Both canonicalize_file_name() and realpath() - * expect @path to exist otherwise they return an error. So - * if we are called over an non-existent file, this could - * return an error. In that case do our best and hope we will - * catch possible error. */ - if ((fullPath = canonicalize_file_name(relPath ? relPath : path))) { + /* Le sigh. realpath() expects @path to exist, otherwise it will + * return an error. So if we are called over an non-existent file, + * this could return an error. In that case do our best and hope we + * will catch possible errors. */ + if ((fullPath = realpath(relPath ? relPath : path, NULL))) { path = fullPath; } else { /* Yeah, our worst nightmares just became true. Path does @@ -148,7 +147,7 @@ checkPath(const char *path) virFileRemoveLastComponent(crippledPath); - if ((fullPath = canonicalize_file_name(crippledPath))) + if ((fullPath = realpath(crippledPath, NULL))) path = fullPath; } -- 2.14.3

On Mon, Apr 30, 2018 at 06:52:58PM +0200, Andrea Bolognani wrote:
The latter is a glibc extension that's not available on other operating systems, notably FreeBSD.
So far we have worked around the issue through gnulib, but that makes it difficult to use mocking in our test suite, so just drop it in favor of the portable alternative.
Sigh, unfortunately realpath() has its own portability problems in that passing NULL as second arg was a glibc invention, which is why we switched from realpath() to canonicalize_file_name() in the first place ! So I wonder if this actually works on OS-X, Mingw and FreeBSD, or whether you didn't see the bug because it is mocked in the tests ?
Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- src/storage/storage_backend_fs.c | 2 +- src/util/virfile.c | 2 +- src/util/virpci.c | 2 +- tests/virstoragetest.c | 10 +++++----- tests/virtestmock.c | 13 ++++++------- 5 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c index 9b0fcf92c5..37f02f0712 100644 --- a/src/storage/storage_backend_fs.c +++ b/src/storage/storage_backend_fs.c @@ -823,7 +823,7 @@ virStorageFileBackendFileGetUniqueIdentifier(virStorageSourcePtr src) virStorageFileBackendFsPrivPtr priv = src->drv->priv;
if (!priv->canonpath) { - if (!(priv->canonpath = canonicalize_file_name(src->path))) { + if (!(priv->canonpath = realpath(src->path, NULL))) { virReportSystemError(errno, _("can't canonicalize path '%s'"), src->path); return NULL; diff --git a/src/util/virfile.c b/src/util/virfile.c index e12a584ca1..270906a009 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -1597,7 +1597,7 @@ virFileResolveLinkHelper(const char *linkpath, return VIR_STRDUP_QUIET(*resultpath, linkpath) < 0 ? -1 : 0; }
- *resultpath = canonicalize_file_name(linkpath); + *resultpath = realpath(linkpath, NULL);
return *resultpath == NULL ? -1 : 0; } diff --git a/src/util/virpci.c b/src/util/virpci.c index 23932558e9..1108d2a7c1 100644 --- a/src/util/virpci.c +++ b/src/util/virpci.c @@ -2622,7 +2622,7 @@ virPCIGetDeviceAddressFromSysfsLink(const char *device_link) return NULL; }
- device_path = canonicalize_file_name(device_link); + device_path = realpath(device_link, NULL); if (device_path == NULL) { virReportSystemError(errno, _("Failed to resolve device link '%s'"), diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c index b032d8b93f..ff95292698 100644 --- a/tests/virstoragetest.c +++ b/tests/virstoragetest.c @@ -171,7 +171,7 @@ testPrepImages(void) fprintf(stderr, "unable to create directory %s\n", datadir "/dir"); goto cleanup; } - if (!(canondir = canonicalize_file_name(absdir))) { + if (!(canondir = realpath(absdir, NULL))) { virReportOOMError(); goto cleanup; } @@ -186,7 +186,7 @@ testPrepImages(void) fprintf(stderr, "unable to create raw file\n"); goto cleanup; } - if (!(canonraw = canonicalize_file_name(absraw))) { + if (!(canonraw = realpath(absraw, NULL))) { virReportOOMError(); goto cleanup; } @@ -206,7 +206,7 @@ testPrepImages(void) "-F", "raw", "-b", "raw", "qcow2", NULL); if (virCommandRun(cmd, NULL) < 0) goto skip; - if (!(canonqcow2 = canonicalize_file_name(absqcow2))) { + if (!(canonqcow2 = realpath(absqcow2, NULL))) { virReportOOMError(); goto cleanup; } @@ -220,7 +220,7 @@ testPrepImages(void) virCommandAddArg(cmd, "wrap"); if (virCommandRun(cmd, NULL) < 0) goto skip; - if (!(canonwrap = canonicalize_file_name(abswrap))) { + if (!(canonwrap = realpath(abswrap, NULL))) { virReportOOMError(); goto cleanup; } @@ -233,7 +233,7 @@ testPrepImages(void) virCommandAddArg(cmd, "qed"); if (virCommandRun(cmd, NULL) < 0) goto skip; - if (!(canonqed = canonicalize_file_name(absqed))) { + if (!(canonqed = realpath(absqed, NULL))) { virReportOOMError(); goto cleanup; } diff --git a/tests/virtestmock.c b/tests/virtestmock.c index 688945d805..9dcff1bd75 100644 --- a/tests/virtestmock.c +++ b/tests/virtestmock.c @@ -133,12 +133,11 @@ checkPath(const char *path) virAsprintfQuiet(&relPath, "./%s", path) < 0) goto error;
- /* Le sigh. Both canonicalize_file_name() and realpath() - * expect @path to exist otherwise they return an error. So - * if we are called over an non-existent file, this could - * return an error. In that case do our best and hope we will - * catch possible error. */ - if ((fullPath = canonicalize_file_name(relPath ? relPath : path))) { + /* Le sigh. realpath() expects @path to exist, otherwise it will + * return an error. So if we are called over an non-existent file, + * this could return an error. In that case do our best and hope we + * will catch possible errors. */ + if ((fullPath = realpath(relPath ? relPath : path, NULL))) { path = fullPath; } else { /* Yeah, our worst nightmares just became true. Path does @@ -148,7 +147,7 @@ checkPath(const char *path)
virFileRemoveLastComponent(crippledPath);
- if ((fullPath = canonicalize_file_name(crippledPath))) + if ((fullPath = realpath(crippledPath, NULL))) path = fullPath; }
-- 2.14.3
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Wed, May 02, 2018 at 05:32:25PM +0100, Daniel P. Berrangé wrote:
On Mon, Apr 30, 2018 at 06:52:58PM +0200, Andrea Bolognani wrote:
The latter is a glibc extension that's not available on other operating systems, notably FreeBSD.
So far we have worked around the issue through gnulib, but that makes it difficult to use mocking in our test suite, so just drop it in favor of the portable alternative.
Sigh, unfortunately realpath() has its own portability problems in that passing NULL as second arg was a glibc invention, which is why we switched from realpath() to canonicalize_file_name() in the first place !
So I wonder if this actually works on OS-X, Mingw and FreeBSD, or whether you didn't see the bug because it is mocked in the tests ?
Hmm, another idea - if the problem is that we can't easily mock canonicalize_file_name() why don't we just provide a trivial wrapper. eg virFileCanonicalize() which does nothing except call canonicalize_file_name(). We can then just mock the virFileCanonicalize() method instead. This would nicely avoid the pain with dealing with versioned symbols for realpath too. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Wed, 2018-05-02 at 17:32 +0100, Daniel P. Berrangé wrote:
On Mon, Apr 30, 2018 at 06:52:58PM +0200, Andrea Bolognani wrote:
The latter is a glibc extension that's not available on other operating systems, notably FreeBSD.
So far we have worked around the issue through gnulib, but that makes it difficult to use mocking in our test suite, so just drop it in favor of the portable alternative.
Sigh, unfortunately realpath() has its own portability problems in that passing NULL as second arg was a glibc invention, which is why we switched from realpath() to canonicalize_file_name() in the first place !
So I wonder if this actually works on OS-X, Mingw and FreeBSD, or whether you didn't see the bug because it is mocked in the tests ?
The FreeBSD[1] and macOS[2] releases we care about ship a sensible implementation of realpath(), which explicitly allows passing NULL as the second parameter. Not sure about MinGW, but doesn't it ship basically a recompiled version of glibc? If so, I would expect it to be perfectly fine too. I should also note that I've successfully run the test suite, with these changes applied, on all CI platforms, so we know there's no Linux distribution we care about shipping an old enough glibc that wouldn't include a reasonable realpath(). As for the mocking hiding the issue, we end up calling the actual realpath() at the end of the day, just on a different file... So I wouldn't expect that to invalidate the results. [1] https://www.freebsd.org/cgi/man.cgi?query=realpath&apropos=0&sektion=3&manpath=FreeBSD+10.4-RELEASE&arch=default&format=html [2] https://developer.apple.com/legacy/library/documentation/Darwin/Reference/Ma... -- Andrea Bolognani / Red Hat / Virtualization

On Wednesday, 2 May 2018 18:32:25 CEST Daniel P. Berrangé wrote:
On Mon, Apr 30, 2018 at 06:52:58PM +0200, Andrea Bolognani wrote:
The latter is a glibc extension that's not available on other operating systems, notably FreeBSD.
So far we have worked around the issue through gnulib, but that makes it difficult to use mocking in our test suite, so just drop it in favor of the portable alternative.
Sigh, unfortunately realpath() has its own portability problems in that passing NULL as second arg was a glibc invention, which is why we switched from realpath() to canonicalize_file_name() in the first place !
It was, but then this behaviour was standardized in POSIX 2007: http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
So I wonder if this actually works on OS-X, Mingw and FreeBSD, or whether you didn't see the bug because it is mocked in the tests ?
Most probably because they implement the realpath(.., NULL) semantics, as described by POSIX. -- Pino Toscano

Since we've dropped all usages from the library, mocking it has become entirely pointless. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/virpcimock.c | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/tests/virpcimock.c b/tests/virpcimock.c index 9a5e6b4cec..001b320683 100644 --- a/tests/virpcimock.c +++ b/tests/virpcimock.c @@ -39,7 +39,6 @@ static int (*real_lstat)(const char *path, struct stat *sb); static int (*real___lxstat)(int ver, const char *path, struct stat *sb); static int (*real_stat)(const char *path, struct stat *sb); static int (*real___xstat)(int ver, const char *path, struct stat *sb); -static char *(*real_canonicalize_file_name)(const char *path); static int (*real_open)(const char *path, int flags, ...); static int (*real_close)(int fd); static DIR * (*real_opendir)(const char *name); @@ -811,7 +810,6 @@ init_syms(void) VIR_MOCK_REAL_INIT(access); VIR_MOCK_REAL_INIT_ALT(lstat, __lxstat); VIR_MOCK_REAL_INIT_ALT(stat, __xstat); - VIR_MOCK_REAL_INIT(canonicalize_file_name); VIR_MOCK_REAL_INIT(open); VIR_MOCK_REAL_INIT(close); VIR_MOCK_REAL_INIT(opendir); @@ -982,25 +980,6 @@ stat(const char *path, struct stat *sb) return ret; } -char * -canonicalize_file_name(const char *path) -{ - char *ret; - - init_syms(); - - if (STRPREFIX(path, SYSFS_PCI_PREFIX)) { - char *newpath; - if (getrealpath(&newpath, path) < 0) - return NULL; - ret = real_canonicalize_file_name(newpath); - VIR_FREE(newpath); - } else { - ret = real_canonicalize_file_name(path); - } - return ret; -} - int open(const char *path, int flags, ...) { -- 2.14.3

We're no longer using canonicalize_file_name(), so we have no use for the gnulib module any more. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- bootstrap.conf | 1 - 1 file changed, 1 deletion(-) diff --git a/bootstrap.conf b/bootstrap.conf index 9559922fce..fb56872c4d 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -29,7 +29,6 @@ c-ctype c-strcase c-strcasestr calloc-posix -canonicalize-lgpl chown clock-time close -- 2.14.3

Now what we have removed all uses of the function, we want to prevent it from creeping back in. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- cfg.mk | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cfg.mk b/cfg.mk index c3d18279ce..7ffa10f563 100644 --- a/cfg.mk +++ b/cfg.mk @@ -462,6 +462,13 @@ sc_prohibit_ctype_h: halt='use c-ctype.h instead of ctype.h' \ $(_sc_search_regexp) +# canonicalize_file_name() is only available in glibc, and the gnulib +# version is difficult to mock; realpath() suffers from neither limitation +sc_prohibit_canonicalize_file_name: + @prohibit='\<canonicalize_file_name\(' \ + halt='use realpath(path, NULL) instead of canonicalize_file_name(path)' \ + $(_sc_search_regexp) + # Insist on correct types for [pug]id. sc_correct_id_types: @prohibit='\<(int|long) *[pug]id\>' \ @@ -1208,6 +1215,9 @@ exclude_file_name_regexp--sc_prohibit_nonreentrant = \ exclude_file_name_regexp--sc_prohibit_select = \ ^cfg\.mk$$ +exclude_file_name_regexp--sc_prohibit_canonicalize_file_name = \ + ^cfg\.mk$$ + exclude_file_name_regexp--sc_prohibit_raw_allocation = \ ^(docs/hacking\.html\.in|src/util/viralloc\.[ch]|examples/.*|tests/(securityselinuxhelper|(vircgroup|nss)mock|commandhelper)\.c|tools/wireshark/src/packet-libvirt\.c)$$ -- 2.14.3

Clang complains about it: virfilewrapper.c:270:27: error: second argument to 'va_arg' is of promotable type 'mode_t' (aka 'unsigned short'); this va_arg has undefined behavior because arguments will be promoted to 'int' [-Werror,-Wvarargs] mode = va_arg(ap, mode_t); ^~~~~~ The issue was raised[1] when the patch was posted, but apparently not acted upon. Implement the suggested fix. [1] https://www.redhat.com/archives/libvir-list/2017-October/msg00127.html Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/virpcimock.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/virpcimock.c b/tests/virpcimock.c index 001b320683..915327e91e 100644 --- a/tests/virpcimock.c +++ b/tests/virpcimock.c @@ -994,11 +994,11 @@ open(const char *path, int flags, ...) if (flags & O_CREAT) { va_list ap; - mode_t mode; + int mode; va_start(ap, flags); - mode = va_arg(ap, mode_t); + mode = va_arg(ap, int); va_end(ap); - ret = real_open(newpath ? newpath : path, flags, mode); + ret = real_open(newpath ? newpath : path, flags, (mode_t) mode); } else { ret = real_open(newpath ? newpath : path, flags); } -- 2.14.3

There are only a couple issues preventing it from working on other platform such as FreeBSD. Let's fix them. With the mocking in place, qemumemlocktest and qemuxml2xmltest can finally succeed on FreeBSD. This commit is best viewed with 'git show -w'. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/virpcimock.c | 63 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/tests/virpcimock.c b/tests/virpcimock.c index 915327e91e..4f73ccbc3c 100644 --- a/tests/virpcimock.c +++ b/tests/virpcimock.c @@ -20,19 +20,18 @@ #include <config.h> -#ifdef __linux__ -# include "virmock.h" -# include <stdio.h> -# include <stdlib.h> -# include <unistd.h> -# include <fcntl.h> -# include <sys/stat.h> -# include <stdarg.h> -# include <dirent.h> -# include "viralloc.h" -# include "virstring.h" -# include "virfile.h" -# include "dirname.h" +#include "virmock.h" +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <stdarg.h> +#include <dirent.h> +#include "viralloc.h" +#include "virstring.h" +#include "virfile.h" +#include "dirname.h" static int (*real_access)(const char *path, int mode); static int (*real_lstat)(const char *path, struct stat *sb); @@ -51,20 +50,20 @@ static char *(*real_realpath)(const char *path, char *resolved); char *fakerootdir; char *fakesysfspcidir; -# define SYSFS_PCI_PREFIX "/sys/bus/pci/" +#define SYSFS_PCI_PREFIX "/sys/bus/pci/" -# define STDERR(...) \ +#define STDERR(...) \ fprintf(stderr, "%s %zu: ", __FUNCTION__, (size_t) __LINE__); \ fprintf(stderr, __VA_ARGS__); \ fprintf(stderr, "\n"); \ -# define ABORT(...) \ +#define ABORT(...) \ do { \ STDERR(__VA_ARGS__); \ abort(); \ } while (0) -# define ABORT_OOM() \ +#define ABORT_OOM() \ ABORT("Out of memory") /* * The plan: @@ -341,6 +340,7 @@ pci_device_new_from_stub(const struct pciDevice *data) char *configSrc; char tmp[256]; struct stat sb; + bool configSrcExists = false; if (VIR_STRDUP_QUIET(id, data->id) < 0) ABORT_OOM(); @@ -368,10 +368,18 @@ pci_device_new_from_stub(const struct pciDevice *data) if (virFileMakePath(devpath) < 0) ABORT("Unable to create: %s", devpath); + if (real_stat && real_stat(configSrc, &sb) == 0) + configSrcExists = true; + +#ifdef HAVE___XSTAT + if (!configSrcExists && + real___xstat && real___xstat(_STAT_VER, configSrc, &sb) == 0) + configSrcExists = true; +#endif + /* If there is a config file for the device within virpcitestdata dir, * symlink it. Otherwise create a dummy config file. */ - if ((real_stat && real_stat(configSrc, &sb) == 0) || - (real___xstat && real___xstat(_STAT_VER, configSrc, &sb) == 0)) { + if (configSrcExists) { /* On success, copy @configSrc into the destination (a copy, * rather than a symlink, is required since we write into the * file, and parallel VPATH builds must not stomp on the @@ -822,11 +830,11 @@ init_syms(void) * Ask for a versioned symbol to make sure we get a working realpath() * on Linux; other operating systems such as FreeBSD don't suffer from * the same limitation and can rely on the default dlsym() behavior */ -# ifdef __linux__ +#ifdef __linux__ VIR_MOCK_REAL_INIT_VERSIONED(realpath, "GLIBC_2.3"); -# else +#else VIR_MOCK_REAL_INIT(realpath); -# endif +#endif } static void @@ -847,7 +855,7 @@ init_env(void) make_file(fakesysfspcidir, "drivers_probe", NULL, -1); -# define MAKE_PCI_DRIVER(name, ...) \ +#define MAKE_PCI_DRIVER(name, ...) \ pci_driver_new(name, 0, __VA_ARGS__, -1, -1) MAKE_PCI_DRIVER("iwlwifi", 0x8086, 0x0044); @@ -855,7 +863,7 @@ init_env(void) MAKE_PCI_DRIVER("pci-stub", -1, -1); pci_driver_new("vfio-pci", PCI_ACTION_BIND, -1, -1); -# define MAKE_PCI_DEVICE(Id, Vendor, Device, ...) \ +#define MAKE_PCI_DEVICE(Id, Vendor, Device, ...) \ do { \ struct pciDevice dev = {.id = (char *)Id, .vendor = Vendor, \ .device = Device, __VA_ARGS__}; \ @@ -904,6 +912,7 @@ access(const char *path, int mode) return ret; } +#ifdef HAVE___LXSTAT int __lxstat(int ver, const char *path, struct stat *sb) { @@ -922,6 +931,7 @@ __lxstat(int ver, const char *path, struct stat *sb) } return ret; } +#endif /* HAVE___LXSTAT */ int lstat(const char *path, struct stat *sb) @@ -942,6 +952,7 @@ lstat(const char *path, struct stat *sb) return ret; } +#ifdef HAVE___XSTAT int __xstat(int ver, const char *path, struct stat *sb) { @@ -960,6 +971,7 @@ __xstat(int ver, const char *path, struct stat *sb) } return ret; } +#endif /* HAVE___XSTAT */ int stat(const char *path, struct stat *sb) @@ -1059,6 +1071,3 @@ realpath(const char *path, char *resolved) } return ret; } -#else -/* Nothing to override on non-__linux__ platforms */ -#endif -- 2.14.3

While the current amount of mocking works just fine on most of our target platforms, it somehow causes issues when using Clang on FreeBSD. Work around the issue by mocking a couple more functions. It's not pretty, but it makes qemuxml2argvtest pass on FreeBSD at long last. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- If anyone can figure out a better way to make this work, please speak up. This is the sanest I've been able to come up with, and not for lack of trying. tests/qemuxml2argvmock.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/tests/qemuxml2argvmock.c b/tests/qemuxml2argvmock.c index adab5c9111..6d78063f00 100644 --- a/tests/qemuxml2argvmock.c +++ b/tests/qemuxml2argvmock.c @@ -55,25 +55,45 @@ time_t time(time_t *t) return ret; } +bool +virNumaIsAvailable(void) +{ + return true; +} + int virNumaGetMaxNode(void) { - const int maxnodesNum = 7; - - return maxnodesNum; + return 7; } -#if WITH_NUMACTL && HAVE_NUMA_BITMASK_ISBITSET -/* - * In case libvirt is compiled with full NUMA support, we need to mock - * this function in order to fake what numa nodes are available. - */ +/* We shouldn't need to mock virNumaNodeIsAvailable() and *definitely* not + * virNumaNodesetIsAvailable(), but it seems to be the only way to get + * mocking to work with Clang on FreeBSD, so keep these duplicates around + * until we figure out a cleaner solution */ bool virNumaNodeIsAvailable(int node) { return node >= 0 && node <= virNumaGetMaxNode(); } -#endif /* WITH_NUMACTL && HAVE_NUMA_BITMASK_ISBITSET */ + +bool +virNumaNodesetIsAvailable(virBitmapPtr nodeset) +{ + ssize_t bit = -1; + + if (!nodeset) + return true; + + while ((bit = virBitmapNextSetBit(nodeset, bit)) >= 0) { + if (virNumaNodeIsAvailable(bit)) + continue; + + return false; + } + + return true; +} char * virTPMCreateCancelPath(const char *devpath) -- 2.14.3

Now that mocking NUMA information works on FreeBSD, there are no longer any test cases that need to be restricted to Linux only. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/qemuxml2argvtest.c | 46 +++++++++++++++------------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index 5b3bd4a996..5d8aedb2c6 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -767,24 +767,6 @@ mymain(void) FLAG_EXPECT_PARSE_ERROR | FLAG_EXPECT_FAILURE, \ parseFlags, GIC_NONE, __VA_ARGS__) -# define DO_TEST_LINUX(name, ...) \ - DO_TEST_LINUX_FULL(name, NULL, -1, 0, 0, GIC_NONE, __VA_ARGS__) - -# ifdef __linux__ - /* This is a macro that invokes test only on Linux. It's - * meant to be called in those cases where qemuxml2argvmock - * cooperation is expected (e.g. we need a fixed time, - * predictable NUMA topology and so on). On non-Linux - * platforms the macro just consume its argument. */ -# define DO_TEST_LINUX_FULL(name, ...) \ - DO_TEST_FULL(name, __VA_ARGS__) -# else /* __linux__ */ -# define DO_TEST_LINUX_FULL(name, ...) \ - do { \ - const char *tmp ATTRIBUTE_UNUSED = name; \ - } while (0) -# endif /* __linux__ */ - # define NONE QEMU_CAPS_LAST /* Unset or set all envvars here that are copied in qemudBuildCommandLine @@ -935,16 +917,16 @@ mymain(void) QEMU_CAPS_HDA_DUPLEX, QEMU_CAPS_USB_REDIR, QEMU_CAPS_DEVICE_PC_DIMM, QEMU_CAPS_OBJECT_MEMORY_FILE); - DO_TEST_LINUX("hugepages-pages", - QEMU_CAPS_OBJECT_MEMORY_RAM, - QEMU_CAPS_OBJECT_MEMORY_FILE); + DO_TEST("hugepages-pages", + QEMU_CAPS_OBJECT_MEMORY_RAM, + QEMU_CAPS_OBJECT_MEMORY_FILE); DO_TEST("hugepages-pages2", QEMU_CAPS_OBJECT_MEMORY_RAM, QEMU_CAPS_OBJECT_MEMORY_FILE); DO_TEST("hugepages-pages3", QEMU_CAPS_OBJECT_MEMORY_RAM, QEMU_CAPS_OBJECT_MEMORY_FILE); - DO_TEST_LINUX("hugepages-shared", - QEMU_CAPS_OBJECT_MEMORY_RAM, - QEMU_CAPS_OBJECT_MEMORY_FILE); + DO_TEST("hugepages-shared", + QEMU_CAPS_OBJECT_MEMORY_RAM, + QEMU_CAPS_OBJECT_MEMORY_FILE); DO_TEST_PARSE_ERROR("hugepages-memaccess-invalid", NONE); DO_TEST_FAILURE("hugepages-pages4", QEMU_CAPS_OBJECT_MEMORY_RAM, QEMU_CAPS_OBJECT_MEMORY_FILE); @@ -1562,9 +1544,9 @@ mymain(void) DO_TEST_FULL("restore-v2-fd", "fd:7", 7, 0, 0, GIC_NONE, NONE); DO_TEST_FULL("migrate", "tcp:10.0.0.1:5000", -1, 0, 0, GIC_NONE, NONE); - DO_TEST_LINUX_FULL("migrate-numa-unaligned", "stdio", 7, 0, 0, GIC_NONE, - QEMU_CAPS_NUMA, - QEMU_CAPS_OBJECT_MEMORY_RAM); + DO_TEST_FULL("migrate-numa-unaligned", "stdio", 7, 0, 0, GIC_NONE, + QEMU_CAPS_NUMA, + QEMU_CAPS_OBJECT_MEMORY_RAM); DO_TEST("qemu-ns", NONE); DO_TEST("qemu-ns-no-env", NONE); @@ -1662,12 +1644,14 @@ mymain(void) DO_TEST("numatune-memory", NONE); DO_TEST_PARSE_ERROR("numatune-memory-invalid-nodeset", NONE); - DO_TEST_LINUX("numatune-memnode", QEMU_CAPS_NUMA, - QEMU_CAPS_OBJECT_MEMORY_RAM); + DO_TEST("numatune-memnode", + QEMU_CAPS_NUMA, + QEMU_CAPS_OBJECT_MEMORY_RAM); DO_TEST_FAILURE("numatune-memnode", NONE); - DO_TEST_LINUX("numatune-memnode-no-memory", QEMU_CAPS_NUMA, - QEMU_CAPS_OBJECT_MEMORY_RAM); + DO_TEST("numatune-memnode-no-memory", + QEMU_CAPS_NUMA, + QEMU_CAPS_OBJECT_MEMORY_RAM); DO_TEST_FAILURE("numatune-memnode-no-memory", NONE); DO_TEST("numatune-distances", QEMU_CAPS_NUMA, QEMU_CAPS_NUMA_DIST); -- 2.14.3
participants (3)
-
Andrea Bolognani
-
Daniel P. Berrangé
-
Pino Toscano