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

24
#include <config.h>
25

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

35

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

68 69
#define VIR_FROM_THIS VIR_FROM_TEST

70 71
VIR_LOG_INIT("test.test_driver");

72

73 74 75 76
#define MAX_CPUS 128

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

#define MAX_CELLS 128
85

86 87 88 89 90 91 92
struct _testAuth {
    char *username;
    char *password;
};
typedef struct _testAuth testAuth;
typedef struct _testAuth *testAuthPtr;

93
struct _testDriver {
94
    virMutex lock;
95

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

107 108 109
    /* virAtomic access only */
    volatile int nextDomID;

110 111 112 113 114 115 116 117 118 119
    /* 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;
120
    virObjectEventStatePtr eventState;
121
};
122 123
typedef struct _testDriver testDriver;
typedef testDriver *testDriverPtr;
124

125
static testDriverPtr defaultConn;
126
static int defaultConnections;
127
static virMutex defaultLock = VIR_MUTEX_INITIALIZER;
128

129
#define TEST_MODEL "i686"
130
#define TEST_EMULATOR "/usr/bin/test-hv"
131

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

143 144 145 146 147 148 149 150 151 152 153 154 155
static void
testDriverFree(testDriverPtr driver)
{
    if (!driver)
        return;

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

    VIR_FREE(driver);
}
162

163

164
static void testDriverLock(testDriverPtr driver)
165
{
166
    virMutexLock(&driver->lock);
167 168
}

169
static void testDriverUnlock(testDriverPtr driver)
170
{
171
    virMutexUnlock(&driver->lock);
172 173
}

174 175 176 177 178 179 180 181 182
static void testObjectEventQueue(testDriverPtr driver,
                                 virObjectEventPtr event)
{
    if (!event)
        return;

    virObjectEventStateQueue(driver->eventState, event);
}

183 184 185 186 187 188
#define TEST_NAMESPACE_HREF "http://libvirt.org/schemas/domain/test/1.0"

typedef struct _testDomainNamespaceDef testDomainNamespaceDef;
typedef testDomainNamespaceDef *testDomainNamespaceDefPtr;
struct _testDomainNamespaceDef {
    int runstate;
189
    bool transient;
C
Cole Robinson 已提交
190
    bool hasManagedSave;
191 192 193

    unsigned int num_snap_nodes;
    xmlNodePtr *snap_nodes;
194 195 196 197 198 199
};

static void
testDomainDefNamespaceFree(void *data)
{
    testDomainNamespaceDefPtr nsdata = data;
200 201 202 203 204 205 206 207 208
    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);
209 210 211 212 213 214 215 216 217 218
    VIR_FREE(nsdata);
}

static int
testDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
                            xmlNodePtr root ATTRIBUTE_UNUSED,
                            xmlXPathContextPtr ctxt,
                            void **data)
{
    testDomainNamespaceDefPtr nsdata = NULL;
219 220 221
    xmlNodePtr *nodes = NULL;
    int tmp, n;
    size_t i;
222 223 224 225 226 227 228 229 230 231 232 233 234
    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;

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    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);

254 255 256 257 258 259 260
    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 已提交
261 262 263 264 265 266 267
    tmp = virXPathBoolean("boolean(./test:hasmanagedsave)", ctxt);
    if (tmp == -1) {
        virReportError(VIR_ERR_XML_ERROR, "%s", _("invalid hasmanagedsave"));
        goto error;
    }
    nsdata->hasManagedSave = tmp;

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    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;
    }

284 285 286 287 288
    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 已提交
289 290 291 292 293
    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;
    }
294

295 296 297
    *data = nsdata;
    return 0;

298
 error:
299
    VIR_FREE(nodes);
300 301 302
    testDomainDefNamespaceFree(nsdata);
    return -1;
}
303

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

314
    if ((caps = virCapabilitiesNew(VIR_ARCH_I686, false, false)) == NULL)
315
        goto error;
316

317
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
318
        goto error;
319
    if (virCapabilitiesAddHostFeature(caps, "nonpae") < 0)
320
        goto error;
321

322 323 324 325 326 327
    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;

328
    for (i = 0; i < privconn->numCells; i++) {
329
        virCapsHostNUMACellCPUPtr cpu_cells;
330 331
        virCapsHostNUMACellPageInfoPtr pages;
        size_t nPages;
332

333 334 335 336 337 338 339
        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;
340 341 342 343

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

344 345 346 347
        for (j = 0; j < nPages; j++)
            pages[j].size = caps->host.pagesSize[j];

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

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

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

365
        if (virCapabilitiesAddGuestDomain(guest,
366
                                          VIR_DOMAIN_VIRT_TEST,
367 368 369 370
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
371
            goto error;
372

373
        if (virCapabilitiesAddGuestFeature(guest, "pae", true, true) == NULL)
374
            goto error;
375
        if (virCapabilitiesAddGuestFeature(guest, "nonpae", true, true) == NULL)
376
            goto error;
377 378
    }

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

385 386
    if (VIR_STRDUP(caps->host.secModels[0].doi, "") < 0)
        goto error;
387

388
    return caps;
389

390
 error:
391
    virObjectUnref(caps);
392
    return NULL;
393 394
}

395

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
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;
    }

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

420
    virAtomicIntSet(&ret->nextDomID, 1);
421 422 423 424 425 426 427 428 429

    return ret;

 error:
    testDriverFree(ret);
    return NULL;
}


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

C
Cole Robinson 已提交
538

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

562
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
563
static const unsigned long long defaultPoolAlloc;
C
Cole Robinson 已提交
564

565
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool);
566
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
567

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

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

    return vm;
}

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

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

597
        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0)
598 599 600
            return NULL;

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

        if (!found)
            return ifname;
611
        VIR_FREE(ifname);
612 613
    }

614 615
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Exceeded max iface limit %d"), maxif);
616 617 618
    return NULL;
}

619
static int
620
testDomainGenerateIfnames(virDomainDefPtr domdef)
621
{
622
    size_t i = 0;
623 624 625 626 627 628

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

629
        ifname = testDomainGenerateIfname(domdef);
630
        if (!ifname)
631
            return -1;
632 633 634 635

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

636
    return 0;
637 638
}

639

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

648 649 650 651
    if (domain)
        domain->id = -1;
}

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

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

663
    if (virDomainObjSetDefTransient(privconn->caps,
664
                                    privconn->xmlopt,
665
                                    dom) < 0) {
666 667 668
        goto cleanup;
    }

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

677

678
static char *testBuildFilename(const char *relativeTo,
679 680
                               const char *filename)
{
681 682
    char *offset;
    int baseLen;
683 684
    char *ret;

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

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

C
Cole Robinson 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
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;
    }

741
 error:
C
Cole Robinson 已提交
742 743 744 745 746
    xmlFreeDoc(doc);
    VIR_FREE(absFile);
    return ret;
}

747 748 749
static int
testParseNodeInfo(virNodeInfoPtr nodeInfo, xmlXPathContextPtr ctxt)
{
750
    char *str;
751 752
    long l;
    int ret;
753

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

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

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

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

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

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

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

830
    return 0;
831
 error:
832 833 834
    return -1;
}

835
static int
836
testParseDomainSnapshots(testDriverPtr privconn,
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
                         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;
887
 error:
888 889 890
    return ret;
}

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

    num = virXPathNodeSet("/node/domain", ctxt, &nodes);
902
    if (num < 0)
903 904
        goto error;

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

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

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

927 928 929 930 931
        if (testParseDomainSnapshots(privconn, obj, file, ctxt) < 0) {
            virObjectUnlock(obj);
            goto error;
        }

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

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

947
        virObjectUnlock(obj);
948
    }
949

950
    ret = 0;
951
 error:
952 953 954 955 956
    VIR_FREE(nodes);
    return ret;
}

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

    num = virXPathNodeSet("/node/network", ctxt, &nodes);
967
    if (num < 0)
968
        goto error;
969 970

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

C
Cole Robinson 已提交
976 977 978
        def = virNetworkDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
979

980
        if (!(obj = virNetworkAssignDef(privconn->networks, def, 0))) {
981 982
            virNetworkDefFree(def);
            goto error;
983
        }
984 985

        obj->active = 1;
986
        virNetworkObjEndAPI(&obj);
987
    }
988

989
    ret = 0;
990
 error:
991 992 993 994 995
    VIR_FREE(nodes);
    return ret;
}

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

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

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

C
Cole Robinson 已提交
1016 1017 1018
        def = virInterfaceDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
1019

1020
        if (!(obj = virInterfaceAssignDef(&privconn->ifaces, def))) {
L
Laine Stump 已提交
1021 1022 1023
            virInterfaceDefFree(def);
            goto error;
        }
1024

1025 1026 1027 1028 1029
        obj->active = 1;
        virInterfaceObjUnlock(obj);
    }

    ret = 0;
1030
 error:
1031 1032 1033 1034 1035
    VIR_FREE(nodes);
    return ret;
}

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

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

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

        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;
1075 1076
        if (VIR_APPEND_ELEMENT_COPY(pool->volumes.objs, pool->volumes.count, def) < 0)
            goto error;
1077

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

1084
    ret = 0;
1085
 error:
1086 1087 1088 1089 1090 1091
    virStorageVolDefFree(def);
    VIR_FREE(nodes);
    return ret;
}

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

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

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

C
Cole Robinson 已提交
1112 1113 1114
        def = virStoragePoolDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
C
Cole Robinson 已提交
1115

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

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

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

1134
        virStoragePoolObjUnlock(obj);
C
Cole Robinson 已提交
1135 1136
    }

1137
    ret = 0;
1138
 error:
1139 1140 1141 1142 1143
    VIR_FREE(nodes);
    return ret;
}

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

    num = virXPathNodeSet("/node/device", ctxt, &nodes);
1154
    if (num < 0)
1155
        goto error;
1156 1157

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

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

        if (!(obj = virNodeDeviceAssignDef(&privconn->devs, def))) {
1169 1170 1171
            virNodeDeviceDefFree(def);
            goto error;
        }
1172 1173 1174 1175 1176

        virNodeDeviceObjUnlock(obj);
    }

    ret = 0;
1177
 error:
1178 1179 1180 1181
    VIR_FREE(nodes);
    return ret;
}

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

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

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

1262
    if (!(privconn = testDriverNew()))
1263
        return VIR_DRV_OPEN_ERROR;
1264

1265 1266 1267 1268 1269 1270
    testDriverLock(privconn);
    conn->privateData = privconn;

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

1271
    if (!(doc = virXMLParseFileCtxt(file, &ctxt)))
1272 1273 1274 1275 1276
        goto error;

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

C
Cole Robinson 已提交
1277
    if (testOpenParse(privconn, file, ctxt) < 0)
1278
        goto error;
1279

J
Jim Meyering 已提交
1280
    xmlXPathFreeContext(ctxt);
1281
    xmlFreeDoc(doc);
1282
    testDriverUnlock(privconn);
1283

1284
    return 0;
1285 1286

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

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

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

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

1340 1341
    if (!(doc = virXMLParseStringCtxt(defaultConnXML,
                                      _("(test driver)"), &ctxt)))
1342 1343
        goto error;

1344
    if (testOpenParse(privconn, NULL, ctxt) < 0)
1345 1346 1347 1348
        goto error;

    defaultConn = privconn;

1349 1350
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);
1351 1352 1353 1354 1355 1356
    virMutexUnlock(&defaultLock);

    return VIR_DRV_OPEN_SUCCESS;

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

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

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

1420
static virDrvOpenStatus testConnectOpen(virConnectPtr conn,
1421
                                        virConnectAuthPtr auth,
1422
                                        virConfPtr conf ATTRIBUTE_UNUSED,
1423
                                        unsigned int flags)
1424
{
1425
    int ret;
1426

E
Eric Blake 已提交
1427 1428
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1429
    if (!conn->uri)
1430
        return VIR_DRV_OPEN_DECLINED;
1431

1432
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1433
        return VIR_DRV_OPEN_DECLINED;
1434

1435
    /* Remote driver should handle these. */
1436
    if (conn->uri->server)
1437 1438
        return VIR_DRV_OPEN_DECLINED;

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

1448
    if (STREQ(conn->uri->path, "/default"))
1449 1450
        ret = testOpenDefault(conn);
    else
1451
        ret = testOpenFromFile(conn,
1452
                               conn->uri->path);
1453

1454 1455 1456
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

1457 1458 1459 1460
    /* Fake authentication. */
    if (testConnectAuthenticate(conn, auth) < 0)
        return VIR_DRV_OPEN_ERROR;

1461
    return VIR_DRV_OPEN_SUCCESS;
1462 1463
}

1464
static int testConnectClose(virConnectPtr conn)
1465
{
1466
    testDriverPtr privconn = conn->privateData;
1467
    bool dflt = false;
1468

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

1478
    testDriverLock(privconn);
1479
    testDriverFree(privconn);
1480 1481 1482

    if (dflt) {
        defaultConn = NULL;
1483
        virMutexUnlock(&defaultLock);
1484 1485
    }

1486
    conn->privateData = NULL;
1487
    return 0;
1488 1489
}

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

1497 1498 1499 1500 1501 1502
static char *testConnectGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return virGetHostname();
}


1503
static int testConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
1504 1505 1506 1507
{
    return 1;
}

1508
static int testConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
1509 1510 1511 1512
{
    return 0;
}

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

1518 1519
static int testConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type ATTRIBUTE_UNUSED)
1520 1521 1522 1523
{
    return 32;
}

1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
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;
}

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

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

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

1579 1580 1581 1582 1583 1584
static const char *
testConnectGetType(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return "TEST";
}

1585
static int testConnectNumOfDomains(virConnectPtr conn)
1586
{
1587
    testDriverPtr privconn = conn->privateData;
1588
    int count;
1589

1590
    testDriverLock(privconn);
1591
    count = virDomainObjListNumOfDomains(privconn->domains, true, NULL, NULL);
1592
    testDriverUnlock(privconn);
1593

1594
    return count;
1595 1596
}

1597 1598 1599
static int testDomainIsActive(virDomainPtr dom)
{
    virDomainObjPtr obj;
1600
    int ret;
1601

1602 1603
    if (!(obj = testDomObjFromDomain(dom)))
        return -1;
1604

1605 1606
    ret = virDomainObjIsActive(obj);
    virDomainObjEndAPI(&obj);
1607 1608 1609 1610 1611 1612
    return ret;
}

