test_driver.c 201.2 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"
44
#include "network_conf.h"
L
Laine Stump 已提交
45
#include "interface_conf.h"
46
#include "domain_conf.h"
47
#include "domain_event.h"
48
#include "network_event.h"
49
#include "snapshot_conf.h"
50
#include "virfdstream.h"
C
Cole Robinson 已提交
51
#include "storage_conf.h"
J
John Ferlan 已提交
52
#include "virstorageobj.h"
53
#include "storage_event.h"
54
#include "node_device_conf.h"
J
John Ferlan 已提交
55
#include "virnodedeviceobj.h"
56
#include "node_device_event.h"
57
#include "virxml.h"
58
#include "virthread.h"
59
#include "virlog.h"
E
Eric Blake 已提交
60
#include "virfile.h"
61
#include "virtypedparam.h"
62
#include "virrandom.h"
63
#include "virstring.h"
64
#include "cpu/cpu.h"
65
#include "virauth.h"
66
#include "viratomic.h"
67
#include "virdomainobjlist.h"
J
John Ferlan 已提交
68
#include "virinterfaceobj.h"
69
#include "virhostcpu.h"
70

71 72
#define VIR_FROM_THIS VIR_FROM_TEST

73 74
VIR_LOG_INIT("test.test_driver");

75

76 77 78 79
#define MAX_CPUS 128

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

#define MAX_CELLS 128
88

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

96
struct _testDriver {
97
    virMutex lock;
98

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

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

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

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

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

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

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

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

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

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

    VIR_FREE(driver);
}
165

166

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

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

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

    virObjectEventStateQueue(driver->eventState, event);
}

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

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

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

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

    if (!nsdata)
        return;

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

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

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

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

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

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

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

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

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

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

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

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

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

298 299 300
    *data = nsdata;
    return 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

391
    return caps;
392

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

398

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

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

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

417
    if (!(ret->xmlopt = virDomainXMLOptionNew(NULL, NULL, &ns)) ||
418 419 420 421 422
        !(ret->eventState = virObjectEventStateNew()) ||
        !(ret->domains = virDomainObjListNew()) ||
        !(ret->networks = virNetworkObjListNew()))
        goto error;

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

    return ret;

 error:
    testDriverFree(ret);
    return NULL;
}


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

C
Cole Robinson 已提交
541

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

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

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

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

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

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

    return vm;
}

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

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

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

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

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

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

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

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

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

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

639
    return 0;
640 641
}

642

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

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

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

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

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

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

680

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            domobj->current_snapshot = snap;
        }
    }

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

    ret = 0;
890
 error:
891 892 893
    return ret;
}

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

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

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

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

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

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

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

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

950
        virObjectUnlock(obj);
951
    }
952

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        virNodeDeviceObjUnlock(obj);
    }

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

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

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

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

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

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

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

    ret = 0;
1220
 error:
1221 1222 1223
    VIR_FREE(nodes);
    return ret;
}
1224

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

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

    return 0;
 error:
    return -1;
}

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

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

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

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

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

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

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

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

1287
    return 0;
1288 1289

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

1297 1298 1299 1300 1301 1302 1303
/* Simultaneous test:///default connections should share the same
 * common state (among other things, this allows testing event
 * detection in one connection for an action caused in another).  */
static int
testOpenDefault(virConnectPtr conn)
{
    testDriverPtr privconn = NULL;
1304 1305
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
1306
    size_t i;
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323

    virMutexLock(&defaultLock);
    if (defaultConnections++) {
        conn->privateData = defaultConn;
        virMutexUnlock(&defaultLock);
        return VIR_DRV_OPEN_SUCCESS;
    }

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

    conn->privateData = privconn;

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

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

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

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

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

    defaultConn = privconn;

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

    return VIR_DRV_OPEN_SUCCESS;

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

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

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

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

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

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

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

    ret = 0;
1417
 cleanup:
1418 1419 1420 1421
    VIR_FREE(username);
    VIR_FREE(password);
    return ret;
}
1422

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

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

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

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

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

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

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

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

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

1464
    return VIR_DRV_OPEN_SUCCESS;
1465 1466
}

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

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

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

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

1489
    conn->privateData = NULL;
1490
    return 0;
1491 1492
}

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

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


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

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

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

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

1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
static char *
testConnectBaselineCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
                       const char **xmlCPUs,
                       unsigned int ncpus,
                       unsigned int flags)
{
    char *cpu;

    virCheckFlags(VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES, NULL);

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

    return cpu;
}

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

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

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
static char *
testConnectGetSysinfo(virConnectPtr conn ATTRIBUTE_UNUSED,
                      unsigned int flags)
{
    char *ret;
    const char *sysinfo = "<sysinfo type='smbios'>\n"
           "  <bios>\n"
           "    <entry name='vendor'>LENOVO</entry>\n"
           "    <entry name='version'>G4ETA1WW (2.61 )</entry>\n"
           "    <entry name='date'>05/07/2014</entry>\n"
           "    <entry name='release'>2.61</entry>\n"
           "  </bios>\n"
           "</sysinfo>\n";

    virCheckFlags(0, NULL);

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

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

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

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

1597
    return count;
1598 1599
}

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

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

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

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

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

    ret = obj->persistent;

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

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

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

1643 1644 1645
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1855 1856
    virCheckFlags(0, -1);

1857

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    virCheckFlags(0, -1);

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

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

1996
    virDomainObjEndAPI(&privdom);
1997 1998

    return 0;
1999 2000
}

2001 2002
#define TEST_SAVE_MAGIC "TestGuestMagic"

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

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

2022

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

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

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

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

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

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

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

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

2084
    ret = 0;
2085
 cleanup:
2086 2087 2088 2089
    VIR_FREE(xml);

    /* Don't report failure in close or unlink, because
     * in either case we're already in a failure scenario
Y
Yuri Chornoivan 已提交
2090
     * and have reported an earlier error */
2091
    if (ret != 0) {
2092
        VIR_FORCE_CLOSE(fd);
2093 2094
        unlink(path);
    }
2095
    virDomainObjEndAPI(&privdom);
2096
    testObjectEventQueue(privconn, event);
2097
    return ret;
2098 2099
}

2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
static int
testDomainSave(virDomainPtr domain,
               const char *path)
{
    return testDomainSaveFlags(domain, path, NULL, 0);
}

static int
testDomainRestoreFlags(virConnectPtr conn,
                       const char *path,
                       const char *dxml,
                       unsigned int flags)
2112
{
2113
    testDriverPtr privconn = conn->privateData;
2114
    char *xml = NULL;
2115
    char magic[15];
2116 2117 2118
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
2119
    virDomainObjPtr dom = NULL;
2120
    virObjectEventPtr event = NULL;
2121
    int ret = -1;
2122

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

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

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

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

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

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

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

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

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

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

2226

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

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

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

2255 2256 2257 2258 2259 2260 2261
    /* we don't support non-raw formats in test driver */
    if (dumpformat != VIR_DOMAIN_CORE_DUMP_FORMAT_RAW) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("kdump-compressed format is not supported here"));
        goto cleanup;
    }

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

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

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

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


static char *
testDomainGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED)
{
2293 2294 2295
    char *ret;

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

2299 2300 2301

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2447 2448
    ret = 0;

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

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

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

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

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

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

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

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

    hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
    maxcpu = maplen * 8;
    if (maxcpu > hostcpus)
        maxcpu = hostcpus;

2500 2501 2502 2503 2504
    if (!(allcpumap = virBitmapNew(hostcpus)))
        goto cleanup;

    virBitmapSetAll(allcpumap);

C
Cole Robinson 已提交
2505
    /* Clamp to actual number of vcpus */
2506 2507
    if (maxinfo > virDomainDefGetVcpus(privdom->def))
        maxinfo = virDomainDefGetVcpus(privdom->def);
C
Cole Robinson 已提交
2508

2509 2510
    memset(info, 0, sizeof(*info) * maxinfo);
    memset(cpumaps, 0, maxinfo * maplen);
C
Cole Robinson 已提交
2511

2512
    for (i = 0; i < maxinfo; i++) {
2513
        virDomainVcpuDefPtr vcpu = virDomainDefGetVcpu(def, i);
2514
        virBitmapPtr bitmap = NULL;
C
Cole Robinson 已提交
2515

2516 2517
        if (!vcpu->online)
            continue;
C
Cole Robinson 已提交
2518

2519 2520
        if (vcpu->cpumask)
            bitmap = vcpu->cpumask;
2521 2522 2523 2524
        else if (def->cpumask)
            bitmap = def->cpumask;
        else
            bitmap = allcpumap;
C
Cole Robinson 已提交
2525

2526 2527
        if (cpumaps)
            virBitmapToDataBuf(bitmap, VIR_GET_CPUMAP(cpumaps, maplen, i), maplen);
C
Cole Robinson 已提交
2528

2529 2530 2531
        info[i].number = i;
        info[i].state = VIR_VCPU_RUNNING;
        info[i].cpu = virBitmapLastSetBit(bitmap);
C
Cole Robinson 已提交
2532

2533 2534
        /* Fake an increasing cpu time value */
        info[i].cpuTime = statbase / 10;
C
Cole Robinson 已提交
2535 2536 2537
    }

    ret = maxinfo;
2538
 cleanup:
2539
    virBitmapFree(allcpumap);
2540
    virDomainObjEndAPI(&privdom);
C
Cole Robinson 已提交
2541 2542 2543
    return ret;
}

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

2554 2555
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
C
Cole Robinson 已提交
2556

2557 2558
    def = privdom->def;

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

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

2573 2574 2575
    virBitmapFree(vcpuinfo->cpumask);

    if (!(vcpuinfo->cpumask = virBitmapNewData(cpumap, maplen)))
2576
        goto cleanup;
C
Cole Robinson 已提交
2577 2578

    ret = 0;
2579

2580
 cleanup:
2581
    virDomainObjEndAPI(&privdom);
C
Cole Robinson 已提交
2582 2583 2584
    return ret;
}

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

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

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

2603 2604 2605
    ret = virDomainDefGetVcpuPinInfoHelper(def, maplen, ncpumaps, cpumaps,
                                           VIR_NODEINFO_MAXCPUS(driver->nodeInfo),
                                           NULL);
2606 2607 2608 2609 2610 2611

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

2612
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2613
{
2614
    testDriverPtr privconn = domain->conn->privateData;
2615
    virDomainDefPtr def;
2616
    virDomainObjPtr privdom;
2617 2618
    char *ret = NULL;

2619 2620
    /* Flags checked by virDomainDefFormat */

2621 2622
    if (!(privdom = testDomObjFromDomain(domain)))
        return NULL;
2623

2624 2625
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2626

2627 2628
    ret = virDomainDefFormat(def, privconn->caps,
                             virDomainDefFormatConvertXMLFlags(flags));
2629

2630
    virDomainObjEndAPI(&privdom);
2631
    return ret;
2632
}
2633

2634 2635
static int testConnectNumOfDefinedDomains(virConnectPtr conn)
{
2636
    testDriverPtr privconn = conn->privateData;
2637

2638
    return virDomainObjListNumOfDomains(privconn->domains, false, NULL, NULL);
2639 2640
}

2641 2642
static int testConnectListDefinedDomains(virConnectPtr conn,
                                         char **const names,
2643 2644
                                         int maxnames)
{
2645

2646
    testDriverPtr privconn = conn->privateData;
2647 2648

    memset(names, 0, sizeof(*names)*maxnames);
2649 2650
    return virDomainObjListGetInactiveNames(privconn->domains, names, maxnames,
                                            NULL, NULL);
2651 2652
}

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

    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);
2666

2667
    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
2668
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
2669

2670
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
2671
                                       NULL, parse_flags)) == NULL)
2672
        goto cleanup;
2673

2674 2675 2676
    if (virXMLCheckIllegalChars("name", def->name, "\n") < 0)
        goto cleanup;

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

2688
    event = virDomainEventLifecycleNewFromObj(dom,
2689
                                     VIR_DOMAIN_EVENT_DEFINED,
2690
                                     !oldDef ?
2691 2692
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2693

2694
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
2695

2696
 cleanup:
2697
    virDomainDefFree(def);
2698
    virDomainDefFree(oldDef);
2699
    if (dom)
2700
        virObjectUnlock(dom);
2701
    testObjectEventQueue(privconn, event);
2702
    return ret;
2703 2704
}

2705 2706 2707 2708 2709 2710
static virDomainPtr
testDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return testDomainDefineXMLFlags(conn, xml, 0);
}

2711 2712 2713 2714 2715 2716
static char *testDomainGetMetadata(virDomainPtr dom,
                                   int type,
                                   const char *uri,
                                   unsigned int flags)
{
    virDomainObjPtr privdom;
2717
    char *ret;
2718 2719 2720 2721

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, NULL);

2722 2723
    if (!(privdom = testDomObjFromDomain(dom)))
        return NULL;
2724

2725
    ret = virDomainObjGetMetadata(privdom, type, uri, flags);
2726

2727
    virDomainObjEndAPI(&privdom);
2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
    return ret;
}

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

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

2745 2746
    if (!(privdom = testDomObjFromDomain(dom)))
        return -1;
2747 2748 2749

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

2752 2753 2754 2755 2756 2757
    if (ret == 0) {
        virObjectEventPtr ev = NULL;
        ev = virDomainEventMetadataChangeNewFromObj(privdom, type, uri);
        testObjectEventQueue(privconn, ev);
    }

2758
    virDomainObjEndAPI(&privdom);
2759 2760 2761 2762
    return ret;
}


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

2772
    testDriverLock(privconn);
