test_driver.c 209.0 KB
Newer Older
1
/*
2
 * test_driver.c: A "mock" hypervisor for use by application unit tests
3
 *
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
VIR_LOG_INIT("test.test_driver");

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

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

76 77 78 79 80
#define MAX_CPUS 128

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

#define MAX_CELLS 128
87

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

95
struct _testConn {
96
    virMutex lock;
97

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

115
    virObjectEventStatePtr eventState;
116 117
};
typedef struct _testConn testConn;
118
typedef testConn *testConnPtr;
119

120 121
static testConn defaultConn;
static int defaultConnections;
122
static virMutex defaultLock = VIR_MUTEX_INITIALIZER;
123

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

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

139

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

144 145
static void testDriverLock(testConnPtr driver)
{
146
    virMutexLock(&driver->lock);
147 148 149 150
}

static void testDriverUnlock(testConnPtr driver)
{
151
    virMutexUnlock(&driver->lock);
152 153
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167
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 已提交
168
    VIR_FREE(priv->vcpu_infos);
169 170 171 172
    VIR_FREE(priv->cpumaps);
    VIR_FREE(priv);
}

173 174 175 176 177 178
#define TEST_NAMESPACE_HREF "http://libvirt.org/schemas/domain/test/1.0"

typedef struct _testDomainNamespaceDef testDomainNamespaceDef;
typedef testDomainNamespaceDef *testDomainNamespaceDefPtr;
struct _testDomainNamespaceDef {
    int runstate;
179
    bool transient;
C
Cole Robinson 已提交
180
    bool hasManagedSave;
181 182 183

    unsigned int num_snap_nodes;
    xmlNodePtr *snap_nodes;
184 185 186 187 188 189
};

static void
testDomainDefNamespaceFree(void *data)
{
    testDomainNamespaceDefPtr nsdata = data;
190 191 192 193 194 195 196 197 198
    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);
199 200 201 202 203 204 205 206 207 208
    VIR_FREE(nsdata);
}

static int
testDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
                            xmlNodePtr root ATTRIBUTE_UNUSED,
                            xmlXPathContextPtr ctxt,
                            void **data)
{
    testDomainNamespaceDefPtr nsdata = NULL;
209 210 211
    xmlNodePtr *nodes = NULL;
    int tmp, n;
    size_t i;
212 213 214 215 216 217 218 219 220 221 222 223 224
    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;

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    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);

244 245 246 247 248 249 250
    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 已提交
251 252 253 254 255 256 257
    tmp = virXPathBoolean("boolean(./test:hasmanagedsave)", ctxt);
    if (tmp == -1) {
        virReportError(VIR_ERR_XML_ERROR, "%s", _("invalid hasmanagedsave"));
        goto error;
    }
    nsdata->hasManagedSave = tmp;

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    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;
    }

274 275 276 277 278
    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 已提交
279 280 281 282 283
    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;
    }
284

285 286 287
    *data = nsdata;
    return 0;

288
 error:
289
    VIR_FREE(nodes);
290 291 292
    testDomainDefNamespaceFree(nsdata);
    return -1;
}
293

294
static virDomainXMLOptionPtr
295 296
testBuildXMLConfig(void)
{
297 298 299 300 301 302 303 304 305 306 307 308
    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);
309 310 311
}


312
static virCapsPtr
313 314
testBuildCapabilities(virConnectPtr conn)
{
315
    testConnPtr privconn = conn->privateData;
316 317 318
    virCapsPtr caps;
    virCapsGuestPtr guest;
    const char *const guest_types[] = { "hvm", "xen" };
319
    size_t i;
320

321
    if ((caps = virCapabilitiesNew(VIR_ARCH_I686, false, false)) == NULL)
322
        goto error;
323

324
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
325
        goto error;
326
    if (virCapabilitiesAddHostFeature(caps, "nonpae") < 0)
327
        goto error;
328

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

        if (VIR_ALLOC_N(cpu_cells, privconn->cells[i].numCpus) < 0)
333
            goto error;
334 335 336 337 338

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


339 340
        if (virCapabilitiesAddHostNUMACell(caps, i, 0,
                                           privconn->cells[i].numCpus,
M
Michal Privoznik 已提交
341
                                           cpu_cells, 0, NULL, 0, NULL) < 0)
342
            goto error;
343 344
    }

345
    for (i = 0; i < ARRAY_CARDINALITY(guest_types); i++) {
346 347
        if ((guest = virCapabilitiesAddGuest(caps,
                                             guest_types[i],
348
                                             VIR_ARCH_I686,
349 350 351 352
                                             TEST_EMULATOR,
                                             NULL,
                                             0,
                                             NULL)) == NULL)
353
            goto error;
354

355 356 357 358 359 360
        if (virCapabilitiesAddGuestDomain(guest,
                                          "test",
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
361
            goto error;
362

363
        if (virCapabilitiesAddGuestFeature(guest, "pae", true, true) == NULL)
364
            goto error;
365
        if (virCapabilitiesAddGuestFeature(guest, "nonpae", true, true) == NULL)
366
            goto error;
367 368
    }

369 370
    caps->host.nsecModels = 1;
    if (VIR_ALLOC_N(caps->host.secModels, caps->host.nsecModels) < 0)
371
        goto error;
372 373
    if (VIR_STRDUP(caps->host.secModels[0].model, "testSecurity") < 0)
        goto error;
374

375 376
    if (VIR_STRDUP(caps->host.secModels[0].doi, "") < 0)
        goto error;
377

378
    return caps;
379

380
 error:
381
    virObjectUnref(caps);
382
    return NULL;
383 384
}

385

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


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

L
Laine Stump 已提交
412 413 414 415 416 417 418 419 420 421 422
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 已提交
423 424 425
static const char *defaultPoolXML =
"<pool type='dir'>"
"  <name>default-pool</name>"
426
"  <uuid>dfe224cb-28fb-8dd0-c4b2-64eb3f0f4566</uuid>"
C
Cole Robinson 已提交
427 428 429 430 431
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
"</pool>";

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
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";

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
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>";

473
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
474
static const unsigned long long defaultPoolAlloc;
C
Cole Robinson 已提交
475

476
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool);
477
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
478

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
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;
}

499
static char *
500 501
testDomainGenerateIfname(virDomainDefPtr domdef)
{
502
    int maxif = 1024;
503 504
    int ifctr;
    size_t i;
505 506 507 508 509

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

510
        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0)
511 512 513
            return NULL;

        /* Generate network interface names */
514
        for (i = 0; i < domdef->nnets; i++) {
515
            if (domdef->nets[i]->ifname &&
516
                STREQ(domdef->nets[i]->ifname, ifname)) {
517 518 519 520 521 522 523
                found = 1;
                break;
            }
        }

        if (!found)
            return ifname;
524
        VIR_FREE(ifname);
525 526
    }

527 528
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Exceeded max iface limit %d"), maxif);
529 530 531
    return NULL;
}

532
static int
533
testDomainGenerateIfnames(virDomainDefPtr domdef)
534
{
535
    size_t i = 0;
536 537 538 539 540 541

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

542
        ifname = testDomainGenerateIfname(domdef);
543
        if (!ifname)
544
            return -1;
545 546 547 548

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

549
    return 0;
550 551
}

552 553
/* Helper to update info for a single VCPU */
static int
554
testDomainUpdateVCPU(virDomainObjPtr dom,
555 556 557 558 559 560 561
                     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);
562
    size_t j;
H
Hu Tao 已提交
563
    bool cpu;
564 565 566 567 568 569 570 571 572 573 574

    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 已提交
575 576 577
            if (virBitmapGetBit(dom->def->cpumask, j, &cpu) < 0)
                return -1;
            if (cpu) {
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
                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
604
testDomainUpdateVCPUs(testConnPtr privconn,
605 606 607 608 609
                      virDomainObjPtr dom,
                      int nvcpus,
                      unsigned int clear_all)
{
    testDomainObjPrivatePtr privdata = dom->privateData;
610 611
    size_t i;
    int ret = -1;
612 613 614 615 616
    int cpumaplen, maxcpu;

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

617
    if (VIR_REALLOC_N(privdata->vcpu_infos, nvcpus) < 0)
618 619
        goto cleanup;

620
    if (VIR_REALLOC_N(privdata->cpumaps, nvcpus * cpumaplen) < 0)
621 622 623 624 625
        goto cleanup;

    /* Set running VCPU and cpumap state */
    if (clear_all) {
        for (i = 0; i < nvcpus; ++i)
626
            if (testDomainUpdateVCPU(dom, i, cpumaplen, maxcpu) < 0)
627 628 629 630 631
                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)
632
            if (testDomainUpdateVCPU(dom, i, cpumaplen, maxcpu) < 0)
633 634 635
                goto cleanup;
    }

636
    dom->def->vcpus = nvcpus;
637
    ret = 0;
638
 cleanup:
639 640 641
    return ret;
}

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

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

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

667
    if (testDomainUpdateVCPUs(privconn, dom, dom->def->vcpus, 1) < 0)
668 669
        goto cleanup;

J
Jiri Denemark 已提交
670
    virDomainObjSetState(dom, VIR_DOMAIN_RUNNING, reason);
671 672
    dom->def->id = privconn->nextDomID++;

673
    if (virDomainObjSetDefTransient(privconn->caps,
674
                                    privconn->xmlopt,
675
                                    dom, false) < 0) {
676 677 678
        goto cleanup;
    }

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

687 688 689 690 691 692 693

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

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

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

    conn->privateData = privconn;
723

724
    if (!(privconn->eventState = virObjectEventStateNew()))
725 726
        goto error;

727 728
    if (!(privconn->domains = virDomainObjListNew()) ||
        VIR_ALLOC(privconn->networks) < 0)
729 730
        goto error;

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

733
    /* Numa setup */
734 735 736 737 738
    privconn->numCells = 2;
    for (u = 0; u < 2; ++u) {
        privconn->cells[u].numCpus = 8;
        privconn->cells[u].mem = (u + 1) * 2048 * 1024;
    }
739
    for (u = 0; u < 16; u++) {
740
        virBitmapPtr siblings = virBitmapNew(16);
741
        if (!siblings)
742 743 744 745 746 747
            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;
748 749
    }

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

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

756 757
    privconn->nextDomID = 1;

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

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

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

781
    virObjectUnlock(domobj);
782

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

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

801
    if (!(pooldef = virStoragePoolDefParseString(defaultPoolXML)))
C
Cole Robinson 已提交
802 803
        goto error;

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

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

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

827
    virMutexUnlock(&defaultLock);
828

829 830
    return VIR_DRV_OPEN_SUCCESS;

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


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

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

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

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

912
 error:
C
Cole Robinson 已提交
913 914 915 916 917
    xmlFreeDoc(doc);
    VIR_FREE(absFile);
    return ret;
}

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

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

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

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

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

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

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

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

1001
    return 0;
1002
 error:
1003 1004 1005
    return -1;
}

1006 1007 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
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;
1059
 error:
1060 1061 1062
    return ret;
}

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

    num = virXPathNodeSet("/node/domain", ctxt, &nodes);
1074
    if (num < 0)
1075 1076
        goto error;

1077
    for (i = 0; i < num; i++) {
1078
        virDomainDefPtr def;
1079
        testDomainNamespaceDefPtr nsdata;
C
Cole Robinson 已提交
1080 1081 1082 1083 1084 1085 1086
        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,
1087
                                    VIR_DOMAIN_DEF_PARSE_INACTIVE);
C
Cole Robinson 已提交
1088 1089
        if (!def)
            goto error;
1090

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

1100 1101 1102 1103 1104
        if (testParseDomainSnapshots(privconn, obj, file, ctxt) < 0) {
            virObjectUnlock(obj);
            goto error;
        }

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

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

1120
        virObjectUnlock(obj);
1121
    }
1122

1123
    ret = 0;
1124
 error:
1125 1126 1127 1128 1129
    VIR_FREE(nodes);
    return ret;
}

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

    num = virXPathNodeSet("/node/network", ctxt, &nodes);
1140
    if (num < 0)
1141
        goto error;
1142 1143

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

C
Cole Robinson 已提交
1149 1150 1151
        def = virNetworkDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
1152

1153
        if (!(obj = virNetworkAssignDef(privconn->networks, def, false))) {
1154 1155
            virNetworkDefFree(def);
            goto error;
1156
        }
1157 1158 1159

        obj->active = 1;
        virNetworkObjUnlock(obj);
1160
    }
1161

1162
    ret = 0;
1163
 error:
1164 1165 1166 1167 1168
    VIR_FREE(nodes);
    return ret;
}

static int
C
Cole Robinson 已提交
1169 1170 1171
testParseInterfaces(testConnPtr privconn,
                    const char *file,
                    xmlXPathContextPtr ctxt)
1172 1173 1174 1175 1176 1177 1178
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virInterfaceObjPtr obj;

    num = virXPathNodeSet("/node/interface", ctxt, &nodes);
1179
    if (num < 0)
L
Laine Stump 已提交
1180
        goto error;
1181 1182

    for (i = 0; i < num; i++) {
L
Laine Stump 已提交
1183
        virInterfaceDefPtr def;
C
Cole Robinson 已提交
1184 1185 1186 1187
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                   "interface");
        if (!node)
            goto error;
L
Laine Stump 已提交
1188

C
Cole Robinson 已提交
1189 1190 1191
        def = virInterfaceDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
1192

1193
        if (!(obj = virInterfaceAssignDef(&privconn->ifaces, def))) {
L
Laine Stump 已提交
1194 1195 1196
            virInterfaceDefFree(def);
            goto error;
        }
1197

1198 1199 1200 1201 1202
        obj->active = 1;
        virInterfaceObjUnlock(obj);
    }

    ret = 0;
1203
 error:
1204 1205 1206 1207 1208
    VIR_FREE(nodes);
    return ret;
}

static int
C
Cole Robinson 已提交
1209
testOpenVolumesForPool(const char *file,
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
                       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);
1226
    if (num < 0)
1227 1228 1229
        goto error;

    for (i = 0; i < num; i++) {
C
Cole Robinson 已提交
1230 1231 1232 1233
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                   "volume");
        if (!node)
            goto error;
1234

1235
        def = virStorageVolDefParseNode(pool->def, ctxt->doc, node, 0);
C
Cole Robinson 已提交
1236 1237
        if (!def)
            goto error;
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247

        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;
1248 1249
        if (VIR_APPEND_ELEMENT_COPY(pool->volumes.objs, pool->volumes.count, def) < 0)
            goto error;
1250

1251
        pool->def->allocation += def->target.allocation;
1252 1253 1254
        pool->def->available = (pool->def->capacity -
                                pool->def->allocation);
        def = NULL;
L
Laine Stump 已提交
1255 1256
    }

1257
    ret = 0;
1258
 error:
1259 1260 1261 1262 1263 1264
    virStorageVolDefFree(def);
    VIR_FREE(nodes);
    return ret;
}

static int
C
Cole Robinson 已提交
1265 1266 1267
testParseStorage(testConnPtr privconn,
                 const char *file,
                 xmlXPathContextPtr ctxt)
1268 1269 1270 1271 1272 1273 1274
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virStoragePoolObjPtr obj;

    num = virXPathNodeSet("/node/pool", ctxt, &nodes);
1275
    if (num < 0)
C
Cole Robinson 已提交
1276
        goto error;
1277 1278

    for (i = 0; i < num; i++) {
C
Cole Robinson 已提交
1279
        virStoragePoolDefPtr def;
C
Cole Robinson 已提交
1280 1281 1282 1283
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                   "pool");
        if (!node)
            goto error;
C
Cole Robinson 已提交
1284

C
Cole Robinson 已提交
1285 1286 1287
        def = virStoragePoolDefParseNode(ctxt->doc, node);
        if (!def)
            goto error;
C
Cole Robinson 已提交
1288

1289
        if (!(obj = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
1290 1291 1292 1293 1294
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

1295 1296
        if (testStoragePoolObjSetDefaults(obj) == -1) {
            virStoragePoolObjUnlock(obj);
C
Cole Robinson 已提交
1297
            goto error;
1298
        }
1299
        obj->active = 1;
1300 1301

        /* Find storage volumes */
C
Cole Robinson 已提交
1302
        if (testOpenVolumesForPool(file, ctxt, obj, i+1) < 0) {
1303
            virStoragePoolObjUnlock(obj);
1304 1305 1306
            goto error;
        }

1307
        virStoragePoolObjUnlock(obj);
C
Cole Robinson 已提交
1308 1309
    }

1310
    ret = 0;
1311
 error:
1312 1313 1314 1315 1316
    VIR_FREE(nodes);
    return ret;
}

static int
C
Cole Robinson 已提交
1317 1318 1319
testParseNodedevs(testConnPtr privconn,
                  const char *file,
                  xmlXPathContextPtr ctxt)
