test_driver.c 198.0 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)) ||
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 960
    VIR_FREE(nodes);
    return ret;
}

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

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

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

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

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

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

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

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

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

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

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

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

1029
        virInterfaceObjSetActive(obj, true);
1030 1031 1032 1033
        virInterfaceObjUnlock(obj);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        virNodeDeviceObjUnlock(obj);
    }

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

1186
static int
1187
testParseAuthUsers(testDriverPtr privconn,
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
                   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;
1221
 error:
1222 1223 1224
    VIR_FREE(nodes);
    return ret;
}
1225

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

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

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

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

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

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

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

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

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

1288
    return 0;
1289 1290

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

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

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

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

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

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

    defaultConn = privconn;

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

    return VIR_DRV_OPEN_SUCCESS;

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

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

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

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

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

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

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

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

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

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

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

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

1465
    return VIR_DRV_OPEN_SUCCESS;
1466 1467
}

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

1598
    return count;
1599 1600
}

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

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

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

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

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

    ret = obj->persistent;

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

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

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

1644 1645 1646
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1856 1857
    virCheckFlags(0, -1);

1858

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    virCheckFlags(0, -1);

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

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

1997
    virDomainObjEndAPI(&privdom);
1998 1999

    return 0;
2000 2001
}

2002 2003
#define TEST_SAVE_MAGIC "TestGuestMagic"

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

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

2023

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2227

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

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

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

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

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

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

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

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

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

2300 2301 2302

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2448 2449
    ret = 0;

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

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

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

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

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

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

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

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

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

    virBitmapSetAll(allcpumap);

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

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

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

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

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

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

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

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

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

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

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

2554 2555
    def = privdom->def;

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

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

2570 2571 2572
    virBitmapFree(vcpuinfo->cpumask);

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

    ret = 0;
2576

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

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

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

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

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

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

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

2616 2617
    /* Flags checked by virDomainDefFormat */

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

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

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

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

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

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

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

2643
    testDriverPtr privconn = conn->privateData;
2644 2645

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

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

    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);
2663

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

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

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

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

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

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

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

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

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

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, NULL);

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

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

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

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

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

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

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

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

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


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

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

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

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

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

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

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

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

2882 2883
    virCheckFlags(0, -1);

2884
    testDriverLock(privconn);
2885

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

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

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

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

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

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

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

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

2929

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

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

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

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

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

2969
    ret = 0;
2970

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

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

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

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

2990
    *autostart = privdom->autostart;
2991

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


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

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

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

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

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

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

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

3021 3022 3023
    return type;
}

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

3033 3034
    virCheckFlags(0, -1);

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

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

    *nparams = 1;
3045 3046
    ret = 0;

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

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

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

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

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

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

3087 3088
    ret = 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3207

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


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

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

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

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

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

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


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

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

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

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


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

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

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

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

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

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

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

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

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

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

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

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

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

3336 3337
    ret = virNetworkObjIsActive(obj);

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

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

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

3352 3353
    ret = obj->persistent;

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE |
                  VIR_NETWORK_UPDATE_AFFECT_CONFIG,
                  -1);

3465
    if (!(network = testNetworkObjFindByUUID(privconn, net->uuid)))
3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485
        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;
3486
 cleanup:
3487
    virNetworkObjEndAPI(&network);
3488 3489 3490
    return ret;
}

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

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

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

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

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

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

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

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

3536 3537
    ret = 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

C
Cole Robinson 已提交
3621

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


3627 3628 3629 3630
static virInterfaceObjPtr
testInterfaceObjFindByName(testDriverPtr privconn,
                           const char *name)
{
3631
    virInterfaceObjPtr obj;
3632 3633

    testDriverLock(privconn);
3634
    obj = virInterfaceObjListFindByName(privconn->ifaces, name);
3635 3636
    testDriverUnlock(privconn);

3637
    if (!obj)
3638 3639 3640 3641
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("no interface with matching name '%s'"),
                       name);

3642
    return obj;
3643 3644 3645
}


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

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

3658 3659 3660 3661 3662

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

    testDriverLock(privconn);
3668 3669
    nnames = virInterfaceObjListGetNames(privconn->ifaces, true,
                                         names, maxnames);
L
Laine Stump 已提交
3670 3671
    testDriverUnlock(privconn);

3672
    return nnames;
L
Laine Stump 已提交
3673 3674
}

3675 3676 3677

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

    testDriverLock(privconn);
3683
    ninterfaces = virInterfaceObjListNumOfInterfaces(privconn->ifaces, false);
L
Laine Stump 已提交
3684
    testDriverUnlock(privconn);
3685
    return ninterfaces;
L
Laine Stump 已提交
3686 3687
}

3688 3689 3690 3691 3692

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

    testDriverLock(privconn);
3698 3699
    nnames = virInterfaceObjListGetNames(privconn->ifaces, false,
                                         names, maxnames);
L
Laine Stump 已提交
3700 3701
    testDriverUnlock(privconn);

3702
    return nnames;
L
Laine Stump 已提交
3703 3704
}

3705 3706 3707 3708

static virInterfacePtr
testInterfaceLookupByName(virConnectPtr conn,
                          const char *name)
L
Laine Stump 已提交
3709
{
3710
    testDriverPtr privconn = conn->privateData;
3711
    virInterfaceObjPtr obj;
3712
    virInterfaceDefPtr def;
L
Laine Stump 已提交
3713 3714
    virInterfacePtr ret = NULL;

3715
    if (!(obj = testInterfaceObjFindByName(privconn, name)))
3716
        return NULL;
3717
    def = virInterfaceObjGetDef(obj);
L
Laine Stump 已提交
3718

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

3721
    virInterfaceObjUnlock(obj);
L
Laine Stump 已提交
3722 3723 3724
    return ret;
}

3725 3726 3727 3728

static virInterfacePtr
testInterfaceLookupByMACString(virConnectPtr conn,
                               const char *mac)
L
Laine Stump 已提交
3729
{
3730
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3731
    int ifacect;
3732
    char *ifacenames[] = { NULL, NULL };
L
Laine Stump 已提交
3733 3734 3735
    virInterfacePtr ret = NULL;

    testDriverLock(privconn);
3736 3737
    ifacect = virInterfaceObjListFindByMACString(privconn->ifaces, mac,
                                                 ifacenames, 2);
L
Laine Stump 已提交
3738 3739 3740
    testDriverUnlock(privconn);

    if (ifacect == 0) {
3741 3742
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("no interface with matching mac '%s'"), mac);
L
Laine Stump 已提交
3743 3744 3745 3746
        goto cleanup;
    }

    if (ifacect > 1) {
3747
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3748 3749 3750
        goto cleanup;
    }

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

3753
 cleanup:
3754 3755
    VIR_FREE(ifacenames[0]);
    VIR_FREE(ifacenames[1]);
L
Laine Stump 已提交
3756 3757 3758
    return ret;
}

3759 3760 3761

static int
testInterfaceIsActive(virInterfacePtr iface)
3762
{
3763
    testDriverPtr privconn = iface->conn->privateData;
3764 3765 3766
    virInterfaceObjPtr obj;
    int ret = -1;

3767
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3768
        return -1;
3769

3770 3771
    ret = virInterfaceObjIsActive(obj);

3772
    virInterfaceObjUnlock(obj);
3773 3774 3775
    return ret;
}