2773
    if (startCell >= privconn->numCells) {
2774 2775
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("Range exceeds available cells"));
2776
        goto cleanup;
2777 2778
    }

2779 2780 2781 2782
    for (cell = startCell, i = 0;
         (cell < privconn->numCells && i < maxCells);
         ++cell, ++i) {
        freemems[i] = privconn->cells[cell].mem;
2783
    }
2784
    ret = i;
2785

2786
 cleanup:
2787
    testDriverUnlock(privconn);
2788
    return ret;
2789 2790
}

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

2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853
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;
}

2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
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;
}

2878 2879
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
{
2880
    testDriverPtr privconn = domain->conn->privateData;
2881
    virDomainObjPtr privdom;
2882
    virObjectEventPtr event = NULL;
2883
    int ret = -1;
2884

2885 2886
    virCheckFlags(0, -1);

2887
    testDriverLock(privconn);
2888

2889
    if (!(privdom = testDomObjFromDomain(domain)))
2890
        goto cleanup;
2891

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

2898
    if (testDomainStartState(privconn, privdom,
J
Jiri Denemark 已提交
2899
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
2900 2901 2902
        goto cleanup;
    domain->id = privdom->def->id;

2903
    event = virDomainEventLifecycleNewFromObj(privdom,
2904 2905
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2906
    ret = 0;
2907

2908
 cleanup:
2909
    virDomainObjEndAPI(&privdom);
2910
    testObjectEventQueue(privconn, event);
2911
    testDriverUnlock(privconn);
2912
    return ret;
2913 2914
}

2915 2916
static int testDomainCreate(virDomainPtr domain)
{
2917 2918 2919
    return testDomainCreateWithFlags(domain, 0);
}

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

2929 2930
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
2931

2932

2933
    if (!(privdom = testDomObjFromDomain(domain)))
2934
        goto cleanup;
2935

C
Cole Robinson 已提交
2936 2937 2938 2939 2940 2941 2942 2943
    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;
    }

2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961
    /* 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. */
    }

2962
    event = virDomainEventLifecycleNewFromObj(privdom,
2963 2964
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
C
Cole Robinson 已提交
2965 2966
    privdom->hasManagedSave = false;

2967
    if (virDomainObjIsActive(privdom))
2968
        privdom->persistent = 0;
2969 2970
    else
        virDomainObjListRemove(privconn->domains, privdom);
2971

2972
    ret = 0;
2973

2974
 cleanup:
2975
    virDomainObjEndAPI(&privdom);
2976
    testObjectEventQueue(privconn, event);
2977
    return ret;
2978 2979
}

2980 2981 2982 2983 2984
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

2985 2986 2987
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2988 2989
    virDomainObjPtr privdom;

2990 2991
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2992

2993
    *autostart = privdom->autostart;
2994

2995
    virDomainObjEndAPI(&privdom);
2996
    return 0;
2997 2998 2999 3000 3001 3002
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
3003 3004
    virDomainObjPtr privdom;

3005 3006
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3007

3008
    privdom->autostart = autostart ? 1 : 0;
3009

3010
    virDomainObjEndAPI(&privdom);
3011
    return 0;
3012
}
3013

3014
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
3015 3016
                                        int *nparams)
{
3017 3018
    char *type = NULL;

3019 3020 3021
    if (nparams)
        *nparams = 1;

3022
    ignore_value(VIR_STRDUP(type, "fair"));
3023

3024 3025 3026
    return type;
}

3027
static int
3028 3029 3030 3031
testDomainGetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int *nparams,
                                      unsigned int flags)
3032
{
3033
    virDomainObjPtr privdom;
3034
    int ret = -1;
3035

3036 3037
    virCheckFlags(0, -1);

3038 3039
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3040

3041 3042
    if (virTypedParameterAssign(params, VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, 50) < 0)
3043
        goto cleanup;
3044 3045
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
3046 3047

    *nparams = 1;
3048 3049
    ret = 0;

3050
 cleanup:
3051
    virDomainObjEndAPI(&privdom);
3052
    return ret;
3053
}
3054

3055
static int
3056 3057 3058
testDomainGetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int *nparams)
3059
{
3060
    return testDomainGetSchedulerParametersFlags(domain, params, nparams, 0);
3061
}
3062

3063
static int
3064 3065 3066 3067
testDomainSetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int nparams,
                                      unsigned int flags)
3068
{
3069
    virDomainObjPtr privdom;
3070 3071
    int ret = -1;
    size_t i;
3072

3073
    virCheckFlags(0, -1);
3074 3075 3076 3077
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_SCHEDULER_WEIGHT,
                               VIR_TYPED_PARAM_UINT,
                               NULL) < 0)
3078
        return -1;
3079

3080 3081
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3082

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

3090 3091
    ret = 0;

3092
    virDomainObjEndAPI(&privdom);
3093
    return ret;
3094 3095
}

3096
static int
3097 3098 3099
testDomainSetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int nparams)
3100
{
3101
    return testDomainSetSchedulerParametersFlags(domain, params, nparams, 0);
3102 3103
}

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

3113 3114 3115 3116 3117 3118
    if (!*path) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("summary statistics are not supported yet"));
        return ret;
    }

3119 3120
    if (!(privdom = testDomObjFromDomain(domain)))
        return ret;
3121

3122 3123 3124 3125 3126 3127
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto error;
    }

3128
    if (virDomainDiskIndexByName(privdom->def, path, false) < 0) {
3129 3130
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path: %s"), path);
3131 3132 3133 3134
        goto error;
    }

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

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

3164 3165
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3166

3167 3168 3169 3170 3171 3172
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto error;
    }

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

    if (!found) {
3182 3183
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path, '%s' is not a known interface"), path);
3184 3185 3186 3187
        goto error;
    }

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

3210

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

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

    return net;
}


3229 3230
static virNetworkPtr testNetworkLookupByUUID(virConnectPtr conn,
                                             const unsigned char *uuid)
3231
{
3232
    testDriverPtr privconn = conn->privateData;
3233
    virNetworkObjPtr net;
3234
    virNetworkPtr ret = NULL;
3235

3236
    if (!(net = testNetworkObjFindByUUID(privconn, uuid)))
3237
        goto cleanup;
3238

3239 3240
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

3241
 cleanup:
3242
    virNetworkObjEndAPI(&net);
3243
    return ret;
3244
}
3245

3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261

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

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

    return net;
}


3262
static virNetworkPtr testNetworkLookupByName(virConnectPtr conn,
3263
                                             const char *name)
3264
{
3265
    testDriverPtr privconn = conn->privateData;
3266 3267
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
3268

3269
    if (!(net = testNetworkObjFindByName(privconn, name)))
3270
        goto cleanup;
3271

3272 3273
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

3274
 cleanup:
3275
    virNetworkObjEndAPI(&net);
3276
    return ret;
3277 3278 3279
}


3280 3281
static int testConnectNumOfNetworks(virConnectPtr conn)
{
3282
    testDriverPtr privconn = conn->privateData;
3283
    int numActive;
3284

3285 3286
    numActive = virNetworkObjListNumOfNetworks(privconn->networks,
                                               true, NULL, conn);
3287
    return numActive;
3288 3289
}

3290
static int testConnectListNetworks(virConnectPtr conn, char **const names, int nnames) {
3291
    testDriverPtr privconn = conn->privateData;
3292
    int n;
3293

3294 3295
    n = virNetworkObjListGetNames(privconn->networks,
                                  true, names, nnames, NULL, conn);
3296
    return n;
3297 3298
}

3299 3300
static int testConnectNumOfDefinedNetworks(virConnectPtr conn)
{
3301
    testDriverPtr privconn = conn->privateData;
3302
    int numInactive;
3303

3304 3305
    numInactive = virNetworkObjListNumOfNetworks(privconn->networks,
                                                 false, NULL, conn);
3306
    return numInactive;
3307 3308
}

3309
static int testConnectListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
3310
    testDriverPtr privconn = conn->privateData;
3311
    int n;
3312

3313 3314
    n = virNetworkObjListGetNames(privconn->networks,
                                  false, names, nnames, NULL, conn);
3315
    return n;
3316 3317
}

3318
static int
3319
testConnectListAllNetworks(virConnectPtr conn,
3320 3321 3322
                           virNetworkPtr **nets,
                           unsigned int flags)
{
3323
    testDriverPtr privconn = conn->privateData;
3324 3325 3326

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

3327
    return virNetworkObjListExport(conn, privconn->networks, nets, NULL, flags);
3328
}
3329 3330 3331

static int testNetworkIsActive(virNetworkPtr net)
{
3332
    testDriverPtr privconn = net->conn->privateData;
3333 3334 3335
    virNetworkObjPtr obj;
    int ret = -1;

3336
    if (!(obj = testNetworkObjFindByUUID(privconn, net->uuid)))
3337
        goto cleanup;
3338

3339 3340
    ret = virNetworkObjIsActive(obj);

3341
 cleanup:
3342
    virNetworkObjEndAPI(&obj);
3343 3344 3345 3346 3347
    return ret;
}

static int testNetworkIsPersistent(virNetworkPtr net)
{
3348
    testDriverPtr privconn = net->conn->privateData;
3349 3350 3351
    virNetworkObjPtr obj;
    int ret = -1;

3352
    if (!(obj = testNetworkObjFindByUUID(privconn, net->uuid)))
3353
        goto cleanup;
3354

3355 3356
    ret = obj->persistent;

3357
 cleanup:
3358
    virNetworkObjEndAPI(&obj);
3359 3360 3361 3362
    return ret;
}


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

3371
    if ((def = virNetworkDefParseString(xml)) == NULL)
3372
        goto cleanup;
3373

3374 3375 3376
    if (!(net = virNetworkAssignDef(privconn->networks, def,
                                    VIR_NETWORK_OBJ_LIST_ADD_LIVE |
                                    VIR_NETWORK_OBJ_LIST_ADD_CHECK_LIVE)))
3377 3378
        goto cleanup;
    def = NULL;
3379
    net->active = 1;
3380

3381
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3382 3383
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3384

3385
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3386

3387
 cleanup:
3388
    virNetworkDefFree(def);
3389
    testObjectEventQueue(privconn, event);
3390
    virNetworkObjEndAPI(&net);
3391
    return ret;
3392 3393
}

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

3403
    if ((def = virNetworkDefParseString(xml)) == NULL)
3404
        goto cleanup;
3405

3406
    if (!(net = virNetworkAssignDef(privconn->networks, def, 0)))
3407 3408
        goto cleanup;
    def = NULL;
3409

3410
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3411 3412
                                        VIR_NETWORK_EVENT_DEFINED,
                                        0);
3413

3414
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3415

3416
 cleanup:
3417
    virNetworkDefFree(def);
3418
    testObjectEventQueue(privconn, event);
3419
    virNetworkObjEndAPI(&net);
3420
    return ret;
3421 3422
}

3423 3424
static int testNetworkUndefine(virNetworkPtr network)
{
3425
    testDriverPtr privconn = network->conn->privateData;
3426
    virNetworkObjPtr privnet;
3427
    int ret = -1;
3428
    virObjectEventPtr event = NULL;
3429

3430
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3431
        goto cleanup;
3432

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

3439
    event = virNetworkEventLifecycleNew(network->name, network->uuid,
3440 3441
                                        VIR_NETWORK_EVENT_UNDEFINED,
                                        0);
3442

3443
    virNetworkRemoveInactive(privconn->networks, privnet);
3444
    ret = 0;
3445

3446
 cleanup:
3447
    testObjectEventQueue(privconn, event);
3448
    virNetworkObjEndAPI(&privnet);
3449
    return ret;
3450 3451
}

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

    virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE |
                  VIR_NETWORK_UPDATE_AFFECT_CONFIG,
                  -1);

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

    /* VIR_NETWORK_UPDATE_AFFECT_CURRENT means "change LIVE if network
     * is active, else change CONFIG
    */
    isActive = virNetworkObjIsActive(network);
    if ((flags & (VIR_NETWORK_UPDATE_AFFECT_LIVE
                   | VIR_NETWORK_UPDATE_AFFECT_CONFIG)) ==
        VIR_NETWORK_UPDATE_AFFECT_CURRENT) {
        if (isActive)
            flags |= VIR_NETWORK_UPDATE_AFFECT_LIVE;
        else
            flags |= VIR_NETWORK_UPDATE_AFFECT_CONFIG;
    }

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

    ret = 0;
3489
 cleanup:
3490
    virNetworkObjEndAPI(&network);
3491 3492 3493
    return ret;
}

3494 3495
static int testNetworkCreate(virNetworkPtr network)
{
3496
    testDriverPtr privconn = network->conn->privateData;
3497
    virNetworkObjPtr privnet;
3498
    int ret = -1;
3499
    virObjectEventPtr event = NULL;
3500

3501
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3502
        goto cleanup;
3503

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

3510
    privnet->active = 1;
3511
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3512 3513
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3514
    ret = 0;
3515

3516
 cleanup:
3517
    testObjectEventQueue(privconn, event);
3518
    virNetworkObjEndAPI(&privnet);
3519
    return ret;
3520 3521
}

3522 3523
static int testNetworkDestroy(virNetworkPtr network)
{
3524
    testDriverPtr privconn = network->conn->privateData;
3525
    virNetworkObjPtr privnet;
3526
    int ret = -1;
3527
    virObjectEventPtr event = NULL;
3528

3529
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3530
        goto cleanup;
3531

3532
    privnet->active = 0;
3533
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3534 3535
                                        VIR_NETWORK_EVENT_STOPPED,
                                        0);
3536
    if (!privnet->persistent)
3537
        virNetworkRemoveInactive(privconn->networks, privnet);
3538