static int testDomainIsPersistent(virDomainPtr dom)
{
    virDomainObjPtr obj;
1613 1614 1615 1616
    int ret;

    if (!(obj = testDomObjFromDomain(dom)))
        return -1;
1617 1618 1619

    ret = obj->persistent;

1620
    virDomainObjEndAPI(&obj);
1621 1622 1623
    return ret;
}

1624 1625 1626 1627 1628
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

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

1640 1641 1642
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_START_VALIDATE)
1643
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
1644

1645
    testDriverLock(privconn);
1646
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
1647
                                       NULL, parse_flags)) == NULL)
1648
        goto cleanup;
1649

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

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

1669
    event = virDomainEventLifecycleNewFromObj(dom,
1670 1671 1672
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1673
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1674
    if (ret)
1675
        ret->id = dom->def->id;
1676

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


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

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

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

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);
1722 1723 1724
    if (ret)
        ret->id = dom->def->id;

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

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

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

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

1747
 cleanup:
1748
    virDomainObjEndAPI(&dom);
1749
    return ret;
1750 1751
}

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

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

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

1769
    if (!(privdom = testDomObjFromDomain(domain)))
1770
        goto cleanup;
1771

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

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

1783 1784
    if (!privdom->persistent)
        virDomainObjListRemove(privconn->domains, privdom);
1785 1786

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

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

1800 1801
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1802

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

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

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

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

1830 1831
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1832

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

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

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

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

1860 1861
    virCheckFlags(0, -1);

1862

1863
    if (!(privdom = testDomObjFromDomain(domain)))
1864
        goto cleanup;
1865

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

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

1877 1878
    if (!privdom->persistent)
        virDomainObjListRemove(privconn->domains, privdom);
1879

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

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

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


1902
    if (!(privdom = testDomObjFromDomain(domain)))
1903
        goto cleanup;
1904

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

J
Jiri Denemark 已提交
1911 1912 1913
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

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

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

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

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

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

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

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

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

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

1965 1966
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1967 1968

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

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

1981
 cleanup:
1982
    virDomainObjEndAPI(&privdom);
1983
    return ret;
1984 1985
}

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

    virCheckFlags(0, -1);

1996 1997
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
1998

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

2001
    virDomainObjEndAPI(&privdom);
2002 2003

    return 0;
2004 2005
}

2006 2007
#define TEST_SAVE_MAGIC "TestGuestMagic"

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

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

2027

2028
    if (!(privdom = testDomObjFromDomain(domain)))
2029
        goto cleanup;
2030

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

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

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

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

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

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

2086 2087
    if (!privdom->persistent)
        virDomainObjListRemove(privconn->domains, privdom);
2088

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

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

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

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

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

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

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

2196
    event = virDomainEventLifecycleNewFromObj(dom,
2197 2198
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
2199
    ret = 0;
2200

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

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

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

E
Eric Blake 已提交
2229 2230
    virCheckFlags(VIR_DUMP_CRASH, -1);

2231

2232
    if (!(privdom = testDomObjFromDomain(domain)))
2233
        goto cleanup;
2234

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

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

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

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

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

2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297

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)
{
2298 2299 2300
    char *ret;

    ignore_value(VIR_STRDUP(ret, "linux"));
2301
    return ret;
2302 2303
}

2304 2305 2306

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

2311 2312
    if (!(privdom = testDomObjFromDomain(domain)))
        return 0;
2313

2314
    ret = virDomainDefGetMemoryTotal(privdom->def);
2315

2316
    virDomainObjEndAPI(&privdom);
2317
    return ret;
2318 2319
}

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

2325 2326
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2327 2328

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

2331
    virDomainObjEndAPI(&privdom);
2332
    return 0;
2333 2334
}

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

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

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

2349
    privdom->def->mem.cur_balloon = memory;
2350 2351
    ret = 0;

2352
 cleanup:
2353
    virDomainObjEndAPI(&privdom);
2354
    return ret;
2355 2356
}

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

2364 2365
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2366 2367
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

2368 2369
    if (!(vm = testDomObjFromDomain(domain)))
        return -1;
2370

2371
    if (!(def = virDomainObjGetOneDef(vm, flags)))
2372
        goto cleanup;
2373

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

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

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

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

2401 2402
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2403 2404
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

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

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

2415 2416
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2417

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

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

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

2437 2438 2439
    if (def &&
        virDomainDefSetVcpus(def, nrCpus) < 0)
        goto cleanup;
2440

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

2452 2453
    ret = 0;

2454
 cleanup:
2455
    virDomainObjEndAPI(&privdom);
2456
    return ret;
2457 2458
}

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

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

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

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

2490
    def = privdom->def;
C
Cole Robinson 已提交
2491 2492

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

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

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

2505 2506 2507 2508 2509
    if (!(allcpumap = virBitmapNew(hostcpus)))
        goto cleanup;

    virBitmapSetAll(allcpumap);

C
Cole Robinson 已提交
2510
    /* Clamp to actual number of vcpus */
2511 2512
    if (maxinfo > virDomainDefGetVcpus(privdom->def))
        maxinfo = virDomainDefGetVcpus(privdom->def);
C
Cole Robinson 已提交
2513

2514 2515
    memset(info, 0, sizeof(*info) * maxinfo);
    memset(cpumaps, 0, maxinfo * maplen);
C
Cole Robinson 已提交
2516

2517
    for (i = 0; i < maxinfo; i++) {
2518
        virDomainVcpuDefPtr vcpu = virDomainDefGetVcpu(def, i);
2519
        virBitmapPtr bitmap = NULL;
C
Cole Robinson 已提交
2520

2521 2522
        if (!vcpu->online)
            continue;
C
Cole Robinson 已提交
2523

2524 2525
        if (vcpu->cpumask)
            bitmap = vcpu->cpumask;
2526 2527 2528 2529
        else if (def->cpumask)
            bitmap = def->cpumask;
        else
            bitmap = allcpumap;
C
Cole Robinson 已提交
2530

2531 2532
        if (cpumaps)
            virBitmapToDataBuf(bitmap, VIR_GET_CPUMAP(cpumaps, maplen, i), maplen);
C
Cole Robinson 已提交
2533

2534 2535 2536
        info[i].number = i;
        info[i].state = VIR_VCPU_RUNNING;
        info[i].cpu = virBitmapLastSetBit(bitmap);
C
Cole Robinson 已提交
2537

2538 2539
        /* Fake an increasing cpu time value */
        info[i].cpuTime = statbase / 10;
C
Cole Robinson 已提交
2540 2541 2542
    }

    ret = maxinfo;
2543
 cleanup:
2544
    virBitmapFree(allcpumap);
2545
    virDomainObjEndAPI(&privdom);
C
Cole Robinson 已提交
2546 2547 2548
    return ret;
}

C
Cole Robinson 已提交
2549 2550 2551 2552 2553
static int testDomainPinVcpu(virDomainPtr domain,
                             unsigned int vcpu,
                             unsigned char *cpumap,
                             int maplen)
{
2554
    virDomainVcpuDefPtr vcpuinfo;
C
Cole Robinson 已提交
2555
    virDomainObjPtr privdom;
2556
    virDomainDefPtr def;
C
Cole Robinson 已提交
2557 2558
    int ret = -1;

2559 2560
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
C
Cole Robinson 已提交
2561

2562 2563
    def = privdom->def;

C
Cole Robinson 已提交
2564
    if (!virDomainObjIsActive(privdom)) {
2565
        virReportError(VIR_ERR_OPERATION_INVALID,
2566
                       "%s", _("cannot pin vcpus on an inactive domain"));
C
Cole Robinson 已提交
2567 2568 2569
        goto cleanup;
    }

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

2578 2579 2580
    virBitmapFree(vcpuinfo->cpumask);

    if (!(vcpuinfo->cpumask = virBitmapNewData(cpumap, maplen)))
2581
        goto cleanup;
C
Cole Robinson 已提交
2582 2583

    ret = 0;
2584

2585
 cleanup:
2586
    virDomainObjEndAPI(&privdom);
C
Cole Robinson 已提交
2587 2588 2589
    return ret;
}

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

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

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

2608 2609 2610
    ret = virDomainDefGetVcpuPinInfoHelper(def, maplen, ncpumaps, cpumaps,
                                           VIR_NODEINFO_MAXCPUS(driver->nodeInfo),
                                           NULL);
2611 2612 2613 2614 2615 2616

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

2617
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2618
{
2619
    testDriverPtr privconn = domain->conn->privateData;
2620
    virDomainDefPtr def;
2621
    virDomainObjPtr privdom;
2622 2623
    char *ret = NULL;

2624 2625
    /* Flags checked by virDomainDefFormat */

2626 2627
    if (!(privdom = testDomObjFromDomain(domain)))
        return NULL;
2628

2629 2630
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2631

2632 2633
    ret = virDomainDefFormat(def, privconn->caps,
                             virDomainDefFormatConvertXMLFlags(flags));
2634

2635
    virDomainObjEndAPI(&privdom);
2636
    return ret;
2637
}
2638

2639 2640
static int testConnectNumOfDefinedDomains(virConnectPtr conn)
{
2641
    testDriverPtr privconn = conn->privateData;
2642

2643
    return virDomainObjListNumOfDomains(privconn->domains, false, NULL, NULL);
2644 2645
}

2646 2647
static int testConnectListDefinedDomains(virConnectPtr conn,
                                         char **const names,
2648 2649
                                         int maxnames)
{
2650

2651
    testDriverPtr privconn = conn->privateData;
2652 2653

    memset(names, 0, sizeof(*names)*maxnames);
2654 2655
    return virDomainObjListGetInactiveNames(privconn->domains, names, maxnames,
                                            NULL, NULL);
2656 2657
}

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

    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);
2671

2672
    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
2673
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE_SCHEMA;
2674

2675
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
2676
                                       NULL, parse_flags)) == NULL)
2677
        goto cleanup;
2678

2679 2680 2681
    if (virXMLCheckIllegalChars("name", def->name, "\n") < 0)
        goto cleanup;

2682
    if (testDomainGenerateIfnames(def) < 0)
2683
        goto cleanup;
2684
    if (!(dom = virDomainObjListAdd(privconn->domains,
2685
                                    def,
2686
                                    privconn->xmlopt,
2687 2688
                                    0,
                                    &oldDef)))
2689
        goto cleanup;
2690
    def = NULL;
2691
    dom->persistent = 1;
2692

2693
    event = virDomainEventLifecycleNewFromObj(dom,
2694
                                     VIR_DOMAIN_EVENT_DEFINED,
2695
                                     !oldDef ?
2696 2697
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2698

2699
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2700
    if (ret)
2701
        ret->id = dom->def->id;
2702

2703
 cleanup:
2704
    virDomainDefFree(def);
2705
    virDomainDefFree(oldDef);
2706
    if (dom)
2707
        virObjectUnlock(dom);
2708
    testObjectEventQueue(privconn, event);
2709
    return ret;
2710 2711
}

2712 2713 2714 2715 2716 2717
static virDomainPtr
testDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return testDomainDefineXMLFlags(conn, xml, 0);
}

2718 2719 2720 2721 2722 2723
static char *testDomainGetMetadata(virDomainPtr dom,
                                   int type,
                                   const char *uri,
                                   unsigned int flags)
{
    virDomainObjPtr privdom;
2724
    char *ret;
2725 2726 2727 2728

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, NULL);

2729 2730
    if (!(privdom = testDomObjFromDomain(dom)))
        return NULL;
2731

2732
    ret = virDomainObjGetMetadata(privdom, type, uri, flags);
2733

2734
    virDomainObjEndAPI(&privdom);
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
    return ret;
}

static int testDomainSetMetadata(virDomainPtr dom,
                                 int type,
                                 const char *metadata,
                                 const char *key,
                                 const char *uri,
                                 unsigned int flags)
{
2745
    testDriverPtr privconn = dom->conn->privateData;
2746
    virDomainObjPtr privdom;
2747
    int ret;
2748 2749 2750 2751

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

2752 2753
    if (!(privdom = testDomObjFromDomain(dom)))
        return -1;
2754 2755 2756

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

2759 2760 2761 2762 2763 2764
    if (ret == 0) {
        virObjectEventPtr ev = NULL;
        ev = virDomainEventMetadataChangeNewFromObj(privdom, type, uri);
        testObjectEventQueue(privconn, ev);
    }

2765
    virDomainObjEndAPI(&privdom);
2766 2767 2768 2769
    return ret;
}


2770 2771
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
2772 2773
                                      int startCell, int maxCells)
{
2774
    testDriverPtr privconn = conn->privateData;
2775 2776
    int cell;
    size_t i;
2777
    int ret = -1;
2778

2779
    testDriverLock(privconn);
2780
    if (startCell >= privconn->numCells) {
2781 2782
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("Range exceeds available cells"));
2783
        goto cleanup;
2784 2785
    }

2786 2787 2788 2789
    for (cell = startCell, i = 0;
         (cell < privconn->numCells && i < maxCells);
         ++cell, ++i) {
        freemems[i] = privconn->cells[cell].mem;
2790
    }
2791
    ret = i;
2792

2793
 cleanup:
2794
    testDriverUnlock(privconn);
2795
    return ret;
2796 2797
}

2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843
#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;
}
2844

2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860
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;
}

2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884
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;
}

2885 2886
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
{
2887
    testDriverPtr privconn = domain->conn->privateData;
2888
    virDomainObjPtr privdom;
2889
    virObjectEventPtr event = NULL;
2890
    int ret = -1;
2891

2892 2893
    virCheckFlags(0, -1);

2894
    testDriverLock(privconn);
2895

2896
    if (!(privdom = testDomObjFromDomain(domain)))
2897
        goto cleanup;
2898

J
Jiri Denemark 已提交
2899
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
2900 2901
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Domain '%s' is already running"), domain->name);
2902
        goto cleanup;
2903 2904
    }

