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

24
#include <config.h>
25

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

35

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

71 72
#define VIR_FROM_THIS VIR_FROM_TEST

73 74
VIR_LOG_INIT("test.test_driver");

75

76 77 78 79
#define MAX_CPUS 128

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

#define MAX_CELLS 128
88

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

96
struct _testDriver {
97
    virMutex lock;
98

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

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

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

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

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

128
static testDriverPtr 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
static void
testDriverFree(testDriverPtr driver)
{
    if (!driver)
        return;

    virObjectUnref(driver->caps);
    virObjectUnref(driver->xmlopt);
    virObjectUnref(driver->domains);
155
    virNodeDeviceObjListFree(driver->devs);
156
    virObjectUnref(driver->networks);
157
    virInterfaceObjListFree(driver->ifaces);
158
    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, NULL, NULL)) ||
418
        !(ret->eventState = virObjectEventStateNew()) ||
419
        !(ret->ifaces = virInterfaceObjListNew()) ||
420
        !(ret->domains = virDomainObjListNew()) ||
421 422
        !(ret->networks = virNetworkObjListNew()) ||
        !(ret->devs = virNodeDeviceObjListNew()))
423 424
        goto error;

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

    return ret;

 error:
    testDriverFree(ret);
    return NULL;
}


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

C
Cole Robinson 已提交
543

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

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

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

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

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

    return vm;
}

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

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

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

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

        if (!found)
            return ifname;
616
        VIR_FREE(ifname);
617 618
    }

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

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

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

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

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

641
    return 0;
642 643
}

644

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

653 654 655 656
    if (domain)
        domain->id = -1;
}

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

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

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

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

682

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

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

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

C
Cole Robinson 已提交
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 744 745
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;
    }

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

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

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

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

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

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

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

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

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

835
    return 0;
836
 error:
837 838 839
    return -1;
}

840
static int
841
testParseDomainSnapshots(testDriverPtr privconn,
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 890 891
                         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;
892
 error:
893 894 895
    return ret;
}

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

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

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

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

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

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

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

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

952
        virObjectUnlock(obj);
953
    }
954

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

961

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

    num = virXPathNodeSet("/node/network", ctxt, &nodes);
973
    if (num < 0)
974
        goto error;
975 976

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

C
Cole Robinson 已提交
982 983 984
        def = virNetworkDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
985

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

991
        virNetworkObjSetActive(obj, true);
992
        virNetworkObjEndAPI(&obj);
993
    }
994

995
    ret = 0;
996
 error:
997 998 999 1000
    VIR_FREE(nodes);
    return ret;
}

1001

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

    num = virXPathNodeSet("/node/interface", ctxt, &nodes);
1013
    if (num < 0)
L
Laine Stump 已提交
1014
        goto error;
1015 1016

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

C
Cole Robinson 已提交
1023 1024 1025
        def = virInterfaceDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
1026

1027
        if (!(obj = virInterfaceObjListAssignDef(privconn->ifaces, def))) {
L
Laine Stump 已提交
1028 1029 1030
            virInterfaceDefFree(def);
            goto error;
        }
1031

1032
        virInterfaceObjSetActive(obj, true);
1033
        virInterfaceObjEndAPI(&obj);
1034 1035 1036
    }

    ret = 0;
1037
 error:
1038 1039 1040 1041
    VIR_FREE(nodes);
    return ret;
}

1042

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

    /* Find storage volumes */
1056
    if (virAsprintf(&vol_xpath, "/node/pool[%d]/volume", objidx) < 0)
1057 1058 1059 1060
        goto error;

    num = virXPathNodeSet(vol_xpath, ctxt, &nodes);
    VIR_FREE(vol_xpath);
1061
    if (num < 0)
1062 1063 1064
        goto error;

    for (i = 0; i < num; i++) {
C
Cole Robinson 已提交
1065 1066 1067 1068
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                   "volume");
        if (!node)
            goto error;
1069

1070
        def = virStorageVolDefParseNode(obj->def, ctxt->doc, node, 0);
C
Cole Robinson 已提交
1071 1072
        if (!def)
            goto error;
1073 1074 1075

        if (def->target.path == NULL) {
            if (virAsprintf(&def->target.path, "%s/%s",
1076
                            obj->def->target.path, def->name) < 0)
1077 1078 1079 1080 1081
                goto error;
        }

        if (!def->key && VIR_STRDUP(def->key, def->target.path) < 0)
            goto error;
1082 1083

        if (virStoragePoolObjAddVol(obj, def) < 0)
1084
            goto error;
1085

1086 1087
        obj->def->allocation += def->target.allocation;
        obj->def->available = (obj->def->capacity - obj->def->allocation);
1088
        def = NULL;
L
Laine Stump 已提交
1089 1090
    }

1091
    ret = 0;
1092
 error:
1093 1094 1095 1096 1097
    virStorageVolDefFree(def);
    VIR_FREE(nodes);
    return ret;
}

1098

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

    num = virXPathNodeSet("/node/pool", ctxt, &nodes);
1110
    if (num < 0)
C
Cole Robinson 已提交
1111
        goto error;
1112 1113

    for (i = 0; i < num; i++) {
C
Cole Robinson 已提交
1114
        virStoragePoolDefPtr def;
C
Cole Robinson 已提交
1115 1116 1117 1118
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                   "pool");
        if (!node)
            goto error;
C
Cole Robinson 已提交
1119

C
Cole Robinson 已提交
1120 1121 1122
        def = virStoragePoolDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
C
Cole Robinson 已提交
1123

1124
        if (!(obj = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
1125 1126 1127 1128 1129
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

1130 1131
        if (testStoragePoolObjSetDefaults(obj) == -1) {
            virStoragePoolObjUnlock(obj);
C
Cole Robinson 已提交
1132
            goto error;
1133
        }
1134
        virStoragePoolObjSetActive(obj, true);
1135 1136

        /* Find storage volumes */
C
Cole Robinson 已提交
1137
        if (testOpenVolumesForPool(file, ctxt, obj, i+1) < 0) {
1138
            virStoragePoolObjUnlock(obj);
1139 1140 1141
            goto error;
        }

1142
        virStoragePoolObjUnlock(obj);
C
Cole Robinson 已提交
1143 1144
    }

1145
    ret = 0;
1146
 error:
1147 1148 1149 1150
    VIR_FREE(nodes);
    return ret;
}

1151

1152
static int
1153
testParseNodedevs(testDriverPtr privconn,
C
Cole Robinson 已提交
1154 1155
                  const char *file,
                  xmlXPathContextPtr ctxt)
1156 1157 1158 1159 1160 1161 1162
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virNodeDeviceObjPtr obj;

    num = virXPathNodeSet("/node/device", ctxt, &nodes);
1163
    if (num < 0)
1164
        goto error;
1165 1166

    for (i = 0; i < num; i++) {
1167
        virNodeDeviceDefPtr def;
C
Cole Robinson 已提交
1168 1169 1170 1171
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                  "nodedev");
        if (!node)
            goto error;
1172

C
Cole Robinson 已提交
1173 1174 1175
        def = virNodeDeviceDefParseNode(ctxt->doc, node, 0, NULL);
        if (!def)
            goto error;
1176

1177
        if (!(obj = virNodeDeviceObjListAssignDef(privconn->devs, def))) {
1178 1179 1180
            virNodeDeviceDefFree(def);
            goto error;
        }
1181

1182
        virNodeDeviceObjEndAPI(&obj);
1183 1184 1185
    }

    ret = 0;
1186
 error:
1187 1188 1189 1190
    VIR_FREE(nodes);
    return ret;
}

1191
static int
1192
testParseAuthUsers(testDriverPtr privconn,
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 1220 1221 1222 1223 1224 1225
                   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;
1226
 error:
1227 1228 1229
    VIR_FREE(nodes);
    return ret;
}
1230

C
Cole Robinson 已提交
1231 1232 1233 1234 1235
static int
testOpenParse(testDriverPtr privconn,
              const char *file,
              xmlXPathContextPtr ctxt)
{
1236
    if (!virXMLNodeNameEqual(ctxt->node, "node")) {
C
Cole Robinson 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
        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;
}

1262 1263
/* No shared state between simultaneous test connections initialized
 * from a file.  */
1264 1265 1266 1267 1268
static int
testOpenFromFile(virConnectPtr conn, const char *file)
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
1269
    testDriverPtr privconn;
1270

1271
    if (!(privconn = testDriverNew()))
1272
        return VIR_DRV_OPEN_ERROR;
1273

1274 1275 1276 1277 1278 1279
    testDriverLock(privconn);
    conn->privateData = privconn;

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

1280
    if (!(doc = virXMLParseFileCtxt(file, &ctxt)))
1281 1282 1283 1284 1285
        goto error;

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

C
Cole Robinson 已提交
1286
    if (testOpenParse(privconn, file, ctxt) < 0)
1287
        goto error;
1288

J
Jim Meyering 已提交
1289
    xmlXPathFreeContext(ctxt);
1290
    xmlFreeDoc(doc);
1291
    testDriverUnlock(privconn);
1292

1293
    return 0;
1294 1295

 error:
J
Jim Meyering 已提交
1296
    xmlXPathFreeContext(ctxt);
1297
    xmlFreeDoc(doc);
1298
    testDriverFree(privconn);
1299
    conn->privateData = NULL;
1300
    return VIR_DRV_OPEN_ERROR;
1301 1302
}

1303 1304 1305 1306 1307 1308 1309
/* 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;
1310 1311
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
1312
    size_t i;
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329

    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;
1330 1331 1332 1333
    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;
1334
    }
1335
    for (i = 0; i < 16; i++) {
1336 1337 1338
        virBitmapPtr siblings = virBitmapNew(16);
        if (!siblings)
            goto error;
1339 1340 1341 1342 1343
        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;
1344 1345 1346 1347 1348
    }

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

1349 1350
    if (!(doc = virXMLParseStringCtxt(defaultConnXML,
                                      _("(test driver)"), &ctxt)))
1351 1352
        goto error;

1353
    if (testOpenParse(privconn, NULL, ctxt) < 0)
1354 1355 1356 1357
        goto error;

    defaultConn = privconn;

1358 1359
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);
1360 1361 1362 1363 1364 1365
    virMutexUnlock(&defaultLock);

    return VIR_DRV_OPEN_SUCCESS;

 error:
    testDriverFree(privconn);
1366 1367
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);
1368 1369 1370 1371 1372 1373
    conn->privateData = NULL;
    defaultConnections--;
    virMutexUnlock(&defaultLock);
    return VIR_DRV_OPEN_ERROR;
}

1374 1375 1376 1377
static int
testConnectAuthenticate(virConnectPtr conn,
                        virConnectAuthPtr auth)
{
1378
    testDriverPtr privconn = conn->privateData;
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
    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;

1403
 found_user:
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
    /* 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;
1423
 cleanup:
1424 1425 1426 1427
    VIR_FREE(username);
    VIR_FREE(password);
    return ret;
}
1428

1429
static virDrvOpenStatus testConnectOpen(virConnectPtr conn,
1430
                                        virConnectAuthPtr auth,
1431
                                        virConfPtr conf ATTRIBUTE_UNUSED,
1432
                                        unsigned int flags)
1433
{
1434
    int ret;
1435

E
Eric Blake 已提交
1436 1437
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1438
    if (!conn->uri)
1439
        return VIR_DRV_OPEN_DECLINED;
1440

1441
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1442
        return VIR_DRV_OPEN_DECLINED;
1443

1444
    /* Remote driver should handle these. */
1445
    if (conn->uri->server)
1446 1447
        return VIR_DRV_OPEN_DECLINED;

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

1457
    if (STREQ(conn->uri->path, "/default"))
1458 1459
        ret = testOpenDefault(conn);
    else
1460
        ret = testOpenFromFile(conn,
1461
                               conn->uri->path);
1462

1463 1464 1465
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

1466 1467 1468 1469
    /* Fake authentication. */
    if (testConnectAuthenticate(conn, auth) < 0)
        return VIR_DRV_OPEN_ERROR;

1470
    return VIR_DRV_OPEN_SUCCESS;
1471 1472
}

1473
static int testConnectClose(virConnectPtr conn)
1474
{
1475
    testDriverPtr privconn = conn->privateData;
1476
    bool dflt = false;
1477

1478 1479
    if (privconn == defaultConn) {
        dflt = true;
1480 1481 1482 1483 1484 1485 1486
        virMutexLock(&defaultLock);
        if (--defaultConnections) {
            virMutexUnlock(&defaultLock);
            return 0;
        }
    }

1487
    testDriverLock(privconn);
1488
    testDriverFree(privconn);
1489 1490 1491

    if (dflt) {
        defaultConn = NULL;
1492
        virMutexUnlock(&defaultLock);
1493 1494
    }

1495
    conn->privateData = NULL;
1496
    return 0;
1497 1498
}

1499 1500
static int testConnectGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                                 unsigned long *hvVer)
1501
{
1502
    *hvVer = 2;
1503
    return 0;
1504 1505
}

1506 1507 1508 1509 1510 1511
static char *testConnectGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return virGetHostname();
}


1512
static int testConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
1513 1514 1515 1516
{
    return 1;
}

1517
static int testConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
1518 1519 1520 1521
{
    return 0;
}

1522
static int testConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
1523 1524 1525 1526
{
    return 1;
}

1527 1528
static int testConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type ATTRIBUTE_UNUSED)
1529 1530 1531 1532
{
    return 32;
}

1533 1534 1535 1536 1537 1538
static char *
testConnectBaselineCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
                       const char **xmlCPUs,
                       unsigned int ncpus,
                       unsigned int flags)
{
J
Jiri Denemark 已提交
1539 1540 1541
    virCPUDefPtr *cpus = NULL;
    virCPUDefPtr cpu = NULL;
    char *cpustr = NULL;
1542 1543 1544

    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, NULL);

J
Jiri Denemark 已提交
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
    if (!(cpus = virCPUDefListParse(xmlCPUs, ncpus, VIR_CPU_TYPE_HOST)))
        goto cleanup;

    if (!(cpu = cpuBaseline(cpus, ncpus, NULL, 0, false)))
        goto cleanup;

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

    cpustr = virCPUDefFormat(cpu, NULL, false);

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

J
Jiri Denemark 已提交
1561
    return cpustr;
1562 1563
}

1564 1565
static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1566
{
1567
    testDriverPtr privconn = conn->privateData;
1568
    testDriverLock(privconn);
1569
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1570
    testDriverUnlock(privconn);
1571
    return 0;
1572 1573
}

1574
static char *testConnectGetCapabilities(virConnectPtr conn)
1575
{
1576
    testDriverPtr privconn = conn->privateData;
1577
    char *xml;
1578
    testDriverLock(privconn);
1579
    xml = virCapabilitiesFormatXML(privconn->caps);
1580
    testDriverUnlock(privconn);
1581
    return xml;
1582 1583
}

1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
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;
}

1604 1605 1606 1607 1608 1609
static const char *
testConnectGetType(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return "TEST";
}

1610
static int testConnectNumOfDomains(virConnectPtr conn)
1611
{
1612
    testDriverPtr privconn = conn->privateData;
1613
    int count;
1614

1615
    testDriverLock(privconn);
1616
    count = virDomainObjListNumOfDomains(privconn->domains, true, NULL, NULL);
1617
    testDriverUnlock(privconn);
1618

1619
    return count;
1620 1621
}

1622 1623 1624
static int testDomainIsActive(virDomainPtr dom)
{
    virDomainObjPtr obj;
1625
    int ret;
1626

1627 1628
    if (!(obj = testDomObjFromDomain(dom)))
        return -1;
1629

1630 1631
    ret = virDomainObjIsActive(obj);
    virDomainObjEndAPI(&obj);
1632 1633 1634 1635 1636 1637
    return ret;
}

static int testDomainIsPersistent(virDomainPtr dom)
{
    virDomainObjPtr obj;
1638 1639 1640 1641
    int ret;

    if (!(obj = testDomObjFromDomain(dom)))
        return -1;
1642 1643 1644

    ret = obj->persistent;

1645
    virDomainObjEndAPI(&obj);
1646 1647 1648
    return ret;
}

1649 1650 1651 1652 1653
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

1654
static virDomainPtr
1655
testDomainCreateXML(virConnectPtr conn, const char *xml,
1656
                      unsigned int flags)
1657
{
1658
    testDriverPtr privconn = conn->privateData;
1659
    virDomainPtr ret = NULL;
1660
    virDomainDefPtr def;
1661
    virDomainObjPtr dom = NULL;
1662
    virObjectEventPtr event = NULL;
1663
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
1664

1665 1666 1667
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_START_VALIDATE)
1668
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
1669

1670
    testDriverLock(privconn);
1671
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
1672
                                       NULL, parse_flags)) == NULL)
1673
        goto cleanup;
1674

1675
    if (testDomainGenerateIfnames(def) < 0)
1676
        goto cleanup;
1677
    if (!(dom = virDomainObjListAdd(privconn->domains,
1678
                                    def,
1679
                                    privconn->xmlopt,
1680
                                    VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
1681 1682
                                    VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                    NULL)))
1683 1684
        goto cleanup;
    def = NULL;
1685

