test_driver.c 157.0 KB
Newer Older
1 2 3
/*
 * test.c: A "mock" hypervisor for use by application unit tests
 *
4
 * Copyright (C) 2006-2012 Red Hat, Inc.
5
 * Copyright (C) 2006 Daniel P. Berrange
6
 *
7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
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

34 35

#include "virterror_internal.h"
36
#include "datatypes.h"
37
#include "test_driver.h"
38
#include "buf.h"
39
#include "util.h"
40
#include "uuid.h"
41
#include "capabilities.h"
42
#include "memory.h"
43
#include "network_conf.h"
L
Laine Stump 已提交
44
#include "interface_conf.h"
45
#include "domain_conf.h"
46
#include "domain_event.h"
C
Cole Robinson 已提交
47
#include "storage_conf.h"
48
#include "node_device_conf.h"
49
#include "xml.h"
50
#include "threads.h"
51
#include "logging.h"
E
Eric Blake 已提交
52
#include "virfile.h"
53
#include "virtypedparam.h"
54
#include "virrandom.h"
55

56 57
#define VIR_FROM_THIS VIR_FROM_TEST

58 59 60 61 62 63 64 65 66
/* Driver specific info to carry with a domain */
struct _testDomainObjPrivate {
    virVcpuInfoPtr vcpu_infos;

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

67 68 69 70 71 72 73 74 75 76 77
#define MAX_CPUS 128

struct _testCell {
    unsigned long mem;
    int numCpus;
    int cpus[MAX_CPUS];
};
typedef struct _testCell testCell;
typedef struct _testCell *testCellPtr;

#define MAX_CELLS 128
78

79
struct _testConn {
80
    virMutex lock;
81

E
Eric Blake 已提交
82
    char *path;
83
    int nextDomID;
84
    virCapsPtr caps;
85
    virNodeInfo nodeInfo;
86
    virDomainObjList domains;
87
    virNetworkObjList networks;
L
Laine Stump 已提交
88
    virInterfaceObjList ifaces;
89 90
    bool transaction_running;
    virInterfaceObjList backupIfaces;
C
Cole Robinson 已提交
91
    virStoragePoolObjList pools;
92
    virNodeDeviceObjList devs;
93 94
    int numCells;
    testCell cells[MAX_CELLS];
95

96
    virDomainEventStatePtr domainEventState;
97 98 99
};
typedef struct _testConn testConn;
typedef struct _testConn *testConnPtr;
100

101
#define TEST_MODEL "i686"
102
#define TEST_MODEL_WORDSIZE 32
103
#define TEST_EMULATOR "/usr/bin/test-hv"
104

105
static const virNodeInfo defaultNodeInfo = {
106
    TEST_MODEL,
107 108 109 110 111 112 113
    1024*1024*3, /* 3 GB */
    16,
    1400,
    2,
    2,
    2,
    2,
114 115
};

116

117
#define testError(code, ...)                                      \
118
        virReportErrorHelper(VIR_FROM_TEST, code, __FILE__,       \
119
                             __FUNCTION__, __LINE__, __VA_ARGS__)
120

121 122 123 124 125
static int testClose(virConnectPtr conn);
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event);


126 127
static void testDriverLock(testConnPtr driver)
{
128
    virMutexLock(&driver->lock);
129 130 131 132
}

static void testDriverUnlock(testConnPtr driver)
{
133
    virMutexUnlock(&driver->lock);
134 135
}

136 137 138 139 140 141 142 143 144 145 146 147 148 149
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 已提交
150
    VIR_FREE(priv->vcpu_infos);
151 152 153 154 155
    VIR_FREE(priv->cpumaps);
    VIR_FREE(priv);
}


156 157 158 159 160
static int testDefaultConsoleType(const char *ostype ATTRIBUTE_UNUSED)
{
    return VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
}

161 162
static virCapsPtr
testBuildCapabilities(virConnectPtr conn) {
163
    testConnPtr privconn = conn->privateData;
164 165 166 167
    virCapsPtr caps;
    virCapsGuestPtr guest;
    const char *const guest_types[] = { "hvm", "xen" };
    int i;
168

169 170
    if ((caps = virCapabilitiesNew(TEST_MODEL, 0, 0)) == NULL)
        goto no_memory;
171

172 173
    caps->defaultConsoleTargetType = testDefaultConsoleType;

174 175 176 177
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
        goto no_memory;
    if (virCapabilitiesAddHostFeature(caps ,"nonpae") < 0)
        goto no_memory;
178

179 180 181 182
    for (i = 0; i < privconn->numCells; i++) {
        if (virCapabilitiesAddHostNUMACell(caps, i, privconn->cells[i].numCpus,
                                           privconn->cells[i].cpus) < 0)
            goto no_memory;
183 184
    }

185 186 187 188 189 190 191 192 193 194
    for (i = 0; i < ARRAY_CARDINALITY(guest_types) ; i++) {
        if ((guest = virCapabilitiesAddGuest(caps,
                                             guest_types[i],
                                             TEST_MODEL,
                                             TEST_MODEL_WORDSIZE,
                                             TEST_EMULATOR,
                                             NULL,
                                             0,
                                             NULL)) == NULL)
            goto no_memory;
195

196 197 198 199 200 201 202
        if (virCapabilitiesAddGuestDomain(guest,
                                          "test",
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
            goto no_memory;
203

204 205 206 207
        if (virCapabilitiesAddGuestFeature(guest, "pae", 1, 1) == NULL)
            goto no_memory;
        if (virCapabilitiesAddGuestFeature(guest ,"nonpae", 1, 1) == NULL)
            goto no_memory;
208 209
    }

210 211 212
    caps->privateDataAllocFunc = testDomainObjPrivateAlloc;
    caps->privateDataFreeFunc = testDomainObjPrivateFree;

213 214 215 216 217 218 219 220
    caps->host.secModel.model = strdup("testSecurity");
    if (!caps->host.secModel.model)
        goto no_memory;

    caps->host.secModel.doi = strdup("");
    if (!caps->host.secModel.doi)
        goto no_memory;

221
    return caps;
222

223
no_memory:
224
    virReportOOMError();
225 226
    virCapabilitiesFree(caps);
    return NULL;
227 228
}

229

230 231 232
static const char *defaultDomainXML =
"<domain type='test'>"
"  <name>test</name>"
233
"  <uuid>6695eb01-f6a4-8304-79aa-97f2502e193f</uuid>"
234 235 236 237 238 239 240
"  <memory>8388608</memory>"
"  <currentMemory>2097152</currentMemory>"
"  <vcpu>2</vcpu>"
"  <os>"
"    <type>hvm</type>"
"  </os>"
"</domain>";
241 242


243 244 245
static const char *defaultNetworkXML =
"<network>"
"  <name>default</name>"
246
"  <uuid>dd8fe884-6c02-601e-7551-cca97df1c5df</uuid>"
247 248 249 250 251 252 253 254
"  <bridge name='virbr0' />"
"  <forward/>"
"  <ip address='192.168.122.1' netmask='255.255.255.0'>"
"    <dhcp>"
"      <range start='192.168.122.2' end='192.168.122.254' />"
"    </dhcp>"
"  </ip>"
"</network>";
255

L
Laine Stump 已提交
256 257 258 259 260 261 262 263 264 265 266
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 已提交
267 268 269
static const char *defaultPoolXML =
"<pool type='dir'>"
"  <name>default-pool</name>"
270
"  <uuid>dfe224cb-28fb-8dd0-c4b2-64eb3f0f4566</uuid>"
C
Cole Robinson 已提交
271 272 273 274 275
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
"</pool>";

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
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";

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
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>";

317
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
C
Cole Robinson 已提交
318 319
static const unsigned long long defaultPoolAlloc = 0;

320
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool);
321
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
322

323
static char *
324
testDomainGenerateIfname(virDomainDefPtr domdef) {
325 326 327 328 329 330 331 332
    int maxif = 1024;
    int ifctr, i;

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

        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0) {
333
            virReportOOMError();
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
            return NULL;
        }

        /* Generate network interface names */
        for (i = 0 ; i < domdef->nnets ; i++) {
            if (domdef->nets[i]->ifname &&
                STREQ (domdef->nets[i]->ifname, ifname)) {
                found = 1;
                break;
            }
        }

        if (!found)
            return ifname;
    }

350
    testError(VIR_ERR_INTERNAL_ERROR,
351 352 353 354
              _("Exceeded max iface limit %d"), maxif);
    return NULL;
}

355
static int
356
testDomainGenerateIfnames(virDomainDefPtr domdef)
357 358 359 360 361 362 363 364
{
    int i = 0;

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

365
        ifname = testDomainGenerateIfname(domdef);
366
        if (!ifname)
367
            return -1;
368 369 370 371

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

372
    return 0;
373 374
}

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
/* Helper to update info for a single VCPU */
static int
testDomainUpdateVCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
                     virDomainObjPtr dom,
                     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);
    int j;

    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) {
            if (dom->def->cpumask[j]) {
                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
testDomainUpdateVCPUs(virConnectPtr conn,
                      virDomainObjPtr dom,
                      int nvcpus,
                      unsigned int clear_all)
{
    testConnPtr privconn = conn->privateData;
    testDomainObjPrivatePtr privdata = dom->privateData;
    int i, ret = -1;
    int cpumaplen, maxcpu;

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

    if (VIR_REALLOC_N(privdata->vcpu_infos, nvcpus) < 0) {
439
        virReportOOMError();
440 441 442 443
        goto cleanup;
    }

    if (VIR_REALLOC_N(privdata->cpumaps, nvcpus * cpumaplen) < 0) {
444
        virReportOOMError();
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
        goto cleanup;
    }

    /* Set running VCPU and cpumap state */
    if (clear_all) {
        for (i = 0; i < nvcpus; ++i)
            if (testDomainUpdateVCPU(conn, dom, i, cpumaplen, maxcpu) < 0)
                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)
            if (testDomainUpdateVCPU(conn, dom, i, cpumaplen, maxcpu) < 0)
                goto cleanup;
    }

461
    dom->def->vcpus = nvcpus;
462 463 464 465 466
    ret = 0;
cleanup:
    return ret;
}

467 468
static void
testDomainShutdownState(virDomainPtr domain,
J
Jiri Denemark 已提交
469 470
                        virDomainObjPtr privdom,
                        virDomainShutoffReason reason)
471 472 473 474 475 476 477
{
    if (privdom->newDef) {
        virDomainDefFree(privdom->def);
        privdom->def = privdom->newDef;
        privdom->newDef = NULL;
    }

J
Jiri Denemark 已提交
478
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF, reason);
479 480 481 482 483
    privdom->def->id = -1;
    if (domain)
        domain->id = -1;
}

484
/* Set up domain runtime state */
485 486
static int
testDomainStartState(virConnectPtr conn,
J
Jiri Denemark 已提交
487 488
                     virDomainObjPtr dom,
                     virDomainRunningReason reason)
