test_driver.c 197.4 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;
L
Laine Stump 已提交
100
    virInterfaceObjList ifaces;
101 102
    bool transaction_running;
    virInterfaceObjList backupIfaces;
C
Cole Robinson 已提交
103
    virStoragePoolObjList pools;
104
    virNodeDeviceObjList 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 defaultConn;
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 155 156 157 158
static void
testDriverFree(testDriverPtr driver)
{
    if (!driver)
        return;

    virObjectUnref(driver->caps);
    virObjectUnref(driver->xmlopt);
    virObjectUnref(driver->domains);
    virNodeDeviceObjListFree(&driver->devs);
    virObjectUnref(driver->networks);
    virInterfaceObjListFree(&driver->ifaces);
    virStoragePoolObjListFree(&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)) ||
418 419 420 421 422
        !(ret->eventState = virObjectEventStateNew()) ||
        !(ret->domains = virDomainObjListNew()) ||
        !(ret->networks = virNetworkObjListNew()))
        goto error;

423
    virAtomicIntSet(&ret->nextDomID, 1);
424 425 426 427 428 429 430 431 432

    return ret;

 error:
    testDriverFree(ret);
    return NULL;
}


433 434
static const char *defaultConnXML =
"<node>"
435 436
"<domain type='test'>"
"  <name>test</name>"
437
"  <uuid>6695eb01-f6a4-8304-79aa-97f2502e193f</uuid>"
438 439 440 441 442 443
"  <memory>8388608</memory>"
"  <currentMemory>2097152</currentMemory>"
"  <vcpu>2</vcpu>"
"  <os>"
"    <type>hvm</type>"
"  </os>"
444 445
"</domain>"
""
446 447
"<network>"
"  <name>default</name>"
448
"  <uuid>dd8fe884-6c02-601e-7551-cca97df1c5df</uuid>"
449
"  <bridge name='virbr0'/>"
450 451 452
"  <forward/>"
"  <ip address='192.168.122.1' netmask='255.255.255.0'>"
"    <dhcp>"
453
"      <range start='192.168.122.2' end='192.168.122.254'/>"
454 455
"    </dhcp>"
"  </ip>"
456 457
"</network>"
""
L
Laine Stump 已提交
458 459 460 461 462 463 464 465
"<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>"
466 467
"</interface>"
""
C
Cole Robinson 已提交
468 469
"<pool type='dir'>"
"  <name>default-pool</name>"
470
"  <uuid>dfe224cb-28fb-8dd0-c4b2-64eb3f0f4566</uuid>"
C
Cole Robinson 已提交
471 472 473
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
"</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>"
492
"<device>"
493
"  <name>scsi_host1</name>"
494 495 496
"  <parent>computer</parent>"
"  <capability type='scsi_host'>"
"    <host>1</host>"
497
"    <unique_id>0</unique_id>"
498 499 500
"    <capability type='fc_host'>"
"      <wwnn>2000000012341234</wwnn>"
"      <wwpn>1000000012341234</wwpn>"
501 502 503 504
"      <fabric_wwn>2000000043214321</fabric_wwn>"
"    </capability>"
"    <capability type='vport_ops'>"
"      <max_vports>127</max_vports>"
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
"      <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>"
522
"      <vports>0</vports>"
523 524 525
"    </capability>"
"  </capability>"
"</device>"
526 527 528 529 530 531 532 533 534 535 536 537 538
"<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>"
539 540
"</node>";

C
Cole Robinson 已提交
541

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
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";

565
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
566
static const unsigned long long defaultPoolAlloc;
C
Cole Robinson 已提交
567

568
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool);
569
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
570

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

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

    return vm;
}

589
static char *
590 591
testDomainGenerateIfname(virDomainDefPtr domdef)
{
592
    int maxif = 1024;
593 594
    int ifctr;
    size_t i;
595 596 597 598 599

    for (ifctr = 0; ifctr < maxif; ++ifctr) {
        char *ifname;
        int found = 0;

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

        /* Generate network interface names */
604
        for (i = 0; i < domdef->nnets; i++) {
605
            if (domdef->nets[i]->ifname &&
606
                STREQ(domdef->nets[i]->ifname, ifname)) {
607 608 609 610 611 612 613
                found = 1;
                break;
            }
        }

        if (!found)
            return ifname;
614
        VIR_FREE(ifname);
615 616
    }

617 618
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Exceeded max iface limit %d"), maxif);
619 620 621
    return NULL;
}

622
static int
623
testDomainGenerateIfnames(virDomainDefPtr domdef)
624
{
625
    size_t i = 0;
626 627 628 629 630 631

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

632
        ifname = testDomainGenerateIfname(domdef);
633
        if (!ifname)
634
            return -1;
635 636 637 638

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

639
    return 0;
640 641
}

642

643 644
static void
testDomainShutdownState(virDomainPtr domain,
J
Jiri Denemark 已提交
645 646
                        virDomainObjPtr privdom,
                        virDomainShutoffReason reason)
647
{
648
    virDomainObjRemoveTransientDef(privdom);
J
Jiri Denemark 已提交
649
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF, reason);
650

651 652 653 654
    if (domain)
        domain->id = -1;
}

655
/* Set up domain runtime state */
656
static int
657
testDomainStartState(testDriverPtr privconn,
J
Jiri Denemark 已提交
658 659
                     virDomainObjPtr dom,
                     virDomainRunningReason reason)
660
{
661
    int ret = -1;
662

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

666
    if (virDomainObjSetDefTransient(privconn->caps,
667
                                    privconn->xmlopt,
668
                                    dom) < 0) {
669 670 671
        goto cleanup;
    }

C
Cole Robinson 已提交
672
    dom->hasManagedSave = false;
673
    ret = 0;
674
 cleanup:
675
    if (ret < 0)
J
Jiri Denemark 已提交
676
        testDomainShutdownState(NULL, dom, VIR_DOMAIN_SHUTOFF_FAILED);
677
    return ret;
678
}
679

680

681
static char *testBuildFilename(const char *relativeTo,
682 683
                               const char *filename)
{
684 685
    char *offset;
    int baseLen;
686 687
    char *ret;

688
    if (!filename || filename[0] == '\0')
689
        return NULL;
690 691 692 693
    if (filename[0] == '/') {
        ignore_value(VIR_STRDUP(ret, filename));
        return ret;
    }
694

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

C
Cole Robinson 已提交
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 738 739 740 741 742 743
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;
    }

744
 error:
C
Cole Robinson 已提交
745 746 747 748 749
    xmlFreeDoc(doc);
    VIR_FREE(absFile);
    return ret;
}

750 751 752
static int
testParseNodeInfo(virNodeInfoPtr nodeInfo, xmlXPathContextPtr ctxt)
{
753
    char *str;
754 755
    long l;
    int ret;
756

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

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

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

784
    ret = virXPathLong("string(/node/cpu/threads[1])", ctxt, &l);
785 786 787
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
788 789
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu threads value"));
790
        goto error;
791
    }
792

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

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

824
    ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
825 826 827
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
828 829
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node memory value"));
830
        goto error;
831
    }
832

833
    return 0;
834
 error:
835 836 837
    return -1;
}

838
static int
839
testParseDomainSnapshots(testDriverPtr privconn,
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 884 885 886 887 888 889
                         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;
890
 error:
891 892 893
    return ret;
}

894
static int
895
testParseDomains(testDriverPtr privconn,
C
Cole Robinson 已提交
896 897
                 const char *file,
                 xmlXPathContextPtr ctxt)
898 899 900 901 902 903 904
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virDomainObjPtr obj;

    num = virXPathNodeSet("/node/domain", ctxt, &nodes);
905
    if (num < 0)
906 907
        goto error;

908
    for (i = 0; i < num; i++) {
909
        virDomainDefPtr def;
910
        testDomainNamespaceDefPtr nsdata;
C
Cole Robinson 已提交
911 912 913 914 915
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file, "domain");
        if (!node)
            goto error;

        def = virDomainDefParseNode(ctxt->doc, node,
916
                                    privconn->caps, privconn->xmlopt, NULL,
917
                                    VIR_DOMAIN_DEF_PARSE_INACTIVE);
C
Cole Robinson 已提交
918 919
        if (!def)
            goto error;
920

921
        if (testDomainGenerateIfnames(def) < 0 ||
922
            !(obj = virDomainObjListAdd(privconn->domains,
923
                                        def,
924
                                        privconn->xmlopt,
925
                                        0, NULL))) {
926
            virDomainDefFree(def);
927 928
            goto error;
        }
929

930 931 932 933 934
        if (testParseDomainSnapshots(privconn, obj, file, ctxt) < 0) {
            virObjectUnlock(obj);
            goto error;
        }

935
        nsdata = def->namespaceData;
936
        obj->persistent = !nsdata->transient;
C
Cole Robinson 已提交
937
        obj->hasManagedSave = nsdata->hasManagedSave;
938 939 940 941 942 943 944 945 946

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

950
        virObjectUnlock(obj);
951
    }
952

953
    ret = 0;
954
 error:
955 956 957 958 959
    VIR_FREE(nodes);
    return ret;
}

static int
960
testParseNetworks(testDriverPtr privconn,
C
Cole Robinson 已提交
961 962
                  const char *file,
                  xmlXPathContextPtr ctxt)
963 964 965 966 967 968 969
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virNetworkObjPtr obj;

    num = virXPathNodeSet("/node/network", ctxt, &nodes);
970
    if (num < 0)
971
        goto error;
972 973

    for (i = 0; i < num; i++) {
974
        virNetworkDefPtr def;
C
Cole Robinson 已提交
975 976 977
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file, "network");
        if (!node)
            goto error;
978

C
Cole Robinson 已提交
979 980 981
        def = virNetworkDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
982

983
        if (!(obj = virNetworkObjAssignDef(privconn->networks, def, 0))) {
984 985
            virNetworkDefFree(def);
            goto error;
986
        }
987 988

        obj->active = 1;
989
        virNetworkObjEndAPI(&obj);
990
    }
991

992
    ret = 0;
993
 error:
994 995 996 997 998
    VIR_FREE(nodes);
    return ret;
}

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

    num = virXPathNodeSet("/node/interface", ctxt, &nodes);
1009
    if (num < 0)
L
Laine Stump 已提交
1010
        goto error;
1011 1012

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

C
Cole Robinson 已提交
1019 1020 1021
        def = virInterfaceDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
1022

1023
        if (!(obj = virInterfaceObjAssignDef(&privconn->ifaces, def))) {
L
Laine Stump 已提交
1024 1025 1026
            virInterfaceDefFree(def);
            goto error;
        }
1027

1028 1029 1030 1031 1032
        obj->active = 1;
        virInterfaceObjUnlock(obj);
    }

    ret = 0;
1033
 error:
1034 1035 1036 1037 1038
    VIR_FREE(nodes);
    return ret;
}

static int
C
Cole Robinson 已提交
1039
testOpenVolumesForPool(const char *file,
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
                       xmlXPathContextPtr ctxt,
                       virStoragePoolObjPtr pool,
                       int poolidx)
{
    char *vol_xpath;
    size_t i;
    int num, ret = -1;
    xmlNodePtr *nodes = NULL;
    virStorageVolDefPtr def = NULL;

    /* Find storage volumes */
    if (virAsprintf(&vol_xpath, "/node/pool[%d]/volume", poolidx) < 0)
        goto error;

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

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

1065
        def = virStorageVolDefParseNode(pool->def, ctxt->doc, node, 0);
C
Cole Robinson 已提交
1066 1067
        if (!def)
            goto error;
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

        if (def->target.path == NULL) {
            if (virAsprintf(&def->target.path, "%s/%s",
                            pool->def->target.path,
                            def->name) == -1)
                goto error;
        }

        if (!def->key && VIR_STRDUP(def->key, def->target.path) < 0)
            goto error;
1078 1079
        if (VIR_APPEND_ELEMENT_COPY(pool->volumes.objs, pool->volumes.count, def) < 0)
            goto error;
1080

1081
        pool->def->allocation += def->target.allocation;
1082 1083 1084
        pool->def->available = (pool->def->capacity -
                                pool->def->allocation);
        def = NULL;
L
Laine Stump 已提交
1085 1086
    }

1087
    ret = 0;
1088
 error:
1089 1090 1091 1092 1093 1094
    virStorageVolDefFree(def);
    VIR_FREE(nodes);
    return ret;
}

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

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

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

C
Cole Robinson 已提交
1115 1116 1117
        def = virStoragePoolDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
C
Cole Robinson 已提交
1118

1119
        if (!(obj = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
1120 1121 1122 1123 1124
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

1125 1126
        if (testStoragePoolObjSetDefaults(obj) == -1) {
            virStoragePoolObjUnlock(obj);
C
Cole Robinson 已提交
1127
            goto error;
1128
        }
1129
        obj->active = 1;
1130 1131

        /* Find storage volumes */
C
Cole Robinson 已提交
1132
        if (testOpenVolumesForPool(file, ctxt, obj, i+1) < 0) {
1133
            virStoragePoolObjUnlock(obj);
1134 1135 1136
            goto error;
        }

1137
        virStoragePoolObjUnlock(obj);
C
Cole Robinson 已提交
1138 1139
    }

1140
    ret = 0;
1141
 error:
1142 1143 1144 1145 1146
    VIR_FREE(nodes);
    return ret;
}

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

    num = virXPathNodeSet("/node/device", ctxt, &nodes);
1157
    if (num < 0)
1158
        goto error;
1159 1160

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

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

1171
        if (!(obj = virNodeDeviceObjAssignDef(&privconn->devs, def))) {
1172 1173 1174
            virNodeDeviceDefFree(def);
            goto error;
        }
1175 1176 1177 1178 1179

        virNodeDeviceObjUnlock(obj);
    }

    ret = 0;
1180
 error:
1181 1182 1183 1184
    VIR_FREE(nodes);
    return ret;
}

1185
static int
1186
testParseAuthUsers(testDriverPtr privconn,
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 1217 1218 1219
                   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;
1220
 error:
1221 1222 1223
    VIR_FREE(nodes);
    return ret;
}
1224

C
Cole Robinson 已提交
1225 1226 1227 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 1253 1254 1255
static int
testOpenParse(testDriverPtr privconn,
              const char *file,
              xmlXPathContextPtr ctxt)
{
    if (!xmlStrEqual(ctxt->node->name, BAD_CAST "node")) {
        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;
}

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

1265
    if (!(privconn = testDriverNew()))
1266
        return VIR_DRV_OPEN_ERROR;
1267

1268 1269 1270 1271 1272 1273
    testDriverLock(privconn);
    conn->privateData = privconn;

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

1274
    if (!(doc = virXMLParseFileCtxt(file, &ctxt)))
1275 1276 1277 1278 1279
        goto error;

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

C
Cole Robinson 已提交
1280
    if (testOpenParse(privconn, file, ctxt) < 0)
1281
        goto error;
1282

J
Jim Meyering 已提交
1283
    xmlXPathFreeContext(ctxt);
1284
    xmlFreeDoc(doc);
1285
    testDriverUnlock(privconn);
1286

1287
    return 0;
1288 1289

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

1297 1298 1299 1300 1301 1302 1303
/* 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)
{
    testDriverPtr privconn = NULL;
1304 1305
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
1306
    size_t i;
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323

    virMutexLock(&defaultLock);
    if (defaultConnections++) {
        conn->privateData = defaultConn;
        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;
1324 1325 1326 1327
    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;
1328
    }
1329
    for (i = 0; i < 16; i++) {
1330 1331 1332
        virBitmapPtr siblings = virBitmapNew(16);
        if (!siblings)
            goto error;
1333 1334 1335 1336 1337
        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;
1338 1339 1340 1341 1342
    }

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

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

1347
    if (testOpenParse(privconn, NULL, ctxt) < 0)
1348 1349 1350 1351
        goto error;

    defaultConn = privconn;

1352 1353
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);
1354 1355 1356 1357 1358 1359
    virMutexUnlock(&defaultLock);

    return VIR_DRV_OPEN_SUCCESS;

 error:
    testDriverFree(privconn);
1360 1361
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);
1362 1363 1364 1365 1366 1367
    conn->privateData = NULL;
    defaultConnections--;
    virMutexUnlock(&defaultLock);
    return VIR_DRV_OPEN_ERROR;
}

1368 1369 1370 1371
static int
testConnectAuthenticate(virConnectPtr conn,
                        virConnectAuthPtr auth)
{
1372
    testDriverPtr privconn = conn->privateData;
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
    int ret = -1;
    ssize_t i;
    char *username = NULL, *password = NULL;

    if (privconn->numAuths == 0)
        return 0;

    /* 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;

1397
 found_user:
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
    /* 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;
1417
 cleanup:
1418 1419 1420 1421
    VIR_FREE(username);
    VIR_FREE(password);
    return ret;
}
1422

1423
static virDrvOpenStatus testConnectOpen(virConnectPtr conn,
1424
                                        virConnectAuthPtr auth,
1425
                                        virConfPtr conf ATTRIBUTE_UNUSED,
1426
                                        unsigned int flags)
1427
{
1428
    int ret;
1429

E
Eric Blake 已提交
1430 1431
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1432
    if (!conn->uri)
1433
        return VIR_DRV_OPEN_DECLINED;
1434

1435
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1436
        return VIR_DRV_OPEN_DECLINED;
1437

1438
    /* Remote driver should handle these. */
1439
    if (conn->uri->server)
1440 1441
        return VIR_DRV_OPEN_DECLINED;

1442
    /* From this point on, the connection is for us. */
1443 1444 1445
    if (!conn->uri->path
        || conn->uri->path[0] == '\0'
        || (conn->uri->path[0] == '/' && conn->uri->path[1] == '\0')) {
1446 1447
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("testOpen: supply a path or use test:///default"));
1448 1449
        return VIR_DRV_OPEN_ERROR;
    }
1450

1451
    if (STREQ(conn->uri->path, "/default"))
1452 1453
        ret = testOpenDefault(conn);
    else
1454
        ret = testOpenFromFile(conn,
1455
                               conn->uri->path);
1456

1457 1458 1459
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

1460 1461 1462 1463
    /* Fake authentication. */
    if (testConnectAuthenticate(conn, auth) < 0)
        return VIR_DRV_OPEN_ERROR;

1464
    return VIR_DRV_OPEN_SUCCESS;
1465 1466
}

1467
static int testConnectClose(virConnectPtr conn)
1468
{
1469
    testDriverPtr privconn = conn->privateData;
1470
    bool dflt = false;
1471

1472 1473
    if (privconn == defaultConn) {
        dflt = true;
1474 1475 1476 1477 1478 1479 1480
        virMutexLock(&defaultLock);
        if (--defaultConnections) {
            virMutexUnlock(&defaultLock);
            return 0;
        }
    }

1481
    testDriverLock(privconn);
1482
    testDriverFree(privconn);
1483 1484 1485

    if (dflt) {
        defaultConn = NULL;
1486
        virMutexUnlock(&defaultLock);
1487 1488
    }

1489
    conn->privateData = NULL;
1490
    return 0;
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 1533 1534 1535 1536 1537 1538 1539 1540 1541
static char *
testConnectBaselineCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
                       const char **xmlCPUs,
                       unsigned int ncpus,
                       unsigned int flags)
{
    char *cpu;

    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, NULL);

    cpu = cpuBaselineXML(xmlCPUs, ncpus, NULL, 0, flags);

    return cpu;
}