3776 3777 3778 3779

static int
testInterfaceChangeBegin(virConnectPtr conn,
                         unsigned int flags)
3780
{
3781
    testDriverPtr privconn = conn->privateData;
3782 3783
    int ret = -1;

E
Eric Blake 已提交
3784 3785
    virCheckFlags(0, -1);

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

    privconn->transaction_running = true;

3795
    if (!(privconn->backupIfaces = virInterfaceObjListClone(privconn->ifaces)))
3796 3797 3798
        goto cleanup;

    ret = 0;
3799
 cleanup:
3800 3801 3802 3803
    testDriverUnlock(privconn);
    return ret;
}

3804 3805 3806 3807

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

E
Eric Blake 已提交
3812 3813
    virCheckFlags(0, -1);

3814 3815 3816
    testDriverLock(privconn);

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

3823
    virInterfaceObjListFree(privconn->backupIfaces);
3824 3825 3826 3827
    privconn->transaction_running = false;

    ret = 0;

3828
 cleanup:
3829 3830 3831 3832 3833
    testDriverUnlock(privconn);

    return ret;
}

3834 3835 3836 3837

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

E
Eric Blake 已提交
3842 3843
    virCheckFlags(0, -1);

3844 3845 3846
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3847
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3848 3849
                       _("no transaction running, "
                         "nothing to rollback."));
3850 3851 3852
        goto cleanup;
    }

3853 3854 3855
    virInterfaceObjListFree(privconn->ifaces);
    privconn->ifaces = privconn->backupIfaces;
    privconn->backupIfaces = NULL;
3856 3857 3858 3859 3860

    privconn->transaction_running = false;

    ret = 0;

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

3866 3867 3868 3869

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

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

3878
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3879
        return NULL;
3880
    def = virInterfaceObjGetDef(obj);
L
Laine Stump 已提交
3881

3882
    ret = virInterfaceDefFormat(def);
L
Laine Stump 已提交
3883

3884
    virInterfaceObjUnlock(obj);
L
Laine Stump 已提交
3885 3886 3887 3888
    return ret;
}


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

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

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

3906
    if ((obj = virInterfaceObjListAssignDef(privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3907 3908
        goto cleanup;
    def = NULL;
3909
    objdef = virInterfaceObjGetDef(obj);
L
Laine Stump 已提交
3910

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

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

3921 3922 3923

static int
testInterfaceUndefine(virInterfacePtr iface)
L
Laine Stump 已提交
3924
{
3925
    testDriverPtr privconn = iface->conn->privateData;
3926
    virInterfaceObjPtr obj;
L
Laine Stump 已提交
3927

3928
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3929
        return -1;
L
Laine Stump 已提交
3930

3931
    virInterfaceObjListRemove(privconn->ifaces, obj);
L
Laine Stump 已提交
3932

3933
    return 0;
L
Laine Stump 已提交
3934 3935
}

3936 3937 3938 3939

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

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

3947
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3948
        return -1;
L
Laine Stump 已提交
3949

3950
    if (virInterfaceObjIsActive(obj)) {
3951
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3952 3953 3954
        goto cleanup;
    }

3955
    virInterfaceObjSetActive(obj, true);
L
Laine Stump 已提交
3956 3957
    ret = 0;

3958
 cleanup:
3959
    virInterfaceObjUnlock(obj);
L
Laine Stump 已提交
3960 3961 3962
    return ret;
}

3963 3964 3965 3966

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

E
Eric Blake 已提交
3972 3973
    virCheckFlags(0, -1);

3974
    if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
3975
        return -1;
L
Laine Stump 已提交
3976

3977
    if (!virInterfaceObjIsActive(obj)) {
3978
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3979 3980 3981
        goto cleanup;
    }

3982
    virInterfaceObjSetActive(obj, false);
L
Laine Stump 已提交
3983 3984
    ret = 0;

3985
 cleanup:
3986
    virInterfaceObjUnlock(obj);
L
Laine Stump 已提交
3987 3988 3989 3990 3991
    return ret;
}



C
Cole Robinson 已提交
3992 3993 3994 3995
/*
 * Storage Driver routines
 */

3996

3997 3998
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool)
{
C
Cole Robinson 已提交
3999 4000 4001 4002 4003

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

4004
    return VIR_STRDUP(pool->configFile, "");
C
Cole Robinson 已提交
4005 4006
}

4007

4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026
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;
}


4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048
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 已提交
4049 4050
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
4051 4052
                            const unsigned char *uuid)
{
4053
    testDriverPtr privconn = conn->privateData;
4054 4055
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4056

4057
    if (!(pool = testStoragePoolObjFindByUUID(privconn, uuid)))
4058
        goto cleanup;
C
Cole Robinson 已提交
4059

4060 4061
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4062

4063
 cleanup:
4064 4065
    if (pool)
        virStoragePoolObjUnlock(pool);
4066
    return ret;
C
Cole Robinson 已提交
4067 4068 4069 4070
}

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

4077
    if (!(pool = testStoragePoolObjFindByName(privconn, name)))
4078
        goto cleanup;
C
Cole Robinson 已提交
4079

4080 4081
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4082

4083
 cleanup:
4084 4085
    if (pool)
        virStoragePoolObjUnlock(pool);
4086
    return ret;
C
Cole Robinson 已提交
4087 4088 4089
}

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

4095

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

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

    return numActive;
}

4110

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

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

    return n;
}

4127

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

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

    return numInactive;
}

4142

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

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

    return n;
}

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

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

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

    return ret;
}
C
Cole Robinson 已提交
4176

4177 4178
static int testStoragePoolIsActive(virStoragePoolPtr pool)
{
4179
    testDriverPtr privconn = pool->conn->privateData;
4180 4181 4182
    virStoragePoolObjPtr obj;
    int ret = -1;

4183
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4184
        goto cleanup;
4185

4186 4187
    ret = virStoragePoolObjIsActive(obj);

4188
 cleanup:
4189 4190 4191 4192 4193 4194 4195
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}

static int testStoragePoolIsPersistent(virStoragePoolPtr pool)
{
4196
    testDriverPtr privconn = pool->conn->privateData;
4197 4198 4199
    virStoragePoolObjPtr obj;
    int ret = -1;

4200
    if (!(obj = testStoragePoolObjFindByUUID(privconn, pool->uuid)))
4201
        goto cleanup;
4202

4203 4204
    ret = obj->configFile ? 1 : 0;

4205
 cleanup:
4206 4207 4208 4209 4210 4211 4212
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}



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

E
Eric Blake 已提交
4222 4223
    virCheckFlags(0, -1);

4224
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4225
        goto cleanup;
4226

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

    privpool->active = 1;
4234 4235 4236 4237

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

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

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

E
Eric Blake 已提交
4257 4258
    virCheckFlags(0, NULL);

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

    if (srcSpec) {
4267
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4268 4269 4270 4271 4272 4273 4274
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
4275
        ignore_value(VIR_STRDUP(ret, defaultPoolSourcesLogicalXML));
4276 4277 4278
        break;

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

4285 4286
        ignore_value(virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                                 source->hosts[0].name));
