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

71 72
#define VIR_FROM_THIS VIR_FROM_TEST

73 74
VIR_LOG_INIT("test.test_driver");

75

76 77 78 79
#define MAX_CPUS 128

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

#define MAX_CELLS 128
88

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

96
struct _testDriver {
97
    virMutex lock;
98

99
    virNodeInfo nodeInfo;
100
    virInterfaceObjListPtr ifaces;
101
    bool transaction_running;
102
    virInterfaceObjListPtr backupIfaces;
C
Cole Robinson 已提交
103
    virStoragePoolObjList pools;
104
    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
static void
testDriverFree(testDriverPtr driver)
{
    if (!driver)
        return;

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

    VIR_FREE(driver);
}
165

166

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

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

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

    virObjectEventStateQueue(driver->eventState, event);
}

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

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

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

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

    if (!nsdata)
        return;

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

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

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

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

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

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

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

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

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

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

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

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

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

298 299 300
    *data = nsdata;
    return 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

391
    return caps;
392

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

398

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

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

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

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

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

    return ret;

 error:
    testDriverFree(ret);
    return NULL;
}


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

C
Cole Robinson 已提交
542

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

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

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

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

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

    return vm;
}

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

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

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

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

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

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

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

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

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

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

640
    return 0;
641 642
}

643

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

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

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

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

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

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

681

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

951
        virObjectUnlock(obj);
952
    }
953

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

960

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

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

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

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

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

        obj->active = 1;
991
        virNetworkObjEndAPI(&obj);
992
    }
993

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

1000

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

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

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

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

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

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

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

static int
C
Cole Robinson 已提交
1042
testOpenVolumesForPool(const char *file,
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
                       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);
1059
    if (num < 0)
1060 1061 1062
        goto error;

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

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

        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;
1081 1082
        if (VIR_APPEND_ELEMENT_COPY(pool->volumes.objs, pool->volumes.count, def) < 0)
            goto error;
1083

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

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

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

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

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

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

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

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

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

1140
        virStoragePoolObjUnlock(obj);
C
Cole Robinson 已提交
1141 1142
    }

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

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

    num = virXPathNodeSet("/node/device", ctxt, &nodes);
1160
    if (num < 0)
1161
        goto error;
1162 1163

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

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

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

        virNodeDeviceObjUnlock(obj);
    }

    ret = 0;
1183
 error:
1184 1185 1186 1187
    VIR_FREE(nodes);
    return ret;
}

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

C
Cole Robinson 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
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;
}

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

1268
    if (!(privconn = testDriverNew()))
1269
        return VIR_DRV_OPEN_ERROR;
1270

1271 1272 1273 1274 1275 1276
    testDriverLock(privconn);
    conn->privateData = privconn;

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

1277
    if (!(doc = virXMLParseFileCtxt(file, &ctxt)))
1278 1279 1280 1281 1282
        goto error;

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

C
Cole Robinson 已提交
1283
    if (testOpenParse(privconn, file, ctxt) < 0)
1284
        goto error;
1285

J
Jim Meyering 已提交
1286
    xmlXPathFreeContext(ctxt);
1287
    xmlFreeDoc(doc);
1288
    testDriverUnlock(privconn);
1289

1290
    return 0;
1291 1292

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

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

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

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

1346 1347
    if (!(doc = virXMLParseStringCtxt(defaultConnXML,
                                      _("(test driver)"), &ctxt)))
1348 1349
        goto error;

1350
    if (testOpenParse(privconn, NULL, ctxt) < 0)
1351 1352 1353 1354
        goto error;

    defaultConn = privconn;

1355 1356
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);
1357 1358 1359 1360 1361 1362
    virMutexUnlock(&defaultLock);

    return VIR_DRV_OPEN_SUCCESS;

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

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

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

1426
static virDrvOpenStatus testConnectOpen(virConnectPtr conn,
1427
                                        virConnectAuthPtr auth,
1428
                                        virConfPtr conf ATTRIBUTE_UNUSED,
1429
                                        unsigned int flags)
1430
{
1431
    int ret;
1432

E
Eric Blake 已提交
1433 1434
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1435
    if (!conn->uri)
1436
        return VIR_DRV_OPEN_DECLINED;
1437

1438
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1439
        return VIR_DRV_OPEN_DECLINED;
1440

1441
    /* Remote driver should handle these. */
1442
    if (conn->uri->server)
1443 1444
        return VIR_DRV_OPEN_DECLINED;

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

1454
    if (STREQ(conn->uri->path, "/default"))
1455 1456
        ret = testOpenDefault(conn);
    else
1457
        ret = testOpenFromFile(conn,
1458
                               conn->uri->path);
1459

1460 1461 1462
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

1463 1464 1465 1466
    /* Fake authentication. */
    if (testConnectAuthenticate(conn, auth) < 0)
        return VIR_DRV_OPEN_ERROR;

1467
    return VIR_DRV_OPEN_SUCCESS;
1468 1469
}

1470
static int testConnectClose(virConnectPtr conn)
1471
{
1472
    testDriverPtr privconn = conn->privateData;
1473
    bool dflt = false;
1474

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

1484
    testDriverLock(privconn);
1485
    testDriverFree(privconn);
1486 1487 1488

    if (dflt) {
        defaultConn = NULL;
1489
        virMutexUnlock(&defaultLock);
1490 1491
    }

1492
    conn->privateData = NULL;
1493
    return 0;
1494 1495
}

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

1503 1504 1505 1506 1507 1508
static char *testConnectGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return virGetHostname();
}


1509
static int testConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
1510 1511 1512 1513
{
    return 1;
}

1514
static int testConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
1515 1516 1517 1518
{
    return 0;
}

1519
static int testConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
1520 1521 1522 1523
{
    return 1;
}

1524 1525
static int testConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type ATTRIBUTE_UNUSED)
1526 1527 1528 1529
{
    return 32;
}

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
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;
}

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

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

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

1585 1586 1587 1588 1589 1590
static const char *
testConnectGetType(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return "TEST";
}

1591
static int testConnectNumOfDomains(virConnectPtr conn)
1592
{
1593
    testDriverPtr privconn = conn->privateData;
1594
    int count;
1595

1596
    testDriverLock(privconn);
1597
    count = virDomainObjListNumOfDomains(privconn->domains, true, NULL, NULL);
1598
    testDriverUnlock(privconn);
1599

1600
    return count;
1601 1602
}

1603 1604 1605
static int testDomainIsActive(virDomainPtr dom)
{
    virDomainObjPtr obj;
1606
    int ret;
1607

1608 1609
    if (!(obj = testDomObjFromDomain(dom)))
        return -1;
1610

1611 1612
    ret = virDomainObjIsActive(obj);
    virDomainObjEndAPI(&obj);
1613 1614 1615 1616 1617 1618
    return ret;
}

static int testDomainIsPersistent(virDomainPtr dom)
{
    virDomainObjPtr obj;
1619 1620 1621 1622
    int ret;

    if (!(obj = testDomObjFromDomain(dom)))
        return -1;
1623 1624 1625

    ret = obj->persistent;

1626
    virDomainObjEndAPI(&obj);
1627 1628 1629
    return ret;
}

1630 1631 1632 1633 1634
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

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

1646 1647 1648
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_START_VALIDATE)
1649
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
1650

1651
    testDriverLock(privconn);
1652
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
1653
                                       NULL, parse_flags)) == NULL)
1654
        goto cleanup;
1655

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

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

1675
    event = virDomainEventLifecycleNewFromObj(dom,
1676 1677 1678
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1679
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1680

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


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

1698
    if (!(dom = virDomainObjListFindByID(privconn->domains, id))) {
1699
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1700
        goto cleanup;
1701 1702
    }

1703
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid, dom->def->id);
1704

1705
 cleanup:
1706
    if (dom)
1707
        virObjectUnlock(dom);
1708
    return ret;
1709 1710
}

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

1718
    if (!(dom = virDomainObjListFindByUUID(privconn->domains, uuid))) {
1719
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1720
        goto cleanup;
1721
    }
1722

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

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

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

1738
    if (!(dom = virDomainObjListFindByName(privconn->domains, name))) {
1739
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1740
        goto cleanup;
1741
    }
1742

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

1745
 cleanup:
1746
    virDomainObjEndAPI(&dom);
1747
    return ret;
1748 1749
}

1750 1751 1752
static int testConnectListDomains(virConnectPtr conn,
                                  int *ids,
                                  int maxids)
1753
{
1754
    testDriverPtr privconn = conn->privateData;
1755

1756 1757
    return virDomainObjListGetActiveIDs(privconn->domains, ids, maxids,
                                        NULL, NULL);
1758 1759
}

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

1767
    if (!(privdom = testDomObjFromDomain(domain)))
1768
        goto cleanup;
1769

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

J
Jiri Denemark 已提交
1776
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1777
    event = virDomainEventLifecycleNewFromObj(privdom,
1778 1779
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1780

1781 1782
    if (!privdom->persistent)
        virDomainObjListRemove(privconn->domains, privdom);
1783 1784

    ret = 0;
1785
 cleanup:
1786
    virDomainObjEndAPI(&privdom);
1787
    testObjectEventQueue(privconn, event);
1788
    return ret;
1789 1790
}

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

1798 1799
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1800

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

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

1814
 cleanup:
1815
    virDomainObjEndAPI(&privdom);
1816
    testObjectEventQueue(privconn, event);
1817
    return ret;
1818 1819
}

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

1828 1829
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1830

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

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

1844
 cleanup:
1845
    virDomainObjEndAPI(&privdom);
1846
    testObjectEventQueue(privconn, event);
1847
    return ret;
1848 1849
}

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

1858 1859
    virCheckFlags(0, -1);

1860

1861
    if (!(privdom = testDomObjFromDomain(domain)))
1862
        goto cleanup;
1863

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

J
Jiri Denemark 已提交
1870
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1871
    event = virDomainEventLifecycleNewFromObj(privdom,
1872 1873
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1874

1875 1876
    if (!privdom->persistent)
        virDomainObjListRemove(privconn->domains, privdom);
1877

1878
    ret = 0;
1879
 cleanup:
1880
    virDomainObjEndAPI(&privdom);
1881
    testObjectEventQueue(privconn, event);
1882
    return ret;
1883 1884
}

1885
static int testDomainShutdown(virDomainPtr domain)
1886
{
1887
    return testDomainShutdownFlags(domain, 0);
1888 1889
}

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


1900
    if (!(privdom = testDomObjFromDomain(domain)))
1901
        goto cleanup;
1902

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

J
Jiri Denemark 已提交
1909 1910 1911
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

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

1918
    case VIR_DOMAIN_LIFECYCLE_RESTART:
J
Jiri Denemark 已提交
1919 1920
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1921 1922
        break;

1923
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
J
Jiri Denemark 已提交
1924 1925
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1926 1927
        break;

1928
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
J
Jiri Denemark 已提交
1929 1930
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1931
        break;
1932

1933
    default:
J
Jiri Denemark 已提交
1934 1935
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1936 1937
        break;
    }
1938

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

1945 1946
        if (!privdom->persistent)
            virDomainObjListRemove(privconn->domains, privdom);
1947 1948
    }

1949
    ret = 0;
1950
 cleanup:
1951
    virDomainObjEndAPI(&privdom);
1952
    testObjectEventQueue(privconn, event);
1953
    return ret;
1954 1955
}

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

1963 1964
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1965 1966

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

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

1979
 cleanup:
1980
    virDomainObjEndAPI(&privdom);
1981
    return ret;
1982 1983
}

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

    virCheckFlags(0, -1);

1994 1995
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1996

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

1999
    virDomainObjEndAPI(&privdom);
2000 2001

    return 0;
2002 2003
}

2004 2005
#define TEST_SAVE_MAGIC "TestGuestMagic"

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

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

2025

2026
    if (!(privdom = testDomObjFromDomain(domain)))
2027
        goto cleanup;
2028

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

2035
    xml = virDomainDefFormat(privdom->def, privconn->caps,
2036
                             VIR_DOMAIN_DEF_FORMAT_SECURE);
