test_driver.c 197.5 KB
Newer Older
1
/*
2
 * test_driver.c: A "mock" hypervisor for use by application unit tests
3
 *
4
 * Copyright (C) 2006-2015 Red Hat, Inc.
5
 * Copyright (C) 2006 Daniel P. Berrange
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23
 *
 * Daniel Berrange <berrange@redhat.com>
 */

24
#include <config.h>
25

26 27 28
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
29 30
#include <fcntl.h>
#include <unistd.h>
31
#include <sys/stat.h>
C
Cole Robinson 已提交
32
#include <libxml/xmlsave.h>
33
#include <libxml/xpathInternals.h>
34

35

36
#include "virerror.h"
37
#include "datatypes.h"
38
#include "test_driver.h"
39
#include "virbuffer.h"
40
#include "viruuid.h"
41
#include "capabilities.h"
42
#include "configmake.h"
43
#include "viralloc.h"
J
John Ferlan 已提交
44
#include "virnetworkobj.h"
L
Laine Stump 已提交
45
#include "interface_conf.h"
46
#include "domain_conf.h"
47
#include "domain_event.h"
48
#include "network_event.h"
49
#include "snapshot_conf.h"
50
#include "virfdstream.h"
C
Cole Robinson 已提交
51
#include "storage_conf.h"
J
John Ferlan 已提交
52
#include "virstorageobj.h"
53
#include "storage_event.h"
54
#include "node_device_conf.h"
J
John Ferlan 已提交
55
#include "virnodedeviceobj.h"
56
#include "node_device_event.h"
57
#include "virxml.h"
58
#include "virthread.h"
59
#include "virlog.h"
E
Eric Blake 已提交
60
#include "virfile.h"
61
#include "virtypedparam.h"
62
#include "virrandom.h"
63
#include "virstring.h"
64
#include "cpu/cpu.h"
65
#include "virauth.h"
66
#include "viratomic.h"
67
#include "virdomainobjlist.h"
J
John Ferlan 已提交
68
#include "virinterfaceobj.h"
69
#include "virhostcpu.h"
70

71 72
#define VIR_FROM_THIS VIR_FROM_TEST

73 74
VIR_LOG_INIT("test.test_driver");

75

76 77 78 79
#define MAX_CPUS 128

struct _testCell {
    unsigned long mem;
80
    unsigned long freeMem;
81
    int numCpus;
82
    virCapsHostNUMACellCPU cpus[MAX_CPUS];
83 84 85 86 87
};
typedef struct _testCell testCell;
typedef struct _testCell *testCellPtr;

#define MAX_CELLS 128
88

89 90 91 92 93 94 95
struct _testAuth {
    char *username;
    char *password;
};
typedef struct _testAuth testAuth;
typedef struct _testAuth *testAuthPtr;

96
struct _testDriver {
97
    virMutex lock;
98

99
    virNodeInfo nodeInfo;
100
    virInterfaceObjListPtr ifaces;
101
    bool transaction_running;
102
    virInterfaceObjListPtr backupIfaces;
103
    virStoragePoolObjListPtr pools;
104
    virNodeDeviceObjListPtr devs;
105 106
    int numCells;
    testCell cells[MAX_CELLS];
107 108
    size_t numAuths;
    testAuthPtr auths;
109

110 111 112
    /* virAtomic access only */
    volatile int nextDomID;

113 114 115 116 117 118 119 120 121 122
    /* immutable pointer, immutable object after being initialized with
     * testBuildCapabilities */
    virCapsPtr caps;

    /* immutable pointer, immutable object */
    virDomainXMLOptionPtr xmlopt;

    /* immutable pointer, self-locking APIs */
    virDomainObjListPtr domains;
    virNetworkObjListPtr networks;
123
    virObjectEventStatePtr eventState;
124
};
125 126
typedef struct _testDriver testDriver;
typedef testDriver *testDriverPtr;
127

128
static testDriverPtr defaultPrivconn;
129
static int defaultConnections;
130
static virMutex defaultLock = VIR_MUTEX_INITIALIZER;
131

132
#define TEST_MODEL "i686"
133
#define TEST_EMULATOR "/usr/bin/test-hv"
134

135
static const virNodeInfo defaultNodeInfo = {
136
    TEST_MODEL,
137 138 139 140 141 142 143
    1024*1024*3, /* 3 GB */
    16,
    1400,
    2,
    2,
    2,
    2,
144 145
};

146 147 148 149 150 151 152 153 154
static void
testDriverFree(testDriverPtr driver)
{
    if (!driver)
        return;

    virObjectUnref(driver->caps);
    virObjectUnref(driver->xmlopt);
    virObjectUnref(driver->domains);
155
    virNodeDeviceObjListFree(driver->devs);
156
    virObjectUnref(driver->networks);
157
    virObjectUnref(driver->ifaces);
158
    virObjectUnref(driver->pools);
159
    virObjectUnref(driver->eventState);
160 161 162 163 164
    virMutexUnlock(&driver->lock);
    virMutexDestroy(&driver->lock);

    VIR_FREE(driver);
}
165

166

167
static void testDriverLock(testDriverPtr driver)
168
{
169
    virMutexLock(&driver->lock);
170 171
}

172
static void testDriverUnlock(testDriverPtr driver)
173
{
174
    virMutexUnlock(&driver->lock);
175 176
}

177 178 179 180 181 182 183 184 185
static void testObjectEventQueue(testDriverPtr driver,
                                 virObjectEventPtr event)
{
    if (!event)
        return;

    virObjectEventStateQueue(driver->eventState, event);
}

186 187 188 189 190 191
#define TEST_NAMESPACE_HREF "http://libvirt.org/schemas/domain/test/1.0"

typedef struct _testDomainNamespaceDef testDomainNamespaceDef;
typedef testDomainNamespaceDef *testDomainNamespaceDefPtr;
struct _testDomainNamespaceDef {
    int runstate;
192
    bool transient;
C
Cole Robinson 已提交
193
    bool hasManagedSave;
194 195 196

    unsigned int num_snap_nodes;
    xmlNodePtr *snap_nodes;
197 198 199 200 201 202
};

static void
testDomainDefNamespaceFree(void *data)
{
    testDomainNamespaceDefPtr nsdata = data;
203 204 205 206 207 208 209 210 211
    size_t i;

    if (!nsdata)
        return;

    for (i = 0; i < nsdata->num_snap_nodes; i++)
        xmlFreeNode(nsdata->snap_nodes[i]);

    VIR_FREE(nsdata->snap_nodes);
212 213 214 215 216 217 218 219 220 221
    VIR_FREE(nsdata);
}

static int
testDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
                            xmlNodePtr root ATTRIBUTE_UNUSED,
                            xmlXPathContextPtr ctxt,
                            void **data)
{
    testDomainNamespaceDefPtr nsdata = NULL;
222 223 224
    xmlNodePtr *nodes = NULL;
    int tmp, n;
    size_t i;
225 226 227 228 229 230 231 232 233 234 235 236 237
    unsigned int tmpuint;

    if (xmlXPathRegisterNs(ctxt, BAD_CAST "test",
                           BAD_CAST TEST_NAMESPACE_HREF) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to register xml namespace '%s'"),
                       TEST_NAMESPACE_HREF);
        return -1;
    }

    if (VIR_ALLOC(nsdata) < 0)
        return -1;

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    n = virXPathNodeSet("./test:domainsnapshot", ctxt, &nodes);
    if (n < 0)
        goto error;

    if (n && VIR_ALLOC_N(nsdata->snap_nodes, n) < 0)
        goto error;

    for (i = 0; i < n; i++) {
        xmlNodePtr newnode = xmlCopyNode(nodes[i], 1);
        if (!newnode) {
            virReportOOMError();
            goto error;
        }

        nsdata->snap_nodes[nsdata->num_snap_nodes] = newnode;
        nsdata->num_snap_nodes++;
    }
    VIR_FREE(nodes);

257 258 259 260 261 262 263
    tmp = virXPathBoolean("boolean(./test:transient)", ctxt);
    if (tmp == -1) {
        virReportError(VIR_ERR_XML_ERROR, "%s", _("invalid transient"));
        goto error;
    }
    nsdata->transient = tmp;

C
Cole Robinson 已提交
264 265 266 267 268 269 270
    tmp = virXPathBoolean("boolean(./test:hasmanagedsave)", ctxt);
    if (tmp == -1) {
        virReportError(VIR_ERR_XML_ERROR, "%s", _("invalid hasmanagedsave"));
        goto error;
    }
    nsdata->hasManagedSave = tmp;

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    tmp = virXPathUInt("string(./test:runstate)", ctxt, &tmpuint);
    if (tmp == 0) {
        if (tmpuint >= VIR_DOMAIN_LAST) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("runstate '%d' out of range'"), tmpuint);
            goto error;
        }
        nsdata->runstate = tmpuint;
    } else if (tmp == -1) {
        nsdata->runstate = VIR_DOMAIN_RUNNING;
    } else if (tmp == -2) {
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid runstate"));
        goto error;
    }

287 288 289 290 291
    if (nsdata->transient && nsdata->runstate == VIR_DOMAIN_SHUTOFF) {
        virReportError(VIR_ERR_XML_ERROR, "%s",
            _("transient domain cannot have runstate 'shutoff'"));
        goto error;
    }
C
Cole Robinson 已提交
292 293 294 295 296
    if (nsdata->hasManagedSave && nsdata->runstate != VIR_DOMAIN_SHUTOFF) {
        virReportError(VIR_ERR_XML_ERROR, "%s",
            _("domain with managedsave data can only have runstate 'shutoff'"));
        goto error;
    }
297

298 299 300
    *data = nsdata;
    return 0;

301
 error:
302
    VIR_FREE(nodes);
303 304 305
    testDomainDefNamespaceFree(nsdata);
    return -1;
}
306

307
static virCapsPtr
308 309
testBuildCapabilities(virConnectPtr conn)
{
310
    testDriverPtr privconn = conn->privateData;
311 312
    virCapsPtr caps;
    virCapsGuestPtr guest;
313 314
    int guest_types[] = { VIR_DOMAIN_OSTYPE_HVM,
                          VIR_DOMAIN_OSTYPE_XEN };
315
    size_t i, j;
316

317
    if ((caps = virCapabilitiesNew(VIR_ARCH_I686, false, false)) == NULL)
318
        goto error;
319

320
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
321
        goto error;
322
    if (virCapabilitiesAddHostFeature(caps, "nonpae") < 0)
323
        goto error;
324

325 326 327 328 329 330
    if (VIR_ALLOC_N(caps->host.pagesSize, 2) < 0)
        goto error;

    caps->host.pagesSize[caps->host.nPagesSize++] = 4;
    caps->host.pagesSize[caps->host.nPagesSize++] = 2048;

331
    for (i = 0; i < privconn->numCells; i++) {
332
        virCapsHostNUMACellCPUPtr cpu_cells;
333 334
        virCapsHostNUMACellPageInfoPtr pages;
        size_t nPages;
335

336 337 338 339 340 341 342
        if (VIR_ALLOC_N(cpu_cells, privconn->cells[i].numCpus) < 0 ||
            VIR_ALLOC_N(pages, caps->host.nPagesSize) < 0) {
                VIR_FREE(cpu_cells);
                goto error;
            }

        nPages = caps->host.nPagesSize;
343 344 345 346

        memcpy(cpu_cells, privconn->cells[i].cpus,
               sizeof(*cpu_cells) * privconn->cells[i].numCpus);

347 348 349 350
        for (j = 0; j < nPages; j++)
            pages[j].size = caps->host.pagesSize[j];

        pages[0].avail = privconn->cells[i].mem / pages[0].size;
351

352
        if (virCapabilitiesAddHostNUMACell(caps, i, privconn->cells[i].mem,
353
                                           privconn->cells[i].numCpus,
354
                                           cpu_cells, 0, NULL, nPages, pages) < 0)
355
            goto error;
356 357
    }

358
    for (i = 0; i < ARRAY_CARDINALITY(guest_types); i++) {
359 360
        if ((guest = virCapabilitiesAddGuest(caps,
                                             guest_types[i],
361
                                             VIR_ARCH_I686,
362 363 364 365
                                             TEST_EMULATOR,
                                             NULL,
                                             0,
                                             NULL)) == NULL)
366
            goto error;
367

368
        if (virCapabilitiesAddGuestDomain(guest,
369
                                          VIR_DOMAIN_VIRT_TEST,
370 371 372 373
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
374
            goto error;
375

376
        if (virCapabilitiesAddGuestFeature(guest, "pae", true, true) == NULL)
377
            goto error;
378
        if (virCapabilitiesAddGuestFeature(guest, "nonpae", true, true) == NULL)
379
            goto error;
380 381
    }

382 383
    caps->host.nsecModels = 1;
    if (VIR_ALLOC_N(caps->host.secModels, caps->host.nsecModels) < 0)
384
        goto error;
385 386
    if (VIR_STRDUP(caps->host.secModels[0].model, "testSecurity") < 0)
        goto error;
387

388 389
    if (VIR_STRDUP(caps->host.secModels[0].doi, "") < 0)
        goto error;
390

391
    return caps;
392

393
 error:
394
    virObjectUnref(caps);
395
    return NULL;
396 397
}

398

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
static testDriverPtr
testDriverNew(void)
{
    virDomainXMLNamespace ns = {
        .parse = testDomainDefNamespaceParse,
        .free = testDomainDefNamespaceFree,
    };
    testDriverPtr ret;

    if (VIR_ALLOC(ret) < 0)
        return NULL;

    if (virMutexInit(&ret->lock) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
        goto error;
    }

417
    if (!(ret->xmlopt = virDomainXMLOptionNew(NULL, NULL, &ns, NULL, NULL)) ||
418
        !(ret->eventState = virObjectEventStateNew()) ||
419
        !(ret->ifaces = virInterfaceObjListNew()) ||
420
        !(ret->domains = virDomainObjListNew()) ||
421
        !(ret->networks = virNetworkObjListNew()) ||
422 423
        !(ret->devs = virNodeDeviceObjListNew()) ||
        !(ret->pools = virStoragePoolObjListNew()))
424 425
        goto error;

426
    virAtomicIntSet(&ret->nextDomID, 1);
427 428 429 430 431 432 433 434 435

    return ret;

 error:
    testDriverFree(ret);
    return NULL;
}


436 437
static const char *defaultConnXML =
"<node>"
438 439
"<domain type='test'>"
"  <name>test</name>"
440
"  <uuid>6695eb01-f6a4-8304-79aa-97f2502e193f</uuid>"
441 442 443 444 445 446
"  <memory>8388608</memory>"
"  <currentMemory>2097152</currentMemory>"
"  <vcpu>2</vcpu>"
"  <os>"
"    <type>hvm</type>"
"  </os>"
447 448
"</domain>"
""
449 450
"<network>"
"  <name>default</name>"
451
"  <uuid>dd8fe884-6c02-601e-7551-cca97df1c5df</uuid>"
452
"  <bridge name='virbr0'/>"
453 454 455
"  <forward/>"
"  <ip address='192.168.122.1' netmask='255.255.255.0'>"
"    <dhcp>"
456
"      <range start='192.168.122.2' end='192.168.122.254'/>"
457 458
"    </dhcp>"
"  </ip>"
459 460
"</network>"
""
L
Laine Stump 已提交
461 462 463 464 465 466 467 468
"<interface type=\"ethernet\" name=\"eth1\">"
"  <start mode=\"onboot\"/>"
"  <mac address=\"aa:bb:cc:dd:ee:ff\"/>"
"  <mtu size=\"1492\"/>"
"  <protocol family=\"ipv4\">"
"    <ip address=\"192.168.0.5\" prefix=\"24\"/>"
"    <route gateway=\"192.168.0.1\"/>"
"  </protocol>"
469 470
"</interface>"
""
C
Cole Robinson 已提交
471 472
"<pool type='dir'>"
"  <name>default-pool</name>"
473
"  <uuid>dfe224cb-28fb-8dd0-c4b2-64eb3f0f4566</uuid>"
C
Cole Robinson 已提交
474 475 476
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
"</pool>"
""
"<device>"
"  <name>computer</name>"
"  <capability type='system'>"
"    <hardware>"
"      <vendor>Libvirt</vendor>"
"      <version>Test driver</version>"
"      <serial>123456</serial>"
"      <uuid>11111111-2222-3333-4444-555555555555</uuid>"
"    </hardware>"
"    <firmware>"
"      <vendor>Libvirt</vendor>"
"      <version>Test Driver</version>"
"      <release_date>01/22/2007</release_date>"
"    </firmware>"
"  </capability>"
"</device>"
495
"<device>"
496
"  <name>scsi_host1</name>"
497 498 499
"  <parent>computer</parent>"
"  <capability type='scsi_host'>"
"    <host>1</host>"
500
"    <unique_id>0</unique_id>"
501 502 503
"    <capability type='fc_host'>"
"      <wwnn>2000000012341234</wwnn>"
"      <wwpn>1000000012341234</wwpn>"
504 505 506 507
"      <fabric_wwn>2000000043214321</fabric_wwn>"
"    </capability>"
"    <capability type='vport_ops'>"
"      <max_vports>127</max_vports>"
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
"      <vports>1</vports>"
"    </capability>"
"  </capability>"
"</device>"
"<device>"
"  <name>scsi_host2</name>"
"  <parent>computer</parent>"
"  <capability type='scsi_host'>"
"    <host>2</host>"
"    <unique_id>1</unique_id>"
"    <capability type='fc_host'>"
"      <wwnn>2000000056785678</wwnn>"
"      <wwpn>1000000056785678</wwpn>"
"      <fabric_wwn>2000000087658765</fabric_wwn>"
"    </capability>"
"    <capability type='vport_ops'>"
"      <max_vports>127</max_vports>"
525
"      <vports>0</vports>"
526 527 528
"    </capability>"
"  </capability>"
"</device>"
529 530 531 532 533 534 535 536 537 538 539 540 541
"<device>"
"  <name>scsi_host11</name>"
"  <parent>scsi_host1</parent>"
"  <capability type='scsi_host'>"
"    <host>11</host>"
"    <unique_id>10</unique_id>"
"    <capability type='fc_host'>"
"      <wwnn>2000000034563456</wwnn>"
"      <wwpn>1000000034563456</wwpn>"
"      <fabric_wwn>2000000043214321</fabric_wwn>"
"    </capability>"
"  </capability>"
 "</device>"
542 543
"</node>";

C
Cole Robinson 已提交
544

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
static const char *defaultPoolSourcesLogicalXML =
"<sources>\n"
"  <source>\n"
"    <device path='/dev/sda20'/>\n"
"    <name>testvg1</name>\n"
"    <format type='lvm2'/>\n"
"  </source>\n"
"  <source>\n"
"    <device path='/dev/sda21'/>\n"
"    <name>testvg2</name>\n"
"    <format type='lvm2'/>\n"
"  </source>\n"
"</sources>\n";

static const char *defaultPoolSourcesNetFSXML =
"<sources>\n"
"  <source>\n"
"    <host name='%s'/>\n"
"    <dir path='/testshare'/>\n"
"    <format type='nfs'/>\n"
"  </source>\n"
"</sources>\n";

568
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
569
static const unsigned long long defaultPoolAlloc;
C
Cole Robinson 已提交
570

571
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr obj);
572
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
573

574 575 576 577
static virDomainObjPtr
testDomObjFromDomain(virDomainPtr domain)
{
    virDomainObjPtr vm;
578
    testDriverPtr driver = domain->conn->privateData;
579 580
    char uuidstr[VIR_UUID_STRING_BUFLEN];

581
    vm = virDomainObjListFindByUUID(driver->domains, domain->uuid);
582 583 584 585 586 587 588 589 590 591
    if (!vm) {
        virUUIDFormat(domain->uuid, uuidstr);
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s' (%s)"),
                       uuidstr, domain->name);
    }

    return vm;
}

592
static char *
593 594
testDomainGenerateIfname(virDomainDefPtr domdef)
{
595
    int maxif = 1024;
596
    int ifctr;
597 598

    for (ifctr = 0; ifctr < maxif; ++ifctr) {
M
Michal Privoznik 已提交
599
        virDomainNetDefPtr net = NULL;
600 601
        char *ifname;

602
        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0)
603 604 605
            return NULL;

        /* Generate network interface names */
M
Michal Privoznik 已提交
606
        if (!(net = virDomainNetFindByName(domdef, ifname)))
607
            return ifname;
608
        VIR_FREE(ifname);
609 610
    }

611 612
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Exceeded max iface limit %d"), maxif);
613 614 615
    return NULL;
}

616
static int
617
testDomainGenerateIfnames(virDomainDefPtr domdef)
618
{
619
    size_t i = 0;
620 621 622 623 624 625

    for (i = 0; i < domdef->nnets; i++) {
        char *ifname;
        if (domdef->nets[i]->ifname)
            continue;

626
        ifname = testDomainGenerateIfname(domdef);
627
        if (!ifname)
628
            return -1;
629 630 631 632

        domdef->nets[i]->ifname = ifname;
    }

633
    return 0;
634 635
}

636

637 638
static void
testDomainShutdownState(virDomainPtr domain,
J
Jiri Denemark 已提交
639 640
                        virDomainObjPtr privdom,
                        virDomainShutoffReason reason)
641
{
642
    virDomainObjRemoveTransientDef(privdom);
J
Jiri Denemark 已提交
643
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF, reason);
644

645 646 647 648
    if (domain)
        domain->id = -1;
}

649
/* Set up domain runtime state */
650
static int
651
testDomainStartState(testDriverPtr privconn,
J
Jiri Denemark 已提交
652 653
                     virDomainObjPtr dom,
                     virDomainRunningReason reason)
654
{
655
    int ret = -1;
656

J
Jiri Denemark 已提交
657
    virDomainObjSetState(dom, VIR_DOMAIN_RUNNING, reason);
658
    dom->def->id = virAtomicIntAdd(&privconn->nextDomID, 1);
659

660
    if (virDomainObjSetDefTransient(privconn->caps,
661
                                    privconn->xmlopt,
662
                                    dom) < 0) {
663 664 665
        goto cleanup;
    }

C
Cole Robinson 已提交
666
    dom->hasManagedSave = false;
667
    ret = 0;
668
 cleanup:
669
    if (ret < 0)
J
Jiri Denemark 已提交
670
        testDomainShutdownState(NULL, dom, VIR_DOMAIN_SHUTOFF_FAILED);
671
    return ret;
672
}
673

674

675
static char *testBuildFilename(const char *relativeTo,
676 677
                               const char *filename)
{
678 679
    char *offset;
    int baseLen;
680 681
    char *ret;

682
    if (!filename || filename[0] == '\0')
683
        return NULL;
684 685 686 687
    if (filename[0] == '/') {
        ignore_value(VIR_STRDUP(ret, filename));
        return ret;
    }
688

689
    offset = strrchr(relativeTo, '/');
690
    if ((baseLen = (offset-relativeTo+1))) {
691
        char *absFile;
C
Chris Lalancette 已提交
692 693
        int totalLen = baseLen + strlen(filename) + 1;
        if (VIR_ALLOC_N(absFile, totalLen) < 0)
694
            return NULL;
C
Chris Lalancette 已提交
695 696 697 698
        if (virStrncpy(absFile, relativeTo, baseLen, totalLen) == NULL) {
            VIR_FREE(absFile);
            return NULL;
        }
699 700 701
        strcat(absFile, filename);
        return absFile;
    } else {
702 703
        ignore_value(VIR_STRDUP(ret, filename));
        return ret;
704
    }
705 706
}

C
Cole Robinson 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
static xmlNodePtr
testParseXMLDocFromFile(xmlNodePtr node, const char *file, const char *type)
{
    xmlNodePtr ret = NULL;
    xmlDocPtr doc = NULL;
    char *absFile = NULL;
    char *relFile = virXMLPropString(node, "file");

    if (relFile != NULL) {
        absFile = testBuildFilename(file, relFile);
        VIR_FREE(relFile);
        if (!absFile) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("resolving %s filename"), type);
            return NULL;
        }

        if (!(doc = virXMLParse(absFile, NULL, type)))
            goto error;

        ret = xmlCopyNode(xmlDocGetRootElement(doc), 1);
        if (!ret) {
            virReportOOMError();
            goto error;
        }
        xmlReplaceNode(node, ret);
        xmlFreeNode(node);
    } else {
        ret = node;
    }

738
 error:
C
Cole Robinson 已提交
739 740 741 742 743
    xmlFreeDoc(doc);
    VIR_FREE(absFile);
    return ret;
}

744 745 746
static int
testParseNodeInfo(virNodeInfoPtr nodeInfo, xmlXPathContextPtr ctxt)
{
747
    char *str;
748 749
    long l;
    int ret;
750

751
    ret = virXPathLong("string(/node/cpu/nodes[1])", ctxt, &l);
752 753 754
    if (ret == 0) {
        nodeInfo->nodes = l;
    } else if (ret == -2) {
755 756
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu nodes value"));
757
        goto error;
758
    }
759

760
    ret = virXPathLong("string(/node/cpu/sockets[1])", ctxt, &l);
761 762 763
    if (ret == 0) {
        nodeInfo->sockets = l;
    } else if (ret == -2) {
764 765
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu sockets value"));
766
        goto error;
767
    }
768

769
    ret = virXPathLong("string(/node/cpu/cores[1])", ctxt, &l);
770 771 772
    if (ret == 0) {
        nodeInfo->cores = l;
    } else if (ret == -2) {
773 774
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu cores value"));
775
        goto error;
776 777
    }

778
    ret = virXPathLong("string(/node/cpu/threads[1])", ctxt, &l);
779 780 781
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
782 783
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu threads value"));
784
        goto error;
785
    }
786

787 788
    nodeInfo->cpus = (nodeInfo->cores * nodeInfo->threads *
                      nodeInfo->sockets * nodeInfo->nodes);
789
    ret = virXPathLong("string(/node/cpu/active[1])", ctxt, &l);
790
    if (ret == 0) {
791
        if (l < nodeInfo->cpus)
792
            nodeInfo->cpus = l;
793
    } else if (ret == -2) {
794 795
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu active value"));
796
        goto error;
797
    }
798
    ret = virXPathLong("string(/node/cpu/mhz[1])", ctxt, &l);
799 800 801
    if (ret == 0) {
        nodeInfo->mhz = l;
    } else if (ret == -2) {
802 803
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu mhz value"));
804
        goto error;
805 806
    }

807
    str = virXPathString("string(/node/cpu/model[1])", ctxt);
808
    if (str != NULL) {
C
Chris Lalancette 已提交
809
        if (virStrcpyStatic(nodeInfo->model, str) == NULL) {
810 811
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Model %s too big for destination"), str);
C
Chris Lalancette 已提交
812 813 814
            VIR_FREE(str);
            goto error;
        }
815
        VIR_FREE(str);
816 817
    }

818
    ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
819 820 821
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
822 823
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node memory value"));
824
        goto error;
825
    }
826

827
    return 0;
828
 error:
829 830 831
    return -1;
}

832
static int
833
testParseDomainSnapshots(testDriverPtr privconn,
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
                         virDomainObjPtr domobj,
                         const char *file,
                         xmlXPathContextPtr ctxt)
{
    size_t i;
    int ret = -1;
    testDomainNamespaceDefPtr nsdata = domobj->def->namespaceData;
    xmlNodePtr *nodes = nsdata->snap_nodes;

    for (i = 0; i < nsdata->num_snap_nodes; i++) {
        virDomainSnapshotObjPtr snap;
        virDomainSnapshotDefPtr def;
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                  "domainsnapshot");
        if (!node)
            goto error;

        def = virDomainSnapshotDefParseNode(ctxt->doc, node,
                                            privconn->caps,
                                            privconn->xmlopt,
                                            VIR_DOMAIN_SNAPSHOT_PARSE_DISKS |
                                            VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL |
                                            VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE);
        if (!def)
            goto error;

        if (!(snap = virDomainSnapshotAssignDef(domobj->snapshots, def))) {
            virDomainSnapshotDefFree(def);
            goto error;
        }

        if (def->current) {
            if (domobj->current_snapshot) {
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("more than one snapshot claims to be active"));
                goto error;
            }

            domobj->current_snapshot = snap;
        }
    }

    if (virDomainSnapshotUpdateRelations(domobj->snapshots) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Snapshots have inconsistent relations for "
                         "domain %s"), domobj->def->name);
        goto error;
    }

    ret = 0;
884
 error:
885 886 887
    return ret;
}

888
static int
889
testParseDomains(testDriverPtr privconn,
C
Cole Robinson 已提交
890 891
                 const char *file,
                 xmlXPathContextPtr ctxt)
892 893 894 895 896 897 898
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virDomainObjPtr obj;

    num = virXPathNodeSet("/node/domain", ctxt, &nodes);
899
    if (num < 0)
900 901
        goto error;

902
    for (i = 0; i < num; i++) {
903
        virDomainDefPtr def;
904
        testDomainNamespaceDefPtr nsdata;
C
Cole Robinson 已提交
905 906 907 908 909
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file, "domain");
        if (!node)
            goto error;

        def = virDomainDefParseNode(ctxt->doc, node,
910
                                    privconn->caps, privconn->xmlopt, NULL,
911
                                    VIR_DOMAIN_DEF_PARSE_INACTIVE);
C
Cole Robinson 已提交
912 913
        if (!def)
            goto error;
914

915
        if (testDomainGenerateIfnames(def) < 0 ||
916
            !(obj = virDomainObjListAdd(privconn->domains,
917
                                        def,
918
                                        privconn->xmlopt,
919
                                        0, NULL))) {
920
            virDomainDefFree(def);
921 922
            goto error;
        }
923

924 925 926 927 928
        if (testParseDomainSnapshots(privconn, obj, file, ctxt) < 0) {
            virObjectUnlock(obj);
            goto error;
        }

929
        nsdata = def->namespaceData;
930
        obj->persistent = !nsdata->transient;
C
Cole Robinson 已提交
931
        obj->hasManagedSave = nsdata->hasManagedSave;
932 933 934 935 936 937 938 939 940

        if (nsdata->runstate != VIR_DOMAIN_SHUTOFF) {
            if (testDomainStartState(privconn, obj,
                                     VIR_DOMAIN_RUNNING_BOOTED) < 0) {
                virObjectUnlock(obj);
                goto error;
            }
        } else {
            testDomainShutdownState(NULL, obj, 0);
941
        }
942
        virDomainObjSetState(obj, nsdata->runstate, 0);
943

944
        virObjectUnlock(obj);
945
    }
946

947
    ret = 0;
948
 error:
949 950 951 952
    VIR_FREE(nodes);
    return ret;
}

953

954
static int
955
testParseNetworks(testDriverPtr privconn,
C
Cole Robinson 已提交
956 957
                  const char *file,
                  xmlXPathContextPtr ctxt)
958 959 960 961 962 963 964
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virNetworkObjPtr obj;

    num = virXPathNodeSet("/node/network", ctxt, &nodes);
965
    if (num < 0)
966
        goto error;
967 968

    for (i = 0; i < num; i++) {
969
        virNetworkDefPtr def;
C
Cole Robinson 已提交
970 971 972
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file, "network");
        if (!node)
            goto error;
973

C
Cole Robinson 已提交
974 975 976
        def = virNetworkDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
977

978
        if (!(obj = virNetworkObjAssignDef(privconn->networks, def, 0))) {
979 980
            virNetworkDefFree(def);
            goto error;
981
        }
982

983
        virNetworkObjSetActive(obj, true);
984
        virNetworkObjEndAPI(&obj);
985
    }
986

987
    ret = 0;
988
 error:
989 990 991 992
    VIR_FREE(nodes);
    return ret;
}

993

994
static int
995
testParseInterfaces(testDriverPtr privconn,
C
Cole Robinson 已提交
996 997
                    const char *file,
                    xmlXPathContextPtr ctxt)
998 999 1000 1001 1002 1003 1004
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virInterfaceObjPtr obj;

    num = virXPathNodeSet("/node/interface", ctxt, &nodes);
1005
    if (num < 0)
L
Laine Stump 已提交
1006
        goto error;
1007 1008

    for (i = 0; i < num; i++) {
L
Laine Stump 已提交
1009
        virInterfaceDefPtr def;
C
Cole Robinson 已提交
1010 1011 1012 1013
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                   "interface");
        if (!node)
            goto error;
L
Laine Stump 已提交
1014

C
Cole Robinson 已提交
1015 1016 1017
        def = virInterfaceDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
1018

1019
        if (!(obj = virInterfaceObjListAssignDef(privconn->ifaces, def))) {
L
Laine Stump 已提交
1020 1021 1022
            virInterfaceDefFree(def);
            goto error;
        }
1023

1024
        virInterfaceObjSetActive(obj, true);
1025
        virInterfaceObjEndAPI(&obj);
1026 1027 1028
    }

    ret = 0;
1029
 error:
1030 1031 1032 1033
    VIR_FREE(nodes);
    return ret;
}

1034

1035
static int
C
Cole Robinson 已提交
1036
testOpenVolumesForPool(const char *file,
1037
                       xmlXPathContextPtr ctxt,
1038 1039
                       virStoragePoolObjPtr obj,
                       int objidx)
1040
{
1041
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(obj);
1042 1043 1044 1045
    char *vol_xpath;
    size_t i;
    int num, ret = -1;
    xmlNodePtr *nodes = NULL;
1046
    virStorageVolDefPtr volDef = NULL;
1047 1048

    /* Find storage volumes */
1049
    if (virAsprintf(&vol_xpath, "/node/pool[%d]/volume", objidx) < 0)
1050 1051 1052 1053
        goto error;

    num = virXPathNodeSet(vol_xpath, ctxt, &nodes);
    VIR_FREE(vol_xpath);
1054
    if (num < 0)
1055 1056 1057
        goto error;

    for (i = 0; i < num; i++) {
C
Cole Robinson 已提交
1058 1059 1060 1061
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                   "volume");
        if (!node)
            goto error;
1062

1063
        if (!(volDef = virStorageVolDefParseNode(def, ctxt->doc, node, 0)))
C
Cole Robinson 已提交
1064
            goto error;
1065

1066 1067
        if (!volDef->target.path) {
            if (virAsprintf(&volDef->target.path, "%s/%s",
1068
                            def->target.path, volDef->name) < 0)
1069 1070 1071
                goto error;
        }

1072
        if (!volDef->key && VIR_STRDUP(volDef->key, volDef->target.path) < 0)
1073
            goto error;
1074

1075
        if (virStoragePoolObjAddVol(obj, volDef) < 0)
1076
            goto error;
1077

1078 1079
        def->allocation += volDef->target.allocation;
        def->available = (def->capacity - def->allocation);
1080
        volDef = NULL;
L
Laine Stump 已提交
1081 1082
    }

1083
    ret = 0;
1084
 error:
1085
    virStorageVolDefFree(volDef);
1086 1087 1088 1089
    VIR_FREE(nodes);
    return ret;
}

1090

1091
static int
1092
testParseStorage(testDriverPtr privconn,
C
Cole Robinson 已提交
1093 1094
                 const char *file,
                 xmlXPathContextPtr ctxt)
1095 1096 1097 1098 1099 1100 1101
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virStoragePoolObjPtr obj;

    num = virXPathNodeSet("/node/pool", ctxt, &nodes);
1102
    if (num < 0)
C
Cole Robinson 已提交
1103
        goto error;
1104 1105

    for (i = 0; i < num; i++) {
C
Cole Robinson 已提交
1106
        virStoragePoolDefPtr def;
C
Cole Robinson 已提交
1107 1108 1109 1110
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                   "pool");
        if (!node)
            goto error;
C
Cole Robinson 已提交
1111

C
Cole Robinson 已提交
1112 1113 1114
        def = virStoragePoolDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
C
Cole Robinson 已提交
1115

1116
        if (!(obj = virStoragePoolObjAssignDef(privconn->pools, def))) {
C
Cole Robinson 已提交
1117 1118 1119 1120
            virStoragePoolDefFree(def);
            goto error;
        }

1121
        if (testStoragePoolObjSetDefaults(obj) == -1) {
1122
            virStoragePoolObjEndAPI(&obj);
C
Cole Robinson 已提交
1123
            goto error;
1124
        }
1125
        virStoragePoolObjSetActive(obj, true);
1126 1127

        /* Find storage volumes */
C
Cole Robinson 已提交
1128
        if (testOpenVolumesForPool(file, ctxt, obj, i+1) < 0) {
1129
            virStoragePoolObjEndAPI(&obj);
1130 1131 1132
            goto error;
        }

1133
        virStoragePoolObjEndAPI(&obj);
C
Cole Robinson 已提交
1134 1135
    }

1136
    ret = 0;
1137
 error:
1138 1139 1140 1141
    VIR_FREE(nodes);
    return ret;
}

1142

1143
static int
1144
testParseNodedevs(testDriverPtr privconn,
C
Cole Robinson 已提交
1145 1146
                  const char *file,
                  xmlXPathContextPtr ctxt)
1147 1148 1149 1150 1151 1152 1153
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virNodeDeviceObjPtr obj;

    num = virXPathNodeSet("/node/device", ctxt, &nodes);
1154
    if (num < 0)
1155
        goto error;
1156 1157

    for (i = 0; i < num; i++) {
1158
        virNodeDeviceDefPtr def;
C
Cole Robinson 已提交
1159 1160 1161 1162
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                  "nodedev");
        if (!node)
            goto error;
1163

C
Cole Robinson 已提交
1164 1165 1166
        def = virNodeDeviceDefParseNode(ctxt->doc, node, 0, NULL);
        if (!def)
            goto error;
1167

1168
        if (!(obj = virNodeDeviceObjListAssignDef(privconn->devs, def))) {
1169 1170 1171
            virNodeDeviceDefFree(def);
            goto error;
        }
1172

1173
        virNodeDeviceObjEndAPI(&obj);
1174 1175 1176
    }

    ret = 0;
1177
 error:
1178 1179 1180 1181
    VIR_FREE(nodes);
    return ret;
}

1182
static int
1183
testParseAuthUsers(testDriverPtr privconn,
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
                   xmlXPathContextPtr ctxt)
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;

    num = virXPathNodeSet("/node/auth/user", ctxt, &nodes);
    if (num < 0)
        goto error;

    privconn->numAuths = num;
    if (num && VIR_ALLOC_N(privconn->auths, num) < 0)
        goto error;

    for (i = 0; i < num; i++) {
        char *username, *password;

        ctxt->node = nodes[i];
        username = virXPathString("string(.)", ctxt);
        if (!username || STREQ(username, "")) {
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("missing username in /node/auth/user field"));
            VIR_FREE(username);
            goto error;
        }
        /* This field is optional. */
        password = virXMLPropString(nodes[i], "password");

        privconn->auths[i].username = username;
        privconn->auths[i].password = password;
    }

    ret = 0;
1217
 error:
1218 1219 1220
    VIR_FREE(nodes);
    return ret;
}
1221

C
Cole Robinson 已提交
1222 1223 1224 1225 1226
static int
testOpenParse(testDriverPtr privconn,
              const char *file,
              xmlXPathContextPtr ctxt)
{
1227
    if (!virXMLNodeNameEqual(ctxt->node, "node")) {
C
Cole Robinson 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Root element is not 'node'"));
        goto error;
    }

    if (testParseNodeInfo(&privconn->nodeInfo, ctxt) < 0)
        goto error;
    if (testParseDomains(privconn, file, ctxt) < 0)
        goto error;
    if (testParseNetworks(privconn, file, ctxt) < 0)
        goto error;
    if (testParseInterfaces(privconn, file, ctxt) < 0)
        goto error;
    if (testParseStorage(privconn, file, ctxt) < 0)
        goto error;
    if (testParseNodedevs(privconn, file, ctxt) < 0)
        goto error;
    if (testParseAuthUsers(privconn, ctxt) < 0)
        goto error;

    return 0;
 error:
    return -1;
}

1253 1254
/* No shared state between simultaneous test connections initialized
 * from a file.  */
1255 1256 1257 1258 1259
static int
testOpenFromFile(virConnectPtr conn, const char *file)
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
1260
    testDriverPtr privconn;
1261

1262
    if (!(privconn = testDriverNew()))
1263
        return VIR_DRV_OPEN_ERROR;
1264

1265 1266 1267 1268 1269 1270
    testDriverLock(privconn);
    conn->privateData = privconn;

    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;

1271
    if (!(doc = virXMLParseFileCtxt(file, &ctxt)))
1272 1273 1274 1275 1276
        goto error;

    privconn->numCells = 0;
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

C
Cole Robinson 已提交
1277
    if (testOpenParse(privconn, file, ctxt) < 0)
1278
        goto error;
1279

J
Jim Meyering 已提交
1280
    xmlXPathFreeContext(ctxt);
1281
    xmlFreeDoc(doc);
1282
    testDriverUnlock(privconn);
1283

1284
    return VIR_DRV_OPEN_SUCCESS;
1285 1286

 error:
J
Jim Meyering 已提交
1287
    xmlXPathFreeContext(ctxt);
1288
    xmlFreeDoc(doc);
1289
    testDriverFree(privconn);
1290
    conn->privateData = NULL;
1291
    return VIR_DRV_OPEN_ERROR;
1292 1293
}

1294 1295 1296 1297 1298 1299
/* Simultaneous test:///default connections should share the same
 * common state (among other things, this allows testing event
 * detection in one connection for an action caused in another).  */
static int
testOpenDefault(virConnectPtr conn)
{
1300
    int ret = VIR_DRV_OPEN_ERROR;
1301
    testDriverPtr privconn = NULL;
1302 1303
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
1304
    size_t i;
1305 1306 1307

    virMutexLock(&defaultLock);
    if (defaultConnections++) {
1308
        conn->privateData = defaultPrivconn;
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
        virMutexUnlock(&defaultLock);
        return VIR_DRV_OPEN_SUCCESS;
    }

    if (!(privconn = testDriverNew()))
        goto error;

    conn->privateData = privconn;

    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    /* Numa setup */
    privconn->numCells = 2;
1322 1323 1324 1325
    for (i = 0; i < privconn->numCells; i++) {
        privconn->cells[i].numCpus = 8;
        privconn->cells[i].mem = (i + 1) * 2048 * 1024;
        privconn->cells[i].freeMem = (i + 1) * 1024 * 1024;
1326
    }
1327
    for (i = 0; i < 16; i++) {
1328 1329 1330
        virBitmapPtr siblings = virBitmapNew(16);
        if (!siblings)
            goto error;
1331 1332 1333 1334 1335
        ignore_value(virBitmapSetBit(siblings, i));
        privconn->cells[i / 8].cpus[(i % 8)].id = i;
        privconn->cells[i / 8].cpus[(i % 8)].socket_id = i / 8;
        privconn->cells[i / 8].cpus[(i % 8)].core_id = i % 8;
        privconn->cells[i / 8].cpus[(i % 8)].siblings = siblings;
1336 1337 1338 1339 1340
    }

    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;

1341 1342
    if (!(doc = virXMLParseStringCtxt(defaultConnXML,
                                      _("(test driver)"), &ctxt)))
1343 1344
        goto error;

1345
    if (testOpenParse(privconn, NULL, ctxt) < 0)
1346 1347
        goto error;

1348
    defaultPrivconn = privconn;
1349 1350 1351
    ret = VIR_DRV_OPEN_SUCCESS;
 cleanup:
    virMutexUnlock(&defaultLock);
1352 1353
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);
1354
    return ret;
1355 1356 1357 1358 1359

 error:
    testDriverFree(privconn);
    conn->privateData = NULL;
    defaultConnections--;
1360
    goto cleanup;
1361 1362
}

1363 1364 1365 1366
static int
testConnectAuthenticate(virConnectPtr conn,
                        virConnectAuthPtr auth)
{
1367
    testDriverPtr privconn = conn->privateData;
1368 1369 1370 1371
    int ret = -1;
    ssize_t i;
    char *username = NULL, *password = NULL;

1372 1373 1374
    testDriverLock(privconn);
    if (privconn->numAuths == 0) {
        testDriverUnlock(privconn);
1375
        return 0;
1376
    }
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394

    /* Authentication is required because the test XML contains a
     * non-empty <auth/> section.  First we must ask for a username.
     */
    username = virAuthGetUsername(conn, auth, "test", NULL, "localhost"/*?*/);
    if (!username) {
        virReportError(VIR_ERR_AUTH_FAILED, "%s",
                       _("authentication failed when asking for username"));
        goto cleanup;
    }

    /* Does the username exist? */
    for (i = 0; i < privconn->numAuths; ++i) {
        if (STREQ(privconn->auths[i].username, username))
            goto found_user;
    }
    i = -1;

1395
 found_user:
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
    /* Even if we didn't find the user, we still ask for a password. */
    if (i == -1 || privconn->auths[i].password != NULL) {
        password = virAuthGetPassword(conn, auth, "test",
                                      username, "localhost");
        if (password == NULL) {
            virReportError(VIR_ERR_AUTH_FAILED, "%s",
                           _("authentication failed when asking for password"));
            goto cleanup;
        }
    }

    if (i == -1 ||
        (password && STRNEQ(privconn->auths[i].password, password))) {
        virReportError(VIR_ERR_AUTH_FAILED, "%s",
                       _("authentication failed, see test XML for the correct username/password"));
        goto cleanup;
    }

    ret = 0;
1415
 cleanup:
1416
    testDriverUnlock(privconn);
1417 1418 1419 1420
    VIR_FREE(username);
    VIR_FREE(password);
    return ret;
}
1421

1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451

static void
testDriverCloseInternal(testDriverPtr driver)
{
    bool dflt = false;

    if (driver == defaultPrivconn) {
        dflt = true;
        virMutexLock(&defaultLock);
        if (--defaultConnections) {
            virMutexUnlock(&defaultLock);
            return;
        }
    }

    testDriverLock(driver);
    testDriverFree(driver);

    if (dflt) {
        defaultPrivconn = NULL;
        virMutexUnlock(&defaultLock);
    }
}


static virDrvOpenStatus
testConnectOpen(virConnectPtr conn,
                virConnectAuthPtr auth,
                virConfPtr conf ATTRIBUTE_UNUSED,
                unsigned int flags)
1452
{
1453
    int ret;
1454

E
Eric Blake 已提交
1455 1456
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1457 1458
    if (conn->uri->path[0] == '\0' ||
        (conn->uri->path[0] == '/' && conn->uri->path[1] == '\0')) {
1459 1460
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("testOpen: supply a path or use test:///default"));
1461 1462
        return VIR_DRV_OPEN_ERROR;
    }
1463

1464
    if (STREQ(conn->uri->path, "/default"))
1465 1466
        ret = testOpenDefault(conn);
    else
1467
        ret = testOpenFromFile(conn,
1468
                               conn->uri->path);
1469

1470 1471 1472
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

1473
    /* Fake authentication. */
1474 1475 1476
    if (testConnectAuthenticate(conn, auth) < 0) {
        testDriverCloseInternal(conn->privateData);
        conn->privateData = NULL;
1477
        return VIR_DRV_OPEN_ERROR;
1478
    }
1479

1480
    return VIR_DRV_OPEN_SUCCESS;
1481 1482
}

1483

1484 1485 1486 1487
static int
testConnectClose(virConnectPtr conn)
{
    testDriverCloseInternal(conn->privateData);
1488
    conn->privateData = NULL;
1489
    return 0;
1490 1491
}

1492

1493 1494
static int testConnectGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                                 unsigned long *hvVer)
1495
{
1496
    *hvVer = 2;
1497
    return 0;
1498 1499
}

1500 1501 1502 1503 1504 1505
static char *testConnectGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return virGetHostname();
}


1506
static int testConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
1507 1508 1509 1510
{
    return 1;
}

1511
static int testConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
1512 1513 1514 1515
{
    return 0;
}

1516
static int testConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
1517 1518 1519 1520
{
    return 1;
}

1521 1522
static int testConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type ATTRIBUTE_UNUSED)
1523 1524 1525 1526
{
    return 32;
}

1527 1528 1529 1530 1531 1532
static char *
testConnectBaselineCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
                       const char **xmlCPUs,
                       unsigned int ncpus,
                       unsigned int flags)
{
J
Jiri Denemark 已提交
1533 1534 1535
    virCPUDefPtr *cpus = NULL;
    virCPUDefPtr cpu = NULL;
    char *cpustr = NULL;
1536 1537 1538

    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, NULL);

J
Jiri Denemark 已提交
1539 1540 1541
    if (!(cpus = virCPUDefListParse(xmlCPUs, ncpus, VIR_CPU_TYPE_HOST)))
        goto cleanup;

1542
    if (!(cpu = cpuBaseline(cpus, ncpus, NULL, false)))
J
Jiri Denemark 已提交
1543 1544 1545 1546 1547 1548
        goto cleanup;

    if ((flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) &&
        virCPUExpandFeatures(cpus[0]->arch, cpu) < 0)
        goto cleanup;

1549
    cpustr = virCPUDefFormat(cpu, NULL);
J
Jiri Denemark 已提交
1550 1551 1552 1553

 cleanup:
    virCPUDefListFree(cpus);
    virCPUDefFree(cpu);
1554

J
Jiri Denemark 已提交
1555
    return cpustr;
1556 1557
}

1558 1559
static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1560
{
1561
    testDriverPtr privconn = conn->privateData;
1562
    testDriverLock(privconn);
1563
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1564
    testDriverUnlock(privconn);
1565
    return 0;
1566 1567
}

1568
static char *testConnectGetCapabilities(virConnectPtr conn)
1569
{
1570
    testDriverPtr privconn = conn->privateData;
1571
    char *xml;
1572
    testDriverLock(privconn);
1573
    xml = virCapabilitiesFormatXML(privconn->caps);
1574
    testDriverUnlock(privconn);
1575
    return xml;
1576 1577
}

1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
static char *
testConnectGetSysinfo(virConnectPtr conn ATTRIBUTE_UNUSED,
                      unsigned int flags)
{
    char *ret;
    const char *sysinfo = "<sysinfo type='smbios'>\n"
           "  <bios>\n"
           "    <entry name='vendor'>LENOVO</entry>\n"
           "    <entry name='version'>G4ETA1WW (2.61 )</entry>\n"
           "    <entry name='date'>05/07/2014</entry>\n"
           "    <entry name='release'>2.61</entry>\n"
           "  </bios>\n"
           "</sysinfo>\n";

    virCheckFlags(0, NULL);

    ignore_value(VIR_STRDUP(ret, sysinfo));
    return ret;
}

1598 1599 1600 1601 1602 1603
static const char *
testConnectGetType(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return "TEST";
}

1604
static int testConnectNumOfDomains(virConnectPtr conn)
1605
{
1606
    testDriverPtr privconn = conn->privateData;
1607
    int count;
1608

1609
    testDriverLock(privconn);
1610
    count = virDomainObjListNumOfDomains(privconn->domains, true, NULL, NULL);
1611
    testDriverUnlock(privconn);
1612

1613
    return count;
1614 1615
}

1616 1617 1618
static int testDomainIsActive(virDomainPtr dom)
{
    virDomainObjPtr obj;
1619
    int ret;
1620

1621 1622
    if (!(obj = testDomObjFromDomain(dom)))
        return -1;
1623

1624 1625
    ret = virDomainObjIsActive(obj);
    virDomainObjEndAPI(&obj);
1626 1627 1628 1629 1630 1631
    return ret;
}

static int testDomainIsPersistent(virDomainPtr dom)
{
    virDomainObjPtr obj;
1632 1633 1634 1635
    int ret;

    if (!(obj = testDomObjFromDomain(dom)))
        return -1;
1636 1637 1638

    ret = obj->persistent;

1639
    virDomainObjEndAPI(&obj);
1640 1641 1642
    return ret;
}

1643 1644 1645 1646 1647
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

1648
static virDomainPtr
1649
testDomainCreateXML(virConnectPtr conn, const char *xml,
1650
                      unsigned int flags)
1651
{
1652
    testDriverPtr privconn = conn->privateData;
1653
    virDomainPtr ret = NULL;
1654
    virDomainDefPtr def;
1655
    virDomainObjPtr dom = NULL;
1656
    virObjectEventPtr event = NULL;
1657
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
1658

1659 1660 1661
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_START_VALIDATE)
1662
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
1663

1664
    testDriverLock(privconn);
1665
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
1666
                                       NULL, parse_flags)) == NULL)
1667
        goto cleanup;
1668

1669
    if (testDomainGenerateIfnames(def) < 0)
1670
        goto cleanup;
1671
    if (!(dom = virDomainObjListAdd(privconn->domains,
1672
                                    def,
1673
                                    privconn->xmlopt,
1674
                                    VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
1675 1676
                                    VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                    NULL)))
1677 1678
        goto cleanup;
    def = NULL;
1679

1680 1681 1682 1683 1684
    if (testDomainStartState(privconn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
        if (!dom->persistent) {
            virDomainObjListRemove(privconn->domains, dom);
            dom = NULL;
        }
1685
        goto cleanup;
1686
    }
1687

1688
    event = virDomainEventLifecycleNewFromObj(dom,
1689 1690 1691
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1692
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1693

1694
 cleanup:
1695
    if (dom)
1696
        virObjectUnlock(dom);
1697
    testObjectEventQueue(privconn, event);
1698
    virDomainDefFree(def);
1699
    testDriverUnlock(privconn);
1700
    return ret;
1701 1702 1703
}


1704
static virDomainPtr testDomainLookupByID(virConnectPtr conn,
1705
                                         int id)
1706
{
1707
    testDriverPtr privconn = conn->privateData;
1708 1709
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1710

1711
    if (!(dom = virDomainObjListFindByIDRef(privconn->domains, id))) {
1712
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1713
        return NULL;
1714 1715
    }

1716
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1717

1718
    virDomainObjEndAPI(&dom);
1719
    return ret;
1720 1721
}

1722
static virDomainPtr testDomainLookupByUUID(virConnectPtr conn,
1723
                                           const unsigned char *uuid)
1724
{
1725
    testDriverPtr privconn = conn->privateData;
1726
    virDomainPtr ret = NULL;
1727
    virDomainObjPtr dom;
1728

1729
    if (!(dom = virDomainObjListFindByUUID(privconn->domains, uuid))) {
1730
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1731
        return NULL;
1732
    }
1733

1734
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1735

1736
    virDomainObjEndAPI(&dom);
1737
    return ret;
1738 1739
}

1740
static virDomainPtr testDomainLookupByName(virConnectPtr conn,
1741
                                           const char *name)
1742
{
1743
    testDriverPtr privconn = conn->privateData;
1744 1745
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1746

1747
    if (!(dom = virDomainObjListFindByName(privconn->domains, name))) {
1748
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1749
        goto cleanup;
1750
    }
1751

1752
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1753

1754
 cleanup:
1755
    virDomainObjEndAPI(&dom);
1756
    return ret;
1757 1758
}

1759 1760 1761
static int testConnectListDomains(virConnectPtr conn,
                                  int *ids,
                                  int maxids)
1762
{
1763
    testDriverPtr privconn = conn->privateData;
1764

1765 1766
    return virDomainObjListGetActiveIDs(privconn->domains, ids, maxids,
                                        NULL, NULL);
1767 1768
}

1769 1770
static int testDomainDestroyFlags(virDomainPtr domain,
                                  unsigned int flags)
1771
{
1772
    testDriverPtr privconn = domain->conn->privateData;
1773
    virDomainObjPtr privdom;
1774
    virObjectEventPtr event = NULL;
1775
    int ret = -1;
1776

1777 1778
    virCheckFlags(VIR_DOMAIN_DESTROY_GRACEFUL, -1);

1779
    if (!(privdom = testDomObjFromDomain(domain)))
1780
        goto cleanup;
1781

1782
    if (virDomainObjCheckActive(privdom) < 0)
1783 1784
        goto cleanup;

J
Jiri Denemark 已提交
1785
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1786
    event = virDomainEventLifecycleNewFromObj(privdom,
1787 1788
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1789

1790
    if (!privdom->persistent) {
1791
        virDomainObjListRemove(privconn->domains, privdom);
1792 1793
        virObjectLock(privdom);
    }
1794 1795

    ret = 0;
1796
 cleanup:
1797
    virDomainObjEndAPI(&privdom);
1798
    testObjectEventQueue(privconn, event);
1799
    return ret;
1800 1801
}

1802 1803 1804 1805 1806
static int testDomainDestroy(virDomainPtr domain)
{
    return testDomainDestroyFlags(domain, 0);
}

1807
static int testDomainResume(virDomainPtr domain)
1808
{
1809
    testDriverPtr privconn = domain->conn->privateData;
1810
    virDomainObjPtr privdom;
1811
    virObjectEventPtr event = NULL;
1812
    int ret = -1;
1813

1814 1815
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1816

J
Jiri Denemark 已提交
1817
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_PAUSED) {
1818 1819
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
                       domain->name);
1820
        goto cleanup;
1821
    }
1822

J
Jiri Denemark 已提交
1823 1824
    virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                         VIR_DOMAIN_RUNNING_UNPAUSED);