3539 3540
    ret = 0;

3541
 cleanup:
3542
    testObjectEventQueue(privconn, event);
3543
    virNetworkObjEndAPI(&privnet);
3544
    return ret;
3545 3546
}

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

E
Eric Blake 已提交
3554 3555
    virCheckFlags(0, NULL);

3556
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3557
        goto cleanup;
3558

3559
    ret = virNetworkDefFormat(privnet->def, flags);
3560

3561
 cleanup:
3562
    virNetworkObjEndAPI(&privnet);
3563
    return ret;
3564 3565 3566
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3567
    testDriverPtr privconn = network->conn->privateData;
3568
    char *bridge = NULL;
3569 3570
    virNetworkObjPtr privnet;

3571
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3572
        goto cleanup;
3573

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

3581
    ignore_value(VIR_STRDUP(bridge, privnet->def->bridge));
3582

3583
 cleanup:
3584
    virNetworkObjEndAPI(&privnet);
3585 3586 3587 3588
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
3589 3590
                                   int *autostart)
{
3591
    testDriverPtr privconn = network->conn->privateData;
3592
    virNetworkObjPtr privnet;
3593
    int ret = -1;
3594

3595
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3596
        goto cleanup;
3597

3598
    *autostart = privnet->autostart;
3599 3600
    ret = 0;

3601
 cleanup:
3602
    virNetworkObjEndAPI(&privnet);
3603
    return ret;
3604 3605 3606
}

static int testNetworkSetAutostart(virNetworkPtr network,
3607 3608
                                   int autostart)
{
3609
    testDriverPtr privconn = network->conn->privateData;
3610
    virNetworkObjPtr privnet;
3611
    int ret = -1;
3612

3613
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3614
        goto cleanup;
3615

3616
    privnet->autostart = autostart ? 1 : 0;
3617 3618
    ret = 0;

3619
 cleanup:
3620
    virNetworkObjEndAPI(&privnet);
3621
    return ret;
3622
}
3623

C
Cole Robinson 已提交
3624

L
Laine Stump 已提交
3625 3626 3627 3628 3629
/*
 * Physical host interface routines
 */


3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648
static virInterfaceObjPtr
testInterfaceObjFindByName(testDriverPtr privconn,
                           const char *name)
{
    virInterfaceObjPtr iface;

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

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

    return iface;
}


3649
static int testConnectNumOfInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3650
{
3651
    testDriverPtr privconn = conn->privateData;
3652
    int ninterfaces;
L
Laine Stump 已提交
3653 3654

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

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

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
3668
    for (i = 0; (i < privconn->ifaces.count) && (n < nnames); i++) {
L
Laine Stump 已提交
3669
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3670
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
3671
            if (VIR_STRDUP(names[n++], privconn->ifaces.objs[i]->def->name) < 0) {
L
Laine Stump 已提交
3672
                virInterfaceObjUnlock(privconn->ifaces.objs[i]);
3673
                goto error;
L
Laine Stump 已提交
3674 3675 3676 3677 3678 3679 3680 3681
            }
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);

    return n;

3682
 error:
3683
    for (n = 0; n < nnames; n++)
L
Laine Stump 已提交
3684 3685 3686 3687 3688
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

3689
static int testConnectNumOfDefinedInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3690
{
3691
    testDriverPtr privconn = conn->privateData;
3692
    int ninterfaces;
L
Laine Stump 已提交
3693 3694

    testDriverLock(privconn);
3695
    ninterfaces = virInterfaceObjNumOfInterfaces(&privconn->ifaces, false);
L
Laine Stump 已提交
3696
    testDriverUnlock(privconn);
3697
    return ninterfaces;
L
Laine Stump 已提交
3698 3699
}

3700
static int testConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
L
Laine Stump 已提交
3701
{
3702
    testDriverPtr privconn = conn->privateData;
3703 3704
    int n = 0;
    size_t i;
L
Laine Stump 已提交
3705 3706 3707

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
3708
    for (i = 0; (i < privconn->ifaces.count) && (n < nnames); i++) {
L
Laine Stump 已提交
3709
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3710
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
3711
            if (VIR_STRDUP(names[n++], privconn->ifaces.objs[i]->def->name) < 0) {
L
Laine Stump 已提交
3712
                virInterfaceObjUnlock(privconn->ifaces.objs[i]);
3713
                goto error;
L
Laine Stump 已提交
3714 3715 3716 3717 3718 3719 3720 3721
            }
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);

    return n;

3722
 error:
3723
    for (n = 0; n < nnames; n++)
L
Laine Stump 已提交
3724 3725 3726 3727 3728
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

3729
static virInterfacePtr testInterfaceLookupByName(virConnectPtr conn,
L
Laine Stump 已提交
3730 3731
                                                 const char *name)
{
3732
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3733 3734 3735
    virInterfaceObjPtr iface;
    virInterfacePtr ret = NULL;

3736
    if (!(iface = testInterfaceObjFindByName(privconn, name)))
L
Laine Stump 已提交
3737 3738 3739 3740
        goto cleanup;

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

3741
 cleanup:
L
Laine Stump 已提交
3742 3743 3744 3745 3746
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

3747
static virInterfacePtr testInterfaceLookupByMACString(virConnectPtr conn,
L
Laine Stump 已提交
3748 3749
                                                      const char *mac)
{
3750
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3751 3752 3753 3754 3755
    virInterfaceObjPtr iface;
    int ifacect;
    virInterfacePtr ret = NULL;

    testDriverLock(privconn);
3756
    ifacect = virInterfaceObjFindByMACString(&privconn->ifaces, mac, &iface, 1);
L
Laine Stump 已提交
3757 3758 3759
    testDriverUnlock(privconn);

    if (ifacect == 0) {
3760
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3761 3762 3763 3764
        goto cleanup;
    }

    if (ifacect > 1) {
3765
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3766 3767 3768 3769 3770
        goto cleanup;
    }

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

3771
 cleanup:
L
Laine Stump 已提交
3772 3773 3774 3775 3776
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

3777 3778
static int testInterfaceIsActive(virInterfacePtr iface)
{
3779
    testDriverPtr privconn = iface->conn->privateData;
3780 3781 3782
    virInterfaceObjPtr obj;
    int ret = -1;

3783
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3784
        goto cleanup;
3785

3786 3787
    ret = virInterfaceObjIsActive(obj);

3788
 cleanup:
3789 3790 3791 3792 3793
    if (obj)
        virInterfaceObjUnlock(obj);
    return ret;
}

3794
static int testInterfaceChangeBegin(virConnectPtr conn,
E
Eric Blake 已提交
3795
                                    unsigned int flags)
3796
{
3797
    testDriverPtr privconn = conn->privateData;
3798 3799
    int ret = -1;

E
Eric Blake 已提交
3800 3801
    virCheckFlags(0, -1);

3802 3803
    testDriverLock(privconn);
    if (privconn->transaction_running) {
3804
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3805
                       _("there is another transaction running."));
3806 3807 3808 3809 3810 3811 3812 3813 3814 3815
        goto cleanup;
    }

    privconn->transaction_running = true;

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

    ret = 0;
3816
 cleanup:
3817 3818 3819 3820 3821
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceChangeCommit(virConnectPtr conn,
E
Eric Blake 已提交
3822
                                     unsigned int flags)
3823
{
3824
    testDriverPtr privconn = conn->privateData;
3825 3826
    int ret = -1;

E
Eric Blake 已提交
3827 3828
    virCheckFlags(0, -1);

3829 3830 3831
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3832
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3833 3834
                       _("no transaction running, "
                         "nothing to be committed."));
3835 3836 3837 3838 3839 3840 3841 3842
        goto cleanup;
    }

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

    ret = 0;

3843
 cleanup:
3844 3845 3846 3847 3848 3849
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
E
Eric Blake 已提交
3850
                                       unsigned int flags)
3851
{
3852
    testDriverPtr privconn = conn->privateData;
3853 3854
    int ret = -1;

E
Eric Blake 已提交
3855 3856
    virCheckFlags(0, -1);

3857 3858 3859
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3860
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3861 3862
                       _("no transaction running, "
                         "nothing to rollback."));
3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875
        goto cleanup;
    }

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

    privconn->transaction_running = false;

    ret = 0;

3876
 cleanup:
3877 3878 3879
    testDriverUnlock(privconn);
    return ret;
}
3880

L
Laine Stump 已提交
3881
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
E
Eric Blake 已提交
3882
                                     unsigned int flags)
L
Laine Stump 已提交
3883
{
3884
    testDriverPtr privconn = iface->conn->privateData;
L
Laine Stump 已提交
3885 3886 3887
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

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

3890
    if (!(privinterface = testInterfaceObjFindByName(privconn, iface->name)))
L
Laine Stump 已提交
3891 3892
        goto cleanup;

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

3895
 cleanup:
L
Laine Stump 已提交
3896 3897 3898 3899 3900 3901 3902
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    return ret;
}


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
E
Eric Blake 已提交
3903
                                              unsigned int flags)