489 490
{
    testConnPtr privconn = conn->privateData;
491
    int ret = -1;
492

493 494 495
    if (testDomainUpdateVCPUs(conn, dom, dom->def->vcpus, 1) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
496
    virDomainObjSetState(dom, VIR_DOMAIN_RUNNING, reason);
497 498
    dom->def->id = privconn->nextDomID++;

499
    if (virDomainObjSetDefTransient(privconn->caps, dom, false) < 0) {
500 501 502
        goto cleanup;
    }

503 504
    ret = 0;
cleanup:
505
    if (ret < 0)
J
Jiri Denemark 已提交
506
        testDomainShutdownState(NULL, dom, VIR_DOMAIN_SHUTOFF_FAILED);
507
    return ret;
508
}
509

510
static int testOpenDefault(virConnectPtr conn) {
511
    int u;
512
    testConnPtr privconn;
513 514 515 516
    virDomainDefPtr domdef = NULL;
    virDomainObjPtr domobj = NULL;
    virNetworkDefPtr netdef = NULL;
    virNetworkObjPtr netobj = NULL;
L
Laine Stump 已提交
517 518
    virInterfaceDefPtr interfacedef = NULL;
    virInterfaceObjPtr interfaceobj = NULL;
C
Cole Robinson 已提交
519 520
    virStoragePoolDefPtr pooldef = NULL;
    virStoragePoolObjPtr poolobj = NULL;
521 522
    virNodeDeviceDefPtr nodedef = NULL;
    virNodeDeviceObjPtr nodeobj = NULL;
523

524
    if (VIR_ALLOC(privconn) < 0) {
525
        virReportOOMError();
526 527
        return VIR_DRV_OPEN_ERROR;
    }
528
    if (virMutexInit(&privconn->lock) < 0) {
529
        testError(VIR_ERR_INTERNAL_ERROR,
530 531 532 533 534
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

535
    testDriverLock(privconn);
536
    conn->privateData = privconn;
537

538 539 540
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

541
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
542

543
    /* Numa setup */
544 545 546 547 548 549 550 551 552
    privconn->numCells = 2;
    for (u = 0; u < 2; ++u) {
        privconn->cells[u].numCpus = 8;
        privconn->cells[u].mem = (u + 1) * 2048 * 1024;
    }
    for (u = 0 ; u < 16 ; u++) {
        privconn->cells[u % 2].cpus[(u / 2)] = u;
    }

553 554 555 556 557
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;

    privconn->nextDomID = 1;

M
Matthias Bolte 已提交
558 559
    if (!(domdef = virDomainDefParseString(privconn->caps, defaultDomainXML,
                                           1 << VIR_DOMAIN_VIRT_TEST,
560
                                           VIR_DOMAIN_XML_INACTIVE)))
561
        goto error;
M
Matthias Bolte 已提交
562

563
    if (testDomainGenerateIfnames(domdef) < 0)
564
        goto error;
565
    if (!(domobj = virDomainAssignDef(privconn->caps,
566
                                      &privconn->domains, domdef, false)))
567 568
        goto error;
    domdef = NULL;
569

570
    domobj->persistent = 1;
J
Jiri Denemark 已提交
571
    if (testDomainStartState(conn, domobj, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
572 573 574 575
        virDomainObjUnlock(domobj);
        goto error;
    }

576
    virDomainObjUnlock(domobj);
577

578
    if (!(netdef = virNetworkDefParseString(defaultNetworkXML)))
579
        goto error;
580
    if (!(netobj = virNetworkAssignDef(&privconn->networks, netdef))) {
581 582 583 584 585
        virNetworkDefFree(netdef);
        goto error;
    }
    netobj->active = 1;
    netobj->persistent = 1;
586
    virNetworkObjUnlock(netobj);
587

588
    if (!(interfacedef = virInterfaceDefParseString(defaultInterfaceXML)))
L
Laine Stump 已提交
589
        goto error;
590
    if (!(interfaceobj = virInterfaceAssignDef(&privconn->ifaces, interfacedef))) {
L
Laine Stump 已提交
591 592 593 594 595 596
        virInterfaceDefFree(interfacedef);
        goto error;
    }
    interfaceobj->active = 1;
    virInterfaceObjUnlock(interfaceobj);

597
    if (!(pooldef = virStoragePoolDefParseString(defaultPoolXML)))
C
Cole Robinson 已提交
598 599
        goto error;

600
    if (!(poolobj = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
601 602 603 604
                                               pooldef))) {
        virStoragePoolDefFree(pooldef);
        goto error;
    }
605

606
    if (testStoragePoolObjSetDefaults(poolobj) == -1) {
607
        virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
608
        goto error;
609
    }
C
Cole Robinson 已提交
610
    poolobj->active = 1;
611
    virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
612

613
    /* Init default node device */
614
    if (!(nodedef = virNodeDeviceDefParseString(defaultNodeXML, 0, NULL)))
615
        goto error;
616
    if (!(nodeobj = virNodeDeviceAssignDef(&privconn->devs,
617 618 619 620 621 622
                                           nodedef))) {
        virNodeDeviceDefFree(nodedef);
        goto error;
    }
    virNodeDeviceObjUnlock(nodeobj);

623
    testDriverUnlock(privconn);
624

625 626 627
    return VIR_DRV_OPEN_SUCCESS;

error:
628
    virDomainObjListDeinit(&privconn->domains);
629
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
630
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
631
    virStoragePoolObjListFree(&privconn->pools);
632
    virNodeDeviceObjListFree(&privconn->devs);
633
    virCapabilitiesFree(privconn->caps);
634
    testDriverUnlock(privconn);
635
    conn->privateData = NULL;
636
    VIR_FREE(privconn);
637
    virDomainDefFree(domdef);
638
    return VIR_DRV_OPEN_ERROR;
639 640 641 642
}


static char *testBuildFilename(const char *relativeTo,
643 644 645 646
                               const char *filename) {
    char *offset;
    int baseLen;
    if (!filename || filename[0] == '\0')
647
        return NULL;
648 649 650
    if (filename[0] == '/')
        return strdup(filename);

651
    offset = strrchr(relativeTo, '/');
652
    if ((baseLen = (offset-relativeTo+1))) {
653
        char *absFile;
C
Chris Lalancette 已提交
654 655
        int totalLen = baseLen + strlen(filename) + 1;
        if (VIR_ALLOC_N(absFile, totalLen) < 0)
656
            return NULL;
C
Chris Lalancette 已提交
657 658 659 660
        if (virStrncpy(absFile, relativeTo, baseLen, totalLen) == NULL) {
            VIR_FREE(absFile);
            return NULL;
        }
661 662 663 664 665
        strcat(absFile, filename);
        return absFile;
    } else {
        return strdup(filename);
    }
666 667
}

668
static int testOpenVolumesForPool(xmlDocPtr xml,
669 670 671 672 673 674 675
                                  xmlXPathContextPtr ctxt,
                                  const char *file,
                                  virStoragePoolObjPtr pool,
                                  int poolidx) {
    char *vol_xpath;
    int i, ret, func_ret = -1;
    xmlNodePtr *vols = NULL;
676
    virStorageVolDefPtr def = NULL;
677 678 679

    /* Find storage volumes */
    if (virAsprintf(&vol_xpath, "/node/pool[%d]/volume", poolidx) < 0) {
680
        virReportOOMError();
681 682 683
        goto error;
    }

684
    ret = virXPathNodeSet(vol_xpath, ctxt, &vols);
685 686 687 688 689 690 691 692 693 694 695
    VIR_FREE(vol_xpath);
    if (ret < 0) {
        goto error;
    }

    for (i = 0 ; i < ret ; i++) {
        char *relFile = virXMLPropString(vols[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
696
                testError(VIR_ERR_INTERNAL_ERROR, "%s",
697 698 699 700
                          _("resolving volume filename"));
                goto error;
            }

701
            def = virStorageVolDefParseFile(pool->def, absFile);
702 703 704 705
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
706
            if ((def = virStorageVolDefParseNode(pool->def, xml,
707 708 709 710 711 712 713
                                                 vols[i])) == NULL) {
                goto error;
            }
        }

        if (VIR_REALLOC_N(pool->volumes.objs,
                          pool->volumes.count+1) < 0) {
714
            virReportOOMError();
715 716 717
            goto error;
        }

718 719 720 721 722 723 724
        if (def->target.path == NULL) {
            if (virAsprintf(&def->target.path, "%s/%s",
                            pool->def->target.path,
                            def->name) == -1) {
                virReportOOMError();
                goto error;
            }
725 726 727
        }

        if (def->key == NULL) {
728 729 730 731 732
            def->key = strdup(def->target.path);
            if (def->key == NULL) {
                virReportOOMError();
                goto error;
            }
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
        }

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

        pool->volumes.objs[pool->volumes.count++] = def;
        def = NULL;
    }

    func_ret = 0;
error:
    virStorageVolDefFree(def);
    VIR_FREE(vols);
    return func_ret;
}

750
static int testOpenFromFile(virConnectPtr conn,
751
                            const char *file) {
752
    int i, ret;
753 754
    long l;
    char *str;
755
    xmlDocPtr xml = NULL;
756 757
    xmlNodePtr *domains = NULL, *networks = NULL, *ifaces = NULL,
               *pools = NULL, *devs = NULL;
758 759
    xmlXPathContextPtr ctxt = NULL;
    virNodeInfoPtr nodeInfo;
760
    virNetworkObjPtr net;
L
Laine Stump 已提交
761
    virInterfaceObjPtr iface;
762
    virDomainObjPtr dom;
763 764
    testConnPtr privconn;
    if (VIR_ALLOC(privconn) < 0) {
765
        virReportOOMError();
766 767
        return VIR_DRV_OPEN_ERROR;
    }
768
    if (virMutexInit(&privconn->lock) < 0) {
769
        testError(VIR_ERR_INTERNAL_ERROR,
770 771 772 773 774
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

775
    testDriverLock(privconn);
776
    conn->privateData = privconn;
777

778 779 780
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

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

784
    if (!(xml = virXMLParseFileCtxt(file, &ctxt))) {
785
        goto error;
786 787
    }

788
    if (!xmlStrEqual(ctxt->node->name, BAD_CAST "node")) {
789
        testError(VIR_ERR_XML_ERROR, "%s",
790
                  _("Root element is not 'node'"));
791
        goto error;
792 793
    }

794
    privconn->nextDomID = 1;
795
    privconn->numCells = 0;
E
Eric Blake 已提交
796 797
    if ((privconn->path = strdup(file)) == NULL) {
        virReportOOMError();
C
Chris Lalancette 已提交
798 799
        goto error;
    }
800 801 802
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    nodeInfo = &privconn->nodeInfo;
803
    ret = virXPathLong("string(/node/cpu/nodes[1])", ctxt, &l);
804 805 806
    if (ret == 0) {
        nodeInfo->nodes = l;
    } else if (ret == -2) {
807 808
        testError(VIR_ERR_XML_ERROR, "%s",
                  _("invalid node cpu nodes value"));
809
        goto error;
810
    }
811

812
    ret = virXPathLong("string(/node/cpu/sockets[1])", ctxt, &l);
813 814 815
    if (ret == 0) {
        nodeInfo->sockets = l;
    } else if (ret == -2) {
816 817
        testError(VIR_ERR_XML_ERROR, "%s",
                  _("invalid node cpu sockets value"));
818
        goto error;
819
    }
820

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

830
    ret = virXPathLong("string(/node/cpu/threads[1])", ctxt, &l);
831 832 833
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
834 835
        testError(VIR_ERR_XML_ERROR, "%s",
                  _("invalid node cpu threads value"));
836
        goto error;
837
    }
838

839
    nodeInfo->cpus = nodeInfo->cores * nodeInfo->threads * nodeInfo->sockets * nodeInfo->nodes;
840
    ret = virXPathLong("string(/node/cpu/active[1])", ctxt, &l);
841 842
    if (ret == 0) {
        if (l < nodeInfo->cpus) {
843 844
            nodeInfo->cpus = l;
        }
845
    } else if (ret == -2) {
846 847
        testError(VIR_ERR_XML_ERROR, "%s",
                  _("invalid node cpu active value"));
848
        goto error;
849
    }
850
    ret = virXPathLong("string(/node/cpu/mhz[1])", ctxt, &l);
851 852 853
    if (ret == 0) {
        nodeInfo->mhz = l;
    } else if (ret == -2) {
854 855
        testError(VIR_ERR_XML_ERROR, "%s",
                  _("invalid node cpu mhz value"));
856
        goto error;
857 858
    }

859
    str = virXPathString("string(/node/cpu/model[1])", ctxt);
860
    if (str != NULL) {
C
Chris Lalancette 已提交
861
        if (virStrcpyStatic(nodeInfo->model, str) == NULL) {
862
            testError(VIR_ERR_INTERNAL_ERROR,
C
Chris Lalancette 已提交
863 864 865 866
                      _("Model %s too big for destination"), str);
            VIR_FREE(str);
            goto error;
        }
867
        VIR_FREE(str);
868 869
    }

870
    ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
871 872 873
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
874 875
        testError(VIR_ERR_XML_ERROR, "%s",
                  _("invalid node memory value"));
876
        goto error;
877
    }
878

879
    ret = virXPathNodeSet("/node/domain", ctxt, &domains);
880
    if (ret < 0) {
881
        goto error;
882
    }
883

884
    for (i = 0 ; i < ret ; i++) {
885 886 887 888 889 890
        virDomainDefPtr def;
        char *relFile = virXMLPropString(domains[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
891
                testError(VIR_ERR_INTERNAL_ERROR, "%s", _("resolving domain filename"));
892 893
                goto error;
            }
894
            def = virDomainDefParseFile(privconn->caps, absFile,
M
Matthias Bolte 已提交
895
                                        1 << VIR_DOMAIN_VIRT_TEST,
896
                                        VIR_DOMAIN_XML_INACTIVE);
897
            VIR_FREE(absFile);
898 899 900
            if (!def)
                goto error;
        } else {
901
            if ((def = virDomainDefParseNode(privconn->caps, xml, domains[i],
M
Matthias Bolte 已提交
902
                                             1 << VIR_DOMAIN_VIRT_TEST,
903
                                             VIR_DOMAIN_XML_INACTIVE)) == NULL)
904 905 906
                goto error;
        }

907
        if (testDomainGenerateIfnames(def) < 0 ||
908
            !(dom = virDomainAssignDef(privconn->caps,
909
                                       &privconn->domains, def, false))) {
910
            virDomainDefFree(def);
911 912
            goto error;
        }
913

914
        dom->persistent = 1;
J
Jiri Denemark 已提交
915
        if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
916 917 918 919
            virDomainObjUnlock(dom);
            goto error;
        }

920
        virDomainObjUnlock(dom);
921
    }
922
    VIR_FREE(domains);
923

924
    ret = virXPathNodeSet("/node/network", ctxt, &networks);
925 926 927 928 929 930 931 932 933
    if (ret < 0) {
        goto error;
    }
    for (i = 0 ; i < ret ; i++) {
        virNetworkDefPtr def;
        char *relFile = virXMLPropString(networks[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
934
            if (!absFile) {
935
                testError(VIR_ERR_INTERNAL_ERROR, "%s", _("resolving network filename"));
936 937
                goto error;
            }
938

939
            def = virNetworkDefParseFile(absFile);
940
            VIR_FREE(absFile);
941 942 943
            if (!def)
                goto error;
        } else {
944
            if ((def = virNetworkDefParseNode(xml, networks[i])) == NULL)
945
                goto error;
946
        }
947
        if (!(net = virNetworkAssignDef(&privconn->networks,
948 949 950
                                        def))) {
            virNetworkDefFree(def);
            goto error;
951
        }
952
        net->persistent = 1;
953
        net->active = 1;
954
        virNetworkObjUnlock(net);
955
    }
956
    VIR_FREE(networks);
957

L
Laine Stump 已提交
958
    /* Parse interface definitions */
959
    ret = virXPathNodeSet("/node/interface", ctxt, &ifaces);
L
Laine Stump 已提交
960 961 962 963 964 965 966 967 968 969
    if (ret < 0) {
        goto error;
    }
    for (i = 0 ; i < ret ; i++) {
        virInterfaceDefPtr def;
        char *relFile = virXMLPropString(ifaces[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
970
                testError(VIR_ERR_INTERNAL_ERROR, "%s", _("resolving interface filename"));
L
Laine Stump 已提交
971 972 973
                goto error;
            }

974
            def = virInterfaceDefParseFile(absFile);
L
Laine Stump 已提交
975 976 977 978
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
979
            if ((def = virInterfaceDefParseNode(xml, ifaces[i])) == NULL)
L
Laine Stump 已提交
980 981
                goto error;
        }
982

983
        if (!(iface = virInterfaceAssignDef(&privconn->ifaces, def))) {
L
Laine Stump 已提交
984 985 986
            virInterfaceDefFree(def);
            goto error;
        }
987 988

        iface->active = 1;
L
Laine Stump 已提交
989 990 991 992
        virInterfaceObjUnlock(iface);
    }
    VIR_FREE(ifaces);

C
Cole Robinson 已提交
993
    /* Parse Storage Pool list */
994
    ret = virXPathNodeSet("/node/pool", ctxt, &pools);
C
Cole Robinson 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005
    if (ret < 0) {
        goto error;
    }
    for (i = 0 ; i < ret ; i++) {
        virStoragePoolDefPtr def;
        virStoragePoolObjPtr pool;
        char *relFile = virXMLPropString(pools[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
1006
                testError(VIR_ERR_INTERNAL_ERROR, "%s",
C
Cole Robinson 已提交
1007 1008 1009 1010
                          _("resolving pool filename"));
                goto error;
            }

1011
            def = virStoragePoolDefParseFile(absFile);
C
Cole Robinson 已提交
1012 1013 1014 1015
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
1016
            if ((def = virStoragePoolDefParseNode(xml,
1017
                                                  pools[i])) == NULL) {
C
Cole Robinson 已提交
1018 1019 1020 1021
                goto error;
            }
        }

1022
        if (!(pool = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
1023 1024 1025 1026 1027
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

1028
        if (testStoragePoolObjSetDefaults(pool) == -1) {
1029
            virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1030
            goto error;
1031
        }
C
Cole Robinson 已提交
1032
        pool->active = 1;
1033 1034

        /* Find storage volumes */
1035
        if (testOpenVolumesForPool(xml, ctxt, file, pool, i+1) < 0) {
1036 1037 1038 1039
            virStoragePoolObjUnlock(pool);
            goto error;
        }

1040
        virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1041
    }
1042
    VIR_FREE(pools);
C
Cole Robinson 已提交
1043

1044
    ret = virXPathNodeSet("/node/device", ctxt, &devs);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
    if (ret < 0) {
        goto error;
    }
    for (i = 0 ; i < ret ; i++) {
        virNodeDeviceDefPtr def;
        virNodeDeviceObjPtr dev;
        char *relFile = virXMLPropString(devs[i], "file");

        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);

            if (!absFile) {
1058
                testError(VIR_ERR_INTERNAL_ERROR, "%s",
1059 1060 1061 1062
                          _("resolving device filename"));
                goto error;
            }

1063
            def = virNodeDeviceDefParseFile(absFile, 0, NULL);
1064 1065 1066 1067
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
1068
            if ((def = virNodeDeviceDefParseNode(xml, devs[i], 0, NULL)) == NULL)
1069 1070
                goto error;
        }
1071
        if (!(dev = virNodeDeviceAssignDef(&privconn->devs, def))) {
1072 1073 1074 1075 1076 1077 1078 1079
            virNodeDeviceDefFree(def);
            goto error;
        }
        virNodeDeviceObjUnlock(dev);
    }
    VIR_FREE(devs);


J
Jim Meyering 已提交
1080
    xmlXPathFreeContext(ctxt);
1081
    xmlFreeDoc(xml);
1082
    testDriverUnlock(privconn);
1083

1084
    return 0;
1085 1086

 error:
J
Jim Meyering 已提交
1087
    xmlXPathFreeContext(ctxt);
1088
    xmlFreeDoc(xml);
1089 1090
    VIR_FREE(domains);
    VIR_FREE(networks);
L
Laine Stump 已提交
1091
    VIR_FREE(ifaces);
C
Cole Robinson 已提交
1092
    VIR_FREE(pools);
1093
    VIR_FREE(devs);
1094
    virDomainObjListDeinit(&privconn->domains);
1095
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1096
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1097
    virStoragePoolObjListFree(&privconn->pools);
E
Eric Blake 已提交
1098
    VIR_FREE(privconn->path);
1099
    testDriverUnlock(privconn);
1100
    VIR_FREE(privconn);
1101
    conn->privateData = NULL;
1102
    return VIR_DRV_OPEN_ERROR;
1103 1104
}

1105

1106
static virDrvOpenStatus testOpen(virConnectPtr conn,
1107
                                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
1108
                                 unsigned int flags)
1109
{
1110
    int ret;
1111
    testConnPtr privconn;
1112

E
Eric Blake 已提交
1113 1114
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1115
    if (!conn->uri)
1116
        return VIR_DRV_OPEN_DECLINED;
1117

1118
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1119
        return VIR_DRV_OPEN_DECLINED;
1120

1121
    /* Remote driver should handle these. */
1122
    if (conn->uri->server)
1123 1124
        return VIR_DRV_OPEN_DECLINED;

1125
    /* From this point on, the connection is for us. */
1126 1127 1128
    if (!conn->uri->path
        || conn->uri->path[0] == '\0'
        || (conn->uri->path[0] == '/' && conn->uri->path[1] == '\0')) {
1129 1130
        testError(VIR_ERR_INVALID_ARG,
                  "%s", _("testOpen: supply a path or use test:///default"));
1131 1132
        return VIR_DRV_OPEN_ERROR;
    }
1133

1134
    if (STREQ(conn->uri->path, "/default"))
1135 1136
        ret = testOpenDefault(conn);
    else
1137
        ret = testOpenFromFile(conn,
1138
                               conn->uri->path);
1139

1140 1141 1142 1143 1144
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

    privconn = conn->privateData;
    testDriverLock(privconn);
1145

1146
    privconn->domainEventState = virDomainEventStateNew();
1147
    if (!privconn->domainEventState) {
1148
        testDriverUnlock(privconn);
1149 1150
        testClose(conn);
        return VIR_DRV_OPEN_ERROR;
1151 1152
    }

1153 1154 1155
    testDriverUnlock(privconn);

    return VIR_DRV_OPEN_SUCCESS;
1156 1157
}

1158
static int testClose(virConnectPtr conn)
1159
{
1160
    testConnPtr privconn = conn->privateData;
1161
    testDriverLock(privconn);
1162
    virCapabilitiesFree(privconn->caps);
1163
    virDomainObjListDeinit(&privconn->domains);
D
Daniel P. Berrange 已提交
1164
    virNodeDeviceObjListFree(&privconn->devs);
1165
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1166
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1167
    virStoragePoolObjListFree(&privconn->pools);
1168
    virDomainEventStateFree(privconn->domainEventState);
E
Eric Blake 已提交
1169
    VIR_FREE(privconn->path);
1170

1171
    testDriverUnlock(privconn);
1172
    virMutexDestroy(&privconn->lock);
1173

1174
    VIR_FREE (privconn);
1175
    conn->privateData = NULL;
1176
    return 0;
1177 1178
}

1179 1180
static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                          unsigned long *hvVer)
1181
{
1182
    *hvVer = 2;
1183
    return 0;
1184 1185
}

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
static int testIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 1;
}

static int testIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 0;
}