4287 4288 4289
        break;

    default:
4290 4291
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4292 4293
    }

4294
 cleanup:
4295 4296
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4297 4298 4299
}


4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327
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 已提交
4328
static virStoragePoolPtr
4329 4330 4331
testStoragePoolCreateXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4332
{
4333
    testDriverPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4334
    virStoragePoolDefPtr def;
4335
    virStoragePoolObjPtr pool = NULL;
4336
    virStoragePoolPtr ret = NULL;
4337
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
4338

E
Eric Blake 已提交
4339 4340
    virCheckFlags(0, NULL);

4341
    testDriverLock(privconn);
4342
    if (!(def = virStoragePoolDefParseString(xml)))
4343
        goto cleanup;
C
Cole Robinson 已提交
4344

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

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

4358
    if (pool->def->source.adapter.type == VIR_STORAGE_ADAPTER_TYPE_FC_HOST) {
4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371
        /* 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;
        }
    }

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

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

4385 4386 4387 4388
    event = virStoragePoolEventLifecycleNew(pool->def->name, pool->def->uuid,
                                            VIR_STORAGE_POOL_EVENT_STARTED,
                                            0);

4389 4390
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4391

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

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

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

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

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

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

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

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

4436 4437
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4438

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

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

4456
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4457
        goto cleanup;
4458

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

4465 4466 4467 4468
    event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid,
                                            VIR_STORAGE_POOL_EVENT_UNDEFINED,
                                            0);

C
Cole Robinson 已提交
4469
    virStoragePoolObjRemove(&privconn->pools, privpool);
4470
    privpool = NULL;
4471
    ret = 0;
C
Cole Robinson 已提交
4472

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

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

E
Eric Blake 已提交
4488 4489
    virCheckFlags(0, -1);

4490
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4491
        goto cleanup;
4492

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

4500
 cleanup:
4501 4502
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4503
    return ret;
C
Cole Robinson 已提交
4504 4505 4506
}


4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544
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 已提交
4545
static int
4546 4547
testStoragePoolDestroy(virStoragePoolPtr pool)
{
4548
    testDriverPtr privconn = pool->conn->privateData;
4549
    virStoragePoolObjPtr privpool;
4550
    int ret = -1;
4551
    virObjectEventPtr event = NULL;
4552

4553
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4554
        goto cleanup;
4555 4556

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

    privpool->active = 0;
4563 4564

    if (privpool->def->source.adapter.type ==
4565
        VIR_STORAGE_ADAPTER_TYPE_FC_HOST) {
4566 4567 4568 4569 4570 4571 4572 4573
        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,
4574 4575
                                            VIR_STORAGE_POOL_EVENT_STOPPED,
                                            0);
C
Cole Robinson 已提交
4576

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

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


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

E
Eric Blake 已提交
4599 4600
    virCheckFlags(0, -1);

4601
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4602 4603 4604
        goto cleanup;

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

4610
    ret = 0;
C
Cole Robinson 已提交
4611

4612
 cleanup:
4613 4614
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4615
    return ret;
C
Cole Robinson 已提交
4616 4617 4618 4619
}


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

E
Eric Blake 已提交
4628 4629
    virCheckFlags(0, -1);

4630
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4631
        goto cleanup;
4632 4633

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

4639
    event = virStoragePoolEventRefreshNew(pool->name, pool->uuid);
4640
    ret = 0;
C
Cole Robinson 已提交
4641

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


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

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

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

4671
 cleanup:
4672 4673
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4674
    return ret;
C
Cole Robinson 已提交
4675 4676 4677
}

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

E
Eric Blake 已提交
4685 4686
    virCheckFlags(0, NULL);

4687
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4688
        goto cleanup;
4689

4690
    ret = virStoragePoolDefFormat(privpool->def);
4691

4692
 cleanup:
4693 4694
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4695
    return ret;
C
Cole Robinson 已提交
4696 4697 4698
}

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

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

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

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

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

4730
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4731
        goto cleanup;
C
Cole Robinson 已提交
4732 4733

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

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4741 4742
    ret = 0;

4743
 cleanup:
4744 4745
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4746
    return ret;
C
Cole Robinson 已提交
4747 4748 4749 4750
}


static int
4751 4752
testStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
4753
    testDriverPtr privconn = pool->conn->privateData;
4754
    virStoragePoolObjPtr privpool;
4755
    int ret = -1;
4756

4757
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4758
        goto cleanup;
4759 4760

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

4766 4767
    ret = virStoragePoolObjNumOfVolumes(&privpool->volumes, pool->conn,
                                        privpool->def, NULL);
4768

4769
 cleanup:
4770 4771
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4772
    return ret;
C
Cole Robinson 已提交
4773 4774
}

4775

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

4785
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4786
        return -1;
4787 4788

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

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

4797
 cleanup:
4798
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4799 4800 4801
    return n;
}

4802

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

    virCheckFlags(0, -1);

4814
    if (!(pool = testStoragePoolObjFindByUUID(privconn, obj->uuid)))
4815
        return -1;
4816 4817 4818 4819 4820 4821 4822

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

4823 4824
    ret = virStoragePoolObjVolumeListExport(obj->conn, &pool->volumes,
                                            pool->def, vols, NULL);
4825 4826

 cleanup:
4827
    virStoragePoolObjUnlock(pool);
4828 4829 4830

    return ret;
}
C
Cole Robinson 已提交
4831 4832

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

4841
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4842
        goto cleanup;
4843 4844

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

    privvol = virStorageVolDefFindByName(privpool, name);

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

4858
    ret = virGetStorageVol(pool->conn, privpool->def->name,
4859 4860
                           privvol->name, privvol->key,
                           NULL, NULL);
4861

4862
 cleanup:
4863 4864
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4865
    return ret;
C
Cole Robinson 已提交
4866 4867 4868 4869
}


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

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

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

4898
    if (!ret)
4899 4900
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
4901 4902

    return ret;
C
Cole Robinson 已提交
4903 4904 4905
}

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

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

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

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

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

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

E
Eric Blake 已提交
4951 4952
    virCheckFlags(0, NULL);

4953
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
4954
        goto cleanup;
4955 4956

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

4962
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
4963
    if (privvol == NULL)
4964
        goto cleanup;
4965 4966

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

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

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

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

4991
    privpool->def->allocation += privvol->target.allocation;
C
Cole Robinson 已提交
4992 4993 4994
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

4995
    ret = virGetStorageVol(pool->conn, privpool->def->name,
4996 4997
                           privvol->name, privvol->key,
                           NULL, NULL);
4998
    privvol = NULL;
4999

5000
 cleanup:
5001
    virStorageVolDefFree(privvol);
5002 5003
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5004
    return ret;
C
Cole Robinson 已提交
5005 5006
}

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

E
Eric Blake 已提交
5018 5019
    virCheckFlags(0, NULL);

5020
    if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name)))
5021 5022 5023
        goto cleanup;

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

5029
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5030 5031 5032 5033
    if (privvol == NULL)
        goto cleanup;

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

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

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

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

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

5068
    privpool->def->allocation += privvol->target.allocation;
5069 5070 5071 5072
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    ret = virGetStorageVol(pool->conn, privpool->def->name,
5073 5074
                           privvol->name, privvol->key,
                           NULL, NULL);
5075 5076
    privvol = NULL;

5077
 cleanup:
5078 5079 5080 5081 5082 5083
    virStorageVolDefFree(privvol);
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    return ret;
}

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

E
Eric Blake 已提交
5094 5095
    virCheckFlags(0, -1);

5096
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5097
        goto cleanup;
5098 5099 5100 5101

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

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

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


5115
    privpool->def->allocation -= privvol->target.allocation;
C
Cole Robinson 已提交
5116 5117 5118
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

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

5129
 cleanup:
5130 5131
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5132
    return ret;
C
Cole Robinson 已提交
5133 5134 5135
}


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

5139
    switch (pooltype) {
C
Cole Robinson 已提交
5140 5141 5142 5143 5144 5145 5146 5147 5148 5149
        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
5150
testStorageVolGetInfo(virStorageVolPtr vol,
5151 5152
                      virStorageVolInfoPtr info)
{
5153
    testDriverPtr privconn = vol->conn->privateData;
5154 5155
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5156
    int ret = -1;
5157

5158
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5159
        goto cleanup;
5160 5161 5162 5163

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

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

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

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

5182
 cleanup:
5183 5184
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5185
    return ret;
C
Cole Robinson 已提交
5186 5187 5188
}

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

E
Eric Blake 已提交
5197 5198
    virCheckFlags(0, NULL);

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

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

5217
    ret = virStorageVolDefFormat(privpool->def, privvol);
5218

5219
 cleanup:
5220 5221
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5222
    return ret;
C
Cole Robinson 已提交
5223 5224 5225
}

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

5233
    if (!(privpool = testStoragePoolObjFindByName(privconn, vol->pool)))
5234
        goto cleanup;
5235 5236 5237 5238

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

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

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

5251
    ignore_value(VIR_STRDUP(ret, privvol->target.path));
5252

5253
 cleanup:
5254 5255
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5256 5257 5258
    return ret;
}

5259

5260
/* Node device implementations */
5261

5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280
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;
}


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

E
Eric Blake 已提交
5289 5290
    virCheckFlags(0, -1);

5291
    testDriverLock(driver);
5292
    ndevs = virNodeDeviceObjNumOfDevices(&driver->devs, conn, cap, NULL);
5293 5294 5295 5296 5297
    testDriverUnlock(driver);

    return ndevs;
}