1320 1321 1322 1323 1324 1325 1326
{
    int num, ret = -1;
    size_t i;
    xmlNodePtr *nodes = NULL;
    virNodeDeviceObjPtr obj;

    num = virXPathNodeSet("/node/device", ctxt, &nodes);
1327
    if (num < 0)
1328
        goto error;
1329 1330

    for (i = 0; i < num; i++) {
1331
        virNodeDeviceDefPtr def;
C
Cole Robinson 已提交
1332 1333 1334 1335
        xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
                                                  "nodedev");
        if (!node)
            goto error;
1336

C
Cole Robinson 已提交
1337 1338 1339
        def = virNodeDeviceDefParseNode(ctxt->doc, node, 0, NULL);
        if (!def)
            goto error;
1340 1341

        if (!(obj = virNodeDeviceAssignDef(&privconn->devs, def))) {
1342 1343 1344
            virNodeDeviceDefFree(def);
            goto error;
        }
1345 1346 1347 1348 1349

        virNodeDeviceObjUnlock(obj);
    }

    ret = 0;
1350
 error:
1351 1352 1353 1354
    VIR_FREE(nodes);
    return ret;
}

1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
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;
1390
 error:
1391 1392 1393
    VIR_FREE(nodes);
    return ret;
}
1394 1395 1396

/* No shared state between simultaneous test connections initialized
 * from a file.  */
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
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;
1411 1412
    }

1413 1414 1415
    testDriverLock(privconn);
    conn->privateData = privconn;

1416 1417
    if (!(privconn->domains = virDomainObjListNew()) ||
        VIR_ALLOC(privconn->networks) < 0)
1418 1419 1420 1421 1422 1423 1424 1425
        goto error;

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

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

1426
    if (!(privconn->eventState = virObjectEventStateNew()))
1427 1428
        goto error;

1429
    if (!(doc = virXMLParseFileCtxt(file, &ctxt)))
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
        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 已提交
1446
    if (testParseDomains(privconn, file, ctxt) < 0)
1447
        goto error;
C
Cole Robinson 已提交
1448
    if (testParseNetworks(privconn, file, ctxt) < 0)
1449
        goto error;
C
Cole Robinson 已提交
1450
    if (testParseInterfaces(privconn, file, ctxt) < 0)
1451
        goto error;
C
Cole Robinson 已提交
1452
    if (testParseStorage(privconn, file, ctxt) < 0)
1453
        goto error;
C
Cole Robinson 已提交
1454
    if (testParseNodedevs(privconn, file, ctxt) < 0)
1455
        goto error;
1456 1457
    if (testParseAuthUsers(privconn, ctxt) < 0)
        goto error;
1458

J
Jim Meyering 已提交
1459
    xmlXPathFreeContext(ctxt);
1460
    xmlFreeDoc(doc);
1461
    testDriverUnlock(privconn);
1462

1463
    return 0;
1464 1465

 error:
J
Jim Meyering 已提交
1466
    xmlXPathFreeContext(ctxt);
1467
    xmlFreeDoc(doc);
1468
    virObjectUnref(privconn->domains);
1469 1470
    virNetworkObjListFree(privconn->networks);
    VIR_FREE(privconn->networks);
L
Laine Stump 已提交
1471
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1472
    virStoragePoolObjListFree(&privconn->pools);
E
Eric Blake 已提交
1473
    VIR_FREE(privconn->path);
1474
    virObjectEventStateFree(privconn->eventState);
1475
    testDriverUnlock(privconn);
1476
    VIR_FREE(privconn);
1477
    conn->privateData = NULL;
1478
    return VIR_DRV_OPEN_ERROR;
1479 1480
}

1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
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;

1510
 found_user:
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
    /* 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;
1530
 cleanup:
1531 1532 1533 1534
    VIR_FREE(username);
    VIR_FREE(password);
    return ret;
}
1535

1536
static virDrvOpenStatus testConnectOpen(virConnectPtr conn,
1537
                                        virConnectAuthPtr auth,
1538
                                        unsigned int flags)
1539
{
1540
    int ret;
1541

E
Eric Blake 已提交
1542 1543
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1544
    if (!conn->uri)
1545
        return VIR_DRV_OPEN_DECLINED;
1546

1547
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1548
        return VIR_DRV_OPEN_DECLINED;
1549

1550
    /* Remote driver should handle these. */
1551
    if (conn->uri->server)
1552 1553
        return VIR_DRV_OPEN_DECLINED;

1554
    /* From this point on, the connection is for us. */
1555 1556 1557
    if (!conn->uri->path
        || conn->uri->path[0] == '\0'
        || (conn->uri->path[0] == '/' && conn->uri->path[1] == '\0')) {
1558 1559
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("testOpen: supply a path or use test:///default"));
1560 1561
        return VIR_DRV_OPEN_ERROR;
    }
1562

1563
    if (STREQ(conn->uri->path, "/default"))
1564 1565
        ret = testOpenDefault(conn);
    else
1566
        ret = testOpenFromFile(conn,
1567
                               conn->uri->path);
1568

1569 1570 1571
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

1572 1573 1574 1575
    /* Fake authentication. */
    if (testConnectAuthenticate(conn, auth) < 0)
        return VIR_DRV_OPEN_ERROR;

1576
    return VIR_DRV_OPEN_SUCCESS;
1577 1578
}

1579
static int testConnectClose(virConnectPtr conn)
1580
{
1581
    testConnPtr privconn = conn->privateData;
1582 1583 1584 1585 1586 1587 1588 1589 1590

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

1591
    testDriverLock(privconn);
1592
    virObjectUnref(privconn->caps);
1593
    virObjectUnref(privconn->xmlopt);
1594
    virObjectUnref(privconn->domains);
D
Daniel P. Berrange 已提交
1595
    virNodeDeviceObjListFree(&privconn->devs);
1596 1597
    virNetworkObjListFree(privconn->networks);
    VIR_FREE(privconn->networks);
L
Laine Stump 已提交
1598
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1599
    virStoragePoolObjListFree(&privconn->pools);
1600
    virObjectEventStateFree(privconn->eventState);
E
Eric Blake 已提交
1601
    VIR_FREE(privconn->path);
1602

1603
    testDriverUnlock(privconn);
1604
    virMutexDestroy(&privconn->lock);
1605

1606 1607 1608 1609
    if (privconn == &defaultConn)
        virMutexUnlock(&defaultLock);
    else
        VIR_FREE(privconn);
1610
    conn->privateData = NULL;
1611
    return 0;
1612 1613
}

1614 1615
static int testConnectGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                                 unsigned long *hvVer)
1616
{
1617
    *hvVer = 2;
1618
    return 0;
1619 1620
}

1621 1622 1623 1624 1625 1626
static char *testConnectGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return virGetHostname();
}


1627
static int testConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
1628 1629 1630 1631
{
    return 1;
}

1632
static int testConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
1633 1634 1635 1636
{
    return 0;
}

1637
static int testConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
1638 1639 1640 1641
{
    return 1;
}

1642 1643
static int testConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type ATTRIBUTE_UNUSED)
1644 1645 1646 1647
{
    return 32;
}

1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
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;
}

1663 1664
static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1665
{
1666
    testConnPtr privconn = conn->privateData;
1667
    testDriverLock(privconn);
1668
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1669
    testDriverUnlock(privconn);
1670
    return 0;
1671 1672
}

1673
static char *testConnectGetCapabilities(virConnectPtr conn)
1674
{
1675
    testConnPtr privconn = conn->privateData;
1676
    char *xml;
1677
    testDriverLock(privconn);
1678
    xml = virCapabilitiesFormatXML(privconn->caps);
1679
    testDriverUnlock(privconn);
1680
    return xml;
1681 1682
}

1683
static int testConnectNumOfDomains(virConnectPtr conn)
1684
{
1685
    testConnPtr privconn = conn->privateData;
1686
    int count;
1687

1688
    testDriverLock(privconn);
1689
    count = virDomainObjListNumOfDomains(privconn->domains, true, NULL, NULL);
1690
    testDriverUnlock(privconn);
1691

1692
    return count;
1693 1694
}

1695 1696 1697 1698 1699 1700 1701
static int testDomainIsActive(virDomainPtr dom)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
1702
    obj = virDomainObjListFindByUUID(privconn->domains, dom->uuid);
1703 1704
    testDriverUnlock(privconn);
    if (!obj) {
1705
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1706 1707 1708 1709
        goto cleanup;
    }
    ret = virDomainObjIsActive(obj);

1710
 cleanup:
1711
    if (obj)
1712
        virObjectUnlock(obj);
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
    return ret;
}

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

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

1731
 cleanup:
1732
    if (obj)
1733
        virObjectUnlock(obj);
1734 1735 1736
    return ret;
}

1737 1738 1739 1740 1741
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

1742
static virDomainPtr
1743
testDomainCreateXML(virConnectPtr conn, const char *xml,
1744
                      unsigned int flags)
1745
{
1746
    testConnPtr privconn = conn->privateData;
1747
    virDomainPtr ret = NULL;
1748
    virDomainDefPtr def;
1749
    virDomainObjPtr dom = NULL;
1750
    virObjectEventPtr event = NULL;
1751
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;
1752

1753 1754 1755 1756
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

    if (flags & VIR_DOMAIN_START_VALIDATE)
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE;
1757

1758
    testDriverLock(privconn);
1759
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
1760
                                       1 << VIR_DOMAIN_VIRT_TEST,
1761
                                       parse_flags)) == NULL)
1762
        goto cleanup;
1763

1764
    if (testDomainGenerateIfnames(def) < 0)
1765
        goto cleanup;
1766
    if (!(dom = virDomainObjListAdd(privconn->domains,
1767
                                    def,
1768
                                    privconn->xmlopt,
1769 1770
                                    VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                    NULL)))
1771 1772
        goto cleanup;
    def = NULL;
1773

1774
    if (testDomainStartState(privconn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0)
1775
        goto cleanup;
1776

1777
    event = virDomainEventLifecycleNewFromObj(dom,
1778 1779 1780
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1781
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1782
    if (ret)
1783
        ret->id = dom->def->id;
1784

1785
 cleanup:
1786
    if (dom)
1787
        virObjectUnlock(dom);
1788
    if (event)
1789
        testObjectEventQueue(privconn, event);
1790
    virDomainDefFree(def);
1791
    testDriverUnlock(privconn);
1792
    return ret;
1793 1794 1795
}


1796
static virDomainPtr testDomainLookupByID(virConnectPtr conn,
1797
                                         int id)
1798
{
1799
    testConnPtr privconn = conn->privateData;
1800 1801
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1802

1803
    testDriverLock(privconn);
1804
    dom = virDomainObjListFindByID(privconn->domains, id);
1805 1806 1807
    testDriverUnlock(privconn);

    if (dom == NULL) {
1808
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1809
        goto cleanup;
1810 1811
    }

1812
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1813 1814 1815
    if (ret)
        ret->id = dom->def->id;

1816
 cleanup:
1817
    if (dom)
1818
        virObjectUnlock(dom);
1819
    return ret;
1820 1821
}

1822
static virDomainPtr testDomainLookupByUUID(virConnectPtr conn,
1823
                                           const unsigned char *uuid)
1824
{
1825
    testConnPtr privconn = conn->privateData;
1826
    virDomainPtr ret = NULL;
1827
    virDomainObjPtr dom;
1828

1829
    testDriverLock(privconn);
1830
    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
1831 1832 1833
    testDriverUnlock(privconn);

    if (dom == NULL) {
1834
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1835
        goto cleanup;
1836
    }
1837

1838
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1839 1840 1841
    if (ret)
        ret->id = dom->def->id;

1842
 cleanup:
1843
    if (dom)
1844
        virObjectUnlock(dom);
1845
    return ret;
1846 1847
}

1848
static virDomainPtr testDomainLookupByName(virConnectPtr conn,
1849
                                           const char *name)
1850
{
1851
    testConnPtr privconn = conn->privateData;
1852 1853
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1854

1855
    testDriverLock(privconn);
1856
    dom = virDomainObjListFindByName(privconn->domains, name);
1857 1858 1859
    testDriverUnlock(privconn);

    if (dom == NULL) {
1860
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1861
        goto cleanup;
1862
    }
1863

1864
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1865 1866 1867
    if (ret)
        ret->id = dom->def->id;

1868
 cleanup:
1869
    if (dom)
1870
        virObjectUnlock(dom);
1871
    return ret;
1872 1873
}

1874 1875 1876
static int testConnectListDomains(virConnectPtr conn,
                                  int *ids,
                                  int maxids)
1877
{
1878
    testConnPtr privconn = conn->privateData;
1879
    int n;
1880

1881
    testDriverLock(privconn);
1882
    n = virDomainObjListGetActiveIDs(privconn->domains, ids, maxids, NULL, NULL);
1883
    testDriverUnlock(privconn);
1884

1885
    return n;
1886 1887
}

1888
static int testDomainDestroy(virDomainPtr domain)
1889
{
1890 1891
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1892
    virObjectEventPtr event = NULL;
1893
    int ret = -1;
1894

1895
    testDriverLock(privconn);
1896 1897
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1898 1899

    if (privdom == NULL) {
1900
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1901
        goto cleanup;
1902
    }
1903

J
Jiri Denemark 已提交
1904
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1905
    event = virDomainEventLifecycleNewFromObj(privdom,
1906 1907
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1908

1909
    if (!privdom->persistent) {
1910 1911
        virDomainObjListRemove(privconn->domains,
                               privdom);
1912
        privdom = NULL;
1913
    }
1914 1915

    ret = 0;
1916
 cleanup:
1917
    if (privdom)
1918
        virObjectUnlock(privdom);
1919
    if (event)
1920
        testObjectEventQueue(privconn, event);
1921
    testDriverUnlock(privconn);
1922
    return ret;
1923 1924
}

1925
static int testDomainResume(virDomainPtr domain)
1926
{
1927 1928
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1929
    virObjectEventPtr event = NULL;
1930
    int ret = -1;
1931

1932
    testDriverLock(privconn);
1933 1934
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1935
    testDriverUnlock(privconn);
1936 1937

    if (privdom == NULL) {
1938
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1939
        goto cleanup;
1940
    }
1941

J
Jiri Denemark 已提交
1942
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_PAUSED) {
1943 1944
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
                       domain->name);
1945
        goto cleanup;
1946
    }
1947

J
Jiri Denemark 已提交
1948 1949
    virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                         VIR_DOMAIN_RUNNING_UNPAUSED);
1950
    event = virDomainEventLifecycleNewFromObj(privdom,
1951 1952
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1953 1954
    ret = 0;

1955
 cleanup:
1956
    if (privdom)
1957
        virObjectUnlock(privdom);
1958 1959
    if (event) {
        testDriverLock(privconn);
1960
        testObjectEventQueue(privconn, event);
1961 1962
        testDriverUnlock(privconn);
    }
1963
    return ret;
1964 1965
}

1966
static int testDomainSuspend(virDomainPtr domain)
1967
{
1968 1969
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1970
    virObjectEventPtr event = NULL;
1971
    int ret = -1;
J
Jiri Denemark 已提交
1972
    int state;
1973

1974
    testDriverLock(privconn);
1975 1976
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1977
    testDriverUnlock(privconn);
1978 1979

    if (privdom == NULL) {
1980
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1981
        goto cleanup;
1982
    }
1983

J
Jiri Denemark 已提交
1984 1985
    state = virDomainObjGetState(privdom, NULL);
    if (state == VIR_DOMAIN_SHUTOFF || state == VIR_DOMAIN_PAUSED) {
1986 1987
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
                       domain->name);
1988
        goto cleanup;
1989
    }
1990