1196 1197 1198 1199 1200
static int testIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 1;
}

1201 1202 1203 1204 1205 1206 1207 1208
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1209
{
1210
    testConnPtr privconn = conn->privateData;
1211
    testDriverLock(privconn);
1212
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1213
    testDriverUnlock(privconn);
1214
    return 0;
1215 1216
}

1217
static char *testGetCapabilities (virConnectPtr conn)
1218
{
1219
    testConnPtr privconn = conn->privateData;
1220
    char *xml;
1221
    testDriverLock(privconn);
1222
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL)
1223
        virReportOOMError();
1224
    testDriverUnlock(privconn);
1225
    return xml;
1226 1227
}

1228
static int testNumOfDomains(virConnectPtr conn)
1229
{
1230
    testConnPtr privconn = conn->privateData;
1231
    int count;
1232

1233
    testDriverLock(privconn);
1234
    count = virDomainObjListNumOfDomains(&privconn->domains, 1);
1235
    testDriverUnlock(privconn);
1236

1237
    return count;
1238 1239
}

1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
static int testDomainIsActive(virDomainPtr dom)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virDomainFindByUUID(&privconn->domains, dom->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
1250
        testError(VIR_ERR_NO_DOMAIN, NULL);
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
        goto cleanup;
    }
    ret = virDomainObjIsActive(obj);

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

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

    testDriverLock(privconn);
    obj = virDomainFindByUUID(&privconn->domains, dom->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
1271
        testError(VIR_ERR_NO_DOMAIN, NULL);
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
        goto cleanup;
    }
    ret = obj->persistent;

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

1282 1283 1284 1285 1286
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

1287
static virDomainPtr
1288
testDomainCreateXML(virConnectPtr conn, const char *xml,
1289
                      unsigned int flags)
1290
{
1291
    testConnPtr privconn = conn->privateData;
1292
    virDomainPtr ret = NULL;
1293
    virDomainDefPtr def;
1294
    virDomainObjPtr dom = NULL;
1295
    virDomainEventPtr event = NULL;
1296

1297 1298
    virCheckFlags(0, NULL);

1299
    testDriverLock(privconn);
1300
    if ((def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
1301
                                       1 << VIR_DOMAIN_VIRT_TEST,
1302
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1303
        goto cleanup;
1304

1305 1306 1307
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1308
    if (testDomainGenerateIfnames(def) < 0)
1309
        goto cleanup;
1310
    if (!(dom = virDomainAssignDef(privconn->caps,
1311
                                   &privconn->domains, def, false)))
1312 1313
        goto cleanup;
    def = NULL;
1314

J
Jiri Denemark 已提交
1315
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0)
1316
        goto cleanup;
1317

1318 1319 1320 1321
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1322
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1323
    if (ret)
1324
        ret->id = dom->def->id;
1325 1326

cleanup:
1327 1328
    if (dom)
        virDomainObjUnlock(dom);
1329 1330
    if (event)
        testDomainEventQueue(privconn, event);
1331
    virDomainDefFree(def);
1332
    testDriverUnlock(privconn);
1333
    return ret;
1334 1335 1336
}


1337 1338
static virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                         int id)
1339
{
1340
    testConnPtr privconn = conn->privateData;
1341 1342
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1343

1344 1345 1346 1347 1348
    testDriverLock(privconn);
    dom = virDomainFindByID(&privconn->domains, id);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1349
        testError(VIR_ERR_NO_DOMAIN, NULL);
1350
        goto cleanup;
1351 1352
    }

1353
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1354 1355 1356 1357
    if (ret)
        ret->id = dom->def->id;

cleanup:
1358 1359
    if (dom)
        virDomainObjUnlock(dom);
1360
    return ret;
1361 1362
}

1363 1364
static virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
1365
{
1366
    testConnPtr privconn = conn->privateData;
1367 1368
    virDomainPtr ret = NULL;
    virDomainObjPtr dom ;
1369

1370 1371 1372 1373 1374
    testDriverLock(privconn);
    dom = virDomainFindByUUID(&privconn->domains, uuid);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1375
        testError(VIR_ERR_NO_DOMAIN, NULL);
1376
        goto cleanup;
1377
    }
1378

1379
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1380 1381 1382 1383
    if (ret)
        ret->id = dom->def->id;

cleanup:
1384 1385
    if (dom)
        virDomainObjUnlock(dom);
1386
    return ret;
1387 1388
}

1389 1390
static virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                           const char *name)
1391
{
1392
    testConnPtr privconn = conn->privateData;
1393 1394
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1395

1396 1397 1398 1399 1400
    testDriverLock(privconn);
    dom = virDomainFindByName(&privconn->domains, name);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1401
        testError(VIR_ERR_NO_DOMAIN, NULL);
1402
        goto cleanup;
1403
    }
1404

1405
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1406 1407 1408 1409
    if (ret)
        ret->id = dom->def->id;

cleanup:
1410 1411
    if (dom)
        virDomainObjUnlock(dom);
1412
    return ret;
1413 1414
}

1415 1416 1417
static int testListDomains (virConnectPtr conn,
                            int *ids,
                            int maxids)
1418
{
1419
    testConnPtr privconn = conn->privateData;
1420
    int n;
1421

1422
    testDriverLock(privconn);
1423
    n = virDomainObjListGetActiveIDs(&privconn->domains, ids, maxids);
1424
    testDriverUnlock(privconn);
1425

1426
    return n;
1427 1428
}

1429
static int testDestroyDomain (virDomainPtr domain)
1430
{
1431 1432
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1433
    virDomainEventPtr event = NULL;
1434
    int ret = -1;
1435

1436
    testDriverLock(privconn);
1437 1438 1439 1440
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1441
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1442
        goto cleanup;
1443
    }
1444

J
Jiri Denemark 已提交
1445
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1446 1447 1448
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1449

1450 1451 1452
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1453
        privdom = NULL;
1454
    }
1455 1456 1457

    ret = 0;
cleanup:
1458 1459
    if (privdom)
        virDomainObjUnlock(privdom);
1460 1461
    if (event)
        testDomainEventQueue(privconn, event);
1462
    testDriverUnlock(privconn);
1463
    return ret;
1464 1465
}

1466
static int testResumeDomain (virDomainPtr domain)
1467
{
1468 1469
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1470
    virDomainEventPtr event = NULL;
1471
    int ret = -1;
1472

1473
    testDriverLock(privconn);
1474 1475
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1476
    testDriverUnlock(privconn);
1477 1478

    if (privdom == NULL) {
1479
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1480
        goto cleanup;
1481
    }
1482

J
Jiri Denemark 已提交
1483
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_PAUSED) {
1484
        testError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
1485
                  domain->name);
1486
        goto cleanup;
1487
    }
1488

J
Jiri Denemark 已提交
1489 1490
    virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                         VIR_DOMAIN_RUNNING_UNPAUSED);
1491 1492 1493
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1494 1495 1496
    ret = 0;

cleanup:
1497 1498
    if (privdom)
        virDomainObjUnlock(privdom);
1499 1500 1501 1502 1503
    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1504
    return ret;
1505 1506
}

1507
static int testPauseDomain (virDomainPtr domain)
1508
{
1509 1510
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1511
    virDomainEventPtr event = NULL;
1512
    int ret = -1;
J
Jiri Denemark 已提交
1513
    int state;
1514

1515
    testDriverLock(privconn);
1516 1517
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1518
    testDriverUnlock(privconn);
1519 1520

    if (privdom == NULL) {
1521
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1522
        goto cleanup;
1523
    }
1524

J
Jiri Denemark 已提交
1525 1526
    state = virDomainObjGetState(privdom, NULL);
    if (state == VIR_DOMAIN_SHUTOFF || state == VIR_DOMAIN_PAUSED) {
1527
        testError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
1528
                  domain->name);
1529
        goto cleanup;
1530
    }
1531

J
Jiri Denemark 已提交
1532
    virDomainObjSetState(privdom, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
1533 1534 1535
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1536 1537 1538
    ret = 0;

cleanup:
1539 1540
    if (privdom)
        virDomainObjUnlock(privdom);
1541 1542 1543 1544 1545 1546

    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1547
    return ret;
1548 1549
}

1550 1551
static int testShutdownDomainFlags(virDomainPtr domain,
                                   unsigned int flags)
1552
{
1553 1554
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1555
    virDomainEventPtr event = NULL;
1556
    int ret = -1;
1557

1558 1559
    virCheckFlags(0, -1);

1560
    testDriverLock(privconn);
1561 1562 1563 1564
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1565
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1566
        goto cleanup;
1567
    }
1568

J
Jiri Denemark 已提交
1569
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
1570
        testError(VIR_ERR_INTERNAL_ERROR,
1571
                  _("domain '%s' not running"), domain->name);
1572
        goto cleanup;
1573
    }
1574

J
Jiri Denemark 已提交
1575
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1576 1577 1578
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1579

1580 1581 1582 1583 1584
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
        privdom = NULL;
    }
1585

1586
    ret = 0;
1587
cleanup:
1588 1589
    if (privdom)
        virDomainObjUnlock(privdom);
1590 1591
    if (event)
        testDomainEventQueue(privconn, event);
1592
    testDriverUnlock(privconn);
1593
    return ret;
1594 1595
}

1596 1597 1598 1599 1600
static int testShutdownDomain (virDomainPtr domain)
{
    return testShutdownDomainFlags(domain, 0);
}

1601
/* Similar behaviour as shutdown */
1602 1603
static int testRebootDomain (virDomainPtr domain,
                             unsigned int action ATTRIBUTE_UNUSED)
1604
{
1605 1606
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1607
    virDomainEventPtr event = NULL;
1608
    int ret = -1;
1609

1610
    testDriverLock(privconn);
1611 1612 1613 1614
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1615
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1616
        goto cleanup;
1617
    }
1618

J
Jiri Denemark 已提交
1619 1620 1621
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

1622 1623
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
J
Jiri Denemark 已提交
1624 1625
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1626 1627
        break;

1628
    case VIR_DOMAIN_LIFECYCLE_RESTART:
J
Jiri Denemark 已提交
1629 1630
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1631 1632
        break;

1633
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
J
Jiri Denemark 已提交
1634 1635
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1636 1637
        break;

1638
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
J
Jiri Denemark 已提交
1639 1640
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1641
        break;
1642

1643
    default:
J
Jiri Denemark 已提交
1644 1645
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1646 1647
        break;
    }
1648

J
Jiri Denemark 已提交
1649 1650
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1651 1652 1653
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1654

1655 1656 1657 1658 1659
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1660 1661
    }

1662 1663
    ret = 0;
cleanup:
1664 1665
    if (privdom)
        virDomainObjUnlock(privdom);
1666 1667
    if (event)
        testDomainEventQueue(privconn, event);
1668
    testDriverUnlock(privconn);
1669
    return ret;
1670 1671
}

1672 1673
static int testGetDomainInfo (virDomainPtr domain,
                              virDomainInfoPtr info)
1674
{
1675
    testConnPtr privconn = domain->conn->privateData;
1676
    struct timeval tv;
1677
    virDomainObjPtr privdom;
1678
    int ret = -1;
1679

1680
    testDriverLock(privconn);
1681 1682
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1683
    testDriverUnlock(privconn);
1684 1685

    if (privdom == NULL) {
1686
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1687
        goto cleanup;
1688
    }
1689 1690

    if (gettimeofday(&tv, NULL) < 0) {
1691
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1692
                  "%s", _("getting time of day"));
1693
        goto cleanup;
1694 1695
    }

J
Jiri Denemark 已提交
1696
    info->state = virDomainObjGetState(privdom, NULL);
1697 1698
    info->memory = privdom->def->mem.cur_balloon;
    info->maxMem = privdom->def->mem.max_balloon;
1699 1700
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1701 1702 1703
    ret = 0;

cleanup:
1704 1705
    if (privdom)
        virDomainObjUnlock(privdom);
1706
    return ret;
1707 1708
}

1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
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);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

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

J
Jiri Denemark 已提交
1731
    *state = virDomainObjGetState(privdom, reason);
1732 1733 1734 1735 1736 1737 1738 1739
    ret = 0;

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

1740 1741
#define TEST_SAVE_MAGIC "TestGuestMagic"

1742 1743 1744
static int
testDomainSaveFlags(virDomainPtr domain, const char *path,
                    const char *dxml, unsigned int flags)
1745
{
1746
    testConnPtr privconn = domain->conn->privateData;
1747 1748 1749
    char *xml = NULL;
    int fd = -1;
    int len;
1750
    virDomainObjPtr privdom;
1751
    virDomainEventPtr event = NULL;
1752
    int ret = -1;
1753

1754 1755 1756 1757 1758 1759 1760
    virCheckFlags(0, -1);
    if (dxml) {
        testError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                  _("xml modification unsupported"));
        return -1;
    }

1761
    testDriverLock(privconn);
1762 1763 1764 1765
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1766
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1767
        goto cleanup;
1768
    }
1769

1770
    xml = virDomainDefFormat(privdom->def,
C
Cole Robinson 已提交
1771 1772
                             VIR_DOMAIN_XML_SECURE);

1773
    if (xml == NULL) {
1774
        virReportSystemError(errno,
1775 1776
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
1777
        goto cleanup;
1778
    }
1779 1780

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1781
        virReportSystemError(errno,
1782 1783
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
1784
        goto cleanup;
1785
    }
1786
    len = strlen(xml);
1787
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1788
        virReportSystemError(errno,
1789 1790
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1791
        goto cleanup;
1792
    }
1793
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
1794
        virReportSystemError(errno,
1795 1796
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1797
        goto cleanup;
1798
    }
1799
    if (safewrite(fd, xml, len) < 0) {
1800
        virReportSystemError(errno,
1801 1802
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1803
        goto cleanup;
1804
    }
1805

1806
    if (VIR_CLOSE(fd) < 0) {
1807
        virReportSystemError(errno,
1808 1809
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1810
        goto cleanup;
1811
    }
1812 1813
    fd = -1;

J
Jiri Denemark 已提交
1814
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
1815 1816 1817
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1818

1819 1820 1821
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1822
        privdom = NULL;
1823
    }
1824

1825
    ret = 0;
1826 1827 1828 1829 1830 1831 1832
cleanup:
    VIR_FREE(xml);

    /* Don't report failure in close or unlink, because
     * in either case we're already in a failure scenario
     * and have reported a earlier error */
    if (ret != 0) {
1833
        VIR_FORCE_CLOSE(fd);
1834 1835
        unlink(path);
    }
1836 1837
    if (privdom)
        virDomainObjUnlock(privdom);
1838 1839
    if (event)
        testDomainEventQueue(privconn, event);
1840
    testDriverUnlock(privconn);
1841
    return ret;
1842 1843
}

1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
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)
1856
{
1857
    testConnPtr privconn = conn->privateData;
1858
    char *xml = NULL;
1859
    char magic[15];
1860 1861 1862
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
1863
    virDomainObjPtr dom = NULL;
1864
    virDomainEventPtr event = NULL;
1865
    int ret = -1;
1866

1867 1868 1869 1870 1871 1872 1873
    virCheckFlags(0, -1);
    if (dxml) {
        testError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                  _("xml modification unsupported"));
        return -1;
    }

