test_driver.c 163.8 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
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23
 *
 * Daniel Berrange <berrange@redhat.com>
 */

24
#include <config.h>
25

26 27 28
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
29 30
#include <fcntl.h>
#include <unistd.h>
31
#include <sys/stat.h>
C
Cole Robinson 已提交
32
#include <libxml/xmlsave.h>
33

34 35

#include "virterror_internal.h"
36
#include "datatypes.h"
37
#include "test_driver.h"
38
#include "virbuffer.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 118 119 120 121
static int testClose(virConnectPtr conn);
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event);


122 123
static void testDriverLock(testConnPtr driver)
{
124
    virMutexLock(&driver->lock);
125 126 127 128
}

static void testDriverUnlock(testConnPtr driver)
{
129
    virMutexUnlock(&driver->lock);
130 131
}

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


152
static int testDefaultConsoleType(const char *ostype ATTRIBUTE_UNUSED,
153
                                  virArch arch ATTRIBUTE_UNUSED)
154 155 156 157
{
    return VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
}

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

166
    if ((caps = virCapabilitiesNew(VIR_ARCH_I686, 0, 0)) == NULL)
167
        goto no_memory;
168

169 170
    caps->defaultConsoleTargetType = testDefaultConsoleType;

171 172 173 174
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
        goto no_memory;
    if (virCapabilitiesAddHostFeature(caps ,"nonpae") < 0)
        goto no_memory;
175

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

182 183 184
    for (i = 0; i < ARRAY_CARDINALITY(guest_types) ; i++) {
        if ((guest = virCapabilitiesAddGuest(caps,
                                             guest_types[i],
185
                                             VIR_ARCH_I686,
186 187 188 189 190
                                             TEST_EMULATOR,
                                             NULL,
                                             0,
                                             NULL)) == NULL)
            goto no_memory;
191

192 193 194 195 196 197 198
        if (virCapabilitiesAddGuestDomain(guest,
                                          "test",
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
            goto no_memory;
199

200 201 202 203
        if (virCapabilitiesAddGuestFeature(guest, "pae", 1, 1) == NULL)
            goto no_memory;
        if (virCapabilitiesAddGuestFeature(guest ,"nonpae", 1, 1) == NULL)
            goto no_memory;
204 205
    }

206 207 208
    caps->privateDataAllocFunc = testDomainObjPrivateAlloc;
    caps->privateDataFreeFunc = testDomainObjPrivateFree;

209 210 211 212 213
    caps->host.nsecModels = 1;
    if (VIR_ALLOC_N(caps->host.secModels, caps->host.nsecModels) < 0)
        goto no_memory;
    caps->host.secModels[0].model = strdup("testSecurity");
    if (!caps->host.secModels[0].model)
214 215
        goto no_memory;

216 217
    caps->host.secModels[0].doi = strdup("");
    if (!caps->host.secModels[0].doi)
218 219
        goto no_memory;

220
    return caps;
221

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

228

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


242 243 244
static const char *defaultNetworkXML =
"<network>"
"  <name>default</name>"
245
"  <uuid>dd8fe884-6c02-601e-7551-cca97df1c5df</uuid>"
246 247 248 249 250 251 252 253
"  <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>";
254

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

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

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

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

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

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

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

        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0) {
332
            virReportOOMError();
333 334 335 336 337 338
            return NULL;
        }

        /* Generate network interface names */
        for (i = 0 ; i < domdef->nnets ; i++) {
            if (domdef->nets[i]->ifname &&
339
                STREQ(domdef->nets[i]->ifname, ifname)) {
340 341 342 343 344 345 346 347 348
                found = 1;
                break;
            }
        }

        if (!found)
            return ifname;
    }

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

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

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

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

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

371
    return 0;
372 373
}

374 375 376 377 378 379 380 381 382 383 384 385
/* 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;
H
Hu Tao 已提交
386
    bool cpu;
387 388 389 390 391 392 393 394 395 396 397

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

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

    if (dom->def->cpumask) {
        for (j = 0; j < maxcpu && j < VIR_DOMAIN_CPUMASK_LEN; ++j) {
H
Hu Tao 已提交
398 399 400
            if (virBitmapGetBit(dom->def->cpumask, j, &cpu) < 0)
                return -1;
            if (cpu) {
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 439 440
                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) {
441
        virReportOOMError();
442 443 444 445
        goto cleanup;
    }

    if (VIR_REALLOC_N(privdata->cpumaps, nvcpus * cpumaplen) < 0) {
446
        virReportOOMError();
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
        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;
    }

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

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

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

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

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

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

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

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

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

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

537
    testDriverLock(privconn);
538
    conn->privateData = privconn;
539

540 541 542
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

543
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
544

545
    /* Numa setup */
546 547 548 549 550 551 552 553 554
    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;
    }

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

    privconn->nextDomID = 1;

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

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

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

578
    virDomainObjUnlock(domobj);
579

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

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

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

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

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

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

625
    testDriverUnlock(privconn);
626

627 628 629
    return VIR_DRV_OPEN_SUCCESS;

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


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

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

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

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

686
    ret = virXPathNodeSet(vol_xpath, ctxt, &vols);
687 688 689 690 691 692 693 694 695 696 697
    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) {
698 699
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving volume filename"));
700 701 702
                goto error;
            }

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

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

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

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

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

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

777
    testDriverLock(privconn);
778
    conn->privateData = privconn;
779

780 781 782
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

923
        virDomainObjUnlock(dom);
924
    }
925
    VIR_FREE(domains);
926

927
    ret = virXPathNodeSet("/node/network", ctxt, &networks);
928 929 930 931 932 933 934 935 936
    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);
937
            if (!absFile) {
938 939
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving network filename"));
940 941
                goto error;
            }
942

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

L
Laine Stump 已提交
961
    /* Parse interface definitions */
962
    ret = virXPathNodeSet("/node/interface", ctxt, &ifaces);
L
Laine Stump 已提交
963 964 965 966 967 968 969 970 971 972
    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) {
973 974
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving interface filename"));
L
Laine Stump 已提交
975 976 977
                goto error;
            }

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

987
        if (!(iface = virInterfaceAssignDef(&privconn->ifaces, def))) {
L
Laine Stump 已提交
988 989 990
            virInterfaceDefFree(def);
            goto error;
        }
991 992

        iface->active = 1;
L
Laine Stump 已提交
993 994 995 996
        virInterfaceObjUnlock(iface);
    }
    VIR_FREE(ifaces);

C
Cole Robinson 已提交
997
    /* Parse Storage Pool list */
998
    ret = virXPathNodeSet("/node/pool", ctxt, &pools);
C
Cole Robinson 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
    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) {
1010 1011
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving pool filename"));
C
Cole Robinson 已提交
1012 1013 1014
                goto error;
            }

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

1026
        if (!(pool = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
1027 1028 1029 1030 1031
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

1032
        if (testStoragePoolObjSetDefaults(pool) == -1) {
1033
            virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1034
            goto error;
1035
        }
C
Cole Robinson 已提交
1036
        pool->active = 1;
1037 1038

        /* Find storage volumes */
1039
        if (testOpenVolumesForPool(xml, ctxt, file, pool, i+1) < 0) {
1040 1041 1042 1043
            virStoragePoolObjUnlock(pool);
            goto error;
        }

1044
        virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1045
    }
1046
    VIR_FREE(pools);
C
Cole Robinson 已提交
1047

1048
    ret = virXPathNodeSet("/node/device", ctxt, &devs);
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
    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) {
1062 1063
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving device filename"));
1064 1065 1066
                goto error;
            }

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


J
Jim Meyering 已提交
1084
    xmlXPathFreeContext(ctxt);
1085
    xmlFreeDoc(xml);
1086
    testDriverUnlock(privconn);
1087

1088
    return 0;
1089 1090

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

1109

1110
static virDrvOpenStatus testOpen(virConnectPtr conn,
1111
                                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
1112
                                 unsigned int flags)
1113
{
1114
    int ret;
1115
    testConnPtr privconn;
1116

E
Eric Blake 已提交
1117 1118
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1119
    if (!conn->uri)
1120
        return VIR_DRV_OPEN_DECLINED;
1121

1122
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1123
        return VIR_DRV_OPEN_DECLINED;
1124

1125
    /* Remote driver should handle these. */
1126
    if (conn->uri->server)
1127 1128
        return VIR_DRV_OPEN_DECLINED;

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

1138
    if (STREQ(conn->uri->path, "/default"))
1139 1140
        ret = testOpenDefault(conn);
    else
1141
        ret = testOpenFromFile(conn,
1142
                               conn->uri->path);
1143

1144 1145 1146 1147 1148
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

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

1150
    privconn->domainEventState = virDomainEventStateNew();
1151
    if (!privconn->domainEventState) {
1152
        testDriverUnlock(privconn);
1153 1154
        testClose(conn);
        return VIR_DRV_OPEN_ERROR;
1155 1156
    }

1157 1158 1159
    testDriverUnlock(privconn);

    return VIR_DRV_OPEN_SUCCESS;
1160 1161
}

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

1175
    testDriverUnlock(privconn);
1176
    virMutexDestroy(&privconn->lock);
1177

1178
    VIR_FREE(privconn);
1179
    conn->privateData = NULL;
1180
    return 0;
1181 1182
}

1183 1184
static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                          unsigned long *hvVer)
1185
{
1186
    *hvVer = 2;
1187
    return 0;
1188 1189
}

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
static int testIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 1;
}

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

1200 1201 1202 1203 1204
static int testIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 1;
}

1205 1206 1207 1208 1209 1210 1211 1212
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

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

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

1232
static int testNumOfDomains(virConnectPtr conn)
1233
{
1234
    testConnPtr privconn = conn->privateData;
1235
    int count;
1236

1237
    testDriverLock(privconn);
1238
    count = virDomainObjListNumOfDomains(&privconn->domains, 1);
1239
    testDriverUnlock(privconn);
1240

1241
    return count;
1242 1243
}

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
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) {
1254
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
        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) {
1275
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
        goto cleanup;
    }
    ret = obj->persistent;

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

1286 1287 1288 1289 1290
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

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

1301 1302
    virCheckFlags(0, NULL);

1303
    testDriverLock(privconn);
