test_driver.c 207.4 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
    if (!(privconn->domains = virDomainObjListNew()) ||
728
        !(privconn->networks = virNetworkObjListNew()))
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
    virNetworkObjEndAPI(&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
    virObjectUnref(privconn->networks);
L
Laine Stump 已提交
834
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
835
    virStoragePoolObjListFree(&privconn->pools);
836
    virNodeDeviceObjListFree(&privconn->devs);
837
    virObjectUnref(privconn->caps);
838
    virObjectEventStateFree(privconn->eventState);
839
    virMutexDestroy(&privconn->lock);
840
    conn->privateData = NULL;
841
    virDomainDefFree(domdef);
842 843
    defaultConnections--;
    virMutexUnlock(&defaultLock);
844
    return VIR_DRV_OPEN_ERROR;
845 846 847 848
}


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

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

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

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

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

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

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

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

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

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

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

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

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

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

1005 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
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;
1058
 error:
1059 1060 1061
    return ret;
}

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

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

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

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

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

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

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

1119
        virObjectUnlock(obj);
1120
    }
1121

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

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

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

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

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

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

        obj->active = 1;
1158
        virNetworkObjEndAPI(&obj);
1159
    }
1160

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        virNodeDeviceObjUnlock(obj);
    }

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

1354 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
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;
1389
 error:
1390 1391 1392
    VIR_FREE(nodes);
    return ret;
}
1393 1394 1395

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

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

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

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

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

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

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

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

1462
    return 0;
1463 1464

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

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

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

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

E
Eric Blake 已提交
1540 1541
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1542
    if (!conn->uri)
1543
        return VIR_DRV_OPEN_DECLINED;
1544

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

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

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

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

1567 1568 1569
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

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

1574
    return VIR_DRV_OPEN_SUCCESS;
1575 1576
}

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

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

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

1600
    testDriverUnlock(privconn);
1601
    virMutexDestroy(&privconn->lock);
1602

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

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

1618 1619 1620 1621 1622 1623
static char *testConnectGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return virGetHostname();
}


1624
static int testConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
1625 1626 1627 1628
{
    return 1;
}

1629
static int testConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
1630 1631 1632 1633
{
    return 0;
}

1634
static int testConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
1635 1636 1637 1638
{
    return 1;
}

1639 1640
static int testConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type ATTRIBUTE_UNUSED)
1641 1642 1643 1644
{
    return 32;
}

1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
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;
}

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

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

1680
static int testConnectNumOfDomains(virConnectPtr conn)
1681
{
1682
    testConnPtr privconn = conn->privateData;
1683
    int count;
1684

1685
    testDriverLock(privconn);
1686
    count = virDomainObjListNumOfDomains(privconn->domains, true, NULL, NULL);
1687
    testDriverUnlock(privconn);
1688

1689
    return count;
1690 1691
}

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

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

1707
 cleanup:
1708
    if (obj)
1709
        virObjectUnlock(obj);
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
    return ret;
}

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

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

1728
 cleanup:
1729
    if (obj)
1730
        virObjectUnlock(obj);
1731 1732 1733
    return ret;
}

1734 1735 1736 1737 1738
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

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

1750 1751 1752 1753
    virCheckFlags(VIR_DOMAIN_START_VALIDATE, NULL);

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

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

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

1771
    if (testDomainStartState(privconn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0)
1772
        goto cleanup;
1773

1774
    event = virDomainEventLifecycleNewFromObj(dom,
1775 1776 1777
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1778
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1779
    if (ret)
1780
        ret->id = dom->def->id;
1781

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


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

1800
    testDriverLock(privconn);
1801
    dom = virDomainObjListFindByID(privconn->domains, id);
1802 1803 1804
    testDriverUnlock(privconn);

    if (dom == NULL) {
1805
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1806
        goto cleanup;
1807 1808
    }

1809
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1810 1811 1812
    if (ret)
        ret->id = dom->def->id;

1813
 cleanup:
1814
    if (dom)
1815
        virObjectUnlock(dom);
1816
    return ret;
1817 1818
}

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

1826
    testDriverLock(privconn);
1827
    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
1828 1829 1830
    testDriverUnlock(privconn);

    if (dom == NULL) {
1831
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1832
        goto cleanup;
1833
    }
1834

1835
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1836 1837 1838
    if (ret)
        ret->id = dom->def->id;

1839
 cleanup:
1840
    if (dom)
1841
        virObjectUnlock(dom);
1842
    return ret;
1843 1844
}

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

1852
    testDriverLock(privconn);
1853
    dom = virDomainObjListFindByName(privconn->domains, name);
1854 1855 1856
    testDriverUnlock(privconn);

    if (dom == NULL) {
1857
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1858
        goto cleanup;
1859
    }
1860

1861
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1862 1863 1864
    if (ret)
        ret->id = dom->def->id;

1865
 cleanup:
1866
    if (dom)
1867
        virObjectUnlock(dom);
1868
    return ret;
1869 1870
}

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

1878
    testDriverLock(privconn);
1879
    n = virDomainObjListGetActiveIDs(privconn->domains, ids, maxids, NULL, NULL);
1880
    testDriverUnlock(privconn);
1881

1882
    return n;
1883 1884
}

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

1892
    testDriverLock(privconn);
1893 1894
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1895 1896

    if (privdom == NULL) {
1897
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1898
        goto cleanup;
1899
    }
1900

J
Jiri Denemark 已提交
1901
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1902
    event = virDomainEventLifecycleNewFromObj(privdom,
1903 1904
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1905

1906
    if (!privdom->persistent) {
1907 1908
        virDomainObjListRemove(privconn->domains,
                               privdom);
1909
        privdom = NULL;
1910
    }
1911 1912

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

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

1929
    testDriverLock(privconn);
1930 1931
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1932
    testDriverUnlock(privconn);
1933 1934

    if (privdom == NULL) {
1935
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1936
        goto cleanup;
1937
    }
1938

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

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

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

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

1971
    testDriverLock(privconn);
1972 1973
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1974
    testDriverUnlock(privconn);
1975 1976

    if (privdom == NULL) {
1977
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1978
        goto cleanup;
1979
    }
1980

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

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

1994
 cleanup:
1995
    if (privdom)
1996
        virObjectUnlock(privdom);
1997 1998 1999

    if (event) {
        testDriverLock(privconn);
2000
        testObjectEventQueue(privconn, event);
2001 2002
        testDriverUnlock(privconn);
    }
2003
    return ret;
2004 2005
}

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

2014 2015
    virCheckFlags(0, -1);

2016
    testDriverLock(privconn);
2017 2018
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2019 2020

    if (privdom == NULL) {
2021
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2022
        goto cleanup;
2023
    }
2024

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

J
Jiri Denemark 已提交
2031
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
2032
    event = virDomainEventLifecycleNewFromObj(privdom,
2033 2034
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
2035

2036
    if (!privdom->persistent) {
2037 2038
        virDomainObjListRemove(privconn->domains,
                               privdom);
2039 2040
        privdom = NULL;
    }
2041

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

2052
static int testDomainShutdown(virDomainPtr domain)
2053
{
2054
    return testDomainShutdownFlags(domain, 0);
2055 2056
}

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

2066
    testDriverLock(privconn);
2067 2068
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2069 2070

    if (privdom == NULL) {
2071
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2072
        goto cleanup;
2073
    }
2074

J
Jiri Denemark 已提交
2075 2076 2077
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

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

2084
    case VIR_DOMAIN_LIFECYCLE_RESTART:
J
Jiri Denemark 已提交
2085 2086
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
2087 2088
        break;

2089
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
J
Jiri Denemark 已提交
2090 2091
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
2092 2093
        break;

2094
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
J
Jiri Denemark 已提交
2095 2096
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
2097
        break;
2098

2099
    default:
J
Jiri Denemark 已提交
2100 2101
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
2102 2103
        break;
    }
2104

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

2111
        if (!privdom->persistent) {
2112 2113
            virDomainObjListRemove(privconn->domains,
                                   privdom);
2114 2115
            privdom = NULL;
        }
2116 2117
    }

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

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

2136
    testDriverLock(privconn);
2137 2138
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2139
    testDriverUnlock(privconn);
2140 2141

    if (privdom == NULL) {
2142
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2143
        goto cleanup;
2144
    }
2145 2146

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

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

2159
 cleanup:
2160
    if (privdom)
2161
        virObjectUnlock(privdom);
2162
    return ret;
2163 2164
}

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

    if (privdom == NULL) {
2183
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2184 2185 2186
        goto cleanup;
    }

J
Jiri Denemark 已提交
2187
    *state = virDomainObjGetState(privdom, reason);
2188 2189
    ret = 0;

2190
 cleanup:
2191
    if (privdom)
2192
        virObjectUnlock(privdom);
2193 2194 2195
    return ret;
}

2196 2197
#define TEST_SAVE_MAGIC "TestGuestMagic"

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

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

2217
    testDriverLock(privconn);
2218 2219
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2220 2221

    if (privdom == NULL) {
2222
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2223
        goto cleanup;
2224
    }
2225

2226
    xml = virDomainDefFormat(privdom->def,
2227
                             VIR_DOMAIN_DEF_FORMAT_SECURE);
C
Cole Robinson 已提交
2228

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

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

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

J
Jiri Denemark 已提交
2270
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
2271
    event = virDomainEventLifecycleNewFromObj(privdom,
2272 2273
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
2274

2275
    if (!privdom->persistent) {
2276 2277
        virDomainObjListRemove(privconn->domains,
                               privdom);
2278
        privdom = NULL;
2279
    }
2280

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

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

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

2330 2331
    testDriverLock(privconn);

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

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

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

2386
    if (testDomainStartState(privconn, dom, VIR_DOMAIN_RUNNING_RESTORED) < 0)
2387 2388
        goto cleanup;

2389
    event = virDomainEventLifecycleNewFromObj(dom,
2390 2391
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
2392
    ret = 0;
2393

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

2406 2407 2408 2409 2410 2411 2412
static int
testDomainRestore(virConnectPtr conn,
                  const char *path)
{
    return testDomainRestoreFlags(conn, path, NULL, 0);
}

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

E
Eric Blake 已提交
2424 2425
    virCheckFlags(VIR_DUMP_CRASH, -1);

2426
    testDriverLock(privconn);
2427 2428
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2429 2430

    if (privdom == NULL) {
2431
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2432
        goto cleanup;
2433
    }
2434 2435

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

2454 2455 2456 2457 2458 2459 2460
    /* 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;
    }

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

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

2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497

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)
{
2498 2499 2500
    char *ret;

    ignore_value(VIR_STRDUP(ret, "linux"));
2501
    return ret;
2502 2503
}

2504 2505 2506

static unsigned long long
testDomainGetMaxMemory(virDomainPtr domain)
2507
{
2508 2509
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2510
    unsigned long long ret = 0;
2511

2512
    testDriverLock(privconn);
2513 2514
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2515
    testDriverUnlock(privconn);
2516 2517

    if (privdom == NULL) {
2518
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2519
        goto cleanup;
2520
    }
2521

2522
    ret = privdom->def->mem.max_balloon;
2523

2524
 cleanup:
2525
    if (privdom)
2526
        virObjectUnlock(privdom);
2527
    return ret;
2528 2529
}

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

2537
    testDriverLock(privconn);
2538 2539
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2540
    testDriverUnlock(privconn);
2541 2542

    if (privdom == NULL) {
2543
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2544
        goto cleanup;
2545
    }
2546 2547

    /* XXX validate not over host memory wrt to other domains */
2548
    privdom->def->mem.max_balloon = memory;
2549 2550
    ret = 0;

2551
 cleanup:
2552
    if (privdom)
2553
        virObjectUnlock(privdom);
2554
    return ret;
2555 2556
}

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

2564
    testDriverLock(privconn);
2565 2566
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2567
    testDriverUnlock(privconn);
2568 2569

    if (privdom == NULL) {
2570
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2571
        goto cleanup;
2572
    }
2573

2574
    if (memory > privdom->def->mem.max_balloon) {
2575
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2576
        goto cleanup;
2577
    }
2578

2579
    privdom->def->mem.cur_balloon = memory;
2580 2581
    ret = 0;

2582
 cleanup:
2583
    if (privdom)
2584
        virObjectUnlock(privdom);
2585
    return ret;
2586 2587
}

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

