test_driver.c 206.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_EMULATOR "/usr/bin/test-hv"
126

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

138

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

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

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

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

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

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

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

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

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

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

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

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

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

284 285 286
    *data = nsdata;
    return 0;

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

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


311
static virCapsPtr
312 313
testBuildCapabilities(virConnectPtr conn)
{
314
    testConnPtr privconn = conn->privateData;
315 316
    virCapsPtr caps;
    virCapsGuestPtr guest;
317 318
    int guest_types[] = { VIR_DOMAIN_OSTYPE_HVM,
                          VIR_DOMAIN_OSTYPE_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, 0))) {
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, 0))) {
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
    info->memory = privdom->def->mem.cur_balloon;
2154
    info->maxMem = virDomainDefGetMemoryActual(privdom->def);
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 = virDomainDefGetMemoryActual(privdom->def);
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
    virDomainDefSetMemoryInitial(privdom->def, 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 > virDomainDefGetMemoryActual(privdom->def)) {
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
    net = virNetworkObjFindByUUID(privconn->networks, uuid);
3499
    if (net == NULL) {
3500
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3501
        goto cleanup;
3502 3503
    }

3504 3505
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

3506
 cleanup:
3507
    virNetworkObjEndAPI(&net);
3508
    return ret;
3509
}
3510

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

3518
    net = virNetworkObjFindByName(privconn->networks, name);
3519
    if (net == NULL) {
3520
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3521
        goto cleanup;
3522 3523
    }

3524 3525
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

3526
 cleanup:
3527
    virNetworkObjEndAPI(&net);
3528
    return ret;
3529 3530 3531
}


3532 3533
static int testConnectNumOfNetworks(virConnectPtr conn)
{
3534
    testConnPtr privconn = conn->privateData;
3535
    int numActive;
3536

3537 3538
    numActive = virNetworkObjListNumOfNetworks(privconn->networks,
                                               true, NULL, conn);
3539
    return numActive;
3540 3541
}

3542
static int testConnectListNetworks(virConnectPtr conn, char **const names, int nnames) {
3543
    testConnPtr privconn = conn->privateData;
3544
    int n;
3545

3546 3547
    n = virNetworkObjListGetNames(privconn->networks,
                                  true, names, nnames, NULL, conn);
3548
    return n;
3549 3550
}

3551 3552
static int testConnectNumOfDefinedNetworks(virConnectPtr conn)
{
3553
    testConnPtr privconn = conn->privateData;
3554
    int numInactive;
3555

3556 3557
    numInactive = virNetworkObjListNumOfNetworks(privconn->networks,
                                                 false, NULL, conn);
3558
    return numInactive;
3559 3560
}

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

3565 3566
    n = virNetworkObjListGetNames(privconn->networks,
                                  false, names, nnames, NULL, conn);
3567
    return n;
3568 3569
}

3570
static int
3571
testConnectListAllNetworks(virConnectPtr conn,
3572 3573 3574 3575 3576 3577 3578
                           virNetworkPtr **nets,
                           unsigned int flags)
{
    testConnPtr privconn = conn->privateData;

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

3579
    return virNetworkObjListExport(conn, privconn->networks, nets, NULL, flags);
3580
}
3581 3582 3583 3584 3585 3586 3587

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

3588
    obj = virNetworkObjFindByUUID(privconn->networks, net->uuid);
3589
    if (!obj) {
3590
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3591 3592 3593 3594
        goto cleanup;
    }
    ret = virNetworkObjIsActive(obj);

3595
 cleanup:
3596
    virNetworkObjEndAPI(&obj);
3597 3598 3599 3600 3601 3602 3603 3604 3605
    return ret;
}

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

3606
    obj = virNetworkObjFindByUUID(privconn->networks, net->uuid);
3607
    if (!obj) {
3608
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3609 3610 3611 3612
        goto cleanup;
    }
    ret = obj->persistent;

3613
 cleanup:
3614
    virNetworkObjEndAPI(&obj);
3615 3616 3617 3618
    return ret;
}


3619 3620
static virNetworkPtr testNetworkCreateXML(virConnectPtr conn, const char *xml)
{
3621
    testConnPtr privconn = conn->privateData;
3622
    virNetworkDefPtr def;
3623
    virNetworkObjPtr net = NULL;
3624
    virNetworkPtr ret = NULL;
3625
    virObjectEventPtr event = NULL;
3626

3627
    if ((def = virNetworkDefParseString(xml)) == NULL)
3628
        goto cleanup;
3629

3630 3631 3632
    if (!(net = virNetworkAssignDef(privconn->networks, def,
                                    VIR_NETWORK_OBJ_LIST_ADD_LIVE |
                                    VIR_NETWORK_OBJ_LIST_ADD_CHECK_LIVE)))
3633 3634
        goto cleanup;
    def = NULL;
3635
    net->active = 1;
3636

3637
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3638 3639
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3640

3641
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3642

3643
 cleanup:
3644
    virNetworkDefFree(def);
3645 3646
    if (event)
        testObjectEventQueue(privconn, event);
3647
    virNetworkObjEndAPI(&net);
3648
    return ret;
3649 3650
}

3651
static
3652
virNetworkPtr testNetworkDefineXML(virConnectPtr conn, const char *xml)
3653
{
3654
    testConnPtr privconn = conn->privateData;
3655
    virNetworkDefPtr def;
3656
    virNetworkObjPtr net = NULL;
3657
    virNetworkPtr ret = NULL;
3658
    virObjectEventPtr event = NULL;
3659

3660
    if ((def = virNetworkDefParseString(xml)) == NULL)
3661
        goto cleanup;
3662

3663
    if (!(net = virNetworkAssignDef(privconn->networks, def, 0)))
3664 3665
        goto cleanup;
    def = NULL;
3666

3667
    event = virNetworkEventLifecycleNew(net->def->name, net->def->uuid,
3668 3669
                                        VIR_NETWORK_EVENT_DEFINED,
                                        0);
3670

3671
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3672

3673
 cleanup:
3674
    virNetworkDefFree(def);
3675 3676
    if (event)
        testObjectEventQueue(privconn, event);
3677
    virNetworkObjEndAPI(&net);
3678
    return ret;
3679 3680
}

3681 3682
static int testNetworkUndefine(virNetworkPtr network)
{
3683 3684
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3685
    int ret = -1;
3686
    virObjectEventPtr event = NULL;
3687

3688
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3689 3690

    if (privnet == NULL) {
3691
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3692
        goto cleanup;
3693
    }
3694

D
Daniel P. Berrange 已提交
3695
    if (virNetworkObjIsActive(privnet)) {
3696 3697
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is still running"), network->name);
3698
        goto cleanup;
3699 3700
    }

3701
    event = virNetworkEventLifecycleNew(network->name, network->uuid,
3702 3703
                                        VIR_NETWORK_EVENT_UNDEFINED,
                                        0);
3704

3705
    virNetworkRemoveInactive(privconn->networks, privnet);
3706
    ret = 0;
3707

3708
 cleanup:
3709 3710
    if (event)
        testObjectEventQueue(privconn, event);
3711
    virNetworkObjEndAPI(&privnet);
3712
    return ret;
3713 3714
}

3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730
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);

3731
    network = virNetworkObjFindByUUID(privconn->networks, net->uuid);
3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755
    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;
3756
 cleanup:
3757
    virNetworkObjEndAPI(&network);
3758 3759 3760
    return ret;
}

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

3768
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3769
    if (privnet == NULL) {
3770
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3771
        goto cleanup;
3772
    }
3773

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

3780
    privnet->active = 1;
3781
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3782 3783
                                        VIR_NETWORK_EVENT_STARTED,
                                        0);
3784
    ret = 0;
3785

3786
 cleanup:
3787 3788
    if (event)
        testObjectEventQueue(privconn, event);
3789
    virNetworkObjEndAPI(&privnet);
3790
    return ret;
3791 3792
}

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

3800
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3801
    if (privnet == NULL) {
3802
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3803
        goto cleanup;
3804
    }
3805

3806
    privnet->active = 0;
3807
    event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
3808 3809
                                        VIR_NETWORK_EVENT_STOPPED,
                                        0);
3810
    if (!privnet->persistent)