1304
    if ((def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
1305
                                       1 << VIR_DOMAIN_VIRT_TEST,
1306
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1307
        goto cleanup;
1308

1309 1310 1311
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1312
    if (testDomainGenerateIfnames(def) < 0)
1313
        goto cleanup;
1314
    if (!(dom = virDomainAssignDef(privconn->caps,
1315
                                   &privconn->domains, def, false)))
1316 1317
        goto cleanup;
    def = NULL;
1318

J
Jiri Denemark 已提交
1319
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0)
1320
        goto cleanup;
1321

1322 1323 1324 1325
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1326
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1327
    if (ret)
1328
        ret->id = dom->def->id;
1329 1330

cleanup:
1331 1332
    if (dom)
        virDomainObjUnlock(dom);
1333 1334
    if (event)
        testDomainEventQueue(privconn, event);
1335
    virDomainDefFree(def);
1336
    testDriverUnlock(privconn);
1337
    return ret;
1338 1339 1340
}


1341 1342
static virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                         int id)
1343
{
1344
    testConnPtr privconn = conn->privateData;
1345 1346
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1347

1348 1349 1350 1351 1352
    testDriverLock(privconn);
    dom = virDomainFindByID(&privconn->domains, id);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1353
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1354
        goto cleanup;
1355 1356
    }

1357
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1358 1359 1360 1361
    if (ret)
        ret->id = dom->def->id;

cleanup:
1362 1363
    if (dom)
        virDomainObjUnlock(dom);
1364
    return ret;
1365 1366
}

1367 1368
static virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
1369
{
1370
    testConnPtr privconn = conn->privateData;
1371 1372
    virDomainPtr ret = NULL;
    virDomainObjPtr dom ;
1373

1374 1375 1376 1377 1378
    testDriverLock(privconn);
    dom = virDomainFindByUUID(&privconn->domains, uuid);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1379
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1380
        goto cleanup;
1381
    }
1382

1383
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1384 1385 1386 1387
    if (ret)
        ret->id = dom->def->id;

cleanup:
1388 1389
    if (dom)
        virDomainObjUnlock(dom);
1390
    return ret;
1391 1392
}

1393 1394
static virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                           const char *name)
1395
{
1396
    testConnPtr privconn = conn->privateData;
1397 1398
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1399

1400 1401 1402 1403 1404
    testDriverLock(privconn);
    dom = virDomainFindByName(&privconn->domains, name);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1405
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1406
        goto cleanup;
1407
    }
1408

1409
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1410 1411 1412 1413
    if (ret)
        ret->id = dom->def->id;

cleanup:
1414 1415
    if (dom)
        virDomainObjUnlock(dom);
1416
    return ret;
1417 1418
}

1419 1420 1421
static int testListDomains(virConnectPtr conn,
                           int *ids,
                           int maxids)
1422
{
1423
    testConnPtr privconn = conn->privateData;
1424
    int n;
1425

1426
    testDriverLock(privconn);
1427
    n = virDomainObjListGetActiveIDs(&privconn->domains, ids, maxids);
1428
    testDriverUnlock(privconn);
1429

1430
    return n;
1431 1432
}

1433
static int testDestroyDomain(virDomainPtr domain)
1434
{
1435 1436
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1437
    virDomainEventPtr event = NULL;
1438
    int ret = -1;
1439

1440
    testDriverLock(privconn);
1441 1442 1443 1444
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1445
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1446
        goto cleanup;
1447
    }
1448

J
Jiri Denemark 已提交
1449
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1450 1451 1452
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1453

1454 1455 1456
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1457
        privdom = NULL;
1458
    }
1459 1460 1461

    ret = 0;
cleanup:
1462 1463
    if (privdom)
        virDomainObjUnlock(privdom);
1464 1465
    if (event)
        testDomainEventQueue(privconn, event);
1466
    testDriverUnlock(privconn);
1467
    return ret;
1468 1469
}

1470
static int testResumeDomain(virDomainPtr domain)
1471
{
1472 1473
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1474
    virDomainEventPtr event = NULL;
1475
    int ret = -1;
1476

1477
    testDriverLock(privconn);
1478 1479
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1480
    testDriverUnlock(privconn);
1481 1482

    if (privdom == NULL) {
1483
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1484
        goto cleanup;
1485
    }
1486

J
Jiri Denemark 已提交
1487
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_PAUSED) {
1488 1489
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
                       domain->name);
1490
        goto cleanup;
1491
    }
1492

J
Jiri Denemark 已提交
1493 1494
    virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                         VIR_DOMAIN_RUNNING_UNPAUSED);
1495 1496 1497
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1498 1499 1500
    ret = 0;

cleanup:
1501 1502
    if (privdom)
        virDomainObjUnlock(privdom);
1503 1504 1505 1506 1507
    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1508
    return ret;
1509 1510
}

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

1519
    testDriverLock(privconn);
1520 1521
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1522
    testDriverUnlock(privconn);
1523 1524

    if (privdom == NULL) {
1525
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1526
        goto cleanup;
1527
    }
1528

J
Jiri Denemark 已提交
1529 1530
    state = virDomainObjGetState(privdom, NULL);
    if (state == VIR_DOMAIN_SHUTOFF || state == VIR_DOMAIN_PAUSED) {
1531 1532
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
                       domain->name);
1533
        goto cleanup;
1534
    }
1535

J
Jiri Denemark 已提交
1536
    virDomainObjSetState(privdom, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
1537 1538 1539
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1540 1541 1542
    ret = 0;

cleanup:
1543 1544
    if (privdom)
        virDomainObjUnlock(privdom);
1545 1546 1547 1548 1549 1550

    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1551
    return ret;
1552 1553
}

1554 1555
static int testShutdownDomainFlags(virDomainPtr domain,
                                   unsigned int flags)
1556
{
1557 1558
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1559
    virDomainEventPtr event = NULL;
1560
    int ret = -1;
1561

1562 1563
    virCheckFlags(0, -1);

1564
    testDriverLock(privconn);
1565 1566 1567 1568
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1569
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1570
        goto cleanup;
1571
    }
1572

J
Jiri Denemark 已提交
1573
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
1574 1575
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain '%s' not running"), domain->name);
1576
        goto cleanup;
1577
    }
1578

J
Jiri Denemark 已提交
1579
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1580 1581 1582
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1583

1584 1585 1586 1587 1588
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
        privdom = NULL;
    }
1589

1590
    ret = 0;
1591
cleanup:
1592 1593
    if (privdom)
        virDomainObjUnlock(privdom);
1594 1595
    if (event)
        testDomainEventQueue(privconn, event);
1596
    testDriverUnlock(privconn);
1597
    return ret;
1598 1599
}

1600
static int testShutdownDomain(virDomainPtr domain)
1601 1602 1603 1604
{
    return testShutdownDomainFlags(domain, 0);
}

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

1614
    testDriverLock(privconn);
1615 1616 1617 1618
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1619
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1620
        goto cleanup;
1621
    }
1622

J
Jiri Denemark 已提交
1623 1624 1625
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

1626 1627
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
J
Jiri Denemark 已提交
1628 1629
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1630 1631
        break;

1632
    case VIR_DOMAIN_LIFECYCLE_RESTART:
J
Jiri Denemark 已提交
1633 1634
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1635 1636
        break;

1637
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
J
Jiri Denemark 已提交
1638 1639
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1640 1641
        break;

1642
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
J
Jiri Denemark 已提交
1643 1644
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1645
        break;
1646

1647
    default:
J
Jiri Denemark 已提交
1648 1649
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1650 1651
        break;
    }
1652

J
Jiri Denemark 已提交
1653 1654
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1655 1656 1657
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1658

1659 1660 1661 1662 1663
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1664 1665
    }

1666 1667
    ret = 0;
cleanup:
1668 1669
    if (privdom)
        virDomainObjUnlock(privdom);
1670 1671
    if (event)
        testDomainEventQueue(privconn, event);
1672
    testDriverUnlock(privconn);
1673
    return ret;
1674 1675
}

1676 1677
static int testGetDomainInfo(virDomainPtr domain,
                             virDomainInfoPtr info)
1678
{
1679
    testConnPtr privconn = domain->conn->privateData;
1680
    struct timeval tv;
1681
    virDomainObjPtr privdom;
1682
    int ret = -1;
1683

1684
    testDriverLock(privconn);
1685 1686
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1687
    testDriverUnlock(privconn);
1688 1689

    if (privdom == NULL) {
1690
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1691
        goto cleanup;
1692
    }
1693 1694

    if (gettimeofday(&tv, NULL) < 0) {
1695 1696
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("getting time of day"));
1697
        goto cleanup;
1698 1699
    }

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

cleanup:
1708 1709
    if (privdom)
        virDomainObjUnlock(privdom);
1710
    return ret;
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) {
1731
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1732 1733 1734
        goto cleanup;
    }

J
Jiri Denemark 已提交
1735
    *state = virDomainObjGetState(privdom, reason);
1736 1737 1738 1739 1740 1741 1742 1743
    ret = 0;

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

1744 1745
#define TEST_SAVE_MAGIC "TestGuestMagic"

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

1758 1759
    virCheckFlags(0, -1);
    if (dxml) {
1760 1761
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1762 1763 1764
        return -1;
    }

1765
    testDriverLock(privconn);
1766 1767 1768 1769
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1770
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1771
        goto cleanup;
1772
    }
1773

1774
    xml = virDomainDefFormat(privdom->def,
C
Cole Robinson 已提交
1775 1776
                             VIR_DOMAIN_XML_SECURE);

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

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

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

J
Jiri Denemark 已提交
1818
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
1819 1820 1821
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1822

1823 1824 1825
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1826
        privdom = NULL;
1827
    }
1828

1829
    ret = 0;
1830 1831 1832 1833 1834 1835 1836
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) {
1837
        VIR_FORCE_CLOSE(fd);
1838 1839
        unlink(path);
    }
1840 1841
    if (privdom)
        virDomainObjUnlock(privdom);
1842 1843
    if (event)
        testDomainEventQueue(privconn, event);
1844
    testDriverUnlock(privconn);
1845
    return ret;
1846 1847
}

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

1871 1872
    virCheckFlags(0, -1);
    if (dxml) {
1873 1874
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1875 1876 1877
        return -1;
    }

1878 1879
    testDriverLock(privconn);

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

1919
    def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
1920
                                  1 << VIR_DOMAIN_VIRT_TEST,
1921
                                  VIR_DOMAIN_XML_INACTIVE);
1922
    if (!def)
1923
        goto cleanup;
1924

1925 1926 1927
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1928
    if (testDomainGenerateIfnames(def) < 0)
1929
        goto cleanup;