J
Jiri Denemark 已提交
1991
    virDomainObjSetState(privdom, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
1992
    event = virDomainEventLifecycleNewFromObj(privdom,
1993 1994
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1995 1996
    ret = 0;

1997
 cleanup:
1998
    if (privdom)
1999
        virObjectUnlock(privdom);
2000 2001 2002

    if (event) {
        testDriverLock(privconn);
2003
        testObjectEventQueue(privconn, event);
2004 2005
        testDriverUnlock(privconn);
    }
2006
    return ret;
2007 2008
}

2009
static int testDomainShutdownFlags(virDomainPtr domain,
2010
                                   unsigned int flags)
2011
{
2012 2013
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2014
    virObjectEventPtr event = NULL;
2015
    int ret = -1;
2016

2017 2018
    virCheckFlags(0, -1);

2019
    testDriverLock(privconn);
2020 2021
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2022 2023

    if (privdom == NULL) {
2024
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2025
        goto cleanup;
2026
    }
2027

J
Jiri Denemark 已提交
2028
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
2029 2030
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain '%s' not running"), domain->name);
2031
        goto cleanup;
2032
    }
2033

J
Jiri Denemark 已提交
2034
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
2035
    event = virDomainEventLifecycleNewFromObj(privdom,
2036 2037
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
2038

2039
    if (!privdom->persistent) {
2040 2041
        virDomainObjListRemove(privconn->domains,
                               privdom);
2042 2043
        privdom = NULL;
    }
2044

2045
    ret = 0;
2046
 cleanup:
2047
    if (privdom)
2048
        virObjectUnlock(privdom);
2049
    if (event)
2050
        testObjectEventQueue(privconn, event);
2051
    testDriverUnlock(privconn);
2052
    return ret;
2053 2054
}

2055
static int testDomainShutdown(virDomainPtr domain)
2056
{
2057
    return testDomainShutdownFlags(domain, 0);
2058 2059
}

2060
/* Similar behaviour as shutdown */
2061
static int testDomainReboot(virDomainPtr domain,
2062
                            unsigned int action ATTRIBUTE_UNUSED)
2063
{
2064 2065
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2066
    virObjectEventPtr event = NULL;
2067
    int ret = -1;
2068

2069
    testDriverLock(privconn);
2070 2071
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2072 2073

    if (privdom == NULL) {
2074
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2075
        goto cleanup;
2076
    }
2077

J
Jiri Denemark 已提交
2078 2079 2080
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

2081 2082
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
J
Jiri Denemark 已提交
2083 2084
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
2085 2086
        break;

2087
    case VIR_DOMAIN_LIFECYCLE_RESTART:
J
Jiri Denemark 已提交
2088 2089
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
2090 2091
        break;

2092
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
J
Jiri Denemark 已提交
2093 2094
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
2095 2096
        break;

2097
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
J
Jiri Denemark 已提交
2098 2099
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
2100
        break;
2101

2102
    default:
J
Jiri Denemark 已提交
2103 2104
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
2105 2106
        break;
    }
2107

J
Jiri Denemark 已提交
2108 2109
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
2110
        event = virDomainEventLifecycleNewFromObj(privdom,
2111 2112
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
2113

2114
        if (!privdom->persistent) {
2115 2116
            virDomainObjListRemove(privconn->domains,
                                   privdom);
2117 2118
            privdom = NULL;
        }
2119 2120
    }

2121
    ret = 0;
2122
 cleanup:
2123
    if (privdom)
2124
        virObjectUnlock(privdom);
2125
    if (event)
2126
        testObjectEventQueue(privconn, event);
2127
    testDriverUnlock(privconn);
2128
    return ret;
2129 2130
}

2131
static int testDomainGetInfo(virDomainPtr domain,
2132
                             virDomainInfoPtr info)
2133
{
2134
    testConnPtr privconn = domain->conn->privateData;
2135
    struct timeval tv;
2136
    virDomainObjPtr privdom;
2137
    int ret = -1;
2138

2139
    testDriverLock(privconn);
2140 2141
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2142
    testDriverUnlock(privconn);
2143 2144

    if (privdom == NULL) {
2145
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2146
        goto cleanup;
2147
    }
2148 2149

    if (gettimeofday(&tv, NULL) < 0) {
2150 2151
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("getting time of day"));
2152
        goto cleanup;
2153 2154
    }

J
Jiri Denemark 已提交
2155
    info->state = virDomainObjGetState(privdom, NULL);
2156 2157
    info->memory = privdom->def->mem.cur_balloon;
    info->maxMem = privdom->def->mem.max_balloon;
2158 2159
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
2160 2161
    ret = 0;

2162
 cleanup:
2163
    if (privdom)
2164
        virObjectUnlock(privdom);
2165
    return ret;
2166 2167
}

2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
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);
2181 2182
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2183 2184 2185
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2186
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2187 2188 2189
        goto cleanup;
    }

J
Jiri Denemark 已提交
2190
    *state = virDomainObjGetState(privdom, reason);
2191 2192
    ret = 0;

2193
 cleanup:
2194
    if (privdom)
2195
        virObjectUnlock(privdom);
2196 2197 2198
    return ret;
}

2199 2200
#define TEST_SAVE_MAGIC "TestGuestMagic"

2201 2202 2203
static int
testDomainSaveFlags(virDomainPtr domain, const char *path,
                    const char *dxml, unsigned int flags)
2204
{
2205
    testConnPtr privconn = domain->conn->privateData;
2206 2207 2208
    char *xml = NULL;
    int fd = -1;
    int len;
2209
    virDomainObjPtr privdom;
2210
    virObjectEventPtr event = NULL;
2211
    int ret = -1;
2212

2213 2214
    virCheckFlags(0, -1);
    if (dxml) {
2215 2216
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
2217 2218 2219
        return -1;
    }

2220
    testDriverLock(privconn);
2221 2222
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2223 2224

    if (privdom == NULL) {
2225
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2226
        goto cleanup;
2227
    }
2228

2229
    xml = virDomainDefFormat(privdom->def,
2230
                             VIR_DOMAIN_DEF_FORMAT_SECURE);
C
Cole Robinson 已提交
2231

2232
    if (xml == NULL) {
2233
        virReportSystemError(errno,
2234 2235
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
2236
        goto cleanup;
2237
    }
2238 2239

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
2240
        virReportSystemError(errno,
2241 2242
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
2243
        goto cleanup;
2244
    }
2245
    len = strlen(xml);
2246
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
2247
        virReportSystemError(errno,
2248 2249
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2250
        goto cleanup;
2251
    }
2252
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
2253
        virReportSystemError(errno,
2254 2255
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2256
        goto cleanup;
2257
    }
2258
    if (safewrite(fd, xml, len) < 0) {
2259
        virReportSystemError(errno,
2260 2261
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2262
        goto cleanup;
2263
    }
2264

2265
    if (VIR_CLOSE(fd) < 0) {
2266
        virReportSystemError(errno,
2267 2268
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
2269
        goto cleanup;
2270
    }
2271 2272
    fd = -1;

J
Jiri Denemark 已提交
2273
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
2274
    event = virDomainEventLifecycleNewFromObj(privdom,
2275 2276
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
2277

2278
    if (!privdom->persistent) {
2279 2280
        virDomainObjListRemove(privconn->domains,
                               privdom);
2281
        privdom = NULL;
2282
    }
2283

2284
    ret = 0;
2285
 cleanup:
2286 2287 2288 2289 2290 2291
    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) {
2292
        VIR_FORCE_CLOSE(fd);
2293 2294
        unlink(path);
    }
2295
    if (privdom)
2296
        virObjectUnlock(privdom);
2297
    if (event)
2298
        testObjectEventQueue(privconn, event);
2299
    testDriverUnlock(privconn);
2300
    return ret;
2301 2302
}

2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314
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)
2315
{
2316
    testConnPtr privconn = conn->privateData;
2317
    char *xml = NULL;
2318
    char magic[15];
2319 2320 2321
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
2322
    virDomainObjPtr dom = NULL;
2323
    virObjectEventPtr event = NULL;
2324
    int ret = -1;
2325

2326 2327
    virCheckFlags(0, -1);
    if (dxml) {
2328 2329
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
2330 2331 2332
        return -1;
    }

2333 2334
    testDriverLock(privconn);

2335
    if ((fd = open(path, O_RDONLY)) < 0) {
2336
        virReportSystemError(errno,
2337 2338
                             _("cannot read domain image '%s'"),
                             path);
2339
        goto cleanup;
2340
    }
2341
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
2342
        virReportSystemError(errno,
2343 2344
                             _("incomplete save header in '%s'"),
                             path);
2345
        goto cleanup;
2346
    }
2347
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
2348 2349
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("mismatched header magic"));
2350
        goto cleanup;
2351
    }
2352
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
2353
        virReportSystemError(errno,
2354 2355
                             _("failed to read metadata length in '%s'"),
                             path);
2356
        goto cleanup;
2357 2358
    }
    if (len < 1 || len > 8192) {
2359 2360
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("length of metadata out of range"));
2361
        goto cleanup;
2362
    }
2363
    if (VIR_ALLOC_N(xml, len+1) < 0)
2364
        goto cleanup;
2365
    if (saferead(fd, xml, len) != len) {
2366
        virReportSystemError(errno,
2367
                             _("incomplete metadata in '%s'"), path);
2368
        goto cleanup;
2369 2370
    }
    xml[len] = '\0';
2371

2372 2373
    def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
                                  1 << VIR_DOMAIN_VIRT_TEST,
2374
                                  VIR_DOMAIN_DEF_PARSE_INACTIVE);
2375
    if (!def)
2376
        goto cleanup;
2377

2378
    if (testDomainGenerateIfnames(def) < 0)
2379
        goto cleanup;
2380
    if (!(dom = virDomainObjListAdd(privconn->domains,
2381
                                    def,
2382
                                    privconn->xmlopt,
2383 2384 2385
                                    VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
                                    VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                    NULL)))
2386 2387
        goto cleanup;
    def = NULL;
2388

2389
    if (testDomainStartState(privconn, dom, VIR_DOMAIN_RUNNING_RESTORED) < 0)
2390 2391
        goto cleanup;

2392
    event = virDomainEventLifecycleNewFromObj(dom,
2393 2394
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
2395
    ret = 0;
2396

2397
 cleanup:
2398 2399
    virDomainDefFree(def);
    VIR_FREE(xml);
2400
    VIR_FORCE_CLOSE(fd);
2401
    if (dom)
2402
        virObjectUnlock(dom);
2403
    if (event)
2404
        testObjectEventQueue(privconn, event);
2405
    testDriverUnlock(privconn);
2406
    return ret;
2407 2408
}

2409 2410 2411 2412 2413 2414 2415
static int
testDomainRestore(virConnectPtr conn,
                  const char *path)
{
    return testDomainRestoreFlags(conn, path, NULL, 0);
}

2416 2417 2418 2419
static int testDomainCoreDumpWithFormat(virDomainPtr domain,
                                        const char *to,
                                        unsigned int dumpformat,
                                        unsigned int flags)
2420
{
2421
    testConnPtr privconn = domain->conn->privateData;
2422
    int fd = -1;
2423
    virDomainObjPtr privdom;
2424
    virObjectEventPtr event = NULL;
2425
    int ret = -1;
2426

E
Eric Blake 已提交
2427 2428
    virCheckFlags(VIR_DUMP_CRASH, -1);

2429
    testDriverLock(privconn);
2430 2431
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2432 2433

    if (privdom == NULL) {
2434
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2435
        goto cleanup;
2436
    }
2437 2438

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
2439
        virReportSystemError(errno,
2440 2441
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
2442
        goto cleanup;
2443
    }
2444
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
2445
        virReportSystemError(errno,
2446 2447
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
2448
        goto cleanup;
2449
    }
2450
    if (VIR_CLOSE(fd) < 0) {
2451
        virReportSystemError(errno,
2452 2453
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
2454
        goto cleanup;
2455
    }
2456

2457 2458 2459 2460 2461 2462 2463
    /* we don't support non-raw formats in test driver */
    if (dumpformat != VIR_DOMAIN_CORE_DUMP_FORMAT_RAW) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("kdump-compressed format is not supported here"));
        goto cleanup;
    }

2464
    if (flags & VIR_DUMP_CRASH) {
J
Jiri Denemark 已提交
2465
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_CRASHED);
2466
        event = virDomainEventLifecycleNewFromObj(privdom,
2467 2468 2469
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
        if (!privdom->persistent) {
2470 2471
            virDomainObjListRemove(privconn->domains,
                                   privdom);
2472 2473
            privdom = NULL;
        }
2474
    }
2475

2476
    ret = 0;
2477
 cleanup:
2478
    VIR_FORCE_CLOSE(fd);
2479
    if (privdom)
2480
        virObjectUnlock(privdom);
2481
    if (event)
2482
        testObjectEventQueue(privconn, event);
2483
    testDriverUnlock(privconn);
2484
    return ret;
2485 2486
}

2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500

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


static char *
testDomainGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED)
{
2501 2502 2503
    char *ret;

    ignore_value(VIR_STRDUP(ret, "linux"));
2504
    return ret;
2505 2506
}

2507 2508 2509

static unsigned long long
testDomainGetMaxMemory(virDomainPtr domain)
2510
{
2511 2512
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2513
    unsigned long long ret = 0;
2514

2515
    testDriverLock(privconn);
2516 2517
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2518
    testDriverUnlock(privconn);
2519 2520

    if (privdom == NULL) {
2521
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2522
        goto cleanup;
2523
    }
2524

2525
    ret = privdom->def->mem.max_balloon;
2526

2527
 cleanup:
2528
    if (privdom)
2529
        virObjectUnlock(privdom);
2530
    return ret;
2531 2532
}

2533 2534
static int testDomainSetMaxMemory(virDomainPtr domain,
                                  unsigned long memory)
2535
{
2536 2537
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2538
    int ret = -1;
2539

2540
    testDriverLock(privconn);
2541 2542
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2543
    testDriverUnlock(privconn);
2544 2545

    if (privdom == NULL) {
2546
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2547
        goto cleanup;
2548
    }
2549 2550

    /* XXX validate not over host memory wrt to other domains */
2551
    privdom->def->mem.max_balloon = memory;
2552 2553
    ret = 0;

2554
 cleanup:
2555
    if (privdom)
2556
        virObjectUnlock(privdom);
2557
    return ret;
2558 2559
}

2560 2561
static int testDomainSetMemory(virDomainPtr domain,
                               unsigned long memory)
2562
{
2563 2564
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2565
    int ret = -1;
2566

2567
    testDriverLock(privconn);
2568 2569
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2570
    testDriverUnlock(privconn);
2571 2572

    if (privdom == NULL) {
2573
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2574
        goto cleanup;
2575
    }
2576

2577
    if (memory > privdom->def->mem.max_balloon) {
2578
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2579
        goto cleanup;
2580
    }
2581

2582
    privdom->def->mem.cur_balloon = memory;
2583 2584
    ret = 0;

2585
 cleanup:
2586
    if (privdom)
2587
        virObjectUnlock(privdom);
2588
    return ret;
2589 2590
}

2591 2592
static int
testDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
C
Cole Robinson 已提交
2593
{
2594 2595 2596 2597 2598
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;

2599 2600
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2601 2602 2603
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    testDriverLock(privconn);
2604
    vm = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
2605 2606 2607 2608 2609
    testDriverUnlock(privconn);

    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(domain->uuid, uuidstr);
2610 2611
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
2612 2613 2614
        goto cleanup;
    }

2615
    if (virDomainLiveConfigHelperMethod(privconn->caps, privconn->xmlopt,
2616
                                        vm, &flags, &def) < 0)
2617
        goto cleanup;
2618

2619
    if (flags & VIR_DOMAIN_AFFECT_LIVE)
2620 2621 2622 2623
        def = vm->def;

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

2624
 cleanup:
2625
    if (vm)
2626
        virObjectUnlock(vm);
2627
    return ret;
C
Cole Robinson 已提交
2628 2629
}

2630 2631 2632
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
2633
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2634 2635 2636 2637 2638 2639 2640
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

static int
testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
                        unsigned int flags)
{
2641
    testConnPtr privconn = domain->conn->privateData;
2642
    virDomainObjPtr privdom = NULL;
2643
    virDomainDefPtr persistentDef;
C
Cole Robinson 已提交
2644 2645
    int ret = -1, maxvcpus;

2646 2647
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2648 2649 2650 2651
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
2652 2653 2654
    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)) {
2655 2656
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
2657 2658
        return -1;
    }
2659
    if (!nrCpus || (maxvcpus = testConnectGetMaxVcpus(domain->conn, NULL)) < nrCpus) {
2660 2661
        virReportError(VIR_ERR_INVALID_ARG,
                       _("argument out of range: %d"), nrCpus);
2662 2663
        return -1;
    }
2664

2665
    testDriverLock(privconn);
2666
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
2667 2668 2669
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2670
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2671
        goto cleanup;
2672
    }
2673

2674
    if (!virDomainObjIsActive(privdom) && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
2675 2676
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot hotplug vcpus for an inactive domain"));
C
Cole Robinson 已提交
2677 2678 2679
        goto cleanup;
    }

2680 2681
    /* We allow more cpus in guest than host, but not more than the
     * domain's starting limit.  */
C
Cole Robinson 已提交
2682 2683
    if (!(flags & (VIR_DOMAIN_VCPU_MAXIMUM)) &&
        privdom->def->maxvcpus < maxvcpus)
2684
        maxvcpus = privdom->def->maxvcpus;
C
Cole Robinson 已提交
2685

C
Cole Robinson 已提交
2686
    if (nrCpus > maxvcpus) {
2687 2688 2689
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested cpu amount exceeds maximum (%d > %d)"),
                       nrCpus, maxvcpus);
2690
        goto cleanup;
2691
    }
2692

2693
    if (!(persistentDef = virDomainObjGetPersistentDef(privconn->caps,
2694
                                                       privconn->xmlopt,
2695 2696 2697
                                                       privdom)))
        goto cleanup;

2698
    switch (flags) {
2699
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_CONFIG:
2700 2701 2702
        persistentDef->maxvcpus = nrCpus;
        if (nrCpus < persistentDef->vcpus)
            persistentDef->vcpus = nrCpus;
2703 2704
        ret = 0;
        break;
2705

2706
    case VIR_DOMAIN_AFFECT_CONFIG:
2707
        persistentDef->vcpus = nrCpus;
2708 2709 2710
        ret = 0;
        break;

2711
    case VIR_DOMAIN_AFFECT_LIVE:
2712
        ret = testDomainUpdateVCPUs(privconn, privdom, nrCpus, 0);
2713 2714
        break;

2715
    case VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG:
2716
        ret = testDomainUpdateVCPUs(privconn, privdom, nrCpus, 0);
2717
        if (ret == 0)
2718
            persistentDef->vcpus = nrCpus;
2719 2720
        break;
    }
