test_driver.c 212.3 KB
Newer Older
1 2 3
/*
 * test.c: A "mock" hypervisor for use by application unit tests
 *
4
 * Copyright (C) 2006-2014 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 "node_device_conf.h"
53
#include "virxml.h"
54
#include "virthread.h"
55
#include "virlog.h"
E
Eric Blake 已提交
56
#include "virfile.h"
57
#include "virtypedparam.h"
58
#include "virrandom.h"
59
#include "virstring.h"
60
#include "cpu/cpu.h"
61
#include "virauth.h"
62

63 64
#define VIR_FROM_THIS VIR_FROM_TEST

65 66 67 68 69 70 71 72 73
/* Driver specific info to carry with a domain */
struct _testDomainObjPrivate {
    virVcpuInfoPtr vcpu_infos;

    unsigned char *cpumaps;
};
typedef struct _testDomainObjPrivate testDomainObjPrivate;
typedef struct _testDomainObjPrivate *testDomainObjPrivatePtr;

74 75 76 77 78
#define MAX_CPUS 128

struct _testCell {
    unsigned long mem;
    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 _testConn {
94
    virMutex lock;
95

E
Eric Blake 已提交
96
    char *path;
97
    int nextDomID;
98
    virCapsPtr caps;
99
    virDomainXMLOptionPtr xmlopt;
100
    virNodeInfo nodeInfo;
101
    virDomainObjListPtr domains;
102
    virNetworkObjList networks;
L
Laine Stump 已提交
103
    virInterfaceObjList ifaces;
104 105
    bool transaction_running;
    virInterfaceObjList backupIfaces;
C
Cole Robinson 已提交
106
    virStoragePoolObjList pools;
107
    virNodeDeviceObjList devs;
108 109
    int numCells;
    testCell cells[MAX_CELLS];
110 111
    size_t numAuths;
    testAuthPtr auths;
112

113
    virObjectEventStatePtr eventState;
114 115 116
};
typedef struct _testConn testConn;
typedef struct _testConn *testConnPtr;
117

118 119 120 121
static testConn defaultConn;
static int defaultConnections;
static virMutex defaultLock;

122
#define TEST_MODEL "i686"
123
#define TEST_MODEL_WORDSIZE 32
124
#define TEST_EMULATOR "/usr/bin/test-hv"
125

126
static const virNodeInfo defaultNodeInfo = {
127
    TEST_MODEL,
128 129 130 131 132 133 134
    1024*1024*3, /* 3 GB */
    16,
    1400,
    2,
    2,
    2,
    2,
135 136
};

137

138
static int testConnectClose(virConnectPtr conn);
139
static void testObjectEventQueue(testConnPtr driver,
140
                                 virObjectEventPtr event);
141

142 143 144 145 146 147 148 149
static int
testOnceInit(void)
{
    return virMutexInit(&defaultLock);
}

VIR_ONCE_GLOBAL_INIT(test)

150

151 152
static void testDriverLock(testConnPtr driver)
{
153
    virMutexLock(&driver->lock);
154 155 156 157
}

static void testDriverUnlock(testConnPtr driver)
{
158
    virMutexUnlock(&driver->lock);
159 160
}

161 162 163 164 165 166 167 168 169 170 171 172 173 174
static void *testDomainObjPrivateAlloc(void)
{
    testDomainObjPrivatePtr priv;

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

    return priv;
}

static void testDomainObjPrivateFree(void *data)
{
    testDomainObjPrivatePtr priv = data;

D
Daniel P. Berrange 已提交
175
    VIR_FREE(priv->vcpu_infos);
176 177 178 179
    VIR_FREE(priv->cpumaps);
    VIR_FREE(priv);
}

180 181 182 183 184 185
#define TEST_NAMESPACE_HREF "http://libvirt.org/schemas/domain/test/1.0"

typedef struct _testDomainNamespaceDef testDomainNamespaceDef;
typedef testDomainNamespaceDef *testDomainNamespaceDefPtr;
struct _testDomainNamespaceDef {
    int runstate;
186
    bool transient;
C
Cole Robinson 已提交
187
    bool hasManagedSave;
188 189 190

    unsigned int num_snap_nodes;
    xmlNodePtr *snap_nodes;
191 192 193 194 195 196
};

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

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

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

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

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    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;
    }

281 282 283 284 285
    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 已提交
286 287 288 289 290
    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;
    }
291

292 293 294 295
    *data = nsdata;
    return 0;

error:
296
    VIR_FREE(nodes);
297 298 299
    testDomainDefNamespaceFree(nsdata);
    return -1;
}
300

301
static virDomainXMLOptionPtr
302 303
testBuildXMLConfig(void)
{
304 305 306 307 308 309 310 311 312 313 314 315
    virDomainXMLPrivateDataCallbacks priv = {
        .alloc = testDomainObjPrivateAlloc,
        .free = testDomainObjPrivateFree
    };

    /* All our XML extensions are input only, so we only need to parse */
    virDomainXMLNamespace ns = {
        .parse = testDomainDefNamespaceParse,
        .free = testDomainDefNamespaceFree,
    };

    return virDomainXMLOptionNew(NULL, &priv, &ns);
316 317 318
}


319 320
static virCapsPtr
testBuildCapabilities(virConnectPtr conn) {
321
    testConnPtr privconn = conn->privateData;
322 323 324
    virCapsPtr caps;
    virCapsGuestPtr guest;
    const char *const guest_types[] = { "hvm", "xen" };
325
    size_t i;
326

327
    if ((caps = virCapabilitiesNew(VIR_ARCH_I686, 0, 0)) == NULL)
328
        goto error;
329

330
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
331
        goto error;
332
    if (virCapabilitiesAddHostFeature(caps, "nonpae") < 0)
333
        goto error;
334

335
    for (i = 0; i < privconn->numCells; i++) {
336 337 338
        virCapsHostNUMACellCPUPtr cpu_cells;

        if (VIR_ALLOC_N(cpu_cells, privconn->cells[i].numCpus) < 0)
339
            goto error;
340 341 342 343 344

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


345
        if (virCapabilitiesAddHostNUMACell(caps, i, privconn->cells[i].numCpus,
346
                                           0, cpu_cells) < 0)
347
            goto error;
348 349
    }

350
    for (i = 0; i < ARRAY_CARDINALITY(guest_types); i++) {
351 352
        if ((guest = virCapabilitiesAddGuest(caps,
                                             guest_types[i],
353
                                             VIR_ARCH_I686,
354 355 356 357
                                             TEST_EMULATOR,
                                             NULL,
                                             0,
                                             NULL)) == NULL)
358
            goto error;
359

360 361 362 363 364 365
        if (virCapabilitiesAddGuestDomain(guest,
                                          "test",
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
366
            goto error;
367

368
        if (virCapabilitiesAddGuestFeature(guest, "pae", 1, 1) == NULL)
369
            goto error;
370
        if (virCapabilitiesAddGuestFeature(guest, "nonpae", 1, 1) == NULL)
371
            goto error;
372 373
    }

374 375
    caps->host.nsecModels = 1;
    if (VIR_ALLOC_N(caps->host.secModels, caps->host.nsecModels) < 0)
376
        goto error;
377 378
    if (VIR_STRDUP(caps->host.secModels[0].model, "testSecurity") < 0)
        goto error;
379

380 381
    if (VIR_STRDUP(caps->host.secModels[0].doi, "") < 0)
        goto error;
382

383
    return caps;
384

385
error:
386
    virObjectUnref(caps);
387
    return NULL;
388 389
}

390

391 392 393
static const char *defaultDomainXML =
"<domain type='test'>"
"  <name>test</name>"
394
"  <uuid>6695eb01-f6a4-8304-79aa-97f2502e193f</uuid>"
395 396 397 398 399 400 401
"  <memory>8388608</memory>"
"  <currentMemory>2097152</currentMemory>"
"  <vcpu>2</vcpu>"
"  <os>"
"    <type>hvm</type>"
"  </os>"
"</domain>";
402 403


404 405 406
static const char *defaultNetworkXML =
"<network>"
"  <name>default</name>"
407
"  <uuid>dd8fe884-6c02-601e-7551-cca97df1c5df</uuid>"
408
"  <bridge name='virbr0'/>"
409 410 411
"  <forward/>"
"  <ip address='192.168.122.1' netmask='255.255.255.0'>"
"    <dhcp>"
412
"      <range start='192.168.122.2' end='192.168.122.254'/>"
413 414 415
"    </dhcp>"
"  </ip>"
"</network>";
416

L
Laine Stump 已提交
417 418 419 420 421 422 423 424 425 426 427
static const char *defaultInterfaceXML =
"<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>"
"</interface>";

C
Cole Robinson 已提交
428 429 430
static const char *defaultPoolXML =
"<pool type='dir'>"
"  <name>default-pool</name>"
431
"  <uuid>dfe224cb-28fb-8dd0-c4b2-64eb3f0f4566</uuid>"
C
Cole Robinson 已提交
432 433 434 435 436
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
"</pool>";

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
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";

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
static const char *defaultNodeXML =
"<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>";

478
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
C
Cole Robinson 已提交
479 480
static const unsigned long long defaultPoolAlloc = 0;

481
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool);
482
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
483

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
static virDomainObjPtr
testDomObjFromDomain(virDomainPtr domain)
{
    virDomainObjPtr vm;
    testConnPtr driver = domain->conn->privateData;
    char uuidstr[VIR_UUID_STRING_BUFLEN];

    testDriverLock(driver);
    vm = virDomainObjListFindByUUID(driver->domains, domain->uuid);
    if (!vm) {
        virUUIDFormat(domain->uuid, uuidstr);
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s' (%s)"),
                       uuidstr, domain->name);
    }

    testDriverUnlock(driver);
    return vm;
}

504
static char *
505
testDomainGenerateIfname(virDomainDefPtr domdef) {
506
    int maxif = 1024;
507 508
    int ifctr;
    size_t i;
509 510 511 512 513

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

514
        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0)
515 516 517
            return NULL;

        /* Generate network interface names */
518
        for (i = 0; i < domdef->nnets; i++) {
519
            if (domdef->nets[i]->ifname &&
520
                STREQ(domdef->nets[i]->ifname, ifname)) {
521 522 523 524 525 526 527 528 529
                found = 1;
                break;
            }
        }

        if (!found)
            return ifname;
    }

530 531
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Exceeded max iface limit %d"), maxif);
532 533 534
    return NULL;
}

535
static int
536
testDomainGenerateIfnames(virDomainDefPtr domdef)
537
{
538
    size_t i = 0;
539 540 541 542 543 544

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

545
        ifname = testDomainGenerateIfname(domdef);
546
        if (!ifname)
547
            return -1;
548 549 550 551

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

552
    return 0;
553 554
}

555 556
/* Helper to update info for a single VCPU */
static int
557
testDomainUpdateVCPU(virDomainObjPtr dom,
558 559 560 561 562 563 564
                     int vcpu,
                     int maplen,
                     int maxcpu)
{
    testDomainObjPrivatePtr privdata = dom->privateData;
    virVcpuInfoPtr info = &privdata->vcpu_infos[vcpu];
    unsigned char *cpumap = VIR_GET_CPUMAP(privdata->cpumaps, maplen, vcpu);
565
    size_t j;
H
Hu Tao 已提交
566
    bool cpu;
567 568 569 570 571 572 573 574 575 576 577

    memset(info, 0, sizeof(virVcpuInfo));
    memset(cpumap, 0, maplen);

    info->number    = vcpu;
    info->state     = VIR_VCPU_RUNNING;
    info->cpuTime   = 5000000;
    info->cpu       = 0;

    if (dom->def->cpumask) {
        for (j = 0; j < maxcpu && j < VIR_DOMAIN_CPUMASK_LEN; ++j) {
H
Hu Tao 已提交
578 579 580
            if (virBitmapGetBit(dom->def->cpumask, j, &cpu) < 0)
                return -1;
            if (cpu) {
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
                VIR_USE_CPU(cpumap, j);
                info->cpu = j;
            }
        }
    } else {
        for (j = 0; j < maxcpu; ++j) {
            if ((j % 3) == 0) {
                /* Mark of every third CPU as usable */
                VIR_USE_CPU(cpumap, j);
                info->cpu = j;
            }
        }
    }

    return 0;
}

/*
 * Update domain VCPU amount and info
 *
 * @conn: virConnectPtr
 * @dom : domain needing updates
 * @nvcpus: New amount of vcpus for the domain
 * @clear_all: If true, rebuild info for ALL vcpus, not just newly added vcpus
 */
static int
607
testDomainUpdateVCPUs(testConnPtr privconn,
608 609 610 611 612
                      virDomainObjPtr dom,
                      int nvcpus,
                      unsigned int clear_all)
{
    testDomainObjPrivatePtr privdata = dom->privateData;
613 614
    size_t i;
    int ret = -1;
615 616 617 618 619
    int cpumaplen, maxcpu;

    maxcpu  = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
    cpumaplen = VIR_CPU_MAPLEN(maxcpu);

620
    if (VIR_REALLOC_N(privdata->vcpu_infos, nvcpus) < 0)
621 622
        goto cleanup;

623
    if (VIR_REALLOC_N(privdata->cpumaps, nvcpus * cpumaplen) < 0)
624 625 626 627 628
        goto cleanup;

    /* Set running VCPU and cpumap state */
    if (clear_all) {
        for (i = 0; i < nvcpus; ++i)
629
            if (testDomainUpdateVCPU(dom, i, cpumaplen, maxcpu) < 0)
630 631 632 633 634
                goto cleanup;

    } else if (nvcpus > dom->def->vcpus) {
        /* VCPU amount has grown, populate info for the new vcpus */
        for (i = dom->def->vcpus; i < nvcpus; ++i)
635
            if (testDomainUpdateVCPU(dom, i, cpumaplen, maxcpu) < 0)
636 637 638
                goto cleanup;
    }

639
    dom->def->vcpus = nvcpus;
640 641 642 643 644
    ret = 0;
cleanup:
    return ret;
}

645 646
static void
testDomainShutdownState(virDomainPtr domain,
J
Jiri Denemark 已提交
647 648
                        virDomainObjPtr privdom,
                        virDomainShutoffReason reason)
649 650 651 652 653 654 655
{
    if (privdom->newDef) {
        virDomainDefFree(privdom->def);
        privdom->def = privdom->newDef;
        privdom->newDef = NULL;
    }

J
Jiri Denemark 已提交
656
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF, reason);
657 658 659 660 661
    privdom->def->id = -1;
    if (domain)
        domain->id = -1;
}

662
/* Set up domain runtime state */
663
static int
664
testDomainStartState(testConnPtr privconn,
J
Jiri Denemark 已提交
665 666
                     virDomainObjPtr dom,
                     virDomainRunningReason reason)
667
{
668
    int ret = -1;
669

670
    if (testDomainUpdateVCPUs(privconn, dom, dom->def->vcpus, 1) < 0)
671 672
        goto cleanup;

J
Jiri Denemark 已提交
673
    virDomainObjSetState(dom, VIR_DOMAIN_RUNNING, reason);
674 675
    dom->def->id = privconn->nextDomID++;

676
    if (virDomainObjSetDefTransient(privconn->caps,
677
                                    privconn->xmlopt,
678
                                    dom, false) < 0) {
679 680 681
        goto cleanup;
    }

C
Cole Robinson 已提交
682
    dom->hasManagedSave = false;
683 684
    ret = 0;
cleanup:
685
    if (ret < 0)
J
Jiri Denemark 已提交
686
        testDomainShutdownState(NULL, dom, VIR_DOMAIN_SHUTOFF_FAILED);
687
    return ret;
688
}
689

690 691 692 693 694 695 696

/* 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)
{
697
    int u;
698
    testConnPtr privconn = &defaultConn;
699 700 701 702
    virDomainDefPtr domdef = NULL;
    virDomainObjPtr domobj = NULL;
    virNetworkDefPtr netdef = NULL;
    virNetworkObjPtr netobj = NULL;
L
Laine Stump 已提交
703 704
    virInterfaceDefPtr interfacedef = NULL;
    virInterfaceObjPtr interfaceobj = NULL;
C
Cole Robinson 已提交
705 706
    virStoragePoolDefPtr pooldef = NULL;
    virStoragePoolObjPtr poolobj = NULL;
707 708
    virNodeDeviceDefPtr nodedef = NULL;
    virNodeDeviceObjPtr nodeobj = NULL;
709

710 711 712 713 714 715 716
    virMutexLock(&defaultLock);
    if (defaultConnections++) {
        conn->privateData = &defaultConn;
        virMutexUnlock(&defaultLock);
        return VIR_DRV_OPEN_SUCCESS;
    }

717
    if (virMutexInit(&privconn->lock) < 0) {
718 719
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
720 721
        defaultConnections--;
        virMutexUnlock(&defaultLock);
722 723 724 725
        return VIR_DRV_OPEN_ERROR;
    }

    conn->privateData = privconn;
726

727
    if (!(privconn->eventState = virObjectEventStateNew()))
728 729
        goto error;

730
    if (!(privconn->domains = virDomainObjListNew()))
731 732
        goto error;

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

735
    /* Numa setup */
736 737 738 739 740
    privconn->numCells = 2;
    for (u = 0; u < 2; ++u) {
        privconn->cells[u].numCpus = 8;
        privconn->cells[u].mem = (u + 1) * 2048 * 1024;
    }
741
    for (u = 0; u < 16; u++) {
742
        virBitmapPtr siblings = virBitmapNew(16);
743
        if (!siblings)
744 745 746 747 748 749
            goto error;
        ignore_value(virBitmapSetBit(siblings, u));
        privconn->cells[u / 8].cpus[(u % 8)].id = u;
        privconn->cells[u / 8].cpus[(u % 8)].socket_id = u / 8;
        privconn->cells[u / 8].cpus[(u % 8)].core_id = u % 8;
        privconn->cells[u / 8].cpus[(u % 8)].siblings = siblings;
750 751
    }

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

755
    if (!(privconn->xmlopt = testBuildXMLConfig()))
756 757
        goto error;

758 759
    privconn->nextDomID = 1;

760 761
    if (!(domdef = virDomainDefParseString(defaultDomainXML,
                                           privconn->caps,
762
                                           privconn->xmlopt,
M
Matthias Bolte 已提交
763
                                           1 << VIR_DOMAIN_VIRT_TEST,
764
                                           VIR_DOMAIN_XML_INACTIVE)))
765
        goto error;
M
Matthias Bolte 已提交
766

767
    if (testDomainGenerateIfnames(domdef) < 0)
768
        goto error;
769
    if (!(domobj = virDomainObjListAdd(privconn->domains,
770
                                       domdef,
771
                                       privconn->xmlopt,
772
                                       0, NULL)))
773 774
        goto error;
    domdef = NULL;
775

776
    domobj->persistent = 1;
777 778
    if (testDomainStartState(privconn, domobj,
                             VIR_DOMAIN_RUNNING_BOOTED) < 0) {
779
        virObjectUnlock(domobj);
780 781 782
        goto error;
    }

783
    virObjectUnlock(domobj);
784

785
    if (!(netdef = virNetworkDefParseString(defaultNetworkXML)))
786
        goto error;
787
    if (!(netobj = virNetworkAssignDef(&privconn->networks, netdef, false))) {
788 789 790 791 792
        virNetworkDefFree(netdef);
        goto error;
    }
    netobj->active = 1;
    netobj->persistent = 1;
793
    virNetworkObjUnlock(netobj);
794

795
    if (!(interfacedef = virInterfaceDefParseString(defaultInterfaceXML)))
L
Laine Stump 已提交
796
        goto error;
797
    if (!(interfaceobj = virInterfaceAssignDef(&privconn->ifaces, interfacedef))) {
L
Laine Stump 已提交
798 799 800 801 802 803
        virInterfaceDefFree(interfacedef);
        goto error;
    }
    interfaceobj->active = 1;
    virInterfaceObjUnlock(interfaceobj);

804
    if (!(pooldef = virStoragePoolDefParseString(defaultPoolXML)))
C
Cole Robinson 已提交
805 806
        goto error;

807
    if (!(poolobj = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
808 809 810 811
                                               pooldef))) {
        virStoragePoolDefFree(pooldef);
        goto error;
    }
812

813
    if (testStoragePoolObjSetDefaults(poolobj) == -1) {
814
        virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
815
        goto error;
816
    }
C
Cole Robinson 已提交
817
    poolobj->active = 1;
818
    virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
819