3811
        virNetworkRemoveInactive(privconn->networks, privnet);
3812

3813 3814
    ret = 0;

3815
 cleanup:
3816 3817
    if (event)
        testObjectEventQueue(privconn, event);
3818
    virNetworkObjEndAPI(&privnet);
3819
    return ret;
3820 3821
}

3822
static char *testNetworkGetXMLDesc(virNetworkPtr network,
E
Eric Blake 已提交
3823
                                   unsigned int flags)
3824
{
3825 3826
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3827
    char *ret = NULL;
3828

E
Eric Blake 已提交
3829 3830
    virCheckFlags(0, NULL);

3831
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3832
    if (privnet == NULL) {
3833
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3834
        goto cleanup;
3835
    }
3836

3837
    ret = virNetworkDefFormat(privnet->def, flags);
3838

3839
 cleanup:
3840
    virNetworkObjEndAPI(&privnet);
3841
    return ret;
3842 3843 3844
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3845
    testConnPtr privconn = network->conn->privateData;
3846
    char *bridge = NULL;
3847 3848
    virNetworkObjPtr privnet;

3849
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3850
    if (privnet == NULL) {
3851
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3852
        goto cleanup;
3853 3854
    }

3855
    if (!(privnet->def->bridge)) {
3856 3857 3858
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("network '%s' does not have a bridge name."),
                       privnet->def->name);
3859 3860 3861
        goto cleanup;
    }

3862
    ignore_value(VIR_STRDUP(bridge, privnet->def->bridge));
3863

3864
 cleanup:
3865
    virNetworkObjEndAPI(&privnet);
3866 3867 3868 3869
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
3870 3871
                                   int *autostart)
{
3872 3873
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3874
    int ret = -1;
3875

3876
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3877
    if (privnet == NULL) {
3878
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3879
        goto cleanup;
3880 3881
    }

3882
    *autostart = privnet->autostart;
3883 3884
    ret = 0;

3885
 cleanup:
3886
    virNetworkObjEndAPI(&privnet);
3887
    return ret;
3888 3889 3890
}

static int testNetworkSetAutostart(virNetworkPtr network,
3891 3892
                                   int autostart)
{
3893 3894
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3895
    int ret = -1;
3896

3897
    privnet = virNetworkObjFindByName(privconn->networks, network->name);
3898
    if (privnet == NULL) {
3899
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3900
        goto cleanup;
3901 3902
    }

3903
    privnet->autostart = autostart ? 1 : 0;
3904 3905
    ret = 0;

3906
 cleanup:
3907
    virNetworkObjEndAPI(&privnet);
3908
    return ret;
3909
}
3910

C
Cole Robinson 已提交
3911

L
Laine Stump 已提交
3912 3913 3914 3915 3916
/*
 * Physical host interface routines
 */


3917
static int testConnectNumOfInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3918 3919
{
    testConnPtr privconn = conn->privateData;
3920 3921
    size_t i;
    int count = 0;
L
Laine Stump 已提交
3922 3923

    testDriverLock(privconn);
3924
    for (i = 0; (i < privconn->ifaces.count); i++) {
L
Laine Stump 已提交
3925
        virInterfaceObjLock(privconn->ifaces.objs[i]);
3926
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i]))
L
Laine Stump 已提交
3927 3928 3929 3930 3931 3932 3933
            count++;
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

3934
static int testConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
L
Laine Stump 已提交
3935 3936
{
    testConnPtr privconn = conn->privateData;
3937 3938
    int n = 0;
    size_t i;
L
Laine Stump 已提交
3939 3940 3941

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
3942
    for (i = 0; (i < privconn->ifaces.count) && (n < nnames); i++) {
L
Laine Stump 已提交
3943
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3944
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
3945
            if (VIR_STRDUP(names[n++], privconn->ifaces.objs[i]->def->name) < 0) {
L
Laine Stump 已提交
3946
                virInterfaceObjUnlock(privconn->ifaces.objs[i]);
3947
                goto error;
L
Laine Stump 已提交
3948 3949 3950 3951 3952 3953 3954 3955
            }
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);

    return n;

3956
 error:
3957
    for (n = 0; n < nnames; n++)
L
Laine Stump 已提交
3958 3959 3960 3961 3962
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

3963
static int testConnectNumOfDefinedInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3964 3965
{
    testConnPtr privconn = conn->privateData;
3966 3967
    size_t i;
    int count = 0;
L
Laine Stump 已提交
3968 3969

    testDriverLock(privconn);
3970
    for (i = 0; i < privconn->ifaces.count; i++) {
L
Laine Stump 已提交
3971
        virInterfaceObjLock(privconn->ifaces.objs[i]);
3972
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i]))
L
Laine Stump 已提交
3973 3974 3975 3976 3977 3978 3979
            count++;
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

3980
static int testConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
L
Laine Stump 已提交
3981 3982
{
    testConnPtr privconn = conn->privateData;
3983 3984
    int n = 0;
    size_t i;
L
Laine Stump 已提交
3985 3986 3987

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

    return n;

4002
 error:
4003
    for (n = 0; n < nnames; n++)
L
Laine Stump 已提交
4004 4005 4006 4007 4008
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

4009
static virInterfacePtr testInterfaceLookupByName(virConnectPtr conn,
L
Laine Stump 已提交
4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020
                                                 const char *name)
{
    testConnPtr privconn = conn->privateData;
    virInterfaceObjPtr iface;
    virInterfacePtr ret = NULL;

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

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

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

4027
 cleanup:
L
Laine Stump 已提交
4028 4029 4030 4031 4032
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

4033
static virInterfacePtr testInterfaceLookupByMACString(virConnectPtr conn,
L
Laine Stump 已提交
4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045
                                                      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) {
4046
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4047 4048 4049 4050
        goto cleanup;
    }

    if (ifacect > 1) {
4051
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
4052 4053 4054 4055 4056
        goto cleanup;
    }

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

4057
 cleanup:
L
Laine Stump 已提交
4058 4059 4060 4061 4062
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

4063 4064 4065 4066 4067 4068 4069 4070 4071 4072
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) {
4073
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
4074 4075 4076 4077
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

4078
 cleanup:
4079 4080 4081 4082 4083
    if (obj)
        virInterfaceObjUnlock(obj);
    return ret;
}

4084
static int testInterfaceChangeBegin(virConnectPtr conn,
E
Eric Blake 已提交
4085
                                    unsigned int flags)
4086 4087 4088 4089
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
4090 4091
    virCheckFlags(0, -1);

4092 4093
    testDriverLock(privconn);
    if (privconn->transaction_running) {
4094
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
4095
                       _("there is another transaction running."));
4096 4097 4098 4099 4100 4101 4102 4103 4104 4105
        goto cleanup;
    }

    privconn->transaction_running = true;

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

    ret = 0;
4106
 cleanup:
4107 4108 4109 4110 4111
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceChangeCommit(virConnectPtr conn,
E
Eric Blake 已提交
4112
                                     unsigned int flags)
4113 4114 4115 4116
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
4117 4118
    virCheckFlags(0, -1);

4119 4120 4121
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
4122
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
4123 4124
                       _("no transaction running, "
                         "nothing to be committed."));
4125 4126 4127 4128 4129 4130 4131 4132
        goto cleanup;
    }

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

    ret = 0;

4133
 cleanup:
4134 4135 4136 4137 4138 4139
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
E
Eric Blake 已提交
4140
                                       unsigned int flags)
4141 4142 4143 4144
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
4145 4146
    virCheckFlags(0, -1);

4147 4148 4149
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
4150
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
4151 4152
                       _("no transaction running, "
                         "nothing to rollback."));
4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165
        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;

4166
 cleanup:
4167 4168 4169
    testDriverUnlock(privconn);
    return ret;
}
4170

L
Laine Stump 已提交
4171
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
E
Eric Blake 已提交
4172
                                     unsigned int flags)
L
Laine Stump 已提交
4173 4174 4175 4176 4177
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

E
Eric Blake 已提交
4178 4179
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
4180 4181 4182 4183 4184 4185
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);
    testDriverUnlock(privconn);

    if (privinterface == NULL) {
4186
        virReportError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
4187 4188 4189
        goto cleanup;
    }

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

4192
 cleanup:
L
Laine Stump 已提交
4193 4194 4195 4196 4197 4198 4199
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    return ret;
}


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
E
Eric Blake 已提交
4200
                                              unsigned int flags)
L
Laine Stump 已提交
4201 4202 4203 4204 4205 4206
{
    testConnPtr privconn = conn->privateData;
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
4207 4208
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
4209
    testDriverLock(privconn);
4210
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
4211 4212
        goto cleanup;

4213
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
4214 4215 4216 4217 4218
        goto cleanup;
    def = NULL;

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

4219
 cleanup:
L
Laine Stump 已提交
4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237
    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) {
4238
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4239 4240 4241 4242 4243 4244 4245
        goto cleanup;
    }

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

4246
 cleanup:
L
Laine Stump 已提交
4247 4248 4249 4250 4251
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
E
Eric Blake 已提交
4252
                               unsigned int flags)
L
Laine Stump 已提交
4253 4254 4255 4256 4257
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
4258 4259
    virCheckFlags(0, -1);

L
Laine Stump 已提交
4260 4261 4262 4263 4264
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
4265
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
4266 4267 4268 4269
        goto cleanup;
    }

    if (privinterface->active != 0) {
4270
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4271 4272 4273 4274 4275 4276
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

4277
 cleanup:
L
Laine Stump 已提交
4278 4279 4280 4281 4282 4283 4284
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceDestroy(virInterfacePtr iface,
E
Eric Blake 已提交
4285
                                unsigned int flags)
L
Laine Stump 已提交
4286 4287 4288 4289 4290
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
4291 4292
    virCheckFlags(0, -1);

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

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

    if (privinterface->active == 0) {
4303
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
4304 4305 4306 4307 4308 4309
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

4310
 cleanup:
L
Laine Stump 已提交
4311 4312 4313 4314 4315 4316 4317 4318
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}



C
Cole Robinson 已提交
4319 4320 4321 4322
/*
 * Storage Driver routines
 */

4323

4324 4325
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool)
{
C
Cole Robinson 已提交
4326 4327 4328 4329 4330

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

4331
    return VIR_STRDUP(pool->configFile, "");
C
Cole Robinson 已提交
4332 4333
}

4334

C
Cole Robinson 已提交
4335 4336
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
4337 4338
                            const unsigned char *uuid)
{
4339
    testConnPtr privconn = conn->privateData;
4340 4341
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4342

4343
    testDriverLock(privconn);
4344
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
4345
    testDriverUnlock(privconn);
4346 4347

    if (pool == NULL) {
4348
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4349
        goto cleanup;
C
Cole Robinson 已提交
4350 4351
    }

4352 4353
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4354

4355
 cleanup:
4356 4357
    if (pool)
        virStoragePoolObjUnlock(pool);
4358
    return ret;
C
Cole Robinson 已提交
4359 4360 4361 4362
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
4363 4364
                            const char *name)
{
4365
    testConnPtr privconn = conn->privateData;
4366 4367
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4368

4369
    testDriverLock(privconn);
4370
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
4371
    testDriverUnlock(privconn);
4372 4373

    if (pool == NULL) {
4374
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4375
        goto cleanup;
C
Cole Robinson 已提交
4376 4377
    }

4378 4379
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4380

4381
 cleanup:
4382 4383
    if (pool)
        virStoragePoolObjUnlock(pool);
4384
    return ret;
C
Cole Robinson 已提交
4385 4386 4387
}

static virStoragePoolPtr
4388 4389
testStoragePoolLookupByVolume(virStorageVolPtr vol)
{
C
Cole Robinson 已提交
4390 4391 4392 4393
    return testStoragePoolLookupByName(vol->conn, vol->pool);
}

static int
4394 4395
testConnectNumOfStoragePools(virConnectPtr conn)
{
4396
    testConnPtr privconn = conn->privateData;
4397 4398
    int numActive = 0;
    size_t i;
C
Cole Robinson 已提交
4399

4400
    testDriverLock(privconn);
4401
    for (i = 0; i < privconn->pools.count; i++)
C
Cole Robinson 已提交
4402 4403
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
4404
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4405 4406 4407 4408 4409

    return numActive;
}

static int
4410 4411
testConnectListStoragePools(virConnectPtr conn,
                            char **const names,
4412 4413
                            int nnames)
{
4414
    testConnPtr privconn = conn->privateData;
4415 4416
    int n = 0;
    size_t i;
C
Cole Robinson 已提交
4417

4418
    testDriverLock(privconn);
C
Cole Robinson 已提交
4419
    memset(names, 0, sizeof(*names)*nnames);
4420
    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
4421
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4422
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
4423
            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
4424
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
4425
            goto error;
4426 4427 4428 4429
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4430 4431 4432

    return n;

4433
 error:
4434
    for (n = 0; n < nnames; n++)
C
Cole Robinson 已提交
4435
        VIR_FREE(names[n]);
4436
    testDriverUnlock(privconn);
4437
    return -1;
C
Cole Robinson 已提交
4438 4439 4440
}

static int
4441 4442
testConnectNumOfDefinedStoragePools(virConnectPtr conn)
{
4443
    testConnPtr privconn = conn->privateData;
4444 4445
    int numInactive = 0;
    size_t i;
C
Cole Robinson 已提交
4446

4447
    testDriverLock(privconn);
4448
    for (i = 0; i < privconn->pools.count; i++) {
4449
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4450 4451
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
4452 4453 4454
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4455 4456 4457 4458 4459

    return numInactive;
}

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

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

    return n;

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

4490
static int
4491 4492 4493
testConnectListAllStoragePools(virConnectPtr conn,
                               virStoragePoolPtr **pools,
                               unsigned int flags)
4494 4495 4496 4497 4498 4499 4500
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

    testDriverLock(privconn);
4501 4502
    ret = virStoragePoolObjListExport(conn, privconn->pools, pools,
                                      NULL, flags);
4503 4504 4505 4506
    testDriverUnlock(privconn);

    return ret;
}
C
Cole Robinson 已提交
4507

4508 4509 4510 4511 4512 4513 4514 4515 4516 4517
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) {
4518
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4519 4520 4521 4522
        goto cleanup;
    }
    ret = virStoragePoolObjIsActive(obj);

4523
 cleanup:
4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538
    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) {
4539
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4540 4541 4542 4543
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

4544
 cleanup:
4545 4546 4547 4548 4549 4550 4551
    if (obj)
        virStoragePoolObjUnlock(obj);
    return ret;
}



C
Cole Robinson 已提交
4552
static int
4553 4554
testStoragePoolCreate(virStoragePoolPtr pool,
                      unsigned int flags)
E
Eric Blake 已提交
4555
{
4556 4557
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4558
    int ret = -1;
4559

E
Eric Blake 已提交
4560 4561
    virCheckFlags(0, -1);

4562
    testDriverLock(privconn);
4563 4564
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4565
    testDriverUnlock(privconn);
4566 4567

    if (privpool == NULL) {
4568
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4569
        goto cleanup;
4570 4571
    }

4572
    if (virStoragePoolObjIsActive(privpool)) {
4573 4574
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4575 4576
        goto cleanup;
    }
C
Cole Robinson 已提交
4577 4578

    privpool->active = 1;
4579
    ret = 0;
C
Cole Robinson 已提交
4580

4581
 cleanup:
4582 4583
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4584
    return ret;
C
Cole Robinson 已提交
4585 4586 4587
}

static char *
4588 4589 4590 4591
testConnectFindStoragePoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type,
                                  const char *srcSpec,
                                  unsigned int flags)
C
Cole Robinson 已提交
4592
{
4593 4594 4595 4596
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4597 4598
    virCheckFlags(0, NULL);

4599 4600
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4601 4602
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
4603 4604 4605 4606
        goto cleanup;
    }

    if (srcSpec) {
4607
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4608 4609 4610 4611 4612 4613 4614
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
4615
        ignore_value(VIR_STRDUP(ret, defaultPoolSourcesLogicalXML));
4616 4617 4618
        break;

    case VIR_STORAGE_POOL_NETFS:
4619
        if (!source || !source->hosts[0].name) {
4620 4621
            virReportError(VIR_ERR_INVALID_ARG,
                           "%s", _("hostname must be specified for netfs sources"));
4622 4623 4624
            goto cleanup;
        }

4625 4626
        ignore_value(virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                                 source->hosts[0].name));