1874 1875
    testDriverLock(privconn);

1876
    if ((fd = open(path, O_RDONLY)) < 0) {
1877
        virReportSystemError(errno,
1878 1879
                             _("cannot read domain image '%s'"),
                             path);
1880
        goto cleanup;
1881
    }
1882
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
1883
        virReportSystemError(errno,
1884 1885
                             _("incomplete save header in '%s'"),
                             path);
1886
        goto cleanup;
1887
    }
1888
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
1889
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1890
                  "%s", _("mismatched header magic"));
1891
        goto cleanup;
1892
    }
1893
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
1894
        virReportSystemError(errno,
1895 1896
                             _("failed to read metadata length in '%s'"),
                             path);
1897
        goto cleanup;
1898 1899
    }
    if (len < 1 || len > 8192) {
1900
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1901
                  "%s", _("length of metadata out of range"));
1902
        goto cleanup;
1903
    }
1904
    if (VIR_ALLOC_N(xml, len+1) < 0) {
1905
        virReportOOMError();
1906
        goto cleanup;
1907
    }
1908
    if (saferead(fd, xml, len) != len) {
1909
        virReportSystemError(errno,
1910
                             _("incomplete metdata in '%s'"), path);
1911
        goto cleanup;
1912 1913
    }
    xml[len] = '\0';
1914

1915
    def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
1916
                                  1 << VIR_DOMAIN_VIRT_TEST,
1917
                                  VIR_DOMAIN_XML_INACTIVE);
1918
    if (!def)
1919
        goto cleanup;
1920

1921 1922 1923
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1924
    if (testDomainGenerateIfnames(def) < 0)
1925
        goto cleanup;
1926
    if (!(dom = virDomainAssignDef(privconn->caps,
1927
                                   &privconn->domains, def, true)))
1928 1929
        goto cleanup;
    def = NULL;
1930

J
Jiri Denemark 已提交
1931
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_RESTORED) < 0)
1932 1933
        goto cleanup;

1934 1935 1936
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1937
    ret = 0;
1938 1939 1940 1941

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
1942
    VIR_FORCE_CLOSE(fd);
1943 1944
    if (dom)
        virDomainObjUnlock(dom);
1945 1946
    if (event)
        testDomainEventQueue(privconn, event);
1947
    testDriverUnlock(privconn);
1948
    return ret;
1949 1950
}

1951 1952 1953 1954 1955 1956 1957
static int
testDomainRestore(virConnectPtr conn,
                  const char *path)
{
    return testDomainRestoreFlags(conn, path, NULL, 0);
}

1958 1959
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
E
Eric Blake 已提交
1960
                              unsigned int flags)
1961
{
1962
    testConnPtr privconn = domain->conn->privateData;
1963
    int fd = -1;
1964
    virDomainObjPtr privdom;
1965
    virDomainEventPtr event = NULL;
1966
    int ret = -1;
1967

E
Eric Blake 已提交
1968 1969
    virCheckFlags(VIR_DUMP_CRASH, -1);

1970
    testDriverLock(privconn);
1971 1972 1973 1974
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1975
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1976
        goto cleanup;
1977
    }
1978 1979

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1980
        virReportSystemError(errno,
1981 1982
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
1983
        goto cleanup;
1984
    }
1985
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1986
        virReportSystemError(errno,
1987 1988
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
1989
        goto cleanup;
1990
    }
1991
    if (VIR_CLOSE(fd) < 0) {
1992
        virReportSystemError(errno,
1993 1994
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
1995
        goto cleanup;
1996
    }
1997

1998
    if (flags & VIR_DUMP_CRASH) {
J
Jiri Denemark 已提交
1999
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_CRASHED);
2000 2001 2002 2003 2004 2005 2006 2007
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
2008
    }
2009

2010
    ret = 0;
2011
cleanup:
2012
    VIR_FORCE_CLOSE(fd);
2013 2014
    if (privdom)
        virDomainObjUnlock(privdom);
2015 2016
    if (event)
        testDomainEventQueue(privconn, event);
2017
    testDriverUnlock(privconn);
2018
    return ret;
2019 2020
}

2021
static char *testGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
2022 2023
    char *ret = strdup("linux");
    if (!ret)
2024
        virReportOOMError();
2025
    return ret;
2026 2027
}

2028
static unsigned long long testGetMaxMemory(virDomainPtr domain) {
2029 2030
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2031
    unsigned long long ret = 0;
2032

2033
    testDriverLock(privconn);
2034 2035
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2036
    testDriverUnlock(privconn);
2037 2038

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

2043
    ret = privdom->def->mem.max_balloon;
2044 2045

cleanup:
2046 2047
    if (privdom)
        virDomainObjUnlock(privdom);
2048
    return ret;
2049 2050 2051 2052 2053
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
2054 2055
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2056
    int ret = -1;
2057

2058
    testDriverLock(privconn);
2059 2060
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2061
    testDriverUnlock(privconn);
2062 2063

    if (privdom == NULL) {
2064
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2065
        goto cleanup;
2066
    }
2067 2068

    /* XXX validate not over host memory wrt to other domains */
2069
    privdom->def->mem.max_balloon = memory;
2070 2071 2072
    ret = 0;

cleanup:
2073 2074
    if (privdom)
        virDomainObjUnlock(privdom);
2075
    return ret;
2076 2077
}

2078 2079 2080
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
2081 2082
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2083
    int ret = -1;
2084

2085
    testDriverLock(privconn);
2086 2087
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2088
    testDriverUnlock(privconn);
2089 2090

    if (privdom == NULL) {
2091
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2092
        goto cleanup;
2093
    }
2094

2095
    if (memory > privdom->def->mem.max_balloon) {
2096
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2097
        goto cleanup;
2098
    }
2099

2100
    privdom->def->mem.cur_balloon = memory;
2101 2102 2103
    ret = 0;

cleanup:
2104 2105
    if (privdom)
        virDomainObjUnlock(privdom);
2106
    return ret;
2107 2108
}

2109 2110
static int
testDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
C
Cole Robinson 已提交
2111
{
2112 2113 2114 2115 2116
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;

2117 2118
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    testDriverLock(privconn);
    vm = virDomainFindByUUID(&privconn->domains, domain->uuid);
    testDriverUnlock(privconn);

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

2133 2134
    if (virDomainLiveConfigHelperMethod(privconn->caps, vm, &flags, &def) < 0)
        goto cleanup;
2135

2136
    if (flags & VIR_DOMAIN_AFFECT_LIVE)
2137 2138 2139 2140 2141 2142 2143 2144
        def = vm->def;

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

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    return ret;
C
Cole Robinson 已提交
2145 2146
}

2147 2148 2149
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
2150
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2151 2152 2153 2154 2155 2156 2157
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

static int
testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
                        unsigned int flags)
{
2158
    testConnPtr privconn = domain->conn->privateData;
2159
    virDomainObjPtr privdom = NULL;
2160
    virDomainDefPtr persistentDef;
C
Cole Robinson 已提交
2161 2162
    int ret = -1, maxvcpus;

2163 2164
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2165 2166 2167 2168
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
2169 2170 2171
    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)) {
2172 2173 2174 2175 2176 2177 2178
        testError(VIR_ERR_INVALID_ARG,
                  _("invalid flag combination: (0x%x)"), flags);
        return -1;
    }
    if (!nrCpus || (maxvcpus = testGetMaxVCPUs(domain->conn, NULL)) < nrCpus) {
        testError(VIR_ERR_INVALID_ARG,
                  _("argument out of range: %d"), nrCpus);
2179 2180
        return -1;
    }
2181

2182
    testDriverLock(privconn);
2183
    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
2184 2185 2186
    testDriverUnlock(privconn);

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

2191
    if (!virDomainObjIsActive(privdom) && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
2192
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2193 2194 2195 2196
                  "%s", _("cannot hotplug vcpus for an inactive domain"));
        goto cleanup;
    }

2197 2198
    /* We allow more cpus in guest than host, but not more than the
     * domain's starting limit.  */
C
Cole Robinson 已提交
2199 2200
    if (!(flags & (VIR_DOMAIN_VCPU_MAXIMUM)) &&
        privdom->def->maxvcpus < maxvcpus)
2201
        maxvcpus = privdom->def->maxvcpus;
C
Cole Robinson 已提交
2202

C
Cole Robinson 已提交
2203
    if (nrCpus > maxvcpus) {
2204
        testError(VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
2205 2206
                  "requested cpu amount exceeds maximum (%d > %d)",
                  nrCpus, maxvcpus);
2207
        goto cleanup;
2208
    }
2209

2210 2211 2212 2213
    if (!(persistentDef = virDomainObjGetPersistentDef(privconn->caps,
                                                       privdom)))
        goto cleanup;

2214
    switch (flags) {
2215
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_CONFIG:
2216 2217 2218
        persistentDef->maxvcpus = nrCpus;
        if (nrCpus < persistentDef->vcpus)
            persistentDef->vcpus = nrCpus;
2219 2220
        ret = 0;
        break;
2221

2222
    case VIR_DOMAIN_AFFECT_CONFIG:
2223
        persistentDef->vcpus = nrCpus;
2224 2225 2226
        ret = 0;
        break;

2227
    case VIR_DOMAIN_AFFECT_LIVE:
2228 2229 2230
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
        break;

2231
    case VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG:
2232
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
2233 2234 2235
        if (ret == 0) {
            persistentDef->vcpus = nrCpus;
        }
2236 2237
        break;
    }
2238 2239

cleanup:
2240 2241
    if (privdom)
        virDomainObjUnlock(privdom);
2242
    return ret;
2243 2244
}

2245 2246 2247
static int
testSetVcpus(virDomainPtr domain, unsigned int nrCpus)
{
2248
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2249 2250
}

C
Cole Robinson 已提交
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
static int testDomainGetVcpus(virDomainPtr domain,
                              virVcpuInfoPtr info,
                              int maxinfo,
                              unsigned char *cpumaps,
                              int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
    int i, v, maxcpu, hostcpus;
    int ret = -1;
    struct timeval tv;
    unsigned long long statbase;

    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains, domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2270
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2271 2272 2273 2274
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2275
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2276 2277 2278 2279 2280 2281 2282
                  "%s",_("cannot list vcpus for an inactive domain"));
        goto cleanup;
    }

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
2283
        virReportSystemError(errno,
C
Cole Robinson 已提交
2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
                             "%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);

        for (i = 0 ; i < maxinfo ; i++) {
            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);

        for (v = 0 ; v < maxinfo ; v++) {
            unsigned char *cpumap = VIR_GET_CPUMAP(cpumaps, maplen, v);

            for (i = 0 ; i < maxcpu ; i++) {
                if (VIR_CPU_USABLE(privdomdata->cpumaps, privmaplen, v, i)) {
                    VIR_USE_CPU(cpumap, i);
                }
            }
        }
    }

    ret = maxinfo;
cleanup:
    if (privdom)
        virDomainObjUnlock(privdom);
    return ret;
}

C
Cole Robinson 已提交
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
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;
    int i, maxcpu, hostcpus, privmaplen;
    int ret = -1;

    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains, domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2356
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2357 2358 2359 2360
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2361
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2362 2363 2364 2365 2366
                  "%s",_("cannot pin vcpus on an inactive domain"));
        goto cleanup;
    }

    if (vcpu > privdom->def->vcpus) {
2367
        testError(VIR_ERR_INVALID_ARG, "%s",
C
Cole Robinson 已提交
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
                  _("requested vcpu is higher than allocated vcpus"));
        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);

    for (i = 0 ; i < maxcpu ; i++) {
        if (VIR_CPU_USABLE(cpumap, maplen, 0, i)) {
            VIR_USE_CPU(privcpumap, i);
        }
    }

    ret = 0;
cleanup:
    if (privdom)
        virDomainObjUnlock(privdom);
    return ret;
}

2396
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2397
{
2398
    testConnPtr privconn = domain->conn->privateData;
2399
    virDomainDefPtr def;
2400
    virDomainObjPtr privdom;
2401 2402
    char *ret = NULL;

2403 2404
    /* Flags checked by virDomainDefFormat */

2405 2406 2407 2408 2409 2410
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2411
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2412
        goto cleanup;
2413
    }
2414

2415 2416
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2417

2418
    ret = virDomainDefFormat(def,
2419 2420 2421
                             flags);

cleanup:
2422 2423
    if (privdom)
        virDomainObjUnlock(privdom);
2424
    return ret;
2425
}
2426

2427
static int testNumOfDefinedDomains(virConnectPtr conn) {
2428
    testConnPtr privconn = conn->privateData;
2429
    int count;
2430

2431
    testDriverLock(privconn);
2432
    count = virDomainObjListNumOfDomains(&privconn->domains, 0);
2433
    testDriverUnlock(privconn);
2434

2435
    return count;
2436 2437
}

2438 2439 2440
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
2441

2442
    testConnPtr privconn = conn->privateData;
2443
    int n;
2444

2445
    testDriverLock(privconn);
2446
    memset(names, 0, sizeof(*names)*maxnames);
2447
    n = virDomainObjListGetInactiveNames(&privconn->domains, names, maxnames);
2448
    testDriverUnlock(privconn);
2449

2450
    return n;
2451 2452
}

2453
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
2454
                                        const char *xml) {
2455
    testConnPtr privconn = conn->privateData;
2456
    virDomainPtr ret = NULL;
2457
    virDomainDefPtr def;
2458
    virDomainObjPtr dom = NULL;
2459
    virDomainEventPtr event = NULL;
2460
    int dupVM;
2461

2462
    testDriverLock(privconn);
2463
    if ((def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
2464
                                       1 << VIR_DOMAIN_VIRT_TEST,
2465
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
2466
        goto cleanup;
2467

2468 2469 2470
    if ((dupVM = virDomainObjIsDuplicate(&privconn->domains, def, 0)) < 0)
        goto cleanup;

2471
    if (testDomainGenerateIfnames(def) < 0)
2472
        goto cleanup;
2473
    if (!(dom = virDomainAssignDef(privconn->caps,
2474
                                   &privconn->domains, def, false)))
2475
        goto cleanup;
2476
    def = NULL;
2477
    dom->persistent = 1;
2478

2479 2480
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
2481 2482 2483
                                     !dupVM ?
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2484

2485
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2486
    if (ret)
2487
        ret->id = dom->def->id;
2488 2489 2490

cleanup:
    virDomainDefFree(def);
2491 2492
    if (dom)
        virDomainObjUnlock(dom);
2493 2494
    if (event)
        testDomainEventQueue(privconn, event);
2495
    testDriverUnlock(privconn);
2496
    return ret;
2497 2498
}

2499 2500 2501
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
2502
    testConnPtr privconn = conn->privateData;
2503
    int i, j;
2504
    int ret = -1;
2505

2506
    testDriverLock(privconn);
2507
    if (startCell > privconn->numCells) {
2508
        testError(VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
2509
                  "%s", _("Range exceeds available cells"));
2510
        goto cleanup;
2511 2512 2513 2514 2515 2516 2517
    }

    for (i = startCell, j = 0;
         (i < privconn->numCells && j < maxCells) ;
         ++i, ++j) {
        freemems[j] = privconn->cells[i].mem;
    }
2518
    ret = j;
2519

2520
cleanup:
2521
    testDriverUnlock(privconn);
2522
    return ret;
2523 2524 2525
}


2526
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags) {
2527 2528
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2529
    virDomainEventPtr event = NULL;
2530
    int ret = -1;
2531

2532 2533
    virCheckFlags(0, -1);

2534
    testDriverLock(privconn);
2535 2536 2537 2538
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2539
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2540
        goto cleanup;
2541
    }
2542

J
Jiri Denemark 已提交
2543
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
2544
        testError(VIR_ERR_INTERNAL_ERROR,
2545
                  _("Domain '%s' is already running"), domain->name);
2546
        goto cleanup;
2547 2548
    }