1542 1543
static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1544
{
1545
    testDriverPtr privconn = conn->privateData;
1546
    testDriverLock(privconn);
1547
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1548
    testDriverUnlock(privconn);
1549
    return 0;
1550 1551
}

1552
static char *testConnectGetCapabilities(virConnectPtr conn)
1553
{
1554
    testDriverPtr privconn = conn->privateData;
1555
    char *xml;
1556
    testDriverLock(privconn);
1557
    xml = virCapabilitiesFormatXML(privconn->caps);
1558
    testDriverUnlock(privconn);
1559
    return xml;
1560 1561
}

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
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;
}

1582 1583 1584 1585 1586 1587
static const char *
testConnectGetType(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return "TEST";
}

1588
static int testConnectNumOfDomains(virConnectPtr conn)
1589
{
1590
    testDriverPtr privconn = conn->privateData;
1591
    int count;
1592

1593
    testDriverLock(privconn);
1594
    count = virDomainObjListNumOfDomains(privconn->domains, true, NULL, NULL);
1595
    testDriverUnlock(privconn);
1596

1597
    return count;
1598 1599
}

1600 1601 1602
static int testDomainIsActive(virDomainPtr dom)
{
    virDomainObjPtr obj;
1603
    int ret;
1604

1605 1606
    if (!(obj = testDomObjFromDomain(dom)))
        return -1;
1607

1608 1609
    ret = virDomainObjIsActive(obj);
    virDomainObjEndAPI(&obj);
1610 1611 1612 1613 1614 1615
    return ret;
}

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

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

    ret = obj->persistent;

1623
    virDomainObjEndAPI(&obj);
1624 1625 1626
    return ret;
}

1627 1628 1629 1630 1631
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

1632
static virDomainPtr
1633
testDomainCreateXML(virConnectPtr conn, const char *xml,
1634
                      unsigned int flags)
1635
{
1636
    testDriverPtr privconn = conn->privateData;
1637
    virDomainPtr ret = NULL;
1638
    virDomainDefPtr def;
1639
    virDomainObjPtr dom = NULL;
1640
    virObjectEventPtr event = NULL;
1641
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
1642

1643 1644 1645
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_START_VALIDATE)
1646
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
1647

1648
    testDriverLock(privconn);
1649
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
1650
                                       NULL, parse_flags)) == NULL)
1651
        goto cleanup;
1652

1653
    if (testDomainGenerateIfnames(def) < 0)
1654
        goto cleanup;
1655
    if (!(dom = virDomainObjListAdd(privconn->domains,
1656
                                    def,
1657
                                    privconn->xmlopt,
1658
                                    VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
1659 1660
                                    VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                    NULL)))
1661 1662
        goto cleanup;
    def = NULL;
1663

1664 1665 1666 1667 1668
    if (testDomainStartState(privconn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
        if (!dom->persistent) {
            virDomainObjListRemove(privconn->domains, dom);
            dom = NULL;
        }
1669
        goto cleanup;
1670
    }
1671

1672
    event = virDomainEventLifecycleNewFromObj(dom,
1673 1674 1675
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1676
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1677

1678
 cleanup:
1679
    if (dom)
1680
        virObjectUnlock(dom);
1681
    testObjectEventQueue(privconn, event);
1682
    virDomainDefFree(def);
1683
    testDriverUnlock(privconn);
1684
    return ret;
1685 1686 1687
}


1688
static virDomainPtr testDomainLookupByID(virConnectPtr conn,
1689
                                         int id)
1690
{
1691
    testDriverPtr privconn = conn->privateData;
1692 1693
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1694

1695
    if (!(dom = virDomainObjListFindByID(privconn->domains, id))) {
1696
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1697
        goto cleanup;
1698 1699
    }

1700
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1701

1702
 cleanup:
1703
    if (dom)
1704
        virObjectUnlock(dom);
1705
    return ret;
1706 1707
}

1708
static virDomainPtr testDomainLookupByUUID(virConnectPtr conn,
1709
                                           const unsigned char *uuid)
1710
{
1711
    testDriverPtr privconn = conn->privateData;
1712
    virDomainPtr ret = NULL;
1713
    virDomainObjPtr dom;
1714

1715
    if (!(dom = virDomainObjListFindByUUID(privconn->domains, uuid))) {
1716
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1717
        goto cleanup;
1718
    }
1719

1720
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1721

1722
 cleanup:
1723
    if (dom)
1724
        virObjectUnlock(dom);
1725
    return ret;
1726 1727
}

1728
static virDomainPtr testDomainLookupByName(virConnectPtr conn,
1729
                                           const char *name)
1730
{
1731
    testDriverPtr privconn = conn->privateData;
1732 1733
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1734

1735
    if (!(dom = virDomainObjListFindByName(privconn->domains, name))) {
1736
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1737
        goto cleanup;
1738
    }
1739

1740
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1741

1742
 cleanup:
1743
    virDomainObjEndAPI(&dom);
1744
    return ret;
1745 1746
}

1747 1748 1749
static int testConnectListDomains(virConnectPtr conn,
                                  int *ids,
                                  int maxids)
1750
{
1751
    testDriverPtr privconn = conn->privateData;
1752

1753 1754
    return virDomainObjListGetActiveIDs(privconn->domains, ids, maxids,
                                        NULL, NULL);
1755 1756
}

1757
static int testDomainDestroy(virDomainPtr domain)
1758
{
1759
    testDriverPtr privconn = domain->conn->privateData;
1760
    virDomainObjPtr privdom;
1761
    virObjectEventPtr event = NULL;
1762
    int ret = -1;
1763

1764
    if (!(privdom = testDomObjFromDomain(domain)))
1765
        goto cleanup;
1766

1767 1768 1769 1770 1771 1772
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto cleanup;
    }

J
Jiri Denemark 已提交
1773
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1774
    event = virDomainEventLifecycleNewFromObj(privdom,
1775 1776
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1777

1778 1779
    if (!privdom->persistent)
        virDomainObjListRemove(privconn->domains, privdom);
1780 1781

    ret = 0;
1782
 cleanup:
1783
    virDomainObjEndAPI(&privdom);
1784
    testObjectEventQueue(privconn, event);
1785
    return ret;
1786 1787
}

1788
static int testDomainResume(virDomainPtr domain)
1789
{
1790
    testDriverPtr privconn = domain->conn->privateData;
1791
    virDomainObjPtr privdom;
1792
    virObjectEventPtr event = NULL;
1793
    int ret = -1;
1794

1795 1796
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1797

J
Jiri Denemark 已提交
1798
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_PAUSED) {
1799 1800
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
                       domain->name);
1801
        goto cleanup;
1802
    }
1803

J
Jiri Denemark 已提交
1804 1805
    virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                         VIR_DOMAIN_RUNNING_UNPAUSED);
1806
    event = virDomainEventLifecycleNewFromObj(privdom,
1807 1808
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1809 1810
    ret = 0;

1811
 cleanup:
1812
    virDomainObjEndAPI(&privdom);
1813
    testObjectEventQueue(privconn, event);
1814
    return ret;
1815 1816
}

1817
static int testDomainSuspend(virDomainPtr domain)
1818
{
1819
    testDriverPtr privconn = domain->conn->privateData;
1820
    virDomainObjPtr privdom;
1821
    virObjectEventPtr event = NULL;
1822
    int ret = -1;
J
Jiri Denemark 已提交
1823
    int state;
1824

1825 1826
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1827

J
Jiri Denemark 已提交
1828 1829
    state = virDomainObjGetState(privdom, NULL);
    if (state == VIR_DOMAIN_SHUTOFF || state == VIR_DOMAIN_PAUSED) {
1830 1831
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
                       domain->name);
1832
        goto cleanup;
1833
    }
1834

J
Jiri Denemark 已提交
1835
    virDomainObjSetState(privdom, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
1836
    event = virDomainEventLifecycleNewFromObj(privdom,
1837 1838
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1839 1840
    ret = 0;

1841
 cleanup:
1842
    virDomainObjEndAPI(&privdom);
1843
    testObjectEventQueue(privconn, event);
1844
    return ret;
1845 1846
}

1847
static int testDomainShutdownFlags(virDomainPtr domain,
1848
                                   unsigned int flags)
1849
{
1850
    testDriverPtr privconn = domain->conn->privateData;
1851
    virDomainObjPtr privdom;
1852
    virObjectEventPtr event = NULL;
1853
    int ret = -1;
1854

1855 1856
    virCheckFlags(0, -1);

1857

1858
    if (!(privdom = testDomObjFromDomain(domain)))
1859
        goto cleanup;
1860

J
Jiri Denemark 已提交
1861
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
1862 1863
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain '%s' not running"), domain->name);
1864
        goto cleanup;
1865
    }
1866

J
Jiri Denemark 已提交
1867
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1868
    event = virDomainEventLifecycleNewFromObj(privdom,
1869 1870
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1871

1872 1873
    if (!privdom->persistent)
        virDomainObjListRemove(privconn->domains, privdom);
1874

1875
    ret = 0;
1876
 cleanup:
1877
    virDomainObjEndAPI(&privdom);
1878
    testObjectEventQueue(privconn, event);
1879
    return ret;
1880 1881
}

1882
static int testDomainShutdown(virDomainPtr domain)
1883
{
1884
    return testDomainShutdownFlags(domain, 0);
1885 1886
}

1887
/* Similar behaviour as shutdown */
1888
static int testDomainReboot(virDomainPtr domain,
1889
                            unsigned int action ATTRIBUTE_UNUSED)
1890
{
1891
    testDriverPtr privconn = domain->conn->privateData;
1892
    virDomainObjPtr privdom;
1893
    virObjectEventPtr event = NULL;
1894
    int ret = -1;
1895 1896


1897
    if (!(privdom = testDomObjFromDomain(domain)))
1898
        goto cleanup;
1899

1900 1901 1902 1903 1904 1905
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto cleanup;
    }

J
Jiri Denemark 已提交
1906 1907 1908
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

1909 1910
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
J
Jiri Denemark 已提交
1911 1912
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1913 1914
        break;

1915
    case VIR_DOMAIN_LIFECYCLE_RESTART:
J
Jiri Denemark 已提交
1916 1917
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1918 1919
        break;

1920
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
J
Jiri Denemark 已提交
1921 1922
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1923 1924
        break;

1925
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
J
Jiri Denemark 已提交
1926 1927
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1928
        break;
1929

1930
    default:
J
Jiri Denemark 已提交
1931 1932
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1933 1934
        break;
    }
1935

J
Jiri Denemark 已提交
1936 1937
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1938
        event = virDomainEventLifecycleNewFromObj(privdom,
1939 1940
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1941

1942 1943
        if (!privdom->persistent)
            virDomainObjListRemove(privconn->domains, privdom);
1944 1945
    }

1946
    ret = 0;
1947
 cleanup:
1948
    virDomainObjEndAPI(&privdom);
1949
    testObjectEventQueue(privconn, event);
1950
    return ret;
1951 1952
}

1953
static int testDomainGetInfo(virDomainPtr domain,
1954
                             virDomainInfoPtr info)
1955
{
1956
    struct timeval tv;
1957
    virDomainObjPtr privdom;
1958
    int ret = -1;
1959

1960 1961
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1962 1963

    if (gettimeofday(&tv, NULL) < 0) {
1964 1965
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("getting time of day"));
1966
        goto cleanup;
1967 1968
    }

J
Jiri Denemark 已提交
1969
    info->state = virDomainObjGetState(privdom, NULL);
1970
    info->memory = privdom->def->mem.cur_balloon;
1971
    info->maxMem = virDomainDefGetMemoryTotal(privdom->def);
1972
    info->nrVirtCpu = virDomainDefGetVcpus(privdom->def);
1973
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1974 1975
    ret = 0;

1976
 cleanup:
1977
    virDomainObjEndAPI(&privdom);
1978
    return ret;
1979 1980
}

1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
static int
testDomainGetState(virDomainPtr domain,
                   int *state,
                   int *reason,
                   unsigned int flags)
{
    virDomainObjPtr privdom;

    virCheckFlags(0, -1);

1991 1992
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1993

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

1996
    virDomainObjEndAPI(&privdom);
1997 1998

    return 0;
1999 2000
}

2001 2002
#define TEST_SAVE_MAGIC "TestGuestMagic"

2003 2004 2005
static int
testDomainSaveFlags(virDomainPtr domain, const char *path,
                    const char *dxml, unsigned int flags)
2006
{
2007
    testDriverPtr privconn = domain->conn->privateData;
2008 2009 2010
    char *xml = NULL;
    int fd = -1;
    int len;
2011
    virDomainObjPtr privdom;
2012
    virObjectEventPtr event = NULL;
2013
    int ret = -1;
2014

2015 2016
    virCheckFlags(0, -1);
    if (dxml) {
2017 2018
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
2019 2020 2021
        return -1;
    }

2022

2023
    if (!(privdom = testDomObjFromDomain(domain)))
2024
        goto cleanup;
2025

2026 2027 2028 2029 2030 2031
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto cleanup;
    }

2032
    xml = virDomainDefFormat(privdom->def, privconn->caps,
2033
                             VIR_DOMAIN_DEF_FORMAT_SECURE);
C
Cole Robinson 已提交
2034

2035
    if (xml == NULL) {
2036
        virReportSystemError(errno,
2037 2038
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
2039
        goto cleanup;
2040
    }
2041 2042

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
2043
        virReportSystemError(errno,
2044 2045
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
2046
        goto cleanup;
2047
    }
2048
    len = strlen(xml);
2049
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
2050
        virReportSystemError(errno,
2051 2052
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2053
        goto cleanup;
2054
    }
2055
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
2056
        virReportSystemError(errno,
2057 2058
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2059
        goto cleanup;
2060
    }
2061
    if (safewrite(fd, xml, len) < 0) {
2062
        virReportSystemError(errno,
2063 2064
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2065
        goto cleanup;
2066
    }
2067

2068
    if (VIR_CLOSE(fd) < 0) {
2069
        virReportSystemError(errno,
2070 2071
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2072
        goto cleanup;
2073
    }
2074 2075
    fd = -1;

J
Jiri Denemark 已提交
2076
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
2077
    event = virDomainEventLifecycleNewFromObj(privdom,
2078 2079
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
2080

2081 2082
    if (!privdom->persistent)
        virDomainObjListRemove(privconn->domains, privdom);
2083

2084
    ret = 0;
2085
 cleanup:
2086 2087 2088 2089
    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 已提交
2090
     * and have reported an earlier error */
2091
    if (ret != 0) {
2092
        VIR_FORCE_CLOSE(fd);
2093 2094
        unlink(path);
    }
2095
    virDomainObjEndAPI(&privdom);
2096
    testObjectEventQueue(privconn, event);
2097
    return ret;
2098 2099
}

2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
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)
2112
{
2113
    testDriverPtr privconn = conn->privateData;
2114
    char *xml = NULL;
2115
    char magic[15];
2116 2117 2118
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
2119
    virDomainObjPtr dom = NULL;
2120
    virObjectEventPtr event = NULL;
2121
    int ret = -1;
2122

2123 2124
    virCheckFlags(0, -1);
    if (dxml) {
2125 2126
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
2127 2128 2129
        return -1;
    }

2130
    if ((fd = open(path, O_RDONLY)) < 0) {
2131
        virReportSystemError(errno,
2132 2133
                             _("cannot read domain image '%s'"),
                             path);
2134
        goto cleanup;
2135
    }
2136
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
2137
        virReportSystemError(errno,
2138 2139
                             _("incomplete save header in '%s'"),
                             path);
2140
        goto cleanup;
2141
    }
2142
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
2143 2144
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("mismatched header magic"));
2145
        goto cleanup;
2146
    }
2147
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
2148
        virReportSystemError(errno,
2149 2150
                             _("failed to read metadata length in '%s'"),
                             path);
2151
        goto cleanup;
2152 2153
    }
    if (len < 1 || len > 8192) {
2154 2155
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("length of metadata out of range"));
2156
        goto cleanup;
2157
    }
2158
    if (VIR_ALLOC_N(xml, len+1) < 0)
2159
        goto cleanup;
2160
    if (saferead(fd, xml, len) != len) {
2161
        virReportSystemError(errno,
2162
                             _("incomplete metadata in '%s'"), path);
2163
        goto cleanup;
2164 2165
    }
    xml[len] = '\0';
2166

2167
    def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
2168
                                  NULL, VIR_DOMAIN_DEF_PARSE_INACTIVE);
2169
    if (!def)
2170
        goto cleanup;
2171

2172
    if (testDomainGenerateIfnames(def) < 0)
2173
        goto cleanup;
2174
    if (!(dom = virDomainObjListAdd(privconn->domains,
2175
                                    def,
2176
                                    privconn->xmlopt,
2177 2178 2179
                                    VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
                                    VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                    NULL)))
2180 2181
        goto cleanup;
    def = NULL;
2182

2183 2184 2185 2186 2187
    if (testDomainStartState(privconn, dom, VIR_DOMAIN_RUNNING_RESTORED) < 0) {
        if (!dom->persistent) {
            virDomainObjListRemove(privconn->domains, dom);
            dom = NULL;
        }
2188
        goto cleanup;
2189
    }
2190

2191
    event = virDomainEventLifecycleNewFromObj(dom,
2192 2193
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
2194
    ret = 0;
2195

2196
 cleanup:
2197 2198
    virDomainDefFree(def);
    VIR_FREE(xml);
2199
    VIR_FORCE_CLOSE(fd);
2200
    if (dom)
2201
        virObjectUnlock(dom);
2202
    testObjectEventQueue(privconn, event);
2203
    return ret;
2204 2205
}

2206 2207 2208 2209 2210 2211 2212
static int
testDomainRestore(virConnectPtr conn,
                  const char *path)
{
    return testDomainRestoreFlags(conn, path, NULL, 0);
}

2213 2214 2215 2216
static int testDomainCoreDumpWithFormat(virDomainPtr domain,
                                        const char *to,
                                        unsigned int dumpformat,
                                        unsigned int flags)
2217
{
2218
    testDriverPtr privconn = domain->conn->privateData;
2219
    int fd = -1;
2220
    virDomainObjPtr privdom;
2221
    virObjectEventPtr event = NULL;
2222
    int ret = -1;
2223

E
Eric Blake 已提交
2224 2225
    virCheckFlags(VIR_DUMP_CRASH, -1);

2226

2227
    if (!(privdom = testDomObjFromDomain(domain)))
2228
        goto cleanup;
2229

2230 2231 2232 2233 2234 2235
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto cleanup;
    }

2236
    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
2237
        virReportSystemError(errno,
2238 2239
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
2240
        goto cleanup;
2241
    }
2242
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
2243
        virReportSystemError(errno,
2244 2245
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
2246
        goto cleanup;
2247
    }