4627 4628 4629
        break;

    default:
4630 4631
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4632 4633
    }

4634
 cleanup:
4635 4636
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4637 4638 4639 4640
}


static virStoragePoolPtr
4641 4642 4643
testStoragePoolCreateXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4644
{
4645
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4646
    virStoragePoolDefPtr def;
4647
    virStoragePoolObjPtr pool = NULL;
4648
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4649

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

4652
    testDriverLock(privconn);
4653
    if (!(def = virStoragePoolDefParseString(xml)))
4654
        goto cleanup;
C
Cole Robinson 已提交
4655

4656 4657 4658 4659
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4660 4661
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("storage pool already exists"));
4662
        goto cleanup;
C
Cole Robinson 已提交
4663 4664
    }

4665
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4666
        goto cleanup;
4667
    def = NULL;
C
Cole Robinson 已提交
4668

4669
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4670
        virStoragePoolObjRemove(&privconn->pools, pool);
4671 4672
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4673 4674 4675
    }
    pool->active = 1;

4676 4677
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4678

4679
 cleanup:
4680
    virStoragePoolDefFree(def);
4681 4682 4683
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4684
    return ret;
C
Cole Robinson 已提交
4685 4686 4687
}

static virStoragePoolPtr
4688 4689 4690
testStoragePoolDefineXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4691
{
4692
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4693
    virStoragePoolDefPtr def;
4694
    virStoragePoolObjPtr pool = NULL;
4695
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4696

E
Eric Blake 已提交
4697 4698
    virCheckFlags(0, NULL);

4699
    testDriverLock(privconn);
4700
    if (!(def = virStoragePoolDefParseString(xml)))
4701
        goto cleanup;
C
Cole Robinson 已提交
4702 4703 4704 4705 4706

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

4707
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4708 4709
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4710

4711
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4712
        virStoragePoolObjRemove(&privconn->pools, pool);
4713 4714
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4715 4716
    }

4717 4718
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4719

4720
 cleanup:
4721
    virStoragePoolDefFree(def);
4722 4723 4724
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4725
    return ret;
C
Cole Robinson 已提交
4726 4727 4728
}

static int
4729 4730
testStoragePoolUndefine(virStoragePoolPtr pool)
{
4731 4732
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4733
    int ret = -1;
4734

4735
    testDriverLock(privconn);
4736 4737 4738 4739
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4740
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4741
        goto cleanup;
4742 4743
    }

4744
    if (virStoragePoolObjIsActive(privpool)) {
4745 4746
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4747 4748
        goto cleanup;
    }
C
Cole Robinson 已提交
4749 4750

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

4753
 cleanup:
4754 4755 4756
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4757
    return ret;
C
Cole Robinson 已提交
4758 4759 4760
}

static int
4761
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4762 4763
                     unsigned int flags)
{
4764 4765
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4766
    int ret = -1;
4767

E
Eric Blake 已提交
4768 4769
    virCheckFlags(0, -1);

4770
    testDriverLock(privconn);
4771 4772
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4773
    testDriverUnlock(privconn);
4774 4775

    if (privpool == NULL) {
4776
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4777
        goto cleanup;
4778 4779
    }

4780
    if (virStoragePoolObjIsActive(privpool)) {
4781 4782
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4783 4784
        goto cleanup;
    }
4785
    ret = 0;
C
Cole Robinson 已提交
4786

4787
 cleanup:
4788 4789
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4790
    return ret;
C
Cole Robinson 已提交
4791 4792 4793 4794
}


static int
4795 4796
testStoragePoolDestroy(virStoragePoolPtr pool)
{
4797 4798
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4799
    int ret = -1;
4800

4801
    testDriverLock(privconn);
4802 4803 4804 4805
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4806
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4807
        goto cleanup;
4808 4809 4810
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4811 4812
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4813
        goto cleanup;
4814
    }
C
Cole Robinson 已提交
4815 4816 4817

    privpool->active = 0;

4818
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4819
        virStoragePoolObjRemove(&privconn->pools, privpool);
4820 4821
        privpool = NULL;
    }
4822
    ret = 0;
C
Cole Robinson 已提交
4823

4824
 cleanup:
4825 4826 4827
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4828
    return ret;
C
Cole Robinson 已提交
4829 4830 4831 4832
}


static int
4833
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4834 4835
                      unsigned int flags)
{
4836 4837
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4838
    int ret = -1;
4839

E
Eric Blake 已提交
4840 4841
    virCheckFlags(0, -1);

4842
    testDriverLock(privconn);
4843 4844
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4845
    testDriverUnlock(privconn);
4846 4847

    if (privpool == NULL) {
4848
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4849 4850 4851 4852
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
4853 4854
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4855
        goto cleanup;
4856 4857
    }

4858
    ret = 0;
C
Cole Robinson 已提交
4859

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


static int
4868
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4869 4870
                       unsigned int flags)
{
4871 4872
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4873
    int ret = -1;
4874

E
Eric Blake 已提交
4875 4876
    virCheckFlags(0, -1);

4877
    testDriverLock(privconn);
4878 4879
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4880
    testDriverUnlock(privconn);
4881 4882

    if (privpool == NULL) {
4883
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4884
        goto cleanup;
4885 4886 4887
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4888 4889
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4890
        goto cleanup;
4891
    }
4892
    ret = 0;
C
Cole Robinson 已提交
4893

4894
 cleanup:
4895 4896
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4897
    return ret;
C
Cole Robinson 已提交
4898 4899 4900 4901
}


static int
4902
testStoragePoolGetInfo(virStoragePoolPtr pool,
4903 4904
                       virStoragePoolInfoPtr info)
{
4905 4906
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4907
    int ret = -1;
4908

4909
    testDriverLock(privconn);
4910 4911
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4912
    testDriverUnlock(privconn);
4913 4914

    if (privpool == NULL) {
4915
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4916
        goto cleanup;
4917
    }
C
Cole Robinson 已提交
4918 4919 4920 4921 4922 4923 4924 4925 4926

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

4929
 cleanup:
4930 4931
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4932
    return ret;
C
Cole Robinson 已提交
4933 4934 4935
}

static char *
4936
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4937 4938
                          unsigned int flags)
{
4939 4940
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4941
    char *ret = NULL;
4942

E
Eric Blake 已提交
4943 4944
    virCheckFlags(0, NULL);

4945
    testDriverLock(privconn);
4946 4947
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4948
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4949

4950
    if (privpool == NULL) {
4951
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4952
        goto cleanup;
4953 4954
    }

4955
    ret = virStoragePoolDefFormat(privpool->def);
4956

4957
 cleanup:
4958 4959
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4960
    return ret;
C
Cole Robinson 已提交
4961 4962 4963
}

