tests: Introduce virhostdevmock

We need this for all tests that use virHostdevManager, because
during creation of this object for unprivileged connections
like those used in the test suite we would end up writing inside
the user's home directory.

That's bad manners in general, but when running the test suite
inside a purposefully constrained environment such as the one
exposed by pbuilder, it turns into an outright test failure:

  Could not initialize HostdevManager - operation failed: Failed
  to create state dir '/nonexistent/.cache/libvirt/hostdevmgr'

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Andrea Bolognani 2020-05-06 14:38:42 +02:00
parent 7c42aafb09
commit eea5d63a22
5 changed files with 27 additions and 2 deletions

View File

@ -99,7 +99,7 @@ char *virGetUserDirectory(void);
char *virGetUserDirectoryByUID(uid_t uid); char *virGetUserDirectoryByUID(uid_t uid);
char *virGetUserConfigDirectory(void); char *virGetUserConfigDirectory(void);
char *virGetUserCacheDirectory(void); char *virGetUserCacheDirectory(void);
char *virGetUserRuntimeDirectory(void); char *virGetUserRuntimeDirectory(void) G_GNUC_NO_INLINE;
char *virGetUserShell(uid_t uid); char *virGetUserShell(uid_t uid);
char *virGetUserName(uid_t uid) G_GNUC_NO_INLINE; char *virGetUserName(uid_t uid) G_GNUC_NO_INLINE;
char *virGetGroupName(gid_t gid) G_GNUC_NO_INLINE; char *virGetGroupName(gid_t gid) G_GNUC_NO_INLINE;

View File

@ -212,6 +212,7 @@ test_libraries = libshunload.la \
libvirnetdaemonmock.la \ libvirnetdaemonmock.la \
libvirnetserverclientmock.la \ libvirnetserverclientmock.la \
libvircgroupmock.la \ libvircgroupmock.la \
libvirhostdevmock.la \
libvirpcimock.la \ libvirpcimock.la \
libvirnetdevmock.la \ libvirnetdevmock.la \
libvirrandommock.la \ libvirrandommock.la \
@ -1226,6 +1227,12 @@ libvirfilecachemock_la_SOURCES = \
libvirfilecachemock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS) libvirfilecachemock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
libvirfilecachemock_la_LIBADD = $(MOCKLIBS_LIBS) libvirfilecachemock_la_LIBADD = $(MOCKLIBS_LIBS)
libvirhostdevmock_la_SOURCES = \
virhostdevmock.c \
$(NULL)
libvirhostdevmock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
libvirhostdevmock_la_LIBADD = $(MOCKLIBS_LIBS)
if WITH_LINUX if WITH_LINUX
vircaps2xmltest_SOURCES = \ vircaps2xmltest_SOURCES = \
vircaps2xmltest.c testutils.h testutils.c virfilewrapper.h virfilewrapper.c vircaps2xmltest.c testutils.h testutils.c virfilewrapper.h virfilewrapper.c

View File

@ -900,6 +900,7 @@ mymain(void)
} }
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MAIN_PRELOAD(mymain,
VIR_TEST_MOCK("virhostdev"),
VIR_TEST_MOCK("virpci"), VIR_TEST_MOCK("virpci"),
VIR_TEST_MOCK("domaincaps"), VIR_TEST_MOCK("domaincaps"),
VIR_TEST_MOCK("virprocess"), VIR_TEST_MOCK("virprocess"),

15
tests/virhostdevmock.c Normal file
View File

@ -0,0 +1,15 @@
/*
* Copyright (C) 2020 Red Hat, Inc.
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include <config.h>
#include "virutil.h"
char *
virGetUserRuntimeDirectory(void)
{
return g_build_filename(g_getenv("LIBVIRT_FAKE_ROOT_DIR"),
"user-runtime-directory", NULL);
}

View File

@ -628,7 +628,9 @@ mymain(void)
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
} }
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virpci")) VIR_TEST_MAIN_PRELOAD(mymain,
VIR_TEST_MOCK("virhostdev"),
VIR_TEST_MOCK("virpci"))
#else #else
int int
main(void) main(void)