2248
    if (VIR_CLOSE(fd) < 0) {
2249
        virReportSystemError(errno,
2250 2251
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
2252
        goto cleanup;
2253
    }
2254

2255 2256 2257 2258 2259 2260 2261
    /* 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;
    }

2262
    if (flags & VIR_DUMP_CRASH) {
J
Jiri Denemark 已提交
2263
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_CRASHED);
2264
        event = virDomainEventLifecycleNewFromObj(privdom,
2265 2266
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
2267 2268
        if (!privdom->persistent)
            virDomainObjListRemove(privconn->domains, privdom);
2269
    }
2270

2271
    ret = 0;
2272
 cleanup:
2273
    VIR_FORCE_CLOSE(fd);
2274
    virDomainObjEndAPI(&privdom);
2275
    testObjectEventQueue(privconn, event);
2276
    return ret;
2277 2278
}

2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292

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)
{
2293 2294 2295
    char *ret;

    ignore_value(VIR_STRDUP(ret, "linux"));
2296
    return ret;
2297 2298
}

2299 2300 2301

static unsigned long long
testDomainGetMaxMemory(virDomainPtr domain)
2302
{
2303
    virDomainObjPtr privdom;
2304
    unsigned long long ret = 0;
2305

2306 2307
    if (!(privdom = testDomObjFromDomain(domain)))
        return 0;
2308

2309
    ret = virDomainDefGetMemoryTotal(privdom->def);
2310

2311
    virDomainObjEndAPI(&privdom);
2312
    return ret;
2313 2314
}

2315 2316
static int testDomainSetMaxMemory(virDomainPtr domain,
                                  unsigned long memory)
2317
{
2318 2319
    virDomainObjPtr privdom;

2320 2321
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2322 2323

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

2326
    virDomainObjEndAPI(&privdom);
2327
    return 0;
2328 2329
}

2330 2331
static int testDomainSetMemory(virDomainPtr domain,
                               unsigned long memory)
2332
{
2333
    virDomainObjPtr privdom;
2334
    int ret = -1;
2335

2336 2337
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2338

2339
    if (memory > virDomainDefGetMemoryTotal(privdom->def)) {
2340
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2341
        goto cleanup;
2342
    }
2343

2344
    privdom->def->mem.cur_balloon = memory;
2345 2346
    ret = 0;

2347
 cleanup:
2348
    virDomainObjEndAPI(&privdom);
2349
    return ret;
2350 2351
}

2352 2353
static int
testDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
C
Cole Robinson 已提交
2354
{
2355 2356 2357 2358
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;

2359 2360
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2361 2362
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

2363 2364
    if (!(vm = testDomObjFromDomain(domain)))
        return -1;
2365

2366
    if (!(def = virDomainObjGetOneDef(vm, flags)))
2367
        goto cleanup;
2368

2369 2370 2371
    if (flags & VIR_DOMAIN_VCPU_MAXIMUM)
        ret = virDomainDefGetVcpusMax(def);
    else
2372
        ret = virDomainDefGetVcpus(def);
2373

2374
 cleanup:
2375
    virDomainObjEndAPI(&vm);
2376
    return ret;
C
Cole Robinson 已提交
2377 2378
}

2379 2380 2381
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
2382
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2383 2384 2385 2386 2387 2388 2389
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

static int
testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
                        unsigned int flags)
{
2390
    testDriverPtr driver = domain->conn->privateData;
2391
    virDomainObjPtr privdom = NULL;
2392
    virDomainDefPtr def;
2393
    virDomainDefPtr persistentDef;
C
Cole Robinson 已提交
2394 2395
    int ret = -1, maxvcpus;

2396 2397
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2398 2399
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

2400
    if ((maxvcpus = testConnectGetMaxVcpus(domain->conn, NULL)) < 0)
2401
        return -1;
2402 2403

    if (nrCpus > maxvcpus) {
2404
        virReportError(VIR_ERR_INVALID_ARG,
2405 2406
                       _("requested cpu amount exceeds maximum supported amount "
                         "(%d > %d)"), nrCpus, maxvcpus);
2407 2408
        return -1;
    }
2409

2410 2411
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2412

2413
    if (virDomainObjGetDefs(privdom, flags, &def, &persistentDef) < 0)
C
Cole Robinson 已提交
2414 2415
        goto cleanup;

2416
    if (def && virDomainDefGetVcpusMax(def) < nrCpus) {
2417 2418
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested cpu amount exceeds maximum (%d > %d)"),
2419
                       nrCpus, virDomainDefGetVcpusMax(def));
2420
        goto cleanup;
2421
    }
2422

2423 2424
    if (persistentDef &&
        !(flags & VIR_DOMAIN_VCPU_MAXIMUM) &&
2425
        virDomainDefGetVcpusMax(persistentDef) < nrCpus) {
2426 2427
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested cpu amount exceeds maximum (%d > %d)"),
2428
                       nrCpus, virDomainDefGetVcpusMax(persistentDef));
2429
        goto cleanup;
2430
    }
2431

2432 2433 2434
    if (def &&
        virDomainDefSetVcpus(def, nrCpus) < 0)
        goto cleanup;
2435

2436 2437
    if (persistentDef) {
        if (flags & VIR_DOMAIN_VCPU_MAXIMUM) {
2438 2439
            if (virDomainDefSetVcpusMax(persistentDef, nrCpus,
                                        driver->xmlopt) < 0)
2440
                goto cleanup;
2441
        } else {
2442 2443
            if (virDomainDefSetVcpus(persistentDef, nrCpus) < 0)
                goto cleanup;
2444
        }
2445
    }
2446

2447 2448
    ret = 0;

2449
 cleanup:
2450
    virDomainObjEndAPI(&privdom);
2451
    return ret;
2452 2453
}

2454
static int
2455
testDomainSetVcpus(virDomainPtr domain, unsigned int nrCpus)
2456
{
2457
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2458 2459
}

C
Cole Robinson 已提交
2460 2461 2462 2463 2464 2465
static int testDomainGetVcpus(virDomainPtr domain,
                              virVcpuInfoPtr info,
                              int maxinfo,
                              unsigned char *cpumaps,
                              int maplen)
{
2466
    testDriverPtr privconn = domain->conn->privateData;
C
Cole Robinson 已提交
2467
    virDomainObjPtr privdom;
2468
    virDomainDefPtr def;
2469
    size_t i;
2470
    int hostcpus;
C
Cole Robinson 已提交
2471 2472 2473
    int ret = -1;
    struct timeval tv;
    unsigned long long statbase;
2474
    virBitmapPtr allcpumap = NULL;
C
Cole Robinson 已提交
2475

2476 2477
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
C
Cole Robinson 已提交
2478 2479

    if (!virDomainObjIsActive(privdom)) {
2480
        virReportError(VIR_ERR_OPERATION_INVALID,
2481
                       "%s", _("cannot list vcpus for an inactive domain"));
C
Cole Robinson 已提交
2482 2483 2484
        goto cleanup;
    }

2485
    def = privdom->def;
C
Cole Robinson 已提交
2486 2487

    if (gettimeofday(&tv, NULL) < 0) {
2488
        virReportSystemError(errno,
C
Cole Robinson 已提交
2489 2490 2491 2492 2493 2494 2495
                             "%s", _("getting time of day"));
        goto cleanup;
    }

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

    hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
2496 2497 2498 2499 2500
    if (!(allcpumap = virBitmapNew(hostcpus)))
        goto cleanup;

    virBitmapSetAll(allcpumap);

C
Cole Robinson 已提交
2501
    /* Clamp to actual number of vcpus */
2502 2503
    if (maxinfo > virDomainDefGetVcpus(privdom->def))
        maxinfo = virDomainDefGetVcpus(privdom->def);
C
Cole Robinson 已提交
2504

2505 2506
    memset(info, 0, sizeof(*info) * maxinfo);
    memset(cpumaps, 0, maxinfo * maplen);
C
Cole Robinson 已提交
2507

2508
    for (i = 0; i < maxinfo; i++) {
2509
        virDomainVcpuDefPtr vcpu = virDomainDefGetVcpu(def, i);
2510
        virBitmapPtr bitmap = NULL;
C
Cole Robinson 已提交
2511

2512 2513
        if (!vcpu->online)
            continue;
C
Cole Robinson 已提交
2514

2515 2516
        if (vcpu->cpumask)
            bitmap = vcpu->cpumask;
2517 2518 2519 2520
        else if (def->cpumask)
            bitmap = def->cpumask;
        else
            bitmap = allcpumap;
C
Cole Robinson 已提交
2521

2522 2523
        if (cpumaps)
            virBitmapToDataBuf(bitmap, VIR_GET_CPUMAP(cpumaps, maplen, i), maplen);
C
Cole Robinson 已提交
2524

2525 2526 2527
        info[i].number = i;
        info[i].state = VIR_VCPU_RUNNING;
        info[i].cpu = virBitmapLastSetBit(bitmap);
C
Cole Robinson 已提交
2528

2529 2530
        /* Fake an increasing cpu time value */
        info[i].cpuTime = statbase / 10;
C
Cole Robinson 已提交
2531 2532 2533
    }

    ret = maxinfo;
2534
 cleanup:
2535
    virBitmapFree(allcpumap);
2536
    virDomainObjEndAPI(&privdom);
C
Cole Robinson 已提交
2537 2538 2539
    return ret;
}

C
Cole Robinson 已提交
2540 2541 2542 2543 2544
static int testDomainPinVcpu(virDomainPtr domain,
                             unsigned int vcpu,
                             unsigned char *cpumap,
                             int maplen)
{
2545
    virDomainVcpuDefPtr vcpuinfo;
C
Cole Robinson 已提交
2546
    virDomainObjPtr privdom;
2547
    virDomainDefPtr def;
C
Cole Robinson 已提交
2548 2549
    int ret = -1;

2550 2551
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
C
Cole Robinson 已提交
2552

2553 2554
    def = privdom->def;

C
Cole Robinson 已提交
2555
    if (!virDomainObjIsActive(privdom)) {
2556
        virReportError(VIR_ERR_OPERATION_INVALID,
2557
                       "%s", _("cannot pin vcpus on an inactive domain"));
C
Cole Robinson 已提交
2558 2559 2560
        goto cleanup;
    }

2561 2562
    if (!(vcpuinfo = virDomainDefGetVcpu(def, vcpu)) ||
        !vcpuinfo->online) {
2563 2564 2565
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested vcpu '%d' is not present in the domain"),
                       vcpu);
C
Cole Robinson 已提交
2566 2567 2568
        goto cleanup;
    }

2569 2570 2571
    virBitmapFree(vcpuinfo->cpumask);

    if (!(vcpuinfo->cpumask = virBitmapNewData(cpumap, maplen)))
2572
        goto cleanup;
C
Cole Robinson 已提交
2573 2574

    ret = 0;
2575

2576
 cleanup:
2577
    virDomainObjEndAPI(&privdom);
C
Cole Robinson 已提交
2578 2579 2580
    return ret;
}

2581 2582 2583 2584 2585 2586 2587
static int
testDomainGetVcpuPinInfo(virDomainPtr dom,
                        int ncpumaps,
                        unsigned char *cpumaps,
                        int maplen,
                        unsigned int flags)
{
2588
    testDriverPtr driver = dom->conn->privateData;
2589 2590
    virDomainObjPtr privdom;
    virDomainDefPtr def;
2591
    int ret = -1;
2592 2593 2594 2595 2596 2597 2598

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

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

2599 2600 2601
    ret = virDomainDefGetVcpuPinInfoHelper(def, maplen, ncpumaps, cpumaps,
                                           VIR_NODEINFO_MAXCPUS(driver->nodeInfo),
                                           NULL);
2602 2603 2604 2605 2606 2607

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

2608
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2609
{
2610
    testDriverPtr privconn = domain->conn->privateData;
2611
    virDomainDefPtr def;
2612
    virDomainObjPtr privdom;
2613 2614
    char *ret = NULL;

2615 2616
    /* Flags checked by virDomainDefFormat */

2617 2618
    if (!(privdom = testDomObjFromDomain(domain)))
        return NULL;
2619

2620 2621
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2622

2623 2624
    ret = virDomainDefFormat(def, privconn->caps,
                             virDomainDefFormatConvertXMLFlags(flags));
2625

2626
    virDomainObjEndAPI(&privdom);
2627
    return ret;
2628
}
2629

2630 2631
static int testConnectNumOfDefinedDomains(virConnectPtr conn)
{
2632
    testDriverPtr privconn = conn->privateData;
2633

2634
    return virDomainObjListNumOfDomains(privconn->domains, false, NULL, NULL);
2635 2636
}

2637 2638
static int testConnectListDefinedDomains(virConnectPtr conn,
                                         char **const names,
2639 2640
                                         int maxnames)
{
2641

2642
    testDriverPtr privconn = conn->privateData;
2643 2644

    memset(names, 0, sizeof(*names)*maxnames);
2645 2646
    return virDomainObjListGetInactiveNames(privconn->domains, names, maxnames,
                                            NULL, NULL);
2647 2648
}

2649 2650 2651
static virDomainPtr testDomainDefineXMLFlags(virConnectPtr conn,
                                             const char *xml,
                                             unsigned int flags)
2652
{
2653
    testDriverPtr privconn = conn->privateData;
2654
    virDomainPtr ret = NULL;
2655
    virDomainDefPtr def;
2656
    virDomainObjPtr dom = NULL;
2657
    virObjectEventPtr event = NULL;
2658
    virDomainDefPtr oldDef = NULL;
2659 2660 2661
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;

    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);
2662

2663
    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
2664
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
2665

2666
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
2667
                                       NULL, parse_flags)) == NULL)
2668
        goto cleanup;
2669

2670 2671 2672
    if (virXMLCheckIllegalChars("name", def->name, "\n") < 0)
        goto cleanup;

2673
    if (testDomainGenerateIfnames(def) < 0)
2674
        goto cleanup;
2675
    if (!(dom = virDomainObjListAdd(privconn->domains,
2676
                                    def,
2677
                                    privconn->xmlopt,
2678 2679
                                    0,
                                    &oldDef)))
2680
        goto cleanup;
2681
    def = NULL;
2682
    dom->persistent = 1;
2683

2684
    event = virDomainEventLifecycleNewFromObj(dom,
2685
                                     VIR_DOMAIN_EVENT_DEFINED,
2686
                                     !oldDef ?
2687 2688
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2689

2690
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
2691

2692
 cleanup:
2693
    virDomainDefFree(def);
2694
    virDomainDefFree(oldDef);
2695
    if (dom)
2696
        virObjectUnlock(dom);
2697
    testObjectEventQueue(privconn, event);
2698
    return ret;
2699 2700
}

2701 2702 2703 2704 2705 2706
static virDomainPtr
testDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return testDomainDefineXMLFlags(conn, xml, 0);
}

2707 2708 2709 2710 2711 2712
static char *testDomainGetMetadata(virDomainPtr dom,
                                   int type,
                                   const char *uri,
                                   unsigned int flags)
{
    virDomainObjPtr privdom;
2713
    char *ret;
2714 2715 2716 2717

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, NULL);

2718 2719
    if (!(privdom = testDomObjFromDomain(dom)))
        return NULL;
2720

2721
    ret = virDomainObjGetMetadata(privdom, type, uri, flags);
2722

2723
    virDomainObjEndAPI(&privdom);
2724 2725 2726 2727 2728 2729 2730 2731 2732 2733
    return ret;
}

static int testDomainSetMetadata(virDomainPtr dom,
                                 int type,
                                 const char *metadata,
                                 const char *key,
                                 const char *uri,
                                 unsigned int flags)
{
2734
    testDriverPtr privconn = dom->conn->privateData;
2735
    virDomainObjPtr privdom;
2736
    int ret;
2737 2738 2739 2740

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

2741 2742
    if (!(privdom = testDomObjFromDomain(dom)))
        return -1;
2743 2744 2745

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

2748 2749 2750 2751 2752 2753
    if (ret == 0) {
        virObjectEventPtr ev = NULL;
        ev = virDomainEventMetadataChangeNewFromObj(privdom, type, uri);
        testObjectEventQueue(privconn, ev);
    }

2754
    virDomainObjEndAPI(&privdom);
2755 2756 2757 2758
    return ret;
}


2759 2760
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
2761 2762
                                      int startCell, int maxCells)
{
2763
    testDriverPtr privconn = conn->privateData;
2764 2765
    int cell;
    size_t i;
2766
    int ret = -1;
2767

2768
    testDriverLock(privconn);
2769
    if (startCell >= privconn->numCells) {
2770 2771
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("Range exceeds available cells"));
2772
        goto cleanup;
2773 2774
    }

2775 2776 2777 2778
    for (cell = startCell, i = 0;
         (cell < privconn->numCells && i < maxCells);
         ++cell, ++i) {
        freemems[i] = privconn->cells[cell].mem;
2779
    }
2780
    ret = i;
2781

2782
 cleanup:
2783
    testDriverUnlock(privconn);
2784
    return ret;
2785 2786
}

2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832
#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;
}
2833

2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849
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;
}

2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873
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;
}

2874 2875
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
{
2876
    testDriverPtr privconn = domain->conn->privateData;
2877
    virDomainObjPtr privdom;
2878
    virObjectEventPtr event = NULL;
2879
    int ret = -1;
2880

2881 2882
    virCheckFlags(0, -1);

2883
    testDriverLock(privconn);
2884

2885
    if (!(privdom = testDomObjFromDomain(domain)))
2886
        goto cleanup;
2887

J
Jiri Denemark 已提交
2888
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
2889 2890
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Domain '%s' is already running"), domain->name);
2891
        goto cleanup;
2892 2893
    }

2894
    if (testDomainStartState(privconn, privdom,
J
Jiri Denemark 已提交
2895
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
2896 2897 2898
        goto cleanup;
    domain->id = privdom->def->id;

2899
    event = virDomainEventLifecycleNewFromObj(privdom,
2900 2901
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2902
    ret = 0;
2903

2904
 cleanup:
2905
    virDomainObjEndAPI(&privdom);
2906
    testObjectEventQueue(privconn, event);
2907
    testDriverUnlock(privconn);
2908
    return ret;
2909 2910
}

2911 2912
static int testDomainCreate(virDomainPtr domain)
{
2913 2914 2915
    return testDomainCreateWithFlags(domain, 0);
}

2916 2917 2918
static int testDomainUndefineFlags(virDomainPtr domain,
                                   unsigned int flags)
{
2919
    testDriverPtr privconn = domain->conn->privateData;
2920
    virDomainObjPtr privdom;
2921
    virObjectEventPtr event = NULL;
2922
    int nsnapshots;
2923
    int ret = -1;
2924

2925 2926
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
2927

2928

2929
    if (!(privdom = testDomObjFromDomain(domain)))
2930
        goto cleanup;
2931

C
Cole Robinson 已提交
2932 2933 2934 2935 2936 2937 2938 2939
    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;
    }

2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957
    /* 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. */
    }

2958
    event = virDomainEventLifecycleNewFromObj(privdom,
2959 2960
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
C
Cole Robinson 已提交
2961 2962
    privdom->hasManagedSave = false;

2963
    if (virDomainObjIsActive(privdom))
2964
        privdom->persistent = 0;
2965 2966
    else
        virDomainObjListRemove(privconn->domains, privdom);
2967

2968
    ret = 0;
2969

2970
 cleanup:
2971
    virDomainObjEndAPI(&privdom);
2972
    testObjectEventQueue(privconn, event);
2973
    return ret;
2974 2975
}

2976 2977 2978 2979 2980
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

2981 2982 2983
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2984 2985
    virDomainObjPtr privdom;

2986 2987
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2988

2989
    *autostart = privdom->autostart;
2990

2991
    virDomainObjEndAPI(&privdom);
2992
    return 0;
2993 2994 2995 2996 2997 2998
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2999 3000
    virDomainObjPtr privdom;

3001 3002
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3003

3004
    privdom->autostart = autostart ? 1 : 0;
3005

3006
    virDomainObjEndAPI(&privdom);
3007
    return 0;
3008
}
3009