static int
4964
testStoragePoolGetAutostart(virStoragePoolPtr pool,
4965 4966
                            int *autostart)
{
4967 4968
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4969
    int ret = -1;
4970

4971
    testDriverLock(privconn);
4972 4973
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4974
    testDriverUnlock(privconn);
4975 4976

    if (privpool == NULL) {
4977
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4978
        goto cleanup;
4979
    }
C
Cole Robinson 已提交
4980 4981 4982 4983 4984 4985

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

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

static int
4995
testStoragePoolSetAutostart(virStoragePoolPtr pool,
4996 4997
                            int autostart)
{
4998 4999
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5000
    int ret = -1;
5001

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

    if (privpool == NULL) {
5008
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5009
        goto cleanup;
5010
    }
C
Cole Robinson 已提交
5011 5012

    if (!privpool->configFile) {
5013 5014
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
5015
        goto cleanup;
C
Cole Robinson 已提交
5016 5017 5018 5019
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
5020 5021
    ret = 0;

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


static int
5030 5031
testStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
5032 5033
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5034
    int ret = -1;
5035

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

    if (privpool == NULL) {
5042
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5043
        goto cleanup;
5044 5045 5046
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5047 5048
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5049
        goto cleanup;
5050
    }
C
Cole Robinson 已提交
5051

5052 5053
    ret = privpool->volumes.count;

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

static int
5061
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
5062
                           char **const names,
5063 5064
                           int maxnames)
{
5065 5066
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5067 5068
    size_t i = 0;
    int n = 0;
C
Cole Robinson 已提交
5069

5070
    memset(names, 0, maxnames * sizeof(*names));
5071 5072

    testDriverLock(privconn);
5073 5074
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5075
    testDriverUnlock(privconn);
5076 5077

    if (privpool == NULL) {
5078
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5079
        goto cleanup;
5080 5081 5082 5083
    }


    if (!virStoragePoolObjIsActive(privpool)) {
5084 5085
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5086
        goto cleanup;
5087 5088
    }

5089
    for (i = 0; i < privpool->volumes.count && n < maxnames; i++) {
5090
        if (VIR_STRDUP(names[n++], privpool->volumes.objs[i]->name) < 0)
C
Cole Robinson 已提交
5091 5092 5093
            goto cleanup;
    }

5094
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5095 5096 5097
    return n;

 cleanup:
5098
    for (n = 0; n < maxnames; n++)
C
Cole Robinson 已提交
5099 5100
        VIR_FREE(names[i]);

5101
    memset(names, 0, maxnames * sizeof(*names));
5102 5103
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5104 5105 5106
    return -1;
}

5107 5108 5109
static int
testStoragePoolListAllVolumes(virStoragePoolPtr obj,
                              virStorageVolPtr **vols,
5110 5111
                              unsigned int flags)
{
5112 5113
    testConnPtr privconn = obj->conn->privateData;
    virStoragePoolObjPtr pool;
5114
    size_t i;
5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143
    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;
    }

5144
    if (VIR_ALLOC_N(tmp_vols, pool->volumes.count + 1) < 0)
5145 5146
         goto cleanup;

5147
    for (i = 0; i < pool->volumes.count; i++) {
5148 5149
        if (!(vol = virGetStorageVol(obj->conn, pool->def->name,
                                     pool->volumes.objs[i]->name,
5150 5151
                                     pool->volumes.objs[i]->key,
                                     NULL, NULL)))
5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165
            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]);
        }
5166
        VIR_FREE(tmp_vols);
5167 5168 5169 5170 5171 5172 5173
    }

    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}
C
Cole Robinson 已提交
5174 5175

static virStorageVolPtr
5176
testStorageVolLookupByName(virStoragePoolPtr pool,
5177 5178
                           const char *name ATTRIBUTE_UNUSED)
{
5179 5180 5181
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5182
    virStorageVolPtr ret = NULL;
5183

5184
    testDriverLock(privconn);
5185 5186
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
5187
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5188

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


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

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
5204 5205
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"), name);
5206
        goto cleanup;
C
Cole Robinson 已提交
5207 5208
    }

5209
    ret = virGetStorageVol(pool->conn, privpool->def->name,
5210 5211
                           privvol->name, privvol->key,
                           NULL, NULL);
5212

5213
 cleanup:
5214 5215
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5216
    return ret;
C
Cole Robinson 已提交
5217 5218 5219 5220
}


static virStorageVolPtr
5221
testStorageVolLookupByKey(virConnectPtr conn,
5222 5223
                          const char *key)
{
5224
    testConnPtr privconn = conn->privateData;
5225
    size_t i;
5226
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
5227

5228
    testDriverLock(privconn);
5229
    for (i = 0; i < privconn->pools.count; i++) {
5230
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5231
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
5232
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
5233 5234
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

5235 5236 5237 5238
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
5239 5240
                                       privvol->key,
                                       NULL, NULL);
5241
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
5242 5243
                break;
            }
C
Cole Robinson 已提交
5244
        }
5245
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5246
    }
5247
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5248

5249
    if (!ret)
5250 5251
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
5252 5253

    return ret;
C
Cole Robinson 已提交
5254 5255 5256
}

static virStorageVolPtr
5257
testStorageVolLookupByPath(virConnectPtr conn,
5258 5259
                           const char *path)
{
5260
    testConnPtr privconn = conn->privateData;
5261
    size_t i;
5262
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
5263

5264
    testDriverLock(privconn);
5265
    for (i = 0; i < privconn->pools.count; i++) {
5266
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5267
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
5268
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
5269 5270
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

5271 5272 5273 5274
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
5275 5276
                                       privvol->key,
                                       NULL, NULL);
5277
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
5278 5279
                break;
            }
C
Cole Robinson 已提交
5280
        }
5281
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
5282
    }
5283
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
5284

5285
    if (!ret)
5286 5287
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
5288 5289

    return ret;
C
Cole Robinson 已提交
5290 5291 5292
}

static virStorageVolPtr
5293 5294 5295
testStorageVolCreateXML(virStoragePoolPtr pool,
                        const char *xmldesc,
                        unsigned int flags)
E
Eric Blake 已提交
5296
{
5297 5298
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
5299 5300
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
5301

E
Eric Blake 已提交
5302 5303
    virCheckFlags(0, NULL);

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

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

    if (!virStoragePoolObjIsActive(privpool)) {
5315 5316
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5317
        goto cleanup;
5318
    }
C
Cole Robinson 已提交
5319

5320
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5321
    if (privvol == NULL)
5322
        goto cleanup;
5323 5324

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5325 5326
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5327
        goto cleanup;
C
Cole Robinson 已提交
5328 5329 5330
    }

    /* Make sure enough space */
5331
    if ((privpool->def->allocation + privvol->target.allocation) >
C
Cole Robinson 已提交
5332
         privpool->def->capacity) {
5333 5334 5335
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5336
        goto cleanup;
C
Cole Robinson 已提交
5337 5338
    }

5339 5340
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5341
                    privvol->name) == -1)
5342
        goto cleanup;
C
Cole Robinson 已提交
5343

5344 5345 5346
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
5347
        goto cleanup;
C
Cole Robinson 已提交
5348

5349
    privpool->def->allocation += privvol->target.allocation;
C
Cole Robinson 已提交
5350 5351 5352
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5353
    ret = virGetStorageVol(pool->conn, privpool->def->name,
5354 5355
                           privvol->name, privvol->key,
                           NULL, NULL);
5356
    privvol = NULL;
5357

5358
 cleanup:
5359
    virStorageVolDefFree(privvol);
5360 5361
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5362
    return ret;
C
Cole Robinson 已提交
5363 5364
}

5365
static virStorageVolPtr
5366 5367 5368 5369
testStorageVolCreateXMLFrom(virStoragePoolPtr pool,
                            const char *xmldesc,
                            virStorageVolPtr clonevol,
                            unsigned int flags)
E
Eric Blake 已提交
5370
{
5371 5372 5373 5374 5375
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
5376 5377
    virCheckFlags(0, NULL);

5378 5379 5380 5381 5382 5383
    testDriverLock(privconn);
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
    testDriverUnlock(privconn);

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

    if (!virStoragePoolObjIsActive(privpool)) {
5389 5390
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
5391 5392 5393
        goto cleanup;
    }

5394
    privvol = virStorageVolDefParseString(privpool->def, xmldesc, 0);
5395 5396 5397 5398
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
5399 5400
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
5401 5402 5403 5404 5405
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
5406 5407 5408
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
5409 5410 5411 5412
        goto cleanup;
    }

    /* Make sure enough space */
5413
    if ((privpool->def->allocation + privvol->target.allocation) >
5414
         privpool->def->capacity) {
5415 5416 5417
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
5418 5419 5420 5421 5422
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5423 5424
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
5425
                    privvol->name) == -1)
5426 5427
        goto cleanup;

5428 5429 5430
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0 ||
        VIR_APPEND_ELEMENT_COPY(privpool->volumes.objs,
                                privpool->volumes.count, privvol) < 0)
5431 5432
        goto cleanup;

5433
    privpool->def->allocation += privvol->target.allocation;
5434 5435 5436 5437
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    ret = virGetStorageVol(pool->conn, privpool->def->name,
5438 5439
                           privvol->name, privvol->key,
                           NULL, NULL);
5440 5441
    privvol = NULL;

5442
 cleanup:
5443 5444 5445 5446 5447 5448
    virStorageVolDefFree(privvol);
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    return ret;
}

C
Cole Robinson 已提交
5449
static int
5450 5451
testStorageVolDelete(virStorageVolPtr vol,
                     unsigned int flags)