2596 2597
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2598 2599 2600
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    testDriverLock(privconn);
2601
    vm = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
2602 2603 2604 2605 2606
    testDriverUnlock(privconn);

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

2612
    if (virDomainLiveConfigHelperMethod(privconn->caps, privconn->xmlopt,
2613
                                        vm, &flags, &def) < 0)
2614
        goto cleanup;
2615

2616
    if (flags & VIR_DOMAIN_AFFECT_LIVE)
2617 2618 2619 2620
        def = vm->def;

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

2621
 cleanup:
2622
    if (vm)
2623
        virObjectUnlock(vm);
2624
    return ret;
C
Cole Robinson 已提交
2625 2626
}

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

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

2643 2644
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2645 2646 2647 2648
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

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

2662
    testDriverLock(privconn);
2663
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
2664 2665 2666
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2667
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2668
        goto cleanup;
2669
    }
2670

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

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

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

2690
    if (!(persistentDef = virDomainObjGetPersistentDef(privconn->caps,
2691
                                                       privconn->xmlopt,
2692 2693 2694
                                                       privdom)))
        goto cleanup;

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

2703
    case VIR_DOMAIN_AFFECT_CONFIG:
2704
        persistentDef->vcpus = nrCpus;
2705 2706 2707
        ret = 0;
        break;

2708
    case VIR_DOMAIN_AFFECT_LIVE:
2709
        ret = testDomainUpdateVCPUs(privconn, privdom, nrCpus, 0);
2710 2711
        break;

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

2719
 cleanup:
2720
    if (privdom)
2721
        virObjectUnlock(privdom);
2722
    return ret;
2723 2724
}

2725
static int
2726
testDomainSetVcpus(virDomainPtr domain, unsigned int nrCpus)
2727
{
2728
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2729 2730
}

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

    testDriverLock(privconn);
2747
    privdom = virDomainObjListFindByName(privconn->domains, domain->name);
C
Cole Robinson 已提交
2748 2749 2750
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2751
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2752 2753 2754 2755
        goto cleanup;
    }

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

    privdomdata = privdom->privateData;

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

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

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

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

    ret = maxinfo;
2813
 cleanup:
C
Cole Robinson 已提交
2814
    if (privdom)
2815
        virObjectUnlock(privdom);
C
Cole Robinson 已提交
2816 2817 2818
    return ret;
}

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

    testDriverLock(privconn);
2833
    privdom = virDomainObjListFindByName(privconn->domains, domain->name);
C
Cole Robinson 已提交
2834 2835 2836
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2837
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2838 2839 2840 2841
        goto cleanup;
    }

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

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

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

    ret = 0;
2870
 cleanup:
C
Cole Robinson 已提交
2871
    if (privdom)
2872
        virObjectUnlock(privdom);
C
Cole Robinson 已提交
2873 2874 2875
    return ret;
}

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

2883 2884
    /* Flags checked by virDomainDefFormat */

2885
    testDriverLock(privconn);
2886 2887
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2888 2889 2890
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2891
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2892
        goto cleanup;
2893
    }
2894

2895 2896
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2897

2898
    ret = virDomainDefFormat(def,
2899
                             virDomainDefFormatConvertXMLFlags(flags));
2900

2901
 cleanup:
2902
    if (privdom)
2903
        virObjectUnlock(privdom);
2904
    return ret;
2905
}
2906

2907 2908
static int testConnectNumOfDefinedDomains(virConnectPtr conn)
{
2909
    testConnPtr privconn = conn->privateData;
2910
    int count;
2911

2912
    testDriverLock(privconn);
2913
    count = virDomainObjListNumOfDomains(privconn->domains, false, NULL, NULL);
2914
    testDriverUnlock(privconn);
2915

2916
    return count;
2917 2918
}

2919 2920
static int testConnectListDefinedDomains(virConnectPtr conn,
                                         char **const names,
2921 2922
                                         int maxnames)
{
2923

2924
    testConnPtr privconn = conn->privateData;
2925
    int n;
2926

2927
    testDriverLock(privconn);
2928
    memset(names, 0, sizeof(*names)*maxnames);
2929 2930
    n = virDomainObjListGetInactiveNames(privconn->domains, names, maxnames,
                                         NULL, NULL);
2931
    testDriverUnlock(privconn);
2932

2933
    return n;
2934 2935
}

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

    virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL);
2949

2950 2951
    if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
        parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE;
2952

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

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

2970
    event = virDomainEventLifecycleNewFromObj(dom,
2971
                                     VIR_DOMAIN_EVENT_DEFINED,
2972
                                     !oldDef ?
2973 2974
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2975

2976
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2977
    if (ret)
2978
        ret->id = dom->def->id;
2979

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

2991 2992 2993 2994 2995 2996
static virDomainPtr
testDomainDefineXML(virConnectPtr conn, const char *xml)
{
    return testDomainDefineXMLFlags(conn, xml, 0);
}

2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021
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);

3022
 cleanup:
3023 3024 3025 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
    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,
3054
                                  NULL, NULL, flags);
3055

3056
 cleanup:
3057 3058 3059 3060 3061 3062
    if (privdom)
        virObjectUnlock(privdom);
    return ret;
}


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

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

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

3086
 cleanup:
3087
    testDriverUnlock(privconn);
3088
    return ret;
3089 3090 3091
}


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

3099 3100
    virCheckFlags(0, -1);

3101
    testDriverLock(privconn);
3102 3103
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3104 3105

    if (privdom == NULL) {
3106
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3107
        goto cleanup;
3108
    }
3109

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

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

3121
    event = virDomainEventLifecycleNewFromObj(privdom,
3122 3123
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
3124
    ret = 0;
3125

3126
 cleanup:
3127
    if (privdom)
3128
        virObjectUnlock(privdom);
3129
    if (event)
3130
        testObjectEventQueue(privconn, event);
3131
    testDriverUnlock(privconn);
3132
    return ret;
3133 3134
}

3135 3136
static int testDomainCreate(virDomainPtr domain)
{
3137 3138 3139
    return testDomainCreateWithFlags(domain, 0);
}

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

3149 3150
    virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
                  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, -1);
3151

3152
    testDriverLock(privconn);
3153 3154
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3155 3156

    if (privdom == NULL) {
3157
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3158
        goto cleanup;
3159
    }
3160

C
Cole Robinson 已提交
3161 3162 3163 3164 3165 3166 3167 3168
    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;
    }

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

3187
    event = virDomainEventLifecycleNewFromObj(privdom,
3188 3189
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
C
Cole Robinson 已提交
3190 3191
    privdom->hasManagedSave = false;

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

3200
    ret = 0;
3201

3202
 cleanup:
3203
    if (privdom)
3204
        virObjectUnlock(privdom);
3205
    if (event)
3206
        testObjectEventQueue(privconn, event);
3207
    testDriverUnlock(privconn);
3208
    return ret;
3209 3210
}

3211 3212 3213 3214 3215
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

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

3223
    testDriverLock(privconn);
3224 3225
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3226
    testDriverUnlock(privconn);
3227 3228

    if (privdom == NULL) {
3229
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3230
        goto cleanup;
3231 3232
    }

3233
    *autostart = privdom->autostart;
3234 3235
    ret = 0;

3236
 cleanup:
3237
    if (privdom)
3238
        virObjectUnlock(privdom);
3239
    return ret;
3240 3241 3242 3243 3244 3245
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
3246 3247
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
3248
    int ret = -1;
3249

3250
    testDriverLock(privconn);
3251 3252
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3253
    testDriverUnlock(privconn);
3254 3255

    if (privdom == NULL) {
3256
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3257
        goto cleanup;
3258 3259
    }

3260
    privdom->autostart = autostart ? 1 : 0;
3261 3262
    ret = 0;

3263
 cleanup:
3264
    if (privdom)
3265
        virObjectUnlock(privdom);
3266
    return ret;
3267
}
3268

3269
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
3270 3271
                                        int *nparams)
{
3272 3273
    char *type = NULL;

3274 3275 3276
    if (nparams)
        *nparams = 1;

3277
    ignore_value(VIR_STRDUP(type, "fair"));
3278

3279 3280 3281
    return type;
}

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

3292 3293
    virCheckFlags(0, -1);

3294
    testDriverLock(privconn);
3295 3296
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3297
    testDriverUnlock(privconn);
3298 3299

    if (privdom == NULL) {
3300
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3301
        goto cleanup;
3302 3303
    }

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

    *nparams = 1;
3311 3312
    ret = 0;

3313
 cleanup:
3314
    if (privdom)
3315
        virObjectUnlock(privdom);
3316
    return ret;
3317
}
3318

3319
static int
3320 3321 3322
testDomainGetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int *nparams)
3323
{
3324
    return testDomainGetSchedulerParametersFlags(domain, params, nparams, 0);
3325
}
3326

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

3338
    virCheckFlags(0, -1);
3339 3340 3341 3342
    if (virTypedParamsValidate(params, nparams,
                               VIR_DOMAIN_SCHEDULER_WEIGHT,
                               VIR_TYPED_PARAM_UINT,
                               NULL) < 0)
3343
        return -1;
3344

3345
    testDriverLock(privconn);
3346 3347
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3348
    testDriverUnlock(privconn);
3349 3350

    if (privdom == NULL) {
3351
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3352
        goto cleanup;
3353 3354
    }

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

3362 3363
    ret = 0;

3364
 cleanup:
3365
    if (privdom)
3366
        virObjectUnlock(privdom);
3367
    return ret;
3368 3369
}

3370
static int
3371 3372 3373
testDomainSetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int nparams)
3374
{
3375
    return testDomainSetSchedulerParametersFlags(domain, params, nparams, 0);
3376 3377
}

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

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

3394
    testDriverLock(privconn);
3395 3396
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3397 3398 3399
    testDriverUnlock(privconn);

    if (privdom == NULL) {
3400
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3401 3402 3403
        goto error;
    }

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

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

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

    testDriverLock(privconn);