1686 1687 1688 1689 1690
    if (testDomainStartState(privconn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
        if (!dom->persistent) {
            virDomainObjListRemove(privconn->domains, dom);
            dom = NULL;
        }
1691
        goto cleanup;
1692
    }
1693

1694
    event = virDomainEventLifecycleNewFromObj(dom,
1695 1696 1697
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1698
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1699

1700
 cleanup:
1701
    if (dom)
1702
        virObjectUnlock(dom);
1703
    testObjectEventQueue(privconn, event);
1704
    virDomainDefFree(def);
1705
    testDriverUnlock(privconn);
1706
    return ret;
1707 1708 1709
}


1710
static virDomainPtr testDomainLookupByID(virConnectPtr conn,
1711
                                         int id)
1712
{
1713
    testDriverPtr privconn = conn->privateData;
1714 1715
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1716

1717
    if (!(dom = virDomainObjListFindByID(privconn->domains, id))) {
1718
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1719
        goto cleanup;
1720 1721
    }

1722
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1723

1724
 cleanup:
1725
    if (dom)
1726
        virObjectUnlock(dom);
1727
    return ret;
1728 1729
}

1730
static virDomainPtr testDomainLookupByUUID(virConnectPtr conn,
1731
                                           const unsigned char *uuid)
1732
{
1733
    testDriverPtr privconn = conn->privateData;
1734
    virDomainPtr ret = NULL;
1735
    virDomainObjPtr dom;
1736

1737
    if (!(dom = virDomainObjListFindByUUID(privconn->domains, uuid))) {
1738
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1739
        goto cleanup;
1740
    }
1741

1742
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1743

1744
 cleanup:
1745
    if (dom)
1746
        virObjectUnlock(dom);
1747
    return ret;
1748 1749
}

1750
static virDomainPtr testDomainLookupByName(virConnectPtr conn,
1751
                                           const char *name)
1752
{
1753
    testDriverPtr privconn = conn->privateData;
1754 1755
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1756

1757
    if (!(dom = virDomainObjListFindByName(privconn->domains, name))) {
1758
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1759
        goto cleanup;
1760
    }
1761

1762
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1763

1764
 cleanup:
1765
    virDomainObjEndAPI(&dom);
1766
    return ret;
1767 1768
}

1769 1770 1771
static int testConnectListDomains(virConnectPtr conn,
                                  int *ids,
                                  int maxids)
1772
{
1773
    testDriverPtr privconn = conn->privateData;
1774

1775 1776
    return virDomainObjListGetActiveIDs(privconn->domains, ids, maxids,
                                        NULL, NULL);
1777 1778
}

1779
static int testDomainDestroy(virDomainPtr domain)
1780
{
1781
    testDriverPtr privconn = domain->conn->privateData;
1782
    virDomainObjPtr privdom;
1783
    virObjectEventPtr event = NULL;
1784
    int ret = -1;
1785

1786
    if (!(privdom = testDomObjFromDomain(domain)))
1787
        goto cleanup;
1788

1789 1790 1791 1792 1793 1794
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto cleanup;
    }

J
Jiri Denemark 已提交
1795
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1796
    event = virDomainEventLifecycleNewFromObj(privdom,
1797 1798
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1799

1800 1801
    if (!privdom->persistent)
        virDomainObjListRemove(privconn->domains, privdom);
1802 1803

    ret = 0;
1804
 cleanup:
1805
    virDomainObjEndAPI(&privdom);
1806
    testObjectEventQueue(privconn, event);
1807
    return ret;
1808 1809
}

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

1817 1818
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1819

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

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

1833
 cleanup:
1834
    virDomainObjEndAPI(&privdom);
1835
    testObjectEventQueue(privconn, event);
1836
    return ret;
1837 1838
}

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

1847 1848
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1849

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

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

1863
 cleanup:
1864
    virDomainObjEndAPI(&privdom);
1865
    testObjectEventQueue(privconn, event);
1866
    return ret;
1867 1868
}

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

1877 1878
    virCheckFlags(0, -1);

1879

1880
    if (!(privdom = testDomObjFromDomain(domain)))
1881
        goto cleanup;
1882

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

J
Jiri Denemark 已提交
1889
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1890
    event = virDomainEventLifecycleNewFromObj(privdom,
1891 1892
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1893

1894 1895
    if (!privdom->persistent)
        virDomainObjListRemove(privconn->domains, privdom);
1896

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

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

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


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

1922 1923 1924 1925 1926 1927
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto cleanup;
    }

J
Jiri Denemark 已提交
1928 1929 1930
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

1931 1932
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
J
Jiri Denemark 已提交
1933 1934
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1935 1936
        break;

1937
    case VIR_DOMAIN_LIFECYCLE_RESTART:
J
Jiri Denemark 已提交
1938 1939
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1940 1941
        break;

1942
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
J
Jiri Denemark 已提交
1943 1944
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1945 1946
        break;

1947
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
J
Jiri Denemark 已提交
1948 1949
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1950
        break;
1951

1952
    default:
J
Jiri Denemark 已提交
1953 1954
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1955 1956
        break;
    }
1957

J
Jiri Denemark 已提交
1958 1959
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1960
        event = virDomainEventLifecycleNewFromObj(privdom,
1961 1962
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1963

1964 1965
        if (!privdom->persistent)
            virDomainObjListRemove(privconn->domains, privdom);
1966 1967
    }

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

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

1982 1983
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1984 1985

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

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

1998
 cleanup:
1999
    virDomainObjEndAPI(&privdom);
2000
    return ret;
2001 2002
}

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

    virCheckFlags(0, -1);

2013 2014
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2015

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

2018
    virDomainObjEndAPI(&privdom);
2019 2020

    return 0;
2021 2022
}

2023 2024
#define TEST_SAVE_MAGIC "TestGuestMagic"

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

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

2044

2045
    if (!(privdom = testDomObjFromDomain(domain)))
2046
        goto cleanup;
2047

2048 2049 2050 2051 2052 2053
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto cleanup;
    }

2054
    xml = virDomainDefFormat(privdom->def, privconn->caps,
2055
                             VIR_DOMAIN_DEF_FORMAT_SECURE);
C
Cole Robinson 已提交
2056

2057
    if (xml == NULL) {
2058
        virReportSystemError(errno,
2059 2060
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
2061
        goto cleanup;
2062
    }
2063 2064

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

2090
    if (VIR_CLOSE(fd) < 0) {
2091
        virReportSystemError(errno,
2092 2093
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2094
        goto cleanup;
2095
    }
2096 2097
    fd = -1;

J
Jiri Denemark 已提交
2098
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
2099
    event = virDomainEventLifecycleNewFromObj(privdom,
2100 2101
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
2102

2103 2104
    if (!privdom->persistent)
        virDomainObjListRemove(privconn->domains, privdom);
2105

2106
    ret = 0;
2107
 cleanup:
2108 2109 2110 2111
    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 已提交
2112
     * and have reported an earlier error */
2113
    if (ret != 0) {
2114
        VIR_FORCE_CLOSE(fd);
2115 2116
        unlink(path);
    }
2117
    virDomainObjEndAPI(&privdom);
2118
    testObjectEventQueue(privconn, event);
2119
    return ret;
2120 2121
}

2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
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)
2134
{
2135
    testDriverPtr privconn = conn->privateData;
2136
    char *xml = NULL;
2137
    char magic[15];
2138 2139 2140
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
2141
    virDomainObjPtr dom = NULL;
2142
    virObjectEventPtr event = NULL;
2143
    int ret = -1;
2144

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

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

2189
    def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
2190
                                  NULL, VIR_DOMAIN_DEF_PARSE_INACTIVE);
2191
    if (!def)
2192
        goto cleanup;
2193

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

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

2213
    event = virDomainEventLifecycleNewFromObj(dom,
2214 2215
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
2216
    ret = 0;
2217

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

2228 2229 2230 2231 2232 2233 2234
static int
testDomainRestore(virConnectPtr conn,
                  const char *path)
{
    return testDomainRestoreFlags(conn, path, NULL, 0);
}

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

E
Eric Blake 已提交
2246 2247
    virCheckFlags(VIR_DUMP_CRASH, -1);

2248

2249
    if (!(privdom = testDomObjFromDomain(domain)))
2250
        goto cleanup;
2251

2252 2253 2254 2255 2256 2257
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto cleanup;
    }

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

2277 2278 2279 2280 2281 2282 2283
    /* 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;
    }

2284
    if (flags & VIR_DUMP_CRASH) {
J
Jiri Denemark 已提交
2285
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_CRASHED);
2286
        event = virDomainEventLifecycleNewFromObj(privdom,
2287 2288
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
2289 2290
        if (!privdom->persistent)
            virDomainObjListRemove(privconn->domains, privdom);
2291
    }
2292

2293
    ret = 0;
2294
 cleanup:
2295
    VIR_FORCE_CLOSE(fd);
2296
    virDomainObjEndAPI(&privdom);
2297
    testObjectEventQueue(privconn, event);
2298
    return ret;
2299 2300
}

2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314

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)
{
2315 2316 2317
    char *ret;

    ignore_value(VIR_STRDUP(ret, "linux"));
2318
    return ret;
2319 2320
}

2321 2322 2323

static unsigned long long
testDomainGetMaxMemory(virDomainPtr domain)
2324
{
2325
    virDomainObjPtr privdom;
2326
    unsigned long long ret = 0;
2327

2328 2329
    if (!(privdom = testDomObjFromDomain(domain)))
        return 0;
2330

2331
    ret = virDomainDefGetMemoryTotal(privdom->def);
2332

2333
    virDomainObjEndAPI(&privdom);
2334
    return ret;
2335 2336
}

2337 2338
static int testDomainSetMaxMemory(virDomainPtr domain,
                                  unsigned long memory)
2339
{
2340 2341
    virDomainObjPtr privdom;

2342 2343
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2344 2345

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

2348
    virDomainObjEndAPI(&privdom);
2349
    return 0;
2350 2351
}

2352 2353
static int testDomainSetMemory(virDomainPtr domain,
                               unsigned long memory)
2354
{
2355
    virDomainObjPtr privdom;
2356
    int ret = -1;
2357

2358 2359
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2360

2361
    if (memory > virDomainDefGetMemoryTotal(privdom->def)) {
2362
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2363
        goto cleanup;
2364
    }
2365

2366
    privdom->def->mem.cur_balloon = memory;
2367 2368
    ret = 0;

2369
 cleanup:
2370
    virDomainObjEndAPI(&privdom);
2371
    return ret;
2372 2373
}

2374 2375
static int
testDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
C
Cole Robinson 已提交
2376
{
2377 2378 2379 2380
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;

2381 2382
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2383 2384
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

2385 2386
    if (!(vm = testDomObjFromDomain(domain)))
        return -1;
2387

2388
    if (!(def = virDomainObjGetOneDef(vm, flags)))
2389
        goto cleanup;
2390

2391 2392 2393
    if (flags & VIR_DOMAIN_VCPU_MAXIMUM)
        ret = virDomainDefGetVcpusMax(def);
    else
2394
        ret = virDomainDefGetVcpus(def);
2395

2396
 cleanup:
2397
    virDomainObjEndAPI(&vm);
2398
    return ret;
C
Cole Robinson 已提交
2399 2400
}

2401 2402 2403
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
2404
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2405 2406 2407 2408 2409 2410 2411
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

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

2418 2419
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2420 2421
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

2422
    if ((maxvcpus = testConnectGetMaxVcpus(domain->conn, NULL)) < 0)
2423
        return -1;
2424 2425

    if (nrCpus > maxvcpus) {
2426
        virReportError(VIR_ERR_INVALID_ARG,
2427 2428
                       _("requested cpu amount exceeds maximum supported amount "
                         "(%d > %d)"), nrCpus, maxvcpus);
2429 2430
        return -1;
    }
2431

2432 2433
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2434

2435
    if (virDomainObjGetDefs(privdom, flags, &def, &persistentDef) < 0)
C
Cole Robinson 已提交
2436 2437
        goto cleanup;

2438
    if (def && virDomainDefGetVcpusMax(def) < nrCpus) {
2439 2440
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested cpu amount exceeds maximum (%d > %d)"),
2441
                       nrCpus, virDomainDefGetVcpusMax(def));
2442
        goto cleanup;
2443
    }
2444

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

2454 2455 2456
    if (def &&
        virDomainDefSetVcpus(def, nrCpus) < 0)
        goto cleanup;
2457

2458 2459
    if (persistentDef) {
        if (flags & VIR_DOMAIN_VCPU_MAXIMUM) {
2460 2461
            if (virDomainDefSetVcpusMax(persistentDef, nrCpus,
                                        driver->xmlopt) < 0)
2462
                goto cleanup;
2463
        } else {
2464 2465
            if (virDomainDefSetVcpus(persistentDef, nrCpus) < 0)
                goto cleanup;
2466
        }
2467
    }
2468

2469 2470
    ret = 0;

2471
 cleanup:
2472
    virDomainObjEndAPI(&privdom);
2473
    return ret;
2474 2475
}

2476
static int
2477
testDomainSetVcpus(virDomainPtr domain, unsigned int nrCpus)
2478
{
2479
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2480 2481
}

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

2498 2499
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
C
Cole Robinson 已提交
2500 2501

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

2507
    def = privdom->def;
C
Cole Robinson 已提交
2508 2509

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

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

    hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
2518 2519 2520 2521 2522
    if (!(allcpumap = virBitmapNew(hostcpus)))
        goto cleanup;

    virBitmapSetAll(allcpumap);

C
Cole Robinson 已提交
2523
    /* Clamp to actual number of vcpus */
2524 2525
    if (maxinfo > virDomainDefGetVcpus(privdom->def))
        maxinfo = virDomainDefGetVcpus(privdom->def);
C
Cole Robinson 已提交
2526

2527 2528
    memset(info, 0, sizeof(*info) * maxinfo);
    memset(cpumaps, 0, maxinfo * maplen);
C
Cole Robinson 已提交
2529

2530
    for (i = 0; i < maxinfo; i++) {
2531
        virDomainVcpuDefPtr vcpu = virDomainDefGetVcpu(def, i);
2532
        virBitmapPtr bitmap = NULL;
C
Cole Robinson 已提交
2533

2534 2535
        if (!vcpu->online)
            continue;
C
Cole Robinson 已提交
2536

2537 2538
        if (vcpu->cpumask)
            bitmap = vcpu->cpumask;
2539 2540 2541 2542
        else if (def->cpumask)
            bitmap = def->cpumask;
        else
            bitmap = allcpumap;
C
Cole Robinson 已提交
2543

2544 2545
        if (cpumaps)
            virBitmapToDataBuf(bitmap, VIR_GET_CPUMAP(cpumaps, maplen, i), maplen);
C
Cole Robinson 已提交
2546

2547 2548 2549
        info[i].number = i;
        info[i].state = VIR_VCPU_RUNNING;
        info[i].cpu = virBitmapLastSetBit(bitmap);
C
Cole Robinson 已提交
2550

2551 2552
        /* Fake an increasing cpu time value */
        info[i].cpuTime = statbase / 10;
C
Cole Robinson 已提交
2553 2554 2555
    }

    ret = maxinfo;
2556
 cleanup:
2557
    virBitmapFree(allcpumap);
2558
    virDomainObjEndAPI(&privdom);
C
Cole Robinson 已提交
2559 2560 2561
    return ret;
}

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

2572 2573
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
C
Cole Robinson 已提交
2574

2575 2576
    def = privdom->def;

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

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

2591 2592 2593
    virBitmapFree(vcpuinfo->cpumask);

    if (!(vcpuinfo->cpumask = virBitmapNewData(cpumap, maplen)))
2594
        goto cleanup;
C
Cole Robinson 已提交
2595 2596

    ret = 0;
2597

2598
 cleanup:
2599
    virDomainObjEndAPI(&privdom);
C
Cole Robinson 已提交
2600 2601 2602
    return ret;
}

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

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

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

2621 2622 2623
    ret = virDomainDefGetVcpuPinInfoHelper(def, maplen, ncpumaps, cpumaps,
                                           VIR_NODEINFO_MAXCPUS(driver->nodeInfo),
                                           NULL);
2624 2625 2626 2627 2628 2629

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

2630
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2631
{
2632
    testDriverPtr privconn = domain->conn->privateData;
2633
    virDomainDefPtr def;
2634
    virDomainObjPtr privdom;
2635 2636
    char *ret = NULL;

2637 2638
    /* Flags checked by virDomainDefFormat */

2639 2640
    if (!(privdom = testDomObjFromDomain(domain)))
        return NULL;
2641

2642 2643
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2644

2645 2646
    ret = virDomainDefFormat(def, privconn->caps,
                             virDomainDefFormatConvertXMLFlags(flags));
2647

2648
    virDomainObjEndAPI(&privdom);
2649
    return ret;
2650
}
2651

2652 2653
static int testConnectNumOfDefinedDomains(virConnectPtr conn)
{
2654
    testDriverPtr privconn = conn->privateData;
2655

2656
    return virDomainObjListNumOfDomains(privconn->domains, false, NULL, NULL);
2657 2658
}

2659 2660
static int testConnectListDefinedDomains(virConnectPtr conn,
                                         char **const names,
2661 2662
                                         int maxnames)
{
2663

2664
    testDriverPtr privconn = conn->privateData;
2665 2666

    memset(names, 0, sizeof(*names)*maxnames);
2667 2668
    return virDomainObjListGetInactiveNames(privconn->domains, names, maxnames,
                                            NULL, NULL);
2669 2670
}

2671 2672 2673
static virDomainPtr testDomainDefineXMLFlags(virConnectPtr conn,
                                             const char *xml,
                                             unsigned int flags)
2674
{
2675
    testDriverPtr privconn = conn->privateData;
2676
    virDomainPtr ret = NULL;
2677
    virDomainDefPtr def;
2678
    virDomainObjPtr dom = NULL;
2679
    virObjectEventPtr event = NULL;
2680
    virDomainDefPtr oldDef = NULL;
2681 2682 2683
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;

    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);
2684

2685
    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
2686
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
2687

2688
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
2689
                                       NULL, parse_flags)) == NULL)
2690
        goto cleanup;
2691

2692 2693 2694
    if (virXMLCheckIllegalChars("name", def->name, "\n") < 0)
        goto cleanup;