E
Eric Blake 已提交
5452
{
5453 5454 5455
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5456
    size_t i;
5457
    int ret = -1;
C
Cole Robinson 已提交
5458

E
Eric Blake 已提交
5459 5460
    virCheckFlags(0, -1);

5461
    testDriverLock(privconn);
5462 5463
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5464
    testDriverUnlock(privconn);
5465 5466

    if (privpool == NULL) {
5467
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5468
        goto cleanup;
5469 5470 5471 5472 5473 5474
    }


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

    if (privvol == NULL) {
5475 5476 5477
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5478
        goto cleanup;
5479 5480 5481
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5482 5483
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5484
        goto cleanup;
5485 5486 5487
    }


5488
    privpool->def->allocation -= privvol->target.allocation;
C
Cole Robinson 已提交
5489 5490 5491
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5492
    for (i = 0; i < privpool->volumes.count; i++) {
C
Cole Robinson 已提交
5493 5494 5495
        if (privpool->volumes.objs[i] == privvol) {
            virStorageVolDefFree(privvol);

5496
            VIR_DELETE_ELEMENT(privpool->volumes.objs, i, privpool->volumes.count);
C
Cole Robinson 已提交
5497 5498 5499
            break;
        }
    }
5500
    ret = 0;
C
Cole Robinson 已提交
5501

5502
 cleanup:
5503 5504
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5505
    return ret;
C
Cole Robinson 已提交
5506 5507 5508
}


5509 5510
static int testStorageVolumeTypeForPool(int pooltype)
{
C
Cole Robinson 已提交
5511

5512
    switch (pooltype) {
C
Cole Robinson 已提交
5513 5514 5515 5516 5517 5518 5519 5520 5521 5522
        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
5523
testStorageVolGetInfo(virStorageVolPtr vol,
5524 5525
                      virStorageVolInfoPtr info)
{
5526 5527 5528
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5529
    int ret = -1;
5530

5531
    testDriverLock(privconn);
5532 5533
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5534
    testDriverUnlock(privconn);
5535 5536

    if (privpool == NULL) {
5537
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5538
        goto cleanup;
5539 5540 5541 5542 5543
    }

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

    if (privvol == NULL) {
5544 5545 5546
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5547
        goto cleanup;
5548 5549 5550
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5551 5552
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5553
        goto cleanup;
5554
    }
C
Cole Robinson 已提交
5555 5556 5557

    memset(info, 0, sizeof(*info));
    info->type = testStorageVolumeTypeForPool(privpool->def->type);
5558 5559
    info->capacity = privvol->target.capacity;
    info->allocation = privvol->target.allocation;
5560
    ret = 0;
C
Cole Robinson 已提交
5561

5562
 cleanup:
5563 5564
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5565
    return ret;
C
Cole Robinson 已提交
5566 5567 5568
}

static char *
5569 5570
testStorageVolGetXMLDesc(virStorageVolPtr vol,
                         unsigned int flags)
E
Eric Blake 已提交
5571
{
5572 5573 5574
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5575
    char *ret = NULL;
5576

E
Eric Blake 已提交
5577 5578
    virCheckFlags(0, NULL);

5579
    testDriverLock(privconn);
5580 5581
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5582
    testDriverUnlock(privconn);
5583 5584

    if (privpool == NULL) {
5585
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5586
        goto cleanup;
5587 5588 5589 5590 5591
    }

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

    if (privvol == NULL) {
5592 5593 5594
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5595
        goto cleanup;
5596
    }
C
Cole Robinson 已提交
5597

5598
    if (!virStoragePoolObjIsActive(privpool)) {
5599 5600
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5601
        goto cleanup;
5602 5603
    }

5604
    ret = virStorageVolDefFormat(privpool->def, privvol);
5605

5606
 cleanup:
5607 5608
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5609
    return ret;
C
Cole Robinson 已提交
5610 5611 5612
}

static char *
5613 5614
testStorageVolGetPath(virStorageVolPtr vol)
{
5615 5616 5617
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5618
    char *ret = NULL;
5619

5620
    testDriverLock(privconn);
5621 5622
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5623
    testDriverUnlock(privconn);
5624 5625

    if (privpool == NULL) {
5626
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5627
        goto cleanup;
5628 5629 5630 5631 5632
    }

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

    if (privvol == NULL) {
5633 5634 5635
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5636
        goto cleanup;
5637 5638 5639
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5640 5641
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5642
        goto cleanup;
5643 5644
    }

5645
    ignore_value(VIR_STRDUP(ret, privvol->target.path));
5646

5647
 cleanup:
5648 5649
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5650 5651 5652
    return ret;
}

5653

5654
/* Node device implementations */
5655

5656 5657 5658
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5659
                     unsigned int flags)
5660 5661 5662
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
5663
    size_t i;
5664

E
Eric Blake 已提交
5665 5666
    virCheckFlags(0, -1);

5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681
    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 已提交
5682
                    unsigned int flags)
5683 5684 5685
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
5686
    size_t i;
5687

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

5690 5691 5692 5693 5694
    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)) {
5695
            if (VIR_STRDUP(names[ndevs++], driver->devs.objs[i]->def->name) < 0) {
5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725
                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) {
5726
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5727 5728 5729 5730 5731
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

5732
 cleanup:
5733 5734 5735 5736 5737 5738
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
5739
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5740
                         unsigned int flags)
5741 5742 5743 5744 5745
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5746 5747
    virCheckFlags(0, NULL);

5748 5749 5750 5751 5752
    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
5753 5754 5755
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5756 5757 5758
        goto cleanup;
    }

5759
    ret = virNodeDeviceDefFormat(obj->def);
5760

5761
 cleanup:
5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778
    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) {
5779 5780 5781
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5782 5783 5784 5785
        goto cleanup;
    }

    if (obj->def->parent) {
5786
        ignore_value(VIR_STRDUP(ret, obj->def->parent));
5787
    } else {
5788 5789
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5790 5791
    }

5792
 cleanup:
5793 5794 5795 5796 5797
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

5798

5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812
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) {
5813 5814 5815
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5816 5817 5818 5819 5820 5821 5822
        goto cleanup;
    }

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

5823
 cleanup:
5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843
    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) {
5844 5845 5846
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5847 5848 5849 5850
        goto cleanup;
    }

    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
5851
        if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->type)) < 0)
5852 5853 5854 5855
            goto cleanup;
    }
    ret = ncaps;

5856
 cleanup:
5857 5858 5859 5860 5861 5862 5863 5864 5865 5866
    if (obj)
        virNodeDeviceObjUnlock(obj);
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
}

5867 5868 5869
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
E
Eric Blake 已提交
5870
                        unsigned int flags)
5871 5872 5873 5874 5875 5876 5877 5878 5879
{
    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 已提交
5880 5881
    virCheckFlags(0, NULL);

5882 5883
    testDriverLock(driver);

5884
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, NULL);
5885
    if (def == NULL)
5886 5887 5888
        goto cleanup;

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

5892
    if (virNodeDeviceGetParentHost(&driver->devs,
5893 5894 5895 5896 5897 5898 5899 5900 5901
                                   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);
5902
    if (VIR_STRDUP(def->name, wwpn) < 0)
5903 5904
        goto cleanup;

J
John Ferlan 已提交
5905 5906
    /* Fill in a random 'host' and 'unique_id' value,
     * since this would also come from the backend */
5907 5908 5909 5910 5911
    caps = def->caps;
    while (caps) {
        if (caps->type != VIR_NODE_DEV_CAP_SCSI_HOST)
            continue;

5912
        caps->data.scsi_host.host = virRandomBits(10);
J
John Ferlan 已提交
5913
        caps->data.scsi_host.unique_id = 2;
5914 5915 5916 5917
        caps = caps->next;
    }


5918
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def)))
5919 5920 5921 5922 5923
        goto cleanup;
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
5924
 cleanup:
5925
    testDriverUnlock(driver);
5926
    virNodeDeviceDefFree(def);
5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945
    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) {
5946
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5947 5948 5949
        goto out;
    }

5950
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1)
5951 5952
        goto out;

5953
    if (VIR_STRDUP(parent_name, obj->def->parent) < 0)
5954 5955 5956 5957 5958 5959 5960 5961 5962
        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 */