820
    /* Init default node device */
821
    if (!(nodedef = virNodeDeviceDefParseString(defaultNodeXML, 0, NULL)))
822
        goto error;
823
    if (!(nodeobj = virNodeDeviceAssignDef(&privconn->devs,
824 825 826 827 828 829
                                           nodedef))) {
        virNodeDeviceDefFree(nodedef);
        goto error;
    }
    virNodeDeviceObjUnlock(nodeobj);

830
    virMutexUnlock(&defaultLock);
831

832 833 834
    return VIR_DRV_OPEN_SUCCESS;

error:
835
    virObjectUnref(privconn->domains);
836
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
837
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
838
    virStoragePoolObjListFree(&privconn->pools);
839
    virNodeDeviceObjListFree(&privconn->devs);
840
    virObjectUnref(privconn->caps);
841
    virObjectEventStateFree(privconn->eventState);
842
    virMutexDestroy(&privconn->lock);
843
    conn->privateData = NULL;
844
    virDomainDefFree(domdef);
845 846
    defaultConnections--;
    virMutexUnlock(&defaultLock);
847
    return VIR_DRV_OPEN_ERROR;
848 849 850 851
}


static char *testBuildFilename(const char *relativeTo,
852 853 854
                               const char *filename) {
    char *offset;
    int baseLen;
855 856
    char *ret;

857
    if (!filename || filename[0] == '\0')
858
        return NULL;
859 860 861 862
    if (filename[0] == '/') {
        ignore_value(VIR_STRDUP(ret, filename));
        return ret;
    }
863

864
    offset = strrchr(relativeTo, '/');
865
    if ((baseLen = (offset-relativeTo+1))) {
866
        char *absFile;
C
Chris Lalancette 已提交
867 868
        int totalLen = baseLen + strlen(filename) + 1;
        if (VIR_ALLOC_N(absFile, totalLen) < 0)
869
            return NULL;
C
Chris Lalancette 已提交
870 871 872 873
        if (virStrncpy(absFile, relativeTo, baseLen, totalLen) == NULL) {
            VIR_FREE(absFile);
            return NULL;
        }
874 875 876
        strcat(absFile, filename);
        return absFile;
    } else {
877 878
        ignore_value(VIR_STRDUP(ret, filename));
        return ret;
879
    }
880 881
}

C
Cole Robinson 已提交
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
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;
    }

error:
    xmlFreeDoc(doc);
    VIR_FREE(absFile);
    return ret;
}

919 920 921
static int
testParseNodeInfo(virNodeInfoPtr nodeInfo, xmlXPathContextPtr ctxt)
{
922
    char *str;
923 924
    long l;
    int ret;
925

926
    ret = virXPathLong("string(/node/cpu/nodes[1])", ctxt, &l);
927 928 929
    if (ret == 0) {
        nodeInfo->nodes = l;
    } else if (ret == -2) {
930 931
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu nodes value"));
932
        goto error;
933
    }
934

935
    ret = virXPathLong("string(/node/cpu/sockets[1])", ctxt, &l);
936 937 938
    if (ret == 0) {
        nodeInfo->sockets = l;
    } else if (ret == -2) {
939 940
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu sockets value"));
941
        goto error;
942
    }
943

944
    ret = virXPathLong("string(/node/cpu/cores[1])", ctxt, &l);
945 946 947
    if (ret == 0) {
        nodeInfo->cores = l;
    } else if (ret == -2) {
948 949
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu cores value"));
950
        goto error;
951 952
    }

953
    ret = virXPathLong("string(/node/cpu/threads[1])", ctxt, &l);
954 955 956
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
957 958
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu threads value"));
959
        goto error;
960
    }
961

962 963
    nodeInfo->cpus = (nodeInfo->cores * nodeInfo->threads *
                      nodeInfo->sockets * nodeInfo->nodes);
964
    ret = virXPathLong("string(/node/cpu/active[1])", ctxt, &l);
965 966
    if (ret == 0) {
        if (l < nodeInfo->cpus) {
967 968
            nodeInfo->cpus = l;
        }
969
    } else if (ret == -2) {
970 971
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu active value"));
972
        goto error;
973
    }
974
    ret = virXPathLong("string(/node/cpu/mhz[1])", ctxt, &l);
975 976 977
    if (ret == 0) {
        nodeInfo->mhz = l;
    } else if (ret == -2) {
978 979
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu mhz value"));
980
        goto error;
981 982
    }

983
    str = virXPathString("string(/node/cpu/model[1])", ctxt);
984
    if (str != NULL) {
C
Chris Lalancette 已提交
985
        if (virStrcpyStatic(nodeInfo->model, str) == NULL) {
986 987
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Model %s too big for destination"), str);
C
Chris Lalancette 已提交
988 989 990
            VIR_FREE(str);
            goto error;
        }
991
        VIR_FREE(str);
992 993
    }

994
    ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
995 996 997
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
998 999
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node memory value"));
1000
        goto error;
1001
    }
1002

1003 1004 1005 1006 1007
    return 0;
error:
    return -1;
}

1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
static int
testParseDomainSnapshots(testConnPtr privconn,
                         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,
                                            1 << VIR_DOMAIN_VIRT_TEST,
                                            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;
error:
    return ret;
}

1065
static int
C
Cole Robinson 已提交
1066 1067 1068
testParseDomains(testConnPtr privconn,
                 const char *file,
                 xmlXPathContextPtr ctxt)
1069 1070 1071 1072 1073 1074 1075 1076
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virDomainObjPtr obj;

    num = virXPathNodeSet("/node/domain", ctxt, &nodes);
    if (num < 0) {
1077
        goto error;
1078
    }
1079

1080
    for (i = 0; i < num; i++) {
1081
        virDomainDefPtr def;
1082
        testDomainNamespaceDefPtr nsdata;
C
Cole Robinson 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file, "domain");
        if (!node)
            goto error;

        def = virDomainDefParseNode(ctxt->doc, node,
                                    privconn->caps, privconn->xmlopt,
                                    1 << VIR_DOMAIN_VIRT_TEST,
                                    VIR_DOMAIN_XML_INACTIVE);
        if (!def)
            goto error;
1093

1094
        if (testDomainGenerateIfnames(def) < 0 ||
1095
            !(obj = virDomainObjListAdd(privconn->domains,
1096
                                        def,
1097
                                        privconn->xmlopt,
1098
                                        0, NULL))) {
1099
            virDomainDefFree(def);
1100 1101
            goto error;
        }
1102

1103 1104 1105 1106 1107
        if (testParseDomainSnapshots(privconn, obj, file, ctxt) < 0) {
            virObjectUnlock(obj);
            goto error;
        }

1108
        nsdata = def->namespaceData;
1109
        obj->persistent = !nsdata->transient;
C
Cole Robinson 已提交
1110
        obj->hasManagedSave = nsdata->hasManagedSave;
1111 1112 1113 1114 1115 1116 1117 1118 1119

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

1123
        virObjectUnlock(obj);
1124
    }
1125

1126 1127 1128 1129 1130 1131 1132
    ret = 0;
error:
    VIR_FREE(nodes);
    return ret;
}

static int
C
Cole Robinson 已提交
1133 1134 1135
testParseNetworks(testConnPtr privconn,
                  const char *file,
                  xmlXPathContextPtr ctxt)
1136 1137 1138 1139 1140 1141 1142 1143
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virNetworkObjPtr obj;

    num = virXPathNodeSet("/node/network", ctxt, &nodes);
    if (num < 0) {
1144 1145
        goto error;
    }
1146 1147

    for (i = 0; i < num; i++) {
1148
        virNetworkDefPtr def;
C
Cole Robinson 已提交
1149 1150 1151
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file, "network");
        if (!node)
            goto error;
1152

C
Cole Robinson 已提交
1153 1154 1155
        def = virNetworkDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
1156 1157

        if (!(obj = virNetworkAssignDef(&privconn->networks, def, false))) {
1158 1159
            virNetworkDefFree(def);
            goto error;
1160
        }
1161 1162 1163 1164

        obj->persistent = 1;
        obj->active = 1;
        virNetworkObjUnlock(obj);
1165
    }
1166

1167 1168 1169 1170 1171 1172 1173
    ret = 0;
error:
    VIR_FREE(nodes);
    return ret;
}

static int
C
Cole Robinson 已提交
1174 1175 1176
testParseInterfaces(testConnPtr privconn,
                    const char *file,
                    xmlXPathContextPtr ctxt)
1177 1178 1179 1180 1181 1182 1183 1184
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virInterfaceObjPtr obj;

    num = virXPathNodeSet("/node/interface", ctxt, &nodes);
    if (num < 0) {
L
Laine Stump 已提交
1185 1186
        goto error;
    }
1187 1188

    for (i = 0; i < num; i++) {
L
Laine Stump 已提交
1189
        virInterfaceDefPtr def;
C
Cole Robinson 已提交
1190 1191 1192 1193
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                   "interface");
        if (!node)
            goto error;
L
Laine Stump 已提交
1194

C
Cole Robinson 已提交
1195 1196 1197
        def = virInterfaceDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
1198

1199
        if (!(obj = virInterfaceAssignDef(&privconn->ifaces, def))) {
L
Laine Stump 已提交
1200 1201 1202
            virInterfaceDefFree(def);
            goto error;
        }
1203

1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
        obj->active = 1;
        virInterfaceObjUnlock(obj);
    }

    ret = 0;
error:
    VIR_FREE(nodes);
    return ret;
}

static int
C
Cole Robinson 已提交
1215
testOpenVolumesForPool(const char *file,
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
                       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);
    if (num < 0) {
        goto error;
    }

    for (i = 0; i < num; i++) {
C
Cole Robinson 已提交
1237 1238 1239 1240
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                   "volume");
        if (!node)
            goto error;
1241

C
Cole Robinson 已提交
1242 1243 1244
        def = virStorageVolDefParseNode(pool->def, ctxt->doc, node);
        if (!def)
            goto error;
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265

        if (VIR_REALLOC_N(pool->volumes.objs,
                          pool->volumes.count+1) < 0)
            goto error;

        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;

        pool->def->allocation += def->allocation;
        pool->def->available = (pool->def->capacity -
                                pool->def->allocation);

        pool->volumes.objs[pool->volumes.count++] = def;
        def = NULL;
L
Laine Stump 已提交
1266 1267
    }

1268 1269 1270 1271 1272 1273 1274 1275
    ret = 0;
error:
    virStorageVolDefFree(def);
    VIR_FREE(nodes);
    return ret;
}

static int
C
Cole Robinson 已提交
1276 1277 1278
testParseStorage(testConnPtr privconn,
                 const char *file,
                 xmlXPathContextPtr ctxt)
1279 1280 1281 1282 1283 1284 1285 1286
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virStoragePoolObjPtr obj;

    num = virXPathNodeSet("/node/pool", ctxt, &nodes);
    if (num < 0) {
C
Cole Robinson 已提交
1287 1288
        goto error;
    }
1289 1290

    for (i = 0; i < num; i++) {
C
Cole Robinson 已提交
1291
        virStoragePoolDefPtr def;
C
Cole Robinson 已提交
1292 1293 1294 1295
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                   "pool");
        if (!node)
            goto error;
C
Cole Robinson 已提交
1296

C
Cole Robinson 已提交
1297 1298 1299
        def = virStoragePoolDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
C
Cole Robinson 已提交
1300

1301
        if (!(obj = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
1302 1303 1304 1305 1306
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

1307 1308
        if (testStoragePoolObjSetDefaults(obj) == -1) {
            virStoragePoolObjUnlock(obj);
C
Cole Robinson 已提交
1309
            goto error;
1310
        }
1311
        obj->active = 1;
1312 1313

        /* Find storage volumes */
C
Cole Robinson 已提交
1314
        if (testOpenVolumesForPool(file, ctxt, obj, i+1) < 0) {
1315
            virStoragePoolObjUnlock(obj);
1316 1317 1318
            goto error;
        }

1319
        virStoragePoolObjUnlock(obj);
C
Cole Robinson 已提交
1320 1321
    }

1322 1323 1324 1325 1326 1327 1328
    ret = 0;
error:
    VIR_FREE(nodes);
    return ret;
}

static int
C
Cole Robinson 已提交
1329 1330 1331
testParseNodedevs(testConnPtr privconn,
                  const char *file,
                  xmlXPathContextPtr ctxt)
1332 1333 1334 1335 1336 1337 1338 1339
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virNodeDeviceObjPtr obj;

    num = virXPathNodeSet("/node/device", ctxt, &nodes);
    if (num < 0) {
1340 1341
        goto error;
    }
1342 1343

    for (i = 0; i < num; i++) {
1344
        virNodeDeviceDefPtr def;
C
Cole Robinson 已提交
1345 1346 1347 1348
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                  "nodedev");
        if (!node)
            goto error;
1349

C
Cole Robinson 已提交
1350 1351 1352
        def = virNodeDeviceDefParseNode(ctxt->doc, node, 0, NULL);
        if (!def)
            goto error;
1353 1354

        if (!(obj = virNodeDeviceAssignDef(&privconn->devs, def))) {
1355 1356 1357
            virNodeDeviceDefFree(def);
            goto error;
        }
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367

        virNodeDeviceObjUnlock(obj);
    }

    ret = 0;
error:
    VIR_FREE(nodes);
    return ret;
}

1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
static int
testParseAuthUsers(testConnPtr privconn,
                   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;
error:
    VIR_FREE(nodes);
    return ret;
}
1407 1408 1409

/* No shared state between simultaneous test connections initialized
 * from a file.  */
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
static int
testOpenFromFile(virConnectPtr conn, const char *file)
{
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    testConnPtr privconn;

    if (VIR_ALLOC(privconn) < 0)
        return VIR_DRV_OPEN_ERROR;
    if (virMutexInit(&privconn->lock) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
1424 1425
    }

1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
    testDriverLock(privconn);
    conn->privateData = privconn;

    if (!(privconn->domains = virDomainObjListNew()))
        goto error;

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

    if (!(privconn->xmlopt = testBuildXMLConfig()))
        goto error;

1438
    if (!(privconn->eventState = virObjectEventStateNew()))
1439 1440
        goto error;

1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
    if (!(doc = virXMLParseFileCtxt(file, &ctxt))) {
        goto error;
    }

    if (!xmlStrEqual(ctxt->node->name, BAD_CAST "node")) {
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Root element is not 'node'"));
        goto error;
    }

    privconn->nextDomID = 1;
    privconn->numCells = 0;
    if (VIR_STRDUP(privconn->path, file) < 0)
        goto error;
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    if (testParseNodeInfo(&privconn->nodeInfo, ctxt) < 0)
        goto error;
C
Cole Robinson 已提交
1459
    if (testParseDomains(privconn, file, ctxt) < 0)
1460
        goto error;
C
Cole Robinson 已提交
1461
    if (testParseNetworks(privconn, file, ctxt) < 0)
1462
        goto error;
C
Cole Robinson 已提交
1463
    if (testParseInterfaces(privconn, file, ctxt) < 0)
1464
        goto error;
C
Cole Robinson 已提交
1465
    if (testParseStorage(privconn, file, ctxt) < 0)
1466
        goto error;
C
Cole Robinson 已提交
1467
    if (testParseNodedevs(privconn, file, ctxt) < 0)
1468
        goto error;
1469 1470
    if (testParseAuthUsers(privconn, ctxt) < 0)
        goto error;
1471

J
Jim Meyering 已提交
1472
    xmlXPathFreeContext(ctxt);
1473
    xmlFreeDoc(doc);
1474
    testDriverUnlock(privconn);
1475

1476
    return 0;
1477 1478

 error:
J
Jim Meyering 已提交
1479
    xmlXPathFreeContext(ctxt);
1480
    xmlFreeDoc(doc);
1481
    virObjectUnref(privconn->domains);
1482
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1483
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1484
    virStoragePoolObjListFree(&privconn->pools);
E
Eric Blake 已提交
1485
    VIR_FREE(privconn->path);
1486
    virObjectEventStateFree(privconn->eventState);
1487
    testDriverUnlock(privconn);
1488
    VIR_FREE(privconn);
1489
    conn->privateData = NULL;
1490
    return VIR_DRV_OPEN_ERROR;
1491 1492
}

1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
static int
testConnectAuthenticate(virConnectPtr conn,
                        virConnectAuthPtr auth)
{
    testConnPtr privconn = conn->privateData;
    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;

found_user:
    /* 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;
cleanup:
    VIR_FREE(username);
    VIR_FREE(password);
    return ret;
}
1547

1548
static virDrvOpenStatus testConnectOpen(virConnectPtr conn,
1549
                                        virConnectAuthPtr auth,
1550
                                        unsigned int flags)
1551
{
1552
    int ret;
1553

E
Eric Blake 已提交
1554 1555
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1556 1557 1558
    if (testInitialize() < 0)
        return VIR_DRV_OPEN_ERROR;

1559
    if (!conn->uri)
1560
        return VIR_DRV_OPEN_DECLINED;
1561

1562
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1563
        return VIR_DRV_OPEN_DECLINED;
1564

1565
    /* Remote driver should handle these. */
1566
    if (conn->uri->server)
1567 1568
        return VIR_DRV_OPEN_DECLINED;

1569
    /* From this point on, the connection is for us. */
1570 1571 1572
    if (!conn->uri->path
        || conn->uri->path[0] == '\0'
        || (conn->uri->path[0] == '/' && conn->uri->path[1] == '\0')) {
1573 1574
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("testOpen: supply a path or use test:///default"));
1575 1576
        return VIR_DRV_OPEN_ERROR;
    }
1577

1578
    if (STREQ(conn->uri->path, "/default"))
1579 1580
        ret = testOpenDefault(conn);
    else
1581
        ret = testOpenFromFile(conn,
1582
                               conn->uri->path);
1583

1584 1585 1586
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

1587 1588 1589 1590
    /* Fake authentication. */
    if (testConnectAuthenticate(conn, auth) < 0)
        return VIR_DRV_OPEN_ERROR;

1591
    return VIR_DRV_OPEN_SUCCESS;
1592 1593
}

1594
static int testConnectClose(virConnectPtr conn)
1595
{
1596
    testConnPtr privconn = conn->privateData;
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608

    if (testInitialize() < 0)
        return -1;

    if (privconn == &defaultConn) {
        virMutexLock(&defaultLock);
        if (--defaultConnections) {
            virMutexUnlock(&defaultLock);
            return 0;
        }
    }

1609
    testDriverLock(privconn);
1610
    virObjectUnref(privconn->caps);
1611
    virObjectUnref(privconn->xmlopt);
1612
    virObjectUnref(privconn->domains);
D
Daniel P. Berrange 已提交
1613
    virNodeDeviceObjListFree(&privconn->devs);
1614
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1615
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1616
    virStoragePoolObjListFree(&privconn->pools);
1617
    virObjectEventStateFree(privconn->eventState);
E
Eric Blake 已提交
1618
    VIR_FREE(privconn->path);
1619

1620
    testDriverUnlock(privconn);
1621
    virMutexDestroy(&privconn->lock);
1622

1623 1624 1625 1626
    if (privconn == &defaultConn)
        virMutexUnlock(&defaultLock);
    else
        VIR_FREE(privconn);
1627
    conn->privateData = NULL;
1628
    return 0;
1629 1630
}

1631 1632
static int testConnectGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                                 unsigned long *hvVer)
1633
{
1634
    *hvVer = 2;
1635
    return 0;
1636 1637
}

1638 1639 1640 1641 1642 1643
static char *testConnectGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return virGetHostname();
}


1644
static int testConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
1645 1646 1647 1648
{
    return 1;
}

1649
static int testConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
1650 1651 1652 1653
{
    return 0;
}

1654
static int testConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
1655 1656 1657 1658
{
    return 1;
}

1659 1660
static int testConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type ATTRIBUTE_UNUSED)
1661 1662 1663 1664
{
    return 32;
}

1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
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;
}

1680 1681
static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1682
{
1683
    testConnPtr privconn = conn->privateData;
1684
    testDriverLock(privconn);
1685
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1686
    testDriverUnlock(privconn);
1687
    return 0;
1688 1689
}

1690
static char *testConnectGetCapabilities(virConnectPtr conn)
1691
{
1692
    testConnPtr privconn = conn->privateData;
1693
    char *xml;
1694
    testDriverLock(privconn);
1695
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL)
1696
        virReportOOMError();
1697
    testDriverUnlock(privconn);
1698
    return xml;
1699 1700
}