5298

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

E
Eric Blake 已提交
5309 5310
    virCheckFlags(0, -1);

5311
    testDriverLock(driver);
5312 5313
    nnames = virNodeDeviceObjGetNames(&driver->devs, conn, NULL,
                                     cap, names, maxnames);
5314 5315
    testDriverUnlock(driver);

5316
    return nnames;
5317 5318
}

5319

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

5328
    if (!(obj = testNodeDeviceObjFindByName(driver, name)))
5329
        goto cleanup;
5330
    def = virNodeDeviceObjGetDef(obj);
5331

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

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

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

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

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

5358
    ret = virNodeDeviceDefFormat(virNodeDeviceObjGetDef(obj));
5359

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

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

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

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

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

5391

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

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

5406
    for (caps = def->caps; caps; caps = caps->next)
5407 5408 5409
        ++ncaps;
    ret = ncaps;

5410
 cleanup:
5411 5412 5413 5414 5415 5416 5417 5418 5419
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}


static int
testNodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
5420
    testDriverPtr driver = dev->conn->privateData;
5421
    virNodeDeviceObjPtr obj;
5422
    virNodeDeviceDefPtr def;
5423 5424 5425 5426
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

5427
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5428
        goto cleanup;
5429
    def = virNodeDeviceObjGetDef(obj);
5430

5431
    for (caps = def->caps; caps && ncaps < maxnames; caps = caps->next) {
5432
        if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->data.type)) < 0)
5433 5434 5435 5436
            goto cleanup;
    }
    ret = ncaps;

5437
 cleanup:
5438 5439 5440 5441 5442 5443 5444 5445 5446 5447
    if (obj)
        virNodeDeviceObjUnlock(obj);
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
}

5448

5449 5450
static virNodeDeviceObjPtr
testNodeDeviceMockCreateVport(testDriverPtr driver,
5451
                              const char *wwnn,
5452
                              const char *wwpn)
5453
{
5454 5455
    char *xml = NULL;
    virNodeDeviceDefPtr def = NULL;
5456
    virNodeDevCapsDefPtr caps;
5457
    virNodeDeviceObjPtr obj = NULL, objcopy = NULL;
5458
    virNodeDeviceDefPtr objdef;
5459
    virObjectEventPtr event = NULL;
5460

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

5473
    xml = virNodeDeviceDefFormat(virNodeDeviceObjGetDef(objcopy));
5474 5475 5476 5477 5478
    virNodeDeviceObjUnlock(objcopy);
    if (!xml)
        goto cleanup;

    if (!(def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL)))
5479 5480
        goto cleanup;

5481
    VIR_FREE(def->name);
5482
    if (VIR_STRDUP(def->name, "scsi_host12") < 0)
5483 5484
        goto cleanup;

5485 5486 5487
    /* 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. */
5488 5489
    caps = def->caps;
    while (caps) {
5490
        if (caps->data.type != VIR_NODE_DEV_CAP_SCSI_HOST)
5491 5492
            continue;

5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506
        /* 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++;
        }
5507 5508 5509
        caps = caps->next;
    }

5510
    if (!(obj = virNodeDeviceObjAssignDef(&driver->devs, def)))
5511
        goto cleanup;
5512
    def = NULL;
5513
    objdef = virNodeDeviceObjGetDef(obj);
5514

5515
    event = virNodeDeviceEventLifecycleNew(objdef->name,
5516 5517
                                           VIR_NODE_DEVICE_EVENT_CREATED,
                                           0);
5518 5519 5520
    testObjectEventQueue(driver, event);

 cleanup:
5521
    VIR_FREE(xml);
5522 5523
    virNodeDeviceDefFree(def);
    return obj;
5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534
}


static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
                        unsigned int flags)
{
    testDriverPtr driver = conn->privateData;
    virNodeDeviceDefPtr def = NULL;
    char *wwnn = NULL, *wwpn = NULL;
5535 5536
    virNodeDevicePtr dev = NULL, ret = NULL;
    virNodeDeviceObjPtr obj = NULL;
5537
    virNodeDeviceDefPtr objdef;
5538 5539 5540 5541 5542 5543 5544 5545

    virCheckFlags(0, NULL);

    testDriverLock(driver);

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

5546 5547 5548
    /* We run this simply for validation - it essentially validates that
     * the input XML either has a wwnn/wwpn or virNodeDevCapSCSIHostParseXML
     * generated a wwnn/wwpn */
5549 5550 5551
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) < 0)
        goto cleanup;

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

5567
    if (!(dev = virGetNodeDevice(conn, objdef->name)))
5568 5569 5570 5571 5572 5573 5574 5575
        goto cleanup;

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

    ret = dev;
    dev = NULL;