C
Cole Robinson 已提交
2037

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

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

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

J
Jiri Denemark 已提交
2079
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
2080
    event = virDomainEventLifecycleNewFromObj(privdom,
2081 2082
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
2083

2084 2085
    if (!privdom->persistent)
        virDomainObjListRemove(privconn->domains, privdom);
2086

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

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

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

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

2170
    def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
2171
                                  NULL, VIR_DOMAIN_DEF_PARSE_INACTIVE);
2172
    if (!def)
2173
        goto cleanup;
2174

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

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

2194
    event = virDomainEventLifecycleNewFromObj(dom,
2195 2196
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
2197
    ret = 0;
2198

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

2209 2210 2211 2212 2213 2214 2215
static int
testDomainRestore(virConnectPtr conn,
                  const char *path)
{
    return testDomainRestoreFlags(conn, path, NULL, 0);
}

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

E
Eric Blake 已提交
2227 2228
    virCheckFlags(VIR_DUMP_CRASH, -1);

2229

2230
    if (!(privdom = testDomObjFromDomain(domain)))
2231
        goto cleanup;
2232

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

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

2258 2259 2260 2261 2262 2263 2264
    /* 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;
    }

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

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

2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295

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)
{
2296 2297 2298
    char *ret;

    ignore_value(VIR_STRDUP(ret, "linux"));
2299
    return ret;
2300 2301
}

2302 2303 2304

static unsigned long long
testDomainGetMaxMemory(virDomainPtr domain)
2305
{
2306
    virDomainObjPtr privdom;
2307
    unsigned long long ret = 0;
2308

2309 2310
    if (!(privdom = testDomObjFromDomain(domain)))
        return 0;
2311

2312
    ret = virDomainDefGetMemoryTotal(privdom->def);
2313

2314
    virDomainObjEndAPI(&privdom);
2315
    return ret;
2316 2317
}

2318 2319
static int testDomainSetMaxMemory(virDomainPtr domain,
                                  unsigned long memory)
2320
{
2321 2322
    virDomainObjPtr privdom;

2323 2324
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2325 2326

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

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

2333 2334
static int testDomainSetMemory(virDomainPtr domain,
                               unsigned long memory)
2335
{
2336
    virDomainObjPtr privdom;
2337
    int ret = -1;
2338

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

2342
    if (memory > virDomainDefGetMemoryTotal(privdom->def)) {
2343
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2344
        goto cleanup;
2345
    }
2346

2347
    privdom->def->mem.cur_balloon = memory;
2348 2349
    ret = 0;

2350
 cleanup:
2351
    virDomainObjEndAPI(&privdom);
2352
    return ret;
2353 2354
}

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

2362 2363
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2364 2365
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

2366 2367
    if (!(vm = testDomObjFromDomain(domain)))
        return -1;
2368

2369
    if (!(def = virDomainObjGetOneDef(vm, flags)))
2370
        goto cleanup;
2371

2372 2373 2374
    if (flags & VIR_DOMAIN_VCPU_MAXIMUM)
        ret = virDomainDefGetVcpusMax(def);
    else
2375
        ret = virDomainDefGetVcpus(def);
2376

2377
 cleanup:
2378
    virDomainObjEndAPI(&vm);
2379
    return ret;
C
Cole Robinson 已提交
2380 2381
}

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

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

2399 2400
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2401 2402
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

2403
    if ((maxvcpus = testConnectGetMaxVcpus(domain->conn, NULL)) < 0)
2404
        return -1;
2405 2406

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

2413 2414
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2415

2416
    if (virDomainObjGetDefs(privdom, flags, &def, &persistentDef) < 0)
C
Cole Robinson 已提交
2417 2418
        goto cleanup;

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

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

2435 2436 2437
    if (def &&
        virDomainDefSetVcpus(def, nrCpus) < 0)
        goto cleanup;
2438

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

2450 2451
    ret = 0;

2452
 cleanup:
2453
    virDomainObjEndAPI(&privdom);
2454
    return ret;
2455 2456
}

2457
static int
2458
testDomainSetVcpus(virDomainPtr domain, unsigned int nrCpus)
2459
{
2460
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2461 2462
}

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

2479 2480
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
C
Cole Robinson 已提交
2481 2482

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

2488
    def = privdom->def;
C
Cole Robinson 已提交
2489 2490

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

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

    hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
2499 2500 2501 2502 2503
    if (!(allcpumap = virBitmapNew(hostcpus)))
        goto cleanup;

    virBitmapSetAll(allcpumap);

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

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

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

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

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

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

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

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

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

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

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

2556 2557
    def = privdom->def;

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

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

2572 2573 2574
    virBitmapFree(vcpuinfo->cpumask);

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

    ret = 0;
2578

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

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

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

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

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

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

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

2618 2619
    /* Flags checked by virDomainDefFormat */

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

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

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

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

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

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

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

2645
    testDriverPtr privconn = conn->privateData;
2646 2647

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

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

    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);
2665

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

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

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

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

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

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

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

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

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

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, NULL);

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

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

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

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

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

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

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

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

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


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

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

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

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

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

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

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

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

2884 2885
    virCheckFlags(0, -1);

2886
    testDriverLock(privconn);
2887

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

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

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

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

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

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

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

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

2931

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

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

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

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

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

2971
    ret = 0;
2972

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

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

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

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

2992
    *autostart = privdom->autostart;
2993

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


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

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

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

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

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

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

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

3023 3024 3025
    return type;
}

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

3035 3036
    virCheckFlags(0, -1);

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

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

    *nparams = 1;
3047 3048
    ret = 0;

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

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

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

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

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

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

3089 3090
    ret = 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3209

3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227
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;
}


3228 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 3263 3264
static virNetworkPtr
testNetworkLookupByName(virConnectPtr conn,
                        const char *name)
3265
{
3266
    testDriverPtr privconn = conn->privateData;
3267 3268
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
3269

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

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

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


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

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

3292 3293 3294 3295 3296 3297

static int
testConnectListNetworks(virConnectPtr conn,
                        char **const names,
                        int nnames)
{
3298
    testDriverPtr privconn = conn->privateData;
3299
    int n;
3300

3301 3302
    n = virNetworkObjListGetNames(privconn->networks,
                                  true, names, nnames, NULL, conn);
3303
    return n;
3304 3305
}

3306 3307 3308

static int
testConnectNumOfDefinedNetworks(virConnectPtr conn)
3309
{
3310
    testDriverPtr privconn = conn->privateData;
3311
    int numInactive;
3312

3313 3314
    numInactive = virNetworkObjListNumOfNetworks(privconn->networks,
                                                 false, NULL, conn);
3315
    return numInactive;
3316 3317
}

3318 3319 3320 3321 3322 3323

static int
testConnectListDefinedNetworks(virConnectPtr conn,
                               char **const names,
                               int nnames)
{
3324
    testDriverPtr privconn = conn->privateData;
3325
    int n;
3326

3327 3328
    n = virNetworkObjListGetNames(privconn->networks,
                                  false, names, nnames, NULL, conn);
3329
    return n;
3330 3331
}

3332

3333
static int
3334
testConnectListAllNetworks(virConnectPtr conn,
3335 3336 3337
                           virNetworkPtr **nets,
                           unsigned int flags)
{
3338
    testDriverPtr privconn = conn->privateData;
3339 3340 3341

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

3342
    return virNetworkObjListExport(conn, privconn->networks, nets, NULL, flags);
3343
}
3344

3345 3346 3347

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

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

3356 3357
    ret = virNetworkObjIsActive(obj);

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

3363 3364 3365

static int
testNetworkIsPersistent(virNetworkPtr net)
3366
{
3367
    testDriverPtr privconn = net->conn->privateData;
3368 3369 3370
    virNetworkObjPtr obj;
    int ret = -1;

3371
    if (!(obj = testNetworkObjFindByUUID(privconn, net->uuid)))
3372
        goto cleanup;
3373

3374 3375
    ret = obj->persistent;

3376
 cleanup:
3377
    virNetworkObjEndAPI(&obj);
3378 3379 3380 3381
    return ret;
}


3382 3383
static virNetworkPtr
testNetworkCreateXML(virConnectPtr conn, const char *xml)
3384
{
3385
    testDriverPtr privconn = conn->privateData;
3386
    virNetworkDefPtr def;
3387
    virNetworkObjPtr net = NULL;
3388
    virNetworkPtr ret = NULL;
3389
    virObjectEventPtr event = NULL;
3390

3391
    if ((def = virNetworkDefParseString(xml)) == NULL)
3392
        goto cleanup;
3393

3394 3395 3396
    if (!(net = virNetworkObjAssignDef(privconn->networks, def,
                                       VIR_NETWORK_OBJ_LIST_ADD_LIVE |
                                       VIR_NETWORK_OBJ_LIST_ADD_CHECK_LIVE)))
3397 3398
        goto cleanup;
    def = NULL;
3399
    net->active = 1;
3400

3401
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3402 3403
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3404

3405
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3406

3407
 cleanup:
3408
    virNetworkDefFree(def);
3409
    testObjectEventQueue(privconn, event);
3410
    virNetworkObjEndAPI(&net);
3411
    return ret;
3412 3413
}

3414 3415 3416 3417

static virNetworkPtr
testNetworkDefineXML(virConnectPtr conn,
                     const char *xml)
3418
{
3419
    testDriverPtr privconn = conn->privateData;
3420
    virNetworkDefPtr def;
3421
    virNetworkObjPtr net = NULL;
3422
    virNetworkPtr ret = NULL;
3423
    virObjectEventPtr event = NULL;
3424

3425
    if ((def = virNetworkDefParseString(xml)) == NULL)
3426
        goto cleanup;
3427

3428
    if (!(net = virNetworkObjAssignDef(privconn->networks, def, 0)))
3429 3430
        goto cleanup;
    def = NULL;
3431

3432
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3433 3434
                                        VIR_NETWORK_EVENT_DEFINED,
                                        0);
3435

3436
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3437

3438
 cleanup:
3439
    virNetworkDefFree(def);
3440
    testObjectEventQueue(privconn, event);
3441
    virNetworkObjEndAPI(&net);
3442
    return ret;
3443 3444
}

3445 3446 3447

static int
testNetworkUndefine(virNetworkPtr network)
3448
{
3449
    testDriverPtr privconn = network->conn->privateData;
3450
    virNetworkObjPtr privnet;
3451
    int ret = -1;
3452
    virObjectEventPtr event = NULL;
3453

3454
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3455
        goto cleanup;
3456

D
Daniel P. Berrange 已提交
3457
    if (virNetworkObjIsActive(privnet)) {
3458 3459
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is still running"), network->name);
3460
        goto cleanup;
3461 3462
    }

3463
    event = virNetworkEventLifecycleNew(network->name, network->uuid,
3464 3465
                                        VIR_NETWORK_EVENT_UNDEFINED,
                                        0);
3466

3467
    virNetworkObjRemoveInactive(privconn->networks, privnet);
3468
    ret = 0;
3469

3470
 cleanup:
3471
    testObjectEventQueue(privconn, event);
3472
    virNetworkObjEndAPI(&privnet);
3473
    return ret;
3474 3475
}

3476

3477 3478 3479 3480 3481 3482 3483 3484
static int
testNetworkUpdate(virNetworkPtr net,
                  unsigned int command,
                  unsigned int section,
                  int parentIndex,
                  const char *xml,
                  unsigned int flags)
{
3485
    testDriverPtr privconn = net->conn->privateData;
3486 3487 3488 3489 3490 3491 3492
    virNetworkObjPtr network = NULL;
    int isActive, ret = -1;

    virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE |
                  VIR_NETWORK_UPDATE_AFFECT_CONFIG,
                  -1);

3493
    if (!(network = testNetworkObjFindByUUID(privconn, net->uuid)))
3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513
        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;
3514
 cleanup:
3515
    virNetworkObjEndAPI(&network);
3516 3517 3518
    return ret;
}

3519 3520 3521

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

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

D
Daniel P. Berrange 已提交
3531
    if (virNetworkObjIsActive(privnet)) {
3532 3533
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is already running"), network->name);
3534
        goto cleanup;
3535 3536
    }

