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

This commit introduces the testDomainRename() for test driver. It includes: - testDomainRename() implementation. - Testcase script to test 'domrename' command. Julio Faracco (2): test: Implementing testDomainRename(). tests: Adding test case for virsh 'domrename' command. src/test/test_driver.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/Makefile.am | 1 + tests/virsh-rename | 43 ++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100755 tests/virsh-rename -- 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 | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/test/test_driver.c b/src/test/test_driver.c index dc743b4..1461efb 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -2618,6 +2618,89 @@ 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, -1); + + 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 (virDomainObjIsActive(privdom)) { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("cannot rename active domain")); + goto cleanup; + } + + if (!privdom->persistent) { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("cannot rename a transient domain")); + goto cleanup; + } + + if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) { + virReportError(VIR_ERR_OPERATION_INVALID, + "%s", _("domain has to be shutoff before renaming")); + 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 +6905,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 01/15/2018 08:18 PM, 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 | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+)
diff --git a/src/test/test_driver.c b/src/test/test_driver.c index dc743b4..1461efb 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -2618,6 +2618,89 @@ 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, -1); + + 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 (virDomainObjIsActive(privdom)) { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("cannot rename active domain")); + goto cleanup; + } + + if (!privdom->persistent) { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("cannot rename a transient domain")); + goto cleanup; + }
This looks like redundant check. I mean, if domain has to be SHUTOFF it will certainly not be transient. But this is more user friendly error message so I'm keeping it in.
+ + if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) { + virReportError(VIR_ERR_OPERATION_INVALID, + "%s", _("domain has to be shutoff before renaming")); + 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 +6905,7 @@ static virHypervisorDriver testHypervisorDriver = { .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */ .connectIsAlive = testConnectIsAlive, /* 0.9.8 */ .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */ + .domainRename = testDomainRename, /* 4.0.0 */
No, 4.0.0 is already released. We're aiming towards 4.1.0 now ;-)
.domainScreenshot = testDomainScreenshot, /* 1.0.5 */ .domainGetMetadata = testDomainGetMetadata, /* 1.1.3 */ .domainSetMetadata = testDomainSetMetadata, /* 1.1.3 */
I'm fixing raised issues (and other small nits), ACKing and pushing. Michal

This commit introduce the virsh-rename test script to test the 'domrename' command. The test contains one succedeed script to rename and another failed test. Signed-off-by: Julio Faracco <jcfaracco@gmail.com> --- tests/Makefile.am | 1 + tests/virsh-rename | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100755 tests/virsh-rename diff --git a/tests/Makefile.am b/tests/Makefile.am index 3441dab..0ff90fb 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -410,6 +410,7 @@ libvirtd_test_scripts = \ virt-admin-self-test \ virsh-start \ virsh-undefine \ + virsh-rename \ virsh-uriprecedence \ virsh-vcpupin \ $(NULL) diff --git a/tests/virsh-rename b/tests/virsh-rename new file mode 100755 index 0000000..4d976bb --- /dev/null +++ b/tests/virsh-rename @@ -0,0 +1,43 @@ +#!/bin/sh +# exercise virsh's "domrename" command + +# Copyright (C) 2008-2009, 2017 Red Hat, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHEXP ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see +# <http://www.gnu.org/licenses/>. + +. "$(dirname $0)/test-lib.sh" + +if test "$VERBOSE" = yes; then + set -x + $abs_top_builddir/tools/virsh --version +fi + +fail=0 + +# Succeed, now: first shut down, then rename the domain. +$abs_top_builddir/tools/virsh -q -c test:///default \ + 'shutdown test; domrename test anothertest' > out 2>&1 +test $? = 1 && fail=1 + +# Failed, now: rename the domain without shutting down. +$abs_top_builddir/tools/virsh -q -c test:///default \ + 'domrename test anothertest' > out 2>&1 +test $? = 1 || fail=1 +cat <<\EOF > expout || fail=1 +error: Requested operation is not valid: cannot rename active domain +EOF +compare expout out || fail=1 + +(exit $fail); exit $fail -- 2.7.4

On 01/15/2018 08:18 PM, Julio Faracco wrote:
This commit introduce the virsh-rename test script to test the 'domrename' command. The test contains one succedeed script to rename and another failed test.
Signed-off-by: Julio Faracco <jcfaracco@gmail.com> --- tests/Makefile.am | 1 + tests/virsh-rename | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100755 tests/virsh-rename
diff --git a/tests/Makefile.am b/tests/Makefile.am index 3441dab..0ff90fb 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -410,6 +410,7 @@ libvirtd_test_scripts = \ virt-admin-self-test \ virsh-start \ virsh-undefine \ + virsh-rename \ virsh-uriprecedence \ virsh-vcpupin \ $(NULL) diff --git a/tests/virsh-rename b/tests/virsh-rename new file mode 100755 index 0000000..4d976bb --- /dev/null +++ b/tests/virsh-rename @@ -0,0 +1,43 @@ +#!/bin/sh +# exercise virsh's "domrename" command + +# Copyright (C) 2008-2009, 2017 Red Hat, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHEXP ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see +# <http://www.gnu.org/licenses/>. + +. "$(dirname $0)/test-lib.sh" + +if test "$VERBOSE" = yes; then + set -x + $abs_top_builddir/tools/virsh --version +fi + +fail=0 + +# Succeed, now: first shut down, then rename the domain. +$abs_top_builddir/tools/virsh -q -c test:///default \ + 'shutdown test; domrename test anothertest' > out 2>&1 +test $? = 1 && fail=1 + +# Failed, now: rename the domain without shutting down. +$abs_top_builddir/tools/virsh -q -c test:///default \ + 'domrename test anothertest' > out 2>&1 +test $? = 1 || fail=1 +cat <<\EOF > expout || fail=1 +error: Requested operation is not valid: cannot rename active domain +EOF +compare expout out || fail=1 + +(exit $fail); exit $fail
This still doesn't check whether rename was successful (which is not the same as 'virsh domrename' returning true). However, for that this would need to be written in C so that the connection object persists among function calls. What I have in mind is: conn = virConnectOpen("test:///default"); dom = virDomainLookupByName(conn, "test"); if (virDomainRename(dom, "anothertest") < 0) { testError(); } if (!(anotherdom = virDomainLookupByName(conn, "anothertest"))) { testError() ; } if (memcmp(dom.uuid, anotherdom.uuid) != 0) { testError(); } if ((dom = virDomainLookupByName(conn, "test"))) { testError{}; } /* This is just a pseudocode, of course you'll need to add more error * checks, dom.uuid is not directly accessible, etc. But you get the * idea. */ Having said that, I'm pushing the first patch and wait for you to post test. Also, I guess the right place for this is virshtest.c (unless you want to write new domrenametest.c). Michal
participants (2)
-
Julio Faracco
-
Michal Privoznik