5576

5577
 cleanup:
5578 5579
    if (obj)
        virNodeDeviceObjUnlock(obj);
5580
    testDriverUnlock(driver);
5581
    virNodeDeviceDefFree(def);
5582
    virObjectUnref(dev);
5583 5584
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
5585
    return ret;
5586 5587 5588 5589 5590 5591
}

static int
testNodeDeviceDestroy(virNodeDevicePtr dev)
{
    int ret = 0;
5592
    testDriverPtr driver = dev->conn->privateData;
5593
    virNodeDeviceObjPtr obj = NULL;
5594
    virNodeDeviceDefPtr def;
5595
    char *parent_name = NULL, *wwnn = NULL, *wwpn = NULL;
5596
    virObjectEventPtr event = NULL;
5597

5598
    if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
5599
        goto out;
5600
    def = virNodeDeviceObjGetDef(obj);
5601

5602
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1)
5603 5604
        goto out;

5605
    if (VIR_STRDUP(parent_name, def->parent) < 0)
5606 5607 5608 5609 5610 5611 5612 5613
        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);

5614 5615
    /* We do this just for basic validation, but also avoid finding a
     * vport capable HBA if for some reason our vHBA doesn't exist */
5616
    if (virNodeDeviceObjGetParentHost(&driver->devs, def,
5617
                                      EXISTING_DEVICE) < 0) {
5618 5619 5620 5621
        obj = NULL;
        goto out;
    }

5622 5623 5624 5625
    event = virNodeDeviceEventLifecycleNew(dev->name,
                                           VIR_NODE_DEVICE_EVENT_DELETED,
                                           0);

5626
    virNodeDeviceObjLock(obj);
5627
    virNodeDeviceObjRemove(&driver->devs, &obj);
5628

5629
 out:
5630 5631
    if (obj)
        virNodeDeviceObjUnlock(obj);
5632
    testObjectEventQueue(driver, event);
5633 5634 5635 5636 5637 5638
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

5639 5640

/* Domain event implementations */
5641
static int
5642 5643 5644 5645
testConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
5646
{
5647
    testDriverPtr driver = conn->privateData;
5648
    int ret = 0;
5649

5650
    if (virDomainEventStateRegister(conn, driver->eventState,
5651 5652
                                    callback, opaque, freecb) < 0)
        ret = -1;
5653 5654 5655 5656

    return ret;
}

5657

5658
static int
5659 5660
testConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
5661
{
5662
    testDriverPtr driver = conn->privateData;
5663
    int ret = 0;
5664

5665
    if (virDomainEventStateDeregister(conn, driver->eventState,
5666 5667
                                      callback) < 0)
        ret = -1;
5668 5669 5670 5671

    return ret;
}

5672 5673

static int
5674 5675 5676 5677 5678 5679
testConnectDomainEventRegisterAny(virConnectPtr conn,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback callback,
                                  void *opaque,
                                  virFreeCallback freecb)
5680
{
5681
    testDriverPtr driver = conn->privateData;
5682 5683
    int ret;

5684
    if (virDomainEventStateRegisterID(conn, driver->eventState,
5685 5686
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
5687
        ret = -1;
5688 5689 5690 5691 5692

    return ret;
}

static int
5693 5694
testConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
5695
{
5696
    testDriverPtr driver = conn->privateData;
5697
    int ret = 0;
5698

5699
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5700 5701
                                        callbackID) < 0)
        ret = -1;
5702 5703 5704 5705 5706

    return ret;
}


5707 5708 5709 5710 5711 5712 5713 5714
static int
testConnectNetworkEventRegisterAny(virConnectPtr conn,
                                   virNetworkPtr net,
                                   int eventID,
                                   virConnectNetworkEventGenericCallback callback,
                                   void *opaque,
                                   virFreeCallback freecb)
{
5715
    testDriverPtr driver = conn->privateData;
5716 5717
    int ret;

5718
    if (virNetworkEventStateRegisterID(conn, driver->eventState,
5719
                                       net, eventID, callback,
5720 5721 5722 5723 5724 5725 5726 5727 5728 5729
                                       opaque, freecb, &ret) < 0)
        ret = -1;

    return ret;
}

static int
testConnectNetworkEventDeregisterAny(virConnectPtr conn,
                                     int callbackID)
{
5730
    testDriverPtr driver = conn->privateData;
5731
    int ret = 0;
5732

5733
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5734 5735
                                        callbackID) < 0)
        ret = -1;
5736 5737 5738 5739

    return ret;
}

5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772
static int
testConnectStoragePoolEventRegisterAny(virConnectPtr conn,
                                       virStoragePoolPtr pool,
                                       int eventID,
                                       virConnectStoragePoolEventGenericCallback callback,
                                       void *opaque,
                                       virFreeCallback freecb)
{
    testDriverPtr driver = conn->privateData;
    int ret;

    if (virStoragePoolEventStateRegisterID(conn, driver->eventState,
                                           pool, eventID, callback,
                                           opaque, freecb, &ret) < 0)
        ret = -1;

    return ret;
}

static int
testConnectStoragePoolEventDeregisterAny(virConnectPtr conn,
                                         int callbackID)
{
    testDriverPtr driver = conn->privateData;
    int ret = 0;

    if (virObjectEventStateDeregisterID(conn, driver->eventState,
                                        callbackID) < 0)
        ret = -1;

    return ret;
}

5773 5774 5775 5776 5777 5778 5779 5780 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
static int
testConnectNodeDeviceEventRegisterAny(virConnectPtr conn,
                                      virNodeDevicePtr dev,
                                      int eventID,
                                      virConnectNodeDeviceEventGenericCallback callback,
                                      void *opaque,
                                      virFreeCallback freecb)
{
    testDriverPtr driver = conn->privateData;
    int ret;

    if (virNodeDeviceEventStateRegisterID(conn, driver->eventState,
                                          dev, eventID, callback,
                                          opaque, freecb, &ret) < 0)
        ret = -1;

    return ret;
}

static int
testConnectNodeDeviceEventDeregisterAny(virConnectPtr conn,
                                        int callbackID)
{
    testDriverPtr driver = conn->privateData;
    int ret = 0;

    if (virObjectEventStateDeregisterID(conn, driver->eventState,
                                        callbackID) < 0)
        ret = -1;

    return ret;
}

5806 5807 5808
static int testConnectListAllDomains(virConnectPtr conn,
                                     virDomainPtr **domains,
                                     unsigned int flags)
5809
{
5810
    testDriverPtr privconn = conn->privateData;
5811

O
Osier Yang 已提交
5812
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
5813

5814 5815
    return virDomainObjListExport(privconn->domains, conn, domains,
                                  NULL, flags);
5816 5817
}

5818
static int
P
Peter Krempa 已提交
5819
testNodeGetCPUMap(virConnectPtr conn ATTRIBUTE_UNUSED,
5820 5821 5822 5823 5824 5825 5826
                  unsigned char **cpumap,
                  unsigned int *online,
                  unsigned int flags)
{
    virCheckFlags(0, -1);

    if (cpumap) {
5827
        if (VIR_ALLOC_N(*cpumap, 1) < 0)
P
Peter Krempa 已提交
5828
            return -1;
5829 5830 5831 5832 5833 5834
        *cpumap[0] = 0x15;
    }

    if (online)
        *online = 3;

P
Peter Krempa 已提交
5835
    return  8;
5836 5837
}