L
Laine Stump 已提交
3904
{
3905
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3906 3907 3908 3909
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
3910 3911
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3912
    testDriverLock(privconn);
3913
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3914 3915
        goto cleanup;

3916
    if ((iface = virInterfaceObjAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3917 3918 3919 3920 3921
        goto cleanup;
    def = NULL;

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

3922
 cleanup:
L
Laine Stump 已提交
3923 3924 3925 3926 3927 3928 3929 3930 3931
    virInterfaceDefFree(def);
    if (iface)
        virInterfaceObjUnlock(iface);
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceUndefine(virInterfacePtr iface)
{
3932
    testDriverPtr privconn = iface->conn->privateData;
L
Laine Stump 已提交
3933 3934 3935
    virInterfaceObjPtr privinterface;
    int ret = -1;

3936
    if (!(privinterface = testInterfaceObjFindByName(privconn, iface->name)))
L
Laine Stump 已提交
3937 3938
        goto cleanup;

3939
    virInterfaceObjRemove(&privconn->ifaces, privinterface);
L
Laine Stump 已提交
3940 3941
    ret = 0;

3942
 cleanup:
L
Laine Stump 已提交
3943 3944 3945 3946 3947
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
E
Eric Blake 已提交
3948
                               unsigned int flags)
L
Laine Stump 已提交
3949
{
3950
    testDriverPtr privconn = iface->conn->privateData;
L
Laine Stump 已提交
3951 3952 3953
    virInterfaceObjPtr privinterface;
    int ret = -1;

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

3956
    if (!(privinterface = testInterfaceObjFindByName(privconn, iface->name)))
L
Laine Stump 已提交
3957 3958 3959
        goto cleanup;

    if (privinterface->active != 0) {
3960
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3961 3962 3963 3964 3965 3966
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

3967
 cleanup:
L
Laine Stump 已提交
3968 3969 3970 3971 3972 3973 3974
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceDestroy(virInterfacePtr iface,
E
Eric Blake 已提交
3975
                                unsigned int flags)
L
Laine Stump 已提交
3976
{
3977
    testDriverPtr privconn = iface->conn->privateData;
L
Laine Stump 已提交
3978 3979 3980
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3981 3982
    virCheckFlags(0, -1);

3983
    if (!(privinterface = testInterfaceObjFindByName(privconn, iface->name)))
L
Laine Stump 已提交
3984 3985 3986
        goto cleanup;

    if (privinterface->active == 0) {
3987
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3988 3989 3990 3991 3992 3993
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

3994
 cleanup:
L
Laine Stump 已提交
3995 3996 3997 3998 3999 4000 4001 4002
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}



C
Cole Robinson 已提交
4003 4004 4005 4006
/*
 * Storage Driver routines
 */

4007

4008 4009
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool)
{
C
Cole Robinson 已提交
4010 4011 4012 4013 4014

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

4015
    return VIR_STRDUP(pool->configFile, "");
C
Cole Robinson 已提交
4016 4017
}

4018

4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037
static virStoragePoolObjPtr
testStoragePoolObjFindByName(testDriverPtr privconn,
                             const char *name)
{
    virStoragePoolObjPtr pool;

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

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

    return pool;
}


4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059
static virStoragePoolObjPtr
testStoragePoolObjFindByUUID(testDriverPtr privconn,
                             const unsigned char *uuid)
{
    virStoragePoolObjPtr pool;
    char uuidstr[VIR_UUID_STRING_BUFLEN];

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

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

    return pool;
}


C
Cole Robinson 已提交
4060 4061
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
4062 4063
                            const unsigned char *uuid)
{
4064
    testDriverPtr privconn = conn->privateData;
4065 4066
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4067

4068
    if (!(pool = testStoragePoolObjFindByUUID(privconn, uuid)))
4069
        goto cleanup;
C
Cole Robinson 已提交
4070

4071 4072
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4073

4074
 cleanup:
4075 4076
    if (pool)
        virStoragePoolObjUnlock(pool);
4077
    return ret;
C
Cole Robinson 已提交
4078 4079 4080 4081
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
4082 4083
                            const char *name)
{
4084
    testDriverPtr privconn = conn->privateData;
4085 4086
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4087

4088
    if (!(pool = testStoragePoolObjFindByName(privconn, name)))
4089
        goto cleanup;
C
Cole Robinson 已提交
4090

4091 4092
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4093

4094
 cleanup:
4095 4096
    if (pool)
        virStoragePoolObjUnlock(pool);
4097
    return ret;
C
Cole Robinson 已提交
4098 4099 4100
}

static virStoragePoolPtr
4101 4102
testStoragePoolLookupByVolume(virStorageVolPtr vol)
{
C
Cole Robinson 已提交
4103 4104 4105 4106
    return testStoragePoolLookupByName(vol->conn, vol->pool);
}

static int
4107 4108
testConnectNumOfStoragePools(virConnectPtr conn)
{
4109
    testDriverPtr privconn = conn->privateData;
4110 4111
    int numActive = 0;
    size_t i;
C
Cole Robinson 已提交
4112

4113
    testDriverLock(privconn);
4114
    for (i = 0; i < privconn->pools.count; i++)
C
Cole Robinson 已提交
4115 4116
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
4117
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4118 4119 4120 4121 4122

    return numActive;
}

static int
4123 4124
testConnectListStoragePools(virConnectPtr conn,
                            char **const names,
4125 4126
                            int nnames)
{
4127
    testDriverPtr privconn = conn->privateData;
4128 4129
    int n = 0;
    size_t i;
C
Cole Robinson 已提交
4130

4131
    testDriverLock(privconn);
C
Cole Robinson 已提交
4132
    memset(names, 0, sizeof(*names)*nnames);
4133
    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
4134
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4135
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
4136
            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
4137
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
4138
            goto error;
4139 4140 4141 4142
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4143 4144 4145

    return n;

4146
 error:
4147
    for (n = 0; n < nnames; n++)
C
Cole Robinson 已提交
4148
        VIR_FREE(names[n]);
4149
    testDriverUnlock(privconn);
4150
    return -1;
C
Cole Robinson 已提交
4151 4152 4153
}

static int
4154 4155
testConnectNumOfDefinedStoragePools(virConnectPtr conn)
{
4156
    testDriverPtr privconn = conn->privateData;
4157 4158
    int numInactive = 0;
    size_t i;
C
Cole Robinson 已提交
4159

4160
    testDriverLock(privconn);
4161
    for (i = 0; i < privconn->pools.count; i++) {
4162
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4163 4164
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
4165 4166 4167
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4168 4169 4170 4171 4172

    return numInactive;
}

static int
4173 4174
testConnectListDefinedStoragePools(virConnectPtr conn,
                                   char **const names,
4175 4176
                                   int nnames)
{
4177
    testDriverPtr privconn = conn->privateData;
4178 4179
    int n = 0;
    size_t i;
C
Cole Robinson 已提交
4180

4181
    testDriverLock(privconn);
C
Cole Robinson 已提交
4182
    memset(names, 0, sizeof(*names)*nnames);
4183
    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
4184
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4185
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
4186
            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
4187
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
4188
            goto error;
4189 4190 4191 4192
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4193 4194 4195

    return n;

4196
 error:
4197
    for (n = 0; n < nnames; n++)
C
Cole Robinson 已提交
4198
        VIR_FREE(names[n]);
4199
    testDriverUnlock(privconn);
4200
    return -1;
C
Cole Robinson 已提交
4201 4202
}

4203
static int
4204 4205 4206
testConnectListAllStoragePools(virConnectPtr conn,
                               virStoragePoolPtr **pools,
                               unsigned int flags)
4207
{
4208
    testDriverPtr privconn = conn->privateData;
4209 4210 4211 4212 4213
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

    testDriverLock(privconn);
4214 4215
    ret = virStoragePoolObjListExport(conn, privconn->pools, pools,
                                      NULL, flags);
4216 4217 4218 4219
    testDriverUnlock(privconn);

    return ret;
}
C
Cole Robinson 已提交
4220

4221 4222
static int testStoragePoolIsActive(virStoragePoolPtr pool)
{
4223
    testDriverPtr privconn = pool->conn->privateData;
4224 4225 4226
    virStoragePoolObjPtr obj;
    int ret = -1;

4227
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4228
        goto cleanup;
4229

4230 4231
    ret = virStoragePoolObjIsActive(obj);

4232
 cleanup:
4233 4234 4235 4236 4237 4238 4239
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}

static int testStoragePoolIsPersistent(virStoragePoolPtr pool)
{
4240
    testDriverPtr privconn = pool->conn->privateData;
4241 4242 4243
    virStoragePoolObjPtr obj;
    int ret = -1;

4244
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4245
        goto cleanup;
4246

4247 4248
    ret = obj->configFile ? 1 : 0;

4249
 cleanup:
4250 4251 4252 4253 4254 4255 4256
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}



C
Cole Robinson 已提交
4257
static int
4258 4259
testStoragePoolCreate(virStoragePoolPtr pool,
                      unsigned int flags)
E
Eric Blake 已提交
4260
{
4261
    testDriverPtr privconn = pool->conn->privateData;
4262
    virStoragePoolObjPtr privpool;
4263
    int ret = -1;
4264
    virObjectEventPtr event = NULL;
4265

E
Eric Blake 已提交
4266 4267
    virCheckFlags(0, -1);

4268
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4269
        goto cleanup;
4270

4271
    if (virStoragePoolObjIsActive(privpool)) {
4272 4273
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4274 4275
        goto cleanup;
    }
C
Cole Robinson 已提交
4276 4277

    privpool->active = 1;
4278 4279 4280 4281

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

4284
 cleanup:
4285
    testObjectEventQueue(privconn, event);
4286 4287
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4288
    return ret;
C
Cole Robinson 已提交
4289 4290 4291
}

static char *
4292 4293 4294 4295
testConnectFindStoragePoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type,
                                  const char *srcSpec,
                                  unsigned int flags)
C
Cole Robinson 已提交
4296
{
4297 4298 4299 4300
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4301 4302
    virCheckFlags(0, NULL);

4303 4304
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4305 4306
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
4307 4308 4309 4310
        goto cleanup;
    }

    if (srcSpec) {
4311
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4312 4313 4314 4315 4316 4317 4318
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
4319
        ignore_value(VIR_STRDUP(ret, defaultPoolSourcesLogicalXML));
4320 4321 4322
        break;

    case VIR_STORAGE_POOL_NETFS:
4323
        if (!source || !source->hosts[0].name) {
4324 4325
            virReportError(VIR_ERR_INVALID_ARG,
                           "%s", _("hostname must be specified for netfs sources"));
4326 4327 4328
            goto cleanup;
        }

4329 4330
        ignore_value(virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                                 source->hosts[0].name));
4331 4332 4333
        break;

    default:
4334 4335
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4336 4337
    }

4338
 cleanup:
4339 4340
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4341 4342 4343
}


4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371
static virNodeDeviceObjPtr
testNodeDeviceMockCreateVport(testDriverPtr driver,
                              const char *wwnn,
                              const char *wwpn);
static int
testCreateVport(testDriverPtr driver,
                const char *wwnn,
                const char *wwpn)
{
    virNodeDeviceObjPtr obj = NULL;
    /* The storage_backend_scsi createVport() will use the input adapter
     * fields parent name, parent_wwnn/parent_wwpn, or parent_fabric_wwn
     * in order to determine whether the provided parent can be used to
     * create a vHBA or will find "an available vport capable" to create
     * a vHBA. In order to do this, it uses the virVHBA* API's which traverse
     * the sysfs looking at various fields (rather than going via nodedev).
     *
     * Since the test environ doesn't have the sysfs for the storage pool
     * test, at least for now use the node device test infrastructure to
     * create the vHBA. In the long run the result is the same. */
    if (!(obj = testNodeDeviceMockCreateVport(driver, wwnn, wwpn)))
        return -1;
    virNodeDeviceObjUnlock(obj);

    return 0;
}


C
Cole Robinson 已提交
4372
static virStoragePoolPtr
4373 4374 4375
testStoragePoolCreateXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4376
{
4377
    testDriverPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4378
    virStoragePoolDefPtr def;
4379
    virStoragePoolObjPtr pool = NULL;
4380
    virStoragePoolPtr ret = NULL;
4381
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
4382

E
Eric Blake 已提交
4383 4384
    virCheckFlags(0, NULL);

4385
    testDriverLock(privconn);
4386
    if (!(def = virStoragePoolDefParseString(xml)))
4387
        goto cleanup;
C
Cole Robinson 已提交
4388

4389 4390 4391 4392
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4393 4394
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("storage pool already exists"));
4395
        goto cleanup;
C
Cole Robinson 已提交
4396 4397
    }

4398
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4399
        goto cleanup;
4400
    def = NULL;
C
Cole Robinson 已提交
4401

4402
    if (pool->def->source.adapter.type == VIR_STORAGE_ADAPTER_TYPE_FC_HOST) {
4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415
        /* In the real code, we'd call virVHBAManageVport followed by
         * find_new_device, but we cannot do that here since we're not
         * mocking udev. The mock routine will copy an existing vHBA and
         * rename a few fields to mock that. */
        if (testCreateVport(privconn,
                            pool->def->source.adapter.data.fchost.wwnn,
                            pool->def->source.adapter.data.fchost.wwpn) < 0) {
            virStoragePoolObjRemove(&privconn->pools, pool);
            pool = NULL;
            goto cleanup;
        }
    }

4416
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4417
        virStoragePoolObjRemove(&privconn->pools, pool);
4418 4419
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4420
    }
4421 4422 4423 4424 4425 4426

    /* *SetDefaults fills this in for the persistent pools, but this
     * would be a transient pool so remove it; otherwise, the Destroy
     * code will not Remove the pool */
    VIR_FREE(pool->configFile);

C
Cole Robinson 已提交
4427 4428
    pool->active = 1;

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

4433 4434
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4435

4436
 cleanup:
4437
    virStoragePoolDefFree(def);
4438
    testObjectEventQueue(privconn, event);
4439 4440 4441
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4442
    return ret;
C
Cole Robinson 已提交
4443 4444 4445
}

static virStoragePoolPtr
4446 4447 4448
testStoragePoolDefineXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4449
{
4450
    testDriverPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4451
    virStoragePoolDefPtr def;
4452
    virStoragePoolObjPtr pool = NULL;
4453
    virStoragePoolPtr ret = NULL;
4454
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
4455

E
Eric Blake 已提交
4456 4457
    virCheckFlags(0, NULL);

4458
    testDriverLock(privconn);
4459
    if (!(def = virStoragePoolDefParseString(xml)))
4460
        goto cleanup;
C
Cole Robinson 已提交
4461 4462 4463 4464 4465

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

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

4470 4471 4472 4473
    event = virStoragePoolEventLifecycleNew(pool->def->name, pool->def->uuid,
                                            VIR_STORAGE_POOL_EVENT_DEFINED,
                                            0);

4474
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4475
        virStoragePoolObjRemove(&privconn->pools, pool);
4476 4477
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4478 4479
    }

4480 4481
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4482

4483
 cleanup:
4484
    virStoragePoolDefFree(def);
4485
    testObjectEventQueue(privconn, event);
4486 4487 4488
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4489
    return ret;
C
Cole Robinson 已提交
4490 4491 4492
}

static int
4493 4494
testStoragePoolUndefine(virStoragePoolPtr pool)
{
4495
    testDriverPtr privconn = pool->conn->privateData;
4496
    virStoragePoolObjPtr privpool;
4497
    int ret = -1;
4498
    virObjectEventPtr event = NULL;
4499

4500
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4501
        goto cleanup;
4502

4503
    if (virStoragePoolObjIsActive(privpool)) {
4504 4505
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4506 4507
        goto cleanup;
    }
C
Cole Robinson 已提交
4508

4509 4510 4511 4512
    event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid,
                                            VIR_STORAGE_POOL_EVENT_UNDEFINED,
                                            0);

C
Cole Robinson 已提交
4513
    virStoragePoolObjRemove(&privconn->pools, privpool);
4514
    privpool = NULL;
4515
    ret = 0;
C
Cole Robinson 已提交
4516

4517
 cleanup:
4518 4519
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4520
    testObjectEventQueue(privconn, event);
4521
    testDriverUnlock(privconn);
4522
    return ret;
C
Cole Robinson 已提交
4523 4524 4525
}

static int
4526
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4527 4528
                     unsigned int flags)
{
4529
    testDriverPtr privconn = pool->conn->privateData;
4530
    virStoragePoolObjPtr privpool;
4531
    int ret = -1;
4532

E
Eric Blake 已提交
4533 4534
    virCheckFlags(0, -1);

4535
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4536
        goto cleanup;
4537

4538
    if (virStoragePoolObjIsActive(privpool)) {
4539 4540
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4541 4542
        goto cleanup;
    }
4543
    ret = 0;
C
Cole Robinson 已提交
4544

4545
 cleanup:
4546 4547
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4548
    return ret;
C
Cole Robinson 已提交
4549 4550 4551
}


4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589
static int
testDestroyVport(testDriverPtr privconn,
                 const char *wwnn ATTRIBUTE_UNUSED,
                 const char *wwpn ATTRIBUTE_UNUSED)
{
    int ret = -1;
    virNodeDeviceObjPtr obj = NULL;
    virObjectEventPtr event = NULL;

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

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

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

    ret = 0;

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


C
Cole Robinson 已提交
4590
static int
4591 4592
testStoragePoolDestroy(virStoragePoolPtr pool)
{
4593
    testDriverPtr privconn = pool->conn->privateData;
4594
    virStoragePoolObjPtr privpool;
4595
    int ret = -1;
4596
    virObjectEventPtr event = NULL;
4597

4598
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4599
        goto cleanup;
4600 4601

    if (!virStoragePoolObjIsActive(privpool)) {
4602 4603
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4604
        goto cleanup;
4605
    }
C
Cole Robinson 已提交
4606 4607

    privpool->active = 0;
4608 4609

    if (privpool->def->source.adapter.type ==
4610
        VIR_STORAGE_ADAPTER_TYPE_FC_HOST) {
4611 4612 4613 4614 4615 4616 4617 4618
        if (testDestroyVport(privconn,
                             privpool->def->source.adapter.data.fchost.wwnn,
                             privpool->def->source.adapter.data.fchost.wwpn) < 0)
            goto cleanup;
    }

    event = virStoragePoolEventLifecycleNew(privpool->def->name,
                                            privpool->def->uuid,
4619 4620
                                            VIR_STORAGE_POOL_EVENT_STOPPED,
                                            0);
C
Cole Robinson 已提交
4621

4622
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4623
        virStoragePoolObjRemove(&privconn->pools, privpool);
4624 4625
        privpool = NULL;
    }
4626
    ret = 0;
C
Cole Robinson 已提交
4627

4628
 cleanup:
4629
    testObjectEventQueue(privconn, event);
4630 4631 4632
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4633
    return ret;
C
Cole Robinson 已提交
4634 4635 4636 4637
}


