[libvirt] [PATCH v2 0/2] Introducing testDomainRename().

This commit introduces the testDomainRename() for test driver. It includes: - testDomainRename() implementation. - Test case for virshtest to test 'domrename' command. Julio Faracco (2): test: Implementing testDomainRename(). tests: Adding test case for 'domrename' command inside virshtest. src/test/test_driver.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/virshtest.c | 13 +++++++++ 2 files changed, 85 insertions(+) -- 2.7.4

There is no method to rename inactive domains for test driver. After this patch, we can rename the domains using 'domrename'. virsh# domrename test anothertest Domain successfully renamed Signed-off-by: Julio Faracco <jcfaracco@gmail.com> --- src/test/test_driver.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/test/test_driver.c b/src/test/test_driver.c index dc743b4..26bc8a2 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -2618,6 +2618,77 @@ testDomainGetVcpuPinInfo(virDomainPtr dom, return ret; } +static int +testDomainRenameCallback(virDomainObjPtr privdom, + const char *new_name, + unsigned int flags, + void *opaque) +{ + testDriverPtr driver = opaque; + virObjectEventPtr event_new = NULL; + virObjectEventPtr event_old = NULL; + int ret = -1; + char *new_dom_name = NULL; + char *old_dom_name = NULL; + + virCheckFlags(0, ret); + + if (VIR_STRDUP(new_dom_name, new_name) < 0) + goto cleanup; + + event_old = virDomainEventLifecycleNewFromObj(privdom, + VIR_DOMAIN_EVENT_UNDEFINED, + VIR_DOMAIN_EVENT_UNDEFINED_RENAMED); + + /* Switch name in domain definition. */ + old_dom_name = privdom->def->name; + privdom->def->name = new_dom_name; + new_dom_name = NULL; + + event_new = virDomainEventLifecycleNewFromObj(privdom, + VIR_DOMAIN_EVENT_DEFINED, + VIR_DOMAIN_EVENT_DEFINED_RENAMED); + ret = 0; + + cleanup: + VIR_FREE(old_dom_name); + VIR_FREE(new_dom_name); + testObjectEventQueue(driver, event_old); + testObjectEventQueue(driver, event_new); + return ret; +} + +static int testDomainRename(virDomainPtr dom, + const char *new_name, + unsigned int flags) +{ + testDriverPtr driver = dom->conn->privateData; + virDomainObjPtr privdom = NULL; + int ret = -1; + + virCheckFlags(0, -1); + + if (!(privdom = testDomObjFromDomain(dom))) + goto cleanup; + + if (!privdom->persistent) { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("cannot rename a transient domain")); + goto cleanup; + } + + if (virDomainObjListRename(driver->domains, privdom, new_name, flags, + testDomainRenameCallback, driver) < 0) + goto cleanup; + + /* Success, domain has been renamed. */ + ret = 0; + + cleanup: + virDomainObjEndAPI(&privdom); + return ret; +} + static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags) { testDriverPtr privconn = domain->conn->privateData; @@ -6822,6 +6893,7 @@ static virHypervisorDriver testHypervisorDriver = { .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */ .connectIsAlive = testConnectIsAlive, /* 0.9.8 */ .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */ + .domainRename = testDomainRename, /* 4.0.0 */ .domainScreenshot = testDomainScreenshot, /* 1.0.5 */ .domainGetMetadata = testDomainGetMetadata, /* 1.1.3 */ .domainSetMetadata = testDomainSetMetadata, /* 1.1.3 */ -- 2.7.4