3010
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
3011 3012
                                        int *nparams)
{
3013 3014
    char *type = NULL;

3015 3016 3017
    if (nparams)
        *nparams = 1;

3018
    ignore_value(VIR_STRDUP(type, "fair"));
3019

3020 3021 3022
    return type;
}

3023
static int
3024 3025 3026 3027
testDomainGetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int *nparams,
                                      unsigned int flags)
3028
{
3029
    virDomainObjPtr privdom;
3030
    int ret = -1;
3031

3032 3033
    virCheckFlags(0, -1);

3034 3035
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3036

3037 3038
    if (virTypedParameterAssign(params, VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, 50) < 0)
3039
        goto cleanup;
3040 3041
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
3042 3043

    *nparams = 1;
3044 3045
    ret = 0;

3046
 cleanup:
3047
    virDomainObjEndAPI(&privdom);
3048
    return ret;
3049
}
3050

3051
static int
3052 3053 3054
testDomainGetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int *nparams)
3055
{
3056
    return testDomainGetSchedulerParametersFlags(domain, params, nparams, 0);
3057
}
3058

3059
static int
3060 3061 3062 3063
testDomainSetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int nparams,
                                      unsigned int flags)
3064
{
3065
    virDomainObjPtr privdom;
3066 3067
    int ret = -1;
    size_t i;
3068

3069
    virCheckFlags(0, -1);
3070 3071 3072 3073
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_SCHEDULER_WEIGHT,
                               VIR_TYPED_PARAM_UINT,
                               NULL) < 0)
3074
        return -1;
3075

3076 3077
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3078

3079
    for (i = 0; i < nparams; i++) {
3080 3081 3082
        if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_WEIGHT)) {
            /* XXX */
            /*privdom->weight = params[i].value.ui;*/
3083
        }
3084
    }
3085

3086 3087
    ret = 0;

3088
    virDomainObjEndAPI(&privdom);
3089
    return ret;
3090 3091
}

3092
static int
3093 3094 3095
testDomainSetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int nparams)
3096
{
3097
    return testDomainSetSchedulerParametersFlags(domain, params, nparams, 0);
3098 3099
}

3100 3101
static int testDomainBlockStats(virDomainPtr domain,
                                const char *path,
3102
                                virDomainBlockStatsPtr stats)
3103 3104 3105 3106
{
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
3107
    int ret = -1;
3108

3109 3110 3111 3112 3113 3114
    if (!*path) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("summary statistics are not supported yet"));
        return ret;
    }

3115 3116
    if (!(privdom = testDomObjFromDomain(domain)))
        return ret;
3117

3118 3119 3120 3121 3122 3123
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto error;
    }

3124
    if (virDomainDiskIndexByName(privdom->def, path, false) < 0) {
3125 3126
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path: %s"), path);
3127 3128 3129 3130
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
3131
        virReportSystemError(errno,
3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144
                             "%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;
3145
 error:
3146
    virDomainObjEndAPI(&privdom);
3147 3148 3149 3150 3151
    return ret;
}

static int testDomainInterfaceStats(virDomainPtr domain,
                                    const char *path,
3152
                                    virDomainInterfaceStatsPtr stats)
3153 3154 3155 3156
{
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
3157 3158
    size_t i;
    int found = 0, ret = -1;
3159

3160 3161
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3162

3163 3164 3165 3166 3167 3168
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto error;
    }

3169
    for (i = 0; i < privdom->def->nnets; i++) {
3170
        if (privdom->def->nets[i]->ifname &&
3171
            STREQ(privdom->def->nets[i]->ifname, path)) {
3172 3173 3174 3175 3176 3177
            found = 1;
            break;
        }
    }

    if (!found) {
3178 3179
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path, '%s' is not a known interface"), path);
3180 3181 3182 3183
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
3184
        virReportSystemError(errno,
3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200
                             "%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;
3201
 error:
3202
    virDomainObjEndAPI(&privdom);
3203 3204 3205
    return ret;
}

3206

3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224
static virNetworkObjPtr
testNetworkObjFindByUUID(testDriverPtr privconn,
                         const unsigned char *uuid)
{
    virNetworkObjPtr net;
    char uuidstr[VIR_UUID_STRING_BUFLEN];

    if (!(net = virNetworkObjFindByUUID(privconn->networks, uuid))) {
        virUUIDFormat(uuid, uuidstr);
        virReportError(VIR_ERR_NO_NETWORK,
                       _("no network with matching uuid '%s'"),
                       uuidstr);
    }

    return net;
}


3225 3226
static virNetworkPtr testNetworkLookupByUUID(virConnectPtr conn,
                                             const unsigned char *uuid)
3227
{
3228
    testDriverPtr privconn = conn->privateData;
3229
    virNetworkObjPtr net;
3230
    virNetworkPtr ret = NULL;
3231

3232
    if (!(net = testNetworkObjFindByUUID(privconn, uuid)))
3233
        goto cleanup;
3234

3235 3236
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

3237
 cleanup:
3238
    virNetworkObjEndAPI(&net);
3239
    return ret;
3240
}
3241

3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257

static virNetworkObjPtr
testNetworkObjFindByName(testDriverPtr privconn,
                         const char *name)
{
    virNetworkObjPtr net;

    if (!(net = virNetworkObjFindByName(privconn->networks, name)))
        virReportError(VIR_ERR_NO_NETWORK,
                       _("no network with matching name '%s'"),
                       name);

    return net;
}


3258
static virNetworkPtr testNetworkLookupByName(virConnectPtr conn,
3259
                                             const char *name)
3260
{
3261
    testDriverPtr privconn = conn->privateData;
3262 3263
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
3264

3265
    if (!(net = testNetworkObjFindByName(privconn, name)))
3266
        goto cleanup;
3267

3268 3269
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

3270
 cleanup:
3271
    virNetworkObjEndAPI(&net);
3272
    return ret;
3273 3274 3275
}


3276 3277
static int testConnectNumOfNetworks(virConnectPtr conn)
{
3278
    testDriverPtr privconn = conn->privateData;
3279
    int numActive;
3280

3281 3282
    numActive = virNetworkObjListNumOfNetworks(privconn->networks,
                                               true, NULL, conn);
3283
    return numActive;
3284 3285
}

3286
static int testConnectListNetworks(virConnectPtr conn, char **const names, int nnames) {
3287
    testDriverPtr privconn = conn->privateData;
3288
    int n;
3289

3290 3291
    n = virNetworkObjListGetNames(privconn->networks,
                                  true, names, nnames, NULL, conn);
3292
    return n;
3293 3294
}

3295 3296
static int testConnectNumOfDefinedNetworks(virConnectPtr conn)
{
3297
    testDriverPtr privconn = conn->privateData;
3298
    int numInactive;
3299

3300 3301
    numInactive = virNetworkObjListNumOfNetworks(privconn->networks,
                                                 false, NULL, conn);
3302
    return numInactive;
3303 3304
}

3305
static int testConnectListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
3306
    testDriverPtr privconn = conn->privateData;
3307
    int n;
3308

3309 3310
    n = virNetworkObjListGetNames(privconn->networks,
                                  false, names, nnames, NULL, conn);
3311
    return n;
3312 3313
}

3314
static int
3315
testConnectListAllNetworks(virConnectPtr conn,
3316 3317 3318
                           virNetworkPtr **nets,
                           unsigned int flags)
{
3319
    testDriverPtr privconn = conn->privateData;
3320 3321 3322

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

3323
    return virNetworkObjListExport(conn, privconn->networks, nets, NULL, flags);
3324
}
3325 3326 3327

static int testNetworkIsActive(virNetworkPtr net)
{
3328
    testDriverPtr privconn = net->conn->privateData;
3329 3330 3331
    virNetworkObjPtr obj;
    int ret = -1;

3332
    if (!(obj = testNetworkObjFindByUUID(privconn, net->uuid)))
3333
        goto cleanup;
3334

3335 3336
    ret = virNetworkObjIsActive(obj);

3337
 cleanup:
3338
    virNetworkObjEndAPI(&obj);
3339 3340 3341 3342 3343
    return ret;
}

static int testNetworkIsPersistent(virNetworkPtr net)
{
3344
    testDriverPtr privconn = net->conn->privateData;
3345 3346 3347
    virNetworkObjPtr obj;
    int ret = -1;

3348
    if (!(obj = testNetworkObjFindByUUID(privconn, net->uuid)))
3349
        goto cleanup;
3350

3351 3352
    ret = obj->persistent;

3353
 cleanup:
3354
    virNetworkObjEndAPI(&obj);
3355 3356 3357 3358
    return ret;
}


3359 3360
static virNetworkPtr testNetworkCreateXML(virConnectPtr conn, const char *xml)
{
3361
    testDriverPtr privconn = conn->privateData;
3362
    virNetworkDefPtr def;
3363
    virNetworkObjPtr net = NULL;
3364
    virNetworkPtr ret = NULL;
3365
    virObjectEventPtr event = NULL;
3366

3367
    if ((def = virNetworkDefParseString(xml)) == NULL)
3368
        goto cleanup;
3369

3370 3371 3372
    if (!(net = virNetworkObjAssignDef(privconn->networks, def,
                                       VIR_NETWORK_OBJ_LIST_ADD_LIVE |
                                       VIR_NETWORK_OBJ_LIST_ADD_CHECK_LIVE)))
3373 3374
        goto cleanup;
    def = NULL;
3375
    net->active = 1;
3376

3377
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3378 3379
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3380

3381
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3382

3383
 cleanup:
3384
    virNetworkDefFree(def);
3385
    testObjectEventQueue(privconn, event);
3386
    virNetworkObjEndAPI(&net);
3387
    return ret;
3388 3389
}

3390
static
3391
virNetworkPtr testNetworkDefineXML(virConnectPtr conn, const char *xml)
3392
{
3393
    testDriverPtr privconn = conn->privateData;
3394
    virNetworkDefPtr def;
3395
    virNetworkObjPtr net = NULL;
3396
    virNetworkPtr ret = NULL;
3397
    virObjectEventPtr event = NULL;
3398

3399
    if ((def = virNetworkDefParseString(xml)) == NULL)
3400
        goto cleanup;
3401

3402
    if (!(net = virNetworkObjAssignDef(privconn->networks, def, 0)))
3403 3404
        goto cleanup;
    def = NULL;
3405

3406
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3407 3408
                                        VIR_NETWORK_EVENT_DEFINED,
                                        0);
3409

3410
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3411

3412
 cleanup:
3413
    virNetworkDefFree(def);
3414
    testObjectEventQueue(privconn, event);
3415
    virNetworkObjEndAPI(&net);
3416
    return ret;
3417 3418
}

3419 3420
static int testNetworkUndefine(virNetworkPtr network)
{
3421
    testDriverPtr privconn = network->conn->privateData;
3422
    virNetworkObjPtr privnet;
3423
    int ret = -1;
3424
    virObjectEventPtr event = NULL;
3425

3426
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3427
        goto cleanup;
3428

D
Daniel P. Berrange 已提交
3429
    if (virNetworkObjIsActive(privnet)) {
3430 3431
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is still running"), network->name);
3432
        goto cleanup;
3433 3434
    }

3435
    event = virNetworkEventLifecycleNew(network->name, network->uuid,
3436 3437
                                        VIR_NETWORK_EVENT_UNDEFINED,
                                        0);
3438

3439
    virNetworkObjRemoveInactive(privconn->networks, privnet);
3440
    ret = 0;
3441

3442
 cleanup:
3443
    testObjectEventQueue(privconn, event);
3444
    virNetworkObjEndAPI(&privnet);
3445
    return ret;
3446 3447
}

3448 3449 3450 3451 3452 3453 3454 3455
static int
testNetworkUpdate(virNetworkPtr net,
                  unsigned int command,
                  unsigned int section,
                  int parentIndex,
                  const char *xml,
                  unsigned int flags)
{
3456
    testDriverPtr privconn = net->conn->privateData;
3457 3458 3459 3460 3461 3462 3463
    virNetworkObjPtr network = NULL;
    int isActive, ret = -1;

    virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE |
                  VIR_NETWORK_UPDATE_AFFECT_CONFIG,
                  -1);

3464
    if (!(network = testNetworkObjFindByUUID(privconn, net->uuid)))
3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484
        goto cleanup;

    /* VIR_NETWORK_UPDATE_AFFECT_CURRENT means "change LIVE if network
     * is active, else change CONFIG
    */
    isActive = virNetworkObjIsActive(network);
    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 */
    if (virNetworkObjUpdate(network, command, section, parentIndex, xml, flags) < 0)
       goto cleanup;

    ret = 0;
3485
 cleanup:
3486
    virNetworkObjEndAPI(&network);
3487 3488 3489
    return ret;
}

3490 3491
static int testNetworkCreate(virNetworkPtr network)
{
3492
    testDriverPtr privconn = network->conn->privateData;
3493
    virNetworkObjPtr privnet;
3494
    int ret = -1;
3495
    virObjectEventPtr event = NULL;
3496

3497
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3498
        goto cleanup;
3499

D
Daniel P. Berrange 已提交
3500
    if (virNetworkObjIsActive(privnet)) {
3501 3502
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is already running"), network->name);
3503
        goto cleanup;
3504 3505
    }

3506
    privnet->active = 1;
3507
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3508 3509
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3510
    ret = 0;
3511

3512
 cleanup:
3513
    testObjectEventQueue(privconn, event);
3514
    virNetworkObjEndAPI(&privnet);
3515
    return ret;
3516 3517
}

3518 3519
static int testNetworkDestroy(virNetworkPtr network)
{
3520
    testDriverPtr privconn = network->conn->privateData;
3521
    virNetworkObjPtr privnet;
3522
    int ret = -1;
3523
    virObjectEventPtr event = NULL;
3524

3525
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3526
        goto cleanup;
3527

3528
    privnet->active = 0;
3529
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3530 3531
                                        VIR_NETWORK_EVENT_STOPPED,
                                        0);
3532
    if (!privnet->persistent)
3533
        virNetworkObjRemoveInactive(privconn->networks, privnet);
3534

3535 3536
    ret = 0;

3537
 cleanup:
3538
    testObjectEventQueue(privconn, event);
3539
    virNetworkObjEndAPI(&privnet);
3540
    return ret;
3541 3542
}

3543
static char *testNetworkGetXMLDesc(virNetworkPtr network,
E
Eric Blake 已提交
3544
                                   unsigned int flags)
3545
{
3546
    testDriverPtr privconn = network->conn->privateData;
3547
    virNetworkObjPtr privnet;
3548
    char *ret = NULL;
3549

E
Eric Blake 已提交
3550 3551
    virCheckFlags(0, NULL);

3552
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3553
        goto cleanup;
3554

3555
    ret = virNetworkDefFormat(privnet->def, flags);
3556

3557
 cleanup:
3558
    virNetworkObjEndAPI(&privnet);
3559
    return ret;
3560 3561 3562
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3563
    testDriverPtr privconn = network->conn->privateData;
3564
    char *bridge = NULL;
3565 3566
    virNetworkObjPtr privnet;

3567
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3568
        goto cleanup;
3569

3570
    if (!(privnet->def->bridge)) {
3571 3572 3573
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("network '%s' does not have a bridge name."),
                       privnet->def->name);
3574 3575 3576
        goto cleanup;
    }

3577
    ignore_value(VIR_STRDUP(bridge, privnet->def->bridge));
3578

3579
 cleanup:
3580
    virNetworkObjEndAPI(&privnet);
3581 3582 3583 3584
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
3585 3586
                                   int *autostart)
{
3587
    testDriverPtr privconn = network->conn->privateData;
3588
    virNetworkObjPtr privnet;
3589
    int ret = -1;
3590

3591
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3592
        goto cleanup;
3593

3594
    *autostart = privnet->autostart;
3595 3596
    ret = 0;

3597
 cleanup:
3598
    virNetworkObjEndAPI(&privnet);
3599
    return ret;
3600 3601 3602
}

static int testNetworkSetAutostart(virNetworkPtr network,
3603 3604
                                   int autostart)
{
3605
    testDriverPtr privconn = network->conn->privateData;
3606
    virNetworkObjPtr privnet;
3607
    int ret = -1;
3608

3609
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3610
        goto cleanup;
3611

3612
    privnet->autostart = autostart ? 1 : 0;
3613 3614
    ret = 0;

3615
 cleanup:
3616
    virNetworkObjEndAPI(&privnet);
3617
    return ret;
3618
}
3619

C
Cole Robinson 已提交
3620

L
Laine Stump 已提交
3621 3622 3623 3624 3625
/*
 * Physical host interface routines
 */


3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644
static virInterfaceObjPtr
testInterfaceObjFindByName(testDriverPtr privconn,
                           const char *name)
{
    virInterfaceObjPtr iface;

    testDriverLock(privconn);
    iface = virInterfaceObjFindByName(&privconn->ifaces, name);
    testDriverUnlock(privconn);

    if (!iface)
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("no interface with matching name '%s'"),
                       name);

    return iface;
}


3645 3646
static int
testConnectNumOfInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3647
{
3648
    testDriverPtr privconn = conn->privateData;
3649
    int ninterfaces;
L
Laine Stump 已提交
3650 3651

    testDriverLock(privconn);
3652
    ninterfaces = virInterfaceObjNumOfInterfaces(&privconn->ifaces, true);
L
Laine Stump 已提交
3653
    testDriverUnlock(privconn);
3654
    return ninterfaces;
L
Laine Stump 已提交
3655 3656
}

3657 3658 3659 3660 3661

static int
testConnectListInterfaces(virConnectPtr conn,
                          char **const names,
                          int maxnames)
L
Laine Stump 已提交
3662
{
3663
    testDriverPtr privconn = conn->privateData;
3664
    int nnames;
L
Laine Stump 已提交
3665 3666

    testDriverLock(privconn);
3667
    nnames = virInterfaceObjGetNames(&privconn->ifaces, true, names, maxnames);
L
Laine Stump 已提交
3668 3669
    testDriverUnlock(privconn);

3670
    return nnames;
L
Laine Stump 已提交
3671 3672
}

3673 3674 3675