1825
    event = virDomainEventLifecycleNewFromObj(privdom,
1826 1827
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1828 1829
    ret = 0;

1830
 cleanup:
1831
    virDomainObjEndAPI(&privdom);
1832
    testObjectEventQueue(privconn, event);
1833
    return ret;
1834 1835
}

1836
static int testDomainSuspend(virDomainPtr domain)
1837
{
1838
    testDriverPtr privconn = domain->conn->privateData;
1839
    virDomainObjPtr privdom;
1840
    virObjectEventPtr event = NULL;
1841
    int ret = -1;
J
Jiri Denemark 已提交
1842
    int state;
1843

1844 1845
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1846

J
Jiri Denemark 已提交
1847 1848
    state = virDomainObjGetState(privdom, NULL);
    if (state == VIR_DOMAIN_SHUTOFF || state == VIR_DOMAIN_PAUSED) {
1849 1850
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
                       domain->name);
1851
        goto cleanup;
1852
    }
1853

J
Jiri Denemark 已提交
1854
    virDomainObjSetState(privdom, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
1855
    event = virDomainEventLifecycleNewFromObj(privdom,
1856 1857
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1858 1859
    ret = 0;

1860
 cleanup:
1861
    virDomainObjEndAPI(&privdom);
1862
    testObjectEventQueue(privconn, event);
1863
    return ret;
1864 1865
}

1866
static int testDomainShutdownFlags(virDomainPtr domain,
1867
                                   unsigned int flags)
1868
{
1869
    testDriverPtr privconn = domain->conn->privateData;
1870
    virDomainObjPtr privdom;
1871
    virObjectEventPtr event = NULL;
1872
    int ret = -1;
1873

1874 1875
    virCheckFlags(0, -1);

1876

1877
    if (!(privdom = testDomObjFromDomain(domain)))
1878
        goto cleanup;
1879

J
Jiri Denemark 已提交
1880
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
1881 1882
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain '%s' not running"), domain->name);
1883
        goto cleanup;
1884
    }
1885

J
Jiri Denemark 已提交
1886
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1887
    event = virDomainEventLifecycleNewFromObj(privdom,
1888 1889
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1890

1891
    if (!privdom->persistent) {
1892
        virDomainObjListRemove(privconn->domains, privdom);
1893 1894
        virObjectLock(privdom);
    }
1895

1896
    ret = 0;
1897
 cleanup:
1898
    virDomainObjEndAPI(&privdom);
1899
    testObjectEventQueue(privconn, event);
1900
    return ret;
1901 1902
}

1903
static int testDomainShutdown(virDomainPtr domain)
1904
{
1905
    return testDomainShutdownFlags(domain, 0);
1906 1907
}

1908
/* Similar behaviour as shutdown */
1909
static int testDomainReboot(virDomainPtr domain,
1910
                            unsigned int action ATTRIBUTE_UNUSED)
1911
{
1912
    testDriverPtr privconn = domain->conn->privateData;
1913
    virDomainObjPtr privdom;
1914
    virObjectEventPtr event = NULL;
1915
    int ret = -1;
1916 1917


1918
    if (!(privdom = testDomObjFromDomain(domain)))
1919
        goto cleanup;
1920

1921
    if (virDomainObjCheckActive(privdom) < 0)
1922 1923
        goto cleanup;

J
Jiri Denemark 已提交
1924 1925 1926
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

1927
    switch (privdom->def->onReboot) {
1928
    case VIR_DOMAIN_LIFECYCLE_ACTION_DESTROY:
J
Jiri Denemark 已提交
1929 1930
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1931 1932
        break;

1933
    case VIR_DOMAIN_LIFECYCLE_ACTION_RESTART:
J
Jiri Denemark 已提交
1934 1935
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1936 1937
        break;

1938
    case VIR_DOMAIN_LIFECYCLE_ACTION_PRESERVE:
J
Jiri Denemark 已提交
1939 1940
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1941 1942
        break;

1943
    case VIR_DOMAIN_LIFECYCLE_ACTION_RESTART_RENAME:
J
Jiri Denemark 已提交
1944 1945
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1946
        break;
1947

1948
    default:
J
Jiri Denemark 已提交
1949 1950
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1951 1952
        break;
    }
1953

J
Jiri Denemark 已提交
1954 1955
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1956
        event = virDomainEventLifecycleNewFromObj(privdom,
1957 1958
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1959

1960
        if (!privdom->persistent) {
1961
            virDomainObjListRemove(privconn->domains, privdom);
1962 1963
            virObjectLock(privdom);
        }
1964 1965
    }

1966
    ret = 0;
1967
 cleanup:
1968
    virDomainObjEndAPI(&privdom);
1969
    testObjectEventQueue(privconn, event);
1970
    return ret;
1971 1972
}

1973
static int testDomainGetInfo(virDomainPtr domain,
1974
                             virDomainInfoPtr info)
1975
{
1976
    struct timeval tv;
1977
    virDomainObjPtr privdom;
1978
    int ret = -1;
1979

1980 1981
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1982 1983

    if (gettimeofday(&tv, NULL) < 0) {
1984 1985
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("getting time of day"));
1986
        goto cleanup;
1987 1988
    }

J
Jiri Denemark 已提交
1989
    info->state = virDomainObjGetState(privdom, NULL);
1990
    info->memory = privdom->def->mem.cur_balloon;
1991
    info->maxMem = virDomainDefGetMemoryTotal(privdom->def);
1992
    info->nrVirtCpu = virDomainDefGetVcpus(privdom->def);
1993
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1994 1995
    ret = 0;

1996
 cleanup:
1997
    virDomainObjEndAPI(&privdom);
1998
    return ret;
1999 2000
}

2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
static int
testDomainGetState(virDomainPtr domain,
                   int *state,
                   int *reason,
                   unsigned int flags)
{
    virDomainObjPtr privdom;

    virCheckFlags(0, -1);

2011 2012
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2013

J
Jiri Denemark 已提交
2014
    *state = virDomainObjGetState(privdom, reason);
2015

2016
    virDomainObjEndAPI(&privdom);
2017 2018

    return 0;
2019 2020
}

2021 2022
#define TEST_SAVE_MAGIC "TestGuestMagic"

2023 2024 2025
static int
testDomainSaveFlags(virDomainPtr domain, const char *path,
                    const char *dxml, unsigned int flags)
2026
{
2027
    testDriverPtr privconn = domain->conn->privateData;
2028 2029 2030
    char *xml = NULL;
    int fd = -1;
    int len;
2031
    virDomainObjPtr privdom;
2032
    virObjectEventPtr event = NULL;
2033
    int ret = -1;
2034

2035 2036
    virCheckFlags(0, -1);
    if (dxml) {
2037 2038
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
2039 2040 2041
        return -1;
    }

2042

2043
    if (!(privdom = testDomObjFromDomain(domain)))
2044
        goto cleanup;
2045

2046
    if (virDomainObjCheckActive(privdom) < 0)
2047 2048
        goto cleanup;

2049
    xml = virDomainDefFormat(privdom->def, privconn->caps,
2050
                             VIR_DOMAIN_DEF_FORMAT_SECURE);
C
Cole Robinson 已提交
2051

2052
    if (xml == NULL) {
2053
        virReportSystemError(errno,
2054 2055
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
2056
        goto cleanup;
2057
    }
2058 2059

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
2060
        virReportSystemError(errno,
2061 2062
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
2063
        goto cleanup;
2064
    }
2065
    len = strlen(xml);
2066
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
2067
        virReportSystemError(errno,
2068 2069
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2070
        goto cleanup;
2071
    }
2072
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
2073
        virReportSystemError(errno,
2074 2075
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2076
        goto cleanup;
2077
    }
2078
    if (safewrite(fd, xml, len) < 0) {
2079
        virReportSystemError(errno,
2080 2081
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2082
        goto cleanup;
2083
    }
2084

2085
    if (VIR_CLOSE(fd) < 0) {
2086
        virReportSystemError(errno,
2087 2088
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2089
        goto cleanup;
2090
    }
2091 2092
    fd = -1;

J
Jiri Denemark 已提交
2093
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
2094
    event = virDomainEventLifecycleNewFromObj(privdom,
2095 2096
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
2097

2098
    if (!privdom->persistent) {
2099
        virDomainObjListRemove(privconn->domains, privdom);
2100 2101
        virObjectLock(privdom);
    }
2102

2103
    ret = 0;
2104
 cleanup:
2105 2106 2107 2108
    VIR_FREE(xml);

    /* Don't report failure in close or unlink, because
     * in either case we're already in a failure scenario
Y
Yuri Chornoivan 已提交
2109
     * and have reported an earlier error */
2110
    if (ret != 0) {
2111
        VIR_FORCE_CLOSE(fd);
2112 2113
        unlink(path);
    }
2114
    virDomainObjEndAPI(&privdom);
2115
    testObjectEventQueue(privconn, event);
2116
    return ret;
2117 2118
}

2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
static int
testDomainSave(virDomainPtr domain,
               const char *path)
{
    return testDomainSaveFlags(domain, path, NULL, 0);
}

static int
testDomainRestoreFlags(virConnectPtr conn,
                       const char *path,
                       const char *dxml,
                       unsigned int flags)
2131
{
2132
    testDriverPtr privconn = conn->privateData;
2133
    char *xml = NULL;
2134
    char magic[15];
2135 2136 2137
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
2138
    virDomainObjPtr dom = NULL;
2139
    virObjectEventPtr event = NULL;
2140
    int ret = -1;
2141

2142 2143
    virCheckFlags(0, -1);
    if (dxml) {
2144 2145
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
2146 2147 2148
        return -1;
    }

2149
    if ((fd = open(path, O_RDONLY)) < 0) {
2150
        virReportSystemError(errno,
2151 2152
                             _("cannot read domain image '%s'"),
                             path);
2153
        goto cleanup;
2154
    }
2155
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
2156
        virReportSystemError(errno,
2157 2158
                             _("incomplete save header in '%s'"),
                             path);
2159
        goto cleanup;
2160
    }
2161
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
2162 2163
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("mismatched header magic"));
2164
        goto cleanup;
2165
    }
2166
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
2167
        virReportSystemError(errno,
2168 2169
                             _("failed to read metadata length in '%s'"),
                             path);
2170
        goto cleanup;
2171 2172
    }
    if (len < 1 || len > 8192) {
2173 2174
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("length of metadata out of range"));
2175
        goto cleanup;
2176
    }
2177
    if (VIR_ALLOC_N(xml, len+1) < 0)
2178
        goto cleanup;
2179
    if (saferead(fd, xml, len) != len) {
2180
        virReportSystemError(errno,
2181
                             _("incomplete metadata in '%s'"), path);
2182
        goto cleanup;
2183 2184
    }
    xml[len] = '\0';
2185

2186
    def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
2187
                                  NULL, VIR_DOMAIN_DEF_PARSE_INACTIVE);
2188
    if (!def)
2189
        goto cleanup;
2190

2191
    if (testDomainGenerateIfnames(def) < 0)
2192
        goto cleanup;
2193
    if (!(dom = virDomainObjListAdd(privconn->domains,
2194
                                    def,
2195
                                    privconn->xmlopt,
2196 2197 2198
                                    VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
                                    VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                    NULL)))
2199 2200
        goto cleanup;
    def = NULL;
2201

2202 2203 2204 2205 2206
    if (testDomainStartState(privconn, dom, VIR_DOMAIN_RUNNING_RESTORED) < 0) {
        if (!dom->persistent) {
            virDomainObjListRemove(privconn->domains, dom);
            dom = NULL;
        }
2207
        goto cleanup;
2208
    }
2209

2210
    event = virDomainEventLifecycleNewFromObj(dom,
2211 2212
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
2213
    ret = 0;
2214

2215
 cleanup:
2216 2217
    virDomainDefFree(def);
    VIR_FREE(xml);
2218
    VIR_FORCE_CLOSE(fd);
2219
    if (dom)
2220
        virObjectUnlock(dom);
2221
    testObjectEventQueue(privconn, event);
2222
    return ret;
2223 2224
}

2225 2226 2227 2228 2229 2230 2231
static int
testDomainRestore(virConnectPtr conn,
                  const char *path)
{
    return testDomainRestoreFlags(conn, path, NULL, 0);
}

2232 2233 2234 2235
static int testDomainCoreDumpWithFormat(virDomainPtr domain,
                                        const char *to,
                                        unsigned int dumpformat,
                                        unsigned int flags)
2236
{
2237
    testDriverPtr privconn = domain->conn->privateData;
2238
    int fd = -1;
2239
    virDomainObjPtr privdom;
2240
    virObjectEventPtr event = NULL;
2241
    int ret = -1;
2242

E
Eric Blake 已提交
2243 2244
    virCheckFlags(VIR_DUMP_CRASH, -1);

2245

2246
    if (!(privdom = testDomObjFromDomain(domain)))
2247
        goto cleanup;
2248

2249
    if (virDomainObjCheckActive(privdom) < 0)
2250 2251
        goto cleanup;

2252
    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
2253
        virReportSystemError(errno,
2254 2255
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
2256
        goto cleanup;
2257
    }
2258
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
2259
        virReportSystemError(errno,
2260 2261
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
2262
        goto cleanup;
2263
    }
2264
    if (VIR_CLOSE(fd) < 0) {
2265
        virReportSystemError(errno,
2266 2267
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
2268
        goto cleanup;
2269
    }
2270

2271 2272 2273 2274 2275 2276 2277
    /* we don't support non-raw formats in test driver */
    if (dumpformat != VIR_DOMAIN_CORE_DUMP_FORMAT_RAW) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("kdump-compressed format is not supported here"));
        goto cleanup;
    }

2278
    if (flags & VIR_DUMP_CRASH) {
J
Jiri Denemark 已提交
2279
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_CRASHED);
2280
        event = virDomainEventLifecycleNewFromObj(privdom,
2281 2282
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
2283
        if (!privdom->persistent) {
2284
            virDomainObjListRemove(privconn->domains, privdom);
2285 2286
            virObjectLock(privdom);
        }
2287
    }
2288

2289
    ret = 0;
2290
 cleanup:
2291
    VIR_FORCE_CLOSE(fd);
2292
    virDomainObjEndAPI(&privdom);
2293
    testObjectEventQueue(privconn, event);
2294
    return ret;
2295 2296
}

2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310

static int
testDomainCoreDump(virDomainPtr domain,
                   const char *to,
                   unsigned int flags)
{
    return testDomainCoreDumpWithFormat(domain, to,
                                        VIR_DOMAIN_CORE_DUMP_FORMAT_RAW, flags);
}


static char *
testDomainGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED)
{
2311 2312 2313
    char *ret;

    ignore_value(VIR_STRDUP(ret, "linux"));
2314
    return ret;
2315 2316
}

2317 2318 2319

static unsigned long long
testDomainGetMaxMemory(virDomainPtr domain)
2320
{
2321
    virDomainObjPtr privdom;
2322
    unsigned long long ret = 0;
2323

2324 2325
    if (!(privdom = testDomObjFromDomain(domain)))
        return 0;
2326

2327
    ret = virDomainDefGetMemoryTotal(privdom->def);
2328

2329
    virDomainObjEndAPI(&privdom);
2330
    return ret;
2331 2332
}

2333 2334
static int testDomainSetMaxMemory(virDomainPtr domain,
                                  unsigned long memory)
2335
{
2336 2337
    virDomainObjPtr privdom;

2338 2339
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2340 2341

    /* XXX validate not over host memory wrt to other domains */
2342
    virDomainDefSetMemoryTotal(privdom->def, memory);
2343

2344
    virDomainObjEndAPI(&privdom);
2345
    return 0;
2346 2347
}

2348 2349
static int testDomainSetMemory(virDomainPtr domain,
                               unsigned long memory)
2350
{
2351
    virDomainObjPtr privdom;
2352
    int ret = -1;
2353

2354 2355
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2356

2357
    if (memory > virDomainDefGetMemoryTotal(privdom->def)) {
2358
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2359
        goto cleanup;
2360
    }
2361

2362
    privdom->def->mem.cur_balloon = memory;
2363 2364
    ret = 0;

2365
 cleanup:
2366
    virDomainObjEndAPI(&privdom);
2367
    return ret;
2368 2369
}

2370 2371
static int
testDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
C
Cole Robinson 已提交
2372
{
2373 2374 2375 2376
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;

2377 2378
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2379 2380
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

2381 2382
    if (!(vm = testDomObjFromDomain(domain)))
        return -1;
2383

2384
    if (!(def = virDomainObjGetOneDef(vm, flags)))
2385
        goto cleanup;
2386

2387 2388 2389
    if (flags & VIR_DOMAIN_VCPU_MAXIMUM)
        ret = virDomainDefGetVcpusMax(def);
    else
2390
        ret = virDomainDefGetVcpus(def);
2391

2392
 cleanup:
2393
    virDomainObjEndAPI(&vm);
2394
    return ret;
C
Cole Robinson 已提交
2395 2396
}

2397 2398 2399
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
2400
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2401 2402 2403 2404 2405 2406 2407
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

static int
testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
                        unsigned int flags)
{
2408
    testDriverPtr driver = domain->conn->privateData;
2409
    virDomainObjPtr privdom = NULL;
2410
    virDomainDefPtr def;
2411
    virDomainDefPtr persistentDef;
C
Cole Robinson 已提交
2412 2413
    int ret = -1, maxvcpus;

2414 2415
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2416 2417
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

2418
    if ((maxvcpus = testConnectGetMaxVcpus(domain->conn, NULL)) < 0)
2419
        return -1;
2420 2421

    if (nrCpus > maxvcpus) {
2422
        virReportError(VIR_ERR_INVALID_ARG,
2423 2424
                       _("requested cpu amount exceeds maximum supported amount "
                         "(%d > %d)"), nrCpus, maxvcpus);
2425 2426
        return -1;
    }
2427

2428 2429
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2430

2431
    if (virDomainObjGetDefs(privdom, flags, &def, &persistentDef) < 0)
C
Cole Robinson 已提交
2432 2433
        goto cleanup;

2434
    if (def && virDomainDefGetVcpusMax(def) < nrCpus) {
2435 2436
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested cpu amount exceeds maximum (%d > %d)"),
2437
                       nrCpus, virDomainDefGetVcpusMax(def));
2438
        goto cleanup;
2439
    }
2440

2441 2442
    if (persistentDef &&
        !(flags & VIR_DOMAIN_VCPU_MAXIMUM) &&
2443
        virDomainDefGetVcpusMax(persistentDef) < nrCpus) {
2444 2445
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested cpu amount exceeds maximum (%d > %d)"),
2446
                       nrCpus, virDomainDefGetVcpusMax(persistentDef));
2447
        goto cleanup;
2448
    }
2449

2450 2451 2452
    if (def &&
        virDomainDefSetVcpus(def, nrCpus) < 0)
        goto cleanup;
2453

2454 2455
    if (persistentDef) {
        if (flags & VIR_DOMAIN_VCPU_MAXIMUM) {
2456 2457
            if (virDomainDefSetVcpusMax(persistentDef, nrCpus,
                                        driver->xmlopt) < 0)
2458
                goto cleanup;
2459
        } else {
2460 2461
            if (virDomainDefSetVcpus(persistentDef, nrCpus) < 0)
                goto cleanup;
2462
        }
2463
    }
2464

2465 2466
    ret = 0;

2467
 cleanup:
2468
    virDomainObjEndAPI(&privdom);
2469
    return ret;
2470 2471
}

2472
static int
2473
testDomainSetVcpus(virDomainPtr domain, unsigned int nrCpus)
2474
{
2475
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2476 2477
}

C
Cole Robinson 已提交
2478 2479 2480 2481 2482 2483
static int testDomainGetVcpus(virDomainPtr domain,
                              virVcpuInfoPtr info,
                              int maxinfo,
                              unsigned char *cpumaps,
                              int maplen)
{
2484
    testDriverPtr privconn = domain->conn->privateData;
C
Cole Robinson 已提交
2485
    virDomainObjPtr privdom;
2486
    virDomainDefPtr def;
2487
    size_t i;
2488
    int hostcpus;
C
Cole Robinson 已提交
2489 2490 2491
    int ret = -1;
    struct timeval tv;
    unsigned long long statbase;
2492
    virBitmapPtr allcpumap = NULL;
C
Cole Robinson 已提交
2493

2494 2495
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
C
Cole Robinson 已提交
2496 2497

    if (!virDomainObjIsActive(privdom)) {
2498
        virReportError(VIR_ERR_OPERATION_INVALID,
2499
                       "%s", _("cannot list vcpus for an inactive domain"));
C
Cole Robinson 已提交
2500 2501 2502
        goto cleanup;
    }

2503
    def = privdom->def;
C
Cole Robinson 已提交
2504 2505

    if (gettimeofday(&tv, NULL) < 0) {
2506
        virReportSystemError(errno,
C
Cole Robinson 已提交
2507 2508 2509 2510 2511 2512 2513
                             "%s", _("getting time of day"));
        goto cleanup;
    }

    statbase = (tv.tv_sec * 1000UL * 1000UL) + tv.tv_usec;

    hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
2514 2515 2516 2517 2518
    if (!(allcpumap = virBitmapNew(hostcpus)))
        goto cleanup;

    virBitmapSetAll(allcpumap);

C
Cole Robinson 已提交
2519
    /* Clamp to actual number of vcpus */
2520 2521
    if (maxinfo > virDomainDefGetVcpus(privdom->def))
        maxinfo = virDomainDefGetVcpus(privdom->def);
C
Cole Robinson 已提交
2522

2523 2524
    memset(info, 0, sizeof(*info) * maxinfo);
    memset(cpumaps, 0, maxinfo * maplen);
C
Cole Robinson 已提交
2525

2526
    for (i = 0; i < maxinfo; i++) {
2527
        virDomainVcpuDefPtr vcpu = virDomainDefGetVcpu(def, i);
2528
        virBitmapPtr bitmap = NULL;
C
Cole Robinson 已提交
2529

2530 2531
        if (!vcpu->online)
            continue;
C
Cole Robinson 已提交
2532

2533 2534
        if (vcpu->cpumask)
            bitmap = vcpu->cpumask;
2535 2536 2537 2538
        else if (def->cpumask)
            bitmap = def->cpumask;
        else
            bitmap = allcpumap;
C
Cole Robinson 已提交
2539

2540 2541
        if (cpumaps)
            virBitmapToDataBuf(bitmap, VIR_GET_CPUMAP(cpumaps, maplen, i), maplen);
C
Cole Robinson 已提交
2542

2543 2544 2545
        info[i].number = i;
        info[i].state = VIR_VCPU_RUNNING;
        info[i].cpu = virBitmapLastSetBit(bitmap);
C
Cole Robinson 已提交
2546

2547 2548
        /* Fake an increasing cpu time value */
        info[i].cpuTime = statbase / 10;
C
Cole Robinson 已提交
2549 2550 2551
    }

    ret = maxinfo;
2552
 cleanup:
2553
    virBitmapFree(allcpumap);
2554
    virDomainObjEndAPI(&privdom);
C
Cole Robinson 已提交
2555 2556 2557
    return ret;
}

C
Cole Robinson 已提交
2558 2559 2560 2561 2562
static int testDomainPinVcpu(virDomainPtr domain,
                             unsigned int vcpu,
                             unsigned char *cpumap,
                             int maplen)
{
2563
    virDomainVcpuDefPtr vcpuinfo;
C
Cole Robinson 已提交
2564
    virDomainObjPtr privdom;
2565
    virDomainDefPtr def;
C
Cole Robinson 已提交
2566 2567
    int ret = -1;

2568 2569
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
C
Cole Robinson 已提交
2570

2571 2572
    def = privdom->def;

C
Cole Robinson 已提交
2573
    if (!virDomainObjIsActive(privdom)) {
2574
        virReportError(VIR_ERR_OPERATION_INVALID,
2575
                       "%s", _("cannot pin vcpus on an inactive domain"));
C
Cole Robinson 已提交
2576 2577 2578
        goto cleanup;
    }

2579 2580
    if (!(vcpuinfo = virDomainDefGetVcpu(def, vcpu)) ||
        !vcpuinfo->online) {
2581 2582 2583
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested vcpu '%d' is not present in the domain"),
                       vcpu);
C
Cole Robinson 已提交
2584 2585 2586
        goto cleanup;
    }

2587 2588 2589
    virBitmapFree(vcpuinfo->cpumask);

    if (!(vcpuinfo->cpumask = virBitmapNewData(cpumap, maplen)))
2590
        goto cleanup;
C
Cole Robinson 已提交
2591 2592

    ret = 0;
2593

2594
 cleanup:
2595
    virDomainObjEndAPI(&privdom);
C
Cole Robinson 已提交
2596 2597 2598
    return ret;
}

2599 2600 2601 2602 2603 2604 2605
static int
testDomainGetVcpuPinInfo(virDomainPtr dom,
                        int ncpumaps,
                        unsigned char *cpumaps,
                        int maplen,
                        unsigned int flags)
{
2606
    testDriverPtr driver = dom->conn->privateData;
2607 2608
    virDomainObjPtr privdom;
    virDomainDefPtr def;
2609
    int ret = -1;
2610 2611 2612 2613 2614 2615 2616

    if (!(privdom = testDomObjFromDomain(dom)))
        return -1;

    if (!(def = virDomainObjGetOneDef(privdom, flags)))
        goto cleanup;

2617 2618 2619
    ret = virDomainDefGetVcpuPinInfoHelper(def, maplen, ncpumaps, cpumaps,
                                           VIR_NODEINFO_MAXCPUS(driver->nodeInfo),
                                           NULL);
2620 2621 2622 2623 2624 2625

 cleanup:
    virDomainObjEndAPI(&privdom);
    return ret;
}

2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709
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, ret);

    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;
}

2710
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2711
{
2712
    testDriverPtr privconn = domain->conn->privateData;
2713
    virDomainDefPtr def;
2714
    virDomainObjPtr privdom;
2715 2716
    char *ret = NULL;

2717 2718
    /* Flags checked by virDomainDefFormat */

2719 2720
    if (!(privdom = testDomObjFromDomain(domain)))
        return NULL;
2721

2722 2723
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2724

2725 2726
    ret = virDomainDefFormat(def, privconn->caps,
                             virDomainDefFormatConvertXMLFlags(flags));
2727

2728
    virDomainObjEndAPI(&privdom);
2729
    return ret;
2730
}
2731

2732 2733
static int testConnectNumOfDefinedDomains(virConnectPtr conn)
{
2734
    testDriverPtr privconn = conn->privateData;
2735

2736
    return virDomainObjListNumOfDomains(privconn->domains, false, NULL, NULL);
2737 2738
}

2739 2740
static int testConnectListDefinedDomains(virConnectPtr conn,
                                         char **const names,
2741 2742
                                         int maxnames)
{
2743

2744
    testDriverPtr privconn = conn->privateData;
2745 2746

    memset(names, 0, sizeof(*names)*maxnames);
2747 2748
    return virDomainObjListGetInactiveNames(privconn->domains, names, maxnames,
                                            NULL, NULL);
2749 2750
}

2751 2752 2753
static virDomainPtr testDomainDefineXMLFlags(virConnectPtr conn,
                                             const char *xml,
                                             unsigned int flags)
2754
{
2755
    testDriverPtr privconn = conn->privateData;
2756
    virDomainPtr ret = NULL;
2757
    virDomainDefPtr def;
2758
    virDomainObjPtr dom = NULL;
2759
    virObjectEventPtr event = NULL;
2760
    virDomainDefPtr oldDef = NULL;
2761 2762 2763
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;

    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);
2764

2765
    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
2766
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
2767

2768
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
2769
                                       NULL, parse_flags)) == NULL)
2770
        goto cleanup;
2771

2772 2773 2774
    if (virXMLCheckIllegalChars("name", def->name, "\n") < 0)
        goto cleanup;

2775
    if (testDomainGenerateIfnames(def) < 0)
2776
        goto cleanup;
2777
    if (!(dom = virDomainObjListAdd(privconn->domains,
2778
                                    def,
2779
                                    privconn->xmlopt,
2780 2781
                                    0,
                                    &oldDef)))
