From e791033df6084d970e9a32441e48543e97cfa867 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Tue, 6 Aug 2013 17:42:16 -0400 Subject: [PATCH] test: Allow specifying object runstate in driver XML When passing in custom driver XML, allow a block like ... 5 This is only read at initial driver start time, and sets the initial run state of the object. This is handy for UI testing. It's only wired up for domains, since that's the only conf/ infrastructure that supports namespaces at the moment. --- src/test/test_driver.c | 91 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 7 deletions(-) diff --git a/src/test/test_driver.c b/src/test/test_driver.c index e1197c9d73..48b5ec1430 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "virerror.h" @@ -152,13 +153,81 @@ static void testDomainObjPrivateFree(void *data) VIR_FREE(priv); } +#define TEST_NAMESPACE_HREF "http://libvirt.org/schemas/domain/test/1.0" + +typedef struct _testDomainNamespaceDef testDomainNamespaceDef; +typedef testDomainNamespaceDef *testDomainNamespaceDefPtr; +struct _testDomainNamespaceDef { + int runstate; +}; + +static void +testDomainDefNamespaceFree(void *data) +{ + testDomainNamespaceDefPtr nsdata = data; + VIR_FREE(nsdata); +} + +static int +testDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED, + xmlNodePtr root ATTRIBUTE_UNUSED, + xmlXPathContextPtr ctxt, + void **data) +{ + testDomainNamespaceDefPtr nsdata = NULL; + int tmp; + unsigned int tmpuint; + + if (xmlXPathRegisterNs(ctxt, BAD_CAST "test", + BAD_CAST TEST_NAMESPACE_HREF) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to register xml namespace '%s'"), + TEST_NAMESPACE_HREF); + return -1; + } + + if (VIR_ALLOC(nsdata) < 0) + return -1; + + tmp = virXPathUInt("string(./test:runstate)", ctxt, &tmpuint); + if (tmp == 0) { + if (tmpuint >= VIR_DOMAIN_LAST) { + virReportError(VIR_ERR_XML_ERROR, + _("runstate '%d' out of range'"), tmpuint); + goto error; + } + nsdata->runstate = tmpuint; + } else if (tmp == -1) { + nsdata->runstate = VIR_DOMAIN_RUNNING; + } else if (tmp == -2) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("invalid runstate")); + goto error; + } + + *data = nsdata; + return 0; + +error: + testDomainDefNamespaceFree(nsdata); + return -1; +} static virDomainXMLOptionPtr testBuildXMLConfig(void) { - virDomainXMLPrivateDataCallbacks priv = { .alloc = testDomainObjPrivateAlloc, - .free = testDomainObjPrivateFree }; - return virDomainXMLOptionNew(NULL, &priv, NULL); + virDomainXMLPrivateDataCallbacks priv = { + .alloc = testDomainObjPrivateAlloc, + .free = testDomainObjPrivateFree + }; + + /* All our XML extensions are input only, so we only need to parse */ + virDomainXMLNamespace ns = { + .parse = testDomainDefNamespaceParse, + .free = testDomainDefNamespaceFree, + }; + + return virDomainXMLOptionNew(NULL, &priv, &ns); } @@ -831,6 +900,7 @@ testParseDomains(testConnPtr privconn, for (i = 0; i < num; i++) { virDomainDefPtr def; + testDomainNamespaceDefPtr nsdata; xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file, "domain"); if (!node) goto error; @@ -851,12 +921,19 @@ testParseDomains(testConnPtr privconn, goto error; } + nsdata = def->namespaceData; obj->persistent = 1; - if (testDomainStartState(privconn, obj, - VIR_DOMAIN_RUNNING_BOOTED) < 0) { - virObjectUnlock(obj); - goto error; + + if (nsdata->runstate != VIR_DOMAIN_SHUTOFF) { + if (testDomainStartState(privconn, obj, + VIR_DOMAIN_RUNNING_BOOTED) < 0) { + virObjectUnlock(obj); + goto error; + } + } else { + testDomainShutdownState(NULL, obj, 0); } + virDomainObjSetState(obj, nsdata->runstate, 0); virObjectUnlock(obj); }