2721

2722
 cleanup:
2723
    if (privdom)
2724
        virObjectUnlock(privdom);
2725
    return ret;
2726 2727
}

2728
static int
2729
testDomainSetVcpus(virDomainPtr domain, unsigned int nrCpus)
2730
{
2731
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2732 2733
}

C
Cole Robinson 已提交
2734 2735 2736 2737 2738 2739 2740 2741 2742
static int testDomainGetVcpus(virDomainPtr domain,
                              virVcpuInfoPtr info,
                              int maxinfo,
                              unsigned char *cpumaps,
                              int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
2743 2744
    size_t i;
    int v, maxcpu, hostcpus;
C
Cole Robinson 已提交
2745 2746 2747 2748 2749
    int ret = -1;
    struct timeval tv;
    unsigned long long statbase;

    testDriverLock(privconn);
2750
    privdom = virDomainObjListFindByName(privconn->domains, domain->name);
C
Cole Robinson 已提交
2751 2752 2753
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2754
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2755 2756 2757 2758
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2759
        virReportError(VIR_ERR_OPERATION_INVALID,
2760
                       "%s", _("cannot list vcpus for an inactive domain"));
C
Cole Robinson 已提交
2761 2762 2763 2764 2765 2766
        goto cleanup;
    }

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
2767
        virReportSystemError(errno,
C
Cole Robinson 已提交
2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787
                             "%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);

2788
        for (i = 0; i < maxinfo; i++) {
C
Cole Robinson 已提交
2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
            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);

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

2808
            for (i = 0; i < maxcpu; i++) {
2809
                if (VIR_CPU_USABLE(privdomdata->cpumaps, privmaplen, v, i))
C
Cole Robinson 已提交
2810 2811 2812 2813 2814 2815
                    VIR_USE_CPU(cpumap, i);
            }
        }
    }

    ret = maxinfo;
2816
 cleanup:
C
Cole Robinson 已提交
2817
    if (privdom)
2818
        virObjectUnlock(privdom);
C
Cole Robinson 已提交
2819 2820 2821
    return ret;
}

C
Cole Robinson 已提交
2822 2823 2824 2825 2826 2827 2828 2829 2830
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;
2831 2832
    size_t i;
    int maxcpu, hostcpus, privmaplen;
C
Cole Robinson 已提交
2833 2834 2835
    int ret = -1;

    testDriverLock(privconn);
2836
    privdom = virDomainObjListFindByName(privconn->domains, domain->name);
C
Cole Robinson 已提交
2837 2838 2839
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2840
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2841 2842 2843 2844
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2845
        virReportError(VIR_ERR_OPERATION_INVALID,
2846
                       "%s", _("cannot pin vcpus on an inactive domain"));
C
Cole Robinson 已提交
2847 2848 2849 2850
        goto cleanup;
    }

    if (vcpu > privdom->def->vcpus) {
2851 2852
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("requested vcpu is higher than allocated vcpus"));
C
Cole Robinson 已提交
2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
        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);

2867
    for (i = 0; i < maxcpu; i++) {
2868
        if (VIR_CPU_USABLE(cpumap, maplen, 0, i))
C
Cole Robinson 已提交
2869 2870 2871 2872
            VIR_USE_CPU(privcpumap, i);
    }

    ret = 0;
2873
 cleanup:
C
Cole Robinson 已提交
2874
    if (privdom)
2875
        virObjectUnlock(privdom);
C
Cole Robinson 已提交
2876 2877 2878
    return ret;
}

2879
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2880
{
2881
    testConnPtr privconn = domain->conn->privateData;
2882
    virDomainDefPtr def;
2883
    virDomainObjPtr privdom;
2884 2885
    char *ret = NULL;

2886 2887
    /* Flags checked by virDomainDefFormat */

2888
    testDriverLock(privconn);
2889 2890
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2891 2892 2893
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2894
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2895
        goto cleanup;
2896
    }
2897

2898 2899
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2900

2901
    ret = virDomainDefFormat(def,
2902
                             virDomainDefFormatConvertXMLFlags(flags));
2903

2904
 cleanup:
2905
    if (privdom)
2906
        virObjectUnlock(privdom);
2907
    return ret;
2908
}
2909

2910 2911
static int testConnectNumOfDefinedDomains(virConnectPtr conn)
{
2912
    testConnPtr privconn = conn->privateData;
2913
    int count;
2914

2915
    testDriverLock(privconn);
2916
    count = virDomainObjListNumOfDomains(privconn->domains, false, NULL, NULL);
2917
    testDriverUnlock(privconn);
2918

2919
    return count;
2920 2921
}

2922 2923
static int testConnectListDefinedDomains(virConnectPtr conn,
                                         char **const names,
2924 2925
                                         int maxnames)
{
2926

2927
    testConnPtr privconn = conn->privateData;
2928
    int n;
2929

2930
    testDriverLock(privconn);
2931
    memset(names, 0, sizeof(*names)*maxnames);
2932 2933
    n = virDomainObjListGetInactiveNames(privconn->domains, names, maxnames,
                                         NULL, NULL);
2934
    testDriverUnlock(privconn);
2935

2936
    return n;
2937 2938
}

2939 2940 2941
static virDomainPtr testDomainDefineXMLFlags(virConnectPtr conn,
                                             const char *xml,
                                             unsigned int flags)
2942
{
2943
    testConnPtr privconn = conn->privateData;
2944
    virDomainPtr ret = NULL;
2945
    virDomainDefPtr def;
2946
    virDomainObjPtr dom = NULL;
2947
    virObjectEventPtr event = NULL;
2948
    virDomainDefPtr oldDef = NULL;
2949 2950 2951
    unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE;

    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);
2952

2953 2954
    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE;
2955

2956
    testDriverLock(privconn);
2957 2958
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
                                       1 << VIR_DOMAIN_VIRT_TEST,
2959
                                       parse_flags)) == NULL)
2960
        goto cleanup;
2961

2962
    if (testDomainGenerateIfnames(def) < 0)
2963
        goto cleanup;
2964
    if (!(dom = virDomainObjListAdd(privconn->domains,
2965
                                    def,
2966
                                    privconn->xmlopt,
2967 2968
                                    0,
                                    &oldDef)))
2969
        goto cleanup;
2970
    def = NULL;
2971
    dom->persistent = 1;
2972

2973
    event = virDomainEventLifecycleNewFromObj(dom,
2974
                                     VIR_DOMAIN_EVENT_DEFINED,
2975
                                     !oldDef ?
2976 2977
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2978

2979
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2980
    if (ret)
2981
        ret->id = dom->def->id;
2982

2983
 cleanup:
2984
    virDomainDefFree(def);
2985
    virDomainDefFree(oldDef);
2986
    if (dom)
2987
        virObjectUnlock(dom);
2988
    if (event)
2989
        testObjectEventQueue(privconn, event);
2990
    testDriverUnlock(privconn);
2991
    return ret;
2992 2993
}

2994 2995 2996 2997 2998 2999
static virDomainPtr
testDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return testDomainDefineXMLFlags(conn, xml, 0);
}

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

3025
 cleanup:
3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056
    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,
3057
                                  NULL, NULL, flags);
3058

3059
 cleanup:
3060 3061 3062 3063 3064 3065
    if (privdom)
        virObjectUnlock(privdom);
    return ret;
}


3066 3067
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
3068 3069
                                      int startCell, int maxCells)
{
3070
    testConnPtr privconn = conn->privateData;
3071 3072
    int cell;
    size_t i;
3073
    int ret = -1;
3074

3075
    testDriverLock(privconn);
3076
    if (startCell > privconn->numCells) {
3077 3078
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("Range exceeds available cells"));
3079
        goto cleanup;
3080 3081
    }

3082 3083 3084 3085
    for (cell = startCell, i = 0;
         (cell < privconn->numCells && i < maxCells);
         ++cell, ++i) {
        freemems[i] = privconn->cells[cell].mem;
3086
    }
3087
    ret = i;
3088

3089
 cleanup:
3090
    testDriverUnlock(privconn);
3091
    return ret;
3092 3093 3094
}


3095 3096
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags)
{
3097 3098
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
3099
    virObjectEventPtr event = NULL;
3100
    int ret = -1;
3101

3102 3103
    virCheckFlags(0, -1);

3104
    testDriverLock(privconn);
3105 3106
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3107 3108

    if (privdom == NULL) {
3109
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3110
        goto cleanup;
3111
    }
3112

J
Jiri Denemark 已提交
3113
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
3114 3115
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Domain '%s' is already running"), domain->name);
3116
        goto cleanup;
3117 3118
    }

3119
    if (testDomainStartState(privconn, privdom,
J
Jiri Denemark 已提交
3120
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
3121 3122 3123
        goto cleanup;
    domain->id = privdom->def->id;

3124
    event = virDomainEventLifecycleNewFromObj(privdom,
3125 3126
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
3127
    ret = 0;
3128

3129
 cleanup:
3130
    if (privdom)
3131
        virObjectUnlock(privdom);
3132
    if (event)
3133
        testObjectEventQueue(privconn, event);
3134
    testDriverUnlock(privconn);
3135
    return ret;
3136 3137
}

3138 3139
static int testDomainCreate(virDomainPtr domain)
{
3140 3141 3142
    return testDomainCreateWithFlags(domain, 0);
}

3143 3144 3145
static int testDomainUndefineFlags(virDomainPtr domain,
                                   unsigned int flags)
{
3146 3147
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
3148
    virObjectEventPtr event = NULL;
3149
    int nsnapshots;
3150
    int ret = -1;
3151

3152 3153
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
3154

3155
    testDriverLock(privconn);
3156 3157
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3158 3159

    if (privdom == NULL) {
3160
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3161
        goto cleanup;
3162
    }
3163

C
Cole Robinson 已提交
3164 3165 3166 3167 3168 3169 3170 3171
    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;
    }

3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189
    /* 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. */
    }

3190
    event = virDomainEventLifecycleNewFromObj(privdom,
3191 3192
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
C
Cole Robinson 已提交
3193 3194
    privdom->hasManagedSave = false;

3195 3196
    if (virDomainObjIsActive(privdom)) {
        privdom->persistent = 0;
3197
    } else {
3198 3199
        virDomainObjListRemove(privconn->domains,
                               privdom);
3200 3201 3202
        privdom = NULL;
    }

3203
    ret = 0;
3204

3205
 cleanup:
3206
    if (privdom)
3207
        virObjectUnlock(privdom);
3208
    if (event)
3209
        testObjectEventQueue(privconn, event);
3210
    testDriverUnlock(privconn);
3211
    return ret;
3212 3213
}

3214 3215 3216 3217 3218
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

3219 3220 3221
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
3222 3223
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
3224
    int ret = -1;
3225

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

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

3236
    *autostart = privdom->autostart;
3237 3238
    ret = 0;

3239
 cleanup:
3240
    if (privdom)
3241
        virObjectUnlock(privdom);
3242
    return ret;
3243 3244 3245 3246 3247 3248
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
3249 3250
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
3251
    int ret = -1;
3252

3253
    testDriverLock(privconn);
3254 3255
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3256
    testDriverUnlock(privconn);
3257 3258

    if (privdom == NULL) {
3259
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3260
        goto cleanup;
3261 3262
    }

3263
    privdom->autostart = autostart ? 1 : 0;
3264 3265
    ret = 0;

3266
 cleanup:
3267
    if (privdom)
3268
        virObjectUnlock(privdom);
3269
    return ret;
3270
}
3271

3272
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
3273 3274
                                        int *nparams)
{
3275 3276
    char *type = NULL;

3277 3278 3279
    if (nparams)
        *nparams = 1;

3280
    ignore_value(VIR_STRDUP(type, "fair"));
3281

3282 3283 3284
    return type;
}

3285
static int
3286 3287 3288 3289
testDomainGetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int *nparams,
                                      unsigned int flags)
3290
{
3291 3292
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
3293
    int ret = -1;
3294

3295 3296
    virCheckFlags(0, -1);

3297
    testDriverLock(privconn);
3298 3299
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3300
    testDriverUnlock(privconn);
3301 3302

    if (privdom == NULL) {
3303
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3304
        goto cleanup;
3305 3306
    }

3307 3308
    if (virTypedParameterAssign(params, VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, 50) < 0)
3309
        goto cleanup;
3310 3311
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
3312 3313

    *nparams = 1;
3314 3315
    ret = 0;

3316
 cleanup:
3317
    if (privdom)
3318
        virObjectUnlock(privdom);
3319
    return ret;
3320
}
3321

3322
static int
3323 3324 3325
testDomainGetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int *nparams)
3326
{
3327
    return testDomainGetSchedulerParametersFlags(domain, params, nparams, 0);
3328
}
3329

3330
static int
3331 3332 3333 3334
testDomainSetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int nparams,
                                      unsigned int flags)
3335
{
3336 3337
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
3338 3339
    int ret = -1;
    size_t i;
3340

3341
    virCheckFlags(0, -1);
3342 3343 3344 3345
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_SCHEDULER_WEIGHT,
                               VIR_TYPED_PARAM_UINT,
                               NULL) < 0)
3346
        return -1;
3347

3348
    testDriverLock(privconn);
3349 3350
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3351
    testDriverUnlock(privconn);
3352 3353

    if (privdom == NULL) {
3354
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3355
        goto cleanup;
3356 3357
    }

3358
    for (i = 0; i < nparams; i++) {
3359 3360 3361
        if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_WEIGHT)) {
            /* XXX */
            /*privdom->weight = params[i].value.ui;*/
3362
        }
3363
    }
3364

3365 3366
    ret = 0;

3367
 cleanup:
3368
    if (privdom)
3369
        virObjectUnlock(privdom);
3370
    return ret;
3371 3372
}

3373
static int
3374 3375 3376
testDomainSetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int nparams)
3377
{
3378
    return testDomainSetSchedulerParametersFlags(domain, params, nparams, 0);
3379 3380
}

3381 3382
static int testDomainBlockStats(virDomainPtr domain,
                                const char *path,
3383
                                virDomainBlockStatsPtr stats)
3384 3385 3386 3387 3388
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
3389
    int ret = -1;
3390

3391 3392 3393 3394 3395 3396
    if (!*path) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("summary statistics are not supported yet"));
        return ret;
    }

3397
    testDriverLock(privconn);
3398 3399
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3400 3401 3402
    testDriverUnlock(privconn);

    if (privdom == NULL) {
3403
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3404 3405 3406
        goto error;
    }

