From: "Daniel P. Berrange" <berrange(a)redhat.com>
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 <berrange(a)redhat.com>
---
.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 6b024e7..acf73a4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -158,6 +158,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 d3f1ca2..23fea0e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -17600,12 +17600,13 @@ virDiskNameToBusDeviceIndex(virDomainDiskDefPtr disk,
}
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 4561ccc..22cbd5b 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -2497,7 +2497,8 @@ int virDiskNameToBusDeviceIndex(virDomainDiskDefPtr disk,
int *busIdx,
int *devIdx);
-virDomainFSDefPtr virDomainGetRootFilesystem(virDomainDefPtr def);
+virDomainFSDefPtr virDomainGetFilesystemForTarget(virDomainDefPtr def,
+ const char *path);
int virDomainFSIndexByName(virDomainDefPtr def, const char *name);
int virDomainVideoDefaultType(const virDomainDef *def);
int virDomainVideoDefaultRAM(const virDomainDef *def, int type);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a705c56..2d3d7f1 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -222,7 +222,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 2bdf957..12d957a 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -1847,7 +1847,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 c51c4d5..d15cde3 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -930,7 +930,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 e46d5f7..0ef82fb 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -64,6 +64,7 @@ EXTRA_DIST = \
commanddata \
confdata \
cputestdata \
+ domainconfdata \
domainschemadata \
domainschematest \
domainsnapshotschematest \
@@ -138,6 +139,7 @@ test_programs = virshtest sockettest \
virportallocatortest \
sysinfotest \
virstoragetest \
+ domainconftest \
$(NULL)
if WITH_REMOTE
@@ -884,6 +886,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 0000000..2ee78b4
--- /dev/null
+++ b/tests/domainconfdata/getfilesystem.xml
@@ -0,0 +1,28 @@
+<domain type='test'>
+ <name>demo</name>
+ <uuid>8369f1ac-7e46-e869-4ca5-759d51478066</uuid>
+ <memory unit='KiB'>500000</memory>
+ <currentMemory unit='KiB'>500000</currentMemory>
+ <vcpu placement='static'>1</vcpu>
+ <os>
+ <type arch='x86_64'>hvm</type>
+ <init>/bin/sh</init>
+ </os>
+ <clock offset='utc'/>
+ <on_poweroff>destroy</on_poweroff>
+ <on_reboot>restart</on_reboot>
+ <on_crash>destroy</on_crash>
+ <devices>
+ <filesystem type='mount' accessmode='passthrough'>
+ <source dir='/'/>
+ <target dir='/'/>
+ </filesystem>
+ <filesystem type='mount' accessmode='passthrough'>
+ <source dir='/'/>
+ <target dir='/dev'/>
+ </filesystem>
+ <console type='pty'>
+ <target type='lxc' port='0'/>
+ </console>
+ </devices>
+</domain>
diff --git a/tests/domainconftest.c b/tests/domainconftest.c
new file mode 100644
index 0000000..d38ef5c
--- /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
+ * <
http://www.gnu.org/licenses/>.
+ *
+ * Author: Daniel P. Berrange <berrange(a)redhat.com>
+ */
+
+#include <config.h>
+
+#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 5d634b4..5975715 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -654,3 +654,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 478b53c..4781e04 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 */
@@ -98,4 +100,7 @@ int virtTestMain(int argc,
return virtTestMain(argc, argv, func); \
}
+virCapsPtr virTestGenericCapsInit(void);
+virDomainXMLOptionPtr virTestGenericDomainXMLConfInit(void);
+
#endif /* __VIT_TEST_UTILS_H__ */
--
1.8.3.1