2695
    if (testDomainGenerateIfnames(def) < 0)
2696
        goto cleanup;
2697
    if (!(dom = virDomainObjListAdd(privconn->domains,
2698
                                    def,
2699
                                    privconn->xmlopt,
2700 2701
                                    0,
                                    &oldDef)))
2702
        goto cleanup;
2703
    def = NULL;
2704
    dom->persistent = 1;
2705

2706
    event = virDomainEventLifecycleNewFromObj(dom,
2707
                                     VIR_DOMAIN_EVENT_DEFINED,
2708
                                     !oldDef ?
2709 2710
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2711

2712
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
2713

2714
 cleanup:
2715
    virDomainDefFree(def);
2716
    virDomainDefFree(oldDef);
2717
    if (dom)
2718
        virObjectUnlock(dom);
2719
    testObjectEventQueue(privconn, event);
2720
    return ret;
2721 2722
}

2723 2724 2725 2726 2727 2728
static virDomainPtr
testDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return testDomainDefineXMLFlags(conn, xml, 0);
}

2729 2730 2731 2732 2733 2734
static char *testDomainGetMetadata(virDomainPtr dom,
                                   int type,
                                   const char *uri,
                                   unsigned int flags)
{
    virDomainObjPtr privdom;
2735
    char *ret;
2736 2737 2738 2739

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, NULL);

2740 2741
    if (!(privdom = testDomObjFromDomain(dom)))
        return NULL;
2742

2743
    ret = virDomainObjGetMetadata(privdom, type, uri, flags);
2744

2745
    virDomainObjEndAPI(&privdom);
2746 2747 2748 2749 2750 2751 2752 2753 2754 2755
    return ret;
}

static int testDomainSetMetadata(virDomainPtr dom,
                                 int type,
                                 const char *metadata,
                                 const char *key,
                                 const char *uri,
                                 unsigned int flags)
{
2756
    testDriverPtr privconn = dom->conn->privateData;
2757
    virDomainObjPtr privdom;
2758
    int ret;
2759 2760 2761 2762

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

2763 2764
    if (!(privdom = testDomObjFromDomain(dom)))
        return -1;
2765 2766 2767

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

2770 2771 2772 2773 2774 2775
    if (ret == 0) {
        virObjectEventPtr ev = NULL;
        ev = virDomainEventMetadataChangeNewFromObj(privdom, type, uri);
        testObjectEventQueue(privconn, ev);
    }

2776
    virDomainObjEndAPI(&privdom);
2777 2778 2779 2780
    return ret;
}


2781 2782
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
2783 2784
                                      int startCell, int maxCells)
{
2785
    testDriverPtr privconn = conn->privateData;
2786 2787
    int cell;
    size_t i;
2788
    int ret = -1;
2789

2790
    testDriverLock(privconn);
2791
    if (startCell >= privconn->numCells) {
2792 2793
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("Range exceeds available cells"));
2794
        goto cleanup;
2795 2796
    }

2797 2798 2799 2800
    for (cell = startCell, i = 0;
         (cell < privconn->numCells && i < maxCells);
         ++cell, ++i) {
        freemems[i] = privconn->cells[cell].mem;
2801
    }
2802
    ret = i;
2803

2804
 cleanup:
2805
    testDriverUnlock(privconn);
2806
    return ret;
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 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854
#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;
}
2855

2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871
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;
}

2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895
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;
}

2896 2897
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
{
2898
    testDriverPtr privconn = domain->conn->privateData;
2899
    virDomainObjPtr privdom;
2900
    virObjectEventPtr event = NULL;
2901
    int ret = -1;
2902

2903 2904
    virCheckFlags(0, -1);

2905
    testDriverLock(privconn);
2906

2907
    if (!(privdom = testDomObjFromDomain(domain)))
2908
        goto cleanup;
2909

J
Jiri Denemark 已提交
2910
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
2911 2912
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Domain '%s' is already running"), domain->name);
2913
        goto cleanup;
2914 2915
    }

2916
    if (testDomainStartState(privconn, privdom,
J
Jiri Denemark 已提交
2917
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
2918 2919 2920
        goto cleanup;
    domain->id = privdom->def->id;

2921
    event = virDomainEventLifecycleNewFromObj(privdom,
2922 2923
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2924
    ret = 0;
2925

2926
 cleanup:
2927
    virDomainObjEndAPI(&privdom);
2928
    testObjectEventQueue(privconn, event);
2929
    testDriverUnlock(privconn);
2930
    return ret;
2931 2932
}

2933 2934
static int testDomainCreate(virDomainPtr domain)
{
2935 2936 2937
    return testDomainCreateWithFlags(domain, 0);
}

2938 2939 2940
static int testDomainUndefineFlags(virDomainPtr domain,
                                   unsigned int flags)
{
2941
    testDriverPtr privconn = domain->conn->privateData;
2942
    virDomainObjPtr privdom;
2943
    virObjectEventPtr event = NULL;
2944
    int nsnapshots;
2945
    int ret = -1;
2946

2947 2948
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
2949

2950

2951
    if (!(privdom = testDomObjFromDomain(domain)))
2952
        goto cleanup;
2953

C
Cole Robinson 已提交
2954 2955 2956 2957 2958 2959 2960 2961
    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;
    }

2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979
    /* 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. */
    }

2980
    event = virDomainEventLifecycleNewFromObj(privdom,
2981 2982
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
C
Cole Robinson 已提交
2983 2984
    privdom->hasManagedSave = false;

2985
    if (virDomainObjIsActive(privdom))
2986
        privdom->persistent = 0;
2987 2988
    else
        virDomainObjListRemove(privconn->domains, privdom);
2989

2990
    ret = 0;
2991

2992
 cleanup:
2993
    virDomainObjEndAPI(&privdom);
2994
    testObjectEventQueue(privconn, event);
2995
    return ret;
2996 2997
}

2998 2999 3000 3001 3002
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

3003 3004 3005
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
3006 3007
    virDomainObjPtr privdom;

3008 3009
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3010

3011
    *autostart = privdom->autostart;
3012

3013
    virDomainObjEndAPI(&privdom);
3014
    return 0;
3015 3016 3017 3018 3019 3020
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
3021 3022
    virDomainObjPtr privdom;

3023 3024
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3025

3026
    privdom->autostart = autostart ? 1 : 0;
3027

3028
    virDomainObjEndAPI(&privdom);
3029
    return 0;
3030
}
3031

3032
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
3033 3034
                                        int *nparams)
{
3035 3036
    char *type = NULL;

3037 3038 3039
    if (nparams)
        *nparams = 1;

3040
    ignore_value(VIR_STRDUP(type, "fair"));
3041

3042 3043 3044
    return type;
}

3045
static int
3046 3047 3048 3049
testDomainGetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int *nparams,
                                      unsigned int flags)
3050
{
3051
    virDomainObjPtr privdom;
3052
    int ret = -1;
3053

3054 3055
    virCheckFlags(0, -1);

3056 3057
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3058

3059 3060
    if (virTypedParameterAssign(params, VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, 50) < 0)
3061
        goto cleanup;
3062 3063
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
3064 3065

    *nparams = 1;
3066 3067
    ret = 0;

3068
 cleanup:
3069
    virDomainObjEndAPI(&privdom);
3070
    return ret;
3071
}
3072

3073
static int
3074 3075 3076
testDomainGetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int *nparams)
3077
{
3078
    return testDomainGetSchedulerParametersFlags(domain, params, nparams, 0);
3079
}
3080

3081
static int
3082 3083 3084 3085
testDomainSetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int nparams,
                                      unsigned int flags)
3086
{
3087
    virDomainObjPtr privdom;
3088 3089
    int ret = -1;
    size_t i;
3090

3091
    virCheckFlags(0, -1);
3092 3093 3094 3095
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_SCHEDULER_WEIGHT,
                               VIR_TYPED_PARAM_UINT,
                               NULL) < 0)
3096
        return -1;
3097

3098 3099
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3100

3101
    for (i = 0; i < nparams; i++) {
3102 3103 3104
        if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_WEIGHT)) {
            /* XXX */
            /*privdom->weight = params[i].value.ui;*/
3105
        }
3106
    }
3107

3108 3109
    ret = 0;

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

3114
static int
3115 3116 3117
testDomainSetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int nparams)
3118
{
3119
    return testDomainSetSchedulerParametersFlags(domain, params, nparams, 0);
3120 3121
}

3122 3123
static int testDomainBlockStats(virDomainPtr domain,
                                const char *path,
3124
                                virDomainBlockStatsPtr stats)
3125 3126 3127 3128
{
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
3129
    int ret = -1;
3130

3131 3132 3133 3134 3135 3136
    if (!*path) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("summary statistics are not supported yet"));
        return ret;
    }

3137 3138
    if (!(privdom = testDomObjFromDomain(domain)))
        return ret;
3139

3140 3141 3142 3143 3144 3145
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto error;
    }