1701
static int testConnectNumOfDomains(virConnectPtr conn)
1702
{
1703
    testConnPtr privconn = conn->privateData;
1704
    int count;
1705

1706
    testDriverLock(privconn);
1707
    count = virDomainObjListNumOfDomains(privconn->domains, true, NULL, NULL);
1708
    testDriverUnlock(privconn);
1709

1710
    return count;
1711 1712
}

1713 1714 1715 1716 1717 1718 1719
static int testDomainIsActive(virDomainPtr dom)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
1720
    obj = virDomainObjListFindByUUID(privconn->domains, dom->uuid);
1721 1722
    testDriverUnlock(privconn);
    if (!obj) {
1723
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1724 1725 1726 1727 1728 1729
        goto cleanup;
    }
    ret = virDomainObjIsActive(obj);

cleanup:
    if (obj)
1730
        virObjectUnlock(obj);
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
    return ret;
}

static int testDomainIsPersistent(virDomainPtr dom)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
1741
    obj = virDomainObjListFindByUUID(privconn->domains, dom->uuid);
1742 1743
    testDriverUnlock(privconn);
    if (!obj) {
1744
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1745 1746 1747 1748 1749 1750
        goto cleanup;
    }
    ret = obj->persistent;

cleanup:
    if (obj)
1751
        virObjectUnlock(obj);
1752 1753 1754
    return ret;
}

1755 1756 1757 1758 1759
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

1760
static virDomainPtr
1761
testDomainCreateXML(virConnectPtr conn, const char *xml,
1762
                      unsigned int flags)
1763
{
1764
    testConnPtr privconn = conn->privateData;
1765
    virDomainPtr ret = NULL;
1766
    virDomainDefPtr def;
1767
    virDomainObjPtr dom = NULL;
1768
    virObjectEventPtr event = NULL;
1769

1770 1771
    virCheckFlags(0, NULL);

1772
    testDriverLock(privconn);
1773
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
1774
                                       1 << VIR_DOMAIN_VIRT_TEST,
1775
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1776
        goto cleanup;
1777

1778
    if (testDomainGenerateIfnames(def) < 0)
1779
        goto cleanup;
1780
    if (!(dom = virDomainObjListAdd(privconn->domains,
1781
                                    def,
1782
                                    privconn->xmlopt,
1783 1784
                                    VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                    NULL)))
1785 1786
        goto cleanup;
    def = NULL;
1787

1788
    if (testDomainStartState(privconn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0)
1789
        goto cleanup;
1790

1791
    event = virDomainEventLifecycleNewFromObj(dom,
1792 1793 1794
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1795
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1796
    if (ret)
1797
        ret->id = dom->def->id;
1798 1799

cleanup:
1800
    if (dom)
1801
        virObjectUnlock(dom);
1802
    if (event)
1803
        testObjectEventQueue(privconn, event);
1804
    virDomainDefFree(def);
1805
    testDriverUnlock(privconn);
1806
    return ret;
1807 1808 1809
}


1810
static virDomainPtr testDomainLookupByID(virConnectPtr conn,
1811
                                         int id)
1812
{
1813
    testConnPtr privconn = conn->privateData;
1814 1815
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1816

1817
    testDriverLock(privconn);
1818
    dom = virDomainObjListFindByID(privconn->domains, id);
1819 1820 1821
    testDriverUnlock(privconn);

    if (dom == NULL) {
1822
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1823
        goto cleanup;
1824 1825
    }

1826
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1827 1828 1829 1830
    if (ret)
        ret->id = dom->def->id;

cleanup:
1831
    if (dom)
1832
        virObjectUnlock(dom);
1833
    return ret;
1834 1835
}

1836
static virDomainPtr testDomainLookupByUUID(virConnectPtr conn,
1837
                                           const unsigned char *uuid)
1838
{
1839
    testConnPtr privconn = conn->privateData;
1840
    virDomainPtr ret = NULL;
1841
    virDomainObjPtr dom;
1842

1843
    testDriverLock(privconn);
1844
    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
1845 1846 1847
    testDriverUnlock(privconn);

    if (dom == NULL) {
1848
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1849
        goto cleanup;
1850
    }
1851

1852
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1853 1854 1855 1856
    if (ret)
        ret->id = dom->def->id;

cleanup:
1857
    if (dom)
1858
        virObjectUnlock(dom);
1859
    return ret;
1860 1861
}

1862
static virDomainPtr testDomainLookupByName(virConnectPtr conn,
1863
                                           const char *name)
1864
{
1865
    testConnPtr privconn = conn->privateData;
1866 1867
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1868

1869
    testDriverLock(privconn);
1870
    dom = virDomainObjListFindByName(privconn->domains, name);
1871 1872 1873
    testDriverUnlock(privconn);

    if (dom == NULL) {
1874
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1875
        goto cleanup;
1876
    }
1877

1878
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1879 1880 1881 1882
    if (ret)
        ret->id = dom->def->id;

cleanup:
1883
    if (dom)
1884
        virObjectUnlock(dom);
1885
    return ret;
1886 1887
}

1888 1889 1890
static int testConnectListDomains(virConnectPtr conn,
                                  int *ids,
                                  int maxids)
1891
{
1892
    testConnPtr privconn = conn->privateData;
1893
    int n;
1894

1895
    testDriverLock(privconn);
1896
    n = virDomainObjListGetActiveIDs(privconn->domains, ids, maxids, NULL, NULL);
1897
    testDriverUnlock(privconn);
1898

1899
    return n;
1900 1901
}

1902
static int testDomainDestroy(virDomainPtr domain)
1903
{
1904 1905
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1906
    virObjectEventPtr event = NULL;
1907
    int ret = -1;
1908

1909
    testDriverLock(privconn);
1910 1911
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1912 1913

    if (privdom == NULL) {
1914
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1915
        goto cleanup;
1916
    }
1917

J
Jiri Denemark 已提交
1918
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1919
    event = virDomainEventLifecycleNewFromObj(privdom,
1920 1921
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1922

1923
    if (!privdom->persistent) {
1924 1925
        virDomainObjListRemove(privconn->domains,
                               privdom);
1926
        privdom = NULL;
1927
    }
1928 1929 1930

    ret = 0;
cleanup:
1931
    if (privdom)
1932
        virObjectUnlock(privdom);
1933
    if (event)
1934
        testObjectEventQueue(privconn, event);
1935
    testDriverUnlock(privconn);
1936
    return ret;
1937 1938
}

1939
static int testDomainResume(virDomainPtr domain)
1940
{
1941 1942
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1943
    virObjectEventPtr event = NULL;
1944
    int ret = -1;
1945

1946
    testDriverLock(privconn);
1947 1948
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1949
    testDriverUnlock(privconn);
1950 1951

    if (privdom == NULL) {
1952
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1953
        goto cleanup;
1954
    }
1955

J
Jiri Denemark 已提交
1956
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_PAUSED) {
1957 1958
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
                       domain->name);
1959
        goto cleanup;
1960
    }
1961

J
Jiri Denemark 已提交
1962 1963
    virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                         VIR_DOMAIN_RUNNING_UNPAUSED);
1964
    event = virDomainEventLifecycleNewFromObj(privdom,
1965 1966
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1967 1968 1969
    ret = 0;

cleanup:
1970
    if (privdom)
1971
        virObjectUnlock(privdom);
1972 1973
    if (event) {
        testDriverLock(privconn);
1974
        testObjectEventQueue(privconn, event);
1975 1976
        testDriverUnlock(privconn);
    }
1977
    return ret;
1978 1979
}

1980
static int testDomainSuspend(virDomainPtr domain)
1981
{
1982 1983
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1984
    virObjectEventPtr event = NULL;
1985
    int ret = -1;
J
Jiri Denemark 已提交
1986
    int state;
1987

1988
    testDriverLock(privconn);
1989 1990
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1991
    testDriverUnlock(privconn);
1992 1993

    if (privdom == NULL) {
1994
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1995
        goto cleanup;
1996
    }
1997

J
Jiri Denemark 已提交
1998 1999
    state = virDomainObjGetState(privdom, NULL);
    if (state == VIR_DOMAIN_SHUTOFF || state == VIR_DOMAIN_PAUSED) {
2000 2001
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
                       domain->name);
2002
        goto cleanup;
2003
    }
2004

J
Jiri Denemark 已提交
2005
    virDomainObjSetState(privdom, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
2006
    event = virDomainEventLifecycleNewFromObj(privdom,
2007 2008
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
2009 2010 2011
    ret = 0;

cleanup:
2012
    if (privdom)
2013
        virObjectUnlock(privdom);
2014 2015 2016

    if (event) {
        testDriverLock(privconn);
2017
        testObjectEventQueue(privconn, event);
2018 2019
        testDriverUnlock(privconn);
    }
2020
    return ret;
2021 2022
}

2023
static int testDomainShutdownFlags(virDomainPtr domain,
2024
                                   unsigned int flags)
2025
{
2026 2027
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2028
    virObjectEventPtr event = NULL;
2029
    int ret = -1;
2030

2031 2032
    virCheckFlags(0, -1);

2033
    testDriverLock(privconn);
2034 2035
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2036 2037

    if (privdom == NULL) {
2038
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2039
        goto cleanup;
2040
    }
2041

J
Jiri Denemark 已提交
2042
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
2043 2044
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain '%s' not running"), domain->name);
2045
        goto cleanup;
2046
    }
2047

J
Jiri Denemark 已提交
2048
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
2049
    event = virDomainEventLifecycleNewFromObj(privdom,
2050 2051
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
2052

2053
    if (!privdom->persistent) {
2054 2055
        virDomainObjListRemove(privconn->domains,
                               privdom);
2056 2057
        privdom = NULL;
    }
2058

2059
    ret = 0;
2060
cleanup:
2061
    if (privdom)
2062
        virObjectUnlock(privdom);
2063
    if (event)
2064
        testObjectEventQueue(privconn, event);
2065
    testDriverUnlock(privconn);
2066
    return ret;
2067 2068
}

2069
static int testDomainShutdown(virDomainPtr domain)
2070
{
2071
    return testDomainShutdownFlags(domain, 0);
2072 2073
}

2074
/* Similar behaviour as shutdown */
2075
static int testDomainReboot(virDomainPtr domain,
2076
                            unsigned int action ATTRIBUTE_UNUSED)
2077
{
2078 2079
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2080
    virObjectEventPtr event = NULL;
2081
    int ret = -1;
2082

2083
    testDriverLock(privconn);
2084 2085
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2086 2087

    if (privdom == NULL) {
2088
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2089
        goto cleanup;
2090
    }
2091

J
Jiri Denemark 已提交
2092 2093 2094
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

2095 2096
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
J
Jiri Denemark 已提交
2097 2098
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
2099 2100
        break;

2101
    case VIR_DOMAIN_LIFECYCLE_RESTART:
J
Jiri Denemark 已提交
2102 2103
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
2104 2105
        break;

2106
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
J
Jiri Denemark 已提交
2107 2108
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
2109 2110
        break;

2111
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
J
Jiri Denemark 已提交
2112 2113
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
2114
        break;
2115

2116
    default:
J
Jiri Denemark 已提交
2117 2118
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
2119 2120
        break;
    }
2121

J
Jiri Denemark 已提交
2122 2123
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
2124
        event = virDomainEventLifecycleNewFromObj(privdom,
2125 2126
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
2127

2128
        if (!privdom->persistent) {
2129 2130
            virDomainObjListRemove(privconn->domains,
                                   privdom);
2131 2132
            privdom = NULL;
        }
2133 2134
    }

2135 2136
    ret = 0;
cleanup:
2137
    if (privdom)
2138
        virObjectUnlock(privdom);
2139
    if (event)
2140
        testObjectEventQueue(privconn, event);
2141
    testDriverUnlock(privconn);
2142
    return ret;
2143 2144
}

2145
static int testDomainGetInfo(virDomainPtr domain,
2146
                             virDomainInfoPtr info)
2147
{
2148
    testConnPtr privconn = domain->conn->privateData;
2149
    struct timeval tv;
2150
    virDomainObjPtr privdom;
2151
    int ret = -1;
2152

2153
    testDriverLock(privconn);
2154 2155
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2156
    testDriverUnlock(privconn);
2157 2158

    if (privdom == NULL) {
2159
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2160
        goto cleanup;
2161
    }
2162 2163

    if (gettimeofday(&tv, NULL) < 0) {
2164 2165
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("getting time of day"));
2166
        goto cleanup;
2167 2168
    }

J
Jiri Denemark 已提交
2169
    info->state = virDomainObjGetState(privdom, NULL);
2170 2171
    info->memory = privdom->def->mem.cur_balloon;
    info->maxMem = privdom->def->mem.max_balloon;
2172 2173
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
2174 2175 2176
    ret = 0;

cleanup:
2177
    if (privdom)
2178
        virObjectUnlock(privdom);
2179
    return ret;
2180 2181
}

2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194
static int
testDomainGetState(virDomainPtr domain,
                   int *state,
                   int *reason,
                   unsigned int flags)
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    int ret = -1;

    virCheckFlags(0, -1);

    testDriverLock(privconn);
2195 2196
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2197 2198 2199
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2200
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2201 2202 2203
        goto cleanup;
    }

J
Jiri Denemark 已提交
2204
    *state = virDomainObjGetState(privdom, reason);
2205 2206 2207 2208
    ret = 0;

cleanup:
    if (privdom)
2209
        virObjectUnlock(privdom);
2210 2211 2212
    return ret;
}

2213 2214
#define TEST_SAVE_MAGIC "TestGuestMagic"

2215 2216 2217
static int
testDomainSaveFlags(virDomainPtr domain, const char *path,
                    const char *dxml, unsigned int flags)
2218
{
2219
    testConnPtr privconn = domain->conn->privateData;
2220 2221 2222
    char *xml = NULL;
    int fd = -1;
    int len;
2223
    virDomainObjPtr privdom;
2224
    virObjectEventPtr event = NULL;
2225
    int ret = -1;
2226

2227 2228
    virCheckFlags(0, -1);
    if (dxml) {
2229 2230
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
2231 2232 2233
        return -1;
    }

2234
    testDriverLock(privconn);
2235 2236
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2237 2238

    if (privdom == NULL) {
2239
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2240
        goto cleanup;
2241
    }
2242

2243
    xml = virDomainDefFormat(privdom->def,
C
Cole Robinson 已提交
2244 2245
                             VIR_DOMAIN_XML_SECURE);

2246
    if (xml == NULL) {
2247
        virReportSystemError(errno,
2248 2249
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
2250
        goto cleanup;
2251
    }
2252 2253

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
2254
        virReportSystemError(errno,
2255 2256
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
2257
        goto cleanup;
2258
    }
2259
    len = strlen(xml);
2260
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
2261
        virReportSystemError(errno,
2262 2263
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2264
        goto cleanup;
2265
    }
2266
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
2267
        virReportSystemError(errno,
2268 2269
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2270
        goto cleanup;
2271
    }
2272
    if (safewrite(fd, xml, len) < 0) {
2273
        virReportSystemError(errno,
2274 2275
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2276
        goto cleanup;
2277
    }
2278

2279
    if (VIR_CLOSE(fd) < 0) {
2280
        virReportSystemError(errno,
2281 2282
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2283
        goto cleanup;
2284
    }
2285 2286
    fd = -1;

J
Jiri Denemark 已提交
2287
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
2288
    event = virDomainEventLifecycleNewFromObj(privdom,
2289 2290
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
2291

2292
    if (!privdom->persistent) {
2293 2294
        virDomainObjListRemove(privconn->domains,
                               privdom);
2295
        privdom = NULL;
2296
    }
2297

2298
    ret = 0;
2299 2300 2301 2302 2303 2304 2305
cleanup:
    VIR_FREE(xml);

    /* Don't report failure in close or unlink, because
     * in either case we're already in a failure scenario
     * and have reported a earlier error */
    if (ret != 0) {
2306
        VIR_FORCE_CLOSE(fd);
2307 2308
        unlink(path);
    }
2309
    if (privdom)
2310
        virObjectUnlock(privdom);
2311
    if (event)
2312
        testObjectEventQueue(privconn, event);
2313
    testDriverUnlock(privconn);
2314
    return ret;
2315 2316
}

2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
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)
2329
{
2330
    testConnPtr privconn = conn->privateData;
2331
    char *xml = NULL;
2332
    char magic[15];
2333 2334 2335
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
2336
    virDomainObjPtr dom = NULL;
2337
    virObjectEventPtr event = NULL;
2338
    int ret = -1;
2339

2340 2341
    virCheckFlags(0, -1);
    if (dxml) {
2342 2343
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
2344 2345 2346
        return -1;
    }

2347 2348
    testDriverLock(privconn);

2349
    if ((fd = open(path, O_RDONLY)) < 0) {
2350
        virReportSystemError(errno,
2351 2352
                             _("cannot read domain image '%s'"),
                             path);
2353
        goto cleanup;
2354
    }
2355
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
2356
        virReportSystemError(errno,
2357 2358
                             _("incomplete save header in '%s'"),
                             path);
2359
        goto cleanup;
2360
    }
2361
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
2362 2363
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("mismatched header magic"));
2364
        goto cleanup;
2365
    }
2366
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
2367
        virReportSystemError(errno,
2368 2369
                             _("failed to read metadata length in '%s'"),
                             path);
2370
        goto cleanup;
2371 2372
    }
    if (len < 1 || len > 8192) {
2373 2374
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("length of metadata out of range"));
2375
        goto cleanup;
2376
    }
2377
    if (VIR_ALLOC_N(xml, len+1) < 0)
2378
        goto cleanup;
2379
    if (saferead(fd, xml, len) != len) {
2380
        virReportSystemError(errno,
2381
                             _("incomplete metadata in '%s'"), path);
2382
        goto cleanup;
2383 2384
    }
    xml[len] = '\0';
2385

2386 2387
    def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
                                  1 << VIR_DOMAIN_VIRT_TEST,
2388
                                  VIR_DOMAIN_XML_INACTIVE);
2389
    if (!def)
2390
        goto cleanup;
2391

2392
    if (testDomainGenerateIfnames(def) < 0)
2393
        goto cleanup;
2394
    if (!(dom = virDomainObjListAdd(privconn->domains,
2395
                                    def,
2396
                                    privconn->xmlopt,
2397 2398 2399
                                    VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
                                    VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                    NULL)))
2400 2401
        goto cleanup;
    def = NULL;
2402

2403
    if (testDomainStartState(privconn, dom, VIR_DOMAIN_RUNNING_RESTORED) < 0)
2404 2405
        goto cleanup;

2406
    event = virDomainEventLifecycleNewFromObj(dom,
2407 2408
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
2409
    ret = 0;
2410 2411 2412 2413

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
2414
    VIR_FORCE_CLOSE(fd);
2415
    if (dom)
2416
        virObjectUnlock(dom);
2417
    if (event)
2418
        testObjectEventQueue(privconn, event);
2419
    testDriverUnlock(privconn);
2420
    return ret;
2421 2422
}

2423 2424 2425 2426 2427 2428 2429
static int
testDomainRestore(virConnectPtr conn,
                  const char *path)
{
    return testDomainRestoreFlags(conn, path, NULL, 0);
}

2430 2431
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
E
Eric Blake 已提交
2432
                              unsigned int flags)
2433
{
2434
    testConnPtr privconn = domain->conn->privateData;
2435
    int fd = -1;
2436
    virDomainObjPtr privdom;
2437
    virObjectEventPtr event = NULL;
2438
    int ret = -1;
2439

E
Eric Blake 已提交
2440 2441
    virCheckFlags(VIR_DUMP_CRASH, -1);

2442
    testDriverLock(privconn);
2443 2444
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2445 2446

    if (privdom == NULL) {
2447
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2448
        goto cleanup;
2449
    }
2450 2451

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
2452
        virReportSystemError(errno,
2453 2454
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
2455
        goto cleanup;
2456
    }
2457
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
2458
        virReportSystemError(errno,
2459 2460
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
2461
        goto cleanup;
2462
    }
2463
    if (VIR_CLOSE(fd) < 0) {
2464
        virReportSystemError(errno,
2465 2466
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
2467
        goto cleanup;
2468
    }
2469

2470
    if (flags & VIR_DUMP_CRASH) {
J
Jiri Denemark 已提交
2471
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_CRASHED);
2472
        event = virDomainEventLifecycleNewFromObj(privdom,
2473 2474 2475
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
        if (!privdom->persistent) {
2476 2477
            virDomainObjListRemove(privconn->domains,
                                   privdom);
2478 2479
            privdom = NULL;
        }
2480
    }
2481

2482
    ret = 0;
2483
cleanup:
2484
    VIR_FORCE_CLOSE(fd);
2485
    if (privdom)
2486
        virObjectUnlock(privdom);
2487
    if (event)
2488
        testObjectEventQueue(privconn, event);
2489
    testDriverUnlock(privconn);
2490
    return ret;
2491 2492
}

2493
static char *testDomainGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
2494 2495 2496
    char *ret;

    ignore_value(VIR_STRDUP(ret, "linux"));
2497
    return ret;
2498 2499
}