1930
    if (!(dom = virDomainAssignDef(privconn->caps,
1931
                                   &privconn->domains, def, true)))
1932 1933
        goto cleanup;
    def = NULL;
1934

J
Jiri Denemark 已提交
1935
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_RESTORED) < 0)
1936 1937
        goto cleanup;

1938 1939 1940
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1941
    ret = 0;
1942 1943 1944 1945

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
1946
    VIR_FORCE_CLOSE(fd);
1947 1948
    if (dom)
        virDomainObjUnlock(dom);
1949 1950
    if (event)
        testDomainEventQueue(privconn, event);
1951
    testDriverUnlock(privconn);
1952
    return ret;
1953 1954
}

1955 1956 1957 1958 1959 1960 1961
static int
testDomainRestore(virConnectPtr conn,
                  const char *path)
{
    return testDomainRestoreFlags(conn, path, NULL, 0);
}

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

E
Eric Blake 已提交
1972 1973
    virCheckFlags(VIR_DUMP_CRASH, -1);

1974
    testDriverLock(privconn);
1975 1976 1977 1978
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

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

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

2014
    ret = 0;
2015
cleanup:
2016
    VIR_FORCE_CLOSE(fd);
2017 2018
    if (privdom)
        virDomainObjUnlock(privdom);
2019 2020
    if (event)
        testDomainEventQueue(privconn, event);
2021
    testDriverUnlock(privconn);
2022
    return ret;
2023 2024
}

2025
static char *testGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
2026 2027
    char *ret = strdup("linux");
    if (!ret)
2028
        virReportOOMError();
2029
    return ret;
2030 2031
}

2032
static unsigned long long testGetMaxMemory(virDomainPtr domain) {
2033 2034
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2035
    unsigned long long ret = 0;
2036

2037
    testDriverLock(privconn);
2038 2039
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2040
    testDriverUnlock(privconn);
2041 2042

    if (privdom == NULL) {
2043
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2044
        goto cleanup;
2045
    }
2046

2047
    ret = privdom->def->mem.max_balloon;
2048 2049

cleanup:
2050 2051
    if (privdom)
        virDomainObjUnlock(privdom);
2052
    return ret;
2053 2054 2055 2056 2057
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
2058 2059
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2060
    int ret = -1;
2061

2062
    testDriverLock(privconn);
2063 2064
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2065
    testDriverUnlock(privconn);
2066 2067

    if (privdom == NULL) {
2068
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2069
        goto cleanup;
2070
    }
2071 2072

    /* XXX validate not over host memory wrt to other domains */
2073
    privdom->def->mem.max_balloon = memory;
2074 2075 2076
    ret = 0;

cleanup:
2077 2078
    if (privdom)
        virDomainObjUnlock(privdom);
2079
    return ret;
2080 2081
}

2082 2083 2084
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
2085 2086
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2087
    int ret = -1;
2088

2089
    testDriverLock(privconn);
2090 2091
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2092
    testDriverUnlock(privconn);
2093 2094

    if (privdom == NULL) {
2095
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2096
        goto cleanup;
2097
    }
2098

2099
    if (memory > privdom->def->mem.max_balloon) {
2100
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2101
        goto cleanup;
2102
    }
2103

2104
    privdom->def->mem.cur_balloon = memory;
2105 2106 2107
    ret = 0;

cleanup:
2108 2109
    if (privdom)
        virDomainObjUnlock(privdom);
2110
    return ret;
2111 2112
}

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

2121 2122
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2123 2124 2125 2126 2127 2128 2129 2130 2131
                  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);
2132 2133
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
2134 2135 2136
        goto cleanup;
    }

2137 2138
    if (virDomainLiveConfigHelperMethod(privconn->caps, vm, &flags, &def) < 0)
        goto cleanup;
2139

2140
    if (flags & VIR_DOMAIN_AFFECT_LIVE)
2141 2142 2143 2144 2145 2146 2147 2148
        def = vm->def;

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

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

2151 2152 2153
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
2154
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2155 2156 2157 2158 2159 2160 2161
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

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

2167 2168
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2169 2170 2171 2172
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

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

2186
    testDriverLock(privconn);
2187
    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
2188 2189 2190
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2191
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2192
        goto cleanup;
2193
    }
2194

2195
    if (!virDomainObjIsActive(privdom) && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
2196 2197
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot hotplug vcpus for an inactive domain"));
C
Cole Robinson 已提交
2198 2199 2200
        goto cleanup;
    }

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

C
Cole Robinson 已提交
2207
    if (nrCpus > maxvcpus) {
2208 2209 2210
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested cpu amount exceeds maximum (%d > %d)"),
                       nrCpus, maxvcpus);
2211
        goto cleanup;
2212
    }
2213

2214 2215 2216 2217
    if (!(persistentDef = virDomainObjGetPersistentDef(privconn->caps,
                                                       privdom)))
        goto cleanup;

2218
    switch (flags) {
2219
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_CONFIG:
2220 2221 2222
        persistentDef->maxvcpus = nrCpus;
        if (nrCpus < persistentDef->vcpus)
            persistentDef->vcpus = nrCpus;
2223 2224
        ret = 0;
        break;
2225

2226
    case VIR_DOMAIN_AFFECT_CONFIG:
2227
        persistentDef->vcpus = nrCpus;
2228 2229 2230
        ret = 0;
        break;

2231
    case VIR_DOMAIN_AFFECT_LIVE:
2232 2233 2234
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
        break;

2235
    case VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG:
2236
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
2237 2238 2239
        if (ret == 0) {
            persistentDef->vcpus = nrCpus;
        }
2240 2241
        break;
    }
2242 2243

cleanup:
2244 2245
    if (privdom)
        virDomainObjUnlock(privdom);
2246
    return ret;
2247 2248
}

2249 2250 2251
static int
testSetVcpus(virDomainPtr domain, unsigned int nrCpus)
{
2252
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2253 2254
}

C
Cole Robinson 已提交
2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
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) {
2274
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2275 2276 2277 2278
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2279 2280
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",_("cannot list vcpus for an inactive domain"));
C
Cole Robinson 已提交
2281 2282 2283 2284 2285 2286
        goto cleanup;
    }

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
2287
        virReportSystemError(errno,
C
Cole Robinson 已提交
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 2339 2340 2341 2342
                             "%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 已提交
2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
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) {
2360
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2361 2362 2363 2364
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2365 2366
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",_("cannot pin vcpus on an inactive domain"));
C
Cole Robinson 已提交
2367 2368 2369 2370
        goto cleanup;
    }

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

2400
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2401
{
2402
    testConnPtr privconn = domain->conn->privateData;
2403
    virDomainDefPtr def;
2404
    virDomainObjPtr privdom;
2405 2406
    char *ret = NULL;

2407 2408
    /* Flags checked by virDomainDefFormat */

2409 2410 2411 2412 2413 2414
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2415
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2416
        goto cleanup;
2417
    }
2418

2419 2420
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2421

2422
    ret = virDomainDefFormat(def,
2423 2424 2425
                             flags);

cleanup:
2426 2427
    if (privdom)
        virDomainObjUnlock(privdom);
2428
    return ret;
2429
}
2430

2431
static int testNumOfDefinedDomains(virConnectPtr conn) {
2432
    testConnPtr privconn = conn->privateData;
2433
    int count;
2434

2435
    testDriverLock(privconn);
2436
    count = virDomainObjListNumOfDomains(&privconn->domains, 0);
2437
    testDriverUnlock(privconn);
2438

2439
    return count;
2440 2441
}

2442 2443 2444
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
2445

2446
    testConnPtr privconn = conn->privateData;
2447
    int n;
2448

2449
    testDriverLock(privconn);
2450
    memset(names, 0, sizeof(*names)*maxnames);
2451
    n = virDomainObjListGetInactiveNames(&privconn->domains, names, maxnames);
2452
    testDriverUnlock(privconn);
2453

2454
    return n;
2455 2456
}

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

2466
    testDriverLock(privconn);
2467
    if ((def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
2468
                                       1 << VIR_DOMAIN_VIRT_TEST,
2469
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
2470
        goto cleanup;
2471

2472 2473 2474
    if ((dupVM = virDomainObjIsDuplicate(&privconn->domains, def, 0)) < 0)
        goto cleanup;

2475
    if (testDomainGenerateIfnames(def) < 0)
2476
        goto cleanup;
2477
    if (!(dom = virDomainAssignDef(privconn->caps,
2478
                                   &privconn->domains, def, false)))
2479
        goto cleanup;
2480
    def = NULL;
2481
    dom->persistent = 1;
2482

2483 2484
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
2485 2486 2487
                                     !dupVM ?
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2488

2489
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2490
    if (ret)
2491
        ret->id = dom->def->id;
2492 2493 2494

cleanup:
    virDomainDefFree(def);
2495 2496
    if (dom)
        virDomainObjUnlock(dom);
2497 2498
    if (event)
        testDomainEventQueue(privconn, event);
2499
    testDriverUnlock(privconn);
2500
    return ret;
2501 2502
}

2503 2504 2505
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
2506
    testConnPtr privconn = conn->privateData;
2507
    int i, j;
2508
    int ret = -1;
2509

2510
    testDriverLock(privconn);
2511
    if (startCell > privconn->numCells) {
2512 2513
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("Range exceeds available cells"));
2514
        goto cleanup;
2515 2516 2517 2518 2519 2520 2521
    }

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

2524
cleanup:
2525
    testDriverUnlock(privconn);
2526
    return ret;
2527 2528 2529
}


2530
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags) {
2531 2532
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2533
    virDomainEventPtr event = NULL;
2534
    int ret = -1;
2535

2536 2537
    virCheckFlags(0, -1);

2538
    testDriverLock(privconn);
2539 2540 2541 2542
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

J
Jiri Denemark 已提交
2547
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
2548 2549
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Domain '%s' is already running"), domain->name);
2550
        goto cleanup;
2551 2552
    }