2905
    if (testDomainStartState(privconn, privdom,
J
Jiri Denemark 已提交
2906
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
2907 2908 2909
        goto cleanup;
    domain->id = privdom->def->id;

2910
    event = virDomainEventLifecycleNewFromObj(privdom,
2911 2912
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2913
    ret = 0;
2914

2915
 cleanup:
2916
    virDomainObjEndAPI(&privdom);
2917
    testObjectEventQueue(privconn, event);
2918
    testDriverUnlock(privconn);
2919
    return ret;
2920 2921
}

2922 2923
static int testDomainCreate(virDomainPtr domain)
{
2924 2925 2926
    return testDomainCreateWithFlags(domain, 0);
}

2927 2928 2929
static int testDomainUndefineFlags(virDomainPtr domain,
                                   unsigned int flags)
{
2930
    testDriverPtr privconn = domain->conn->privateData;
2931
    virDomainObjPtr privdom;
2932
    virObjectEventPtr event = NULL;
2933
    int nsnapshots;
2934
    int ret = -1;
2935

2936 2937
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
2938

2939

2940
    if (!(privdom = testDomObjFromDomain(domain)))
2941
        goto cleanup;
2942

C
Cole Robinson 已提交
2943 2944 2945 2946 2947 2948 2949 2950
    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;
    }

2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968
    /* 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. */
    }

2969
    event = virDomainEventLifecycleNewFromObj(privdom,
2970 2971
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
C
Cole Robinson 已提交
2972 2973
    privdom->hasManagedSave = false;

2974
    if (virDomainObjIsActive(privdom))
2975
        privdom->persistent = 0;
2976 2977
    else
        virDomainObjListRemove(privconn->domains, privdom);
2978

2979
    ret = 0;
2980

2981
 cleanup:
2982
    virDomainObjEndAPI(&privdom);
2983
    testObjectEventQueue(privconn, event);
2984
    return ret;
2985 2986
}

2987 2988 2989 2990 2991
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

2992 2993 2994
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2995 2996
    virDomainObjPtr privdom;

2997 2998
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
2999

3000
    *autostart = privdom->autostart;
3001

3002
    virDomainObjEndAPI(&privdom);
3003
    return 0;
3004 3005 3006 3007 3008 3009
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
3010 3011
    virDomainObjPtr privdom;

3012 3013
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3014

3015
    privdom->autostart = autostart ? 1 : 0;
3016

3017
    virDomainObjEndAPI(&privdom);
3018
    return 0;
3019
}
3020

3021
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
3022 3023
                                        int *nparams)
{
3024 3025
    char *type = NULL;

3026 3027 3028
    if (nparams)
        *nparams = 1;

3029
    ignore_value(VIR_STRDUP(type, "fair"));
3030

3031 3032 3033
    return type;
}

3034
static int
3035 3036 3037 3038
testDomainGetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int *nparams,
                                      unsigned int flags)
3039
{
3040
    virDomainObjPtr privdom;
3041
    int ret = -1;
3042

3043 3044
    virCheckFlags(0, -1);

3045 3046
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3047

3048 3049
    if (virTypedParameterAssign(params, VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, 50) < 0)
3050
        goto cleanup;
3051 3052
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
3053 3054

    *nparams = 1;
3055 3056
    ret = 0;

3057
 cleanup:
3058
    virDomainObjEndAPI(&privdom);
3059
    return ret;
3060
}
3061

3062
static int
3063 3064 3065
testDomainGetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int *nparams)
3066
{
3067
    return testDomainGetSchedulerParametersFlags(domain, params, nparams, 0);
3068
}
3069

3070
static int
3071 3072 3073 3074
testDomainSetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int nparams,
                                      unsigned int flags)
3075
{
3076
    virDomainObjPtr privdom;
3077 3078
    int ret = -1;
    size_t i;
3079

3080
    virCheckFlags(0, -1);
3081 3082 3083 3084
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_SCHEDULER_WEIGHT,
                               VIR_TYPED_PARAM_UINT,
                               NULL) < 0)
3085
        return -1;
3086

3087 3088
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3089

3090
    for (i = 0; i < nparams; i++) {
3091 3092 3093
        if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_WEIGHT)) {
            /* XXX */
            /*privdom->weight = params[i].value.ui;*/
3094
        }
3095
    }
3096

3097 3098
    ret = 0;

3099
    virDomainObjEndAPI(&privdom);
3100
    return ret;
3101 3102
}

3103
static int
3104 3105 3106
testDomainSetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int nparams)
3107
{
3108
    return testDomainSetSchedulerParametersFlags(domain, params, nparams, 0);
3109 3110
}

3111 3112
static int testDomainBlockStats(virDomainPtr domain,
                                const char *path,
3113
                                virDomainBlockStatsPtr stats)
3114 3115 3116 3117
{
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
3118
    int ret = -1;
3119

3120 3121 3122 3123 3124 3125
    if (!*path) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("summary statistics are not supported yet"));
        return ret;
    }

3126 3127
    if (!(privdom = testDomObjFromDomain(domain)))
        return ret;
3128

3129 3130 3131 3132 3133 3134
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto error;
    }

3135
    if (virDomainDiskIndexByName(privdom->def, path, false) < 0) {
3136 3137
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path: %s"), path);
3138 3139 3140 3141
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
3142
        virReportSystemError(errno,
3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155
                             "%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;
3156
 error:
3157
    virDomainObjEndAPI(&privdom);
3158 3159 3160 3161 3162
    return ret;
}

static int testDomainInterfaceStats(virDomainPtr domain,
                                    const char *path,
3163
                                    virDomainInterfaceStatsPtr stats)
3164 3165 3166 3167
{
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
3168 3169
    size_t i;
    int found = 0, ret = -1;
3170

3171 3172
    if (!(privdom = testDomObjFromDomain(domain)))
        return -1;
3173

3174 3175 3176 3177 3178 3179
    if (!virDomainObjIsActive(privdom)) {
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("domain is not running"));
        goto error;
    }

3180
    for (i = 0; i < privdom->def->nnets; i++) {
3181
        if (privdom->def->nets[i]->ifname &&
3182
            STREQ(privdom->def->nets[i]->ifname, path)) {
3183 3184 3185 3186 3187 3188
            found = 1;
            break;
        }
    }

    if (!found) {
3189 3190
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path, '%s' is not a known interface"), path);
3191 3192 3193 3194
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
3195
        virReportSystemError(errno,
3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211
                             "%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;
3212
 error:
3213
    virDomainObjEndAPI(&privdom);
3214 3215 3216
    return ret;
}

3217

3218 3219
static virNetworkPtr testNetworkLookupByUUID(virConnectPtr conn,
                                             const unsigned char *uuid)
3220
{
3221
    testDriverPtr privconn = conn->privateData;
3222
    virNetworkObjPtr net;
3223
    virNetworkPtr ret = NULL;
3224

3225
    net = virNetworkObjFindByUUID(privconn->networks, uuid);
3226
    if (net == NULL) {
3227
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3228
        goto cleanup;
3229 3230
    }

3231 3232
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

3233
 cleanup:
3234
    virNetworkObjEndAPI(&net);
3235
    return ret;
3236
}
3237

3238
static virNetworkPtr testNetworkLookupByName(virConnectPtr conn,
3239
                                             const char *name)
3240
{
3241
    testDriverPtr privconn = conn->privateData;
3242 3243
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
3244

3245
    net = virNetworkObjFindByName(privconn->networks, name);
3246
    if (net == NULL) {
3247
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3248
        goto cleanup;
3249 3250
    }

3251 3252
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

3253
 cleanup:
3254
    virNetworkObjEndAPI(&net);
3255
    return ret;
3256 3257 3258
}


3259 3260
static int testConnectNumOfNetworks(virConnectPtr conn)
{
3261
    testDriverPtr privconn = conn->privateData;
3262
    int numActive;
3263

3264 3265
    numActive = virNetworkObjListNumOfNetworks(privconn->networks,
                                               true, NULL, conn);
3266
    return numActive;
3267 3268
}

3269
static int testConnectListNetworks(virConnectPtr conn, char **const names, int nnames) {
3270
    testDriverPtr privconn = conn->privateData;
3271
    int n;
3272

3273 3274
    n = virNetworkObjListGetNames(privconn->networks,
                                  true, names, nnames, NULL, conn);
3275
    return n;
3276 3277
}

3278 3279
static int testConnectNumOfDefinedNetworks(virConnectPtr conn)
{
3280
    testDriverPtr privconn = conn->privateData;
3281
    int numInactive;
3282

3283 3284
    numInactive = virNetworkObjListNumOfNetworks(privconn->networks,
                                                 false, NULL, conn);
3285
    return numInactive;
3286 3287
}

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

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

3297
static int
3298
testConnectListAllNetworks(virConnectPtr conn,
3299 3300 3301
                           virNetworkPtr **nets,
                           unsigned int flags)
{
3302
    testDriverPtr privconn = conn->privateData;
3303 3304 3305

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

3306
    return virNetworkObjListExport(conn, privconn->networks, nets, NULL, flags);
3307
}
3308 3309 3310

static int testNetworkIsActive(virNetworkPtr net)
{
3311
    testDriverPtr privconn = net->conn->privateData;
3312 3313 3314
    virNetworkObjPtr obj;
    int ret = -1;

3315
    obj = virNetworkObjFindByUUID(privconn->networks, net->uuid);
3316
    if (!obj) {
3317
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3318 3319 3320 3321
        goto cleanup;
    }
    ret = virNetworkObjIsActive(obj);

3322
 cleanup:
3323
    virNetworkObjEndAPI(&obj);
3324 3325 3326 3327 3328
    return ret;
}

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

3333
    obj = virNetworkObjFindByUUID(privconn->networks, net->uuid);
3334
    if (!obj) {
3335
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3336 3337 3338 3339
        goto cleanup;
    }
    ret = obj->persistent;

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


3346 3347
static virNetworkPtr testNetworkCreateXML(virConnectPtr conn, const char *xml)
{
3348
    testDriverPtr privconn = conn->privateData;
3349
    virNetworkDefPtr def;
3350
    virNetworkObjPtr net = NULL;
3351
    virNetworkPtr ret = NULL;
3352
    virObjectEventPtr event = NULL;
3353

3354
    if ((def = virNetworkDefParseString(xml)) == NULL)
3355
        goto cleanup;
3356

3357 3358 3359
    if (!(net = virNetworkAssignDef(privconn->networks, def,
                                    VIR_NETWORK_OBJ_LIST_ADD_LIVE |
                                    VIR_NETWORK_OBJ_LIST_ADD_CHECK_LIVE)))
3360 3361
        goto cleanup;
    def = NULL;
3362
    net->active = 1;
3363

3364
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3365 3366
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3367

3368
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3369

3370
 cleanup:
3371
    virNetworkDefFree(def);
3372
    testObjectEventQueue(privconn, event);
3373
    virNetworkObjEndAPI(&net);
3374
    return ret;
3375 3376
}

3377
static
3378
virNetworkPtr testNetworkDefineXML(virConnectPtr conn, const char *xml)
3379
{
3380
    testDriverPtr privconn = conn->privateData;
3381
    virNetworkDefPtr def;
3382
    virNetworkObjPtr net = NULL;
3383
    virNetworkPtr ret = NULL;
3384
    virObjectEventPtr event = NULL;
3385

3386
    if ((def = virNetworkDefParseString(xml)) == NULL)
3387
        goto cleanup;
3388

3389
    if (!(net = virNetworkAssignDef(privconn->networks, def, 0)))
3390 3391
        goto cleanup;
    def = NULL;
3392

3393
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3394 3395
                                        VIR_NETWORK_EVENT_DEFINED,
                                        0);
3396

3397
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3398

3399
 cleanup:
3400
    virNetworkDefFree(def);
3401
    testObjectEventQueue(privconn, event);
3402
    virNetworkObjEndAPI(&net);
3403
    return ret;
3404 3405
}

3406 3407
static int testNetworkUndefine(virNetworkPtr network)
{
3408
    testDriverPtr privconn = network->conn->privateData;
3409
    virNetworkObjPtr privnet;
3410
    int ret = -1;
3411
    virObjectEventPtr event = NULL;
3412

3413
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3414 3415

    if (privnet == NULL) {
3416
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3417
        goto cleanup;
3418
    }
3419

D
Daniel P. Berrange 已提交
3420
    if (virNetworkObjIsActive(privnet)) {
3421 3422
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is still running"), network->name);
3423
        goto cleanup;
3424 3425
    }

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

3430
    virNetworkRemoveInactive(privconn->networks, privnet);
3431
    ret = 0;
3432

3433
 cleanup:
3434
    testObjectEventQueue(privconn, event);
3435
    virNetworkObjEndAPI(&privnet);
3436
    return ret;
3437 3438
}

3439 3440 3441 3442 3443 3444 3445 3446
static int
testNetworkUpdate(virNetworkPtr net,
                  unsigned int command,
                  unsigned int section,
                  int parentIndex,
                  const char *xml,
                  unsigned int flags)
{
3447
    testDriverPtr privconn = net->conn->privateData;
3448 3449 3450 3451 3452 3453 3454
    virNetworkObjPtr network = NULL;
    int isActive, ret = -1;

    virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE |
                  VIR_NETWORK_UPDATE_AFFECT_CONFIG,
                  -1);

3455
    network = virNetworkObjFindByUUID(privconn->networks, net->uuid);
3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479
    if (!network) {
        virReportError(VIR_ERR_NO_NETWORK,
                       "%s", _("no network with matching uuid"));
        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;
3480
 cleanup:
3481
    virNetworkObjEndAPI(&network);
3482 3483 3484
    return ret;
}

3485 3486
static int testNetworkCreate(virNetworkPtr network)
{
3487
    testDriverPtr privconn = network->conn->privateData;
3488
    virNetworkObjPtr privnet;
3489
    int ret = -1;
3490
    virObjectEventPtr event = NULL;
3491

3492
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3493
    if (privnet == NULL) {
3494
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3495
        goto cleanup;
3496
    }
3497

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

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

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

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

3523
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3524
    if (privnet == NULL) {
3525
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3526
        goto cleanup;
3527
    }
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
        virNetworkRemoveInactive(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
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3554
    if (privnet == NULL) {
3555
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3556
        goto cleanup;
3557
    }
3558

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

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

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

3571
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3572
    if (privnet == NULL) {
3573
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3574
        goto cleanup;
3575 3576
    }

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

3584
    ignore_value(VIR_STRDUP(bridge, privnet->def->bridge));
3585

3586
 cleanup:
3587
    virNetworkObjEndAPI(&privnet);
3588 3589 3590 3591
    return bridge;
}

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

3598
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3599
    if (privnet == NULL) {
3600
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3601
        goto cleanup;
3602 3603
    }

3604
    *autostart = privnet->autostart;
3605 3606
    ret = 0;

3607
 cleanup:
3608
    virNetworkObjEndAPI(&privnet);
3609
    return ret;
3610 3611 3612
}

static int testNetworkSetAutostart(virNetworkPtr network,
3613 3614
                                   int autostart)
{
3615
    testDriverPtr privconn = network->conn->privateData;
3616
    virNetworkObjPtr privnet;
3617
    int ret = -1;
3618

3619
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3620
    if (privnet == NULL) {
3621
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3622
        goto cleanup;
3623 3624
    }

3625
    privnet->autostart = autostart ? 1 : 0;
3626 3627
    ret = 0;

3628
 cleanup:
3629
    virNetworkObjEndAPI(&privnet);
3630
    return ret;
3631
}
3632

C
Cole Robinson 已提交
3633

L
Laine Stump 已提交
3634 3635 3636 3637 3638
/*
 * Physical host interface routines
 */


3639
static int testConnectNumOfInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3640
{
3641
    testDriverPtr privconn = conn->privateData;
3642 3643
    size_t i;
    int count = 0;
L
Laine Stump 已提交
3644 3645

    testDriverLock(privconn);
3646
    for (i = 0; (i < privconn->ifaces.count); i++) {
L
Laine Stump 已提交
3647
        virInterfaceObjLock(privconn->ifaces.objs[i]);
3648
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i]))
L
Laine Stump 已提交
3649 3650 3651 3652 3653 3654 3655
            count++;
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

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

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

    return n;