5963
    if (virNodeDeviceGetParentHost(&driver->devs,
5964 5965 5966 5967 5968 5969 5970 5971 5972 5973
                                   dev->name,
                                   parent_name,
                                   &parent_host) == -1) {
        obj = NULL;
        goto out;
    }

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

5974
 out:
5975 5976 5977 5978 5979 5980 5981 5982
    if (obj)
        virNodeDeviceObjUnlock(obj);
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

5983 5984

/* Domain event implementations */
5985
static int
5986 5987 5988 5989
testConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
5990 5991
{
    testConnPtr driver = conn->privateData;
5992
    int ret = 0;
5993 5994

    testDriverLock(driver);
5995
    if (virDomainEventStateRegister(conn, driver->eventState,
5996 5997
                                    callback, opaque, freecb) < 0)
        ret = -1;
5998 5999 6000 6001 6002
    testDriverUnlock(driver);

    return ret;
}

6003

6004
static int
6005 6006
testConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
6007 6008
{
    testConnPtr driver = conn->privateData;
6009
    int ret = 0;
6010 6011

    testDriverLock(driver);
6012
    if (virDomainEventStateDeregister(conn, driver->eventState,
6013 6014
                                      callback) < 0)
        ret = -1;
6015 6016 6017 6018 6019
    testDriverUnlock(driver);

    return ret;
}

6020 6021

static int
6022 6023 6024 6025 6026 6027
testConnectDomainEventRegisterAny(virConnectPtr conn,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback callback,
                                  void *opaque,
                                  virFreeCallback freecb)
6028 6029 6030 6031 6032
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
6033
    if (virDomainEventStateRegisterID(conn, driver->eventState,
6034 6035
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
6036
        ret = -1;
6037 6038 6039 6040 6041 6042
    testDriverUnlock(driver);

    return ret;
}

static int
6043 6044
testConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
6045 6046
{
    testConnPtr driver = conn->privateData;
6047
    int ret = 0;
6048 6049

    testDriverLock(driver);
6050
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
6051 6052
                                        callbackID) < 0)
        ret = -1;
6053 6054 6055 6056 6057 6058
    testDriverUnlock(driver);

    return ret;
}


6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070
static int
testConnectNetworkEventRegisterAny(virConnectPtr conn,
                                   virNetworkPtr net,
                                   int eventID,
                                   virConnectNetworkEventGenericCallback callback,
                                   void *opaque,
                                   virFreeCallback freecb)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
6071
    if (virNetworkEventStateRegisterID(conn, driver->eventState,
6072
                                       net, eventID, callback,
6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084
                                       opaque, freecb, &ret) < 0)
        ret = -1;
    testDriverUnlock(driver);

    return ret;
}

static int
testConnectNetworkEventDeregisterAny(virConnectPtr conn,
                                     int callbackID)
{
    testConnPtr driver = conn->privateData;
6085
    int ret = 0;
6086 6087

    testDriverLock(driver);
6088
    if (virObjectEventStateDeregisterID(conn, driver->eventState,
6089 6090
                                        callbackID) < 0)
        ret = -1;
6091 6092 6093 6094 6095 6096
    testDriverUnlock(driver);

    return ret;
}


6097
/* driver must be locked before calling */
6098
static void testObjectEventQueue(testConnPtr driver,
6099
                                 virObjectEventPtr event)
6100
{
6101
    virObjectEventStateQueue(driver->eventState, event);
6102 6103
}

6104

6105 6106 6107
static int testConnectListAllDomains(virConnectPtr conn,
                                     virDomainPtr **domains,
                                     unsigned int flags)
6108 6109 6110 6111
{
    testConnPtr privconn = conn->privateData;
    int ret;

O
Osier Yang 已提交
6112
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
6113 6114

    testDriverLock(privconn);
6115 6116
    ret = virDomainObjListExport(privconn->domains, conn, domains,
                                 NULL, flags);
6117 6118 6119 6120 6121
    testDriverUnlock(privconn);

    return ret;
}

6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134
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) {
6135
        if (VIR_ALLOC_N(*cpumap, 1) < 0)
6136 6137 6138 6139 6140 6141 6142 6143 6144
            goto cleanup;
        *cpumap[0] = 0x15;
    }

    if (online)
        *online = 3;

    ret = 8;

6145
 cleanup:
6146 6147 6148 6149
    testDriverUnlock(privconn);
    return ret;
}

6150 6151 6152 6153 6154 6155 6156 6157 6158 6159
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

6160
    if (VIR_STRDUP(ret, "image/png") < 0)
6161 6162
        return NULL;

6163
    if (virFDStreamOpenFile(st, PKGDATADIR "/libvirtLogo.png", 0, 0, O_RDONLY) < 0)
6164 6165 6166 6167 6168
        VIR_FREE(ret);

    return ret;
}

6169 6170 6171 6172 6173 6174 6175 6176 6177
static int
testConnectGetCPUModelNames(virConnectPtr conn ATTRIBUTE_UNUSED,
                            const char *arch,
                            char ***models,
                            unsigned int flags)
{
    virCheckFlags(0, -1);
    return cpuGetModels(arch, models);
}
6178

C
Cole Robinson 已提交
6179 6180 6181 6182 6183
static int
testDomainManagedSave(virDomainPtr dom, unsigned int flags)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr vm = NULL;
6184
    virObjectEventPtr event = NULL;
C
Cole Robinson 已提交
6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212
    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);
6213
    event = virDomainEventLifecycleNewFromObj(vm,
C
Cole Robinson 已提交
6214 6215 6216 6217 6218
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
    vm->hasManagedSave = true;

    ret = 0;
6219
 cleanup:
C
Cole Robinson 已提交
6220 6221 6222 6223
    if (vm)
        virObjectUnlock(vm);
    if (event) {
        testDriverLock(privconn);
6224
        testObjectEventQueue(privconn, event);
C
Cole Robinson 已提交
6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249
        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;
6250
 cleanup:
C
Cole Robinson 已提交
6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275
    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;
6276
 cleanup:
C
Cole Robinson 已提交
6277 6278 6279 6280 6281 6282 6283
    if (vm)
        virObjectUnlock(vm);
    testDriverUnlock(privconn);
    return ret;
}


6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327
/*
 * 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);

6328
 cleanup:
6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351
    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);

6352
 cleanup:
6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373
    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);

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

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

6427
 cleanup:
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
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);

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

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

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

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

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

6581 6582 6583
    xml = virDomainSnapshotDefFormat(uuidstr, snap->def,
                                     virDomainDefFormatConvertXMLFlags(flags),
                                     0);
6584

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

6606
 cleanup:
6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624
    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 已提交
6625
    if (!testSnapObjFromSnapshot(vm, snapshot))
6626 6627 6628 6629
        goto cleanup;

    ret = 1;

6630
 cleanup:
6631 6632 6633 6634 6635
    if (vm)
        virObjectUnlock(vm);
    return ret;
}

6636 6637 6638 6639 6640 6641
static int
testDomainSnapshotAlignDisks(virDomainObjPtr vm,
                             virDomainSnapshotDefPtr def,
                             unsigned int flags)
{
    int align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL;
E
Eric Blake 已提交
6642
    bool align_match = true;
6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675

    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;
6676
    virObjectEventPtr event = NULL;
6677
    char *xml = NULL;
6678 6679
    bool update_current = true;
    bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE;
6680 6681 6682 6683 6684 6685 6686 6687
    unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;

    /*
     * DISK_ONLY: Not implemented yet
     * REUSE_EXT: Not implemented yet
     *
     * NO_METADATA: Explicitly not implemented
     *
6688
     * REDEFINE + CURRENT: Implemented
6689 6690 6691 6692 6693 6694
     * HALT: Implemented
     * QUIESCE: Nothing to do
     * ATOMIC: Nothing to do
     * LIVE: Nothing to do
     */
    virCheckFlags(
6695 6696
        VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
        VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT |
6697 6698 6699 6700 6701
        VIR_DOMAIN_SNAPSHOT_CREATE_HALT |
        VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE |
        VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC |
        VIR_DOMAIN_SNAPSHOT_CREATE_LIVE, NULL);

6702 6703 6704 6705 6706
    if ((redefine && !(flags & VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT)))
        update_current = false;
    if (redefine)
        parse_flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;

6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722
    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;

6723
    if (redefine) {
C
Cole Robinson 已提交
6724 6725
        if (virDomainSnapshotRedefinePrep(domain, vm, &def, &snap,
                                          &update_current, flags) < 0)
6726 6727 6728 6729 6730 6731 6732
            goto cleanup;
    } else {
        if (!(def->dom = virDomainDefCopy(vm->def,
                                          privconn->caps,
                                          privconn->xmlopt,
                                          true)))
            goto cleanup;
6733

6734
        if (testDomainSnapshotAlignDisks(vm, def, flags) < 0)
6735 6736 6737
            goto cleanup;
    }

6738 6739 6740 6741
    if (!snap) {
        if (!(snap = virDomainSnapshotAssignDef(vm->snapshots, def)))
            goto cleanup;
        def = NULL;
6742 6743
    }

6744 6745 6746 6747 6748 6749 6750 6751 6752 6753
    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);