3537
    privnet->active = 1;
3538
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3539 3540
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3541
    ret = 0;
3542

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

3549 3550 3551

static int
testNetworkDestroy(virNetworkPtr network)
3552
{
3553
    testDriverPtr privconn = network->conn->privateData;
3554
    virNetworkObjPtr privnet;
3555
    int ret = -1;
3556
    virObjectEventPtr event = NULL;
3557

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

3561
    privnet->active = 0;
3562
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3563 3564
                                        VIR_NETWORK_EVENT_STOPPED,
                                        0);
3565
    if (!privnet->persistent)
3566
        virNetworkObjRemoveInactive(privconn->networks, privnet);
3567

3568 3569
    ret = 0;

3570
 cleanup:
3571
    testObjectEventQueue(privconn, event);
3572
    virNetworkObjEndAPI(&privnet);
3573
    return ret;
3574 3575
}

3576 3577 3578 3579

static char *
testNetworkGetXMLDesc(virNetworkPtr network,
                      unsigned int flags)
3580
{
3581
    testDriverPtr privconn = network->conn->privateData;
3582
    virNetworkObjPtr privnet;
3583
    char *ret = NULL;
3584

E
Eric Blake 已提交
3585 3586
    virCheckFlags(0, NULL);

3587
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3588
        goto cleanup;
3589

3590
    ret = virNetworkDefFormat(privnet->def, flags);
3591

3592
 cleanup:
3593
    virNetworkObjEndAPI(&privnet);
3594
    return ret;
3595 3596
}

3597 3598 3599 3600

static char *
testNetworkGetBridgeName(virNetworkPtr network)
{
3601
    testDriverPtr privconn = network->conn->privateData;
3602
    char *bridge = NULL;
3603 3604
    virNetworkObjPtr privnet;

3605
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3606
        goto cleanup;
3607

3608
    if (!(privnet->def->bridge)) {
3609 3610 3611
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("network '%s' does not have a bridge name."),
                       privnet->def->name);
3612 3613 3614
        goto cleanup;
    }

3615
    ignore_value(VIR_STRDUP(bridge, privnet->def->bridge));
3616

3617
 cleanup:
3618
    virNetworkObjEndAPI(&privnet);
3619 3620 3621
    return bridge;
}

3622 3623 3624 3625

static int
testNetworkGetAutostart(virNetworkPtr network,
                        int *autostart)
3626
{
3627
    testDriverPtr privconn = network->conn->privateData;
3628
    virNetworkObjPtr privnet;
3629
    int ret = -1;
3630

3631
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3632
        goto cleanup;
3633

3634
    *autostart = privnet->autostart;
3635 3636
    ret = 0;

3637
 cleanup:
3638
    virNetworkObjEndAPI(&privnet);
3639
    return ret;
3640 3641
}

3642 3643 3644 3645

static int
testNetworkSetAutostart(virNetworkPtr network,
                        int autostart)
3646
{
3647
    testDriverPtr privconn = network->conn->privateData;
3648
    virNetworkObjPtr privnet;
3649
    int ret = -1;
3650

3651
    if (!(privnet = testNetworkObjFindByName(privconn, network->name)))
3652
        goto cleanup;
3653

3654
    privnet->autostart = autostart ? 1 : 0;
3655 3656
    ret = 0;

3657
 cleanup:
3658
    virNetworkObjEndAPI(&privnet);
3659
    return ret;
3660
}
3661

C
Cole Robinson 已提交
3662

L
Laine Stump 已提交
3663 3664 3665 3666 3667
/*
 * Physical host interface routines
 */


3668 3669 3670 3671
static virInterfaceObjPtr
testInterfaceObjFindByName(testDriverPtr privconn,
                           const char *name)
{
3672
    virInterfaceObjPtr obj;
3673 3674

    testDriverLock(privconn);
3675
    obj = virInterfaceObjListFindByName(privconn->ifaces, name);
3676 3677
    testDriverUnlock(privconn);

3678
    if (!obj)
3679 3680 3681 3682
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("no interface with matching name '%s'"),
                       name);

3683
    return obj;
3684 3685 3686
}


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

    testDriverLock(privconn);
3694
    ninterfaces = virInterfaceObjListNumOfInterfaces(privconn->ifaces, true);
L
Laine Stump 已提交
3695
    testDriverUnlock(privconn);
3696
    return ninterfaces;
L
Laine Stump 已提交
3697 3698
}

3699 3700 3701 3702 3703

static int
testConnectListInterfaces(virConnectPtr conn,
                          char **const names,
                          int maxnames)
L
Laine Stump 已提交
3704
{
3705
    testDriverPtr privconn = conn->privateData;
3706
    int nnames;
L
Laine Stump 已提交
3707 3708

    testDriverLock(privconn);
3709 3710
    nnames = virInterfaceObjListGetNames(privconn->ifaces, true,
                                         names, maxnames);
L
Laine Stump 已提交
3711 3712
    testDriverUnlock(privconn);

3713
    return nnames;
L
Laine Stump 已提交
3714 3715
}

3716 3717 3718

static int
testConnectNumOfDefinedInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3719
{
3720
    testDriverPtr privconn = conn->privateData;
3721
    int ninterfaces;
L
Laine Stump 已提交
3722 3723

    testDriverLock(privconn);
3724
    ninterfaces = virInterfaceObjListNumOfInterfaces(privconn->ifaces, false);
L
Laine Stump 已提交
3725
    testDriverUnlock(privconn);
3726
    return ninterfaces;
L
Laine Stump 已提交
3727 3728
}

3729 3730 3731 3732 3733

static int
testConnectListDefinedInterfaces(virConnectPtr conn,
                                 char **const names,
                                 int maxnames)
L
Laine Stump 已提交
3734
{
3735
    testDriverPtr privconn = conn->privateData;
3736
    int nnames;
L
Laine Stump 已提交
3737 3738

    testDriverLock(privconn);
3739 3740
    nnames = virInterfaceObjListGetNames(privconn->ifaces, false,
                                         names, maxnames);
L
Laine Stump 已提交
3741 3742
    testDriverUnlock(privconn);

3743
    return nnames;
L
Laine Stump 已提交
3744 3745
}

3746 3747 3748 3749

static virInterfacePtr
testInterfaceLookupByName(virConnectPtr conn,
                          const char *name)
L
Laine Stump 已提交
3750
{
3751
    testDriverPtr privconn = conn->privateData;
3752
    virInterfaceObjPtr obj;
3753
    virInterfaceDefPtr def;
L
Laine Stump 已提交
3754 3755
    virInterfacePtr ret = NULL;

3756
    if (!(obj = testInterfaceObjFindByName(privconn, name)))
3757
        return NULL;
3758
    def = virInterfaceObjGetDef(obj);
L
Laine Stump 已提交
3759

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

3762
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
3763 3764 3765
    return ret;
}

3766 3767 3768 3769

static virInterfacePtr
testInterfaceLookupByMACString(virConnectPtr conn,
                               const char *mac)
L
Laine Stump 已提交
3770
{
3771
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3772
    int ifacect;
3773
    char *ifacenames[] = { NULL, NULL };
L
Laine Stump 已提交
3774 3775 3776
    virInterfacePtr ret = NULL;

    testDriverLock(privconn);
3777 3778
    ifacect = virInterfaceObjListFindByMACString(privconn->ifaces, mac,
                                                 ifacenames, 2);
L
Laine Stump 已提交
3779 3780 3781
    testDriverUnlock(privconn);

    if (ifacect == 0) {
3782 3783
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("no interface with matching mac '%s'"), mac);
L
Laine Stump 已提交
3784 3785 3786 3787
        goto cleanup;
    }

    if (ifacect > 1) {
3788
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3789 3790 3791
        goto cleanup;
    }

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

3794
 cleanup:
3795 3796
    VIR_FREE(ifacenames[0]);
    VIR_FREE(ifacenames[1]);
L
Laine Stump 已提交
3797 3798 3799
    return ret;
}

3800 3801 3802

static int
testInterfaceIsActive(virInterfacePtr iface)
3803
{
3804
    testDriverPtr privconn = iface->conn->privateData;
3805 3806 3807
    virInterfaceObjPtr obj;
    int ret = -1;

3808
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3809
        return -1;
3810

3811 3812
    ret = virInterfaceObjIsActive(obj);

3813
    virInterfaceObjEndAPI(&obj);
3814 3815 3816
    return ret;
}

3817 3818 3819 3820

static int
testInterfaceChangeBegin(virConnectPtr conn,
                         unsigned int flags)
3821
{
3822
    testDriverPtr privconn = conn->privateData;
3823 3824
    int ret = -1;

E
Eric Blake 已提交
3825 3826
    virCheckFlags(0, -1);

3827 3828
    testDriverLock(privconn);
    if (privconn->transaction_running) {
3829
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3830
                       _("there is another transaction running."));
3831 3832 3833 3834 3835
        goto cleanup;
    }

    privconn->transaction_running = true;

3836
    if (!(privconn->backupIfaces = virInterfaceObjListClone(privconn->ifaces)))
3837 3838 3839
        goto cleanup;

    ret = 0;
3840
 cleanup:
3841 3842 3843 3844
    testDriverUnlock(privconn);
    return ret;
}

3845 3846 3847 3848

static int
testInterfaceChangeCommit(virConnectPtr conn,
                          unsigned int flags)
3849
{
3850
    testDriverPtr privconn = conn->privateData;
3851 3852
    int ret = -1;

E
Eric Blake 已提交
3853 3854
    virCheckFlags(0, -1);

3855 3856 3857
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3858
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3859 3860
                       _("no transaction running, "
                         "nothing to be committed."));
3861 3862 3863
        goto cleanup;
    }

3864
    virInterfaceObjListFree(privconn->backupIfaces);
3865 3866 3867 3868
    privconn->transaction_running = false;

    ret = 0;

3869
 cleanup:
3870 3871 3872 3873 3874
    testDriverUnlock(privconn);

    return ret;
}

3875 3876 3877 3878

static int
testInterfaceChangeRollback(virConnectPtr conn,
                            unsigned int flags)
3879
{
3880
    testDriverPtr privconn = conn->privateData;
3881 3882
    int ret = -1;

E
Eric Blake 已提交
3883 3884
    virCheckFlags(0, -1);

3885 3886 3887
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3888
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3889 3890
                       _("no transaction running, "
                         "nothing to rollback."));
3891 3892 3893
        goto cleanup;
    }

3894 3895 3896
    virInterfaceObjListFree(privconn->ifaces);
    privconn->ifaces = privconn->backupIfaces;
    privconn->backupIfaces = NULL;
3897 3898 3899 3900 3901

    privconn->transaction_running = false;

    ret = 0;

3902
 cleanup:
3903 3904 3905
    testDriverUnlock(privconn);
    return ret;
}
3906

3907 3908 3909 3910

static char *
testInterfaceGetXMLDesc(virInterfacePtr iface,
                        unsigned int flags)
L
Laine Stump 已提交
3911
{
3912
    testDriverPtr privconn = iface->conn->privateData;
3913
    virInterfaceObjPtr obj;
3914
    virInterfaceDefPtr def;
L
Laine Stump 已提交
3915 3916
    char *ret = NULL;

E
Eric Blake 已提交
3917 3918
    virCheckFlags(0, NULL);

3919
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3920
        return NULL;
3921
    def = virInterfaceObjGetDef(obj);
L
Laine Stump 已提交
3922

3923
    ret = virInterfaceDefFormat(def);
L
Laine Stump 已提交
3924

3925
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
3926 3927 3928 3929
    return ret;
}


3930 3931 3932 3933
static virInterfacePtr
testInterfaceDefineXML(virConnectPtr conn,
                       const char *xmlStr,
                       unsigned int flags)