3146
    if (virDomainDiskIndexByName(privdom->def, path, false) < 0) {
3147 3148
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path: %s"), path);
3149 3150 3151 3152
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
3153
        virReportSystemError(errno,
3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
                             "%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;
3167
 error:
3168
    virDomainObjEndAPI(&privdom);
3169 3170 3171 3172 3173
    return ret;
}

static int testDomainInterfaceStats(virDomainPtr domain,
                                    const char *path,
3174
                                    virDomainInterfaceStatsPtr stats)
3175 3176 3177 3178
{
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
3179 3180
    size_t i;
    int found = 0, ret = -1;
3181

3182 3183
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3184

3185 3186 3187 3188 3189 3190
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto error;
    }

3191
    for (i = 0; i < privdom->def->nnets; i++) {
3192
        if (privdom->def->nets[i]->ifname &&
3193
            STREQ(privdom->def->nets[i]->ifname, path)) {
3194 3195 3196 3197 3198 3199
            found = 1;
            break;
        }
    }

    if (!found) {
3200 3201
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path, '%s' is not a known interface"), path);
3202 3203 3204 3205
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
3206
        virReportSystemError(errno,
3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
                             "%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;
3223
 error:
3224
    virDomainObjEndAPI(&privdom);
3225 3226 3227
    return ret;
}

3228

3229 3230 3231 3232
static virNetworkObjPtr
testNetworkObjFindByUUID(testDriverPtr privconn,
                         const unsigned char *uuid)
{
3233
    virNetworkObjPtr obj;
3234 3235
    char uuidstr[VIR_UUID_STRING_BUFLEN];

3236
    if (!(obj = virNetworkObjFindByUUID(privconn->networks, uuid))) {
3237 3238 3239 3240 3241 3242
        virUUIDFormat(uuid, uuidstr);
        virReportError(VIR_ERR_NO_NETWORK,
                       _("no network with matching uuid '%s'"),
                       uuidstr);
    }

3243
    return obj;
3244 3245 3246
}


3247 3248 3249
static virNetworkPtr
testNetworkLookupByUUID(virConnectPtr conn,
                        const unsigned char *uuid)
3250
{
3251
    testDriverPtr privconn = conn->privateData;
3252
    virNetworkObjPtr obj;
3253
    virNetworkDefPtr def;
3254
    virNetworkPtr net = NULL;
3255

3256
    if (!(obj = testNetworkObjFindByUUID(privconn, uuid)))
3257
        goto cleanup;
3258
    def = virNetworkObjGetDef(obj);
3259

3260
    net = virGetNetwork(conn, def->name, def->uuid);
3261

3262
 cleanup:
3263 3264
    virNetworkObjEndAPI(&obj);
    return net;
3265
}
3266

3267 3268 3269 3270 3271

static virNetworkObjPtr
testNetworkObjFindByName(testDriverPtr privconn,
                         const char *name)
{
3272
    virNetworkObjPtr obj;
3273

3274
    if (!(obj = virNetworkObjFindByName(privconn->networks, name)))
3275 3276 3277 3278
        virReportError(VIR_ERR_NO_NETWORK,
                       _("no network with matching name '%s'"),
                       name);

3279
    return obj;
3280 3281 3282
}


3283 3284 3285
static virNetworkPtr
testNetworkLookupByName(virConnectPtr conn,
                        const char *name)
3286
{
3287
    testDriverPtr privconn = conn->privateData;
3288
    virNetworkObjPtr obj;
3289
    virNetworkDefPtr def;
3290
    virNetworkPtr net = NULL;
3291

3292
    if (!(obj = testNetworkObjFindByName(privconn, name)))
3293
        goto cleanup;
3294
    def = virNetworkObjGetDef(obj);
3295

3296
    net = virGetNetwork(conn, def->name, def->uuid);
3297

3298
 cleanup:
3299 3300
    virNetworkObjEndAPI(&obj);
    return net;
3301 3302 3303
}


3304 3305
static int
testConnectNumOfNetworks(virConnectPtr conn)
3306
{
3307
    testDriverPtr privconn = conn->privateData;
3308
    int numActive;
3309

3310 3311
    numActive = virNetworkObjListNumOfNetworks(privconn->networks,
                                               true, NULL, conn);
3312
    return numActive;
3313 3314
}

3315 3316 3317 3318

static int
testConnectListNetworks(virConnectPtr conn,
                        char **const names,
3319
                        int maxnames)
3320
{
3321
    testDriverPtr privconn = conn->privateData;
3322
    int n;
3323

3324
    n = virNetworkObjListGetNames(privconn->networks,
3325
                                  true, names, maxnames, NULL, conn);
3326
    return n;
3327 3328
}

3329 3330 3331

static int
testConnectNumOfDefinedNetworks(virConnectPtr conn)
3332
{
3333
    testDriverPtr privconn = conn->privateData;
3334
    int numInactive;
3335

3336 3337
    numInactive = virNetworkObjListNumOfNetworks(privconn->networks,
                                                 false, NULL, conn);
3338
    return numInactive;
3339 3340
}

3341 3342 3343 3344

static int
testConnectListDefinedNetworks(virConnectPtr conn,
                               char **const names,
3345
                               int maxnames)
3346
{
3347
    testDriverPtr privconn = conn->privateData;
3348
    int n;
3349

3350
    n = virNetworkObjListGetNames(privconn->networks,
3351
                                  false, names, maxnames, NULL, conn);
3352
    return n;
3353 3354
}

3355

3356
static int
3357
testConnectListAllNetworks(virConnectPtr conn,
3358 3359 3360
                           virNetworkPtr **nets,
                           unsigned int flags)
{
3361
    testDriverPtr privconn = conn->privateData;
3362 3363 3364

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

3365
    return virNetworkObjListExport(conn, privconn->networks, nets, NULL, flags);
3366
}
3367

3368 3369 3370

static int
testNetworkIsActive(virNetworkPtr net)
3371
{
3372
    testDriverPtr privconn = net->conn->privateData;
3373 3374 3375
    virNetworkObjPtr obj;
    int ret = -1;

3376
    if (!(obj = testNetworkObjFindByUUID(privconn, net->uuid)))
3377
        goto cleanup;
3378

3379 3380
    ret = virNetworkObjIsActive(obj);

3381
 cleanup:
3382
    virNetworkObjEndAPI(&obj);
3383 3384 3385
    return ret;
}

3386 3387 3388

static int
testNetworkIsPersistent(virNetworkPtr net)
3389
{
3390
    testDriverPtr privconn = net->conn->privateData;
3391 3392 3393
    virNetworkObjPtr obj;
    int ret = -1;

3394
    if (!(obj = testNetworkObjFindByUUID(privconn, net->uuid)))
3395
        goto cleanup;
3396

3397
    ret = virNetworkObjIsPersistent(obj);
3398

3399
 cleanup:
3400
    virNetworkObjEndAPI(&obj);
3401 3402 3403 3404
    return ret;
}


3405 3406
static virNetworkPtr
testNetworkCreateXML(virConnectPtr conn, const char *xml)
3407
{
3408
    testDriverPtr privconn = conn->privateData;
3409
    virNetworkDefPtr newDef;
3410
    virNetworkObjPtr obj = NULL;
3411
    virNetworkDefPtr def;
3412
    virNetworkPtr net = NULL;
3413
    virObjectEventPtr event = NULL;
3414

3415
    if ((newDef = virNetworkDefParseString(xml)) == NULL)
3416
        goto cleanup;
3417

3418
    if (!(obj = virNetworkObjAssignDef(privconn->networks, newDef,
3419 3420
                                       VIR_NETWORK_OBJ_LIST_ADD_LIVE |
                                       VIR_NETWORK_OBJ_LIST_ADD_CHECK_LIVE)))
3421
        goto cleanup;
3422 3423
    newDef = NULL;
    def = virNetworkObjGetDef(obj);
3424
    virNetworkObjSetActive(obj, true);
3425

3426
    event = virNetworkEventLifecycleNew(def->name, def->uuid,
3427 3428
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3429

3430
    net = virGetNetwork(conn, def->name, def->uuid);
3431

3432
 cleanup:
3433
    virNetworkDefFree(newDef);
3434
    testObjectEventQueue(privconn, event);
3435 3436
    virNetworkObjEndAPI(&obj);
    return net;
3437 3438
}

3439 3440 3441 3442

static virNetworkPtr
testNetworkDefineXML(virConnectPtr conn,
                     const char *xml)
3443
{
3444
    testDriverPtr privconn = conn->privateData;
3445
    virNetworkDefPtr newDef;
3446
    virNetworkObjPtr obj = NULL;
3447
    virNetworkDefPtr def;
3448
    virNetworkPtr net = NULL;
3449
    virObjectEventPtr event = NULL;
3450

3451
    if ((newDef = virNetworkDefParseString(xml)) == NULL)
3452
        goto cleanup;
3453

3454
    if (!(obj = virNetworkObjAssignDef(privconn->networks, newDef, 0)))
3455
        goto cleanup;
3456 3457
    newDef = NULL;
    def = virNetworkObjGetDef(obj);
3458

3459
    event = virNetworkEventLifecycleNew(def->name, def->uuid,
3460 3461
                                        VIR_NETWORK_EVENT_DEFINED,
                                        0);
3462

3463
    net = virGetNetwork(conn, def->name, def->uuid);
3464

3465
 cleanup:
3466
    virNetworkDefFree(newDef);
3467
    testObjectEventQueue(privconn, event);
3468 3469
    virNetworkObjEndAPI(&obj);
    return net;
3470 3471
}

3472 3473

static int
3474
testNetworkUndefine(virNetworkPtr net)
3475
{
3476 3477
    testDriverPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
3478
    int ret = -1;
3479
    virObjectEventPtr event = NULL;
3480

3481
    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
3482
        goto cleanup;
3483

3484
    if (virNetworkObjIsActive(obj)) {
3485
        virReportError(VIR_ERR_OPERATION_INVALID,
3486
                       _("Network '%s' is still running"), net->name);
3487
        goto cleanup;
3488 3489
    }

3490
    event = virNetworkEventLifecycleNew(net->name, net->uuid,
3491 3492
                                        VIR_NETWORK_EVENT_UNDEFINED,
                                        0);
3493

3494
    virNetworkObjRemoveInactive(privconn->networks, obj);
3495
    ret = 0;
3496

3497
 cleanup:
3498
    testObjectEventQueue(privconn, event);
3499
    virNetworkObjEndAPI(&obj);
3500
    return ret;
3501 3502
}

3503

3504 3505 3506 3507 3508 3509 3510 3511
static int
testNetworkUpdate(virNetworkPtr net,
                  unsigned int command,
                  unsigned int section,
                  int parentIndex,
                  const char *xml,
                  unsigned int flags)
{
3512
    testDriverPtr privconn = net->conn->privateData;
3513
    virNetworkObjPtr obj = NULL;
3514 3515 3516 3517 3518 3519
    int isActive, ret = -1;

    virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE |
                  VIR_NETWORK_UPDATE_AFFECT_CONFIG,
                  -1);

3520
    if (!(obj = testNetworkObjFindByUUID(privconn, net->uuid)))
3521 3522 3523 3524 3525
        goto cleanup;

    /* VIR_NETWORK_UPDATE_AFFECT_CURRENT means "change LIVE if network
     * is active, else change CONFIG
    */
3526
    isActive = virNetworkObjIsActive(obj);
3527 3528 3529 3530 3531 3532 3533 3534 3535 3536
    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 */
3537
    if (virNetworkObjUpdate(obj, command, section, parentIndex, xml, flags) < 0)
3538 3539 3540
       goto cleanup;

    ret = 0;
3541
 cleanup:
3542
    virNetworkObjEndAPI(&obj);
3543 3544 3545
    return ret;
}

3546 3547

static int
3548
testNetworkCreate(virNetworkPtr net)
3549
{
3550 3551
    testDriverPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
3552
    virNetworkDefPtr def;
3553
    int ret = -1;
3554
    virObjectEventPtr event = NULL;
3555

3556
    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
3557
        goto cleanup;
3558
    def = virNetworkObjGetDef(obj);
3559

3560
    if (virNetworkObjIsActive(obj)) {
3561
        virReportError(VIR_ERR_OPERATION_INVALID,
3562
                       _("Network '%s' is already running"), net->name);
3563
        goto cleanup;
3564 3565
    }

3566
    virNetworkObjSetActive(obj, true);
3567
    event = virNetworkEventLifecycleNew(def->name, def->uuid,
3568 3569
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3570
    ret = 0;
3571

3572
 cleanup:
3573
    testObjectEventQueue(privconn, event);
3574
    virNetworkObjEndAPI(&obj);
3575
    return ret;
3576 3577
}

3578 3579

static int
3580
testNetworkDestroy(virNetworkPtr net)
3581
{
3582 3583
    testDriverPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
3584
    virNetworkDefPtr def;
3585
    int ret = -1;
3586
    virObjectEventPtr event = NULL;
3587

3588
    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
3589
        goto cleanup;
3590
    def = virNetworkObjGetDef(obj);
3591

3592
    virNetworkObjSetActive(obj, false);
3593
    event = virNetworkEventLifecycleNew(def->name, def->uuid,
3594 3595
                                        VIR_NETWORK_EVENT_STOPPED,
                                        0);
3596
    if (!virNetworkObjIsPersistent(obj))
3597
        virNetworkObjRemoveInactive(privconn->networks, obj);
3598

3599 3600
    ret = 0;

3601
 cleanup:
3602
    testObjectEventQueue(privconn, event);
3603
    virNetworkObjEndAPI(&obj);
3604
    return ret;
3605 3606
}

3607 3608

static char *
3609
testNetworkGetXMLDesc(virNetworkPtr net,
3610
                      unsigned int flags)
3611
{
3612 3613
    testDriverPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
3614
    char *ret = NULL;
3615

E
Eric Blake 已提交
3616 3617
    virCheckFlags(0, NULL);

3618
    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
3619
        goto cleanup;
3620

3621
    ret = virNetworkDefFormat(virNetworkObjGetDef(obj), flags);
3622

3623
 cleanup:
3624
    virNetworkObjEndAPI(&obj);
3625
    return ret;
3626 3627
}

3628 3629

static char *
3630
testNetworkGetBridgeName(virNetworkPtr net)
3631
{
3632
    testDriverPtr privconn = net->conn->privateData;
3633
    char *bridge = NULL;
3634
    virNetworkObjPtr obj;
3635
    virNetworkDefPtr def;
3636

3637
    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
3638
        goto cleanup;
3639
    def = virNetworkObjGetDef(obj);
3640

3641
    if (!(def->bridge)) {
3642 3643
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("network '%s' does not have a bridge name."),
3644
                       def->name);
3645 3646 3647
        goto cleanup;
    }

3648
    ignore_value(VIR_STRDUP(bridge, def->bridge));
3649

3650
 cleanup:
3651
    virNetworkObjEndAPI(&obj);
3652 3653 3654
    return bridge;
}

3655 3656

static int
3657
testNetworkGetAutostart(virNetworkPtr net,
3658
                        int *autostart)
3659
{
3660 3661
    testDriverPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
3662
    int ret = -1;
3663

3664
    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
3665
        goto cleanup;
3666

3667
    *autostart = virNetworkObjIsAutostart(obj) ? 1 : 0;
3668 3669
    ret = 0;

3670
 cleanup:
3671
    virNetworkObjEndAPI(&obj);
3672
    return ret;
3673 3674
}

3675 3676

static int
3677
testNetworkSetAutostart(virNetworkPtr net,
3678
                        int autostart)
3679
{
3680 3681
    testDriverPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
3682
    bool new_autostart = (autostart != 0);
3683
    int ret = -1;
3684

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

3688 3689
    virNetworkObjSetAutostart(obj, new_autostart);

3690 3691
    ret = 0;

3692
 cleanup:
3693
    virNetworkObjEndAPI(&obj);
3694
    return ret;
3695
}
3696

C
Cole Robinson 已提交
3697

L
Laine Stump 已提交
3698 3699 3700 3701 3702
/*
 * Physical host interface routines
 */


3703 3704 3705 3706
static virInterfaceObjPtr
testInterfaceObjFindByName(testDriverPtr privconn,
                           const char *name)
{
3707
    virInterfaceObjPtr obj;
3708 3709

    testDriverLock(privconn);
3710
    obj = virInterfaceObjListFindByName(privconn->ifaces, name);
3711 3712
    testDriverUnlock(privconn);

3713
    if (!obj)
3714 3715 3716 3717
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("no interface with matching name '%s'"),
                       name);

3718
    return obj;
3719 3720 3721
}


3722 3723
static int
testConnectNumOfInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3724
{
3725
    testDriverPtr privconn = conn->privateData;
3726
    int ninterfaces;
L
Laine Stump 已提交
3727 3728

    testDriverLock(privconn);
3729
    ninterfaces = virInterfaceObjListNumOfInterfaces(privconn->ifaces, true);
L
Laine Stump 已提交
3730
    testDriverUnlock(privconn);
3731
    return ninterfaces;
L
Laine Stump 已提交
3732 3733
}

3734 3735 3736 3737 3738

static int
testConnectListInterfaces(virConnectPtr conn,
                          char **const names,
                          int maxnames)
L
Laine Stump 已提交
3739
{
3740
    testDriverPtr privconn = conn->privateData;
3741
    int nnames;
L
Laine Stump 已提交
3742 3743

    testDriverLock(privconn);
3744 3745
    nnames = virInterfaceObjListGetNames(privconn->ifaces, true,
                                         names, maxnames);
L
Laine Stump 已提交
3746 3747
    testDriverUnlock(privconn);

3748
    return nnames;
L
Laine Stump 已提交
3749 3750
}

3751 3752 3753

static int
testConnectNumOfDefinedInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3754
{
3755
    testDriverPtr privconn = conn->privateData;
3756
    int ninterfaces;
L
Laine Stump 已提交
3757 3758

    testDriverLock(privconn);
3759
    ninterfaces = virInterfaceObjListNumOfInterfaces(privconn->ifaces, false);
L
Laine Stump 已提交
3760
    testDriverUnlock(privconn);
3761
    return ninterfaces;
L
Laine Stump 已提交
3762 3763
}

3764 3765 3766 3767 3768

static int
testConnectListDefinedInterfaces(virConnectPtr conn,
                                 char **const names,
                                 int maxnames)
L
Laine Stump 已提交
3769
{
3770
    testDriverPtr privconn = conn->privateData;
3771
    int nnames;
L
Laine Stump 已提交
3772 3773

    testDriverLock(privconn);
3774 3775
    nnames = virInterfaceObjListGetNames(privconn->ifaces, false,
                                         names, maxnames);
L
Laine Stump 已提交
3776 3777
    testDriverUnlock(privconn);

3778
    return nnames;
L
Laine Stump 已提交
3779 3780
}

3781 3782 3783 3784

static virInterfacePtr
testInterfaceLookupByName(virConnectPtr conn,
                          const char *name)
L
Laine Stump 已提交
3785
{
3786
    testDriverPtr privconn = conn->privateData;
3787
    virInterfaceObjPtr obj;
3788
    virInterfaceDefPtr def;
L
Laine Stump 已提交
3789 3790
    virInterfacePtr ret = NULL;

3791
    if (!(obj = testInterfaceObjFindByName(privconn, name)))
3792
        return NULL;
3793
    def = virInterfaceObjGetDef(obj);
L
Laine Stump 已提交
3794

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

3797
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
3798 3799 3800
    return ret;
}

3801 3802 3803 3804

static virInterfacePtr
testInterfaceLookupByMACString(virConnectPtr conn,
                               const char *mac)
L
Laine Stump 已提交
3805
{
3806
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3807
    int ifacect;
3808
    char *ifacenames[] = { NULL, NULL };
L
Laine Stump 已提交
3809 3810 3811
    virInterfacePtr ret = NULL;

    testDriverLock(privconn);
3812 3813
    ifacect = virInterfaceObjListFindByMACString(privconn->ifaces, mac,
                                                 ifacenames, 2);
L
Laine Stump 已提交
3814 3815 3816
    testDriverUnlock(privconn);

    if (ifacect == 0) {
3817 3818
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("no interface with matching mac '%s'"), mac);
L
Laine Stump 已提交
3819 3820 3821 3822
        goto cleanup;
    }

    if (ifacect > 1) {
3823
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3824 3825 3826
        goto cleanup;
    }

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

3829
 cleanup:
3830 3831
    VIR_FREE(ifacenames[0]);
    VIR_FREE(ifacenames[1]);
L
Laine Stump 已提交
3832 3833 3834
    return ret;
}

3835 3836 3837

static int
testInterfaceIsActive(virInterfacePtr iface)
3838
{
3839
    testDriverPtr privconn = iface->conn->privateData;
3840 3841 3842
    virInterfaceObjPtr obj;
    int ret = -1;

3843
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3844
        return -1;
3845

3846 3847
    ret = virInterfaceObjIsActive(obj);

3848
    virInterfaceObjEndAPI(&obj);
3849 3850 3851
    return ret;
}

3852 3853 3854 3855

static int
testInterfaceChangeBegin(virConnectPtr conn,
                         unsigned int flags)
3856
{
3857
    testDriverPtr privconn = conn->privateData;
3858 3859
    int ret = -1;

E
Eric Blake 已提交
3860 3861
    virCheckFlags(0, -1);

3862 3863
    testDriverLock(privconn);
    if (privconn->transaction_running) {
3864
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3865
                       _("there is another transaction running."));
3866 3867 3868 3869 3870
        goto cleanup;
    }

    privconn->transaction_running = true;

3871
    if (!(privconn->backupIfaces = virInterfaceObjListClone(privconn->ifaces)))
3872 3873 3874
        goto cleanup;

    ret = 0;
3875
 cleanup:
3876 3877 3878 3879
    testDriverUnlock(privconn);
    return ret;
}

3880 3881 3882 3883

static int
testInterfaceChangeCommit(virConnectPtr conn,
                          unsigned int flags)
3884
{
3885
    testDriverPtr privconn = conn->privateData;
3886 3887
    int ret = -1;

E
Eric Blake 已提交
3888 3889
    virCheckFlags(0, -1);

3890 3891 3892
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3893
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3894 3895
                       _("no transaction running, "
                         "nothing to be committed."));
3896 3897 3898
        goto cleanup;
    }

3899
    virInterfaceObjListFree(privconn->backupIfaces);
3900 3901 3902 3903
    privconn->transaction_running = false;

    ret = 0;

3904
 cleanup:
3905 3906 3907 3908 3909
    testDriverUnlock(privconn);

    return ret;
}

3910 3911 3912 3913

static int
testInterfaceChangeRollback(virConnectPtr conn,
                            unsigned int flags)
3914
{
3915
    testDriverPtr privconn = conn->privateData;
3916 3917
    int ret = -1;

E
Eric Blake 已提交
3918 3919
    virCheckFlags(0, -1);

3920 3921 3922
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3923
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3924 3925
                       _("no transaction running, "
                         "nothing to rollback."));
3926 3927 3928
        goto cleanup;
    }

3929 3930 3931
    virInterfaceObjListFree(privconn->ifaces);
    privconn->ifaces = privconn->backupIfaces;
    privconn->backupIfaces = NULL;
3932 3933 3934 3935 3936

    privconn->transaction_running = false;

    ret = 0;

3937
 cleanup:
3938 3939 3940
    testDriverUnlock(privconn);
    return ret;
}
3941

3942 3943 3944 3945

static char *
testInterfaceGetXMLDesc(virInterfacePtr iface,
                        unsigned int flags)
L
Laine Stump 已提交
3946
{
3947
    testDriverPtr privconn = iface->conn->privateData;
3948
    virInterfaceObjPtr obj;
3949
    virInterfaceDefPtr def;
L
Laine Stump 已提交
3950 3951
    char *ret = NULL;

E
Eric Blake 已提交
3952 3953
    virCheckFlags(0, NULL);

3954
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3955
        return NULL;
3956
    def = virInterfaceObjGetDef(obj);
L
Laine Stump 已提交
3957

3958
    ret = virInterfaceDefFormat(def);
L
Laine Stump 已提交
3959

3960
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
3961 3962 3963 3964
    return ret;
}


3965 3966 3967 3968
static virInterfacePtr
testInterfaceDefineXML(virConnectPtr conn,
                       const char *xmlStr,
                       unsigned int flags)