3678
 error:
3679
    for (n = 0; n < nnames; n++)
L
Laine Stump 已提交
3680 3681 3682 3683 3684
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

3685
static int testConnectNumOfDefinedInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3686
{
3687
    testDriverPtr privconn = conn->privateData;
3688 3689
    size_t i;
    int count = 0;
L
Laine Stump 已提交
3690 3691

    testDriverLock(privconn);
3692
    for (i = 0; i < privconn->ifaces.count; i++) {
L
Laine Stump 已提交
3693
        virInterfaceObjLock(privconn->ifaces.objs[i]);
3694
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i]))
L
Laine Stump 已提交
3695 3696 3697 3698 3699 3700 3701
            count++;
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

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

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

    return n;

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

3731
static virInterfacePtr testInterfaceLookupByName(virConnectPtr conn,
L
Laine Stump 已提交
3732 3733
                                                 const char *name)
{
3734
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3735 3736 3737 3738 3739 3740 3741 3742
    virInterfaceObjPtr iface;
    virInterfacePtr ret = NULL;

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

    if (iface == NULL) {
3743
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3744 3745 3746 3747 3748
        goto cleanup;
    }

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

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

3755
static virInterfacePtr testInterfaceLookupByMACString(virConnectPtr conn,
L
Laine Stump 已提交
3756 3757
                                                      const char *mac)
{
3758
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3759 3760 3761 3762 3763 3764 3765 3766 3767
    virInterfaceObjPtr iface;
    int ifacect;
    virInterfacePtr ret = NULL;

    testDriverLock(privconn);
    ifacect = virInterfaceFindByMACString(&privconn->ifaces, mac, &iface, 1);
    testDriverUnlock(privconn);

    if (ifacect == 0) {
3768
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3769 3770 3771 3772
        goto cleanup;
    }

    if (ifacect > 1) {
3773
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3774 3775 3776 3777 3778
        goto cleanup;
    }

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

3779
 cleanup:
L
Laine Stump 已提交
3780 3781 3782 3783 3784
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

3785 3786
static int testInterfaceIsActive(virInterfacePtr iface)
{
3787
    testDriverPtr privconn = iface->conn->privateData;
3788 3789 3790 3791 3792 3793 3794
    virInterfaceObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virInterfaceFindByName(&privconn->ifaces, iface->name);
    testDriverUnlock(privconn);
    if (!obj) {
3795
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
3796 3797 3798 3799
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

3800
 cleanup:
3801 3802 3803 3804 3805
    if (obj)
        virInterfaceObjUnlock(obj);
    return ret;
}

3806
static int testInterfaceChangeBegin(virConnectPtr conn,
E
Eric Blake 已提交
3807
                                    unsigned int flags)
3808
{
3809
    testDriverPtr privconn = conn->privateData;
3810 3811
    int ret = -1;

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

3814 3815
    testDriverLock(privconn);
    if (privconn->transaction_running) {
3816
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3817
                       _("there is another transaction running."));
3818 3819 3820 3821 3822 3823 3824 3825 3826 3827
        goto cleanup;
    }

    privconn->transaction_running = true;

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

    ret = 0;
3828
 cleanup:
3829 3830 3831 3832 3833
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceChangeCommit(virConnectPtr conn,
E
Eric Blake 已提交
3834
                                     unsigned int flags)
3835
{
3836
    testDriverPtr privconn = conn->privateData;
3837 3838
    int ret = -1;

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

3841 3842 3843
    testDriverLock(privconn);

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

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

    ret = 0;

3855
 cleanup:
3856 3857 3858 3859 3860 3861
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
E
Eric Blake 已提交
3862
                                       unsigned int flags)
3863
{
3864
    testDriverPtr privconn = conn->privateData;
3865 3866
    int ret = -1;

E
Eric Blake 已提交
3867 3868
    virCheckFlags(0, -1);

3869 3870 3871
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3872
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3873 3874
                       _("no transaction running, "
                         "nothing to rollback."));
3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887
        goto cleanup;
    }

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

    privconn->transaction_running = false;

    ret = 0;

3888
 cleanup:
3889 3890 3891
    testDriverUnlock(privconn);
    return ret;
}
3892

L
Laine Stump 已提交
3893
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
E
Eric Blake 已提交
3894
                                     unsigned int flags)
L
Laine Stump 已提交
3895
{
3896
    testDriverPtr privconn = iface->conn->privateData;
L
Laine Stump 已提交
3897 3898 3899
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

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

L
Laine Stump 已提交
3902 3903 3904 3905 3906 3907
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);
    testDriverUnlock(privconn);

    if (privinterface == NULL) {
3908
        virReportError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
3909 3910 3911
        goto cleanup;
    }

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

3914
 cleanup:
L
Laine Stump 已提交
3915 3916 3917 3918 3919 3920 3921
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    return ret;
}


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
E
Eric Blake 已提交
3922
                                              unsigned int flags)
L
Laine Stump 已提交
3923
{
3924
    testDriverPtr privconn = conn->privateData;
L
Laine Stump 已提交
3925 3926 3927 3928
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
3929 3930
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3931
    testDriverLock(privconn);
3932
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3933 3934
        goto cleanup;

3935
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3936 3937 3938 3939 3940
        goto cleanup;
    def = NULL;

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

3941
 cleanup:
L
Laine Stump 已提交
3942 3943 3944 3945 3946 3947 3948 3949 3950
    virInterfaceDefFree(def);
    if (iface)
        virInterfaceObjUnlock(iface);
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceUndefine(virInterfacePtr iface)
{
3951
    testDriverPtr privconn = iface->conn->privateData;
L
Laine Stump 已提交
3952 3953 3954 3955 3956 3957 3958 3959
    virInterfaceObjPtr privinterface;
    int ret = -1;

    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3960
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3961 3962 3963 3964 3965 3966 3967
        goto cleanup;
    }

    virInterfaceRemove(&privconn->ifaces,
                       privinterface);
    ret = 0;

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

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

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

L
Laine Stump 已提交
3982 3983 3984 3985 3986
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3987
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3988 3989 3990 3991
        goto cleanup;
    }

    if (privinterface->active != 0) {
3992
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3993 3994 3995 3996 3997 3998
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

3999
 cleanup:
L
Laine Stump 已提交
4000 4001 4002 4003 4004 4005 4006
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}

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

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

L
Laine Stump 已提交
4015 4016 4017 4018 4019
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
4020
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4021 4022 4023 4024
        goto cleanup;
    }

    if (privinterface->active == 0) {
4025
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4026 4027 4028 4029 4030 4031
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

4032
 cleanup:
L
Laine Stump 已提交
4033 4034 4035 4036 4037 4038 4039 4040
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}



C
Cole Robinson 已提交
4041 4042 4043 4044
/*
 * Storage Driver routines
 */

4045

4046 4047
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool)
{
C
Cole Robinson 已提交
4048 4049 4050 4051 4052

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

4053
    return VIR_STRDUP(pool->configFile, "");
C
Cole Robinson 已提交
4054 4055
}

4056

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

4065
    testDriverLock(privconn);
4066
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
4067
    testDriverUnlock(privconn);
4068 4069

    if (pool == NULL) {
4070
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4071
        goto cleanup;
C
Cole Robinson 已提交
4072 4073
    }

4074 4075
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4076

4077
 cleanup:
4078 4079
    if (pool)
        virStoragePoolObjUnlock(pool);
4080
    return ret;
C
Cole Robinson 已提交
4081 4082 4083 4084
}

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

4091
    testDriverLock(privconn);
4092
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
4093
    testDriverUnlock(privconn);
4094 4095

    if (pool == NULL) {
4096
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4097
        goto cleanup;
C
Cole Robinson 已提交
4098 4099
    }

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

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

static virStoragePoolPtr
4110 4111
testStoragePoolLookupByVolume(virStorageVolPtr vol)
{
C
Cole Robinson 已提交
4112 4113 4114 4115
    return testStoragePoolLookupByName(vol->conn, vol->pool);
}

static int
4116 4117
testConnectNumOfStoragePools(virConnectPtr conn)
{
4118
    testDriverPtr privconn = conn->privateData;
4119 4120
    int numActive = 0;
    size_t i;
C
Cole Robinson 已提交
4121

4122
    testDriverLock(privconn);
4123
    for (i = 0; i < privconn->pools.count; i++)
C
Cole Robinson 已提交
4124 4125
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
4126
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4127 4128 4129 4130 4131

    return numActive;
}

static int
4132 4133
testConnectListStoragePools(virConnectPtr conn,
                            char **const names,
4134 4135
                            int nnames)
{
4136
    testDriverPtr privconn = conn->privateData;
4137 4138
    int n = 0;
    size_t i;
C
Cole Robinson 已提交
4139

4140
    testDriverLock(privconn);
C
Cole Robinson 已提交
4141
    memset(names, 0, sizeof(*names)*nnames);
4142
    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
4143
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4144
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
4145
            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
4146
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
4147
            goto error;
4148 4149 4150 4151
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4152 4153 4154

    return n;

4155
 error:
4156
    for (n = 0; n < nnames; n++)
C
Cole Robinson 已提交
4157
        VIR_FREE(names[n]);
4158
    testDriverUnlock(privconn);
4159
    return -1;
C
Cole Robinson 已提交
4160 4161 4162
}

static int
4163 4164
testConnectNumOfDefinedStoragePools(virConnectPtr conn)
{
4165
    testDriverPtr privconn = conn->privateData;
4166 4167
    int numInactive = 0;
    size_t i;
C
Cole Robinson 已提交
4168

4169
    testDriverLock(privconn);
4170
    for (i = 0; i < privconn->pools.count; i++) {
4171
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4172 4173
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
4174 4175 4176
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4177 4178 4179 4180 4181

    return numInactive;
}

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

4190
    testDriverLock(privconn);
C
Cole Robinson 已提交
4191
    memset(names, 0, sizeof(*names)*nnames);
4192
    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
4193
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4194
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
4195
            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
4196
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
4197
            goto error;
4198 4199 4200 4201
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4202 4203 4204

    return n;

4205
 error:
4206
    for (n = 0; n < nnames; n++)
C
Cole Robinson 已提交
4207
        VIR_FREE(names[n]);
4208
    testDriverUnlock(privconn);
4209
    return -1;
C
Cole Robinson 已提交
4210 4211
}

4212
static int
4213 4214 4215
testConnectListAllStoragePools(virConnectPtr conn,
                               virStoragePoolPtr **pools,
                               unsigned int flags)
4216
{
4217
    testDriverPtr privconn = conn->privateData;
4218 4219 4220 4221 4222
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

    testDriverLock(privconn);
4223 4224
    ret = virStoragePoolObjListExport(conn, privconn->pools, pools,
                                      NULL, flags);
4225 4226 4227 4228
    testDriverUnlock(privconn);

    return ret;
}
C
Cole Robinson 已提交
4229

4230 4231
static int testStoragePoolIsActive(virStoragePoolPtr pool)
{
4232
    testDriverPtr privconn = pool->conn->privateData;
4233 4234 4235 4236 4237 4238 4239
    virStoragePoolObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virStoragePoolObjFindByUUID(&privconn->pools, pool->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
4240
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4241 4242 4243 4244
        goto cleanup;
    }
    ret = virStoragePoolObjIsActive(obj);

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

static int testStoragePoolIsPersistent(virStoragePoolPtr pool)
{
4253
    testDriverPtr privconn = pool->conn->privateData;
4254 4255 4256 4257 4258 4259 4260
    virStoragePoolObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virStoragePoolObjFindByUUID(&privconn->pools, pool->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
4261
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4262 4263 4264 4265
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

4266
 cleanup:
4267 4268 4269 4270 4271 4272 4273
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}



C
Cole Robinson 已提交
4274
static int
4275 4276
testStoragePoolCreate(virStoragePoolPtr pool,
                      unsigned int flags)
E
Eric Blake 已提交
4277
{
4278
    testDriverPtr privconn = pool->conn->privateData;
4279
    virStoragePoolObjPtr privpool;
4280
    int ret = -1;
4281
    virObjectEventPtr event = NULL;
4282

E
Eric Blake 已提交
4283 4284
    virCheckFlags(0, -1);

4285
    testDriverLock(privconn);
4286 4287
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4288
    testDriverUnlock(privconn);
4289 4290

    if (privpool == NULL) {
4291
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4292
        goto cleanup;
4293 4294
    }

4295
    if (virStoragePoolObjIsActive(privpool)) {
4296 4297
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4298 4299
        goto cleanup;
    }
C
Cole Robinson 已提交
4300 4301

    privpool->active = 1;
4302 4303 4304 4305

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

4308
 cleanup:
4309
    testObjectEventQueue(privconn, event);
4310 4311
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4312
    return ret;
C
Cole Robinson 已提交
4313 4314 4315
}

static char *
4316 4317 4318 4319
testConnectFindStoragePoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type,
                                  const char *srcSpec,
                                  unsigned int flags)
C
Cole Robinson 已提交
4320
{
4321 4322 4323 4324
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4325 4326
    virCheckFlags(0, NULL);

4327 4328
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4329 4330
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
4331 4332 4333 4334
        goto cleanup;
    }

    if (srcSpec) {
4335
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4336 4337 4338 4339 4340 4341 4342
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
4343
        ignore_value(VIR_STRDUP(ret, defaultPoolSourcesLogicalXML));
4344 4345 4346
        break;

    case VIR_STORAGE_POOL_NETFS:
4347
        if (!source || !source->hosts[0].name) {
4348 4349
            virReportError(VIR_ERR_INVALID_ARG,
                           "%s", _("hostname must be specified for netfs sources"));
4350 4351 4352
            goto cleanup;
        }

4353 4354
        ignore_value(virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                                 source->hosts[0].name));
4355 4356 4357
        break;

    default:
4358 4359
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4360 4361
    }