static int
4638
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4639 4640
                      unsigned int flags)
{
4641
    testDriverPtr privconn = pool->conn->privateData;
4642
    virStoragePoolObjPtr privpool;
4643
    int ret = -1;
4644

E
Eric Blake 已提交
4645 4646
    virCheckFlags(0, -1);

4647
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4648 4649 4650
        goto cleanup;

    if (virStoragePoolObjIsActive(privpool)) {
4651 4652
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4653
        goto cleanup;
4654 4655
    }

4656
    ret = 0;
C
Cole Robinson 已提交
4657

4658
 cleanup:
4659 4660
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4661
    return ret;
C
Cole Robinson 已提交
4662 4663 4664 4665
}


static int
4666
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4667 4668
                       unsigned int flags)
{
4669
    testDriverPtr privconn = pool->conn->privateData;
4670
    virStoragePoolObjPtr privpool;
4671
    int ret = -1;
4672
    virObjectEventPtr event = NULL;
4673

E
Eric Blake 已提交
4674 4675
    virCheckFlags(0, -1);

4676
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4677
        goto cleanup;
4678 4679

    if (!virStoragePoolObjIsActive(privpool)) {
4680 4681
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4682
        goto cleanup;
4683
    }
4684

4685
    event = virStoragePoolEventRefreshNew(pool->name, pool->uuid);
4686
    ret = 0;
C
Cole Robinson 已提交
4687

4688
 cleanup:
4689
    testObjectEventQueue(privconn, event);
4690 4691
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4692
    return ret;
C
Cole Robinson 已提交
4693 4694 4695 4696
}


static int
4697
testStoragePoolGetInfo(virStoragePoolPtr pool,
4698 4699
                       virStoragePoolInfoPtr info)
{
4700
    testDriverPtr privconn = pool->conn->privateData;
4701
    virStoragePoolObjPtr privpool;
4702
    int ret = -1;
4703

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

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

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

static char *
4724
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4725 4726
                          unsigned int flags)
{
4727
    testDriverPtr privconn = pool->conn->privateData;
4728
    virStoragePoolObjPtr privpool;
4729
    char *ret = NULL;
4730

E
Eric Blake 已提交
4731 4732
    virCheckFlags(0, NULL);

4733
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4734
        goto cleanup;
4735

4736
    ret = virStoragePoolDefFormat(privpool->def);
4737

4738
 cleanup:
4739 4740
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4741
    return ret;
C
Cole Robinson 已提交
4742 4743 4744
}

static int
4745
testStoragePoolGetAutostart(virStoragePoolPtr pool,
4746 4747
                            int *autostart)
{
4748
    testDriverPtr privconn = pool->conn->privateData;
4749
    virStoragePoolObjPtr privpool;
4750
    int ret = -1;
4751

4752
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4753
        goto cleanup;
C
Cole Robinson 已提交
4754 4755 4756 4757 4758 4759

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

4762
 cleanup:
4763 4764
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4765
    return ret;
C
Cole Robinson 已提交
4766 4767 4768
}

static int
4769
testStoragePoolSetAutostart(virStoragePoolPtr pool,
4770 4771
                            int autostart)
{
4772
    testDriverPtr privconn = pool->conn->privateData;
4773
    virStoragePoolObjPtr privpool;
4774
    int ret = -1;
4775

4776
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4777
        goto cleanup;
C
Cole Robinson 已提交
4778 4779

    if (!privpool->configFile) {
4780 4781
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
4782
        goto cleanup;
C
Cole Robinson 已提交
4783 4784 4785 4786
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4787 4788
    ret = 0;

4789
 cleanup:
4790 4791
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4792
    return ret;
C
Cole Robinson 已提交
4793 4794 4795 4796
}


static int
4797 4798
testStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
4799
    testDriverPtr privconn = pool->conn->privateData;
4800
    virStoragePoolObjPtr privpool;
4801
    int ret = -1;
4802

4803
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4804
        goto cleanup;
4805 4806

    if (!virStoragePoolObjIsActive(privpool)) {
4807 4808
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4809
        goto cleanup;
4810
    }
C
Cole Robinson 已提交
4811

4812 4813
    ret = privpool->volumes.count;

4814
 cleanup:
4815 4816
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4817
    return ret;
C
Cole Robinson 已提交
4818 4819 4820
}

static int
4821
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4822
                           char **const names,
4823 4824
                           int maxnames)
{
4825
    testDriverPtr privconn = pool->conn->privateData;
4826
    virStoragePoolObjPtr privpool;
4827 4828
    size_t i = 0;
    int n = 0;
C
Cole Robinson 已提交
4829

4830
    memset(names, 0, maxnames * sizeof(*names));
4831

4832
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4833
        goto cleanup;
4834 4835

    if (!virStoragePoolObjIsActive(privpool)) {
4836 4837
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4838
        goto cleanup;
4839 4840
    }

4841
    for (i = 0; i < privpool->volumes.count && n < maxnames; i++) {
4842
        if (VIR_STRDUP(names[n++], privpool->volumes.objs[i]->name) < 0)
C
Cole Robinson 已提交
4843 4844 4845
            goto cleanup;
    }

4846
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4847 4848 4849
    return n;

 cleanup:
4850
    for (n = 0; n < maxnames; n++)
C
Cole Robinson 已提交
4851 4852
        VIR_FREE(names[i]);

4853
    memset(names, 0, maxnames * sizeof(*names));
4854 4855
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4856 4857 4858
    return -1;
}

4859 4860 4861
static int
testStoragePoolListAllVolumes(virStoragePoolPtr obj,
                              virStorageVolPtr **vols,
4862 4863
                              unsigned int flags)
{
4864
    testDriverPtr privconn = obj->conn->privateData;
4865
    virStoragePoolObjPtr pool;
4866
    size_t i;
4867 4868 4869 4870 4871 4872 4873
    virStorageVolPtr *tmp_vols = NULL;
    virStorageVolPtr vol = NULL;
    int nvols = 0;
    int ret = -1;

    virCheckFlags(0, -1);

4874
    if (!(pool = testStoragePoolObjFindByUUID(privconn, obj->uuid)))
4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888
        goto cleanup;

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

     /* Just returns the volumes count */
    if (!vols) {
        ret = pool->volumes.count;
        goto cleanup;
    }

4889
    if (VIR_ALLOC_N(tmp_vols, pool->volumes.count + 1) < 0)
4890 4891
         goto cleanup;

4892
    for (i = 0; i < pool->volumes.count; i++) {
4893 4894
        if (!(vol = virGetStorageVol(obj->conn, pool->def->name,
                                     pool->volumes.objs[i]->name,
4895 4896
                                     pool->volumes.objs[i]->key,
                                     NULL, NULL)))
4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910
            goto cleanup;
        tmp_vols[nvols++] = vol;
    }

    *vols = tmp_vols;
    tmp_vols = NULL;
    ret = nvols;

 cleanup:
    if (tmp_vols) {
        for (i = 0; i < nvols; i++) {
            if (tmp_vols[i])
                virStorageVolFree(tmp_vols[i]);
        }
4911
        VIR_FREE(tmp_vols);
4912 4913 4914 4915 4916 4917 4918
    }

    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}
C
Cole Robinson 已提交
4919 4920

static virStorageVolPtr
4921
testStorageVolLookupByName(virStoragePoolPtr pool,
4922 4923
                           const char *name ATTRIBUTE_UNUSED)
{
4924
    testDriverPtr privconn = pool->conn->privateData;
4925 4926
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4927
    virStorageVolPtr ret = NULL;
4928

4929
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4930
        goto cleanup;
4931 4932

    if (!virStoragePoolObjIsActive(privpool)) {
4933 4934
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4935
        goto cleanup;
4936 4937 4938 4939 4940
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4941 4942
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"), name);
4943
        goto cleanup;
C
Cole Robinson 已提交
4944 4945
    }

4946
    ret = virGetStorageVol(pool->conn, privpool->def->name,
4947 4948
                           privvol->name, privvol->key,
                           NULL, NULL);
4949

4950
 cleanup:
4951 4952
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4953
    return ret;
C
Cole Robinson 已提交
4954 4955 4956 4957
}


static virStorageVolPtr
4958
testStorageVolLookupByKey(virConnectPtr conn,
4959 4960
                          const char *key)
{
4961
    testDriverPtr privconn = conn->privateData;
4962
    size_t i;
4963
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4964

4965
    testDriverLock(privconn);
4966
    for (i = 0; i < privconn->pools.count; i++) {
4967
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4968
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4969
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4970 4971
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4972 4973 4974 4975
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
4976 4977
                                       privvol->key,
                                       NULL, NULL);
4978
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4979 4980
                break;
            }
C
Cole Robinson 已提交
4981
        }
4982
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4983
    }
4984
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4985

4986
    if (!ret)
4987 4988
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
4989 4990

    return ret;
C
Cole Robinson 已提交
4991 4992 4993
}

static virStorageVolPtr
4994
testStorageVolLookupByPath(virConnectPtr conn,
4995 4996
                           const char *path)
{
4997
    testDriverPtr privconn = conn->privateData;
4998
    size_t i;
4999
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
5000

5001
    testDriverLock(privconn);
5002
    for (i = 0; i < privconn->pools.count; i++) {
5003
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5004
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
5005
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
5006 5007
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

5008 5009 5010 5011
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
5012 5013
                                       privvol->key,
                                       NULL, NULL);
5014
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
5015 5016
                break;
            }
C
Cole Robinson 已提交
5017
        }
5018
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5019
    }
5020
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5021

5022
    if (!ret)
5023 5024
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
5025 5026

    return ret;
C
Cole Robinson 已提交
5027 5028 5029
}

static virStorageVolPtr
5030 5031 5032
testStorageVolCreateXML(virStoragePoolPtr pool,
                        const char *xmldesc,
                        unsigned int flags)
E
Eric Blake 已提交
5033
{
5034
    testDriverPtr privconn = pool->conn->privateData;
5035
    virStoragePoolObjPtr privpool;
5036 5037
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
5038

E
Eric Blake 已提交
5039 5040
    virCheckFlags(0, NULL);

5041
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
5042
        goto cleanup;
5043 5044

    if (!virStoragePoolObjIsActive(privpool)) {
5045 5046
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5047
        goto cleanup;
5048
    }
C
Cole Robinson 已提交
5049

5050
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5051
    if (privvol == NULL)
5052
        goto cleanup;
5053 5054

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5055 5056
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5057
        goto cleanup;
C
Cole Robinson 已提交
5058 5059 5060
    }

    /* Make sure enough space */
5061
    if ((privpool->def->allocation + privvol->target.allocation) >
C
Cole Robinson 已提交
5062
         privpool->def->capacity) {
5063 5064 5065
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5066
        goto cleanup;
C
Cole Robinson 已提交
5067 5068
    }

5069 5070
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5071
                    privvol->name) == -1)
5072
        goto cleanup;
C
Cole Robinson 已提交
5073

5074 5075 5076
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
5077
        goto cleanup;
C
Cole Robinson 已提交
5078

5079
    privpool->def->allocation += privvol->target.allocation;
C
Cole Robinson 已提交
5080 5081 5082
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5083
    ret = virGetStorageVol(pool->conn, privpool->def->name,
5084 5085
                           privvol->name, privvol->key,
                           NULL, NULL);
5086
    privvol = NULL;
5087

5088
 cleanup:
5089
    virStorageVolDefFree(privvol);
5090 5091
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5092
    return ret;
C
Cole Robinson 已提交
5093 5094
}

5095
static virStorageVolPtr
5096 5097 5098 5099
testStorageVolCreateXMLFrom(virStoragePoolPtr pool,
                            const char *xmldesc,
                            virStorageVolPtr clonevol,
                            unsigned int flags)
E
Eric Blake 已提交
5100
{
5101
    testDriverPtr privconn = pool->conn->privateData;
5102 5103 5104 5105
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
5106 5107
    virCheckFlags(0, NULL);

5108
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
5109 5110 5111
        goto cleanup;

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

5117
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5118 5119 5120 5121
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5122 5123
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5124 5125 5126 5127 5128
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
5129 5130 5131
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
5132 5133 5134 5135
        goto cleanup;
    }

    /* Make sure enough space */
5136
    if ((privpool->def->allocation + privvol->target.allocation) >
5137
         privpool->def->capacity) {
5138 5139 5140
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5141 5142 5143 5144 5145
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5146 5147
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5148
                    privvol->name) == -1)
5149 5150
        goto cleanup;

5151 5152 5153
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
5154 5155
        goto cleanup;

5156
    privpool->def->allocation += privvol->target.allocation;
5157 5158 5159 5160
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    ret = virGetStorageVol(pool->conn, privpool->def->name,
5161 5162
                           privvol->name, privvol->key,
                           NULL, NULL);
5163 5164
    privvol = NULL;

5165
 cleanup:
5166 5167 5168 5169 5170 5171
    virStorageVolDefFree(privvol);
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    return ret;
}