L
Laine Stump 已提交
3969
{
3970
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3971
    virInterfaceDefPtr def;
3972
    virInterfaceObjPtr obj = NULL;
3973
    virInterfaceDefPtr objdef;
L
Laine Stump 已提交
3974 3975
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
3976 3977
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3978
    testDriverLock(privconn);
3979
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3980 3981
        goto cleanup;

3982
    if ((obj = virInterfaceObjListAssignDef(privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3983 3984
        goto cleanup;
    def = NULL;
3985
    objdef = virInterfaceObjGetDef(obj);
L
Laine Stump 已提交
3986

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

3989
 cleanup:
L
Laine Stump 已提交
3990
    virInterfaceDefFree(def);
3991
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
3992 3993 3994 3995
    testDriverUnlock(privconn);
    return ret;
}

3996 3997 3998

static int
testInterfaceUndefine(virInterfacePtr iface)
L
Laine Stump 已提交
3999
{
4000
    testDriverPtr privconn = iface->conn->privateData;
4001
    virInterfaceObjPtr obj;
L
Laine Stump 已提交
4002

4003
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
4004
        return -1;
L
Laine Stump 已提交
4005

4006
    virInterfaceObjListRemove(privconn->ifaces, obj);
4007
    virObjectUnref(obj);
L
Laine Stump 已提交
4008

4009
    return 0;
L
Laine Stump 已提交
4010 4011
}

4012 4013 4014 4015

static int
testInterfaceCreate(virInterfacePtr iface,
                    unsigned int flags)
L
Laine Stump 已提交
4016
{
4017
    testDriverPtr privconn = iface->conn->privateData;
4018
    virInterfaceObjPtr obj;
L
Laine Stump 已提交
4019 4020
    int ret = -1;

E
Eric Blake 已提交
4021 4022
    virCheckFlags(0, -1);

4023
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
4024
        return -1;
L
Laine Stump 已提交
4025

4026
    if (virInterfaceObjIsActive(obj)) {
4027
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4028 4029 4030
        goto cleanup;
    }

4031
    virInterfaceObjSetActive(obj, true);
L
Laine Stump 已提交
4032 4033
    ret = 0;

4034
 cleanup:
4035
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
4036 4037 4038
    return ret;
}

4039 4040 4041 4042

static int
testInterfaceDestroy(virInterfacePtr iface,
                     unsigned int flags)
L
Laine Stump 已提交
4043
{
4044
    testDriverPtr privconn = iface->conn->privateData;
4045
    virInterfaceObjPtr obj;
L
Laine Stump 已提交
4046 4047
    int ret = -1;

E
Eric Blake 已提交
4048 4049
    virCheckFlags(0, -1);

4050
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
4051
        return -1;
L
Laine Stump 已提交
4052

4053
    if (!virInterfaceObjIsActive(obj)) {
4054
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4055 4056 4057
        goto cleanup;
    }

4058
    virInterfaceObjSetActive(obj, false);
L
Laine Stump 已提交
4059 4060
    ret = 0;

4061
 cleanup:
4062
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
4063 4064 4065 4066 4067
    return ret;
}



C
Cole Robinson 已提交
4068 4069 4070 4071
/*
 * Storage Driver routines
 */

4072
static int
4073
testStoragePoolObjSetDefaults(virStoragePoolObjPtr obj)
4074
{
4075
    char *configFile;
C
Cole Robinson 已提交
4076

4077 4078 4079
    obj->def->capacity = defaultPoolCap;
    obj->def->allocation = defaultPoolAlloc;
    obj->def->available = defaultPoolCap - defaultPoolAlloc;
C
Cole Robinson 已提交
4080

4081 4082 4083 4084 4085
    if (VIR_STRDUP(configFile, "") < 0)
        return -1;

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

4088

4089 4090 4091 4092
static virStoragePoolObjPtr
testStoragePoolObjFindByName(testDriverPtr privconn,
                             const char *name)
{
4093
    virStoragePoolObjPtr obj;
4094 4095

    testDriverLock(privconn);
4096
    obj = virStoragePoolObjFindByName(&privconn->pools, name);
4097 4098
    testDriverUnlock(privconn);

4099
    if (!obj)
4100 4101 4102 4103
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       _("no storage pool with matching name '%s'"),
                       name);

4104
    return obj;
4105 4106 4107
}


4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147
static virStoragePoolObjPtr
testStoragePoolObjFindActiveByName(testDriverPtr privconn,
                                   const char *name)
{
    virStoragePoolObjPtr obj;

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

    if (!virStoragePoolObjIsActive(obj)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), name);
        virStoragePoolObjUnlock(obj);
        return NULL;
    }

    return obj;
}


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

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

    if (virStoragePoolObjIsActive(obj)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is active"), name);
        virStoragePoolObjUnlock(obj);
        return NULL;
    }

    return obj;
}


4148 4149 4150 4151
static virStoragePoolObjPtr
testStoragePoolObjFindByUUID(testDriverPtr privconn,
                             const unsigned char *uuid)
{
4152
    virStoragePoolObjPtr obj;
4153 4154 4155
    char uuidstr[VIR_UUID_STRING_BUFLEN];

    testDriverLock(privconn);
4156
    obj = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
4157 4158
    testDriverUnlock(privconn);

4159
    if (!obj) {
4160 4161 4162 4163 4164 4165
        virUUIDFormat(uuid, uuidstr);
        virReportError(VIR_ERR_NO_STORAGE_POOL,
                       _("no storage pool with matching uuid '%s'"),
                       uuidstr);
    }

4166
    return obj;
4167 4168 4169
}


C
Cole Robinson 已提交
4170 4171
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
4172 4173
                            const unsigned char *uuid)
{
4174
    testDriverPtr privconn = conn->privateData;
4175 4176
    virStoragePoolObjPtr obj;
    virStoragePoolPtr pool = NULL;
C
Cole Robinson 已提交
4177

4178
    if (!(obj = testStoragePoolObjFindByUUID(privconn, uuid)))
4179
        return NULL;
C
Cole Robinson 已提交
4180

4181 4182
    pool = virGetStoragePool(conn, obj->def->name, obj->def->uuid,
                             NULL, NULL);
4183

4184
    virStoragePoolObjUnlock(obj);
4185
    return pool;
C
Cole Robinson 已提交
4186 4187
}

4188

C
Cole Robinson 已提交
4189 4190
static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
4191 4192
                            const char *name)
{
4193
    testDriverPtr privconn = conn->privateData;
4194 4195
    virStoragePoolObjPtr obj;
    virStoragePoolPtr pool = NULL;
C
Cole Robinson 已提交
4196

4197
    if (!(obj = testStoragePoolObjFindByName(privconn, name)))
4198
        return NULL;
C
Cole Robinson 已提交
4199

4200 4201
    pool = virGetStoragePool(conn, obj->def->name, obj->def->uuid,
                             NULL, NULL);
4202

4203
    virStoragePoolObjUnlock(obj);
4204
    return pool;
C
Cole Robinson 已提交
4205 4206
}

4207

C
Cole Robinson 已提交
4208
static virStoragePoolPtr
4209 4210
testStoragePoolLookupByVolume(virStorageVolPtr vol)
{
C
Cole Robinson 已提交
4211 4212 4213
    return testStoragePoolLookupByName(vol->conn, vol->pool);
}

4214

C
Cole Robinson 已提交
4215
static int
4216 4217
testConnectNumOfStoragePools(virConnectPtr conn)
{
4218
    testDriverPtr privconn = conn->privateData;
4219
    int numActive = 0;
C
Cole Robinson 已提交
4220

4221
    testDriverLock(privconn);
4222 4223
    numActive = virStoragePoolObjNumOfStoragePools(&privconn->pools, conn,
                                                   true, NULL);
4224
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4225 4226 4227 4228

    return numActive;
}

4229

C
Cole Robinson 已提交
4230
static int
4231 4232
testConnectListStoragePools(virConnectPtr conn,
                            char **const names,
4233
                            int maxnames)
4234
{
4235
    testDriverPtr privconn = conn->privateData;
4236
    int n = 0;
C
Cole Robinson 已提交
4237

4238
    testDriverLock(privconn);
4239 4240
    n = virStoragePoolObjGetNames(&privconn->pools, conn, true, NULL,
                                  names, maxnames);
4241
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4242 4243 4244 4245

    return n;
}

4246

C
Cole Robinson 已提交
4247
static int
4248 4249
testConnectNumOfDefinedStoragePools(virConnectPtr conn)
{
4250
    testDriverPtr privconn = conn->privateData;
4251
    int numInactive = 0;
C
Cole Robinson 已提交
4252

4253
    testDriverLock(privconn);
4254 4255
    numInactive = virStoragePoolObjNumOfStoragePools(&privconn->pools, conn,
                                                     false, NULL);
4256
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4257 4258 4259 4260

    return numInactive;
}

4261

C
Cole Robinson 已提交
4262
static int
4263 4264
testConnectListDefinedStoragePools(virConnectPtr conn,
                                   char **const names,
4265
                                   int maxnames)
4266
{
4267
    testDriverPtr privconn = conn->privateData;
4268
    int n = 0;
C
Cole Robinson 已提交
4269

4270
    testDriverLock(privconn);
4271 4272
    n = virStoragePoolObjGetNames(&privconn->pools, conn, false, NULL,
                                  names, maxnames);
4273
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4274 4275 4276 4277

    return n;
}

4278

4279
static int
4280 4281 4282
testConnectListAllStoragePools(virConnectPtr conn,
                               virStoragePoolPtr **pools,
                               unsigned int flags)
4283
{
4284
    testDriverPtr privconn = conn->privateData;
4285 4286 4287 4288 4289
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

    testDriverLock(privconn);
J
John Ferlan 已提交
4290
    ret = virStoragePoolObjListExport(conn, &privconn->pools, pools,
4291
                                      NULL, flags);
4292 4293 4294 4295
    testDriverUnlock(privconn);

    return ret;
}
C
Cole Robinson 已提交
4296

4297 4298 4299

static int
testStoragePoolIsActive(virStoragePoolPtr pool)
4300
{
4301
    testDriverPtr privconn = pool->conn->privateData;
4302 4303 4304
    virStoragePoolObjPtr obj;
    int ret = -1;

4305
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4306
        goto cleanup;
4307

4308 4309
    ret = virStoragePoolObjIsActive(obj);

4310
 cleanup:
4311 4312 4313 4314 4315
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}

4316 4317 4318

static int
testStoragePoolIsPersistent(virStoragePoolPtr pool)
4319
{
4320
    testDriverPtr privconn = pool->conn->privateData;
4321 4322 4323
    virStoragePoolObjPtr obj;
    int ret = -1;

4324
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4325
        return -1;
4326

4327
    ret = virStoragePoolObjGetConfigFile(obj) ? 1 : 0;
4328

4329
    virStoragePoolObjUnlock(obj);
4330 4331 4332 4333
    return ret;
}


C
Cole Robinson 已提交
4334
static int
4335 4336
testStoragePoolCreate(virStoragePoolPtr pool,
                      unsigned int flags)
E
Eric Blake 已提交
4337
{
4338
    testDriverPtr privconn = pool->conn->privateData;
4339
    virStoragePoolObjPtr obj;
4340
    virObjectEventPtr event = NULL;
4341

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

4344 4345
    if (!(obj = testStoragePoolObjFindInactiveByName(privconn, pool->name)))
        return -1;
C
Cole Robinson 已提交
4346

4347
    virStoragePoolObjSetActive(obj, true);
4348 4349 4350 4351

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

4353
    testObjectEventQueue(privconn, event);
4354 4355
    virStoragePoolObjUnlock(obj);
    return 0;
C
Cole Robinson 已提交
4356 4357
}

4358

C
Cole Robinson 已提交
4359
static char *
4360 4361 4362 4363
testConnectFindStoragePoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type,
                                  const char *srcSpec,
                                  unsigned int flags)
C
Cole Robinson 已提交
4364
{
4365 4366 4367 4368
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4369 4370
    virCheckFlags(0, NULL);

4371 4372
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4373 4374
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
4375 4376 4377 4378
        goto cleanup;
    }

    if (srcSpec) {
4379
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4380 4381 4382 4383 4384 4385 4386
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
4387
        ignore_value(VIR_STRDUP(ret, defaultPoolSourcesLogicalXML));
4388 4389 4390
        break;

    case VIR_STORAGE_POOL_NETFS:
4391
        if (!source || !source->hosts[0].name) {
4392 4393
            virReportError(VIR_ERR_INVALID_ARG,
                           "%s", _("hostname must be specified for netfs sources"));
4394 4395 4396
            goto cleanup;
        }

4397 4398
        ignore_value(virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                                 source->hosts[0].name));
4399 4400 4401
        break;

    default:
4402 4403
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4404 4405
    }

4406
 cleanup:
4407 4408
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4409 4410 4411
}


4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433
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;
4434
    virNodeDeviceObjEndAPI(&obj);
4435 4436 4437 4438 4439

    return 0;
}


C
Cole Robinson 已提交
4440
static virStoragePoolPtr
4441 4442 4443
testStoragePoolCreateXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4444
{
4445
    testDriverPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4446
    virStoragePoolDefPtr def;
4447 4448
    virStoragePoolObjPtr obj = NULL;
    virStoragePoolPtr pool = NULL;
4449
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
4450

E
Eric Blake 已提交
4451 4452
    virCheckFlags(0, NULL);

4453
    testDriverLock(privconn);
4454
    if (!(def = virStoragePoolDefParseString(xml)))
4455
        goto cleanup;
C
Cole Robinson 已提交
4456

4457 4458 4459 4460
    obj = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!obj)
        obj = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (obj) {
4461 4462
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("storage pool already exists"));
4463
        goto cleanup;
C
Cole Robinson 已提交
4464 4465
    }

4466
    if (!(obj = virStoragePoolObjAssignDef(&privconn->pools, def)))
4467
        goto cleanup;
4468
    def = NULL;
C
Cole Robinson 已提交
4469

4470
    if (obj->def->source.adapter.type == VIR_STORAGE_ADAPTER_TYPE_FC_HOST) {
4471 4472 4473 4474 4475
        /* 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,
4476 4477 4478 4479
                            obj->def->source.adapter.data.fchost.wwnn,
                            obj->def->source.adapter.data.fchost.wwpn) < 0) {
            virStoragePoolObjRemove(&privconn->pools, obj);
            obj = NULL;
4480 4481 4482 4483
            goto cleanup;
        }
    }

4484 4485 4486
    if (testStoragePoolObjSetDefaults(obj) == -1) {
        virStoragePoolObjRemove(&privconn->pools, obj);
        obj = NULL;
4487
        goto cleanup;
C
Cole Robinson 已提交
4488
    }
4489 4490 4491 4492

    /* *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 */
4493
    virStoragePoolObjSetConfigFile(obj, NULL);
4494

4495
    virStoragePoolObjSetActive(obj, true);
C
Cole Robinson 已提交
4496

4497
    event = virStoragePoolEventLifecycleNew(obj->def->name, obj->def->uuid,
4498 4499 4500
                                            VIR_STORAGE_POOL_EVENT_STARTED,
                                            0);

4501
    pool = virGetStoragePool(conn, obj->def->name, obj->def->uuid, NULL, NULL);
4502

4503
 cleanup:
4504
    virStoragePoolDefFree(def);
4505
    testObjectEventQueue(privconn, event);
4506 4507
    if (obj)
        virStoragePoolObjUnlock(obj);
4508
    testDriverUnlock(privconn);
4509
    return pool;
C
Cole Robinson 已提交
4510 4511
}

4512

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

E
Eric Blake 已提交
4524 4525
    virCheckFlags(0, NULL);

4526
    testDriverLock(privconn);
4527
    if (!(def = virStoragePoolDefParseString(xml)))
4528
        goto cleanup;
C
Cole Robinson 已提交
4529 4530 4531 4532 4533

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

4534
    if (!(obj = virStoragePoolObjAssignDef(&privconn->pools, def)))
4535 4536
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4537

4538
    event = virStoragePoolEventLifecycleNew(obj->def->name, obj->def->uuid,
4539 4540 4541
                                            VIR_STORAGE_POOL_EVENT_DEFINED,
                                            0);

4542 4543 4544
    if (testStoragePoolObjSetDefaults(obj) == -1) {
        virStoragePoolObjRemove(&privconn->pools, obj);
        obj = NULL;
4545
        goto cleanup;
C
Cole Robinson 已提交
4546 4547
    }

4548
    pool = virGetStoragePool(conn, obj->def->name, obj->def->uuid, NULL, NULL);
4549

4550
 cleanup:
4551
    virStoragePoolDefFree(def);
4552
    testObjectEventQueue(privconn, event);
4553 4554
    if (obj)
        virStoragePoolObjUnlock(obj);
4555
    testDriverUnlock(privconn);
4556
    return pool;
C
Cole Robinson 已提交
4557 4558
}

4559

C
Cole Robinson 已提交
4560
static int
4561 4562
testStoragePoolUndefine(virStoragePoolPtr pool)
{
4563
    testDriverPtr privconn = pool->conn->privateData;
4564
    virStoragePoolObjPtr obj;
4565
    virObjectEventPtr event = NULL;
4566

4567 4568
    if (!(obj = testStoragePoolObjFindInactiveByName(privconn, pool->name)))
        return -1;
C
Cole Robinson 已提交
4569

4570 4571 4572 4573
    event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid,
                                            VIR_STORAGE_POOL_EVENT_UNDEFINED,
                                            0);

4574
    virStoragePoolObjRemove(&privconn->pools, obj);
C
Cole Robinson 已提交
4575

4576
    testObjectEventQueue(privconn, event);
4577
    return 0;
C
Cole Robinson 已提交
4578 4579
}

4580

C
Cole Robinson 已提交
4581
static int
4582
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4583 4584
                     unsigned int flags)
{
4585
    testDriverPtr privconn = pool->conn->privateData;
4586
    virStoragePoolObjPtr obj;
4587

E
Eric Blake 已提交
4588 4589
    virCheckFlags(0, -1);

4590 4591
    if (!(obj = testStoragePoolObjFindInactiveByName(privconn, pool->name)))
        return -1;
C
Cole Robinson 已提交
4592

4593 4594
    virStoragePoolObjUnlock(obj);
    return 0;
C
Cole Robinson 已提交
4595 4596 4597
}


4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612
static int
testDestroyVport(testDriverPtr privconn,
                 const char *wwnn ATTRIBUTE_UNUSED,
                 const char *wwpn ATTRIBUTE_UNUSED)
{
    virNodeDeviceObjPtr obj = NULL;
    virObjectEventPtr event = NULL;

    /* NB: Cannot use virVHBAGetHostByWWN (yet) like the storage_backend_scsi
     * deleteVport() helper since that traverses the file system looking for
     * the wwnn/wwpn. So our choice short term is to cheat and use the name
     * (scsi_host12) we know was created.
     *
     * Reaching across the boundaries of space and time into the
     * Node Device in order to remove */
4613 4614
    if (!(obj = virNodeDeviceObjListFindByName(privconn->devs,
                                               "scsi_host12"))) {
4615 4616
        virReportError(VIR_ERR_NO_NODE_DEVICE, "%s",
                       _("no node device with matching name 'scsi_host12'"));
4617
        return -1;
4618 4619 4620 4621 4622 4623
    }

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

4624
    virNodeDeviceObjListRemove(privconn->devs, obj);
4625
    virObjectUnref(obj);
4626 4627

    testObjectEventQueue(privconn, event);
4628
    return 0;
4629 4630 4631
}


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

4640
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name)))
4641
        return -1;
4642

4643
    virStoragePoolObjSetActive(obj, false);
4644

4645
    if (obj->def->source.adapter.type ==
4646
        VIR_STORAGE_ADAPTER_TYPE_FC_HOST) {
4647
        if (testDestroyVport(privconn,
4648 4649
                             obj->def->source.adapter.data.fchost.wwnn,
                             obj->def->source.adapter.data.fchost.wwpn) < 0)
4650 4651 4652
            goto cleanup;
    }