3407
    if (virDomainDiskIndexByName(privdom->def, path, false) < 0) {
3408 3409
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path: %s"), path);
3410 3411 3412 3413
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
3414
        virReportSystemError(errno,
3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427
                             "%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;
3428
 error:
3429
    if (privdom)
3430
        virObjectUnlock(privdom);
3431 3432 3433 3434 3435
    return ret;
}

static int testDomainInterfaceStats(virDomainPtr domain,
                                    const char *path,
3436
                                    virDomainInterfaceStatsPtr stats)
3437 3438 3439 3440 3441
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
3442 3443
    size_t i;
    int found = 0, ret = -1;
3444 3445

    testDriverLock(privconn);
3446 3447
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3448 3449 3450
    testDriverUnlock(privconn);

    if (privdom == NULL) {
3451
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3452 3453 3454
        goto error;
    }

3455
    for (i = 0; i < privdom->def->nnets; i++) {
3456
        if (privdom->def->nets[i]->ifname &&
3457
            STREQ(privdom->def->nets[i]->ifname, path)) {
3458 3459 3460 3461 3462 3463
            found = 1;
            break;
        }
    }

    if (!found) {
3464 3465
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path, '%s' is not a known interface"), path);
3466 3467 3468 3469
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
3470
        virReportSystemError(errno,
3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486
                             "%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;
3487
 error:
3488
    if (privdom)
3489
        virObjectUnlock(privdom);
3490 3491 3492
    return ret;
}

3493

3494 3495
static virNetworkPtr testNetworkLookupByUUID(virConnectPtr conn,
                                             const unsigned char *uuid)
3496
{
3497 3498
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
3499
    virNetworkPtr ret = NULL;
3500

3501
    testDriverLock(privconn);
3502
    net = virNetworkObjFindByUUID(privconn->networks, uuid);
3503 3504 3505
    testDriverUnlock(privconn);

    if (net == NULL) {
3506
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3507
        goto cleanup;
3508 3509
    }

3510 3511
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

3512
 cleanup:
3513 3514
    if (net)
        virNetworkObjUnlock(net);
3515
    return ret;
3516
}
3517

3518
static virNetworkPtr testNetworkLookupByName(virConnectPtr conn,
3519
                                             const char *name)
3520
{
3521
    testConnPtr privconn = conn->privateData;
3522 3523
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
3524

3525
    testDriverLock(privconn);
3526
    net = virNetworkFindByName(privconn->networks, name);
3527 3528 3529
    testDriverUnlock(privconn);

    if (net == NULL) {
3530
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3531
        goto cleanup;
3532 3533
    }

3534 3535
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

3536
 cleanup:
3537 3538
    if (net)
        virNetworkObjUnlock(net);
3539
    return ret;
3540 3541 3542
}


3543 3544
static int testConnectNumOfNetworks(virConnectPtr conn)
{
3545
    testConnPtr privconn = conn->privateData;
3546 3547
    int numActive = 0;
    size_t i;
3548

3549
    testDriverLock(privconn);
3550 3551 3552
    for (i = 0; i < privconn->networks->count; i++) {
        virNetworkObjLock(privconn->networks->objs[i]);
        if (virNetworkObjIsActive(privconn->networks->objs[i]))
3553
            numActive++;
3554
        virNetworkObjUnlock(privconn->networks->objs[i]);
3555 3556
    }
    testDriverUnlock(privconn);
3557

3558
    return numActive;
3559 3560
}

3561
static int testConnectListNetworks(virConnectPtr conn, char **const names, int nnames) {
3562
    testConnPtr privconn = conn->privateData;
3563 3564
    int n = 0;
    size_t i;
3565

3566
    testDriverLock(privconn);
3567
    memset(names, 0, sizeof(*names)*nnames);
3568 3569 3570 3571 3572
    for (i = 0; i < privconn->networks->count && n < nnames; i++) {
        virNetworkObjLock(privconn->networks->objs[i]);
        if (virNetworkObjIsActive(privconn->networks->objs[i]) &&
            VIR_STRDUP(names[n++], privconn->networks->objs[i]->def->name) < 0) {
            virNetworkObjUnlock(privconn->networks->objs[i]);
3573
            goto error;
3574
        }
3575
        virNetworkObjUnlock(privconn->networks->objs[i]);
3576 3577
    }
    testDriverUnlock(privconn);
3578

3579 3580
    return n;

3581
 error:
3582
    for (n = 0; n < nnames; n++)
3583
        VIR_FREE(names[n]);
3584
    testDriverUnlock(privconn);
3585
    return -1;
3586 3587
}

3588 3589
static int testConnectNumOfDefinedNetworks(virConnectPtr conn)
{
3590
    testConnPtr privconn = conn->privateData;
3591 3592
    int numInactive = 0;
    size_t i;
3593

3594
    testDriverLock(privconn);
3595 3596 3597
    for (i = 0; i < privconn->networks->count; i++) {
        virNetworkObjLock(privconn->networks->objs[i]);
        if (!virNetworkObjIsActive(privconn->networks->objs[i]))
3598
            numInactive++;
3599
        virNetworkObjUnlock(privconn->networks->objs[i]);
3600 3601
    }
    testDriverUnlock(privconn);
3602

3603
    return numInactive;
3604 3605
}

3606
static int testConnectListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
3607
    testConnPtr privconn = conn->privateData;
3608 3609
    int n = 0;
    size_t i;
3610

3611
    testDriverLock(privconn);
3612
    memset(names, 0, sizeof(*names)*nnames);
3613 3614 3615 3616 3617
    for (i = 0; i < privconn->networks->count && n < nnames; i++) {
        virNetworkObjLock(privconn->networks->objs[i]);
        if (!virNetworkObjIsActive(privconn->networks->objs[i]) &&
            VIR_STRDUP(names[n++], privconn->networks->objs[i]->def->name) < 0) {
            virNetworkObjUnlock(privconn->networks->objs[i]);
3618
            goto error;
3619
        }
3620
        virNetworkObjUnlock(privconn->networks->objs[i]);
3621 3622
    }
    testDriverUnlock(privconn);
3623

3624 3625
    return n;

3626
 error:
3627
    for (n = 0; n < nnames; n++)
3628
        VIR_FREE(names[n]);
3629
    testDriverUnlock(privconn);
3630
    return -1;
3631 3632
}

3633
static int
3634
testConnectListAllNetworks(virConnectPtr conn,
3635 3636 3637 3638 3639 3640 3641 3642 3643
                           virNetworkPtr **nets,
                           unsigned int flags)
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

    testDriverLock(privconn);
3644
    ret = virNetworkObjListExport(conn, privconn->networks, nets, NULL, flags);
3645 3646 3647 3648
    testDriverUnlock(privconn);

    return ret;
}
3649 3650 3651 3652 3653 3654 3655 3656

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

    testDriverLock(privconn);
3657
    obj = virNetworkObjFindByUUID(privconn->networks, net->uuid);
3658 3659
    testDriverUnlock(privconn);
    if (!obj) {
3660
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3661 3662 3663 3664
        goto cleanup;
    }
    ret = virNetworkObjIsActive(obj);

3665
 cleanup:
3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677
    if (obj)
        virNetworkObjUnlock(obj);
    return ret;
}

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

    testDriverLock(privconn);
3678
    obj = virNetworkObjFindByUUID(privconn->networks, net->uuid);
3679 3680
    testDriverUnlock(privconn);
    if (!obj) {
3681
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3682 3683 3684 3685
        goto cleanup;
    }
    ret = obj->persistent;

3686
 cleanup:
3687 3688 3689 3690 3691 3692
    if (obj)
        virNetworkObjUnlock(obj);
    return ret;
}


3693 3694
static virNetworkPtr testNetworkCreateXML(virConnectPtr conn, const char *xml)
{
3695
    testConnPtr privconn = conn->privateData;
3696
    virNetworkDefPtr def;
3697
    virNetworkObjPtr net = NULL;
3698
    virNetworkPtr ret = NULL;
3699
    virObjectEventPtr event = NULL;
3700

3701
    testDriverLock(privconn);
3702
    if ((def = virNetworkDefParseString(xml)) == NULL)
3703
        goto cleanup;
3704

3705
    if (!(net = virNetworkAssignDef(privconn->networks, def, true)))
3706 3707
        goto cleanup;
    def = NULL;
3708
    net->active = 1;
3709

3710
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3711 3712
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3713

3714
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3715

3716
 cleanup:
3717
    virNetworkDefFree(def);
3718 3719
    if (event)
        testObjectEventQueue(privconn, event);
3720 3721 3722
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3723
    return ret;
3724 3725
}

3726
static
3727
virNetworkPtr testNetworkDefineXML(virConnectPtr conn, const char *xml)
3728
{
3729
    testConnPtr privconn = conn->privateData;
3730
    virNetworkDefPtr def;
3731
    virNetworkObjPtr net = NULL;
3732
    virNetworkPtr ret = NULL;
3733
    virObjectEventPtr event = NULL;
3734

3735
    testDriverLock(privconn);
3736
    if ((def = virNetworkDefParseString(xml)) == NULL)
3737
        goto cleanup;
3738

3739
    if (!(net = virNetworkAssignDef(privconn->networks, def, false)))
3740 3741
        goto cleanup;
    def = NULL;
3742

3743
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3744 3745
                                        VIR_NETWORK_EVENT_DEFINED,
                                        0);
3746

3747
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3748

3749
 cleanup:
3750
    virNetworkDefFree(def);
3751 3752
    if (event)
        testObjectEventQueue(privconn, event);
3753 3754 3755
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3756
    return ret;
3757 3758
}

3759 3760
static int testNetworkUndefine(virNetworkPtr network)
{
3761 3762
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3763
    int ret = -1;
3764
    virObjectEventPtr event = NULL;
3765

3766
    testDriverLock(privconn);
3767
    privnet = virNetworkFindByName(privconn->networks, network->name);
3768 3769

    if (privnet == NULL) {
3770
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3771
        goto cleanup;
3772
    }
3773

D
Daniel P. Berrange 已提交
3774
    if (virNetworkObjIsActive(privnet)) {
3775 3776
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is still running"), network->name);
3777
        goto cleanup;
3778 3779
    }

3780
    event = virNetworkEventLifecycleNew(network->name, network->uuid,
3781 3782
                                        VIR_NETWORK_EVENT_UNDEFINED,
                                        0);
3783

3784
    virNetworkRemoveInactive(privconn->networks, privnet);
3785
    privnet = NULL;
3786
    ret = 0;
3787

3788
 cleanup:
3789 3790
    if (event)
        testObjectEventQueue(privconn, event);
3791 3792 3793
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3794
    return ret;
3795 3796
}

3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814
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);

3815
    network = virNetworkObjFindByUUID(privconn->networks, net->uuid);
3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839
    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;
3840
 cleanup:
3841 3842
    if (network)
        virNetworkObjUnlock(network);
3843 3844 3845 3846
    testDriverUnlock(privconn);
    return ret;
}

3847 3848
static int testNetworkCreate(virNetworkPtr network)
{
3849 3850
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3851
    int ret = -1;
3852
    virObjectEventPtr event = NULL;
3853

3854
    testDriverLock(privconn);
3855
    privnet = virNetworkFindByName(privconn->networks, network->name);
3856
    testDriverUnlock(privconn);
3857 3858

    if (privnet == NULL) {
3859
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3860
        goto cleanup;
3861
    }
3862

D
Daniel P. Berrange 已提交
3863
    if (virNetworkObjIsActive(privnet)) {
3864 3865
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is already running"), network->name);
3866
        goto cleanup;
3867 3868
    }

3869
    privnet->active = 1;
3870
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3871 3872
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3873
    ret = 0;
3874

3875
 cleanup:
3876 3877
    if (event)
        testObjectEventQueue(privconn, event);
3878 3879
    if (privnet)
        virNetworkObjUnlock(privnet);
3880
    return ret;
3881 3882
}

3883 3884
static int testNetworkDestroy(virNetworkPtr network)
{
3885 3886
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3887
    int ret = -1;
3888
    virObjectEventPtr event = NULL;
3889

3890
    testDriverLock(privconn);
3891
    privnet = virNetworkFindByName(privconn->networks, network->name);
3892 3893

    if (privnet == NULL) {
3894
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3895
        goto cleanup;
3896
    }
3897

3898
    privnet->active = 0;
3899
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3900 3901
                                        VIR_NETWORK_EVENT_STOPPED,
                                        0);
3902
    if (!privnet->persistent) {
3903
        virNetworkRemoveInactive(privconn->networks, privnet);
3904
        privnet = NULL;
3905
    }
3906 3907
    ret = 0;

3908
 cleanup:
3909 3910
    if (event)
        testObjectEventQueue(privconn, event);
3911 3912 3913
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3914
    return ret;
3915 3916
}

3917
static char *testNetworkGetXMLDesc(virNetworkPtr network,
E
Eric Blake 已提交
3918
                                   unsigned int flags)
3919
{
3920 3921
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3922
    char *ret = NULL;
3923

E
Eric Blake 已提交
3924 3925
    virCheckFlags(0, NULL);

3926
    testDriverLock(privconn);
3927
    privnet = virNetworkFindByName(privconn->networks, network->name);
3928
    testDriverUnlock(privconn);
3929 3930

    if (privnet == NULL) {
3931
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3932
        goto cleanup;
3933
    }
3934

3935
    ret = virNetworkDefFormat(privnet->def, flags);
3936

3937
 cleanup:
3938 3939
    if (privnet)
        virNetworkObjUnlock(privnet);
3940
    return ret;
3941 3942 3943
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3944
    testConnPtr privconn = network->conn->privateData;
3945
    char *bridge = NULL;
3946 3947
    virNetworkObjPtr privnet;

3948
    testDriverLock(privconn);
3949
    privnet = virNetworkFindByName(privconn->networks, network->name);
3950
    testDriverUnlock(privconn);
3951 3952

    if (privnet == NULL) {
3953
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3954
        goto cleanup;
3955 3956
    }

3957
    if (!(privnet->def->bridge)) {
3958 3959 3960
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("network '%s' does not have a bridge name."),
                       privnet->def->name);
3961 3962 3963
        goto cleanup;
    }

3964
    ignore_value(VIR_STRDUP(bridge, privnet->def->bridge));
3965

3966
 cleanup:
3967 3968
    if (privnet)
        virNetworkObjUnlock(privnet);
3969 3970 3971 3972
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
3973 3974
                                   int *autostart)
{
3975 3976
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3977
    int ret = -1;
3978

3979
    testDriverLock(privconn);
3980
    privnet = virNetworkFindByName(privconn->networks, network->name);
3981
    testDriverUnlock(privconn);
3982 3983

    if (privnet == NULL) {
3984
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3985
        goto cleanup;
3986 3987
    }

3988
    *autostart = privnet->autostart;
3989 3990
    ret = 0;

3991
 cleanup:
3992 3993
    if (privnet)
        virNetworkObjUnlock(privnet);
3994
    return ret;
3995 3996 3997
}

static int testNetworkSetAutostart(virNetworkPtr network,
3998 3999
                                   int autostart)
{
4000 4001
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
4002
    int ret = -1;
4003

4004
    testDriverLock(privconn);
4005
    privnet = virNetworkFindByName(privconn->networks, network->name);
4006
    testDriverUnlock(privconn);
4007 4008

    if (privnet == NULL) {
4009
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4010
        goto cleanup;
4011 4012
    }

4013
    privnet->autostart = autostart ? 1 : 0;
4014 4015
    ret = 0;

4016
 cleanup:
4017 4018
    if (privnet)
        virNetworkObjUnlock(privnet);
4019
    return ret;
4020
}
4021

C
Cole Robinson 已提交
4022

L
Laine Stump 已提交
4023 4024 4025 4026 4027
/*
 * Physical host interface routines
 */


4028
static int testConnectNumOfInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
4029 4030
{
    testConnPtr privconn = conn->privateData;
4031 4032
    size_t i;
    int count = 0;
L
Laine Stump 已提交
4033 4034

    testDriverLock(privconn);
4035
    for (i = 0; (i < privconn->ifaces.count); i++) {
L
Laine Stump 已提交
4036
        virInterfaceObjLock(privconn->ifaces.objs[i]);
4037
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i]))
L
Laine Stump 已提交
4038 4039 4040 4041 4042 4043 4044
            count++;
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

4045
static int testConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
L
Laine Stump 已提交
4046 4047
{
    testConnPtr privconn = conn->privateData;
4048 4049
    int n = 0;
    size_t i;
L
Laine Stump 已提交
4050 4051 4052

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

    return n;

4067
 error:
4068
    for (n = 0; n < nnames; n++)
L
Laine Stump 已提交
4069 4070 4071 4072 4073
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

4074
static int testConnectNumOfDefinedInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
4075 4076
{
    testConnPtr privconn = conn->privateData;
4077 4078
    size_t i;
    int count = 0;
L
Laine Stump 已提交
4079 4080

    testDriverLock(privconn);
4081
    for (i = 0; i < privconn->ifaces.count; i++) {
L
Laine Stump 已提交
4082
        virInterfaceObjLock(privconn->ifaces.objs[i]);
4083
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i]))
L
Laine Stump 已提交
4084 4085 4086 4087 4088 4089 4090
            count++;
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

4091
static int testConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
L
Laine Stump 已提交
4092 4093
{
    testConnPtr privconn = conn->privateData;
4094 4095
    int n = 0;
    size_t i;
L
Laine Stump 已提交
4096 4097 4098

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

    return n;

4113
 error:
4114
    for (n = 0; n < nnames; n++)
L
Laine Stump 已提交
4115 4116 4117 4118 4119
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

4120
static virInterfacePtr testInterfaceLookupByName(virConnectPtr conn,
L
Laine Stump 已提交
4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131
                                                 const char *name)
{
    testConnPtr privconn = conn->privateData;
    virInterfaceObjPtr iface;
    virInterfacePtr ret = NULL;

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

    if (iface == NULL) {
4132
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4133 4134 4135 4136 4137
        goto cleanup;
    }

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

4138
 cleanup:
L
Laine Stump 已提交
4139 4140 4141 4142 4143
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

4144
static virInterfacePtr testInterfaceLookupByMACString(virConnectPtr conn,
L
Laine Stump 已提交
4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156
                                                      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) {
4157
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4158 4159 4160 4161
        goto cleanup;
    }

    if (ifacect > 1) {
4162
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
4163 4164 4165 4166 4167
        goto cleanup;
    }

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

4168
 cleanup:
L
Laine Stump 已提交
4169 4170 4171 4172 4173
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

4174 4175 4176 4177 4178 4179 4180 4181 4182 4183
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) {
4184
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
4185 4186 4187 4188
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

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

4195
static int testInterfaceChangeBegin(virConnectPtr conn,
E
Eric Blake 已提交
4196
                                    unsigned int flags)
4197 4198 4199 4200
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
4201 4202
    virCheckFlags(0, -1);

4203 4204
    testDriverLock(privconn);
    if (privconn->transaction_running) {
4205
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
4206
                       _("there is another transaction running."));
4207 4208 4209 4210 4211 4212 4213 4214 4215 4216
        goto cleanup;
    }

    privconn->transaction_running = true;

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

    ret = 0;
4217
 cleanup:
4218 4219 4220 4221 4222
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceChangeCommit(virConnectPtr conn,
E
Eric Blake 已提交
4223
                                     unsigned int flags)
4224 4225 4226 4227
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
4228 4229
    virCheckFlags(0, -1);

4230 4231 4232
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
4233
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
4234 4235
                       _("no transaction running, "
                         "nothing to be committed."));