On Mon, Jan 08, 2018 at 01:02:59 -0200, Julio Faracco wrote:
There is no method to rename inactive domains for test driver. After this patch, we can rename the domains using 'domrename'.
virsh# domrename test anothertest Domain successfully renamed
Signed-off-by: Julio Faracco <jcfaracco@gmail.com> --- src/test/test_driver.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+)
diff --git a/src/test/test_driver.c b/src/test/test_driver.c index dc743b4..26bc8a2 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -2618,6 +2618,77 @@ testDomainGetVcpuPinInfo(virDomainPtr dom, return ret; }
+static int +testDomainRenameCallback(virDomainObjPtr privdom, + const char *new_name, + unsigned int flags, + void *opaque) +{ + testDriverPtr driver = opaque; + virObjectEventPtr event_new = NULL; + virObjectEventPtr event_old = NULL; + int ret = -1; + char *new_dom_name = NULL; + char *old_dom_name = NULL; + + virCheckFlags(0, ret); + + if (VIR_STRDUP(new_dom_name, new_name) < 0) + goto cleanup; + + event_old = virDomainEventLifecycleNewFromObj(privdom, + VIR_DOMAIN_EVENT_UNDEFINED, + VIR_DOMAIN_EVENT_UNDEFINED_RENAMED); + + /* Switch name in domain definition. */ + old_dom_name = privdom->def->name; + privdom->def->name = new_dom_name; + new_dom_name = NULL;
Since the function below does not check that the VM is inactive at this point, this code lacks rename of the 'newDef' object which might be populated. Renaming an active VM which has newDef would then lead to returing to old name once it turns off.
+ + event_new = virDomainEventLifecycleNewFromObj(privdom, + VIR_DOMAIN_EVENT_DEFINED, + VIR_DOMAIN_EVENT_DEFINED_RENAMED); + ret = 0; + + cleanup: + VIR_FREE(old_dom_name); + VIR_FREE(new_dom_name); + testObjectEventQueue(driver, event_old); + testObjectEventQueue(driver, event_new); + return ret; +} + +static int testDomainRename(virDomainPtr dom, + const char *new_name, + unsigned int flags)
You are mixing two distinct coding styles of function headers in 1 hunk?
+{ + testDriverPtr driver = dom->conn->privateData; + virDomainObjPtr privdom = NULL; + int ret = -1; + + virCheckFlags(0, -1); + + if (!(privdom = testDomObjFromDomain(dom))) + goto cleanup; + + if (!privdom->persistent) { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("cannot rename a transient domain")); + goto cleanup; + }
If you are going to support renaming of running VMs, this check does not make sense.
+ + if (virDomainObjListRename(driver->domains, privdom, new_name, flags, + testDomainRenameCallback, driver) < 0) + goto cleanup; + + /* Success, domain has been renamed. */ + ret = 0; + + cleanup: + virDomainObjEndAPI(&privdom); + return ret; +} + static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags) { testDriverPtr privconn = domain->conn->privateData;

This commit adds a domrename test case inside the 'virshtest' test for test driver. Signed-off-by: Julio Faracco <jcfaracco@gmail.com> --- tests/virshtest.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/virshtest.c b/tests/virshtest.c index 67453bd..f1cd5df 100644 --- a/tests/virshtest.c +++ b/tests/virshtest.c @@ -40,6 +40,7 @@ static const char *domuuid_fc4 = DOM_UUID "\n\n"; static const char *domid_fc4 = "2\n\n"; static const char *domname_fc4 = "fc4\n\n"; static const char *domstate_fc4 = "running\n\n"; +static const char *domrename_fc4 = "Domain successfully renamed\n\n"; static int testFilterLine(char *buffer, const char *toRemove) @@ -237,6 +238,14 @@ static int testCompareDomstateByName(const void *data ATTRIBUTE_UNUSED) return testCompareOutputLit(exp, NULL, argv); } +static int testCompareDomrenameByName(const void *data ATTRIBUTE_UNUSED) +{ + const char *const argv[] = { VIRSH_CUSTOM, "domrename", "fc4", + "fc4new", NULL }; + const char *exp = domrename_fc4; + return testCompareOutputLit(exp, NULL, argv); +} + struct testInfo { const char *const *argv; const char *result; @@ -322,6 +331,10 @@ mymain(void) testCompareDomstateByName, NULL) != 0) ret = -1; + if (virTestRun("virsh domrename (by name)", + testCompareDomrenameByName, NULL) != 0) + ret = -1; + /* It's a bit awkward listing result before argument, but that's a * limitation of C99 vararg macros. */ # define DO_TEST(i, result, ...) \ -- 2.7.4

On Mon, Jan 08, 2018 at 01:03:00 -0200, Julio Faracco wrote:
This commit adds a domrename test case inside the 'virshtest' test for test driver.
Signed-off-by: Julio Faracco <jcfaracco@gmail.com> --- tests/virshtest.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
diff --git a/tests/virshtest.c b/tests/virshtest.c index 67453bd..f1cd5df 100644 --- a/tests/virshtest.c +++ b/tests/virshtest.c @@ -40,6 +40,7 @@ static const char *domuuid_fc4 = DOM_UUID "\n\n"; static const char *domid_fc4 = "2\n\n"; static const char *domname_fc4 = "fc4\n\n"; static const char *domstate_fc4 = "running\n\n"; +static const char *domrename_fc4 = "Domain successfully renamed\n\n";
static int testFilterLine(char *buffer, const char *toRemove) @@ -237,6 +238,14 @@ static int testCompareDomstateByName(const void *data ATTRIBUTE_UNUSED) return testCompareOutputLit(exp, NULL, argv); }
+static int testCompareDomrenameByName(const void *data ATTRIBUTE_UNUSED) +{ + const char *const argv[] = { VIRSH_CUSTOM, "domrename", "fc4", + "fc4new", NULL }; + const char *exp = domrename_fc4; + return testCompareOutputLit(exp, NULL, argv);
This is not checking that the VM was actually renamed, only that the success message was reported, which is pretty useless.
participants (2)
-
Julio Faracco
-
Peter Krempa