2782
        goto cleanup;
2783
    def = NULL;
2784
    dom->persistent = 1;
2785

2786
    event = virDomainEventLifecycleNewFromObj(dom,
2787
                                     VIR_DOMAIN_EVENT_DEFINED,
2788
                                     !oldDef ?
2789 2790
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2791

2792
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
2793

2794
 cleanup:
2795
    virDomainDefFree(def);
2796
    virDomainDefFree(oldDef);
2797
    if (dom)
2798
        virObjectUnlock(dom);
2799
    testObjectEventQueue(privconn, event);
2800
    return ret;
2801 2802
}

2803 2804 2805 2806 2807 2808
static virDomainPtr
testDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return testDomainDefineXMLFlags(conn, xml, 0);
}

2809 2810 2811 2812 2813 2814
static char *testDomainGetMetadata(virDomainPtr dom,
                                   int type,
                                   const char *uri,
                                   unsigned int flags)
{
    virDomainObjPtr privdom;
2815
    char *ret;
2816 2817 2818 2819

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, NULL);

2820 2821
    if (!(privdom = testDomObjFromDomain(dom)))
        return NULL;
2822

2823
    ret = virDomainObjGetMetadata(privdom, type, uri, flags);
2824

2825
    virDomainObjEndAPI(&privdom);
2826 2827 2828 2829 2830 2831 2832 2833 2834 2835
    return ret;
}

static int testDomainSetMetadata(virDomainPtr dom,
                                 int type,
                                 const char *metadata,
                                 const char *key,
                                 const char *uri,
                                 unsigned int flags)
{
2836
    testDriverPtr privconn = dom->conn->privateData;
2837
    virDomainObjPtr privdom;
2838
    int ret;
2839 2840 2841 2842

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

2843 2844
    if (!(privdom = testDomObjFromDomain(dom)))
        return -1;
2845 2846 2847

    ret = virDomainObjSetMetadata(privdom, type, metadata, key, uri,
                                  privconn->caps, privconn->xmlopt,
2848
                                  NULL, NULL, flags);
2849

2850 2851 2852 2853 2854 2855
    if (ret == 0) {
        virObjectEventPtr ev = NULL;
        ev = virDomainEventMetadataChangeNewFromObj(privdom, type, uri);
        testObjectEventQueue(privconn, ev);
    }

2856
    virDomainObjEndAPI(&privdom);
2857 2858 2859 2860
    return ret;
}


2861 2862
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
2863 2864
                                      int startCell, int maxCells)
{
2865
    testDriverPtr privconn = conn->privateData;
2866 2867
    int cell;
    size_t i;
2868
    int ret = -1;
2869

2870
    testDriverLock(privconn);
2871
    if (startCell >= privconn->numCells) {
2872 2873
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("Range exceeds available cells"));
2874
        goto cleanup;
2875 2876
    }

2877 2878 2879 2880
    for (cell = startCell, i = 0;
         (cell < privconn->numCells && i < maxCells);
         ++cell, ++i) {
        freemems[i] = privconn->cells[cell].mem;
2881
    }
2882
    ret = i;
2883

2884
 cleanup:
2885
    testDriverUnlock(privconn);
2886
    return ret;
2887 2888
}

2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934
#define TEST_NB_CPU_STATS 4

static int
testNodeGetCPUStats(virConnectPtr conn ATTRIBUTE_UNUSED,
                    int cpuNum ATTRIBUTE_UNUSED,
                    virNodeCPUStatsPtr params,
                    int *nparams,
                    unsigned int flags)
{
    size_t i = 0;

    virCheckFlags(0, -1);

    if (params == NULL) {
        *nparams = TEST_NB_CPU_STATS;
        return 0;
    }

    for (i = 0; i < *nparams && i < 4; i++) {
        switch (i) {
        case 0:
            if (virHostCPUStatsAssign(&params[i],
                                      VIR_NODE_CPU_STATS_USER, 9797400000) < 0)
                return -1;
            break;
        case 1:
            if (virHostCPUStatsAssign(&params[i],
                                      VIR_NODE_CPU_STATS_KERNEL, 34678723400000) < 0)
                return -1;
            break;
        case 2:
            if (virHostCPUStatsAssign(&params[i],
                                      VIR_NODE_CPU_STATS_IDLE, 87264900000) < 0)
                return -1;
            break;
        case 3:
            if (virHostCPUStatsAssign(&params[i],
                                      VIR_NODE_CPU_STATS_IOWAIT, 763600000) < 0)
                return -1;
            break;
        }
    }

    *nparams = i;
    return 0;
}
2935

2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951
static unsigned long long
testNodeGetFreeMemory(virConnectPtr conn)
{
    testDriverPtr privconn = conn->privateData;
    unsigned int freeMem = 0;
    size_t i;

    testDriverLock(privconn);

    for (i = 0; i < privconn->numCells; i++)
        freeMem += privconn->cells[i].freeMem;

    testDriverUnlock(privconn);
    return freeMem;
}

2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975
static int
testNodeGetFreePages(virConnectPtr conn ATTRIBUTE_UNUSED,
                     unsigned int npages,
                     unsigned int *pages ATTRIBUTE_UNUSED,
                     int startCell ATTRIBUTE_UNUSED,
                     unsigned int cellCount,
                     unsigned long long *counts,
                     unsigned int flags)
{
    size_t i = 0, j = 0;
    int x = 6;

    virCheckFlags(0, -1);

    for (i = 0; i < cellCount; i++) {
        for (j = 0; j < npages; j++) {
            x = x * 2 + 7;
            counts[(i * npages) +  j] = x;
        }
    }

    return 0;
}

2976 2977
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
{
2978
    testDriverPtr privconn = domain->conn->privateData;
2979
    virDomainObjPtr privdom;
2980
    virObjectEventPtr event = NULL;
2981
    int ret = -1;
2982

2983 2984
    virCheckFlags(0, -1);

2985
    testDriverLock(privconn);
2986

2987
    if (!(privdom = testDomObjFromDomain(domain)))
2988
        goto cleanup;
2989

J
Jiri Denemark 已提交
2990
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
2991 2992
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Domain '%s' is already running"), domain->name);
2993
        goto cleanup;
2994 2995
    }

2996
    if (testDomainStartState(privconn, privdom,
J
Jiri Denemark 已提交
2997
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
2998 2999 3000
        goto cleanup;
    domain->id = privdom->def->id;

3001
    event = virDomainEventLifecycleNewFromObj(privdom,
3002 3003
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
3004
    ret = 0;
3005

3006
 cleanup:
3007
    virDomainObjEndAPI(&privdom);
3008
    testObjectEventQueue(privconn, event);
3009
    testDriverUnlock(privconn);
3010
    return ret;
3011 3012
}

3013 3014
static int testDomainCreate(virDomainPtr domain)
{
3015 3016 3017
    return testDomainCreateWithFlags(domain, 0);
}

3018 3019 3020
static int testDomainUndefineFlags(virDomainPtr domain,
                                   unsigned int flags)
{
3021
    testDriverPtr privconn = domain->conn->privateData;
3022
    virDomainObjPtr privdom;
3023
    virObjectEventPtr event = NULL;
3024
    int nsnapshots;
3025
    int ret = -1;
3026

3027 3028
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
3029

3030

3031
    if (!(privdom = testDomObjFromDomain(domain)))
3032
        goto cleanup;
3033

C
Cole Robinson 已提交
3034 3035 3036 3037 3038 3039 3040 3041
    if (privdom->hasManagedSave &&
        !(flags & VIR_DOMAIN_UNDEFINE_MANAGED_SAVE)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Refusing to undefine while domain managed "
                         "save image exists"));
        goto cleanup;
    }

3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059
    /* Requiring an inactive VM is part of the documented API for
     * UNDEFINE_SNAPSHOTS_METADATA
     */
    if (!virDomainObjIsActive(privdom) &&
        (nsnapshots = virDomainSnapshotObjListNum(privdom->snapshots,
                                                  NULL, 0))) {
        if (!(flags & VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA)) {
            virReportError(VIR_ERR_OPERATION_INVALID,
                           _("cannot delete inactive domain with %d "
                             "snapshots"),
                           nsnapshots);
            goto cleanup;
        }

        /* There isn't actually anything to do, we are just emulating qemu
         * behavior here. */
    }

3060
    event = virDomainEventLifecycleNewFromObj(privdom,
3061 3062
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
C
Cole Robinson 已提交
3063 3064
    privdom->hasManagedSave = false;

3065
    if (virDomainObjIsActive(privdom)) {
3066
        privdom->persistent = 0;
3067
    } else {
3068
        virDomainObjListRemove(privconn->domains, privdom);
3069 3070
        virObjectLock(privdom);
    }
3071

3072
    ret = 0;
3073

3074
 cleanup:
3075
    virDomainObjEndAPI(&privdom);
3076
    testObjectEventQueue(privconn, event);
3077
    return ret;
3078 3079
}

3080 3081 3082 3083 3084
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

3085 3086 3087
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
3088 3089
    virDomainObjPtr privdom;

3090 3091
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3092

3093
    *autostart = privdom->autostart;
3094

3095
    virDomainObjEndAPI(&privdom);
3096
    return 0;
3097 3098 3099 3100 3101 3102
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
3103 3104
    virDomainObjPtr privdom;

3105 3106
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3107

3108
    privdom->autostart = autostart ? 1 : 0;
3109

3110
    virDomainObjEndAPI(&privdom);
3111
    return 0;
3112
}
3113

3114
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
3115 3116
                                        int *nparams)
{
3117 3118
    char *type = NULL;

3119 3120 3121
    if (nparams)
        *nparams = 1;

3122
    ignore_value(VIR_STRDUP(type, "fair"));
3123

3124 3125 3126
    return type;
}

3127
static int
3128 3129 3130 3131
testDomainGetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int *nparams,
                                      unsigned int flags)
3132
{
3133
    virDomainObjPtr privdom;
3134
    int ret = -1;
3135

3136 3137
    virCheckFlags(0, -1);

3138 3139
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3140

3141 3142
    if (virTypedParameterAssign(params, VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, 50) < 0)
3143
        goto cleanup;
3144 3145
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
3146 3147

    *nparams = 1;
3148 3149
    ret = 0;

3150
 cleanup:
3151
    virDomainObjEndAPI(&privdom);
3152
    return ret;
3153
}
3154

3155
static int
3156 3157 3158
testDomainGetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int *nparams)
3159
{
3160
    return testDomainGetSchedulerParametersFlags(domain, params, nparams, 0);
3161
}
3162

3163
static int
3164 3165 3166 3167
testDomainSetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int nparams,
                                      unsigned int flags)
3168
{
3169
    virDomainObjPtr privdom;
3170 3171
    int ret = -1;
    size_t i;
3172

3173
    virCheckFlags(0, -1);
3174 3175 3176 3177
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_SCHEDULER_WEIGHT,
                               VIR_TYPED_PARAM_UINT,
                               NULL) < 0)
3178
        return -1;
3179

3180 3181
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3182

3183
    for (i = 0; i < nparams; i++) {
3184 3185 3186
        if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_WEIGHT)) {
            /* XXX */
            /*privdom->weight = params[i].value.ui;*/
3187
        }
3188
    }
3189

3190 3191
    ret = 0;

3192
    virDomainObjEndAPI(&privdom);
3193
    return ret;
3194 3195
}

3196
static int
3197 3198 3199
testDomainSetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int nparams)
3200
{
3201
    return testDomainSetSchedulerParametersFlags(domain, params, nparams, 0);
3202 3203
}

3204 3205
static int testDomainBlockStats(virDomainPtr domain,
                                const char *path,
3206
                                virDomainBlockStatsPtr stats)
3207 3208 3209 3210
{
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
3211
    int ret = -1;
3212

3213 3214 3215 3216 3217 3218
    if (!*path) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("summary statistics are not supported yet"));
        return ret;
    }

3219 3220
    if (!(privdom = testDomObjFromDomain(domain)))
        return ret;
3221

3222
    if (virDomainObjCheckActive(privdom) < 0)
3223 3224
        goto error;

3225
    if (virDomainDiskIndexByName(privdom->def, path, false) < 0) {
3226 3227
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path: %s"), path);
3228 3229 3230 3231
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
3232
        virReportSystemError(errno,
3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245
                             "%s", _("getting time of day"));
        goto error;
    }

    /* No significance to these numbers, just enough to mix it up*/
    statbase = (tv.tv_sec * 1000UL * 1000UL) + tv.tv_usec;
    stats->rd_req = statbase / 10;
    stats->rd_bytes = statbase / 20;
    stats->wr_req = statbase / 30;
    stats->wr_bytes = statbase / 40;
    stats->errs = tv.tv_sec / 2;

    ret = 0;
3246
 error:
3247
    virDomainObjEndAPI(&privdom);
3248 3249 3250
    return ret;
}

3251 3252 3253 3254
static int
testDomainInterfaceStats(virDomainPtr domain,
                         const char *device,
                         virDomainInterfaceStatsPtr stats)
3255 3256 3257 3258
{
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
M
Michal Privoznik 已提交
3259 3260 3261
    virDomainNetDefPtr net = NULL;
    int ret = -1;

3262

3263 3264
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3265

3266
    if (virDomainObjCheckActive(privdom) < 0)
3267 3268
        goto error;

3269
    if (!(net = virDomainNetFind(privdom->def, device)))
3270 3271 3272
        goto error;

    if (gettimeofday(&tv, NULL) < 0) {
3273
        virReportSystemError(errno,
3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289
                             "%s", _("getting time of day"));
        goto error;
    }

    /* No significance to these numbers, just enough to mix it up*/
    statbase = (tv.tv_sec * 1000UL * 1000UL) + tv.tv_usec;
    stats->rx_bytes = statbase / 10;
    stats->rx_packets = statbase / 100;
    stats->rx_errs = tv.tv_sec / 1;
    stats->rx_drop = tv.tv_sec / 2;
    stats->tx_bytes = statbase / 20;
    stats->tx_packets = statbase / 110;
    stats->tx_errs = tv.tv_sec / 3;
    stats->tx_drop = tv.tv_sec / 4;

    ret = 0;
3290
 error:
3291
    virDomainObjEndAPI(&privdom);
3292 3293 3294
    return ret;
}

3295

3296 3297 3298 3299
static virNetworkObjPtr
testNetworkObjFindByUUID(testDriverPtr privconn,
                         const unsigned char *uuid)
{
3300
    virNetworkObjPtr obj;
3301 3302
    char uuidstr[VIR_UUID_STRING_BUFLEN];

3303
    if (!(obj = virNetworkObjFindByUUID(privconn->networks, uuid))) {
3304 3305 3306 3307 3308 3309
        virUUIDFormat(uuid, uuidstr);
        virReportError(VIR_ERR_NO_NETWORK,
                       _("no network with matching uuid '%s'"),
                       uuidstr);
    }

3310
    return obj;
3311 3312 3313
}


3314 3315 3316
static virNetworkPtr
testNetworkLookupByUUID(virConnectPtr conn,
                        const unsigned char *uuid)
3317
{
3318
    testDriverPtr privconn = conn->privateData;
3319
    virNetworkObjPtr obj;
3320
    virNetworkDefPtr def;
3321
    virNetworkPtr net = NULL;
3322

3323
    if (!(obj = testNetworkObjFindByUUID(privconn, uuid)))
3324
        goto cleanup;
3325
    def = virNetworkObjGetDef(obj);
3326

3327
    net = virGetNetwork(conn, def->name, def->uuid);
3328

3329
 cleanup:
3330 3331
    virNetworkObjEndAPI(&obj);
    return net;
3332
}
3333

3334 3335 3336 3337 3338

static virNetworkObjPtr
testNetworkObjFindByName(testDriverPtr privconn,
                         const char *name)
{
3339
    virNetworkObjPtr obj;
3340

3341
    if (!(obj = virNetworkObjFindByName(privconn->networks, name)))
3342 3343 3344 3345
        virReportError(VIR_ERR_NO_NETWORK,
                       _("no network with matching name '%s'"),
                       name);

3346
    return obj;
3347 3348 3349
}


3350 3351 3352
static virNetworkPtr
testNetworkLookupByName(virConnectPtr conn,
                        const char *name)
3353
{
3354
    testDriverPtr privconn = conn->privateData;
3355
    virNetworkObjPtr obj;
3356
    virNetworkDefPtr def;
3357
    virNetworkPtr net = NULL;
3358

3359
    if (!(obj = testNetworkObjFindByName(privconn, name)))
3360
        goto cleanup;
3361
    def = virNetworkObjGetDef(obj);
3362

3363
    net = virGetNetwork(conn, def->name, def->uuid);
3364

3365
 cleanup:
3366 3367
    virNetworkObjEndAPI(&obj);
    return net;
3368 3369 3370
}


3371 3372
static int
testConnectNumOfNetworks(virConnectPtr conn)
3373
{
3374
    testDriverPtr privconn = conn->privateData;
3375
    int numActive;
3376

3377 3378
    numActive = virNetworkObjListNumOfNetworks(privconn->networks,
                                               true, NULL, conn);
3379
    return numActive;
3380 3381
}

3382 3383 3384 3385

static int
testConnectListNetworks(virConnectPtr conn,
                        char **const names,
3386
                        int maxnames)
3387
{
3388
    testDriverPtr privconn = conn->privateData;
3389
    int n;
3390

3391
    n = virNetworkObjListGetNames(privconn->networks,
3392
                                  true, names, maxnames, NULL, conn);
3393
    return n;
3394 3395
}

3396 3397 3398

static int
testConnectNumOfDefinedNetworks(virConnectPtr conn)
3399
{
3400
    testDriverPtr privconn = conn->privateData;
3401
    int numInactive;
3402

3403 3404
    numInactive = virNetworkObjListNumOfNetworks(privconn->networks,
                                                 false, NULL, conn);
3405
    return numInactive;
3406 3407
}

3408 3409 3410 3411

static int
testConnectListDefinedNetworks(virConnectPtr conn,
                               char **const names,
3412
                               int maxnames)
3413
{
3414
    testDriverPtr privconn = conn->privateData;
3415
    int n;
3416

3417
    n = virNetworkObjListGetNames(privconn->networks,
3418
                                  false, names, maxnames, NULL, conn);
3419
    return n;
3420 3421
}

3422

3423
static int
3424
testConnectListAllNetworks(virConnectPtr conn,
3425 3426 3427
                           virNetworkPtr **nets,
                           unsigned int flags)
{
3428
    testDriverPtr privconn = conn->privateData;
3429 3430 3431

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

3432
    return virNetworkObjListExport(conn, privconn->networks, nets, NULL, flags);
3433
}
3434

3435 3436 3437

static int
testNetworkIsActive(virNetworkPtr net)
3438
{
3439
    testDriverPtr privconn = net->conn->privateData;
3440 3441 3442
    virNetworkObjPtr obj;
    int ret = -1;

3443
    if (!(obj = testNetworkObjFindByUUID(privconn, net->uuid)))
3444
        goto cleanup;
3445

3446 3447
    ret = virNetworkObjIsActive(obj);

3448
 cleanup:
3449
    virNetworkObjEndAPI(&obj);
3450 3451 3452
    return ret;
}

3453 3454 3455

static int
testNetworkIsPersistent(virNetworkPtr net)
3456
{
3457
    testDriverPtr privconn = net->conn->privateData;
3458 3459 3460
    virNetworkObjPtr obj;
    int ret = -1;

3461
    if (!(obj = testNetworkObjFindByUUID(privconn, net->uuid)))
3462
        goto cleanup;
3463

3464
    ret = virNetworkObjIsPersistent(obj);
3465

3466
 cleanup:
3467
    virNetworkObjEndAPI(&obj);
3468 3469 3470 3471
    return ret;
}


3472 3473
static virNetworkPtr
testNetworkCreateXML(virConnectPtr conn, const char *xml)
3474
{
3475
    testDriverPtr privconn = conn->privateData;
3476
    virNetworkDefPtr newDef;
3477
    virNetworkObjPtr obj = NULL;
3478
    virNetworkDefPtr def;
3479
    virNetworkPtr net = NULL;
3480
    virObjectEventPtr event = NULL;
3481

3482
    if ((newDef = virNetworkDefParseString(xml)) == NULL)
3483
        goto cleanup;
3484

3485
    if (!(obj = virNetworkObjAssignDef(privconn->networks, newDef,
3486 3487
                                       VIR_NETWORK_OBJ_LIST_ADD_LIVE |
                                       VIR_NETWORK_OBJ_LIST_ADD_CHECK_LIVE)))
3488
        goto cleanup;
3489 3490
    newDef = NULL;
    def = virNetworkObjGetDef(obj);
3491
    virNetworkObjSetActive(obj, true);
3492

3493
    event = virNetworkEventLifecycleNew(def->name, def->uuid,
3494 3495
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3496

3497
    net = virGetNetwork(conn, def->name, def->uuid);
3498

3499
 cleanup:
3500
    virNetworkDefFree(newDef);
3501
    testObjectEventQueue(privconn, event);
3502 3503
    virNetworkObjEndAPI(&obj);
    return net;
3504 3505
}

3506 3507 3508 3509

static virNetworkPtr
testNetworkDefineXML(virConnectPtr conn,
                     const char *xml)
3510
{
3511
    testDriverPtr privconn = conn->privateData;
3512
    virNetworkDefPtr newDef;
3513
    virNetworkObjPtr obj = NULL;
3514
    virNetworkDefPtr def;
3515
    virNetworkPtr net = NULL;
3516
    virObjectEventPtr event = NULL;
3517

3518
    if ((newDef = virNetworkDefParseString(xml)) == NULL)
3519
        goto cleanup;
3520

3521
    if (!(obj = virNetworkObjAssignDef(privconn->networks, newDef, 0)))
3522
        goto cleanup;
3523 3524
    newDef = NULL;
    def = virNetworkObjGetDef(obj);
3525

3526
    event = virNetworkEventLifecycleNew(def->name, def->uuid,
3527 3528
                                        VIR_NETWORK_EVENT_DEFINED,
                                        0);
3529

3530
    net = virGetNetwork(conn, def->name, def->uuid);
3531

3532
 cleanup:
3533
    virNetworkDefFree(newDef);
3534
    testObjectEventQueue(privconn, event);
3535 3536
    virNetworkObjEndAPI(&obj);
    return net;
3537 3538
}

3539 3540

static int
3541
testNetworkUndefine(virNetworkPtr net)
3542
{
3543 3544
    testDriverPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
3545
    int ret = -1;
3546
    virObjectEventPtr event = NULL;
3547

3548
    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
3549
        goto cleanup;
3550

3551
    if (virNetworkObjIsActive(obj)) {
3552
        virReportError(VIR_ERR_OPERATION_INVALID,
3553
                       _("Network '%s' is still running"), net->name);
3554
        goto cleanup;
3555 3556
    }

3557
    event = virNetworkEventLifecycleNew(net->name, net->uuid,
3558 3559
                                        VIR_NETWORK_EVENT_UNDEFINED,
                                        0);
3560

3561
    virNetworkObjRemoveInactive(privconn->networks, obj);
3562
    ret = 0;
3563

3564
 cleanup:
3565
    testObjectEventQueue(privconn, event);
3566
    virNetworkObjEndAPI(&obj);
3567
    return ret;
3568 3569
}

3570

3571 3572 3573 3574 3575 3576 3577 3578
static int
testNetworkUpdate(virNetworkPtr net,
                  unsigned int command,
                  unsigned int section,
                  int parentIndex,
                  const char *xml,
                  unsigned int flags)
{
3579
    testDriverPtr privconn = net->conn->privateData;
3580
    virNetworkObjPtr obj = NULL;
3581 3582 3583 3584 3585 3586
    int isActive, ret = -1;

    virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE |
                  VIR_NETWORK_UPDATE_AFFECT_CONFIG,
                  -1);

3587
    if (!(obj = testNetworkObjFindByUUID(privconn, net->uuid)))
3588 3589 3590 3591 3592
        goto cleanup;

    /* VIR_NETWORK_UPDATE_AFFECT_CURRENT means "change LIVE if network
     * is active, else change CONFIG
    */
3593
    isActive = virNetworkObjIsActive(obj);
3594 3595 3596 3597 3598 3599 3600 3601 3602 3603
    if ((flags & (VIR_NETWORK_UPDATE_AFFECT_LIVE
                   | VIR_NETWORK_UPDATE_AFFECT_CONFIG)) ==
        VIR_NETWORK_UPDATE_AFFECT_CURRENT) {
        if (isActive)
            flags |= VIR_NETWORK_UPDATE_AFFECT_LIVE;
        else
            flags |= VIR_NETWORK_UPDATE_AFFECT_CONFIG;
    }

    /* update the network config in memory/on disk */
3604
    if (virNetworkObjUpdate(obj, command, section, parentIndex, xml, flags) < 0)
3605 3606 3607
       goto cleanup;

    ret = 0;
3608
 cleanup:
3609
    virNetworkObjEndAPI(&obj);
3610 3611 3612
    return ret;
}

3613 3614

static int
3615
testNetworkCreate(virNetworkPtr net)
3616
{
3617 3618
    testDriverPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
3619
    virNetworkDefPtr def;
3620
    int ret = -1;
3621
    virObjectEventPtr event = NULL;
3622

3623
    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
3624
        goto cleanup;
3625
    def = virNetworkObjGetDef(obj);
3626

3627
    if (virNetworkObjIsActive(obj)) {
3628
        virReportError(VIR_ERR_OPERATION_INVALID,
3629
                       _("Network '%s' is already running"), net->name);
3630
        goto cleanup;
3631 3632
    }

3633
    virNetworkObjSetActive(obj, true);
3634
    event = virNetworkEventLifecycleNew(def->name, def->uuid,
3635 3636
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3637
    ret = 0;
3638

3639
 cleanup:
3640
    testObjectEventQueue(privconn, event);
3641
    virNetworkObjEndAPI(&obj);
3642
    return ret;
3643 3644
}

3645 3646

static int
3647
testNetworkDestroy(virNetworkPtr net)
3648
{
3649 3650
    testDriverPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
3651
    virNetworkDefPtr def;
3652
    int ret = -1;
3653
    virObjectEventPtr event = NULL;
3654

3655
    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
3656
        goto cleanup;
3657
    def = virNetworkObjGetDef(obj);
3658

3659
    virNetworkObjSetActive(obj, false);
3660
    event = virNetworkEventLifecycleNew(def->name, def->uuid,
3661 3662
                                        VIR_NETWORK_EVENT_STOPPED,
                                        0);
3663
    if (!virNetworkObjIsPersistent(obj))
3664
        virNetworkObjRemoveInactive(privconn->networks, obj);
3665

3666 3667
    ret = 0;

3668
 cleanup:
3669
    testObjectEventQueue(privconn, event);
3670
    virNetworkObjEndAPI(&obj);
3671
    return ret;
3672 3673
}

3674 3675

static char *
3676
testNetworkGetXMLDesc(virNetworkPtr net,
3677
                      unsigned int flags)
3678
{
3679 3680
    testDriverPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
3681
    char *ret = NULL;
3682

E
Eric Blake 已提交
3683 3684
    virCheckFlags(0, NULL);

3685
    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
3686
        goto cleanup;
3687

3688
    ret = virNetworkDefFormat(virNetworkObjGetDef(obj), flags);
3689

3690
 cleanup:
3691
    virNetworkObjEndAPI(&obj);
3692
    return ret;
3693 3694
}

3695 3696

static char *
3697
testNetworkGetBridgeName(virNetworkPtr net)
3698
{
3699
    testDriverPtr privconn = net->conn->privateData;
3700
    char *bridge = NULL;
3701
    virNetworkObjPtr obj;
3702
    virNetworkDefPtr def;
3703

3704
    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
3705
        goto cleanup;
3706
    def = virNetworkObjGetDef(obj);
3707

3708
    if (!(def->bridge)) {
3709 3710
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("network '%s' does not have a bridge name."),
3711
                       def->name);
3712 3713 3714
        goto cleanup;
    }

3715
    ignore_value(VIR_STRDUP(bridge, def->bridge));
3716

3717
 cleanup:
3718
    virNetworkObjEndAPI(&obj);
3719 3720 3721
    return bridge;
}

3722 3723

static int
3724
testNetworkGetAutostart(virNetworkPtr net,
3725
                        int *autostart)