4653 4654
    event = virStoragePoolEventLifecycleNew(obj->def->name,
                                            obj->def->uuid,
4655 4656
                                            VIR_STORAGE_POOL_EVENT_STOPPED,
                                            0);
C
Cole Robinson 已提交
4657

4658
    if (!(virStoragePoolObjGetConfigFile(obj))) {
4659 4660
        virStoragePoolObjRemove(&privconn->pools, obj);
        obj = NULL;
4661
    }
4662
    ret = 0;
C
Cole Robinson 已提交
4663

4664
 cleanup:
4665
    testObjectEventQueue(privconn, event);
4666 4667
    if (obj)
        virStoragePoolObjUnlock(obj);
4668
    return ret;
C
Cole Robinson 已提交
4669 4670 4671 4672
}


static int
4673
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4674 4675
                      unsigned int flags)
{
4676
    testDriverPtr privconn = pool->conn->privateData;
4677
    virStoragePoolObjPtr obj;
4678

E
Eric Blake 已提交
4679 4680
    virCheckFlags(0, -1);

4681 4682
    if (!(obj = testStoragePoolObjFindInactiveByName(privconn, pool->name)))
        return -1;
C
Cole Robinson 已提交
4683

4684 4685
    virStoragePoolObjUnlock(obj);
    return 0;
C
Cole Robinson 已提交
4686 4687 4688 4689
}


static int
4690
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4691 4692
                       unsigned int flags)
{
4693
    testDriverPtr privconn = pool->conn->privateData;
4694
    virStoragePoolObjPtr obj;
4695
    virObjectEventPtr event = NULL;
4696

E
Eric Blake 已提交
4697 4698
    virCheckFlags(0, -1);

4699 4700
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name)))
        return -1;
4701

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

4704
    testObjectEventQueue(privconn, event);
4705 4706
    virStoragePoolObjUnlock(obj);
    return 0;
C
Cole Robinson 已提交
4707 4708 4709 4710
}


static int
4711
testStoragePoolGetInfo(virStoragePoolPtr pool,
4712 4713
                       virStoragePoolInfoPtr info)
{
4714
    testDriverPtr privconn = pool->conn->privateData;
4715
    virStoragePoolObjPtr obj;
4716

4717
    if (!(obj = testStoragePoolObjFindByName(privconn, pool->name)))
4718
        return -1;
C
Cole Robinson 已提交
4719 4720

    memset(info, 0, sizeof(virStoragePoolInfo));
4721
    if (virStoragePoolObjIsActive(obj))
C
Cole Robinson 已提交
4722 4723 4724
        info->state = VIR_STORAGE_POOL_RUNNING;
    else
        info->state = VIR_STORAGE_POOL_INACTIVE;
4725 4726 4727
    info->capacity = obj->def->capacity;
    info->allocation = obj->def->allocation;
    info->available = obj->def->available;
C
Cole Robinson 已提交
4728

4729 4730
    virStoragePoolObjUnlock(obj);
    return 0;
C
Cole Robinson 已提交
4731 4732
}

4733

C
Cole Robinson 已提交
4734
static char *
4735
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4736 4737
                          unsigned int flags)
{
4738
    testDriverPtr privconn = pool->conn->privateData;
4739
    virStoragePoolObjPtr obj;
4740
    char *ret = NULL;
4741

E
Eric Blake 已提交
4742 4743
    virCheckFlags(0, NULL);

4744
    if (!(obj = testStoragePoolObjFindByName(privconn, pool->name)))
4745
        return NULL;
4746

4747
    ret = virStoragePoolDefFormat(obj->def);
4748

4749
    virStoragePoolObjUnlock(obj);
4750
    return ret;
C
Cole Robinson 已提交
4751 4752
}

4753

C
Cole Robinson 已提交
4754
static int
4755
testStoragePoolGetAutostart(virStoragePoolPtr pool,
4756 4757
                            int *autostart)
{
4758
    testDriverPtr privconn = pool->conn->privateData;
4759
    virStoragePoolObjPtr obj;
4760

4761
    if (!(obj = testStoragePoolObjFindByName(privconn, pool->name)))
4762
        return -1;
C
Cole Robinson 已提交
4763

4764
    if (!virStoragePoolObjGetConfigFile(obj))
C
Cole Robinson 已提交
4765
        *autostart = 0;
4766
    else
4767
        *autostart = virStoragePoolObjIsAutostart(obj) ? 1 : 0;
C
Cole Robinson 已提交
4768

4769 4770
    virStoragePoolObjUnlock(obj);
    return 0;
C
Cole Robinson 已提交
4771 4772
}

4773

C
Cole Robinson 已提交
4774
static int
4775
testStoragePoolSetAutostart(virStoragePoolPtr pool,
4776 4777
                            int autostart)
{
4778
    testDriverPtr privconn = pool->conn->privateData;
4779
    virStoragePoolObjPtr obj;
4780
    int ret = -1;
4781

4782
    if (!(obj = testStoragePoolObjFindByName(privconn, pool->name)))
4783
        return -1;
C
Cole Robinson 已提交
4784

4785
    if (!virStoragePoolObjGetConfigFile(obj)) {
4786 4787
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
4788
        goto cleanup;
C
Cole Robinson 已提交
4789 4790 4791
    }

    autostart = (autostart != 0);
4792
    virStoragePoolObjSetAutostart(obj, autostart);
4793 4794
    ret = 0;

4795
 cleanup:
4796
    virStoragePoolObjUnlock(obj);
4797
    return ret;
C
Cole Robinson 已提交
4798 4799 4800 4801
}


static int
4802 4803
testStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
4804
    testDriverPtr privconn = pool->conn->privateData;
4805
    virStoragePoolObjPtr obj;
4806
    int ret = -1;
4807

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

4811
    ret = virStoragePoolObjNumOfVolumes(obj, pool->conn, NULL);
4812

4813
    virStoragePoolObjUnlock(obj);
4814
    return ret;
C
Cole Robinson 已提交
4815 4816
}

4817

C
Cole Robinson 已提交
4818
static int
4819
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4820
                           char **const names,
4821 4822
                           int maxnames)
{
4823
    testDriverPtr privconn = pool->conn->privateData;
4824
    virStoragePoolObjPtr obj;
4825
    int n = -1;
4826

4827
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name)))
4828
        return -1;
4829

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

4832
    virStoragePoolObjUnlock(obj);
C
Cole Robinson 已提交
4833 4834 4835
    return n;
}

4836

4837
static int
4838
testStoragePoolListAllVolumes(virStoragePoolPtr pool,
4839
                              virStorageVolPtr **vols,
4840 4841
                              unsigned int flags)
{
4842 4843
    testDriverPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr obj;
4844 4845 4846 4847
    int ret = -1;

    virCheckFlags(0, -1);

4848
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4849
        return -1;
4850

4851
    if (!virStoragePoolObjIsActive(obj)) {
4852 4853 4854 4855 4856
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("storage pool is not active"));
        goto cleanup;
    }

4857
    ret = virStoragePoolObjVolumeListExport(pool->conn, obj, vols, NULL);
4858 4859

 cleanup:
4860
    virStoragePoolObjUnlock(obj);
4861 4862 4863

    return ret;
}
C
Cole Robinson 已提交
4864

4865

4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880
static virStorageVolDefPtr
testStorageVolDefFindByName(virStoragePoolObjPtr obj,
                            const char *name)
{
    virStorageVolDefPtr privvol;

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

    return privvol;
}


C
Cole Robinson 已提交
4881
static virStorageVolPtr
4882
testStorageVolLookupByName(virStoragePoolPtr pool,
4883
                           const char *name)
4884
{
4885
    testDriverPtr privconn = pool->conn->privateData;
4886
    virStoragePoolObjPtr obj;
4887
    virStorageVolDefPtr privvol;
4888
    virStorageVolPtr ret = NULL;
4889

4890 4891
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name)))
        return NULL;
4892

4893
    if (!(privvol = testStorageVolDefFindByName(obj, name)))
4894
        goto cleanup;
C
Cole Robinson 已提交
4895

4896
    ret = virGetStorageVol(pool->conn, obj->def->name,
4897 4898
                           privvol->name, privvol->key,
                           NULL, NULL);
4899

4900
 cleanup:
4901
    virStoragePoolObjUnlock(obj);
4902
    return ret;
C
Cole Robinson 已提交
4903 4904 4905 4906
}


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

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

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

4935
    if (!ret)
4936 4937
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
4938 4939

    return ret;
C
Cole Robinson 已提交
4940 4941
}

4942

C
Cole Robinson 已提交
4943
static virStorageVolPtr
4944
testStorageVolLookupByPath(virConnectPtr conn,
4945 4946
                           const char *path)
{
4947
    testDriverPtr privconn = conn->privateData;
4948
    size_t i;
4949
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4950

4951
    testDriverLock(privconn);
4952
    for (i = 0; i < privconn->pools.count; i++) {
4953
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4954
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4955
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4956 4957
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

4958 4959 4960 4961
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
4962 4963
                                       privvol->key,
                                       NULL, NULL);
4964
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4965 4966
                break;
            }
C
Cole Robinson 已提交
4967
        }
4968
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4969
    }
4970
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4971

4972
    if (!ret)
4973 4974
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
4975 4976

    return ret;
C
Cole Robinson 已提交
4977 4978
}

4979

C
Cole Robinson 已提交
4980
static virStorageVolPtr
4981 4982 4983
testStorageVolCreateXML(virStoragePoolPtr pool,
                        const char *xmldesc,
                        unsigned int flags)
E
Eric Blake 已提交
4984
{
4985
    testDriverPtr privconn = pool->conn->privateData;
4986
    virStoragePoolObjPtr obj;
4987 4988
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
4989

E
Eric Blake 已提交
4990 4991
    virCheckFlags(0, NULL);

4992 4993
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name)))
        return NULL;
C
Cole Robinson 已提交
4994

4995
    privvol = virStorageVolDefParseString(obj->def, xmldesc, 0);
4996
    if (privvol == NULL)
4997
        goto cleanup;
4998

4999
    if (virStorageVolDefFindByName(obj, privvol->name)) {
5000 5001
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5002
        goto cleanup;
C
Cole Robinson 已提交
5003 5004 5005
    }

    /* Make sure enough space */
5006 5007
    if ((obj->def->allocation + privvol->target.allocation) >
         obj->def->capacity) {
5008 5009 5010
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5011
        goto cleanup;
C
Cole Robinson 已提交
5012 5013
    }

5014
    if (virAsprintf(&privvol->target.path, "%s/%s",
5015
                    obj->def->target.path, privvol->name) < 0)
5016
        goto cleanup;
C
Cole Robinson 已提交
5017

5018
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
5019
        virStoragePoolObjAddVol(obj, privvol) < 0)
5020
        goto cleanup;
C
Cole Robinson 已提交
5021

5022 5023
    obj->def->allocation += privvol->target.allocation;
    obj->def->available = (obj->def->capacity - obj->def->allocation);
C
Cole Robinson 已提交
5024

5025
    ret = virGetStorageVol(pool->conn, obj->def->name,
5026 5027
                           privvol->name, privvol->key,
                           NULL, NULL);
5028
    privvol = NULL;
5029

5030
 cleanup:
5031
    virStorageVolDefFree(privvol);
5032
    virStoragePoolObjUnlock(obj);
5033
    return ret;
C
Cole Robinson 已提交
5034 5035
}

5036

5037
static virStorageVolPtr
5038 5039 5040 5041
testStorageVolCreateXMLFrom(virStoragePoolPtr pool,
                            const char *xmldesc,
                            virStorageVolPtr clonevol,
                            unsigned int flags)
E
Eric Blake 已提交
5042
{
5043
    testDriverPtr privconn = pool->conn->privateData;
5044
    virStoragePoolObjPtr obj;
5045 5046 5047
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
5048 5049
    virCheckFlags(0, NULL);

5050 5051
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name)))
        return NULL;
5052

5053
    privvol = virStorageVolDefParseString(obj->def, xmldesc, 0);
5054 5055 5056
    if (privvol == NULL)
        goto cleanup;

5057
    if (virStorageVolDefFindByName(obj, privvol->name)) {
5058 5059
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5060 5061 5062
        goto cleanup;
    }

5063
    origvol = virStorageVolDefFindByName(obj, clonevol->name);
5064
    if (!origvol) {
5065 5066 5067
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
5068 5069 5070 5071
        goto cleanup;
    }

    /* Make sure enough space */
5072 5073
    if ((obj->def->allocation + privvol->target.allocation) >
         obj->def->capacity) {
5074 5075 5076
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5077 5078
        goto cleanup;
    }
5079
    obj->def->available = (obj->def->capacity - obj->def->allocation);
5080

5081
    if (virAsprintf(&privvol->target.path, "%s/%s",
5082
                    obj->def->target.path, privvol->name) < 0)
5083 5084
        goto cleanup;

5085
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
5086
        virStoragePoolObjAddVol(obj, privvol) < 0)
5087 5088
        goto cleanup;

5089 5090
    obj->def->allocation += privvol->target.allocation;
    obj->def->available = (obj->def->capacity - obj->def->allocation);
5091

5092
    ret = virGetStorageVol(pool->conn, obj->def->name,
5093 5094
                           privvol->name, privvol->key,
                           NULL, NULL);
5095 5096
    privvol = NULL;

5097
 cleanup:
5098
    virStorageVolDefFree(privvol);
5099
    virStoragePoolObjUnlock(obj);
5100 5101 5102
    return ret;
}

5103

C
Cole Robinson 已提交
5104
static int
5105 5106
testStorageVolDelete(virStorageVolPtr vol,
                     unsigned int flags)
E
Eric Blake 已提交
5107
{
5108
    testDriverPtr privconn = vol->conn->privateData;
5109
    virStoragePoolObjPtr obj;
5110
    virStorageVolDefPtr privvol;
5111
    int ret = -1;
C
Cole Robinson 已提交
5112

E
Eric Blake 已提交
5113 5114
    virCheckFlags(0, -1);

5115 5116
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, vol->pool)))
        return -1;
5117

5118
    if (!(privvol = testStorageVolDefFindByName(obj, vol->name)))
5119
        goto cleanup;
5120

5121 5122
    obj->def->allocation -= privvol->target.allocation;
    obj->def->available = (obj->def->capacity - obj->def->allocation);
C
Cole Robinson 已提交
5123

5124
    virStoragePoolObjRemoveVol(obj, privvol);
C
Cole Robinson 已提交
5125

5126
    ret = 0;
C
Cole Robinson 已提交
5127

5128
 cleanup:
5129
    virStoragePoolObjUnlock(obj);
5130
    return ret;
C
Cole Robinson 已提交
5131 5132 5133
}


5134 5135
static int
testStorageVolumeTypeForPool(int pooltype)
5136
{
C
Cole Robinson 已提交
5137

5138
    switch (pooltype) {
C
Cole Robinson 已提交
5139 5140 5141 5142 5143 5144 5145 5146 5147
        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;
    }
}

5148

C
Cole Robinson 已提交
5149
static int
5150
testStorageVolGetInfo(virStorageVolPtr vol,
5151 5152
                      virStorageVolInfoPtr info)
{
5153
    testDriverPtr privconn = vol->conn->privateData;
5154
    virStoragePoolObjPtr obj;
5155
    virStorageVolDefPtr privvol;
5156
    int ret = -1;
5157

5158 5159
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, vol->pool)))
        return -1;
5160

5161
    if (!(privvol = testStorageVolDefFindByName(obj, vol->name)))
5162
        goto cleanup;
5163

C
Cole Robinson 已提交
5164
    memset(info, 0, sizeof(*info));
5165
    info->type = testStorageVolumeTypeForPool(obj->def->type);
5166 5167
    info->capacity = privvol->target.capacity;
    info->allocation = privvol->target.allocation;
5168
    ret = 0;
C
Cole Robinson 已提交
5169

5170
 cleanup:
5171
    virStoragePoolObjUnlock(obj);
5172
    return ret;
C
Cole Robinson 已提交
5173 5174
}

5175

C
Cole Robinson 已提交
5176
static char *
5177 5178
testStorageVolGetXMLDesc(virStorageVolPtr vol,
                         unsigned int flags)
E
Eric Blake 已提交
5179
{
5180
    testDriverPtr privconn = vol->conn->privateData;
5181
    virStoragePoolObjPtr obj;
5182
    virStorageVolDefPtr privvol;
5183
    char *ret = NULL;
5184

E
Eric Blake 已提交
5185 5186
    virCheckFlags(0, NULL);

5187 5188
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, vol->pool)))
        return NULL;
5189

5190
    if (!(privvol = testStorageVolDefFindByName(obj, vol->name)))
5191
        goto cleanup;
C
Cole Robinson 已提交
5192

5193
    ret = virStorageVolDefFormat(obj->def, privvol);
5194

5195
 cleanup:
5196
    virStoragePoolObjUnlock(obj);
5197
    return ret;
C
Cole Robinson 已提交
5198 5199
}

5200

C
Cole Robinson 已提交
5201
static char *
5202 5203
testStorageVolGetPath(virStorageVolPtr vol)
{
5204
    testDriverPtr privconn = vol->conn->privateData;
5205
    virStoragePoolObjPtr obj;
5206
    virStorageVolDefPtr privvol;
5207
    char *ret = NULL;
5208

5209 5210
    if (!(obj = testStoragePoolObjFindActiveByName(privconn, vol->pool)))
        return NULL;
5211

5212
    if (!(privvol = testStorageVolDefFindByName(obj, vol->name)))
5213
        goto cleanup;
5214

5215
    ignore_value(VIR_STRDUP(ret, privvol->target.path));
5216

5217
 cleanup:
5218
    virStoragePoolObjUnlock(obj);
C
Cole Robinson 已提交
5219 5220 5221
    return ret;
}