3443 3444
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
3445 3446 3447
    testDriverUnlock(privconn);

    if (privdom == NULL) {
3448
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3449 3450 3451
        goto error;
    }

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

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

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

3490

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

3498
    testDriverLock(privconn);
3499
    net = virNetworkObjFindByUUID(privconn->networks, uuid);
3500 3501 3502
    testDriverUnlock(privconn);

    if (net == NULL) {
3503
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3504
        goto cleanup;
3505 3506
    }

3507 3508
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

3509
 cleanup:
3510
    virNetworkObjEndAPI(&net);
3511
    return ret;
3512
}
3513

3514
static virNetworkPtr testNetworkLookupByName(virConnectPtr conn,
3515
                                             const char *name)
3516
{
3517
    testConnPtr privconn = conn->privateData;
3518 3519
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
3520

3521
    testDriverLock(privconn);
3522
    net = virNetworkObjFindByName(privconn->networks, name);
3523 3524 3525
    testDriverUnlock(privconn);

    if (net == NULL) {
3526
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3527
        goto cleanup;
3528 3529
    }

3530 3531
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

3532
 cleanup:
3533
    virNetworkObjEndAPI(&net);
3534
    return ret;
3535 3536 3537
}


3538 3539
static int testConnectNumOfNetworks(virConnectPtr conn)
{
3540
    testConnPtr privconn = conn->privateData;
3541
    int numActive;
3542

3543
    testDriverLock(privconn);
3544 3545
    numActive = virNetworkObjListNumOfNetworks(privconn->networks,
                                               true, NULL, conn);
3546
    testDriverUnlock(privconn);
3547

3548
    return numActive;
3549 3550
}

3551
static int testConnectListNetworks(virConnectPtr conn, char **const names, int nnames) {
3552
    testConnPtr privconn = conn->privateData;
3553
    int n;
3554

3555
    testDriverLock(privconn);
3556 3557
    n = virNetworkObjListGetNames(privconn->networks,
                                  true, names, nnames, NULL, conn);
3558
    testDriverUnlock(privconn);
3559

3560
    return n;
3561 3562
}

3563 3564
static int testConnectNumOfDefinedNetworks(virConnectPtr conn)
{
3565
    testConnPtr privconn = conn->privateData;
3566
    int numInactive;
3567

3568
    testDriverLock(privconn);
3569 3570
    numInactive = virNetworkObjListNumOfNetworks(privconn->networks,
                                                 false, NULL, conn);
3571
    testDriverUnlock(privconn);
3572

3573
    return numInactive;
3574 3575
}

3576
static int testConnectListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
3577
    testConnPtr privconn = conn->privateData;
3578
    int n;
3579

3580
    testDriverLock(privconn);
3581 3582
    n = virNetworkObjListGetNames(privconn->networks,
                                  false, names, nnames, NULL, conn);
3583
    testDriverUnlock(privconn);
3584

3585
    return n;
3586 3587
}

3588
static int
3589
testConnectListAllNetworks(virConnectPtr conn,
3590 3591 3592 3593 3594 3595 3596 3597 3598
                           virNetworkPtr **nets,
                           unsigned int flags)
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

    testDriverLock(privconn);
3599
    ret = virNetworkObjListExport(conn, privconn->networks, nets, NULL, flags);
3600 3601 3602 3603
    testDriverUnlock(privconn);

    return ret;
}
3604 3605 3606 3607 3608 3609 3610 3611

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

    testDriverLock(privconn);
3612
    obj = virNetworkObjFindByUUID(privconn->networks, net->uuid);
3613 3614
    testDriverUnlock(privconn);
    if (!obj) {
3615
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3616 3617 3618 3619
        goto cleanup;
    }
    ret = virNetworkObjIsActive(obj);

3620
 cleanup:
3621
    virNetworkObjEndAPI(&obj);
3622 3623 3624 3625 3626 3627 3628 3629 3630 3631
    return ret;
}

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

    testDriverLock(privconn);
3632
    obj = virNetworkObjFindByUUID(privconn->networks, net->uuid);
3633 3634
    testDriverUnlock(privconn);
    if (!obj) {
3635
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3636 3637 3638 3639
        goto cleanup;
    }
    ret = obj->persistent;

3640
 cleanup:
3641
    virNetworkObjEndAPI(&obj);
3642 3643 3644 3645
    return ret;
}


3646 3647
static virNetworkPtr testNetworkCreateXML(virConnectPtr conn, const char *xml)
{
3648
    testConnPtr privconn = conn->privateData;
3649
    virNetworkDefPtr def;
3650
    virNetworkObjPtr net = NULL;
3651
    virNetworkPtr ret = NULL;
3652
    virObjectEventPtr event = NULL;
3653

3654
    testDriverLock(privconn);
3655
    if ((def = virNetworkDefParseString(xml)) == NULL)
3656
        goto cleanup;
3657

3658
    if (!(net = virNetworkAssignDef(privconn->networks, def, true)))
3659 3660
        goto cleanup;
    def = NULL;
3661
    net->active = 1;
3662

3663
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3664 3665
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3666

3667
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3668

3669
 cleanup:
3670
    virNetworkDefFree(def);
3671 3672
    if (event)
        testObjectEventQueue(privconn, event);
3673
    virNetworkObjEndAPI(&net);
3674
    testDriverUnlock(privconn);
3675
    return ret;
3676 3677
}

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

3687
    testDriverLock(privconn);
3688
    if ((def = virNetworkDefParseString(xml)) == NULL)
3689
        goto cleanup;
3690

3691
    if (!(net = virNetworkAssignDef(privconn->networks, def, false)))
3692 3693
        goto cleanup;
    def = NULL;
3694

3695
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3696 3697
                                        VIR_NETWORK_EVENT_DEFINED,
                                        0);
3698

3699
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3700

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

3710 3711
static int testNetworkUndefine(virNetworkPtr network)
{
3712 3713
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3714
    int ret = -1;
3715
    virObjectEventPtr event = NULL;
3716

3717
    testDriverLock(privconn);
3718
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3719 3720

    if (privnet == NULL) {
3721
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3722
        goto cleanup;
3723
    }
3724

D
Daniel P. Berrange 已提交
3725
    if (virNetworkObjIsActive(privnet)) {
3726 3727
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is still running"), network->name);
3728
        goto cleanup;
3729 3730
    }

3731
    event = virNetworkEventLifecycleNew(network->name, network->uuid,
3732 3733
                                        VIR_NETWORK_EVENT_UNDEFINED,
                                        0);
3734

3735
    virNetworkRemoveInactive(privconn->networks, privnet);
3736
    ret = 0;
3737

3738
 cleanup:
3739 3740
    if (event)
        testObjectEventQueue(privconn, event);
3741
    virNetworkObjEndAPI(&privnet);
3742
    testDriverUnlock(privconn);
3743
    return ret;
3744 3745
}

3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763
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);

3764
    network = virNetworkObjFindByUUID(privconn->networks, net->uuid);
3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788
    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;
3789
 cleanup:
3790
    virNetworkObjEndAPI(&network);
3791 3792 3793 3794
    testDriverUnlock(privconn);
    return ret;
}

3795 3796
static int testNetworkCreate(virNetworkPtr network)
{
3797 3798
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3799
    int ret = -1;
3800
    virObjectEventPtr event = NULL;
3801

3802
    testDriverLock(privconn);
3803
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3804
    testDriverUnlock(privconn);
3805 3806

    if (privnet == NULL) {
3807
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3808
        goto cleanup;
3809
    }
3810

D
Daniel P. Berrange 已提交
3811
    if (virNetworkObjIsActive(privnet)) {
3812 3813
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is already running"), network->name);
3814
        goto cleanup;
3815 3816
    }

3817
    privnet->active = 1;
3818
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3819 3820
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3821
    ret = 0;
3822

3823
 cleanup:
3824 3825
    if (event)
        testObjectEventQueue(privconn, event);
3826
    virNetworkObjEndAPI(&privnet);
3827
    return ret;
3828 3829
}

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

3837
    testDriverLock(privconn);
3838
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3839 3840

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

3845
    privnet->active = 0;
3846
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3847 3848
                                        VIR_NETWORK_EVENT_STOPPED,
                                        0);
3849
    if (!privnet->persistent)
3850
        virNetworkRemoveInactive(privconn->networks, privnet);
3851

3852 3853
    ret = 0;

3854
 cleanup:
3855 3856
    if (event)
        testObjectEventQueue(privconn, event);
3857
    virNetworkObjEndAPI(&privnet);
3858
    testDriverUnlock(privconn);
3859
    return ret;
3860 3861
}

3862
static char *testNetworkGetXMLDesc(virNetworkPtr network,
E
Eric Blake 已提交
3863
                                   unsigned int flags)
3864
{
3865 3866
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3867
    char *ret = NULL;
3868

E
Eric Blake 已提交
3869 3870
    virCheckFlags(0, NULL);

3871
    testDriverLock(privconn);
3872
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3873
    testDriverUnlock(privconn);
3874 3875

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

3880
    ret = virNetworkDefFormat(privnet->def, flags);
3881

3882
 cleanup:
3883
    virNetworkObjEndAPI(&privnet);
3884
    return ret;
3885 3886 3887
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3888
    testConnPtr privconn = network->conn->privateData;
3889
    char *bridge = NULL;
3890 3891
    virNetworkObjPtr privnet;

3892
    testDriverLock(privconn);
3893
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3894
    testDriverUnlock(privconn);
3895 3896

    if (privnet == NULL) {
3897
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3898
        goto cleanup;
3899 3900
    }

3901
    if (!(privnet->def->bridge)) {
3902 3903 3904
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("network '%s' does not have a bridge name."),
                       privnet->def->name);
3905 3906 3907
        goto cleanup;
    }

3908
    ignore_value(VIR_STRDUP(bridge, privnet->def->bridge));
3909

3910
 cleanup:
3911
    virNetworkObjEndAPI(&privnet);
3912 3913 3914 3915
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
3916 3917
                                   int *autostart)
{
3918 3919
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3920
    int ret = -1;
3921

3922
    testDriverLock(privconn);
3923
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3924
    testDriverUnlock(privconn);
3925 3926

    if (privnet == NULL) {
3927
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3928
        goto cleanup;
3929 3930
    }

3931
    *autostart = privnet->autostart;
3932 3933
    ret = 0;

3934
 cleanup:
3935
    virNetworkObjEndAPI(&privnet);
3936
    return ret;
3937 3938 3939
}

static int testNetworkSetAutostart(virNetworkPtr network,
3940 3941
                                   int autostart)
{
3942 3943
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3944
    int ret = -1;
3945

3946
    testDriverLock(privconn);
3947
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3948
    testDriverUnlock(privconn);
3949 3950

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

3955
    privnet->autostart = autostart ? 1 : 0;
3956 3957
    ret = 0;

3958
 cleanup:
3959
    virNetworkObjEndAPI(&privnet);
3960
    return ret;
3961
}
3962

C
Cole Robinson 已提交
3963

L
Laine Stump 已提交
3964 3965 3966 3967 3968
/*
 * Physical host interface routines
 */