L
Laine Stump 已提交
3934
{
3935
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3936
    virInterfaceDefPtr def;
3937
    virInterfaceObjPtr obj = NULL;
3938
    virInterfaceDefPtr objdef;
L
Laine Stump 已提交
3939 3940
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
3941 3942
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3943
    testDriverLock(privconn);
3944
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3945 3946
        goto cleanup;

3947
    if ((obj = virInterfaceObjListAssignDef(privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3948 3949
        goto cleanup;
    def = NULL;
3950
    objdef = virInterfaceObjGetDef(obj);
L
Laine Stump 已提交
3951

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

3954
 cleanup:
L
Laine Stump 已提交
3955
    virInterfaceDefFree(def);
3956
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
3957 3958 3959 3960
    testDriverUnlock(privconn);
    return ret;
}

3961 3962 3963

static int
testInterfaceUndefine(virInterfacePtr iface)
L
Laine Stump 已提交
3964
{
3965
    testDriverPtr privconn = iface->conn->privateData;
3966
    virInterfaceObjPtr obj;
L
Laine Stump 已提交
3967

3968
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3969
        return -1;
L
Laine Stump 已提交
3970

3971
    virInterfaceObjListRemove(privconn->ifaces, obj);
3972
    virObjectUnref(obj);
L
Laine Stump 已提交
3973

3974
    return 0;
L
Laine Stump 已提交
3975 3976
}

3977 3978 3979 3980

static int
testInterfaceCreate(virInterfacePtr iface,
                    unsigned int flags)
L
Laine Stump 已提交
3981
{
3982
    testDriverPtr privconn = iface->conn->privateData;
3983
    virInterfaceObjPtr obj;
L
Laine Stump 已提交
3984 3985
    int ret = -1;

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

3988
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3989
        return -1;
L
Laine Stump 已提交
3990

3991
    if (virInterfaceObjIsActive(obj)) {
3992
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3993 3994 3995
        goto cleanup;
    }

3996
    virInterfaceObjSetActive(obj, true);
L
Laine Stump 已提交
3997 3998
    ret = 0;

3999
 cleanup:
4000
    virInterfaceObjEndAPI(&obj);
L
Laine Stump 已提交
4001 4002 4003
    return ret;
}

4004 4005 4006 4007

static int
testInterfaceDestroy(virInterfacePtr iface,
                     unsigned int flags)
L
Laine Stump 已提交
4008
{
4009
    testDriverPtr privconn = iface->conn->privateData;
4010
    virInterfaceObjPtr obj;
L
Laine Stump 已提交
4011 4012
    int ret = -1;

E
Eric Blake 已提交
4013 4014
    virCheckFlags(0, -1);

4015
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
4016
        return -1;
L
Laine Stump 已提交
4017

4018
    if (!virInterfaceObjIsActive(obj)) {
4019
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4020 4021 4022
        goto cleanup;
    }

4023
    virInterfaceObjSetActive(obj, false);
L
Laine Stump 已提交
4024 4025
    ret = 0;

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



C
Cole Robinson 已提交
4033 4034 4035 4036
/*
 * Storage Driver routines
 */

4037

4038 4039
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool)
{
C
Cole Robinson 已提交
4040 4041 4042 4043 4044

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

4045
    return VIR_STRDUP(pool->configFile, "");
C
Cole Robinson 已提交
4046 4047
}

4048

4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067
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;
}


4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089
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 已提交
4090 4091
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
4092 4093
                            const unsigned char *uuid)
{
4094
    testDriverPtr privconn = conn->privateData;
4095 4096
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4097

4098
    if (!(pool = testStoragePoolObjFindByUUID(privconn, uuid)))
4099
        goto cleanup;
C
Cole Robinson 已提交
4100

4101 4102
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4103

4104
 cleanup:
4105 4106
    if (pool)
        virStoragePoolObjUnlock(pool);
4107
    return ret;
C
Cole Robinson 已提交
4108 4109 4110 4111
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
4112 4113
                            const char *name)
{
4114
    testDriverPtr privconn = conn->privateData;
4115 4116
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4117

4118
    if (!(pool = testStoragePoolObjFindByName(privconn, name)))
4119
        goto cleanup;
C
Cole Robinson 已提交
4120

4121 4122
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4123

4124
 cleanup:
4125 4126
    if (pool)
        virStoragePoolObjUnlock(pool);
4127
    return ret;
C
Cole Robinson 已提交
4128 4129 4130
}

static virStoragePoolPtr
4131 4132
testStoragePoolLookupByVolume(virStorageVolPtr vol)
{
C
Cole Robinson 已提交
4133 4134 4135
    return testStoragePoolLookupByName(vol->conn, vol->pool);
}

4136

C
Cole Robinson 已提交
4137
static int
4138 4139
testConnectNumOfStoragePools(virConnectPtr conn)
{
4140
    testDriverPtr privconn = conn->privateData;
4141
    int numActive = 0;
C
Cole Robinson 已提交
4142

4143
    testDriverLock(privconn);
4144 4145
    numActive = virStoragePoolObjNumOfStoragePools(&privconn->pools, conn,
                                                   true, NULL);
4146
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4147 4148 4149 4150

    return numActive;
}

4151

C
Cole Robinson 已提交
4152
static int
4153 4154
testConnectListStoragePools(virConnectPtr conn,
                            char **const names,
4155
                            int maxnames)
4156
{
4157
    testDriverPtr privconn = conn->privateData;
4158
    int n = 0;
C
Cole Robinson 已提交
4159

4160
    testDriverLock(privconn);
4161 4162
    n = virStoragePoolObjGetNames(&privconn->pools, conn, true, NULL,
                                  names, maxnames);
4163
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4164 4165 4166 4167

    return n;
}

4168

C
Cole Robinson 已提交
4169
static int
4170 4171
testConnectNumOfDefinedStoragePools(virConnectPtr conn)
{
4172
    testDriverPtr privconn = conn->privateData;
4173
    int numInactive = 0;
C
Cole Robinson 已提交
4174

4175
    testDriverLock(privconn);
4176 4177
    numInactive = virStoragePoolObjNumOfStoragePools(&privconn->pools, conn,
                                                     false, NULL);
4178
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4179 4180 4181 4182

    return numInactive;
}

4183

C
Cole Robinson 已提交
4184
static int
4185 4186
testConnectListDefinedStoragePools(virConnectPtr conn,
                                   char **const names,
4187
                                   int maxnames)
4188
{
4189
    testDriverPtr privconn = conn->privateData;
4190
    int n = 0;
C
Cole Robinson 已提交
4191

4192
    testDriverLock(privconn);
4193 4194
    n = virStoragePoolObjGetNames(&privconn->pools, conn, false, NULL,
                                  names, maxnames);
4195
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4196 4197 4198 4199

    return n;
}

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

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

    testDriverLock(privconn);
J
John Ferlan 已提交
4211
    ret = virStoragePoolObjListExport(conn, &privconn->pools, pools,
4212
                                      NULL, flags);
4213 4214 4215 4216
    testDriverUnlock(privconn);

    return ret;
}
C
Cole Robinson 已提交
4217

4218 4219
static int testStoragePoolIsActive(virStoragePoolPtr pool)
{
4220
    testDriverPtr privconn = pool->conn->privateData;
4221 4222 4223
    virStoragePoolObjPtr obj;
    int ret = -1;

4224
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4225
        goto cleanup;
4226

4227 4228
    ret = virStoragePoolObjIsActive(obj);

4229
 cleanup:
4230 4231 4232 4233 4234 4235 4236
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}

static int testStoragePoolIsPersistent(virStoragePoolPtr pool)
{
4237
    testDriverPtr privconn = pool->conn->privateData;
4238 4239 4240
    virStoragePoolObjPtr obj;
    int ret = -1;

4241
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4242
        goto cleanup;
4243

4244 4245
    ret = obj->configFile ? 1 : 0;

4246
 cleanup:
4247 4248 4249 4250 4251 4252 4253
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}



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

E
Eric Blake 已提交
4263 4264
    virCheckFlags(0, -1);

4265
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4266
        goto cleanup;
4267

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

    privpool->active = 1;
4275 4276 4277 4278

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

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

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

E
Eric Blake 已提交
4298 4299
    virCheckFlags(0, NULL);

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

    if (srcSpec) {
4308
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4309 4310 4311 4312 4313 4314 4315
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
4316
        ignore_value(VIR_STRDUP(ret, defaultPoolSourcesLogicalXML));
4317 4318 4319
        break;

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

4326 4327
        ignore_value(virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                                 source->hosts[0].name));
4328 4329 4330
        break;

    default:
4331 4332
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4333 4334
    }

4335
 cleanup:
4336 4337
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4338 4339 4340
}


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

E
Eric Blake 已提交
4380 4381
    virCheckFlags(0, NULL);

4382
    testDriverLock(privconn);
4383
    if (!(def = virStoragePoolDefParseString(xml)))
4384
        goto cleanup;
C
Cole Robinson 已提交
4385

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

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