static int
testConnectNumOfDefinedInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3676
{
3677
    testDriverPtr privconn = conn->privateData;
3678
    int ninterfaces;
L
Laine Stump 已提交
3679 3680

    testDriverLock(privconn);
3681
    ninterfaces = virInterfaceObjNumOfInterfaces(&privconn->ifaces, false);
L
Laine Stump 已提交
3682
    testDriverUnlock(privconn);
3683
    return ninterfaces;
L
Laine Stump 已提交
3684 3685
}

3686 3687 3688 3689 3690

static int
testConnectListDefinedInterfaces(virConnectPtr conn,
                                 char **const names,
                                 int maxnames)
L
Laine Stump 已提交
3691
{
3692
    testDriverPtr privconn = conn->privateData;
3693
    int nnames;
L
Laine Stump 已提交
3694 3695

    testDriverLock(privconn);
3696
    nnames = virInterfaceObjGetNames(&privconn->ifaces, false, names, maxnames);
L
Laine Stump 已提交
3697 3698
    testDriverUnlock(privconn);

3699
    return nnames;
L
Laine Stump 已提交
3700 3701
}

3702 3703 3704 3705

static virInterfacePtr
testInterfaceLookupByName(virConnectPtr conn,
                          const char *name)
L
Laine Stump 已提交
3706
{
3707
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3708 3709 3710
    virInterfaceObjPtr iface;
    virInterfacePtr ret = NULL;

3711
    if (!(iface = testInterfaceObjFindByName(privconn, name)))
L
Laine Stump 已提交
3712 3713 3714 3715
        goto cleanup;

    ret = virGetInterface(conn, iface->def->name, iface->def->mac);

3716
 cleanup:
L
Laine Stump 已提交
3717 3718 3719 3720 3721
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

3722 3723 3724 3725

static virInterfacePtr
testInterfaceLookupByMACString(virConnectPtr conn,
                               const char *mac)
L
Laine Stump 已提交
3726
{
3727
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3728 3729 3730 3731 3732
    virInterfaceObjPtr iface;
    int ifacect;
    virInterfacePtr ret = NULL;

    testDriverLock(privconn);
3733
    ifacect = virInterfaceObjFindByMACString(&privconn->ifaces, mac, &iface, 1);
L
Laine Stump 已提交
3734 3735 3736
    testDriverUnlock(privconn);

    if (ifacect == 0) {
3737
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3738 3739 3740 3741
        goto cleanup;
    }

    if (ifacect > 1) {
3742
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3743 3744 3745 3746 3747
        goto cleanup;
    }

    ret = virGetInterface(conn, iface->def->name, iface->def->mac);

3748
 cleanup:
L
Laine Stump 已提交
3749 3750 3751 3752 3753
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

3754 3755 3756

static int
testInterfaceIsActive(virInterfacePtr iface)
3757
{
3758
    testDriverPtr privconn = iface->conn->privateData;
3759 3760 3761
    virInterfaceObjPtr obj;
    int ret = -1;

3762
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3763
        goto cleanup;
3764

3765 3766
    ret = virInterfaceObjIsActive(obj);

3767
 cleanup:
3768 3769 3770 3771 3772
    if (obj)
        virInterfaceObjUnlock(obj);
    return ret;
}

3773 3774 3775 3776

static int
testInterfaceChangeBegin(virConnectPtr conn,
                         unsigned int flags)
3777
{
3778
    testDriverPtr privconn = conn->privateData;
3779 3780
    int ret = -1;

E
Eric Blake 已提交
3781 3782
    virCheckFlags(0, -1);

3783 3784
    testDriverLock(privconn);
    if (privconn->transaction_running) {
3785
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3786
                       _("there is another transaction running."));
3787 3788 3789 3790 3791 3792 3793 3794 3795 3796
        goto cleanup;
    }

    privconn->transaction_running = true;

    if (virInterfaceObjListClone(&privconn->ifaces,
                                 &privconn->backupIfaces) < 0)
        goto cleanup;

    ret = 0;
3797
 cleanup:
3798 3799 3800 3801
    testDriverUnlock(privconn);
    return ret;
}

3802 3803 3804 3805

static int
testInterfaceChangeCommit(virConnectPtr conn,
                          unsigned int flags)
3806
{
3807
    testDriverPtr privconn = conn->privateData;
3808 3809
    int ret = -1;

E
Eric Blake 已提交
3810 3811
    virCheckFlags(0, -1);

3812 3813 3814
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3815
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3816 3817
                       _("no transaction running, "
                         "nothing to be committed."));
3818 3819 3820 3821 3822 3823 3824 3825
        goto cleanup;
    }

    virInterfaceObjListFree(&privconn->backupIfaces);
    privconn->transaction_running = false;

    ret = 0;

3826
 cleanup:
3827 3828 3829 3830 3831
    testDriverUnlock(privconn);

    return ret;
}

3832 3833 3834 3835

static int
testInterfaceChangeRollback(virConnectPtr conn,
                            unsigned int flags)
3836
{
3837
    testDriverPtr privconn = conn->privateData;
3838 3839
    int ret = -1;

E
Eric Blake 已提交
3840 3841
    virCheckFlags(0, -1);

3842 3843 3844
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3845
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3846 3847
                       _("no transaction running, "
                         "nothing to rollback."));
3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860
        goto cleanup;
    }

    virInterfaceObjListFree(&privconn->ifaces);
    privconn->ifaces.count = privconn->backupIfaces.count;
    privconn->ifaces.objs = privconn->backupIfaces.objs;
    privconn->backupIfaces.count = 0;
    privconn->backupIfaces.objs = NULL;

    privconn->transaction_running = false;

    ret = 0;

3861
 cleanup:
3862 3863 3864
    testDriverUnlock(privconn);
    return ret;
}
3865

3866 3867 3868 3869

static char *
testInterfaceGetXMLDesc(virInterfacePtr iface,
                        unsigned int flags)
L
Laine Stump 已提交
3870
{
3871
    testDriverPtr privconn = iface->conn->privateData;
L
Laine Stump 已提交
3872 3873 3874
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

E
Eric Blake 已提交
3875 3876
    virCheckFlags(0, NULL);

3877
    if (!(privinterface = testInterfaceObjFindByName(privconn, iface->name)))
L
Laine Stump 已提交
3878 3879
        goto cleanup;

3880
    ret = virInterfaceDefFormat(privinterface->def);
L
Laine Stump 已提交
3881

3882
 cleanup:
L
Laine Stump 已提交
3883 3884 3885 3886 3887 3888
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    return ret;
}


3889 3890 3891 3892
static virInterfacePtr
testInterfaceDefineXML(virConnectPtr conn,
                       const char *xmlStr,
                       unsigned int flags)
L
Laine Stump 已提交
3893
{
3894
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3895 3896 3897 3898
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
3899 3900
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3901
    testDriverLock(privconn);
3902
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3903 3904
        goto cleanup;

3905
    if ((iface = virInterfaceObjAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3906 3907 3908 3909 3910
        goto cleanup;
    def = NULL;

    ret = virGetInterface(conn, iface->def->name, iface->def->mac);

3911
 cleanup:
L
Laine Stump 已提交
3912 3913 3914 3915 3916 3917 3918
    virInterfaceDefFree(def);
    if (iface)
        virInterfaceObjUnlock(iface);
    testDriverUnlock(privconn);
    return ret;
}

3919 3920 3921

static int
testInterfaceUndefine(virInterfacePtr iface)
L
Laine Stump 已提交
3922
{
3923
    testDriverPtr privconn = iface->conn->privateData;
L
Laine Stump 已提交
3924 3925 3926
    virInterfaceObjPtr privinterface;
    int ret = -1;

3927
    if (!(privinterface = testInterfaceObjFindByName(privconn, iface->name)))
L
Laine Stump 已提交
3928 3929
        goto cleanup;

3930
    virInterfaceObjRemove(&privconn->ifaces, privinterface);
L
Laine Stump 已提交
3931 3932
    ret = 0;

3933
 cleanup:
L
Laine Stump 已提交
3934 3935 3936
    return ret;
}

3937 3938 3939 3940

static int
testInterfaceCreate(virInterfacePtr iface,
                    unsigned int flags)
L
Laine Stump 已提交
3941
{
3942
    testDriverPtr privconn = iface->conn->privateData;
L
Laine Stump 已提交
3943 3944 3945
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3946 3947
    virCheckFlags(0, -1);

3948
    if (!(privinterface = testInterfaceObjFindByName(privconn, iface->name)))
L
Laine Stump 已提交
3949 3950 3951
        goto cleanup;

    if (privinterface->active != 0) {
3952
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3953 3954 3955 3956 3957 3958
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

3959
 cleanup:
L
Laine Stump 已提交
3960 3961 3962 3963 3964
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    return ret;
}

3965 3966 3967 3968

static int
testInterfaceDestroy(virInterfacePtr iface,
                     unsigned int flags)
L
Laine Stump 已提交
3969
{
3970
    testDriverPtr privconn = iface->conn->privateData;
L
Laine Stump 已提交
3971 3972 3973
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3974 3975
    virCheckFlags(0, -1);

3976
    if (!(privinterface = testInterfaceObjFindByName(privconn, iface->name)))
L
Laine Stump 已提交
3977 3978 3979
        goto cleanup;

    if (privinterface->active == 0) {
3980
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3981 3982 3983 3984 3985 3986
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

3987
 cleanup:
L
Laine Stump 已提交
3988 3989 3990 3991 3992 3993 3994
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    return ret;
}



C
Cole Robinson 已提交
3995 3996 3997 3998
/*
 * Storage Driver routines
 */

3999

4000 4001
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool)
{
C
Cole Robinson 已提交
4002 4003 4004 4005 4006

    pool->def->capacity = defaultPoolCap;
    pool->def->allocation = defaultPoolAlloc;
    pool->def->available = defaultPoolCap - defaultPoolAlloc;

4007
    return VIR_STRDUP(pool->configFile, "");
C
Cole Robinson 已提交
4008 4009
}

4010

4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029
static virStoragePoolObjPtr
testStoragePoolObjFindByName(testDriverPtr privconn,
                             const char *name)
{
    virStoragePoolObjPtr pool;

    testDriverLock(privconn);
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
    testDriverUnlock(privconn);

    if (!pool)
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       _("no storage pool with matching name '%s'"),
                       name);

    return pool;
}


4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051
static virStoragePoolObjPtr
testStoragePoolObjFindByUUID(testDriverPtr privconn,
                             const unsigned char *uuid)
{
    virStoragePoolObjPtr pool;
    char uuidstr[VIR_UUID_STRING_BUFLEN];

    testDriverLock(privconn);
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
    testDriverUnlock(privconn);

    if (!pool) {
        virUUIDFormat(uuid, uuidstr);
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       _("no storage pool with matching uuid '%s'"),
                       uuidstr);
    }

    return pool;
}


C
Cole Robinson 已提交
4052 4053
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
4054 4055
                            const unsigned char *uuid)
{
4056
    testDriverPtr privconn = conn->privateData;
4057 4058
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4059

4060
    if (!(pool = testStoragePoolObjFindByUUID(privconn, uuid)))
4061
        goto cleanup;
C
Cole Robinson 已提交
4062

4063 4064
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4065

4066
 cleanup:
4067 4068
    if (pool)
        virStoragePoolObjUnlock(pool);
4069
    return ret;
C
Cole Robinson 已提交
4070 4071 4072 4073
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
4074 4075
                            const char *name)
{
4076
    testDriverPtr privconn = conn->privateData;
4077 4078
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4079

4080
    if (!(pool = testStoragePoolObjFindByName(privconn, name)))
4081
        goto cleanup;
C
Cole Robinson 已提交
4082

4083 4084
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4085

4086
 cleanup:
4087 4088
    if (pool)
        virStoragePoolObjUnlock(pool);
4089
    return ret;
C
Cole Robinson 已提交
4090 4091 4092
}

static virStoragePoolPtr
4093 4094
testStoragePoolLookupByVolume(virStorageVolPtr vol)
{
C
Cole Robinson 已提交
4095 4096 4097
    return testStoragePoolLookupByName(vol->conn, vol->pool);
}

4098

C
Cole Robinson 已提交
4099
static int
4100 4101
testConnectNumOfStoragePools(virConnectPtr conn)
{
4102
    testDriverPtr privconn = conn->privateData;
4103
    int numActive = 0;
C
Cole Robinson 已提交
4104

4105
    testDriverLock(privconn);
4106 4107
    numActive = virStoragePoolObjNumOfStoragePools(&privconn->pools, conn,
                                                   true, NULL);
4108
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4109 4110 4111 4112

    return numActive;
}

4113

C
Cole Robinson 已提交
4114
static int
4115 4116
testConnectListStoragePools(virConnectPtr conn,
                            char **const names,
4117
                            int maxnames)
4118
{
4119
    testDriverPtr privconn = conn->privateData;
4120
    int n = 0;
C
Cole Robinson 已提交
4121

4122
    testDriverLock(privconn);
4123 4124
    n = virStoragePoolObjGetNames(&privconn->pools, conn, true, NULL,
                                  names, maxnames);
4125
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4126 4127 4128 4129

    return n;
}

4130

C
Cole Robinson 已提交
4131
static int
4132 4133
testConnectNumOfDefinedStoragePools(virConnectPtr conn)
{
4134
    testDriverPtr privconn = conn->privateData;
4135
    int numInactive = 0;
C
Cole Robinson 已提交
4136

4137
    testDriverLock(privconn);
4138 4139
    numInactive = virStoragePoolObjNumOfStoragePools(&privconn->pools, conn,
                                                     false, NULL);
4140
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4141 4142 4143 4144

    return numInactive;
}

4145

C
Cole Robinson 已提交
4146
static int
4147 4148
testConnectListDefinedStoragePools(virConnectPtr conn,
                                   char **const names,
4149
                                   int maxnames)
4150
{
4151
    testDriverPtr privconn = conn->privateData;
4152
    int n = 0;
C
Cole Robinson 已提交
4153

4154
    testDriverLock(privconn);
4155 4156
    n = virStoragePoolObjGetNames(&privconn->pools, conn, false, NULL,
                                  names, maxnames);
4157
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4158 4159 4160 4161

    return n;
}

4162
static int
4163 4164 4165
testConnectListAllStoragePools(virConnectPtr conn,
                               virStoragePoolPtr **pools,
                               unsigned int flags)
4166
{
4167
    testDriverPtr privconn = conn->privateData;
4168 4169 4170 4171 4172
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

    testDriverLock(privconn);
J
John Ferlan 已提交
4173
    ret = virStoragePoolObjListExport(conn, &privconn->pools, pools,
4174
                                      NULL, flags);
4175 4176 4177 4178
    testDriverUnlock(privconn);

    return ret;
}
C
Cole Robinson 已提交
4179

4180 4181
static int testStoragePoolIsActive(virStoragePoolPtr pool)
{
4182
    testDriverPtr privconn = pool->conn->privateData;
4183 4184 4185
    virStoragePoolObjPtr obj;
    int ret = -1;

4186
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4187
        goto cleanup;
4188

4189 4190
    ret = virStoragePoolObjIsActive(obj);

4191
 cleanup:
4192 4193 4194 4195 4196 4197 4198
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}

static int testStoragePoolIsPersistent(virStoragePoolPtr pool)
{
4199
    testDriverPtr privconn = pool->conn->privateData;
4200 4201 4202
    virStoragePoolObjPtr obj;
    int ret = -1;

4203
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4204
        goto cleanup;
4205

4206 4207
    ret = obj->configFile ? 1 : 0;

4208
 cleanup:
4209 4210 4211 4212 4213 4214 4215
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}



C
Cole Robinson 已提交
4216
static int
4217 4218
testStoragePoolCreate(virStoragePoolPtr pool,
                      unsigned int flags)
E
Eric Blake 已提交
4219
{
4220
    testDriverPtr privconn = pool->conn->privateData;
4221
    virStoragePoolObjPtr privpool;
4222
    int ret = -1;
4223
    virObjectEventPtr event = NULL;
4224

E
Eric Blake 已提交
4225 4226
    virCheckFlags(0, -1);

4227
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4228
        goto cleanup;
4229

4230
    if (virStoragePoolObjIsActive(privpool)) {
4231 4232
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4233 4234
        goto cleanup;
    }
C
Cole Robinson 已提交
4235 4236

    privpool->active = 1;
4237 4238 4239 4240

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

4243
 cleanup:
4244
    testObjectEventQueue(privconn, event);
4245 4246
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4247
    return ret;
C
Cole Robinson 已提交
4248 4249 4250
}

static char *
4251 4252 4253 4254
testConnectFindStoragePoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type,
                                  const char *srcSpec,
                                  unsigned int flags)
C
Cole Robinson 已提交
4255
{
4256 4257 4258 4259
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4260 4261
    virCheckFlags(0, NULL);

4262 4263
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4264 4265
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
4266 4267 4268 4269
        goto cleanup;
    }

    if (srcSpec) {
4270
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4271 4272 4273 4274 4275 4276 4277
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
4278
        ignore_value(VIR_STRDUP(ret, defaultPoolSourcesLogicalXML));
4279 4280 4281
        break;

    case VIR_STORAGE_POOL_NETFS:
4282
        if (!source || !source->hosts[0].name) {
4283 4284
            virReportError(VIR_ERR_INVALID_ARG,
                           "%s", _("hostname must be specified for netfs sources"));
4285 4286 4287
            goto cleanup;
        }

4288 4289
        ignore_value(virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                                 source->hosts[0].name));
4290 4291 4292
        break;

    default:
4293 4294
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4295 4296
    }

4297
 cleanup:
4298 4299
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4300 4301 4302
}


4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330
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;
    virNodeDeviceObjUnlock(obj);

    return 0;
}


C
Cole Robinson 已提交
4331
static virStoragePoolPtr
4332 4333 4334
testStoragePoolCreateXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4335
{
4336
    testDriverPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4337
    virStoragePoolDefPtr def;
4338
    virStoragePoolObjPtr pool = NULL;
4339
    virStoragePoolPtr ret = NULL;
4340
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
4341

E
Eric Blake 已提交
4342 4343
    virCheckFlags(0, NULL);

4344
    testDriverLock(privconn);
4345
    if (!(def = virStoragePoolDefParseString(xml)))
4346
        goto cleanup;
C
Cole Robinson 已提交
4347

4348 4349 4350 4351
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4352 4353
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("storage pool already exists"));
4354
        goto cleanup;
C
Cole Robinson 已提交
4355 4356
    }

4357
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4358
        goto cleanup;
4359
    def = NULL;
C
Cole Robinson 已提交
4360

4361
    if (pool->def->source.adapter.type == VIR_STORAGE_ADAPTER_TYPE_FC_HOST) {
4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374
        /* 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,
                            pool->def->source.adapter.data.fchost.wwnn,
                            pool->def->source.adapter.data.fchost.wwpn) < 0) {
            virStoragePoolObjRemove(&privconn->pools, pool);
            pool = NULL;
            goto cleanup;
        }
    }

4375
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4376
        virStoragePoolObjRemove(&privconn->pools, pool);
4377 4378
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4379
    }
4380 4381 4382 4383 4384 4385

    /* *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 */
    VIR_FREE(pool->configFile);

C
Cole Robinson 已提交
4386 4387
    pool->active = 1;

4388 4389 4390 4391
    event = virStoragePoolEventLifecycleNew(pool->def->name, pool->def->uuid,
                                            VIR_STORAGE_POOL_EVENT_STARTED,
                                            0);

4392 4393
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4394

4395
 cleanup:
4396
    virStoragePoolDefFree(def);
4397
    testObjectEventQueue(privconn, event);
4398 4399 4400
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4401
    return ret;
C
Cole Robinson 已提交
4402 4403 4404
}