2500
static unsigned long long testDomainGetMaxMemory(virDomainPtr domain) {
2501 2502
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2503
    unsigned long long ret = 0;
2504

2505
    testDriverLock(privconn);
2506 2507
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2508
    testDriverUnlock(privconn);
2509 2510

    if (privdom == NULL) {
2511
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2512
        goto cleanup;
2513
    }
2514

2515
    ret = privdom->def->mem.max_balloon;
2516 2517

cleanup:
2518
    if (privdom)
2519
        virObjectUnlock(privdom);
2520
    return ret;
2521 2522
}

2523 2524
static int testDomainSetMaxMemory(virDomainPtr domain,
                                  unsigned long memory)
2525
{
2526 2527
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2528
    int ret = -1;
2529

2530
    testDriverLock(privconn);
2531 2532
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2533
    testDriverUnlock(privconn);
2534 2535

    if (privdom == NULL) {
2536
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2537
        goto cleanup;
2538
    }
2539 2540

    /* XXX validate not over host memory wrt to other domains */
2541
    privdom->def->mem.max_balloon = memory;
2542 2543 2544
    ret = 0;

cleanup:
2545
    if (privdom)
2546
        virObjectUnlock(privdom);
2547
    return ret;
2548 2549
}

2550 2551
static int testDomainSetMemory(virDomainPtr domain,
                               unsigned long memory)
2552
{
2553 2554
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2555
    int ret = -1;
2556

2557
    testDriverLock(privconn);
2558 2559
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2560
    testDriverUnlock(privconn);
2561 2562

    if (privdom == NULL) {
2563
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2564
        goto cleanup;
2565
    }
2566

2567
    if (memory > privdom->def->mem.max_balloon) {
2568
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2569
        goto cleanup;
2570
    }
2571

2572
    privdom->def->mem.cur_balloon = memory;
2573 2574 2575
    ret = 0;

cleanup:
2576
    if (privdom)
2577
        virObjectUnlock(privdom);
2578
    return ret;
2579 2580
}

2581 2582
static int
testDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
C
Cole Robinson 已提交
2583
{
2584 2585 2586 2587 2588
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;

2589 2590
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2591 2592 2593
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    testDriverLock(privconn);
2594
    vm = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
2595 2596 2597 2598 2599
    testDriverUnlock(privconn);

    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(domain->uuid, uuidstr);
2600 2601
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
2602 2603 2604
        goto cleanup;
    }

2605
    if (virDomainLiveConfigHelperMethod(privconn->caps, privconn->xmlopt,
2606
                                        vm, &flags, &def) < 0)
2607
        goto cleanup;
2608

2609
    if (flags & VIR_DOMAIN_AFFECT_LIVE)
2610 2611 2612 2613 2614 2615
        def = vm->def;

    ret = (flags & VIR_DOMAIN_VCPU_MAXIMUM) ? def->maxvcpus : def->vcpus;

cleanup:
    if (vm)
2616
        virObjectUnlock(vm);
2617
    return ret;
C
Cole Robinson 已提交
2618 2619
}

2620 2621 2622
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
2623
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2624 2625 2626 2627 2628 2629 2630
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

static int
testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
                        unsigned int flags)
{
2631
    testConnPtr privconn = domain->conn->privateData;
2632
    virDomainObjPtr privdom = NULL;
2633
    virDomainDefPtr persistentDef;
C
Cole Robinson 已提交
2634 2635
    int ret = -1, maxvcpus;

2636 2637
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2638 2639 2640 2641
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
2642 2643 2644
    if ((flags & (VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG)) == 0 ||
        (flags & (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_LIVE)) ==
         (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_LIVE)) {
2645 2646
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
2647 2648
        return -1;
    }
2649
    if (!nrCpus || (maxvcpus = testConnectGetMaxVcpus(domain->conn, NULL)) < nrCpus) {
2650 2651
        virReportError(VIR_ERR_INVALID_ARG,
                       _("argument out of range: %d"), nrCpus);
2652 2653
        return -1;
    }
2654

2655
    testDriverLock(privconn);
2656
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
2657 2658 2659
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2660
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2661
        goto cleanup;
2662
    }
2663

2664
    if (!virDomainObjIsActive(privdom) && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
2665 2666
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot hotplug vcpus for an inactive domain"));
C
Cole Robinson 已提交
2667 2668 2669
        goto cleanup;
    }

2670 2671
    /* We allow more cpus in guest than host, but not more than the
     * domain's starting limit.  */
C
Cole Robinson 已提交
2672 2673
    if (!(flags & (VIR_DOMAIN_VCPU_MAXIMUM)) &&
        privdom->def->maxvcpus < maxvcpus)
2674
        maxvcpus = privdom->def->maxvcpus;
C
Cole Robinson 已提交
2675

C
Cole Robinson 已提交
2676
    if (nrCpus > maxvcpus) {
2677 2678 2679
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested cpu amount exceeds maximum (%d > %d)"),
                       nrCpus, maxvcpus);
2680
        goto cleanup;
2681
    }
2682

2683
    if (!(persistentDef = virDomainObjGetPersistentDef(privconn->caps,
2684
                                                       privconn->xmlopt,
2685 2686 2687
                                                       privdom)))
        goto cleanup;

2688
    switch (flags) {
2689
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_CONFIG:
2690 2691 2692
        persistentDef->maxvcpus = nrCpus;
        if (nrCpus < persistentDef->vcpus)
            persistentDef->vcpus = nrCpus;
2693 2694
        ret = 0;
        break;
2695

2696
    case VIR_DOMAIN_AFFECT_CONFIG:
2697
        persistentDef->vcpus = nrCpus;
2698 2699 2700
        ret = 0;
        break;

2701
    case VIR_DOMAIN_AFFECT_LIVE:
2702
        ret = testDomainUpdateVCPUs(privconn, privdom, nrCpus, 0);
2703 2704
        break;

2705
    case VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG:
2706
        ret = testDomainUpdateVCPUs(privconn, privdom, nrCpus, 0);
2707 2708 2709
        if (ret == 0) {
            persistentDef->vcpus = nrCpus;
        }
2710 2711
        break;
    }
2712 2713

cleanup:
2714
    if (privdom)
2715
        virObjectUnlock(privdom);
2716
    return ret;
2717 2718
}

2719
static int
2720
testDomainSetVcpus(virDomainPtr domain, unsigned int nrCpus)
2721
{
2722
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2723 2724
}

C
Cole Robinson 已提交
2725 2726 2727 2728 2729 2730 2731 2732 2733
static int testDomainGetVcpus(virDomainPtr domain,
                              virVcpuInfoPtr info,
                              int maxinfo,
                              unsigned char *cpumaps,
                              int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
2734 2735
    size_t i;
    int v, maxcpu, hostcpus;
C
Cole Robinson 已提交
2736 2737 2738 2739 2740
    int ret = -1;
    struct timeval tv;
    unsigned long long statbase;

    testDriverLock(privconn);
2741
    privdom = virDomainObjListFindByName(privconn->domains, domain->name);
C
Cole Robinson 已提交
2742 2743 2744
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2745
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2746 2747 2748 2749
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2750
        virReportError(VIR_ERR_OPERATION_INVALID,
2751
                       "%s", _("cannot list vcpus for an inactive domain"));
C
Cole Robinson 已提交
2752 2753 2754 2755 2756 2757
        goto cleanup;
    }

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
2758
        virReportSystemError(errno,
C
Cole Robinson 已提交
2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778
                             "%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;

    /* Clamp to actual number of vcpus */
    if (maxinfo > privdom->def->vcpus)
        maxinfo = privdom->def->vcpus;

    /* Populate virVcpuInfo structures */
    if (info != NULL) {
        memset(info, 0, sizeof(*info) * maxinfo);

2779
        for (i = 0; i < maxinfo; i++) {
C
Cole Robinson 已提交
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
            virVcpuInfo privinfo = privdomdata->vcpu_infos[i];

            info[i].number = privinfo.number;
            info[i].state = privinfo.state;
            info[i].cpu = privinfo.cpu;

            /* Fake an increasing cpu time value */
            info[i].cpuTime = statbase / 10;
        }
    }

    /* Populate cpumaps */
    if (cpumaps != NULL) {
        int privmaplen = VIR_CPU_MAPLEN(hostcpus);
        memset(cpumaps, 0, maplen * maxinfo);

2796
        for (v = 0; v < maxinfo; v++) {
C
Cole Robinson 已提交
2797 2798
            unsigned char *cpumap = VIR_GET_CPUMAP(cpumaps, maplen, v);

2799
            for (i = 0; i < maxcpu; i++) {
C
Cole Robinson 已提交
2800 2801 2802 2803 2804 2805 2806 2807 2808 2809
                if (VIR_CPU_USABLE(privdomdata->cpumaps, privmaplen, v, i)) {
                    VIR_USE_CPU(cpumap, i);
                }
            }
        }
    }

    ret = maxinfo;
cleanup:
    if (privdom)
2810
        virObjectUnlock(privdom);
C
Cole Robinson 已提交
2811 2812 2813
    return ret;
}

C
Cole Robinson 已提交
2814 2815 2816 2817 2818 2819 2820 2821 2822
static int testDomainPinVcpu(virDomainPtr domain,
                             unsigned int vcpu,
                             unsigned char *cpumap,
                             int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
    unsigned char *privcpumap;
2823 2824
    size_t i;
    int maxcpu, hostcpus, privmaplen;
C
Cole Robinson 已提交
2825 2826 2827
    int ret = -1;

    testDriverLock(privconn);
2828
    privdom = virDomainObjListFindByName(privconn->domains, domain->name);
C
Cole Robinson 已提交
2829 2830 2831
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2832
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2833 2834 2835 2836
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2837
        virReportError(VIR_ERR_OPERATION_INVALID,
2838
                       "%s", _("cannot pin vcpus on an inactive domain"));
C
Cole Robinson 已提交
2839 2840 2841 2842
        goto cleanup;
    }

    if (vcpu > privdom->def->vcpus) {
2843 2844
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("requested vcpu is higher than allocated vcpus"));
C
Cole Robinson 已提交
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858
        goto cleanup;
    }

    privdomdata = privdom->privateData;
    hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
    privmaplen = VIR_CPU_MAPLEN(hostcpus);

    maxcpu = maplen * 8;
    if (maxcpu > hostcpus)
        maxcpu = hostcpus;

    privcpumap = VIR_GET_CPUMAP(privdomdata->cpumaps, privmaplen, vcpu);
    memset(privcpumap, 0, privmaplen);

2859
    for (i = 0; i < maxcpu; i++) {
C
Cole Robinson 已提交
2860 2861 2862 2863 2864 2865 2866 2867
        if (VIR_CPU_USABLE(cpumap, maplen, 0, i)) {
            VIR_USE_CPU(privcpumap, i);
        }
    }

    ret = 0;
cleanup:
    if (privdom)
2868
        virObjectUnlock(privdom);
C
Cole Robinson 已提交
2869 2870 2871
    return ret;
}

2872
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2873
{
2874
    testConnPtr privconn = domain->conn->privateData;
2875
    virDomainDefPtr def;
2876
    virDomainObjPtr privdom;
2877 2878
    char *ret = NULL;

2879 2880
    /* Flags checked by virDomainDefFormat */

2881
    testDriverLock(privconn);
2882 2883
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2884 2885 2886
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2887
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2888
        goto cleanup;
2889
    }
2890

2891 2892
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2893

2894
    ret = virDomainDefFormat(def,
2895 2896 2897
                             flags);

cleanup:
2898
    if (privdom)
2899
        virObjectUnlock(privdom);
2900
    return ret;
2901
}
2902

2903
static int testConnectNumOfDefinedDomains(virConnectPtr conn) {
2904
    testConnPtr privconn = conn->privateData;
2905
    int count;
2906

2907
    testDriverLock(privconn);
2908
    count = virDomainObjListNumOfDomains(privconn->domains, false, NULL, NULL);
2909
    testDriverUnlock(privconn);
2910

2911
    return count;
2912 2913
}

2914 2915 2916
static int testConnectListDefinedDomains(virConnectPtr conn,
                                         char **const names,
                                         int maxnames) {
2917

2918
    testConnPtr privconn = conn->privateData;
2919
    int n;
2920

2921
    testDriverLock(privconn);
2922
    memset(names, 0, sizeof(*names)*maxnames);
2923 2924
    n = virDomainObjListGetInactiveNames(privconn->domains, names, maxnames,
                                         NULL, NULL);
2925
    testDriverUnlock(privconn);
2926

2927
    return n;
2928 2929
}

2930
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
2931
                                        const char *xml) {
2932
    testConnPtr privconn = conn->privateData;
2933
    virDomainPtr ret = NULL;
2934
    virDomainDefPtr def;
2935
    virDomainObjPtr dom = NULL;
2936
    virObjectEventPtr event = NULL;
2937
    virDomainDefPtr oldDef = NULL;
2938

2939
    testDriverLock(privconn);
2940 2941
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
                                       1 << VIR_DOMAIN_VIRT_TEST,
2942
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
2943
        goto cleanup;
2944

2945
    if (testDomainGenerateIfnames(def) < 0)
2946
        goto cleanup;
2947
    if (!(dom = virDomainObjListAdd(privconn->domains,
2948
                                    def,
2949
                                    privconn->xmlopt,
2950 2951
                                    0,
                                    &oldDef)))
2952
        goto cleanup;
2953
    def = NULL;
2954
    dom->persistent = 1;
2955

2956
    event = virDomainEventLifecycleNewFromObj(dom,
2957
                                     VIR_DOMAIN_EVENT_DEFINED,
2958
                                     !oldDef ?
2959 2960
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2961

2962
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2963
    if (ret)
2964
        ret->id = dom->def->id;
2965 2966 2967

cleanup:
    virDomainDefFree(def);
2968
    virDomainDefFree(oldDef);
2969
    if (dom)
2970
        virObjectUnlock(dom);
2971
    if (event)
2972
        testObjectEventQueue(privconn, event);
2973
    testDriverUnlock(privconn);
2974
    return ret;
2975 2976
}

2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
static char *testDomainGetMetadata(virDomainPtr dom,
                                   int type,
                                   const char *uri,
                                   unsigned int flags)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr privdom;
    char *ret = NULL;

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, NULL);

    testDriverLock(privconn);
    privdom = virDomainObjListFindByName(privconn->domains,
                                         dom->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto cleanup;
    }

    ret = virDomainObjGetMetadata(privdom, type, uri, privconn->caps,
                                  privconn->xmlopt, flags);

cleanup:
    if (privdom)
        virObjectUnlock(privdom);
    return ret;
}

static int testDomainSetMetadata(virDomainPtr dom,
                                 int type,
                                 const char *metadata,
                                 const char *key,
                                 const char *uri,
                                 unsigned int flags)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr privdom;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG, -1);

    testDriverLock(privconn);
    privdom = virDomainObjListFindByName(privconn->domains,
                                         dom->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto cleanup;
    }

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

cleanup:
    if (privdom)
        virObjectUnlock(privdom);
    return ret;
}


3043 3044 3045
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
3046
    testConnPtr privconn = conn->privateData;
3047 3048
    int cell;
    size_t i;
3049
    int ret = -1;
3050

3051
    testDriverLock(privconn);
3052
    if (startCell > privconn->numCells) {
3053 3054
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("Range exceeds available cells"));
3055
        goto cleanup;
3056 3057
    }

3058 3059 3060 3061
    for (cell = startCell, i = 0;
         (cell < privconn->numCells && i < maxCells);
         ++cell, ++i) {
        freemems[i] = privconn->cells[cell].mem;
3062
    }
3063
    ret = i;
3064

3065
cleanup:
3066
    testDriverUnlock(privconn);
3067
    return ret;
3068 3069 3070
}


3071
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags) {
3072 3073
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
3074
    virObjectEventPtr event = NULL;
3075
    int ret = -1;
3076

3077 3078
    virCheckFlags(0, -1);

3079
    testDriverLock(privconn);
3080 3081
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3082 3083

    if (privdom == NULL) {
3084
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3085
        goto cleanup;
3086
    }
3087

J
Jiri Denemark 已提交
3088
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
3089 3090
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Domain '%s' is already running"), domain->name);
3091
        goto cleanup;
3092 3093
    }

3094
    if (testDomainStartState(privconn, privdom,
J
Jiri Denemark 已提交
3095
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
3096 3097 3098
        goto cleanup;
    domain->id = privdom->def->id;

3099
    event = virDomainEventLifecycleNewFromObj(privdom,
3100 3101
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
3102
    ret = 0;
3103

3104
cleanup:
3105
    if (privdom)
3106
        virObjectUnlock(privdom);
3107
    if (event)
3108
        testObjectEventQueue(privconn, event);
3109
    testDriverUnlock(privconn);
3110
    return ret;
3111 3112
}

3113 3114 3115 3116
static int testDomainCreate(virDomainPtr domain) {
    return testDomainCreateWithFlags(domain, 0);
}

3117 3118 3119
static int testDomainUndefineFlags(virDomainPtr domain,
                                   unsigned int flags)
{
3120 3121
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
3122
    virObjectEventPtr event = NULL;
3123
    int nsnapshots;
3124
    int ret = -1;
3125

3126 3127
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
3128

3129
    testDriverLock(privconn);
3130 3131
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3132 3133

    if (privdom == NULL) {
3134
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3135
        goto cleanup;
3136
    }
3137

C
Cole Robinson 已提交
3138 3139 3140 3141 3142 3143 3144 3145
    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;
    }

3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
    /* 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. */
    }

3164
    event = virDomainEventLifecycleNewFromObj(privdom,
3165 3166
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
C
Cole Robinson 已提交
3167 3168
    privdom->hasManagedSave = false;

3169 3170
    if (virDomainObjIsActive(privdom)) {
        privdom->persistent = 0;
3171
    } else {
3172 3173
        virDomainObjListRemove(privconn->domains,
                               privdom);
3174 3175 3176
        privdom = NULL;
    }

3177
    ret = 0;
3178

3179
cleanup:
3180
    if (privdom)
3181
        virObjectUnlock(privdom);
3182
    if (event)
3183
        testObjectEventQueue(privconn, event);
3184
    testDriverUnlock(privconn);
3185
    return ret;
3186 3187
}

3188 3189 3190 3191 3192
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

3193 3194 3195
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
3196 3197
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
3198
    int ret = -1;
3199

3200
    testDriverLock(privconn);
3201 3202
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3203
    testDriverUnlock(privconn);
3204 3205

    if (privdom == NULL) {
3206
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3207
        goto cleanup;
3208 3209
    }

3210
    *autostart = privdom->autostart;
3211 3212 3213
    ret = 0;

cleanup:
3214
    if (privdom)
3215
        virObjectUnlock(privdom);
3216
    return ret;
3217 3218 3219 3220 3221 3222
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
3223 3224
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
3225
    int ret = -1;
3226

3227
    testDriverLock(privconn);
3228 3229
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3230
    testDriverUnlock(privconn);
3231 3232

    if (privdom == NULL) {
3233
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3234
        goto cleanup;
3235 3236
    }

3237
    privdom->autostart = autostart ? 1 : 0;
3238 3239 3240
    ret = 0;

cleanup:
3241
    if (privdom)
3242
        virObjectUnlock(privdom);
3243
    return ret;
3244
}
3245

3246
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
3247 3248
                                        int *nparams)
{
3249 3250
    char *type = NULL;

3251 3252 3253
    if (nparams)
        *nparams = 1;

3254
    ignore_value(VIR_STRDUP(type, "fair"));
3255

3256 3257 3258
    return type;
}

3259
static int
3260 3261 3262 3263
testDomainGetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int *nparams,
                                      unsigned int flags)