4399
    if (pool->def->source.adapter.type == VIR_STORAGE_ADAPTER_TYPE_FC_HOST) {
4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412
        /* 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;
        }
    }

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

    /* *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 已提交
4424 4425
    pool->active = 1;

4426 4427 4428 4429
    event = virStoragePoolEventLifecycleNew(pool->def->name, pool->def->uuid,
                                            VIR_STORAGE_POOL_EVENT_STARTED,
                                            0);

4430 4431
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4432

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

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

E
Eric Blake 已提交
4453 4454
    virCheckFlags(0, NULL);

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

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

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

4467 4468 4469 4470
    event = virStoragePoolEventLifecycleNew(pool->def->name, pool->def->uuid,
                                            VIR_STORAGE_POOL_EVENT_DEFINED,
                                            0);

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

4477 4478
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4479

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

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

4497
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4498
        goto cleanup;
4499

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

4506 4507 4508 4509
    event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid,
                                            VIR_STORAGE_POOL_EVENT_UNDEFINED,
                                            0);

C
Cole Robinson 已提交
4510
    virStoragePoolObjRemove(&privconn->pools, privpool);
4511
    privpool = NULL;
4512
    ret = 0;
C
Cole Robinson 已提交
4513

4514
 cleanup:
4515 4516
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4517
    testObjectEventQueue(privconn, event);
4518
    return ret;
C
Cole Robinson 已提交
4519 4520 4521
}

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

E
Eric Blake 已提交
4529 4530
    virCheckFlags(0, -1);

4531
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4532
        goto cleanup;
4533

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

4541
 cleanup:
4542 4543
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4544
    return ret;
C
Cole Robinson 已提交
4545 4546 4547
}


4548 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
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 已提交
4586
static int
4587 4588
testStoragePoolDestroy(virStoragePoolPtr pool)
{
4589
    testDriverPtr privconn = pool->conn->privateData;
4590
    virStoragePoolObjPtr privpool;
4591
    int ret = -1;
4592
    virObjectEventPtr event = NULL;
4593

4594
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4595
        goto cleanup;
4596 4597

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

    privpool->active = 0;
4604 4605

    if (privpool->def->source.adapter.type ==
4606
        VIR_STORAGE_ADAPTER_TYPE_FC_HOST) {
4607 4608 4609 4610 4611 4612 4613 4614
        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,
4615 4616
                                            VIR_STORAGE_POOL_EVENT_STOPPED,
                                            0);
C
Cole Robinson 已提交
4617

4618
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4619
        virStoragePoolObjRemove(&privconn->pools, privpool);
4620 4621
        privpool = NULL;
    }
4622
    ret = 0;
C
Cole Robinson 已提交
4623

4624
 cleanup:
4625
    testObjectEventQueue(privconn, event);
4626 4627
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4628
    return ret;
C
Cole Robinson 已提交
4629 4630 4631 4632
}


static int
4633
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4634 4635
                      unsigned int flags)
{
4636
    testDriverPtr privconn = pool->conn->privateData;
4637
    virStoragePoolObjPtr privpool;
4638
    int ret = -1;
4639

E
Eric Blake 已提交
4640 4641
    virCheckFlags(0, -1);

4642
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4643 4644 4645
        goto cleanup;

    if (virStoragePoolObjIsActive(privpool)) {
4646 4647
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4648
        goto cleanup;
4649 4650
    }

4651
    ret = 0;
C
Cole Robinson 已提交
4652

4653
 cleanup:
4654 4655
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4656
    return ret;
C
Cole Robinson 已提交
4657 4658 4659 4660
}


static int
4661
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4662 4663
                       unsigned int flags)
{
4664
    testDriverPtr privconn = pool->conn->privateData;
4665
    virStoragePoolObjPtr privpool;
4666
    int ret = -1;
4667
    virObjectEventPtr event = NULL;
4668

E
Eric Blake 已提交
4669 4670
    virCheckFlags(0, -1);

4671
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4672
        goto cleanup;
4673 4674

    if (!virStoragePoolObjIsActive(privpool)) {
4675 4676
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4677
        goto cleanup;
4678
    }
4679

4680
    event = virStoragePoolEventRefreshNew(pool->name, pool->uuid);
4681
    ret = 0;
C
Cole Robinson 已提交
4682

4683
 cleanup:
4684
    testObjectEventQueue(privconn, event);
4685 4686
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4687
    return ret;
C
Cole Robinson 已提交
4688 4689 4690 4691
}


static int
4692
testStoragePoolGetInfo(virStoragePoolPtr pool,
4693 4694
                       virStoragePoolInfoPtr info)
{
4695
    testDriverPtr privconn = pool->conn->privateData;
4696
    virStoragePoolObjPtr privpool;
4697
    int ret = -1;
4698

4699
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4700
        goto cleanup;
C
Cole Robinson 已提交
4701 4702 4703 4704 4705 4706 4707 4708 4709

    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;
4710
    ret = 0;
C
Cole Robinson 已提交
4711

4712
 cleanup:
4713 4714
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4715
    return ret;
C
Cole Robinson 已提交
4716 4717 4718
}

static char *
4719
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4720 4721
                          unsigned int flags)
{
4722
    testDriverPtr privconn = pool->conn->privateData;
4723
    virStoragePoolObjPtr privpool;
4724
    char *ret = NULL;
4725

E
Eric Blake 已提交
4726 4727
    virCheckFlags(0, NULL);

4728
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4729
        goto cleanup;
4730

4731
    ret = virStoragePoolDefFormat(privpool->def);
4732

4733
 cleanup:
4734 4735
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4736
    return ret;
C
Cole Robinson 已提交
4737 4738 4739
}

static int
4740
testStoragePoolGetAutostart(virStoragePoolPtr pool,
4741 4742
                            int *autostart)
{
4743
    testDriverPtr privconn = pool->conn->privateData;
4744
    virStoragePoolObjPtr privpool;
4745
    int ret = -1;
4746

4747
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4748
        goto cleanup;
C
Cole Robinson 已提交
4749 4750 4751 4752 4753 4754

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

4757
 cleanup:
4758 4759
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4760
    return ret;
C
Cole Robinson 已提交
4761 4762 4763
}

static int
4764
testStoragePoolSetAutostart(virStoragePoolPtr pool,
4765 4766
                            int autostart)
{
4767
    testDriverPtr privconn = pool->conn->privateData;
4768
    virStoragePoolObjPtr privpool;
4769
    int ret = -1;
4770

4771
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4772
        goto cleanup;
C
Cole Robinson 已提交
4773 4774

    if (!privpool->configFile) {
4775 4776
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
4777
        goto cleanup;
C
Cole Robinson 已提交
4778 4779 4780 4781
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4782 4783
    ret = 0;

4784
 cleanup:
4785 4786
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4787
    return ret;
C
Cole Robinson 已提交
4788 4789 4790 4791
}


static int
4792 4793
testStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
4794
    testDriverPtr privconn = pool->conn->privateData;
4795
    virStoragePoolObjPtr privpool;
4796
    int ret = -1;
4797

4798
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4799
        goto cleanup;
4800 4801

    if (!virStoragePoolObjIsActive(privpool)) {
4802 4803
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4804
        goto cleanup;
4805
    }
C
Cole Robinson 已提交
4806

4807 4808
    ret = virStoragePoolObjNumOfVolumes(&privpool->volumes, pool->conn,
                                        privpool->def, NULL);
4809

4810
 cleanup:
4811 4812
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4813
    return ret;
C
Cole Robinson 已提交
4814 4815
}

4816

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

4826
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4827
        return -1;
4828 4829

    if (!virStoragePoolObjIsActive(privpool)) {
4830 4831
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4832
        goto cleanup;
4833 4834
    }

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

4838
 cleanup:
4839
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4840 4841 4842
    return n;
}

4843

4844 4845 4846
static int
testStoragePoolListAllVolumes(virStoragePoolPtr obj,
                              virStorageVolPtr **vols,
4847 4848
                              unsigned int flags)
{
4849
    testDriverPtr privconn = obj->conn->privateData;
4850 4851 4852 4853 4854
    virStoragePoolObjPtr pool;
    int ret = -1;

    virCheckFlags(0, -1);

4855
    if (!(pool = testStoragePoolObjFindByUUID(privconn, obj->uuid)))
4856
        return -1;
4857 4858 4859 4860 4861 4862 4863

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

4864 4865
    ret = virStoragePoolObjVolumeListExport(obj->conn, &pool->volumes,
                                            pool->def, vols, NULL);
4866 4867

 cleanup:
4868
    virStoragePoolObjUnlock(pool);
4869 4870 4871

    return ret;
}
C
Cole Robinson 已提交
4872 4873

static virStorageVolPtr
4874
testStorageVolLookupByName(virStoragePoolPtr pool,
4875 4876
                           const char *name ATTRIBUTE_UNUSED)
{
4877
    testDriverPtr privconn = pool->conn->privateData;
4878 4879
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4880
    virStorageVolPtr ret = NULL;
4881

4882
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4883
        goto cleanup;
4884 4885

    if (!virStoragePoolObjIsActive(privpool)) {
4886 4887
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4888
        goto cleanup;
4889 4890 4891 4892 4893
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4894 4895
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"), name);
4896
        goto cleanup;
C
Cole Robinson 已提交
4897 4898
    }

4899
    ret = virGetStorageVol(pool->conn, privpool->def->name,
4900 4901
                           privvol->name, privvol->key,
                           NULL, NULL);
4902

4903
 cleanup:
4904 4905
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4906
    return ret;
C
Cole Robinson 已提交
4907 4908 4909 4910
}


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

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

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

4939
    if (!ret)
4940 4941
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
4942 4943

    return ret;
C
Cole Robinson 已提交
4944 4945 4946
}

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

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

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

4975
    if (!ret)
4976 4977
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
4978 4979

    return ret;
C
Cole Robinson 已提交
4980 4981 4982
}

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

E
Eric Blake 已提交
4992 4993
    virCheckFlags(0, NULL);

4994
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4995
        goto cleanup;
4996 4997

    if (!virStoragePoolObjIsActive(privpool)) {
4998 4999
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5000
        goto cleanup;
5001
    }
C
Cole Robinson 已提交
5002

5003
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5004
    if (privvol == NULL)
5005
        goto cleanup;
5006 5007

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5008 5009
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5010
        goto cleanup;
C
Cole Robinson 已提交
5011 5012 5013
    }

    /* Make sure enough space */
5014
    if ((privpool->def->allocation + privvol->target.allocation) >
C
Cole Robinson 已提交
5015
         privpool->def->capacity) {
5016 5017 5018
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5019
        goto cleanup;
C
Cole Robinson 已提交
5020 5021
    }

5022 5023
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5024
                    privvol->name) == -1)
5025
        goto cleanup;
C
Cole Robinson 已提交
5026

5027 5028 5029
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
5030
        goto cleanup;
C
Cole Robinson 已提交
5031

5032
    privpool->def->allocation += privvol->target.allocation;
C
Cole Robinson 已提交
5033 5034 5035
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5036
    ret = virGetStorageVol(pool->conn, privpool->def->name,
5037 5038
                           privvol->name, privvol->key,
                           NULL, NULL);
5039
    privvol = NULL;
5040

5041
 cleanup:
5042
    virStorageVolDefFree(privvol);
5043 5044
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5045
    return ret;
C
Cole Robinson 已提交
5046 5047
}

5048
static virStorageVolPtr
5049 5050 5051 5052
testStorageVolCreateXMLFrom(virStoragePoolPtr pool,
                            const char *xmldesc,
                            virStorageVolPtr clonevol,
                            unsigned int flags)
E
Eric Blake 已提交
5053
{
5054
    testDriverPtr privconn = pool->conn->privateData;
5055 5056 5057 5058
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
5059 5060
    virCheckFlags(0, NULL);

5061
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
5062 5063 5064
        goto cleanup;

    if (!virStoragePoolObjIsActive(privpool)) {
5065 5066
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5067 5068 5069
        goto cleanup;
    }

5070
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5071 5072 5073 5074
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5075 5076
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5077 5078 5079 5080 5081
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
5082 5083 5084
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
5085 5086 5087 5088
        goto cleanup;
    }

    /* Make sure enough space */
5089
    if ((privpool->def->allocation + privvol->target.allocation) >
5090
         privpool->def->capacity) {
5091 5092 5093
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5094 5095 5096 5097 5098
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5099 5100
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5101
                    privvol->name) == -1)
5102 5103
        goto cleanup;

5104 5105 5106
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
5107 5108
        goto cleanup;

5109
    privpool->def->allocation += privvol->target.allocation;
5110 5111 5112 5113
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    ret = virGetStorageVol(pool->conn, privpool->def->name,
5114 5115
                           privvol->name, privvol->key,
                           NULL, NULL);
5116 5117
    privvol = NULL;

5118
 cleanup:
5119 5120 5121 5122 5123 5124
    virStorageVolDefFree(privvol);
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    return ret;
}

C
Cole Robinson 已提交
5125
static int
5126 5127
testStorageVolDelete(virStorageVolPtr vol,
                     unsigned int flags)