4236 4237 4238 4239 4240 4241 4242 4243
        goto cleanup;
    }

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

    ret = 0;

4244
 cleanup:
4245 4246 4247 4248 4249 4250
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
E
Eric Blake 已提交
4251
                                       unsigned int flags)
4252 4253 4254 4255
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
4256 4257
    virCheckFlags(0, -1);

4258 4259 4260
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
4261
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
4262 4263
                       _("no transaction running, "
                         "nothing to rollback."));
4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276
        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;

4277
 cleanup:
4278 4279 4280
    testDriverUnlock(privconn);
    return ret;
}
4281

L
Laine Stump 已提交
4282
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
E
Eric Blake 已提交
4283
                                     unsigned int flags)
L
Laine Stump 已提交
4284 4285 4286 4287 4288
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

E
Eric Blake 已提交
4289 4290
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
4291 4292 4293 4294 4295 4296
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);
    testDriverUnlock(privconn);

    if (privinterface == NULL) {
4297
        virReportError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
4298 4299 4300
        goto cleanup;
    }

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

4303
 cleanup:
L
Laine Stump 已提交
4304 4305 4306 4307 4308 4309 4310
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    return ret;
}


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
E
Eric Blake 已提交
4311
                                              unsigned int flags)
L
Laine Stump 已提交
4312 4313 4314 4315 4316 4317
{
    testConnPtr privconn = conn->privateData;
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
4318 4319
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
4320
    testDriverLock(privconn);
4321
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
4322 4323
        goto cleanup;

4324
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
4325 4326 4327 4328 4329
        goto cleanup;
    def = NULL;

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

4330
 cleanup:
L
Laine Stump 已提交
4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348
    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) {
4349
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4350 4351 4352 4353 4354 4355 4356
        goto cleanup;
    }

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

4357
 cleanup:
L
Laine Stump 已提交
4358 4359 4360 4361 4362
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
E
Eric Blake 已提交
4363
                               unsigned int flags)
L
Laine Stump 已提交
4364 4365 4366 4367 4368
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
4369 4370
    virCheckFlags(0, -1);

L
Laine Stump 已提交
4371 4372 4373 4374 4375
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
4376
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4377 4378 4379 4380
        goto cleanup;
    }

    if (privinterface->active != 0) {
4381
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4382 4383 4384 4385 4386 4387
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

4388
 cleanup:
L
Laine Stump 已提交
4389 4390 4391 4392 4393 4394 4395
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceDestroy(virInterfacePtr iface,
E
Eric Blake 已提交
4396
                                unsigned int flags)
L
Laine Stump 已提交
4397 4398 4399 4400 4401
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
4402 4403
    virCheckFlags(0, -1);

L
Laine Stump 已提交
4404 4405 4406 4407 4408
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
4409
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4410 4411 4412 4413
        goto cleanup;
    }

    if (privinterface->active == 0) {
4414
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4415 4416 4417 4418 4419 4420
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

4421
 cleanup:
L
Laine Stump 已提交
4422 4423 4424 4425 4426 4427 4428 4429
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}



C
Cole Robinson 已提交
4430 4431 4432 4433
/*
 * Storage Driver routines
 */

4434

4435 4436
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool)
{
C
Cole Robinson 已提交
4437 4438 4439 4440 4441

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

4442
    return VIR_STRDUP(pool->configFile, "");
C
Cole Robinson 已提交
4443 4444
}

4445

C
Cole Robinson 已提交
4446 4447
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
4448 4449
                            const unsigned char *uuid)
{
4450
    testConnPtr privconn = conn->privateData;
4451 4452
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4453

4454
    testDriverLock(privconn);
4455
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
4456
    testDriverUnlock(privconn);
4457 4458

    if (pool == NULL) {
4459
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4460
        goto cleanup;
C
Cole Robinson 已提交
4461 4462
    }

4463 4464
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4465

4466
 cleanup:
4467 4468
    if (pool)
        virStoragePoolObjUnlock(pool);
4469
    return ret;
C
Cole Robinson 已提交
4470 4471 4472 4473
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
4474 4475
                            const char *name)
{
4476
    testConnPtr privconn = conn->privateData;
4477 4478
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4479

4480
    testDriverLock(privconn);
4481
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
4482
    testDriverUnlock(privconn);
4483 4484

    if (pool == NULL) {
4485
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4486
        goto cleanup;
C
Cole Robinson 已提交
4487 4488
    }

4489 4490
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4491

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

static virStoragePoolPtr
4499 4500
testStoragePoolLookupByVolume(virStorageVolPtr vol)
{
C
Cole Robinson 已提交
4501 4502 4503 4504
    return testStoragePoolLookupByName(vol->conn, vol->pool);
}

static int
4505 4506
testConnectNumOfStoragePools(virConnectPtr conn)
{
4507
    testConnPtr privconn = conn->privateData;
4508 4509
    int numActive = 0;
    size_t i;
C
Cole Robinson 已提交
4510

4511
    testDriverLock(privconn);
4512
    for (i = 0; i < privconn->pools.count; i++)
C
Cole Robinson 已提交
4513 4514
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
4515
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4516 4517 4518 4519 4520

    return numActive;
}

static int
4521 4522
testConnectListStoragePools(virConnectPtr conn,
                            char **const names,
4523 4524
                            int nnames)
{
4525
    testConnPtr privconn = conn->privateData;
4526 4527
    int n = 0;
    size_t i;
C
Cole Robinson 已提交
4528

4529
    testDriverLock(privconn);
C
Cole Robinson 已提交
4530
    memset(names, 0, sizeof(*names)*nnames);
4531
    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
4532
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4533
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
4534
            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
4535
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
4536
            goto error;
4537 4538 4539 4540
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4541 4542 4543

    return n;

4544
 error:
4545
    for (n = 0; n < nnames; n++)
C
Cole Robinson 已提交
4546
        VIR_FREE(names[n]);
4547
    testDriverUnlock(privconn);
4548
    return -1;
C
Cole Robinson 已提交
4549 4550 4551
}

static int
4552 4553
testConnectNumOfDefinedStoragePools(virConnectPtr conn)
{
4554
    testConnPtr privconn = conn->privateData;
4555 4556
    int numInactive = 0;
    size_t i;
C
Cole Robinson 已提交
4557

4558
    testDriverLock(privconn);
4559
    for (i = 0; i < privconn->pools.count; i++) {
4560
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4561 4562
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
4563 4564 4565
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4566 4567 4568 4569 4570

    return numInactive;
}

static int
4571 4572
testConnectListDefinedStoragePools(virConnectPtr conn,
                                   char **const names,
4573 4574
                                   int nnames)
{
4575
    testConnPtr privconn = conn->privateData;
4576 4577
    int n = 0;
    size_t i;
C
Cole Robinson 已提交
4578

4579
    testDriverLock(privconn);
C
Cole Robinson 已提交
4580
    memset(names, 0, sizeof(*names)*nnames);
4581
    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
4582
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4583
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
4584
            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
4585
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
4586
            goto error;
4587 4588 4589 4590
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4591 4592 4593

    return n;

4594
 error:
4595
    for (n = 0; n < nnames; n++)
C
Cole Robinson 已提交
4596
        VIR_FREE(names[n]);
4597
    testDriverUnlock(privconn);
4598
    return -1;
C
Cole Robinson 已提交
4599 4600
}

4601
static int
4602 4603 4604
testConnectListAllStoragePools(virConnectPtr conn,
                               virStoragePoolPtr **pools,
                               unsigned int flags)
4605 4606 4607 4608 4609 4610 4611
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

    testDriverLock(privconn);
4612 4613
    ret = virStoragePoolObjListExport(conn, privconn->pools, pools,
                                      NULL, flags);
4614 4615 4616 4617
    testDriverUnlock(privconn);

    return ret;
}
C
Cole Robinson 已提交
4618

4619 4620 4621 4622 4623 4624 4625 4626 4627 4628
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) {
4629
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4630 4631 4632 4633
        goto cleanup;
    }
    ret = virStoragePoolObjIsActive(obj);

4634
 cleanup:
4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649
    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) {
4650
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4651 4652 4653 4654
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

4655
 cleanup:
4656 4657 4658 4659 4660 4661 4662
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}



C
Cole Robinson 已提交
4663
static int
4664 4665
testStoragePoolCreate(virStoragePoolPtr pool,
                      unsigned int flags)
E
Eric Blake 已提交
4666
{
4667 4668
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4669
    int ret = -1;
4670

E
Eric Blake 已提交
4671 4672
    virCheckFlags(0, -1);

4673
    testDriverLock(privconn);
4674 4675
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4676
    testDriverUnlock(privconn);
4677 4678

    if (privpool == NULL) {
4679
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4680
        goto cleanup;
4681 4682
    }

4683
    if (virStoragePoolObjIsActive(privpool)) {
4684 4685
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4686 4687
        goto cleanup;
    }
C
Cole Robinson 已提交
4688 4689

    privpool->active = 1;
4690
    ret = 0;
C
Cole Robinson 已提交
4691

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

static char *
4699 4700 4701 4702
testConnectFindStoragePoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type,
                                  const char *srcSpec,
                                  unsigned int flags)
C
Cole Robinson 已提交
4703
{
4704 4705 4706 4707
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4708 4709
    virCheckFlags(0, NULL);

4710 4711
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4712 4713
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
4714 4715 4716 4717
        goto cleanup;
    }

    if (srcSpec) {
4718
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4719 4720 4721 4722 4723 4724 4725
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
4726
        ignore_value(VIR_STRDUP(ret, defaultPoolSourcesLogicalXML));
4727 4728 4729
        break;

    case VIR_STORAGE_POOL_NETFS:
4730
        if (!source || !source->hosts[0].name) {
4731 4732
            virReportError(VIR_ERR_INVALID_ARG,
                           "%s", _("hostname must be specified for netfs sources"));
4733 4734 4735
            goto cleanup;
        }

4736 4737
        ignore_value(virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                                 source->hosts[0].name));
4738 4739 4740
        break;

    default:
4741 4742
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4743 4744
    }

4745
 cleanup:
4746 4747
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4748 4749 4750 4751
}


static virStoragePoolPtr
4752 4753 4754
testStoragePoolCreateXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4755
{
4756
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4757
    virStoragePoolDefPtr def;
4758
    virStoragePoolObjPtr pool = NULL;
4759
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4760

E
Eric Blake 已提交
4761 4762
    virCheckFlags(0, NULL);

4763
    testDriverLock(privconn);
4764
    if (!(def = virStoragePoolDefParseString(xml)))
4765
        goto cleanup;
C
Cole Robinson 已提交
4766

4767 4768 4769 4770
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4771 4772
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("storage pool already exists"));
4773
        goto cleanup;
C
Cole Robinson 已提交
4774 4775
    }

4776
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4777
        goto cleanup;
4778
    def = NULL;
C
Cole Robinson 已提交
4779

4780
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4781
        virStoragePoolObjRemove(&privconn->pools, pool);
4782 4783
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4784 4785 4786
    }
    pool->active = 1;

4787 4788
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4789

4790
 cleanup:
4791
    virStoragePoolDefFree(def);
4792 4793 4794
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4795
    return ret;
C
Cole Robinson 已提交
4796 4797 4798
}

static virStoragePoolPtr
4799 4800 4801
testStoragePoolDefineXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4802
{
4803
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4804
    virStoragePoolDefPtr def;
4805
    virStoragePoolObjPtr pool = NULL;
4806
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4807

E
Eric Blake 已提交
4808 4809
    virCheckFlags(0, NULL);

4810
    testDriverLock(privconn);
4811
    if (!(def = virStoragePoolDefParseString(xml)))
4812
        goto cleanup;
C
Cole Robinson 已提交
4813 4814 4815 4816 4817

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

4818
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4819 4820
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4821

4822
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4823
        virStoragePoolObjRemove(&privconn->pools, pool);
4824 4825
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4826 4827
    }

4828 4829
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4830

4831
 cleanup:
4832
    virStoragePoolDefFree(def);
4833 4834 4835
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4836
    return ret;
C
Cole Robinson 已提交
4837 4838 4839
}

static int
4840 4841
testStoragePoolUndefine(virStoragePoolPtr pool)
{
4842 4843
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4844
    int ret = -1;
4845

4846
    testDriverLock(privconn);
4847 4848 4849 4850
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4851
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4852
        goto cleanup;
4853 4854
    }

4855
    if (virStoragePoolObjIsActive(privpool)) {
4856 4857
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4858 4859
        goto cleanup;
    }
C
Cole Robinson 已提交
4860 4861

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

4864
 cleanup:
4865 4866 4867
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4868
    return ret;
C
Cole Robinson 已提交
4869 4870 4871
}

static int
4872
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4873 4874
                     unsigned int flags)
{
4875 4876
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4877
    int ret = -1;
4878

E
Eric Blake 已提交
4879 4880
    virCheckFlags(0, -1);

4881
    testDriverLock(privconn);
4882 4883
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4884
    testDriverUnlock(privconn);
4885 4886

    if (privpool == NULL) {
4887
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4888
        goto cleanup;
4889 4890
    }

4891
    if (virStoragePoolObjIsActive(privpool)) {
4892 4893
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4894 4895
        goto cleanup;
    }
4896
    ret = 0;
C
Cole Robinson 已提交
4897

4898
 cleanup:
4899 4900
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4901
    return ret;
C
Cole Robinson 已提交
4902 4903 4904 4905
}


static int
4906 4907
testStoragePoolDestroy(virStoragePoolPtr pool)
{
4908 4909
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4910
    int ret = -1;
4911

4912
    testDriverLock(privconn);
4913 4914 4915 4916
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4917
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4918
        goto cleanup;
4919 4920 4921
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4922 4923
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4924
        goto cleanup;
4925
    }
C
Cole Robinson 已提交
4926 4927 4928

    privpool->active = 0;

4929
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4930
        virStoragePoolObjRemove(&privconn->pools, privpool);
4931 4932
        privpool = NULL;
    }
4933
    ret = 0;
C
Cole Robinson 已提交
4934

4935
 cleanup:
4936 4937 4938
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4939
    return ret;
C
Cole Robinson 已提交
4940 4941 4942 4943
}


static int
4944
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4945 4946
                      unsigned int flags)
{
4947 4948
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4949
    int ret = -1;
4950

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

4953
    testDriverLock(privconn);
4954 4955
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4956
    testDriverUnlock(privconn);
4957 4958

    if (privpool == NULL) {
4959
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4960 4961 4962 4963
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
4964 4965
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4966
        goto cleanup;
4967 4968
    }

4969
    ret = 0;
C
Cole Robinson 已提交
4970

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


static int
4979
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4980 4981
                       unsigned int flags)
{
4982 4983
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4984
    int ret = -1;
4985

E
Eric Blake 已提交
4986 4987
    virCheckFlags(0, -1);

4988
    testDriverLock(privconn);
4989 4990
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4991
    testDriverUnlock(privconn);
4992 4993

    if (privpool == NULL) {
4994
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4995
        goto cleanup;
4996 4997 4998
    }

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

5005
 cleanup:
5006 5007
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5008
    return ret;
C
Cole Robinson 已提交
5009 5010 5011 5012
}


static int
5013
testStoragePoolGetInfo(virStoragePoolPtr pool,
5014 5015
                       virStoragePoolInfoPtr info)
{
5016 5017
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5018
    int ret = -1;
5019

5020
    testDriverLock(privconn);
5021 5022
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5023
    testDriverUnlock(privconn);
5024 5025

    if (privpool == NULL) {
5026
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5027
        goto cleanup;
5028
    }
C
Cole Robinson 已提交
5029 5030 5031 5032 5033 5034 5035 5036 5037

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

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

static char *
5047
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
5048 5049
                          unsigned int flags)
{
5050 5051
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5052
    char *ret = NULL;
5053

E
Eric Blake 已提交
5054 5055
    virCheckFlags(0, NULL);

5056
    testDriverLock(privconn);
5057 5058
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5059
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5060

5061
    if (privpool == NULL) {
5062
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5063
        goto cleanup;
5064 5065
    }

5066
    ret = virStoragePoolDefFormat(privpool->def);
5067

5068
 cleanup:
5069 5070
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5071
    return ret;
C
Cole Robinson 已提交
5072 5073 5074
}

static int
5075
testStoragePoolGetAutostart(virStoragePoolPtr pool,
5076 5077
                            int *autostart)
{
5078 5079
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5080
    int ret = -1;
5081

5082
    testDriverLock(privconn);
5083 5084
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5085
    testDriverUnlock(privconn);
5086 5087

    if (privpool == NULL) {
5088
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5089
        goto cleanup;
5090
    }
C
Cole Robinson 已提交
5091 5092 5093 5094 5095 5096

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

5099
 cleanup:
5100 5101
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5102
    return ret;
C
Cole Robinson 已提交
5103 5104 5105
}