J
Jiri Denemark 已提交
2549 2550
    if (testDomainStartState(domain->conn, privdom,
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
2551 2552 2553
        goto cleanup;
    domain->id = privdom->def->id;

2554 2555 2556
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2557
    ret = 0;
2558

2559
cleanup:
2560 2561
    if (privdom)
        virDomainObjUnlock(privdom);
2562 2563
    if (event)
        testDomainEventQueue(privconn, event);
2564
    testDriverUnlock(privconn);
2565
    return ret;
2566 2567
}

2568 2569 2570 2571
static int testDomainCreate(virDomainPtr domain) {
    return testDomainCreateWithFlags(domain, 0);
}

2572 2573 2574
static int testDomainUndefineFlags(virDomainPtr domain,
                                   unsigned int flags)
{
2575 2576
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2577
    virDomainEventPtr event = NULL;
2578
    int ret = -1;
2579

2580 2581
    virCheckFlags(0, -1);

2582
    testDriverLock(privconn);
2583 2584 2585 2586
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2587
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2588
        goto cleanup;
2589
    }
2590

2591 2592 2593
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2594 2595
    if (virDomainObjIsActive(privdom)) {
        privdom->persistent = 0;
2596 2597 2598 2599 2600 2601
    } else {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
        privdom = NULL;
    }

2602
    ret = 0;
2603

2604
cleanup:
2605 2606
    if (privdom)
        virDomainObjUnlock(privdom);
2607 2608
    if (event)
        testDomainEventQueue(privconn, event);
2609
    testDriverUnlock(privconn);
2610
    return ret;
2611 2612
}

2613 2614 2615 2616 2617
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

2618 2619 2620
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2621 2622
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2623
    int ret = -1;
2624

2625
    testDriverLock(privconn);
2626 2627
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2628
    testDriverUnlock(privconn);
2629 2630

    if (privdom == NULL) {
2631
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2632
        goto cleanup;
2633 2634
    }

2635
    *autostart = privdom->autostart;
2636 2637 2638
    ret = 0;

cleanup:
2639 2640
    if (privdom)
        virDomainObjUnlock(privdom);
2641
    return ret;
2642 2643 2644 2645 2646 2647
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2648 2649
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2650
    int ret = -1;
2651

2652
    testDriverLock(privconn);
2653 2654
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2655
    testDriverUnlock(privconn);
2656 2657

    if (privdom == NULL) {
2658
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2659
        goto cleanup;
2660 2661
    }

2662
    privdom->autostart = autostart ? 1 : 0;
2663 2664 2665
    ret = 0;

cleanup:
2666 2667
    if (privdom)
        virDomainObjUnlock(privdom);
2668
    return ret;
2669
}
2670

2671
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
2672 2673
                                        int *nparams)
{
2674 2675
    char *type = NULL;

2676 2677 2678
    if (nparams)
        *nparams = 1;

2679
    type = strdup("fair");
2680
    if (!type)
2681
        virReportOOMError();
2682

2683 2684 2685
    return type;
}

2686 2687 2688 2689 2690
static int
testDomainGetSchedulerParamsFlags(virDomainPtr domain,
                                  virTypedParameterPtr params,
                                  int *nparams,
                                  unsigned int flags)
2691
{
2692 2693
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2694
    int ret = -1;
2695

2696 2697
    virCheckFlags(0, -1);

2698
    testDriverLock(privconn);
2699 2700
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2701
    testDriverUnlock(privconn);
2702 2703

    if (privdom == NULL) {
2704
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2705
        goto cleanup;
2706 2707
    }

2708 2709
    if (virTypedParameterAssign(params, VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, 50) < 0)
2710
        goto cleanup;
2711 2712
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
2713 2714

    *nparams = 1;
2715 2716 2717
    ret = 0;

cleanup:
2718 2719
    if (privdom)
        virDomainObjUnlock(privdom);
2720
    return ret;
2721
}
2722

2723 2724 2725 2726 2727 2728 2729
static int
testDomainGetSchedulerParams(virDomainPtr domain,
                             virTypedParameterPtr params,
                             int *nparams)
{
    return testDomainGetSchedulerParamsFlags(domain, params, nparams, 0);
}
2730

2731 2732 2733 2734 2735
static int
testDomainSetSchedulerParamsFlags(virDomainPtr domain,
                                  virTypedParameterPtr params,
                                  int nparams,
                                  unsigned int flags)
2736
{
2737 2738
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2739
    int ret = -1, i;
2740

2741
    virCheckFlags(0, -1);
2742 2743 2744 2745 2746
    if (virTypedParameterArrayValidate(params, nparams,
                                       VIR_DOMAIN_SCHEDULER_WEIGHT,
                                       VIR_TYPED_PARAM_UINT,
                                       NULL) < 0)
        return -1;
2747

2748
    testDriverLock(privconn);
2749 2750
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2751
    testDriverUnlock(privconn);
2752 2753

    if (privdom == NULL) {
2754
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2755
        goto cleanup;
2756 2757
    }

2758
    for (i = 0; i < nparams; i++) {
2759 2760 2761
        if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_WEIGHT)) {
            /* XXX */
            /*privdom->weight = params[i].value.ui;*/
2762
        }
2763
    }
2764

2765 2766 2767
    ret = 0;

cleanup:
2768 2769
    if (privdom)
        virDomainObjUnlock(privdom);
2770
    return ret;
2771 2772
}

2773 2774 2775 2776 2777 2778 2779 2780
static int
testDomainSetSchedulerParams(virDomainPtr domain,
                             virTypedParameterPtr params,
                             int nparams)
{
    return testDomainSetSchedulerParamsFlags(domain, params, nparams, 0);
}

2781 2782 2783 2784 2785 2786 2787 2788
static int testDomainBlockStats(virDomainPtr domain,
                                const char *path,
                                struct _virDomainBlockStats *stats)
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
2789
    int ret = -1;
2790 2791 2792 2793 2794 2795 2796

    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2797
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2798 2799 2800
        goto error;
    }

2801
    if (virDomainDiskIndexByName(privdom->def, path, false) < 0) {
2802
        testError(VIR_ERR_INVALID_ARG,
2803 2804 2805 2806 2807
                  _("invalid path: %s"), path);
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2808
        virReportSystemError(errno,
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843
                             "%s", _("getting time of day"));
        goto error;
    }

    /* No significance to these numbers, just enough to mix it up*/
    statbase = (tv.tv_sec * 1000UL * 1000UL) + tv.tv_usec;
    stats->rd_req = statbase / 10;
    stats->rd_bytes = statbase / 20;
    stats->wr_req = statbase / 30;
    stats->wr_bytes = statbase / 40;
    stats->errs = tv.tv_sec / 2;

    ret = 0;
error:
    if (privdom)
        virDomainObjUnlock(privdom);
    return ret;
}

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

    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2844
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856
        goto error;
    }

    for (i = 0 ; i < privdom->def->nnets ; i++) {
        if (privdom->def->nets[i]->ifname &&
            STREQ (privdom->def->nets[i]->ifname, path)) {
            found = 1;
            break;
        }
    }

    if (!found) {
2857
        testError(VIR_ERR_INVALID_ARG,
2858 2859 2860 2861 2862
                  _("invalid path, '%s' is not a known interface"), path);
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2863
        virReportSystemError(errno,
2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885
                             "%s", _("getting time of day"));
        goto error;
    }

    /* No significance to these numbers, just enough to mix it up*/
    statbase = (tv.tv_sec * 1000UL * 1000UL) + tv.tv_usec;
    stats->rx_bytes = statbase / 10;
    stats->rx_packets = statbase / 100;
    stats->rx_errs = tv.tv_sec / 1;
    stats->rx_drop = tv.tv_sec / 2;
    stats->tx_bytes = statbase / 20;
    stats->tx_packets = statbase / 110;
    stats->tx_errs = tv.tv_sec / 3;
    stats->tx_drop = tv.tv_sec / 4;

    ret = 0;
error:
    if (privdom)
        virDomainObjUnlock(privdom);
    return ret;
}

2886
static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
2887
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
2888 2889 2890 2891
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

static int testCloseNetwork(virConnectPtr conn) {
    conn->networkPrivateData = NULL;
    return 0;
}


static virNetworkPtr testLookupNetworkByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
{
2908 2909
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
2910
    virNetworkPtr ret = NULL;
2911

2912 2913 2914 2915 2916
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
2917
        testError(VIR_ERR_NO_NETWORK, NULL);
2918
        goto cleanup;
2919 2920
    }

2921 2922 2923
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2924 2925
    if (net)
        virNetworkObjUnlock(net);
2926
    return ret;
2927
}
2928

2929
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
2930
                                             const char *name)
2931
{
2932
    testConnPtr privconn = conn->privateData;
2933 2934
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
2935

2936 2937 2938 2939 2940
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
2941
        testError(VIR_ERR_NO_NETWORK, NULL);
2942
        goto cleanup;
2943 2944
    }

2945 2946 2947
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2948 2949
    if (net)
        virNetworkObjUnlock(net);
2950
    return ret;
2951 2952 2953 2954
}


static int testNumNetworks(virConnectPtr conn) {
2955
    testConnPtr privconn = conn->privateData;
2956
    int numActive = 0, i;
2957

2958 2959 2960
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2961
        if (virNetworkObjIsActive(privconn->networks.objs[i]))
2962
            numActive++;
2963 2964 2965
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2966

2967
    return numActive;
2968 2969 2970
}

static int testListNetworks(virConnectPtr conn, char **const names, int nnames) {
2971
    testConnPtr privconn = conn->privateData;
2972
    int n = 0, i;
2973

2974
    testDriverLock(privconn);
2975
    memset(names, 0, sizeof(*names)*nnames);
2976 2977
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2978
        if (virNetworkObjIsActive(privconn->networks.objs[i]) &&
2979 2980
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2981
            goto no_memory;
2982 2983 2984 2985
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2986

2987 2988 2989
    return n;

no_memory:
2990
    virReportOOMError();
2991 2992
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2993
    testDriverUnlock(privconn);
2994
    return -1;
2995 2996 2997
}

static int testNumDefinedNetworks(virConnectPtr conn) {
2998
    testConnPtr privconn = conn->privateData;
2999
    int numInactive = 0, i;
3000

3001 3002 3003
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3004
        if (!virNetworkObjIsActive(privconn->networks.objs[i]))
3005
            numInactive++;
3006 3007 3008
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3009

3010
    return numInactive;
3011 3012 3013
}

static int testListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
3014
    testConnPtr privconn = conn->privateData;
3015
    int n = 0, i;
3016

3017
    testDriverLock(privconn);
3018
    memset(names, 0, sizeof(*names)*nnames);
3019 3020
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3021
        if (!virNetworkObjIsActive(privconn->networks.objs[i]) &&
3022 3023
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
3024
            goto no_memory;
3025 3026 3027 3028
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3029

3030 3031 3032
    return n;

no_memory:
3033
    virReportOOMError();
3034 3035
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3036
    testDriverUnlock(privconn);
3037
    return -1;
3038 3039
}

3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050

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

    testDriverLock(privconn);
    obj = virNetworkFindByUUID(&privconn->networks, net->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
3051
        testError(VIR_ERR_NO_NETWORK, NULL);
3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071
        goto cleanup;
    }
    ret = virNetworkObjIsActive(obj);

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

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

    testDriverLock(privconn);
    obj = virNetworkFindByUUID(&privconn->networks, net->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
3072
        testError(VIR_ERR_NO_NETWORK, NULL);
3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
        goto cleanup;
    }
    ret = obj->persistent;

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


3084
static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
3085
    testConnPtr privconn = conn->privateData;
3086
    virNetworkDefPtr def;
3087
    virNetworkObjPtr net = NULL;
3088
    virNetworkPtr ret = NULL;
3089

3090
    testDriverLock(privconn);
3091
    if ((def = virNetworkDefParseString(xml)) == NULL)
3092
        goto cleanup;
3093

3094
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
3095 3096
        goto cleanup;
    def = NULL;
3097
    net->active = 1;
3098

3099
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3100

3101 3102
cleanup:
    virNetworkDefFree(def);
3103 3104 3105
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3106
    return ret;
3107 3108 3109
}

static virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml) {
3110
    testConnPtr privconn = conn->privateData;
3111
    virNetworkDefPtr def;
3112
    virNetworkObjPtr net = NULL;
3113
    virNetworkPtr ret = NULL;
3114

3115
    testDriverLock(privconn);
3116
    if ((def = virNetworkDefParseString(xml)) == NULL)
3117
        goto cleanup;
3118

3119
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
3120 3121
        goto cleanup;
    def = NULL;
3122
    net->persistent = 1;
3123

3124
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3125 3126 3127

cleanup:
    virNetworkDefFree(def);
3128 3129 3130
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3131
    return ret;
3132 3133 3134
}

static int testNetworkUndefine(virNetworkPtr network) {
3135 3136
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3137
    int ret = -1;
3138

3139
    testDriverLock(privconn);
3140 3141 3142 3143
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3144
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3145
        goto cleanup;
3146
    }
3147

D
Daniel P. Berrange 已提交
3148
    if (virNetworkObjIsActive(privnet)) {
3149
        testError(VIR_ERR_OPERATION_INVALID,
3150
                  _("Network '%s' is still running"), network->name);
3151
        goto cleanup;
3152 3153
    }

3154 3155
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
3156
    privnet = NULL;
3157
    ret = 0;
3158

3159
cleanup:
3160 3161 3162
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3163
    return ret;
3164 3165 3166
}

static int testNetworkStart(virNetworkPtr network) {
3167 3168
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3169
    int ret = -1;
3170

3171
    testDriverLock(privconn);
3172 3173
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3174
    testDriverUnlock(privconn);
3175 3176

    if (privnet == NULL) {
3177
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3178
        goto cleanup;
3179
    }
3180

D
Daniel P. Berrange 已提交
3181
    if (virNetworkObjIsActive(privnet)) {
3182
        testError(VIR_ERR_OPERATION_INVALID,
3183
                  _("Network '%s' is already running"), network->name);
3184
        goto cleanup;
3185 3186
    }

3187
    privnet->active = 1;
3188
    ret = 0;
3189

3190
cleanup:
3191 3192
    if (privnet)
        virNetworkObjUnlock(privnet);
3193
    return ret;
3194 3195 3196
}

static int testNetworkDestroy(virNetworkPtr network) {
3197 3198
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3199
    int ret = -1;
3200

3201
    testDriverLock(privconn);
3202 3203 3204 3205
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

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

3210 3211 3212 3213
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
3214
        privnet = NULL;
3215
    }
3216 3217 3218
    ret = 0;

cleanup:
3219 3220 3221
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3222
    return ret;
3223 3224
}

3225
static char *testNetworkGetXMLDesc(virNetworkPtr network,
E
Eric Blake 已提交
3226
                                   unsigned int flags)
3227
{
3228 3229
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3230
    char *ret = NULL;
3231

E
Eric Blake 已提交
3232 3233
    virCheckFlags(0, NULL);

3234
    testDriverLock(privconn);
3235 3236
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3237
    testDriverUnlock(privconn);
3238 3239

    if (privnet == NULL) {
3240
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3241
        goto cleanup;
3242
    }
3243

3244
    ret = virNetworkDefFormat(privnet->def, flags);
3245 3246

cleanup:
3247 3248
    if (privnet)
        virNetworkObjUnlock(privnet);
3249
    return ret;
3250 3251 3252
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3253
    testConnPtr privconn = network->conn->privateData;
3254
    char *bridge = NULL;
3255 3256
    virNetworkObjPtr privnet;

3257
    testDriverLock(privconn);
3258 3259
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3260
    testDriverUnlock(privconn);
3261 3262

    if (privnet == NULL) {
3263
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3264
        goto cleanup;
3265 3266
    }

3267
    if (!(privnet->def->bridge)) {
3268
        testError(VIR_ERR_INTERNAL_ERROR,
3269 3270 3271 3272 3273 3274
                  _("network '%s' does not have a bridge name."),
                  privnet->def->name);
        goto cleanup;
    }

    if (!(bridge = strdup(privnet->def->bridge))) {
3275
        virReportOOMError();
3276
        goto cleanup;
3277
    }
3278 3279

cleanup:
3280 3281
    if (privnet)
        virNetworkObjUnlock(privnet);
3282 3283 3284 3285 3286
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
3287 3288
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3289
    int ret = -1;
3290

3291
    testDriverLock(privconn);
3292 3293
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3294
    testDriverUnlock(privconn);
3295 3296

    if (privnet == NULL) {
3297
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3298
        goto cleanup;
3299 3300
    }

3301
    *autostart = privnet->autostart;
3302 3303 3304
    ret = 0;

cleanup:
3305 3306
    if (privnet)
        virNetworkObjUnlock(privnet);
3307
    return ret;
3308 3309 3310 3311
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
3312 3313
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3314
    int ret = -1;
3315

3316
    testDriverLock(privconn);
3317 3318
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3319
    testDriverUnlock(privconn);
3320 3321

    if (privnet == NULL) {
3322
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3323
        goto cleanup;
3324 3325
    }

3326
    privnet->autostart = autostart ? 1 : 0;
3327 3328 3329
    ret = 0;

cleanup:
3330 3331
    if (privnet)
        virNetworkObjUnlock(privnet);
3332
    return ret;
3333
}
3334