C
Cole Robinson 已提交
5172
static int
5173 5174
testStorageVolDelete(virStorageVolPtr vol,
                     unsigned int flags)
E
Eric Blake 已提交
5175
{
5176
    testDriverPtr privconn = vol->conn->privateData;
5177 5178
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5179
    size_t i;
5180
    int ret = -1;
C
Cole Robinson 已提交
5181

E
Eric Blake 已提交
5182 5183
    virCheckFlags(0, -1);

5184
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5185
        goto cleanup;
5186 5187 5188 5189

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

    if (privvol == NULL) {
5190 5191 5192
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5193
        goto cleanup;
5194 5195 5196
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5197 5198
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5199
        goto cleanup;
5200 5201 5202
    }


5203
    privpool->def->allocation -= privvol->target.allocation;
C
Cole Robinson 已提交
5204 5205 5206
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5207
    for (i = 0; i < privpool->volumes.count; i++) {
C
Cole Robinson 已提交
5208 5209 5210
        if (privpool->volumes.objs[i] == privvol) {
            virStorageVolDefFree(privvol);

5211
            VIR_DELETE_ELEMENT(privpool->volumes.objs, i, privpool->volumes.count);
C
Cole Robinson 已提交
5212 5213 5214
            break;
        }
    }
5215
    ret = 0;
C
Cole Robinson 已提交
5216

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


5224 5225
static int testStorageVolumeTypeForPool(int pooltype)
{
C
Cole Robinson 已提交
5226

5227
    switch (pooltype) {
C
Cole Robinson 已提交
5228 5229 5230 5231 5232 5233 5234 5235 5236 5237
        case VIR_STORAGE_POOL_DIR:
        case VIR_STORAGE_POOL_FS:
        case VIR_STORAGE_POOL_NETFS:
            return VIR_STORAGE_VOL_FILE;
        default:
            return VIR_STORAGE_VOL_BLOCK;
    }
}

static int
5238
testStorageVolGetInfo(virStorageVolPtr vol,
5239 5240
                      virStorageVolInfoPtr info)
{
5241
    testDriverPtr privconn = vol->conn->privateData;
5242 5243
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5244
    int ret = -1;
5245

5246
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5247
        goto cleanup;
5248 5249 5250 5251

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

    if (privvol == NULL) {
5252 5253 5254
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5255
        goto cleanup;
5256 5257 5258
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5259 5260
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5261
        goto cleanup;
5262
    }
C
Cole Robinson 已提交
5263 5264 5265

    memset(info, 0, sizeof(*info));
    info->type = testStorageVolumeTypeForPool(privpool->def->type);
5266 5267
    info->capacity = privvol->target.capacity;
    info->allocation = privvol->target.allocation;
5268
    ret = 0;
C
Cole Robinson 已提交
5269

5270
 cleanup:
5271 5272
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5273
    return ret;
C
Cole Robinson 已提交
5274 5275 5276
}

static char *
5277 5278
testStorageVolGetXMLDesc(virStorageVolPtr vol,
                         unsigned int flags)
E
Eric Blake 已提交
5279
{
5280
    testDriverPtr privconn = vol->conn->privateData;
5281 5282
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5283
    char *ret = NULL;
5284

E
Eric Blake 已提交
5285 5286
    virCheckFlags(0, NULL);

5287
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5288
        goto cleanup;
5289 5290 5291 5292

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

    if (privvol == NULL) {
5293 5294 5295
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5296
        goto cleanup;
5297
    }
C
Cole Robinson 已提交
5298

5299
    if (!virStoragePoolObjIsActive(privpool)) {
5300 5301
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5302
        goto cleanup;
5303 5304
    }

5305
    ret = virStorageVolDefFormat(privpool->def, privvol);
5306

5307
 cleanup:
5308 5309
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5310
    return ret;
C
Cole Robinson 已提交
5311 5312 5313
}

static char *
5314 5315
testStorageVolGetPath(virStorageVolPtr vol)
{
5316
    testDriverPtr privconn = vol->conn->privateData;
5317 5318
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5319
    char *ret = NULL;
5320

5321
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5322
        goto cleanup;
5323 5324 5325 5326

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

    if (privvol == NULL) {
5327 5328 5329
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5330
        goto cleanup;
5331 5332 5333
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5334 5335
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5336
        goto cleanup;
5337 5338
    }

5339
    ignore_value(VIR_STRDUP(ret, privvol->target.path));
5340

5341
 cleanup:
5342 5343
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5344 5345 5346
    return ret;
}

5347

5348
/* Node device implementations */
5349

5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368
static virNodeDeviceObjPtr
testNodeDeviceObjFindByName(testDriverPtr driver,
                            const char *name)
{
    virNodeDeviceObjPtr obj;

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

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

    return obj;
}


5369 5370 5371
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5372
                     unsigned int flags)
5373
{
5374
    testDriverPtr driver = conn->privateData;
5375
    int ndevs = 0;
5376
    size_t i;
5377

E
Eric Blake 已提交
5378 5379
    virCheckFlags(0, -1);

5380 5381 5382
    testDriverLock(driver);
    for (i = 0; i < driver->devs.count; i++)
        if ((cap == NULL) ||
5383
            virNodeDeviceObjHasCap(driver->devs.objs[i], cap))
5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394
            ++ndevs;
    testDriverUnlock(driver);

    return ndevs;
}

static int
testNodeListDevices(virConnectPtr conn,
                    const char *cap,
                    char **const names,
                    int maxnames,
E
Eric Blake 已提交
5395
                    unsigned int flags)
5396
{
5397
    testDriverPtr driver = conn->privateData;
5398
    int ndevs = 0;
5399
    size_t i;
5400

E
Eric Blake 已提交
5401 5402
    virCheckFlags(0, -1);

5403 5404 5405 5406
    testDriverLock(driver);
    for (i = 0; i < driver->devs.count && ndevs < maxnames; i++) {
        virNodeDeviceObjLock(driver->devs.objs[i]);
        if (cap == NULL ||
5407
            virNodeDeviceObjHasCap(driver->devs.objs[i], cap)) {
5408
            if (VIR_STRDUP(names[ndevs++], driver->devs.objs[i]->def->name) < 0) {
5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429
                virNodeDeviceObjUnlock(driver->devs.objs[i]);
                goto failure;
            }
        }
        virNodeDeviceObjUnlock(driver->devs.objs[i]);
    }
    testDriverUnlock(driver);

    return ndevs;

 failure:
    testDriverUnlock(driver);
    --ndevs;
    while (--ndevs >= 0)
        VIR_FREE(names[ndevs]);
    return -1;
}

static virNodeDevicePtr
testNodeDeviceLookupByName(virConnectPtr conn, const char *name)
{
5430
    testDriverPtr driver = conn->privateData;
5431 5432 5433
    virNodeDeviceObjPtr obj;
    virNodeDevicePtr ret = NULL;

5434
    if (!(obj = testNodeDeviceObjFindByName(driver, name)))
5435 5436
        goto cleanup;

5437 5438 5439 5440
    if ((ret = virGetNodeDevice(conn, name))) {
        if (VIR_STRDUP(ret->parent, obj->def->parent) < 0)
            virObjectUnref(ret);
    }
5441

5442
 cleanup:
5443 5444 5445 5446 5447 5448
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
5449
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5450
                         unsigned int flags)
5451
{
5452
    testDriverPtr driver = dev->conn->privateData;
5453 5454 5455
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5456 5457
    virCheckFlags(0, NULL);

5458
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5459 5460
        goto cleanup;

5461
    ret = virNodeDeviceDefFormat(obj->def);
5462

5463
 cleanup:
5464 5465 5466 5467 5468 5469 5470 5471
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
testNodeDeviceGetParent(virNodeDevicePtr dev)
{
5472
    testDriverPtr driver = dev->conn->privateData;
5473 5474 5475
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

5476
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5477 5478 5479
        goto cleanup;

    if (obj->def->parent) {
5480
        ignore_value(VIR_STRDUP(ret, obj->def->parent));
5481
    } else {
5482 5483
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5484 5485
    }

5486
 cleanup:
5487 5488 5489 5490 5491
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

5492

5493 5494 5495
static int
testNodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
5496
    testDriverPtr driver = dev->conn->privateData;
5497 5498 5499 5500 5501
    virNodeDeviceObjPtr obj;
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

5502
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5503 5504 5505 5506 5507 5508
        goto cleanup;

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

5509
 cleanup:
5510 5511 5512 5513 5514 5515 5516 5517 5518
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}


static int
testNodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
5519
    testDriverPtr driver = dev->conn->privateData;
5520 5521 5522 5523 5524
    virNodeDeviceObjPtr obj;
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

5525
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5526 5527 5528
        goto cleanup;

    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
5529
        if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->data.type)) < 0)
5530 5531 5532 5533
            goto cleanup;
    }
    ret = ncaps;

5534
 cleanup:
5535 5536 5537 5538 5539 5540 5541 5542 5543 5544
    if (obj)
        virNodeDeviceObjUnlock(obj);
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
}

5545

5546 5547
static virNodeDeviceObjPtr
testNodeDeviceMockCreateVport(testDriverPtr driver,
5548
                              const char *wwnn,
5549
                              const char *wwpn)
5550
{
5551 5552
    char *xml = NULL;
    virNodeDeviceDefPtr def = NULL;
5553
    virNodeDevCapsDefPtr caps;
5554
    virNodeDeviceObjPtr obj = NULL, objcopy = NULL;
5555
    virObjectEventPtr event = NULL;
5556

5557 5558 5559 5560 5561 5562 5563 5564 5565
    /* 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. */
5566
    if (!(objcopy = virNodeDeviceObjFindByName(&driver->devs, "scsi_host11")))
5567 5568 5569 5570 5571 5572 5573 5574
        goto cleanup;

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

    if (!(def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL)))
5575 5576
        goto cleanup;

5577
    VIR_FREE(def->name);
5578
    if (VIR_STRDUP(def->name, "scsi_host12") < 0)
5579 5580
        goto cleanup;

5581 5582 5583
    /* 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. */
5584 5585
    caps = def->caps;
    while (caps) {
5586
        if (caps->data.type != VIR_NODE_DEV_CAP_SCSI_HOST)
5587 5588
            continue;

5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602
        /* 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++;
        }
5603 5604 5605
        caps = caps->next;
    }

5606
    if (!(obj = virNodeDeviceObjAssignDef(&driver->devs, def)))
5607
        goto cleanup;
5608
    def = NULL;
5609

5610
    event = virNodeDeviceEventLifecycleNew(obj->def->name,
5611 5612
                                           VIR_NODE_DEVICE_EVENT_CREATED,
                                           0);
5613 5614 5615
    testObjectEventQueue(driver, event);

 cleanup:
5616
    VIR_FREE(xml);
5617 5618
    virNodeDeviceDefFree(def);
    return obj;
5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629
}


static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
                        unsigned int flags)
{
    testDriverPtr driver = conn->privateData;
    virNodeDeviceDefPtr def = NULL;
    char *wwnn = NULL, *wwpn = NULL;
5630 5631
    virNodeDevicePtr dev = NULL, ret = NULL;
    virNodeDeviceObjPtr obj = NULL;
5632 5633 5634 5635 5636 5637 5638 5639

    virCheckFlags(0, NULL);

    testDriverLock(driver);

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

5640 5641 5642
    /* We run this simply for validation - it essentially validates that
     * the input XML either has a wwnn/wwpn or virNodeDevCapSCSIHostParseXML
     * generated a wwnn/wwpn */
5643 5644 5645
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) < 0)
        goto cleanup;

5646 5647 5648
    /* 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. */
5649
    if (virNodeDeviceObjGetParentHost(&driver->devs, def, CREATE_DEVICE) < 0)
5650 5651 5652 5653
        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
5654 5655 5656
     * 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 */
5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668
    if (!(obj = testNodeDeviceMockCreateVport(driver, wwnn, wwpn)))
        goto cleanup;

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

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

    ret = dev;
    dev = NULL;
5669

5670
 cleanup:
5671 5672
    if (obj)
        virNodeDeviceObjUnlock(obj);
5673
    testDriverUnlock(driver);
5674
    virNodeDeviceDefFree(def);
5675
    virObjectUnref(dev);
5676 5677
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
5678
    return ret;
5679 5680 5681 5682 5683 5684
}

static int
testNodeDeviceDestroy(virNodeDevicePtr dev)
{
    int ret = 0;
5685
    testDriverPtr driver = dev->conn->privateData;
5686 5687
    virNodeDeviceObjPtr obj = NULL;
    char *parent_name = NULL, *wwnn = NULL, *wwpn = NULL;
5688
    virObjectEventPtr event = NULL;
5689

5690
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5691 5692
        goto out;

5693
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1)
5694 5695
        goto out;

5696
    if (VIR_STRDUP(parent_name, obj->def->parent) < 0)
5697 5698 5699 5700 5701 5702 5703 5704
        goto out;

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

5705 5706
    /* We do this just for basic validation, but also avoid finding a
     * vport capable HBA if for some reason our vHBA doesn't exist */
5707 5708
    if (virNodeDeviceObjGetParentHost(&driver->devs, obj->def,
                                      EXISTING_DEVICE) < 0) {
5709 5710 5711 5712
        obj = NULL;
        goto out;
    }

5713 5714 5715 5716
    event = virNodeDeviceEventLifecycleNew(dev->name,
                                           VIR_NODE_DEVICE_EVENT_DELETED,
                                           0);

5717
    virNodeDeviceObjLock(obj);
5718
    virNodeDeviceObjRemove(&driver->devs, &obj);