3969
static int testConnectNumOfInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3970 3971
{
    testConnPtr privconn = conn->privateData;
3972 3973
    size_t i;
    int count = 0;
L
Laine Stump 已提交
3974 3975

    testDriverLock(privconn);
3976
    for (i = 0; (i < privconn->ifaces.count); i++) {
L
Laine Stump 已提交
3977
        virInterfaceObjLock(privconn->ifaces.objs[i]);
3978
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i]))
L
Laine Stump 已提交
3979 3980 3981 3982 3983 3984 3985
            count++;
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

3986
static int testConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
L
Laine Stump 已提交
3987 3988
{
    testConnPtr privconn = conn->privateData;
3989 3990
    int n = 0;
    size_t i;
L
Laine Stump 已提交
3991 3992 3993

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
3994
    for (i = 0; (i < privconn->ifaces.count) && (n < nnames); i++) {
L
Laine Stump 已提交
3995
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3996
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
3997
            if (VIR_STRDUP(names[n++], privconn->ifaces.objs[i]->def->name) < 0) {
L
Laine Stump 已提交
3998
                virInterfaceObjUnlock(privconn->ifaces.objs[i]);
3999
                goto error;
L
Laine Stump 已提交
4000 4001 4002 4003 4004 4005 4006 4007
            }
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);

    return n;

4008
 error:
4009
    for (n = 0; n < nnames; n++)
L
Laine Stump 已提交
4010 4011 4012 4013 4014
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

4015
static int testConnectNumOfDefinedInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
4016 4017
{
    testConnPtr privconn = conn->privateData;
4018 4019
    size_t i;
    int count = 0;
L
Laine Stump 已提交
4020 4021

    testDriverLock(privconn);
4022
    for (i = 0; i < privconn->ifaces.count; i++) {
L
Laine Stump 已提交
4023
        virInterfaceObjLock(privconn->ifaces.objs[i]);
4024
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i]))
L
Laine Stump 已提交
4025 4026 4027 4028 4029 4030 4031
            count++;
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

4032
static int testConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
L
Laine Stump 已提交
4033 4034
{
    testConnPtr privconn = conn->privateData;
4035 4036
    int n = 0;
    size_t i;
L
Laine Stump 已提交
4037 4038 4039

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
4040
    for (i = 0; (i < privconn->ifaces.count) && (n < nnames); i++) {
L
Laine Stump 已提交
4041
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
4042
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
4043
            if (VIR_STRDUP(names[n++], privconn->ifaces.objs[i]->def->name) < 0) {
L
Laine Stump 已提交
4044
                virInterfaceObjUnlock(privconn->ifaces.objs[i]);
4045
                goto error;
L
Laine Stump 已提交
4046 4047 4048 4049 4050 4051 4052 4053
            }
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);

    return n;

4054
 error:
4055
    for (n = 0; n < nnames; n++)
L
Laine Stump 已提交
4056 4057 4058 4059 4060
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

4061
static virInterfacePtr testInterfaceLookupByName(virConnectPtr conn,
L
Laine Stump 已提交
4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072
                                                 const char *name)
{
    testConnPtr privconn = conn->privateData;
    virInterfaceObjPtr iface;
    virInterfacePtr ret = NULL;

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

    if (iface == NULL) {
4073
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4074 4075 4076 4077 4078
        goto cleanup;
    }

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

4079
 cleanup:
L
Laine Stump 已提交
4080 4081 4082 4083 4084
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

4085
static virInterfacePtr testInterfaceLookupByMACString(virConnectPtr conn,
L
Laine Stump 已提交
4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097
                                                      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) {
4098
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4099 4100 4101 4102
        goto cleanup;
    }

    if (ifacect > 1) {
4103
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
4104 4105 4106 4107 4108
        goto cleanup;
    }

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

4109
 cleanup:
L
Laine Stump 已提交
4110 4111 4112 4113 4114
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

4115 4116 4117 4118 4119 4120 4121 4122 4123 4124
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) {
4125
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
4126 4127 4128 4129
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

4130
 cleanup:
4131 4132 4133 4134 4135
    if (obj)
        virInterfaceObjUnlock(obj);
    return ret;
}

4136
static int testInterfaceChangeBegin(virConnectPtr conn,
E
Eric Blake 已提交
4137
                                    unsigned int flags)
4138 4139 4140 4141
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
4142 4143
    virCheckFlags(0, -1);

4144 4145
    testDriverLock(privconn);
    if (privconn->transaction_running) {
4146
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
4147
                       _("there is another transaction running."));
4148 4149 4150 4151 4152 4153 4154 4155 4156 4157
        goto cleanup;
    }

    privconn->transaction_running = true;

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

    ret = 0;
4158
 cleanup:
4159 4160 4161 4162 4163
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceChangeCommit(virConnectPtr conn,
E
Eric Blake 已提交
4164
                                     unsigned int flags)
4165 4166 4167 4168
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
4169 4170
    virCheckFlags(0, -1);

4171 4172 4173
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
4174
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
4175 4176
                       _("no transaction running, "
                         "nothing to be committed."));
4177 4178 4179 4180 4181 4182 4183 4184
        goto cleanup;
    }

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

    ret = 0;

4185
 cleanup:
4186 4187 4188 4189 4190 4191
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
E
Eric Blake 已提交
4192
                                       unsigned int flags)
4193 4194 4195 4196
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
4197 4198
    virCheckFlags(0, -1);

4199 4200 4201
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
4202
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
4203 4204
                       _("no transaction running, "
                         "nothing to rollback."));
4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217
        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;

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

L
Laine Stump 已提交
4223
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
E
Eric Blake 已提交
4224
                                     unsigned int flags)
L
Laine Stump 已提交
4225 4226 4227 4228 4229
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

E
Eric Blake 已提交
4230 4231
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
4232 4233 4234 4235 4236 4237
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);
    testDriverUnlock(privconn);

    if (privinterface == NULL) {
4238
        virReportError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
4239 4240 4241
        goto cleanup;
    }

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

4244
 cleanup:
L
Laine Stump 已提交
4245 4246 4247 4248 4249 4250 4251
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    return ret;
}


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
E
Eric Blake 已提交
4252
                                              unsigned int flags)
L
Laine Stump 已提交
4253 4254 4255 4256 4257 4258
{
    testConnPtr privconn = conn->privateData;
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
4259 4260
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
4261
    testDriverLock(privconn);
4262
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
4263 4264
        goto cleanup;

4265
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
4266 4267 4268 4269 4270
        goto cleanup;
    def = NULL;

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

4271
 cleanup:
L
Laine Stump 已提交
4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289
    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) {
4290
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4291 4292 4293 4294 4295 4296 4297
        goto cleanup;
    }

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

4298
 cleanup:
L
Laine Stump 已提交
4299 4300 4301 4302 4303
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
E
Eric Blake 已提交
4304
                               unsigned int flags)
L
Laine Stump 已提交
4305 4306 4307 4308 4309
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
4310 4311
    virCheckFlags(0, -1);

L
Laine Stump 已提交
4312 4313 4314 4315 4316
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
4317
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4318 4319 4320 4321
        goto cleanup;
    }

    if (privinterface->active != 0) {
4322
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4323 4324 4325 4326 4327 4328
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

4329
 cleanup:
L
Laine Stump 已提交
4330 4331 4332 4333 4334 4335 4336
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceDestroy(virInterfacePtr iface,
E
Eric Blake 已提交
4337
                                unsigned int flags)
L
Laine Stump 已提交
4338 4339 4340 4341 4342
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
4343 4344
    virCheckFlags(0, -1);

L
Laine Stump 已提交
4345 4346 4347 4348 4349
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
4350
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4351 4352 4353 4354
        goto cleanup;
    }

    if (privinterface->active == 0) {
4355
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4356 4357 4358 4359 4360 4361
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

4362
 cleanup:
L
Laine Stump 已提交
4363 4364 4365 4366 4367 4368 4369 4370
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}



C
Cole Robinson 已提交
4371 4372 4373 4374
/*
 * Storage Driver routines
 */

4375

4376 4377
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool)
{
C
Cole Robinson 已提交
4378 4379 4380 4381 4382

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

4383
    return VIR_STRDUP(pool->configFile, "");
C
Cole Robinson 已提交
4384 4385
}

4386

C
Cole Robinson 已提交
4387 4388
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
4389 4390
                            const unsigned char *uuid)
{
4391
    testConnPtr privconn = conn->privateData;
4392 4393
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4394

4395
    testDriverLock(privconn);
4396
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
4397
    testDriverUnlock(privconn);
4398 4399

    if (pool == NULL) {
4400
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4401
        goto cleanup;
C
Cole Robinson 已提交
4402 4403
    }

4404 4405
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4406

4407
 cleanup:
4408 4409
    if (pool)
        virStoragePoolObjUnlock(pool);
4410
    return ret;
C
Cole Robinson 已提交
4411 4412 4413 4414
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
4415 4416
                            const char *name)
{
4417
    testConnPtr privconn = conn->privateData;
4418 4419
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4420

4421
    testDriverLock(privconn);
4422
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
4423
    testDriverUnlock(privconn);
4424 4425

    if (pool == NULL) {
4426
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4427
        goto cleanup;
C
Cole Robinson 已提交
4428 4429
    }

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

4433
 cleanup:
4434 4435
    if (pool)
        virStoragePoolObjUnlock(pool);
4436
    return ret;
C
Cole Robinson 已提交
4437 4438 4439
}

static virStoragePoolPtr
4440 4441
testStoragePoolLookupByVolume(virStorageVolPtr vol)
{
C
Cole Robinson 已提交
4442 4443 4444 4445
    return testStoragePoolLookupByName(vol->conn, vol->pool);
}

static int
4446 4447
testConnectNumOfStoragePools(virConnectPtr conn)
{
4448
    testConnPtr privconn = conn->privateData;
4449 4450
    int numActive = 0;
    size_t i;
C
Cole Robinson 已提交
4451

4452
    testDriverLock(privconn);
4453
    for (i = 0; i < privconn->pools.count; i++)
C
Cole Robinson 已提交
4454 4455
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
4456
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4457 4458 4459 4460 4461

    return numActive;
}

static int
4462 4463
testConnectListStoragePools(virConnectPtr conn,
                            char **const names,
4464 4465
                            int nnames)
{
4466
    testConnPtr privconn = conn->privateData;
4467 4468
    int n = 0;
    size_t i;
C
Cole Robinson 已提交
4469

4470
    testDriverLock(privconn);
C
Cole Robinson 已提交
4471
    memset(names, 0, sizeof(*names)*nnames);
4472
    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
4473
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4474
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
4475
            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
4476
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
4477
            goto error;
4478 4479 4480 4481
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4482 4483 4484

    return n;

4485
 error:
4486
    for (n = 0; n < nnames; n++)
C
Cole Robinson 已提交
4487
        VIR_FREE(names[n]);
4488
    testDriverUnlock(privconn);
4489
    return -1;
C
Cole Robinson 已提交
4490 4491 4492
}