J
Jiri Denemark 已提交
2553 2554
    if (testDomainStartState(domain->conn, privdom,
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
2555 2556 2557
        goto cleanup;
    domain->id = privdom->def->id;

2558 2559 2560
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2561
    ret = 0;
2562

2563
cleanup:
2564 2565
    if (privdom)
        virDomainObjUnlock(privdom);
2566 2567
    if (event)
        testDomainEventQueue(privconn, event);
2568
    testDriverUnlock(privconn);
2569
    return ret;
2570 2571
}

2572 2573 2574 2575
static int testDomainCreate(virDomainPtr domain) {
    return testDomainCreateWithFlags(domain, 0);
}

2576 2577 2578
static int testDomainUndefineFlags(virDomainPtr domain,
                                   unsigned int flags)
{
2579 2580
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2581
    virDomainEventPtr event = NULL;
2582
    int ret = -1;
2583

2584 2585
    virCheckFlags(0, -1);

2586
    testDriverLock(privconn);
2587 2588 2589 2590
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2591
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2592
        goto cleanup;
2593
    }
2594

2595 2596 2597
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2598 2599
    if (virDomainObjIsActive(privdom)) {
        privdom->persistent = 0;
2600 2601 2602 2603 2604 2605
    } else {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
        privdom = NULL;
    }

2606
    ret = 0;
2607

2608
cleanup:
2609 2610
    if (privdom)
        virDomainObjUnlock(privdom);
2611 2612
    if (event)
        testDomainEventQueue(privconn, event);
2613
    testDriverUnlock(privconn);
2614
    return ret;
2615 2616
}

2617 2618 2619 2620 2621
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

2622 2623 2624
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2625 2626
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2627
    int ret = -1;
2628

2629
    testDriverLock(privconn);
2630 2631
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2632
    testDriverUnlock(privconn);
2633 2634

    if (privdom == NULL) {
2635
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2636
        goto cleanup;
2637 2638
    }

2639
    *autostart = privdom->autostart;
2640 2641 2642
    ret = 0;

cleanup:
2643 2644
    if (privdom)
        virDomainObjUnlock(privdom);
2645
    return ret;
2646 2647 2648 2649 2650 2651
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2652 2653
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2654
    int ret = -1;
2655

2656
    testDriverLock(privconn);
2657 2658
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2659
    testDriverUnlock(privconn);
2660 2661

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

2666
    privdom->autostart = autostart ? 1 : 0;
2667 2668 2669
    ret = 0;

cleanup:
2670 2671
    if (privdom)
        virDomainObjUnlock(privdom);
2672
    return ret;
2673
}
2674

2675
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
2676 2677
                                        int *nparams)
{
2678 2679
    char *type = NULL;

2680 2681 2682
    if (nparams)
        *nparams = 1;

2683
    type = strdup("fair");
2684
    if (!type)
2685
        virReportOOMError();
2686

2687 2688 2689
    return type;
}

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

2700 2701
    virCheckFlags(0, -1);

2702
    testDriverLock(privconn);
2703 2704
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2705
    testDriverUnlock(privconn);
2706 2707

    if (privdom == NULL) {
2708
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2709
        goto cleanup;
2710 2711
    }

2712 2713
    if (virTypedParameterAssign(params, VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, 50) < 0)
2714
        goto cleanup;
2715 2716
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
2717 2718

    *nparams = 1;
2719 2720 2721
    ret = 0;

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

2727 2728 2729 2730 2731 2732 2733
static int
testDomainGetSchedulerParams(virDomainPtr domain,
                             virTypedParameterPtr params,
                             int *nparams)
{
    return testDomainGetSchedulerParamsFlags(domain, params, nparams, 0);
}
2734

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

2745
    virCheckFlags(0, -1);
2746 2747 2748 2749 2750
    if (virTypedParameterArrayValidate(params, nparams,
                                       VIR_DOMAIN_SCHEDULER_WEIGHT,
                                       VIR_TYPED_PARAM_UINT,
                                       NULL) < 0)
        return -1;
2751

2752
    testDriverLock(privconn);
2753 2754
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2755
    testDriverUnlock(privconn);
2756 2757

    if (privdom == NULL) {
2758
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2759
        goto cleanup;
2760 2761
    }

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

2769 2770 2771
    ret = 0;

cleanup:
2772 2773
    if (privdom)
        virDomainObjUnlock(privdom);
2774
    return ret;
2775 2776
}

2777 2778 2779 2780 2781 2782 2783 2784
static int
testDomainSetSchedulerParams(virDomainPtr domain,
                             virTypedParameterPtr params,
                             int nparams)
{
    return testDomainSetSchedulerParamsFlags(domain, params, nparams, 0);
}

2785 2786 2787 2788 2789 2790 2791 2792
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;
2793
    int ret = -1;
2794 2795 2796 2797 2798 2799 2800

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

    if (privdom == NULL) {
2801
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2802 2803 2804
        goto error;
    }

2805
    if (virDomainDiskIndexByName(privdom->def, path, false) < 0) {
2806 2807
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path: %s"), path);
2808 2809 2810 2811
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2812
        virReportSystemError(errno,
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 2844 2845 2846 2847
                             "%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) {
2848
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2849 2850 2851 2852 2853
        goto error;
    }

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

    if (!found) {
2861 2862
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path, '%s' is not a known interface"), path);
2863 2864 2865 2866
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2867
        virReportSystemError(errno,
2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
                             "%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;
}

2890
static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
2891
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
2892 2893 2894 2895
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911
    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)
{
2912 2913
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
2914
    virNetworkPtr ret = NULL;
2915

2916 2917 2918 2919 2920
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
2921
        virReportError(VIR_ERR_NO_NETWORK, NULL);
2922
        goto cleanup;
2923 2924
    }

2925 2926 2927
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2928 2929
    if (net)
        virNetworkObjUnlock(net);
2930
    return ret;
2931
}
2932

2933
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
2934
                                             const char *name)
2935
{
2936
    testConnPtr privconn = conn->privateData;
2937 2938
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
2939

2940 2941 2942 2943 2944
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
2945
        virReportError(VIR_ERR_NO_NETWORK, NULL);
2946
        goto cleanup;
2947 2948
    }

2949 2950 2951
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2952 2953
    if (net)
        virNetworkObjUnlock(net);
2954
    return ret;
2955 2956 2957 2958
}


static int testNumNetworks(virConnectPtr conn) {
2959
    testConnPtr privconn = conn->privateData;
2960
    int numActive = 0, i;
2961

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

2971
    return numActive;
2972 2973 2974
}

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

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

2991 2992 2993
    return n;

no_memory:
2994
    virReportOOMError();
2995 2996
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2997
    testDriverUnlock(privconn);
2998
    return -1;
2999 3000 3001
}

static int testNumDefinedNetworks(virConnectPtr conn) {
3002
    testConnPtr privconn = conn->privateData;
3003
    int numInactive = 0, i;
3004

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

3014
    return numInactive;
3015 3016 3017
}

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

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

3034 3035 3036
    return n;

no_memory:
3037
    virReportOOMError();
3038 3039
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3040
    testDriverUnlock(privconn);
3041
    return -1;
3042 3043
}

3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059
static int
testNetworkListAllNetworks(virConnectPtr conn,
                           virNetworkPtr **nets,
                           unsigned int flags)
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

    testDriverLock(privconn);
    ret = virNetworkList(conn, privconn->networks, nets, flags);
    testDriverUnlock(privconn);

    return ret;
}
3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070

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) {
3071
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091
        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) {
3092
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103
        goto cleanup;
    }
    ret = obj->persistent;

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


3104
static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
3105
    testConnPtr privconn = conn->privateData;
3106
    virNetworkDefPtr def;
3107
    virNetworkObjPtr net = NULL;
3108
    virNetworkPtr ret = NULL;
3109

3110
    testDriverLock(privconn);
3111
    if ((def = virNetworkDefParseString(xml)) == NULL)
3112
        goto cleanup;
3113

3114
    if (!(net = virNetworkAssignDef(&privconn->networks, def, false)))
3115 3116
        goto cleanup;
    def = NULL;
3117
    net->active = 1;
3118

3119
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3120

3121 3122
cleanup:
    virNetworkDefFree(def);
3123 3124 3125
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3126
    return ret;
3127 3128
}

3129 3130 3131
static
virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml)
{
3132
    testConnPtr privconn = conn->privateData;
3133
    virNetworkDefPtr def;
3134
    virNetworkObjPtr net = NULL;
3135
    virNetworkPtr ret = NULL;
3136

3137
    testDriverLock(privconn);
3138
    if ((def = virNetworkDefParseString(xml)) == NULL)
3139
        goto cleanup;
3140

3141
    if (!(net = virNetworkAssignDef(&privconn->networks, def, false)))
3142 3143
        goto cleanup;
    def = NULL;
3144
    net->persistent = 1;
3145

3146
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3147 3148 3149

cleanup:
    virNetworkDefFree(def);
3150 3151 3152
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3153
    return ret;
3154 3155 3156
}

static int testNetworkUndefine(virNetworkPtr network) {
3157 3158
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3159
    int ret = -1;
3160

3161
    testDriverLock(privconn);
3162 3163 3164 3165
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3166
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3167
        goto cleanup;
3168
    }
3169

D
Daniel P. Berrange 已提交
3170
    if (virNetworkObjIsActive(privnet)) {
3171 3172
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is still running"), network->name);
3173
        goto cleanup;
3174 3175
    }

3176 3177
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
3178
    privnet = NULL;
3179
    ret = 0;
3180

3181
cleanup:
3182 3183 3184
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3185
    return ret;
3186 3187
}

3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235
static int
testNetworkUpdate(virNetworkPtr net,
                  unsigned int command,
                  unsigned int section,
                  int parentIndex,
                  const char *xml,
                  unsigned int flags)
{
    testConnPtr privconn = net->conn->privateData;
    virNetworkObjPtr network = NULL;
    int isActive, ret = -1;

    virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE |
                  VIR_NETWORK_UPDATE_AFFECT_CONFIG,
                  -1);

    testDriverLock(privconn);

    network = virNetworkFindByUUID(&privconn->networks, net->uuid);
    if (!network) {
        virReportError(VIR_ERR_NO_NETWORK,
                       "%s", _("no network with matching uuid"));
        goto cleanup;
    }

    /* VIR_NETWORK_UPDATE_AFFECT_CURRENT means "change LIVE if network
     * is active, else change CONFIG
    */
    isActive = virNetworkObjIsActive(network);
    if ((flags & (VIR_NETWORK_UPDATE_AFFECT_LIVE
                   | VIR_NETWORK_UPDATE_AFFECT_CONFIG)) ==
        VIR_NETWORK_UPDATE_AFFECT_CURRENT) {
        if (isActive)
            flags |= VIR_NETWORK_UPDATE_AFFECT_LIVE;
        else
            flags |= VIR_NETWORK_UPDATE_AFFECT_CONFIG;
    }

    /* update the network config in memory/on disk */
    if (virNetworkObjUpdate(network, command, section, parentIndex, xml, flags) < 0)
       goto cleanup;

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