5719

5720
 out:
5721 5722
    if (obj)
        virNodeDeviceObjUnlock(obj);
5723
    testObjectEventQueue(driver, event);
5724 5725 5726 5727 5728 5729
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

5730 5731

/* Domain event implementations */
5732
static int
5733 5734 5735 5736
testConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
5737
{
5738
    testDriverPtr driver = conn->privateData;
5739
    int ret = 0;
5740

5741
    if (virDomainEventStateRegister(conn, driver->eventState,
5742 5743
                                    callback, opaque, freecb) < 0)
        ret = -1;
5744 5745 5746 5747

    return ret;
}

5748

5749
static int
5750 5751
testConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
5752
{
5753
    testDriverPtr driver = conn->privateData;
5754
    int ret = 0;
5755

5756
    if (virDomainEventStateDeregister(conn, driver->eventState,
5757 5758
                                      callback) < 0)
        ret = -1;
5759 5760 5761 5762

    return ret;
}

5763 5764

static int
5765 5766 5767 5768 5769 5770
testConnectDomainEventRegisterAny(virConnectPtr conn,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback callback,
                                  void *opaque,
                                  virFreeCallback freecb)
5771
{
5772
    testDriverPtr driver = conn->privateData;
5773 5774
    int ret;

5775
    if (virDomainEventStateRegisterID(conn, driver->eventState,
5776 5777
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
5778
        ret = -1;
5779 5780 5781 5782 5783

    return ret;
}

static int
5784 5785
testConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
5786
{
5787
    testDriverPtr driver = conn->privateData;
5788
    int ret = 0;
5789

5790
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5791 5792
                                        callbackID) < 0)
        ret = -1;
5793 5794 5795 5796 5797

    return ret;
}


5798 5799 5800 5801 5802 5803 5804 5805
static int
testConnectNetworkEventRegisterAny(virConnectPtr conn,
                                   virNetworkPtr net,
                                   int eventID,
                                   virConnectNetworkEventGenericCallback callback,
                                   void *opaque,
                                   virFreeCallback freecb)
{
5806
    testDriverPtr driver = conn->privateData;
5807 5808
    int ret;

5809
    if (virNetworkEventStateRegisterID(conn, driver->eventState,
5810
                                       net, eventID, callback,
5811 5812 5813 5814 5815 5816 5817 5818 5819 5820
                                       opaque, freecb, &ret) < 0)
        ret = -1;

    return ret;
}

static int
testConnectNetworkEventDeregisterAny(virConnectPtr conn,
                                     int callbackID)
{
5821
    testDriverPtr driver = conn->privateData;
5822
    int ret = 0;
5823

5824
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5825 5826
                                        callbackID) < 0)
        ret = -1;
5827 5828 5829 5830

    return ret;
}

5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863
static int
testConnectStoragePoolEventRegisterAny(virConnectPtr conn,
                                       virStoragePoolPtr pool,
                                       int eventID,
                                       virConnectStoragePoolEventGenericCallback callback,
                                       void *opaque,
                                       virFreeCallback freecb)
{
    testDriverPtr driver = conn->privateData;
    int ret;

    if (virStoragePoolEventStateRegisterID(conn, driver->eventState,
                                           pool, eventID, callback,
                                           opaque, freecb, &ret) < 0)
        ret = -1;

    return ret;
}

static int
testConnectStoragePoolEventDeregisterAny(virConnectPtr conn,
                                         int callbackID)
{
    testDriverPtr driver = conn->privateData;
    int ret = 0;

    if (virObjectEventStateDeregisterID(conn, driver->eventState,
                                        callbackID) < 0)
        ret = -1;

    return ret;
}

5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896
static int
testConnectNodeDeviceEventRegisterAny(virConnectPtr conn,
                                      virNodeDevicePtr dev,
                                      int eventID,
                                      virConnectNodeDeviceEventGenericCallback callback,
                                      void *opaque,
                                      virFreeCallback freecb)
{
    testDriverPtr driver = conn->privateData;
    int ret;

    if (virNodeDeviceEventStateRegisterID(conn, driver->eventState,
                                          dev, eventID, callback,
                                          opaque, freecb, &ret) < 0)
        ret = -1;

    return ret;
}

static int
testConnectNodeDeviceEventDeregisterAny(virConnectPtr conn,
                                        int callbackID)
{
    testDriverPtr driver = conn->privateData;
    int ret = 0;

    if (virObjectEventStateDeregisterID(conn, driver->eventState,
                                        callbackID) < 0)
        ret = -1;

    return ret;
}

5897 5898 5899
static int testConnectListAllDomains(virConnectPtr conn,
                                     virDomainPtr **domains,
                                     unsigned int flags)
5900
{
5901
    testDriverPtr privconn = conn->privateData;
5902

O
Osier Yang 已提交
5903
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
5904

5905 5906
    return virDomainObjListExport(privconn->domains, conn, domains,
                                  NULL, flags);
5907 5908
}

5909
static int
P
Peter Krempa 已提交
5910
testNodeGetCPUMap(virConnectPtr conn ATTRIBUTE_UNUSED,
5911 5912 5913 5914 5915 5916 5917
                  unsigned char **cpumap,
                  unsigned int *online,
                  unsigned int flags)
{
    virCheckFlags(0, -1);

    if (cpumap) {
5918
        if (VIR_ALLOC_N(*cpumap, 1) < 0)
P
Peter Krempa 已提交
5919
            return -1;
5920 5921 5922 5923 5924 5925
        *cpumap[0] = 0x15;
    }

    if (online)
        *online = 3;

P
Peter Krempa 已提交
5926
    return  8;
5927 5928
}

5929 5930 5931 5932 5933 5934 5935 5936 5937 5938
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

5939
    if (VIR_STRDUP(ret, "image/png") < 0)
5940 5941
        return NULL;

D
Daniel P. Berrange 已提交
5942
    if (virFDStreamOpenFile(st, PKGDATADIR "/test-screenshot.png", 0, 0, O_RDONLY) < 0)
5943 5944 5945 5946 5947
        VIR_FREE(ret);

    return ret;
}

5948 5949
static int
testConnectGetCPUModelNames(virConnectPtr conn ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
5950
                            const char *archName,
5951 5952 5953
                            char ***models,
                            unsigned int flags)
{
J
Jiri Denemark 已提交
5954 5955
    virArch arch;

5956
    virCheckFlags(0, -1);
J
Jiri Denemark 已提交
5957 5958 5959 5960 5961 5962 5963 5964

    if (!(arch = virArchFromString(archName))) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("cannot find architecture %s"),
                       archName);
        return -1;
    }

J
Jiri Denemark 已提交
5965
    return virCPUGetModels(arch, models);
5966
}
5967

C
Cole Robinson 已提交
5968 5969 5970
static int
testDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
5971
    testDriverPtr privconn = dom->conn->privateData;
C
Cole Robinson 已提交
5972
    virDomainObjPtr vm = NULL;
5973
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
5974 5975 5976 5977 5978 5979
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_SAVE_BYPASS_CACHE |
                  VIR_DOMAIN_SAVE_RUNNING |
                  VIR_DOMAIN_SAVE_PAUSED, -1);

5980 5981
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995

    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);
5996
    event = virDomainEventLifecycleNewFromObj(vm,
C
Cole Robinson 已提交
5997 5998 5999 6000 6001
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
    vm->hasManagedSave = true;

    ret = 0;
6002
 cleanup:
6003
    virDomainObjEndAPI(&vm);
6004
    testObjectEventQueue(privconn, event);
C
Cole Robinson 已提交
6005 6006 6007 6008 6009 6010 6011 6012 6013

    return ret;
}


static int
testDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;
6014
    int ret;
C
Cole Robinson 已提交
6015 6016 6017

    virCheckFlags(0, -1);

6018 6019
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
6020 6021

    ret = vm->hasManagedSave;
6022

6023
    virDomainObjEndAPI(&vm);
C
Cole Robinson 已提交
6024 6025 6026 6027 6028 6029 6030 6031 6032 6033
    return ret;
}

static int
testDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;

    virCheckFlags(0, -1);

6034 6035
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
6036 6037

    vm->hasManagedSave = false;
6038

6039
    virDomainObjEndAPI(&vm);
6040
    return 0;
C
Cole Robinson 已提交
6041 6042 6043
}


6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077
/*
 * 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;
6078
    int n;
6079 6080 6081 6082 6083

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6084
        return -1;
6085 6086 6087

    n = virDomainSnapshotObjListNum(vm->snapshots, NULL, flags);

6088
    virDomainObjEndAPI(&vm);
6089 6090 6091 6092 6093 6094 6095 6096 6097 6098
    return n;
}

static int
testDomainSnapshotListNames(virDomainPtr domain,
                            char **names,
                            int nameslen,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6099
    int n;
6100 6101 6102 6103 6104

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6105
        return -1;
6106 6107 6108 6109

    n = virDomainSnapshotObjListGetNames(vm->snapshots, NULL, names, nameslen,
                                         flags);

6110
    virDomainObjEndAPI(&vm);
6111 6112 6113 6114 6115 6116 6117 6118 6119
    return n;
}

static int
testDomainListAllSnapshots(virDomainPtr domain,
                           virDomainSnapshotPtr **snaps,
                           unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6120
    int n;
6121 6122 6123 6124 6125

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6126
        return -1;
6127 6128 6129

    n = virDomainListSnapshots(vm->snapshots, NULL, domain, snaps, flags);

6130
    virDomainObjEndAPI(&vm);
6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147
    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)))
6148
        return -1;
6149 6150 6151 6152 6153 6154 6155

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListGetNames(vm->snapshots, snap, names, nameslen,
                                         flags);

6156
 cleanup:
6157
    virDomainObjEndAPI(&vm);
6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172
    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)))
6173
        return -1;
6174 6175 6176 6177 6178 6179

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListNum(vm->snapshots, snap, flags);

6180
 cleanup:
6181
    virDomainObjEndAPI(&vm);
6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197
    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)))
6198
        return -1;
6199 6200 6201 6202 6203 6204 6205

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainListSnapshots(vm->snapshots, snap, snapshot->domain, snaps,
                               flags);

6206
 cleanup:
6207
    virDomainObjEndAPI(&vm);
6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222
    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)))
6223
        return NULL;
6224 6225 6226 6227 6228 6229

    if (!(snap = testSnapObjFromName(vm, name)))
        goto cleanup;

    snapshot = virGetDomainSnapshot(domain, snap->def->name);

6230
 cleanup:
6231
    virDomainObjEndAPI(&vm);
6232 6233 6234 6235 6236 6237 6238 6239
    return snapshot;
}

static int
testDomainHasCurrentSnapshot(virDomainPtr domain,
                             unsigned int flags)
{
    virDomainObjPtr vm;
6240
    int ret;
6241 6242 6243 6244

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6245
        return -1;
6246 6247 6248

    ret = (vm->current_snapshot != NULL);

6249
    virDomainObjEndAPI(&vm);
6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263
    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)))
6264
        return NULL;
6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277

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

6278
 cleanup:
6279
    virDomainObjEndAPI(&vm);
6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292
    return parent;
}

static virDomainSnapshotPtr
testDomainSnapshotCurrent(virDomainPtr domain,
                          unsigned int flags)
{
    virDomainObjPtr vm;
    virDomainSnapshotPtr snapshot = NULL;

    virCheckFlags(0, NULL);

    if (!(vm = testDomObjFromDomain(domain)))
6293
        return NULL;
6294 6295 6296 6297 6298 6299 6300 6301 6302

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

6303
 cleanup:
6304
    virDomainObjEndAPI(&vm);
6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315
    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];
6316
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6317 6318 6319 6320

    virCheckFlags(VIR_DOMAIN_XML_SECURE, NULL);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6321
        return NULL;
6322 6323 6324 6325 6326 6327

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    virUUIDFormat(snapshot->domain->uuid, uuidstr);

6328
    xml = virDomainSnapshotDefFormat(uuidstr, snap->def, privconn->caps,
6329 6330
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);
6331

6332
 cleanup:
6333
    virDomainObjEndAPI(&vm);
6334 6335 6336 6337 6338 6339 6340 6341
    return xml;
}

static int
testDomainSnapshotIsCurrent(virDomainSnapshotPtr snapshot,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6342
    int ret;
6343 6344 6345 6346

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6347
        return -1;
6348 6349 6350 6351

    ret = (vm->current_snapshot &&
           STREQ(snapshot->name, vm->current_snapshot->def->name));

6352
    virDomainObjEndAPI(&vm);
6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366
    return ret;
}


static int
testDomainSnapshotHasMetadata(virDomainSnapshotPtr snapshot,
                              unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6367
        return -1;
6368

C
Cole Robinson 已提交
6369
    if (!testSnapObjFromSnapshot(vm, snapshot))
6370 6371 6372 6373
        goto cleanup;

    ret = 1;

6374
 cleanup:
6375
    virDomainObjEndAPI(&vm);
6376 6377 6378
    return ret;
}

6379 6380 6381 6382 6383 6384
static int
testDomainSnapshotAlignDisks(virDomainObjPtr vm,
                             virDomainSnapshotDefPtr def,
                             unsigned int flags)
{
    int align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
E
Eric Blake 已提交
6385
    bool align_match = true;
6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413

    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)
{
6414
    testDriverPtr privconn = domain->conn->privateData;
6415 6416 6417 6418
    virDomainObjPtr vm = NULL;
    virDomainSnapshotDefPtr def = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    virDomainSnapshotPtr snapshot = NULL;
6419
    virObjectEventPtr event = NULL;
6420
    char *xml = NULL;
6421 6422
    bool update_current = true;
    bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE;
6423 6424 6425 6426 6427 6428 6429 6430
    unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;

    /*
     * DISK_ONLY: Not implemented yet
     * REUSE_EXT: Not implemented yet
     *
     * NO_METADATA: Explicitly not implemented
     *
6431
     * REDEFINE + CURRENT: Implemented
6432 6433 6434 6435 6436 6437
     * HALT: Implemented
     * QUIESCE: Nothing to do
     * ATOMIC: Nothing to do
     * LIVE: Nothing to do
     */
    virCheckFlags(
6438 6439
        VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
        VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT |
6440 6441 6442 6443 6444
        VIR_DOMAIN_SNAPSHOT_CREATE_HALT |
        VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE |
        VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC |
        VIR_DOMAIN_SNAPSHOT_CREATE_LIVE, NULL);