static int
5106
testStoragePoolSetAutostart(virStoragePoolPtr pool,
5107 5108
                            int autostart)
{
5109 5110
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5111
    int ret = -1;
5112

5113
    testDriverLock(privconn);
5114 5115
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5116
    testDriverUnlock(privconn);
5117 5118

    if (privpool == NULL) {
5119
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5120
        goto cleanup;
5121
    }
C
Cole Robinson 已提交
5122 5123

    if (!privpool->configFile) {
5124 5125
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
5126
        goto cleanup;
C
Cole Robinson 已提交
5127 5128 5129 5130
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
5131 5132
    ret = 0;

5133
 cleanup:
5134 5135
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5136
    return ret;
C
Cole Robinson 已提交
5137 5138 5139 5140
}


static int
5141 5142
testStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
5143 5144
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5145
    int ret = -1;
5146

5147
    testDriverLock(privconn);
5148 5149
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5150
    testDriverUnlock(privconn);
5151 5152

    if (privpool == NULL) {
5153
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5154
        goto cleanup;
5155 5156 5157
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5158 5159
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5160
        goto cleanup;
5161
    }
C
Cole Robinson 已提交
5162

5163 5164
    ret = privpool->volumes.count;

5165
 cleanup:
5166 5167
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5168
    return ret;
C
Cole Robinson 已提交
5169 5170 5171
}

static int
5172
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
5173
                           char **const names,
5174 5175
                           int maxnames)
{
5176 5177
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5178 5179
    size_t i = 0;
    int n = 0;
C
Cole Robinson 已提交
5180

5181
    memset(names, 0, maxnames * sizeof(*names));
5182 5183

    testDriverLock(privconn);
5184 5185
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5186
    testDriverUnlock(privconn);
5187 5188

    if (privpool == NULL) {
5189
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5190
        goto cleanup;
5191 5192 5193 5194
    }


    if (!virStoragePoolObjIsActive(privpool)) {
5195 5196
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5197
        goto cleanup;
5198 5199
    }

5200
    for (i = 0; i < privpool->volumes.count && n < maxnames; i++) {
5201
        if (VIR_STRDUP(names[n++], privpool->volumes.objs[i]->name) < 0)
C
Cole Robinson 已提交
5202 5203 5204
            goto cleanup;
    }

5205
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5206 5207 5208
    return n;

 cleanup:
5209
    for (n = 0; n < maxnames; n++)
C
Cole Robinson 已提交
5210 5211
        VIR_FREE(names[i]);

5212
    memset(names, 0, maxnames * sizeof(*names));
5213 5214
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5215 5216 5217
    return -1;
}

5218 5219 5220
static int
testStoragePoolListAllVolumes(virStoragePoolPtr obj,
                              virStorageVolPtr **vols,
5221 5222
                              unsigned int flags)
{
5223 5224
    testConnPtr privconn = obj->conn->privateData;
    virStoragePoolObjPtr pool;
5225
    size_t i;
5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254
    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;
    }

5255
    if (VIR_ALLOC_N(tmp_vols, pool->volumes.count + 1) < 0)
5256 5257
         goto cleanup;

5258
    for (i = 0; i < pool->volumes.count; i++) {
5259 5260
        if (!(vol = virGetStorageVol(obj->conn, pool->def->name,
                                     pool->volumes.objs[i]->name,
5261 5262
                                     pool->volumes.objs[i]->key,
                                     NULL, NULL)))
5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276
            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]);
        }
5277
        VIR_FREE(tmp_vols);
5278 5279 5280 5281 5282 5283 5284
    }

    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}
C
Cole Robinson 已提交
5285 5286

static virStorageVolPtr
5287
testStorageVolLookupByName(virStoragePoolPtr pool,
5288 5289
                           const char *name ATTRIBUTE_UNUSED)
{
5290 5291 5292
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5293
    virStorageVolPtr ret = NULL;
5294

5295
    testDriverLock(privconn);
5296 5297
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5298
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5299

5300
    if (privpool == NULL) {
5301
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5302
        goto cleanup;
5303 5304 5305 5306
    }


    if (!virStoragePoolObjIsActive(privpool)) {
5307 5308
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5309
        goto cleanup;
5310 5311 5312 5313 5314
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
5315 5316
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"), name);
5317
        goto cleanup;
C
Cole Robinson 已提交
5318 5319
    }

5320
    ret = virGetStorageVol(pool->conn, privpool->def->name,
5321 5322
                           privvol->name, privvol->key,
                           NULL, NULL);
5323

5324
 cleanup:
5325 5326
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5327
    return ret;
C
Cole Robinson 已提交
5328 5329 5330 5331
}


static virStorageVolPtr
5332
testStorageVolLookupByKey(virConnectPtr conn,
5333 5334
                          const char *key)
{
5335
    testConnPtr privconn = conn->privateData;
5336
    size_t i;
5337
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
5338

5339
    testDriverLock(privconn);
5340
    for (i = 0; i < privconn->pools.count; i++) {
5341
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5342
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
5343
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
5344 5345
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

5346 5347 5348 5349
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
5350 5351
                                       privvol->key,
                                       NULL, NULL);
5352
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
5353 5354
                break;
            }
C
Cole Robinson 已提交
5355
        }
5356
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5357
    }
5358
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5359

5360
    if (!ret)
5361 5362
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
5363 5364

    return ret;
C
Cole Robinson 已提交
5365 5366 5367
}

static virStorageVolPtr
5368
testStorageVolLookupByPath(virConnectPtr conn,
5369 5370
                           const char *path)
{
5371
    testConnPtr privconn = conn->privateData;
5372
    size_t i;
5373
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
5374

5375
    testDriverLock(privconn);
5376
    for (i = 0; i < privconn->pools.count; i++) {
5377
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5378
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
5379
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
5380 5381
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

5382 5383 5384 5385
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
5386 5387
                                       privvol->key,
                                       NULL, NULL);
5388
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
5389 5390
                break;
            }
C
Cole Robinson 已提交
5391
        }
5392
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5393
    }
5394
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5395

5396
    if (!ret)
5397 5398
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
5399 5400

    return ret;
C
Cole Robinson 已提交
5401 5402 5403
}

static virStorageVolPtr
5404 5405 5406
testStorageVolCreateXML(virStoragePoolPtr pool,
                        const char *xmldesc,
                        unsigned int flags)
E
Eric Blake 已提交
5407
{
5408 5409
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5410 5411
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
5412

E
Eric Blake 已提交
5413 5414
    virCheckFlags(0, NULL);

5415
    testDriverLock(privconn);
5416 5417
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5418
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5419

5420
    if (privpool == NULL) {
5421
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5422
        goto cleanup;
5423 5424 5425
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5426 5427
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5428
        goto cleanup;
5429
    }
C
Cole Robinson 已提交
5430

5431
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5432
    if (privvol == NULL)
5433
        goto cleanup;
5434 5435

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5436 5437
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5438
        goto cleanup;
C
Cole Robinson 已提交
5439 5440 5441
    }

    /* Make sure enough space */
5442
    if ((privpool->def->allocation + privvol->target.allocation) >
C
Cole Robinson 已提交
5443
         privpool->def->capacity) {
5444 5445 5446
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5447
        goto cleanup;
C
Cole Robinson 已提交
5448 5449
    }

5450 5451
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5452
                    privvol->name) == -1)
5453
        goto cleanup;
C
Cole Robinson 已提交
5454

5455 5456 5457
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
5458
        goto cleanup;
C
Cole Robinson 已提交
5459

5460
    privpool->def->allocation += privvol->target.allocation;
C
Cole Robinson 已提交
5461 5462 5463
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5464
    ret = virGetStorageVol(pool->conn, privpool->def->name,
5465 5466
                           privvol->name, privvol->key,
                           NULL, NULL);
5467
    privvol = NULL;
5468

5469
 cleanup:
5470
    virStorageVolDefFree(privvol);
5471 5472
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5473
    return ret;
C
Cole Robinson 已提交
5474 5475
}

5476
static virStorageVolPtr
5477 5478 5479 5480
testStorageVolCreateXMLFrom(virStoragePoolPtr pool,
                            const char *xmldesc,
                            virStorageVolPtr clonevol,
                            unsigned int flags)
E
Eric Blake 已提交
5481
{
5482 5483 5484 5485 5486
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
5487 5488
    virCheckFlags(0, NULL);

5489 5490 5491 5492 5493 5494
    testDriverLock(privconn);
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
    testDriverUnlock(privconn);

    if (privpool == NULL) {
5495
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5496 5497 5498 5499
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5500 5501
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5502 5503 5504
        goto cleanup;
    }

5505
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5506 5507 5508 5509
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5510 5511
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5512 5513 5514 5515 5516
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
5517 5518 5519
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
5520 5521 5522 5523
        goto cleanup;
    }

    /* Make sure enough space */
5524
    if ((privpool->def->allocation + privvol->target.allocation) >
5525
         privpool->def->capacity) {
5526 5527 5528
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5529 5530 5531 5532 5533
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5534 5535
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5536
                    privvol->name) == -1)
5537 5538
        goto cleanup;

5539 5540 5541
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
5542 5543
        goto cleanup;

5544
    privpool->def->allocation += privvol->target.allocation;
5545 5546 5547 5548
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    ret = virGetStorageVol(pool->conn, privpool->def->name,
5549 5550
                           privvol->name, privvol->key,
                           NULL, NULL);
5551 5552
    privvol = NULL;

5553
 cleanup:
5554 5555 5556 5557 5558 5559
    virStorageVolDefFree(privvol);
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    return ret;
}

C
Cole Robinson 已提交
5560
static int
5561 5562
testStorageVolDelete(virStorageVolPtr vol,
                     unsigned int flags)
E
Eric Blake 已提交
5563
{
5564 5565 5566
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5567
    size_t i;
5568
    int ret = -1;
C
Cole Robinson 已提交
5569

E
Eric Blake 已提交
5570 5571
    virCheckFlags(0, -1);

5572
    testDriverLock(privconn);
5573 5574
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5575
    testDriverUnlock(privconn);
5576 5577

    if (privpool == NULL) {
5578
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5579
        goto cleanup;
5580 5581 5582 5583 5584 5585
    }


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

    if (privvol == NULL) {
5586 5587 5588
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5589
        goto cleanup;
5590 5591 5592
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5593 5594
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5595
        goto cleanup;
5596 5597 5598
    }


5599
    privpool->def->allocation -= privvol->target.allocation;
C
Cole Robinson 已提交
5600 5601 5602
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5603
    for (i = 0; i < privpool->volumes.count; i++) {
C
Cole Robinson 已提交
5604 5605 5606
        if (privpool->volumes.objs[i] == privvol) {
            virStorageVolDefFree(privvol);

5607
            VIR_DELETE_ELEMENT(privpool->volumes.objs, i, privpool->volumes.count);
C
Cole Robinson 已提交
5608 5609 5610
            break;
        }
    }
5611
    ret = 0;
C
Cole Robinson 已提交
5612

5613
 cleanup:
5614 5615
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5616
    return ret;
C
Cole Robinson 已提交
5617 5618 5619
}


5620 5621
static int testStorageVolumeTypeForPool(int pooltype)
{
C
Cole Robinson 已提交
5622

5623
    switch (pooltype) {
C
Cole Robinson 已提交
5624 5625 5626 5627 5628 5629 5630 5631 5632 5633
        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
5634
testStorageVolGetInfo(virStorageVolPtr vol,
5635 5636
                      virStorageVolInfoPtr info)
{
5637 5638 5639
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5640
    int ret = -1;
5641

5642
    testDriverLock(privconn);
5643 5644
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5645
    testDriverUnlock(privconn);
5646 5647

    if (privpool == NULL) {
5648
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5649
        goto cleanup;
5650 5651 5652 5653 5654
    }

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

    if (privvol == NULL) {
5655 5656 5657
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5658
        goto cleanup;
5659 5660 5661
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5662 5663
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5664
        goto cleanup;
5665
    }
C
Cole Robinson 已提交
5666 5667 5668

    memset(info, 0, sizeof(*info));
    info->type = testStorageVolumeTypeForPool(privpool->def->type);
5669 5670
    info->capacity = privvol->target.capacity;
    info->allocation = privvol->target.allocation;
5671
    ret = 0;
C
Cole Robinson 已提交
5672

5673
 cleanup:
5674 5675
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5676
    return ret;
C
Cole Robinson 已提交
5677 5678 5679
}

static char *
5680 5681
testStorageVolGetXMLDesc(virStorageVolPtr vol,
                         unsigned int flags)
E
Eric Blake 已提交
5682
{
5683 5684 5685
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5686
    char *ret = NULL;
5687

E
Eric Blake 已提交
5688 5689
    virCheckFlags(0, NULL);

5690
    testDriverLock(privconn);
5691 5692
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5693
    testDriverUnlock(privconn);
5694 5695

    if (privpool == NULL) {
5696
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5697
        goto cleanup;
5698 5699 5700 5701 5702
    }

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

    if (privvol == NULL) {
5703 5704 5705
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5706
        goto cleanup;
5707
    }
C
Cole Robinson 已提交
5708

5709
    if (!virStoragePoolObjIsActive(privpool)) {
5710 5711
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5712
        goto cleanup;
5713 5714
    }

5715
    ret = virStorageVolDefFormat(privpool->def, privvol);
5716

5717
 cleanup:
5718 5719
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5720
    return ret;
C
Cole Robinson 已提交
5721 5722 5723
}

static char *
5724 5725
testStorageVolGetPath(virStorageVolPtr vol)
{
5726 5727 5728
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5729
    char *ret = NULL;
5730

5731
    testDriverLock(privconn);
5732 5733
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5734
    testDriverUnlock(privconn);
5735 5736

    if (privpool == NULL) {
5737
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5738
        goto cleanup;
5739 5740 5741 5742 5743
    }

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

    if (privvol == NULL) {
5744 5745 5746
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5747
        goto cleanup;
5748 5749 5750
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5751 5752
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5753
        goto cleanup;
5754 5755
    }

5756
    ignore_value(VIR_STRDUP(ret, privvol->target.path));
5757

5758
 cleanup:
5759 5760
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5761 5762 5763
    return ret;
}

5764

5765
/* Node device implementations */
5766

5767 5768 5769
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5770
                     unsigned int flags)
5771 5772 5773
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
5774
    size_t i;
5775

E
Eric Blake 已提交
5776 5777
    virCheckFlags(0, -1);

5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792
    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 已提交
5793
                    unsigned int flags)
5794 5795 5796
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
5797
    size_t i;
5798

E
Eric Blake 已提交
5799 5800
    virCheckFlags(0, -1);

5801 5802 5803 5804 5805
    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)) {
5806
            if (VIR_STRDUP(names[ndevs++], driver->devs.objs[i]->def->name) < 0) {
5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836
                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) {
5837
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5838 5839 5840 5841 5842
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

5843
 cleanup:
5844 5845 5846 5847 5848 5849
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
5850
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5851
                         unsigned int flags)
5852 5853 5854 5855 5856
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5857 5858
    virCheckFlags(0, NULL);

5859 5860 5861 5862 5863
    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
5864 5865 5866
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5867 5868 5869
        goto cleanup;
    }

5870
    ret = virNodeDeviceDefFormat(obj->def);
5871

5872
 cleanup:
5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889
    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) {
5890 5891 5892
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5893 5894 5895 5896
        goto cleanup;
    }

    if (obj->def->parent) {
5897
        ignore_value(VIR_STRDUP(ret, obj->def->parent));
5898
    } else {
5899 5900
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5901 5902
    }

5903
 cleanup:
5904 5905 5906 5907 5908
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

5909

5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923
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) {
5924 5925 5926
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5927 5928 5929 5930 5931 5932 5933
        goto cleanup;
    }

    for (caps = obj->def->caps; caps; caps = caps->next)
        ++ncaps;
    ret = ncaps;

5934
 cleanup:
5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954
    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) {
5955 5956 5957
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5958 5959 5960 5961
        goto cleanup;
    }

    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
5962
        if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->type)) < 0)
5963 5964 5965 5966
            goto cleanup;
    }
    ret = ncaps;

5967
 cleanup:
5968 5969 5970 5971 5972 5973 5974 5975 5976 5977
    if (obj)
        virNodeDeviceObjUnlock(obj);
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
}

5978 5979 5980
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
E
Eric Blake 已提交
5981
                        unsigned int flags)
5982 5983 5984 5985 5986 5987 5988 5989 5990
{
    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 已提交
5991 5992
    virCheckFlags(0, NULL);

5993 5994
    testDriverLock(driver);

5995
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, NULL);
5996
    if (def == NULL)
5997 5998 5999
        goto cleanup;

    /* We run these next two simply for validation */
6000
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1)
6001 6002
        goto cleanup;