3264
{
3265 3266
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
3267
    int ret = -1;
3268

3269 3270
    virCheckFlags(0, -1);

3271
    testDriverLock(privconn);
3272 3273
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3274
    testDriverUnlock(privconn);
3275 3276

    if (privdom == NULL) {
3277
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3278
        goto cleanup;
3279 3280
    }

3281 3282
    if (virTypedParameterAssign(params, VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, 50) < 0)
3283
        goto cleanup;
3284 3285
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
3286 3287

    *nparams = 1;
3288 3289 3290
    ret = 0;

cleanup:
3291
    if (privdom)
3292
        virObjectUnlock(privdom);
3293
    return ret;
3294
}
3295

3296
static int
3297 3298 3299
testDomainGetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int *nparams)
3300
{
3301
    return testDomainGetSchedulerParametersFlags(domain, params, nparams, 0);
3302
}
3303

3304
static int
3305 3306 3307 3308
testDomainSetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int nparams,
                                      unsigned int flags)
3309
{
3310 3311
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
3312 3313
    int ret = -1;
    size_t i;
3314

3315
    virCheckFlags(0, -1);
3316 3317 3318 3319
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_SCHEDULER_WEIGHT,
                               VIR_TYPED_PARAM_UINT,
                               NULL) < 0)
3320
        return -1;
3321

3322
    testDriverLock(privconn);
3323 3324
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3325
    testDriverUnlock(privconn);
3326 3327

    if (privdom == NULL) {
3328
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3329
        goto cleanup;
3330 3331
    }

3332
    for (i = 0; i < nparams; i++) {
3333 3334 3335
        if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_WEIGHT)) {
            /* XXX */
            /*privdom->weight = params[i].value.ui;*/
3336
        }
3337
    }
3338

3339 3340 3341
    ret = 0;

cleanup:
3342
    if (privdom)
3343
        virObjectUnlock(privdom);
3344
    return ret;
3345 3346
}

3347
static int
3348 3349 3350
testDomainSetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int nparams)
3351
{
3352
    return testDomainSetSchedulerParametersFlags(domain, params, nparams, 0);
3353 3354
}

3355 3356 3357 3358 3359 3360 3361 3362
static int testDomainBlockStats(virDomainPtr domain,
                                const char *path,
                                struct _virDomainBlockStats *stats)
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
3363
    int ret = -1;
3364 3365

    testDriverLock(privconn);
3366 3367
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3368 3369 3370
    testDriverUnlock(privconn);

    if (privdom == NULL) {
3371
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3372 3373 3374
        goto error;
    }

3375
    if (virDomainDiskIndexByName(privdom->def, path, false) < 0) {
3376 3377
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path: %s"), path);
3378 3379 3380 3381
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
3382
        virReportSystemError(errno,
3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397
                             "%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;
error:
    if (privdom)
3398
        virObjectUnlock(privdom);
3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409
    return ret;
}

static int testDomainInterfaceStats(virDomainPtr domain,
                                    const char *path,
                                    struct _virDomainInterfaceStats *stats)
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
3410 3411
    size_t i;
    int found = 0, ret = -1;
3412 3413

    testDriverLock(privconn);
3414 3415
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3416 3417 3418
    testDriverUnlock(privconn);

    if (privdom == NULL) {
3419
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3420 3421 3422
        goto error;
    }

3423
    for (i = 0; i < privdom->def->nnets; i++) {
3424
        if (privdom->def->nets[i]->ifname &&
3425
            STREQ(privdom->def->nets[i]->ifname, path)) {
3426 3427 3428 3429 3430 3431
            found = 1;
            break;
        }
    }

    if (!found) {
3432 3433
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path, '%s' is not a known interface"), path);
3434 3435 3436 3437
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
3438
        virReportSystemError(errno,
3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456
                             "%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;
error:
    if (privdom)
3457
        virObjectUnlock(privdom);
3458 3459 3460
    return ret;
}

3461
static virDrvOpenStatus testNetworkOpen(virConnectPtr conn,
3462
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3463 3464 3465 3466
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3467 3468 3469 3470 3471 3472 3473
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

    conn->networkPrivateData = conn->privateData;
    return VIR_DRV_OPEN_SUCCESS;
}

3474
static int testNetworkClose(virConnectPtr conn) {
3475 3476 3477 3478 3479
    conn->networkPrivateData = NULL;
    return 0;
}


3480 3481
static virNetworkPtr testNetworkLookupByUUID(virConnectPtr conn,
                                             const unsigned char *uuid)
3482
{
3483 3484
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
3485
    virNetworkPtr ret = NULL;
3486

3487 3488 3489 3490 3491
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
3492
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3493
        goto cleanup;
3494 3495
    }

3496 3497 3498
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
3499 3500
    if (net)
        virNetworkObjUnlock(net);
3501
    return ret;
3502
}
3503

3504
static virNetworkPtr testNetworkLookupByName(virConnectPtr conn,
3505
                                             const char *name)
3506
{
3507
    testConnPtr privconn = conn->privateData;
3508 3509
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
3510

3511 3512 3513 3514 3515
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
3516
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3517
        goto cleanup;
3518 3519
    }

3520 3521 3522
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
3523 3524
    if (net)
        virNetworkObjUnlock(net);
3525
    return ret;
3526 3527 3528
}


3529
static int testConnectNumOfNetworks(virConnectPtr conn) {
3530
    testConnPtr privconn = conn->privateData;
3531 3532
    int numActive = 0;
    size_t i;
3533

3534
    testDriverLock(privconn);
3535
    for (i = 0; i < privconn->networks.count; i++) {
3536
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3537
        if (virNetworkObjIsActive(privconn->networks.objs[i]))
3538
            numActive++;
3539 3540 3541
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3542

3543
    return numActive;
3544 3545
}

3546
static int testConnectListNetworks(virConnectPtr conn, char **const names, int nnames) {
3547
    testConnPtr privconn = conn->privateData;
3548 3549
    int n = 0;
    size_t i;
3550

3551
    testDriverLock(privconn);
3552
    memset(names, 0, sizeof(*names)*nnames);
3553
    for (i = 0; i < privconn->networks.count && n < nnames; i++) {
3554
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3555
        if (virNetworkObjIsActive(privconn->networks.objs[i]) &&
3556
            VIR_STRDUP(names[n++], privconn->networks.objs[i]->def->name) < 0) {
3557
            virNetworkObjUnlock(privconn->networks.objs[i]);
3558
            goto error;
3559 3560 3561 3562
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3563

3564 3565
    return n;

3566
error:
3567
    for (n = 0; n < nnames; n++)
3568
        VIR_FREE(names[n]);
3569
    testDriverUnlock(privconn);
3570
    return -1;
3571 3572
}

3573
static int testConnectNumOfDefinedNetworks(virConnectPtr conn) {
3574
    testConnPtr privconn = conn->privateData;
3575 3576
    int numInactive = 0;
    size_t i;
3577

3578
    testDriverLock(privconn);
3579
    for (i = 0; i < privconn->networks.count; i++) {
3580
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3581
        if (!virNetworkObjIsActive(privconn->networks.objs[i]))
3582
            numInactive++;
3583 3584 3585
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3586

3587
    return numInactive;
3588 3589
}

3590
static int testConnectListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
3591
    testConnPtr privconn = conn->privateData;
3592 3593
    int n = 0;
    size_t i;
3594

3595
    testDriverLock(privconn);
3596
    memset(names, 0, sizeof(*names)*nnames);
3597
    for (i = 0; i < privconn->networks.count && n < nnames; i++) {
3598
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3599
        if (!virNetworkObjIsActive(privconn->networks.objs[i]) &&
3600
            VIR_STRDUP(names[n++], privconn->networks.objs[i]->def->name) < 0) {
3601
            virNetworkObjUnlock(privconn->networks.objs[i]);
3602
            goto error;
3603 3604 3605 3606
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3607

3608 3609
    return n;

3610
error:
3611
    for (n = 0; n < nnames; n++)
3612
        VIR_FREE(names[n]);
3613
    testDriverUnlock(privconn);
3614
    return -1;
3615 3616
}

3617
static int
3618
testConnectListAllNetworks(virConnectPtr conn,
3619 3620 3621 3622 3623 3624 3625 3626 3627
                           virNetworkPtr **nets,
                           unsigned int flags)
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

    testDriverLock(privconn);
3628
    ret = virNetworkObjListExport(conn, privconn->networks, nets, NULL, flags);
3629 3630 3631 3632
    testDriverUnlock(privconn);

    return ret;
}
3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643

static int testNetworkIsActive(virNetworkPtr net)
{
    testConnPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virNetworkFindByUUID(&privconn->networks, net->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
3644
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664
        goto cleanup;
    }
    ret = virNetworkObjIsActive(obj);

cleanup:
    if (obj)
        virNetworkObjUnlock(obj);
    return ret;
}

static int testNetworkIsPersistent(virNetworkPtr net)
{
    testConnPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virNetworkFindByUUID(&privconn->networks, net->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
3665
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676
        goto cleanup;
    }
    ret = obj->persistent;

cleanup:
    if (obj)
        virNetworkObjUnlock(obj);
    return ret;
}


3677
static virNetworkPtr testNetworkCreateXML(virConnectPtr conn, const char *xml) {
3678
    testConnPtr privconn = conn->privateData;
3679
    virNetworkDefPtr def;
3680
    virNetworkObjPtr net = NULL;
3681
    virNetworkPtr ret = NULL;
3682
    virObjectEventPtr event = NULL;
3683

3684
    testDriverLock(privconn);
3685
    if ((def = virNetworkDefParseString(xml)) == NULL)
3686
        goto cleanup;
3687

3688
    if (!(net = virNetworkAssignDef(&privconn->networks, def, false)))
3689 3690
        goto cleanup;
    def = NULL;
3691
    net->active = 1;
3692

3693
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3694 3695
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3696

3697
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3698

3699 3700
cleanup:
    virNetworkDefFree(def);
3701 3702
    if (event)
        testObjectEventQueue(privconn, event);
3703 3704 3705
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3706
    return ret;
3707 3708
}

3709
static
3710
virNetworkPtr testNetworkDefineXML(virConnectPtr conn, const char *xml)
3711
{
3712
    testConnPtr privconn = conn->privateData;
3713
    virNetworkDefPtr def;
3714
    virNetworkObjPtr net = NULL;
3715
    virNetworkPtr ret = NULL;
3716
    virObjectEventPtr event = NULL;
3717

3718
    testDriverLock(privconn);
3719
    if ((def = virNetworkDefParseString(xml)) == NULL)
3720
        goto cleanup;
3721

3722
    if (!(net = virNetworkAssignDef(&privconn->networks, def, false)))
3723 3724
        goto cleanup;
    def = NULL;
3725
    net->persistent = 1;
3726

3727
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3728 3729
                                        VIR_NETWORK_EVENT_DEFINED,
                                        0);
3730

3731
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3732 3733 3734

cleanup:
    virNetworkDefFree(def);
3735 3736
    if (event)
        testObjectEventQueue(privconn, event);
3737 3738 3739
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3740
    return ret;
3741 3742 3743
}

static int testNetworkUndefine(virNetworkPtr network) {
3744 3745
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3746
    int ret = -1;
3747
    virObjectEventPtr event = NULL;
3748

3749
    testDriverLock(privconn);
3750 3751 3752 3753
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3754
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3755
        goto cleanup;
3756
    }
3757

D
Daniel P. Berrange 已提交
3758
    if (virNetworkObjIsActive(privnet)) {
3759 3760
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is still running"), network->name);
3761
        goto cleanup;
3762 3763
    }

3764
    event = virNetworkEventLifecycleNew(network->name, network->uuid,
3765 3766
                                        VIR_NETWORK_EVENT_UNDEFINED,
                                        0);
3767

3768 3769
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
3770
    privnet = NULL;
3771
    ret = 0;
3772

3773
cleanup:
3774 3775
    if (event)
        testObjectEventQueue(privconn, event);
3776 3777 3778
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3779
    return ret;
3780 3781
}

3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829
static int
testNetworkUpdate(virNetworkPtr net,
                  unsigned int command,
                  unsigned int section,
                  int parentIndex,
                  const char *xml,
                  unsigned int flags)
{
    testConnPtr privconn = net->conn->privateData;
    virNetworkObjPtr network = NULL;
    int isActive, ret = -1;

    virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE |
                  VIR_NETWORK_UPDATE_AFFECT_CONFIG,
                  -1);

    testDriverLock(privconn);

    network = virNetworkFindByUUID(&privconn->networks, net->uuid);
    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;
cleanup:
    testDriverUnlock(privconn);
    return ret;
}

3830
static int testNetworkCreate(virNetworkPtr network) {
3831 3832
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3833
    int ret = -1;
3834
    virObjectEventPtr event = NULL;
3835

3836
    testDriverLock(privconn);
3837 3838
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3839
    testDriverUnlock(privconn);
3840 3841

    if (privnet == NULL) {
3842
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3843
        goto cleanup;
3844
    }
3845

D
Daniel P. Berrange 已提交
3846
    if (virNetworkObjIsActive(privnet)) {
3847 3848
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is already running"), network->name);
3849
        goto cleanup;
3850 3851
    }

3852
    privnet->active = 1;
3853
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3854 3855
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3856
    ret = 0;
3857

3858
cleanup:
3859 3860
    if (event)
        testObjectEventQueue(privconn, event);
3861 3862
    if (privnet)
        virNetworkObjUnlock(privnet);
3863
    return ret;
3864 3865 3866
}

static int testNetworkDestroy(virNetworkPtr network) {
3867 3868
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3869
    int ret = -1;
3870
    virObjectEventPtr event = NULL;
3871

3872
    testDriverLock(privconn);
3873 3874 3875 3876
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3877
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3878
        goto cleanup;
3879
    }
3880

3881
    privnet->active = 0;
3882
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3883 3884
                                        VIR_NETWORK_EVENT_STOPPED,
                                        0);
3885 3886 3887
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
3888
        privnet = NULL;
3889
    }
3890 3891 3892
    ret = 0;

cleanup:
3893 3894
    if (event)
        testObjectEventQueue(privconn, event);
3895 3896 3897
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3898
    return ret;
3899 3900
}

3901
static char *testNetworkGetXMLDesc(virNetworkPtr network,
E
Eric Blake 已提交
3902
                                   unsigned int flags)
3903
{
3904 3905
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3906
    char *ret = NULL;
3907

E
Eric Blake 已提交
3908 3909
    virCheckFlags(0, NULL);

3910
    testDriverLock(privconn);
3911 3912
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3913
    testDriverUnlock(privconn);
3914 3915

    if (privnet == NULL) {
3916
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3917
        goto cleanup;
3918
    }
3919

3920
    ret = virNetworkDefFormat(privnet->def, flags);
3921 3922

cleanup:
3923 3924
    if (privnet)
        virNetworkObjUnlock(privnet);
3925
    return ret;
3926 3927 3928
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3929
    testConnPtr privconn = network->conn->privateData;
3930
    char *bridge = NULL;
3931 3932
    virNetworkObjPtr privnet;

3933
    testDriverLock(privconn);
3934 3935
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3936
    testDriverUnlock(privconn);
3937 3938

    if (privnet == NULL) {
3939
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3940
        goto cleanup;
3941 3942
    }

3943
    if (!(privnet->def->bridge)) {
3944 3945 3946
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("network '%s' does not have a bridge name."),
                       privnet->def->name);
3947 3948 3949
        goto cleanup;
    }

3950
    ignore_value(VIR_STRDUP(bridge, privnet->def->bridge));
3951 3952

cleanup:
3953 3954
    if (privnet)
        virNetworkObjUnlock(privnet);
3955 3956 3957 3958 3959
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
3960 3961
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3962
    int ret = -1;
3963

3964
    testDriverLock(privconn);
3965 3966
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3967
    testDriverUnlock(privconn);
3968 3969

    if (privnet == NULL) {
3970
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3971
        goto cleanup;
3972 3973
    }

3974
    *autostart = privnet->autostart;
3975 3976 3977
    ret = 0;

cleanup:
3978 3979
    if (privnet)
        virNetworkObjUnlock(privnet);
3980
    return ret;
3981 3982 3983 3984
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
3985 3986
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3987
    int ret = -1;
3988

3989
    testDriverLock(privconn);
3990 3991
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3992
    testDriverUnlock(privconn);
3993 3994

    if (privnet == NULL) {
3995
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3996
        goto cleanup;
3997 3998
    }

3999
    privnet->autostart = autostart ? 1 : 0;
4000 4001 4002
    ret = 0;

cleanup:
4003 4004
    if (privnet)
        virNetworkObjUnlock(privnet);
4005
    return ret;
4006
}
4007

C
Cole Robinson 已提交
4008

L
Laine Stump 已提交
4009 4010 4011 4012
/*
 * Physical host interface routines
 */

4013
static virDrvOpenStatus testInterfaceOpen(virConnectPtr conn,
L
Laine Stump 已提交
4014
                                          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
4015
                                          unsigned int flags)
L
Laine Stump 已提交
4016
{
E
Eric Blake 已提交
4017 4018
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

L
Laine Stump 已提交
4019 4020 4021 4022 4023 4024 4025
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

    conn->interfacePrivateData = conn->privateData;
    return VIR_DRV_OPEN_SUCCESS;
}

4026
static int testInterfaceClose(virConnectPtr conn)
L
Laine Stump 已提交
4027 4028 4029 4030 4031 4032
{
    conn->interfacePrivateData = NULL;
    return 0;
}


4033
static int testConnectNumOfInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
4034 4035
{
    testConnPtr privconn = conn->privateData;
4036 4037
    size_t i;
    int count = 0;
L
Laine Stump 已提交
4038 4039

    testDriverLock(privconn);
4040
    for (i = 0; (i < privconn->ifaces.count); i++) {
L
Laine Stump 已提交
4041
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
4042
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
4043 4044 4045 4046 4047 4048 4049 4050
            count++;
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

4051
static int testConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
L
Laine Stump 已提交
4052 4053
{
    testConnPtr privconn = conn->privateData;
4054 4055
    int n = 0;
    size_t i;
L
Laine Stump 已提交
4056 4057 4058

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
4059
    for (i = 0; (i < privconn->ifaces.count) && (n < nnames); i++) {
L
Laine Stump 已提交
4060
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
4061
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
4062
            if (VIR_STRDUP(names[n++], privconn->ifaces.objs[i]->def->name) < 0) {
L
Laine Stump 已提交
4063
                virInterfaceObjUnlock(privconn->ifaces.objs[i]);
4064
                goto error;
L
Laine Stump 已提交
4065 4066 4067 4068 4069 4070 4071 4072
            }
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);

    return n;

4073
error:
4074
    for (n = 0; n < nnames; n++)
L
Laine Stump 已提交
4075 4076 4077 4078 4079
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

4080
static int testConnectNumOfDefinedInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
4081 4082
{
    testConnPtr privconn = conn->privateData;
4083 4084
    size_t i;
    int count = 0;
L
Laine Stump 已提交
4085 4086

    testDriverLock(privconn);
4087
    for (i = 0; i < privconn->ifaces.count; i++) {
L
Laine Stump 已提交
4088
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
4089
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
4090 4091 4092 4093 4094 4095 4096 4097
            count++;
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

4098
static int testConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
L
Laine Stump 已提交
4099 4100
{
    testConnPtr privconn = conn->privateData;
4101 4102
    int n = 0;
    size_t i;
L
Laine Stump 已提交
4103 4104 4105

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
4106
    for (i = 0; (i < privconn->ifaces.count) && (n < nnames); i++) {
L
Laine Stump 已提交
4107
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
4108
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
4109
            if (VIR_STRDUP(names[n++], privconn->ifaces.objs[i]->def->name) < 0) {
L
Laine Stump 已提交
4110
                virInterfaceObjUnlock(privconn->ifaces.objs[i]);
4111
                goto error;
L
Laine Stump 已提交
4112 4113 4114 4115 4116 4117 4118 4119
            }
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);

    return n;

4120
error:
4121
    for (n = 0; n < nnames; n++)
L
Laine Stump 已提交
4122 4123 4124 4125 4126
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

4127
static virInterfacePtr testInterfaceLookupByName(virConnectPtr conn,
L
Laine Stump 已提交
4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138
                                                 const char *name)
{
    testConnPtr privconn = conn->privateData;
    virInterfaceObjPtr iface;
    virInterfacePtr ret = NULL;

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

    if (iface == NULL) {
4139
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150
        goto cleanup;
    }

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

cleanup:
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

4151
static virInterfacePtr testInterfaceLookupByMACString(virConnectPtr conn,
L
Laine Stump 已提交
4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163
                                                      const char *mac)
{
    testConnPtr privconn = conn->privateData;
    virInterfaceObjPtr iface;
    int ifacect;
    virInterfacePtr ret = NULL;

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

    if (ifacect == 0) {
4164
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4165 4166 4167 4168
        goto cleanup;
    }

    if (ifacect > 1) {
4169
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180
        goto cleanup;
    }

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

cleanup:
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

4181 4182 4183 4184 4185 4186 4187 4188 4189 4190
static int testInterfaceIsActive(virInterfacePtr iface)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virInterfaceFindByName(&privconn->ifaces, iface->name);
    testDriverUnlock(privconn);
    if (!obj) {
4191
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
4192 4193 4194 4195 4196 4197 4198 4199 4200 4201
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

cleanup:
    if (obj)
        virInterfaceObjUnlock(obj);
    return ret;
}

4202
static int testInterfaceChangeBegin(virConnectPtr conn,
E
Eric Blake 已提交
4203
                                    unsigned int flags)
4204 4205 4206 4207
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
4208 4209
    virCheckFlags(0, -1);

4210 4211
    testDriverLock(privconn);
    if (privconn->transaction_running) {
4212
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
4213
                       _("there is another transaction running."));
4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229
        goto cleanup;
    }

    privconn->transaction_running = true;

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

    ret = 0;
cleanup:
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceChangeCommit(virConnectPtr conn,
E
Eric Blake 已提交
4230
                                     unsigned int flags)
4231 4232 4233 4234
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
4235 4236
    virCheckFlags(0, -1);

4237 4238 4239
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
4240
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
4241 4242
                       _("no transaction running, "
                         "nothing to be committed."));
4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257
        goto cleanup;
    }

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

    ret = 0;

cleanup:
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
E
Eric Blake 已提交
4258
                                       unsigned int flags)
4259 4260 4261 4262
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

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

4265 4266 4267
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
4268
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
4269 4270
                       _("no transaction running, "
                         "nothing to rollback."));
4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287
        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;

cleanup:
    testDriverUnlock(privconn);
    return ret;
}
4288

L
Laine Stump 已提交
4289
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
E
Eric Blake 已提交
4290
                                     unsigned int flags)
L
Laine Stump 已提交
4291 4292 4293 4294 4295
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

E
Eric Blake 已提交
4296 4297
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
4298 4299 4300 4301 4302 4303
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);
    testDriverUnlock(privconn);

    if (privinterface == NULL) {
4304
        virReportError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
4305 4306 4307
        goto cleanup;
    }