3236
static int testNetworkStart(virNetworkPtr network) {
3237 3238
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3239
    int ret = -1;
3240

3241
    testDriverLock(privconn);
3242 3243
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3244
    testDriverUnlock(privconn);
3245 3246

    if (privnet == NULL) {
3247
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3248
        goto cleanup;
3249
    }
3250

D
Daniel P. Berrange 已提交
3251
    if (virNetworkObjIsActive(privnet)) {
3252 3253
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is already running"), network->name);
3254
        goto cleanup;
3255 3256
    }

3257
    privnet->active = 1;
3258
    ret = 0;
3259

3260
cleanup:
3261 3262
    if (privnet)
        virNetworkObjUnlock(privnet);
3263
    return ret;
3264 3265 3266
}

static int testNetworkDestroy(virNetworkPtr network) {
3267 3268
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3269
    int ret = -1;
3270

3271
    testDriverLock(privconn);
3272 3273 3274 3275
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

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

3280 3281 3282 3283
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
3284
        privnet = NULL;
3285
    }
3286 3287 3288
    ret = 0;

cleanup:
3289 3290 3291
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3292
    return ret;
3293 3294
}

3295
static char *testNetworkGetXMLDesc(virNetworkPtr network,
E
Eric Blake 已提交
3296
                                   unsigned int flags)
3297
{
3298 3299
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3300
    char *ret = NULL;
3301

E
Eric Blake 已提交
3302 3303
    virCheckFlags(0, NULL);

3304
    testDriverLock(privconn);
3305 3306
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3307
    testDriverUnlock(privconn);
3308 3309

    if (privnet == NULL) {
3310
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3311
        goto cleanup;
3312
    }
3313

3314
    ret = virNetworkDefFormat(privnet->def, flags);
3315 3316

cleanup:
3317 3318
    if (privnet)
        virNetworkObjUnlock(privnet);
3319
    return ret;
3320 3321 3322
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3323
    testConnPtr privconn = network->conn->privateData;
3324
    char *bridge = NULL;
3325 3326
    virNetworkObjPtr privnet;

3327
    testDriverLock(privconn);
3328 3329
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3330
    testDriverUnlock(privconn);
3331 3332

    if (privnet == NULL) {
3333
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3334
        goto cleanup;
3335 3336
    }

3337
    if (!(privnet->def->bridge)) {
3338 3339 3340
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("network '%s' does not have a bridge name."),
                       privnet->def->name);
3341 3342 3343 3344
        goto cleanup;
    }

    if (!(bridge = strdup(privnet->def->bridge))) {
3345
        virReportOOMError();
3346
        goto cleanup;
3347
    }
3348 3349

cleanup:
3350 3351
    if (privnet)
        virNetworkObjUnlock(privnet);
3352 3353 3354 3355 3356
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
3357 3358
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3359
    int ret = -1;
3360

3361
    testDriverLock(privconn);
3362 3363
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3364
    testDriverUnlock(privconn);
3365 3366

    if (privnet == NULL) {
3367
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3368
        goto cleanup;
3369 3370
    }

3371
    *autostart = privnet->autostart;
3372 3373 3374
    ret = 0;

cleanup:
3375 3376
    if (privnet)
        virNetworkObjUnlock(privnet);
3377
    return ret;
3378 3379 3380 3381
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
3382 3383
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3384
    int ret = -1;
3385

3386
    testDriverLock(privconn);
3387 3388
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3389
    testDriverUnlock(privconn);
3390 3391

    if (privnet == NULL) {
3392
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3393
        goto cleanup;
3394 3395
    }

3396
    privnet->autostart = autostart ? 1 : 0;
3397 3398 3399
    ret = 0;

cleanup:
3400 3401
    if (privnet)
        virNetworkObjUnlock(privnet);
3402
    return ret;
3403
}
3404

C
Cole Robinson 已提交
3405

L
Laine Stump 已提交
3406 3407 3408 3409 3410 3411
/*
 * Physical host interface routines
 */

static virDrvOpenStatus testOpenInterface(virConnectPtr conn,
                                          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3412
                                          unsigned int flags)
L
Laine Stump 已提交
3413
{
E
Eric Blake 已提交
3414 3415
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

L
Laine Stump 已提交
3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437
    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 已提交
3438
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455
            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 已提交
3456
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468
            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:
3469
    virReportOOMError();
L
Laine Stump 已提交
3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483
    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 已提交
3484
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501
            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 已提交
3502
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514
            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:
3515
    virReportOOMError();
L
Laine Stump 已提交
3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533
    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) {
3534
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558
        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) {
3559
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3560 3561 3562 3563
        goto cleanup;
    }

    if (ifacect > 1) {
3564
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575
        goto cleanup;
    }

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

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

3576 3577 3578 3579 3580 3581 3582 3583 3584 3585
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) {
3586
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
3587 3588 3589 3590 3591 3592 3593 3594 3595 3596
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

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

3597
static int testInterfaceChangeBegin(virConnectPtr conn,
E
Eric Blake 已提交
3598
                                    unsigned int flags)
3599 3600 3601 3602
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3603 3604
    virCheckFlags(0, -1);

3605 3606
    testDriverLock(privconn);
    if (privconn->transaction_running) {
3607
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3608
                       _("there is another transaction running."));
3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624
        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 已提交
3625
                                     unsigned int flags)
3626 3627 3628 3629
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3630 3631
    virCheckFlags(0, -1);

3632 3633 3634
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3635
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3636 3637
                       _("no transaction running, "
                         "nothing to be committed."));
3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652
        goto cleanup;
    }

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

    ret = 0;

cleanup:
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
E
Eric Blake 已提交
3653
                                       unsigned int flags)
3654 3655 3656 3657
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3658 3659
    virCheckFlags(0, -1);

3660 3661 3662
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3663
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3664 3665
                       _("no transaction running, "
                         "nothing to rollback."));
3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682
        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;
}
3683

L
Laine Stump 已提交
3684
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
E
Eric Blake 已提交
3685
                                     unsigned int flags)
L
Laine Stump 已提交
3686 3687 3688 3689 3690
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

E
Eric Blake 已提交
3691 3692
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3693 3694 3695 3696 3697 3698
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);
    testDriverUnlock(privconn);

    if (privinterface == NULL) {
3699
        virReportError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
3700 3701 3702
        goto cleanup;
    }

3703
    ret = virInterfaceDefFormat(privinterface->def);
L
Laine Stump 已提交
3704 3705 3706 3707 3708 3709 3710 3711 3712

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


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
E
Eric Blake 已提交
3713
                                              unsigned int flags)
L
Laine Stump 已提交
3714 3715 3716 3717 3718 3719
{
    testConnPtr privconn = conn->privateData;
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
3720 3721
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3722
    testDriverLock(privconn);
3723
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3724 3725
        goto cleanup;

3726
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750
        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) {
3751
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764
        goto cleanup;
    }

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

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
E
Eric Blake 已提交
3765
                               unsigned int flags)
L
Laine Stump 已提交
3766 3767 3768 3769 3770
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3771 3772
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3773 3774 3775 3776 3777
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3778
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3779 3780 3781 3782
        goto cleanup;
    }

    if (privinterface->active != 0) {
3783
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

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

static int testInterfaceDestroy(virInterfacePtr iface,
E
Eric Blake 已提交
3798
                                unsigned int flags)
L
Laine Stump 已提交
3799 3800 3801 3802 3803
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3804 3805
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3806 3807 3808 3809 3810
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3811
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3812 3813 3814 3815
        goto cleanup;
    }

    if (privinterface->active == 0) {
3816
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

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



C
Cole Robinson 已提交
3832 3833 3834 3835
/*
 * Storage Driver routines
 */

3836

3837
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3838 3839 3840 3841 3842 3843 3844

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

    pool->configFile = strdup("\0");
    if (!pool->configFile) {
3845
        virReportOOMError();
C
Cole Robinson 已提交
3846 3847 3848 3849 3850 3851
        return -1;
    }

    return 0;
}

3852 3853
static virDrvOpenStatus testStorageOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3854 3855 3856 3857
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869
    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;
}

3870

C
Cole Robinson 已提交
3871 3872 3873
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
3874
    testConnPtr privconn = conn->privateData;
3875 3876
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3877

3878
    testDriverLock(privconn);
3879
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
3880
    testDriverUnlock(privconn);
3881 3882

    if (pool == NULL) {
3883
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
3884
        goto cleanup;
C
Cole Robinson 已提交
3885 3886
    }

3887 3888
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
3889 3890

cleanup:
3891 3892
    if (pool)
        virStoragePoolObjUnlock(pool);
3893
    return ret;
C
Cole Robinson 已提交
3894 3895 3896 3897 3898
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
3899
    testConnPtr privconn = conn->privateData;
3900 3901
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3902

3903
    testDriverLock(privconn);
3904
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
3905
    testDriverUnlock(privconn);
3906 3907

    if (pool == NULL) {
3908
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
3909
        goto cleanup;
C
Cole Robinson 已提交
3910 3911
    }

3912 3913
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
3914 3915

cleanup:
3916 3917
    if (pool)
        virStoragePoolObjUnlock(pool);
3918
    return ret;
C
Cole Robinson 已提交
3919 3920 3921 3922 3923 3924 3925 3926 3927
}

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

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

3931
    testDriverLock(privconn);
C
Cole Robinson 已提交
3932 3933 3934
    for (i = 0 ; i < privconn->pools.count ; i++)
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
3935
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3936 3937 3938 3939 3940 3941 3942 3943

    return numActive;
}

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

3947
    testDriverLock(privconn);
C
Cole Robinson 已提交
3948
    memset(names, 0, sizeof(*names)*nnames);
3949 3950
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3951
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3952 3953
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3954
            goto no_memory;
3955 3956 3957 3958
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3959 3960 3961 3962

    return n;

no_memory:
3963
    virReportOOMError();
C
Cole Robinson 已提交
3964 3965
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3966
    testDriverUnlock(privconn);
3967
    return -1;
C
Cole Robinson 已提交
3968 3969 3970 3971
}

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

3975 3976 3977
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->pools.count ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3978 3979
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
3980 3981 3982
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3983 3984 3985 3986 3987 3988 3989 3990

    return numInactive;
}

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

3994
    testDriverLock(privconn);
C
Cole Robinson 已提交
3995
    memset(names, 0, sizeof(*names)*nnames);