5222

5223
/* Node device implementations */
5224

5225 5226 5227 5228 5229 5230
static virNodeDeviceObjPtr
testNodeDeviceObjFindByName(testDriverPtr driver,
                            const char *name)
{
    virNodeDeviceObjPtr obj;

5231
    if (!(obj = virNodeDeviceObjListFindByName(driver->devs, name)))
5232 5233 5234 5235 5236 5237 5238 5239
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       name);

    return obj;
}


5240 5241 5242
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5243
                     unsigned int flags)
5244
{
5245
    testDriverPtr driver = conn->privateData;
5246

E
Eric Blake 已提交
5247 5248
    virCheckFlags(0, -1);

5249
    return virNodeDeviceObjListNumOfDevices(driver->devs, conn, cap, NULL);
5250 5251
}

5252

5253 5254 5255 5256 5257
static int
testNodeListDevices(virConnectPtr conn,
                    const char *cap,
                    char **const names,
                    int maxnames,
E
Eric Blake 已提交
5258
                    unsigned int flags)
5259
{
5260
    testDriverPtr driver = conn->privateData;
5261

E
Eric Blake 已提交
5262 5263
    virCheckFlags(0, -1);

5264 5265
    return virNodeDeviceObjListGetNames(driver->devs, conn, NULL,
                                        cap, names, maxnames);
5266 5267
}

5268

5269 5270 5271
static virNodeDevicePtr
testNodeDeviceLookupByName(virConnectPtr conn, const char *name)
{
5272
    testDriverPtr driver = conn->privateData;
5273
    virNodeDeviceObjPtr obj;
5274
    virNodeDeviceDefPtr def;
5275 5276
    virNodeDevicePtr ret = NULL;

5277
    if (!(obj = testNodeDeviceObjFindByName(driver, name)))
5278
        return NULL;
5279
    def = virNodeDeviceObjGetDef(obj);
5280

5281
    if ((ret = virGetNodeDevice(conn, name))) {
5282
        if (VIR_STRDUP(ret->parent, def->parent) < 0) {
5283
            virObjectUnref(ret);
5284 5285
            ret = NULL;
        }
5286
    }
5287

5288
    virNodeDeviceObjEndAPI(&obj);
5289 5290 5291 5292
    return ret;
}

static char *
5293
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5294
                         unsigned int flags)
5295
{
5296
    testDriverPtr driver = dev->conn->privateData;
5297 5298 5299
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5300 5301
    virCheckFlags(0, NULL);

5302
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5303
        return NULL;
5304

5305
    ret = virNodeDeviceDefFormat(virNodeDeviceObjGetDef(obj));
5306

5307
    virNodeDeviceObjEndAPI(&obj);
5308 5309 5310 5311 5312 5313
    return ret;
}

static char *
testNodeDeviceGetParent(virNodeDevicePtr dev)
{
5314
    testDriverPtr driver = dev->conn->privateData;
5315
    virNodeDeviceObjPtr obj;
5316
    virNodeDeviceDefPtr def;
5317 5318
    char *ret = NULL;

5319
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5320
        return NULL;
5321
    def = virNodeDeviceObjGetDef(obj);
5322

5323 5324
    if (def->parent) {
        ignore_value(VIR_STRDUP(ret, def->parent));
5325
    } else {
5326 5327
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5328 5329
    }

5330
    virNodeDeviceObjEndAPI(&obj);
5331 5332 5333
    return ret;
}

5334

5335 5336 5337
static int
testNodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
5338
    testDriverPtr driver = dev->conn->privateData;
5339
    virNodeDeviceObjPtr obj;
5340
    virNodeDeviceDefPtr def;
5341 5342 5343
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;

5344
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5345
        return -1;
5346
    def = virNodeDeviceObjGetDef(obj);
5347

5348
    for (caps = def->caps; caps; caps = caps->next)
5349 5350
        ++ncaps;

5351
    virNodeDeviceObjEndAPI(&obj);
5352
    return ncaps;
5353 5354 5355 5356 5357 5358
}


static int
testNodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
5359
    testDriverPtr driver = dev->conn->privateData;
5360
    virNodeDeviceObjPtr obj;
5361
    virNodeDeviceDefPtr def;
5362 5363 5364
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;

5365
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5366
        return -1;
5367
    def = virNodeDeviceObjGetDef(obj);
5368

5369
    for (caps = def->caps; caps && ncaps < maxnames; caps = caps->next) {
5370 5371 5372 5373
        if (VIR_STRDUP(names[ncaps],
                       virNodeDevCapTypeToString(caps->data.type)) < 0)
            goto error;
        ncaps++;
5374 5375
    }

5376
    virNodeDeviceObjEndAPI(&obj);
5377 5378 5379 5380 5381
    return ncaps;

 error:
    while (--ncaps >= 0)
        VIR_FREE(names[ncaps]);
5382
    virNodeDeviceObjEndAPI(&obj);
5383
    return -1;
5384 5385
}

5386

5387 5388
static virNodeDeviceObjPtr
testNodeDeviceMockCreateVport(testDriverPtr driver,
5389
                              const char *wwnn,
5390
                              const char *wwpn)
5391
{
5392 5393
    char *xml = NULL;
    virNodeDeviceDefPtr def = NULL;
5394
    virNodeDevCapsDefPtr caps;
5395
    virNodeDeviceObjPtr obj = NULL, objcopy = NULL;
5396
    virNodeDeviceDefPtr objdef;
5397
    virObjectEventPtr event = NULL;
5398

5399 5400 5401 5402 5403 5404 5405 5406 5407
    /* 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. */
5408 5409
    if (!(objcopy = virNodeDeviceObjListFindByName(driver->devs,
                                                   "scsi_host11")))
5410 5411
        goto cleanup;

5412
    xml = virNodeDeviceDefFormat(virNodeDeviceObjGetDef(objcopy));
5413
    virNodeDeviceObjEndAPI(&objcopy);
5414 5415 5416 5417
    if (!xml)
        goto cleanup;

    if (!(def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL)))
5418 5419
        goto cleanup;

5420
    VIR_FREE(def->name);
5421
    if (VIR_STRDUP(def->name, "scsi_host12") < 0)
5422 5423
        goto cleanup;

5424 5425 5426
    /* 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. */
5427 5428
    caps = def->caps;
    while (caps) {
5429
        if (caps->data.type != VIR_NODE_DEV_CAP_SCSI_HOST)
5430 5431
            continue;

5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445
        /* 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++;
        }
5446 5447 5448
        caps = caps->next;
    }

5449
    if (!(obj = virNodeDeviceObjListAssignDef(driver->devs, def)))
5450
        goto cleanup;
5451
    def = NULL;
5452
    objdef = virNodeDeviceObjGetDef(obj);
5453

5454
    event = virNodeDeviceEventLifecycleNew(objdef->name,
5455 5456
                                           VIR_NODE_DEVICE_EVENT_CREATED,
                                           0);
5457 5458 5459
    testObjectEventQueue(driver, event);

 cleanup:
5460
    VIR_FREE(xml);
5461 5462
    virNodeDeviceDefFree(def);
    return obj;
5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473
}


static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
                        unsigned int flags)
{
    testDriverPtr driver = conn->privateData;
    virNodeDeviceDefPtr def = NULL;
    char *wwnn = NULL, *wwpn = NULL;
5474 5475
    virNodeDevicePtr dev = NULL, ret = NULL;
    virNodeDeviceObjPtr obj = NULL;
5476
    virNodeDeviceDefPtr objdef;
5477 5478 5479 5480 5481 5482

    virCheckFlags(0, NULL);

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

5483 5484 5485
    /* We run this simply for validation - it essentially validates that
     * the input XML either has a wwnn/wwpn or virNodeDevCapSCSIHostParseXML
     * generated a wwnn/wwpn */
5486 5487 5488
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) < 0)
        goto cleanup;

5489 5490 5491
    /* 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. */
5492
    if (virNodeDeviceObjListGetParentHost(driver->devs, def) < 0)
5493 5494 5495 5496
        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
5497 5498 5499
     * 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 */
5500 5501
    if (!(obj = testNodeDeviceMockCreateVport(driver, wwnn, wwpn)))
        goto cleanup;
5502
    objdef = virNodeDeviceObjGetDef(obj);
5503

5504
    if (!(dev = virGetNodeDevice(conn, objdef->name)))
5505 5506 5507 5508 5509 5510 5511 5512
        goto cleanup;

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

    ret = dev;
    dev = NULL;
5513

5514
 cleanup:
5515
    virNodeDeviceObjEndAPI(&obj);
5516
    virNodeDeviceDefFree(def);
5517
    virObjectUnref(dev);
5518 5519
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
5520
    return ret;
5521 5522 5523 5524 5525 5526
}

static int
testNodeDeviceDestroy(virNodeDevicePtr dev)
{
    int ret = 0;
5527
    testDriverPtr driver = dev->conn->privateData;
5528
    virNodeDeviceObjPtr obj = NULL;
5529
    virNodeDeviceObjPtr parentobj = NULL;
5530
    virNodeDeviceDefPtr def;
5531
    char *wwnn = NULL, *wwpn = NULL;
5532
    virObjectEventPtr event = NULL;
5533

5534
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5535
        return -1;
5536
    def = virNodeDeviceObjGetDef(obj);
5537

5538
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1)
5539
        goto cleanup;
5540

5541 5542 5543 5544
    /* Unlike the real code we cannot run into the udevAddOneDevice race
     * which would replace obj->def, so no need to save off the parent,
     * but do need to drop the @obj lock so that the FindByName code doesn't
     * deadlock on ourselves */
5545
    virObjectUnlock(obj);
5546

5547 5548 5549 5550 5551 5552
    /* We do this just for basic validation and throw away the parentobj
     * since there's no vport_delete to be run */
    if (!(parentobj = virNodeDeviceObjListFindByName(driver->devs,
                                                     def->parent))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("cannot find parent '%s' definition"), def->parent);
5553
        virObjectLock(obj);
5554
        goto cleanup;
5555
    }
5556
    virNodeDeviceObjEndAPI(&parentobj);
5557

5558 5559 5560 5561
    event = virNodeDeviceEventLifecycleNew(dev->name,
                                           VIR_NODE_DEVICE_EVENT_DELETED,
                                           0);

5562
    virObjectLock(obj);
5563
    virNodeDeviceObjListRemove(driver->devs, obj);
5564
    virObjectUnref(obj);
5565
    obj = NULL;
5566

5567
 cleanup:
5568
    virNodeDeviceObjEndAPI(&obj);
5569
    testObjectEventQueue(driver, event);
5570 5571 5572 5573 5574
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

5575 5576

/* Domain event implementations */
5577
static int
5578 5579 5580 5581
testConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
5582
{
5583
    testDriverPtr driver = conn->privateData;
5584
    int ret = 0;
5585

5586
    if (virDomainEventStateRegister(conn, driver->eventState,
5587 5588
                                    callback, opaque, freecb) < 0)
        ret = -1;
5589 5590 5591 5592

    return ret;
}

5593

5594
static int
5595 5596
testConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
5597
{
5598
    testDriverPtr driver = conn->privateData;
5599
    int ret = 0;
5600

5601
    if (virDomainEventStateDeregister(conn, driver->eventState,
5602 5603
                                      callback) < 0)
        ret = -1;
5604 5605 5606 5607

    return ret;
}

5608 5609

static int
5610 5611 5612 5613 5614 5615
testConnectDomainEventRegisterAny(virConnectPtr conn,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback callback,
                                  void *opaque,
                                  virFreeCallback freecb)
5616
{
5617
    testDriverPtr driver = conn->privateData;
5618 5619
    int ret;

5620
    if (virDomainEventStateRegisterID(conn, driver->eventState,
5621 5622
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
5623
        ret = -1;
5624 5625 5626 5627 5628

    return ret;
}

static int
5629 5630
testConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
5631
{
5632
    testDriverPtr driver = conn->privateData;
5633
    int ret = 0;
5634

5635
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5636
                                        callbackID, true) < 0)
5637
        ret = -1;
5638 5639 5640 5641 5642

    return ret;
}


5643 5644 5645 5646 5647 5648 5649 5650
static int
testConnectNetworkEventRegisterAny(virConnectPtr conn,
                                   virNetworkPtr net,
                                   int eventID,
                                   virConnectNetworkEventGenericCallback callback,
                                   void *opaque,
                                   virFreeCallback freecb)
{
5651
    testDriverPtr driver = conn->privateData;
5652 5653
    int ret;

5654
    if (virNetworkEventStateRegisterID(conn, driver->eventState,
5655
                                       net, eventID, callback,
5656 5657 5658 5659 5660 5661 5662 5663 5664 5665
                                       opaque, freecb, &ret) < 0)
        ret = -1;

    return ret;
}

static int
testConnectNetworkEventDeregisterAny(virConnectPtr conn,
                                     int callbackID)
{
5666
    testDriverPtr driver = conn->privateData;
5667
    int ret = 0;
5668

5669
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5670
                                        callbackID, true) < 0)
5671
        ret = -1;
5672 5673 5674 5675

    return ret;
}

5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702
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,
5703
                                        callbackID, true) < 0)
5704 5705 5706 5707 5708
        ret = -1;

    return ret;
}

5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735
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,
5736
                                        callbackID, true) < 0)
5737 5738 5739 5740 5741
        ret = -1;

    return ret;
}

5742 5743 5744
static int testConnectListAllDomains(virConnectPtr conn,
                                     virDomainPtr **domains,
                                     unsigned int flags)
5745
{
5746
    testDriverPtr privconn = conn->privateData;
5747

O
Osier Yang 已提交
5748
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
5749

5750 5751
    return virDomainObjListExport(privconn->domains, conn, domains,
                                  NULL, flags);
5752 5753
}

5754
static int
P
Peter Krempa 已提交
5755
testNodeGetCPUMap(virConnectPtr conn ATTRIBUTE_UNUSED,
5756 5757 5758 5759 5760 5761 5762
                  unsigned char **cpumap,
                  unsigned int *online,
                  unsigned int flags)
{
    virCheckFlags(0, -1);

    if (cpumap) {
5763
        if (VIR_ALLOC_N(*cpumap, 1) < 0)
P
Peter Krempa 已提交
5764
            return -1;
5765 5766 5767 5768 5769 5770
        *cpumap[0] = 0x15;
    }

    if (online)
        *online = 3;

P
Peter Krempa 已提交
5771
    return  8;
5772 5773
}

5774 5775 5776 5777 5778 5779 5780 5781 5782 5783
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

5784
    if (VIR_STRDUP(ret, "image/png") < 0)
5785 5786
        return NULL;

D
Daniel P. Berrange 已提交
5787
    if (virFDStreamOpenFile(st, PKGDATADIR "/test-screenshot.png", 0, 0, O_RDONLY) < 0)
5788 5789 5790 5791 5792
        VIR_FREE(ret);

    return ret;
}

5793 5794
static int
testConnectGetCPUModelNames(virConnectPtr conn ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
5795
                            const char *archName,
5796 5797 5798
                            char ***models,
                            unsigned int flags)
{
J
Jiri Denemark 已提交
5799 5800
    virArch arch;

5801
    virCheckFlags(0, -1);
J
Jiri Denemark 已提交
5802 5803 5804 5805 5806 5807 5808 5809

    if (!(arch = virArchFromString(archName))) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("cannot find architecture %s"),
                       archName);
        return -1;
    }

J
Jiri Denemark 已提交
5810
    return virCPUGetModels(arch, models);
5811
}
5812

C
Cole Robinson 已提交
5813 5814 5815
static int
testDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
5816
    testDriverPtr privconn = dom->conn->privateData;
C
Cole Robinson 已提交
5817
    virDomainObjPtr vm = NULL;
5818
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
5819 5820 5821 5822 5823 5824
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_SAVE_BYPASS_CACHE |
                  VIR_DOMAIN_SAVE_RUNNING |
                  VIR_DOMAIN_SAVE_PAUSED, -1);

5825 5826
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840

    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);
5841
    event = virDomainEventLifecycleNewFromObj(vm,
C
Cole Robinson 已提交
5842 5843 5844 5845 5846
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
    vm->hasManagedSave = true;

    ret = 0;
5847
 cleanup:
5848
    virDomainObjEndAPI(&vm);
5849
    testObjectEventQueue(privconn, event);
C
Cole Robinson 已提交
5850 5851 5852 5853 5854 5855 5856 5857 5858

    return ret;
}


static int
testDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;
5859
    int ret;
C
Cole Robinson 已提交
5860 5861 5862

    virCheckFlags(0, -1);

5863 5864
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5865 5866

    ret = vm->hasManagedSave;
5867

5868
    virDomainObjEndAPI(&vm);
C
Cole Robinson 已提交
5869 5870 5871 5872 5873 5874 5875 5876 5877 5878
    return ret;
}

static int
testDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;

    virCheckFlags(0, -1);

5879 5880
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5881 5882

    vm->hasManagedSave = false;
5883

5884
    virDomainObjEndAPI(&vm);
5885
    return 0;
C
Cole Robinson 已提交
5886 5887 5888
}


