Extracting the code logic for writing a test image to disk from
testDomainSaveFlags into a separate function, allows us to reuse this
code in other functions such as testDomainSaveImageDefineXML.
Signed-off-by: Ilias Stamatis <stamatis.iliass(a)gmail.com>
---
src/test/test_driver.c | 115 +++++++++++++++++++++++++----------------
1 file changed, 70 insertions(+), 45 deletions(-)
diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index 1aa79ce898..3331ed5495 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -1999,75 +1999,107 @@ testDomainGetTime(virDomainPtr dom ATTRIBUTE_UNUSED,
#define TEST_SAVE_MAGIC "TestGuestMagic"
-static int
-testDomainSaveFlags(virDomainPtr domain, const char *path,
- const char *dxml, unsigned int flags)
+
+/**
+ * testDomainSaveImageWrite:
+ * @driver: test driver data
+ * @def: domain definition whose XML will be stored in the image
+ * @path: path to the saved image
+ *
+ * Returns true on success, else false.
+ */
+static bool
+testDomainSaveImageWrite(testDriverPtr driver,
+ const char *path,
+ virDomainDefPtr def)
{
- testDriverPtr privconn = domain->conn->privateData;
- int fd = -1;
int len;
- virDomainObjPtr privdom;
- virObjectEventPtr event = NULL;
- int ret = -1;
+ int fd = -1;
VIR_AUTOFREE(char *) xml = NULL;
- virCheckFlags(0, -1);
- if (dxml) {
- virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
- _("xml modification unsupported"));
- return -1;
- }
-
-
- if (!(privdom = testDomObjFromDomain(domain)))
- goto cleanup;
-
- if (virDomainObjCheckActive(privdom) < 0)
- goto cleanup;
-
- xml = virDomainDefFormat(privdom->def, privconn->caps,
- VIR_DOMAIN_DEF_FORMAT_SECURE);
+ xml = virDomainDefFormat(def, driver->caps, VIR_DOMAIN_DEF_FORMAT_SECURE);
if (xml == NULL) {
virReportSystemError(errno,
_("saving domain '%s' failed to allocate space
for metadata"),
- domain->name);
- goto cleanup;
+ def->name);
+ goto error;
}
if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
virReportSystemError(errno,
_("saving domain '%s' to '%s': open
failed"),
- domain->name, path);
- goto cleanup;
+ def->name, path);
+ goto error;
}
- len = strlen(xml);
+
if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
virReportSystemError(errno,
_("saving domain '%s' to '%s': write
failed"),
- domain->name, path);
- goto cleanup;
+ def->name, path);
+ goto error;
}
+
+ len = strlen(xml);
if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
virReportSystemError(errno,
_("saving domain '%s' to '%s': write
failed"),
- domain->name, path);
- goto cleanup;
+ def->name, path);
+ goto error;
}
+
if (safewrite(fd, xml, len) < 0) {
virReportSystemError(errno,
_("saving domain '%s' to '%s': write
failed"),
- domain->name, path);
- goto cleanup;
+ def->name, path);
+ goto error;
}
if (VIR_CLOSE(fd) < 0) {
virReportSystemError(errno,
_("saving domain '%s' to '%s': write
failed"),
- domain->name, path);
- goto cleanup;
+ def->name, path);
+ goto error;
+ }
+
+ return true;
+
+ error:
+ /* Don't report failure in close or unlink, because
+ * in either case we're already in a failure scenario
+ * and have reported an earlier error */
+ VIR_FORCE_CLOSE(fd);
+ unlink(path);
+
+ return false;
+}
+
+
+static int
+testDomainSaveFlags(virDomainPtr domain, const char *path,
+ const char *dxml, unsigned int flags)
+{
+ testDriverPtr privconn = domain->conn->privateData;
+ virDomainObjPtr privdom;
+ virObjectEventPtr event = NULL;
+ int ret = -1;
+
+ virCheckFlags(0, -1);
+
+ if (dxml) {
+ virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
+ _("xml modification unsupported"));
+ return -1;
}
- fd = -1;
+
+ if (!(privdom = testDomObjFromDomain(domain)))
+ goto cleanup;
+
+ if (virDomainObjCheckActive(privdom) < 0)
+ goto cleanup;
+
+ if (!testDomainSaveImageWrite(privconn, path, privdom->def))
+ goto cleanup;
testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
event = virDomainEventLifecycleNewFromObj(privdom,
@@ -2079,13 +2111,6 @@ testDomainSaveFlags(virDomainPtr domain, const char *path,
ret = 0;
cleanup:
- /* Don't report failure in close or unlink, because
- * in either case we're already in a failure scenario
- * and have reported an earlier error */
- if (ret != 0) {
- VIR_FORCE_CLOSE(fd);
- unlink(path);
- }
virDomainObjEndAPI(&privdom);
virObjectEventStateQueue(privconn->eventState, event);
return ret;
--
2.21.0