4362
 cleanup:
4363 4364
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4365 4366 4367 4368
}


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

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

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

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

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

4398
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4399
        virStoragePoolObjRemove(&privconn->pools, pool);
4400 4401
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4402 4403 4404
    }
    pool->active = 1;

4405 4406 4407 4408
    event = virStoragePoolEventLifecycleNew(pool->def->name, pool->def->uuid,
                                            VIR_STORAGE_POOL_EVENT_STARTED,
                                            0);

4409 4410
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4411

4412
 cleanup:
4413
    virStoragePoolDefFree(def);
4414
    testObjectEventQueue(privconn, event);
4415 4416 4417
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4418
    return ret;
C
Cole Robinson 已提交
4419 4420 4421
}

static virStoragePoolPtr
4422 4423 4424
testStoragePoolDefineXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4425
{
4426
    testDriverPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4427
    virStoragePoolDefPtr def;
4428
    virStoragePoolObjPtr pool = NULL;
4429
    virStoragePoolPtr ret = NULL;
4430
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
4431

E
Eric Blake 已提交
4432 4433
    virCheckFlags(0, NULL);

4434
    testDriverLock(privconn);
4435
    if (!(def = virStoragePoolDefParseString(xml)))
4436
        goto cleanup;
C
Cole Robinson 已提交
4437 4438 4439 4440 4441

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

4442
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4443 4444
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4445

4446 4447 4448 4449
    event = virStoragePoolEventLifecycleNew(pool->def->name, pool->def->uuid,
                                            VIR_STORAGE_POOL_EVENT_DEFINED,
                                            0);

4450
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4451
        virStoragePoolObjRemove(&privconn->pools, pool);
4452 4453
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4454 4455
    }

4456 4457
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4458

4459
 cleanup:
4460
    virStoragePoolDefFree(def);
4461
    testObjectEventQueue(privconn, event);
4462 4463 4464
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4465
    return ret;
C
Cole Robinson 已提交
4466 4467 4468
}

static int
4469 4470
testStoragePoolUndefine(virStoragePoolPtr pool)
{
4471
    testDriverPtr privconn = pool->conn->privateData;
4472
    virStoragePoolObjPtr privpool;
4473
    int ret = -1;
4474
    virObjectEventPtr event = NULL;
4475

4476
    testDriverLock(privconn);
4477 4478 4479 4480
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4481
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4482
        goto cleanup;
4483 4484
    }

4485
    if (virStoragePoolObjIsActive(privpool)) {
4486 4487
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4488 4489
        goto cleanup;
    }
C
Cole Robinson 已提交
4490

4491 4492 4493 4494
    event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid,
                                            VIR_STORAGE_POOL_EVENT_UNDEFINED,
                                            0);

C
Cole Robinson 已提交
4495
    virStoragePoolObjRemove(&privconn->pools, privpool);
4496
    privpool = NULL;
4497
    ret = 0;
C
Cole Robinson 已提交
4498

4499
 cleanup:
4500 4501
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4502
    testObjectEventQueue(privconn, event);
4503
    testDriverUnlock(privconn);
4504
    return ret;
C
Cole Robinson 已提交
4505 4506 4507
}

static int
4508
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4509 4510
                     unsigned int flags)
{
4511
    testDriverPtr privconn = pool->conn->privateData;
4512
    virStoragePoolObjPtr privpool;
4513
    int ret = -1;
4514

E
Eric Blake 已提交
4515 4516
    virCheckFlags(0, -1);

4517
    testDriverLock(privconn);
4518 4519
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4520
    testDriverUnlock(privconn);
4521 4522

    if (privpool == NULL) {
4523
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4524
        goto cleanup;
4525 4526
    }

4527
    if (virStoragePoolObjIsActive(privpool)) {
4528 4529
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4530 4531
        goto cleanup;
    }
4532
    ret = 0;
C
Cole Robinson 已提交
4533

4534
 cleanup:
4535 4536
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4537
    return ret;
C
Cole Robinson 已提交
4538 4539 4540 4541
}


static int
4542 4543
testStoragePoolDestroy(virStoragePoolPtr pool)
{
4544
    testDriverPtr privconn = pool->conn->privateData;
4545
    virStoragePoolObjPtr privpool;
4546
    int ret = -1;
4547
    virObjectEventPtr event = NULL;
4548

4549
    testDriverLock(privconn);
4550 4551 4552 4553
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4554
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4555
        goto cleanup;
4556 4557 4558
    }

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

    privpool->active = 0;
4565 4566 4567
    event = virStoragePoolEventLifecycleNew(privpool->def->name, privpool->def->uuid,
                                            VIR_STORAGE_POOL_EVENT_STOPPED,
                                            0);
C
Cole Robinson 已提交
4568

4569
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4570
        virStoragePoolObjRemove(&privconn->pools, privpool);
4571 4572
        privpool = NULL;
    }
4573
    ret = 0;
C
Cole Robinson 已提交
4574

4575
 cleanup:
4576
    testObjectEventQueue(privconn, event);
4577 4578 4579
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4580
    return ret;
C
Cole Robinson 已提交
4581 4582 4583 4584
}


static int
4585
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4586 4587
                      unsigned int flags)
{
4588
    testDriverPtr privconn = pool->conn->privateData;
4589
    virStoragePoolObjPtr privpool;
4590
    int ret = -1;
4591

E
Eric Blake 已提交
4592 4593
    virCheckFlags(0, -1);

4594
    testDriverLock(privconn);
4595 4596
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4597
    testDriverUnlock(privconn);
4598 4599

    if (privpool == NULL) {
4600
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4601 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
    testDriverLock(privconn);
4631 4632
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4633
    testDriverUnlock(privconn);
4634 4635

    if (privpool == NULL) {
4636
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4637
        goto cleanup;
4638 4639 4640
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4641 4642
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4643
        goto cleanup;
4644
    }
4645

4646
    event = virStoragePoolEventRefreshNew(pool->name, pool->uuid);
4647
    ret = 0;
C
Cole Robinson 已提交
4648

4649
 cleanup:
4650
    testObjectEventQueue(privconn, event);
4651 4652
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4653
    return ret;
C
Cole Robinson 已提交
4654 4655 4656 4657
}


static int
4658
testStoragePoolGetInfo(virStoragePoolPtr pool,
4659 4660
                       virStoragePoolInfoPtr info)
{
4661
    testDriverPtr privconn = pool->conn->privateData;
4662
    virStoragePoolObjPtr privpool;
4663
    int ret = -1;
4664

4665
    testDriverLock(privconn);
4666 4667
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4668
    testDriverUnlock(privconn);
4669 4670

    if (privpool == NULL) {
4671
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4672
        goto cleanup;
4673
    }
C
Cole Robinson 已提交
4674 4675 4676 4677 4678 4679 4680 4681 4682

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

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

static char *
4692
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4693 4694
                          unsigned int flags)
{
4695
    testDriverPtr privconn = pool->conn->privateData;
4696
    virStoragePoolObjPtr privpool;
4697
    char *ret = NULL;
4698

E
Eric Blake 已提交
4699 4700
    virCheckFlags(0, NULL);

4701
    testDriverLock(privconn);
4702 4703
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4704
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4705

4706
    if (privpool == NULL) {
4707
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4708
        goto cleanup;
4709 4710
    }

4711
    ret = virStoragePoolDefFormat(privpool->def);
4712

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

static int
4720
testStoragePoolGetAutostart(virStoragePoolPtr pool,
4721 4722
                            int *autostart)
{
4723
    testDriverPtr privconn = pool->conn->privateData;
4724
    virStoragePoolObjPtr privpool;
4725
    int ret = -1;
4726

4727
    testDriverLock(privconn);
4728 4729
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4730
    testDriverUnlock(privconn);
4731 4732

    if (privpool == NULL) {
4733
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4734
        goto cleanup;
4735
    }
C
Cole Robinson 已提交
4736 4737 4738 4739 4740 4741

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

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

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

4758
    testDriverLock(privconn);
4759 4760
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4761
    testDriverUnlock(privconn);
4762 4763

    if (privpool == NULL) {
4764
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4765
        goto cleanup;
4766
    }
C
Cole Robinson 已提交
4767 4768

    if (!privpool->configFile) {
4769 4770
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
4771
        goto cleanup;
C
Cole Robinson 已提交
4772 4773 4774 4775
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4776 4777
    ret = 0;

4778
 cleanup:
4779 4780
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4781
    return ret;
C
Cole Robinson 已提交
4782 4783 4784 4785
}


static int
4786 4787
testStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
4788
    testDriverPtr privconn = pool->conn->privateData;
4789
    virStoragePoolObjPtr privpool;
4790
    int ret = -1;
4791

4792
    testDriverLock(privconn);
4793 4794
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4795
    testDriverUnlock(privconn);
4796 4797

    if (privpool == NULL) {
4798
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4799
        goto cleanup;
4800 4801 4802
    }

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

4808 4809
    ret = privpool->volumes.count;

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

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

4826
    memset(names, 0, maxnames * sizeof(*names));
4827 4828

    testDriverLock(privconn);
4829 4830
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4831
    testDriverUnlock(privconn);
4832 4833

    if (privpool == NULL) {
4834
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4835
        goto cleanup;
4836 4837 4838 4839
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4840 4841
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4842
        goto cleanup;
4843 4844
    }

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

4850
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4851 4852 4853
    return n;

 cleanup:
4854
    for (n = 0; n < maxnames; n++)
C
Cole Robinson 已提交
4855 4856
        VIR_FREE(names[i]);

4857
    memset(names, 0, maxnames * sizeof(*names));
4858 4859
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4860 4861 4862
    return -1;
}

4863 4864 4865
static int
testStoragePoolListAllVolumes(virStoragePoolPtr obj,
                              virStorageVolPtr **vols,
4866 4867
                              unsigned int flags)
{
4868
    testDriverPtr privconn = obj->conn->privateData;
4869
    virStoragePoolObjPtr pool;
4870
    size_t i;
4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899
    virStorageVolPtr *tmp_vols = NULL;
    virStorageVolPtr vol = NULL;
    int nvols = 0;
    int ret = -1;

    virCheckFlags(0, -1);

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

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

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

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

4900
    if (VIR_ALLOC_N(tmp_vols, pool->volumes.count + 1) < 0)
4901 4902
         goto cleanup;

4903
    for (i = 0; i < pool->volumes.count; i++) {
4904 4905
        if (!(vol = virGetStorageVol(obj->conn, pool->def->name,
                                     pool->volumes.objs[i]->name,
4906 4907
                                     pool->volumes.objs[i]->key,
                                     NULL, NULL)))
4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921
            goto cleanup;
        tmp_vols[nvols++] = vol;
    }

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

 cleanup:
    if (tmp_vols) {
        for (i = 0; i < nvols; i++) {
            if (tmp_vols[i])
                virStorageVolFree(tmp_vols[i]);
        }
4922
        VIR_FREE(tmp_vols);
4923 4924 4925 4926 4927 4928 4929
    }

    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}
C
Cole Robinson 已提交
4930 4931

static virStorageVolPtr
4932
testStorageVolLookupByName(virStoragePoolPtr pool,
4933 4934
                           const char *name ATTRIBUTE_UNUSED)
{
4935
    testDriverPtr privconn = pool->conn->privateData;
4936 4937
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4938
    virStorageVolPtr ret = NULL;
4939

4940
    testDriverLock(privconn);
4941 4942
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4943
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4944

4945
    if (privpool == NULL) {
4946
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4947
        goto cleanup;
4948 4949 4950 4951
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4952 4953
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4954
        goto cleanup;
4955 4956 4957 4958 4959
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4960 4961
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"), name);
4962
        goto cleanup;
C
Cole Robinson 已提交
4963 4964
    }

4965
    ret = virGetStorageVol(pool->conn, privpool->def->name,
4966 4967
                           privvol->name, privvol->key,
                           NULL, NULL);
4968

4969
 cleanup:
4970 4971
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4972
    return ret;
C
Cole Robinson 已提交
4973 4974 4975 4976
}


static virStorageVolPtr
4977
testStorageVolLookupByKey(virConnectPtr conn,
4978 4979
                          const char *key)
{
4980
    testDriverPtr privconn = conn->privateData;
4981
    size_t i;
4982
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4983

4984
    testDriverLock(privconn);
4985
    for (i = 0; i < privconn->pools.count; i++) {
4986
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4987
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4988
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4989 4990
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4991 4992 4993 4994
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
4995 4996
                                       privvol->key,
                                       NULL, NULL);
4997
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4998 4999
                break;
            }
C
Cole Robinson 已提交
5000
        }
5001
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5002
    }
5003
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5004

5005
    if (!ret)
5006 5007
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
5008 5009

    return ret;
C
Cole Robinson 已提交
5010 5011 5012
}