static virStoragePoolPtr
4405 4406 4407
testStoragePoolDefineXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4408
{
4409
    testDriverPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4410
    virStoragePoolDefPtr def;
4411
    virStoragePoolObjPtr pool = NULL;
4412
    virStoragePoolPtr ret = NULL;
4413
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
4414

E
Eric Blake 已提交
4415 4416
    virCheckFlags(0, NULL);

4417
    testDriverLock(privconn);
4418
    if (!(def = virStoragePoolDefParseString(xml)))
4419
        goto cleanup;
C
Cole Robinson 已提交
4420 4421 4422 4423 4424

    def->capacity = defaultPoolCap;
    def->allocation = defaultPoolAlloc;
    def->available = defaultPoolCap - defaultPoolAlloc;

4425
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4426 4427
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4428

4429 4430 4431 4432
    event = virStoragePoolEventLifecycleNew(pool->def->name, pool->def->uuid,
                                            VIR_STORAGE_POOL_EVENT_DEFINED,
                                            0);

4433
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4434
        virStoragePoolObjRemove(&privconn->pools, pool);
4435 4436
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4437 4438
    }

4439 4440
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4441

4442
 cleanup:
4443
    virStoragePoolDefFree(def);
4444
    testObjectEventQueue(privconn, event);
4445 4446 4447
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4448
    return ret;
C
Cole Robinson 已提交
4449 4450 4451
}

static int
4452 4453
testStoragePoolUndefine(virStoragePoolPtr pool)
{
4454
    testDriverPtr privconn = pool->conn->privateData;
4455
    virStoragePoolObjPtr privpool;
4456
    int ret = -1;
4457
    virObjectEventPtr event = NULL;
4458

4459
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4460
        goto cleanup;
4461

4462
    if (virStoragePoolObjIsActive(privpool)) {
4463 4464
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4465 4466
        goto cleanup;
    }
C
Cole Robinson 已提交
4467

4468 4469 4470 4471
    event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid,
                                            VIR_STORAGE_POOL_EVENT_UNDEFINED,
                                            0);

C
Cole Robinson 已提交
4472
    virStoragePoolObjRemove(&privconn->pools, privpool);
4473
    privpool = NULL;
4474
    ret = 0;
C
Cole Robinson 已提交
4475

4476
 cleanup:
4477 4478
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4479
    testObjectEventQueue(privconn, event);
4480
    return ret;
C
Cole Robinson 已提交
4481 4482 4483
}

static int
4484
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4485 4486
                     unsigned int flags)
{
4487
    testDriverPtr privconn = pool->conn->privateData;
4488
    virStoragePoolObjPtr privpool;
4489
    int ret = -1;
4490

E
Eric Blake 已提交
4491 4492
    virCheckFlags(0, -1);

4493
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4494
        goto cleanup;
4495

4496
    if (virStoragePoolObjIsActive(privpool)) {
4497 4498
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4499 4500
        goto cleanup;
    }
4501
    ret = 0;
C
Cole Robinson 已提交
4502

4503
 cleanup:
4504 4505
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4506
    return ret;
C
Cole Robinson 已提交
4507 4508 4509
}


4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547
static int
testDestroyVport(testDriverPtr privconn,
                 const char *wwnn ATTRIBUTE_UNUSED,
                 const char *wwpn ATTRIBUTE_UNUSED)
{
    int ret = -1;
    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 */
    if (!(obj = virNodeDeviceObjFindByName(&privconn->devs, "scsi_host12"))) {
        virReportError(VIR_ERR_NO_NODE_DEVICE, "%s",
                       _("no node device with matching name 'scsi_host12'"));
        goto cleanup;
    }

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

    virNodeDeviceObjRemove(&privconn->devs, &obj);

    ret = 0;

 cleanup:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    testObjectEventQueue(privconn, event);
    return ret;
}


C
Cole Robinson 已提交
4548
static int
4549 4550
testStoragePoolDestroy(virStoragePoolPtr pool)
{
4551
    testDriverPtr privconn = pool->conn->privateData;
4552
    virStoragePoolObjPtr privpool;
4553
    int ret = -1;
4554
    virObjectEventPtr event = NULL;
4555

4556
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4557
        goto cleanup;
4558 4559

    if (!virStoragePoolObjIsActive(privpool)) {
4560 4561
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4562
        goto cleanup;
4563
    }
C
Cole Robinson 已提交
4564 4565

    privpool->active = 0;
4566 4567

    if (privpool->def->source.adapter.type ==
4568
        VIR_STORAGE_ADAPTER_TYPE_FC_HOST) {
4569 4570 4571 4572 4573 4574 4575 4576
        if (testDestroyVport(privconn,
                             privpool->def->source.adapter.data.fchost.wwnn,
                             privpool->def->source.adapter.data.fchost.wwpn) < 0)
            goto cleanup;
    }

    event = virStoragePoolEventLifecycleNew(privpool->def->name,
                                            privpool->def->uuid,
4577 4578
                                            VIR_STORAGE_POOL_EVENT_STOPPED,
                                            0);
C
Cole Robinson 已提交
4579

4580
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4581
        virStoragePoolObjRemove(&privconn->pools, privpool);
4582 4583
        privpool = NULL;
    }
4584
    ret = 0;
C
Cole Robinson 已提交
4585

4586
 cleanup:
4587
    testObjectEventQueue(privconn, event);
4588 4589
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4590
    return ret;
C
Cole Robinson 已提交
4591 4592 4593 4594
}


static int
4595
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4596 4597
                      unsigned int flags)
{
4598
    testDriverPtr privconn = pool->conn->privateData;
4599
    virStoragePoolObjPtr privpool;
4600
    int ret = -1;
4601

E
Eric Blake 已提交
4602 4603
    virCheckFlags(0, -1);

4604
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4605 4606 4607
        goto cleanup;

    if (virStoragePoolObjIsActive(privpool)) {
4608 4609
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4610
        goto cleanup;
4611 4612
    }

4613
    ret = 0;
C
Cole Robinson 已提交
4614

4615
 cleanup:
4616 4617
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4618
    return ret;
C
Cole Robinson 已提交
4619 4620 4621 4622
}


static int
4623
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4624 4625
                       unsigned int flags)
{
4626
    testDriverPtr privconn = pool->conn->privateData;
4627
    virStoragePoolObjPtr privpool;
4628
    int ret = -1;
4629
    virObjectEventPtr event = NULL;
4630

E
Eric Blake 已提交
4631 4632
    virCheckFlags(0, -1);

4633
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4634
        goto cleanup;
4635 4636

    if (!virStoragePoolObjIsActive(privpool)) {
4637 4638
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4639
        goto cleanup;
4640
    }
4641

4642
    event = virStoragePoolEventRefreshNew(pool->name, pool->uuid);
4643
    ret = 0;
C
Cole Robinson 已提交
4644

4645
 cleanup:
4646
    testObjectEventQueue(privconn, event);
4647 4648
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4649
    return ret;
C
Cole Robinson 已提交
4650 4651 4652 4653
}


static int
4654
testStoragePoolGetInfo(virStoragePoolPtr pool,
4655 4656
                       virStoragePoolInfoPtr info)
{
4657
    testDriverPtr privconn = pool->conn->privateData;
4658
    virStoragePoolObjPtr privpool;
4659
    int ret = -1;
4660

4661
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4662
        goto cleanup;
C
Cole Robinson 已提交
4663 4664 4665 4666 4667 4668 4669 4670 4671

    memset(info, 0, sizeof(virStoragePoolInfo));
    if (privpool->active)
        info->state = VIR_STORAGE_POOL_RUNNING;
    else
        info->state = VIR_STORAGE_POOL_INACTIVE;
    info->capacity = privpool->def->capacity;
    info->allocation = privpool->def->allocation;
    info->available = privpool->def->available;
4672
    ret = 0;
C
Cole Robinson 已提交
4673

4674
 cleanup:
4675 4676
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4677
    return ret;
C
Cole Robinson 已提交
4678 4679 4680
}

static char *
4681
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4682 4683
                          unsigned int flags)
{
4684
    testDriverPtr privconn = pool->conn->privateData;
4685
    virStoragePoolObjPtr privpool;
4686
    char *ret = NULL;
4687

E
Eric Blake 已提交
4688 4689
    virCheckFlags(0, NULL);

4690
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4691
        goto cleanup;
4692

4693
    ret = virStoragePoolDefFormat(privpool->def);
4694

4695
 cleanup:
4696 4697
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4698
    return ret;
C
Cole Robinson 已提交
4699 4700 4701
}

static int
4702
testStoragePoolGetAutostart(virStoragePoolPtr pool,
4703 4704
                            int *autostart)
{
4705
    testDriverPtr privconn = pool->conn->privateData;
4706
    virStoragePoolObjPtr privpool;
4707
    int ret = -1;
4708

4709
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4710
        goto cleanup;
C
Cole Robinson 已提交
4711 4712 4713 4714 4715 4716

    if (!privpool->configFile) {
        *autostart = 0;
    } else {
        *autostart = privpool->autostart;
    }
4717
    ret = 0;
C
Cole Robinson 已提交
4718

4719
 cleanup:
4720 4721
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4722
    return ret;
C
Cole Robinson 已提交
4723 4724 4725
}

static int
4726
testStoragePoolSetAutostart(virStoragePoolPtr pool,
4727 4728
                            int autostart)
{
4729
    testDriverPtr privconn = pool->conn->privateData;
4730
    virStoragePoolObjPtr privpool;
4731
    int ret = -1;
4732

4733
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4734
        goto cleanup;
C
Cole Robinson 已提交
4735 4736

    if (!privpool->configFile) {
4737 4738
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
4739
        goto cleanup;
C
Cole Robinson 已提交
4740 4741 4742 4743
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4744 4745
    ret = 0;

4746
 cleanup:
4747 4748
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4749
    return ret;
C
Cole Robinson 已提交
4750 4751 4752 4753
}


static int
4754 4755
testStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
4756
    testDriverPtr privconn = pool->conn->privateData;
4757
    virStoragePoolObjPtr privpool;
4758
    int ret = -1;
4759

4760
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4761
        goto cleanup;
4762 4763

    if (!virStoragePoolObjIsActive(privpool)) {
4764 4765
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4766
        goto cleanup;
4767
    }
C
Cole Robinson 已提交
4768

4769 4770
    ret = virStoragePoolObjNumOfVolumes(&privpool->volumes, pool->conn,
                                        privpool->def, NULL);
4771

4772
 cleanup:
4773 4774
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4775
    return ret;
C
Cole Robinson 已提交
4776 4777
}

4778

C
Cole Robinson 已提交
4779
static int
4780
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4781
                           char **const names,
4782 4783
                           int maxnames)
{
4784
    testDriverPtr privconn = pool->conn->privateData;
4785
    virStoragePoolObjPtr privpool;
4786
    int n = -1;
4787

4788
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4789
        return -1;
4790 4791

    if (!virStoragePoolObjIsActive(privpool)) {
4792 4793
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4794
        goto cleanup;
4795 4796
    }

4797 4798
    n = virStoragePoolObjVolumeGetNames(&privpool->volumes, pool->conn,
                                        privpool->def, NULL, names, maxnames);
C
Cole Robinson 已提交
4799

4800
 cleanup:
4801
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4802 4803 4804
    return n;
}

4805

4806 4807 4808
static int
testStoragePoolListAllVolumes(virStoragePoolPtr obj,
                              virStorageVolPtr **vols,
4809 4810
                              unsigned int flags)
{
4811
    testDriverPtr privconn = obj->conn->privateData;
4812 4813 4814 4815 4816
    virStoragePoolObjPtr pool;
    int ret = -1;

    virCheckFlags(0, -1);

4817
    if (!(pool = testStoragePoolObjFindByUUID(privconn, obj->uuid)))
4818
        return -1;
4819 4820 4821 4822 4823 4824 4825

    if (!virStoragePoolObjIsActive(pool)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("storage pool is not active"));
        goto cleanup;
    }

4826 4827
    ret = virStoragePoolObjVolumeListExport(obj->conn, &pool->volumes,
                                            pool->def, vols, NULL);
4828 4829

 cleanup:
4830
    virStoragePoolObjUnlock(pool);
4831 4832 4833

    return ret;
}
C
Cole Robinson 已提交
4834 4835

static virStorageVolPtr
4836
testStorageVolLookupByName(virStoragePoolPtr pool,
4837 4838
                           const char *name ATTRIBUTE_UNUSED)
{
4839
    testDriverPtr privconn = pool->conn->privateData;
4840 4841
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4842
    virStorageVolPtr ret = NULL;
4843

4844
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4845
        goto cleanup;
4846 4847

    if (!virStoragePoolObjIsActive(privpool)) {
4848 4849
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4850
        goto cleanup;
4851 4852 4853 4854 4855
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4856 4857
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"), name);
4858
        goto cleanup;
C
Cole Robinson 已提交
4859 4860
    }

4861
    ret = virGetStorageVol(pool->conn, privpool->def->name,
4862 4863
                           privvol->name, privvol->key,
                           NULL, NULL);
4864

4865
 cleanup:
4866 4867
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4868
    return ret;
C
Cole Robinson 已提交
4869 4870 4871 4872
}


static virStorageVolPtr
4873
testStorageVolLookupByKey(virConnectPtr conn,
4874 4875
                          const char *key)
{
4876
    testDriverPtr privconn = conn->privateData;
4877
    size_t i;
4878
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4879

4880
    testDriverLock(privconn);
4881
    for (i = 0; i < privconn->pools.count; i++) {
4882
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4883
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4884
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4885 4886
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4887 4888 4889 4890
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
4891 4892
                                       privvol->key,
                                       NULL, NULL);
4893
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4894 4895
                break;
            }
C
Cole Robinson 已提交
4896
        }
4897
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4898
    }
4899
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4900

4901
    if (!ret)
4902 4903
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
4904 4905

    return ret;
C
Cole Robinson 已提交
4906 4907 4908
}

static virStorageVolPtr
4909
testStorageVolLookupByPath(virConnectPtr conn,
4910 4911
                           const char *path)
{
4912
    testDriverPtr privconn = conn->privateData;
4913
    size_t i;
4914
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4915

4916
    testDriverLock(privconn);
4917
    for (i = 0; i < privconn->pools.count; i++) {
4918
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4919
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4920
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4921 4922
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

4923 4924 4925 4926
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
4927 4928
                                       privvol->key,
                                       NULL, NULL);
4929
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4930 4931
                break;
            }
C
Cole Robinson 已提交
4932
        }
4933
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4934
    }
4935
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4936

4937
    if (!ret)
4938 4939
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
4940 4941

    return ret;
C
Cole Robinson 已提交
4942 4943 4944
}

static virStorageVolPtr
4945 4946 4947
testStorageVolCreateXML(virStoragePoolPtr pool,
                        const char *xmldesc,
                        unsigned int flags)
E
Eric Blake 已提交
4948
{
4949
    testDriverPtr privconn = pool->conn->privateData;
4950
    virStoragePoolObjPtr privpool;
4951 4952
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
4953

E
Eric Blake 已提交
4954 4955
    virCheckFlags(0, NULL);

4956
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4957
        goto cleanup;
4958 4959

    if (!virStoragePoolObjIsActive(privpool)) {
4960 4961
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4962
        goto cleanup;
4963
    }
C
Cole Robinson 已提交
4964

4965
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
4966
    if (privvol == NULL)
4967
        goto cleanup;
4968 4969

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4970 4971
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
4972
        goto cleanup;
C
Cole Robinson 已提交
4973 4974 4975
    }

    /* Make sure enough space */
4976
    if ((privpool->def->allocation + privvol->target.allocation) >
C
Cole Robinson 已提交
4977
         privpool->def->capacity) {
4978 4979 4980
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
4981
        goto cleanup;
C
Cole Robinson 已提交
4982 4983
    }

4984 4985
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
4986
                    privvol->name) == -1)
4987
        goto cleanup;
C
Cole Robinson 已提交
4988

4989 4990 4991
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
4992
        goto cleanup;
C
Cole Robinson 已提交
4993

4994
    privpool->def->allocation += privvol->target.allocation;
C
Cole Robinson 已提交
4995 4996 4997
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

4998
    ret = virGetStorageVol(pool->conn, privpool->def->name,
4999 5000
                           privvol->name, privvol->key,
                           NULL, NULL);
5001
    privvol = NULL;
5002

5003
 cleanup:
5004
    virStorageVolDefFree(privvol);
5005 5006
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5007
    return ret;
C
Cole Robinson 已提交
5008 5009
}

5010
static virStorageVolPtr
5011 5012 5013 5014
testStorageVolCreateXMLFrom(virStoragePoolPtr pool,
                            const char *xmldesc,
                            virStorageVolPtr clonevol,
                            unsigned int flags)
E
Eric Blake 已提交
5015
{
5016
    testDriverPtr privconn = pool->conn->privateData;
5017 5018 5019 5020
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
5021 5022
    virCheckFlags(0, NULL);

5023
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
5024 5025 5026
        goto cleanup;

    if (!virStoragePoolObjIsActive(privpool)) {
5027 5028
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5029 5030 5031
        goto cleanup;
    }

5032
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5033 5034 5035 5036
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5037 5038
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5039 5040 5041 5042 5043
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
5044 5045 5046
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
5047 5048 5049 5050
        goto cleanup;
    }

    /* Make sure enough space */
5051
    if ((privpool->def->allocation + privvol->target.allocation) >
5052
         privpool->def->capacity) {
5053 5054 5055
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5056 5057 5058 5059 5060
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5061 5062
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5063
                    privvol->name) == -1)
5064 5065
        goto cleanup;

5066 5067 5068
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
5069 5070
        goto cleanup;

5071
    privpool->def->allocation += privvol->target.allocation;
5072 5073 5074 5075
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    ret = virGetStorageVol(pool->conn, privpool->def->name,
5076 5077
                           privvol->name, privvol->key,
                           NULL, NULL);
5078 5079
    privvol = NULL;

5080
 cleanup:
5081 5082 5083 5084 5085 5086
    virStorageVolDefFree(privvol);
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    return ret;
}

C
Cole Robinson 已提交
5087
static int
5088 5089
testStorageVolDelete(virStorageVolPtr vol,
                     unsigned int flags)
