From nobody Tue Mar 24 01:23:04 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1773302485; cv=none; d=zohomail.com; s=zohoarc; b=FboSvtk9MVtJyZL6hSKx9G8UUAj/gX5d8f5EyDaj0igkTvpiXWcmcdCK2SxH7RONc91qXUe6c8n9kG2rimLynIHjIF67xonKlJkfk2asJ7Xrul1Poi0Trw5mnF5J686772A7tH4NBWyeXPW4CxnrdRLG6L/O6NU6RWi+T/v7PQ4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1773302485; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=lJLfFXFhHl7nKGcXTQW+2MF9JkGf7AAu92z74tCf8tE=; b=QlVh5qUNt6aJMbkIT2PNpseskHsSWhskE8P2r2rCW0ouHSrkb2F070hFhphIsVz828imHW6gbsd50teRfRKNbrPH81d18WTul3tbuGxnJk6BzpjjEurN0Njk/v1LmeLQXMUHvgWtWEdSmmlmDrePVM9TfpruyPelOJAskQhlpOs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1773302485275450.40269747099376; Thu, 12 Mar 2026 01:01:25 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 4EAEA418AC; Thu, 12 Mar 2026 04:01:24 -0400 (EDT) Received: from [172.19.199.12] (lists.libvirt.org [8.43.85.245]) by lists.libvirt.org (Postfix) with ESMTP id E334741B47; Thu, 12 Mar 2026 03:57:34 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 993) id C4A943F34D; Thu, 12 Mar 2026 03:57:26 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits) server-digest SHA256) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id C1E263F362 for ; Thu, 12 Mar 2026 03:57:25 -0400 (EDT) Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-41-0Sdd9QfeMg6SgLH_OSv4LQ-1; Thu, 12 Mar 2026 03:57:23 -0400 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E734E195609D for ; Thu, 12 Mar 2026 07:57:22 +0000 (UTC) Received: from moe (unknown [10.43.3.236]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 21B55180066F for ; Thu, 12 Mar 2026 07:57:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HELO_MISC_IP,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1773302245; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lJLfFXFhHl7nKGcXTQW+2MF9JkGf7AAu92z74tCf8tE=; b=Hdbv+kuOo/bn0nnF16rUrpJQrakuZAHxUk8uhdwF0ZQxLRf906XDUnpQYoX5lACpCyoCeE kkGfozPL5Hzrh8yZFHMnXHCkfkjnKuofObSvsQ+D88Iu+r8NwidbPW86hxK43EeowJhnkD NhmKzvb7ev/LTVVbXYevUVOxFizPjDA= X-MC-Unique: 0Sdd9QfeMg6SgLH_OSv4LQ-1 X-Mimecast-MFC-AGG-ID: 0Sdd9QfeMg6SgLH_OSv4LQ_1773302243 To: devel@lists.libvirt.org Subject: [PATCH 2/5] qemuxmlconftest: Separate fake drivers into a separate file Date: Thu, 12 Mar 2026 08:57:11 +0100 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: KADYSiVRoQd1EEwZlW4iPwGfD4liiV6K31Ng0Y5lG0s_1773302243 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: HOVMGSLQLNFPYYKDSYS3IEMU5YFIMF6Q X-Message-ID-Hash: HOVMGSLQLNFPYYKDSYS3IEMU5YFIMF6Q X-MailFrom: mprivozn@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-devel.lists.libvirt.org-0; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Michal Privoznik via Devel Reply-To: Michal Privoznik X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1773302485742158500 Content-Type: text/plain; charset="utf-8"; x-default="true" From: Michal Privoznik One of the tests that qemuxmlconftest does is generate cmd line for given domain XML. This process might involve talking to other drivers (secret/storage/nwfilter/network). To produce predictable output the test comes with fake implementation of APIs of those drivers. Well, move that implementation into a separate file so that it can be reused by other tests (notably, qemuhotplugtest is going to use it). Signed-off-by: Michal Privoznik --- tests/meson.build | 5 +- tests/qemufakedrivers.c | 368 ++++++++++++++++++++++++++++++++++++++++ tests/qemufakedrivers.h | 19 +++ tests/qemuxmlconftest.c | 331 +----------------------------------- 4 files changed, 396 insertions(+), 327 deletions(-) create mode 100644 tests/qemufakedrivers.c create mode 100644 tests/qemufakedrivers.h diff --git a/tests/meson.build b/tests/meson.build index 847538d2e4..83aa0104bb 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -151,7 +151,10 @@ endif if conf.has('WITH_QEMU') test_utils_qemu_lib =3D static_library( 'test_utils_qemu', - [ 'testutilsqemu.c', 'testutilsqemuschema.c' ], + [ 'qemufakedrivers.c', + 'testutilsqemu.c', + 'testutilsqemuschema.c', + ], dependencies: [ tests_dep ], ) =20 diff --git a/tests/qemufakedrivers.c b/tests/qemufakedrivers.c new file mode 100644 index 0000000000..94ad5aeb0b --- /dev/null +++ b/tests/qemufakedrivers.c @@ -0,0 +1,368 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#include + +#include "qemufakedrivers.h" +#include "testutils.h" +#include "datatypes.h" +#include "storage_conf.h" + +#define VIR_FROM_THIS VIR_FROM_QEMU + +static unsigned char * +fakeSecretGetValue(virSecretPtr obj G_GNUC_UNUSED, + size_t *value_size, + unsigned int fakeflags G_GNUC_UNUSED) +{ + char *secret; + secret =3D g_strdup("AQCVn5hO6HzFAhAAq0NCv8jtJcIcE+HOBlMQ1A"); + *value_size =3D strlen(secret); + return (unsigned char *) secret; +} + + +static virSecretPtr +fakeSecretLookupByUsage(virConnectPtr conn, + int usageType, + const char *usageID) +{ + unsigned char uuid[VIR_UUID_BUFLEN]; + if (usageType =3D=3D VIR_SECRET_USAGE_TYPE_VOLUME) { + if (!STRPREFIX(usageID, "/storage/guest_disks/")) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "test provided invalid volume storage prefix '%= s'", + usageID); + return NULL; + } + } else if (STRNEQ(usageID, "mycluster_myname") && + STRNEQ(usageID, "client.admin secret")) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "test provided incorrect usage '%s'", usageID); + return NULL; + } + + if (virUUIDGenerate(uuid) < 0) + return NULL; + + return virGetSecret(conn, uuid, usageType, usageID); +} + + +static virSecretPtr +fakeSecretLookupByUUID(virConnectPtr conn, + const unsigned char *uuid) +{ + /* NB: This mocked value could be "tls" or "volume" depending on + * which test is being run, we'll leave at NONE (or 0) */ + return virGetSecret(conn, uuid, VIR_SECRET_USAGE_TYPE_NONE, ""); +} + + +static virSecretDriver fakeSecretDriver =3D { + .connectNumOfSecrets =3D NULL, + .connectListSecrets =3D NULL, + .secretLookupByUUID =3D fakeSecretLookupByUUID, + .secretLookupByUsage =3D fakeSecretLookupByUsage, + .secretDefineXML =3D NULL, + .secretGetXMLDesc =3D NULL, + .secretSetValue =3D NULL, + .secretGetValue =3D fakeSecretGetValue, + .secretUndefine =3D NULL, +}; + + +virSecretDriver * +testQemuGetFakeSecretDriver(void) +{ + return &fakeSecretDriver; +} + + +#define STORAGE_POOL_XML_PATH "storagepoolxml2xmlout/" +static const unsigned char fakeUUID[VIR_UUID_BUFLEN] =3D "fakeuuid"; + +static virStoragePoolPtr +fakeStoragePoolLookupByName(virConnectPtr conn, + const char *name) +{ + g_autofree char *xmlpath =3D NULL; + + if (STRNEQ(name, "inactive")) { + xmlpath =3D g_strdup_printf("%s/%s%s.xml", abs_srcdir, + STORAGE_POOL_XML_PATH, name); + + if (!virFileExists(xmlpath)) { + virReportError(VIR_ERR_NO_STORAGE_POOL, + "File '%s' not found", xmlpath); + return NULL; + } + } + + return virGetStoragePool(conn, name, fakeUUID, NULL, NULL); +} + + +static virStorageVolPtr +fakeStorageVolLookupByName(virStoragePoolPtr pool, + const char *name) +{ + g_auto(GStrv) volinfo =3D NULL; + + if (STREQ(pool->name, "inactive")) { + virReportError(VIR_ERR_OPERATION_INVALID, + "storage pool '%s' is not active", pool->name); + return NULL; + } + + if (STREQ(name, "nonexistent")) { + virReportError(VIR_ERR_NO_STORAGE_VOL, + "no storage vol with matching name '%s'", name); + return NULL; + } + + if (!(volinfo =3D g_strsplit(name, "+", 2))) + return NULL; + + if (!volinfo[1]) { + return virGetStorageVol(pool->conn, pool->name, name, "block", NUL= L, NULL); + } + + return virGetStorageVol(pool->conn, pool->name, volinfo[1], volinfo[0], + NULL, NULL); +} + + +static int +fakeStorageVolGetInfo(virStorageVolPtr vol, + virStorageVolInfoPtr info) +{ + memset(info, 0, sizeof(*info)); + + info->type =3D virStorageVolTypeFromString(vol->key); + + if (info->type < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "Invalid volume type '%s'", vol->key); + return -1; + } + + return 0; +} + + +static char * +fakeStorageVolGetPath(virStorageVolPtr vol) +{ + return g_strdup_printf("/some/%s/device/%s", vol->key, vol->name); +} + + +static char * +fakeStoragePoolGetXMLDesc(virStoragePoolPtr pool, + unsigned int flags_unused G_GNUC_UNUSED) +{ + g_autofree char *xmlpath =3D NULL; + char *xmlbuf =3D NULL; + + if (STREQ(pool->name, "inactive")) { + virReportError(VIR_ERR_NO_STORAGE_POOL, NULL); + return NULL; + } + + xmlpath =3D g_strdup_printf("%s/%s%s.xml", abs_srcdir, STORAGE_POOL_XM= L_PATH, + pool->name); + + if (virTestLoadFile(xmlpath, &xmlbuf) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "failed to load XML file '%s'", + xmlpath); + return NULL; + } + + return xmlbuf; +} + + +static int +fakeStoragePoolIsActive(virStoragePoolPtr pool) +{ + if (STREQ(pool->name, "inactive")) + return 0; + + return 1; +} + + +/* Test storage pool implementation + * + * These functions aid testing of storage pool related stuff when creating= a + * qemu command line. + * + * There are a few "magic" values to pass to these functions: + * + * 1) "inactive" as a pool name to create an inactive pool. All other name= s are + * interpreted as file names in storagepoolxml2xmlout/ and are used as the + * definition for the pool. If the file doesn't exist the pool doesn't exi= st. + * + * 2) "nonexistent" returns an error while looking up a volume. Otherwise + * pattern VOLUME_TYPE+VOLUME_PATH can be used to simulate a volume in a p= ool. + * This creates a fake path for this volume. If the '+' sign is omitted, b= lock + * type is assumed. + */ +static virStorageDriver fakeStorageDriver =3D { + .storagePoolLookupByName =3D fakeStoragePoolLookupByName, + .storageVolLookupByName =3D fakeStorageVolLookupByName, + .storagePoolGetXMLDesc =3D fakeStoragePoolGetXMLDesc, + .storageVolGetPath =3D fakeStorageVolGetPath, + .storageVolGetInfo =3D fakeStorageVolGetInfo, + .storagePoolIsActive =3D fakeStoragePoolIsActive, +}; + + +virStorageDriver * +testQemuGetFakeStorageDriver(void) +{ + return &fakeStorageDriver; +} + + +/* virNetDevOpenvswitchGetVhostuserIfname mocks a portdev name - handle th= at */ +static virNWFilterBindingPtr +fakeNWFilterBindingLookupByPortDev(virConnectPtr conn, + const char *portdev) +{ + if (STREQ(portdev, "vhost-user0")) + return virGetNWFilterBinding(conn, "fake_vnet0", "fakeFilterName"); + + virReportError(VIR_ERR_NO_NWFILTER_BINDING, + "no nwfilter binding for port dev '%s'", portdev); + return NULL; +} + + +static int +fakeNWFilterBindingDelete(virNWFilterBindingPtr binding G_GNUC_UNUSED) +{ + return 0; +} + + +static virNWFilterDriver fakeNWFilterDriver =3D { + .nwfilterBindingLookupByPortDev =3D fakeNWFilterBindingLookupByPortDev, + .nwfilterBindingDelete =3D fakeNWFilterBindingDelete, +}; + + +virNWFilterDriver * +testQemuGetFakeNWFilterDriver(void) +{ + return &fakeNWFilterDriver; +} + + +/* name of the fake network shall be constructed as: + * NETWORKXMLNAME;NETWORKPORTXMLNAME + * where: + * NETWORKXMLNAME resolves to abs_srcdir/networkxmlconfdata/NETWORKXMLNAM= E.xml + * NETWORKPORTXMLNAME resolves to abs_srcdir/virnetworkportxml2xmldata/NE= TWORKPORTXMLNAME.xml + */ +static virNetworkPtr +fakeNetworkLookupByName(virConnectPtr conn, + const char *name) +{ + unsigned char uuid[VIR_UUID_BUFLEN]; + g_autofree char *netname =3D g_strdup(name); + g_autofree char *path =3D NULL; + char *tmp; + + memset(uuid, 0, VIR_UUID_BUFLEN); + + if ((tmp =3D strchr(netname, ';'))) { + *tmp =3D '\0'; + } else { + virReportError(VIR_ERR_NO_NETWORK, + "Malformed fake network name '%s'. See fakeNetworkL= ookupByName.", + name); + return NULL; + } + + path =3D g_strdup_printf(abs_srcdir "/networkxmlconfdata/%s.xml", netn= ame); + + if (!virFileExists(path)) { + virReportError(VIR_ERR_NO_NETWORK, "fake network '%s' not found", = path); + return NULL; + } + + return virGetNetwork(conn, name, uuid); +} + + +static char * +fakeNetworkGetXMLDesc(virNetworkPtr network, + unsigned int noflags G_GNUC_UNUSED) +{ + g_autofree char *netname =3D g_strdup(network->name); + g_autofree char *path =3D NULL; + char *xml =3D NULL; + + *(strchr(netname, ';')) =3D '\0'; + + path =3D g_strdup_printf(abs_srcdir "/networkxmlconfdata/%s.xml", netn= ame); + + if (virFileReadAll(path, 4 * 1024, &xml) < 0) + return NULL; + + return xml; +} + + +static virNetworkPortPtr +fakeNetworkPortCreateXML(virNetworkPtr net, + const char *xmldesc G_GNUC_UNUSED, + unsigned int noflags G_GNUC_UNUSED) +{ + unsigned char uuid[VIR_UUID_BUFLEN]; + g_autofree char *portname =3D g_strdup(strchr(net->name, ';') + 1); + g_autofree char *path =3D g_strdup_printf(abs_srcdir "/virnetworkportx= ml2xmldata/%s.xml", portname); + + memset(uuid, 0, VIR_UUID_BUFLEN); + + if (!virFileExists(path)) { + virReportError(VIR_ERR_NO_NETWORK_PORT, "fake network port '%s' no= t found", path); + return NULL; + } + + return virGetNetworkPort(net, uuid); +} + + +static char * +fakeNetworkPortGetXMLDesc(virNetworkPortPtr port, + unsigned int noflags G_GNUC_UNUSED) +{ + g_autofree char *portname =3D g_strdup(strchr(port->net->name, ';') + = 1); + g_autofree char *path =3D g_strdup_printf(abs_srcdir "/virnetworkportx= ml2xmldata/%s.xml", portname); + char *xml =3D NULL; + + if (virFileReadAll(path, 4 * 1024, &xml) < 0) + return NULL; + + return xml; +} + + +static virNetworkDriver fakeNetworkDriver =3D { + .networkLookupByName =3D fakeNetworkLookupByName, + .networkGetXMLDesc =3D fakeNetworkGetXMLDesc, + .networkPortCreateXML =3D fakeNetworkPortCreateXML, + .networkPortGetXMLDesc =3D fakeNetworkPortGetXMLDesc, +}; + + +virNetworkDriver * +testQemuGetFakeNetworkDriver(void) +{ + return &fakeNetworkDriver; +} diff --git a/tests/qemufakedrivers.h b/tests/qemufakedrivers.h new file mode 100644 index 0000000000..172ae48451 --- /dev/null +++ b/tests/qemufakedrivers.h @@ -0,0 +1,19 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#pragma once + +#include "driver.h" + +virSecretDriver * +testQemuGetFakeSecretDriver(void); + +virStorageDriver * +testQemuGetFakeStorageDriver(void); + +virNWFilterDriver * +testQemuGetFakeNWFilterDriver(void); + +virNetworkDriver * +testQemuGetFakeNetworkDriver(void); diff --git a/tests/qemuxmlconftest.c b/tests/qemuxmlconftest.c index cf147e70d4..32eb42dd19 100644 --- a/tests/qemuxmlconftest.c +++ b/tests/qemuxmlconftest.c @@ -23,6 +23,7 @@ #include "virfilewrapper.h" #include "configmake.h" #include "testutilsqemuschema.h" +#include "qemufakedrivers.h" =20 #define LIBVIRT_QEMU_CAPSPRIV_H_ALLOW #include "qemu/qemu_capspriv.h" @@ -33,320 +34,6 @@ =20 static virQEMUDriver driver; =20 -static unsigned char * -fakeSecretGetValue(virSecretPtr obj G_GNUC_UNUSED, - size_t *value_size, - unsigned int fakeflags G_GNUC_UNUSED) -{ - char *secret; - secret =3D g_strdup("AQCVn5hO6HzFAhAAq0NCv8jtJcIcE+HOBlMQ1A"); - *value_size =3D strlen(secret); - return (unsigned char *) secret; -} - -static virSecretPtr -fakeSecretLookupByUsage(virConnectPtr conn, - int usageType, - const char *usageID) -{ - unsigned char uuid[VIR_UUID_BUFLEN]; - if (usageType =3D=3D VIR_SECRET_USAGE_TYPE_VOLUME) { - if (!STRPREFIX(usageID, "/storage/guest_disks/")) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "test provided invalid volume storage prefix '%= s'", - usageID); - return NULL; - } - } else if (STRNEQ(usageID, "mycluster_myname") && - STRNEQ(usageID, "client.admin secret")) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "test provided incorrect usage '%s'", usageID); - return NULL; - } - - if (virUUIDGenerate(uuid) < 0) - return NULL; - - return virGetSecret(conn, uuid, usageType, usageID); -} - -static virSecretPtr -fakeSecretLookupByUUID(virConnectPtr conn, - const unsigned char *uuid) -{ - /* NB: This mocked value could be "tls" or "volume" depending on - * which test is being run, we'll leave at NONE (or 0) */ - return virGetSecret(conn, uuid, VIR_SECRET_USAGE_TYPE_NONE, ""); -} - -static virSecretDriver fakeSecretDriver =3D { - .connectNumOfSecrets =3D NULL, - .connectListSecrets =3D NULL, - .secretLookupByUUID =3D fakeSecretLookupByUUID, - .secretLookupByUsage =3D fakeSecretLookupByUsage, - .secretDefineXML =3D NULL, - .secretGetXMLDesc =3D NULL, - .secretSetValue =3D NULL, - .secretGetValue =3D fakeSecretGetValue, - .secretUndefine =3D NULL, -}; - - -#define STORAGE_POOL_XML_PATH "storagepoolxml2xmlout/" -static const unsigned char fakeUUID[VIR_UUID_BUFLEN] =3D "fakeuuid"; - -static virStoragePoolPtr -fakeStoragePoolLookupByName(virConnectPtr conn, - const char *name) -{ - g_autofree char *xmlpath =3D NULL; - - if (STRNEQ(name, "inactive")) { - xmlpath =3D g_strdup_printf("%s/%s%s.xml", abs_srcdir, - STORAGE_POOL_XML_PATH, name); - - if (!virFileExists(xmlpath)) { - virReportError(VIR_ERR_NO_STORAGE_POOL, - "File '%s' not found", xmlpath); - return NULL; - } - } - - return virGetStoragePool(conn, name, fakeUUID, NULL, NULL); -} - - -static virStorageVolPtr -fakeStorageVolLookupByName(virStoragePoolPtr pool, - const char *name) -{ - g_auto(GStrv) volinfo =3D NULL; - - if (STREQ(pool->name, "inactive")) { - virReportError(VIR_ERR_OPERATION_INVALID, - "storage pool '%s' is not active", pool->name); - return NULL; - } - - if (STREQ(name, "nonexistent")) { - virReportError(VIR_ERR_NO_STORAGE_VOL, - "no storage vol with matching name '%s'", name); - return NULL; - } - - if (!(volinfo =3D g_strsplit(name, "+", 2))) - return NULL; - - if (!volinfo[1]) { - return virGetStorageVol(pool->conn, pool->name, name, "block", NUL= L, NULL); - } - - return virGetStorageVol(pool->conn, pool->name, volinfo[1], volinfo[0], - NULL, NULL); -} - -static int -fakeStorageVolGetInfo(virStorageVolPtr vol, - virStorageVolInfoPtr info) -{ - memset(info, 0, sizeof(*info)); - - info->type =3D virStorageVolTypeFromString(vol->key); - - if (info->type < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "Invalid volume type '%s'", vol->key); - return -1; - } - - return 0; -} - - -static char * -fakeStorageVolGetPath(virStorageVolPtr vol) -{ - return g_strdup_printf("/some/%s/device/%s", vol->key, vol->name); -} - - -static char * -fakeStoragePoolGetXMLDesc(virStoragePoolPtr pool, - unsigned int flags_unused G_GNUC_UNUSED) -{ - g_autofree char *xmlpath =3D NULL; - char *xmlbuf =3D NULL; - - if (STREQ(pool->name, "inactive")) { - virReportError(VIR_ERR_NO_STORAGE_POOL, NULL); - return NULL; - } - - xmlpath =3D g_strdup_printf("%s/%s%s.xml", abs_srcdir, STORAGE_POOL_XM= L_PATH, - pool->name); - - if (virTestLoadFile(xmlpath, &xmlbuf) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "failed to load XML file '%s'", - xmlpath); - return NULL; - } - - return xmlbuf; -} - -static int -fakeStoragePoolIsActive(virStoragePoolPtr pool) -{ - if (STREQ(pool->name, "inactive")) - return 0; - - return 1; -} - -/* Test storage pool implementation - * - * These functions aid testing of storage pool related stuff when creating= a - * qemu command line. - * - * There are a few "magic" values to pass to these functions: - * - * 1) "inactive" as a pool name to create an inactive pool. All other name= s are - * interpreted as file names in storagepoolxml2xmlout/ and are used as the - * definition for the pool. If the file doesn't exist the pool doesn't exi= st. - * - * 2) "nonexistent" returns an error while looking up a volume. Otherwise - * pattern VOLUME_TYPE+VOLUME_PATH can be used to simulate a volume in a p= ool. - * This creates a fake path for this volume. If the '+' sign is omitted, b= lock - * type is assumed. - */ -static virStorageDriver fakeStorageDriver =3D { - .storagePoolLookupByName =3D fakeStoragePoolLookupByName, - .storageVolLookupByName =3D fakeStorageVolLookupByName, - .storagePoolGetXMLDesc =3D fakeStoragePoolGetXMLDesc, - .storageVolGetPath =3D fakeStorageVolGetPath, - .storageVolGetInfo =3D fakeStorageVolGetInfo, - .storagePoolIsActive =3D fakeStoragePoolIsActive, -}; - - -/* virNetDevOpenvswitchGetVhostuserIfname mocks a portdev name - handle th= at */ -static virNWFilterBindingPtr -fakeNWFilterBindingLookupByPortDev(virConnectPtr conn, - const char *portdev) -{ - if (STREQ(portdev, "vhost-user0")) - return virGetNWFilterBinding(conn, "fake_vnet0", "fakeFilterName"); - - virReportError(VIR_ERR_NO_NWFILTER_BINDING, - "no nwfilter binding for port dev '%s'", portdev); - return NULL; -} - - -static int -fakeNWFilterBindingDelete(virNWFilterBindingPtr binding G_GNUC_UNUSED) -{ - return 0; -} - - -static virNWFilterDriver fakeNWFilterDriver =3D { - .nwfilterBindingLookupByPortDev =3D fakeNWFilterBindingLookupByPortDev, - .nwfilterBindingDelete =3D fakeNWFilterBindingDelete, -}; - - -/* name of the fake network shall be constructed as: - * NETWORKXMLNAME;NETWORKPORTXMLNAME - * where: - * NETWORKXMLNAME resolves to abs_srcdir/networkxmlconfdata/NETWORKXMLNAM= E.xml - * NETWORKPORTXMLNAME resolves to abs_srcdir/virnetworkportxml2xmldata/NE= TWORKPORTXMLNAME.xml - */ -static virNetworkPtr -fakeNetworkLookupByName(virConnectPtr conn, - const char *name) -{ - unsigned char uuid[VIR_UUID_BUFLEN]; - g_autofree char *netname =3D g_strdup(name); - g_autofree char *path =3D NULL; - char *tmp; - - memset(uuid, 0, VIR_UUID_BUFLEN); - - if ((tmp =3D strchr(netname, ';'))) { - *tmp =3D '\0'; - } else { - virReportError(VIR_ERR_NO_NETWORK, - "Malformed fake network name '%s'. See fakeNetworkL= ookupByName.", - name); - return NULL; - } - - path =3D g_strdup_printf(abs_srcdir "/networkxmlconfdata/%s.xml", netn= ame); - - if (!virFileExists(path)) { - virReportError(VIR_ERR_NO_NETWORK, "fake network '%s' not found", = path); - return NULL; - } - - return virGetNetwork(conn, name, uuid); -} - - -static char * -fakeNetworkGetXMLDesc(virNetworkPtr network, - unsigned int noflags G_GNUC_UNUSED) -{ - g_autofree char *netname =3D g_strdup(network->name); - g_autofree char *path =3D NULL; - char *xml =3D NULL; - - *(strchr(netname, ';')) =3D '\0'; - - path =3D g_strdup_printf(abs_srcdir "/networkxmlconfdata/%s.xml", netn= ame); - - if (virFileReadAll(path, 4 * 1024, &xml) < 0) - return NULL; - - return xml; -} - - -static virNetworkPortPtr -fakeNetworkPortCreateXML(virNetworkPtr net, - const char *xmldesc G_GNUC_UNUSED, - unsigned int noflags G_GNUC_UNUSED) -{ - unsigned char uuid[VIR_UUID_BUFLEN]; - g_autofree char *portname =3D g_strdup(strchr(net->name, ';') + 1); - g_autofree char *path =3D g_strdup_printf(abs_srcdir "/virnetworkportx= ml2xmldata/%s.xml", portname); - - memset(uuid, 0, VIR_UUID_BUFLEN); - - if (!virFileExists(path)) { - virReportError(VIR_ERR_NO_NETWORK_PORT, "fake network port '%s' no= t found", path); - return NULL; - } - - return virGetNetworkPort(net, uuid); -} - - -static char * -fakeNetworkPortGetXMLDesc(virNetworkPortPtr port, - unsigned int noflags G_GNUC_UNUSED) -{ - g_autofree char *portname =3D g_strdup(strchr(port->net->name, ';') + = 1); - g_autofree char *path =3D g_strdup_printf(abs_srcdir "/virnetworkportx= ml2xmldata/%s.xml", portname); - char *xml =3D NULL; - - if (virFileReadAll(path, 4 * 1024, &xml) < 0) - return NULL; - - return xml; -} - =20 static void testQemuPrepareHostdevPCI(virDomainHostdevDef *hostdev) @@ -418,14 +105,6 @@ testQemuPrepareHostdev(virDomainObj *vm) } =20 =20 -static virNetworkDriver fakeNetworkDriver =3D { - .networkLookupByName =3D fakeNetworkLookupByName, - .networkGetXMLDesc =3D fakeNetworkGetXMLDesc, - .networkPortCreateXML =3D fakeNetworkPortCreateXML, - .networkPortGetXMLDesc =3D fakeNetworkPortGetXMLDesc, -}; - - static void testUpdateQEMUCapsHostCPUModel(virQEMUCaps *qemuCaps, virArch hostArch) { @@ -1273,10 +952,10 @@ mymain(void) if (!(conn =3D virGetConnect())) return EXIT_FAILURE; =20 - conn->secretDriver =3D &fakeSecretDriver; - conn->storageDriver =3D &fakeStorageDriver; - conn->nwfilterDriver =3D &fakeNWFilterDriver; - conn->networkDriver =3D &fakeNetworkDriver; + conn->secretDriver =3D testQemuGetFakeSecretDriver(); + conn->storageDriver =3D testQemuGetFakeStorageDriver(); + conn->nwfilterDriver =3D testQemuGetFakeNWFilterDriver(); + conn->networkDriver =3D testQemuGetFakeNetworkDriver(); =20 virSetConnectInterface(conn); virSetConnectNetwork(conn); --=20 2.52.0