static int
4493 4494
testConnectNumOfDefinedStoragePools(virConnectPtr conn)
{
4495
    testConnPtr privconn = conn->privateData;
4496 4497
    int numInactive = 0;
    size_t i;
C
Cole Robinson 已提交
4498

4499
    testDriverLock(privconn);
4500
    for (i = 0; i < privconn->pools.count; i++) {
4501
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4502 4503
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
4504 4505 4506
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4507 4508 4509 4510 4511

    return numInactive;
}

static int
4512 4513
testConnectListDefinedStoragePools(virConnectPtr conn,
                                   char **const names,
4514 4515
                                   int nnames)
{
4516
    testConnPtr privconn = conn->privateData;
4517 4518
    int n = 0;
    size_t i;
C
Cole Robinson 已提交
4519

4520
    testDriverLock(privconn);
C
Cole Robinson 已提交
4521
    memset(names, 0, sizeof(*names)*nnames);
4522
    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
4523
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4524
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
4525
            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
4526
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
4527
            goto error;
4528 4529 4530 4531
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4532 4533 4534

    return n;

4535
 error:
4536
    for (n = 0; n < nnames; n++)
C
Cole Robinson 已提交
4537
        VIR_FREE(names[n]);
4538
    testDriverUnlock(privconn);
4539
    return -1;
C
Cole Robinson 已提交
4540 4541
}

4542
static int
4543 4544 4545
testConnectListAllStoragePools(virConnectPtr conn,
                               virStoragePoolPtr **pools,
                               unsigned int flags)
4546 4547 4548 4549 4550 4551 4552
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

    testDriverLock(privconn);
4553 4554
    ret = virStoragePoolObjListExport(conn, privconn->pools, pools,
                                      NULL, flags);
4555 4556 4557 4558
    testDriverUnlock(privconn);

    return ret;
}
C
Cole Robinson 已提交
4559

4560 4561 4562 4563 4564 4565 4566 4567 4568 4569
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) {
4570
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4571 4572 4573 4574
        goto cleanup;
    }
    ret = virStoragePoolObjIsActive(obj);

4575
 cleanup:
4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590
    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) {
4591
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4592 4593 4594 4595
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

4596
 cleanup:
4597 4598 4599 4600 4601 4602 4603
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}



C
Cole Robinson 已提交
4604
static int
4605 4606
testStoragePoolCreate(virStoragePoolPtr pool,
                      unsigned int flags)
E
Eric Blake 已提交
4607
{
4608 4609
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4610
    int ret = -1;
4611

E
Eric Blake 已提交
4612 4613
    virCheckFlags(0, -1);

4614
    testDriverLock(privconn);
4615 4616
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4617
    testDriverUnlock(privconn);
4618 4619

    if (privpool == NULL) {
4620
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4621
        goto cleanup;
4622 4623
    }

4624
    if (virStoragePoolObjIsActive(privpool)) {
4625 4626
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4627 4628
        goto cleanup;
    }
C
Cole Robinson 已提交
4629 4630

    privpool->active = 1;
4631
    ret = 0;
C
Cole Robinson 已提交
4632

4633
 cleanup:
4634 4635
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4636
    return ret;
C
Cole Robinson 已提交
4637 4638 4639
}

static char *
4640 4641 4642 4643
testConnectFindStoragePoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type,
                                  const char *srcSpec,
                                  unsigned int flags)
C
Cole Robinson 已提交
4644
{
4645 4646 4647 4648
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4649 4650
    virCheckFlags(0, NULL);

4651 4652
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4653 4654
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
4655 4656 4657 4658
        goto cleanup;
    }

    if (srcSpec) {
4659
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4660 4661 4662 4663 4664 4665 4666
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
4667
        ignore_value(VIR_STRDUP(ret, defaultPoolSourcesLogicalXML));
4668 4669 4670
        break;

    case VIR_STORAGE_POOL_NETFS:
4671
        if (!source || !source->hosts[0].name) {
4672 4673
            virReportError(VIR_ERR_INVALID_ARG,
                           "%s", _("hostname must be specified for netfs sources"));
4674 4675 4676
            goto cleanup;
        }

4677 4678
        ignore_value(virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                                 source->hosts[0].name));
4679 4680 4681
        break;

    default:
4682 4683
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4684 4685
    }

4686
 cleanup:
4687 4688
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4689 4690 4691 4692
}


static virStoragePoolPtr
4693 4694 4695
testStoragePoolCreateXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4696
{
4697
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4698
    virStoragePoolDefPtr def;
4699
    virStoragePoolObjPtr pool = NULL;
4700
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4701

E
Eric Blake 已提交
4702 4703
    virCheckFlags(0, NULL);

4704
    testDriverLock(privconn);
4705
    if (!(def = virStoragePoolDefParseString(xml)))
4706
        goto cleanup;
C
Cole Robinson 已提交
4707

4708 4709 4710 4711
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4712 4713
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("storage pool already exists"));
4714
        goto cleanup;
C
Cole Robinson 已提交
4715 4716
    }

4717
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4718
        goto cleanup;
4719
    def = NULL;
C
Cole Robinson 已提交
4720

4721
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4722
        virStoragePoolObjRemove(&privconn->pools, pool);
4723 4724
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4725 4726 4727
    }
    pool->active = 1;

4728 4729
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4730

4731
 cleanup:
4732
    virStoragePoolDefFree(def);
4733 4734 4735
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4736
    return ret;
C
Cole Robinson 已提交
4737 4738 4739
}

static virStoragePoolPtr
4740 4741 4742
testStoragePoolDefineXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4743
{
4744
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4745
    virStoragePoolDefPtr def;
4746
    virStoragePoolObjPtr pool = NULL;
4747
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4748

E
Eric Blake 已提交
4749 4750
    virCheckFlags(0, NULL);

4751
    testDriverLock(privconn);
4752
    if (!(def = virStoragePoolDefParseString(xml)))
4753
        goto cleanup;
C
Cole Robinson 已提交
4754 4755 4756 4757 4758

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

4759
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4760 4761
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4762

4763
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4764
        virStoragePoolObjRemove(&privconn->pools, pool);
4765 4766
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4767 4768
    }

4769 4770
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4771

4772
 cleanup:
4773
    virStoragePoolDefFree(def);
4774 4775 4776
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4777
    return ret;
C
Cole Robinson 已提交
4778 4779 4780
}

static int
4781 4782
testStoragePoolUndefine(virStoragePoolPtr pool)
{
4783 4784
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4785
    int ret = -1;
4786

4787
    testDriverLock(privconn);
4788 4789 4790 4791
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4792
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4793
        goto cleanup;
4794 4795
    }

4796
    if (virStoragePoolObjIsActive(privpool)) {
4797 4798
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4799 4800
        goto cleanup;
    }
C
Cole Robinson 已提交
4801 4802

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

4805
 cleanup:
4806 4807 4808
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4809
    return ret;
C
Cole Robinson 已提交
4810 4811 4812
}

static int
4813
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4814 4815
                     unsigned int flags)
{
4816 4817
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4818
    int ret = -1;
4819

E
Eric Blake 已提交
4820 4821
    virCheckFlags(0, -1);

4822
    testDriverLock(privconn);
4823 4824
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4825
    testDriverUnlock(privconn);
4826 4827

    if (privpool == NULL) {
4828
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4829
        goto cleanup;
4830 4831
    }

4832
    if (virStoragePoolObjIsActive(privpool)) {
4833 4834
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4835 4836
        goto cleanup;
    }
4837
    ret = 0;
C
Cole Robinson 已提交
4838

4839
 cleanup:
4840 4841
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4842
    return ret;
C
Cole Robinson 已提交
4843 4844 4845 4846
}


static int
4847 4848
testStoragePoolDestroy(virStoragePoolPtr pool)
{
4849 4850
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4851
    int ret = -1;
4852

4853
    testDriverLock(privconn);
4854 4855 4856 4857
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4858
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4859
        goto cleanup;
4860 4861 4862
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4863 4864
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4865
        goto cleanup;
4866
    }
C
Cole Robinson 已提交
4867 4868 4869

    privpool->active = 0;

4870
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4871
        virStoragePoolObjRemove(&privconn->pools, privpool);
4872 4873
        privpool = NULL;
    }
4874
    ret = 0;
C
Cole Robinson 已提交
4875

4876
 cleanup:
4877 4878 4879
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4880
    return ret;
C
Cole Robinson 已提交
4881 4882 4883 4884
}


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

E
Eric Blake 已提交
4892 4893
    virCheckFlags(0, -1);

4894
    testDriverLock(privconn);
4895 4896
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4897
    testDriverUnlock(privconn);
4898 4899

    if (privpool == NULL) {
4900
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4901 4902 4903 4904
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
4905 4906
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4907
        goto cleanup;
4908 4909
    }

4910
    ret = 0;
C
Cole Robinson 已提交
4911

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


static int
4920
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4921 4922
                       unsigned int flags)
{
4923 4924
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4925
    int ret = -1;
4926

E
Eric Blake 已提交
4927 4928
    virCheckFlags(0, -1);

4929
    testDriverLock(privconn);
4930 4931
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4932
    testDriverUnlock(privconn);
4933 4934

    if (privpool == NULL) {
4935
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4936
        goto cleanup;
4937 4938 4939
    }

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

4946
 cleanup:
4947 4948
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4949
    return ret;
C
Cole Robinson 已提交
4950 4951 4952 4953
}


static int
4954
testStoragePoolGetInfo(virStoragePoolPtr pool,
4955 4956
                       virStoragePoolInfoPtr info)
{
4957 4958
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4959
    int ret = -1;
4960

4961
    testDriverLock(privconn);
4962 4963
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4964
    testDriverUnlock(privconn);
4965 4966

    if (privpool == NULL) {
4967
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4968
        goto cleanup;
4969
    }
C
Cole Robinson 已提交
4970 4971 4972 4973 4974 4975 4976 4977 4978

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

4981
 cleanup:
4982 4983
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4984
    return ret;
C
Cole Robinson 已提交
4985 4986 4987
}

static char *
4988
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4989 4990
                          unsigned int flags)
{
4991 4992
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4993
    char *ret = NULL;
4994

E
Eric Blake 已提交
4995 4996
    virCheckFlags(0, NULL);

4997
    testDriverLock(privconn);
4998 4999
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5000
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5001

5002
    if (privpool == NULL) {
5003
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5004
        goto cleanup;
5005 5006
    }

5007
    ret = virStoragePoolDefFormat(privpool->def);
5008

5009
 cleanup:
5010 5011
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5012
    return ret;
C
Cole Robinson 已提交
5013 5014 5015
}