3726
{
3727 3728
    testDriverPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
3729
    int ret = -1;
3730

3731
    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
3732
        goto cleanup;
3733

3734
    *autostart = virNetworkObjIsAutostart(obj) ? 1 : 0;
3735 3736
    ret = 0;

3737
 cleanup:
3738
    virNetworkObjEndAPI(&obj);
3739
    return ret;
3740 3741
}

3742 3743

static int
3744
testNetworkSetAutostart(virNetworkPtr net,
3745
                        int autostart)
3746
{
3747 3748
    testDriverPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
3749
    bool new_autostart = (autostart != 0);
3750
    int ret = -1;
3751

3752
    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
3753
        goto cleanup;
3754

3755 3756
    virNetworkObjSetAutostart(obj, new_autostart);

3757 3758
    ret = 0;

3759
 cleanup:
3760
    virNetworkObjEndAPI(&obj);
3761
    return ret;
3762
}
3763

C
Cole Robinson 已提交
3764

L
Laine Stump 已提交
3765 3766 3767 3768 3769
/*
 * Physical host interface routines
 */


3770 3771 3772 3773
static virInterfaceObjPtr
testInterfaceObjFindByName(testDriverPtr privconn,
                           const char *name)
{
3774
    virInterfaceObjPtr obj;
3775 3776

    testDriverLock(privconn);
3777
    obj = virInterfaceObjListFindByName(privconn->ifaces, name);
3778 3779
    testDriverUnlock(privconn);

3780
    if (!obj)
3781 3782 3783 3784
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("no interface with matching name '%s'"),
                       name);

3785
    return obj;
3786 3787 3788
}


3789 3790
static int
testConnectNumOfInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3791
{
3792
    testDriverPtr privconn = conn->privateData;
3793
    int ninterfaces;
L
Laine Stump 已提交
3794 3795

    testDriverLock(privconn);
3796
    ninterfaces = virInterfaceObjListNumOfInterfaces(privconn->ifaces, true);
L
Laine Stump 已提交
3797
    testDriverUnlock(privconn);
3798
    return ninterfaces;
L
Laine Stump 已提交
3799 3800
}

3801 3802 3803 3804 3805

static int
testConnectListInterfaces(virConnectPtr conn,
                          char **const names,
                          int maxnames)
L
Laine Stump 已提交
3806
{
3807
    testDriverPtr privconn = conn->privateData;
3808
    int nnames;
L
Laine Stump 已提交
3809 3810

    testDriverLock(privconn);
3811 3812
    nnames = virInterfaceObjListGetNames(privconn->ifaces, true,
                                         names, maxnames);
L
Laine Stump 已提交
3813 3814
    testDriverUnlock(privconn);

3815
    return nnames;
L
Laine Stump 已提交
3816 3817
}

3818 3819 3820

static int
testConnectNumOfDefinedInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3821
{
3822
    testDriverPtr privconn = conn->privateData;
3823
    int ninterfaces;
L
Laine Stump 已提交
3824 3825

    testDriverLock(privconn);
3826
    ninterfaces = virInterfaceObjListNumOfInterfaces(privconn->ifaces, false);
L
Laine Stump 已提交
3827
    testDriverUnlock(privconn);
3828
    return ninterfaces;
L
Laine Stump 已提交
3829 3830
}

3831 3832 3833 3834 3835

static int
testConnectListDefinedInterfaces(virConnectPtr conn,
                                 char **const names,
                                 int maxnames)
L
Laine Stump 已提交
3836
{
3837
    testDriverPtr privconn = conn->privateData;
3838
    int nnames;
L
Laine Stump 已提交
3839 3840

    testDriverLock(privconn);
3841 3842
    nnames = virInterfaceObjListGetNames(privconn->ifaces, false,
                                         names, maxnames);
L
Laine Stump 已提交
3843 3844
    testDriverUnlock(privconn);

3845
    return nnames;
L
Laine Stump 已提交
3846 3847
}

3848 3849 3850 3851

static virInterfacePtr
testInterfaceLookupByName(virConnectPtr conn,
                          const char *name)
L
Laine Stump 已提交
3852
{
3853
    testDriverPtr privconn = conn->privateData;
3854
    virInterfaceObjPtr obj;
3855
    virInterfaceDefPtr def;
L
Laine Stump 已提交
3856 3857
    virInterfacePtr ret = NULL;

3858
    if (!(obj = testInterfaceObjFindByName(privconn, name)))
3859
        return NULL;
3860
    def = virInterfaceObjGetDef(obj);
L
Laine Stump 已提交
3861

3862
    ret = virGetInterface(conn, def->name, def->mac);
L
Laine Stump 已提交
3863

3864
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
3865 3866 3867
    return ret;
}

3868 3869 3870 3871

static virInterfacePtr
testInterfaceLookupByMACString(virConnectPtr conn,
                               const char *mac)
L
Laine Stump 已提交
3872
{
3873
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3874
    int ifacect;
3875
    char *ifacenames[] = { NULL, NULL };
L
Laine Stump 已提交
3876 3877 3878
    virInterfacePtr ret = NULL;

    testDriverLock(privconn);
3879 3880
    ifacect = virInterfaceObjListFindByMACString(privconn->ifaces, mac,
                                                 ifacenames, 2);
L
Laine Stump 已提交
3881 3882 3883
    testDriverUnlock(privconn);

    if (ifacect == 0) {
3884 3885
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("no interface with matching mac '%s'"), mac);
L
Laine Stump 已提交
3886 3887 3888 3889
        goto cleanup;
    }

    if (ifacect > 1) {
3890
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3891 3892 3893
        goto cleanup;
    }

3894
    ret = virGetInterface(conn, ifacenames[0], mac);
L
Laine Stump 已提交
3895

3896
 cleanup:
3897 3898
    VIR_FREE(ifacenames[0]);
    VIR_FREE(ifacenames[1]);
L
Laine Stump 已提交
3899 3900 3901
    return ret;
}

3902 3903 3904

static int
testInterfaceIsActive(virInterfacePtr iface)
3905
{
3906
    testDriverPtr privconn = iface->conn->privateData;
3907 3908 3909
    virInterfaceObjPtr obj;
    int ret = -1;

3910
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3911
        return -1;
3912

3913 3914
    ret = virInterfaceObjIsActive(obj);

3915
    virInterfaceObjEndAPI(&obj);
3916 3917 3918
    return ret;
}

3919 3920 3921 3922

static int
testInterfaceChangeBegin(virConnectPtr conn,
                         unsigned int flags)
3923
{
3924
    testDriverPtr privconn = conn->privateData;
3925 3926
    int ret = -1;

E
Eric Blake 已提交
3927 3928
    virCheckFlags(0, -1);

3929 3930
    testDriverLock(privconn);
    if (privconn->transaction_running) {
3931
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3932
                       _("there is another transaction running."));
3933 3934 3935 3936 3937
        goto cleanup;
    }

    privconn->transaction_running = true;

3938
    if (!(privconn->backupIfaces = virInterfaceObjListClone(privconn->ifaces)))
3939 3940 3941
        goto cleanup;

    ret = 0;
3942
 cleanup:
3943 3944 3945 3946
    testDriverUnlock(privconn);
    return ret;
}

3947 3948 3949 3950

static int
testInterfaceChangeCommit(virConnectPtr conn,
                          unsigned int flags)
3951
{
3952
    testDriverPtr privconn = conn->privateData;
3953 3954
    int ret = -1;

E
Eric Blake 已提交
3955 3956
    virCheckFlags(0, -1);

3957 3958 3959
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3960
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3961 3962
                       _("no transaction running, "
                         "nothing to be committed."));
3963 3964 3965
        goto cleanup;
    }

3966
    virObjectUnref(privconn->backupIfaces);
3967 3968 3969 3970
    privconn->transaction_running = false;

    ret = 0;

3971
 cleanup:
3972 3973 3974 3975 3976
    testDriverUnlock(privconn);

    return ret;
}

3977 3978 3979 3980

static int
testInterfaceChangeRollback(virConnectPtr conn,
                            unsigned int flags)
3981
{
3982
    testDriverPtr privconn = conn->privateData;
3983 3984
    int ret = -1;

E
Eric Blake 已提交
3985 3986
    virCheckFlags(0, -1);

3987 3988 3989
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3990
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3991 3992
                       _("no transaction running, "
                         "nothing to rollback."));
3993 3994 3995
        goto cleanup;
    }

3996
    virObjectUnref(privconn->ifaces);
3997 3998
    privconn->ifaces = privconn->backupIfaces;
    privconn->backupIfaces = NULL;
3999 4000 4001 4002 4003

    privconn->transaction_running = false;

    ret = 0;

4004
 cleanup:
4005 4006 4007
    testDriverUnlock(privconn);
    return ret;
}
4008

4009 4010 4011 4012

static char *
testInterfaceGetXMLDesc(virInterfacePtr iface,
                        unsigned int flags)
L
Laine Stump 已提交
4013
{
4014
    testDriverPtr privconn = iface->conn->privateData;
4015
    virInterfaceObjPtr obj;
4016
    virInterfaceDefPtr def;
L
Laine Stump 已提交
4017 4018
    char *ret = NULL;

E
Eric Blake 已提交
4019 4020
    virCheckFlags(0, NULL);

4021
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
4022
        return NULL;
4023
    def = virInterfaceObjGetDef(obj);
L
Laine Stump 已提交
4024

4025
    ret = virInterfaceDefFormat(def);
L
Laine Stump 已提交
4026

4027
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
4028 4029 4030 4031
    return ret;
}


4032 4033 4034 4035
static virInterfacePtr
testInterfaceDefineXML(virConnectPtr conn,
                       const char *xmlStr,
                       unsigned int flags)
L
Laine Stump 已提交
4036
{
4037
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
4038
    virInterfaceDefPtr def;
4039
    virInterfaceObjPtr obj = NULL;
4040
    virInterfaceDefPtr objdef;
L
Laine Stump 已提交
4041 4042
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
4043 4044
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
4045
    testDriverLock(privconn);
4046
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
4047 4048
        goto cleanup;

4049
    if ((obj = virInterfaceObjListAssignDef(privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
4050 4051
        goto cleanup;
    def = NULL;
4052
    objdef = virInterfaceObjGetDef(obj);
L
Laine Stump 已提交
4053

4054
    ret = virGetInterface(conn, objdef->name, objdef->mac);
L
Laine Stump 已提交
4055

4056
 cleanup:
L
Laine Stump 已提交
4057
    virInterfaceDefFree(def);
4058
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
4059 4060 4061 4062
    testDriverUnlock(privconn);
    return ret;
}

4063 4064 4065

static int
testInterfaceUndefine(virInterfacePtr iface)
L
Laine Stump 已提交
4066
{
4067
    testDriverPtr privconn = iface->conn->privateData;
4068
    virInterfaceObjPtr obj;
L
Laine Stump 已提交
4069

4070
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
4071
        return -1;
L
Laine Stump 已提交
4072

4073
    virInterfaceObjListRemove(privconn->ifaces, obj);
4074
    virObjectUnref(obj);
L
Laine Stump 已提交
4075

4076
    return 0;
L
Laine Stump 已提交
4077 4078
}

4079 4080 4081 4082

static int
testInterfaceCreate(virInterfacePtr iface,
                    unsigned int flags)
L
Laine Stump 已提交
4083
{
4084
    testDriverPtr privconn = iface->conn->privateData;
4085
    virInterfaceObjPtr obj;
L
Laine Stump 已提交
4086 4087
    int ret = -1;

E
Eric Blake 已提交
4088 4089
    virCheckFlags(0, -1);

4090
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
4091
        return -1;
L
Laine Stump 已提交
4092

4093
    if (virInterfaceObjIsActive(obj)) {
4094
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4095 4096 4097
        goto cleanup;
    }

4098
    virInterfaceObjSetActive(obj, true);
L
Laine Stump 已提交
4099 4100
    ret = 0;

4101
 cleanup:
4102
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
4103 4104 4105
    return ret;
}

4106 4107 4108 4109

static int
testInterfaceDestroy(virInterfacePtr iface,
                     unsigned int flags)
L
Laine Stump 已提交
4110
{
4111
    testDriverPtr privconn = iface->conn->privateData;
4112
    virInterfaceObjPtr obj;
L
Laine Stump 已提交
4113 4114
    int ret = -1;

E
Eric Blake 已提交
4115 4116
    virCheckFlags(0, -1);

4117
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
4118
        return -1;
L
Laine Stump 已提交
4119

4120
    if (!virInterfaceObjIsActive(obj)) {
4121
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4122 4123 4124
        goto cleanup;
    }

4125
    virInterfaceObjSetActive(obj, false);
L
Laine Stump 已提交
4126 4127
    ret = 0;

4128
 cleanup:
4129
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
4130 4131 4132 4133 4134
    return ret;
}



C
Cole Robinson 已提交
4135 4136 4137 4138
/*
 * Storage Driver routines
 */

4139
static int
4140
testStoragePoolObjSetDefaults(virStoragePoolObjPtr obj)
4141
{
4142
    char *configFile;
4143
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(obj);
C
Cole Robinson 已提交
4144

4145 4146 4147
    def->capacity = defaultPoolCap;
    def->allocation = defaultPoolAlloc;
    def->available = defaultPoolCap - defaultPoolAlloc;
C
Cole Robinson 已提交
4148

4149 4150 4151 4152 4153
    if (VIR_STRDUP(configFile, "") < 0)
        return -1;

    virStoragePoolObjSetConfigFile(obj, configFile);
    return 0;
C
Cole Robinson 已提交
4154 4155
}

4156

4157 4158 4159 4160
static virStoragePoolObjPtr
testStoragePoolObjFindByName(testDriverPtr privconn,
                             const char *name)
{
4161
    virStoragePoolObjPtr obj;
4162 4163

    testDriverLock(privconn);
4164
    obj = virStoragePoolObjFindByName(privconn->pools, name);
4165 4166
    testDriverUnlock(privconn);

4167
    if (!obj)
4168 4169 4170 4171
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       _("no storage pool with matching name '%s'"),
                       name);

4172
    return obj;
4173 4174 4175
}


4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187
static virStoragePoolObjPtr
testStoragePoolObjFindActiveByName(testDriverPtr privconn,
                                   const char *name)
{
    virStoragePoolObjPtr obj;

    if (!(obj = testStoragePoolObjFindByName(privconn, name)))
        return NULL;

    if (!virStoragePoolObjIsActive(obj)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), name);
4188
        virStoragePoolObjEndAPI(&obj);
4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207
        return NULL;
    }

    return obj;
}


static virStoragePoolObjPtr
testStoragePoolObjFindInactiveByName(testDriverPtr privconn,
                                     const char *name)
{
    virStoragePoolObjPtr obj;

    if (!(obj = testStoragePoolObjFindByName(privconn, name)))
        return NULL;

    if (virStoragePoolObjIsActive(obj)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is active"), name);
4208
        virStoragePoolObjEndAPI(&obj);
4209 4210 4211 4212 4213 4214 4215
        return NULL;
    }

    return obj;
}


4216 4217 4218 4219
static virStoragePoolObjPtr
testStoragePoolObjFindByUUID(testDriverPtr privconn,
                             const unsigned char *uuid)
{
4220
    virStoragePoolObjPtr obj;
4221 4222 4223
    char uuidstr[VIR_UUID_STRING_BUFLEN];

    testDriverLock(privconn);
4224
    obj = virStoragePoolObjFindByUUID(privconn->pools, uuid);
4225 4226
    testDriverUnlock(privconn);

4227
    if (!obj) {
4228 4229 4230 4231 4232 4233
        virUUIDFormat(uuid, uuidstr);
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       _("no storage pool with matching uuid '%s'"),
                       uuidstr);
    }

4234
    return obj;
4235 4236 4237
}


C
Cole Robinson 已提交
4238 4239
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
4240 4241
                            const unsigned char *uuid)
{
4242
    testDriverPtr privconn = conn->privateData;
4243
    virStoragePoolObjPtr obj;
4244
    virStoragePoolDefPtr def;
4245
    virStoragePoolPtr pool = NULL;
C
Cole Robinson 已提交
4246

4247
    if (!(obj = testStoragePoolObjFindByUUID(privconn, uuid)))
4248
        return NULL;
4249
    def = virStoragePoolObjGetDef(obj);
C
Cole Robinson 已提交
4250

4251
    pool = virGetStoragePool(conn, def->name, def->uuid, NULL, NULL);
4252

4253
    virStoragePoolObjEndAPI(&obj);
4254
    return pool;
C
Cole Robinson 已提交
4255 4256
}

4257

C
Cole Robinson 已提交
4258 4259
static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
4260 4261
                            const char *name)
{
4262
    testDriverPtr privconn = conn->privateData;
4263
    virStoragePoolObjPtr obj;
4264
    virStoragePoolDefPtr def;
4265
    virStoragePoolPtr pool = NULL;
C
Cole Robinson 已提交
4266

4267
    if (!(obj = testStoragePoolObjFindByName(privconn, name)))
4268
        return NULL;
4269
    def = virStoragePoolObjGetDef(obj);
C
Cole Robinson 已提交
4270

4271
    pool = virGetStoragePool(conn, def->name, def->uuid, NULL, NULL);
4272

4273
    virStoragePoolObjEndAPI(&obj);
4274
    return pool;
C
Cole Robinson 已提交
4275 4276
}

4277

C
Cole Robinson 已提交
4278
static virStoragePoolPtr
4279 4280
testStoragePoolLookupByVolume(virStorageVolPtr vol)
{
C
Cole Robinson 已提交
4281 4282 4283
    return testStoragePoolLookupByName(vol->conn, vol->pool);
}

4284

C
Cole Robinson 已提交
4285
static int
4286 4287
testConnectNumOfStoragePools(virConnectPtr conn)
{
4288
    testDriverPtr privconn = conn->privateData;
4289
    int numActive = 0;
C
Cole Robinson 已提交
4290

4291
    testDriverLock(privconn);
4292
    numActive = virStoragePoolObjNumOfStoragePools(privconn->pools, conn,
4293
                                                   true, NULL);
4294
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4295 4296 4297 4298

    return numActive;
}

4299

C
Cole Robinson 已提交
4300
static int
4301 4302
testConnectListStoragePools(virConnectPtr conn,
                            char **const names,
4303
                            int maxnames)
4304
{
4305
    testDriverPtr privconn = conn->privateData;
4306
    int n = 0;
C
Cole Robinson 已提交
4307

4308
    testDriverLock(privconn);
4309
    n = virStoragePoolObjGetNames(privconn->pools, conn, true, NULL,
4310
                                  names, maxnames);
4311
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4312 4313 4314 4315

    return n;
}

4316

C
Cole Robinson 已提交
4317
static int
4318 4319
testConnectNumOfDefinedStoragePools(virConnectPtr conn)
{
4320
    testDriverPtr privconn = conn->privateData;
4321
    int numInactive = 0;
C
Cole Robinson 已提交
4322

4323
    testDriverLock(privconn);
4324
    numInactive = virStoragePoolObjNumOfStoragePools(privconn->pools, conn,
4325
                                                     false, NULL);
4326
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4327 4328 4329 4330

    return numInactive;
}

4331

C
Cole Robinson 已提交
4332
static int
4333 4334
testConnectListDefinedStoragePools(virConnectPtr conn,
                                   char **const names,
4335
                                   int maxnames)
4336
{
4337
    testDriverPtr privconn = conn->privateData;
4338
    int n = 0;
C
Cole Robinson 已提交
4339

4340
    testDriverLock(privconn);
4341
    n = virStoragePoolObjGetNames(privconn->pools, conn, false, NULL,
4342
                                  names, maxnames);
4343
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4344 4345 4346 4347

    return n;
}

4348

4349
static int
4350 4351 4352
testConnectListAllStoragePools(virConnectPtr conn,
                               virStoragePoolPtr **pools,
                               unsigned int flags)
4353
{
4354
    testDriverPtr privconn = conn->privateData;
4355 4356 4357 4358 4359
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

    testDriverLock(privconn);
4360
    ret = virStoragePoolObjListExport(conn, privconn->pools, pools,
4361
                                      NULL, flags);
4362 4363 4364 4365
    testDriverUnlock(privconn);

    return ret;
}
C
Cole Robinson 已提交
4366

4367 4368 4369

static int
testStoragePoolIsActive(virStoragePoolPtr pool)
4370
{
4371
    testDriverPtr privconn = pool->conn->privateData;
4372 4373 4374
    virStoragePoolObjPtr obj;
    int ret = -1;

4375
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4376
        goto cleanup;
4377

4378 4379
    ret = virStoragePoolObjIsActive(obj);

4380
 cleanup:
4381
    if (obj)
4382
        virStoragePoolObjEndAPI(&obj);
4383 4384 4385
    return ret;
}

4386 4387 4388

static int
testStoragePoolIsPersistent(virStoragePoolPtr pool)
4389
{
4390
    testDriverPtr privconn = pool->conn->privateData;
4391 4392 4393
    virStoragePoolObjPtr obj;
    int ret = -1;

4394
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4395
        return -1;
4396

4397
    ret = virStoragePoolObjGetConfigFile(obj) ? 1 : 0;
4398

4399
    virStoragePoolObjEndAPI(&obj);
4400 4401 4402 4403
    return ret;
}


C
Cole Robinson 已提交
4404
static int
4405 4406
testStoragePoolCreate(virStoragePoolPtr pool,
                      unsigned int flags)
E
Eric Blake 已提交
4407
{
4408
    testDriverPtr privconn = pool->conn->privateData;
4409
    virStoragePoolObjPtr obj;
4410
    virObjectEventPtr event = NULL;
4411

E
Eric Blake 已提交
4412 4413
    virCheckFlags(0, -1);

4414 4415
    if (!(obj = testStoragePoolObjFindInactiveByName(privconn, pool->name)))
        return -1;
C
Cole Robinson 已提交
4416

4417
    virStoragePoolObjSetActive(obj, true);
4418 4419 4420 4421

    event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid,
                                            VIR_STORAGE_POOL_EVENT_STARTED,
                                            0);
C
Cole Robinson 已提交
4422

4423
    testObjectEventQueue(privconn, event);
4424
    virStoragePoolObjEndAPI(&obj);
4425
    return 0;
C
Cole Robinson 已提交
4426 4427
}

4428

C
Cole Robinson 已提交
4429
static char *
4430 4431 4432 4433
testConnectFindStoragePoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type,
                                  const char *srcSpec,
                                  unsigned int flags)
C
Cole Robinson 已提交
4434
{
4435 4436 4437 4438
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4439 4440
    virCheckFlags(0, NULL);

4441 4442
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4443 4444
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
4445 4446 4447 4448
        goto cleanup;
    }

    if (srcSpec) {
4449
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4450 4451 4452 4453 4454 4455 4456
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
4457
        ignore_value(VIR_STRDUP(ret, defaultPoolSourcesLogicalXML));
4458 4459 4460
        break;

    case VIR_STORAGE_POOL_NETFS:
4461
        if (!source || !source->hosts[0].name) {
4462 4463
            virReportError(VIR_ERR_INVALID_ARG,
                           "%s", _("hostname must be specified for netfs sources"));
4464 4465 4466
            goto cleanup;
        }

4467 4468
        ignore_value(virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                                 source->hosts[0].name));
4469 4470 4471
        break;

    default:
4472 4473
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4474 4475
    }

4476
 cleanup:
4477 4478
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4479 4480 4481
}


4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503
static virNodeDeviceObjPtr
testNodeDeviceMockCreateVport(testDriverPtr driver,
                              const char *wwnn,
                              const char *wwpn);
static int
testCreateVport(testDriverPtr driver,
                const char *wwnn,
                const char *wwpn)
{
    virNodeDeviceObjPtr obj = NULL;
    /* The storage_backend_scsi createVport() will use the input adapter
     * fields parent name, parent_wwnn/parent_wwpn, or parent_fabric_wwn
     * in order to determine whether the provided parent can be used to
     * create a vHBA or will find "an available vport capable" to create
     * a vHBA. In order to do this, it uses the virVHBA* API's which traverse
     * the sysfs looking at various fields (rather than going via nodedev).
     *
     * Since the test environ doesn't have the sysfs for the storage pool
     * test, at least for now use the node device test infrastructure to
     * create the vHBA. In the long run the result is the same. */
    if (!(obj = testNodeDeviceMockCreateVport(driver, wwnn, wwpn)))
        return -1;
4504
    virNodeDeviceObjEndAPI(&obj);
4505 4506 4507 4508 4509

    return 0;
}


C
Cole Robinson 已提交
4510
static virStoragePoolPtr
4511 4512 4513
testStoragePoolCreateXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4514
{
4515
    testDriverPtr privconn = conn->privateData;
4516
    virStoragePoolDefPtr newDef;
4517
    virStoragePoolObjPtr obj = NULL;
4518
    virStoragePoolDefPtr def;
4519
    virStoragePoolPtr pool = NULL;
4520
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
4521

E
Eric Blake 已提交
4522 4523
    virCheckFlags(0, NULL);

4524
    testDriverLock(privconn);
4525
    if (!(newDef = virStoragePoolDefParseString(xml)))
4526
        goto cleanup;
C
Cole Robinson 已提交
4527

4528
    if (virStoragePoolObjIsDuplicate(privconn->pools, newDef, true) < 0)
4529
        goto cleanup;
C
Cole Robinson 已提交
4530

4531
    if (!(obj = virStoragePoolObjAssignDef(privconn->pools, newDef)))
4532
        goto cleanup;
4533 4534
    newDef = NULL;
    def = virStoragePoolObjGetDef(obj);
C
Cole Robinson 已提交
4535

4536
    if (def->source.adapter.type == VIR_STORAGE_ADAPTER_TYPE_FC_HOST) {
4537 4538 4539 4540 4541
        /* In the real code, we'd call virVHBAManageVport followed by
         * find_new_device, but we cannot do that here since we're not
         * mocking udev. The mock routine will copy an existing vHBA and
         * rename a few fields to mock that. */
        if (testCreateVport(privconn,
4542 4543
                            def->source.adapter.data.fchost.wwnn,
                            def->source.adapter.data.fchost.wwpn) < 0) {
4544
            virStoragePoolObjRemove(privconn->pools, obj);
4545
            virObjectUnref(obj);
4546
            obj = NULL;
4547 4548 4549 4550
            goto cleanup;
        }
    }

4551
    if (testStoragePoolObjSetDefaults(obj) == -1) {
4552
        virStoragePoolObjRemove(privconn->pools, obj);
4553
        virObjectUnref(obj);
4554
        obj = NULL;
4555
        goto cleanup;
C
Cole Robinson 已提交
4556
    }
4557 4558 4559 4560

    /* *SetDefaults fills this in for the persistent pools, but this
     * would be a transient pool so remove it; otherwise, the Destroy
     * code will not Remove the pool */
4561
    virStoragePoolObjSetConfigFile(obj, NULL);
4562

4563
    virStoragePoolObjSetActive(obj, true);
C
Cole Robinson 已提交
4564

4565
    event = virStoragePoolEventLifecycleNew(def->name, def->uuid,
4566 4567 4568
                                            VIR_STORAGE_POOL_EVENT_STARTED,
                                            0);

4569
    pool = virGetStoragePool(conn, def->name, def->uuid, NULL, NULL);
4570

4571
 cleanup:
4572
    virStoragePoolDefFree(newDef);
4573
    testObjectEventQueue(privconn, event);
4574
    virStoragePoolObjEndAPI(&obj);
4575
    testDriverUnlock(privconn);
4576
    return pool;
C
Cole Robinson 已提交
4577 4578
}

4579