4308
    ret = virInterfaceDefFormat(privinterface->def);
L
Laine Stump 已提交
4309 4310 4311 4312 4313 4314 4315 4316 4317

cleanup:
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    return ret;
}


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
E
Eric Blake 已提交
4318
                                              unsigned int flags)
L
Laine Stump 已提交
4319 4320 4321 4322 4323 4324
{
    testConnPtr privconn = conn->privateData;
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

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

L
Laine Stump 已提交
4327
    testDriverLock(privconn);
4328
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
4329 4330
        goto cleanup;

4331
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355
        goto cleanup;
    def = NULL;

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

cleanup:
    virInterfaceDefFree(def);
    if (iface)
        virInterfaceObjUnlock(iface);
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceUndefine(virInterfacePtr iface)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

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

    if (privinterface == NULL) {
4356
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369
        goto cleanup;
    }

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

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
E
Eric Blake 已提交
4370
                               unsigned int flags)
L
Laine Stump 已提交
4371 4372 4373 4374 4375
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
4376 4377
    virCheckFlags(0, -1);

L
Laine Stump 已提交
4378 4379 4380 4381 4382
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
4383
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4384 4385 4386 4387
        goto cleanup;
    }

    if (privinterface->active != 0) {
4388
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

cleanup:
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceDestroy(virInterfacePtr iface,
E
Eric Blake 已提交
4403
                                unsigned int flags)
L
Laine Stump 已提交
4404 4405 4406 4407 4408
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
4409 4410
    virCheckFlags(0, -1);

L
Laine Stump 已提交
4411 4412 4413 4414 4415
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
4416
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4417 4418 4419 4420
        goto cleanup;
    }

    if (privinterface->active == 0) {
4421
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

cleanup:
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}



C
Cole Robinson 已提交
4437 4438 4439 4440
/*
 * Storage Driver routines
 */

4441

4442
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
4443 4444 4445 4446 4447

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

4448
    return VIR_STRDUP(pool->configFile, "");
C
Cole Robinson 已提交
4449 4450
}

4451 4452
static virDrvOpenStatus testStorageOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
4453 4454 4455 4456
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

    conn->storagePrivateData = conn->privateData;
    return VIR_DRV_OPEN_SUCCESS;
}

static int testStorageClose(virConnectPtr conn) {
    conn->storagePrivateData = NULL;
    return 0;
}

4469

C
Cole Robinson 已提交
4470 4471 4472
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
4473
    testConnPtr privconn = conn->privateData;
4474 4475
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4476

4477
    testDriverLock(privconn);
4478
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
4479
    testDriverUnlock(privconn);
4480 4481

    if (pool == NULL) {
4482
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4483
        goto cleanup;
C
Cole Robinson 已提交
4484 4485
    }

4486 4487
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4488 4489

cleanup:
4490 4491
    if (pool)
        virStoragePoolObjUnlock(pool);
4492
    return ret;
C
Cole Robinson 已提交
4493 4494 4495 4496 4497
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
4498
    testConnPtr privconn = conn->privateData;
4499 4500
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4501

4502
    testDriverLock(privconn);
4503
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
4504
    testDriverUnlock(privconn);
4505 4506

    if (pool == NULL) {
4507
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4508
        goto cleanup;
C
Cole Robinson 已提交
4509 4510
    }

4511 4512
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4513 4514

cleanup:
4515 4516
    if (pool)
        virStoragePoolObjUnlock(pool);
4517
    return ret;
C
Cole Robinson 已提交
4518 4519 4520 4521 4522 4523 4524 4525
}

static virStoragePoolPtr
testStoragePoolLookupByVolume(virStorageVolPtr vol) {
    return testStoragePoolLookupByName(vol->conn, vol->pool);
}

static int
4526
testConnectNumOfStoragePools(virConnectPtr conn) {
4527
    testConnPtr privconn = conn->privateData;
4528 4529
    int numActive = 0;
    size_t i;
C
Cole Robinson 已提交
4530

4531
    testDriverLock(privconn);
4532
    for (i = 0; i < privconn->pools.count; i++)
C
Cole Robinson 已提交
4533 4534
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
4535
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4536 4537 4538 4539 4540

    return numActive;
}

static int
4541 4542 4543
testConnectListStoragePools(virConnectPtr conn,
                            char **const names,
                            int nnames) {
4544
    testConnPtr privconn = conn->privateData;
4545 4546
    int n = 0;
    size_t i;
C
Cole Robinson 已提交
4547

4548
    testDriverLock(privconn);
C
Cole Robinson 已提交
4549
    memset(names, 0, sizeof(*names)*nnames);
4550
    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
4551
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4552
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
4553
            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
4554
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
4555
            goto error;
4556 4557 4558 4559
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4560 4561 4562

    return n;

4563
error:
4564
    for (n = 0; n < nnames; n++)
C
Cole Robinson 已提交
4565
        VIR_FREE(names[n]);
4566
    testDriverUnlock(privconn);
4567
    return -1;
C
Cole Robinson 已提交
4568 4569 4570
}

static int
4571
testConnectNumOfDefinedStoragePools(virConnectPtr conn) {
4572
    testConnPtr privconn = conn->privateData;
4573 4574
    int numInactive = 0;
    size_t i;
C
Cole Robinson 已提交
4575

4576
    testDriverLock(privconn);
4577
    for (i = 0; i < privconn->pools.count; i++) {
4578
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4579 4580
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
4581 4582 4583
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4584 4585 4586 4587 4588

    return numInactive;
}

static int
4589 4590 4591
testConnectListDefinedStoragePools(virConnectPtr conn,
                                   char **const names,
                                   int nnames) {
4592
    testConnPtr privconn = conn->privateData;
4593 4594
    int n = 0;
    size_t i;
C
Cole Robinson 已提交
4595

4596
    testDriverLock(privconn);
C
Cole Robinson 已提交
4597
    memset(names, 0, sizeof(*names)*nnames);
4598
    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
4599
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4600
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
4601
            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
4602
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
4603
            goto error;
4604 4605 4606 4607
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4608 4609 4610

    return n;

4611
error:
4612
    for (n = 0; n < nnames; n++)
C
Cole Robinson 已提交
4613
        VIR_FREE(names[n]);
4614
    testDriverUnlock(privconn);
4615
    return -1;
C
Cole Robinson 已提交
4616 4617
}

4618
static int
4619 4620 4621
testConnectListAllStoragePools(virConnectPtr conn,
                               virStoragePoolPtr **pools,
                               unsigned int flags)
4622 4623 4624 4625 4626 4627 4628
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

    testDriverLock(privconn);
4629 4630
    ret = virStoragePoolObjListExport(conn, privconn->pools, pools,
                                      NULL, flags);
4631 4632 4633 4634
    testDriverUnlock(privconn);

    return ret;
}
C
Cole Robinson 已提交
4635

4636 4637 4638 4639 4640 4641 4642 4643 4644 4645
static int testStoragePoolIsActive(virStoragePoolPtr pool)
{
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virStoragePoolObjFindByUUID(&privconn->pools, pool->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
4646
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666
        goto cleanup;
    }
    ret = virStoragePoolObjIsActive(obj);

cleanup:
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}

static int testStoragePoolIsPersistent(virStoragePoolPtr pool)
{
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virStoragePoolObjFindByUUID(&privconn->pools, pool->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
4667
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

cleanup:
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}



C
Cole Robinson 已提交
4680
static int
4681 4682
testStoragePoolCreate(virStoragePoolPtr pool,
                      unsigned int flags)
E
Eric Blake 已提交
4683
{
4684 4685
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4686
    int ret = -1;
4687

E
Eric Blake 已提交
4688 4689
    virCheckFlags(0, -1);

4690
    testDriverLock(privconn);
4691 4692
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4693
    testDriverUnlock(privconn);
4694 4695

    if (privpool == NULL) {
4696
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4697
        goto cleanup;
4698 4699
    }

4700
    if (virStoragePoolObjIsActive(privpool)) {
4701 4702
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4703 4704
        goto cleanup;
    }
C
Cole Robinson 已提交
4705 4706

    privpool->active = 1;
4707
    ret = 0;
C
Cole Robinson 已提交
4708

4709
cleanup:
4710 4711
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4712
    return ret;
C
Cole Robinson 已提交
4713 4714 4715
}

static char *
4716 4717 4718 4719
testConnectFindStoragePoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type,
                                  const char *srcSpec,
                                  unsigned int flags)
C
Cole Robinson 已提交
4720
{
4721 4722 4723 4724
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

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

4727 4728
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4729 4730
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
4731 4732 4733 4734
        goto cleanup;
    }

    if (srcSpec) {
4735
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4736 4737 4738 4739 4740 4741 4742
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
4743
        ignore_value(VIR_STRDUP(ret, defaultPoolSourcesLogicalXML));
4744 4745 4746
        break;

    case VIR_STORAGE_POOL_NETFS:
4747
        if (!source || !source->hosts[0].name) {
4748 4749
            virReportError(VIR_ERR_INVALID_ARG,
                           "%s", _("hostname must be specified for netfs sources"));
4750 4751 4752
            goto cleanup;
        }

4753 4754
        ignore_value(virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                                 source->hosts[0].name));
4755 4756 4757
        break;

    default:
4758 4759
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4760 4761 4762 4763 4764
    }

cleanup:
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4765 4766 4767 4768
}


static virStoragePoolPtr
4769 4770 4771
testStoragePoolCreateXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4772
{
4773
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4774
    virStoragePoolDefPtr def;
4775
    virStoragePoolObjPtr pool = NULL;
4776
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4777

E
Eric Blake 已提交
4778 4779
    virCheckFlags(0, NULL);

4780
    testDriverLock(privconn);
4781
    if (!(def = virStoragePoolDefParseString(xml)))
4782
        goto cleanup;
C
Cole Robinson 已提交
4783

4784 4785 4786 4787
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4788 4789
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("storage pool already exists"));
4790
        goto cleanup;
C
Cole Robinson 已提交
4791 4792
    }

4793
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4794
        goto cleanup;
4795
    def = NULL;
C
Cole Robinson 已提交
4796

4797
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4798
        virStoragePoolObjRemove(&privconn->pools, pool);
4799 4800
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4801 4802 4803
    }
    pool->active = 1;

4804 4805
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4806 4807 4808

cleanup:
    virStoragePoolDefFree(def);
4809 4810 4811
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4812
    return ret;
C
Cole Robinson 已提交
4813 4814 4815
}

static virStoragePoolPtr
4816 4817 4818
testStoragePoolDefineXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4819
{
4820
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4821
    virStoragePoolDefPtr def;
4822
    virStoragePoolObjPtr pool = NULL;
4823
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4824

E
Eric Blake 已提交
4825 4826
    virCheckFlags(0, NULL);

4827
    testDriverLock(privconn);
4828
    if (!(def = virStoragePoolDefParseString(xml)))
4829
        goto cleanup;
C
Cole Robinson 已提交
4830 4831 4832 4833 4834

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

4835
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4836 4837
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4838

4839
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4840
        virStoragePoolObjRemove(&privconn->pools, pool);
4841 4842
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4843 4844
    }

4845 4846
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4847 4848 4849

cleanup:
    virStoragePoolDefFree(def);
4850 4851 4852
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4853
    return ret;
C
Cole Robinson 已提交
4854 4855 4856
}

static int
4857 4858 4859
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4860
    int ret = -1;
4861

4862
    testDriverLock(privconn);
4863 4864 4865 4866
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4867
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4868
        goto cleanup;
4869 4870
    }

4871
    if (virStoragePoolObjIsActive(privpool)) {
4872 4873
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4874 4875
        goto cleanup;
    }
C
Cole Robinson 已提交
4876 4877

    virStoragePoolObjRemove(&privconn->pools, privpool);
4878
    ret = 0;
C
Cole Robinson 已提交
4879

4880
cleanup:
4881 4882 4883
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4884
    return ret;
C
Cole Robinson 已提交
4885 4886 4887
}

static int
4888
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4889 4890
                     unsigned int flags)
{
4891 4892
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4893
    int ret = -1;
4894

E
Eric Blake 已提交
4895 4896
    virCheckFlags(0, -1);

4897
    testDriverLock(privconn);
4898 4899
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4900
    testDriverUnlock(privconn);
4901 4902

    if (privpool == NULL) {
4903
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4904
        goto cleanup;
4905 4906
    }

4907
    if (virStoragePoolObjIsActive(privpool)) {
4908 4909
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4910 4911
        goto cleanup;
    }
4912
    ret = 0;
C
Cole Robinson 已提交
4913

4914
cleanup:
4915 4916
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4917
    return ret;
C
Cole Robinson 已提交
4918 4919 4920 4921
}


static int
4922 4923 4924
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4925
    int ret = -1;
4926

4927
    testDriverLock(privconn);
4928 4929 4930 4931
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4932
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4933
        goto cleanup;
4934 4935 4936
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4937 4938
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4939
        goto cleanup;
4940
    }
C
Cole Robinson 已提交
4941 4942 4943

    privpool->active = 0;

4944
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4945
        virStoragePoolObjRemove(&privconn->pools, privpool);
4946 4947
        privpool = NULL;
    }
4948
    ret = 0;
C
Cole Robinson 已提交
4949

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


static int
4959
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4960 4961
                      unsigned int flags)
{
4962 4963
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4964
    int ret = -1;
4965

E
Eric Blake 已提交
4966 4967
    virCheckFlags(0, -1);

4968
    testDriverLock(privconn);
4969 4970
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4971
    testDriverUnlock(privconn);
4972 4973

    if (privpool == NULL) {
4974
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4975 4976 4977 4978
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
4979 4980
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4981
        goto cleanup;
4982 4983
    }

4984
    ret = 0;
C
Cole Robinson 已提交
4985

4986
cleanup:
4987 4988
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4989
    return ret;
C
Cole Robinson 已提交
4990 4991 4992 4993
}


static int
4994
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4995 4996
                       unsigned int flags)
{
4997 4998
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4999
    int ret = -1;
5000

E
Eric Blake 已提交
5001 5002
    virCheckFlags(0, -1);

5003
    testDriverLock(privconn);
5004 5005
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5006
    testDriverUnlock(privconn);
5007 5008

    if (privpool == NULL) {
5009
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5010
        goto cleanup;
5011 5012 5013
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5014 5015
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5016
        goto cleanup;
5017
    }
5018
    ret = 0;
C
Cole Robinson 已提交
5019

5020
cleanup:
5021 5022
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5023
    return ret;
C
Cole Robinson 已提交
5024 5025 5026 5027
}


static int
5028
testStoragePoolGetInfo(virStoragePoolPtr pool,
C
Cole Robinson 已提交
5029
                       virStoragePoolInfoPtr info) {
5030 5031
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5032
    int ret = -1;
5033

5034
    testDriverLock(privconn);
5035 5036
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5037
    testDriverUnlock(privconn);
5038 5039

    if (privpool == NULL) {
5040
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5041
        goto cleanup;
5042
    }
C
Cole Robinson 已提交
5043 5044 5045 5046 5047 5048 5049 5050 5051

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

5054
cleanup:
5055 5056
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5057
    return ret;
C
Cole Robinson 已提交
5058 5059 5060
}

static char *
5061
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
5062 5063
                          unsigned int flags)
{
5064 5065
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5066
    char *ret = NULL;
5067

E
Eric Blake 已提交
5068 5069
    virCheckFlags(0, NULL);

5070
    testDriverLock(privconn);
5071 5072
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5073
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5074

5075
    if (privpool == NULL) {
5076
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5077
        goto cleanup;
5078 5079
    }

5080
    ret = virStoragePoolDefFormat(privpool->def);
5081 5082

cleanup:
5083 5084
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5085
    return ret;
C
Cole Robinson 已提交
5086 5087 5088
}

static int
5089
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
5090
                            int *autostart) {
5091 5092
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5093
    int ret = -1;
5094

5095
    testDriverLock(privconn);
5096 5097
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5098
    testDriverUnlock(privconn);
5099 5100

    if (privpool == NULL) {
5101
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5102
        goto cleanup;
5103
    }
C
Cole Robinson 已提交
5104 5105 5106 5107 5108 5109

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

5112
cleanup:
5113 5114
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5115
    return ret;
C
Cole Robinson 已提交
5116 5117 5118
}

static int
5119
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
5120
                            int autostart) {
5121 5122
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5123
    int ret = -1;
5124

5125
    testDriverLock(privconn);
5126 5127
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5128
    testDriverUnlock(privconn);
5129 5130

    if (privpool == NULL) {
5131
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5132
        goto cleanup;
5133
    }
C
Cole Robinson 已提交
5134 5135

    if (!privpool->configFile) {
5136 5137
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
5138
        goto cleanup;
C
Cole Robinson 已提交
5139 5140 5141 5142
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
5143 5144 5145
    ret = 0;

cleanup:
5146 5147
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5148
    return ret;
C
Cole Robinson 已提交
5149 5150 5151 5152
}


static int
5153
testStoragePoolNumOfVolumes(virStoragePoolPtr pool) {
5154 5155
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5156
    int ret = -1;
5157

5158
    testDriverLock(privconn);
5159 5160
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5161
    testDriverUnlock(privconn);
5162 5163

    if (privpool == NULL) {
5164
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5165
        goto cleanup;
5166 5167 5168
    }

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

5174 5175 5176
    ret = privpool->volumes.count;

cleanup:
5177 5178
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5179
    return ret;
C
Cole Robinson 已提交
5180 5181 5182
}

static int
5183
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
5184 5185
                           char **const names,
                           int maxnames) {
5186 5187
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5188 5189
    size_t i = 0;
    int n = 0;
C
Cole Robinson 已提交
5190

5191
    memset(names, 0, maxnames * sizeof(*names));
5192 5193

    testDriverLock(privconn);
5194 5195
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5196
    testDriverUnlock(privconn);
5197 5198

    if (privpool == NULL) {
5199
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5200
        goto cleanup;
5201 5202 5203 5204
    }


    if (!virStoragePoolObjIsActive(privpool)) {
5205 5206
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5207
        goto cleanup;
5208 5209
    }

5210
    for (i = 0; i < privpool->volumes.count && n < maxnames; i++) {
5211
        if (VIR_STRDUP(names[n++], privpool->volumes.objs[i]->name) < 0)
C
Cole Robinson 已提交
5212 5213 5214
            goto cleanup;
    }

5215
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5216 5217 5218
    return n;

 cleanup:
5219
    for (n = 0; n < maxnames; n++)
C
Cole Robinson 已提交
5220 5221
        VIR_FREE(names[i]);

5222
    memset(names, 0, maxnames * sizeof(*names));
5223 5224
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5225 5226 5227
    return -1;
}