static int
5016
testStoragePoolGetAutostart(virStoragePoolPtr pool,
5017 5018
                            int *autostart)
{
5019 5020
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5021
    int ret = -1;
5022

5023
    testDriverLock(privconn);
5024 5025
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5026
    testDriverUnlock(privconn);
5027 5028

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

    if (!privpool->configFile) {
        *autostart = 0;
    } else {
        *autostart = privpool->autostart;
    }
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 int
5047
testStoragePoolSetAutostart(virStoragePoolPtr pool,
5048 5049
                            int autostart)
{
5050 5051
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5052
    int ret = -1;
5053

5054
    testDriverLock(privconn);
5055 5056
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5057
    testDriverUnlock(privconn);
5058 5059

    if (privpool == NULL) {
5060
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5061
        goto cleanup;
5062
    }
C
Cole Robinson 已提交
5063 5064

    if (!privpool->configFile) {
5065 5066
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
5067
        goto cleanup;
C
Cole Robinson 已提交
5068 5069 5070 5071
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
5072 5073
    ret = 0;

5074
 cleanup:
5075 5076
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5077
    return ret;
C
Cole Robinson 已提交
5078 5079 5080 5081
}


static int
5082 5083
testStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
5084 5085
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5086
    int ret = -1;
5087

5088
    testDriverLock(privconn);
5089 5090
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5091
    testDriverUnlock(privconn);
5092 5093

    if (privpool == NULL) {
5094
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5095
        goto cleanup;
5096 5097 5098
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5099 5100
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5101
        goto cleanup;
5102
    }
C
Cole Robinson 已提交
5103

5104 5105
    ret = privpool->volumes.count;

5106
 cleanup:
5107 5108
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5109
    return ret;
C
Cole Robinson 已提交
5110 5111 5112
}

static int
5113
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
5114
                           char **const names,
5115 5116
                           int maxnames)
{
5117 5118
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5119 5120
    size_t i = 0;
    int n = 0;
C
Cole Robinson 已提交
5121

5122
    memset(names, 0, maxnames * sizeof(*names));
5123 5124

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

    if (privpool == NULL) {
5130
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5131
        goto cleanup;
5132 5133 5134 5135
    }


    if (!virStoragePoolObjIsActive(privpool)) {
5136 5137
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5138
        goto cleanup;
5139 5140
    }

5141
    for (i = 0; i < privpool->volumes.count && n < maxnames; i++) {
5142
        if (VIR_STRDUP(names[n++], privpool->volumes.objs[i]->name) < 0)
C
Cole Robinson 已提交
5143 5144 5145
            goto cleanup;
    }

5146
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5147 5148 5149
    return n;

 cleanup:
5150
    for (n = 0; n < maxnames; n++)
C
Cole Robinson 已提交
5151 5152
        VIR_FREE(names[i]);

5153
    memset(names, 0, maxnames * sizeof(*names));
5154 5155
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5156 5157 5158
    return -1;
}

5159 5160 5161
static int
testStoragePoolListAllVolumes(virStoragePoolPtr obj,
                              virStorageVolPtr **vols,
5162 5163
                              unsigned int flags)
{
5164 5165
    testConnPtr privconn = obj->conn->privateData;
    virStoragePoolObjPtr pool;
5166
    size_t i;
5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195
    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;
    }

5196
    if (VIR_ALLOC_N(tmp_vols, pool->volumes.count + 1) < 0)
5197 5198
         goto cleanup;

5199
    for (i = 0; i < pool->volumes.count; i++) {
5200 5201
        if (!(vol = virGetStorageVol(obj->conn, pool->def->name,
                                     pool->volumes.objs[i]->name,
5202 5203
                                     pool->volumes.objs[i]->key,
                                     NULL, NULL)))
5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217
            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]);
        }
5218
        VIR_FREE(tmp_vols);
5219 5220 5221 5222 5223 5224 5225
    }

    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}
C
Cole Robinson 已提交
5226 5227

static virStorageVolPtr
5228
testStorageVolLookupByName(virStoragePoolPtr pool,
5229 5230
                           const char *name ATTRIBUTE_UNUSED)
{
5231 5232 5233
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5234
    virStorageVolPtr ret = NULL;
5235

5236
    testDriverLock(privconn);
5237 5238
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5239
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5240

5241
    if (privpool == NULL) {
5242
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5243
        goto cleanup;
5244 5245 5246 5247
    }


    if (!virStoragePoolObjIsActive(privpool)) {
5248 5249
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5250
        goto cleanup;
5251 5252 5253 5254 5255
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
5256 5257
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"), name);
5258
        goto cleanup;
C
Cole Robinson 已提交
5259 5260
    }

5261
    ret = virGetStorageVol(pool->conn, privpool->def->name,
5262 5263
                           privvol->name, privvol->key,
                           NULL, NULL);
5264

5265
 cleanup:
5266 5267
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5268
    return ret;
C
Cole Robinson 已提交
5269 5270 5271 5272
}


static virStorageVolPtr
5273
testStorageVolLookupByKey(virConnectPtr conn,
5274 5275
                          const char *key)
{
5276
    testConnPtr privconn = conn->privateData;
5277
    size_t i;
5278
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
5279

5280
    testDriverLock(privconn);
5281
    for (i = 0; i < privconn->pools.count; i++) {
5282
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5283
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
5284
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
5285 5286
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

5287 5288 5289 5290
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
5291 5292
                                       privvol->key,
                                       NULL, NULL);
5293
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
5294 5295
                break;
            }
C
Cole Robinson 已提交
5296
        }
5297
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5298
    }
5299
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5300

5301
    if (!ret)
5302 5303
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
5304 5305

    return ret;
C
Cole Robinson 已提交
5306 5307 5308
}

static virStorageVolPtr
5309
testStorageVolLookupByPath(virConnectPtr conn,
5310 5311
                           const char *path)
{
5312
    testConnPtr privconn = conn->privateData;
5313
    size_t i;
5314
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
5315

5316
    testDriverLock(privconn);
5317
    for (i = 0; i < privconn->pools.count; i++) {
5318
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5319
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
5320
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
5321 5322
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

5323 5324 5325 5326
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
5327 5328
                                       privvol->key,
                                       NULL, NULL);
5329
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
5330 5331
                break;
            }
C
Cole Robinson 已提交
5332
        }
5333
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5334
    }
5335
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5336

5337
    if (!ret)
5338 5339
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
5340 5341

    return ret;
C
Cole Robinson 已提交
5342 5343 5344
}

static virStorageVolPtr
5345 5346 5347
testStorageVolCreateXML(virStoragePoolPtr pool,
                        const char *xmldesc,
                        unsigned int flags)
E
Eric Blake 已提交
5348
{
5349 5350
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5351 5352
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
5353

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

5356
    testDriverLock(privconn);
5357 5358
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5359
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5360

5361
    if (privpool == NULL) {
5362
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5363
        goto cleanup;
5364 5365 5366
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5367 5368
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5369
        goto cleanup;
5370
    }
C
Cole Robinson 已提交
5371

5372
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5373
    if (privvol == NULL)
5374
        goto cleanup;
5375 5376

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5377 5378
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5379
        goto cleanup;
C
Cole Robinson 已提交
5380 5381 5382
    }

    /* Make sure enough space */
5383
    if ((privpool->def->allocation + privvol->target.allocation) >
C
Cole Robinson 已提交
5384
         privpool->def->capacity) {
5385 5386 5387
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5388
        goto cleanup;
C
Cole Robinson 已提交
5389 5390
    }

5391 5392
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5393
                    privvol->name) == -1)
5394
        goto cleanup;
C
Cole Robinson 已提交
5395

5396 5397 5398
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
5399
        goto cleanup;
C
Cole Robinson 已提交
5400

5401
    privpool->def->allocation += privvol->target.allocation;
C
Cole Robinson 已提交
5402 5403 5404
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5405
    ret = virGetStorageVol(pool->conn, privpool->def->name,
5406 5407
                           privvol->name, privvol->key,
                           NULL, NULL);
5408
    privvol = NULL;
5409

5410
 cleanup:
5411
    virStorageVolDefFree(privvol);
5412 5413
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5414
    return ret;
C
Cole Robinson 已提交
5415 5416
}

5417
static virStorageVolPtr
5418 5419 5420 5421
testStorageVolCreateXMLFrom(virStoragePoolPtr pool,
                            const char *xmldesc,
                            virStorageVolPtr clonevol,
                            unsigned int flags)
E
Eric Blake 已提交
5422
{
5423 5424 5425 5426 5427
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
5428 5429
    virCheckFlags(0, NULL);

5430 5431 5432 5433 5434 5435
    testDriverLock(privconn);
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
    testDriverUnlock(privconn);

    if (privpool == NULL) {
5436
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5437 5438 5439 5440
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5441 5442
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5443 5444 5445
        goto cleanup;
    }

5446
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5447 5448 5449 5450
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5451 5452
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5453 5454 5455 5456 5457
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
5458 5459 5460
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
5461 5462 5463 5464
        goto cleanup;
    }

    /* Make sure enough space */
5465
    if ((privpool->def->allocation + privvol->target.allocation) >
5466
         privpool->def->capacity) {
5467 5468 5469
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5470 5471 5472 5473 5474
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5475 5476
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5477
                    privvol->name) == -1)
5478 5479
        goto cleanup;

5480 5481 5482
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
5483 5484
        goto cleanup;

5485
    privpool->def->allocation += privvol->target.allocation;
5486 5487 5488 5489
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    ret = virGetStorageVol(pool->conn, privpool->def->name,
5490 5491
                           privvol->name, privvol->key,
                           NULL, NULL);
5492 5493
    privvol = NULL;

5494
 cleanup:
5495 5496 5497 5498 5499 5500
    virStorageVolDefFree(privvol);
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    return ret;
}

C
Cole Robinson 已提交
5501
static int
5502 5503
testStorageVolDelete(virStorageVolPtr vol,
                     unsigned int flags)
E
Eric Blake 已提交
5504
{
5505 5506 5507
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5508
    size_t i;
5509
    int ret = -1;
C
Cole Robinson 已提交
5510

E
Eric Blake 已提交
5511 5512
    virCheckFlags(0, -1);

5513
    testDriverLock(privconn);
5514 5515
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5516
    testDriverUnlock(privconn);
5517 5518

    if (privpool == NULL) {
5519
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5520
        goto cleanup;
5521 5522 5523 5524 5525 5526
    }


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

    if (privvol == NULL) {
5527 5528 5529
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5530
        goto cleanup;
5531 5532 5533
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5534 5535
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5536
        goto cleanup;
5537 5538 5539
    }


5540
    privpool->def->allocation -= privvol->target.allocation;
C
Cole Robinson 已提交
5541 5542 5543
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5544
    for (i = 0; i < privpool->volumes.count; i++) {
C
Cole Robinson 已提交
5545 5546 5547
        if (privpool->volumes.objs[i] == privvol) {
            virStorageVolDefFree(privvol);

5548
            VIR_DELETE_ELEMENT(privpool->volumes.objs, i, privpool->volumes.count);
C
Cole Robinson 已提交
5549 5550 5551
            break;
        }
    }
5552
    ret = 0;
C
Cole Robinson 已提交
5553

5554
 cleanup:
5555 5556
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5557
    return ret;
C
Cole Robinson 已提交
5558 5559 5560
}