C
Cole Robinson 已提交
4580
static virStoragePoolPtr
4581 4582 4583
testStoragePoolDefineXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4584
{
4585
    testDriverPtr privconn = conn->privateData;
4586
    virStoragePoolDefPtr newDef;
4587
    virStoragePoolObjPtr obj = NULL;
4588
    virStoragePoolDefPtr def;
4589
    virStoragePoolPtr pool = NULL;
4590
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
4591

E
Eric Blake 已提交
4592 4593
    virCheckFlags(0, NULL);

4594
    testDriverLock(privconn);
4595
    if (!(newDef = virStoragePoolDefParseString(xml)))
4596
        goto cleanup;
C
Cole Robinson 已提交
4597

4598 4599 4600
    newDef->capacity = defaultPoolCap;
    newDef->allocation = defaultPoolAlloc;
    newDef->available = defaultPoolCap - defaultPoolAlloc;
C
Cole Robinson 已提交
4601

4602 4603 4604
    if (virStoragePoolObjIsDuplicate(privconn->pools, newDef, false) < 0)
        goto cleanup;

4605
    if (!(obj = virStoragePoolObjAssignDef(privconn->pools, newDef)))
4606
        goto cleanup;
4607 4608
    newDef = NULL;
    def = virStoragePoolObjGetDef(obj);
C
Cole Robinson 已提交
4609

4610
    event = virStoragePoolEventLifecycleNew(def->name, def->uuid,
4611 4612 4613
                                            VIR_STORAGE_POOL_EVENT_DEFINED,
                                            0);

4614
    if (testStoragePoolObjSetDefaults(obj) == -1) {
4615
        virStoragePoolObjRemove(privconn->pools, obj);
4616
        virObjectUnref(obj);
4617
        obj = NULL;
4618
        goto cleanup;
C
Cole Robinson 已提交
4619 4620
    }

4621
    pool = virGetStoragePool(conn, def->name, def->uuid, NULL, NULL);
4622

4623
 cleanup:
4624
    virStoragePoolDefFree(newDef);
4625
    testObjectEventQueue(privconn, event);
4626
    virStoragePoolObjEndAPI(&obj);
4627
    testDriverUnlock(privconn);
4628
    return pool;
C
Cole Robinson 已提交
4629 4630
}

4631

C
Cole Robinson 已提交
4632
static int
4633 4634
testStoragePoolUndefine(virStoragePoolPtr pool)
{
4635
    testDriverPtr privconn = pool->conn->privateData;
4636
    virStoragePoolObjPtr obj;
4637
    virObjectEventPtr event = NULL;
4638

4639 4640
    if (!(obj = testStoragePoolObjFindInactiveByName(privconn, pool->name)))
        return -1;
C
Cole Robinson 已提交
4641

4642 4643 4644 4645
    event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid,
                                            VIR_STORAGE_POOL_EVENT_UNDEFINED,
                                            0);

4646
    virStoragePoolObjRemove(privconn->pools, obj);
4647
    virObjectUnref(obj);
C
Cole Robinson 已提交
4648

4649
    testObjectEventQueue(privconn, event);
4650
    return 0;
C
Cole Robinson 已提交
4651 4652
}

4653

C
Cole Robinson 已提交
4654
static int
4655
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4656 4657
                     unsigned int flags)
{
4658
    testDriverPtr privconn = pool->conn->privateData;
4659
    virStoragePoolObjPtr obj;
4660
    virObjectEventPtr event = NULL;
4661

E
Eric Blake 已提交
4662 4663
    virCheckFlags(0, -1);

4664 4665
    if (!(obj = testStoragePoolObjFindInactiveByName(privconn, pool->name)))
        return -1;
C
Cole Robinson 已提交
4666

4667 4668 4669 4670
    event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid,
                                            VIR_STORAGE_POOL_EVENT_CREATED,
                                            0);

4671
    virStoragePoolObjEndAPI(&obj);
4672 4673

    testObjectEventQueue(privconn, event);
4674
    return 0;
C
Cole Robinson 已提交
4675 4676 4677
}


4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692
static int
testDestroyVport(testDriverPtr privconn,
                 const char *wwnn ATTRIBUTE_UNUSED,
                 const char *wwpn ATTRIBUTE_UNUSED)
{
    virNodeDeviceObjPtr obj = NULL;
    virObjectEventPtr event = NULL;

    /* NB: Cannot use virVHBAGetHostByWWN (yet) like the storage_backend_scsi
     * deleteVport() helper since that traverses the file system looking for
     * the wwnn/wwpn. So our choice short term is to cheat and use the name
     * (scsi_host12) we know was created.
     *
     * Reaching across the boundaries of space and time into the
     * Node Device in order to remove */
4693 4694
    if (!(obj = virNodeDeviceObjListFindByName(privconn->devs,
                                               "scsi_host12"))) {
4695 4696
        virReportError(VIR_ERR_NO_NODE_DEVICE, "%s",
                       _("no node device with matching name 'scsi_host12'"));
4697
        return -1;
4698 4699 4700 4701 4702 4703
    }

    event = virNodeDeviceEventLifecycleNew("scsi_host12",
                                           VIR_NODE_DEVICE_EVENT_DELETED,
                                           0);

4704
    virNodeDeviceObjListRemove(privconn->devs, obj);
4705
    virObjectUnref(obj);
4706 4707

    testObjectEventQueue(privconn, event);
4708
    return 0;
4709 4710 4711
}


C
Cole Robinson 已提交
4712
static int
4713 4714
testStoragePoolDestroy(virStoragePoolPtr pool)
{
4715
    testDriverPtr privconn = pool->conn->privateData;
4716
    virStoragePoolObjPtr obj;
4717
    virStoragePoolDefPtr def;
4718
    int ret = -1;
4719
    virObjectEventPtr event = NULL;
4720

4721
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name)))
4722
        return -1;
4723
    def = virStoragePoolObjGetDef(obj);
4724

4725
    virStoragePoolObjSetActive(obj, false);
4726

4727
    if (def->source.adapter.type == VIR_STORAGE_ADAPTER_TYPE_FC_HOST) {
4728
        if (testDestroyVport(privconn,
4729 4730
                             def->source.adapter.data.fchost.wwnn,
                             def->source.adapter.data.fchost.wwpn) < 0)
4731 4732 4733
            goto cleanup;
    }

4734 4735
    event = virStoragePoolEventLifecycleNew(def->name,
                                            def->uuid,
4736 4737
                                            VIR_STORAGE_POOL_EVENT_STOPPED,
                                            0);
C
Cole Robinson 已提交
4738

4739
    if (!(virStoragePoolObjGetConfigFile(obj))) {
4740
        virStoragePoolObjRemove(privconn->pools, obj);
4741
        virObjectUnref(obj);
4742
        obj = NULL;
4743
    }
4744
    ret = 0;
C
Cole Robinson 已提交
4745

4746
 cleanup:
4747
    testObjectEventQueue(privconn, event);
4748
    virStoragePoolObjEndAPI(&obj);
4749
    return ret;
C
Cole Robinson 已提交
4750 4751 4752 4753
}


static int
4754
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4755 4756
                      unsigned int flags)
{
4757
    testDriverPtr privconn = pool->conn->privateData;
4758
    virStoragePoolObjPtr obj;
4759
    virObjectEventPtr event = NULL;
4760

E
Eric Blake 已提交
4761 4762
    virCheckFlags(0, -1);

4763 4764
    if (!(obj = testStoragePoolObjFindInactiveByName(privconn, pool->name)))
        return -1;
C
Cole Robinson 已提交
4765

4766 4767 4768 4769 4770 4771
    event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid,
                                            VIR_STORAGE_POOL_EVENT_DELETED,
                                            0);

    testObjectEventQueue(privconn, event);

4772
    virStoragePoolObjEndAPI(&obj);
4773
    return 0;
C
Cole Robinson 已提交
4774 4775 4776 4777
}


static int
4778
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4779 4780
                       unsigned int flags)
{
4781
    testDriverPtr privconn = pool->conn->privateData;
4782
    virStoragePoolObjPtr obj;
4783
    virObjectEventPtr event = NULL;
4784

E
Eric Blake 已提交
4785 4786
    virCheckFlags(0, -1);

4787 4788
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name)))
        return -1;
4789

4790
    event = virStoragePoolEventRefreshNew(pool->name, pool->uuid);
C
Cole Robinson 已提交
4791

4792
    testObjectEventQueue(privconn, event);
4793
    virStoragePoolObjEndAPI(&obj);
4794
    return 0;
C
Cole Robinson 已提交
4795 4796 4797 4798
}


static int
4799
testStoragePoolGetInfo(virStoragePoolPtr pool,
4800 4801
                       virStoragePoolInfoPtr info)
{
4802
    testDriverPtr privconn = pool->conn->privateData;
4803
    virStoragePoolObjPtr obj;
4804
    virStoragePoolDefPtr def;
4805

4806
    if (!(obj = testStoragePoolObjFindByName(privconn, pool->name)))
4807
        return -1;
4808
    def = virStoragePoolObjGetDef(obj);
C
Cole Robinson 已提交
4809 4810

    memset(info, 0, sizeof(virStoragePoolInfo));
4811
    if (virStoragePoolObjIsActive(obj))
C
Cole Robinson 已提交
4812 4813 4814
        info->state = VIR_STORAGE_POOL_RUNNING;
    else
        info->state = VIR_STORAGE_POOL_INACTIVE;
4815 4816 4817
    info->capacity = def->capacity;
    info->allocation = def->allocation;
    info->available = def->available;
C
Cole Robinson 已提交
4818

4819
    virStoragePoolObjEndAPI(&obj);
4820
    return 0;
C
Cole Robinson 已提交
4821 4822
}

4823

C
Cole Robinson 已提交
4824
static char *
4825
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4826 4827
                          unsigned int flags)
{
4828
    testDriverPtr privconn = pool->conn->privateData;
4829
    virStoragePoolObjPtr obj;
4830
    char *ret = NULL;
4831

E
Eric Blake 已提交
4832 4833
    virCheckFlags(0, NULL);

4834
    if (!(obj = testStoragePoolObjFindByName(privconn, pool->name)))
4835
        return NULL;
4836

4837
    ret = virStoragePoolDefFormat(virStoragePoolObjGetDef(obj));
4838

4839
    virStoragePoolObjEndAPI(&obj);
4840
    return ret;
C
Cole Robinson 已提交
4841 4842
}

4843

C
Cole Robinson 已提交
4844
static int
4845
testStoragePoolGetAutostart(virStoragePoolPtr pool,
4846 4847
                            int *autostart)
{
4848
    testDriverPtr privconn = pool->conn->privateData;
4849
    virStoragePoolObjPtr obj;
4850

4851
    if (!(obj = testStoragePoolObjFindByName(privconn, pool->name)))
4852
        return -1;
C
Cole Robinson 已提交
4853

4854
    if (!virStoragePoolObjGetConfigFile(obj))
C
Cole Robinson 已提交
4855
        *autostart = 0;
4856
    else
4857
        *autostart = virStoragePoolObjIsAutostart(obj) ? 1 : 0;
C
Cole Robinson 已提交
4858

4859
    virStoragePoolObjEndAPI(&obj);
4860
    return 0;
C
Cole Robinson 已提交
4861 4862
}

4863

C
Cole Robinson 已提交
4864
static int
4865
testStoragePoolSetAutostart(virStoragePoolPtr pool,
4866 4867
                            int autostart)
{
4868
    testDriverPtr privconn = pool->conn->privateData;
4869
    virStoragePoolObjPtr obj;
4870
    bool new_autostart = (autostart != 0);
4871
    int ret = -1;
4872

4873
    if (!(obj = testStoragePoolObjFindByName(privconn, pool->name)))
4874
        return -1;
C
Cole Robinson 已提交
4875

4876
    if (!virStoragePoolObjGetConfigFile(obj)) {
4877 4878
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
4879
        goto cleanup;
C
Cole Robinson 已提交
4880 4881
    }

4882
    virStoragePoolObjSetAutostart(obj, new_autostart);
4883 4884
    ret = 0;

4885
 cleanup:
4886
    virStoragePoolObjEndAPI(&obj);
4887
    return ret;
C
Cole Robinson 已提交
4888 4889 4890 4891
}


static int
4892 4893
testStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
4894
    testDriverPtr privconn = pool->conn->privateData;
4895
    virStoragePoolObjPtr obj;
4896
    int ret = -1;
4897

4898 4899
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name)))
        return -1;
C
Cole Robinson 已提交
4900

4901
    ret = virStoragePoolObjNumOfVolumes(obj, pool->conn, NULL);
4902

4903
    virStoragePoolObjEndAPI(&obj);
4904
    return ret;
C
Cole Robinson 已提交
4905 4906
}

4907

C
Cole Robinson 已提交
4908
static int
4909
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4910
                           char **const names,
4911 4912
                           int maxnames)
{
4913
    testDriverPtr privconn = pool->conn->privateData;
4914
    virStoragePoolObjPtr obj;
4915
    int n = -1;
4916

4917
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name)))
4918
        return -1;
4919

4920
    n = virStoragePoolObjVolumeGetNames(obj, pool->conn, NULL, names, maxnames);
C
Cole Robinson 已提交
4921

4922
    virStoragePoolObjEndAPI(&obj);
C
Cole Robinson 已提交
4923 4924 4925
    return n;
}

4926

4927
static int
4928
testStoragePoolListAllVolumes(virStoragePoolPtr pool,
4929
                              virStorageVolPtr **vols,
4930 4931
                              unsigned int flags)
{
4932 4933
    testDriverPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr obj;
4934 4935 4936 4937
    int ret = -1;

    virCheckFlags(0, -1);

4938
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4939
        return -1;
4940

4941
    if (!virStoragePoolObjIsActive(obj)) {
4942 4943 4944 4945 4946
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("storage pool is not active"));
        goto cleanup;
    }

4947
    ret = virStoragePoolObjVolumeListExport(pool->conn, obj, vols, NULL);
4948 4949

 cleanup:
4950
    virStoragePoolObjEndAPI(&obj);
4951 4952 4953

    return ret;
}
C
Cole Robinson 已提交
4954

4955

4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970
static virStorageVolDefPtr
testStorageVolDefFindByName(virStoragePoolObjPtr obj,
                            const char *name)
{
    virStorageVolDefPtr privvol;

    if (!(privvol = virStorageVolDefFindByName(obj, name))) {
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"), name);
    }

    return privvol;
}


C
Cole Robinson 已提交
4971
static virStorageVolPtr
4972
testStorageVolLookupByName(virStoragePoolPtr pool,
4973
                           const char *name)
4974
{
4975
    testDriverPtr privconn = pool->conn->privateData;
4976
    virStoragePoolObjPtr obj;
4977
    virStoragePoolDefPtr def;
4978
    virStorageVolDefPtr privvol;
4979
    virStorageVolPtr ret = NULL;
4980

4981 4982
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name)))
        return NULL;
4983
    def = virStoragePoolObjGetDef(obj);
4984

4985
    if (!(privvol = testStorageVolDefFindByName(obj, name)))
4986
        goto cleanup;
C
Cole Robinson 已提交
4987

4988
    ret = virGetStorageVol(pool->conn, def->name,
4989 4990
                           privvol->name, privvol->key,
                           NULL, NULL);
4991

4992
 cleanup:
4993
    virStoragePoolObjEndAPI(&obj);
4994
    return ret;
C
Cole Robinson 已提交
4995 4996 4997
}


4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016
struct storageVolLookupData {
    const char *key;
    const char *path;
    virStorageVolDefPtr voldef;
};

static bool
testStorageVolLookupByKeyCallback(virStoragePoolObjPtr obj,
                                  const void *opaque)
{
    struct storageVolLookupData *data = (struct storageVolLookupData *) opaque;

    if (virStoragePoolObjIsActive(obj))
        data->voldef = virStorageVolDefFindByKey(obj, data->key);

    return !!data->voldef;
}


C
Cole Robinson 已提交
5017
static virStorageVolPtr
5018
testStorageVolLookupByKey(virConnectPtr conn,
5019 5020
                          const char *key)
{
5021
    testDriverPtr privconn = conn->privateData;
5022
    virStoragePoolObjPtr obj;
5023
    virStoragePoolDefPtr def;
5024
    struct storageVolLookupData data = {
5025
        .key = key, .voldef = NULL };
5026
    virStorageVolPtr vol = NULL;
C
Cole Robinson 已提交
5027

5028
    testDriverLock(privconn);
5029
    if ((obj = virStoragePoolObjListSearch(privconn->pools,
5030 5031
                                           testStorageVolLookupByKeyCallback,
                                           &data)) && data.voldef) {
5032
        def = virStoragePoolObjGetDef(obj);
5033 5034 5035
        vol = virGetStorageVol(conn, def->name,
                               data.voldef->name, data.voldef->key,
                               NULL, NULL);
5036
        virStoragePoolObjEndAPI(&obj);
C
Cole Robinson 已提交
5037
    }
5038
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5039

5040
    if (!vol)
5041 5042
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
5043

5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057
    return vol;
}


static bool
testStorageVolLookupByPathCallback(virStoragePoolObjPtr obj,
                                   const void *opaque)
{
    struct storageVolLookupData *data = (struct storageVolLookupData *) opaque;

    if (virStoragePoolObjIsActive(obj))
        data->voldef = virStorageVolDefFindByPath(obj, data->path);

    return !!data->voldef;
C
Cole Robinson 已提交
5058 5059
}

5060

C
Cole Robinson 已提交
5061
static virStorageVolPtr
5062
testStorageVolLookupByPath(virConnectPtr conn,
5063 5064
                           const char *path)
{
5065
    testDriverPtr privconn = conn->privateData;
5066
    virStoragePoolObjPtr obj;
5067
    virStoragePoolDefPtr def;
5068
    struct storageVolLookupData data = {
5069
        .path = path, .voldef = NULL };
5070
    virStorageVolPtr vol = NULL;
C
Cole Robinson 已提交
5071

5072
    testDriverLock(privconn);
5073
    if ((obj = virStoragePoolObjListSearch(privconn->pools,
5074 5075
                                           testStorageVolLookupByPathCallback,
                                           &data)) && data.voldef) {
5076
        def = virStoragePoolObjGetDef(obj);
5077 5078 5079
        vol = virGetStorageVol(conn, def->name,
                               data.voldef->name, data.voldef->key,
                               NULL, NULL);
5080
        virStoragePoolObjEndAPI(&obj);
C
Cole Robinson 已提交
5081
    }
5082
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5083

5084
    if (!vol)
5085 5086
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
5087

5088
    return vol;
C
Cole Robinson 已提交
5089 5090
}

5091

C
Cole Robinson 已提交
5092
static virStorageVolPtr
5093 5094 5095
testStorageVolCreateXML(virStoragePoolPtr pool,
                        const char *xmldesc,
                        unsigned int flags)
E
Eric Blake 已提交
5096
{
5097
    testDriverPtr privconn = pool->conn->privateData;
5098
    virStoragePoolObjPtr obj;
5099
    virStoragePoolDefPtr def;
5100 5101
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
5102

E
Eric Blake 已提交
5103 5104
    virCheckFlags(0, NULL);

5105 5106
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name)))
        return NULL;
5107
    def = virStoragePoolObjGetDef(obj);
C
Cole Robinson 已提交
5108

5109
    privvol = virStorageVolDefParseString(def, xmldesc, 0);
5110
    if (privvol == NULL)
5111
        goto cleanup;
5112

5113
    if (virStorageVolDefFindByName(obj, privvol->name)) {
5114 5115
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5116
        goto cleanup;
C
Cole Robinson 已提交
5117 5118 5119
    }

    /* Make sure enough space */
5120 5121
    if ((def->allocation + privvol->target.allocation) >
         def->capacity) {
5122 5123 5124
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5125
        goto cleanup;
C
Cole Robinson 已提交
5126 5127
    }

5128
    if (virAsprintf(&privvol->target.path, "%s/%s",
5129
                    def->target.path, privvol->name) < 0)
5130
        goto cleanup;
C
Cole Robinson 已提交
5131

5132
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
5133
        virStoragePoolObjAddVol(obj, privvol) < 0)
5134
        goto cleanup;
C
Cole Robinson 已提交
5135

5136 5137
    def->allocation += privvol->target.allocation;
    def->available = (def->capacity - def->allocation);
C
Cole Robinson 已提交
5138

5139
    ret = virGetStorageVol(pool->conn, def->name,
5140 5141
                           privvol->name, privvol->key,
                           NULL, NULL);
5142
    privvol = NULL;
5143

5144
 cleanup:
5145
    virStorageVolDefFree(privvol);
5146
    virStoragePoolObjEndAPI(&obj);
5147
    return ret;
C
Cole Robinson 已提交
5148 5149
}

5150

5151
static virStorageVolPtr
5152 5153 5154 5155
testStorageVolCreateXMLFrom(virStoragePoolPtr pool,
                            const char *xmldesc,
                            virStorageVolPtr clonevol,
                            unsigned int flags)
E
Eric Blake 已提交
5156
{
5157
    testDriverPtr privconn = pool->conn->privateData;
5158
    virStoragePoolObjPtr obj;
5159
    virStoragePoolDefPtr def;
5160 5161 5162
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
5163 5164
    virCheckFlags(0, NULL);

5165 5166
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name)))
        return NULL;
5167
    def = virStoragePoolObjGetDef(obj);
5168

5169
    privvol = virStorageVolDefParseString(def, xmldesc, 0);
5170 5171 5172
    if (privvol == NULL)
        goto cleanup;

5173
    if (virStorageVolDefFindByName(obj, privvol->name)) {
5174 5175
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5176 5177 5178
        goto cleanup;
    }

5179
    origvol = virStorageVolDefFindByName(obj, clonevol->name);
5180
    if (!origvol) {
5181 5182 5183
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
5184 5185 5186 5187
        goto cleanup;
    }

    /* Make sure enough space */
5188
    if ((def->allocation + privvol->target.allocation) > def->capacity) {
5189 5190 5191
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5192 5193
        goto cleanup;
    }
5194
    def->available = (def->capacity - def->allocation);
5195

5196
    if (virAsprintf(&privvol->target.path, "%s/%s",
5197
                    def->target.path, privvol->name) < 0)
5198 5199
        goto cleanup;

5200
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
5201
        virStoragePoolObjAddVol(obj, privvol) < 0)
5202 5203
        goto cleanup;

5204 5205
    def->allocation += privvol->target.allocation;
    def->available = (def->capacity - def->allocation);
5206

5207
    ret = virGetStorageVol(pool->conn, def->name,
5208 5209
                           privvol->name, privvol->key,
                           NULL, NULL);
5210 5211
    privvol = NULL;

5212
 cleanup:
5213
    virStorageVolDefFree(privvol);
5214
    virStoragePoolObjEndAPI(&obj);
5215 5216 5217
    return ret;
}

5218

C
Cole Robinson 已提交
5219
static int
5220 5221
testStorageVolDelete(virStorageVolPtr vol,
                     unsigned int flags)
E
Eric Blake 已提交
5222
{
5223
    testDriverPtr privconn = vol->conn->privateData;
5224
    virStoragePoolObjPtr obj;
5225
    virStoragePoolDefPtr def;
5226
    virStorageVolDefPtr privvol;
5227
    int ret = -1;
C
Cole Robinson 已提交
5228

E
Eric Blake 已提交
5229 5230
    virCheckFlags(0, -1);

5231 5232
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, vol->pool)))
        return -1;
5233
    def = virStoragePoolObjGetDef(obj);
5234

5235
    if (!(privvol = testStorageVolDefFindByName(obj, vol->name)))
5236
        goto cleanup;
5237

5238 5239
    def->allocation -= privvol->target.allocation;
    def->available = (def->capacity - def->allocation);
C
Cole Robinson 已提交
5240

5241
    virStoragePoolObjRemoveVol(obj, privvol);
C
Cole Robinson 已提交
5242

5243
    ret = 0;
C
Cole Robinson 已提交
5244

5245
 cleanup:
5246
    virStoragePoolObjEndAPI(&obj);
5247
    return ret;
C
Cole Robinson 已提交
5248 5249 5250
}


5251 5252
static int
testStorageVolumeTypeForPool(int pooltype)
5253
{
C
Cole Robinson 已提交
5254

5255
    switch (pooltype) {
C
Cole Robinson 已提交
5256 5257 5258 5259 5260 5261 5262 5263 5264
        case VIR_STORAGE_POOL_DIR:
        case VIR_STORAGE_POOL_FS:
        case VIR_STORAGE_POOL_NETFS:
            return VIR_STORAGE_VOL_FILE;
        default:
            return VIR_STORAGE_VOL_BLOCK;
    }
}

5265

C
Cole Robinson 已提交
5266
static int
5267
testStorageVolGetInfo(virStorageVolPtr vol,
5268 5269
                      virStorageVolInfoPtr info)
{
5270
    testDriverPtr privconn = vol->conn->privateData;
5271
    virStoragePoolObjPtr obj;
5272
    virStoragePoolDefPtr def;
5273
    virStorageVolDefPtr privvol;
5274
    int ret = -1;
5275

5276 5277
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, vol->pool)))
        return -1;
5278
    def = virStoragePoolObjGetDef(obj);
5279

5280
    if (!(privvol = testStorageVolDefFindByName(obj, vol->name)))
5281
        goto cleanup;
5282

C
Cole Robinson 已提交
5283
    memset(info, 0, sizeof(*info));
5284
    info->type = testStorageVolumeTypeForPool(def->type);
5285 5286
    info->capacity = privvol->target.capacity;
    info->allocation = privvol->target.allocation;
5287
    ret = 0;
C
Cole Robinson 已提交
5288

5289
 cleanup:
5290
    virStoragePoolObjEndAPI(&obj);
5291
    return ret;
C
Cole Robinson 已提交
5292 5293
}

5294

C
Cole Robinson 已提交
5295
static char *
5296 5297
testStorageVolGetXMLDesc(virStorageVolPtr vol,
                         unsigned int flags)
E
Eric Blake 已提交
5298
{
5299
    testDriverPtr privconn = vol->conn->privateData;
5300
    virStoragePoolObjPtr obj;
5301
    virStorageVolDefPtr privvol;
5302
    char *ret = NULL;
5303

E
Eric Blake 已提交
5304 5305
    virCheckFlags(0, NULL);

5306 5307
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, vol->pool)))
        return NULL;
5308

5309
    if (!(privvol = testStorageVolDefFindByName(obj, vol->name)))
5310
        goto cleanup;
C
Cole Robinson 已提交
5311

5312
    ret = virStorageVolDefFormat(virStoragePoolObjGetDef(obj), privvol);
5313

5314
 cleanup:
5315
    virStoragePoolObjEndAPI(&obj);
5316
    return ret;
C
Cole Robinson 已提交
5317 5318
}

5319

C
Cole Robinson 已提交
5320
static char *
5321 5322
testStorageVolGetPath(virStorageVolPtr vol)
{
5323
    testDriverPtr privconn = vol->conn->privateData;
5324
    virStoragePoolObjPtr obj;
5325
    virStorageVolDefPtr privvol;
5326
    char *ret = NULL;
5327

5328 5329
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, vol->pool)))
        return NULL;
5330

5331
    if (!(privvol = testStorageVolDefFindByName(obj, vol->name)))
5332
        goto cleanup;
5333

5334
    ignore_value(VIR_STRDUP(ret, privvol->target.path));
5335

5336
 cleanup:
5337
    virStoragePoolObjEndAPI(&obj);
C
Cole Robinson 已提交
5338 5339 5340
    return ret;
}

5341

5342
/* Node device implementations */
5343

5344 5345 5346 5347 5348 5349
static virNodeDeviceObjPtr
testNodeDeviceObjFindByName(testDriverPtr driver,
                            const char *name)
{
    virNodeDeviceObjPtr obj;

5350
    if (!(obj = virNodeDeviceObjListFindByName(driver->devs, name)))
5351 5352 5353 5354 5355 5356 5357 5358
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       name);

    return obj;
}


5359 5360 5361
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5362
                     unsigned int flags)
5363
{
5364
    testDriverPtr driver = conn->privateData;
5365

E
Eric Blake 已提交
5366 5367
    virCheckFlags(0, -1);

5368
    return virNodeDeviceObjListNumOfDevices(driver->devs, conn, cap, NULL);
5369 5370
}

5371

5372 5373 5374 5375 5376
static int
testNodeListDevices(virConnectPtr conn,
                    const char *cap,
                    char **const names,
                    int maxnames,
E
Eric Blake 已提交
5377
                    unsigned int flags)
5378
{
5379
    testDriverPtr driver = conn->privateData;
5380

E
Eric Blake 已提交
5381 5382
    virCheckFlags(0, -1);

5383 5384
    return virNodeDeviceObjListGetNames(driver->devs, conn, NULL,
                                        cap, names, maxnames);
5385 5386
}

5387

5388 5389 5390
static virNodeDevicePtr
testNodeDeviceLookupByName(virConnectPtr conn, const char *name)
{
5391
    testDriverPtr driver = conn->privateData;
5392
    virNodeDeviceObjPtr obj;
5393
    virNodeDeviceDefPtr def;
5394 5395
    virNodeDevicePtr ret = NULL;

5396
    if (!(obj = testNodeDeviceObjFindByName(driver, name)))
5397
        return NULL;
5398
    def = virNodeDeviceObjGetDef(obj);
5399

5400
    if ((ret = virGetNodeDevice(conn, name))) {
5401
        if (VIR_STRDUP(ret->parentName, def->parent) < 0) {
5402
            virObjectUnref(ret);
5403 5404
            ret = NULL;
        }
5405
    }
5406

5407
    virNodeDeviceObjEndAPI(&obj);
5408 5409 5410 5411
    return ret;
}