5838 5839 5840 5841 5842 5843 5844 5845 5846 5847
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

5848
    if (VIR_STRDUP(ret, "image/png") < 0)
5849 5850
        return NULL;

D
Daniel P. Berrange 已提交
5851
    if (virFDStreamOpenFile(st, PKGDATADIR "/test-screenshot.png", 0, 0, O_RDONLY) < 0)
5852 5853 5854 5855 5856
        VIR_FREE(ret);

    return ret;
}

5857 5858
static int
testConnectGetCPUModelNames(virConnectPtr conn ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
5859
                            const char *archName,
5860 5861 5862
                            char ***models,
                            unsigned int flags)
{
J
Jiri Denemark 已提交
5863 5864
    virArch arch;

5865
    virCheckFlags(0, -1);
J
Jiri Denemark 已提交
5866 5867 5868 5869 5870 5871 5872 5873

    if (!(arch = virArchFromString(archName))) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("cannot find architecture %s"),
                       archName);
        return -1;
    }

J
Jiri Denemark 已提交
5874
    return virCPUGetModels(arch, models);
5875
}
5876

C
Cole Robinson 已提交
5877 5878 5879
static int
testDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
5880
    testDriverPtr privconn = dom->conn->privateData;
C
Cole Robinson 已提交
5881
    virDomainObjPtr vm = NULL;
5882
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
5883 5884 5885 5886 5887 5888
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_SAVE_BYPASS_CACHE |
                  VIR_DOMAIN_SAVE_RUNNING |
                  VIR_DOMAIN_SAVE_PAUSED, -1);

5889 5890
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904

    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);
5905
    event = virDomainEventLifecycleNewFromObj(vm,
C
Cole Robinson 已提交
5906 5907 5908 5909 5910
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
    vm->hasManagedSave = true;

    ret = 0;
5911
 cleanup:
5912
    virDomainObjEndAPI(&vm);
5913
    testObjectEventQueue(privconn, event);
C
Cole Robinson 已提交
5914 5915 5916 5917 5918 5919 5920 5921 5922

    return ret;
}


static int
testDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;
5923
    int ret;
C
Cole Robinson 已提交
5924 5925 5926

    virCheckFlags(0, -1);

5927 5928
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5929 5930

    ret = vm->hasManagedSave;
5931

5932
    virDomainObjEndAPI(&vm);
C
Cole Robinson 已提交
5933 5934 5935 5936 5937 5938 5939 5940 5941 5942
    return ret;
}

static int
testDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;

    virCheckFlags(0, -1);

5943 5944
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
5945 5946

    vm->hasManagedSave = false;
5947

5948
    virDomainObjEndAPI(&vm);
5949
    return 0;
C
Cole Robinson 已提交
5950 5951 5952
}


5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986
/*
 * 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;
5987
    int n;
5988 5989 5990 5991 5992

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
5993
        return -1;
5994 5995 5996

    n = virDomainSnapshotObjListNum(vm->snapshots, NULL, flags);

5997
    virDomainObjEndAPI(&vm);
5998 5999 6000 6001 6002 6003 6004 6005 6006 6007
    return n;
}

static int
testDomainSnapshotListNames(virDomainPtr domain,
                            char **names,
                            int nameslen,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6008
    int n;
6009 6010 6011 6012 6013

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6014
        return -1;
6015 6016 6017 6018

    n = virDomainSnapshotObjListGetNames(vm->snapshots, NULL, names, nameslen,
                                         flags);

6019
    virDomainObjEndAPI(&vm);
6020 6021 6022 6023 6024 6025 6026 6027 6028
    return n;
}

static int
testDomainListAllSnapshots(virDomainPtr domain,
                           virDomainSnapshotPtr **snaps,
                           unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6029
    int n;
6030 6031 6032 6033 6034

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6035
        return -1;
6036 6037 6038

    n = virDomainListSnapshots(vm->snapshots, NULL, domain, snaps, flags);

6039
    virDomainObjEndAPI(&vm);
6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056
    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)))
6057
        return -1;
6058 6059 6060 6061 6062 6063 6064

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListGetNames(vm->snapshots, snap, names, nameslen,
                                         flags);

6065
 cleanup:
6066
    virDomainObjEndAPI(&vm);
6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081
    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)))
6082
        return -1;
6083 6084 6085 6086 6087 6088

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainSnapshotObjListNum(vm->snapshots, snap, flags);

6089
 cleanup:
6090
    virDomainObjEndAPI(&vm);
6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106
    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)))
6107
        return -1;
6108 6109 6110 6111 6112 6113 6114

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    n = virDomainListSnapshots(vm->snapshots, snap, snapshot->domain, snaps,
                               flags);

6115
 cleanup:
6116
    virDomainObjEndAPI(&vm);
6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131
    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)))
6132
        return NULL;
6133 6134 6135 6136 6137 6138

    if (!(snap = testSnapObjFromName(vm, name)))
        goto cleanup;

    snapshot = virGetDomainSnapshot(domain, snap->def->name);

6139
 cleanup:
6140
    virDomainObjEndAPI(&vm);
6141 6142 6143 6144 6145 6146 6147 6148
    return snapshot;
}

static int
testDomainHasCurrentSnapshot(virDomainPtr domain,
                             unsigned int flags)
{
    virDomainObjPtr vm;
6149
    int ret;
6150 6151 6152 6153

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6154
        return -1;
6155 6156 6157

    ret = (vm->current_snapshot != NULL);

6158
    virDomainObjEndAPI(&vm);
6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172
    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)))
6173
        return NULL;
6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186

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

6187
 cleanup:
6188
    virDomainObjEndAPI(&vm);
6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201
    return parent;
}

static virDomainSnapshotPtr
testDomainSnapshotCurrent(virDomainPtr domain,
                          unsigned int flags)
{
    virDomainObjPtr vm;
    virDomainSnapshotPtr snapshot = NULL;

    virCheckFlags(0, NULL);

    if (!(vm = testDomObjFromDomain(domain)))
6202
        return NULL;
6203 6204 6205 6206 6207 6208 6209 6210 6211

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

6212
 cleanup:
6213
    virDomainObjEndAPI(&vm);
6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224
    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];
6225
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6226 6227 6228 6229

    virCheckFlags(VIR_DOMAIN_XML_SECURE, NULL);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6230
        return NULL;
6231 6232 6233 6234 6235 6236

    if (!(snap = testSnapObjFromSnapshot(vm, snapshot)))
        goto cleanup;

    virUUIDFormat(snapshot->domain->uuid, uuidstr);

6237
    xml = virDomainSnapshotDefFormat(uuidstr, snap->def, privconn->caps,
6238 6239
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);
6240

6241
 cleanup:
6242
    virDomainObjEndAPI(&vm);
6243 6244 6245 6246 6247 6248 6249 6250
    return xml;
}

static int
testDomainSnapshotIsCurrent(virDomainSnapshotPtr snapshot,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6251
    int ret;
6252 6253 6254 6255

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6256
        return -1;
6257 6258 6259 6260

    ret = (vm->current_snapshot &&
           STREQ(snapshot->name, vm->current_snapshot->def->name));

6261
    virDomainObjEndAPI(&vm);
6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275
    return ret;
}


static int
testDomainSnapshotHasMetadata(virDomainSnapshotPtr snapshot,
                              unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6276
        return -1;
6277

C
Cole Robinson 已提交
6278
    if (!testSnapObjFromSnapshot(vm, snapshot))
6279 6280 6281 6282
        goto cleanup;

    ret = 1;

6283
 cleanup:
6284
    virDomainObjEndAPI(&vm);
6285 6286 6287
    return ret;
}

6288 6289 6290 6291 6292 6293
static int
testDomainSnapshotAlignDisks(virDomainObjPtr vm,
                             virDomainSnapshotDefPtr def,
                             unsigned int flags)
{
    int align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
E
Eric Blake 已提交
6294
    bool align_match = true;
6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322

    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)
{
6323
    testDriverPtr privconn = domain->conn->privateData;
6324 6325 6326 6327
    virDomainObjPtr vm = NULL;
    virDomainSnapshotDefPtr def = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    virDomainSnapshotPtr snapshot = NULL;
6328
    virObjectEventPtr event = NULL;
6329
    char *xml = NULL;
6330 6331
    bool update_current = true;
    bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE;
6332 6333 6334 6335 6336 6337 6338 6339
    unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;

    /*
     * DISK_ONLY: Not implemented yet
     * REUSE_EXT: Not implemented yet
     *
     * NO_METADATA: Explicitly not implemented
     *
6340
     * REDEFINE + CURRENT: Implemented
6341 6342 6343 6344 6345 6346
     * HALT: Implemented
     * QUIESCE: Nothing to do
     * ATOMIC: Nothing to do
     * LIVE: Nothing to do
     */
    virCheckFlags(
6347 6348
        VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
        VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT |
6349 6350 6351 6352 6353
        VIR_DOMAIN_SNAPSHOT_CREATE_HALT |
        VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE |
        VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC |
        VIR_DOMAIN_SNAPSHOT_CREATE_LIVE, NULL);

