From nobody Thu May 2 05:57:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.37 as permitted sender) client-ip=209.132.183.37; envelope-from=libvir-list-bounces@redhat.com; helo=mx5-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.37 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx5-phx2.redhat.com (mx5-phx2.redhat.com [209.132.183.37]) by mx.zohomail.com with SMTPS id 1488639862306326.4178852690518; Sat, 4 Mar 2017 07:04:22 -0800 (PST) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx5-phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v24F0x3O004656; Sat, 4 Mar 2017 10:00:59 -0500 Received: from smtp.corp.redhat.com (int-mx16.intmail.prod.int.phx2.redhat.com [10.5.11.28]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v24F0Rcv009855 for ; Sat, 4 Mar 2017 10:00:27 -0500 Received: by smtp.corp.redhat.com (Postfix) id EF62C2D654; Sat, 4 Mar 2017 15:00:27 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-78.phx2.redhat.com [10.3.116.78]) by smtp.corp.redhat.com (Postfix) with ESMTP id 987342D653 for ; Sat, 4 Mar 2017 15:00:27 +0000 (UTC) From: John Ferlan To: libvir-list@redhat.com Date: Sat, 4 Mar 2017 10:00:22 -0500 Message-Id: <20170304150024.30063-2-jferlan@redhat.com> In-Reply-To: <20170304150024.30063-1-jferlan@redhat.com> References: <20170304150024.30063-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.74 on 10.5.11.28 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/3] test: Make common test*ObjFindByName helpers X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Rather than have continued repeated sequences of : testDriverLock() xxx =3D vir*ObjFindByName() testDriverUnlock() if (xxx =3D=3D NULL) { virReportError goto cleanup; } Make some common helpers which will use the pattern and make a single reference using a single common error message. Altered for Interfaces, Storage Pools, Storage Volumes, and Node Devices. For each the common error message can now also indicate which 'name' was not found. For Storage Volumes, the "new" error will be more specific rather than just invalid argument. Signed-off-by: John Ferlan --- src/test/test_driver.c | 346 +++++++++++++--------------------------------= ---- 1 file changed, 90 insertions(+), 256 deletions(-) diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 61c82b9..c2536c9 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -3637,6 +3637,25 @@ static int testNetworkSetAutostart(virNetworkPtr net= work, */ =20 =20 +static virInterfaceObjPtr +testInterfaceObjFindByName(testDriverPtr privconn, + const char *name) +{ + virInterfaceObjPtr iface; + + testDriverLock(privconn); + iface =3D virInterfaceFindByName(&privconn->ifaces, name); + testDriverUnlock(privconn); + + if (!iface) + virReportError(VIR_ERR_NO_INTERFACE, + _("no interface with matching name '%s'"), + name); + + return iface; +} + + static int testConnectNumOfInterfaces(virConnectPtr conn) { testDriverPtr privconn =3D conn->privateData; @@ -3736,14 +3755,8 @@ static virInterfacePtr testInterfaceLookupByName(vir= ConnectPtr conn, virInterfaceObjPtr iface; virInterfacePtr ret =3D NULL; =20 - testDriverLock(privconn); - iface =3D virInterfaceFindByName(&privconn->ifaces, name); - testDriverUnlock(privconn); - - if (iface =3D=3D NULL) { - virReportError(VIR_ERR_NO_INTERFACE, NULL); + if (!(iface =3D testInterfaceObjFindByName(privconn, name))) goto cleanup; - } =20 ret =3D virGetInterface(conn, iface->def->name, iface->def->mac); =20 @@ -3789,13 +3802,9 @@ static int testInterfaceIsActive(virInterfacePtr ifa= ce) virInterfaceObjPtr obj; int ret =3D -1; =20 - testDriverLock(privconn); - obj =3D virInterfaceFindByName(&privconn->ifaces, iface->name); - testDriverUnlock(privconn); - if (!obj) { - virReportError(VIR_ERR_NO_INTERFACE, NULL); + if (!(obj =3D testInterfaceObjFindByName(privconn, iface->name))) goto cleanup; - } + ret =3D virInterfaceObjIsActive(obj); =20 cleanup: @@ -3900,15 +3909,8 @@ static char *testInterfaceGetXMLDesc(virInterfacePtr= iface, =20 virCheckFlags(0, NULL); =20 - testDriverLock(privconn); - privinterface =3D virInterfaceFindByName(&privconn->ifaces, - iface->name); - testDriverUnlock(privconn); - - if (privinterface =3D=3D NULL) { - virReportError(VIR_ERR_NO_INTERFACE, __FUNCTION__); + if (!(privinterface =3D testInterfaceObjFindByName(privconn, iface->na= me))) goto cleanup; - } =20 ret =3D virInterfaceDefFormat(privinterface->def); =20 @@ -3953,14 +3955,8 @@ static int testInterfaceUndefine(virInterfacePtr ifa= ce) virInterfaceObjPtr privinterface; int ret =3D -1; =20 - testDriverLock(privconn); - privinterface =3D virInterfaceFindByName(&privconn->ifaces, - iface->name); - - if (privinterface =3D=3D NULL) { - virReportError(VIR_ERR_NO_INTERFACE, NULL); + if (!(privinterface =3D testInterfaceObjFindByName(privconn, iface->na= me))) goto cleanup; - } =20 virInterfaceRemove(&privconn->ifaces, privinterface); @@ -3980,14 +3976,8 @@ static int testInterfaceCreate(virInterfacePtr iface, =20 virCheckFlags(0, -1); =20 - testDriverLock(privconn); - privinterface =3D virInterfaceFindByName(&privconn->ifaces, - iface->name); - - if (privinterface =3D=3D NULL) { - virReportError(VIR_ERR_NO_INTERFACE, NULL); + if (!(privinterface =3D testInterfaceObjFindByName(privconn, iface->na= me))) goto cleanup; - } =20 if (privinterface->active !=3D 0) { virReportError(VIR_ERR_OPERATION_INVALID, NULL); @@ -4013,14 +4003,8 @@ static int testInterfaceDestroy(virInterfacePtr ifac= e, =20 virCheckFlags(0, -1); =20 - testDriverLock(privconn); - privinterface =3D virInterfaceFindByName(&privconn->ifaces, - iface->name); - - if (privinterface =3D=3D NULL) { - virReportError(VIR_ERR_NO_INTERFACE, NULL); + if (!(privinterface =3D testInterfaceObjFindByName(privconn, iface->na= me))) goto cleanup; - } =20 if (privinterface->active =3D=3D 0) { virReportError(VIR_ERR_OPERATION_INVALID, NULL); @@ -4055,6 +4039,25 @@ static int testStoragePoolObjSetDefaults(virStorageP= oolObjPtr pool) } =20 =20 +static virStoragePoolObjPtr +testStoragePoolObjFindByName(testDriverPtr privconn, + const char *name) +{ + virStoragePoolObjPtr pool; + + testDriverLock(privconn); + pool =3D virStoragePoolObjFindByName(&privconn->pools, name); + testDriverUnlock(privconn); + + if (!pool) + virReportError(VIR_ERR_NO_STORAGE_POOL, + _("no storage pool with matching name '%s'"), + name); + + return pool; +} + + static virStoragePoolPtr testStoragePoolLookupByUUID(virConnectPtr conn, const unsigned char *uuid) @@ -4089,14 +4092,8 @@ testStoragePoolLookupByName(virConnectPtr conn, virStoragePoolObjPtr pool; virStoragePoolPtr ret =3D NULL; =20 - testDriverLock(privconn); - pool =3D virStoragePoolObjFindByName(&privconn->pools, name); - testDriverUnlock(privconn); - - if (pool =3D=3D NULL) { - virReportError(VIR_ERR_NO_STORAGE_POOL, NULL); + if (!(pool =3D testStoragePoolObjFindByName(privconn, name))) goto cleanup; - } =20 ret =3D virGetStoragePool(conn, pool->def->name, pool->def->uuid, NULL, NULL); @@ -4283,15 +4280,8 @@ testStoragePoolCreate(virStoragePoolPtr pool, =20 virCheckFlags(0, -1); =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } =20 if (virStoragePoolObjIsActive(privpool)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -4474,14 +4464,8 @@ testStoragePoolUndefine(virStoragePoolPtr pool) int ret =3D -1; virObjectEventPtr event =3D NULL; =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } =20 if (virStoragePoolObjIsActive(privpool)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -4515,15 +4499,8 @@ testStoragePoolBuild(virStoragePoolPtr pool, =20 virCheckFlags(0, -1); =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } =20 if (virStoragePoolObjIsActive(privpool)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -4547,14 +4524,8 @@ testStoragePoolDestroy(virStoragePoolPtr pool) int ret =3D -1; virObjectEventPtr event =3D NULL; =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } =20 if (!virStoragePoolObjIsActive(privpool)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -4592,15 +4563,8 @@ testStoragePoolDelete(virStoragePoolPtr pool, =20 virCheckFlags(0, -1); =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } =20 if (virStoragePoolObjIsActive(privpool)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -4628,15 +4592,8 @@ testStoragePoolRefresh(virStoragePoolPtr pool, =20 virCheckFlags(0, -1); =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } =20 if (!virStoragePoolObjIsActive(privpool)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -4663,15 +4620,8 @@ testStoragePoolGetInfo(virStoragePoolPtr pool, virStoragePoolObjPtr privpool; int ret =3D -1; =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } =20 memset(info, 0, sizeof(virStoragePoolInfo)); if (privpool->active) @@ -4699,15 +4649,8 @@ testStoragePoolGetXMLDesc(virStoragePoolPtr pool, =20 virCheckFlags(0, NULL); =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } =20 ret =3D virStoragePoolDefFormat(privpool->def); =20 @@ -4725,15 +4668,8 @@ testStoragePoolGetAutostart(virStoragePoolPtr pool, virStoragePoolObjPtr privpool; int ret =3D -1; =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } =20 if (!privpool->configFile) { *autostart =3D 0; @@ -4756,15 +4692,8 @@ testStoragePoolSetAutostart(virStoragePoolPtr pool, virStoragePoolObjPtr privpool; int ret =3D -1; =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } =20 if (!privpool->configFile) { virReportError(VIR_ERR_INVALID_ARG, @@ -4790,15 +4719,8 @@ testStoragePoolNumOfVolumes(virStoragePoolPtr pool) virStoragePoolObjPtr privpool; int ret =3D -1; =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } =20 if (!virStoragePoolObjIsActive(privpool)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -4826,16 +4748,8 @@ testStoragePoolListVolumes(virStoragePoolPtr pool, =20 memset(names, 0, maxnames * sizeof(*names)); =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } - =20 if (!virStoragePoolObjIsActive(privpool)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -4938,16 +4852,8 @@ testStorageVolLookupByName(virStoragePoolPtr pool, virStorageVolDefPtr privvol; virStorageVolPtr ret =3D NULL; =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } - =20 if (!virStoragePoolObjIsActive(privpool)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -5058,15 +4964,8 @@ testStorageVolCreateXML(virStoragePoolPtr pool, =20 virCheckFlags(0, NULL); =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } =20 if (!virStoragePoolObjIsActive(privpool)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -5132,15 +5031,8 @@ testStorageVolCreateXMLFrom(virStoragePoolPtr pool, =20 virCheckFlags(0, NULL); =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - pool->name); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, pool->name))) goto cleanup; - } =20 if (!virStoragePoolObjIsActive(privpool)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -5215,16 +5107,8 @@ testStorageVolDelete(virStorageVolPtr vol, =20 virCheckFlags(0, -1); =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - vol->pool); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, vol->pool))) goto cleanup; - } - =20 privvol =3D virStorageVolDefFindByName(privpool, vol->name); =20 @@ -5285,15 +5169,8 @@ testStorageVolGetInfo(virStorageVolPtr vol, virStorageVolDefPtr privvol; int ret =3D -1; =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - vol->pool); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, vol->pool))) goto cleanup; - } =20 privvol =3D virStorageVolDefFindByName(privpool, vol->name); =20 @@ -5333,15 +5210,8 @@ testStorageVolGetXMLDesc(virStorageVolPtr vol, =20 virCheckFlags(0, NULL); =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - vol->pool); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, vol->pool))) goto cleanup; - } =20 privvol =3D virStorageVolDefFindByName(privpool, vol->name); =20 @@ -5374,15 +5244,8 @@ testStorageVolGetPath(virStorageVolPtr vol) virStorageVolDefPtr privvol; char *ret =3D NULL; =20 - testDriverLock(privconn); - privpool =3D virStoragePoolObjFindByName(&privconn->pools, - vol->pool); - testDriverUnlock(privconn); - - if (privpool =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privpool =3D testStoragePoolObjFindByName(privconn, vol->pool))) goto cleanup; - } =20 privvol =3D virStorageVolDefFindByName(privpool, vol->name); =20 @@ -5410,6 +5273,25 @@ testStorageVolGetPath(virStorageVolPtr vol) =20 /* Node device implementations */ =20 +static virNodeDeviceObjPtr +testNodeDeviceObjFindByName(testDriverPtr driver, + const char *name) +{ + virNodeDeviceObjPtr obj; + + testDriverLock(driver); + obj =3D virNodeDeviceObjFindByName(&driver->devs, name); + testDriverUnlock(driver); + + if (!obj) + virReportError(VIR_ERR_NO_NODE_DEVICE, + _("no node device with matching name '%s'"), + name); + + return obj; +} + + static int testNodeNumOfDevices(virConnectPtr conn, const char *cap, @@ -5475,16 +5357,8 @@ testNodeDeviceLookupByName(virConnectPtr conn, const= char *name) virNodeDeviceObjPtr obj; virNodeDevicePtr ret =3D NULL; =20 - testDriverLock(driver); - obj =3D virNodeDeviceObjFindByName(&driver->devs, name); - testDriverUnlock(driver); - - if (!obj) { - virReportError(VIR_ERR_NO_NODE_DEVICE, - _("no node device with matching name '%s'"), - name); + if (!(obj =3D testNodeDeviceObjFindByName(driver, name))) goto cleanup; - } =20 if ((ret =3D virGetNodeDevice(conn, name))) { if (VIR_STRDUP(ret->parent, obj->def->parent) < 0) @@ -5507,16 +5381,8 @@ testNodeDeviceGetXMLDesc(virNodeDevicePtr dev, =20 virCheckFlags(0, NULL); =20 - testDriverLock(driver); - obj =3D virNodeDeviceObjFindByName(&driver->devs, dev->name); - testDriverUnlock(driver); - - if (!obj) { - virReportError(VIR_ERR_NO_NODE_DEVICE, - _("no node device with matching name '%s'"), - dev->name); + if (!(obj =3D testNodeDeviceObjFindByName(driver, dev->name))) goto cleanup; - } =20 ret =3D virNodeDeviceDefFormat(obj->def); =20 @@ -5533,16 +5399,8 @@ testNodeDeviceGetParent(virNodeDevicePtr dev) virNodeDeviceObjPtr obj; char *ret =3D NULL; =20 - testDriverLock(driver); - obj =3D virNodeDeviceObjFindByName(&driver->devs, dev->name); - testDriverUnlock(driver); - - if (!obj) { - virReportError(VIR_ERR_NO_NODE_DEVICE, - _("no node device with matching name '%s'"), - dev->name); + if (!(obj =3D testNodeDeviceObjFindByName(driver, dev->name))) goto cleanup; - } =20 if (obj->def->parent) { ignore_value(VIR_STRDUP(ret, obj->def->parent)); @@ -5567,16 +5425,8 @@ testNodeDeviceNumOfCaps(virNodeDevicePtr dev) int ncaps =3D 0; int ret =3D -1; =20 - testDriverLock(driver); - obj =3D virNodeDeviceObjFindByName(&driver->devs, dev->name); - testDriverUnlock(driver); - - if (!obj) { - virReportError(VIR_ERR_NO_NODE_DEVICE, - _("no node device with matching name '%s'"), - dev->name); + if (!(obj =3D testNodeDeviceObjFindByName(driver, dev->name))) goto cleanup; - } =20 for (caps =3D obj->def->caps; caps; caps =3D caps->next) ++ncaps; @@ -5598,16 +5448,8 @@ testNodeDeviceListCaps(virNodeDevicePtr dev, char **= const names, int maxnames) int ncaps =3D 0; int ret =3D -1; =20 - testDriverLock(driver); - obj =3D virNodeDeviceObjFindByName(&driver->devs, dev->name); - testDriverUnlock(driver); - - if (!obj) { - virReportError(VIR_ERR_NO_NODE_DEVICE, - _("no node device with matching name '%s'"), - dev->name); + if (!(obj =3D testNodeDeviceObjFindByName(driver, dev->name))) goto cleanup; - } =20 for (caps =3D obj->def->caps; caps && ncaps < maxnames; caps =3D caps-= >next) { if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->dat= a.type)) < 0) @@ -5771,16 +5613,8 @@ testNodeDeviceDestroy(virNodeDevicePtr dev) char *parent_name =3D NULL, *wwnn =3D NULL, *wwpn =3D NULL; virObjectEventPtr event =3D NULL; =20 - testDriverLock(driver); - obj =3D virNodeDeviceObjFindByName(&driver->devs, dev->name); - testDriverUnlock(driver); - - if (!obj) { - virReportError(VIR_ERR_NO_NODE_DEVICE, - _("no node device with matching name '%s'"), - dev->name); + if (!(obj =3D testNodeDeviceObjFindByName(driver, dev->name))) goto out; - } =20 if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) =3D=3D -1) goto out; --=20 2.9.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 05:57:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.24 as permitted sender) client-ip=209.132.183.24; envelope-from=libvir-list-bounces@redhat.com; helo=mx3-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.24 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx3-phx2.redhat.com (mx3-phx2.redhat.com [209.132.183.24]) by mx.zohomail.com with SMTPS id 1488639843875887.7340181518516; Sat, 4 Mar 2017 07:04:03 -0800 (PST) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx3-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v24F0Tpc021988; Sat, 4 Mar 2017 10:00:29 -0500 Received: from smtp.corp.redhat.com (int-mx16.intmail.prod.int.phx2.redhat.com [10.5.11.28]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v24F0SbK009863 for ; Sat, 4 Mar 2017 10:00:28 -0500 Received: by smtp.corp.redhat.com (Postfix) id 61F822D654; Sat, 4 Mar 2017 15:00:28 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-78.phx2.redhat.com [10.3.116.78]) by smtp.corp.redhat.com (Postfix) with ESMTP id 218BF2D653 for ; Sat, 4 Mar 2017 15:00:28 +0000 (UTC) From: John Ferlan To: libvir-list@redhat.com Date: Sat, 4 Mar 2017 10:00:23 -0500 Message-Id: <20170304150024.30063-3-jferlan@redhat.com> In-Reply-To: <20170304150024.30063-1-jferlan@redhat.com> References: <20170304150024.30063-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.74 on 10.5.11.28 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/3] test: Make a common testNetworkObjFindByName X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Rather than have multiple places using the same pattern to find a network by name using virNetworkObjFindByName, create a common helper which will provide a consistent error message as well. Signed-off-by: John Ferlan --- src/test/test_driver.c | 57 +++++++++++++++++++++-------------------------= ---- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/src/test/test_driver.c b/src/test/test_driver.c index c2536c9..e4402cc 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -3236,6 +3236,22 @@ static virNetworkPtr testNetworkLookupByUUID(virConn= ectPtr conn, return ret; } =20 + +static virNetworkObjPtr +testNetworkObjFindByName(testDriverPtr privconn, + const char *name) +{ + virNetworkObjPtr net; + + if (!(net =3D virNetworkObjFindByName(privconn->networks, name))) + virReportError(VIR_ERR_NO_NETWORK, + _("no network with matching name '%s'"), + name); + + return net; +} + + static virNetworkPtr testNetworkLookupByName(virConnectPtr conn, const char *name) { @@ -3243,11 +3259,8 @@ static virNetworkPtr testNetworkLookupByName(virConn= ectPtr conn, virNetworkObjPtr net; virNetworkPtr ret =3D NULL; =20 - net =3D virNetworkObjFindByName(privconn->networks, name); - if (net =3D=3D NULL) { - virReportError(VIR_ERR_NO_NETWORK, NULL); + if (!(net =3D testNetworkObjFindByName(privconn, name))) goto cleanup; - } =20 ret =3D virGetNetwork(conn, net->def->name, net->def->uuid); =20 @@ -3411,12 +3424,8 @@ static int testNetworkUndefine(virNetworkPtr network) int ret =3D -1; virObjectEventPtr event =3D NULL; =20 - privnet =3D virNetworkObjFindByName(privconn->networks, network->name); - - if (privnet =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privnet =3D testNetworkObjFindByName(privconn, network->name))) goto cleanup; - } =20 if (virNetworkObjIsActive(privnet)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -3490,11 +3499,8 @@ static int testNetworkCreate(virNetworkPtr network) int ret =3D -1; virObjectEventPtr event =3D NULL; =20 - privnet =3D virNetworkObjFindByName(privconn->networks, network->name); - if (privnet =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privnet =3D testNetworkObjFindByName(privconn, network->name))) goto cleanup; - } =20 if (virNetworkObjIsActive(privnet)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -3521,11 +3527,8 @@ static int testNetworkDestroy(virNetworkPtr network) int ret =3D -1; virObjectEventPtr event =3D NULL; =20 - privnet =3D virNetworkObjFindByName(privconn->networks, network->name); - if (privnet =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privnet =3D testNetworkObjFindByName(privconn, network->name))) goto cleanup; - } =20 privnet->active =3D 0; event =3D virNetworkEventLifecycleNew(privnet->def->name, privnet->def= ->uuid, @@ -3551,11 +3554,8 @@ static char *testNetworkGetXMLDesc(virNetworkPtr net= work, =20 virCheckFlags(0, NULL); =20 - privnet =3D virNetworkObjFindByName(privconn->networks, network->name); - if (privnet =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privnet =3D testNetworkObjFindByName(privconn, network->name))) goto cleanup; - } =20 ret =3D virNetworkDefFormat(privnet->def, flags); =20 @@ -3569,11 +3569,8 @@ static char *testNetworkGetBridgeName(virNetworkPtr = network) { char *bridge =3D NULL; virNetworkObjPtr privnet; =20 - privnet =3D virNetworkObjFindByName(privconn->networks, network->name); - if (privnet =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privnet =3D testNetworkObjFindByName(privconn, network->name))) goto cleanup; - } =20 if (!(privnet->def->bridge)) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -3596,11 +3593,8 @@ static int testNetworkGetAutostart(virNetworkPtr net= work, virNetworkObjPtr privnet; int ret =3D -1; =20 - privnet =3D virNetworkObjFindByName(privconn->networks, network->name); - if (privnet =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privnet =3D testNetworkObjFindByName(privconn, network->name))) goto cleanup; - } =20 *autostart =3D privnet->autostart; ret =3D 0; @@ -3617,11 +3611,8 @@ static int testNetworkSetAutostart(virNetworkPtr net= work, virNetworkObjPtr privnet; int ret =3D -1; =20 - privnet =3D virNetworkObjFindByName(privconn->networks, network->name); - if (privnet =3D=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__); + if (!(privnet =3D testNetworkObjFindByName(privconn, network->name))) goto cleanup; - } =20 privnet->autostart =3D autostart ? 1 : 0; ret =3D 0; --=20 2.9.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 05:57:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.25 as permitted sender) client-ip=209.132.183.25; envelope-from=libvir-list-bounces@redhat.com; helo=mx4-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.25 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx4-phx2.redhat.com (mx4-phx2.redhat.com [209.132.183.25]) by mx.zohomail.com with SMTPS id 1488639862603929.1891413580888; Sat, 4 Mar 2017 07:04:22 -0800 (PST) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx4-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v24F0VLe026493; Sat, 4 Mar 2017 10:00:32 -0500 Received: from smtp.corp.redhat.com (int-mx16.intmail.prod.int.phx2.redhat.com [10.5.11.28]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v24F0SEH009870 for ; Sat, 4 Mar 2017 10:00:28 -0500 Received: by smtp.corp.redhat.com (Postfix) id CBF332D654; Sat, 4 Mar 2017 15:00:28 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-78.phx2.redhat.com [10.3.116.78]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8881F2D653 for ; Sat, 4 Mar 2017 15:00:28 +0000 (UTC) From: John Ferlan To: libvir-list@redhat.com Date: Sat, 4 Mar 2017 10:00:24 -0500 Message-Id: <20170304150024.30063-4-jferlan@redhat.com> In-Reply-To: <20170304150024.30063-1-jferlan@redhat.com> References: <20170304150024.30063-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.74 on 10.5.11.28 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 3/3] test: Make common test*ObjFindByUUID helpers X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Make common helpers testNetworkObjFindByUUID and testStoragePoolObjFindByUU= ID which will replace the repeated patter for each to find objects by UUID. As a bonus, the error message processing will also provide the failed uuids= tr rather than a generic error message. Signed-off-by: John Ferlan --- src/test/test_driver.c | 96 +++++++++++++++++++++++++++-------------------= ---- 1 file changed, 52 insertions(+), 44 deletions(-) diff --git a/src/test/test_driver.c b/src/test/test_driver.c index e4402cc..1c54069 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -3216,6 +3216,24 @@ static int testDomainInterfaceStats(virDomainPtr dom= ain, } =20 =20 +static virNetworkObjPtr +testNetworkObjFindByUUID(testDriverPtr privconn, + const unsigned char *uuid) +{ + virNetworkObjPtr net; + char uuidstr[VIR_UUID_STRING_BUFLEN]; + + if (!(net =3D virNetworkObjFindByUUID(privconn->networks, uuid))) { + virUUIDFormat(uuid, uuidstr); + virReportError(VIR_ERR_NO_NETWORK, + _("no network with matching uuid '%s'"), + uuidstr); + } + + return net; +} + + static virNetworkPtr testNetworkLookupByUUID(virConnectPtr conn, const unsigned char *uuid) { @@ -3223,11 +3241,8 @@ static virNetworkPtr testNetworkLookupByUUID(virConn= ectPtr conn, virNetworkObjPtr net; virNetworkPtr ret =3D NULL; =20 - net =3D virNetworkObjFindByUUID(privconn->networks, uuid); - if (net =3D=3D NULL) { - virReportError(VIR_ERR_NO_NETWORK, NULL); + if (!(net =3D testNetworkObjFindByUUID(privconn, uuid))) goto cleanup; - } =20 ret =3D virGetNetwork(conn, net->def->name, net->def->uuid); =20 @@ -3326,11 +3341,9 @@ static int testNetworkIsActive(virNetworkPtr net) virNetworkObjPtr obj; int ret =3D -1; =20 - obj =3D virNetworkObjFindByUUID(privconn->networks, net->uuid); - if (!obj) { - virReportError(VIR_ERR_NO_NETWORK, NULL); + if (!(obj =3D testNetworkObjFindByUUID(privconn, net->uuid))) goto cleanup; - } + ret =3D virNetworkObjIsActive(obj); =20 cleanup: @@ -3344,11 +3357,9 @@ static int testNetworkIsPersistent(virNetworkPtr net) virNetworkObjPtr obj; int ret =3D -1; =20 - obj =3D virNetworkObjFindByUUID(privconn->networks, net->uuid); - if (!obj) { - virReportError(VIR_ERR_NO_NETWORK, NULL); + if (!(obj =3D testNetworkObjFindByUUID(privconn, net->uuid))) goto cleanup; - } + ret =3D obj->persistent; =20 cleanup: @@ -3462,12 +3473,8 @@ testNetworkUpdate(virNetworkPtr net, VIR_NETWORK_UPDATE_AFFECT_CONFIG, -1); =20 - network =3D virNetworkObjFindByUUID(privconn->networks, net->uuid); - if (!network) { - virReportError(VIR_ERR_NO_NETWORK, - "%s", _("no network with matching uuid")); + if (!(network =3D testNetworkObjFindByUUID(privconn, net->uuid))) goto cleanup; - } =20 /* VIR_NETWORK_UPDATE_AFFECT_CURRENT means "change LIVE if network * is active, else change CONFIG @@ -4049,6 +4056,28 @@ testStoragePoolObjFindByName(testDriverPtr privconn, } =20 =20 +static virStoragePoolObjPtr +testStoragePoolObjFindByUUID(testDriverPtr privconn, + const unsigned char *uuid) +{ + virStoragePoolObjPtr pool; + char uuidstr[VIR_UUID_STRING_BUFLEN]; + + testDriverLock(privconn); + pool =3D virStoragePoolObjFindByUUID(&privconn->pools, uuid); + testDriverUnlock(privconn); + + if (!pool) { + virUUIDFormat(uuid, uuidstr); + virReportError(VIR_ERR_NO_STORAGE_POOL, + _("no storage pool with matching uuid '%s'"), + uuidstr); + } + + return pool; +} + + static virStoragePoolPtr testStoragePoolLookupByUUID(virConnectPtr conn, const unsigned char *uuid) @@ -4057,14 +4086,8 @@ testStoragePoolLookupByUUID(virConnectPtr conn, virStoragePoolObjPtr pool; virStoragePoolPtr ret =3D NULL; =20 - testDriverLock(privconn); - pool =3D virStoragePoolObjFindByUUID(&privconn->pools, uuid); - testDriverUnlock(privconn); - - if (pool =3D=3D NULL) { - virReportError(VIR_ERR_NO_STORAGE_POOL, NULL); + if (!(pool =3D testStoragePoolObjFindByUUID(privconn, uuid))) goto cleanup; - } =20 ret =3D virGetStoragePool(conn, pool->def->name, pool->def->uuid, NULL, NULL); @@ -4222,13 +4245,9 @@ static int testStoragePoolIsActive(virStoragePoolPtr= pool) virStoragePoolObjPtr obj; int ret =3D -1; =20 - testDriverLock(privconn); - obj =3D virStoragePoolObjFindByUUID(&privconn->pools, pool->uuid); - testDriverUnlock(privconn); - if (!obj) { - virReportError(VIR_ERR_NO_STORAGE_POOL, NULL); + if (!(obj =3D testStoragePoolObjFindByUUID(privconn, pool->uuid))) goto cleanup; - } + ret =3D virStoragePoolObjIsActive(obj); =20 cleanup: @@ -4243,13 +4262,9 @@ static int testStoragePoolIsPersistent(virStoragePoo= lPtr pool) virStoragePoolObjPtr obj; int ret =3D -1; =20 - testDriverLock(privconn); - obj =3D virStoragePoolObjFindByUUID(&privconn->pools, pool->uuid); - testDriverUnlock(privconn); - if (!obj) { - virReportError(VIR_ERR_NO_STORAGE_POOL, NULL); + if (!(obj =3D testStoragePoolObjFindByUUID(privconn, pool->uuid))) goto cleanup; - } + ret =3D obj->configFile ? 1 : 0; =20 cleanup: @@ -4781,15 +4796,8 @@ testStoragePoolListAllVolumes(virStoragePoolPtr obj, =20 virCheckFlags(0, -1); =20 - testDriverLock(privconn); - pool =3D virStoragePoolObjFindByUUID(&privconn->pools, obj->uuid); - testDriverUnlock(privconn); - - if (!pool) { - virReportError(VIR_ERR_NO_STORAGE_POOL, "%s", - _("no storage pool with matching uuid")); + if (!(pool =3D testStoragePoolObjFindByUUID(privconn, obj->uuid))) goto cleanup; - } =20 if (!virStoragePoolObjIsActive(pool)) { virReportError(VIR_ERR_OPERATION_INVALID, "%s", --=20 2.9.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list