3996 3997
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3998
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3999 4000
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4001
            goto no_memory;
4002 4003 4004 4005
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4006 4007 4008 4009

    return n;

no_memory:
4010
    virReportOOMError();
C
Cole Robinson 已提交
4011 4012
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
4013
    testDriverUnlock(privconn);
4014
    return -1;
C
Cole Robinson 已提交
4015 4016
}

4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032
static int
testStorageListAllPools(virConnectPtr conn,
                        virStoragePoolPtr **pools,
                        unsigned int flags)
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

    testDriverLock(privconn);
    ret = virStoragePoolList(conn, privconn->pools, pools, flags);
    testDriverUnlock(privconn);

    return ret;
}
C
Cole Robinson 已提交
4033

4034 4035 4036 4037 4038 4039 4040 4041 4042 4043
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) {
4044
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064
        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) {
4065
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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



C
Cole Robinson 已提交
4078
static int
4079
testStoragePoolStart(virStoragePoolPtr pool,
E
Eric Blake 已提交
4080 4081
                     unsigned int flags)
{
4082 4083
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4084
    int ret = -1;
4085

E
Eric Blake 已提交
4086 4087
    virCheckFlags(0, -1);

4088
    testDriverLock(privconn);
4089 4090
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4091
    testDriverUnlock(privconn);
4092 4093

    if (privpool == NULL) {
4094
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4095
        goto cleanup;
4096 4097
    }

4098
    if (virStoragePoolObjIsActive(privpool)) {
4099 4100
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4101 4102
        goto cleanup;
    }
C
Cole Robinson 已提交
4103 4104

    privpool->active = 1;
4105
    ret = 0;
C
Cole Robinson 已提交
4106

4107
cleanup:
4108 4109
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4110
    return ret;
C
Cole Robinson 已提交
4111 4112 4113
}

static char *
4114
testStorageFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
4115 4116
                           const char *type,
                           const char *srcSpec,
E
Eric Blake 已提交
4117
                           unsigned int flags)
C
Cole Robinson 已提交
4118
{
4119 4120 4121 4122
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4123 4124
    virCheckFlags(0, NULL);

4125 4126
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4127 4128
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
4129 4130 4131 4132
        goto cleanup;
    }

    if (srcSpec) {
4133
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4134 4135 4136 4137 4138 4139 4140 4141 4142
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
        ret = strdup(defaultPoolSourcesLogicalXML);
        if (!ret)
4143
            virReportOOMError();
4144 4145 4146
        break;

    case VIR_STORAGE_POOL_NETFS:
4147
        if (!source || !source->hosts[0].name) {
4148 4149
            virReportError(VIR_ERR_INVALID_ARG,
                           "%s", _("hostname must be specified for netfs sources"));
4150 4151 4152 4153
            goto cleanup;
        }

        if (virAsprintf(&ret, defaultPoolSourcesNetFSXML,
4154
                        source->hosts[0].name) < 0)
4155
            virReportOOMError();
4156 4157 4158
        break;

    default:
4159 4160
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4161 4162 4163 4164 4165
    }

cleanup:
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4166 4167 4168 4169 4170 4171
}


static virStoragePoolPtr
testStoragePoolCreate(virConnectPtr conn,
                      const char *xml,
E
Eric Blake 已提交
4172 4173
                      unsigned int flags)
{
4174
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4175
    virStoragePoolDefPtr def;
4176
    virStoragePoolObjPtr pool = NULL;
4177
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4178

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

4181
    testDriverLock(privconn);
4182
    if (!(def = virStoragePoolDefParseString(xml)))
4183
        goto cleanup;
C
Cole Robinson 已提交
4184

4185 4186 4187 4188
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4189 4190
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("storage pool already exists"));
4191
        goto cleanup;
C
Cole Robinson 已提交
4192 4193
    }

4194
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4195
        goto cleanup;
4196
    def = NULL;
C
Cole Robinson 已提交
4197

4198
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4199
        virStoragePoolObjRemove(&privconn->pools, pool);
4200 4201
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4202 4203 4204
    }
    pool->active = 1;

4205 4206
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4207 4208 4209

cleanup:
    virStoragePoolDefFree(def);
4210 4211 4212
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4213
    return ret;
C
Cole Robinson 已提交
4214 4215 4216 4217 4218
}

static virStoragePoolPtr
testStoragePoolDefine(virConnectPtr conn,
                      const char *xml,
E
Eric Blake 已提交
4219 4220
                      unsigned int flags)
{
4221
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4222
    virStoragePoolDefPtr def;
4223
    virStoragePoolObjPtr pool = NULL;
4224
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4225

E
Eric Blake 已提交
4226 4227
    virCheckFlags(0, NULL);

4228
    testDriverLock(privconn);
4229
    if (!(def = virStoragePoolDefParseString(xml)))
4230
        goto cleanup;
C
Cole Robinson 已提交
4231 4232 4233 4234 4235

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

4236
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4237 4238
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4239

4240
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4241
        virStoragePoolObjRemove(&privconn->pools, pool);
4242 4243
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4244 4245
    }

4246 4247
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4248 4249 4250

cleanup:
    virStoragePoolDefFree(def);
4251 4252 4253
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4254
    return ret;
C
Cole Robinson 已提交
4255 4256 4257
}

static int
4258 4259 4260
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4261
    int ret = -1;
4262

4263
    testDriverLock(privconn);
4264 4265 4266 4267
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4268
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4269
        goto cleanup;
4270 4271
    }

4272
    if (virStoragePoolObjIsActive(privpool)) {
4273 4274
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4275 4276
        goto cleanup;
    }
C
Cole Robinson 已提交
4277 4278

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

4281
cleanup:
4282 4283 4284
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4285
    return ret;
C
Cole Robinson 已提交
4286 4287 4288
}

static int
4289
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4290 4291
                     unsigned int flags)
{
4292 4293
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4294
    int ret = -1;
4295

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

4298
    testDriverLock(privconn);
4299 4300
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4301
    testDriverUnlock(privconn);
4302 4303

    if (privpool == NULL) {
4304
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4305
        goto cleanup;
4306 4307
    }

4308
    if (virStoragePoolObjIsActive(privpool)) {
4309 4310
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4311 4312
        goto cleanup;
    }
4313
    ret = 0;
C
Cole Robinson 已提交
4314

4315
cleanup:
4316 4317
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4318
    return ret;
C
Cole Robinson 已提交
4319 4320 4321 4322
}


static int
4323 4324 4325
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4326
    int ret = -1;
4327

4328
    testDriverLock(privconn);
4329 4330 4331 4332
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4333
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4334
        goto cleanup;
4335 4336 4337
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4338 4339
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4340
        goto cleanup;
4341
    }
C
Cole Robinson 已提交
4342 4343 4344

    privpool->active = 0;

4345
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4346
        virStoragePoolObjRemove(&privconn->pools, privpool);
4347 4348
        privpool = NULL;
    }
4349
    ret = 0;
C
Cole Robinson 已提交
4350

4351
cleanup:
4352 4353 4354
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4355
    return ret;
C
Cole Robinson 已提交
4356 4357 4358 4359
}


static int
4360
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4361 4362
                      unsigned int flags)
{
4363 4364
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4365
    int ret = -1;
4366

E
Eric Blake 已提交
4367 4368
    virCheckFlags(0, -1);

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

    if (privpool == NULL) {
4375
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4376 4377 4378 4379
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
4380 4381
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4382
        goto cleanup;
4383 4384
    }

4385
    ret = 0;
C
Cole Robinson 已提交
4386

4387
cleanup:
4388 4389
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4390
    return ret;
C
Cole Robinson 已提交
4391 4392 4393 4394
}


static int
4395
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4396 4397
                       unsigned int flags)
{
4398 4399
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4400
    int ret = -1;
4401

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

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

    if (privpool == NULL) {
4410
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4411
        goto cleanup;
4412 4413 4414
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4415 4416
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4417
        goto cleanup;
4418
    }
4419
    ret = 0;
C
Cole Robinson 已提交
4420

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


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

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

    if (privpool == NULL) {
4441
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4442
        goto cleanup;
4443
    }
C
Cole Robinson 已提交
4444 4445 4446 4447 4448 4449 4450 4451 4452

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

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

static char *
4462
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4463 4464
                          unsigned int flags)
{
4465 4466
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4467
    char *ret = NULL;
4468

E
Eric Blake 已提交
4469 4470
    virCheckFlags(0, NULL);

4471
    testDriverLock(privconn);
4472 4473
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4474
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4475

4476
    if (privpool == NULL) {
4477
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4478
        goto cleanup;
4479 4480
    }

4481
    ret = virStoragePoolDefFormat(privpool->def);
4482 4483

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

static int
4490
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4491
                            int *autostart) {
4492 4493
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4494
    int ret = -1;
4495

4496
    testDriverLock(privconn);
4497 4498
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4499
    testDriverUnlock(privconn);
4500 4501

    if (privpool == NULL) {
4502
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4503
        goto cleanup;
4504
    }
C
Cole Robinson 已提交
4505 4506 4507 4508 4509 4510

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

4513
cleanup:
4514 4515
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4516
    return ret;
C
Cole Robinson 已提交
4517 4518 4519
}

static int
4520
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4521
                            int autostart) {
4522 4523
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4524
    int ret = -1;
4525

4526
    testDriverLock(privconn);
4527 4528
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4529
    testDriverUnlock(privconn);
4530 4531

    if (privpool == NULL) {
4532
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4533
        goto cleanup;
4534
    }
C
Cole Robinson 已提交
4535 4536

    if (!privpool->configFile) {
4537 4538
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
4539
        goto cleanup;
C
Cole Robinson 已提交
4540 4541 4542 4543
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4544 4545 4546
    ret = 0;

cleanup:
4547 4548
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4549
    return ret;
C
Cole Robinson 已提交
4550 4551 4552 4553
}


static int
4554 4555 4556
testStoragePoolNumVolumes(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4557
    int ret = -1;
4558

4559
    testDriverLock(privconn);
4560 4561
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4562
    testDriverUnlock(privconn);
4563 4564

    if (privpool == NULL) {
4565
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4566
        goto cleanup;
4567 4568 4569
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4570 4571
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4572
        goto cleanup;
4573
    }
C
Cole Robinson 已提交
4574

4575 4576 4577
    ret = privpool->volumes.count;

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

static int
4584
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4585 4586
                           char **const names,
                           int maxnames) {
4587 4588
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
C
Cole Robinson 已提交
4589 4590
    int i = 0, n = 0;

4591
    memset(names, 0, maxnames * sizeof(*names));
4592 4593

    testDriverLock(privconn);
4594 4595
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4596
    testDriverUnlock(privconn);
4597 4598

    if (privpool == NULL) {
4599
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4600
        goto cleanup;
4601 4602 4603 4604
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4605 4606
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4607
        goto cleanup;
4608 4609
    }

C
Cole Robinson 已提交
4610 4611
    for (i = 0 ; i < privpool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(privpool->volumes.objs[i]->name)) == NULL) {
4612
            virReportOOMError();
C
Cole Robinson 已提交
4613 4614 4615 4616
            goto cleanup;
        }
    }