6354 6355 6356 6357 6358
    if ((redefine && !(flags & VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT)))
        update_current = false;
    if (redefine)
        parse_flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;

6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373
    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;

6374
    if (redefine) {
C
Cole Robinson 已提交
6375
        if (virDomainSnapshotRedefinePrep(domain, vm, &def, &snap,
6376
                                          privconn->xmlopt,
C
Cole Robinson 已提交
6377
                                          &update_current, flags) < 0)
6378 6379 6380 6381 6382
            goto cleanup;
    } else {
        if (!(def->dom = virDomainDefCopy(vm->def,
                                          privconn->caps,
                                          privconn->xmlopt,
6383
                                          NULL,
6384 6385
                                          true)))
            goto cleanup;
6386

6387
        if (testDomainSnapshotAlignDisks(vm, def, flags) < 0)
6388 6389 6390
            goto cleanup;
    }

6391 6392 6393 6394
    if (!snap) {
        if (!(snap = virDomainSnapshotAssignDef(vm->snapshots, def)))
            goto cleanup;
        def = NULL;
6395 6396
    }

6397 6398 6399 6400 6401 6402 6403 6404 6405 6406
    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);
6407
            event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
6408 6409 6410
                                    VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
        }
    }
6411 6412

    snapshot = virGetDomainSnapshot(domain, snap->def->name);
6413
 cleanup:
6414 6415 6416 6417
    VIR_FREE(xml);
    if (vm) {
        if (snapshot) {
            virDomainSnapshotObjPtr other;
6418 6419
            if (update_current)
                vm->current_snapshot = snap;
6420 6421 6422 6423 6424 6425 6426
            other = virDomainSnapshotFindByName(vm->snapshots,
                                                snap->def->parent);
            snap->parent = other;
            other->nchildren++;
            snap->sibling = other->first_child;
            other->first_child = snap;
        }
6427
        virDomainObjEndAPI(&vm);
6428
    }
6429
    testObjectEventQueue(privconn, event);
6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441
    virDomainSnapshotDefFree(def);
    return snapshot;
}


typedef struct _testSnapRemoveData testSnapRemoveData;
typedef testSnapRemoveData *testSnapRemoveDataPtr;
struct _testSnapRemoveData {
    virDomainObjPtr vm;
    bool current;
};

6442
static int
6443
testDomainSnapshotDiscardAll(void *payload,
6444 6445
                             const void *name ATTRIBUTE_UNUSED,
                             void *data)
6446 6447 6448 6449 6450 6451 6452
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapRemoveDataPtr curr = data;

    if (snap->def->current)
        curr->current = true;
    virDomainSnapshotObjListRemove(curr->vm->snapshots, snap);
6453
    return 0;
6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464
}

typedef struct _testSnapReparentData testSnapReparentData;
typedef testSnapReparentData *testSnapReparentDataPtr;
struct _testSnapReparentData {
    virDomainSnapshotObjPtr parent;
    virDomainObjPtr vm;
    int err;
    virDomainSnapshotObjPtr last;
};

6465
static int
6466 6467 6468 6469 6470 6471 6472
testDomainSnapshotReparentChildren(void *payload,
                                   const void *name ATTRIBUTE_UNUSED,
                                   void *data)
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapReparentDataPtr rep = data;

6473
    if (rep->err < 0)
6474
        return 0;
6475 6476 6477 6478 6479 6480 6481

    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;
6482
        return 0;
6483 6484 6485 6486
    }

    if (!snap->sibling)
        rep->last = snap;
6487
    return 0;
6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516
}

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) {
6517
            if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY)
6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560
                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;
6561
 cleanup:
6562
    virDomainObjEndAPI(&vm);
6563 6564 6565 6566 6567 6568 6569
    return ret;
}