6754
            event = virDomainEventLifecycleNewFromObj(vm, VIR_DOMAIN_EVENT_STOPPED,
6755 6756 6757
                                    VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
        }
    }
6758 6759

    snapshot = virGetDomainSnapshot(domain, snap->def->name);
6760
 cleanup:
6761 6762 6763 6764
    VIR_FREE(xml);
    if (vm) {
        if (snapshot) {
            virDomainSnapshotObjPtr other;
6765 6766
            if (update_current)
                vm->current_snapshot = snap;
6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777
            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);
6778
        testObjectEventQueue(privconn, event);
6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822
        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;

6823
    if (rep->err < 0)
6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865
        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) {
6866
            if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY)
6867 6868 6869 6870 6871 6872 6873 6874 6875 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
                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;
6910
 cleanup:
6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922
    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;
6923 6924
    virObjectEventPtr event = NULL;
    virObjectEventPtr event2 = NULL;
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 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017
    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);
7018
                event = virDomainEventLifecycleNewFromObj(vm,
7019 7020 7021
                            VIR_DOMAIN_EVENT_STOPPED,
                            VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT);
                if (event)
7022
                    testObjectEventQueue(privconn, event);
7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033
                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. */
7034
                event = virDomainEventLifecycleNewFromObj(vm,
7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047
                                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;
7048
            event = virDomainEventLifecycleNewFromObj(vm,
7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061
                                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 */
7062
                event2 = virDomainEventLifecycleNewFromObj(vm,
7063 7064 7065 7066 7067
                                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 已提交
7068
            virObjectUnref(event);
7069 7070 7071 7072
            event = NULL;

            if (was_stopped) {
                /* Transition 2 */
7073
                event = virDomainEventLifecycleNewFromObj(vm,
7074 7075 7076 7077
                                VIR_DOMAIN_EVENT_STARTED,
                                VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            } else if (was_running) {
                /* Transition 8 */
7078
                event = virDomainEventLifecycleNewFromObj(vm,
7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090
                                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);
7091
            event = virDomainEventLifecycleNewFromObj(vm,
7092 7093 7094 7095 7096 7097 7098 7099 7100 7101
                                    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)
7102
                testObjectEventQueue(privconn, event);
7103
            event = virDomainEventLifecycleNewFromObj(vm,
7104 7105 7106
                            VIR_DOMAIN_EVENT_STARTED,
                            VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT);
            if (paused) {
7107
                event2 = virDomainEventLifecycleNewFromObj(vm,
7108 7109 7110 7111 7112 7113 7114 7115
                                VIR_DOMAIN_EVENT_SUSPENDED,
                                VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT);
            }
        }
    }

    vm->current_snapshot = snap;
    ret = 0;
7116
 cleanup:
7117
    if (event) {
7118
        testObjectEventQueue(privconn, event);
7119
        if (event2)
7120
            testObjectEventQueue(privconn, event2);
C
Cole Robinson 已提交
7121
    } else {
C
Cédric Bosdonnat 已提交
7122
        virObjectUnref(event2);
7123 7124 7125 7126 7127 7128 7129 7130
    }
    virObjectUnlock(vm);
    testDriverUnlock(privconn);

    return ret;
}


7131

7132
static virHypervisorDriver testHypervisorDriver = {
7133
    .name = "Test",
7134 7135 7136
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
7137
    .connectGetHostname = testConnectGetHostname, /* 0.6.3 */
7138
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
7139
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
7140 7141 7142 7143
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
7144
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158
    .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 */
7159 7160
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
7161
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
7162
    .domainRestore = testDomainRestore, /* 0.3.2 */
7163
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
7164
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
7165
    .domainCoreDumpWithFormat = testDomainCoreDumpWithFormat, /* 1.2.3 */
7166
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
7167 7168 7169 7170 7171 7172
    .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 */
7173 7174
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
7175 7176 7177
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
7178
    .domainDefineXMLFlags = testDomainDefineXMLFlags, /* 1.2.12 */
7179
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
7180
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
7181 7182 7183
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
7184 7185 7186 7187
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
7188 7189 7190
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
7191 7192 7193 7194
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
7195 7196 7197
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
7198 7199 7200
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
7201
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
7202
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
7203 7204
    .domainGetMetadata = testDomainGetMetadata, /* 1.1.3 */
    .domainSetMetadata = testDomainSetMetadata, /* 1.1.3 */
7205
    .connectGetCPUModelNames = testConnectGetCPUModelNames, /* 1.1.3 */
7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222
    .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 */
7223 7224 7225
    .domainSnapshotCreateXML = testDomainSnapshotCreateXML, /* 1.1.4 */
    .domainRevertToSnapshot = testDomainRevertToSnapshot, /* 1.1.4 */
    .domainSnapshotDelete = testDomainSnapshotDelete, /* 1.1.4 */
7226

E
Eric Blake 已提交
7227
    .connectBaselineCPU = testConnectBaselineCPU, /* 1.2.0 */
7228 7229 7230
};

static virNetworkDriver testNetworkDriver = {
7231 7232 7233 7234 7235
    .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 */
7236 7237
    .connectNetworkEventRegisterAny = testConnectNetworkEventRegisterAny, /* 1.2.1 */
    .connectNetworkEventDeregisterAny = testConnectNetworkEventDeregisterAny, /* 1.2.1 */
7238 7239 7240 7241
    .networkLookupByUUID = testNetworkLookupByUUID, /* 0.3.2 */
    .networkLookupByName = testNetworkLookupByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreateXML, /* 0.3.2 */
    .networkDefineXML = testNetworkDefineXML, /* 0.3.2 */
7242
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
7243
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
7244
    .networkCreate = testNetworkCreate, /* 0.3.2 */
7245 7246 7247 7248 7249 7250 7251
    .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 */
7252 7253
};

L
Laine Stump 已提交
7254
static virInterfaceDriver testInterfaceDriver = {
7255 7256 7257 7258 7259 7260
    .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 */
7261 7262 7263 7264 7265 7266
    .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 */
7267 7268 7269
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
7270 7271 7272
};


7273
static virStorageDriver testStorageDriver = {
7274 7275 7276 7277 7278 7279
    .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 */
7280 7281 7282
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
7283 7284
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
7285 7286
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
7287
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
7288 7289 7290 7291 7292 7293 7294
    .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 */
7295
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
7296 7297 7298
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

7299 7300 7301 7302 7303 7304 7305 7306 7307
    .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 */
7308 7309
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
7310 7311
};

7312
static virNodeDeviceDriver testNodeDeviceDriver = {
7313 7314 7315 7316 7317 7318 7319 7320 7321
    .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 */
7322 7323
};

7324 7325 7326 7327 7328 7329 7330 7331
static virConnectDriver testConnectDriver = {
    .hypervisorDriver = &testHypervisorDriver,
    .interfaceDriver = &testInterfaceDriver,
    .networkDriver = &testNetworkDriver,
    .nodeDeviceDriver = &testNodeDeviceDriver,
    .nwfilterDriver = NULL,
    .secretDriver = NULL,
    .storageDriver = &testStorageDriver,
7332 7333
};

7334 7335 7336 7337 7338 7339 7340 7341
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
7342 7343
    return virRegisterConnectDriver(&testConnectDriver,
                                    false);
7344
}