4617
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4618 4619 4620 4621 4622 4623
    return n;

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

4624
    memset(names, 0, maxnames * sizeof(*names));
4625 4626
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4627 4628 4629
    return -1;
}

4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673
static int
testStoragePoolListAllVolumes(virStoragePoolPtr obj,
                              virStorageVolPtr **vols,
                              unsigned int flags) {
    testConnPtr privconn = obj->conn->privateData;
    virStoragePoolObjPtr pool;
    int i;
    virStorageVolPtr *tmp_vols = NULL;
    virStorageVolPtr vol = NULL;
    int nvols = 0;
    int ret = -1;

    virCheckFlags(0, -1);

    testDriverLock(privconn);
    pool = virStoragePoolObjFindByUUID(&privconn->pools, obj->uuid);
    testDriverUnlock(privconn);

    if (!pool) {
        virReportError(VIR_ERR_NO_STORAGE_POOL, "%s",
                       _("no storage pool with matching uuid"));
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(pool)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("storage pool is not active"));
        goto cleanup;
    }

     /* Just returns the volumes count */
    if (!vols) {
        ret = pool->volumes.count;
        goto cleanup;
    }

    if (VIR_ALLOC_N(tmp_vols, pool->volumes.count + 1) < 0) {
         virReportOOMError();
         goto cleanup;
    }

    for (i = 0 ; i < pool->volumes.count; i++) {
        if (!(vol = virGetStorageVol(obj->conn, pool->def->name,
                                     pool->volumes.objs[i]->name,
4674 4675
                                     pool->volumes.objs[i]->key,
                                     NULL, NULL)))
4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696
            goto cleanup;
        tmp_vols[nvols++] = vol;
    }

    *vols = tmp_vols;
    tmp_vols = NULL;
    ret = nvols;

 cleanup:
    if (tmp_vols) {
        for (i = 0; i < nvols; i++) {
            if (tmp_vols[i])
                virStorageVolFree(tmp_vols[i]);
        }
    }

    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}
C
Cole Robinson 已提交
4697 4698

static virStorageVolPtr
4699
testStorageVolumeLookupByName(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4700
                              const char *name ATTRIBUTE_UNUSED) {
4701 4702 4703
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4704
    virStorageVolPtr ret = NULL;
4705

4706
    testDriverLock(privconn);
4707 4708
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4709
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4710

4711
    if (privpool == NULL) {
4712
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4713
        goto cleanup;
4714 4715 4716 4717
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4718 4719
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4720
        goto cleanup;
4721 4722 4723 4724 4725
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4726 4727
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"), name);
4728
        goto cleanup;
C
Cole Robinson 已提交
4729 4730
    }

4731
    ret = virGetStorageVol(pool->conn, privpool->def->name,
4732 4733
                           privvol->name, privvol->key,
                           NULL, NULL);
4734 4735

cleanup:
4736 4737
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4738
    return ret;
C
Cole Robinson 已提交
4739 4740 4741 4742 4743 4744
}


static virStorageVolPtr
testStorageVolumeLookupByKey(virConnectPtr conn,
                             const char *key) {
4745
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4746
    unsigned int i;
4747
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4748

4749
    testDriverLock(privconn);
C
Cole Robinson 已提交
4750
    for (i = 0 ; i < privconn->pools.count ; i++) {
4751
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4752
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4753
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4754 4755
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4756 4757 4758 4759
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
4760 4761
                                       privvol->key,
                                       NULL, NULL);
4762
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4763 4764
                break;
            }
C
Cole Robinson 已提交
4765
        }
4766
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4767
    }
4768
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4769

4770
    if (!ret)
4771 4772
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
4773 4774

    return ret;
C
Cole Robinson 已提交
4775 4776 4777 4778 4779
}

static virStorageVolPtr
testStorageVolumeLookupByPath(virConnectPtr conn,
                              const char *path) {
4780
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4781
    unsigned int i;
4782
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4783

4784
    testDriverLock(privconn);
C
Cole Robinson 已提交
4785
    for (i = 0 ; i < privconn->pools.count ; i++) {
4786
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4787
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4788
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4789 4790
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

4791 4792 4793 4794
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
4795 4796
                                       privvol->key,
                                       NULL, NULL);
4797
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4798 4799
                break;
            }
C
Cole Robinson 已提交
4800
        }
4801
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4802
    }
4803
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4804

4805
    if (!ret)
4806 4807
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
4808 4809

    return ret;
C
Cole Robinson 已提交
4810 4811 4812
}

static virStorageVolPtr
4813
testStorageVolumeCreateXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4814
                           const char *xmldesc,
E
Eric Blake 已提交
4815 4816
                           unsigned int flags)
{
4817 4818
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4819 4820
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
4821

E
Eric Blake 已提交
4822 4823
    virCheckFlags(0, NULL);

4824
    testDriverLock(privconn);
4825 4826
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4827
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4828

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

    if (!virStoragePoolObjIsActive(privpool)) {
4835 4836
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4837
        goto cleanup;
4838
    }
C
Cole Robinson 已提交
4839

4840
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4841
    if (privvol == NULL)
4842
        goto cleanup;
4843 4844

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4845 4846
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
4847
        goto cleanup;
C
Cole Robinson 已提交
4848 4849 4850
    }

    /* Make sure enough space */
4851
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
4852
         privpool->def->capacity) {
4853 4854 4855
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
4856
        goto cleanup;
C
Cole Robinson 已提交
4857 4858 4859 4860
    }

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4861
        virReportOOMError();
4862
        goto cleanup;
C
Cole Robinson 已提交
4863 4864
    }

4865 4866 4867
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4868
        virReportOOMError();
4869
        goto cleanup;
C
Cole Robinson 已提交
4870 4871
    }

4872 4873
    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4874
        virReportOOMError();
4875
        goto cleanup;
C
Cole Robinson 已提交
4876 4877
    }

4878
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
4879 4880 4881
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

4884
    ret = virGetStorageVol(pool->conn, privpool->def->name,
4885 4886
                           privvol->name, privvol->key,
                           NULL, NULL);
4887
    privvol = NULL;
4888 4889 4890

cleanup:
    virStorageVolDefFree(privvol);
4891 4892
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4893
    return ret;
C
Cole Robinson 已提交
4894 4895
}

4896 4897 4898 4899
static virStorageVolPtr
testStorageVolumeCreateXMLFrom(virStoragePoolPtr pool,
                               const char *xmldesc,
                               virStorageVolPtr clonevol,
E
Eric Blake 已提交
4900 4901
                               unsigned int flags)
{
4902 4903 4904 4905 4906
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
4907 4908
    virCheckFlags(0, NULL);

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

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

    if (!virStoragePoolObjIsActive(privpool)) {
4920 4921
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4922 4923 4924
        goto cleanup;
    }

4925
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4926 4927 4928 4929
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4930 4931
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
4932 4933 4934 4935 4936
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
4937 4938 4939
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
4940 4941 4942 4943 4944 4945
        goto cleanup;
    }

    /* Make sure enough space */
    if ((privpool->def->allocation + privvol->allocation) >
         privpool->def->capacity) {
4946 4947 4948
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
4949 4950 4951 4952 4953 4954 4955
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4956
        virReportOOMError();
4957 4958 4959
        goto cleanup;
    }

4960 4961 4962
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4963
        virReportOOMError();
4964 4965 4966 4967 4968
        goto cleanup;
    }

    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4969
        virReportOOMError();
4970 4971 4972 4973 4974 4975 4976 4977 4978 4979
        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,
4980 4981
                           privvol->name, privvol->key,
                           NULL, NULL);
4982 4983 4984 4985 4986 4987 4988 4989 4990
    privvol = NULL;

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

C
Cole Robinson 已提交
4991
static int
4992
testStorageVolumeDelete(virStorageVolPtr vol,
E
Eric Blake 已提交
4993 4994
                        unsigned int flags)
{
4995 4996 4997
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
4998
    int i;
4999
    int ret = -1;
C
Cole Robinson 已提交
5000

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

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

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


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

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

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


C
Cole Robinson 已提交
5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052
    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;
        }
    }
5053
    ret = 0;
C
Cole Robinson 已提交
5054

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


static int testStorageVolumeTypeForPool(int pooltype) {

5064
    switch (pooltype) {
C
Cole Robinson 已提交
5065 5066 5067 5068 5069 5070 5071 5072 5073 5074
        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
5075
testStorageVolumeGetInfo(virStorageVolPtr vol,
C
Cole Robinson 已提交
5076
                         virStorageVolInfoPtr info) {
5077 5078 5079
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5080
    int ret = -1;
5081

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

    if (privpool == NULL) {
5088
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5089
        goto cleanup;
5090 5091 5092 5093 5094
    }

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

    if (privvol == NULL) {
5095 5096 5097
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5098
        goto cleanup;
5099 5100 5101
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5102 5103
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5104
        goto cleanup;
5105
    }
C
Cole Robinson 已提交
5106 5107 5108 5109 5110

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

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

static char *
5120
testStorageVolumeGetXMLDesc(virStorageVolPtr vol,
E
Eric Blake 已提交
5121 5122
                            unsigned int flags)
{
5123 5124 5125
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5126
    char *ret = NULL;
5127

E
Eric Blake 已提交
5128 5129
    virCheckFlags(0, NULL);

5130
    testDriverLock(privconn);
5131 5132
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5133
    testDriverUnlock(privconn);
5134 5135

    if (privpool == NULL) {
5136
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5137
        goto cleanup;
5138 5139 5140 5141 5142
    }

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

    if (privvol == NULL) {
5143 5144 5145
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5146
        goto cleanup;
5147
    }
C
Cole Robinson 已提交
5148

5149
    if (!virStoragePoolObjIsActive(privpool)) {
5150 5151
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5152
        goto cleanup;
5153 5154
    }

5155
    ret = virStorageVolDefFormat(privpool->def, privvol);
5156 5157

cleanup:
5158 5159
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5160
    return ret;
C
Cole Robinson 已提交
5161 5162 5163
}

static char *
5164 5165 5166 5167
testStorageVolumeGetPath(virStorageVolPtr vol) {
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5168
    char *ret = NULL;
5169

5170
    testDriverLock(privconn);
5171 5172
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5173
    testDriverUnlock(privconn);
5174 5175

    if (privpool == NULL) {
5176
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5177
        goto cleanup;
5178 5179 5180 5181 5182
    }

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

    if (privvol == NULL) {
5183 5184 5185
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5186
        goto cleanup;
5187 5188 5189
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5190 5191
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5192
        goto cleanup;
5193 5194
    }

C
Cole Robinson 已提交
5195
    ret = strdup(privvol->target.path);
5196
    if (ret == NULL)
5197
        virReportOOMError();
5198 5199

cleanup:
5200 5201
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5202 5203 5204
    return ret;
}

5205

5206
/* Node device implementations */
5207 5208
static virDrvOpenStatus testDevMonOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5209 5210 5211 5212
                                       unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224
    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;
}

5225 5226 5227
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5228
                     unsigned int flags)
5229 5230 5231 5232 5233
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
5234 5235
    virCheckFlags(0, -1);

5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250
    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 已提交
5251
                    unsigned int flags)