E
Eric Blake 已提交
5128
{
5129
    testDriverPtr privconn = vol->conn->privateData;
5130 5131
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5132
    size_t i;
5133
    int ret = -1;
C
Cole Robinson 已提交
5134

E
Eric Blake 已提交
5135 5136
    virCheckFlags(0, -1);

5137
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5138
        goto cleanup;
5139 5140 5141 5142

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

    if (privvol == NULL) {
5143 5144 5145
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5146
        goto cleanup;
5147 5148 5149
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5150 5151
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5152
        goto cleanup;
5153 5154 5155
    }


5156
    privpool->def->allocation -= privvol->target.allocation;
C
Cole Robinson 已提交
5157 5158 5159
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5160
    for (i = 0; i < privpool->volumes.count; i++) {
C
Cole Robinson 已提交
5161 5162 5163
        if (privpool->volumes.objs[i] == privvol) {
            virStorageVolDefFree(privvol);

5164
            VIR_DELETE_ELEMENT(privpool->volumes.objs, i, privpool->volumes.count);
C
Cole Robinson 已提交
5165 5166 5167
            break;
        }
    }
5168
    ret = 0;
C
Cole Robinson 已提交
5169

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


5177 5178
static int testStorageVolumeTypeForPool(int pooltype)
{
C
Cole Robinson 已提交
5179

5180
    switch (pooltype) {
C
Cole Robinson 已提交
5181 5182 5183 5184 5185 5186 5187 5188 5189 5190
        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
5191
testStorageVolGetInfo(virStorageVolPtr vol,
5192 5193
                      virStorageVolInfoPtr info)
{
5194
    testDriverPtr privconn = vol->conn->privateData;
5195 5196
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5197
    int ret = -1;
5198

5199
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5200
        goto cleanup;
5201 5202 5203 5204

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

    if (privvol == NULL) {
5205 5206 5207
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5208
        goto cleanup;
5209 5210 5211
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5212 5213
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5214
        goto cleanup;
5215
    }
C
Cole Robinson 已提交
5216 5217 5218

    memset(info, 0, sizeof(*info));
    info->type = testStorageVolumeTypeForPool(privpool->def->type);
5219 5220
    info->capacity = privvol->target.capacity;
    info->allocation = privvol->target.allocation;
5221
    ret = 0;
C
Cole Robinson 已提交
5222

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

static char *
5230 5231
testStorageVolGetXMLDesc(virStorageVolPtr vol,
                         unsigned int flags)
E
Eric Blake 已提交
5232
{
5233
    testDriverPtr privconn = vol->conn->privateData;
5234 5235
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5236
    char *ret = NULL;
5237

E
Eric Blake 已提交
5238 5239
    virCheckFlags(0, NULL);

5240
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5241
        goto cleanup;
5242 5243 5244 5245

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

    if (privvol == NULL) {
5246 5247 5248
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5249
        goto cleanup;
5250
    }
C
Cole Robinson 已提交
5251

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

5258
    ret = virStorageVolDefFormat(privpool->def, privvol);
5259

5260
 cleanup:
5261 5262
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5263
    return ret;
C
Cole Robinson 已提交
5264 5265 5266
}

static char *
5267 5268
testStorageVolGetPath(virStorageVolPtr vol)
{
5269
    testDriverPtr privconn = vol->conn->privateData;
5270 5271
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5272
    char *ret = NULL;
5273

5274
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5275
        goto cleanup;
5276 5277 5278 5279

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

    if (privvol == NULL) {
5280 5281 5282
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5283
        goto cleanup;
5284 5285 5286
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5287 5288
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5289
        goto cleanup;
5290 5291
    }

5292
    ignore_value(VIR_STRDUP(ret, privvol->target.path));
5293

5294
 cleanup:
5295 5296
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5297 5298 5299
    return ret;
}

5300

5301
/* Node device implementations */
5302

5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321
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;
}


5322 5323 5324
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5325
                     unsigned int flags)
5326
{
5327
    testDriverPtr driver = conn->privateData;
5328 5329
    int ndevs = 0;

E
Eric Blake 已提交
5330 5331
    virCheckFlags(0, -1);

5332
    testDriverLock(driver);
5333
    ndevs = virNodeDeviceObjNumOfDevices(&driver->devs, conn, cap, NULL);
5334 5335 5336 5337 5338
    testDriverUnlock(driver);

    return ndevs;
}

5339

5340 5341 5342 5343 5344
static int
testNodeListDevices(virConnectPtr conn,
                    const char *cap,
                    char **const names,
                    int maxnames,
E
Eric Blake 已提交
5345
                    unsigned int flags)
5346
{
5347
    testDriverPtr driver = conn->privateData;
5348
    int nnames = 0;
5349

E
Eric Blake 已提交
5350 5351
    virCheckFlags(0, -1);

5352
    testDriverLock(driver);
5353 5354
    nnames = virNodeDeviceObjGetNames(&driver->devs, conn, NULL,
                                     cap, names, maxnames);
5355 5356
    testDriverUnlock(driver);

5357
    return nnames;
5358 5359
}

5360

5361 5362 5363
static virNodeDevicePtr
testNodeDeviceLookupByName(virConnectPtr conn, const char *name)
{
5364
    testDriverPtr driver = conn->privateData;
5365
    virNodeDeviceObjPtr obj;
5366
    virNodeDeviceDefPtr def;
5367 5368
    virNodeDevicePtr ret = NULL;

5369
    if (!(obj = testNodeDeviceObjFindByName(driver, name)))
5370
        goto cleanup;
5371
    def = virNodeDeviceObjGetDef(obj);
5372

5373
    if ((ret = virGetNodeDevice(conn, name))) {
5374
        if (VIR_STRDUP(ret->parent, def->parent) < 0) {
5375
            virObjectUnref(ret);
5376 5377
            ret = NULL;
        }
5378
    }
5379

5380
 cleanup:
5381 5382 5383 5384 5385 5386
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
5387
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5388
                         unsigned int flags)
5389
{
5390
    testDriverPtr driver = dev->conn->privateData;
5391 5392 5393
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5394 5395
    virCheckFlags(0, NULL);

5396
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5397 5398
        goto cleanup;

5399
    ret = virNodeDeviceDefFormat(virNodeDeviceObjGetDef(obj));
5400

5401
 cleanup:
5402 5403 5404 5405 5406 5407 5408 5409
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
testNodeDeviceGetParent(virNodeDevicePtr dev)
{
5410
    testDriverPtr driver = dev->conn->privateData;
5411
    virNodeDeviceObjPtr obj;
5412
    virNodeDeviceDefPtr def;
5413 5414
    char *ret = NULL;

5415
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5416
        goto cleanup;
5417
    def = virNodeDeviceObjGetDef(obj);
5418

5419 5420
    if (def->parent) {
        ignore_value(VIR_STRDUP(ret, def->parent));
5421
    } else {
5422 5423
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5424 5425
    }

5426
 cleanup:
5427 5428 5429 5430 5431
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

5432

5433 5434 5435
static int
testNodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
5436
    testDriverPtr driver = dev->conn->privateData;
5437
    virNodeDeviceObjPtr obj;
5438
    virNodeDeviceDefPtr def;
5439 5440 5441 5442
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

5443
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5444
        goto cleanup;
5445
    def = virNodeDeviceObjGetDef(obj);
5446

5447
    for (caps = def->caps; caps; caps = caps->next)
5448 5449 5450
        ++ncaps;
    ret = ncaps;

5451
 cleanup:
5452 5453 5454 5455 5456 5457 5458 5459 5460
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}


static int
testNodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
5461
    testDriverPtr driver = dev->conn->privateData;
5462
    virNodeDeviceObjPtr obj;
5463
    virNodeDeviceDefPtr def;
5464 5465 5466 5467
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

5468
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5469
        goto cleanup;
5470
    def = virNodeDeviceObjGetDef(obj);
5471

5472
    for (caps = def->caps; caps && ncaps < maxnames; caps = caps->next) {
5473
        if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->data.type)) < 0)
5474 5475 5476 5477
            goto cleanup;
    }
    ret = ncaps;

5478
 cleanup:
5479 5480 5481 5482 5483 5484 5485 5486 5487 5488
    if (obj)
        virNodeDeviceObjUnlock(obj);
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
}

5489

5490 5491
static virNodeDeviceObjPtr
testNodeDeviceMockCreateVport(testDriverPtr driver,
5492
                              const char *wwnn,
5493
                              const char *wwpn)
5494
{
5495 5496
    char *xml = NULL;
    virNodeDeviceDefPtr def = NULL;
5497
    virNodeDevCapsDefPtr caps;
5498
    virNodeDeviceObjPtr obj = NULL, objcopy = NULL;
5499
    virNodeDeviceDefPtr objdef;
5500
    virObjectEventPtr event = NULL;
5501

5502 5503 5504 5505 5506 5507 5508 5509 5510
    /* 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. */
5511
    if (!(objcopy = virNodeDeviceObjFindByName(&driver->devs, "scsi_host11")))
5512 5513
        goto cleanup;

5514
    xml = virNodeDeviceDefFormat(virNodeDeviceObjGetDef(objcopy));
5515 5516 5517 5518 5519
    virNodeDeviceObjUnlock(objcopy);
    if (!xml)
        goto cleanup;

    if (!(def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL)))
5520 5521
        goto cleanup;

5522
    VIR_FREE(def->name);
5523
    if (VIR_STRDUP(def->name, "scsi_host12") < 0)
5524 5525
        goto cleanup;

5526 5527 5528
    /* 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. */
5529 5530
    caps = def->caps;
    while (caps) {
5531
        if (caps->data.type != VIR_NODE_DEV_CAP_SCSI_HOST)
5532 5533
            continue;

5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547
        /* 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++;
        }
5548 5549 5550
        caps = caps->next;
    }

5551
    if (!(obj = virNodeDeviceObjAssignDef(&driver->devs, def)))
5552
        goto cleanup;
5553
    def = NULL;
5554
    objdef = virNodeDeviceObjGetDef(obj);
5555

5556
    event = virNodeDeviceEventLifecycleNew(objdef->name,
5557 5558
                                           VIR_NODE_DEVICE_EVENT_CREATED,
                                           0);
5559 5560 5561
    testObjectEventQueue(driver, event);

 cleanup:
5562
    VIR_FREE(xml);
5563 5564
    virNodeDeviceDefFree(def);
    return obj;
5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575
}


static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
                        unsigned int flags)
{
    testDriverPtr driver = conn->privateData;
    virNodeDeviceDefPtr def = NULL;
    char *wwnn = NULL, *wwpn = NULL;
5576 5577
    virNodeDevicePtr dev = NULL, ret = NULL;
    virNodeDeviceObjPtr obj = NULL;
5578
    virNodeDeviceDefPtr objdef;
5579 5580 5581 5582 5583 5584 5585 5586

    virCheckFlags(0, NULL);

    testDriverLock(driver);

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

5587 5588 5589
    /* We run this simply for validation - it essentially validates that
     * the input XML either has a wwnn/wwpn or virNodeDevCapSCSIHostParseXML
     * generated a wwnn/wwpn */
5590 5591 5592
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) < 0)
        goto cleanup;

5593 5594 5595
    /* 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. */
5596
    if (virNodeDeviceObjGetParentHost(&driver->devs, def, CREATE_DEVICE) < 0)
5597 5598 5599 5600
        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
5601 5602 5603
     * 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 */
5604 5605
    if (!(obj = testNodeDeviceMockCreateVport(driver, wwnn, wwpn)))
        goto cleanup;
5606
    objdef = virNodeDeviceObjGetDef(obj);
5607

5608
    if (!(dev = virGetNodeDevice(conn, objdef->name)))
5609 5610 5611 5612 5613 5614 5615 5616
        goto cleanup;

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

    ret = dev;
    dev = NULL;
5617

5618
 cleanup:
5619 5620
    if (obj)
        virNodeDeviceObjUnlock(obj);
5621
    testDriverUnlock(driver);
5622
    virNodeDeviceDefFree(def);
5623
    virObjectUnref(dev);
5624 5625
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
5626
    return ret;
5627 5628 5629 5630 5631 5632
}

static int
testNodeDeviceDestroy(virNodeDevicePtr dev)
{
    int ret = 0;
5633
    testDriverPtr driver = dev->conn->privateData;
5634
    virNodeDeviceObjPtr obj = NULL;
5635
    virNodeDeviceDefPtr def;
5636
    char *parent_name = NULL, *wwnn = NULL, *wwpn = NULL;
5637
    virObjectEventPtr event = NULL;
5638

5639
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5640
        goto out;
5641
    def = virNodeDeviceObjGetDef(obj);
5642

5643
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1)
5644 5645
        goto out;

5646
    if (VIR_STRDUP(parent_name, def->parent) < 0)
5647 5648 5649 5650 5651 5652 5653 5654
        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);

5655 5656
    /* We do this just for basic validation, but also avoid finding a
     * vport capable HBA if for some reason our vHBA doesn't exist */
5657
    if (virNodeDeviceObjGetParentHost(&driver->devs, def,
5658
                                      EXISTING_DEVICE) < 0) {
5659 5660 5661 5662
        obj = NULL;
        goto out;
    }

5663 5664 5665 5666
    event = virNodeDeviceEventLifecycleNew(dev->name,
                                           VIR_NODE_DEVICE_EVENT_DELETED,
                                           0);

5667
    virNodeDeviceObjLock(obj);
5668
    virNodeDeviceObjRemove(&driver->devs, &obj);
5669

5670
 out:
5671 5672
    if (obj)
        virNodeDeviceObjUnlock(obj);
5673
    testObjectEventQueue(driver, event);
5674 5675 5676 5677 5678 5679
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

5680 5681

/* Domain event implementations */
5682
static int
5683 5684 5685 5686
testConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
5687
{
5688
    testDriverPtr driver = conn->privateData;
5689
    int ret = 0;
5690

5691
    if (virDomainEventStateRegister(conn, driver->eventState,
5692 5693
                                    callback, opaque, freecb) < 0)
        ret = -1;
5694 5695 5696 5697

    return ret;
}

5698

5699
static int
5700 5701
testConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
5702
{
5703
    testDriverPtr driver = conn->privateData;
5704
    int ret = 0;
5705

5706
    if (virDomainEventStateDeregister(conn, driver->eventState,
5707 5708
                                      callback) < 0)
        ret = -1;
5709 5710 5711 5712

    return ret;
}