static int
testDomainRevertToSnapshot(virDomainSnapshotPtr snapshot,
                           unsigned int flags)
{
6570
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6571 6572
    virDomainObjPtr vm = NULL;
    virDomainSnapshotObjPtr snap = NULL;
6573 6574
    virObjectEventPtr event = NULL;
    virObjectEventPtr event2 = NULL;
6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637
    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;
6638 6639
    config = virDomainDefCopy(snap->def->dom, privconn->caps,
                              privconn->xmlopt, NULL, true);
6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651
    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.  */
6652 6653
            if (!virDomainDefCheckABIStability(vm->def, config,
                                               privconn->xmlopt)) {
6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666
                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);
6667
                event = virDomainEventLifecycleNewFromObj(vm,
6668 6669
                            VIR_DOMAIN_EVENT_STOPPED,
                            VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
6670
                testObjectEventQueue(privconn, event);
6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681
                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. */
6682
                event = virDomainEventLifecycleNewFromObj(vm,
6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695
                                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;
6696
            event = virDomainEventLifecycleNewFromObj(vm,
6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709
                                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 */
6710
                event2 = virDomainEventLifecycleNewFromObj(vm,
6711 6712 6713 6714 6715
                                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 已提交
6716
            virObjectUnref(event);
6717 6718 6719 6720
            event = NULL;

            if (was_stopped) {
                /* Transition 2 */
6721
                event = virDomainEventLifecycleNewFromObj(vm,
6722 6723 6724 6725
                                VIR_DOMAIN_EVENT_STARTED,
                                VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            } else if (was_running) {
                /* Transition 8 */
6726
                event = virDomainEventLifecycleNewFromObj(vm,
6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738
                                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);
6739
            event = virDomainEventLifecycleNewFromObj(vm,
6740 6741 6742 6743 6744 6745 6746 6747 6748
                                    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;

6749
            testObjectEventQueue(privconn, event);
6750
            event = virDomainEventLifecycleNewFromObj(vm,
6751 6752 6753
                            VIR_DOMAIN_EVENT_STARTED,
                            VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            if (paused) {
6754
                event2 = virDomainEventLifecycleNewFromObj(vm,
6755 6756 6757 6758 6759 6760 6761 6762
                                VIR_DOMAIN_EVENT_SUSPENDED,
                                VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT);
            }
        }
    }

    vm->current_snapshot = snap;
    ret = 0;
6763
 cleanup:
6764
    if (event) {
6765
        testObjectEventQueue(privconn, event);
6766
        testObjectEventQueue(privconn, event2);
C
Cole Robinson 已提交
6767
    } else {
C
Cédric Bosdonnat 已提交
6768
        virObjectUnref(event2);
6769
    }
6770
    virDomainObjEndAPI(&vm);
6771 6772 6773 6774 6775

    return ret;
}


6776

6777
static virHypervisorDriver testHypervisorDriver = {
6778
    .name = "Test",
6779 6780 6781
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
6782
    .connectGetHostname = testConnectGetHostname, /* 0.6.3 */
6783
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
6784
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
6785
    .nodeGetCPUStats = testNodeGetCPUStats, /* 2.3.0 */
6786
    .nodeGetFreeMemory = testNodeGetFreeMemory, /* 2.3.0 */
6787
    .nodeGetFreePages = testNodeGetFreePages, /* 2.3.0 */
6788
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
6789
    .connectGetSysinfo = testConnectGetSysinfo, /* 2.3.0 */
6790
    .connectGetType = testConnectGetType, /* 2.3.0 */
6791 6792 6793
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
6794
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808
    .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 */
6809 6810
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
6811
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
6812
    .domainRestore = testDomainRestore, /* 0.3.2 */
6813
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
6814
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
6815
    .domainCoreDumpWithFormat = testDomainCoreDumpWithFormat, /* 1.2.3 */
6816
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
6817 6818 6819 6820
    .domainSetVcpusFlags = testDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = testDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = testDomainPinVcpu, /* 0.7.3 */
    .domainGetVcpus = testDomainGetVcpus, /* 0.7.3 */
6821
    .domainGetVcpuPinInfo = testDomainGetVcpuPinInfo, /* 1.2.18 */
6822 6823
    .domainGetMaxVcpus = testDomainGetMaxVcpus, /* 0.7.3 */
    .domainGetXMLDesc = testDomainGetXMLDesc, /* 0.1.4 */
6824 6825
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
6826 6827 6828
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
6829
    .domainDefineXMLFlags = testDomainDefineXMLFlags, /* 1.2.12 */
6830
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
6831
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
6832 6833 6834
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
6835 6836 6837 6838
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
6839 6840 6841
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
6842 6843 6844 6845
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
6846 6847 6848
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
6849 6850 6851
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
6852
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
6853
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
6854 6855
    .domainGetMetadata = testDomainGetMetadata, /* 1.1.3 */
    .domainSetMetadata = testDomainSetMetadata, /* 1.1.3 */
6856
    .connectGetCPUModelNames = testConnectGetCPUModelNames, /* 1.1.3 */
6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873
    .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 */
6874 6875 6876
    .domainSnapshotCreateXML = testDomainSnapshotCreateXML, /* 1.1.4 */
    .domainRevertToSnapshot = testDomainRevertToSnapshot, /* 1.1.4 */
    .domainSnapshotDelete = testDomainSnapshotDelete, /* 1.1.4 */
6877

E
Eric Blake 已提交
6878
    .connectBaselineCPU = testConnectBaselineCPU, /* 1.2.0 */
6879 6880 6881
};

static virNetworkDriver testNetworkDriver = {
6882 6883 6884 6885 6886
    .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 */
6887 6888
    .connectNetworkEventRegisterAny = testConnectNetworkEventRegisterAny, /* 1.2.1 */
    .connectNetworkEventDeregisterAny = testConnectNetworkEventDeregisterAny, /* 1.2.1 */
6889 6890 6891 6892
    .networkLookupByUUID = testNetworkLookupByUUID, /* 0.3.2 */
    .networkLookupByName = testNetworkLookupByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreateXML, /* 0.3.2 */
    .networkDefineXML = testNetworkDefineXML, /* 0.3.2 */
6893
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
6894
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
6895
    .networkCreate = testNetworkCreate, /* 0.3.2 */
6896 6897 6898 6899 6900 6901 6902
    .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 */
6903 6904
};

L
Laine Stump 已提交
6905
static virInterfaceDriver testInterfaceDriver = {
6906 6907 6908 6909 6910 6911
    .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 */
6912 6913 6914 6915 6916 6917
    .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 */
6918 6919 6920
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
6921 6922 6923
};


6924
static virStorageDriver testStorageDriver = {
6925 6926 6927 6928 6929 6930
    .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 */
6931 6932
    .connectStoragePoolEventRegisterAny = testConnectStoragePoolEventRegisterAny, /* 2.0.0 */
    .connectStoragePoolEventDeregisterAny = testConnectStoragePoolEventDeregisterAny, /* 2.0.0 */
6933 6934 6935
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
6936 6937
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
6938 6939
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
6940
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
6941 6942 6943 6944 6945 6946 6947
    .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 */
6948
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
6949 6950 6951
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

6952 6953 6954 6955 6956 6957 6958 6959 6960
    .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 */
6961 6962
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
6963 6964
};

6965
static virNodeDeviceDriver testNodeDeviceDriver = {
6966 6967
    .connectNodeDeviceEventRegisterAny = testConnectNodeDeviceEventRegisterAny, /* 2.2.0 */
    .connectNodeDeviceEventDeregisterAny = testConnectNodeDeviceEventDeregisterAny, /* 2.2.0 */
6968 6969 6970 6971 6972 6973 6974 6975 6976
    .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 */
6977 6978
};

6979 6980 6981 6982 6983 6984 6985 6986
static virConnectDriver testConnectDriver = {
    .hypervisorDriver = &testHypervisorDriver,
    .interfaceDriver = &testInterfaceDriver,
    .networkDriver = &testNetworkDriver,
    .nodeDeviceDriver = &testNodeDeviceDriver,
    .nwfilterDriver = NULL,
    .secretDriver = NULL,
    .storageDriver = &testStorageDriver,
6987 6988
};

6989 6990 6991 6992 6993 6994 6995 6996
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
6997 6998
    return virRegisterConnectDriver(&testConnectDriver,
                                    false);
6999
}