static virStorageVolPtr
5013
testStorageVolLookupByPath(virConnectPtr conn,
5014 5015
                           const char *path)
{
5016
    testDriverPtr privconn = conn->privateData;
5017
    size_t i;
5018
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
5019

5020
    testDriverLock(privconn);
5021
    for (i = 0; i < privconn->pools.count; i++) {
5022
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5023
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
5024
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
5025 5026
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

5027 5028 5029 5030
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
5031 5032
                                       privvol->key,
                                       NULL, NULL);
5033
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
5034 5035
                break;
            }
C
Cole Robinson 已提交
5036
        }
5037
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5038
    }
5039
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5040

5041
    if (!ret)
5042 5043
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
5044 5045

    return ret;
C
Cole Robinson 已提交
5046 5047 5048
}

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

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

5060
    testDriverLock(privconn);
5061 5062
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5063
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5064

5065
    if (privpool == NULL) {
5066
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5067
        goto cleanup;
5068 5069 5070
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5071 5072
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5073
        goto cleanup;
5074
    }
C
Cole Robinson 已提交
5075

5076
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5077
    if (privvol == NULL)
5078
        goto cleanup;
5079 5080

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5081 5082
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5083
        goto cleanup;
C
Cole Robinson 已提交
5084 5085 5086
    }

    /* Make sure enough space */
5087
    if ((privpool->def->allocation + privvol->target.allocation) >
C
Cole Robinson 已提交
5088
         privpool->def->capacity) {
5089 5090 5091
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5092
        goto cleanup;
C
Cole Robinson 已提交
5093 5094
    }

5095 5096
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5097
                    privvol->name) == -1)
5098
        goto cleanup;
C
Cole Robinson 已提交
5099

5100 5101 5102
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
5103
        goto cleanup;
C
Cole Robinson 已提交
5104

5105
    privpool->def->allocation += privvol->target.allocation;
C
Cole Robinson 已提交
5106 5107 5108
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5109
    ret = virGetStorageVol(pool->conn, privpool->def->name,
5110 5111
                           privvol->name, privvol->key,
                           NULL, NULL);
5112
    privvol = NULL;
5113

5114
 cleanup:
5115
    virStorageVolDefFree(privvol);
5116 5117
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5118
    return ret;
C
Cole Robinson 已提交
5119 5120
}

5121
static virStorageVolPtr
5122 5123 5124 5125
testStorageVolCreateXMLFrom(virStoragePoolPtr pool,
                            const char *xmldesc,
                            virStorageVolPtr clonevol,
                            unsigned int flags)
E
Eric Blake 已提交
5126
{
5127
    testDriverPtr privconn = pool->conn->privateData;
5128 5129 5130 5131
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
5132 5133
    virCheckFlags(0, NULL);

5134 5135 5136 5137 5138 5139
    testDriverLock(privconn);
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
    testDriverUnlock(privconn);

    if (privpool == NULL) {
5140
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5141 5142 5143 5144
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5145 5146
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5147 5148 5149
        goto cleanup;
    }

5150
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5151 5152 5153 5154
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5155 5156
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5157 5158 5159 5160 5161
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
5162 5163 5164
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
5165 5166 5167 5168
        goto cleanup;
    }

    /* Make sure enough space */
5169
    if ((privpool->def->allocation + privvol->target.allocation) >
5170
         privpool->def->capacity) {
5171 5172 5173
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5174 5175 5176 5177 5178
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5179 5180
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5181
                    privvol->name) == -1)
5182 5183
        goto cleanup;

5184 5185 5186
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
5187 5188
        goto cleanup;

5189
    privpool->def->allocation += privvol->target.allocation;
5190 5191 5192 5193
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    ret = virGetStorageVol(pool->conn, privpool->def->name,
5194 5195
                           privvol->name, privvol->key,
                           NULL, NULL);
5196 5197
    privvol = NULL;

5198
 cleanup:
5199 5200 5201 5202 5203 5204
    virStorageVolDefFree(privvol);
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    return ret;
}

C
Cole Robinson 已提交
5205
static int
5206 5207
testStorageVolDelete(virStorageVolPtr vol,
                     unsigned int flags)
E
Eric Blake 已提交
5208
{
5209
    testDriverPtr privconn = vol->conn->privateData;
5210 5211
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5212
    size_t i;
5213
    int ret = -1;
C
Cole Robinson 已提交
5214

E
Eric Blake 已提交
5215 5216
    virCheckFlags(0, -1);

5217
    testDriverLock(privconn);
5218 5219
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5220
    testDriverUnlock(privconn);
5221 5222

    if (privpool == NULL) {
5223
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5224
        goto cleanup;
5225 5226 5227 5228 5229 5230
    }


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

    if (privvol == NULL) {
5231 5232 5233
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5234
        goto cleanup;
5235 5236 5237
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5238 5239
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5240
        goto cleanup;
5241 5242 5243
    }


5244
    privpool->def->allocation -= privvol->target.allocation;
C
Cole Robinson 已提交
5245 5246 5247
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5248
    for (i = 0; i < privpool->volumes.count; i++) {
C
Cole Robinson 已提交
5249 5250 5251
        if (privpool->volumes.objs[i] == privvol) {
            virStorageVolDefFree(privvol);

5252
            VIR_DELETE_ELEMENT(privpool->volumes.objs, i, privpool->volumes.count);
C
Cole Robinson 已提交
5253 5254 5255
            break;
        }
    }
5256
    ret = 0;
C
Cole Robinson 已提交
5257

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


5265 5266
static int testStorageVolumeTypeForPool(int pooltype)
{
C
Cole Robinson 已提交
5267

5268
    switch (pooltype) {
C
Cole Robinson 已提交
5269 5270 5271 5272 5273 5274 5275 5276 5277 5278
        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
5279
testStorageVolGetInfo(virStorageVolPtr vol,
5280 5281
                      virStorageVolInfoPtr info)
{
5282
    testDriverPtr privconn = vol->conn->privateData;
5283 5284
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5285
    int ret = -1;
5286

5287
    testDriverLock(privconn);
5288 5289
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5290
    testDriverUnlock(privconn);
5291 5292

    if (privpool == NULL) {
5293
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5294
        goto cleanup;
5295 5296 5297 5298 5299
    }

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

    if (privvol == NULL) {
5300 5301 5302
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5303
        goto cleanup;
5304 5305 5306
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5307 5308
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5309
        goto cleanup;
5310
    }
C
Cole Robinson 已提交
5311 5312 5313

    memset(info, 0, sizeof(*info));
    info->type = testStorageVolumeTypeForPool(privpool->def->type);
5314 5315
    info->capacity = privvol->target.capacity;
    info->allocation = privvol->target.allocation;
5316
    ret = 0;
C
Cole Robinson 已提交
5317

5318
 cleanup:
5319 5320
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5321
    return ret;
C
Cole Robinson 已提交
5322 5323 5324
}

static char *
5325 5326
testStorageVolGetXMLDesc(virStorageVolPtr vol,
                         unsigned int flags)
E
Eric Blake 已提交
5327
{
5328
    testDriverPtr privconn = vol->conn->privateData;
5329 5330
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5331
    char *ret = NULL;
5332

E
Eric Blake 已提交
5333 5334
    virCheckFlags(0, NULL);

5335
    testDriverLock(privconn);
5336 5337
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5338
    testDriverUnlock(privconn);
5339 5340

    if (privpool == NULL) {
5341
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5342
        goto cleanup;
5343 5344 5345 5346 5347
    }

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

    if (privvol == NULL) {
5348 5349 5350
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5351
        goto cleanup;
5352
    }
C
Cole Robinson 已提交
5353

5354
    if (!virStoragePoolObjIsActive(privpool)) {
5355 5356
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5357
        goto cleanup;
5358 5359
    }

5360
    ret = virStorageVolDefFormat(privpool->def, privvol);
5361

5362
 cleanup:
5363 5364
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5365
    return ret;
C
Cole Robinson 已提交
5366 5367 5368
}

static char *
5369 5370
testStorageVolGetPath(virStorageVolPtr vol)
{
5371
    testDriverPtr privconn = vol->conn->privateData;
5372 5373
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5374
    char *ret = NULL;
5375

5376
    testDriverLock(privconn);
5377 5378
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5379
    testDriverUnlock(privconn);
5380 5381

    if (privpool == NULL) {
5382
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5383
        goto cleanup;
5384 5385 5386 5387 5388
    }

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

    if (privvol == NULL) {
5389 5390 5391
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5392
        goto cleanup;
5393 5394 5395
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5396 5397
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5398
        goto cleanup;
5399 5400
    }

5401
    ignore_value(VIR_STRDUP(ret, privvol->target.path));
5402

5403
 cleanup:
5404 5405
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5406 5407 5408
    return ret;
}

5409

5410
/* Node device implementations */
5411

5412 5413 5414
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5415
                     unsigned int flags)
5416
{
5417
    testDriverPtr driver = conn->privateData;
5418
    int ndevs = 0;
5419
    size_t i;
5420

E
Eric Blake 已提交
5421 5422
    virCheckFlags(0, -1);

5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437
    testDriverLock(driver);
    for (i = 0; i < driver->devs.count; i++)
        if ((cap == NULL) ||
            virNodeDeviceHasCap(driver->devs.objs[i], cap))
            ++ndevs;
    testDriverUnlock(driver);

    return ndevs;
}

static int
testNodeListDevices(virConnectPtr conn,
                    const char *cap,
                    char **const names,
                    int maxnames,
E
Eric Blake 已提交
5438
                    unsigned int flags)
5439
{
5440
    testDriverPtr driver = conn->privateData;
5441
    int ndevs = 0;
5442
    size_t i;
5443

E
Eric Blake 已提交
5444 5445
    virCheckFlags(0, -1);

5446 5447 5448 5449 5450
    testDriverLock(driver);
    for (i = 0; i < driver->devs.count && ndevs < maxnames; i++) {
        virNodeDeviceObjLock(driver->devs.objs[i]);
        if (cap == NULL ||
            virNodeDeviceHasCap(driver->devs.objs[i], cap)) {
5451
            if (VIR_STRDUP(names[ndevs++], driver->devs.objs[i]->def->name) < 0) {
5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472
                virNodeDeviceObjUnlock(driver->devs.objs[i]);
                goto failure;
            }
        }
        virNodeDeviceObjUnlock(driver->devs.objs[i]);
    }
    testDriverUnlock(driver);

    return ndevs;

 failure:
    testDriverUnlock(driver);
    --ndevs;
    while (--ndevs >= 0)
        VIR_FREE(names[ndevs]);
    return -1;
}

static virNodeDevicePtr
testNodeDeviceLookupByName(virConnectPtr conn, const char *name)
{
5473
    testDriverPtr driver = conn->privateData;
5474 5475 5476 5477 5478 5479 5480 5481
    virNodeDeviceObjPtr obj;
    virNodeDevicePtr ret = NULL;

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

    if (!obj) {
5482 5483 5484
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       name);
5485 5486 5487
        goto cleanup;
    }

5488 5489 5490 5491
    if ((ret = virGetNodeDevice(conn, name))) {
        if (VIR_STRDUP(ret->parent, obj->def->parent) < 0)
            virObjectUnref(ret);
    }
5492

5493
 cleanup:
5494 5495 5496 5497 5498 5499
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
5500
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5501
                         unsigned int flags)
5502
{
5503
    testDriverPtr driver = dev->conn->privateData;
5504 5505 5506
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5507 5508
    virCheckFlags(0, NULL);

5509 5510 5511 5512 5513
    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
5514 5515 5516
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5517 5518 5519
        goto cleanup;
    }

5520
    ret = virNodeDeviceDefFormat(obj->def);
5521

5522
 cleanup:
5523 5524 5525 5526 5527 5528 5529 5530
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
testNodeDeviceGetParent(virNodeDevicePtr dev)
{
5531
    testDriverPtr driver = dev->conn->privateData;
5532 5533 5534 5535 5536 5537 5538 5539
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

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

    if (!obj) {
5540 5541 5542
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5543 5544 5545 5546
        goto cleanup;
    }

    if (obj->def->parent) {
5547
        ignore_value(VIR_STRDUP(ret, obj->def->parent));
5548
    } else {
5549 5550
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5551 5552
    }

5553
 cleanup:
5554 5555 5556 5557 5558
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

5559

5560 5561 5562
static int
testNodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
5563
    testDriverPtr driver = dev->conn->privateData;
5564 5565 5566 5567 5568 5569 5570 5571 5572 5573
    virNodeDeviceObjPtr obj;
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

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

    if (!obj) {
5574 5575 5576
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5577 5578 5579 5580 5581 5582 5583
        goto cleanup;
    }

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

5584
 cleanup:
5585 5586 5587 5588 5589 5590 5591 5592 5593
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}


static int
testNodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
5594
    testDriverPtr driver = dev->conn->privateData;
5595 5596 5597 5598 5599 5600 5601 5602 5603 5604
    virNodeDeviceObjPtr obj;
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

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

    if (!obj) {
5605 5606 5607
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5608 5609 5610 5611
        goto cleanup;
    }

    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
5612
        if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->data.type)) < 0)
5613 5614 5615 5616
            goto cleanup;
    }
    ret = ncaps;

5617
 cleanup:
5618 5619 5620 5621 5622 5623 5624 5625 5626 5627
    if (obj)
        virNodeDeviceObjUnlock(obj);
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
}

5628

5629
static virNodeDeviceDefPtr
5630
testNodeDeviceMockCreateVport(virConnectPtr conn,
5631
                              const char *wwnn,
5632
                              const char *wwpn)