E
Eric Blake 已提交
5090
{
5091
    testDriverPtr privconn = vol->conn->privateData;
5092 5093
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5094
    size_t i;
5095
    int ret = -1;
C
Cole Robinson 已提交
5096

E
Eric Blake 已提交
5097 5098
    virCheckFlags(0, -1);

5099
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5100
        goto cleanup;
5101 5102 5103 5104

    privvol = virStorageVolDefFindByName(privpool, vol->name);

    if (privvol == NULL) {
5105 5106 5107
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5108
        goto cleanup;
5109 5110 5111
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5112 5113
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5114
        goto cleanup;
5115 5116 5117
    }


5118
    privpool->def->allocation -= privvol->target.allocation;
C
Cole Robinson 已提交
5119 5120 5121
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5122
    for (i = 0; i < privpool->volumes.count; i++) {
C
Cole Robinson 已提交
5123 5124 5125
        if (privpool->volumes.objs[i] == privvol) {
            virStorageVolDefFree(privvol);

5126
            VIR_DELETE_ELEMENT(privpool->volumes.objs, i, privpool->volumes.count);
C
Cole Robinson 已提交
5127 5128 5129
            break;
        }
    }
5130
    ret = 0;
C
Cole Robinson 已提交
5131

5132
 cleanup:
5133 5134
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5135
    return ret;
C
Cole Robinson 已提交
5136 5137 5138
}


5139 5140
static int testStorageVolumeTypeForPool(int pooltype)
{
C
Cole Robinson 已提交
5141

5142
    switch (pooltype) {
C
Cole Robinson 已提交
5143 5144 5145 5146 5147 5148 5149 5150 5151 5152
        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;
    }
}

static int
5153
testStorageVolGetInfo(virStorageVolPtr vol,
5154 5155
                      virStorageVolInfoPtr info)
{
5156
    testDriverPtr privconn = vol->conn->privateData;
5157 5158
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5159
    int ret = -1;
5160

5161
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5162
        goto cleanup;
5163 5164 5165 5166

    privvol = virStorageVolDefFindByName(privpool, vol->name);

    if (privvol == NULL) {
5167 5168 5169
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5170
        goto cleanup;
5171 5172 5173
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5174 5175
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5176
        goto cleanup;
5177
    }
C
Cole Robinson 已提交
5178 5179 5180

    memset(info, 0, sizeof(*info));
    info->type = testStorageVolumeTypeForPool(privpool->def->type);
5181 5182
    info->capacity = privvol->target.capacity;
    info->allocation = privvol->target.allocation;
5183
    ret = 0;
C
Cole Robinson 已提交
5184

5185
 cleanup:
5186 5187
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5188
    return ret;
C
Cole Robinson 已提交
5189 5190 5191
}

static char *
5192 5193
testStorageVolGetXMLDesc(virStorageVolPtr vol,
                         unsigned int flags)
E
Eric Blake 已提交
5194
{
5195
    testDriverPtr privconn = vol->conn->privateData;
5196 5197
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5198
    char *ret = NULL;
5199

E
Eric Blake 已提交
5200 5201
    virCheckFlags(0, NULL);

5202
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5203
        goto cleanup;
5204 5205 5206 5207

    privvol = virStorageVolDefFindByName(privpool, vol->name);

    if (privvol == NULL) {
5208 5209 5210
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5211
        goto cleanup;
5212
    }
C
Cole Robinson 已提交
5213

5214
    if (!virStoragePoolObjIsActive(privpool)) {
5215 5216
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5217
        goto cleanup;
5218 5219
    }

5220
    ret = virStorageVolDefFormat(privpool->def, privvol);
5221

5222
 cleanup:
5223 5224
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5225
    return ret;
C
Cole Robinson 已提交
5226 5227 5228
}

static char *
5229 5230
testStorageVolGetPath(virStorageVolPtr vol)
{
5231
    testDriverPtr privconn = vol->conn->privateData;
5232 5233
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5234
    char *ret = NULL;
5235

5236
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5237
        goto cleanup;
5238 5239 5240 5241

    privvol = virStorageVolDefFindByName(privpool, vol->name);

    if (privvol == NULL) {
5242 5243 5244
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5245
        goto cleanup;
5246 5247 5248
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5249 5250
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5251
        goto cleanup;
5252 5253
    }

5254
    ignore_value(VIR_STRDUP(ret, privvol->target.path));
5255

5256
 cleanup:
5257 5258
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5259 5260 5261
    return ret;
}

5262

5263
/* Node device implementations */
5264

5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283
static virNodeDeviceObjPtr
testNodeDeviceObjFindByName(testDriverPtr driver,
                            const char *name)
{
    virNodeDeviceObjPtr obj;

    testDriverLock(driver);
    obj = virNodeDeviceObjFindByName(&driver->devs, name);
    testDriverUnlock(driver);

    if (!obj)
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       name);

    return obj;
}


5284 5285 5286
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5287
                     unsigned int flags)
5288
{
5289
    testDriverPtr driver = conn->privateData;
5290 5291
    int ndevs = 0;

E
Eric Blake 已提交
5292 5293
    virCheckFlags(0, -1);

5294
    testDriverLock(driver);
5295
    ndevs = virNodeDeviceObjNumOfDevices(&driver->devs, conn, cap, NULL);
5296 5297 5298 5299 5300
    testDriverUnlock(driver);

    return ndevs;
}

5301

5302 5303 5304 5305 5306
static int
testNodeListDevices(virConnectPtr conn,
                    const char *cap,
                    char **const names,
                    int maxnames,
E
Eric Blake 已提交
5307
                    unsigned int flags)
5308
{
5309
    testDriverPtr driver = conn->privateData;
5310
    int nnames = 0;
5311

E
Eric Blake 已提交
5312 5313
    virCheckFlags(0, -1);

5314
    testDriverLock(driver);
5315 5316
    nnames = virNodeDeviceObjGetNames(&driver->devs, conn, NULL,
                                     cap, names, maxnames);
5317 5318
    testDriverUnlock(driver);

5319
    return nnames;
5320 5321
}

5322

5323 5324 5325
static virNodeDevicePtr
testNodeDeviceLookupByName(virConnectPtr conn, const char *name)
{
5326
    testDriverPtr driver = conn->privateData;
5327 5328 5329
    virNodeDeviceObjPtr obj;
    virNodeDevicePtr ret = NULL;

5330
    if (!(obj = testNodeDeviceObjFindByName(driver, name)))
5331 5332
        goto cleanup;

5333
    if ((ret = virGetNodeDevice(conn, name))) {
5334
        if (VIR_STRDUP(ret->parent, obj->def->parent) < 0) {
5335
            virObjectUnref(ret);
5336 5337
            ret = NULL;
        }
5338
    }
5339

5340
 cleanup:
5341 5342 5343 5344 5345 5346
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
5347
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5348
                         unsigned int flags)
5349
{
5350
    testDriverPtr driver = dev->conn->privateData;
5351 5352 5353
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5354 5355
    virCheckFlags(0, NULL);

5356
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5357 5358
        goto cleanup;

5359
    ret = virNodeDeviceDefFormat(obj->def);
5360

5361
 cleanup:
5362 5363 5364 5365 5366 5367 5368 5369
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
testNodeDeviceGetParent(virNodeDevicePtr dev)
{
5370
    testDriverPtr driver = dev->conn->privateData;
5371 5372 5373
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

5374
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5375 5376 5377
        goto cleanup;

    if (obj->def->parent) {
5378
        ignore_value(VIR_STRDUP(ret, obj->def->parent));
5379
    } else {
5380 5381
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5382 5383
    }

5384
 cleanup:
5385 5386 5387 5388 5389
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

5390

5391 5392 5393
static int
testNodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
5394
    testDriverPtr driver = dev->conn->privateData;
5395 5396 5397 5398 5399
    virNodeDeviceObjPtr obj;
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

5400
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5401 5402 5403 5404 5405 5406
        goto cleanup;

    for (caps = obj->def->caps; caps; caps = caps->next)
        ++ncaps;
    ret = ncaps;

5407
 cleanup:
5408 5409 5410 5411 5412 5413 5414 5415 5416
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}


static int
testNodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
5417
    testDriverPtr driver = dev->conn->privateData;
5418 5419 5420 5421 5422
    virNodeDeviceObjPtr obj;
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

5423
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5424 5425 5426
        goto cleanup;

    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
5427
        if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->data.type)) < 0)
5428 5429 5430 5431
            goto cleanup;
    }
    ret = ncaps;

5432
 cleanup:
5433 5434 5435 5436 5437 5438 5439 5440 5441 5442
    if (obj)
        virNodeDeviceObjUnlock(obj);
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
}

5443

5444 5445
static virNodeDeviceObjPtr
testNodeDeviceMockCreateVport(testDriverPtr driver,
5446
                              const char *wwnn,
5447
                              const char *wwpn)
5448
{
5449 5450
    char *xml = NULL;
    virNodeDeviceDefPtr def = NULL;
5451
    virNodeDevCapsDefPtr caps;
5452
    virNodeDeviceObjPtr obj = NULL, objcopy = NULL;
5453
    virObjectEventPtr event = NULL;
5454

5455 5456 5457 5458 5459 5460 5461 5462 5463
    /* 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. */
5464
    if (!(objcopy = virNodeDeviceObjFindByName(&driver->devs, "scsi_host11")))
5465 5466 5467 5468 5469 5470 5471 5472
        goto cleanup;

    xml = virNodeDeviceDefFormat(objcopy->def);
    virNodeDeviceObjUnlock(objcopy);
    if (!xml)
        goto cleanup;

    if (!(def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL)))
5473 5474
        goto cleanup;

5475
    VIR_FREE(def->name);
5476
    if (VIR_STRDUP(def->name, "scsi_host12") < 0)
5477 5478
        goto cleanup;

5479 5480 5481
    /* 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. */
5482 5483
    caps = def->caps;
    while (caps) {
5484
        if (caps->data.type != VIR_NODE_DEV_CAP_SCSI_HOST)
5485 5486
            continue;

5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500
        /* 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++;
        }
5501 5502 5503
        caps = caps->next;
    }

5504
    if (!(obj = virNodeDeviceObjAssignDef(&driver->devs, def)))
5505
        goto cleanup;
5506
    def = NULL;
5507

5508
    event = virNodeDeviceEventLifecycleNew(obj->def->name,
5509 5510
                                           VIR_NODE_DEVICE_EVENT_CREATED,
                                           0);
5511 5512 5513
    testObjectEventQueue(driver, event);

 cleanup:
5514
    VIR_FREE(xml);
5515 5516
    virNodeDeviceDefFree(def);
    return obj;
5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527
}


static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
                        unsigned int flags)
{
    testDriverPtr driver = conn->privateData;
    virNodeDeviceDefPtr def = NULL;
    char *wwnn = NULL, *wwpn = NULL;
5528 5529
    virNodeDevicePtr dev = NULL, ret = NULL;
    virNodeDeviceObjPtr obj = NULL;
5530 5531 5532 5533 5534 5535 5536 5537

    virCheckFlags(0, NULL);

    testDriverLock(driver);

    if (!(def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, NULL)))
        goto cleanup;

5538 5539 5540
    /* We run this simply for validation - it essentially validates that
     * the input XML either has a wwnn/wwpn or virNodeDevCapSCSIHostParseXML
     * generated a wwnn/wwpn */
5541 5542 5543
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) < 0)
        goto cleanup;

5544 5545 5546
    /* 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. */
5547
    if (virNodeDeviceObjGetParentHost(&driver->devs, def, CREATE_DEVICE) < 0)
5548 5549 5550 5551
        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
5552 5553 5554
     * 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 */
5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566
    if (!(obj = testNodeDeviceMockCreateVport(driver, wwnn, wwpn)))
        goto cleanup;

    if (!(dev = virGetNodeDevice(conn, obj->def->name)))
        goto cleanup;

    VIR_FREE(dev->parent);
    if (VIR_STRDUP(dev->parent, def->parent) < 0)
        goto cleanup;

    ret = dev;
    dev = NULL;
5567

5568
 cleanup:
5569 5570
    if (obj)
        virNodeDeviceObjUnlock(obj);
5571
    testDriverUnlock(driver);
5572
    virNodeDeviceDefFree(def);
5573
    virObjectUnref(dev);
5574 5575
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
5576
    return ret;
5577 5578 5579 5580 5581 5582
}

static int
testNodeDeviceDestroy(virNodeDevicePtr dev)
{
    int ret = 0;
5583
    testDriverPtr driver = dev->conn->privateData;
5584 5585
    virNodeDeviceObjPtr obj = NULL;
    char *parent_name = NULL, *wwnn = NULL, *wwpn = NULL;
5586
    virObjectEventPtr event = NULL;
5587

5588
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5589 5590
        goto out;

5591
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1)
5592 5593
        goto out;

5594
    if (VIR_STRDUP(parent_name, obj->def->parent) < 0)
5595 5596 5597 5598 5599 5600 5601 5602
        goto out;

    /* virNodeDeviceGetParentHost will cause the device object's lock to be
     * taken, so we have to dup the parent's name and drop the lock
     * before calling it.  We don't need the reference to the object
     * any more once we have the parent's name.  */
    virNodeDeviceObjUnlock(obj);

5603 5604
    /* We do this just for basic validation, but also avoid finding a
     * vport capable HBA if for some reason our vHBA doesn't exist */
5605 5606
    if (virNodeDeviceObjGetParentHost(&driver->devs, obj->def,
                                      EXISTING_DEVICE) < 0) {
5607 5608 5609 5610
        obj = NULL;
        goto out;
    }

5611 5612 5613 5614
    event = virNodeDeviceEventLifecycleNew(dev->name,
                                           VIR_NODE_DEVICE_EVENT_DELETED,
                                           0);

5615
    virNodeDeviceObjLock(obj);
5616
    virNodeDeviceObjRemove(&driver->devs, &obj);
5617

5618
 out:
5619 5620
    if (obj)
        virNodeDeviceObjUnlock(obj);
5621
    testObjectEventQueue(driver, event);
5622 5623 5624 5625 5626 5627
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

5628 5629

/* Domain event implementations */
5630
static int
5631 5632 5633 5634
testConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
5635
{
5636
    testDriverPtr driver = conn->privateData;
5637
    int ret = 0;
5638

5639
    if (virDomainEventStateRegister(conn, driver->eventState,
5640 5641
                                    callback, opaque, freecb) < 0)
        ret = -1;
5642 5643 5644 5645

    return ret;
}

5646

5647
static int
5648 5649
testConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
5650
{
5651
    testDriverPtr driver = conn->privateData;
5652
    int ret = 0;
5653

5654
    if (virDomainEventStateDeregister(conn, driver->eventState,
5655 5656
                                      callback) < 0)
        ret = -1;
5657 5658 5659 5660

    return ret;
}

5661 5662

static int
5663 5664 5665 5666 5667 5668
testConnectDomainEventRegisterAny(virConnectPtr conn,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback callback,
                                  void *opaque,
                                  virFreeCallback freecb)
5669
{
5670
    testDriverPtr driver = conn->privateData;
5671 5672
    int ret;

5673
    if (virDomainEventStateRegisterID(conn, driver->eventState,
5674 5675
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
5676
        ret = -1;
5677 5678 5679 5680 5681

    return ret;
}

static int
5682 5683
testConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
5684
{
5685
    testDriverPtr driver = conn->privateData;
5686
    int ret = 0;
5687

5688
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5689 5690
                                        callbackID) < 0)
        ret = -1;
5691 5692 5693 5694 5695

    return ret;
}


5696 5697 5698 5699 5700 5701 5702 5703
static int
testConnectNetworkEventRegisterAny(virConnectPtr conn,
                                   virNetworkPtr net,
                                   int eventID,
                                   virConnectNetworkEventGenericCallback callback,
                                   void *opaque,
                                   virFreeCallback freecb)
{
5704
    testDriverPtr driver = conn->privateData;
5705 5706
    int ret;

5707
    if (virNetworkEventStateRegisterID(conn, driver->eventState,
5708
                                       net, eventID, callback,
5709 5710 5711 5712 5713 5714 5715 5716 5717 5718
                                       opaque, freecb, &ret) < 0)
        ret = -1;

    return ret;
}

static int
testConnectNetworkEventDeregisterAny(virConnectPtr conn,
                                     int callbackID)
{
5719
    testDriverPtr driver = conn->privateData;
5720
    int ret = 0;
5721

5722
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5723 5724
                                        callbackID) < 0)
        ret = -1;
5725 5726 5727 5728

    return ret;
}

5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761
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,
                                        callbackID) < 0)
        ret = -1;

    return ret;
}

5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794
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,
                                        callbackID) < 0)
        ret = -1;

    return ret;
}

5795 5796 5797
static int testConnectListAllDomains(virConnectPtr conn,
                                     virDomainPtr **domains,
                                     unsigned int flags)
5798
{
5799
    testDriverPtr privconn = conn->privateData;
5800

O
Osier Yang 已提交
5801
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
5802

5803 5804
    return virDomainObjListExport(privconn->domains, conn, domains,
                                  NULL, flags);
5805 5806
}

5807
static int
P
Peter Krempa 已提交
5808
testNodeGetCPUMap(virConnectPtr conn ATTRIBUTE_UNUSED,
5809 5810 5811 5812 5813 5814 5815
                  unsigned char **cpumap,
                  unsigned int *online,
                  unsigned int flags)
{
    virCheckFlags(0, -1);

    if (cpumap) {
5816
        if (VIR_ALLOC_N(*cpumap, 1) < 0)
P
Peter Krempa 已提交
5817
            return -1;
5818 5819 5820 5821 5822 5823
        *cpumap[0] = 0x15;
    }

    if (online)
        *online = 3;

P
Peter Krempa 已提交
5824
    return  8;
5825 5826
}

5827 5828 5829 5830 5831 5832 5833 5834 5835 5836
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

5837
    if (VIR_STRDUP(ret, "image/png") < 0)
5838 5839
        return NULL;

D
Daniel P. Berrange 已提交
5840
    if (virFDStreamOpenFile(st, PKGDATADIR "/test-screenshot.png", 0, 0, O_RDONLY) < 0)
5841 5842 5843 5844 5845
        VIR_FREE(ret);

    return ret;
}

5846 5847
static int
testConnectGetCPUModelNames(virConnectPtr conn ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
5848
                            const char *archName,
5849 5850 5851
                            char ***models,
                            unsigned int flags)
{
J
Jiri Denemark 已提交
5852 5853
    virArch arch;

5854
    virCheckFlags(0, -1);
J
Jiri Denemark 已提交
5855 5856 5857 5858 5859 5860 5861 5862

    if (!(arch = virArchFromString(archName))) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("cannot find architecture %s"),
                       archName);
        return -1;
    }

J
Jiri Denemark 已提交
5863
    return virCPUGetModels(arch, models);
5864
}
5865

C
Cole Robinson 已提交
5866 5867 5868
static int
testDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
5869
    testDriverPtr privconn = dom->conn->privateData;
C
Cole Robinson 已提交
5870
    virDomainObjPtr vm = NULL;
5871
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
5872 5873 5874 5875 5876 5877
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_SAVE_BYPASS_CACHE |
                  VIR_DOMAIN_SAVE_RUNNING |
                  VIR_DOMAIN_SAVE_PAUSED, -1);

5878 5879
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893

    if (!virDomainObjIsActive(vm)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        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);