6003
    if (virNodeDeviceGetParentHost(&driver->devs,
6004 6005 6006 6007 6008 6009 6010 6011 6012
                                   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);
6013
    if (VIR_STRDUP(def->name, wwpn) < 0)
6014 6015
        goto cleanup;

J
John Ferlan 已提交
6016 6017
    /* Fill in a random 'host' and 'unique_id' value,
     * since this would also come from the backend */
6018 6019 6020 6021 6022
    caps = def->caps;
    while (caps) {
        if (caps->type != VIR_NODE_DEV_CAP_SCSI_HOST)
            continue;

6023
        caps->data.scsi_host.host = virRandomBits(10);
J
John Ferlan 已提交
6024
        caps->data.scsi_host.unique_id = 2;
6025 6026 6027 6028
        caps = caps->next;
    }


6029
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def)))
6030 6031 6032 6033 6034
        goto cleanup;
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
6035
 cleanup:
6036
    testDriverUnlock(driver);
6037
    virNodeDeviceDefFree(def);
6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056
    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) {
6057
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
6058 6059 6060
        goto out;
    }

6061
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1)
6062 6063
        goto out;

6064
    if (VIR_STRDUP(parent_name, obj->def->parent) < 0)
6065 6066 6067 6068 6069 6070 6071 6072 6073
        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 */
6074
    if (virNodeDeviceGetParentHost(&driver->devs,
6075 6076 6077 6078 6079 6080 6081 6082 6083 6084
                                   dev->name,
                                   parent_name,
                                   &parent_host) == -1) {
        obj = NULL;
        goto out;
    }

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

6085
 out:
6086 6087 6088 6089 6090 6091 6092 6093
    if (obj)
        virNodeDeviceObjUnlock(obj);
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

6094 6095

/* Domain event implementations */
6096
static int
6097 6098 6099 6100
testConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
6101 6102
{
    testConnPtr driver = conn->privateData;
6103
    int ret = 0;
6104 6105

    testDriverLock(driver);
6106
    if (virDomainEventStateRegister(conn, driver->eventState,
6107 6108
                                    callback, opaque, freecb) < 0)
        ret = -1;
6109 6110 6111 6112 6113
    testDriverUnlock(driver);

    return ret;
}

6114

6115
static int
6116 6117
testConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
6118 6119
{
    testConnPtr driver = conn->privateData;
6120
    int ret = 0;
6121 6122

    testDriverLock(driver);
6123
    if (virDomainEventStateDeregister(conn, driver->eventState,
6124 6125
                                      callback) < 0)
        ret = -1;
6126 6127 6128 6129 6130
    testDriverUnlock(driver);

    return ret;
}

6131 6132

static int
6133 6134 6135 6136 6137 6138
testConnectDomainEventRegisterAny(virConnectPtr conn,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback callback,
                                  void *opaque,
                                  virFreeCallback freecb)
6139 6140 6141 6142 6143
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
6144
    if (virDomainEventStateRegisterID(conn, driver->eventState,
6145 6146
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
6147
        ret = -1;
6148 6149 6150 6151 6152 6153
    testDriverUnlock(driver);

    return ret;
}

static int
6154 6155
testConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
6156 6157
{
    testConnPtr driver = conn->privateData;
6158
    int ret = 0;
6159 6160

    testDriverLock(driver);
6161
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
6162 6163
                                        callbackID) < 0)
        ret = -1;
6164 6165 6166 6167 6168 6169
    testDriverUnlock(driver);

    return ret;
}


6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181
static int
testConnectNetworkEventRegisterAny(virConnectPtr conn,
                                   virNetworkPtr net,
                                   int eventID,
                                   virConnectNetworkEventGenericCallback callback,
                                   void *opaque,
                                   virFreeCallback freecb)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
6182
    if (virNetworkEventStateRegisterID(conn, driver->eventState,
6183
                                       net, eventID, callback,
6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195
                                       opaque, freecb, &ret) < 0)
        ret = -1;
    testDriverUnlock(driver);

    return ret;
}

static int
testConnectNetworkEventDeregisterAny(virConnectPtr conn,
                                     int callbackID)
{
    testConnPtr driver = conn->privateData;
6196
    int ret = 0;
6197 6198

    testDriverLock(driver);
6199
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
6200 6201
                                        callbackID) < 0)
        ret = -1;
6202 6203 6204 6205 6206 6207
    testDriverUnlock(driver);

    return ret;
}


6208
/* driver must be locked before calling */
6209
static void testObjectEventQueue(testConnPtr driver,
6210
                                 virObjectEventPtr event)
6211
{
6212
    virObjectEventStateQueue(driver->eventState, event);
6213 6214
}

6215

6216 6217 6218
static int testConnectListAllDomains(virConnectPtr conn,
                                     virDomainPtr **domains,
                                     unsigned int flags)
6219 6220 6221 6222
{
    testConnPtr privconn = conn->privateData;
    int ret;

O
Osier Yang 已提交
6223
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
6224 6225

    testDriverLock(privconn);
6226 6227
    ret = virDomainObjListExport(privconn->domains, conn, domains,
                                 NULL, flags);
6228 6229 6230 6231 6232
    testDriverUnlock(privconn);

    return ret;
}

6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245
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) {
6246
        if (VIR_ALLOC_N(*cpumap, 1) < 0)
6247 6248 6249 6250 6251 6252 6253 6254 6255
            goto cleanup;
        *cpumap[0] = 0x15;
    }

    if (online)
        *online = 3;

    ret = 8;

6256
 cleanup:
6257 6258 6259 6260
    testDriverUnlock(privconn);
    return ret;
}

6261 6262 6263 6264 6265 6266 6267 6268 6269 6270
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

6271
    if (VIR_STRDUP(ret, "image/png") < 0)
6272 6273
        return NULL;

6274
    if (virFDStreamOpenFile(st, PKGDATADIR "/libvirtLogo.png", 0, 0, O_RDONLY) < 0)
6275 6276 6277 6278 6279
        VIR_FREE(ret);

    return ret;
}

6280 6281 6282 6283 6284 6285 6286 6287 6288
static int
testConnectGetCPUModelNames(virConnectPtr conn ATTRIBUTE_UNUSED,
                            const char *arch,
                            char ***models,
                            unsigned int flags)
{
    virCheckFlags(0, -1);
    return cpuGetModels(arch, models);
}
6289

C
Cole Robinson 已提交
6290 6291 6292 6293 6294
static int
testDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr vm = NULL;
6295
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323
    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);
6324
    event = virDomainEventLifecycleNewFromObj(vm,
C
Cole Robinson 已提交
6325 6326 6327 6328 6329
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
    vm->hasManagedSave = true;

    ret = 0;
6330
 cleanup:
C
Cole Robinson 已提交
6331 6332 6333 6334
    if (vm)
        virObjectUnlock(vm);
    if (event) {
        testDriverLock(privconn);
6335
        testObjectEventQueue(privconn, event);
C
Cole Robinson 已提交
6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360
        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;
6361
 cleanup:
C
Cole Robinson 已提交
6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386
    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;
6387
 cleanup:
C
Cole Robinson 已提交
6388 6389 6390 6391 6392 6393 6394
    if (vm)
        virObjectUnlock(vm);
    testDriverUnlock(privconn);
    return ret;
}


6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438
/*
 * 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);

6439
 cleanup:
6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462
    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);

6463
 cleanup:
6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484
    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);

6485
 cleanup:
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
    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);

6513
 cleanup:
6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537
    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);

6538
 cleanup:
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
    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);

6565
 cleanup:
6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589
    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);

6590
 cleanup:
6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609
    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);

6610
 cleanup:
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
    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);

6641
 cleanup:
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
    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);

6667
 cleanup:
6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691
    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);

6692 6693 6694
    xml = virDomainSnapshotDefFormat(uuidstr, snap->def,
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);
6695

6696
 cleanup:
6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716
    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));

6717
 cleanup:
6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735
    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 已提交
6736
    if (!testSnapObjFromSnapshot(vm, snapshot))
6737 6738 6739 6740
        goto cleanup;

    ret = 1;

6741
 cleanup:
6742 6743 6744 6745 6746
    if (vm)
        virObjectUnlock(vm);
    return ret;
}

6747 6748 6749 6750 6751 6752
static int
testDomainSnapshotAlignDisks(virDomainObjPtr vm,
                             virDomainSnapshotDefPtr def,
                             unsigned int flags)
{
    int align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
E
Eric Blake 已提交
6753
    bool align_match = true;
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

    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;
6787
    virObjectEventPtr event = NULL;
6788
    char *xml = NULL;
6789 6790
    bool update_current = true;
    bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE;
6791 6792 6793 6794 6795 6796 6797 6798
    unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;

    /*
     * DISK_ONLY: Not implemented yet
     * REUSE_EXT: Not implemented yet
     *
     * NO_METADATA: Explicitly not implemented
     *
6799
     * REDEFINE + CURRENT: Implemented
6800 6801 6802 6803 6804 6805
     * HALT: Implemented
     * QUIESCE: Nothing to do
     * ATOMIC: Nothing to do
     * LIVE: Nothing to do
     */
    virCheckFlags(
6806 6807
        VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
        VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT |
6808 6809 6810 6811 6812
        VIR_DOMAIN_SNAPSHOT_CREATE_HALT |
        VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE |
        VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC |
        VIR_DOMAIN_SNAPSHOT_CREATE_LIVE, NULL);

6813 6814 6815 6816 6817
    if ((redefine && !(flags & VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT)))
        update_current = false;
    if (redefine)
        parse_flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;

6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833
    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;

6834
    if (redefine) {
C
Cole Robinson 已提交
6835 6836
        if (virDomainSnapshotRedefinePrep(domain, vm, &def, &snap,
                                          &update_current, flags) < 0)
6837 6838 6839 6840 6841 6842 6843
            goto cleanup;
    } else {
        if (!(def->dom = virDomainDefCopy(vm->def,
                                          privconn->caps,
                                          privconn->xmlopt,
                                          true)))
            goto cleanup;
6844

6845
        if (testDomainSnapshotAlignDisks(vm, def, flags) < 0)
6846 6847 6848
            goto cleanup;
    }

6849 6850 6851 6852
    if (!snap) {
        if (!(snap = virDomainSnapshotAssignDef(vm->snapshots, def)))
            goto cleanup;
        def = NULL;
6853 6854
    }

6855 6856 6857 6858 6859 6860 6861 6862 6863 6864
    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);
6865
            event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
6866 6867 6868
                                    VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
        }
    }
6869 6870

    snapshot = virGetDomainSnapshot(domain, snap->def->name);
6871
 cleanup:
6872 6873 6874 6875
    VIR_FREE(xml);
    if (vm) {
        if (snapshot) {
            virDomainSnapshotObjPtr other;
6876 6877
            if (update_current)
                vm->current_snapshot = snap;
6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888
            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);
6889
        testObjectEventQueue(privconn, event);
6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933
        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;

6934
    if (rep->err < 0)
6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976
        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) {
6977
            if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY)
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
                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;
7021
 cleanup:
7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033
    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;
7034 7035
    virObjectEventPtr event = NULL;
    virObjectEventPtr event2 = NULL;
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 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128
    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);
7129
                event = virDomainEventLifecycleNewFromObj(vm,
7130 7131 7132
                            VIR_DOMAIN_EVENT_STOPPED,
                            VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
                if (event)
7133
                    testObjectEventQueue(privconn, event);
7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144
                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. */
7145
                event = virDomainEventLifecycleNewFromObj(vm,
7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158
                                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;
7159
            event = virDomainEventLifecycleNewFromObj(vm,
7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172
                                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 */
7173
                event2 = virDomainEventLifecycleNewFromObj(vm,
7174 7175 7176 7177 7178
                                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 已提交
7179
            virObjectUnref(event);
7180 7181 7182 7183
            event = NULL;

            if (was_stopped) {
                /* Transition 2 */
7184
                event = virDomainEventLifecycleNewFromObj(vm,
7185 7186 7187 7188
                                VIR_DOMAIN_EVENT_STARTED,
                                VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            } else if (was_running) {
                /* Transition 8 */
7189
                event = virDomainEventLifecycleNewFromObj(vm,
7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201
                                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);
7202
            event = virDomainEventLifecycleNewFromObj(vm,
7203 7204 7205 7206 7207 7208 7209 7210 7211 7212
                                    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)
7213
                testObjectEventQueue(privconn, event);
7214
            event = virDomainEventLifecycleNewFromObj(vm,
7215 7216 7217
                            VIR_DOMAIN_EVENT_STARTED,
                            VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            if (paused) {
7218
                event2 = virDomainEventLifecycleNewFromObj(vm,
7219 7220 7221 7222 7223 7224 7225 7226
                                VIR_DOMAIN_EVENT_SUSPENDED,
                                VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT);
            }
        }
    }

    vm->current_snapshot = snap;
    ret = 0;
7227
 cleanup:
7228
    if (event) {
7229
        testObjectEventQueue(privconn, event);
7230
        if (event2)
7231
            testObjectEventQueue(privconn, event2);
C
Cole Robinson 已提交
7232
    } else {
C
Cédric Bosdonnat 已提交
7233
        virObjectUnref(event2);
7234 7235 7236 7237 7238 7239 7240 7241
    }
    virObjectUnlock(vm);
    testDriverUnlock(privconn);

    return ret;
}


7242

7243
static virHypervisorDriver testHypervisorDriver = {
7244
    .name = "Test",
7245 7246 7247
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
7248
    .connectGetHostname = testConnectGetHostname, /* 0.6.3 */
7249
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
7250
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
7251 7252 7253 7254
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
7255
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269
    .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 */
7270 7271
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
7272
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
7273
    .domainRestore = testDomainRestore, /* 0.3.2 */
7274
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
7275
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
7276
    .domainCoreDumpWithFormat = testDomainCoreDumpWithFormat, /* 1.2.3 */
7277
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
7278 7279 7280 7281 7282 7283
    .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 */
7284 7285
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
7286 7287 7288
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
7289
    .domainDefineXMLFlags = testDomainDefineXMLFlags, /* 1.2.12 */
7290
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
7291
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
7292 7293 7294
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
7295 7296 7297 7298
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
7299 7300 7301
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
7302 7303 7304 7305
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
7306 7307 7308
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
7309 7310 7311
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
7312
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
7313
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
7314 7315
    .domainGetMetadata = testDomainGetMetadata, /* 1.1.3 */
    .domainSetMetadata = testDomainSetMetadata, /* 1.1.3 */
7316
    .connectGetCPUModelNames = testConnectGetCPUModelNames, /* 1.1.3 */
7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333
    .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 */
7334 7335 7336
    .domainSnapshotCreateXML = testDomainSnapshotCreateXML, /* 1.1.4 */
    .domainRevertToSnapshot = testDomainRevertToSnapshot, /* 1.1.4 */
    .domainSnapshotDelete = testDomainSnapshotDelete, /* 1.1.4 */
7337

E
Eric Blake 已提交
7338
    .connectBaselineCPU = testConnectBaselineCPU, /* 1.2.0 */
7339 7340 7341
};

static virNetworkDriver testNetworkDriver = {
7342 7343 7344 7345 7346
    .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 */
7347 7348
    .connectNetworkEventRegisterAny = testConnectNetworkEventRegisterAny, /* 1.2.1 */
    .connectNetworkEventDeregisterAny = testConnectNetworkEventDeregisterAny, /* 1.2.1 */
7349 7350 7351 7352
    .networkLookupByUUID = testNetworkLookupByUUID, /* 0.3.2 */
    .networkLookupByName = testNetworkLookupByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreateXML, /* 0.3.2 */
    .networkDefineXML = testNetworkDefineXML, /* 0.3.2 */
7353
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
7354
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
7355
    .networkCreate = testNetworkCreate, /* 0.3.2 */
7356 7357 7358 7359 7360 7361 7362
    .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 */
7363 7364
};

L
Laine Stump 已提交
7365
static virInterfaceDriver testInterfaceDriver = {
7366 7367 7368 7369 7370 7371
    .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 */
7372 7373 7374 7375 7376 7377
    .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 */
7378 7379 7380
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
7381 7382 7383
};


7384
static virStorageDriver testStorageDriver = {
7385 7386 7387 7388 7389 7390
    .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 */
7391 7392 7393
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
7394 7395
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
7396 7397
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
7398
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
7399 7400 7401 7402 7403 7404 7405
    .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 */
7406
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
7407 7408 7409
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

7410 7411 7412 7413 7414 7415 7416 7417 7418
    .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 */
7419 7420
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
7421 7422
};

7423
static virNodeDeviceDriver testNodeDeviceDriver = {
7424 7425 7426 7427 7428 7429 7430 7431 7432
    .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 */
7433 7434
};

7435 7436 7437 7438 7439 7440 7441 7442
static virConnectDriver testConnectDriver = {
    .hypervisorDriver = &testHypervisorDriver,
    .interfaceDriver = &testInterfaceDriver,
    .networkDriver = &testNetworkDriver,
    .nodeDeviceDriver = &testNodeDeviceDriver,
    .nwfilterDriver = NULL,
    .secretDriver = NULL,
    .storageDriver = &testStorageDriver,
7443 7444
};

7445 7446 7447 7448 7449 7450 7451 7452
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
7453 7454
    return virRegisterConnectDriver(&testConnectDriver,
                                    false);
7455
}