From 432a3fee3b547cb403e16399a16a2c361f73831b Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Tue, 12 Nov 2013 11:57:56 +0000 Subject: [PATCH] Rename virDomainGetRootFilesystem to virDomainGetFilesystemForTarget The virDomainGetRootFilesystem method can be generalized to allow any filesystem path to be obtained. While doing this, start a new test case for purpose of testing various helper methods in the domain_conf.{c,h} files, such as this one. Signed-off-by: Daniel P. Berrange --- .gitignore | 1 + src/conf/domain_conf.c | 5 +- src/conf/domain_conf.h | 3 +- src/libvirt_private.syms | 2 +- src/lxc/lxc_container.c | 2 +- src/lxc/lxc_process.c | 2 +- tests/Makefile.am | 6 ++ tests/domainconfdata/getfilesystem.xml | 28 ++++++ tests/domainconftest.c | 120 +++++++++++++++++++++++++ tests/testutils.c | 57 ++++++++++++ tests/testutils.h | 5 ++ 11 files changed, 225 insertions(+), 6 deletions(-) create mode 100644 tests/domainconfdata/getfilesystem.xml create mode 100644 tests/domainconftest.c diff --git a/.gitignore b/.gitignore index 1c1548992e..69c81dfd1e 100644 --- a/.gitignore +++ b/.gitignore @@ -149,6 +149,7 @@ /tests/commandtest /tests/conftest /tests/cputest +/tests/domainconftest /tests/domainsnapshotxml2xmltest /tests/esxutilstest /tests/eventtest diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 0a249e4bc9..064a40e366 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -18083,12 +18083,13 @@ virDomainFSRemove(virDomainDefPtr def, size_t i) } virDomainFSDefPtr -virDomainGetRootFilesystem(virDomainDefPtr def) +virDomainGetFilesystemForTarget(virDomainDefPtr def, + const char *path) { size_t i; for (i = 0; i < def->nfss; i++) { - if (STREQ(def->fss[i]->dst, "/")) + if (STREQ(def->fss[i]->dst, path)) return def->fss[i]; } diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index b961425c1b..97d6337ba8 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -2563,7 +2563,8 @@ int virDiskNameToBusDeviceIndex(virDomainDiskDefPtr disk, int *busIdx, int *devIdx); -virDomainFSDefPtr virDomainGetRootFilesystem(virDomainDefPtr def); +virDomainFSDefPtr virDomainGetFilesystemForTarget(virDomainDefPtr def, + const char *path); int virDomainFSInsert(virDomainDefPtr def, virDomainFSDefPtr fs); int virDomainFSIndexByName(virDomainDefPtr def, const char *name); virDomainFSDefPtr virDomainFSRemove(virDomainDefPtr def, size_t i); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 341d2e0639..08962876e4 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -229,7 +229,7 @@ virDomainFSTypeFromString; virDomainFSTypeToString; virDomainFSWrpolicyTypeFromString; virDomainFSWrpolicyTypeToString; -virDomainGetRootFilesystem; +virDomainGetFilesystemForTarget; virDomainGraphicsAuthConnectedTypeFromString; virDomainGraphicsAuthConnectedTypeToString; virDomainGraphicsDefFree; diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index f08dbc2b1c..71548043eb 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -1823,7 +1823,7 @@ static int lxcContainerChild(void *data) if (lxcContainerSetID(vmDef) < 0) goto cleanup; - root = virDomainGetRootFilesystem(vmDef); + root = virDomainGetFilesystemForTarget(vmDef, "/"); if (argv->nttyPaths) { const char *tty = argv->ttyPaths[0]; diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index ac7ed39c56..1ebad9c22e 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -957,7 +957,7 @@ virLXCProcessReadLogOutput(virDomainObjPtr vm, static int virLXCProcessEnsureRootFS(virDomainObjPtr vm) { - virDomainFSDefPtr root = virDomainGetRootFilesystem(vm->def); + virDomainFSDefPtr root = virDomainGetFilesystemForTarget(vm->def, "/"); if (root) return 0; diff --git a/tests/Makefile.am b/tests/Makefile.am index 0c72cc2489..91eeeac6b5 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -65,6 +65,7 @@ EXTRA_DIST = \ commanddata \ confdata \ cputestdata \ + domainconfdata \ domainschemadata \ domainschematest \ domainsnapshotschematest \ @@ -147,6 +148,7 @@ test_programs = virshtest sockettest \ virnetdevbandwidthtest \ virkmodtest \ vircapstest \ + domainconftest \ $(NULL) if WITH_REMOTE @@ -939,6 +941,10 @@ sysinfotest_SOURCES = \ sysinfotest.c testutils.h testutils.c sysinfotest_LDADD = $(LDADDS) +domainconftest_SOURCES = \ + domainconftest.c testutils.h testutils.c +domainconftest_LDADD = $(LDADDS) + fdstreamtest_SOURCES = \ fdstreamtest.c testutils.h testutils.c fdstreamtest_LDADD = $(LDADDS) diff --git a/tests/domainconfdata/getfilesystem.xml b/tests/domainconfdata/getfilesystem.xml new file mode 100644 index 0000000000..2ee78b4eb9 --- /dev/null +++ b/tests/domainconfdata/getfilesystem.xml @@ -0,0 +1,28 @@ + + demo + 8369f1ac-7e46-e869-4ca5-759d51478066 + 500000 + 500000 + 1 + + hvm + /bin/sh + + + destroy + restart + destroy + + + + + + + + + + + + + + diff --git a/tests/domainconftest.c b/tests/domainconftest.c new file mode 100644 index 0000000000..d38ef5c42c --- /dev/null +++ b/tests/domainconftest.c @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2013 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + * Author: Daniel P. Berrange + */ + +#include + +#include "testutils.h" +#include "virerror.h" +#include "viralloc.h" +#include "virlog.h" + +#include "domain_conf.h" + +#define VIR_FROM_THIS VIR_FROM_NONE + +static virCapsPtr caps; +static virDomainXMLOptionPtr xmlopt; + +struct testGetFilesystemData { + const char *filename; + const char *path; + bool expectEntry; +}; + +static int testGetFilesystem(const void *opaque) +{ + int ret = -1; + char *xmlData = NULL; + virDomainDefPtr def = NULL; + char *filename = NULL; + const struct testGetFilesystemData *data = opaque; + virDomainFSDefPtr fsdef; + + if (virAsprintf(&filename, "%s/domainconfdata/%s.xml", + abs_srcdir, data->filename) < 0) + goto cleanup; + + if (virtTestLoadFile(filename, &xmlData) < 0) + goto cleanup; + + if (!(def = virDomainDefParseString(xmlData, caps, xmlopt, + 1 << VIR_DOMAIN_VIRT_TEST, 0))) + goto cleanup; + + fsdef = virDomainGetFilesystemForTarget(def, + data->path); + if (!fsdef) { + if (data->expectEntry) { + fprintf(stderr, "Expected FS for path '%s' in '%s'\n", + data->path, filename); + goto cleanup; + } + } else { + if (!data->expectEntry) { + fprintf(stderr, "Unexpected FS for path '%s' in '%s'\n", + data->path, filename); + goto cleanup; + } + } + + ret = 0; + +cleanup: + virDomainDefFree(def); + VIR_FREE(xmlData); + VIR_FREE(filename); + return ret; +} + +static int +mymain(void) +{ + int ret = 0; + + if ((caps = virTestGenericCapsInit()) == NULL) + goto cleanup; + + if (!(xmlopt = virTestGenericDomainXMLConfInit())) + goto cleanup; + +#define DO_TEST_GET_FS(fspath, expect) \ + do { \ + struct testGetFilesystemData data = { \ + .filename = "getfilesystem", \ + .path = fspath, \ + .expectEntry = expect, \ + }; \ + if (virtTestRun("Get FS " fspath, testGetFilesystem, &data) < 0) \ + ret = -1; \ + } while (0) + + DO_TEST_GET_FS("/", true); + DO_TEST_GET_FS("/dev", true); + DO_TEST_GET_FS("/dev/pts", false); + DO_TEST_GET_FS("/doesnotexist", false); + + virObjectUnref(caps); + virObjectUnref(xmlopt); + + cleanup: + return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} + +VIRT_TEST_MAIN(mymain) diff --git a/tests/testutils.c b/tests/testutils.c index 3f42b13270..97d5b50cdc 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -834,3 +834,60 @@ int virtTestClearLineRegex(const char *pattern, return 0; } + + +virCapsPtr virTestGenericCapsInit(void) +{ + virCapsPtr caps; + virCapsGuestPtr guest; + + if ((caps = virCapabilitiesNew(VIR_ARCH_X86_64, + 0, 0)) == NULL) + return NULL; + + if ((guest = virCapabilitiesAddGuest(caps, "hvm", VIR_ARCH_I686, + "/usr/bin/acme-virt", NULL, + 0, NULL)) == NULL) + goto error; + + if (!virCapabilitiesAddGuestDomain(guest, "test", NULL, NULL, 0, NULL)) + goto error; + + + if ((guest = virCapabilitiesAddGuest(caps, "hvm", VIR_ARCH_X86_64, + "/usr/bin/acme-virt", NULL, + 0, NULL)) == NULL) + goto error; + + if (!virCapabilitiesAddGuestDomain(guest, "test", NULL, NULL, 0, NULL)) + goto error; + + + if (virTestGetDebug()) { + char *caps_str; + + caps_str = virCapabilitiesFormatXML(caps); + if (!caps_str) + goto error; + + fprintf(stderr, "Generic driver capabilities:\n%s", caps_str); + + VIR_FREE(caps_str); + } + + return caps; + +error: + virObjectUnref(caps); + return NULL; +} + +static virDomainDefParserConfig virTestGenericDomainDefParserConfig; +static virDomainXMLPrivateDataCallbacks virTestGenericPrivateDataCallbacks; + +virDomainXMLOptionPtr virTestGenericDomainXMLConfInit(void) +{ + return virDomainXMLOptionNew(&virTestGenericDomainDefParserConfig, + &virTestGenericPrivateDataCallbacks, + NULL); +} diff --git a/tests/testutils.h b/tests/testutils.h index 29eb9d91d1..fa37246b48 100644 --- a/tests/testutils.h +++ b/tests/testutils.h @@ -27,6 +27,8 @@ # include "viralloc.h" # include "virfile.h" # include "virstring.h" +# include "capabilities.h" +# include "domain_conf.h" # define EXIT_AM_SKIP 77 /* tell Automake we're skipping a test */ # define EXIT_AM_HARDFAIL 99 /* tell Automake that the framework is broken */ @@ -104,4 +106,7 @@ int virtTestMain(int argc, return virtTestMain(argc, argv, func); \ } +virCapsPtr virTestGenericCapsInit(void); +virDomainXMLOptionPtr virTestGenericDomainXMLConfInit(void); + #endif /* __VIT_TEST_UTILS_H__ */