C
Cole Robinson 已提交
3335

L
Laine Stump 已提交
3336 3337 3338 3339 3340 3341
/*
 * Physical host interface routines
 */

static virDrvOpenStatus testOpenInterface(virConnectPtr conn,
                                          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3342
                                          unsigned int flags)
L
Laine Stump 已提交
3343
{
E
Eric Blake 已提交
3344 3345
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

L
Laine Stump 已提交
3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

static int testCloseInterface(virConnectPtr conn)
{
    conn->interfacePrivateData = NULL;
    return 0;
}


static int testNumOfInterfaces(virConnectPtr conn)
{
    testConnPtr privconn = conn->privateData;
    int i, count = 0;

    testDriverLock(privconn);
    for (i = 0 ; (i < privconn->ifaces.count); i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3368
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385
            count++;
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

static int testListInterfaces(virConnectPtr conn, char **const names, int nnames)
{
    testConnPtr privconn = conn->privateData;
    int n = 0, i;

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
    for (i = 0 ; (i < privconn->ifaces.count) && (n < nnames); i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3386
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398
            if (!(names[n++] = strdup(privconn->ifaces.objs[i]->def->name))) {
                virInterfaceObjUnlock(privconn->ifaces.objs[i]);
                goto no_memory;
            }
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);

    return n;

no_memory:
3399
    virReportOOMError();
L
Laine Stump 已提交
3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

static int testNumOfDefinedInterfaces(virConnectPtr conn)
{
    testConnPtr privconn = conn->privateData;
    int i, count = 0;

    testDriverLock(privconn);
    for (i = 0 ; i < privconn->ifaces.count; i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3414
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431
            count++;
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

static int testListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
{
    testConnPtr privconn = conn->privateData;
    int n = 0, i;

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
    for (i = 0 ; (i < privconn->ifaces.count) && (n < nnames); i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3432
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444
            if (!(names[n++] = strdup(privconn->ifaces.objs[i]->def->name))) {
                virInterfaceObjUnlock(privconn->ifaces.objs[i]);
                goto no_memory;
            }
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);

    return n;

no_memory:
3445
    virReportOOMError();
L
Laine Stump 已提交
3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

static virInterfacePtr testLookupInterfaceByName(virConnectPtr conn,
                                                 const char *name)
{
    testConnPtr privconn = conn->privateData;
    virInterfaceObjPtr iface;
    virInterfacePtr ret = NULL;

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

    if (iface == NULL) {
3464
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488
        goto cleanup;
    }

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

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

static virInterfacePtr testLookupInterfaceByMACString(virConnectPtr conn,
                                                      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) {
3489
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3490 3491 3492 3493
        goto cleanup;
    }

    if (ifacect > 1) {
3494
        testError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505
        goto cleanup;
    }

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

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

3506 3507 3508 3509 3510 3511 3512 3513 3514 3515
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) {
3516
        testError(VIR_ERR_NO_INTERFACE, NULL);
3517 3518 3519 3520 3521 3522 3523 3524 3525 3526
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

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

3527
static int testInterfaceChangeBegin(virConnectPtr conn,
E
Eric Blake 已提交
3528
                                    unsigned int flags)
3529 3530 3531 3532
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3533 3534
    virCheckFlags(0, -1);

3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554
    testDriverLock(privconn);
    if (privconn->transaction_running) {
        testError(VIR_ERR_OPERATION_INVALID,
                  _("there is another transaction running."));
        goto cleanup;
    }

    privconn->transaction_running = true;

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

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

static int testInterfaceChangeCommit(virConnectPtr conn,
E
Eric Blake 已提交
3555
                                     unsigned int flags)
3556 3557 3558 3559
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3560 3561
    virCheckFlags(0, -1);

3562 3563 3564 3565
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
        testError(VIR_ERR_OPERATION_INVALID, _("no transaction running, "
Y
Yuri Chornoivan 已提交
3566
                  "nothing to be committed."));
3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581
        goto cleanup;
    }

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

    ret = 0;

cleanup:
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
E
Eric Blake 已提交
3582
                                       unsigned int flags)
3583 3584 3585 3586
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3587 3588
    virCheckFlags(0, -1);

3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
        testError(VIR_ERR_OPERATION_INVALID, _("no transaction running, "
                  "nothing to rollback."));
        goto cleanup;
    }

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

    privconn->transaction_running = false;

    ret = 0;

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

L
Laine Stump 已提交
3612
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
E
Eric Blake 已提交
3613
                                     unsigned int flags)
L
Laine Stump 已提交
3614 3615 3616 3617 3618
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

E
Eric Blake 已提交
3619 3620
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3621 3622 3623 3624 3625 3626
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);
    testDriverUnlock(privconn);

    if (privinterface == NULL) {
3627
        testError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
3628 3629 3630
        goto cleanup;
    }

3631
    ret = virInterfaceDefFormat(privinterface->def);
L
Laine Stump 已提交
3632 3633 3634 3635 3636 3637 3638 3639 3640

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


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
E
Eric Blake 已提交
3641
                                              unsigned int flags)
L
Laine Stump 已提交
3642 3643 3644 3645 3646 3647
{
    testConnPtr privconn = conn->privateData;
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
3648 3649
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3650
    testDriverLock(privconn);
3651
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3652 3653
        goto cleanup;

3654
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678
        goto cleanup;
    def = NULL;

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

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

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

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

    if (privinterface == NULL) {
3679
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692
        goto cleanup;
    }

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

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
E
Eric Blake 已提交
3693
                               unsigned int flags)
L
Laine Stump 已提交
3694 3695 3696 3697 3698
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3699 3700
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3701 3702 3703 3704 3705
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3706
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3707 3708 3709 3710
        goto cleanup;
    }

    if (privinterface->active != 0) {
3711
        testError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

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

static int testInterfaceDestroy(virInterfacePtr iface,
E
Eric Blake 已提交
3726
                                unsigned int flags)
L
Laine Stump 已提交
3727 3728 3729 3730 3731
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3732 3733
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3734 3735 3736 3737 3738
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3739
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3740 3741 3742 3743
        goto cleanup;
    }

    if (privinterface->active == 0) {
3744
        testError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

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



C
Cole Robinson 已提交
3760 3761 3762 3763
/*
 * Storage Driver routines
 */

3764

3765
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3766 3767 3768 3769 3770 3771 3772

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

    pool->configFile = strdup("\0");
    if (!pool->configFile) {
3773
        virReportOOMError();
C
Cole Robinson 已提交
3774 3775 3776 3777 3778 3779
        return -1;
    }

    return 0;
}

3780 3781
static virDrvOpenStatus testStorageOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3782 3783 3784 3785
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

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

3798

C
Cole Robinson 已提交
3799 3800 3801
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
3802
    testConnPtr privconn = conn->privateData;
3803 3804
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3805

3806
    testDriverLock(privconn);
3807
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
3808
    testDriverUnlock(privconn);
3809 3810

    if (pool == NULL) {
3811
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3812
        goto cleanup;
C
Cole Robinson 已提交
3813 3814
    }

3815 3816 3817
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3818 3819
    if (pool)
        virStoragePoolObjUnlock(pool);
3820
    return ret;
C
Cole Robinson 已提交
3821 3822 3823 3824 3825
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
3826
    testConnPtr privconn = conn->privateData;
3827 3828
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3829

3830
    testDriverLock(privconn);
3831
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
3832
    testDriverUnlock(privconn);
3833 3834

    if (pool == NULL) {
3835
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3836
        goto cleanup;
C
Cole Robinson 已提交
3837 3838
    }

3839 3840 3841
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3842 3843
    if (pool)
        virStoragePoolObjUnlock(pool);
3844
    return ret;
C
Cole Robinson 已提交
3845 3846 3847 3848 3849 3850 3851 3852 3853
}

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

static int
testStorageNumPools(virConnectPtr conn) {
3854
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3855 3856
    int numActive = 0, i;

3857
    testDriverLock(privconn);
C
Cole Robinson 已提交
3858 3859 3860
    for (i = 0 ; i < privconn->pools.count ; i++)
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
3861
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3862 3863 3864 3865 3866 3867 3868 3869

    return numActive;
}

static int
testStorageListPools(virConnectPtr conn,
                     char **const names,
                     int nnames) {
3870
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3871 3872
    int n = 0, i;

3873
    testDriverLock(privconn);
C
Cole Robinson 已提交
3874
    memset(names, 0, sizeof(*names)*nnames);
3875 3876
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3877
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3878 3879
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3880
            goto no_memory;
3881 3882 3883 3884
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3885 3886 3887 3888

    return n;

no_memory:
3889
    virReportOOMError();
C
Cole Robinson 已提交
3890 3891
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3892
    testDriverUnlock(privconn);
3893
    return -1;
C
Cole Robinson 已提交
3894 3895 3896 3897
}

static int
testStorageNumDefinedPools(virConnectPtr conn) {
3898
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3899 3900
    int numInactive = 0, i;

3901 3902 3903
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->pools.count ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3904 3905
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
3906 3907 3908
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3909 3910 3911 3912 3913 3914 3915 3916

    return numInactive;
}

static int
testStorageListDefinedPools(virConnectPtr conn,
                            char **const names,
                            int nnames) {
3917
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3918 3919
    int n = 0, i;

3920
    testDriverLock(privconn);
C
Cole Robinson 已提交
3921
    memset(names, 0, sizeof(*names)*nnames);
3922 3923
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3924
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3925 3926
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3927
            goto no_memory;
3928 3929 3930 3931
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3932 3933 3934 3935

    return n;

no_memory:
3936
    virReportOOMError();
C
Cole Robinson 已提交
3937 3938
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3939
    testDriverUnlock(privconn);
3940
    return -1;
C
Cole Robinson 已提交
3941 3942 3943
}


3944 3945 3946 3947 3948 3949 3950 3951 3952 3953
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) {
3954
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974
        goto cleanup;
    }
    ret = virStoragePoolObjIsActive(obj);

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

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

    testDriverLock(privconn);
    obj = virStoragePoolObjFindByUUID(&privconn->pools, pool->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
3975
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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



C
Cole Robinson 已提交
3988
static int
3989
testStoragePoolStart(virStoragePoolPtr pool,
E
Eric Blake 已提交
3990 3991
                     unsigned int flags)
{
3992 3993
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3994
    int ret = -1;
3995

E
Eric Blake 已提交
3996 3997
    virCheckFlags(0, -1);

3998
    testDriverLock(privconn);
3999 4000
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4001
    testDriverUnlock(privconn);
4002 4003

    if (privpool == NULL) {
4004
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4005
        goto cleanup;
4006 4007
    }

4008
    if (virStoragePoolObjIsActive(privpool)) {
4009
        testError(VIR_ERR_OPERATION_INVALID,
4010 4011 4012
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
4013 4014

    privpool->active = 1;
4015
    ret = 0;
C
Cole Robinson 已提交
4016

4017
cleanup:
4018 4019
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4020
    return ret;
C
Cole Robinson 已提交
4021 4022 4023
}

static char *
4024
testStorageFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
4025 4026
                           const char *type,
                           const char *srcSpec,
E
Eric Blake 已提交
4027
                           unsigned int flags)
C
Cole Robinson 已提交
4028
{
4029 4030 4031 4032
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4033 4034
    virCheckFlags(0, NULL);

4035 4036
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4037
        testError(VIR_ERR_INTERNAL_ERROR,
4038 4039 4040 4041 4042
                  _("unknown storage pool type %s"), type);
        goto cleanup;
    }

    if (srcSpec) {
4043
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4044 4045 4046 4047 4048 4049 4050 4051 4052
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
        ret = strdup(defaultPoolSourcesLogicalXML);
        if (!ret)
4053
            virReportOOMError();
4054 4055 4056
        break;

    case VIR_STORAGE_POOL_NETFS:
4057
        if (!source || !source->hosts[0].name) {
4058
            testError(VIR_ERR_INVALID_ARG,
4059 4060 4061 4062 4063
                      "%s", "hostname must be specified for netfs sources");
            goto cleanup;
        }

        if (virAsprintf(&ret, defaultPoolSourcesNetFSXML,
4064
                        source->hosts[0].name) < 0)
4065
            virReportOOMError();
4066 4067 4068
        break;

    default:
4069
        testError(VIR_ERR_NO_SUPPORT,
4070 4071 4072 4073 4074 4075
                  _("pool type '%s' does not support source discovery"), type);
    }

cleanup:
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4076 4077 4078 4079 4080 4081
}


static virStoragePoolPtr
testStoragePoolCreate(virConnectPtr conn,
                      const char *xml,
E
Eric Blake 已提交
4082 4083
                      unsigned int flags)
{
4084
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4085
    virStoragePoolDefPtr def;
4086
    virStoragePoolObjPtr pool = NULL;
4087
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4088

E
Eric Blake 已提交
4089 4090
    virCheckFlags(0, NULL);

4091
    testDriverLock(privconn);
4092
    if (!(def = virStoragePoolDefParseString(xml)))
4093
        goto cleanup;
C
Cole Robinson 已提交
4094

4095 4096 4097 4098
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4099
        testError(VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
4100
                  "%s", _("storage pool already exists"));
4101
        goto cleanup;
C
Cole Robinson 已提交
4102 4103
    }

4104
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4105
        goto cleanup;
4106
    def = NULL;
C
Cole Robinson 已提交
4107

4108
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4109
        virStoragePoolObjRemove(&privconn->pools, pool);
4110 4111
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4112 4113 4114
    }
    pool->active = 1;

4115 4116 4117 4118
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
4119 4120 4121
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4122
    return ret;
C
Cole Robinson 已提交
4123 4124 4125 4126 4127
}

static virStoragePoolPtr
testStoragePoolDefine(virConnectPtr conn,
                      const char *xml,
E
Eric Blake 已提交
4128 4129
                      unsigned int flags)
{
4130
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4131
    virStoragePoolDefPtr def;
4132
    virStoragePoolObjPtr pool = NULL;
4133
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4134

E
Eric Blake 已提交
4135 4136
    virCheckFlags(0, NULL);

4137
    testDriverLock(privconn);
4138
    if (!(def = virStoragePoolDefParseString(xml)))
4139
        goto cleanup;
C
Cole Robinson 已提交
4140 4141 4142 4143 4144

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

4145
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4146 4147
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4148

4149
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4150
        virStoragePoolObjRemove(&privconn->pools, pool);
4151 4152
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4153 4154
    }

4155 4156 4157 4158
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
4159 4160 4161
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4162
    return ret;
C
Cole Robinson 已提交
4163 4164 4165
}

static int
4166 4167 4168
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4169
    int ret = -1;
4170

4171
    testDriverLock(privconn);
4172 4173 4174 4175
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4176
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4177
        goto cleanup;
4178 4179
    }

4180
    if (virStoragePoolObjIsActive(privpool)) {
4181
        testError(VIR_ERR_OPERATION_INVALID,
4182 4183 4184
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
4185 4186

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

4189
cleanup:
4190 4191 4192
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4193
    return ret;
C
Cole Robinson 已提交
4194 4195 4196
}

static int
4197
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4198 4199
                     unsigned int flags)
{
4200 4201
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4202
    int ret = -1;
4203

E
Eric Blake 已提交
4204 4205
    virCheckFlags(0, -1);

4206
    testDriverLock(privconn);
4207 4208
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4209
    testDriverUnlock(privconn);
4210 4211

    if (privpool == NULL) {
4212
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4213
        goto cleanup;
4214 4215
    }

4216
    if (virStoragePoolObjIsActive(privpool)) {
4217
        testError(VIR_ERR_OPERATION_INVALID,
4218 4219 4220
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
4221
    ret = 0;
C
Cole Robinson 已提交
4222

4223
cleanup:
4224 4225
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4226
    return ret;
C
Cole Robinson 已提交
4227 4228 4229 4230
}


static int
4231 4232 4233
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4234
    int ret = -1;
4235

4236
    testDriverLock(privconn);
4237 4238 4239 4240
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4241
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4242
        goto cleanup;
4243 4244 4245
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4246
        testError(VIR_ERR_OPERATION_INVALID,
4247
                  _("storage pool '%s' is not active"), pool->name);
4248
        goto cleanup;
4249
    }
C
Cole Robinson 已提交
4250 4251 4252

    privpool->active = 0;

4253
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4254
        virStoragePoolObjRemove(&privconn->pools, privpool);
4255 4256
        privpool = NULL;
    }