5228 5229 5230 5231 5232 5233
static int
testStoragePoolListAllVolumes(virStoragePoolPtr obj,
                              virStorageVolPtr **vols,
                              unsigned int flags) {
    testConnPtr privconn = obj->conn->privateData;
    virStoragePoolObjPtr pool;
5234
    size_t i;
5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263
    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;
    }

5264
    if (VIR_ALLOC_N(tmp_vols, pool->volumes.count + 1) < 0)
5265 5266
         goto cleanup;

5267
    for (i = 0; i < pool->volumes.count; i++) {
5268 5269
        if (!(vol = virGetStorageVol(obj->conn, pool->def->name,
                                     pool->volumes.objs[i]->name,
5270 5271
                                     pool->volumes.objs[i]->key,
                                     NULL, NULL)))
5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285
            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]);
        }
5286
        VIR_FREE(tmp_vols);
5287 5288 5289 5290 5291 5292 5293
    }

    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}
C
Cole Robinson 已提交
5294 5295

static virStorageVolPtr
5296 5297
testStorageVolLookupByName(virStoragePoolPtr pool,
                           const char *name ATTRIBUTE_UNUSED) {
5298 5299 5300
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5301
    virStorageVolPtr ret = NULL;
5302

5303
    testDriverLock(privconn);
5304 5305
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5306
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5307

5308
    if (privpool == NULL) {
5309
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5310
        goto cleanup;
5311 5312 5313 5314
    }


    if (!virStoragePoolObjIsActive(privpool)) {
5315 5316
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5317
        goto cleanup;
5318 5319 5320 5321 5322
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
5323 5324
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"), name);
5325
        goto cleanup;
C
Cole Robinson 已提交
5326 5327
    }

5328
    ret = virGetStorageVol(pool->conn, privpool->def->name,
5329 5330
                           privvol->name, privvol->key,
                           NULL, NULL);
5331 5332

cleanup:
5333 5334
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5335
    return ret;
C
Cole Robinson 已提交
5336 5337 5338 5339
}


static virStorageVolPtr
5340 5341
testStorageVolLookupByKey(virConnectPtr conn,
                          const char *key) {
5342
    testConnPtr privconn = conn->privateData;
5343
    size_t i;
5344
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
5345

5346
    testDriverLock(privconn);
5347
    for (i = 0; i < privconn->pools.count; i++) {
5348
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5349
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
5350
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
5351 5352
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

5353 5354 5355 5356
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
5357 5358
                                       privvol->key,
                                       NULL, NULL);
5359
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
5360 5361
                break;
            }
C
Cole Robinson 已提交
5362
        }
5363
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5364
    }
5365
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5366

5367
    if (!ret)
5368 5369
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
5370 5371

    return ret;
C
Cole Robinson 已提交
5372 5373 5374
}

static virStorageVolPtr
5375 5376
testStorageVolLookupByPath(virConnectPtr conn,
                           const char *path) {
5377
    testConnPtr privconn = conn->privateData;
5378
    size_t i;
5379
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
5380

5381
    testDriverLock(privconn);
5382
    for (i = 0; i < privconn->pools.count; i++) {
5383
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5384
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
5385
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
5386 5387
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

5388 5389 5390 5391
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
5392 5393
                                       privvol->key,
                                       NULL, NULL);
5394
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
5395 5396
                break;
            }
C
Cole Robinson 已提交
5397
        }
5398
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5399
    }
5400
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5401

5402
    if (!ret)
5403 5404
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
5405 5406

    return ret;
C
Cole Robinson 已提交
5407 5408 5409
}

static virStorageVolPtr
5410 5411 5412
testStorageVolCreateXML(virStoragePoolPtr pool,
                        const char *xmldesc,
                        unsigned int flags)
E
Eric Blake 已提交
5413
{
5414 5415
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5416 5417
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
5418

E
Eric Blake 已提交
5419 5420
    virCheckFlags(0, NULL);

5421
    testDriverLock(privconn);
5422 5423
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5424
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5425

5426
    if (privpool == NULL) {
5427
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5428
        goto cleanup;
5429 5430 5431
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5432 5433
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5434
        goto cleanup;
5435
    }
C
Cole Robinson 已提交
5436

5437
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
5438
    if (privvol == NULL)
5439
        goto cleanup;
5440 5441

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5442 5443
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5444
        goto cleanup;
C
Cole Robinson 已提交
5445 5446 5447
    }

    /* Make sure enough space */
5448
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
5449
         privpool->def->capacity) {
5450 5451 5452
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5453
        goto cleanup;
C
Cole Robinson 已提交
5454 5455 5456
    }

    if (VIR_REALLOC_N(privpool->volumes.objs,
5457
                      privpool->volumes.count+1) < 0)
5458
        goto cleanup;
C
Cole Robinson 已提交
5459

5460 5461
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5462
                    privvol->name) == -1)
5463
        goto cleanup;
C
Cole Robinson 已提交
5464

5465
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0)
5466
        goto cleanup;
C
Cole Robinson 已提交
5467

5468
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
5469 5470 5471
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5472
    privpool->volumes.objs[privpool->volumes.count++] = privvol;
C
Cole Robinson 已提交
5473

5474
    ret = virGetStorageVol(pool->conn, privpool->def->name,
5475 5476
                           privvol->name, privvol->key,
                           NULL, NULL);
5477
    privvol = NULL;
5478 5479 5480

cleanup:
    virStorageVolDefFree(privvol);
5481 5482
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5483
    return ret;
C
Cole Robinson 已提交
5484 5485
}

5486
static virStorageVolPtr
5487 5488 5489 5490
testStorageVolCreateXMLFrom(virStoragePoolPtr pool,
                            const char *xmldesc,
                            virStorageVolPtr clonevol,
                            unsigned int flags)
E
Eric Blake 已提交
5491
{
5492 5493 5494 5495 5496
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
5497 5498
    virCheckFlags(0, NULL);

5499 5500 5501 5502 5503 5504
    testDriverLock(privconn);
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
    testDriverUnlock(privconn);

    if (privpool == NULL) {
5505
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5506 5507 5508 5509
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5510 5511
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5512 5513 5514
        goto cleanup;
    }

5515
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
5516 5517 5518 5519
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5520 5521
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5522 5523 5524 5525 5526
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
5527 5528 5529
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
5530 5531 5532 5533 5534 5535
        goto cleanup;
    }

    /* Make sure enough space */
    if ((privpool->def->allocation + privvol->allocation) >
         privpool->def->capacity) {
5536 5537 5538
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5539 5540 5541 5542 5543 5544
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    if (VIR_REALLOC_N(privpool->volumes.objs,
5545
                      privpool->volumes.count+1) < 0)
5546 5547
        goto cleanup;

5548 5549
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5550
                    privvol->name) == -1)
5551 5552
        goto cleanup;

5553
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0)
5554 5555 5556 5557 5558 5559 5560 5561 5562
        goto cleanup;

    privpool->def->allocation += privvol->allocation;
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    privpool->volumes.objs[privpool->volumes.count++] = privvol;

    ret = virGetStorageVol(pool->conn, privpool->def->name,
5563 5564
                           privvol->name, privvol->key,
                           NULL, NULL);
5565 5566 5567 5568 5569 5570 5571 5572 5573
    privvol = NULL;

cleanup:
    virStorageVolDefFree(privvol);
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    return ret;
}

C
Cole Robinson 已提交
5574
static int
5575 5576
testStorageVolDelete(virStorageVolPtr vol,
                     unsigned int flags)
E
Eric Blake 已提交
5577
{
5578 5579 5580
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5581
    size_t i;
5582
    int ret = -1;
C
Cole Robinson 已提交
5583

E
Eric Blake 已提交
5584 5585
    virCheckFlags(0, -1);

5586
    testDriverLock(privconn);
5587 5588
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5589
    testDriverUnlock(privconn);
5590 5591

    if (privpool == NULL) {
5592
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5593
        goto cleanup;
5594 5595 5596 5597 5598 5599
    }


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

    if (privvol == NULL) {
5600 5601 5602
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5603
        goto cleanup;
5604 5605 5606
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5607 5608
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5609
        goto cleanup;
5610 5611 5612
    }


C
Cole Robinson 已提交
5613 5614 5615 5616
    privpool->def->allocation -= privvol->allocation;
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5617
    for (i = 0; i < privpool->volumes.count; i++) {
C
Cole Robinson 已提交
5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635
        if (privpool->volumes.objs[i] == privvol) {
            virStorageVolDefFree(privvol);

            if (i < (privpool->volumes.count - 1))
                memmove(privpool->volumes.objs + i,
                        privpool->volumes.objs + i + 1,
                        sizeof(*(privpool->volumes.objs)) *
                                (privpool->volumes.count - (i + 1)));

            if (VIR_REALLOC_N(privpool->volumes.objs,
                              privpool->volumes.count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            privpool->volumes.count--;

            break;
        }
    }
5636
    ret = 0;
C
Cole Robinson 已提交
5637

5638
cleanup:
5639 5640
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5641
    return ret;
C
Cole Robinson 已提交
5642 5643 5644 5645 5646
}


static int testStorageVolumeTypeForPool(int pooltype) {

5647
    switch (pooltype) {
C
Cole Robinson 已提交
5648 5649 5650 5651 5652 5653 5654 5655 5656 5657
        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
5658 5659
testStorageVolGetInfo(virStorageVolPtr vol,
                      virStorageVolInfoPtr info) {
5660 5661 5662
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5663
    int ret = -1;
5664

5665
    testDriverLock(privconn);
5666 5667
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5668
    testDriverUnlock(privconn);
5669 5670

    if (privpool == NULL) {
5671
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5672
        goto cleanup;
5673 5674 5675 5676 5677
    }

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

    if (privvol == NULL) {
5678 5679 5680
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5681
        goto cleanup;
5682 5683 5684
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5685 5686
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5687
        goto cleanup;
5688
    }
C
Cole Robinson 已提交
5689 5690 5691 5692 5693

    memset(info, 0, sizeof(*info));
    info->type = testStorageVolumeTypeForPool(privpool->def->type);
    info->capacity = privvol->capacity;
    info->allocation = privvol->allocation;
5694
    ret = 0;
C
Cole Robinson 已提交
5695

5696
cleanup:
5697 5698
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5699
    return ret;
C
Cole Robinson 已提交
5700 5701 5702
}

static char *
5703 5704
testStorageVolGetXMLDesc(virStorageVolPtr vol,
                         unsigned int flags)
E
Eric Blake 已提交
5705
{
5706 5707 5708
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5709
    char *ret = NULL;
5710

E
Eric Blake 已提交
5711 5712
    virCheckFlags(0, NULL);

5713
    testDriverLock(privconn);
5714 5715
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5716
    testDriverUnlock(privconn);
5717 5718

    if (privpool == NULL) {
5719
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5720
        goto cleanup;
5721 5722 5723 5724 5725
    }

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

    if (privvol == NULL) {
5726 5727 5728
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5729
        goto cleanup;
5730
    }
C
Cole Robinson 已提交
5731

5732
    if (!virStoragePoolObjIsActive(privpool)) {
5733 5734
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5735
        goto cleanup;
5736 5737
    }

5738
    ret = virStorageVolDefFormat(privpool->def, privvol);
5739 5740

cleanup:
5741 5742
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5743
    return ret;
C
Cole Robinson 已提交
5744 5745 5746
}

static char *
5747
testStorageVolGetPath(virStorageVolPtr vol) {
5748 5749 5750
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5751
    char *ret = NULL;
5752

5753
    testDriverLock(privconn);
5754 5755
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5756
    testDriverUnlock(privconn);
5757 5758

    if (privpool == NULL) {
5759
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5760
        goto cleanup;
5761 5762 5763 5764 5765
    }

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

    if (privvol == NULL) {
5766 5767 5768
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5769
        goto cleanup;
5770 5771 5772
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5773 5774
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5775
        goto cleanup;
5776 5777
    }

5778
    ignore_value(VIR_STRDUP(ret, privvol->target.path));
5779 5780

cleanup:
5781 5782
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5783 5784 5785
    return ret;
}

5786

5787
/* Node device implementations */
5788 5789 5790
static virDrvOpenStatus testNodeDeviceOpen(virConnectPtr conn,
                                           virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                           unsigned int flags)
E
Eric Blake 已提交
5791 5792 5793
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5794 5795 5796
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5797
    conn->nodeDevicePrivateData = conn->privateData;
5798 5799 5800
    return VIR_DRV_OPEN_SUCCESS;
}

5801 5802
static int testNodeDeviceClose(virConnectPtr conn) {
    conn->nodeDevicePrivateData = NULL;
5803 5804 5805
    return 0;
}

5806 5807 5808
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5809
                     unsigned int flags)
5810 5811 5812
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
5813
    size_t i;
5814

E
Eric Blake 已提交
5815 5816
    virCheckFlags(0, -1);

5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831
    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 已提交
5832
                    unsigned int flags)
5833 5834 5835
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
5836
    size_t i;
5837

E
Eric Blake 已提交
5838 5839
    virCheckFlags(0, -1);

5840 5841 5842 5843 5844
    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)) {
5845
            if (VIR_STRDUP(names[ndevs++], driver->devs.objs[i]->def->name) < 0) {
5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875
                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)
{
    testConnPtr driver = conn->privateData;
    virNodeDeviceObjPtr obj;
    virNodeDevicePtr ret = NULL;

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

    if (!obj) {
5876
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

cleanup:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
5889
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5890
                         unsigned int flags)
5891 5892 5893 5894 5895
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5896 5897
    virCheckFlags(0, NULL);

5898 5899 5900 5901 5902
    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
5903 5904 5905
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5906 5907 5908
        goto cleanup;
    }

5909
    ret = virNodeDeviceDefFormat(obj->def);
5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928

cleanup:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
testNodeDeviceGetParent(virNodeDevicePtr dev)
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

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

    if (!obj) {
5929 5930 5931
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5932 5933 5934 5935
        goto cleanup;
    }

    if (obj->def->parent) {
5936
        ignore_value(VIR_STRDUP(ret, obj->def->parent));
5937
    } else {
5938 5939
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5940 5941 5942 5943 5944 5945 5946 5947
    }

cleanup:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

5948

5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962
static int
testNodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

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

    if (!obj) {
5963 5964 5965
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993
        goto cleanup;
    }

    for (caps = obj->def->caps; caps; caps = caps->next)
        ++ncaps;
    ret = ncaps;

cleanup:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}


static int
testNodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

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

    if (!obj) {
5994 5995 5996
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5997 5998 5999 6000
        goto cleanup;
    }

    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
6001
        if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->type)) < 0)
6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016
            goto cleanup;
    }
    ret = ncaps;

cleanup:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
}

6017 6018 6019
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
E
Eric Blake 已提交
6020
                        unsigned int flags)
6021 6022 6023 6024 6025 6026 6027 6028 6029
{
    testConnPtr driver = conn->privateData;
    virNodeDeviceDefPtr def = NULL;
    virNodeDeviceObjPtr obj = NULL;
    char *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;
    virNodeDevicePtr dev = NULL;
    virNodeDevCapsDefPtr caps;

E
Eric Blake 已提交
6030 6031
    virCheckFlags(0, NULL);

6032 6033
    testDriverLock(driver);

6034
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, NULL);
6035 6036 6037 6038 6039
    if (def == NULL) {
        goto cleanup;
    }

    /* We run these next two simply for validation */
6040
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1) {
6041 6042 6043
        goto cleanup;
    }

6044
    if (virNodeDeviceGetParentHost(&driver->devs,
6045 6046 6047 6048 6049 6050 6051 6052 6053
                                   def->name,
                                   def->parent,
                                   &parent_host) == -1) {
        goto cleanup;
    }

    /* 'name' is supposed to be filled in by the node device backend, which
     * we don't have. Use WWPN instead. */
    VIR_FREE(def->name);
6054
    if (VIR_STRDUP(def->name, wwpn) < 0)
6055 6056 6057 6058 6059 6060 6061 6062 6063
        goto cleanup;

    /* Fill in a random 'host' value, since this would also come from
     * the backend */
    caps = def->caps;
    while (caps) {
        if (caps->type != VIR_NODE_DEV_CAP_SCSI_HOST)
            continue;

6064
        caps->data.scsi_host.host = virRandomBits(10);
6065 6066 6067 6068
        caps = caps->next;
    }


6069
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def))) {
6070 6071 6072 6073 6074 6075 6076 6077
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
cleanup:
    testDriverUnlock(driver);
6078
    virNodeDeviceDefFree(def);
6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return dev;
}

static int
testNodeDeviceDestroy(virNodeDevicePtr dev)
{
    int ret = 0;
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj = NULL;
    char *parent_name = NULL, *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;

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

    if (!obj) {
6098
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
6099 6100 6101
        goto out;
    }

6102
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
6103 6104 6105
        goto out;
    }

6106
    if (VIR_STRDUP(parent_name, obj->def->parent) < 0)
6107 6108 6109 6110 6111 6112 6113 6114 6115
        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 */
6116
    if (virNodeDeviceGetParentHost(&driver->devs,
6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135
                                   dev->name,
                                   parent_name,
                                   &parent_host) == -1) {
        obj = NULL;
        goto out;
    }

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

out:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

6136 6137

/* Domain event implementations */
6138
static int
6139 6140 6141 6142
testConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
6143 6144
{
    testConnPtr driver = conn->privateData;
6145
    int ret = 0;
6146 6147

    testDriverLock(driver);
6148
    if (virDomainEventStateRegister(conn, driver->eventState,
6149 6150
                                    callback, opaque, freecb) < 0)
        ret = -1;
6151 6152 6153 6154 6155
    testDriverUnlock(driver);

    return ret;
}

6156

6157
static int
6158 6159
testConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
6160 6161
{
    testConnPtr driver = conn->privateData;
6162
    int ret = 0;
6163 6164

    testDriverLock(driver);
6165
    if (virDomainEventStateDeregister(conn, driver->eventState,
6166 6167
                                      callback) < 0)
        ret = -1;
6168 6169 6170 6171 6172
    testDriverUnlock(driver);

    return ret;
}

6173 6174

static int
6175 6176 6177 6178 6179 6180
testConnectDomainEventRegisterAny(virConnectPtr conn,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback callback,
                                  void *opaque,
                                  virFreeCallback freecb)
6181 6182 6183 6184 6185
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
6186
    if (virDomainEventStateRegisterID(conn, driver->eventState,
6187 6188
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
6189
        ret = -1;
6190 6191 6192 6193 6194 6195
    testDriverUnlock(driver);

    return ret;
}

static int
6196 6197
testConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
6198 6199
{
    testConnPtr driver = conn->privateData;
6200
    int ret = 0;
6201 6202

    testDriverLock(driver);
6203
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
6204 6205
                                        callbackID) < 0)
        ret = -1;
6206 6207 6208 6209 6210 6211
    testDriverUnlock(driver);

    return ret;
}


6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223
static int
testConnectNetworkEventRegisterAny(virConnectPtr conn,
                                   virNetworkPtr net,
                                   int eventID,
                                   virConnectNetworkEventGenericCallback callback,
                                   void *opaque,
                                   virFreeCallback freecb)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
6224
    if (virNetworkEventStateRegisterID(conn, driver->eventState,
6225
                                       net, eventID, callback,
6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237
                                       opaque, freecb, &ret) < 0)
        ret = -1;
    testDriverUnlock(driver);

    return ret;
}

static int
testConnectNetworkEventDeregisterAny(virConnectPtr conn,
                                     int callbackID)
{
    testConnPtr driver = conn->privateData;
6238
    int ret = 0;
6239 6240

    testDriverLock(driver);
6241
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
6242 6243
                                        callbackID) < 0)
        ret = -1;