5894
    event = virDomainEventLifecycleNewFromObj(vm,
C
Cole Robinson 已提交
5895 5896 5897 5898 5899
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
    vm->hasManagedSave = true;

    ret = 0;
5900
 cleanup:
5901
    virDomainObjEndAPI(&vm);
5902
    testObjectEventQueue(privconn, event);
C
Cole Robinson 已提交
5903 5904 5905 5906 5907 5908 5909 5910 5911

    return ret;
}


static int
testDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;
5912
    int ret;
C
Cole Robinson 已提交
5913 5914 5915

    virCheckFlags(0, -1);

5916 5917
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5918 5919

    ret = vm->hasManagedSave;
5920

5921
    virDomainObjEndAPI(&vm);
C
Cole Robinson 已提交
5922 5923 5924 5925 5926 5927 5928 5929 5930 5931
    return ret;
}

static int
testDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;

    virCheckFlags(0, -1);

5932 5933
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5934 5935

    vm->hasManagedSave = false;
5936

5937
    virDomainObjEndAPI(&vm);
5938
    return 0;
C
Cole Robinson 已提交
5939 5940 5941
}


5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975
/*
 * 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;
5976
    int n;
5977 5978 5979 5980 5981

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
5982
        return -1;
5983 5984 5985

    n = virDomainSnapshotObjListNum(vm->snapshots, NULL, flags);

5986
    virDomainObjEndAPI(&vm);
5987 5988 5989 5990 5991 5992 5993 5994 5995 5996
    return n;
}

static int
testDomainSnapshotListNames(virDomainPtr domain,
                            char **names,
                            int nameslen,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
5997
    int n;
5998 5999 6000 6001 6002

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6003
        return -1;
6004 6005 6006 6007

    n = virDomainSnapshotObjListGetNames(vm->snapshots, NULL, names, nameslen,
                                         flags);

6008
    virDomainObjEndAPI(&vm);
6009 6010 6011 6012 6013 6014 6015 6016 6017
    return n;
}

static int
testDomainListAllSnapshots(virDomainPtr domain,
                           virDomainSnapshotPtr **snaps,
                           unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6018
    int n;
6019 6020 6021 6022 6023

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6024
        return -1;
6025 6026 6027

    n = virDomainListSnapshots(vm->snapshots, NULL, domain, snaps, flags);

6028
    virDomainObjEndAPI(&vm);
6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045
    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)))
6046
        return -1;
6047 6048 6049 6050 6051 6052 6053

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListGetNames(vm->snapshots, snap, names, nameslen,
                                         flags);

6054
 cleanup:
6055
    virDomainObjEndAPI(&vm);
6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070
    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)))
6071
        return -1;
6072 6073 6074 6075 6076 6077

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListNum(vm->snapshots, snap, flags);

6078
 cleanup:
6079
    virDomainObjEndAPI(&vm);
6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095
    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)))
6096
        return -1;
6097 6098 6099 6100 6101 6102 6103

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainListSnapshots(vm->snapshots, snap, snapshot->domain, snaps,
                               flags);

6104
 cleanup:
6105
    virDomainObjEndAPI(&vm);
6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120
    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)))
6121
        return NULL;
6122 6123 6124 6125 6126 6127

    if (!(snap = testSnapObjFromName(vm, name)))
        goto cleanup;

    snapshot = virGetDomainSnapshot(domain, snap->def->name);

6128
 cleanup:
6129
    virDomainObjEndAPI(&vm);
6130 6131 6132 6133 6134 6135 6136 6137
    return snapshot;
}

static int
testDomainHasCurrentSnapshot(virDomainPtr domain,
                             unsigned int flags)
{
    virDomainObjPtr vm;
6138
    int ret;
6139 6140 6141 6142

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6143
        return -1;
6144 6145 6146

    ret = (vm->current_snapshot != NULL);

6147
    virDomainObjEndAPI(&vm);
6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161
    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)))
6162
        return NULL;
6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175

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

6176
 cleanup:
6177
    virDomainObjEndAPI(&vm);
6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190
    return parent;
}

static virDomainSnapshotPtr
testDomainSnapshotCurrent(virDomainPtr domain,
                          unsigned int flags)
{
    virDomainObjPtr vm;
    virDomainSnapshotPtr snapshot = NULL;

    virCheckFlags(0, NULL);

    if (!(vm = testDomObjFromDomain(domain)))
6191
        return NULL;
6192 6193 6194 6195 6196 6197 6198 6199 6200

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

6201
 cleanup:
6202
    virDomainObjEndAPI(&vm);
6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213
    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];
6214
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6215 6216 6217 6218

    virCheckFlags(VIR_DOMAIN_XML_SECURE, NULL);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6219
        return NULL;
6220 6221 6222 6223 6224 6225

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    virUUIDFormat(snapshot->domain->uuid, uuidstr);

6226
    xml = virDomainSnapshotDefFormat(uuidstr, snap->def, privconn->caps,
6227 6228
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);
6229

6230
 cleanup:
6231
    virDomainObjEndAPI(&vm);
6232 6233 6234 6235 6236 6237 6238 6239
    return xml;
}

static int
testDomainSnapshotIsCurrent(virDomainSnapshotPtr snapshot,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6240
    int ret;
6241 6242 6243 6244

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6245
        return -1;
6246 6247 6248 6249

    ret = (vm->current_snapshot &&
           STREQ(snapshot->name, vm->current_snapshot->def->name));

6250
    virDomainObjEndAPI(&vm);
6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264
    return ret;
}


static int
testDomainSnapshotHasMetadata(virDomainSnapshotPtr snapshot,
                              unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6265
        return -1;
6266

C
Cole Robinson 已提交
6267
    if (!testSnapObjFromSnapshot(vm, snapshot))
6268 6269 6270 6271
        goto cleanup;

    ret = 1;

6272
 cleanup:
6273
    virDomainObjEndAPI(&vm);
6274 6275 6276
    return ret;
}

6277 6278 6279 6280 6281 6282
static int
testDomainSnapshotAlignDisks(virDomainObjPtr vm,
                             virDomainSnapshotDefPtr def,
                             unsigned int flags)
{
    int align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
E
Eric Blake 已提交
6283
    bool align_match = true;
6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311

    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)
{
6312
    testDriverPtr privconn = domain->conn->privateData;
6313 6314 6315 6316
    virDomainObjPtr vm = NULL;
    virDomainSnapshotDefPtr def = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    virDomainSnapshotPtr snapshot = NULL;
6317
    virObjectEventPtr event = NULL;
6318
    char *xml = NULL;
6319 6320
    bool update_current = true;
    bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE;
6321 6322 6323 6324 6325 6326 6327 6328
    unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;

    /*
     * DISK_ONLY: Not implemented yet
     * REUSE_EXT: Not implemented yet
     *
     * NO_METADATA: Explicitly not implemented
     *
6329
     * REDEFINE + CURRENT: Implemented
6330 6331 6332 6333 6334 6335
     * HALT: Implemented
     * QUIESCE: Nothing to do
     * ATOMIC: Nothing to do
     * LIVE: Nothing to do
     */
    virCheckFlags(
6336 6337
        VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
        VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT |
6338 6339 6340 6341 6342
        VIR_DOMAIN_SNAPSHOT_CREATE_HALT |
        VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE |
        VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC |
        VIR_DOMAIN_SNAPSHOT_CREATE_LIVE, NULL);

6343 6344 6345 6346 6347
    if ((redefine && !(flags & VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT)))
        update_current = false;
    if (redefine)
        parse_flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;

6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362
    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;

6363
    if (redefine) {
C
Cole Robinson 已提交
6364 6365
        if (virDomainSnapshotRedefinePrep(domain, vm, &def, &snap,
                                          &update_current, flags) < 0)
6366 6367 6368 6369 6370
            goto cleanup;
    } else {
        if (!(def->dom = virDomainDefCopy(vm->def,
                                          privconn->caps,
                                          privconn->xmlopt,
6371
                                          NULL,
6372 6373
                                          true)))
            goto cleanup;
6374

6375
        if (testDomainSnapshotAlignDisks(vm, def, flags) < 0)
6376 6377 6378
            goto cleanup;
    }

6379 6380 6381 6382
    if (!snap) {
        if (!(snap = virDomainSnapshotAssignDef(vm->snapshots, def)))
            goto cleanup;
        def = NULL;
6383 6384
    }

6385 6386 6387 6388 6389 6390 6391 6392 6393 6394
    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);
6395
            event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
6396 6397 6398
                                    VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
        }
    }
6399 6400

    snapshot = virGetDomainSnapshot(domain, snap->def->name);
6401
 cleanup:
6402 6403 6404 6405
    VIR_FREE(xml);
    if (vm) {
        if (snapshot) {
            virDomainSnapshotObjPtr other;
6406 6407
            if (update_current)
                vm->current_snapshot = snap;
6408 6409 6410 6411 6412 6413 6414
            other = virDomainSnapshotFindByName(vm->snapshots,
                                                snap->def->parent);
            snap->parent = other;
            other->nchildren++;
            snap->sibling = other->first_child;
            other->first_child = snap;
        }
6415
        virDomainObjEndAPI(&vm);
6416
    }
6417
    testObjectEventQueue(privconn, event);
6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429
    virDomainSnapshotDefFree(def);
    return snapshot;
}


typedef struct _testSnapRemoveData testSnapRemoveData;
typedef testSnapRemoveData *testSnapRemoveDataPtr;
struct _testSnapRemoveData {
    virDomainObjPtr vm;
    bool current;
};

6430
static int
6431
testDomainSnapshotDiscardAll(void *payload,
6432 6433
                             const void *name ATTRIBUTE_UNUSED,
                             void *data)
6434 6435 6436 6437 6438 6439 6440
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapRemoveDataPtr curr = data;

    if (snap->def->current)
        curr->current = true;
    virDomainSnapshotObjListRemove(curr->vm->snapshots, snap);
6441
    return 0;
6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452
}

typedef struct _testSnapReparentData testSnapReparentData;
typedef testSnapReparentData *testSnapReparentDataPtr;
struct _testSnapReparentData {
    virDomainSnapshotObjPtr parent;
    virDomainObjPtr vm;
    int err;
    virDomainSnapshotObjPtr last;
};

6453
static int
6454 6455 6456 6457 6458 6459 6460
testDomainSnapshotReparentChildren(void *payload,
                                   const void *name ATTRIBUTE_UNUSED,
                                   void *data)
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapReparentDataPtr rep = data;

6461
    if (rep->err < 0)
6462
        return 0;
6463 6464 6465 6466 6467 6468 6469

    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;
6470
        return 0;
6471 6472 6473 6474
    }

    if (!snap->sibling)
        rep->last = snap;
6475
    return 0;
6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504
}

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) {
6505
            if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY)
6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548
                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;
6549
 cleanup:
6550
    virDomainObjEndAPI(&vm);
6551 6552 6553 6554 6555 6556 6557
    return ret;
}

static int
testDomainRevertToSnapshot(virDomainSnapshotPtr snapshot,
                           unsigned int flags)
{
6558
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6559 6560
    virDomainObjPtr vm = NULL;
    virDomainSnapshotObjPtr snap = NULL;
6561 6562
    virObjectEventPtr event = NULL;
    virObjectEventPtr event2 = NULL;
6563 6564 6565 6566 6567 6568 6569 6570 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 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625
    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;
6626 6627
    config = virDomainDefCopy(snap->def->dom, privconn->caps,
                              privconn->xmlopt, NULL, true);
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
    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.  */
            if (!virDomainDefCheckABIStability(vm->def, config)) {
                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);
6654
                event = virDomainEventLifecycleNewFromObj(vm,
6655 6656
                            VIR_DOMAIN_EVENT_STOPPED,
                            VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
6657
                testObjectEventQueue(privconn, event);
6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668
                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. */
6669
                event = virDomainEventLifecycleNewFromObj(vm,
6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682
                                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;
6683
            event = virDomainEventLifecycleNewFromObj(vm,
6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696
                                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 */
6697
                event2 = virDomainEventLifecycleNewFromObj(vm,
6698 6699 6700 6701 6702
                                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 已提交
6703
            virObjectUnref(event);
6704 6705 6706 6707
            event = NULL;

            if (was_stopped) {
                /* Transition 2 */
6708
                event = virDomainEventLifecycleNewFromObj(vm,
6709 6710 6711 6712
                                VIR_DOMAIN_EVENT_STARTED,
                                VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            } else if (was_running) {
                /* Transition 8 */
6713
                event = virDomainEventLifecycleNewFromObj(vm,
6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725
                                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);
6726
            event = virDomainEventLifecycleNewFromObj(vm,
6727 6728 6729 6730 6731 6732 6733 6734 6735
                                    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;

6736
            testObjectEventQueue(privconn, event);
6737
            event = virDomainEventLifecycleNewFromObj(vm,
6738 6739 6740
                            VIR_DOMAIN_EVENT_STARTED,
                            VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            if (paused) {
6741
                event2 = virDomainEventLifecycleNewFromObj(vm,
6742 6743 6744 6745 6746 6747 6748 6749
                                VIR_DOMAIN_EVENT_SUSPENDED,
                                VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT);
            }
        }
    }

    vm->current_snapshot = snap;
    ret = 0;
6750
 cleanup:
6751
    if (event) {
6752
        testObjectEventQueue(privconn, event);
6753
        testObjectEventQueue(privconn, event2);
C
Cole Robinson 已提交
6754
    } else {
C
Cédric Bosdonnat 已提交
6755
        virObjectUnref(event2);
6756
    }
6757
    virDomainObjEndAPI(&vm);
6758 6759 6760 6761 6762

    return ret;
}


6763

6764
static virHypervisorDriver testHypervisorDriver = {
6765
    .name = "Test",
6766 6767 6768
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
6769
    .connectGetHostname = testConnectGetHostname, /* 0.6.3 */
6770
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
6771
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
6772
    .nodeGetCPUStats = testNodeGetCPUStats, /* 2.3.0 */
6773
    .nodeGetFreeMemory = testNodeGetFreeMemory, /* 2.3.0 */
6774
    .nodeGetFreePages = testNodeGetFreePages, /* 2.3.0 */
6775
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
6776
    .connectGetSysinfo = testConnectGetSysinfo, /* 2.3.0 */
6777
    .connectGetType = testConnectGetType, /* 2.3.0 */
6778 6779 6780
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
6781
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795
    .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 */
    .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 */
6796 6797
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
6798
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
6799
    .domainRestore = testDomainRestore, /* 0.3.2 */
6800
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
6801
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
6802
    .domainCoreDumpWithFormat = testDomainCoreDumpWithFormat, /* 1.2.3 */
6803
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
6804 6805 6806 6807
    .domainSetVcpusFlags = testDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = testDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = testDomainPinVcpu, /* 0.7.3 */
    .domainGetVcpus = testDomainGetVcpus, /* 0.7.3 */
6808
    .domainGetVcpuPinInfo = testDomainGetVcpuPinInfo, /* 1.2.18 */
6809 6810
    .domainGetMaxVcpus = testDomainGetMaxVcpus, /* 0.7.3 */
    .domainGetXMLDesc = testDomainGetXMLDesc, /* 0.1.4 */
6811 6812
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
6813 6814 6815
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
6816
    .domainDefineXMLFlags = testDomainDefineXMLFlags, /* 1.2.12 */
6817
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
6818
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
6819 6820 6821
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
6822 6823 6824 6825
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
6826 6827 6828
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
6829 6830 6831 6832
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
6833 6834 6835
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
6836 6837 6838
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
6839
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
6840
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
6841 6842
    .domainGetMetadata = testDomainGetMetadata, /* 1.1.3 */
    .domainSetMetadata = testDomainSetMetadata, /* 1.1.3 */
6843
    .connectGetCPUModelNames = testConnectGetCPUModelNames, /* 1.1.3 */
6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860
    .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 */
6861 6862 6863
    .domainSnapshotCreateXML = testDomainSnapshotCreateXML, /* 1.1.4 */
    .domainRevertToSnapshot = testDomainRevertToSnapshot, /* 1.1.4 */
    .domainSnapshotDelete = testDomainSnapshotDelete, /* 1.1.4 */
6864

E
Eric Blake 已提交
6865
    .connectBaselineCPU = testConnectBaselineCPU, /* 1.2.0 */
6866 6867 6868
};

static virNetworkDriver testNetworkDriver = {
6869 6870 6871 6872 6873
    .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 */
6874 6875
    .connectNetworkEventRegisterAny = testConnectNetworkEventRegisterAny, /* 1.2.1 */
    .connectNetworkEventDeregisterAny = testConnectNetworkEventDeregisterAny, /* 1.2.1 */
6876 6877 6878 6879
    .networkLookupByUUID = testNetworkLookupByUUID, /* 0.3.2 */
    .networkLookupByName = testNetworkLookupByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreateXML, /* 0.3.2 */
    .networkDefineXML = testNetworkDefineXML, /* 0.3.2 */
6880
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
6881
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
6882
    .networkCreate = testNetworkCreate, /* 0.3.2 */
6883 6884 6885 6886 6887 6888 6889
    .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 */
6890 6891
};

L
Laine Stump 已提交
6892
static virInterfaceDriver testInterfaceDriver = {
6893 6894 6895 6896 6897 6898
    .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 */
6899 6900 6901 6902 6903 6904
    .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 */
6905 6906 6907
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
6908 6909 6910
};


6911
static virStorageDriver testStorageDriver = {
6912 6913 6914 6915 6916 6917
    .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 */
6918 6919
    .connectStoragePoolEventRegisterAny = testConnectStoragePoolEventRegisterAny, /* 2.0.0 */
    .connectStoragePoolEventDeregisterAny = testConnectStoragePoolEventDeregisterAny, /* 2.0.0 */
6920 6921 6922
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
6923 6924
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
6925 6926
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
6927
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
6928 6929 6930 6931 6932 6933 6934
    .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 */
6935
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
6936 6937 6938
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

6939 6940 6941 6942 6943 6944 6945 6946 6947
    .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 */
6948 6949
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
6950 6951
};

6952
static virNodeDeviceDriver testNodeDeviceDriver = {
6953 6954
    .connectNodeDeviceEventRegisterAny = testConnectNodeDeviceEventRegisterAny, /* 2.2.0 */
    .connectNodeDeviceEventDeregisterAny = testConnectNodeDeviceEventDeregisterAny, /* 2.2.0 */
6955 6956 6957 6958 6959 6960 6961 6962 6963
    .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 */
6964 6965
};

6966 6967 6968 6969 6970 6971 6972 6973
static virConnectDriver testConnectDriver = {
    .hypervisorDriver = &testHypervisorDriver,
    .interfaceDriver = &testInterfaceDriver,
    .networkDriver = &testNetworkDriver,
    .nodeDeviceDriver = &testNodeDeviceDriver,
    .nwfilterDriver = NULL,
    .secretDriver = NULL,
    .storageDriver = &testStorageDriver,
6974 6975
};

6976 6977 6978 6979 6980 6981 6982 6983
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
6984 6985
    return virRegisterConnectDriver(&testConnectDriver,
                                    false);
6986
}