4257
    ret = 0;
C
Cole Robinson 已提交
4258

4259
cleanup:
4260 4261 4262
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4263
    return ret;
C
Cole Robinson 已提交
4264 4265 4266 4267
}


static int
4268
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4269 4270
                      unsigned int flags)
{
4271 4272
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4273
    int ret = -1;
4274

E
Eric Blake 已提交
4275 4276
    virCheckFlags(0, -1);

4277
    testDriverLock(privconn);
4278 4279
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4280
    testDriverUnlock(privconn);
4281 4282

    if (privpool == NULL) {
4283
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4284 4285 4286 4287
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
4288
        testError(VIR_ERR_OPERATION_INVALID,
4289 4290
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
4291 4292
    }

4293
    ret = 0;
C
Cole Robinson 已提交
4294

4295
cleanup:
4296 4297
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4298
    return ret;
C
Cole Robinson 已提交
4299 4300 4301 4302
}


static int
4303
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4304 4305
                       unsigned int flags)
{
4306 4307
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4308
    int ret = -1;
4309

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

4312
    testDriverLock(privconn);
4313 4314
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4315
    testDriverUnlock(privconn);
4316 4317

    if (privpool == NULL) {
4318
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4319
        goto cleanup;
4320 4321 4322
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4323
        testError(VIR_ERR_OPERATION_INVALID,
4324
                  _("storage pool '%s' is not active"), pool->name);
4325
        goto cleanup;
4326
    }
4327
    ret = 0;
C
Cole Robinson 已提交
4328

4329
cleanup:
4330 4331
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4332
    return ret;
C
Cole Robinson 已提交
4333 4334 4335 4336
}


static int
4337
testStoragePoolGetInfo(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4338
                       virStoragePoolInfoPtr info) {
4339 4340
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4341
    int ret = -1;
4342

4343
    testDriverLock(privconn);
4344 4345
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4346
    testDriverUnlock(privconn);
4347 4348

    if (privpool == NULL) {
4349
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4350
        goto cleanup;
4351
    }
C
Cole Robinson 已提交
4352 4353 4354 4355 4356 4357 4358 4359 4360

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

4363
cleanup:
4364 4365
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4366
    return ret;
C
Cole Robinson 已提交
4367 4368 4369
}

static char *
4370
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4371 4372
                          unsigned int flags)
{
4373 4374
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4375
    char *ret = NULL;
4376

E
Eric Blake 已提交
4377 4378
    virCheckFlags(0, NULL);

4379
    testDriverLock(privconn);
4380 4381
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4382
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4383

4384
    if (privpool == NULL) {
4385
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4386
        goto cleanup;
4387 4388
    }

4389
    ret = virStoragePoolDefFormat(privpool->def);
4390 4391

cleanup:
4392 4393
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4394
    return ret;
C
Cole Robinson 已提交
4395 4396 4397
}

static int
4398
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4399
                            int *autostart) {
4400 4401
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4402
    int ret = -1;
4403

4404
    testDriverLock(privconn);
4405 4406
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4407
    testDriverUnlock(privconn);
4408 4409

    if (privpool == NULL) {
4410
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4411
        goto cleanup;
4412
    }
C
Cole Robinson 已提交
4413 4414 4415 4416 4417 4418

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

4421
cleanup:
4422 4423
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4424
    return ret;
C
Cole Robinson 已提交
4425 4426 4427
}

static int
4428
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4429
                            int autostart) {
4430 4431
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4432
    int ret = -1;
4433

4434
    testDriverLock(privconn);
4435 4436
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4437
    testDriverUnlock(privconn);
4438 4439

    if (privpool == NULL) {
4440
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4441
        goto cleanup;
4442
    }
C
Cole Robinson 已提交
4443 4444

    if (!privpool->configFile) {
4445
        testError(VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
4446
                  "%s", _("pool has no config file"));
4447
        goto cleanup;
C
Cole Robinson 已提交
4448 4449 4450 4451
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4452 4453 4454
    ret = 0;

cleanup:
4455 4456
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4457
    return ret;
C
Cole Robinson 已提交
4458 4459 4460 4461
}


static int
4462 4463 4464
testStoragePoolNumVolumes(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4465
    int ret = -1;
4466

4467
    testDriverLock(privconn);
4468 4469
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4470
    testDriverUnlock(privconn);
4471 4472

    if (privpool == NULL) {
4473
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4474
        goto cleanup;
4475 4476 4477
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4478
        testError(VIR_ERR_OPERATION_INVALID,
4479
                  _("storage pool '%s' is not active"), pool->name);
4480
        goto cleanup;
4481
    }
C
Cole Robinson 已提交
4482

4483 4484 4485
    ret = privpool->volumes.count;

cleanup:
4486 4487
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4488
    return ret;
C
Cole Robinson 已提交
4489 4490 4491
}

static int
4492
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4493 4494
                           char **const names,
                           int maxnames) {
4495 4496
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
C
Cole Robinson 已提交
4497 4498
    int i = 0, n = 0;

4499
    memset(names, 0, maxnames * sizeof(*names));
4500 4501

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

    if (privpool == NULL) {
4507
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4508
        goto cleanup;
4509 4510 4511 4512
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4513
        testError(VIR_ERR_OPERATION_INVALID,
4514
                  _("storage pool '%s' is not active"), pool->name);
4515
        goto cleanup;
4516 4517
    }

C
Cole Robinson 已提交
4518 4519
    for (i = 0 ; i < privpool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(privpool->volumes.objs[i]->name)) == NULL) {
4520
            virReportOOMError();
C
Cole Robinson 已提交
4521 4522 4523 4524
            goto cleanup;
        }
    }

4525
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4526 4527 4528 4529 4530 4531
    return n;

 cleanup:
    for (n = 0 ; n < maxnames ; n++)
        VIR_FREE(names[i]);

4532
    memset(names, 0, maxnames * sizeof(*names));
4533 4534
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4535 4536 4537 4538 4539
    return -1;
}


static virStorageVolPtr
4540
testStorageVolumeLookupByName(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4541
                              const char *name ATTRIBUTE_UNUSED) {
4542 4543 4544
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4545
    virStorageVolPtr ret = NULL;
4546

4547
    testDriverLock(privconn);
4548 4549
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4550
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4551

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


    if (!virStoragePoolObjIsActive(privpool)) {
4559
        testError(VIR_ERR_OPERATION_INVALID,
4560
                  _("storage pool '%s' is not active"), pool->name);
4561
        goto cleanup;
4562 4563 4564 4565 4566
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4567
        testError(VIR_ERR_NO_STORAGE_VOL,
C
Cole Robinson 已提交
4568
                  _("no storage vol with matching name '%s'"), name);
4569
        goto cleanup;
C
Cole Robinson 已提交
4570 4571
    }

4572 4573 4574 4575
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);

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


static virStorageVolPtr
testStorageVolumeLookupByKey(virConnectPtr conn,
                             const char *key) {
4585
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4586
    unsigned int i;
4587
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4588

4589
    testDriverLock(privconn);
C
Cole Robinson 已提交
4590
    for (i = 0 ; i < privconn->pools.count ; i++) {
4591
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4592
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4593
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4594 4595
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4596 4597 4598 4599 4600
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4601
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4602 4603
                break;
            }
C
Cole Robinson 已提交
4604
        }
4605
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4606
    }
4607
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4608

4609
    if (!ret)
4610
        testError(VIR_ERR_NO_STORAGE_VOL,
4611 4612 4613
                  _("no storage vol with matching key '%s'"), key);

    return ret;
C
Cole Robinson 已提交
4614 4615 4616 4617 4618
}

static virStorageVolPtr
testStorageVolumeLookupByPath(virConnectPtr conn,
                              const char *path) {
4619
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4620
    unsigned int i;
4621
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4622

4623
    testDriverLock(privconn);
C
Cole Robinson 已提交
4624
    for (i = 0 ; i < privconn->pools.count ; i++) {
4625
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4626
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4627
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4628 4629
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

4630 4631 4632 4633 4634
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4635
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4636 4637
                break;
            }
C
Cole Robinson 已提交
4638
        }
4639
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4640
    }
4641
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4642

4643
    if (!ret)
4644
        testError(VIR_ERR_NO_STORAGE_VOL,
4645 4646 4647
                  _("no storage vol with matching path '%s'"), path);

    return ret;
C
Cole Robinson 已提交
4648 4649 4650
}

static virStorageVolPtr
4651
testStorageVolumeCreateXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4652
                           const char *xmldesc,
E
Eric Blake 已提交
4653 4654
                           unsigned int flags)
{
4655 4656
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4657 4658
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
4659

E
Eric Blake 已提交
4660 4661
    virCheckFlags(0, NULL);

4662
    testDriverLock(privconn);
4663 4664
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4665
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4666

4667
    if (privpool == NULL) {
4668
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4669
        goto cleanup;
4670 4671 4672
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4673
        testError(VIR_ERR_OPERATION_INVALID,
4674
                  _("storage pool '%s' is not active"), pool->name);
4675
        goto cleanup;
4676
    }
C
Cole Robinson 已提交
4677

4678
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4679
    if (privvol == NULL)
4680
        goto cleanup;
4681 4682

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4683
        testError(VIR_ERR_OPERATION_FAILED,
C
Cole Robinson 已提交
4684
                  "%s", _("storage vol already exists"));
4685
        goto cleanup;
C
Cole Robinson 已提交
4686 4687 4688
    }

    /* Make sure enough space */
4689
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
4690
         privpool->def->capacity) {
4691
        testError(VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
4692
                  _("Not enough free space in pool for volume '%s'"),
4693
                  privvol->name);
4694
        goto cleanup;
C
Cole Robinson 已提交
4695 4696 4697 4698
    }

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4699
        virReportOOMError();
4700
        goto cleanup;
C
Cole Robinson 已提交
4701 4702
    }

4703 4704 4705
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4706
        virReportOOMError();
4707
        goto cleanup;
C
Cole Robinson 已提交
4708 4709
    }

4710 4711
    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4712
        virReportOOMError();
4713
        goto cleanup;
C
Cole Robinson 已提交
4714 4715
    }

4716
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
4717 4718 4719
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

4722 4723
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);
4724
    privvol = NULL;
4725 4726 4727

cleanup:
    virStorageVolDefFree(privvol);
4728 4729
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4730
    return ret;
C
Cole Robinson 已提交
4731 4732
}

4733 4734 4735 4736
static virStorageVolPtr
testStorageVolumeCreateXMLFrom(virStoragePoolPtr pool,
                               const char *xmldesc,
                               virStorageVolPtr clonevol,
E
Eric Blake 已提交
4737 4738
                               unsigned int flags)
{
4739 4740 4741 4742 4743
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
4744 4745
    virCheckFlags(0, NULL);

4746 4747 4748 4749 4750 4751
    testDriverLock(privconn);
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
    testDriverUnlock(privconn);

    if (privpool == NULL) {
4752
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4753 4754 4755 4756
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4757
        testError(VIR_ERR_OPERATION_INVALID,
4758 4759 4760 4761
                  _("storage pool '%s' is not active"), pool->name);
        goto cleanup;
    }

4762
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4763 4764 4765 4766
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4767
        testError(VIR_ERR_OPERATION_FAILED,
4768 4769 4770 4771 4772 4773
                  "%s", _("storage vol already exists"));
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
4774
        testError(VIR_ERR_NO_STORAGE_VOL,
4775 4776 4777 4778 4779 4780 4781 4782
                  _("no storage vol with matching name '%s'"),
                  clonevol->name);
        goto cleanup;
    }

    /* Make sure enough space */
    if ((privpool->def->allocation + privvol->allocation) >
         privpool->def->capacity) {
4783
        testError(VIR_ERR_INTERNAL_ERROR,
4784 4785 4786 4787 4788 4789 4790 4791 4792
                  _("Not enough free space in pool for volume '%s'"),
                  privvol->name);
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4793
        virReportOOMError();
4794 4795 4796
        goto cleanup;
    }

4797 4798 4799
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4800
        virReportOOMError();
4801 4802 4803 4804 4805
        goto cleanup;
    }

    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4806
        virReportOOMError();
4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826
        goto cleanup;
    }

    privpool->def->allocation += privvol->allocation;
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    privpool->volumes.objs[privpool->volumes.count++] = privvol;

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

cleanup:
    virStorageVolDefFree(privvol);
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    return ret;
}

C
Cole Robinson 已提交
4827
static int
4828
testStorageVolumeDelete(virStorageVolPtr vol,
E
Eric Blake 已提交
4829 4830
                        unsigned int flags)
{
4831 4832 4833
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
4834
    int i;
4835
    int ret = -1;
C
Cole Robinson 已提交
4836

E
Eric Blake 已提交
4837 4838
    virCheckFlags(0, -1);

4839
    testDriverLock(privconn);
4840 4841
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4842
    testDriverUnlock(privconn);
4843 4844

    if (privpool == NULL) {
4845
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4846
        goto cleanup;
4847 4848 4849 4850 4851 4852
    }


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

    if (privvol == NULL) {
4853
        testError(VIR_ERR_NO_STORAGE_VOL,
4854 4855
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4856
        goto cleanup;
4857 4858 4859
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4860
        testError(VIR_ERR_OPERATION_INVALID,
4861
                  _("storage pool '%s' is not active"), vol->pool);
4862
        goto cleanup;
4863 4864 4865
    }


C
Cole Robinson 已提交
4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888
    privpool->def->allocation -= privvol->allocation;
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    for (i = 0 ; i < privpool->volumes.count ; i++) {
        if (privpool->volumes.objs[i] == privvol) {
            virStorageVolDefFree(privvol);

            if (i < (privpool->volumes.count - 1))
                memmove(privpool->volumes.objs + i,
                        privpool->volumes.objs + i + 1,
                        sizeof(*(privpool->volumes.objs)) *
                                (privpool->volumes.count - (i + 1)));

            if (VIR_REALLOC_N(privpool->volumes.objs,
                              privpool->volumes.count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            privpool->volumes.count--;

            break;
        }
    }
4889
    ret = 0;
C
Cole Robinson 已提交
4890

4891
cleanup:
4892 4893
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4894
    return ret;
C
Cole Robinson 已提交
4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910
}


static int testStorageVolumeTypeForPool(int pooltype) {

    switch(pooltype) {
        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
4911
testStorageVolumeGetInfo(virStorageVolPtr vol,
C
Cole Robinson 已提交
4912
                         virStorageVolInfoPtr info) {
4913 4914 4915
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4916
    int ret = -1;
4917

4918
    testDriverLock(privconn);
4919 4920
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4921
    testDriverUnlock(privconn);
4922 4923

    if (privpool == NULL) {
4924
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4925
        goto cleanup;
4926 4927 4928 4929 4930
    }

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

    if (privvol == NULL) {
4931
        testError(VIR_ERR_NO_STORAGE_VOL,
4932 4933
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4934
        goto cleanup;
4935 4936 4937
    }

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

    memset(info, 0, sizeof(*info));
    info->type = testStorageVolumeTypeForPool(privpool->def->type);
    info->capacity = privvol->capacity;
    info->allocation = privvol->allocation;
4947
    ret = 0;
C
Cole Robinson 已提交
4948

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

static char *
4956
testStorageVolumeGetXMLDesc(virStorageVolPtr vol,
E
Eric Blake 已提交
4957 4958
                            unsigned int flags)
{
4959 4960 4961
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4962
    char *ret = NULL;
4963

E
Eric Blake 已提交
4964 4965
    virCheckFlags(0, NULL);

4966
    testDriverLock(privconn);
4967 4968
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4969
    testDriverUnlock(privconn);
4970 4971

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

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

    if (privvol == NULL) {
4979
        testError(VIR_ERR_NO_STORAGE_VOL,
4980 4981
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4982
        goto cleanup;
4983
    }
C
Cole Robinson 已提交
4984

4985
    if (!virStoragePoolObjIsActive(privpool)) {
4986
        testError(VIR_ERR_OPERATION_INVALID,
4987
                  _("storage pool '%s' is not active"), vol->pool);
4988
        goto cleanup;
4989 4990
    }

4991
    ret = virStorageVolDefFormat(privpool->def, privvol);
4992 4993

cleanup:
4994 4995
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4996
    return ret;
C
Cole Robinson 已提交
4997 4998 4999
}

static char *
5000 5001 5002 5003
testStorageVolumeGetPath(virStorageVolPtr vol) {
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5004
    char *ret = NULL;
5005

5006
    testDriverLock(privconn);
5007 5008
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5009
    testDriverUnlock(privconn);
5010 5011

    if (privpool == NULL) {
5012
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5013
        goto cleanup;
5014 5015 5016 5017 5018
    }

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

    if (privvol == NULL) {
5019
        testError(VIR_ERR_NO_STORAGE_VOL,
5020 5021
                  _("no storage vol with matching name '%s'"),
                  vol->name);
5022
        goto cleanup;
5023 5024 5025
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5026
        testError(VIR_ERR_OPERATION_INVALID,
5027
                  _("storage pool '%s' is not active"), vol->pool);
5028
        goto cleanup;
5029 5030
    }

C
Cole Robinson 已提交
5031
    ret = strdup(privvol->target.path);
5032
    if (ret == NULL)
5033
        virReportOOMError();
5034 5035

cleanup:
5036 5037
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5038 5039 5040
    return ret;
}

5041

5042
/* Node device implementations */
5043 5044
static virDrvOpenStatus testDevMonOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5045 5046 5047 5048
                                       unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

static int testDevMonClose(virConnectPtr conn) {
    conn->devMonPrivateData = NULL;
    return 0;
}

5061 5062 5063
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5064
                     unsigned int flags)
5065 5066 5067 5068 5069
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
5070 5071
    virCheckFlags(0, -1);

5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086
    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 已提交
5087
                    unsigned int flags)
5088 5089 5090 5091 5092
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
5093 5094
    virCheckFlags(0, -1);

5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130
    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)) {
            if ((names[ndevs++] = strdup(driver->devs.objs[i]->def->name)) == NULL) {
                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) {
5131
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

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

static char *
5144
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5145
                         unsigned int flags)
5146 5147 5148 5149 5150
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5151 5152
    virCheckFlags(0, NULL);

5153 5154 5155 5156 5157
    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
5158 5159
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
5160 5161 5162 5163
                                 dev->name);
        goto cleanup;
    }

5164
    ret = virNodeDeviceDefFormat(obj->def);
5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183

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

static char *
testNodeDeviceGetParent(virNodeDevicePtr dev)
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

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

    if (!obj) {
5184
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
5185 5186 5187 5188 5189 5190 5191 5192
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
5193
            virReportOOMError();
5194
    } else {
5195
        virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
5196 5197 5198 5199 5200 5201 5202 5203 5204
                                 "%s", _("no parent for this device"));
    }

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

5205

5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219
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) {
5220 5221
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250
                                 dev->name);
        goto cleanup;
    }

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

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