6244 6245 6246 6247 6248 6249
    testDriverUnlock(driver);

    return ret;
}


6250
/* driver must be locked before calling */
6251
static void testObjectEventQueue(testConnPtr driver,
6252
                                 virObjectEventPtr event)
6253
{
6254
    virObjectEventStateQueue(driver->eventState, event);
6255 6256
}

6257 6258
static virDrvOpenStatus testSecretOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
6259 6260 6261 6262
                                       unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

6263 6264 6265
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

6266
    conn->secretPrivateData = conn->privateData;
6267 6268 6269 6270 6271 6272 6273
    return VIR_DRV_OPEN_SUCCESS;
}

static int testSecretClose(virConnectPtr conn) {
    conn->secretPrivateData = NULL;
    return 0;
}
6274

6275 6276 6277

static virDrvOpenStatus testNWFilterOpen(virConnectPtr conn,
                                         virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
6278 6279 6280 6281
                                         unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

6282 6283 6284
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

6285
    conn->nwfilterPrivateData = conn->privateData;
6286 6287 6288 6289 6290 6291 6292 6293
    return VIR_DRV_OPEN_SUCCESS;
}

static int testNWFilterClose(virConnectPtr conn) {
    conn->nwfilterPrivateData = NULL;
    return 0;
}

6294

6295 6296 6297
static int testConnectListAllDomains(virConnectPtr conn,
                                     virDomainPtr **domains,
                                     unsigned int flags)
6298 6299 6300 6301
{
    testConnPtr privconn = conn->privateData;
    int ret;

O
Osier Yang 已提交
6302
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
6303 6304

    testDriverLock(privconn);
6305 6306
    ret = virDomainObjListExport(privconn->domains, conn, domains,
                                 NULL, flags);
6307 6308 6309 6310 6311
    testDriverUnlock(privconn);

    return ret;
}

6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324
static int
testNodeGetCPUMap(virConnectPtr conn,
                  unsigned char **cpumap,
                  unsigned int *online,
                  unsigned int flags)
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(0, -1);

    testDriverLock(privconn);
    if (cpumap) {
6325
        if (VIR_ALLOC_N(*cpumap, 1) < 0)
6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339
            goto cleanup;
        *cpumap[0] = 0x15;
    }

    if (online)
        *online = 3;

    ret = 8;

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

6340 6341 6342 6343 6344 6345 6346 6347 6348 6349
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

6350
    if (VIR_STRDUP(ret, "image/png") < 0)
6351 6352
        return NULL;

6353
    if (virFDStreamOpenFile(st, PKGDATADIR "/libvirtLogo.png", 0, 0, O_RDONLY) < 0)
6354 6355 6356 6357 6358
        VIR_FREE(ret);

    return ret;
}

6359 6360 6361 6362 6363 6364 6365 6366 6367
static int
testConnectGetCPUModelNames(virConnectPtr conn ATTRIBUTE_UNUSED,
                            const char *arch,
                            char ***models,
                            unsigned int flags)
{
    virCheckFlags(0, -1);
    return cpuGetModels(arch, models);
}
6368

C
Cole Robinson 已提交
6369 6370 6371 6372 6373
static int
testDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr vm = NULL;
6374
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402
    int ret = -1;

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

    testDriverLock(privconn);
    vm = virDomainObjListFindByName(privconn->domains, dom->name);
    testDriverUnlock(privconn);

    if (vm == NULL) {
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto cleanup;
    }

    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);
6403
    event = virDomainEventLifecycleNewFromObj(vm,
C
Cole Robinson 已提交
6404 6405 6406 6407 6408 6409 6410 6411 6412 6413
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
    vm->hasManagedSave = true;

    ret = 0;
cleanup:
    if (vm)
        virObjectUnlock(vm);
    if (event) {
        testDriverLock(privconn);
6414
        testObjectEventQueue(privconn, event);
C
Cole Robinson 已提交
6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473
        testDriverUnlock(privconn);
    }

    return ret;
}


static int
testDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

    virCheckFlags(0, -1);

    testDriverLock(privconn);

    vm = virDomainObjListFindByName(privconn->domains, dom->name);
    if (vm == NULL) {
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto cleanup;
    }

    ret = vm->hasManagedSave;
cleanup:
    if (vm)
        virObjectUnlock(vm);
    testDriverUnlock(privconn);
    return ret;
}

static int
testDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

    virCheckFlags(0, -1);

    testDriverLock(privconn);

    vm = virDomainObjListFindByName(privconn->domains, dom->name);
    if (vm == NULL) {
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto cleanup;
    }

    vm->hasManagedSave = false;
    ret = 0;
cleanup:
    if (vm)
        virObjectUnlock(vm);
    testDriverUnlock(privconn);
    return ret;
}


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 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 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
/*
 * 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;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
        goto cleanup;

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

cleanup:
    if (vm)
        virObjectUnlock(vm);
    return n;
}

static int
testDomainSnapshotListNames(virDomainPtr domain,
                            char **names,
                            int nameslen,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
        goto cleanup;

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

cleanup:
    if (vm)
        virObjectUnlock(vm);
    return n;
}

static int
testDomainListAllSnapshots(virDomainPtr domain,
                           virDomainSnapshotPtr **snaps,
                           unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int n = -1;

    virCheckFlags(VIR_DOMAIN_SNAPSHOT_LIST_ROOTS |
                  VIR_DOMAIN_SNAPSHOT_FILTERS_ALL, -1);

    if (!(vm = testDomObjFromDomain(domain)))
        goto cleanup;

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

cleanup:
    if (vm)
        virObjectUnlock(vm);
    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)))
        goto cleanup;

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

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

cleanup:
    if (vm)
        virObjectUnlock(vm);
    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)))
        goto cleanup;

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

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

cleanup:
    if (vm)
        virObjectUnlock(vm);
    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)))
        goto cleanup;

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

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

cleanup:
    if (vm)
        virObjectUnlock(vm);
    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)))
        goto cleanup;

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

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

cleanup:
    if (vm)
        virObjectUnlock(vm);
    return snapshot;
}

static int
testDomainHasCurrentSnapshot(virDomainPtr domain,
                             unsigned int flags)
{
    virDomainObjPtr vm;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromDomain(domain)))
        goto cleanup;

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

cleanup:
    if (vm)
        virObjectUnlock(vm);
    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)))
        goto cleanup;

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

cleanup:
    if (vm)
        virObjectUnlock(vm);
    return parent;
}

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

    virCheckFlags(0, NULL);

    if (!(vm = testDomObjFromDomain(domain)))
        goto cleanup;

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

cleanup:
    if (vm)
        virObjectUnlock(vm);
    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];

    virCheckFlags(VIR_DOMAIN_XML_SECURE, NULL);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
        goto cleanup;

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

    virUUIDFormat(snapshot->domain->uuid, uuidstr);

    xml = virDomainSnapshotDefFormat(uuidstr, snap->def, flags, 0);

cleanup:
    if (vm)
        virObjectUnlock(vm);
    return xml;
}

static int
testDomainSnapshotIsCurrent(virDomainSnapshotPtr snapshot,
                            unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
        goto cleanup;

    ret = (vm->current_snapshot &&
           STREQ(snapshot->name, vm->current_snapshot->def->name));

cleanup:
    if (vm)
        virObjectUnlock(vm);
    return ret;
}


static int
testDomainSnapshotHasMetadata(virDomainSnapshotPtr snapshot,
                              unsigned int flags)
{
    virDomainObjPtr vm = NULL;
    int ret = -1;

    virCheckFlags(0, -1);

    if (!(vm = testDomObjFromSnapshot(snapshot)))
        goto cleanup;

C
Cole Robinson 已提交
6813
    if (!testSnapObjFromSnapshot(vm, snapshot))
6814 6815 6816 6817 6818 6819 6820 6821 6822 6823
        goto cleanup;

    ret = 1;

cleanup:
    if (vm)
        virObjectUnlock(vm);
    return ret;
}

6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863
static int
testDomainSnapshotAlignDisks(virDomainObjPtr vm,
                             virDomainSnapshotDefPtr def,
                             unsigned int flags)
{
    int align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
    int align_match = true;

    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)
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr vm = NULL;
    virDomainSnapshotDefPtr def = NULL;
    virDomainSnapshotObjPtr snap = NULL;
    virDomainSnapshotPtr snapshot = NULL;
6864
    virObjectEventPtr event = NULL;
6865
    char *xml = NULL;
6866 6867
    bool update_current = true;
    bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE;
6868 6869 6870 6871 6872 6873 6874 6875
    unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;

    /*
     * DISK_ONLY: Not implemented yet
     * REUSE_EXT: Not implemented yet
     *
     * NO_METADATA: Explicitly not implemented
     *
6876
     * REDEFINE + CURRENT: Implemented
6877 6878 6879 6880 6881 6882
     * HALT: Implemented
     * QUIESCE: Nothing to do
     * ATOMIC: Nothing to do
     * LIVE: Nothing to do
     */
    virCheckFlags(
6883 6884
        VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
        VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT |
6885 6886 6887 6888 6889
        VIR_DOMAIN_SNAPSHOT_CREATE_HALT |
        VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE |
        VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC |
        VIR_DOMAIN_SNAPSHOT_CREATE_LIVE, NULL);

6890 6891 6892 6893 6894
    if ((redefine && !(flags & VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT)))
        update_current = false;
    if (redefine)
        parse_flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;

6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910
    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,
                                                1 << VIR_DOMAIN_VIRT_TEST,
                                                parse_flags)))
        goto cleanup;

6911
    if (redefine) {
C
Cole Robinson 已提交
6912 6913
        if (virDomainSnapshotRedefinePrep(domain, vm, &def, &snap,
                                          &update_current, flags) < 0)
6914 6915 6916 6917 6918 6919 6920
            goto cleanup;
    } else {
        if (!(def->dom = virDomainDefCopy(vm->def,
                                          privconn->caps,
                                          privconn->xmlopt,
                                          true)))
            goto cleanup;
6921

6922
        if (testDomainSnapshotAlignDisks(vm, def, flags) < 0)
6923 6924 6925
            goto cleanup;
    }

6926 6927 6928 6929
    if (!snap) {
        if (!(snap = virDomainSnapshotAssignDef(vm->snapshots, def)))
            goto cleanup;
        def = NULL;
6930 6931
    }

6932 6933 6934 6935 6936 6937 6938 6939 6940 6941
    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);
6942
            event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
6943 6944 6945
                                    VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
        }
    }
6946 6947 6948 6949 6950 6951 6952

    snapshot = virGetDomainSnapshot(domain, snap->def->name);
cleanup:
    VIR_FREE(xml);
    if (vm) {
        if (snapshot) {
            virDomainSnapshotObjPtr other;
6953 6954
            if (update_current)
                vm->current_snapshot = snap;
6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965
            other = virDomainSnapshotFindByName(vm->snapshots,
                                                snap->def->parent);
            snap->parent = other;
            other->nchildren++;
            snap->sibling = other->first_child;
            other->first_child = snap;
        }
        virObjectUnlock(vm);
    }
    if (event) {
        testDriverLock(privconn);
6966
        testObjectEventQueue(privconn, event);
6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112
        testDriverUnlock(privconn);
    }
    virDomainSnapshotDefFree(def);
    return snapshot;
}


typedef struct _testSnapRemoveData testSnapRemoveData;
typedef testSnapRemoveData *testSnapRemoveDataPtr;
struct _testSnapRemoveData {
    virDomainObjPtr vm;
    bool current;
};

static void
testDomainSnapshotDiscardAll(void *payload,
                          const void *name ATTRIBUTE_UNUSED,
                          void *data)
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapRemoveDataPtr curr = data;

    if (snap->def->current)
        curr->current = true;
    virDomainSnapshotObjListRemove(curr->vm->snapshots, snap);
}

typedef struct _testSnapReparentData testSnapReparentData;
typedef testSnapReparentData *testSnapReparentDataPtr;
struct _testSnapReparentData {
    virDomainSnapshotObjPtr parent;
    virDomainObjPtr vm;
    int err;
    virDomainSnapshotObjPtr last;
};

static void
testDomainSnapshotReparentChildren(void *payload,
                                   const void *name ATTRIBUTE_UNUSED,
                                   void *data)
{
    virDomainSnapshotObjPtr snap = payload;
    testSnapReparentDataPtr rep = data;

    if (rep->err < 0) {
        return;
    }

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

    if (!snap->sibling)
        rep->last = snap;
}

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) {
            if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY) {
                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;
cleanup:
    if (vm)
        virObjectUnlock(vm);
    return ret;
}

static int
testDomainRevertToSnapshot(virDomainSnapshotPtr snapshot,
                           unsigned int flags)
{
    testConnPtr privconn = snapshot->domain->conn->privateData;
    virDomainObjPtr vm = NULL;
    virDomainSnapshotObjPtr snap = NULL;
7113 7114
    virObjectEventPtr event = NULL;
    virObjectEventPtr event2 = NULL;
7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207
    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;

    testDriverLock(privconn);

    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;
    config = virDomainDefCopy(snap->def->dom,
                              privconn->caps, privconn->xmlopt, true);
    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);
7208
                event = virDomainEventLifecycleNewFromObj(vm,
7209 7210 7211
                            VIR_DOMAIN_EVENT_STOPPED,
                            VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
                if (event)
7212
                    testObjectEventQueue(privconn, event);
7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223
                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. */
7224
                event = virDomainEventLifecycleNewFromObj(vm,
7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237
                                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;
7238
            event = virDomainEventLifecycleNewFromObj(vm,
7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251
                                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 */
7252
                event2 = virDomainEventLifecycleNewFromObj(vm,
7253 7254 7255 7256 7257
                                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 已提交
7258
            virObjectUnref(event);
7259 7260 7261 7262
            event = NULL;

            if (was_stopped) {
                /* Transition 2 */
7263
                event = virDomainEventLifecycleNewFromObj(vm,
7264 7265 7266 7267
                                VIR_DOMAIN_EVENT_STARTED,
                                VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            } else if (was_running) {
                /* Transition 8 */
7268
                event = virDomainEventLifecycleNewFromObj(vm,
7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280
                                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);
7281
            event = virDomainEventLifecycleNewFromObj(vm,
7282 7283 7284 7285 7286 7287 7288 7289 7290 7291
                                    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;

            if (event)
7292
                testObjectEventQueue(privconn, event);
7293
            event = virDomainEventLifecycleNewFromObj(vm,
7294 7295 7296
                            VIR_DOMAIN_EVENT_STARTED,
                            VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            if (paused) {
7297
                event2 = virDomainEventLifecycleNewFromObj(vm,
7298 7299 7300 7301 7302 7303 7304 7305 7306 7307
                                VIR_DOMAIN_EVENT_SUSPENDED,
                                VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT);
            }
        }
    }

    vm->current_snapshot = snap;
    ret = 0;
cleanup:
    if (event) {
7308
        testObjectEventQueue(privconn, event);
7309
        if (event2)
7310
            testObjectEventQueue(privconn, event2);
C
Cole Robinson 已提交
7311
    } else {
C
Cédric Bosdonnat 已提交
7312
        virObjectUnref(event2);
7313 7314 7315 7316 7317 7318 7319 7320
    }
    virObjectUnlock(vm);
    testDriverUnlock(privconn);

    return ret;
}


7321

7322
static virDriver testDriver = {
7323 7324
    .no = VIR_DRV_TEST,
    .name = "Test",
7325 7326 7327
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
7328
    .connectGetHostname = testConnectGetHostname, /* 0.6.3 */
7329
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
7330
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
7331 7332 7333 7334
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
7335
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349
    .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 */
7350 7351
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
7352
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
7353
    .domainRestore = testDomainRestore, /* 0.3.2 */
7354
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
7355
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
7356
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
7357 7358 7359 7360 7361 7362
    .domainSetVcpusFlags = testDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = testDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = testDomainPinVcpu, /* 0.7.3 */
    .domainGetVcpus = testDomainGetVcpus, /* 0.7.3 */
    .domainGetMaxVcpus = testDomainGetMaxVcpus, /* 0.7.3 */
    .domainGetXMLDesc = testDomainGetXMLDesc, /* 0.1.4 */
7363 7364
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
7365 7366 7367 7368
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
7369
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
7370 7371 7372
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
7373 7374 7375 7376
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
7377 7378 7379
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
7380 7381 7382 7383
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
7384 7385 7386
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
7387 7388 7389
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
7390
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
7391
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
7392 7393
    .domainGetMetadata = testDomainGetMetadata, /* 1.1.3 */
    .domainSetMetadata = testDomainSetMetadata, /* 1.1.3 */
7394
    .connectGetCPUModelNames = testConnectGetCPUModelNames, /* 1.1.3 */
7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411
    .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 */
7412 7413 7414
    .domainSnapshotCreateXML = testDomainSnapshotCreateXML, /* 1.1.4 */
    .domainRevertToSnapshot = testDomainRevertToSnapshot, /* 1.1.4 */
    .domainSnapshotDelete = testDomainSnapshotDelete, /* 1.1.4 */
7415

E
Eric Blake 已提交
7416
    .connectBaselineCPU = testConnectBaselineCPU, /* 1.2.0 */
7417 7418 7419 7420
};

static virNetworkDriver testNetworkDriver = {
    "Test",
7421 7422 7423 7424 7425 7426 7427
    .networkOpen = testNetworkOpen, /* 0.3.2 */
    .networkClose = testNetworkClose, /* 0.3.2 */
    .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 */
7428 7429
    .connectNetworkEventRegisterAny = testConnectNetworkEventRegisterAny, /* 1.2.1 */
    .connectNetworkEventDeregisterAny = testConnectNetworkEventDeregisterAny, /* 1.2.1 */
7430 7431 7432 7433
    .networkLookupByUUID = testNetworkLookupByUUID, /* 0.3.2 */
    .networkLookupByName = testNetworkLookupByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreateXML, /* 0.3.2 */
    .networkDefineXML = testNetworkDefineXML, /* 0.3.2 */
7434
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
7435
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
7436
    .networkCreate = testNetworkCreate, /* 0.3.2 */
7437 7438 7439 7440 7441 7442 7443
    .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 */
7444 7445
};

L
Laine Stump 已提交
7446 7447
static virInterfaceDriver testInterfaceDriver = {
    "Test",                     /* name */
7448 7449 7450 7451 7452 7453 7454 7455
    .interfaceOpen = testInterfaceOpen, /* 0.7.0 */
    .interfaceClose = testInterfaceClose, /* 0.7.0 */
    .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 */
7456 7457 7458 7459 7460 7461
    .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 */
7462 7463 7464
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
7465 7466 7467
};


7468 7469
static virStorageDriver testStorageDriver = {
    .name = "Test",
7470 7471
    .storageOpen = testStorageOpen, /* 0.4.1 */
    .storageClose = testStorageClose, /* 0.4.1 */
7472

7473 7474 7475 7476 7477 7478
    .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 */
7479 7480 7481
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
7482 7483
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
7484 7485
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
7486
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
7487 7488 7489 7490 7491 7492 7493
    .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 */
7494
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
7495 7496 7497
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

7498 7499 7500 7501 7502 7503 7504 7505 7506
    .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 */
7507 7508
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
7509 7510
};

7511
static virNodeDeviceDriver testNodeDeviceDriver = {
7512
    .name = "Test",
7513 7514
    .nodeDeviceOpen = testNodeDeviceOpen, /* 0.6.0 */
    .nodeDeviceClose = testNodeDeviceClose, /* 0.6.0 */
7515 7516 7517 7518 7519 7520 7521 7522 7523 7524

    .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 */
7525 7526
};

7527 7528
static virSecretDriver testSecretDriver = {
    .name = "Test",
7529 7530
    .secretOpen = testSecretOpen, /* 0.7.1 */
    .secretClose = testSecretClose, /* 0.7.1 */
7531
};
7532 7533


7534 7535
static virNWFilterDriver testNWFilterDriver = {
    .name = "Test",
7536 7537
    .nwfilterOpen = testNWFilterOpen, /* 0.8.0 */
    .nwfilterClose = testNWFilterClose, /* 0.8.0 */
7538 7539
};

7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
7552 7553
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
7554 7555
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
7556
    if (virRegisterNodeDeviceDriver(&testNodeDeviceDriver) < 0)
7557
        return -1;
7558 7559
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
7560 7561
    if (virRegisterNWFilterDriver(&testNWFilterDriver) < 0)
        return -1;
7562

7563 7564
    return 0;
}