5561 5562
static int testStorageVolumeTypeForPool(int pooltype)
{
C
Cole Robinson 已提交
5563

5564
    switch (pooltype) {
C
Cole Robinson 已提交
5565 5566 5567 5568 5569 5570 5571 5572 5573 5574
        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
5575
testStorageVolGetInfo(virStorageVolPtr vol,
5576 5577
                      virStorageVolInfoPtr info)
{
5578 5579 5580
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5581
    int ret = -1;
5582

5583
    testDriverLock(privconn);
5584 5585
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5586
    testDriverUnlock(privconn);
5587 5588

    if (privpool == NULL) {
5589
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5590
        goto cleanup;
5591 5592 5593 5594 5595
    }

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

    if (privvol == NULL) {
5596 5597 5598
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5599
        goto cleanup;
5600 5601 5602
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5603 5604
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5605
        goto cleanup;
5606
    }
C
Cole Robinson 已提交
5607 5608 5609

    memset(info, 0, sizeof(*info));
    info->type = testStorageVolumeTypeForPool(privpool->def->type);
5610 5611
    info->capacity = privvol->target.capacity;
    info->allocation = privvol->target.allocation;
5612
    ret = 0;
C
Cole Robinson 已提交
5613

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

static char *
5621 5622
testStorageVolGetXMLDesc(virStorageVolPtr vol,
                         unsigned int flags)
E
Eric Blake 已提交
5623
{
5624 5625 5626
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5627
    char *ret = NULL;
5628

E
Eric Blake 已提交
5629 5630
    virCheckFlags(0, NULL);

5631
    testDriverLock(privconn);
5632 5633
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5634
    testDriverUnlock(privconn);
5635 5636

    if (privpool == NULL) {
5637
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5638
        goto cleanup;
5639 5640 5641 5642 5643
    }

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

    if (privvol == NULL) {
5644 5645 5646
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5647
        goto cleanup;
5648
    }
C
Cole Robinson 已提交
5649

5650
    if (!virStoragePoolObjIsActive(privpool)) {
5651 5652
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5653
        goto cleanup;
5654 5655
    }

5656
    ret = virStorageVolDefFormat(privpool->def, privvol);
5657

5658
 cleanup:
5659 5660
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5661
    return ret;
C
Cole Robinson 已提交
5662 5663 5664
}

static char *
5665 5666
testStorageVolGetPath(virStorageVolPtr vol)
{
5667 5668 5669
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5670
    char *ret = NULL;
5671

5672
    testDriverLock(privconn);
5673 5674
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5675
    testDriverUnlock(privconn);
5676 5677

    if (privpool == NULL) {
5678
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5679
        goto cleanup;
5680 5681 5682 5683 5684
    }

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

    if (privvol == NULL) {
5685 5686 5687
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5688
        goto cleanup;
5689 5690 5691
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5692 5693
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5694
        goto cleanup;
5695 5696
    }

5697
    ignore_value(VIR_STRDUP(ret, privvol->target.path));
5698

5699
 cleanup:
5700 5701
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5702 5703 5704
    return ret;
}

5705

5706
/* Node device implementations */
5707

5708 5709 5710
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5711
                     unsigned int flags)
5712 5713 5714
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
5715
    size_t i;
5716

E
Eric Blake 已提交
5717 5718
    virCheckFlags(0, -1);

5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733
    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 已提交
5734
                    unsigned int flags)
5735 5736 5737
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
5738
    size_t i;
5739

E
Eric Blake 已提交
5740 5741
    virCheckFlags(0, -1);

5742 5743 5744 5745 5746
    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)) {
5747
            if (VIR_STRDUP(names[ndevs++], driver->devs.objs[i]->def->name) < 0) {
5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777
                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) {
5778
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5779 5780 5781 5782 5783
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

5784
 cleanup:
5785 5786 5787 5788 5789 5790
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
5791
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5792
                         unsigned int flags)
5793 5794 5795 5796 5797
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5798 5799
    virCheckFlags(0, NULL);

5800 5801 5802 5803 5804
    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
5805 5806 5807
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5808 5809 5810
        goto cleanup;
    }

5811
    ret = virNodeDeviceDefFormat(obj->def);
5812

5813
 cleanup:
5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830
    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) {
5831 5832 5833
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5834 5835 5836 5837
        goto cleanup;
    }

    if (obj->def->parent) {
5838
        ignore_value(VIR_STRDUP(ret, obj->def->parent));
5839
    } else {
5840 5841
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5842 5843
    }

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

5850

5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864
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) {
5865 5866 5867
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5868 5869 5870 5871 5872 5873 5874
        goto cleanup;
    }

    for (caps = obj->def->caps; caps; caps = caps->next)
        ++ncaps;
    ret = ncaps;

5875
 cleanup:
5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895
    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) {
5896 5897 5898
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5899 5900 5901 5902
        goto cleanup;
    }

    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
5903
        if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->type)) < 0)
5904 5905 5906 5907
            goto cleanup;
    }
    ret = ncaps;

5908
 cleanup:
5909 5910 5911 5912 5913 5914 5915 5916 5917 5918
    if (obj)
        virNodeDeviceObjUnlock(obj);
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
}

5919 5920 5921
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
E
Eric Blake 已提交
5922
                        unsigned int flags)
5923 5924 5925 5926 5927 5928 5929 5930 5931
{
    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 已提交
5932 5933
    virCheckFlags(0, NULL);

5934 5935
    testDriverLock(driver);

5936
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, NULL);
5937
    if (def == NULL)
5938 5939 5940
        goto cleanup;

    /* We run these next two simply for validation */
5941
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1)
5942 5943
        goto cleanup;

5944
    if (virNodeDeviceGetParentHost(&driver->devs,
5945 5946 5947 5948 5949 5950 5951 5952 5953
                                   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);
5954
    if (VIR_STRDUP(def->name, wwpn) < 0)
5955 5956
        goto cleanup;

J
John Ferlan 已提交
5957 5958
    /* Fill in a random 'host' and 'unique_id' value,
     * since this would also come from the backend */
5959 5960 5961 5962 5963
    caps = def->caps;
    while (caps) {
        if (caps->type != VIR_NODE_DEV_CAP_SCSI_HOST)
            continue;

5964
        caps->data.scsi_host.host = virRandomBits(10);
J
John Ferlan 已提交
5965
        caps->data.scsi_host.unique_id = 2;
5966 5967 5968 5969
        caps = caps->next;
    }


5970
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def)))
5971 5972 5973 5974 5975
        goto cleanup;
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
5976
 cleanup:
5977
    testDriverUnlock(driver);
5978
    virNodeDeviceDefFree(def);
5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997
    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) {
5998
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5999 6000 6001
        goto out;
    }

6002
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1)
6003 6004
        goto out;

6005
    if (VIR_STRDUP(parent_name, obj->def->parent) < 0)
6006 6007 6008 6009 6010 6011 6012 6013 6014
        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 */
6015
    if (virNodeDeviceGetParentHost(&driver->devs,
6016 6017 6018 6019 6020 6021 6022 6023 6024 6025
                                   dev->name,
                                   parent_name,
                                   &parent_host) == -1) {
        obj = NULL;
        goto out;
    }

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

6026
 out:
6027 6028 6029 6030 6031 6032 6033 6034
    if (obj)
        virNodeDeviceObjUnlock(obj);
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

6035 6036

/* Domain event implementations */
6037
static int
6038 6039 6040 6041
testConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
6042 6043
{
    testConnPtr driver = conn->privateData;
6044
    int ret = 0;
6045 6046

    testDriverLock(driver);
6047
    if (virDomainEventStateRegister(conn, driver->eventState,
6048 6049
                                    callback, opaque, freecb) < 0)
        ret = -1;
6050 6051 6052 6053 6054
    testDriverUnlock(driver);

    return ret;
}

6055

6056
static int
6057 6058
testConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
6059 6060
{
    testConnPtr driver = conn->privateData;
6061
    int ret = 0;
6062 6063

    testDriverLock(driver);
6064
    if (virDomainEventStateDeregister(conn, driver->eventState,
6065 6066
                                      callback) < 0)
        ret = -1;
6067 6068 6069 6070 6071
    testDriverUnlock(driver);

    return ret;
}

6072 6073

static int
6074 6075 6076 6077 6078 6079
testConnectDomainEventRegisterAny(virConnectPtr conn,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback callback,
                                  void *opaque,
                                  virFreeCallback freecb)
6080 6081 6082 6083 6084
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
6085
    if (virDomainEventStateRegisterID(conn, driver->eventState,
6086 6087
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
6088
        ret = -1;
6089 6090 6091 6092 6093 6094
    testDriverUnlock(driver);

    return ret;
}

static int
6095 6096
testConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
6097 6098
{
    testConnPtr driver = conn->privateData;
6099
    int ret = 0;
6100 6101

    testDriverLock(driver);
6102
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
6103 6104
                                        callbackID) < 0)
        ret = -1;
6105 6106 6107 6108 6109 6110
    testDriverUnlock(driver);

    return ret;
}


6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122
static int
testConnectNetworkEventRegisterAny(virConnectPtr conn,
                                   virNetworkPtr net,
                                   int eventID,
                                   virConnectNetworkEventGenericCallback callback,
                                   void *opaque,
                                   virFreeCallback freecb)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
6123
    if (virNetworkEventStateRegisterID(conn, driver->eventState,
6124
                                       net, eventID, callback,
6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136
                                       opaque, freecb, &ret) < 0)
        ret = -1;
    testDriverUnlock(driver);

    return ret;
}

static int
testConnectNetworkEventDeregisterAny(virConnectPtr conn,
                                     int callbackID)
{
    testConnPtr driver = conn->privateData;
6137
    int ret = 0;
6138 6139

    testDriverLock(driver);
6140
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
6141 6142
                                        callbackID) < 0)
        ret = -1;
6143 6144 6145 6146 6147 6148
    testDriverUnlock(driver);

    return ret;
}


6149
/* driver must be locked before calling */
6150
static void testObjectEventQueue(testConnPtr driver,
6151
                                 virObjectEventPtr event)
6152
{
6153
    virObjectEventStateQueue(driver->eventState, event);
6154 6155
}

6156

6157 6158 6159
static int testConnectListAllDomains(virConnectPtr conn,
                                     virDomainPtr **domains,
                                     unsigned int flags)
6160 6161 6162 6163
{
    testConnPtr privconn = conn->privateData;
    int ret;

O
Osier Yang 已提交
6164
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
6165 6166

    testDriverLock(privconn);
6167 6168
    ret = virDomainObjListExport(privconn->domains, conn, domains,
                                 NULL, flags);
6169 6170 6171 6172 6173
    testDriverUnlock(privconn);

    return ret;
}

6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186
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) {
6187
        if (VIR_ALLOC_N(*cpumap, 1) < 0)
6188 6189 6190 6191 6192 6193 6194 6195 6196
            goto cleanup;
        *cpumap[0] = 0x15;
    }

    if (online)
        *online = 3;

    ret = 8;

6197
 cleanup:
6198 6199 6200 6201
    testDriverUnlock(privconn);
    return ret;
}

6202 6203 6204 6205 6206 6207 6208 6209 6210 6211
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

6212
    if (VIR_STRDUP(ret, "image/png") < 0)
6213 6214
        return NULL;

6215
    if (virFDStreamOpenFile(st, PKGDATADIR "/libvirtLogo.png", 0, 0, O_RDONLY) < 0)
6216 6217 6218 6219 6220
        VIR_FREE(ret);

    return ret;
}

6221 6222 6223 6224 6225 6226 6227 6228 6229
static int
testConnectGetCPUModelNames(virConnectPtr conn ATTRIBUTE_UNUSED,
                            const char *arch,
                            char ***models,
                            unsigned int flags)
{
    virCheckFlags(0, -1);
    return cpuGetModels(arch, models);
}
6230