static char *
5412
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5413
                         unsigned int flags)
5414
{
5415
    testDriverPtr driver = dev->conn->privateData;
5416 5417 5418
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5419 5420
    virCheckFlags(0, NULL);

5421
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5422
        return NULL;
5423

5424
    ret = virNodeDeviceDefFormat(virNodeDeviceObjGetDef(obj));
5425

5426
    virNodeDeviceObjEndAPI(&obj);
5427 5428 5429 5430 5431 5432
    return ret;
}

static char *
testNodeDeviceGetParent(virNodeDevicePtr dev)
{
5433
    testDriverPtr driver = dev->conn->privateData;
5434
    virNodeDeviceObjPtr obj;
5435
    virNodeDeviceDefPtr def;
5436 5437
    char *ret = NULL;

5438
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5439
        return NULL;
5440
    def = virNodeDeviceObjGetDef(obj);
5441

5442 5443
    if (def->parent) {
        ignore_value(VIR_STRDUP(ret, def->parent));
5444
    } else {
5445 5446
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5447 5448
    }

5449
    virNodeDeviceObjEndAPI(&obj);
5450 5451 5452
    return ret;
}

5453

5454 5455 5456
static int
testNodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
5457
    testDriverPtr driver = dev->conn->privateData;
5458
    virNodeDeviceObjPtr obj;
5459
    virNodeDeviceDefPtr def;
5460 5461 5462
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;

5463
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5464
        return -1;
5465
    def = virNodeDeviceObjGetDef(obj);
5466

5467
    for (caps = def->caps; caps; caps = caps->next)
5468 5469
        ++ncaps;

5470
    virNodeDeviceObjEndAPI(&obj);
5471
    return ncaps;
5472 5473 5474 5475 5476 5477
}


static int
testNodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
5478
    testDriverPtr driver = dev->conn->privateData;
5479
    virNodeDeviceObjPtr obj;
5480
    virNodeDeviceDefPtr def;
5481 5482 5483
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;

5484
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5485
        return -1;
5486
    def = virNodeDeviceObjGetDef(obj);
5487

5488
    for (caps = def->caps; caps && ncaps < maxnames; caps = caps->next) {
5489 5490 5491 5492
        if (VIR_STRDUP(names[ncaps],
                       virNodeDevCapTypeToString(caps->data.type)) < 0)
            goto error;
        ncaps++;
5493 5494
    }

5495
    virNodeDeviceObjEndAPI(&obj);
5496 5497 5498 5499 5500
    return ncaps;

 error:
    while (--ncaps >= 0)
        VIR_FREE(names[ncaps]);
5501
    virNodeDeviceObjEndAPI(&obj);
5502
    return -1;
5503 5504
}

5505

5506 5507
static virNodeDeviceObjPtr
testNodeDeviceMockCreateVport(testDriverPtr driver,
5508
                              const char *wwnn,
5509
                              const char *wwpn)
5510
{
5511 5512
    char *xml = NULL;
    virNodeDeviceDefPtr def = NULL;
5513
    virNodeDevCapsDefPtr caps;
5514
    virNodeDeviceObjPtr obj = NULL, objcopy = NULL;
5515
    virNodeDeviceDefPtr objdef;
5516
    virObjectEventPtr event = NULL;
5517

5518 5519 5520 5521 5522 5523 5524 5525 5526
    /* In the real code, we'd call virVHBAManageVport which would take the
     * wwnn/wwpn from the input XML in order to call the "vport_create"
     * function for the parent. That in turn would set off a sequence of
     * events resulting in the creation of a vHBA scsi_hostN in the
     * node device objects list using the "next" host number with the
     * wwnn/wwpn from the input XML. The following will mock this by
     * using the scsi_host11 definition, changing the name and the
     * scsi_host capability fields before calling virNodeDeviceAssignDef
     * to add the def to the node device objects list. */
5527 5528
    if (!(objcopy = virNodeDeviceObjListFindByName(driver->devs,
                                                   "scsi_host11")))
5529 5530
        goto cleanup;

5531
    xml = virNodeDeviceDefFormat(virNodeDeviceObjGetDef(objcopy));
5532
    virNodeDeviceObjEndAPI(&objcopy);
5533 5534 5535 5536
    if (!xml)
        goto cleanup;

    if (!(def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL)))
5537 5538
        goto cleanup;

5539
    VIR_FREE(def->name);
5540
    if (VIR_STRDUP(def->name, "scsi_host12") < 0)
5541 5542
        goto cleanup;

5543 5544 5545
    /* Find the 'scsi_host' cap and alter the host # and unique_id and
     * then for the 'fc_host' capability modify the wwnn/wwpn to be that
     * of the input XML. */
5546 5547
    caps = def->caps;
    while (caps) {
5548
        if (caps->data.type != VIR_NODE_DEV_CAP_SCSI_HOST)
5549 5550
            continue;

5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564
        /* For the "fc_host" cap - change the wwnn/wwpn to match the input */
        if (caps->data.scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
            VIR_FREE(caps->data.scsi_host.wwnn);
            VIR_FREE(caps->data.scsi_host.wwpn);
            if (VIR_STRDUP(caps->data.scsi_host.wwnn, wwnn) < 0 ||
                VIR_STRDUP(caps->data.scsi_host.wwpn, wwpn) < 0)
                goto cleanup;
        } else {
            /* For the "scsi_host" cap, increment our host and unique_id to
             * give the appearance that something new was created - then add
             * that to the node device driver */
            caps->data.scsi_host.host++;
            caps->data.scsi_host.unique_id++;
        }
5565 5566 5567
        caps = caps->next;
    }

5568
    if (!(obj = virNodeDeviceObjListAssignDef(driver->devs, def)))
5569
        goto cleanup;
5570
    def = NULL;
5571
    objdef = virNodeDeviceObjGetDef(obj);
5572

5573
    event = virNodeDeviceEventLifecycleNew(objdef->name,
5574 5575
                                           VIR_NODE_DEVICE_EVENT_CREATED,
                                           0);
5576 5577 5578
    testObjectEventQueue(driver, event);

 cleanup:
5579
    VIR_FREE(xml);
5580 5581
    virNodeDeviceDefFree(def);
    return obj;
5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592
}


static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
                        unsigned int flags)
{
    testDriverPtr driver = conn->privateData;
    virNodeDeviceDefPtr def = NULL;
    char *wwnn = NULL, *wwpn = NULL;
5593 5594
    virNodeDevicePtr dev = NULL, ret = NULL;
    virNodeDeviceObjPtr obj = NULL;
5595
    virNodeDeviceDefPtr objdef;
5596 5597 5598 5599 5600 5601

    virCheckFlags(0, NULL);

    if (!(def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, NULL)))
        goto cleanup;

5602 5603 5604
    /* We run this simply for validation - it essentially validates that
     * the input XML either has a wwnn/wwpn or virNodeDevCapSCSIHostParseXML
     * generated a wwnn/wwpn */
5605 5606 5607
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) < 0)
        goto cleanup;

5608 5609 5610
    /* Unlike the "real" code we don't need the parent_host in order to
     * call virVHBAManageVport, but still let's make sure the code finds
     * something valid and no one messed up the mock environment. */
5611
    if (virNodeDeviceObjListGetParentHost(driver->devs, def) < 0)
5612 5613 5614 5615
        goto cleanup;

    /* In the real code, we'd call virVHBAManageVport followed by
     * find_new_device, but we cannot do that here since we're not
5616 5617 5618
     * mocking udev. The mock routine will copy an existing vHBA and
     * rename a few fields to mock that. So in order to allow that to
     * work properly, we need to drop our lock */
5619 5620
    if (!(obj = testNodeDeviceMockCreateVport(driver, wwnn, wwpn)))
        goto cleanup;
5621
    objdef = virNodeDeviceObjGetDef(obj);
5622

5623
    if (!(dev = virGetNodeDevice(conn, objdef->name)))
5624 5625
        goto cleanup;

5626 5627
    VIR_FREE(dev->parentName);
    if (VIR_STRDUP(dev->parentName, def->parent) < 0)
5628 5629 5630 5631
        goto cleanup;

    ret = dev;
    dev = NULL;
5632

5633
 cleanup:
5634
    virNodeDeviceObjEndAPI(&obj);
5635
    virNodeDeviceDefFree(def);
5636
    virObjectUnref(dev);
5637 5638
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
5639
    return ret;
5640 5641 5642 5643 5644 5645
}

static int
testNodeDeviceDestroy(virNodeDevicePtr dev)
{
    int ret = 0;
5646
    testDriverPtr driver = dev->conn->privateData;
5647
    virNodeDeviceObjPtr obj = NULL;
5648
    virNodeDeviceObjPtr parentobj = NULL;
5649
    virNodeDeviceDefPtr def;
5650
    char *wwnn = NULL, *wwpn = NULL;
5651
    virObjectEventPtr event = NULL;
5652

5653
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5654
        return -1;
5655
    def = virNodeDeviceObjGetDef(obj);
5656

5657
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1)
5658
        goto cleanup;
5659

5660 5661 5662 5663
    /* Unlike the real code we cannot run into the udevAddOneDevice race
     * which would replace obj->def, so no need to save off the parent,
     * but do need to drop the @obj lock so that the FindByName code doesn't
     * deadlock on ourselves */
5664
    virObjectUnlock(obj);
5665

5666 5667 5668 5669 5670 5671
    /* We do this just for basic validation and throw away the parentobj
     * since there's no vport_delete to be run */
    if (!(parentobj = virNodeDeviceObjListFindByName(driver->devs,
                                                     def->parent))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("cannot find parent '%s' definition"), def->parent);
5672
        virObjectLock(obj);
5673
        goto cleanup;
5674
    }
5675
    virNodeDeviceObjEndAPI(&parentobj);
5676

5677 5678 5679 5680
    event = virNodeDeviceEventLifecycleNew(dev->name,
                                           VIR_NODE_DEVICE_EVENT_DELETED,
                                           0);

5681
    virObjectLock(obj);
5682
    virNodeDeviceObjListRemove(driver->devs, obj);
5683
    virObjectUnref(obj);
5684
    obj = NULL;
5685

5686
 cleanup:
5687
    virNodeDeviceObjEndAPI(&obj);
5688
    testObjectEventQueue(driver, event);
5689 5690 5691 5692 5693
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

5694 5695

/* Domain event implementations */
5696
static int
5697 5698 5699 5700
testConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
5701
{
5702
    testDriverPtr driver = conn->privateData;
5703
    int ret = 0;
5704

5705
    if (virDomainEventStateRegister(conn, driver->eventState,
5706 5707
                                    callback, opaque, freecb) < 0)
        ret = -1;
5708 5709 5710 5711

    return ret;
}

5712

5713
static int
5714 5715
testConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
5716
{
5717
    testDriverPtr driver = conn->privateData;
5718
    int ret = 0;
5719

5720
    if (virDomainEventStateDeregister(conn, driver->eventState,
5721 5722
                                      callback) < 0)
        ret = -1;
5723 5724 5725 5726

    return ret;
}

5727 5728

static int
5729 5730 5731 5732 5733 5734
testConnectDomainEventRegisterAny(virConnectPtr conn,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback callback,
                                  void *opaque,
                                  virFreeCallback freecb)
5735
{
5736
    testDriverPtr driver = conn->privateData;
5737 5738
    int ret;

5739
    if (virDomainEventStateRegisterID(conn, driver->eventState,
5740 5741
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
5742
        ret = -1;
5743 5744 5745 5746 5747

    return ret;
}

static int
5748 5749
testConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
5750
{
5751
    testDriverPtr driver = conn->privateData;
5752
    int ret = 0;
5753

5754
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5755
                                        callbackID, true) < 0)
5756
        ret = -1;
5757 5758 5759 5760 5761

    return ret;
}


5762 5763 5764 5765 5766 5767 5768 5769
static int
testConnectNetworkEventRegisterAny(virConnectPtr conn,
                                   virNetworkPtr net,
                                   int eventID,
                                   virConnectNetworkEventGenericCallback callback,
                                   void *opaque,
                                   virFreeCallback freecb)
{
5770
    testDriverPtr driver = conn->privateData;
5771 5772
    int ret;

5773
    if (virNetworkEventStateRegisterID(conn, driver->eventState,
5774
                                       net, eventID, callback,
5775 5776 5777 5778 5779 5780 5781 5782 5783 5784
                                       opaque, freecb, &ret) < 0)
        ret = -1;

    return ret;
}

static int
testConnectNetworkEventDeregisterAny(virConnectPtr conn,
                                     int callbackID)
{
5785
    testDriverPtr driver = conn->privateData;
5786
    int ret = 0;
5787

5788
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5789
                                        callbackID, true) < 0)
5790
        ret = -1;
5791 5792 5793 5794

    return ret;
}

5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821
static int
testConnectStoragePoolEventRegisterAny(virConnectPtr conn,
                                       virStoragePoolPtr pool,
                                       int eventID,
                                       virConnectStoragePoolEventGenericCallback callback,
                                       void *opaque,
                                       virFreeCallback freecb)
{
    testDriverPtr driver = conn->privateData;
    int ret;

    if (virStoragePoolEventStateRegisterID(conn, driver->eventState,
                                           pool, eventID, callback,
                                           opaque, freecb, &ret) < 0)
        ret = -1;

    return ret;
}

static int
testConnectStoragePoolEventDeregisterAny(virConnectPtr conn,
                                         int callbackID)
{
    testDriverPtr driver = conn->privateData;
    int ret = 0;

    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5822
                                        callbackID, true) < 0)
5823 5824 5825 5826 5827
        ret = -1;

    return ret;
}

5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854
static int
testConnectNodeDeviceEventRegisterAny(virConnectPtr conn,
                                      virNodeDevicePtr dev,
                                      int eventID,
                                      virConnectNodeDeviceEventGenericCallback callback,
                                      void *opaque,
                                      virFreeCallback freecb)
{
    testDriverPtr driver = conn->privateData;
    int ret;

    if (virNodeDeviceEventStateRegisterID(conn, driver->eventState,
                                          dev, eventID, callback,
                                          opaque, freecb, &ret) < 0)
        ret = -1;

    return ret;
}

static int
testConnectNodeDeviceEventDeregisterAny(virConnectPtr conn,
                                        int callbackID)
{
    testDriverPtr driver = conn->privateData;
    int ret = 0;

    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5855
                                        callbackID, true) < 0)
5856 5857 5858 5859 5860
        ret = -1;

    return ret;
}

5861 5862 5863
static int testConnectListAllDomains(virConnectPtr conn,
                                     virDomainPtr **domains,
                                     unsigned int flags)
5864
{
5865
    testDriverPtr privconn = conn->privateData;
5866

O
Osier Yang 已提交
5867
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
5868

5869 5870
    return virDomainObjListExport(privconn->domains, conn, domains,
                                  NULL, flags);
5871 5872
}

5873
static int
P
Peter Krempa 已提交
5874
testNodeGetCPUMap(virConnectPtr conn ATTRIBUTE_UNUSED,
5875 5876 5877 5878 5879 5880 5881
                  unsigned char **cpumap,
                  unsigned int *online,
                  unsigned int flags)
{
    virCheckFlags(0, -1);

    if (cpumap) {
5882
        if (VIR_ALLOC_N(*cpumap, 1) < 0)
P
Peter Krempa 已提交
5883
            return -1;
5884 5885 5886 5887 5888 5889
        *cpumap[0] = 0x15;
    }

    if (online)
        *online = 3;

P
Peter Krempa 已提交
5890
    return  8;
5891 5892
}

5893 5894 5895 5896 5897 5898 5899 5900 5901 5902
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

5903
    if (VIR_STRDUP(ret, "image/png") < 0)
5904 5905
        return NULL;

D
Daniel P. Berrange 已提交
5906
    if (virFDStreamOpenFile(st, PKGDATADIR "/test-screenshot.png", 0, 0, O_RDONLY) < 0)
5907 5908 5909 5910 5911
        VIR_FREE(ret);

    return ret;
}

5912 5913
static int
testConnectGetCPUModelNames(virConnectPtr conn ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
5914
                            const char *archName,
5915 5916 5917
                            char ***models,
                            unsigned int flags)
{
J
Jiri Denemark 已提交
5918 5919
    virArch arch;

5920
    virCheckFlags(0, -1);
J
Jiri Denemark 已提交
5921 5922 5923 5924 5925 5926 5927 5928

    if (!(arch = virArchFromString(archName))) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("cannot find architecture %s"),
                       archName);
        return -1;
    }

J
Jiri Denemark 已提交
5929
    return virCPUGetModels(arch, models);
5930
}
5931

C
Cole Robinson 已提交
5932 5933 5934
static int
testDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
5935
    testDriverPtr privconn = dom->conn->privateData;
C
Cole Robinson 已提交
5936
    virDomainObjPtr vm = NULL;
5937
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
5938 5939 5940 5941 5942 5943
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_SAVE_BYPASS_CACHE |
                  VIR_DOMAIN_SAVE_RUNNING |
                  VIR_DOMAIN_SAVE_PAUSED, -1);

5944 5945
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5946

5947
    if (virDomainObjCheckActive(vm) < 0)
C
Cole Robinson 已提交
5948 5949 5950 5951 5952 5953 5954 5955 5956
        goto cleanup;

    if (!vm->persistent) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot do managed save for transient domain"));
        goto cleanup;
    }

    testDomainShutdownState(dom, vm, VIR_DOMAIN_SHUTOFF_SAVED);
5957
    event = virDomainEventLifecycleNewFromObj(vm,
C
Cole Robinson 已提交
5958 5959 5960 5961 5962
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
    vm->hasManagedSave = true;

    ret = 0;
5963
 cleanup:
5964
    virDomainObjEndAPI(&vm);
5965
    testObjectEventQueue(privconn, event);
C
Cole Robinson 已提交
5966 5967 5968 5969 5970 5971 5972 5973 5974

    return ret;
}


static int
testDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;
5975
    int ret;
C
Cole Robinson 已提交
5976 5977 5978

    virCheckFlags(0, -1);

5979 5980
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5981 5982

    ret = vm->hasManagedSave;
5983

5984
    virDomainObjEndAPI(&vm);
C
Cole Robinson 已提交
5985 5986 5987 5988 5989 5990 5991 5992 5993 5994
    return ret;
}

static int
testDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;

    virCheckFlags(0, -1);

5995 5996
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5997 5998

    vm->hasManagedSave = false;
5999

6000
    virDomainObjEndAPI(&vm);
6001
    return 0;
C
Cole Robinson 已提交
6002 6003 6004
}


6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038
/*
 * Snapshot APIs
 */

static virDomainSnapshotObjPtr
testSnapObjFromName(virDomainObjPtr vm,
                    const char *name)
{
    virDomainSnapshotObjPtr snap = NULL;
    snap = virDomainSnapshotFindByName(vm->snapshots, name);
    if (!snap)
        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                       _("no domain snapshot with matching name '%s'"),
                       name);
    return snap;
}

static virDomainSnapshotObjPtr
testSnapObjFromSnapshot(virDomainObjPtr vm,
                        virDomainSnapshotPtr snapshot)
{
    return testSnapObjFromName(vm, snapshot->name);
}

static virDomainObjPtr
testDomObjFromSnapshot(virDomainSnapshotPtr snapshot)
{
    return testDomObjFromDomain(snapshot->domain);
}

static int
testDomainSnapshotNum(virDomainPtr domain, unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6039
    int n;
6040 6041 6042 6043 6044

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6045
        return -1;
6046 6047 6048

    n = virDomainSnapshotObjListNum(vm->snapshots, NULL, flags);

6049
    virDomainObjEndAPI(&vm);
6050 6051 6052 6053 6054 6055 6056 6057 6058 6059
    return n;
}

static int
testDomainSnapshotListNames(virDomainPtr domain,
                            char **names,
                            int nameslen,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6060
    int n;
6061 6062 6063 6064 6065

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6066
        return -1;
6067 6068 6069 6070

    n = virDomainSnapshotObjListGetNames(vm->snapshots, NULL, names, nameslen,
                                         flags);

6071
    virDomainObjEndAPI(&vm);
6072 6073 6074 6075 6076 6077 6078 6079 6080
    return n;
}

static int
testDomainListAllSnapshots(virDomainPtr domain,
                           virDomainSnapshotPtr **snaps,
                           unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6081
    int n;
6082 6083 6084 6085 6086

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6087
        return -1;
6088 6089 6090

    n = virDomainListSnapshots(vm->snapshots, NULL, domain, snaps, flags);

6091
    virDomainObjEndAPI(&vm);
6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108
    return n;
}

static int
testDomainSnapshotListChildrenNames(virDomainSnapshotPtr snapshot,
                                    char **names,
                                    int nameslen,
                                    unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6109
        return -1;
6110 6111 6112 6113 6114 6115 6116

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListGetNames(vm->snapshots, snap, names, nameslen,
                                         flags);

6117
 cleanup:
6118
    virDomainObjEndAPI(&vm);
6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133
    return n;
}

static int
testDomainSnapshotNumChildren(virDomainSnapshotPtr snapshot,
                              unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6134
        return -1;
6135 6136 6137 6138 6139 6140

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListNum(vm->snapshots, snap, flags);

6141
 cleanup:
6142
    virDomainObjEndAPI(&vm);
6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158
    return n;
}

static int
testDomainSnapshotListAllChildren(virDomainSnapshotPtr snapshot,
                                  virDomainSnapshotPtr **snaps,
                                  unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6159
        return -1;
6160 6161 6162 6163 6164 6165 6166

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainListSnapshots(vm->snapshots, snap, snapshot->domain, snaps,
                               flags);

6167
 cleanup:
6168
    virDomainObjEndAPI(&vm);
6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183
    return n;
}

static virDomainSnapshotPtr
testDomainSnapshotLookupByName(virDomainPtr domain,
                               const char *name,
                               unsigned int flags)
{
    virDomainObjPtr vm;
    virDomainSnapshotObjPtr snap = NULL;
    virDomainSnapshotPtr snapshot = NULL;

    virCheckFlags(0, NULL);

    if (!(vm = testDomObjFromDomain(domain)))
6184
        return NULL;
6185 6186 6187 6188 6189 6190

    if (!(snap = testSnapObjFromName(vm, name)))
        goto cleanup;

    snapshot = virGetDomainSnapshot(domain, snap->def->name);

6191
 cleanup:
6192
    virDomainObjEndAPI(&vm);
6193 6194 6195 6196 6197 6198 6199 6200
    return snapshot;
}

static int
testDomainHasCurrentSnapshot(virDomainPtr domain,
                             unsigned int flags)
{
    virDomainObjPtr vm;
6201
    int ret;
6202 6203 6204 6205

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6206
        return -1;
6207 6208 6209

    ret = (vm->current_snapshot != NULL);

6210
    virDomainObjEndAPI(&vm);
6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224
    return ret;
}

static virDomainSnapshotPtr
testDomainSnapshotGetParent(virDomainSnapshotPtr snapshot,
                            unsigned int flags)
{
    virDomainObjPtr vm;
    virDomainSnapshotObjPtr snap = NULL;
    virDomainSnapshotPtr parent = NULL;

    virCheckFlags(0, NULL);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6225
        return NULL;
6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    if (!snap->def->parent) {
        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
                       _("snapshot '%s' does not have a parent"),
                       snap->def->name);
        goto cleanup;
    }

    parent = virGetDomainSnapshot(snapshot->domain, snap->def->parent);

6239
 cleanup:
6240
    virDomainObjEndAPI(&vm);
6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253
    return parent;
}

static virDomainSnapshotPtr
testDomainSnapshotCurrent(virDomainPtr domain,
                          unsigned int flags)
{
    virDomainObjPtr vm;
    virDomainSnapshotPtr snapshot = NULL;

    virCheckFlags(0, NULL);

    if (!(vm = testDomObjFromDomain(domain)))
6254
        return NULL;
6255 6256 6257 6258 6259 6260 6261 6262 6263

    if (!vm->current_snapshot) {
        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT, "%s",
                       _("the domain does not have a current snapshot"));
        goto cleanup;
    }

    snapshot = virGetDomainSnapshot(domain, vm->current_snapshot->def->name);

6264
 cleanup:
6265
    virDomainObjEndAPI(&vm);
6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276
    return snapshot;
}

static char *
testDomainSnapshotGetXMLDesc(virDomainSnapshotPtr snapshot,
                             unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    char *xml = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    char uuidstr[VIR_UUID_STRING_BUFLEN];
6277
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6278 6279 6280 6281

    virCheckFlags(VIR_DOMAIN_XML_SECURE, NULL);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6282
        return NULL;
6283 6284 6285 6286 6287 6288

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    virUUIDFormat(snapshot->domain->uuid, uuidstr);

6289
    xml = virDomainSnapshotDefFormat(uuidstr, snap->def, privconn->caps,
6290
                                     privconn->xmlopt,
6291 6292
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);
6293

6294
 cleanup:
6295
    virDomainObjEndAPI(&vm);
6296 6297 6298 6299 6300 6301 6302 6303
    return xml;
}

static int
testDomainSnapshotIsCurrent(virDomainSnapshotPtr snapshot,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6304
    int ret;
6305 6306 6307 6308

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6309
        return -1;
6310 6311 6312 6313

    ret = (vm->current_snapshot &&
           STREQ(snapshot->name, vm->current_snapshot->def->name));

6314
    virDomainObjEndAPI(&vm);
6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328
    return ret;
}


static int
testDomainSnapshotHasMetadata(virDomainSnapshotPtr snapshot,
                              unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6329
        return -1;
6330

C
Cole Robinson 已提交
6331
    if (!testSnapObjFromSnapshot(vm, snapshot))
6332 6333 6334 6335
        goto cleanup;

    ret = 1;

6336
 cleanup:
6337
    virDomainObjEndAPI(&vm);
6338 6339 6340
    return ret;
}

6341 6342 6343 6344 6345 6346
static int
testDomainSnapshotAlignDisks(virDomainObjPtr vm,
                             virDomainSnapshotDefPtr def,
                             unsigned int flags)
{
    int align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
E
Eric Blake 已提交
6347
    bool align_match = true;
6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375

    if (flags & VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY) {
        align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL;
        align_match = false;
        if (virDomainObjIsActive(vm))
            def->state = VIR_DOMAIN_DISK_SNAPSHOT;
        else
            def->state = VIR_DOMAIN_SHUTOFF;
        def->memory = VIR_DOMAIN_SNAPSHOT_LOCATION_NONE;
    } else if (def->memory == VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL) {
        def->state = virDomainObjGetState(vm, NULL);
        align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL;
        align_match = false;
    } else {
        def->state = virDomainObjGetState(vm, NULL);
        def->memory = def->state == VIR_DOMAIN_SHUTOFF ?
                      VIR_DOMAIN_SNAPSHOT_LOCATION_NONE :
                      VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
    }

    return virDomainSnapshotAlignDisks(def, align_location, align_match);
}

static virDomainSnapshotPtr
testDomainSnapshotCreateXML(virDomainPtr domain,
                            const char *xmlDesc,
                            unsigned int flags)
{
6376
    testDriverPtr privconn = domain->conn->privateData;
6377 6378 6379 6380
    virDomainObjPtr vm = NULL;
    virDomainSnapshotDefPtr def = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    virDomainSnapshotPtr snapshot = NULL;
6381
    virObjectEventPtr event = NULL;
6382
    char *xml = NULL;
6383 6384
    bool update_current = true;
    bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE;
6385 6386 6387 6388 6389 6390 6391 6392
    unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;

    /*
     * DISK_ONLY: Not implemented yet
     * REUSE_EXT: Not implemented yet
     *
     * NO_METADATA: Explicitly not implemented
     *
6393
     * REDEFINE + CURRENT: Implemented
6394 6395 6396 6397 6398 6399
     * HALT: Implemented
     * QUIESCE: Nothing to do
     * ATOMIC: Nothing to do
     * LIVE: Nothing to do
     */
    virCheckFlags(
6400 6401
        VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
        VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT |
6402 6403 6404 6405 6406
        VIR_DOMAIN_SNAPSHOT_CREATE_HALT |
        VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE |
        VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC |
        VIR_DOMAIN_SNAPSHOT_CREATE_LIVE, NULL);

6407 6408 6409 6410 6411
    if ((redefine && !(flags & VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT)))
        update_current = false;
    if (redefine)
        parse_flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;

6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426
    if (!(vm = testDomObjFromDomain(domain)))
        goto cleanup;

    if (!vm->persistent && (flags & VIR_DOMAIN_SNAPSHOT_CREATE_HALT)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("cannot halt after transient domain snapshot"));
        goto cleanup;
    }

    if (!(def = virDomainSnapshotDefParseString(xmlDesc,
                                                privconn->caps,
                                                privconn->xmlopt,
                                                parse_flags)))
        goto cleanup;