5633
{
5634
    testDriverPtr driver = conn->privateData;
5635 5636 5637
    virNodeDevicePtr devcpy = NULL;
    char *xml = NULL;
    virNodeDeviceDefPtr def = NULL;
5638
    virNodeDevCapsDefPtr caps;
5639
    virNodeDeviceObjPtr obj = NULL;
5640
    virObjectEventPtr event = NULL;
5641

5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655
    /* 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. */
    if (!(devcpy = virNodeDeviceLookupByName(conn, "scsi_host11")) ||
        !(xml = virNodeDeviceGetXMLDesc(devcpy, 0)) ||
        !(def = virNodeDeviceDefParseString(xml, EXISTING_DEVICE, NULL)))
        goto cleanup;

5656
    VIR_FREE(def->name);
5657
    if (VIR_STRDUP(def->name, "scsi_host12") < 0)
5658 5659
        goto cleanup;

5660 5661 5662
    /* 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. */
5663 5664
    caps = def->caps;
    while (caps) {
5665
        if (caps->data.type != VIR_NODE_DEV_CAP_SCSI_HOST)
5666 5667
            continue;

5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681
        /* 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++;
        }
5682 5683 5684
        caps = caps->next;
    }

5685
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def)))
5686 5687 5688
        goto cleanup;
    virNodeDeviceObjUnlock(obj);

5689 5690 5691
    event = virNodeDeviceEventLifecycleNew(def->name,
                                           VIR_NODE_DEVICE_EVENT_CREATED,
                                           0);
5692 5693 5694
    testObjectEventQueue(driver, event);

 cleanup:
5695 5696 5697 5698 5699 5700 5701 5702 5703
    VIR_FREE(xml);
    if (!obj) {
        virNodeDeviceDefFree(def);
        def = NULL;
    }
    if (devcpy)
        virNodeDeviceFree(devcpy);

    return def;
5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716
}


static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
                        unsigned int flags)
{
    testDriverPtr driver = conn->privateData;
    virNodeDeviceDefPtr def = NULL;
    char *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;
    virNodeDevicePtr dev = NULL;
5717
    virNodeDeviceDefPtr newdef = NULL;
5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738

    virCheckFlags(0, NULL);

    testDriverLock(driver);

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

    /* We run these next two simply for validation - they are essentially
     * 'validating' that the input XML either has a wwnn/wwpn or the
     * virNodeDevCapSCSIHostParseXML generated a wwnn/wwpn and that the
     * input XML has a parent host defined. */
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) < 0)
        goto cleanup;

    if (virNodeDeviceGetParentHost(&driver->devs, def->name,
                                   def->parent, &parent_host) < 0)
        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
5739 5740 5741 5742 5743 5744 5745 5746 5747 5748
     * 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 */
    testDriverUnlock(driver);
    if ((newdef = testNodeDeviceMockCreateVport(conn, wwnn, wwpn))) {
        if ((dev = virNodeDeviceLookupByName(conn, newdef->name)))
            ignore_value(VIR_STRDUP(dev->parent, def->parent));
    }
    testDriverLock(driver);
    newdef = NULL;
5749

5750
 cleanup:
5751
    testDriverUnlock(driver);
5752
    virNodeDeviceDefFree(def);
5753 5754 5755 5756 5757 5758 5759 5760 5761
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return dev;
}

static int
testNodeDeviceDestroy(virNodeDevicePtr dev)
{
    int ret = 0;
5762
    testDriverPtr driver = dev->conn->privateData;
5763 5764 5765
    virNodeDeviceObjPtr obj = NULL;
    char *parent_name = NULL, *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;
5766
    virObjectEventPtr event = NULL;
5767 5768 5769 5770 5771 5772

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

    if (!obj) {
5773 5774 5775
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5776 5777 5778
        goto out;
    }

5779
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1)
5780 5781
        goto out;

5782
    if (VIR_STRDUP(parent_name, obj->def->parent) < 0)
5783 5784 5785 5786 5787 5788 5789 5790 5791
        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);

    /* We do this just for basic validation */
5792
    if (virNodeDeviceGetParentHost(&driver->devs,
5793 5794 5795 5796 5797 5798 5799
                                   dev->name,
                                   parent_name,
                                   &parent_host) == -1) {
        obj = NULL;
        goto out;
    }

5800 5801 5802 5803
    event = virNodeDeviceEventLifecycleNew(dev->name,
                                           VIR_NODE_DEVICE_EVENT_DELETED,
                                           0);

5804
    virNodeDeviceObjLock(obj);
5805
    virNodeDeviceObjRemove(&driver->devs, &obj);
5806

5807
 out:
5808 5809
    if (obj)
        virNodeDeviceObjUnlock(obj);
5810
    testObjectEventQueue(driver, event);
5811 5812 5813 5814 5815 5816
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

5817 5818

/* Domain event implementations */
5819
static int
5820 5821 5822 5823
testConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
5824
{
5825
    testDriverPtr driver = conn->privateData;
5826
    int ret = 0;
5827

5828
    if (virDomainEventStateRegister(conn, driver->eventState,
5829 5830
                                    callback, opaque, freecb) < 0)
        ret = -1;
5831 5832 5833 5834

    return ret;
}

5835

5836
static int
5837 5838
testConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
5839
{
5840
    testDriverPtr driver = conn->privateData;
5841
    int ret = 0;
5842

5843
    if (virDomainEventStateDeregister(conn, driver->eventState,
5844 5845
                                      callback) < 0)
        ret = -1;
5846 5847 5848 5849

    return ret;
}

5850 5851

static int
5852 5853 5854 5855 5856 5857
testConnectDomainEventRegisterAny(virConnectPtr conn,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback callback,
                                  void *opaque,
                                  virFreeCallback freecb)
5858
{
5859
    testDriverPtr driver = conn->privateData;
5860 5861
    int ret;

5862
    if (virDomainEventStateRegisterID(conn, driver->eventState,
5863 5864
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
5865
        ret = -1;
5866 5867 5868 5869 5870

    return ret;
}

static int
5871 5872
testConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
5873
{
5874
    testDriverPtr driver = conn->privateData;
5875
    int ret = 0;
5876

5877
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5878 5879
                                        callbackID) < 0)
        ret = -1;
5880 5881 5882 5883 5884

    return ret;
}


5885 5886 5887 5888 5889 5890 5891 5892
static int
testConnectNetworkEventRegisterAny(virConnectPtr conn,
                                   virNetworkPtr net,
                                   int eventID,
                                   virConnectNetworkEventGenericCallback callback,
                                   void *opaque,
                                   virFreeCallback freecb)
{
5893
    testDriverPtr driver = conn->privateData;
5894 5895
    int ret;

5896
    if (virNetworkEventStateRegisterID(conn, driver->eventState,
5897
                                       net, eventID, callback,
5898 5899 5900 5901 5902 5903 5904 5905 5906 5907
                                       opaque, freecb, &ret) < 0)
        ret = -1;

    return ret;
}

static int
testConnectNetworkEventDeregisterAny(virConnectPtr conn,
                                     int callbackID)
{
5908
    testDriverPtr driver = conn->privateData;
5909
    int ret = 0;
5910

5911
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
5912 5913
                                        callbackID) < 0)
        ret = -1;
5914 5915 5916 5917

    return ret;
}

5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950
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;
}

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

5984 5985 5986
static int testConnectListAllDomains(virConnectPtr conn,
                                     virDomainPtr **domains,
                                     unsigned int flags)
5987
{
5988
    testDriverPtr privconn = conn->privateData;
5989

O
Osier Yang 已提交
5990
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
5991

5992 5993
    return virDomainObjListExport(privconn->domains, conn, domains,
                                  NULL, flags);
5994 5995
}

5996
static int
P
Peter Krempa 已提交
5997
testNodeGetCPUMap(virConnectPtr conn ATTRIBUTE_UNUSED,
5998 5999 6000 6001 6002 6003 6004
                  unsigned char **cpumap,
                  unsigned int *online,
                  unsigned int flags)
{
    virCheckFlags(0, -1);

    if (cpumap) {
6005
        if (VIR_ALLOC_N(*cpumap, 1) < 0)
P
Peter Krempa 已提交
6006
            return -1;
6007 6008 6009 6010 6011 6012
        *cpumap[0] = 0x15;
    }

    if (online)
        *online = 3;

P
Peter Krempa 已提交
6013
    return  8;
6014 6015
}

6016 6017 6018 6019 6020 6021 6022 6023 6024 6025
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

6026
    if (VIR_STRDUP(ret, "image/png") < 0)
6027 6028
        return NULL;

D
Daniel P. Berrange 已提交
6029
    if (virFDStreamOpenFile(st, PKGDATADIR "/test-screenshot.png", 0, 0, O_RDONLY) < 0)
6030 6031 6032 6033 6034
        VIR_FREE(ret);

    return ret;
}

6035 6036
static int
testConnectGetCPUModelNames(virConnectPtr conn ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
6037
                            const char *archName,
6038 6039 6040
                            char ***models,
                            unsigned int flags)
{
J
Jiri Denemark 已提交
6041 6042
    virArch arch;

6043
    virCheckFlags(0, -1);
J
Jiri Denemark 已提交
6044 6045 6046 6047 6048 6049 6050 6051

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

J
Jiri Denemark 已提交
6052
    return virCPUGetModels(arch, models);
6053
}
6054

C
Cole Robinson 已提交
6055 6056 6057
static int
testDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
6058
    testDriverPtr privconn = dom->conn->privateData;
C
Cole Robinson 已提交
6059
    virDomainObjPtr vm = NULL;
6060
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
6061 6062 6063 6064 6065 6066
    int ret = -1;

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

6067 6068
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082

    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);
6083
    event = virDomainEventLifecycleNewFromObj(vm,
C
Cole Robinson 已提交
6084 6085 6086 6087 6088
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
    vm->hasManagedSave = true;

    ret = 0;
6089
 cleanup:
6090
    virDomainObjEndAPI(&vm);
6091
    testObjectEventQueue(privconn, event);
C
Cole Robinson 已提交
6092 6093 6094 6095 6096 6097 6098 6099 6100

    return ret;
}


static int
testDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    virDomainObjPtr vm;
6101
    int ret;
C
Cole Robinson 已提交
6102 6103 6104

    virCheckFlags(0, -1);

6105 6106
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
6107 6108

    ret = vm->hasManagedSave;
6109

6110
    virDomainObjEndAPI(&vm);
C
Cole Robinson 已提交
6111 6112 6113 6114 6115 6116 6117 6118 6119 6120
    return ret;
}

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

    virCheckFlags(0, -1);

6121 6122
    if (!(vm = testDomObjFromDomain(dom)))
        return -1;
C
Cole Robinson 已提交
6123 6124

    vm->hasManagedSave = false;
6125

6126
    virDomainObjEndAPI(&vm);
6127
    return 0;
C
Cole Robinson 已提交
6128 6129 6130
}


6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164
/*
 * 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;
6165
    int n;
6166 6167 6168 6169 6170

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6171
        return -1;
6172 6173 6174

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

6175
    virDomainObjEndAPI(&vm);
6176 6177 6178 6179 6180 6181 6182 6183 6184 6185
    return n;
}

static int
testDomainSnapshotListNames(virDomainPtr domain,
                            char **names,
                            int nameslen,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6186
    int n;
6187 6188 6189 6190 6191

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6192
        return -1;
6193 6194 6195 6196

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

6197
    virDomainObjEndAPI(&vm);
6198 6199 6200 6201 6202 6203 6204 6205 6206
    return n;
}

static int
testDomainListAllSnapshots(virDomainPtr domain,
                           virDomainSnapshotPtr **snaps,
                           unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6207
    int n;
6208 6209 6210 6211 6212

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6213
        return -1;
6214 6215 6216

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

6217
    virDomainObjEndAPI(&vm);
6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234
    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)))
6235
        return -1;
6236 6237 6238 6239 6240 6241 6242

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

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

6243
 cleanup:
6244
    virDomainObjEndAPI(&vm);
6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259
    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)))
6260
        return -1;
6261 6262 6263 6264 6265 6266

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

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

6267
 cleanup:
6268
    virDomainObjEndAPI(&vm);
6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284
    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)))
6285
        return -1;
6286 6287 6288 6289 6290 6291 6292

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

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

6293
 cleanup:
6294
    virDomainObjEndAPI(&vm);
6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309
    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)))
6310
        return NULL;
6311 6312 6313 6314 6315 6316

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

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

6317
 cleanup:
6318
    virDomainObjEndAPI(&vm);
6319 6320 6321 6322 6323 6324 6325 6326
    return snapshot;
}

static int
testDomainHasCurrentSnapshot(virDomainPtr domain,
                             unsigned int flags)
{
    virDomainObjPtr vm;
6327
    int ret;
6328 6329 6330 6331

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromDomain(domain)))
6332
        return -1;
6333 6334 6335

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

6336
    virDomainObjEndAPI(&vm);
6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350
    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)))
6351
        return NULL;
6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364

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

6365
 cleanup:
6366
    virDomainObjEndAPI(&vm);
6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379
    return parent;
}

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

    virCheckFlags(0, NULL);

    if (!(vm = testDomObjFromDomain(domain)))
6380
        return NULL;
6381 6382 6383 6384 6385 6386 6387 6388 6389

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

6390
 cleanup:
6391
    virDomainObjEndAPI(&vm);
6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402
    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];
6403
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6404 6405 6406 6407

    virCheckFlags(VIR_DOMAIN_XML_SECURE, NULL);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6408
        return NULL;
6409 6410 6411 6412 6413 6414

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

    virUUIDFormat(snapshot->domain->uuid, uuidstr);

6415
    xml = virDomainSnapshotDefFormat(uuidstr, snap->def, privconn->caps,
6416 6417
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);
6418

6419
 cleanup:
6420
    virDomainObjEndAPI(&vm);
6421 6422 6423 6424 6425 6426 6427 6428
    return xml;
}

static int
testDomainSnapshotIsCurrent(virDomainSnapshotPtr snapshot,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
6429
    int ret;
6430 6431 6432 6433

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6434
        return -1;
6435 6436 6437 6438

    ret = (vm->current_snapshot &&
           STREQ(snapshot->name, vm->current_snapshot->def->name));

6439
    virDomainObjEndAPI(&vm);
6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453
    return ret;
}


static int
testDomainSnapshotHasMetadata(virDomainSnapshotPtr snapshot,
                              unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
6454
        return -1;
6455

C
Cole Robinson 已提交
6456
    if (!testSnapObjFromSnapshot(vm, snapshot))
6457 6458 6459 6460
        goto cleanup;

    ret = 1;

6461
 cleanup:
6462
    virDomainObjEndAPI(&vm);
6463 6464 6465
    return ret;
}

6466 6467 6468 6469 6470 6471
static int
testDomainSnapshotAlignDisks(virDomainObjPtr vm,
                             virDomainSnapshotDefPtr def,
                             unsigned int flags)
{
    int align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
E
Eric Blake 已提交
6472
    bool align_match = true;
6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500

    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)
{
6501
    testDriverPtr privconn = domain->conn->privateData;
6502 6503 6504 6505
    virDomainObjPtr vm = NULL;
    virDomainSnapshotDefPtr def = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    virDomainSnapshotPtr snapshot = NULL;
6506
    virObjectEventPtr event = NULL;
6507
    char *xml = NULL;
6508 6509
    bool update_current = true;
    bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE;
6510 6511 6512 6513 6514 6515 6516 6517
    unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;

    /*
     * DISK_ONLY: Not implemented yet
     * REUSE_EXT: Not implemented yet
     *
     * NO_METADATA: Explicitly not implemented
     *
6518
     * REDEFINE + CURRENT: Implemented
6519 6520 6521 6522 6523 6524
     * HALT: Implemented
     * QUIESCE: Nothing to do
     * ATOMIC: Nothing to do
     * LIVE: Nothing to do
     */
    virCheckFlags(
6525 6526
        VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
        VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT |
6527 6528 6529 6530 6531
        VIR_DOMAIN_SNAPSHOT_CREATE_HALT |
        VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE |
        VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC |
        VIR_DOMAIN_SNAPSHOT_CREATE_LIVE, NULL);