C
Cole Robinson 已提交
6231 6232 6233 6234 6235
static int
testDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr vm = NULL;
6236
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264
    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);
6265
    event = virDomainEventLifecycleNewFromObj(vm,
C
Cole Robinson 已提交
6266 6267 6268 6269 6270
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
    vm->hasManagedSave = true;

    ret = 0;
6271
 cleanup:
C
Cole Robinson 已提交
6272 6273 6274 6275
    if (vm)
        virObjectUnlock(vm);
    if (event) {
        testDriverLock(privconn);
6276
        testObjectEventQueue(privconn, event);
C
Cole Robinson 已提交
6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301
        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;
6302
 cleanup:
C
Cole Robinson 已提交
6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327
    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;
6328
 cleanup:
C
Cole Robinson 已提交
6329 6330 6331 6332 6333 6334 6335
    if (vm)
        virObjectUnlock(vm);
    testDriverUnlock(privconn);
    return ret;
}


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 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379
/*
 * 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);

6380
 cleanup:
6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403
    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);

6404
 cleanup:
6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425
    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);

6426
 cleanup:
6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453
    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);

6454
 cleanup:
6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478
    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);

6479
 cleanup:
6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505
    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);

6506
 cleanup:
6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530
    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);

6531
 cleanup:
6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550
    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);

6551
 cleanup:
6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581
    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);

6582
 cleanup:
6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607
    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);

6608
 cleanup:
6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632
    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);

6633 6634 6635
    xml = virDomainSnapshotDefFormat(uuidstr, snap->def,
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);
6636

6637
 cleanup:
6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657
    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));

6658
 cleanup:
6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676
    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 已提交
6677
    if (!testSnapObjFromSnapshot(vm, snapshot))
6678 6679 6680 6681
        goto cleanup;

    ret = 1;

6682
 cleanup:
6683 6684 6685 6686 6687
    if (vm)
        virObjectUnlock(vm);
    return ret;
}

6688 6689 6690 6691 6692 6693
static int
testDomainSnapshotAlignDisks(virDomainObjPtr vm,
                             virDomainSnapshotDefPtr def,
                             unsigned int flags)
{
    int align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
E
Eric Blake 已提交
6694
    bool align_match = true;
6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727

    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;
6728
    virObjectEventPtr event = NULL;
6729
    char *xml = NULL;
6730 6731
    bool update_current = true;
    bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE;
6732 6733 6734 6735 6736 6737 6738 6739
    unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;

    /*
     * DISK_ONLY: Not implemented yet
     * REUSE_EXT: Not implemented yet
     *
     * NO_METADATA: Explicitly not implemented
     *
6740
     * REDEFINE + CURRENT: Implemented
6741 6742 6743 6744 6745 6746
     * HALT: Implemented
     * QUIESCE: Nothing to do
     * ATOMIC: Nothing to do
     * LIVE: Nothing to do
     */
    virCheckFlags(
6747 6748
        VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
        VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT |
6749 6750 6751 6752 6753
        VIR_DOMAIN_SNAPSHOT_CREATE_HALT |
        VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE |
        VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC |
        VIR_DOMAIN_SNAPSHOT_CREATE_LIVE, NULL);

6754 6755 6756 6757 6758
    if ((redefine && !(flags & VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT)))
        update_current = false;
    if (redefine)
        parse_flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;

6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774
    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;

6775
    if (redefine) {
C
Cole Robinson 已提交
6776 6777
        if (virDomainSnapshotRedefinePrep(domain, vm, &def, &snap,
                                          &update_current, flags) < 0)
6778 6779 6780 6781 6782 6783 6784
            goto cleanup;
    } else {
        if (!(def->dom = virDomainDefCopy(vm->def,
                                          privconn->caps,
                                          privconn->xmlopt,
                                          true)))
            goto cleanup;
6785

6786
        if (testDomainSnapshotAlignDisks(vm, def, flags) < 0)
6787 6788 6789
            goto cleanup;
    }

6790 6791 6792 6793
    if (!snap) {
        if (!(snap = virDomainSnapshotAssignDef(vm->snapshots, def)))
            goto cleanup;
        def = NULL;
6794 6795
    }

6796 6797 6798 6799 6800 6801 6802 6803 6804 6805
    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);
6806
            event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
6807 6808 6809
                                    VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
        }
    }
6810 6811

    snapshot = virGetDomainSnapshot(domain, snap->def->name);
6812
 cleanup:
6813 6814 6815 6816
    VIR_FREE(xml);
    if (vm) {
        if (snapshot) {
            virDomainSnapshotObjPtr other;
6817 6818
            if (update_current)
                vm->current_snapshot = snap;
6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829
            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);
6830
        testObjectEventQueue(privconn, event);
6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874
        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;

6875
    if (rep->err < 0)
6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 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
        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) {
6918
            if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY)
6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 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
                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;
6962
 cleanup:
6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974
    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;
6975 6976
    virObjectEventPtr event = NULL;
    virObjectEventPtr event2 = NULL;
6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069
    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);
7070
                event = virDomainEventLifecycleNewFromObj(vm,
7071 7072 7073
                            VIR_DOMAIN_EVENT_STOPPED,
                            VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
                if (event)
7074
                    testObjectEventQueue(privconn, event);
7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085
                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. */
7086
                event = virDomainEventLifecycleNewFromObj(vm,
7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099
                                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;
7100
            event = virDomainEventLifecycleNewFromObj(vm,
7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113
                                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 */
7114
                event2 = virDomainEventLifecycleNewFromObj(vm,
7115 7116 7117 7118 7119
                                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 已提交
7120
            virObjectUnref(event);
7121 7122 7123 7124
            event = NULL;

            if (was_stopped) {
                /* Transition 2 */
7125
                event = virDomainEventLifecycleNewFromObj(vm,
7126 7127 7128 7129
                                VIR_DOMAIN_EVENT_STARTED,
                                VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            } else if (was_running) {
                /* Transition 8 */
7130
                event = virDomainEventLifecycleNewFromObj(vm,
7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142
                                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);
7143
            event = virDomainEventLifecycleNewFromObj(vm,
7144 7145 7146 7147 7148 7149 7150 7151 7152 7153
                                    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)
7154
                testObjectEventQueue(privconn, event);
7155
            event = virDomainEventLifecycleNewFromObj(vm,
7156 7157 7158
                            VIR_DOMAIN_EVENT_STARTED,
                            VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            if (paused) {
7159
                event2 = virDomainEventLifecycleNewFromObj(vm,
7160 7161 7162 7163 7164 7165 7166 7167
                                VIR_DOMAIN_EVENT_SUSPENDED,
                                VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT);
            }
        }
    }

    vm->current_snapshot = snap;
    ret = 0;
7168
 cleanup:
7169
    if (event) {
7170
        testObjectEventQueue(privconn, event);
7171
        if (event2)
7172
            testObjectEventQueue(privconn, event2);
C
Cole Robinson 已提交
7173
    } else {
C
Cédric Bosdonnat 已提交
7174
        virObjectUnref(event2);
7175 7176 7177 7178 7179 7180 7181 7182
    }
    virObjectUnlock(vm);
    testDriverUnlock(privconn);

    return ret;
}


7183

7184
static virHypervisorDriver testHypervisorDriver = {
7185
    .name = "Test",
7186 7187 7188
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
7189
    .connectGetHostname = testConnectGetHostname, /* 0.6.3 */
7190
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
7191
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
7192 7193 7194 7195
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
7196
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210
    .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 */
7211 7212
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
7213
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
7214
    .domainRestore = testDomainRestore, /* 0.3.2 */
7215
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
7216
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
7217
    .domainCoreDumpWithFormat = testDomainCoreDumpWithFormat, /* 1.2.3 */
7218
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
7219 7220 7221 7222 7223 7224
    .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 */
7225 7226
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
7227 7228 7229
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
7230
    .domainDefineXMLFlags = testDomainDefineXMLFlags, /* 1.2.12 */
7231
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
7232
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
7233 7234 7235
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
7236 7237 7238 7239
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
7240 7241 7242
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
7243 7244 7245 7246
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
7247 7248 7249
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
7250 7251 7252
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
7253
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
7254
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
7255 7256
    .domainGetMetadata = testDomainGetMetadata, /* 1.1.3 */
    .domainSetMetadata = testDomainSetMetadata, /* 1.1.3 */
7257
    .connectGetCPUModelNames = testConnectGetCPUModelNames, /* 1.1.3 */
7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274
    .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 */
7275 7276 7277
    .domainSnapshotCreateXML = testDomainSnapshotCreateXML, /* 1.1.4 */
    .domainRevertToSnapshot = testDomainRevertToSnapshot, /* 1.1.4 */
    .domainSnapshotDelete = testDomainSnapshotDelete, /* 1.1.4 */
7278

E
Eric Blake 已提交
7279
    .connectBaselineCPU = testConnectBaselineCPU, /* 1.2.0 */
7280 7281 7282
};

static virNetworkDriver testNetworkDriver = {
7283 7284 7285 7286 7287
    .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 */
7288 7289
    .connectNetworkEventRegisterAny = testConnectNetworkEventRegisterAny, /* 1.2.1 */
    .connectNetworkEventDeregisterAny = testConnectNetworkEventDeregisterAny, /* 1.2.1 */
7290 7291 7292 7293
    .networkLookupByUUID = testNetworkLookupByUUID, /* 0.3.2 */
    .networkLookupByName = testNetworkLookupByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreateXML, /* 0.3.2 */
    .networkDefineXML = testNetworkDefineXML, /* 0.3.2 */
7294
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
7295
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
7296
    .networkCreate = testNetworkCreate, /* 0.3.2 */
7297 7298 7299 7300 7301 7302 7303
    .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 */
7304 7305
};

L
Laine Stump 已提交
7306
static virInterfaceDriver testInterfaceDriver = {
7307 7308 7309 7310 7311 7312
    .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 */
7313 7314 7315 7316 7317 7318
    .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 */
7319 7320 7321
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
7322 7323 7324
};


7325
static virStorageDriver testStorageDriver = {
7326 7327 7328 7329 7330 7331
    .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 */
7332 7333 7334
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
7335 7336
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
7337 7338
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
7339
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
7340 7341 7342 7343 7344 7345 7346
    .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 */
7347
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
7348 7349 7350
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

7351 7352 7353 7354 7355 7356 7357 7358 7359
    .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 */
7360 7361
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
7362 7363
};

7364
static virNodeDeviceDriver testNodeDeviceDriver = {
7365 7366 7367 7368 7369 7370 7371 7372 7373
    .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 */
7374 7375
};

7376 7377 7378 7379 7380 7381 7382 7383
static virConnectDriver testConnectDriver = {
    .hypervisorDriver = &testHypervisorDriver,
    .interfaceDriver = &testInterfaceDriver,
    .networkDriver = &testNetworkDriver,
    .nodeDeviceDriver = &testNodeDeviceDriver,
    .nwfilterDriver = NULL,
    .secretDriver = NULL,
    .storageDriver = &testStorageDriver,
7384 7385
};

7386 7387 7388 7389 7390 7391 7392 7393
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
7394 7395
    return virRegisterConnectDriver(&testConnectDriver,
                                    false);
7396
}