static int
testNodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

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

    if (!obj) {
5251
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
        names[ncaps] = strdup(virNodeDevCapTypeToString(caps->type));
        if (names[ncaps++] == NULL)
            goto cleanup;
    }
    ret = ncaps;

cleanup:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
}

5275 5276 5277
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
E
Eric Blake 已提交
5278
                        unsigned int flags)
5279 5280 5281 5282 5283 5284 5285 5286 5287
{
    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 已提交
5288 5289
    virCheckFlags(0, NULL);

5290 5291
    testDriverLock(driver);

5292
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, NULL);
5293 5294 5295 5296 5297
    if (def == NULL) {
        goto cleanup;
    }

    /* We run these next two simply for validation */
5298
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1) {
5299 5300 5301
        goto cleanup;
    }

5302
    if (virNodeDeviceGetParentHost(&driver->devs,
5303 5304 5305 5306 5307 5308 5309 5310 5311 5312
                                   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);
    if (!(def->name = strdup(wwpn))) {
5313
        virReportOOMError();
5314 5315 5316 5317 5318 5319 5320 5321 5322 5323
        goto cleanup;
    }

    /* Fill in a random 'host' value, since this would also come from
     * the backend */
    caps = def->caps;
    while (caps) {
        if (caps->type != VIR_NODE_DEV_CAP_SCSI_HOST)
            continue;

5324
        caps->data.scsi_host.host = virRandomBits(10);
5325 5326 5327 5328
        caps = caps->next;
    }


5329
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def))) {
5330 5331 5332 5333 5334 5335 5336 5337
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
cleanup:
    testDriverUnlock(driver);
5338
    virNodeDeviceDefFree(def);
5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357
    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) {
5358
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5359 5360 5361
        goto out;
    }

5362
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
5363 5364 5365 5366 5367
        goto out;
    }

    parent_name = strdup(obj->def->parent);
    if (parent_name == NULL) {
5368
        virReportOOMError();
5369 5370 5371 5372 5373 5374 5375 5376 5377 5378
        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 */
5379
    if (virNodeDeviceGetParentHost(&driver->devs,
5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398
                                   dev->name,
                                   parent_name,
                                   &parent_host) == -1) {
        obj = NULL;
        goto out;
    }

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

out:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

5399 5400

/* Domain event implementations */
5401
static int
5402 5403 5404 5405
testDomainEventRegister(virConnectPtr conn,
                        virConnectDomainEventCallback callback,
                        void *opaque,
                        virFreeCallback freecb)
5406 5407 5408 5409 5410
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5411 5412 5413
    ret = virDomainEventStateRegister(conn,
                                      driver->domainEventState,
                                      callback, opaque, freecb);
5414 5415 5416 5417 5418
    testDriverUnlock(driver);

    return ret;
}

5419

5420
static int
5421 5422
testDomainEventDeregister(virConnectPtr conn,
                          virConnectDomainEventCallback callback)
5423 5424 5425 5426 5427
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5428 5429 5430
    ret = virDomainEventStateDeregister(conn,
                                        driver->domainEventState,
                                        callback);
5431 5432 5433 5434 5435
    testDriverUnlock(driver);

    return ret;
}

5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448

static int
testDomainEventRegisterAny(virConnectPtr conn,
                           virDomainPtr dom,
                           int eventID,
                           virConnectDomainEventGenericCallback callback,
                           void *opaque,
                           virFreeCallback freecb)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5449 5450 5451 5452
    if (virDomainEventStateRegisterID(conn,
                                      driver->domainEventState,
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
5453
        ret = -1;
5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466
    testDriverUnlock(driver);

    return ret;
}

static int
testDomainEventDeregisterAny(virConnectPtr conn,
                             int callbackID)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5467 5468 5469
    ret = virDomainEventStateDeregisterID(conn,
                                          driver->domainEventState,
                                          callbackID);
5470 5471 5472 5473 5474 5475
    testDriverUnlock(driver);

    return ret;
}


5476 5477 5478 5479
/* driver must be locked before calling */
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event)
{
5480
    virDomainEventStateQueue(driver->domainEventState, event);
5481 5482
}

5483 5484
static virDrvOpenStatus testSecretOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5485 5486 5487 5488
                                       unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5489 5490 5491
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5492
    conn->secretPrivateData = conn->privateData;
5493 5494 5495 5496 5497 5498 5499
    return VIR_DRV_OPEN_SUCCESS;
}

static int testSecretClose(virConnectPtr conn) {
    conn->secretPrivateData = NULL;
    return 0;
}
5500

5501 5502 5503

static virDrvOpenStatus testNWFilterOpen(virConnectPtr conn,
                                         virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5504 5505 5506 5507
                                         unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5508 5509 5510
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5511
    conn->nwfilterPrivateData = conn->privateData;
5512 5513 5514 5515 5516 5517 5518 5519
    return VIR_DRV_OPEN_SUCCESS;
}

static int testNWFilterClose(virConnectPtr conn) {
    conn->nwfilterPrivateData = NULL;
    return 0;
}

5520
static virDriver testDriver = {
5521 5522
    .no = VIR_DRV_TEST,
    .name = "Test",
5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538
    .open = testOpen, /* 0.1.1 */
    .close = testClose, /* 0.1.1 */
    .version = testGetVersion, /* 0.1.1 */
    .getHostname = virGetHostname, /* 0.6.3 */
    .getMaxVcpus = testGetMaxVCPUs, /* 0.3.2 */
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
    .getCapabilities = testGetCapabilities, /* 0.2.1 */
    .listDomains = testListDomains, /* 0.1.1 */
    .numOfDomains = testNumOfDomains, /* 0.1.1 */
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
    .domainLookupByID = testLookupDomainByID, /* 0.1.1 */
    .domainLookupByUUID = testLookupDomainByUUID, /* 0.1.1 */
    .domainLookupByName = testLookupDomainByName, /* 0.1.1 */
    .domainSuspend = testPauseDomain, /* 0.1.1 */
    .domainResume = testResumeDomain, /* 0.1.1 */
    .domainShutdown = testShutdownDomain, /* 0.1.1 */
5539
    .domainShutdownFlags = testShutdownDomainFlags, /* 0.9.10 */
5540 5541 5542 5543 5544 5545 5546 5547 5548
    .domainReboot = testRebootDomain, /* 0.1.1 */
    .domainDestroy = testDestroyDomain, /* 0.1.1 */
    .domainGetOSType = testGetOSType, /* 0.1.9 */
    .domainGetMaxMemory = testGetMaxMemory, /* 0.1.4 */
    .domainSetMaxMemory = testSetMaxMemory, /* 0.1.1 */
    .domainSetMemory = testSetMemory, /* 0.1.4 */
    .domainGetInfo = testGetDomainInfo, /* 0.1.1 */
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
5549
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
5550
    .domainRestore = testDomainRestore, /* 0.3.2 */
5551
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
    .domainSetVcpus = testSetVcpus, /* 0.1.4 */
    .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 */
    .listDefinedDomains = testListDefinedDomains, /* 0.1.11 */
    .numOfDefinedDomains = testNumOfDefinedDomains, /* 0.1.11 */
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
5566
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
5567 5568 5569 5570
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
    .domainGetSchedulerParameters = testDomainGetSchedulerParams, /* 0.3.2 */
5571
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParamsFlags, /* 0.9.2 */
5572
    .domainSetSchedulerParameters = testDomainSetSchedulerParams, /* 0.3.2 */
5573
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParamsFlags, /* 0.9.2 */
5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
    .domainEventRegister = testDomainEventRegister, /* 0.6.0 */
    .domainEventDeregister = testDomainEventDeregister, /* 0.6.0 */
    .isEncrypted = testIsEncrypted, /* 0.7.3 */
    .isSecure = testIsSecure, /* 0.7.3 */
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
    .domainEventRegisterAny = testDomainEventRegisterAny, /* 0.8.0 */
    .domainEventDeregisterAny = testDomainEventDeregisterAny, /* 0.8.0 */
5586
    .isAlive = testIsAlive, /* 0.9.8 */
5587 5588 5589 5590
};

static virNetworkDriver testNetworkDriver = {
    "Test",
5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609
    .open = testOpenNetwork, /* 0.3.2 */
    .close = testCloseNetwork, /* 0.3.2 */
    .numOfNetworks = testNumNetworks, /* 0.3.2 */
    .listNetworks = testListNetworks, /* 0.3.2 */
    .numOfDefinedNetworks = testNumDefinedNetworks, /* 0.3.2 */
    .listDefinedNetworks = testListDefinedNetworks, /* 0.3.2 */
    .networkLookupByUUID = testLookupNetworkByUUID, /* 0.3.2 */
    .networkLookupByName = testLookupNetworkByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreate, /* 0.3.2 */
    .networkDefineXML = testNetworkDefine, /* 0.3.2 */
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
    .networkCreate = testNetworkStart, /* 0.3.2 */
    .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 */
5610 5611
};

L
Laine Stump 已提交
5612 5613
static virInterfaceDriver testInterfaceDriver = {
    "Test",                     /* name */
5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627
    .open = testOpenInterface, /* 0.7.0 */
    .close = testCloseInterface, /* 0.7.0 */
    .numOfInterfaces = testNumOfInterfaces, /* 0.7.0 */
    .listInterfaces = testListInterfaces, /* 0.7.0 */
    .numOfDefinedInterfaces = testNumOfDefinedInterfaces, /* 0.7.0 */
    .listDefinedInterfaces = testListDefinedInterfaces, /* 0.7.0 */
    .interfaceLookupByName = testLookupInterfaceByName, /* 0.7.0 */
    .interfaceLookupByMACString = testLookupInterfaceByMACString, /* 0.7.0 */
    .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 */
5628 5629 5630
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
5631 5632 5633
};


5634 5635
static virStorageDriver testStorageDriver = {
    .name = "Test",
5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672
    .open = testStorageOpen, /* 0.4.1 */
    .close = testStorageClose, /* 0.4.1 */

    .numOfPools = testStorageNumPools, /* 0.5.0 */
    .listPools = testStorageListPools, /* 0.5.0 */
    .numOfDefinedPools = testStorageNumDefinedPools, /* 0.5.0 */
    .listDefinedPools = testStorageListDefinedPools, /* 0.5.0 */
    .findPoolSources = testStorageFindPoolSources, /* 0.5.0 */
    .poolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .poolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .poolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
    .poolCreateXML = testStoragePoolCreate, /* 0.5.0 */
    .poolDefineXML = testStoragePoolDefine, /* 0.5.0 */
    .poolBuild = testStoragePoolBuild, /* 0.5.0 */
    .poolUndefine = testStoragePoolUndefine, /* 0.5.0 */
    .poolCreate = testStoragePoolStart, /* 0.5.0 */
    .poolDestroy = testStoragePoolDestroy, /* 0.5.0 */
    .poolDelete = testStoragePoolDelete, /* 0.5.0 */
    .poolRefresh = testStoragePoolRefresh, /* 0.5.0 */
    .poolGetInfo = testStoragePoolGetInfo, /* 0.5.0 */
    .poolGetXMLDesc = testStoragePoolGetXMLDesc, /* 0.5.0 */
    .poolGetAutostart = testStoragePoolGetAutostart, /* 0.5.0 */
    .poolSetAutostart = testStoragePoolSetAutostart, /* 0.5.0 */
    .poolNumOfVolumes = testStoragePoolNumVolumes, /* 0.5.0 */
    .poolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */

    .volLookupByName = testStorageVolumeLookupByName, /* 0.5.0 */
    .volLookupByKey = testStorageVolumeLookupByKey, /* 0.5.0 */
    .volLookupByPath = testStorageVolumeLookupByPath, /* 0.5.0 */
    .volCreateXML = testStorageVolumeCreateXML, /* 0.5.0 */
    .volCreateXMLFrom = testStorageVolumeCreateXMLFrom, /* 0.6.4 */
    .volDelete = testStorageVolumeDelete, /* 0.5.0 */
    .volGetInfo = testStorageVolumeGetInfo, /* 0.5.0 */
    .volGetXMLDesc = testStorageVolumeGetXMLDesc, /* 0.5.0 */
    .volGetPath = testStorageVolumeGetPath, /* 0.5.0 */
    .poolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .poolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
5673 5674
};

5675 5676
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688
    .open = testDevMonOpen, /* 0.6.0 */
    .close = testDevMonClose, /* 0.6.0 */

    .numOfDevices = testNodeNumOfDevices, /* 0.7.2 */
    .listDevices = testNodeListDevices, /* 0.7.2 */
    .deviceLookupByName = testNodeDeviceLookupByName, /* 0.7.2 */
    .deviceGetXMLDesc = testNodeDeviceGetXMLDesc, /* 0.7.2 */
    .deviceGetParent = testNodeDeviceGetParent, /* 0.7.2 */
    .deviceNumOfCaps = testNodeDeviceNumOfCaps, /* 0.7.2 */
    .deviceListCaps = testNodeDeviceListCaps, /* 0.7.2 */
    .deviceCreateXML = testNodeDeviceCreateXML, /* 0.7.3 */
    .deviceDestroy = testNodeDeviceDestroy, /* 0.7.3 */
5689 5690
};

5691 5692
static virSecretDriver testSecretDriver = {
    .name = "Test",
5693 5694
    .open = testSecretOpen, /* 0.7.1 */
    .close = testSecretClose, /* 0.7.1 */
5695
};
5696 5697


5698 5699
static virNWFilterDriver testNWFilterDriver = {
    .name = "Test",
5700 5701
    .open = testNWFilterOpen, /* 0.8.0 */
    .close = testNWFilterClose, /* 0.8.0 */
5702 5703
};

5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
5716 5717
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
5718 5719
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
5720 5721
    if (virRegisterDeviceMonitor(&testDevMonitor) < 0)
        return -1;
5722 5723
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
5724 5725
    if (virRegisterNWFilterDriver(&testNWFilterDriver) < 0)
        return -1;
5726

5727 5728
    return 0;
}