6532 6533 6534 6535 6536
    if ((redefine && !(flags & VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT)))
        update_current = false;
    if (redefine)
        parse_flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;

6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551
    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;

6552
    if (redefine) {
C
Cole Robinson 已提交
6553 6554
        if (virDomainSnapshotRedefinePrep(domain, vm, &def, &snap,
                                          &update_current, flags) < 0)
6555 6556 6557 6558 6559
            goto cleanup;
    } else {
        if (!(def->dom = virDomainDefCopy(vm->def,
                                          privconn->caps,
                                          privconn->xmlopt,
6560
                                          NULL,
6561 6562
                                          true)))
            goto cleanup;
6563

6564
        if (testDomainSnapshotAlignDisks(vm, def, flags) < 0)
6565 6566 6567
            goto cleanup;
    }

6568 6569 6570 6571
    if (!snap) {
        if (!(snap = virDomainSnapshotAssignDef(vm->snapshots, def)))
            goto cleanup;
        def = NULL;
6572 6573
    }

6574 6575 6576 6577 6578 6579 6580 6581 6582 6583
    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);
6584
            event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
6585 6586 6587
                                    VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
        }
    }
6588 6589

    snapshot = virGetDomainSnapshot(domain, snap->def->name);
6590
 cleanup:
6591 6592 6593 6594
    VIR_FREE(xml);
    if (vm) {
        if (snapshot) {
            virDomainSnapshotObjPtr other;
6595 6596
            if (update_current)
                vm->current_snapshot = snap;
6597 6598 6599 6600 6601 6602 6603
            other = virDomainSnapshotFindByName(vm->snapshots,
                                                snap->def->parent);
            snap->parent = other;
            other->nchildren++;
            snap->sibling = other->first_child;
            other->first_child = snap;
        }
6604
        virDomainObjEndAPI(&vm);
6605
    }
6606
    testObjectEventQueue(privconn, event);
6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618
    virDomainSnapshotDefFree(def);
    return snapshot;
}


typedef struct _testSnapRemoveData testSnapRemoveData;
typedef testSnapRemoveData *testSnapRemoveDataPtr;
struct _testSnapRemoveData {
    virDomainObjPtr vm;
    bool current;
};

6619
static int
6620
testDomainSnapshotDiscardAll(void *payload,
6621 6622
                             const void *name ATTRIBUTE_UNUSED,
                             void *data)
6623 6624 6625 6626 6627 6628 6629
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapRemoveDataPtr curr = data;

    if (snap->def->current)
        curr->current = true;
    virDomainSnapshotObjListRemove(curr->vm->snapshots, snap);
6630
    return 0;
6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641
}

typedef struct _testSnapReparentData testSnapReparentData;
typedef testSnapReparentData *testSnapReparentDataPtr;
struct _testSnapReparentData {
    virDomainSnapshotObjPtr parent;
    virDomainObjPtr vm;
    int err;
    virDomainSnapshotObjPtr last;
};

6642
static int
6643 6644 6645 6646 6647 6648 6649
testDomainSnapshotReparentChildren(void *payload,
                                   const void *name ATTRIBUTE_UNUSED,
                                   void *data)
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapReparentDataPtr rep = data;

6650
    if (rep->err < 0)
6651
        return 0;
6652 6653 6654 6655 6656 6657 6658

    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;
6659
        return 0;
6660 6661 6662 6663
    }

    if (!snap->sibling)
        rep->last = snap;
6664
    return 0;
6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693
}

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) {
6694
            if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY)
6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737
                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;
6738
 cleanup:
6739
    virDomainObjEndAPI(&vm);
6740 6741 6742 6743 6744 6745 6746
    return ret;
}

static int
testDomainRevertToSnapshot(virDomainSnapshotPtr snapshot,
                           unsigned int flags)
{
6747
    testDriverPtr privconn = snapshot->domain->conn->privateData;
6748 6749
    virDomainObjPtr vm = NULL;
    virDomainSnapshotObjPtr snap = NULL;
6750 6751
    virObjectEventPtr event = NULL;
    virObjectEventPtr event2 = NULL;
6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814
    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;
6815 6816
    config = virDomainDefCopy(snap->def->dom, privconn->caps,
                              privconn->xmlopt, NULL, true);
6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842
    if (!config)
        goto cleanup;

    if (snap->def->state == VIR_DOMAIN_RUNNING ||
        snap->def->state == VIR_DOMAIN_PAUSED) {
        /* Transitions 2, 3, 5, 6, 8, 9 */
        bool was_running = false;
        bool was_stopped = false;

        if (virDomainObjIsActive(vm)) {
            /* Transitions 5, 6, 8, 9 */
            /* Check for ABI compatibility.  */
            if (!virDomainDefCheckABIStability(vm->def, config)) {
                virErrorPtr err = virGetLastError();

                if (!(flags & VIR_DOMAIN_SNAPSHOT_REVERT_FORCE)) {
                    /* Re-spawn error using correct category. */
                    if (err->code == VIR_ERR_CONFIG_UNSUPPORTED)
                        virReportError(VIR_ERR_SNAPSHOT_REVERT_RISKY, "%s",
                                       err->str2);
                    goto cleanup;
                }

                virResetError(err);
                testDomainShutdownState(snapshot->domain, vm,
                                        VIR_DOMAIN_SHUTOFF_FROM_SNAPSHOT);
6843
                event = virDomainEventLifecycleNewFromObj(vm,
6844 6845
                            VIR_DOMAIN_EVENT_STOPPED,
                            VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
6846
                testObjectEventQueue(privconn, event);
6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857
                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. */
6858
                event = virDomainEventLifecycleNewFromObj(vm,
6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871
                                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;
6872
            event = virDomainEventLifecycleNewFromObj(vm,
6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885
                                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 */
6886
                event2 = virDomainEventLifecycleNewFromObj(vm,
6887 6888 6889 6890 6891
                                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 已提交
6892
            virObjectUnref(event);
6893 6894 6895 6896
            event = NULL;

            if (was_stopped) {
                /* Transition 2 */
6897
                event = virDomainEventLifecycleNewFromObj(vm,
6898 6899 6900 6901
                                VIR_DOMAIN_EVENT_STARTED,
                                VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            } else if (was_running) {
                /* Transition 8 */
6902
                event = virDomainEventLifecycleNewFromObj(vm,
6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914
                                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);
6915
            event = virDomainEventLifecycleNewFromObj(vm,
6916 6917 6918 6919 6920 6921 6922 6923 6924
                                    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;

6925
            testObjectEventQueue(privconn, event);
6926
            event = virDomainEventLifecycleNewFromObj(vm,
6927 6928 6929
                            VIR_DOMAIN_EVENT_STARTED,
                            VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            if (paused) {
6930
                event2 = virDomainEventLifecycleNewFromObj(vm,
6931 6932 6933 6934 6935 6936 6937 6938
                                VIR_DOMAIN_EVENT_SUSPENDED,
                                VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT);
            }
        }
    }

    vm->current_snapshot = snap;
    ret = 0;
6939
 cleanup:
6940
    if (event) {
6941
        testObjectEventQueue(privconn, event);
6942
        testObjectEventQueue(privconn, event2);
C
Cole Robinson 已提交
6943
    } else {
C
Cédric Bosdonnat 已提交
6944
        virObjectUnref(event2);
6945
    }
6946
    virDomainObjEndAPI(&vm);
6947 6948 6949 6950 6951

    return ret;
}


6952

6953
static virHypervisorDriver testHypervisorDriver = {
6954
    .name = "Test",
6955 6956 6957
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
6958
    .connectGetHostname = testConnectGetHostname, /* 0.6.3 */
6959
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
6960
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
6961
    .nodeGetCPUStats = testNodeGetCPUStats, /* 2.3.0 */
6962
    .nodeGetFreeMemory = testNodeGetFreeMemory, /* 2.3.0 */
6963
    .nodeGetFreePages = testNodeGetFreePages, /* 2.3.0 */
6964
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
6965
    .connectGetSysinfo = testConnectGetSysinfo, /* 2.3.0 */
6966
    .connectGetType = testConnectGetType, /* 2.3.0 */
6967 6968 6969
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
6970
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984
    .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 */
6985 6986
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
6987
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
6988
    .domainRestore = testDomainRestore, /* 0.3.2 */
6989
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
6990
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
6991
    .domainCoreDumpWithFormat = testDomainCoreDumpWithFormat, /* 1.2.3 */
6992
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
6993 6994 6995 6996
    .domainSetVcpusFlags = testDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = testDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = testDomainPinVcpu, /* 0.7.3 */
    .domainGetVcpus = testDomainGetVcpus, /* 0.7.3 */
6997
    .domainGetVcpuPinInfo = testDomainGetVcpuPinInfo, /* 1.2.18 */
6998 6999
    .domainGetMaxVcpus = testDomainGetMaxVcpus, /* 0.7.3 */
    .domainGetXMLDesc = testDomainGetXMLDesc, /* 0.1.4 */
7000 7001
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
7002 7003 7004
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
7005
    .domainDefineXMLFlags = testDomainDefineXMLFlags, /* 1.2.12 */
7006
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
7007
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
7008 7009 7010
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
7011 7012 7013 7014
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
7015 7016 7017
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
7018 7019 7020 7021
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
7022 7023 7024
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
7025 7026 7027
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
7028
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
7029
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
7030 7031
    .domainGetMetadata = testDomainGetMetadata, /* 1.1.3 */
    .domainSetMetadata = testDomainSetMetadata, /* 1.1.3 */
7032
    .connectGetCPUModelNames = testConnectGetCPUModelNames, /* 1.1.3 */
7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049
    .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 */
7050 7051 7052
    .domainSnapshotCreateXML = testDomainSnapshotCreateXML, /* 1.1.4 */
    .domainRevertToSnapshot = testDomainRevertToSnapshot, /* 1.1.4 */
    .domainSnapshotDelete = testDomainSnapshotDelete, /* 1.1.4 */
7053

E
Eric Blake 已提交
7054
    .connectBaselineCPU = testConnectBaselineCPU, /* 1.2.0 */
7055 7056 7057
};

static virNetworkDriver testNetworkDriver = {
7058 7059 7060 7061 7062
    .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 */
7063 7064
    .connectNetworkEventRegisterAny = testConnectNetworkEventRegisterAny, /* 1.2.1 */
    .connectNetworkEventDeregisterAny = testConnectNetworkEventDeregisterAny, /* 1.2.1 */
7065 7066 7067 7068
    .networkLookupByUUID = testNetworkLookupByUUID, /* 0.3.2 */
    .networkLookupByName = testNetworkLookupByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreateXML, /* 0.3.2 */
    .networkDefineXML = testNetworkDefineXML, /* 0.3.2 */
7069
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
7070
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
7071
    .networkCreate = testNetworkCreate, /* 0.3.2 */
7072 7073 7074 7075 7076 7077 7078
    .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 */
7079 7080
};

L
Laine Stump 已提交
7081
static virInterfaceDriver testInterfaceDriver = {
7082 7083 7084 7085 7086 7087
    .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 */
7088 7089 7090 7091 7092 7093
    .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 */
7094 7095 7096
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
7097 7098 7099
};


7100
static virStorageDriver testStorageDriver = {
7101 7102 7103 7104 7105 7106
    .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 */
7107 7108
    .connectStoragePoolEventRegisterAny = testConnectStoragePoolEventRegisterAny, /* 2.0.0 */
    .connectStoragePoolEventDeregisterAny = testConnectStoragePoolEventDeregisterAny, /* 2.0.0 */
7109 7110 7111
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
7112 7113
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
7114 7115
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
7116
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
7117 7118 7119 7120 7121 7122 7123
    .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 */
7124
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
7125 7126 7127
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

7128 7129 7130 7131 7132 7133 7134 7135 7136
    .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 */
7137 7138
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
7139 7140
};

7141
static virNodeDeviceDriver testNodeDeviceDriver = {
7142 7143
    .connectNodeDeviceEventRegisterAny = testConnectNodeDeviceEventRegisterAny, /* 2.2.0 */
    .connectNodeDeviceEventDeregisterAny = testConnectNodeDeviceEventDeregisterAny, /* 2.2.0 */
7144 7145 7146 7147 7148 7149 7150 7151 7152
    .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 */
7153 7154
};

7155 7156 7157 7158 7159 7160 7161 7162
static virConnectDriver testConnectDriver = {
    .hypervisorDriver = &testHypervisorDriver,
    .interfaceDriver = &testInterfaceDriver,
    .networkDriver = &testNetworkDriver,
    .nodeDeviceDriver = &testNodeDeviceDriver,
    .nwfilterDriver = NULL,
    .secretDriver = NULL,
    .storageDriver = &testStorageDriver,
7163 7164
};

7165 7166 7167 7168 7169 7170 7171 7172
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
7173 7174
    return virRegisterConnectDriver(&testConnectDriver,
                                    false);
7175
}