5713 5714

static int
5715 5716 5717 5718 5719 5720
testConnectDomainEventRegisterAny(virConnectPtr conn,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback callback,
                                  void *opaque,
                                  virFreeCallback freecb)
5721
{
5722
    testDriverPtr driver = conn->privateData;
5723 5724
    int ret;

5725
    if (virDomainEventStateRegisterID(conn, driver->eventState,
5726 5727
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
5728
        ret = -1;
5729 5730 5731 5732 5733

    return ret;
}

static int
5734 5735
testConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
5736
{
5737
    testDriverPtr driver = conn->privateData;
5738
    int ret = 0;
5739

5740
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5741
                                        callbackID, true) < 0)
5742
        ret = -1;
5743 5744 5745 5746 5747

    return ret;
}


5748 5749 5750 5751 5752 5753 5754 5755
static int
testConnectNetworkEventRegisterAny(virConnectPtr conn,
                                   virNetworkPtr net,
                                   int eventID,
                                   virConnectNetworkEventGenericCallback callback,
                                   void *opaque,
                                   virFreeCallback freecb)
{
5756
    testDriverPtr driver = conn->privateData;
5757 5758
    int ret;

5759
    if (virNetworkEventStateRegisterID(conn, driver->eventState,
5760
                                       net, eventID, callback,
5761 5762 5763 5764 5765 5766 5767 5768 5769 5770
                                       opaque, freecb, &ret) < 0)
        ret = -1;

    return ret;
}

static int
testConnectNetworkEventDeregisterAny(virConnectPtr conn,
                                     int callbackID)
{
5771
    testDriverPtr driver = conn->privateData;
5772
    int ret = 0;
5773

5774
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5775
                                        callbackID, true) < 0)
5776
        ret = -1;
5777 5778 5779 5780

    return ret;
}

5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807
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,
5808
                                        callbackID, true) < 0)
5809 5810 5811 5812 5813
        ret = -1;

    return ret;
}

5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840
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,
5841
                                        callbackID, true) < 0)
5842 5843 5844 5845 5846
        ret = -1;

    return ret;
}

5847 5848 5849
static int testConnectListAllDomains(virConnectPtr conn,
                                     virDomainPtr **domains,
                                     unsigned int flags)
5850
{
5851
    testDriverPtr privconn = conn->privateData;
5852

O
Osier Yang 已提交
5853
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
5854

5855 5856
    return virDomainObjListExport(privconn->domains, conn, domains,
                                  NULL, flags);
5857 5858
}

5859
static int
P
Peter Krempa 已提交
5860
testNodeGetCPUMap(virConnectPtr conn ATTRIBUTE_UNUSED,
5861 5862 5863 5864 5865 5866 5867
                  unsigned char **cpumap,
                  unsigned int *online,
                  unsigned int flags)
{
    virCheckFlags(0, -1);

    if (cpumap) {
5868
        if (VIR_ALLOC_N(*cpumap, 1) < 0)
P
Peter Krempa 已提交
5869
            return -1;
5870 5871 5872 5873 5874 5875
        *cpumap[0] = 0x15;
    }

    if (online)
        *online = 3;

P
Peter Krempa 已提交
5876
    return  8;
5877 5878
}

5879 5880 5881 5882 5883 5884 5885 5886 5887 5888
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

5889
    if (VIR_STRDUP(ret, "image/png") < 0)
5890 5891
        return NULL;

D
Daniel P. Berrange 已提交
5892
    if (virFDStreamOpenFile(st, PKGDATADIR "/test-screenshot.png", 0, 0, O_RDONLY) < 0)
5893 5894 5895 5896 5897
        VIR_FREE(ret);

    return ret;
}

5898 5899
static int
testConnectGetCPUModelNames(virConnectPtr conn ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
5900
                            const char *archName,
5901 5902 5903
                            char ***models,
                            unsigned int flags)
{
J
Jiri Denemark 已提交
5904 5905
    virArch arch;

5906
    virCheckFlags(0, -1);
J
Jiri Denemark 已提交
5907 5908 5909 5910 5911 5912 5913 5914

    if (!(arch = virArchFromString(archName))) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("cannot find architecture %s"),
                       archName);
        return -1;
    }

J
Jiri Denemark 已提交
5915
    return virCPUGetModels(arch, models);
5916
}
5917

C
Cole Robinson 已提交
5918 5919 5920
static int
testDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
5921
    testDriverPtr privconn = dom->conn->privateData;
C
Cole Robinson 已提交
5922
    virDomainObjPtr vm = NULL;
5923
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
5924 5925 5926 5927 5928 5929
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_SAVE_BYPASS_CACHE |
                  VIR_DOMAIN_SAVE_RUNNING |
                  VIR_DOMAIN_SAVE_PAUSED, -1);

5930 5931
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945

    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);
5946
    event = virDomainEventLifecycleNewFromObj(vm,
C
Cole Robinson 已提交
5947 5948 5949 5950 5951
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
    vm->hasManagedSave = true;

    ret = 0;
5952
 cleanup:
5953
    virDomainObjEndAPI(&vm);
5954
    testObjectEventQueue(privconn, event);
C
Cole Robinson 已提交
5955 5956 5957 5958 5959 5960 5961 5962 5963

    return ret;
}


static int
testDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;
5964
    int ret;
C
Cole Robinson 已提交
5965 5966 5967

    virCheckFlags(0, -1);

5968 5969
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5970 5971

    ret = vm->hasManagedSave;
5972

5973
    virDomainObjEndAPI(&vm);
C
Cole Robinson 已提交
5974 5975 5976 5977 5978 5979 5980 5981 5982 5983
    return ret;
}

static int
testDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;

    virCheckFlags(0, -1);

5984 5985
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5986 5987

    vm->hasManagedSave = false;
5988

5989
    virDomainObjEndAPI(&vm);
5990
    return 0;
C
Cole Robinson 已提交
5991 5992 5993
}


5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027
/*
 * 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;
6028
    int n;
6029 6030 6031 6032 6033

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6034
        return -1;
6035 6036 6037

    n = virDomainSnapshotObjListNum(vm->snapshots, NULL, flags);

6038
    virDomainObjEndAPI(&vm);
6039 6040 6041 6042 6043 6044 6045 6046 6047 6048
    return n;
}

static int
testDomainSnapshotListNames(virDomainPtr domain,
                            char **names,
                            int nameslen,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6049
    int n;
6050 6051 6052 6053 6054

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6055
        return -1;
6056 6057 6058 6059

    n = virDomainSnapshotObjListGetNames(vm->snapshots, NULL, names, nameslen,
                                         flags);

6060
    virDomainObjEndAPI(&vm);
6061 6062 6063 6064 6065 6066 6067 6068 6069
    return n;
}

static int
testDomainListAllSnapshots(virDomainPtr domain,
                           virDomainSnapshotPtr **snaps,
                           unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6070
    int n;
6071 6072 6073 6074 6075

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6076
        return -1;
6077 6078 6079

    n = virDomainListSnapshots(vm->snapshots, NULL, domain, snaps, flags);

6080
    virDomainObjEndAPI(&vm);
6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097
    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)))
6098
        return -1;
6099 6100 6101 6102 6103 6104 6105

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListGetNames(vm->snapshots, snap, names, nameslen,
                                         flags);

6106
 cleanup:
6107
    virDomainObjEndAPI(&vm);
6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122
    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)))
6123
        return -1;
6124 6125 6126 6127 6128 6129

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListNum(vm->snapshots, snap, flags);

6130
 cleanup:
6131
    virDomainObjEndAPI(&vm);
6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147
    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)))
6148
        return -1;
6149 6150 6151 6152 6153 6154 6155

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainListSnapshots(vm->snapshots, snap, snapshot->domain, snaps,
                               flags);

6156
 cleanup:
6157
    virDomainObjEndAPI(&vm);
6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172
    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)))
6173
        return NULL;
6174 6175 6176 6177 6178 6179

    if (!(snap = testSnapObjFromName(vm, name)))
        goto cleanup;

    snapshot = virGetDomainSnapshot(domain, snap->def->name);

6180
 cleanup:
6181
    virDomainObjEndAPI(&vm);
6182 6183 6184 6185 6186 6187 6188 6189
    return snapshot;
}

static int
testDomainHasCurrentSnapshot(virDomainPtr domain,
                             unsigned int flags)
{
    virDomainObjPtr vm;
6190
    int ret;
6191 6192 6193 6194

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6195
        return -1;
6196 6197 6198

    ret = (vm->current_snapshot != NULL);

6199
    virDomainObjEndAPI(&vm);
6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213
    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)))
6214
        return NULL;
6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227

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

6228
 cleanup:
6229
    virDomainObjEndAPI(&vm);
6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242
    return parent;
}

static virDomainSnapshotPtr
testDomainSnapshotCurrent(virDomainPtr domain,
                          unsigned int flags)
{
    virDomainObjPtr vm;
    virDomainSnapshotPtr snapshot = NULL;

    virCheckFlags(0, NULL);

    if (!(vm = testDomObjFromDomain(domain)))
6243
        return NULL;
6244 6245 6246 6247 6248 6249 6250 6251 6252

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

6253
 cleanup:
6254
    virDomainObjEndAPI(&vm);
6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265
    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];
6266
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6267 6268 6269 6270

    virCheckFlags(VIR_DOMAIN_XML_SECURE, NULL);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6271
        return NULL;
6272 6273 6274 6275 6276 6277

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    virUUIDFormat(snapshot->domain->uuid, uuidstr);

6278
    xml = virDomainSnapshotDefFormat(uuidstr, snap->def, privconn->caps,
6279
                                     privconn->xmlopt,
6280 6281
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);
6282

6283
 cleanup:
6284
    virDomainObjEndAPI(&vm);
6285 6286 6287 6288 6289 6290 6291 6292
    return xml;
}

static int
testDomainSnapshotIsCurrent(virDomainSnapshotPtr snapshot,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6293
    int ret;
6294 6295 6296 6297

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6298
        return -1;
6299 6300 6301 6302

    ret = (vm->current_snapshot &&
           STREQ(snapshot->name, vm->current_snapshot->def->name));

6303
    virDomainObjEndAPI(&vm);
6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317
    return ret;
}


static int
testDomainSnapshotHasMetadata(virDomainSnapshotPtr snapshot,
                              unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6318
        return -1;
6319

C
Cole Robinson 已提交
6320
    if (!testSnapObjFromSnapshot(vm, snapshot))
6321 6322 6323 6324
        goto cleanup;

    ret = 1;

6325
 cleanup:
6326
    virDomainObjEndAPI(&vm);
6327 6328 6329
    return ret;
}

6330 6331 6332 6333 6334 6335
static int
testDomainSnapshotAlignDisks(virDomainObjPtr vm,
                             virDomainSnapshotDefPtr def,
                             unsigned int flags)
{
    int align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
E
Eric Blake 已提交
6336
    bool align_match = true;
6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364

    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)
{
6365
    testDriverPtr privconn = domain->conn->privateData;
6366 6367 6368 6369
    virDomainObjPtr vm = NULL;
    virDomainSnapshotDefPtr def = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    virDomainSnapshotPtr snapshot = NULL;
6370
    virObjectEventPtr event = NULL;
6371
    char *xml = NULL;
6372 6373
    bool update_current = true;
    bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE;
6374 6375 6376 6377 6378 6379 6380 6381
    unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;

    /*
     * DISK_ONLY: Not implemented yet
     * REUSE_EXT: Not implemented yet
     *
     * NO_METADATA: Explicitly not implemented
     *
6382
     * REDEFINE + CURRENT: Implemented
6383 6384 6385 6386 6387 6388
     * HALT: Implemented
     * QUIESCE: Nothing to do
     * ATOMIC: Nothing to do
     * LIVE: Nothing to do
     */
    virCheckFlags(
6389 6390
        VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
        VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT |
6391 6392 6393 6394 6395
        VIR_DOMAIN_SNAPSHOT_CREATE_HALT |
        VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE |
        VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC |
        VIR_DOMAIN_SNAPSHOT_CREATE_LIVE, NULL);