6427
    if (redefine) {
C
Cole Robinson 已提交
6428
        if (virDomainSnapshotRedefinePrep(domain, vm, &def, &snap,
6429
                                          privconn->xmlopt,
C
Cole Robinson 已提交
6430
                                          &update_current, flags) < 0)
6431 6432 6433 6434 6435
            goto cleanup;
    } else {
        if (!(def->dom = virDomainDefCopy(vm->def,
                                          privconn->caps,
                                          privconn->xmlopt,
6436
                                          NULL,
6437 6438
                                          true)))
            goto cleanup;
6439

6440
        if (testDomainSnapshotAlignDisks(vm, def, flags) < 0)
6441 6442 6443
            goto cleanup;
    }

6444 6445 6446 6447
    if (!snap) {
        if (!(snap = virDomainSnapshotAssignDef(vm->snapshots, def)))
            goto cleanup;
        def = NULL;
6448 6449
    }

6450 6451 6452 6453 6454 6455 6456 6457 6458 6459
    if (!redefine) {
        if (vm->current_snapshot &&
            (VIR_STRDUP(snap->def->parent,
                        vm->current_snapshot->def->name) < 0))
            goto cleanup;

        if ((flags & VIR_DOMAIN_SNAPSHOT_CREATE_HALT) &&
            virDomainObjIsActive(vm)) {
            testDomainShutdownState(domain, vm,
                                    VIR_DOMAIN_SHUTOFF_FROM_SNAPSHOT);
6460
            event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
6461 6462 6463
                                    VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
        }
    }
6464 6465

    snapshot = virGetDomainSnapshot(domain, snap->def->name);
6466
 cleanup:
6467 6468 6469 6470
    VIR_FREE(xml);
    if (vm) {
        if (snapshot) {
            virDomainSnapshotObjPtr other;
6471 6472
            if (update_current)
                vm->current_snapshot = snap;
6473 6474 6475 6476 6477 6478 6479
            other = virDomainSnapshotFindByName(vm->snapshots,
                                                snap->def->parent);
            snap->parent = other;
            other->nchildren++;
            snap->sibling = other->first_child;
            other->first_child = snap;
        }
6480
        virDomainObjEndAPI(&vm);
6481
    }
6482
    testObjectEventQueue(privconn, event);
6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494
    virDomainSnapshotDefFree(def);
    return snapshot;
}


typedef struct _testSnapRemoveData testSnapRemoveData;
typedef testSnapRemoveData *testSnapRemoveDataPtr;
struct _testSnapRemoveData {
    virDomainObjPtr vm;
    bool current;
};

6495
static int
6496
testDomainSnapshotDiscardAll(void *payload,
6497 6498
                             const void *name ATTRIBUTE_UNUSED,
                             void *data)
6499 6500 6501 6502 6503 6504 6505
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapRemoveDataPtr curr = data;

    if (snap->def->current)
        curr->current = true;
    virDomainSnapshotObjListRemove(curr->vm->snapshots, snap);
6506
    return 0;
6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517
}

typedef struct _testSnapReparentData testSnapReparentData;
typedef testSnapReparentData *testSnapReparentDataPtr;
struct _testSnapReparentData {
    virDomainSnapshotObjPtr parent;
    virDomainObjPtr vm;
    int err;
    virDomainSnapshotObjPtr last;
};

6518
static int
6519 6520 6521 6522 6523 6524 6525
testDomainSnapshotReparentChildren(void *payload,
                                   const void *name ATTRIBUTE_UNUSED,
                                   void *data)
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapReparentDataPtr rep = data;

6526
    if (rep->err < 0)
6527
        return 0;
6528 6529 6530 6531 6532 6533 6534

    VIR_FREE(snap->def->parent);
    snap->parent = rep->parent;

    if (rep->parent->def &&
        VIR_STRDUP(snap->def->parent, rep->parent->def->name) < 0) {
        rep->err = -1;
6535
        return 0;
6536 6537 6538 6539
    }

    if (!snap->sibling)
        rep->last = snap;
6540
    return 0;
6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569
}

static int
testDomainSnapshotDelete(virDomainSnapshotPtr snapshot,
                         unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    virDomainSnapshotObjPtr parentsnap = NULL;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN |
                  VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
        return -1;

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    if (flags & (VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN |
                 VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY)) {
        testSnapRemoveData rem;
        rem.vm = vm;
        rem.current = false;
        virDomainSnapshotForEachDescendant(snap,
                                           testDomainSnapshotDiscardAll,
                                           &rem);
        if (rem.current) {
6570
            if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY)
6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613
                snap->def->current = true;
            vm->current_snapshot = snap;
        }
    } else if (snap->nchildren) {
        testSnapReparentData rep;
        rep.parent = snap->parent;
        rep.vm = vm;
        rep.err = 0;
        rep.last = NULL;
        virDomainSnapshotForEachChild(snap,
                                      testDomainSnapshotReparentChildren,
                                      &rep);
        if (rep.err < 0)
            goto cleanup;

        /* Can't modify siblings during ForEachChild, so do it now.  */
        snap->parent->nchildren += snap->nchildren;
        rep.last->sibling = snap->parent->first_child;
        snap->parent->first_child = snap->first_child;
    }

    if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY) {
        snap->nchildren = 0;
        snap->first_child = NULL;
    } else {
        virDomainSnapshotDropParent(snap);
        if (snap == vm->current_snapshot) {
            if (snap->def->parent) {
                parentsnap = virDomainSnapshotFindByName(vm->snapshots,
                                                         snap->def->parent);
                if (!parentsnap) {
                    VIR_WARN("missing parent snapshot matching name '%s'",
                             snap->def->parent);
                } else {
                    parentsnap->def->current = true;
                }
            }
            vm->current_snapshot = parentsnap;
        }
        virDomainSnapshotObjListRemove(vm->snapshots, snap);
    }

    ret = 0;
6614
 cleanup:
6615
    virDomainObjEndAPI(&vm);
6616 6617 6618 6619 6620 6621 6622
    return ret;
}

static int
testDomainRevertToSnapshot(virDomainSnapshotPtr snapshot,
                           unsigned int flags)
{
6623
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6624 6625
    virDomainObjPtr vm = NULL;
    virDomainSnapshotObjPtr snap = NULL;
6626 6627
    virObjectEventPtr event = NULL;
    virObjectEventPtr event2 = NULL;
6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690
    virDomainDefPtr config = NULL;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_REVERT_RUNNING |
                  VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED |
                  VIR_DOMAIN_SNAPSHOT_REVERT_FORCE, -1);

    /* We have the following transitions, which create the following events:
     * 1. inactive -> inactive: none
     * 2. inactive -> running:  EVENT_STARTED
     * 3. inactive -> paused:   EVENT_STARTED, EVENT_PAUSED
     * 4. running  -> inactive: EVENT_STOPPED
     * 5. running  -> running:  none
     * 6. running  -> paused:   EVENT_PAUSED
     * 7. paused   -> inactive: EVENT_STOPPED
     * 8. paused   -> running:  EVENT_RESUMED
     * 9. paused   -> paused:   none
     * Also, several transitions occur even if we fail partway through,
     * and use of FORCE can cause multiple transitions.
     */

    if (!(vm = testDomObjFromSnapshot(snapshot)))
        return -1;

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    if (!vm->persistent &&
        snap->def->state != VIR_DOMAIN_RUNNING &&
        snap->def->state != VIR_DOMAIN_PAUSED &&
        (flags & (VIR_DOMAIN_SNAPSHOT_REVERT_RUNNING |
                  VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED)) == 0) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("transient domain needs to request run or pause "
                         "to revert to inactive snapshot"));
        goto cleanup;
    }

    if (!(flags & VIR_DOMAIN_SNAPSHOT_REVERT_FORCE)) {
        if (!snap->def->dom) {
            virReportError(VIR_ERR_SNAPSHOT_REVERT_RISKY,
                           _("snapshot '%s' lacks domain '%s' rollback info"),
                           snap->def->name, vm->def->name);
            goto cleanup;
        }
        if (virDomainObjIsActive(vm) &&
            !(snap->def->state == VIR_DOMAIN_RUNNING
              || snap->def->state == VIR_DOMAIN_PAUSED) &&
            (flags & (VIR_DOMAIN_SNAPSHOT_REVERT_RUNNING |
                      VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED))) {
            virReportError(VIR_ERR_SNAPSHOT_REVERT_RISKY, "%s",
                           _("must respawn guest to start inactive snapshot"));
            goto cleanup;
        }
    }


    if (vm->current_snapshot) {
        vm->current_snapshot->def->current = false;
        vm->current_snapshot = NULL;
    }

    snap->def->current = true;
6691 6692
    config = virDomainDefCopy(snap->def->dom, privconn->caps,
                              privconn->xmlopt, NULL, true);
6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704
    if (!config)
        goto cleanup;

    if (snap->def->state == VIR_DOMAIN_RUNNING ||
        snap->def->state == VIR_DOMAIN_PAUSED) {
        /* Transitions 2, 3, 5, 6, 8, 9 */
        bool was_running = false;
        bool was_stopped = false;

        if (virDomainObjIsActive(vm)) {
            /* Transitions 5, 6, 8, 9 */
            /* Check for ABI compatibility.  */
6705 6706
            if (!virDomainDefCheckABIStability(vm->def, config,
                                               privconn->xmlopt)) {
6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719
                virErrorPtr err = virGetLastError();

                if (!(flags & VIR_DOMAIN_SNAPSHOT_REVERT_FORCE)) {
                    /* Re-spawn error using correct category. */
                    if (err->code == VIR_ERR_CONFIG_UNSUPPORTED)
                        virReportError(VIR_ERR_SNAPSHOT_REVERT_RISKY, "%s",
                                       err->str2);
                    goto cleanup;
                }

                virResetError(err);
                testDomainShutdownState(snapshot->domain, vm,
                                        VIR_DOMAIN_SHUTOFF_FROM_SNAPSHOT);
6720
                event = virDomainEventLifecycleNewFromObj(vm,
6721 6722
                            VIR_DOMAIN_EVENT_STOPPED,
                            VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
6723
                testObjectEventQueue(privconn, event);
6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734
                goto load;
            }

            if (virDomainObjGetState(vm, NULL) == VIR_DOMAIN_RUNNING) {
                /* Transitions 5, 6 */
                was_running = true;
                virDomainObjSetState(vm, VIR_DOMAIN_PAUSED,
                                     VIR_DOMAIN_PAUSED_FROM_SNAPSHOT);
                /* Create an event now in case the restore fails, so
                 * that user will be alerted that they are now paused.
                 * If restore later succeeds, we might replace this. */
6735
                event = virDomainEventLifecycleNewFromObj(vm,
6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748
                                VIR_DOMAIN_EVENT_SUSPENDED,
                                VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT);
            }
            virDomainObjAssignDef(vm, config, false, NULL);

        } else {
            /* Transitions 2, 3 */
        load:
            was_stopped = true;
            virDomainObjAssignDef(vm, config, false, NULL);
            if (testDomainStartState(privconn, vm,
                                VIR_DOMAIN_RUNNING_FROM_SNAPSHOT) < 0)
                goto cleanup;
6749
            event = virDomainEventLifecycleNewFromObj(vm,
6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762
                                VIR_DOMAIN_EVENT_STARTED,
                                VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
        }

        /* Touch up domain state.  */
        if (!(flags & VIR_DOMAIN_SNAPSHOT_REVERT_RUNNING) &&
            (snap->def->state == VIR_DOMAIN_PAUSED ||
             (flags & VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED))) {
            /* Transitions 3, 6, 9 */
            virDomainObjSetState(vm, VIR_DOMAIN_PAUSED,
                                 VIR_DOMAIN_PAUSED_FROM_SNAPSHOT);
            if (was_stopped) {
                /* Transition 3, use event as-is and add event2 */
6763
                event2 = virDomainEventLifecycleNewFromObj(vm,
6764 6765 6766 6767 6768
                                VIR_DOMAIN_EVENT_SUSPENDED,
                                VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT);
            } /* else transition 6 and 9 use event as-is */
        } else {
            /* Transitions 2, 5, 8 */
C
Cédric Bosdonnat 已提交
6769
            virObjectUnref(event);
6770 6771 6772 6773
            event = NULL;

            if (was_stopped) {
                /* Transition 2 */
6774
                event = virDomainEventLifecycleNewFromObj(vm,
6775 6776 6777 6778
                                VIR_DOMAIN_EVENT_STARTED,
                                VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            } else if (was_running) {
                /* Transition 8 */
6779
                event = virDomainEventLifecycleNewFromObj(vm,
6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791
                                VIR_DOMAIN_EVENT_RESUMED,
                                VIR_DOMAIN_EVENT_RESUMED);
            }
        }
    } else {
        /* Transitions 1, 4, 7 */
        virDomainObjAssignDef(vm, config, false, NULL);

        if (virDomainObjIsActive(vm)) {
            /* Transitions 4, 7 */
            testDomainShutdownState(snapshot->domain, vm,
                                    VIR_DOMAIN_SHUTOFF_FROM_SNAPSHOT);
6792
            event = virDomainEventLifecycleNewFromObj(vm,
6793 6794 6795 6796 6797 6798 6799 6800 6801
                                    VIR_DOMAIN_EVENT_STOPPED,
                                    VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
        }

        if (flags & (VIR_DOMAIN_SNAPSHOT_REVERT_RUNNING |
                     VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED)) {
            /* Flush first event, now do transition 2 or 3 */
            bool paused = (flags & VIR_DOMAIN_SNAPSHOT_REVERT_PAUSED) != 0;

6802
            testObjectEventQueue(privconn, event);
6803
            event = virDomainEventLifecycleNewFromObj(vm,
6804 6805 6806
                            VIR_DOMAIN_EVENT_STARTED,
                            VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            if (paused) {
6807
                event2 = virDomainEventLifecycleNewFromObj(vm,
6808 6809 6810 6811 6812 6813 6814 6815
                                VIR_DOMAIN_EVENT_SUSPENDED,
                                VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT);
            }
        }
    }

    vm->current_snapshot = snap;
    ret = 0;
6816
 cleanup:
6817
    if (event) {
6818
        testObjectEventQueue(privconn, event);
6819
        testObjectEventQueue(privconn, event2);
C
Cole Robinson 已提交
6820
    } else {
C
Cédric Bosdonnat 已提交
6821
        virObjectUnref(event2);
6822
    }
6823
    virDomainObjEndAPI(&vm);
6824 6825 6826 6827 6828

    return ret;
}


6829

6830
static virHypervisorDriver testHypervisorDriver = {
6831
    .name = "Test",
6832 6833 6834
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
6835
    .connectGetHostname = testConnectGetHostname, /* 0.6.3 */
6836
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
6837
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
6838
    .nodeGetCPUStats = testNodeGetCPUStats, /* 2.3.0 */
6839
    .nodeGetFreeMemory = testNodeGetFreeMemory, /* 2.3.0 */
6840
    .nodeGetFreePages = testNodeGetFreePages, /* 2.3.0 */
6841
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
6842
    .connectGetSysinfo = testConnectGetSysinfo, /* 2.3.0 */
6843
    .connectGetType = testConnectGetType, /* 2.3.0 */
6844 6845 6846
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
6847
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
6848 6849 6850 6851 6852 6853 6854 6855 6856
    .domainLookupByID = testDomainLookupByID, /* 0.1.1 */
    .domainLookupByUUID = testDomainLookupByUUID, /* 0.1.1 */
    .domainLookupByName = testDomainLookupByName, /* 0.1.1 */
    .domainSuspend = testDomainSuspend, /* 0.1.1 */
    .domainResume = testDomainResume, /* 0.1.1 */
    .domainShutdown = testDomainShutdown, /* 0.1.1 */
    .domainShutdownFlags = testDomainShutdownFlags, /* 0.9.10 */
    .domainReboot = testDomainReboot, /* 0.1.1 */
    .domainDestroy = testDomainDestroy, /* 0.1.1 */
6857
    .domainDestroyFlags = testDomainDestroyFlags, /* 4.2.0 */
6858 6859 6860 6861 6862
    .domainGetOSType = testDomainGetOSType, /* 0.1.9 */
    .domainGetMaxMemory = testDomainGetMaxMemory, /* 0.1.4 */
    .domainSetMaxMemory = testDomainSetMaxMemory, /* 0.1.1 */
    .domainSetMemory = testDomainSetMemory, /* 0.1.4 */
    .domainGetInfo = testDomainGetInfo, /* 0.1.1 */
6863 6864
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
6865
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
6866
    .domainRestore = testDomainRestore, /* 0.3.2 */
6867
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
6868
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
6869
    .domainCoreDumpWithFormat = testDomainCoreDumpWithFormat, /* 1.2.3 */
6870
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
6871 6872 6873 6874
    .domainSetVcpusFlags = testDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = testDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = testDomainPinVcpu, /* 0.7.3 */
    .domainGetVcpus = testDomainGetVcpus, /* 0.7.3 */
6875
    .domainGetVcpuPinInfo = testDomainGetVcpuPinInfo, /* 1.2.18 */
6876 6877
    .domainGetMaxVcpus = testDomainGetMaxVcpus, /* 0.7.3 */
    .domainGetXMLDesc = testDomainGetXMLDesc, /* 0.1.4 */
6878 6879
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
6880 6881 6882
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
6883
    .domainDefineXMLFlags = testDomainDefineXMLFlags, /* 1.2.12 */
6884
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
6885
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
6886 6887 6888
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
6889 6890 6891 6892
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
6893 6894 6895
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
6896 6897 6898 6899
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
6900 6901 6902
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
6903 6904 6905
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
6906
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
6907
    .domainRename = testDomainRename, /* 4.1.0 */
6908
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
6909 6910
    .domainGetMetadata = testDomainGetMetadata, /* 1.1.3 */
    .domainSetMetadata = testDomainSetMetadata, /* 1.1.3 */
6911
    .connectGetCPUModelNames = testConnectGetCPUModelNames, /* 1.1.3 */
6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928
    .domainManagedSave = testDomainManagedSave, /* 1.1.4 */
    .domainHasManagedSaveImage = testDomainHasManagedSaveImage, /* 1.1.4 */
    .domainManagedSaveRemove = testDomainManagedSaveRemove, /* 1.1.4 */

    .domainSnapshotNum = testDomainSnapshotNum, /* 1.1.4 */
    .domainSnapshotListNames = testDomainSnapshotListNames, /* 1.1.4 */
    .domainListAllSnapshots = testDomainListAllSnapshots, /* 1.1.4 */
    .domainSnapshotGetXMLDesc = testDomainSnapshotGetXMLDesc, /* 1.1.4 */
    .domainSnapshotNumChildren = testDomainSnapshotNumChildren, /* 1.1.4 */
    .domainSnapshotListChildrenNames = testDomainSnapshotListChildrenNames, /* 1.1.4 */
    .domainSnapshotListAllChildren = testDomainSnapshotListAllChildren, /* 1.1.4 */
    .domainSnapshotLookupByName = testDomainSnapshotLookupByName, /* 1.1.4 */
    .domainHasCurrentSnapshot = testDomainHasCurrentSnapshot, /* 1.1.4 */
    .domainSnapshotGetParent = testDomainSnapshotGetParent, /* 1.1.4 */
    .domainSnapshotCurrent = testDomainSnapshotCurrent, /* 1.1.4 */
    .domainSnapshotIsCurrent = testDomainSnapshotIsCurrent, /* 1.1.4 */
    .domainSnapshotHasMetadata = testDomainSnapshotHasMetadata, /* 1.1.4 */
6929 6930 6931
    .domainSnapshotCreateXML = testDomainSnapshotCreateXML, /* 1.1.4 */
    .domainRevertToSnapshot = testDomainRevertToSnapshot, /* 1.1.4 */
    .domainSnapshotDelete = testDomainSnapshotDelete, /* 1.1.4 */
6932

E
Eric Blake 已提交
6933
    .connectBaselineCPU = testConnectBaselineCPU, /* 1.2.0 */
6934 6935 6936
};

static virNetworkDriver testNetworkDriver = {
6937 6938 6939 6940 6941
    .connectNumOfNetworks = testConnectNumOfNetworks, /* 0.3.2 */
    .connectListNetworks = testConnectListNetworks, /* 0.3.2 */
    .connectNumOfDefinedNetworks = testConnectNumOfDefinedNetworks, /* 0.3.2 */
    .connectListDefinedNetworks = testConnectListDefinedNetworks, /* 0.3.2 */
    .connectListAllNetworks = testConnectListAllNetworks, /* 0.10.2 */
6942 6943
    .connectNetworkEventRegisterAny = testConnectNetworkEventRegisterAny, /* 1.2.1 */
    .connectNetworkEventDeregisterAny = testConnectNetworkEventDeregisterAny, /* 1.2.1 */
6944 6945 6946 6947
    .networkLookupByUUID = testNetworkLookupByUUID, /* 0.3.2 */
    .networkLookupByName = testNetworkLookupByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreateXML, /* 0.3.2 */
    .networkDefineXML = testNetworkDefineXML, /* 0.3.2 */
6948
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
6949
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
6950
    .networkCreate = testNetworkCreate, /* 0.3.2 */
6951 6952 6953 6954 6955 6956 6957
    .networkDestroy = testNetworkDestroy, /* 0.3.2 */
    .networkGetXMLDesc = testNetworkGetXMLDesc, /* 0.3.2 */
    .networkGetBridgeName = testNetworkGetBridgeName, /* 0.3.2 */
    .networkGetAutostart = testNetworkGetAutostart, /* 0.3.2 */
    .networkSetAutostart = testNetworkSetAutostart, /* 0.3.2 */
    .networkIsActive = testNetworkIsActive, /* 0.7.3 */
    .networkIsPersistent = testNetworkIsPersistent, /* 0.7.3 */
6958 6959
};

L
Laine Stump 已提交
6960
static virInterfaceDriver testInterfaceDriver = {
6961 6962 6963 6964 6965 6966
    .connectNumOfInterfaces = testConnectNumOfInterfaces, /* 0.7.0 */
    .connectListInterfaces = testConnectListInterfaces, /* 0.7.0 */
    .connectNumOfDefinedInterfaces = testConnectNumOfDefinedInterfaces, /* 0.7.0 */
    .connectListDefinedInterfaces = testConnectListDefinedInterfaces, /* 0.7.0 */
    .interfaceLookupByName = testInterfaceLookupByName, /* 0.7.0 */
    .interfaceLookupByMACString = testInterfaceLookupByMACString, /* 0.7.0 */
6967 6968 6969 6970 6971 6972
    .interfaceGetXMLDesc = testInterfaceGetXMLDesc, /* 0.7.0 */
    .interfaceDefineXML = testInterfaceDefineXML, /* 0.7.0 */
    .interfaceUndefine = testInterfaceUndefine, /* 0.7.0 */
    .interfaceCreate = testInterfaceCreate, /* 0.7.0 */
    .interfaceDestroy = testInterfaceDestroy, /* 0.7.0 */
    .interfaceIsActive = testInterfaceIsActive, /* 0.7.3 */
6973 6974 6975
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
6976 6977 6978
};


6979
static virStorageDriver testStorageDriver = {
6980 6981 6982 6983 6984 6985
    .connectNumOfStoragePools = testConnectNumOfStoragePools, /* 0.5.0 */
    .connectListStoragePools = testConnectListStoragePools, /* 0.5.0 */
    .connectNumOfDefinedStoragePools = testConnectNumOfDefinedStoragePools, /* 0.5.0 */
    .connectListDefinedStoragePools = testConnectListDefinedStoragePools, /* 0.5.0 */
    .connectListAllStoragePools = testConnectListAllStoragePools, /* 0.10.2 */
    .connectFindStoragePoolSources = testConnectFindStoragePoolSources, /* 0.5.0 */
6986 6987
    .connectStoragePoolEventRegisterAny = testConnectStoragePoolEventRegisterAny, /* 2.0.0 */
    .connectStoragePoolEventDeregisterAny = testConnectStoragePoolEventDeregisterAny, /* 2.0.0 */
6988 6989 6990
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
6991 6992
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
6993 6994
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
6995
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
6996 6997 6998 6999 7000 7001 7002
    .storagePoolDestroy = testStoragePoolDestroy, /* 0.5.0 */
    .storagePoolDelete = testStoragePoolDelete, /* 0.5.0 */
    .storagePoolRefresh = testStoragePoolRefresh, /* 0.5.0 */
    .storagePoolGetInfo = testStoragePoolGetInfo, /* 0.5.0 */
    .storagePoolGetXMLDesc = testStoragePoolGetXMLDesc, /* 0.5.0 */
    .storagePoolGetAutostart = testStoragePoolGetAutostart, /* 0.5.0 */
    .storagePoolSetAutostart = testStoragePoolSetAutostart, /* 0.5.0 */
7003
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
7004 7005 7006
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

7007 7008 7009 7010 7011 7012 7013 7014 7015
    .storageVolLookupByName = testStorageVolLookupByName, /* 0.5.0 */
    .storageVolLookupByKey = testStorageVolLookupByKey, /* 0.5.0 */
    .storageVolLookupByPath = testStorageVolLookupByPath, /* 0.5.0 */
    .storageVolCreateXML = testStorageVolCreateXML, /* 0.5.0 */
    .storageVolCreateXMLFrom = testStorageVolCreateXMLFrom, /* 0.6.4 */
    .storageVolDelete = testStorageVolDelete, /* 0.5.0 */
    .storageVolGetInfo = testStorageVolGetInfo, /* 0.5.0 */
    .storageVolGetXMLDesc = testStorageVolGetXMLDesc, /* 0.5.0 */
    .storageVolGetPath = testStorageVolGetPath, /* 0.5.0 */
7016 7017
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
7018 7019
};

7020
static virNodeDeviceDriver testNodeDeviceDriver = {
7021 7022
    .connectNodeDeviceEventRegisterAny = testConnectNodeDeviceEventRegisterAny, /* 2.2.0 */
    .connectNodeDeviceEventDeregisterAny = testConnectNodeDeviceEventDeregisterAny, /* 2.2.0 */
7023 7024 7025 7026 7027 7028 7029 7030 7031
    .nodeNumOfDevices = testNodeNumOfDevices, /* 0.7.2 */
    .nodeListDevices = testNodeListDevices, /* 0.7.2 */
    .nodeDeviceLookupByName = testNodeDeviceLookupByName, /* 0.7.2 */
    .nodeDeviceGetXMLDesc = testNodeDeviceGetXMLDesc, /* 0.7.2 */
    .nodeDeviceGetParent = testNodeDeviceGetParent, /* 0.7.2 */
    .nodeDeviceNumOfCaps = testNodeDeviceNumOfCaps, /* 0.7.2 */
    .nodeDeviceListCaps = testNodeDeviceListCaps, /* 0.7.2 */
    .nodeDeviceCreateXML = testNodeDeviceCreateXML, /* 0.7.3 */
    .nodeDeviceDestroy = testNodeDeviceDestroy, /* 0.7.3 */
7032 7033
};

7034
static virConnectDriver testConnectDriver = {
7035
    .localOnly = true,
7036
    .uriSchemes = (const char *[]){ "test", NULL },
7037 7038 7039 7040 7041 7042 7043
    .hypervisorDriver = &testHypervisorDriver,
    .interfaceDriver = &testInterfaceDriver,
    .networkDriver = &testNetworkDriver,
    .nodeDeviceDriver = &testNodeDeviceDriver,
    .nwfilterDriver = NULL,
    .secretDriver = NULL,
    .storageDriver = &testStorageDriver,
7044 7045
};

7046 7047 7048 7049 7050 7051 7052 7053
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
7054 7055
    return virRegisterConnectDriver(&testConnectDriver,
                                    false);
7056
}