6445 6446 6447 6448 6449
    if ((redefine && !(flags & VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT)))
        update_current = false;
    if (redefine)
        parse_flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;

6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464
    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;

6465
    if (redefine) {
C
Cole Robinson 已提交
6466 6467
        if (virDomainSnapshotRedefinePrep(domain, vm, &def, &snap,
                                          &update_current, flags) < 0)
6468 6469 6470 6471 6472
            goto cleanup;
    } else {
        if (!(def->dom = virDomainDefCopy(vm->def,
                                          privconn->caps,
                                          privconn->xmlopt,
6473
                                          NULL,
6474 6475
                                          true)))
            goto cleanup;
6476

6477
        if (testDomainSnapshotAlignDisks(vm, def, flags) < 0)
6478 6479 6480
            goto cleanup;
    }

6481 6482 6483 6484
    if (!snap) {
        if (!(snap = virDomainSnapshotAssignDef(vm->snapshots, def)))
            goto cleanup;
        def = NULL;
6485 6486
    }

6487 6488 6489 6490 6491 6492 6493 6494 6495 6496
    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);
6497
            event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
6498 6499 6500
                                    VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
        }
    }
6501 6502

    snapshot = virGetDomainSnapshot(domain, snap->def->name);
6503
 cleanup:
6504 6505 6506 6507
    VIR_FREE(xml);
    if (vm) {
        if (snapshot) {
            virDomainSnapshotObjPtr other;
6508 6509
            if (update_current)
                vm->current_snapshot = snap;
6510 6511 6512 6513 6514 6515 6516
            other = virDomainSnapshotFindByName(vm->snapshots,
                                                snap->def->parent);
            snap->parent = other;
            other->nchildren++;
            snap->sibling = other->first_child;
            other->first_child = snap;
        }
6517
        virDomainObjEndAPI(&vm);
6518
    }
6519
    testObjectEventQueue(privconn, event);
6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531
    virDomainSnapshotDefFree(def);
    return snapshot;
}


typedef struct _testSnapRemoveData testSnapRemoveData;
typedef testSnapRemoveData *testSnapRemoveDataPtr;
struct _testSnapRemoveData {
    virDomainObjPtr vm;
    bool current;
};

6532
static int
6533
testDomainSnapshotDiscardAll(void *payload,
6534 6535
                             const void *name ATTRIBUTE_UNUSED,
                             void *data)
6536 6537 6538 6539 6540 6541 6542
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapRemoveDataPtr curr = data;

    if (snap->def->current)
        curr->current = true;
    virDomainSnapshotObjListRemove(curr->vm->snapshots, snap);
6543
    return 0;
6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554
}

typedef struct _testSnapReparentData testSnapReparentData;
typedef testSnapReparentData *testSnapReparentDataPtr;
struct _testSnapReparentData {
    virDomainSnapshotObjPtr parent;
    virDomainObjPtr vm;
    int err;
    virDomainSnapshotObjPtr last;
};

6555
static int
6556 6557 6558 6559 6560 6561 6562
testDomainSnapshotReparentChildren(void *payload,
                                   const void *name ATTRIBUTE_UNUSED,
                                   void *data)
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapReparentDataPtr rep = data;

6563
    if (rep->err < 0)
6564
        return 0;
6565 6566 6567 6568 6569 6570 6571

    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;
6572
        return 0;
6573 6574 6575 6576
    }

    if (!snap->sibling)
        rep->last = snap;
6577
    return 0;
6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606
}

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) {
6607
            if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY)
6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650
                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;
6651
 cleanup:
6652
    virDomainObjEndAPI(&vm);
6653 6654 6655 6656 6657 6658 6659
    return ret;
}

static int
testDomainRevertToSnapshot(virDomainSnapshotPtr snapshot,
                           unsigned int flags)
{
6660
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6661 6662
    virDomainObjPtr vm = NULL;
    virDomainSnapshotObjPtr snap = NULL;
6663 6664
    virObjectEventPtr event = NULL;
    virObjectEventPtr event2 = NULL;
6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727
    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;
6728 6729
    config = virDomainDefCopy(snap->def->dom, privconn->caps,
                              privconn->xmlopt, NULL, true);
6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755
    if (!config)
        goto cleanup;

    if (snap->def->state == VIR_DOMAIN_RUNNING ||
        snap->def->state == VIR_DOMAIN_PAUSED) {
        /* Transitions 2, 3, 5, 6, 8, 9 */
        bool was_running = false;
        bool was_stopped = false;

        if (virDomainObjIsActive(vm)) {
            /* Transitions 5, 6, 8, 9 */
            /* Check for ABI compatibility.  */
            if (!virDomainDefCheckABIStability(vm->def, config)) {
                virErrorPtr err = virGetLastError();

                if (!(flags & VIR_DOMAIN_SNAPSHOT_REVERT_FORCE)) {
                    /* Re-spawn error using correct category. */
                    if (err->code == VIR_ERR_CONFIG_UNSUPPORTED)
                        virReportError(VIR_ERR_SNAPSHOT_REVERT_RISKY, "%s",
                                       err->str2);
                    goto cleanup;
                }

                virResetError(err);
                testDomainShutdownState(snapshot->domain, vm,
                                        VIR_DOMAIN_SHUTOFF_FROM_SNAPSHOT);
6756
                event = virDomainEventLifecycleNewFromObj(vm,
6757 6758
                            VIR_DOMAIN_EVENT_STOPPED,
                            VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
6759
                testObjectEventQueue(privconn, event);
6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770
                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. */
6771
                event = virDomainEventLifecycleNewFromObj(vm,
6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784
                                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;
6785
            event = virDomainEventLifecycleNewFromObj(vm,
6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798
                                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 */
6799
                event2 = virDomainEventLifecycleNewFromObj(vm,
6800 6801 6802 6803 6804
                                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 已提交
6805
            virObjectUnref(event);
6806 6807 6808 6809
            event = NULL;

            if (was_stopped) {
                /* Transition 2 */
6810
                event = virDomainEventLifecycleNewFromObj(vm,
6811 6812 6813 6814
                                VIR_DOMAIN_EVENT_STARTED,
                                VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            } else if (was_running) {
                /* Transition 8 */
6815
                event = virDomainEventLifecycleNewFromObj(vm,
6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827
                                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);
6828
            event = virDomainEventLifecycleNewFromObj(vm,
6829 6830 6831 6832 6833 6834 6835 6836 6837
                                    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;

6838
            testObjectEventQueue(privconn, event);
6839
            event = virDomainEventLifecycleNewFromObj(vm,
6840 6841 6842
                            VIR_DOMAIN_EVENT_STARTED,
                            VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            if (paused) {
6843
                event2 = virDomainEventLifecycleNewFromObj(vm,
6844 6845 6846 6847 6848 6849 6850 6851
                                VIR_DOMAIN_EVENT_SUSPENDED,
                                VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT);
            }
        }
    }

    vm->current_snapshot = snap;
    ret = 0;
6852
 cleanup:
6853
    if (event) {
6854
        testObjectEventQueue(privconn, event);
6855
        testObjectEventQueue(privconn, event2);
C
Cole Robinson 已提交
6856
    } else {
C
Cédric Bosdonnat 已提交
6857
        virObjectUnref(event2);
6858
    }
6859
    virDomainObjEndAPI(&vm);
6860 6861 6862 6863 6864

    return ret;
}


6865

6866
static virHypervisorDriver testHypervisorDriver = {
6867
    .name = "Test",
6868 6869 6870
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
6871
    .connectGetHostname = testConnectGetHostname, /* 0.6.3 */
6872
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
6873
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
6874
    .nodeGetCPUStats = testNodeGetCPUStats, /* 2.3.0 */
6875
    .nodeGetFreeMemory = testNodeGetFreeMemory, /* 2.3.0 */
6876
    .nodeGetFreePages = testNodeGetFreePages, /* 2.3.0 */
6877
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
6878
    .connectGetSysinfo = testConnectGetSysinfo, /* 2.3.0 */
6879
    .connectGetType = testConnectGetType, /* 2.3.0 */
6880 6881 6882
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
6883
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897
    .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 */
6898 6899
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
6900
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
6901
    .domainRestore = testDomainRestore, /* 0.3.2 */
6902
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
6903
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
6904
    .domainCoreDumpWithFormat = testDomainCoreDumpWithFormat, /* 1.2.3 */
6905
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
6906 6907 6908 6909
    .domainSetVcpusFlags = testDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = testDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = testDomainPinVcpu, /* 0.7.3 */
    .domainGetVcpus = testDomainGetVcpus, /* 0.7.3 */
6910
    .domainGetVcpuPinInfo = testDomainGetVcpuPinInfo, /* 1.2.18 */
6911 6912
    .domainGetMaxVcpus = testDomainGetMaxVcpus, /* 0.7.3 */
    .domainGetXMLDesc = testDomainGetXMLDesc, /* 0.1.4 */
6913 6914
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
6915 6916 6917
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
6918
    .domainDefineXMLFlags = testDomainDefineXMLFlags, /* 1.2.12 */
6919
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
6920
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
6921 6922 6923
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
6924 6925 6926 6927
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
6928 6929 6930
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
6931 6932 6933 6934
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
6935 6936 6937
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
6938 6939 6940
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
6941
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
6942
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
6943 6944
    .domainGetMetadata = testDomainGetMetadata, /* 1.1.3 */
    .domainSetMetadata = testDomainSetMetadata, /* 1.1.3 */
6945
    .connectGetCPUModelNames = testConnectGetCPUModelNames, /* 1.1.3 */
6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962
    .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 */
6963 6964 6965
    .domainSnapshotCreateXML = testDomainSnapshotCreateXML, /* 1.1.4 */
    .domainRevertToSnapshot = testDomainRevertToSnapshot, /* 1.1.4 */
    .domainSnapshotDelete = testDomainSnapshotDelete, /* 1.1.4 */
6966

E
Eric Blake 已提交
6967
    .connectBaselineCPU = testConnectBaselineCPU, /* 1.2.0 */
6968 6969 6970
};

static virNetworkDriver testNetworkDriver = {
6971 6972 6973 6974 6975
    .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 */
6976 6977
    .connectNetworkEventRegisterAny = testConnectNetworkEventRegisterAny, /* 1.2.1 */
    .connectNetworkEventDeregisterAny = testConnectNetworkEventDeregisterAny, /* 1.2.1 */
6978 6979 6980 6981
    .networkLookupByUUID = testNetworkLookupByUUID, /* 0.3.2 */
    .networkLookupByName = testNetworkLookupByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreateXML, /* 0.3.2 */
    .networkDefineXML = testNetworkDefineXML, /* 0.3.2 */
6982
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
6983
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
6984
    .networkCreate = testNetworkCreate, /* 0.3.2 */
6985 6986 6987 6988 6989 6990 6991
    .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 */
6992 6993
};

L
Laine Stump 已提交
6994
static virInterfaceDriver testInterfaceDriver = {
6995 6996 6997 6998 6999 7000
    .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 */
7001 7002 7003 7004 7005 7006
    .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 */
7007 7008 7009
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
7010 7011 7012
};


7013
static virStorageDriver testStorageDriver = {
7014 7015 7016 7017 7018 7019
    .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 */
7020 7021
    .connectStoragePoolEventRegisterAny = testConnectStoragePoolEventRegisterAny, /* 2.0.0 */
    .connectStoragePoolEventDeregisterAny = testConnectStoragePoolEventDeregisterAny, /* 2.0.0 */
7022 7023 7024
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
7025 7026
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
7027 7028
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
7029
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
7030 7031 7032 7033 7034 7035 7036
    .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 */
7037
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
7038 7039 7040
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

7041 7042 7043 7044 7045 7046 7047 7048 7049
    .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 */
7050 7051
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
7052 7053
};

7054
static virNodeDeviceDriver testNodeDeviceDriver = {
7055 7056
    .connectNodeDeviceEventRegisterAny = testConnectNodeDeviceEventRegisterAny, /* 2.2.0 */
    .connectNodeDeviceEventDeregisterAny = testConnectNodeDeviceEventDeregisterAny, /* 2.2.0 */
7057 7058 7059 7060 7061 7062 7063 7064 7065
    .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 */
7066 7067
};

7068 7069 7070 7071 7072 7073 7074 7075
static virConnectDriver testConnectDriver = {
    .hypervisorDriver = &testHypervisorDriver,
    .interfaceDriver = &testInterfaceDriver,
    .networkDriver = &testNetworkDriver,
    .nodeDeviceDriver = &testNodeDeviceDriver,
    .nwfilterDriver = NULL,
    .secretDriver = NULL,
    .storageDriver = &testStorageDriver,
7076 7077
};

7078 7079 7080 7081 7082 7083 7084 7085
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
7086 7087
    return virRegisterConnectDriver(&testConnectDriver,
                                    false);
7088
}