6396 6397 6398 6399 6400
    if ((redefine && !(flags & VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT)))
        update_current = false;
    if (redefine)
        parse_flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;

6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415
    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;

6416
    if (redefine) {
C
Cole Robinson 已提交
6417
        if (virDomainSnapshotRedefinePrep(domain, vm, &def, &snap,
6418
                                          privconn->xmlopt,
C
Cole Robinson 已提交
6419
                                          &update_current, flags) < 0)
6420 6421 6422 6423 6424
            goto cleanup;
    } else {
        if (!(def->dom = virDomainDefCopy(vm->def,
                                          privconn->caps,
                                          privconn->xmlopt,
6425
                                          NULL,
6426 6427
                                          true)))
            goto cleanup;
6428

6429
        if (testDomainSnapshotAlignDisks(vm, def, flags) < 0)
6430 6431 6432
            goto cleanup;
    }

6433 6434 6435 6436
    if (!snap) {
        if (!(snap = virDomainSnapshotAssignDef(vm->snapshots, def)))
            goto cleanup;
        def = NULL;
6437 6438
    }

6439 6440 6441 6442 6443 6444 6445 6446 6447 6448
    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);
6449
            event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
6450 6451 6452
                                    VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
        }
    }
6453 6454

    snapshot = virGetDomainSnapshot(domain, snap->def->name);
6455
 cleanup:
6456 6457 6458 6459
    VIR_FREE(xml);
    if (vm) {
        if (snapshot) {
            virDomainSnapshotObjPtr other;
6460 6461
            if (update_current)
                vm->current_snapshot = snap;
6462 6463 6464 6465 6466 6467 6468
            other = virDomainSnapshotFindByName(vm->snapshots,
                                                snap->def->parent);
            snap->parent = other;
            other->nchildren++;
            snap->sibling = other->first_child;
            other->first_child = snap;
        }
6469
        virDomainObjEndAPI(&vm);
6470
    }
6471
    testObjectEventQueue(privconn, event);
6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483
    virDomainSnapshotDefFree(def);
    return snapshot;
}


typedef struct _testSnapRemoveData testSnapRemoveData;
typedef testSnapRemoveData *testSnapRemoveDataPtr;
struct _testSnapRemoveData {
    virDomainObjPtr vm;
    bool current;
};

6484
static int
6485
testDomainSnapshotDiscardAll(void *payload,
6486 6487
                             const void *name ATTRIBUTE_UNUSED,
                             void *data)
6488 6489 6490 6491 6492 6493 6494
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapRemoveDataPtr curr = data;

    if (snap->def->current)
        curr->current = true;
    virDomainSnapshotObjListRemove(curr->vm->snapshots, snap);
6495
    return 0;
6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506
}

typedef struct _testSnapReparentData testSnapReparentData;
typedef testSnapReparentData *testSnapReparentDataPtr;
struct _testSnapReparentData {
    virDomainSnapshotObjPtr parent;
    virDomainObjPtr vm;
    int err;
    virDomainSnapshotObjPtr last;
};

6507
static int
6508 6509 6510 6511 6512 6513 6514
testDomainSnapshotReparentChildren(void *payload,
                                   const void *name ATTRIBUTE_UNUSED,
                                   void *data)
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapReparentDataPtr rep = data;

6515
    if (rep->err < 0)
6516
        return 0;
6517 6518 6519 6520 6521 6522 6523

    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;
6524
        return 0;
6525 6526 6527 6528
    }

    if (!snap->sibling)
        rep->last = snap;
6529
    return 0;
6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558
}

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) {
6559
            if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY)
6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602
                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;
6603
 cleanup:
6604
    virDomainObjEndAPI(&vm);
6605 6606 6607 6608 6609 6610 6611
    return ret;
}

static int
testDomainRevertToSnapshot(virDomainSnapshotPtr snapshot,
                           unsigned int flags)
{
6612
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6613 6614
    virDomainObjPtr vm = NULL;
    virDomainSnapshotObjPtr snap = NULL;
6615 6616
    virObjectEventPtr event = NULL;
    virObjectEventPtr event2 = NULL;
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 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679
    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;
6680 6681
    config = virDomainDefCopy(snap->def->dom, privconn->caps,
                              privconn->xmlopt, NULL, true);
6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693
    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.  */
6694 6695
            if (!virDomainDefCheckABIStability(vm->def, config,
                                               privconn->xmlopt)) {
6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708
                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);
6709
                event = virDomainEventLifecycleNewFromObj(vm,
6710 6711
                            VIR_DOMAIN_EVENT_STOPPED,
                            VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
6712
                testObjectEventQueue(privconn, event);
6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723
                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. */
6724
                event = virDomainEventLifecycleNewFromObj(vm,
6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737
                                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;
6738
            event = virDomainEventLifecycleNewFromObj(vm,
6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751
                                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 */
6752
                event2 = virDomainEventLifecycleNewFromObj(vm,
6753 6754 6755 6756 6757
                                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 已提交
6758
            virObjectUnref(event);
6759 6760 6761 6762
            event = NULL;

            if (was_stopped) {
                /* Transition 2 */
6763
                event = virDomainEventLifecycleNewFromObj(vm,
6764 6765 6766 6767
                                VIR_DOMAIN_EVENT_STARTED,
                                VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            } else if (was_running) {
                /* Transition 8 */
6768
                event = virDomainEventLifecycleNewFromObj(vm,
6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780
                                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);
6781
            event = virDomainEventLifecycleNewFromObj(vm,
6782 6783 6784 6785 6786 6787 6788 6789 6790
                                    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;

6791
            testObjectEventQueue(privconn, event);
6792
            event = virDomainEventLifecycleNewFromObj(vm,
6793 6794 6795
                            VIR_DOMAIN_EVENT_STARTED,
                            VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            if (paused) {
6796
                event2 = virDomainEventLifecycleNewFromObj(vm,
6797 6798 6799 6800 6801 6802 6803 6804
                                VIR_DOMAIN_EVENT_SUSPENDED,
                                VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT);
            }
        }
    }

    vm->current_snapshot = snap;
    ret = 0;
6805
 cleanup:
6806
    if (event) {
6807
        testObjectEventQueue(privconn, event);
6808
        testObjectEventQueue(privconn, event2);
C
Cole Robinson 已提交
6809
    } else {
C
Cédric Bosdonnat 已提交
6810
        virObjectUnref(event2);
6811
    }
6812
    virDomainObjEndAPI(&vm);
6813 6814 6815 6816 6817

    return ret;
}


6818

6819
static virHypervisorDriver testHypervisorDriver = {
6820
    .name = "Test",
6821 6822 6823
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
6824
    .connectGetHostname = testConnectGetHostname, /* 0.6.3 */
6825
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
6826
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
6827
    .nodeGetCPUStats = testNodeGetCPUStats, /* 2.3.0 */
6828
    .nodeGetFreeMemory = testNodeGetFreeMemory, /* 2.3.0 */
6829
    .nodeGetFreePages = testNodeGetFreePages, /* 2.3.0 */
6830
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
6831
    .connectGetSysinfo = testConnectGetSysinfo, /* 2.3.0 */
6832
    .connectGetType = testConnectGetType, /* 2.3.0 */
6833 6834 6835
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
6836
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850
    .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 */
6851 6852
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
6853
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
6854
    .domainRestore = testDomainRestore, /* 0.3.2 */
6855
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
6856
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
6857
    .domainCoreDumpWithFormat = testDomainCoreDumpWithFormat, /* 1.2.3 */
6858
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
6859 6860 6861 6862
    .domainSetVcpusFlags = testDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = testDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = testDomainPinVcpu, /* 0.7.3 */
    .domainGetVcpus = testDomainGetVcpus, /* 0.7.3 */
6863
    .domainGetVcpuPinInfo = testDomainGetVcpuPinInfo, /* 1.2.18 */
6864 6865
    .domainGetMaxVcpus = testDomainGetMaxVcpus, /* 0.7.3 */
    .domainGetXMLDesc = testDomainGetXMLDesc, /* 0.1.4 */
6866 6867
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
6868 6869 6870
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
6871
    .domainDefineXMLFlags = testDomainDefineXMLFlags, /* 1.2.12 */
6872
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
6873
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
6874 6875 6876
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
6877 6878 6879 6880
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
6881 6882 6883
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
6884 6885 6886 6887
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
6888 6889 6890
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
6891 6892 6893
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
6894
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
6895
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
6896 6897
    .domainGetMetadata = testDomainGetMetadata, /* 1.1.3 */
    .domainSetMetadata = testDomainSetMetadata, /* 1.1.3 */
6898
    .connectGetCPUModelNames = testConnectGetCPUModelNames, /* 1.1.3 */
6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915
    .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 */
6916 6917 6918
    .domainSnapshotCreateXML = testDomainSnapshotCreateXML, /* 1.1.4 */
    .domainRevertToSnapshot = testDomainRevertToSnapshot, /* 1.1.4 */
    .domainSnapshotDelete = testDomainSnapshotDelete, /* 1.1.4 */
6919

E
Eric Blake 已提交
6920
    .connectBaselineCPU = testConnectBaselineCPU, /* 1.2.0 */
6921 6922 6923
};

static virNetworkDriver testNetworkDriver = {
6924 6925 6926 6927 6928
    .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 */
6929 6930
    .connectNetworkEventRegisterAny = testConnectNetworkEventRegisterAny, /* 1.2.1 */
    .connectNetworkEventDeregisterAny = testConnectNetworkEventDeregisterAny, /* 1.2.1 */
6931 6932 6933 6934
    .networkLookupByUUID = testNetworkLookupByUUID, /* 0.3.2 */
    .networkLookupByName = testNetworkLookupByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreateXML, /* 0.3.2 */
    .networkDefineXML = testNetworkDefineXML, /* 0.3.2 */
6935
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
6936
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
6937
    .networkCreate = testNetworkCreate, /* 0.3.2 */
6938 6939 6940 6941 6942 6943 6944
    .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 */
6945 6946
};

L
Laine Stump 已提交
6947
static virInterfaceDriver testInterfaceDriver = {
6948 6949 6950 6951 6952 6953
    .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 */
6954 6955 6956 6957 6958 6959
    .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 */
6960 6961 6962
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
6963 6964 6965
};


6966
static virStorageDriver testStorageDriver = {
6967 6968 6969 6970 6971 6972
    .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 */
6973 6974
    .connectStoragePoolEventRegisterAny = testConnectStoragePoolEventRegisterAny, /* 2.0.0 */
    .connectStoragePoolEventDeregisterAny = testConnectStoragePoolEventDeregisterAny, /* 2.0.0 */
6975 6976 6977
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
6978 6979
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
6980 6981
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
6982
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
6983 6984 6985 6986 6987 6988 6989
    .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 */
6990
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
6991 6992 6993
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

6994 6995 6996 6997 6998 6999 7000 7001 7002
    .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 */
7003 7004
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
7005 7006
};

7007
static virNodeDeviceDriver testNodeDeviceDriver = {
7008 7009
    .connectNodeDeviceEventRegisterAny = testConnectNodeDeviceEventRegisterAny, /* 2.2.0 */
    .connectNodeDeviceEventDeregisterAny = testConnectNodeDeviceEventDeregisterAny, /* 2.2.0 */
7010 7011 7012 7013 7014 7015 7016 7017 7018
    .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 */
7019 7020
};

7021 7022 7023 7024 7025 7026 7027 7028
static virConnectDriver testConnectDriver = {
    .hypervisorDriver = &testHypervisorDriver,
    .interfaceDriver = &testInterfaceDriver,
    .networkDriver = &testNetworkDriver,
    .nodeDeviceDriver = &testNodeDeviceDriver,
    .nwfilterDriver = NULL,
    .secretDriver = NULL,
    .storageDriver = &testStorageDriver,
7029 7030
};

7031 7032 7033 7034 7035 7036 7037 7038
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
7039 7040
    return virRegisterConnectDriver(&testConnectDriver,
                                    false);
7041
}