5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922
/*
 * 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;
5923
    int n;
5924 5925 5926 5927 5928

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
5929
        return -1;
5930 5931 5932

    n = virDomainSnapshotObjListNum(vm->snapshots, NULL, flags);

5933
    virDomainObjEndAPI(&vm);
5934 5935 5936 5937 5938 5939 5940 5941 5942 5943
    return n;
}

static int
testDomainSnapshotListNames(virDomainPtr domain,
                            char **names,
                            int nameslen,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
5944
    int n;
5945 5946 5947 5948 5949

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
5950
        return -1;
5951 5952 5953 5954

    n = virDomainSnapshotObjListGetNames(vm->snapshots, NULL, names, nameslen,
                                         flags);

5955
    virDomainObjEndAPI(&vm);
5956 5957 5958 5959 5960 5961 5962 5963 5964
    return n;
}

static int
testDomainListAllSnapshots(virDomainPtr domain,
                           virDomainSnapshotPtr **snaps,
                           unsigned int flags)
{
    virDomainObjPtr vm = NULL;
5965
    int n;
5966 5967 5968 5969 5970

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
5971
        return -1;
5972 5973 5974

    n = virDomainListSnapshots(vm->snapshots, NULL, domain, snaps, flags);

5975
    virDomainObjEndAPI(&vm);
5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992
    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)))
5993
        return -1;
5994 5995 5996 5997 5998 5999 6000

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListGetNames(vm->snapshots, snap, names, nameslen,
                                         flags);

6001
 cleanup:
6002
    virDomainObjEndAPI(&vm);
6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017
    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)))
6018
        return -1;
6019 6020 6021 6022 6023 6024

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListNum(vm->snapshots, snap, flags);

6025
 cleanup:
6026
    virDomainObjEndAPI(&vm);
6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042
    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)))
6043
        return -1;
6044 6045 6046 6047 6048 6049 6050

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainListSnapshots(vm->snapshots, snap, snapshot->domain, snaps,
                               flags);

6051
 cleanup:
6052
    virDomainObjEndAPI(&vm);
6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067
    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)))
6068
        return NULL;
6069 6070 6071 6072 6073 6074

    if (!(snap = testSnapObjFromName(vm, name)))
        goto cleanup;

    snapshot = virGetDomainSnapshot(domain, snap->def->name);

6075
 cleanup:
6076
    virDomainObjEndAPI(&vm);
6077 6078 6079 6080 6081 6082 6083 6084
    return snapshot;
}

static int
testDomainHasCurrentSnapshot(virDomainPtr domain,
                             unsigned int flags)
{
    virDomainObjPtr vm;
6085
    int ret;
6086 6087 6088 6089

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6090
        return -1;
6091 6092 6093

    ret = (vm->current_snapshot != NULL);

6094
    virDomainObjEndAPI(&vm);
6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108
    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)))
6109
        return NULL;
6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122

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

6123
 cleanup:
6124
    virDomainObjEndAPI(&vm);
6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137
    return parent;
}

static virDomainSnapshotPtr
testDomainSnapshotCurrent(virDomainPtr domain,
                          unsigned int flags)
{
    virDomainObjPtr vm;
    virDomainSnapshotPtr snapshot = NULL;

    virCheckFlags(0, NULL);

    if (!(vm = testDomObjFromDomain(domain)))
6138
        return NULL;
6139 6140 6141 6142 6143 6144 6145 6146 6147

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

6148
 cleanup:
6149
    virDomainObjEndAPI(&vm);
6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160
    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];
6161
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6162 6163 6164 6165

    virCheckFlags(VIR_DOMAIN_XML_SECURE, NULL);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6166
        return NULL;
6167 6168 6169 6170 6171 6172

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    virUUIDFormat(snapshot->domain->uuid, uuidstr);

6173
    xml = virDomainSnapshotDefFormat(uuidstr, snap->def, privconn->caps,
6174
                                     privconn->xmlopt,
6175 6176
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);
6177

6178
 cleanup:
6179
    virDomainObjEndAPI(&vm);
6180 6181 6182 6183 6184 6185 6186 6187
    return xml;
}

static int
testDomainSnapshotIsCurrent(virDomainSnapshotPtr snapshot,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6188
    int ret;
6189 6190 6191 6192

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6193
        return -1;
6194 6195 6196 6197

    ret = (vm->current_snapshot &&
           STREQ(snapshot->name, vm->current_snapshot->def->name));

6198
    virDomainObjEndAPI(&vm);
6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212
    return ret;
}


static int
testDomainSnapshotHasMetadata(virDomainSnapshotPtr snapshot,
                              unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6213
        return -1;
6214

C
Cole Robinson 已提交
6215
    if (!testSnapObjFromSnapshot(vm, snapshot))
6216 6217 6218 6219
        goto cleanup;

    ret = 1;

6220
 cleanup:
6221
    virDomainObjEndAPI(&vm);
6222 6223 6224
    return ret;
}

6225 6226 6227 6228 6229 6230
static int
testDomainSnapshotAlignDisks(virDomainObjPtr vm,
                             virDomainSnapshotDefPtr def,
                             unsigned int flags)
{
    int align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
E
Eric Blake 已提交
6231
    bool align_match = true;
6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259

    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)
{
6260
    testDriverPtr privconn = domain->conn->privateData;
6261 6262 6263 6264
    virDomainObjPtr vm = NULL;
    virDomainSnapshotDefPtr def = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    virDomainSnapshotPtr snapshot = NULL;
6265
    virObjectEventPtr event = NULL;
6266
    char *xml = NULL;
6267 6268
    bool update_current = true;
    bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE;
6269 6270 6271 6272 6273 6274 6275 6276
    unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;

    /*
     * DISK_ONLY: Not implemented yet
     * REUSE_EXT: Not implemented yet
     *
     * NO_METADATA: Explicitly not implemented
     *
6277
     * REDEFINE + CURRENT: Implemented
6278 6279 6280 6281 6282 6283
     * HALT: Implemented
     * QUIESCE: Nothing to do
     * ATOMIC: Nothing to do
     * LIVE: Nothing to do
     */
    virCheckFlags(
6284 6285
        VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
        VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT |
6286 6287 6288 6289 6290
        VIR_DOMAIN_SNAPSHOT_CREATE_HALT |
        VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE |
        VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC |
        VIR_DOMAIN_SNAPSHOT_CREATE_LIVE, NULL);

6291 6292 6293 6294 6295
    if ((redefine && !(flags & VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT)))
        update_current = false;
    if (redefine)
        parse_flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;

6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310
    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;

6311
    if (redefine) {
C
Cole Robinson 已提交
6312
        if (virDomainSnapshotRedefinePrep(domain, vm, &def, &snap,
6313
                                          privconn->xmlopt,
C
Cole Robinson 已提交
6314
                                          &update_current, flags) < 0)
6315 6316 6317 6318 6319
            goto cleanup;
    } else {
        if (!(def->dom = virDomainDefCopy(vm->def,
                                          privconn->caps,
                                          privconn->xmlopt,
6320
                                          NULL,
6321 6322
                                          true)))
            goto cleanup;
6323

6324
        if (testDomainSnapshotAlignDisks(vm, def, flags) < 0)
6325 6326 6327
            goto cleanup;
    }

6328 6329 6330 6331
    if (!snap) {
        if (!(snap = virDomainSnapshotAssignDef(vm->snapshots, def)))
            goto cleanup;
        def = NULL;
6332 6333
    }

6334 6335 6336 6337 6338 6339 6340 6341 6342 6343
    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);
6344
            event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
6345 6346 6347
                                    VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
        }
    }
6348 6349

    snapshot = virGetDomainSnapshot(domain, snap->def->name);
6350
 cleanup:
6351 6352 6353 6354
    VIR_FREE(xml);
    if (vm) {
        if (snapshot) {
            virDomainSnapshotObjPtr other;
6355 6356
            if (update_current)
                vm->current_snapshot = snap;
6357 6358 6359 6360 6361 6362 6363
            other = virDomainSnapshotFindByName(vm->snapshots,
                                                snap->def->parent);
            snap->parent = other;
            other->nchildren++;
            snap->sibling = other->first_child;
            other->first_child = snap;
        }
6364
        virDomainObjEndAPI(&vm);
6365
    }
6366
    testObjectEventQueue(privconn, event);
6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378
    virDomainSnapshotDefFree(def);
    return snapshot;
}


typedef struct _testSnapRemoveData testSnapRemoveData;
typedef testSnapRemoveData *testSnapRemoveDataPtr;
struct _testSnapRemoveData {
    virDomainObjPtr vm;
    bool current;
};

6379
static int
6380
testDomainSnapshotDiscardAll(void *payload,
6381 6382
                             const void *name ATTRIBUTE_UNUSED,
                             void *data)
6383 6384 6385 6386 6387 6388 6389
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapRemoveDataPtr curr = data;

    if (snap->def->current)
        curr->current = true;
    virDomainSnapshotObjListRemove(curr->vm->snapshots, snap);
6390
    return 0;
6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401
}

typedef struct _testSnapReparentData testSnapReparentData;
typedef testSnapReparentData *testSnapReparentDataPtr;
struct _testSnapReparentData {
    virDomainSnapshotObjPtr parent;
    virDomainObjPtr vm;
    int err;
    virDomainSnapshotObjPtr last;
};

6402
static int
6403 6404 6405 6406 6407 6408 6409
testDomainSnapshotReparentChildren(void *payload,
                                   const void *name ATTRIBUTE_UNUSED,
                                   void *data)
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapReparentDataPtr rep = data;

6410
    if (rep->err < 0)
6411
        return 0;
6412 6413 6414 6415 6416 6417 6418

    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;
6419
        return 0;
6420 6421 6422 6423
    }

    if (!snap->sibling)
        rep->last = snap;
6424
    return 0;
6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453
}

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) {
6454
            if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY)
6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497
                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;
6498
 cleanup:
6499
    virDomainObjEndAPI(&vm);
6500 6501 6502 6503 6504 6505 6506
    return ret;
}

static int
testDomainRevertToSnapshot(virDomainSnapshotPtr snapshot,
                           unsigned int flags)
{
6507
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6508 6509
    virDomainObjPtr vm = NULL;
    virDomainSnapshotObjPtr snap = NULL;
6510 6511
    virObjectEventPtr event = NULL;
    virObjectEventPtr event2 = NULL;
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 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574
    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;
6575 6576
    config = virDomainDefCopy(snap->def->dom, privconn->caps,
                              privconn->xmlopt, NULL, true);
6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588
    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.  */
6589 6590
            if (!virDomainDefCheckABIStability(vm->def, config,
                                               privconn->xmlopt)) {
6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603
                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);
6604
                event = virDomainEventLifecycleNewFromObj(vm,
6605 6606
                            VIR_DOMAIN_EVENT_STOPPED,
                            VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
6607
                testObjectEventQueue(privconn, event);
6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618
                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. */
6619
                event = virDomainEventLifecycleNewFromObj(vm,
6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632
                                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;
6633
            event = virDomainEventLifecycleNewFromObj(vm,
6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646
                                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 */
6647
                event2 = virDomainEventLifecycleNewFromObj(vm,
6648 6649 6650 6651 6652
                                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 已提交
6653
            virObjectUnref(event);
6654 6655 6656 6657
            event = NULL;

            if (was_stopped) {
                /* Transition 2 */
6658
                event = virDomainEventLifecycleNewFromObj(vm,
6659 6660 6661 6662
                                VIR_DOMAIN_EVENT_STARTED,
                                VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            } else if (was_running) {
                /* Transition 8 */
6663
                event = virDomainEventLifecycleNewFromObj(vm,
6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675
                                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);
6676
            event = virDomainEventLifecycleNewFromObj(vm,
6677 6678 6679 6680 6681 6682 6683 6684 6685
                                    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;

6686
            testObjectEventQueue(privconn, event);
6687
            event = virDomainEventLifecycleNewFromObj(vm,
6688 6689 6690
                            VIR_DOMAIN_EVENT_STARTED,
                            VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            if (paused) {
6691
                event2 = virDomainEventLifecycleNewFromObj(vm,
6692 6693 6694 6695 6696 6697 6698 6699
                                VIR_DOMAIN_EVENT_SUSPENDED,
                                VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT);
            }
        }
    }

    vm->current_snapshot = snap;
    ret = 0;
6700
 cleanup:
6701
    if (event) {
6702
        testObjectEventQueue(privconn, event);
6703
        testObjectEventQueue(privconn, event2);
C
Cole Robinson 已提交
6704
    } else {
C
Cédric Bosdonnat 已提交
6705
        virObjectUnref(event2);
6706
    }
6707
    virDomainObjEndAPI(&vm);
6708 6709 6710 6711 6712

    return ret;
}


6713

6714
static virHypervisorDriver testHypervisorDriver = {
6715
    .name = "Test",
6716 6717 6718
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
6719
    .connectGetHostname = testConnectGetHostname, /* 0.6.3 */
6720
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
6721
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
6722
    .nodeGetCPUStats = testNodeGetCPUStats, /* 2.3.0 */
6723
    .nodeGetFreeMemory = testNodeGetFreeMemory, /* 2.3.0 */
6724
    .nodeGetFreePages = testNodeGetFreePages, /* 2.3.0 */
6725
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
6726
    .connectGetSysinfo = testConnectGetSysinfo, /* 2.3.0 */
6727
    .connectGetType = testConnectGetType, /* 2.3.0 */
6728 6729 6730
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
6731
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745
    .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 */
6746 6747
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
6748
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
6749
    .domainRestore = testDomainRestore, /* 0.3.2 */
6750
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
6751
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
6752
    .domainCoreDumpWithFormat = testDomainCoreDumpWithFormat, /* 1.2.3 */
6753
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
6754 6755 6756 6757
    .domainSetVcpusFlags = testDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = testDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = testDomainPinVcpu, /* 0.7.3 */
    .domainGetVcpus = testDomainGetVcpus, /* 0.7.3 */
6758
    .domainGetVcpuPinInfo = testDomainGetVcpuPinInfo, /* 1.2.18 */
6759 6760
    .domainGetMaxVcpus = testDomainGetMaxVcpus, /* 0.7.3 */
    .domainGetXMLDesc = testDomainGetXMLDesc, /* 0.1.4 */
6761 6762
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
6763 6764 6765
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
6766
    .domainDefineXMLFlags = testDomainDefineXMLFlags, /* 1.2.12 */
6767
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
6768
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
6769 6770 6771
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
6772 6773 6774 6775
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
6776 6777 6778
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
6779 6780 6781 6782
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
6783 6784 6785
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
6786 6787 6788
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
6789
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
6790
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
6791 6792
    .domainGetMetadata = testDomainGetMetadata, /* 1.1.3 */
    .domainSetMetadata = testDomainSetMetadata, /* 1.1.3 */
6793
    .connectGetCPUModelNames = testConnectGetCPUModelNames, /* 1.1.3 */
6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810
    .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 */
6811 6812 6813
    .domainSnapshotCreateXML = testDomainSnapshotCreateXML, /* 1.1.4 */
    .domainRevertToSnapshot = testDomainRevertToSnapshot, /* 1.1.4 */
    .domainSnapshotDelete = testDomainSnapshotDelete, /* 1.1.4 */
6814

E
Eric Blake 已提交
6815
    .connectBaselineCPU = testConnectBaselineCPU, /* 1.2.0 */
6816 6817 6818
};

static virNetworkDriver testNetworkDriver = {
6819 6820 6821 6822 6823
    .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 */
6824 6825
    .connectNetworkEventRegisterAny = testConnectNetworkEventRegisterAny, /* 1.2.1 */
    .connectNetworkEventDeregisterAny = testConnectNetworkEventDeregisterAny, /* 1.2.1 */
6826 6827 6828 6829
    .networkLookupByUUID = testNetworkLookupByUUID, /* 0.3.2 */
    .networkLookupByName = testNetworkLookupByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreateXML, /* 0.3.2 */
    .networkDefineXML = testNetworkDefineXML, /* 0.3.2 */
6830
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
6831
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
6832
    .networkCreate = testNetworkCreate, /* 0.3.2 */
6833 6834 6835 6836 6837 6838 6839
    .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 */
6840 6841
};

L
Laine Stump 已提交
6842
static virInterfaceDriver testInterfaceDriver = {
6843 6844 6845 6846 6847 6848
    .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 */
6849 6850 6851 6852 6853 6854
    .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 */
6855 6856 6857
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
6858 6859 6860
};


6861
static virStorageDriver testStorageDriver = {
6862 6863 6864 6865 6866 6867
    .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 */
6868 6869
    .connectStoragePoolEventRegisterAny = testConnectStoragePoolEventRegisterAny, /* 2.0.0 */
    .connectStoragePoolEventDeregisterAny = testConnectStoragePoolEventDeregisterAny, /* 2.0.0 */
6870 6871 6872
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
6873 6874
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
6875 6876
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
6877
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
6878 6879 6880 6881 6882 6883 6884
    .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 */
6885
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
6886 6887 6888
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

6889 6890 6891 6892 6893 6894 6895 6896 6897
    .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 */
6898 6899
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
6900 6901
};

6902
static virNodeDeviceDriver testNodeDeviceDriver = {
6903 6904
    .connectNodeDeviceEventRegisterAny = testConnectNodeDeviceEventRegisterAny, /* 2.2.0 */
    .connectNodeDeviceEventDeregisterAny = testConnectNodeDeviceEventDeregisterAny, /* 2.2.0 */
6905 6906 6907 6908 6909 6910 6911 6912 6913
    .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 */
6914 6915
};

6916 6917 6918 6919 6920 6921 6922 6923
static virConnectDriver testConnectDriver = {
    .hypervisorDriver = &testHypervisorDriver,
    .interfaceDriver = &testInterfaceDriver,
    .networkDriver = &testNetworkDriver,
    .nodeDeviceDriver = &testNodeDeviceDriver,
    .nwfilterDriver = NULL,
    .secretDriver = NULL,
    .storageDriver = &testStorageDriver,
6924 6925
};

6926 6927 6928 6929 6930 6931 6932 6933
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
6934 6935
    return virRegisterConnectDriver(&testConnectDriver,
                                    false);
6936
}