5252 5253 5254 5255 5256
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
5257 5258
    virCheckFlags(0, -1);

5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294
    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) {
5295
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

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

static char *
5308
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5309
                         unsigned int flags)
5310 5311 5312 5313 5314
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5315 5316
    virCheckFlags(0, NULL);

5317 5318 5319 5320 5321
    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
5322 5323 5324
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5325 5326 5327
        goto cleanup;
    }

5328
    ret = virNodeDeviceDefFormat(obj->def);
5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347

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) {
5348 5349 5350
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5351 5352 5353 5354 5355 5356
        goto cleanup;
    }

    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
5357
            virReportOOMError();
5358
    } else {
5359 5360
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5361 5362 5363 5364 5365 5366 5367 5368
    }

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

5369

5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383
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) {
5384 5385 5386
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414
        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) {
5415 5416 5417
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438
        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;
}

5439 5440 5441
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
E
Eric Blake 已提交
5442
                        unsigned int flags)
5443 5444 5445 5446 5447 5448 5449 5450 5451
{
    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 已提交
5452 5453
    virCheckFlags(0, NULL);

5454 5455
    testDriverLock(driver);

5456
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, NULL);
5457 5458 5459 5460 5461
    if (def == NULL) {
        goto cleanup;
    }

    /* We run these next two simply for validation */
5462
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1) {
5463 5464 5465
        goto cleanup;
    }

5466
    if (virNodeDeviceGetParentHost(&driver->devs,
5467 5468 5469 5470 5471 5472 5473 5474 5475 5476
                                   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))) {
5477
        virReportOOMError();
5478 5479 5480 5481 5482 5483 5484 5485 5486 5487
        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;

5488
        caps->data.scsi_host.host = virRandomBits(10);
5489 5490 5491 5492
        caps = caps->next;
    }


5493
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def))) {
5494 5495 5496 5497 5498 5499 5500 5501
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
cleanup:
    testDriverUnlock(driver);
5502
    virNodeDeviceDefFree(def);
5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521
    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) {
5522
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5523 5524 5525
        goto out;
    }

5526
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
5527 5528 5529 5530 5531
        goto out;
    }

    parent_name = strdup(obj->def->parent);
    if (parent_name == NULL) {
5532
        virReportOOMError();
5533 5534 5535 5536 5537 5538 5539 5540 5541 5542
        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 */
5543
    if (virNodeDeviceGetParentHost(&driver->devs,
5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562
                                   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;
}

5563 5564

/* Domain event implementations */
5565
static int
5566 5567 5568 5569
testDomainEventRegister(virConnectPtr conn,
                        virConnectDomainEventCallback callback,
                        void *opaque,
                        virFreeCallback freecb)
5570 5571 5572 5573 5574
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5575 5576 5577
    ret = virDomainEventStateRegister(conn,
                                      driver->domainEventState,
                                      callback, opaque, freecb);
5578 5579 5580 5581 5582
    testDriverUnlock(driver);

    return ret;
}

5583

5584
static int
5585 5586
testDomainEventDeregister(virConnectPtr conn,
                          virConnectDomainEventCallback callback)
5587 5588 5589 5590 5591
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5592 5593 5594
    ret = virDomainEventStateDeregister(conn,
                                        driver->domainEventState,
                                        callback);
5595 5596 5597 5598 5599
    testDriverUnlock(driver);

    return ret;
}

5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612

static int
testDomainEventRegisterAny(virConnectPtr conn,
                           virDomainPtr dom,
                           int eventID,
                           virConnectDomainEventGenericCallback callback,
                           void *opaque,
                           virFreeCallback freecb)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5613 5614 5615 5616
    if (virDomainEventStateRegisterID(conn,
                                      driver->domainEventState,
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
5617
        ret = -1;
5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630
    testDriverUnlock(driver);

    return ret;
}

static int
testDomainEventDeregisterAny(virConnectPtr conn,
                             int callbackID)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5631 5632 5633
    ret = virDomainEventStateDeregisterID(conn,
                                          driver->domainEventState,
                                          callbackID);
5634 5635 5636 5637 5638 5639
    testDriverUnlock(driver);

    return ret;
}


5640 5641 5642 5643
/* driver must be locked before calling */
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event)
{
5644
    virDomainEventStateQueue(driver->domainEventState, event);
5645 5646
}

5647 5648
static virDrvOpenStatus testSecretOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5649 5650 5651 5652
                                       unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5653 5654 5655
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5656
    conn->secretPrivateData = conn->privateData;
5657 5658 5659 5660 5661 5662 5663
    return VIR_DRV_OPEN_SUCCESS;
}

static int testSecretClose(virConnectPtr conn) {
    conn->secretPrivateData = NULL;
    return 0;
}
5664

5665 5666 5667

static virDrvOpenStatus testNWFilterOpen(virConnectPtr conn,
                                         virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5668 5669 5670 5671
                                         unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5672 5673 5674
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5675
    conn->nwfilterPrivateData = conn->privateData;
5676 5677 5678 5679 5680 5681 5682 5683
    return VIR_DRV_OPEN_SUCCESS;
}

static int testNWFilterClose(virConnectPtr conn) {
    conn->nwfilterPrivateData = NULL;
    return 0;
}

5684 5685 5686 5687 5688 5689 5690
static int testListAllDomains(virConnectPtr conn,
                              virDomainPtr **domains,
                              unsigned int flags)
{
    testConnPtr privconn = conn->privateData;
    int ret;

O
Osier Yang 已提交
5691
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
5692 5693 5694 5695 5696 5697 5698 5699

    testDriverLock(privconn);
    ret = virDomainList(conn, privconn->domains.objs, domains, flags);
    testDriverUnlock(privconn);

    return ret;
}

5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729
static int
testNodeGetCPUMap(virConnectPtr conn,
                  unsigned char **cpumap,
                  unsigned int *online,
                  unsigned int flags)
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(0, -1);

    testDriverLock(privconn);
    if (cpumap) {
        if (VIR_ALLOC_N(*cpumap, 1) < 0) {
            virReportOOMError();
            goto cleanup;
        }
        *cpumap[0] = 0x15;
    }

    if (online)
        *online = 3;

    ret = 8;

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

5730

5731
static virDriver testDriver = {
5732 5733
    .no = VIR_DRV_TEST,
    .name = "Test",
5734 5735 5736 5737 5738 5739 5740 5741 5742
    .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 */
5743
    .listAllDomains = testListAllDomains, /* 0.9.13 */
5744 5745 5746 5747 5748 5749 5750
    .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 */
5751
    .domainShutdownFlags = testShutdownDomainFlags, /* 0.9.10 */
5752 5753 5754 5755 5756 5757 5758 5759 5760
    .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 */
5761
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
5762
    .domainRestore = testDomainRestore, /* 0.3.2 */
5763
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777
    .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 */
5778
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
5779 5780 5781 5782
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
    .domainGetSchedulerParameters = testDomainGetSchedulerParams, /* 0.3.2 */
5783
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParamsFlags, /* 0.9.2 */
5784
    .domainSetSchedulerParameters = testDomainSetSchedulerParams, /* 0.3.2 */
5785
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParamsFlags, /* 0.9.2 */
5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797
    .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 */
5798
    .isAlive = testIsAlive, /* 0.9.8 */
5799
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
5800 5801 5802 5803
};

static virNetworkDriver testNetworkDriver = {
    "Test",
5804 5805 5806 5807 5808 5809
    .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 */
5810
    .listAllNetworks = testNetworkListAllNetworks, /* 0.10.2 */
5811 5812 5813 5814 5815
    .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 */
5816
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
5817 5818 5819 5820 5821 5822 5823 5824
    .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 */
5825 5826
};

L
Laine Stump 已提交
5827 5828
static virInterfaceDriver testInterfaceDriver = {
    "Test",                     /* name */
5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842
    .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 */
5843 5844 5845
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
5846 5847 5848
};


5849 5850
static virStorageDriver testStorageDriver = {
    .name = "Test",
5851 5852 5853 5854 5855 5856 5857
    .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 */
5858
    .listAllPools = testStorageListAllPools, /* 0.10.2 */
5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876
    .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 */
5877
    .poolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */
5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889

    .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 */
5890 5891
};

5892 5893
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905
    .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 */
5906 5907
};

5908 5909
static virSecretDriver testSecretDriver = {
    .name = "Test",
5910 5911
    .open = testSecretOpen, /* 0.7.1 */
    .close = testSecretClose, /* 0.7.1 */
5912
};
5913 5914


5915 5916
static virNWFilterDriver testNWFilterDriver = {
    .name = "Test",
5917 5918
    .open = testNWFilterOpen, /* 0.8.0 */
    .close = testNWFilterClose, /* 0.8.0 */
5919 5920
};

5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
5933 5934
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
5935 5936
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
5937 5938
    if (virRegisterDeviceMonitor(&testDevMonitor) < 0)
        return -1;
5939 5940
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
5941 5942
    if (virRegisterNWFilterDriver(&testNWFilterDriver) < 0)
        return -1;
5943

5944 5945
    return 0;
}