test_driver.c 167.6 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 "virerror.h"
36
#include "datatypes.h"
37
#include "test_driver.h"
38
#include "virbuffer.h"
39
#include "virutil.h"
40
#include "viruuid.h"
41
#include "capabilities.h"
42
#include "configmake.h"
43
#include "viralloc.h"
44
#include "network_conf.h"
L
Laine Stump 已提交
45
#include "interface_conf.h"
46
#include "domain_conf.h"
47
#include "domain_event.h"
48
#include "fdstream.h"
C
Cole Robinson 已提交
49
#include "storage_conf.h"
50
#include "node_device_conf.h"
51
#include "virxml.h"
52
#include "virthread.h"
53
#include "virlog.h"
E
Eric Blake 已提交
54
#include "virfile.h"
55
#include "virtypedparam.h"
56
#include "virrandom.h"
57

58 59
#define VIR_FROM_THIS VIR_FROM_TEST

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

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

69 70 71 72 73
#define MAX_CPUS 128

struct _testCell {
    unsigned long mem;
    int numCpus;
74
    virCapsHostNUMACellCPU cpus[MAX_CPUS];
75 76 77 78 79
};
typedef struct _testCell testCell;
typedef struct _testCell *testCellPtr;

#define MAX_CELLS 128
80

81
struct _testConn {
82
    virMutex lock;
83

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

99
    virDomainEventStatePtr domainEventState;
100 101 102
};
typedef struct _testConn testConn;
typedef struct _testConn *testConnPtr;
103

104
#define TEST_MODEL "i686"
105
#define TEST_MODEL_WORDSIZE 32
106
#define TEST_EMULATOR "/usr/bin/test-hv"
107

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

119

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


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

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

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


155
static virDomainXMLOptionPtr
156 157 158 159
testBuildXMLConfig(void)
{
    virDomainXMLPrivateDataCallbacks priv = { .alloc = testDomainObjPrivateAlloc,
                                              .free = testDomainObjPrivateFree };
160
    return virDomainXMLOptionNew(NULL, &priv, NULL);
161 162 163
}


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

172
    if ((caps = virCapabilitiesNew(VIR_ARCH_I686, 0, 0)) == NULL)
173
        goto no_memory;
174

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

180
    for (i = 0; i < privconn->numCells; i++) {
181 182 183 184 185 186 187 188 189
        virCapsHostNUMACellCPUPtr cpu_cells;

        if (VIR_ALLOC_N(cpu_cells, privconn->cells[i].numCpus) < 0)
            goto no_memory;

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


190
        if (virCapabilitiesAddHostNUMACell(caps, i, privconn->cells[i].numCpus,
191
                                           0, cpu_cells) < 0)
192
            goto no_memory;
193 194
    }

195 196 197
    for (i = 0; i < ARRAY_CARDINALITY(guest_types) ; i++) {
        if ((guest = virCapabilitiesAddGuest(caps,
                                             guest_types[i],
198
                                             VIR_ARCH_I686,
199 200 201 202 203
                                             TEST_EMULATOR,
                                             NULL,
                                             0,
                                             NULL)) == NULL)
            goto no_memory;
204

205 206 207 208 209 210 211
        if (virCapabilitiesAddGuestDomain(guest,
                                          "test",
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
            goto no_memory;
212

213 214 215 216
        if (virCapabilitiesAddGuestFeature(guest, "pae", 1, 1) == NULL)
            goto no_memory;
        if (virCapabilitiesAddGuestFeature(guest ,"nonpae", 1, 1) == NULL)
            goto no_memory;
217 218
    }

219 220 221 222 223
    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)
224 225
        goto no_memory;

226 227
    caps->host.secModels[0].doi = strdup("");
    if (!caps->host.secModels[0].doi)
228 229
        goto no_memory;

230
    return caps;
231

232
no_memory:
233
    virReportOOMError();
234
    virObjectUnref(caps);
235
    return NULL;
236 237
}

238

239 240 241
static const char *defaultDomainXML =
"<domain type='test'>"
"  <name>test</name>"
242
"  <uuid>6695eb01-f6a4-8304-79aa-97f2502e193f</uuid>"
243 244 245 246 247 248 249
"  <memory>8388608</memory>"
"  <currentMemory>2097152</currentMemory>"
"  <vcpu>2</vcpu>"
"  <os>"
"    <type>hvm</type>"
"  </os>"
"</domain>";
250 251


252 253 254
static const char *defaultNetworkXML =
"<network>"
"  <name>default</name>"
255
"  <uuid>dd8fe884-6c02-601e-7551-cca97df1c5df</uuid>"
256 257 258 259 260 261 262 263
"  <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>";
264

L
Laine Stump 已提交
265 266 267 268 269 270 271 272 273 274 275
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 已提交
276 277 278
static const char *defaultPoolXML =
"<pool type='dir'>"
"  <name>default-pool</name>"
279
"  <uuid>dfe224cb-28fb-8dd0-c4b2-64eb3f0f4566</uuid>"
C
Cole Robinson 已提交
280 281 282 283 284
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
"</pool>";

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
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";

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
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>";

326
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
C
Cole Robinson 已提交
327 328
static const unsigned long long defaultPoolAlloc = 0;

329
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool);
330
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
331

332
static char *
333
testDomainGenerateIfname(virDomainDefPtr domdef) {
334 335 336 337 338 339 340 341
    int maxif = 1024;
    int ifctr, i;

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

        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0) {
342
            virReportOOMError();
343 344 345 346 347 348
            return NULL;
        }

        /* Generate network interface names */
        for (i = 0 ; i < domdef->nnets ; i++) {
            if (domdef->nets[i]->ifname &&
349
                STREQ(domdef->nets[i]->ifname, ifname)) {
350 351 352 353 354 355 356 357 358
                found = 1;
                break;
            }
        }

        if (!found)
            return ifname;
    }

359 360
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Exceeded max iface limit %d"), maxif);
361 362 363
    return NULL;
}

364
static int
365
testDomainGenerateIfnames(virDomainDefPtr domdef)
366 367 368 369 370 371 372 373
{
    int i = 0;

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

374
        ifname = testDomainGenerateIfname(domdef);
375
        if (!ifname)
376
            return -1;
377 378 379 380

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

381
    return 0;
382 383
}

384 385 386 387 388 389 390 391 392 393 394 395
/* 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 已提交
396
    bool cpu;
397 398 399 400 401 402 403 404 405 406 407

    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 已提交
408 409 410
            if (virBitmapGetBit(dom->def->cpumask, j, &cpu) < 0)
                return -1;
            if (cpu) {
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 441 442 443 444 445 446 447 448 449 450
                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) {
451
        virReportOOMError();
452 453 454 455
        goto cleanup;
    }

    if (VIR_REALLOC_N(privdata->cpumaps, nvcpus * cpumaplen) < 0) {
456
        virReportOOMError();
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
        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;
    }

473
    dom->def->vcpus = nvcpus;
474 475 476 477 478
    ret = 0;
cleanup:
    return ret;
}

479 480
static void
testDomainShutdownState(virDomainPtr domain,
J
Jiri Denemark 已提交
481 482
                        virDomainObjPtr privdom,
                        virDomainShutoffReason reason)
483 484 485 486 487 488 489
{
    if (privdom->newDef) {
        virDomainDefFree(privdom->def);
        privdom->def = privdom->newDef;
        privdom->newDef = NULL;
    }

J
Jiri Denemark 已提交
490
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF, reason);
491 492 493 494 495
    privdom->def->id = -1;
    if (domain)
        domain->id = -1;
}

496
/* Set up domain runtime state */
497 498
static int
testDomainStartState(virConnectPtr conn,
J
Jiri Denemark 已提交
499 500
                     virDomainObjPtr dom,
                     virDomainRunningReason reason)
501 502
{
    testConnPtr privconn = conn->privateData;
503
    int ret = -1;
504

505 506 507
    if (testDomainUpdateVCPUs(conn, dom, dom->def->vcpus, 1) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
508
    virDomainObjSetState(dom, VIR_DOMAIN_RUNNING, reason);
509 510
    dom->def->id = privconn->nextDomID++;

511
    if (virDomainObjSetDefTransient(privconn->caps,
512
                                    privconn->xmlopt,
513
                                    dom, false) < 0) {
514 515 516
        goto cleanup;
    }

517 518
    ret = 0;
cleanup:
519
    if (ret < 0)
J
Jiri Denemark 已提交
520
        testDomainShutdownState(NULL, dom, VIR_DOMAIN_SHUTOFF_FAILED);
521
    return ret;
522
}
523

524
static int testOpenDefault(virConnectPtr conn) {
525
    int u;
526
    testConnPtr privconn;
527 528 529 530
    virDomainDefPtr domdef = NULL;
    virDomainObjPtr domobj = NULL;
    virNetworkDefPtr netdef = NULL;
    virNetworkObjPtr netobj = NULL;
L
Laine Stump 已提交
531 532
    virInterfaceDefPtr interfacedef = NULL;
    virInterfaceObjPtr interfaceobj = NULL;
C
Cole Robinson 已提交
533 534
    virStoragePoolDefPtr pooldef = NULL;
    virStoragePoolObjPtr poolobj = NULL;
535 536
    virNodeDeviceDefPtr nodedef = NULL;
    virNodeDeviceObjPtr nodeobj = NULL;
537

538
    if (VIR_ALLOC(privconn) < 0) {
539
        virReportOOMError();
540 541
        return VIR_DRV_OPEN_ERROR;
    }
542
    if (virMutexInit(&privconn->lock) < 0) {
543 544
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
545 546 547 548
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

549
    testDriverLock(privconn);
550
    conn->privateData = privconn;
551

552
    if (!(privconn->domains = virDomainObjListNew()))
553 554
        goto error;

555
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
556

557
    /* Numa setup */
558 559 560 561 562 563
    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++) {
564 565 566 567 568 569 570 571 572 573
        virBitmapPtr siblings = virBitmapNew(16);
        if (!siblings) {
            virReportOOMError();
            goto error;
        }
        ignore_value(virBitmapSetBit(siblings, u));
        privconn->cells[u / 8].cpus[(u % 8)].id = u;
        privconn->cells[u / 8].cpus[(u % 8)].socket_id = u / 8;
        privconn->cells[u / 8].cpus[(u % 8)].core_id = u % 8;
        privconn->cells[u / 8].cpus[(u % 8)].siblings = siblings;
574 575
    }

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

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

582 583
    privconn->nextDomID = 1;

584 585
    if (!(domdef = virDomainDefParseString(defaultDomainXML,
                                           privconn->caps,
586
                                           privconn->xmlopt,
M
Matthias Bolte 已提交
587
                                           1 << VIR_DOMAIN_VIRT_TEST,
588
                                           VIR_DOMAIN_XML_INACTIVE)))
589
        goto error;
M
Matthias Bolte 已提交
590

591
    if (testDomainGenerateIfnames(domdef) < 0)
592
        goto error;
593
    if (!(domobj = virDomainObjListAdd(privconn->domains,
594
                                       domdef,
595
                                       privconn->xmlopt,
596
                                       0, NULL)))
597 598
        goto error;
    domdef = NULL;
599

600
    domobj->persistent = 1;
J
Jiri Denemark 已提交
601
    if (testDomainStartState(conn, domobj, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
602
        virObjectUnlock(domobj);
603 604 605
        goto error;
    }

606
    virObjectUnlock(domobj);
607

608
    if (!(netdef = virNetworkDefParseString(defaultNetworkXML)))
609
        goto error;
610
    if (!(netobj = virNetworkAssignDef(&privconn->networks, netdef, false))) {
611 612 613 614 615
        virNetworkDefFree(netdef);
        goto error;
    }
    netobj->active = 1;
    netobj->persistent = 1;
616
    virNetworkObjUnlock(netobj);
617

618
    if (!(interfacedef = virInterfaceDefParseString(defaultInterfaceXML)))
L
Laine Stump 已提交
619
        goto error;
620
    if (!(interfaceobj = virInterfaceAssignDef(&privconn->ifaces, interfacedef))) {
L
Laine Stump 已提交
621 622 623 624 625 626
        virInterfaceDefFree(interfacedef);
        goto error;
    }
    interfaceobj->active = 1;
    virInterfaceObjUnlock(interfaceobj);

627
    if (!(pooldef = virStoragePoolDefParseString(defaultPoolXML)))
C
Cole Robinson 已提交
628 629
        goto error;

630
    if (!(poolobj = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
631 632 633 634
                                               pooldef))) {
        virStoragePoolDefFree(pooldef);
        goto error;
    }
635

636
    if (testStoragePoolObjSetDefaults(poolobj) == -1) {
637
        virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
638
        goto error;
639
    }
C
Cole Robinson 已提交
640
    poolobj->active = 1;
641
    virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
642

643
    /* Init default node device */
644
    if (!(nodedef = virNodeDeviceDefParseString(defaultNodeXML, 0, NULL)))
645
        goto error;
646
    if (!(nodeobj = virNodeDeviceAssignDef(&privconn->devs,
647 648 649 650 651 652
                                           nodedef))) {
        virNodeDeviceDefFree(nodedef);
        goto error;
    }
    virNodeDeviceObjUnlock(nodeobj);

653
    testDriverUnlock(privconn);
654

655 656 657
    return VIR_DRV_OPEN_SUCCESS;

error:
658
    virObjectUnref(privconn->domains);
659
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
660
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
661
    virStoragePoolObjListFree(&privconn->pools);
662
    virNodeDeviceObjListFree(&privconn->devs);
663
    virObjectUnref(privconn->caps);
664
    testDriverUnlock(privconn);
665
    conn->privateData = NULL;
666
    VIR_FREE(privconn);
667
    virDomainDefFree(domdef);
668
    return VIR_DRV_OPEN_ERROR;
669 670 671 672
}


static char *testBuildFilename(const char *relativeTo,
673 674 675 676
                               const char *filename) {
    char *offset;
    int baseLen;
    if (!filename || filename[0] == '\0')
677
        return NULL;
678 679 680
    if (filename[0] == '/')
        return strdup(filename);

681
    offset = strrchr(relativeTo, '/');
682
    if ((baseLen = (offset-relativeTo+1))) {
683
        char *absFile;
C
Chris Lalancette 已提交
684 685
        int totalLen = baseLen + strlen(filename) + 1;
        if (VIR_ALLOC_N(absFile, totalLen) < 0)
686
            return NULL;
C
Chris Lalancette 已提交
687 688 689 690
        if (virStrncpy(absFile, relativeTo, baseLen, totalLen) == NULL) {
            VIR_FREE(absFile);
            return NULL;
        }
691 692 693 694 695
        strcat(absFile, filename);
        return absFile;
    } else {
        return strdup(filename);
    }
696 697
}

698
static int testOpenVolumesForPool(xmlDocPtr xml,
699 700 701 702 703 704 705
                                  xmlXPathContextPtr ctxt,
                                  const char *file,
                                  virStoragePoolObjPtr pool,
                                  int poolidx) {
    char *vol_xpath;
    int i, ret, func_ret = -1;
    xmlNodePtr *vols = NULL;
706
    virStorageVolDefPtr def = NULL;
707 708 709

    /* Find storage volumes */
    if (virAsprintf(&vol_xpath, "/node/pool[%d]/volume", poolidx) < 0) {
710
        virReportOOMError();
711 712 713
        goto error;
    }

714
    ret = virXPathNodeSet(vol_xpath, ctxt, &vols);
715 716 717 718 719 720 721 722 723 724 725
    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) {
726 727
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving volume filename"));
728 729 730
                goto error;
            }

731
            def = virStorageVolDefParseFile(pool->def, absFile);
732 733 734 735
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
736
            if ((def = virStorageVolDefParseNode(pool->def, xml,
737 738 739 740 741 742 743
                                                 vols[i])) == NULL) {
                goto error;
            }
        }

        if (VIR_REALLOC_N(pool->volumes.objs,
                          pool->volumes.count+1) < 0) {
744
            virReportOOMError();
745 746 747
            goto error;
        }

748 749 750 751 752 753 754
        if (def->target.path == NULL) {
            if (virAsprintf(&def->target.path, "%s/%s",
                            pool->def->target.path,
                            def->name) == -1) {
                virReportOOMError();
                goto error;
            }
755 756 757
        }

        if (def->key == NULL) {
758 759 760 761 762
            def->key = strdup(def->target.path);
            if (def->key == NULL) {
                virReportOOMError();
                goto error;
            }
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
        }

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

780
static int testOpenFromFile(virConnectPtr conn,
781
                            const char *file) {
782
    int i, ret;
783 784
    long l;
    char *str;
785
    xmlDocPtr xml = NULL;
786 787
    xmlNodePtr *domains = NULL, *networks = NULL, *ifaces = NULL,
               *pools = NULL, *devs = NULL;
788 789
    xmlXPathContextPtr ctxt = NULL;
    virNodeInfoPtr nodeInfo;
790
    virNetworkObjPtr net;
L
Laine Stump 已提交
791
    virInterfaceObjPtr iface;
792
    virDomainObjPtr dom;
793 794
    testConnPtr privconn;
    if (VIR_ALLOC(privconn) < 0) {
795
        virReportOOMError();
796 797
        return VIR_DRV_OPEN_ERROR;
    }
798
    if (virMutexInit(&privconn->lock) < 0) {
799 800
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
801 802 803 804
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

805
    testDriverLock(privconn);
806
    conn->privateData = privconn;
807

808
    if (!(privconn->domains = virDomainObjListNew()))
809 810
        goto error;

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

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

817
    if (!(xml = virXMLParseFileCtxt(file, &ctxt))) {
818
        goto error;
819 820
    }

821
    if (!xmlStrEqual(ctxt->node->name, BAD_CAST "node")) {
822 823
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Root element is not 'node'"));
824
        goto error;
825 826
    }

827
    privconn->nextDomID = 1;
828
    privconn->numCells = 0;
E
Eric Blake 已提交
829 830
    if ((privconn->path = strdup(file)) == NULL) {
        virReportOOMError();
C
Chris Lalancette 已提交
831 832
        goto error;
    }
833 834 835
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    nodeInfo = &privconn->nodeInfo;
836
    ret = virXPathLong("string(/node/cpu/nodes[1])", ctxt, &l);
837 838 839
    if (ret == 0) {
        nodeInfo->nodes = l;
    } else if (ret == -2) {
840 841
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu nodes value"));
842
        goto error;
843
    }
844

845
    ret = virXPathLong("string(/node/cpu/sockets[1])", ctxt, &l);
846 847 848
    if (ret == 0) {
        nodeInfo->sockets = l;
    } else if (ret == -2) {
849 850
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu sockets value"));
851
        goto error;
852
    }
853

854
    ret = virXPathLong("string(/node/cpu/cores[1])", ctxt, &l);
855 856 857
    if (ret == 0) {
        nodeInfo->cores = l;
    } else if (ret == -2) {
858 859
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu cores value"));
860
        goto error;
861 862
    }

863
    ret = virXPathLong("string(/node/cpu/threads[1])", ctxt, &l);
864 865 866
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
867 868
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu threads value"));
869
        goto error;
870
    }
871

872
    nodeInfo->cpus = nodeInfo->cores * nodeInfo->threads * nodeInfo->sockets * nodeInfo->nodes;
873
    ret = virXPathLong("string(/node/cpu/active[1])", ctxt, &l);
874 875
    if (ret == 0) {
        if (l < nodeInfo->cpus) {
876 877
            nodeInfo->cpus = l;
        }
878
    } else if (ret == -2) {
879 880
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu active value"));
881
        goto error;
882
    }
883
    ret = virXPathLong("string(/node/cpu/mhz[1])", ctxt, &l);
884 885 886
    if (ret == 0) {
        nodeInfo->mhz = l;
    } else if (ret == -2) {
887 888
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu mhz value"));
889
        goto error;
890 891
    }

892
    str = virXPathString("string(/node/cpu/model[1])", ctxt);
893
    if (str != NULL) {
C
Chris Lalancette 已提交
894
        if (virStrcpyStatic(nodeInfo->model, str) == NULL) {
895 896
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Model %s too big for destination"), str);
C
Chris Lalancette 已提交
897 898 899
            VIR_FREE(str);
            goto error;
        }
900
        VIR_FREE(str);
901 902
    }

903
    ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
904 905 906
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
907 908
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node memory value"));
909
        goto error;
910
    }
911

912
    ret = virXPathNodeSet("/node/domain", ctxt, &domains);
913
    if (ret < 0) {
914
        goto error;
915
    }
916

917
    for (i = 0 ; i < ret ; i++) {
918 919 920 921 922 923
        virDomainDefPtr def;
        char *relFile = virXMLPropString(domains[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
924 925
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving domain filename"));
926 927
                goto error;
            }
928 929
            def = virDomainDefParseFile(absFile, privconn->caps,
                                        privconn->xmlopt,
M
Matthias Bolte 已提交
930
                                        1 << VIR_DOMAIN_VIRT_TEST,
931
                                        VIR_DOMAIN_XML_INACTIVE);
932
            VIR_FREE(absFile);
933 934 935
            if (!def)
                goto error;
        } else {
936 937
            if ((def = virDomainDefParseNode(xml, domains[i],
                                             privconn->caps, privconn->xmlopt,
M
Matthias Bolte 已提交
938
                                             1 << VIR_DOMAIN_VIRT_TEST,
939
                                             VIR_DOMAIN_XML_INACTIVE)) == NULL)
940 941 942
                goto error;
        }

943
        if (testDomainGenerateIfnames(def) < 0 ||
944
            !(dom = virDomainObjListAdd(privconn->domains,
945
                                        def,
946
                                        privconn->xmlopt,
947
                                        0, NULL))) {
948
            virDomainDefFree(def);
949 950
            goto error;
        }
951

952
        dom->persistent = 1;
J
Jiri Denemark 已提交
953
        if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
954
            virObjectUnlock(dom);
955 956 957
            goto error;
        }

958
        virObjectUnlock(dom);
959
    }
960
    VIR_FREE(domains);
961

962
    ret = virXPathNodeSet("/node/network", ctxt, &networks);
963 964 965 966 967 968 969 970 971
    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);
972
            if (!absFile) {
973 974
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving network filename"));
975 976
                goto error;
            }
977

978
            def = virNetworkDefParseFile(absFile);
979
            VIR_FREE(absFile);
980 981 982
            if (!def)
                goto error;
        } else {
983
            if ((def = virNetworkDefParseNode(xml, networks[i])) == NULL)
984
                goto error;
985
        }
986
        if (!(net = virNetworkAssignDef(&privconn->networks, def, false))) {
987 988
            virNetworkDefFree(def);
            goto error;
989
        }
990
        net->persistent = 1;
991
        net->active = 1;
992
        virNetworkObjUnlock(net);
993
    }
994
    VIR_FREE(networks);
995

L
Laine Stump 已提交
996
    /* Parse interface definitions */
997
    ret = virXPathNodeSet("/node/interface", ctxt, &ifaces);
L
Laine Stump 已提交
998 999 1000 1001 1002 1003 1004 1005 1006 1007
    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) {
1008 1009
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving interface filename"));
L
Laine Stump 已提交
1010 1011 1012
                goto error;
            }

1013
            def = virInterfaceDefParseFile(absFile);
L
Laine Stump 已提交
1014 1015 1016 1017
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
1018
            if ((def = virInterfaceDefParseNode(xml, ifaces[i])) == NULL)
L
Laine Stump 已提交
1019 1020
                goto error;
        }
1021

1022
        if (!(iface = virInterfaceAssignDef(&privconn->ifaces, def))) {
L
Laine Stump 已提交
1023 1024 1025
            virInterfaceDefFree(def);
            goto error;
        }
1026 1027

        iface->active = 1;
L
Laine Stump 已提交
1028 1029 1030 1031
        virInterfaceObjUnlock(iface);
    }
    VIR_FREE(ifaces);

C
Cole Robinson 已提交
1032
    /* Parse Storage Pool list */
1033
    ret = virXPathNodeSet("/node/pool", ctxt, &pools);
C
Cole Robinson 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
    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) {
1045 1046
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving pool filename"));
C
Cole Robinson 已提交
1047 1048 1049
                goto error;
            }

1050
            def = virStoragePoolDefParseFile(absFile);
C
Cole Robinson 已提交
1051 1052 1053 1054
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
1055
            if ((def = virStoragePoolDefParseNode(xml,
1056
                                                  pools[i])) == NULL) {
C
Cole Robinson 已提交
1057 1058 1059 1060
                goto error;
            }
        }

1061
        if (!(pool = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
1062 1063 1064 1065 1066
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

1067
        if (testStoragePoolObjSetDefaults(pool) == -1) {
1068
            virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1069
            goto error;
1070
        }
C
Cole Robinson 已提交
1071
        pool->active = 1;
1072 1073

        /* Find storage volumes */
1074
        if (testOpenVolumesForPool(xml, ctxt, file, pool, i+1) < 0) {
1075 1076 1077 1078
            virStoragePoolObjUnlock(pool);
            goto error;
        }

1079
        virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1080
    }
1081
    VIR_FREE(pools);
C
Cole Robinson 已提交
1082

1083
    ret = virXPathNodeSet("/node/device", ctxt, &devs);
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
    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) {
1097 1098
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving device filename"));
1099 1100 1101
                goto error;
            }

1102
            def = virNodeDeviceDefParseFile(absFile, 0, NULL);
1103 1104 1105 1106
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
1107
            if ((def = virNodeDeviceDefParseNode(xml, devs[i], 0, NULL)) == NULL)
1108 1109
                goto error;
        }
1110
        if (!(dev = virNodeDeviceAssignDef(&privconn->devs, def))) {
1111 1112 1113 1114 1115 1116 1117 1118
            virNodeDeviceDefFree(def);
            goto error;
        }
        virNodeDeviceObjUnlock(dev);
    }
    VIR_FREE(devs);


J
Jim Meyering 已提交
1119
    xmlXPathFreeContext(ctxt);
1120
    xmlFreeDoc(xml);
1121
    testDriverUnlock(privconn);
1122

1123
    return 0;
1124 1125

 error:
J
Jim Meyering 已提交
1126
    xmlXPathFreeContext(ctxt);
1127
    xmlFreeDoc(xml);
1128 1129
    VIR_FREE(domains);
    VIR_FREE(networks);
L
Laine Stump 已提交
1130
    VIR_FREE(ifaces);
C
Cole Robinson 已提交
1131
    VIR_FREE(pools);
1132
    VIR_FREE(devs);
1133
    virObjectUnref(privconn->domains);
1134
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1135
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1136
    virStoragePoolObjListFree(&privconn->pools);
E
Eric Blake 已提交
1137
    VIR_FREE(privconn->path);
1138
    testDriverUnlock(privconn);
1139
    VIR_FREE(privconn);
1140
    conn->privateData = NULL;
1141
    return VIR_DRV_OPEN_ERROR;
1142 1143
}

1144

1145 1146 1147
static virDrvOpenStatus testConnectOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                        unsigned int flags)
1148
{
1149
    int ret;
1150
    testConnPtr privconn;
1151

E
Eric Blake 已提交
1152 1153
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1154
    if (!conn->uri)
1155
        return VIR_DRV_OPEN_DECLINED;
1156

1157
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1158
        return VIR_DRV_OPEN_DECLINED;
1159

1160
    /* Remote driver should handle these. */
1161
    if (conn->uri->server)
1162 1163
        return VIR_DRV_OPEN_DECLINED;

1164
    /* From this point on, the connection is for us. */
1165 1166 1167
    if (!conn->uri->path
        || conn->uri->path[0] == '\0'
        || (conn->uri->path[0] == '/' && conn->uri->path[1] == '\0')) {
1168 1169
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("testOpen: supply a path or use test:///default"));
1170 1171
        return VIR_DRV_OPEN_ERROR;
    }
1172

1173
    if (STREQ(conn->uri->path, "/default"))
1174 1175
        ret = testOpenDefault(conn);
    else
1176
        ret = testOpenFromFile(conn,
1177
                               conn->uri->path);
1178

1179 1180 1181 1182 1183
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

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

1185
    privconn->domainEventState = virDomainEventStateNew();
1186
    if (!privconn->domainEventState) {
1187
        testDriverUnlock(privconn);
1188
        testConnectClose(conn);
1189
        return VIR_DRV_OPEN_ERROR;
1190 1191
    }

1192 1193 1194
    testDriverUnlock(privconn);

    return VIR_DRV_OPEN_SUCCESS;
1195 1196
}

1197
static int testConnectClose(virConnectPtr conn)
1198
{
1199
    testConnPtr privconn = conn->privateData;
1200
    testDriverLock(privconn);
1201
    virObjectUnref(privconn->caps);
1202
    virObjectUnref(privconn->xmlopt);
1203
    virObjectUnref(privconn->domains);
D
Daniel P. Berrange 已提交
1204
    virNodeDeviceObjListFree(&privconn->devs);
1205
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1206
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1207
    virStoragePoolObjListFree(&privconn->pools);
1208
    virDomainEventStateFree(privconn->domainEventState);
E
Eric Blake 已提交
1209
    VIR_FREE(privconn->path);
1210

1211
    testDriverUnlock(privconn);
1212
    virMutexDestroy(&privconn->lock);
1213

1214
    VIR_FREE(privconn);
1215
    conn->privateData = NULL;
1216
    return 0;
1217 1218
}

1219 1220
static int testConnectGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                                 unsigned long *hvVer)
1221
{
1222
    *hvVer = 2;
1223
    return 0;
1224 1225
}

1226
static int testConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
1227 1228 1229 1230
{
    return 1;
}

1231
static int testConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
1232 1233 1234 1235
{
    return 0;
}

1236
static int testConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
1237 1238 1239 1240
{
    return 1;
}

1241 1242
static int testConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type ATTRIBUTE_UNUSED)
1243 1244 1245 1246 1247 1248
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1249
{
1250
    testConnPtr privconn = conn->privateData;
1251
    testDriverLock(privconn);
1252
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1253
    testDriverUnlock(privconn);
1254
    return 0;
1255 1256
}

1257
static char *testConnectGetCapabilities(virConnectPtr conn)
1258
{
1259
    testConnPtr privconn = conn->privateData;
1260
    char *xml;
1261
    testDriverLock(privconn);
1262
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL)
1263
        virReportOOMError();
1264
    testDriverUnlock(privconn);
1265
    return xml;
1266 1267
}

1268
static int testConnectNumOfDomains(virConnectPtr conn)
1269
{
1270
    testConnPtr privconn = conn->privateData;
1271
    int count;
1272

1273
    testDriverLock(privconn);
1274
    count = virDomainObjListNumOfDomains(privconn->domains, 1);
1275
    testDriverUnlock(privconn);
1276

1277
    return count;
1278 1279
}

1280 1281 1282 1283 1284 1285 1286
static int testDomainIsActive(virDomainPtr dom)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
1287
    obj = virDomainObjListFindByUUID(privconn->domains, dom->uuid);
1288 1289
    testDriverUnlock(privconn);
    if (!obj) {
1290
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1291 1292 1293 1294 1295 1296
        goto cleanup;
    }
    ret = virDomainObjIsActive(obj);

cleanup:
    if (obj)
1297
        virObjectUnlock(obj);
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
    return ret;
}

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

    testDriverLock(privconn);
1308
    obj = virDomainObjListFindByUUID(privconn->domains, dom->uuid);
1309 1310
    testDriverUnlock(privconn);
    if (!obj) {
1311
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1312 1313 1314 1315 1316 1317
        goto cleanup;
    }
    ret = obj->persistent;

cleanup:
    if (obj)
1318
        virObjectUnlock(obj);
1319 1320 1321
    return ret;
}

1322 1323 1324 1325 1326
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

1327
static virDomainPtr
1328
testDomainCreateXML(virConnectPtr conn, const char *xml,
1329
                      unsigned int flags)
1330
{
1331
    testConnPtr privconn = conn->privateData;
1332
    virDomainPtr ret = NULL;
1333
    virDomainDefPtr def;
1334
    virDomainObjPtr dom = NULL;
1335
    virDomainEventPtr event = NULL;
1336

1337 1338
    virCheckFlags(0, NULL);

1339
    testDriverLock(privconn);
1340 1341
    if ((def = virDomainDefParseString(xml,privconn->caps, privconn->xmlopt,
                                       1 << VIR_DOMAIN_VIRT_TEST,
1342
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1343
        goto cleanup;
1344

1345
    if (testDomainGenerateIfnames(def) < 0)
1346
        goto cleanup;
1347
    if (!(dom = virDomainObjListAdd(privconn->domains,
1348
                                    def,
1349
                                    privconn->xmlopt,
1350 1351
                                    VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                    NULL)))
1352 1353
        goto cleanup;
    def = NULL;
1354

J
Jiri Denemark 已提交
1355
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0)
1356
        goto cleanup;
1357

1358 1359 1360 1361
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1362
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1363
    if (ret)
1364
        ret->id = dom->def->id;
1365 1366

cleanup:
1367
    if (dom)
1368
        virObjectUnlock(dom);
1369 1370
    if (event)
        testDomainEventQueue(privconn, event);
1371
    virDomainDefFree(def);
1372
    testDriverUnlock(privconn);
1373
    return ret;
1374 1375 1376
}


1377
static virDomainPtr testDomainLookupByID(virConnectPtr conn,
1378
                                         int id)
1379
{
1380
    testConnPtr privconn = conn->privateData;
1381 1382
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1383

1384
    testDriverLock(privconn);
1385
    dom = virDomainObjListFindByID(privconn->domains, id);
1386 1387 1388
    testDriverUnlock(privconn);

    if (dom == NULL) {
1389
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1390
        goto cleanup;
1391 1392
    }

1393
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1394 1395 1396 1397
    if (ret)
        ret->id = dom->def->id;

cleanup:
1398
    if (dom)
1399
        virObjectUnlock(dom);
1400
    return ret;
1401 1402
}

1403
static virDomainPtr testDomainLookupByUUID(virConnectPtr conn,
1404
                                           const unsigned char *uuid)
1405
{
1406
    testConnPtr privconn = conn->privateData;
1407 1408
    virDomainPtr ret = NULL;
    virDomainObjPtr dom ;
1409

1410
    testDriverLock(privconn);
1411
    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
1412 1413 1414
    testDriverUnlock(privconn);

    if (dom == NULL) {
1415
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1416
        goto cleanup;
1417
    }
1418

1419
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1420 1421 1422 1423
    if (ret)
        ret->id = dom->def->id;

cleanup:
1424
    if (dom)
1425
        virObjectUnlock(dom);
1426
    return ret;
1427 1428
}

1429
static virDomainPtr testDomainLookupByName(virConnectPtr conn,
1430
                                           const char *name)
1431
{
1432
    testConnPtr privconn = conn->privateData;
1433 1434
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1435

1436
    testDriverLock(privconn);
1437
    dom = virDomainObjListFindByName(privconn->domains, name);
1438 1439 1440
    testDriverUnlock(privconn);

    if (dom == NULL) {
1441
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1442
        goto cleanup;
1443
    }
1444

1445
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1446 1447 1448 1449
    if (ret)
        ret->id = dom->def->id;

cleanup:
1450
    if (dom)
1451
        virObjectUnlock(dom);
1452
    return ret;
1453 1454
}

1455 1456 1457
static int testConnectListDomains(virConnectPtr conn,
                                  int *ids,
                                  int maxids)
1458
{
1459
    testConnPtr privconn = conn->privateData;
1460
    int n;
1461

1462
    testDriverLock(privconn);
1463
    n = virDomainObjListGetActiveIDs(privconn->domains, ids, maxids);
1464
    testDriverUnlock(privconn);
1465

1466
    return n;
1467 1468
}

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

1476
    testDriverLock(privconn);
1477 1478
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1479 1480

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

J
Jiri Denemark 已提交
1485
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1486 1487 1488
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1489

1490
    if (!privdom->persistent) {
1491 1492
        virDomainObjListRemove(privconn->domains,
                               privdom);
1493
        privdom = NULL;
1494
    }
1495 1496 1497

    ret = 0;
cleanup:
1498
    if (privdom)
1499
        virObjectUnlock(privdom);
1500 1501
    if (event)
        testDomainEventQueue(privconn, event);
1502
    testDriverUnlock(privconn);
1503
    return ret;
1504 1505
}

1506
static int testDomainResume(virDomainPtr domain)
1507
{
1508 1509
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1510
    virDomainEventPtr event = NULL;
1511
    int ret = -1;
1512

1513
    testDriverLock(privconn);
1514 1515
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1516
    testDriverUnlock(privconn);
1517 1518

    if (privdom == NULL) {
1519
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1520
        goto cleanup;
1521
    }
1522

J
Jiri Denemark 已提交
1523
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_PAUSED) {
1524 1525
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
                       domain->name);
1526
        goto cleanup;
1527
    }
1528

J
Jiri Denemark 已提交
1529 1530
    virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                         VIR_DOMAIN_RUNNING_UNPAUSED);
1531 1532 1533
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1534 1535 1536
    ret = 0;

cleanup:
1537
    if (privdom)
1538
        virObjectUnlock(privdom);
1539 1540 1541 1542 1543
    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1544
    return ret;
1545 1546
}

1547
static int testDomainSuspend(virDomainPtr domain)
1548
{
1549 1550
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1551
    virDomainEventPtr event = NULL;
1552
    int ret = -1;
J
Jiri Denemark 已提交
1553
    int state;
1554

1555
    testDriverLock(privconn);
1556 1557
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1558
    testDriverUnlock(privconn);
1559 1560

    if (privdom == NULL) {
1561
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1562
        goto cleanup;
1563
    }
1564

J
Jiri Denemark 已提交
1565 1566
    state = virDomainObjGetState(privdom, NULL);
    if (state == VIR_DOMAIN_SHUTOFF || state == VIR_DOMAIN_PAUSED) {
1567 1568
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
                       domain->name);
1569
        goto cleanup;
1570
    }
1571

J
Jiri Denemark 已提交
1572
    virDomainObjSetState(privdom, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
1573 1574 1575
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1576 1577 1578
    ret = 0;

cleanup:
1579
    if (privdom)
1580
        virObjectUnlock(privdom);
1581 1582 1583 1584 1585 1586

    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1587
    return ret;
1588 1589
}

1590
static int testDomainShutdownFlags(virDomainPtr domain,
1591
                                   unsigned int flags)
1592
{
1593 1594
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1595
    virDomainEventPtr event = NULL;
1596
    int ret = -1;
1597

1598 1599
    virCheckFlags(0, -1);

1600
    testDriverLock(privconn);
1601 1602
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1603 1604

    if (privdom == NULL) {
1605
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1606
        goto cleanup;
1607
    }
1608

J
Jiri Denemark 已提交
1609
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
1610 1611
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain '%s' not running"), domain->name);
1612
        goto cleanup;
1613
    }
1614

J
Jiri Denemark 已提交
1615
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1616 1617 1618
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1619

1620
    if (!privdom->persistent) {
1621 1622
        virDomainObjListRemove(privconn->domains,
                               privdom);
1623 1624
        privdom = NULL;
    }
1625

1626
    ret = 0;
1627
cleanup:
1628
    if (privdom)
1629
        virObjectUnlock(privdom);
1630 1631
    if (event)
        testDomainEventQueue(privconn, event);
1632
    testDriverUnlock(privconn);
1633
    return ret;
1634 1635
}

1636
static int testDomainShutdown(virDomainPtr domain)
1637
{
1638
    return testDomainShutdownFlags(domain, 0);
1639 1640
}

1641
/* Similar behaviour as shutdown */
1642
static int testDomainReboot(virDomainPtr domain,
1643
                            unsigned int action ATTRIBUTE_UNUSED)
1644
{
1645 1646
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1647
    virDomainEventPtr event = NULL;
1648
    int ret = -1;
1649

1650
    testDriverLock(privconn);
1651 1652
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1653 1654

    if (privdom == NULL) {
1655
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1656
        goto cleanup;
1657
    }
1658

J
Jiri Denemark 已提交
1659 1660 1661
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

1662 1663
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
J
Jiri Denemark 已提交
1664 1665
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1666 1667
        break;

1668
    case VIR_DOMAIN_LIFECYCLE_RESTART:
J
Jiri Denemark 已提交
1669 1670
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1671 1672
        break;

1673
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
J
Jiri Denemark 已提交
1674 1675
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1676 1677
        break;

1678
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
J
Jiri Denemark 已提交
1679 1680
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1681
        break;
1682

1683
    default:
J
Jiri Denemark 已提交
1684 1685
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1686 1687
        break;
    }
1688

J
Jiri Denemark 已提交
1689 1690
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1691 1692 1693
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1694

1695
        if (!privdom->persistent) {
1696 1697
            virDomainObjListRemove(privconn->domains,
                                   privdom);
1698 1699
            privdom = NULL;
        }
1700 1701
    }

1702 1703
    ret = 0;
cleanup:
1704
    if (privdom)
1705
        virObjectUnlock(privdom);
1706 1707
    if (event)
        testDomainEventQueue(privconn, event);
1708
    testDriverUnlock(privconn);
1709
    return ret;
1710 1711
}

1712
static int testDomainGetInfo(virDomainPtr domain,
1713
                             virDomainInfoPtr info)
1714
{
1715
    testConnPtr privconn = domain->conn->privateData;
1716
    struct timeval tv;
1717
    virDomainObjPtr privdom;
1718
    int ret = -1;
1719

1720
    testDriverLock(privconn);
1721 1722
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1723
    testDriverUnlock(privconn);
1724 1725

    if (privdom == NULL) {
1726
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1727
        goto cleanup;
1728
    }
1729 1730

    if (gettimeofday(&tv, NULL) < 0) {
1731 1732
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("getting time of day"));
1733
        goto cleanup;
1734 1735
    }

J
Jiri Denemark 已提交
1736
    info->state = virDomainObjGetState(privdom, NULL);
1737 1738
    info->memory = privdom->def->mem.cur_balloon;
    info->maxMem = privdom->def->mem.max_balloon;
1739 1740
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1741 1742 1743
    ret = 0;

cleanup:
1744
    if (privdom)
1745
        virObjectUnlock(privdom);
1746
    return ret;
1747 1748
}

1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
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);
1762 1763
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1764 1765 1766
    testDriverUnlock(privconn);

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

J
Jiri Denemark 已提交
1771
    *state = virDomainObjGetState(privdom, reason);
1772 1773 1774 1775
    ret = 0;

cleanup:
    if (privdom)
1776
        virObjectUnlock(privdom);
1777 1778 1779
    return ret;
}

1780 1781
#define TEST_SAVE_MAGIC "TestGuestMagic"

1782 1783 1784
static int
testDomainSaveFlags(virDomainPtr domain, const char *path,
                    const char *dxml, unsigned int flags)
1785
{
1786
    testConnPtr privconn = domain->conn->privateData;
1787 1788 1789
    char *xml = NULL;
    int fd = -1;
    int len;
1790
    virDomainObjPtr privdom;
1791
    virDomainEventPtr event = NULL;
1792
    int ret = -1;
1793

1794 1795
    virCheckFlags(0, -1);
    if (dxml) {
1796 1797
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1798 1799 1800
        return -1;
    }

1801
    testDriverLock(privconn);
1802 1803
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1804 1805

    if (privdom == NULL) {
1806
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1807
        goto cleanup;
1808
    }
1809

1810
    xml = virDomainDefFormat(privdom->def,
C
Cole Robinson 已提交
1811 1812
                             VIR_DOMAIN_XML_SECURE);

1813
    if (xml == NULL) {
1814
        virReportSystemError(errno,
1815 1816
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
1817
        goto cleanup;
1818
    }
1819 1820

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1821
        virReportSystemError(errno,
1822 1823
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
1824
        goto cleanup;
1825
    }
1826
    len = strlen(xml);
1827
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1828
        virReportSystemError(errno,
1829 1830
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1831
        goto cleanup;
1832
    }
1833
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
1834
        virReportSystemError(errno,
1835 1836
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1837
        goto cleanup;
1838
    }
1839
    if (safewrite(fd, xml, len) < 0) {
1840
        virReportSystemError(errno,
1841 1842
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1843
        goto cleanup;
1844
    }
1845

1846
    if (VIR_CLOSE(fd) < 0) {
1847
        virReportSystemError(errno,
1848 1849
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1850
        goto cleanup;
1851
    }
1852 1853
    fd = -1;

J
Jiri Denemark 已提交
1854
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
1855 1856 1857
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1858

1859
    if (!privdom->persistent) {
1860 1861
        virDomainObjListRemove(privconn->domains,
                               privdom);
1862
        privdom = NULL;
1863
    }
1864

1865
    ret = 0;
1866 1867 1868 1869 1870 1871 1872
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) {
1873
        VIR_FORCE_CLOSE(fd);
1874 1875
        unlink(path);
    }
1876
    if (privdom)
1877
        virObjectUnlock(privdom);
1878 1879
    if (event)
        testDomainEventQueue(privconn, event);
1880
    testDriverUnlock(privconn);
1881
    return ret;
1882 1883
}

1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
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)
1896
{
1897
    testConnPtr privconn = conn->privateData;
1898
    char *xml = NULL;
1899
    char magic[15];
1900 1901 1902
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
1903
    virDomainObjPtr dom = NULL;
1904
    virDomainEventPtr event = NULL;
1905
    int ret = -1;
1906

1907 1908
    virCheckFlags(0, -1);
    if (dxml) {
1909 1910
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1911 1912 1913
        return -1;
    }

1914 1915
    testDriverLock(privconn);

1916
    if ((fd = open(path, O_RDONLY)) < 0) {
1917
        virReportSystemError(errno,
1918 1919
                             _("cannot read domain image '%s'"),
                             path);
1920
        goto cleanup;
1921
    }
1922
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
1923
        virReportSystemError(errno,
1924 1925
                             _("incomplete save header in '%s'"),
                             path);
1926
        goto cleanup;
1927
    }
1928
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
1929 1930
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("mismatched header magic"));
1931
        goto cleanup;
1932
    }
1933
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
1934
        virReportSystemError(errno,
1935 1936
                             _("failed to read metadata length in '%s'"),
                             path);
1937
        goto cleanup;
1938 1939
    }
    if (len < 1 || len > 8192) {
1940 1941
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("length of metadata out of range"));
1942
        goto cleanup;
1943
    }
1944
    if (VIR_ALLOC_N(xml, len+1) < 0) {
1945
        virReportOOMError();
1946
        goto cleanup;
1947
    }
1948
    if (saferead(fd, xml, len) != len) {
1949
        virReportSystemError(errno,
1950
                             _("incomplete metdata in '%s'"), path);
1951
        goto cleanup;
1952 1953
    }
    xml[len] = '\0';
1954

1955 1956
    def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
                                  1 << VIR_DOMAIN_VIRT_TEST,
1957
                                  VIR_DOMAIN_XML_INACTIVE);
1958
    if (!def)
1959
        goto cleanup;
1960

1961
    if (testDomainGenerateIfnames(def) < 0)
1962
        goto cleanup;
1963
    if (!(dom = virDomainObjListAdd(privconn->domains,
1964
                                    def,
1965
                                    privconn->xmlopt,
1966 1967 1968
                                    VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
                                    VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                    NULL)))
1969 1970
        goto cleanup;
    def = NULL;
1971

J
Jiri Denemark 已提交
1972
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_RESTORED) < 0)
1973 1974
        goto cleanup;

1975 1976 1977
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1978
    ret = 0;
1979 1980 1981 1982

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
1983
    VIR_FORCE_CLOSE(fd);
1984
    if (dom)
1985
        virObjectUnlock(dom);
1986 1987
    if (event)
        testDomainEventQueue(privconn, event);
1988
    testDriverUnlock(privconn);
1989
    return ret;
1990 1991
}

1992 1993 1994 1995 1996 1997 1998
static int
testDomainRestore(virConnectPtr conn,
                  const char *path)
{
    return testDomainRestoreFlags(conn, path, NULL, 0);
}

1999 2000
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
E
Eric Blake 已提交
2001
                              unsigned int flags)
2002
{
2003
    testConnPtr privconn = domain->conn->privateData;
2004
    int fd = -1;
2005
    virDomainObjPtr privdom;
2006
    virDomainEventPtr event = NULL;
2007
    int ret = -1;
2008

E
Eric Blake 已提交
2009 2010
    virCheckFlags(VIR_DUMP_CRASH, -1);

2011
    testDriverLock(privconn);
2012 2013
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2014 2015

    if (privdom == NULL) {
2016
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2017
        goto cleanup;
2018
    }
2019 2020

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
2021
        virReportSystemError(errno,
2022 2023
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
2024
        goto cleanup;
2025
    }
2026
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
2027
        virReportSystemError(errno,
2028 2029
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
2030
        goto cleanup;
2031
    }
2032
    if (VIR_CLOSE(fd) < 0) {
2033
        virReportSystemError(errno,
2034 2035
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
2036
        goto cleanup;
2037
    }
2038

2039
    if (flags & VIR_DUMP_CRASH) {
J
Jiri Denemark 已提交
2040
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_CRASHED);
2041 2042 2043 2044
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
        if (!privdom->persistent) {
2045 2046
            virDomainObjListRemove(privconn->domains,
                                   privdom);
2047 2048
            privdom = NULL;
        }
2049
    }
2050

2051
    ret = 0;
2052
cleanup:
2053
    VIR_FORCE_CLOSE(fd);
2054
    if (privdom)
2055
        virObjectUnlock(privdom);
2056 2057
    if (event)
        testDomainEventQueue(privconn, event);
2058
    testDriverUnlock(privconn);
2059
    return ret;
2060 2061
}

2062
static char *testDomainGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
2063 2064
    char *ret = strdup("linux");
    if (!ret)
2065
        virReportOOMError();
2066
    return ret;
2067 2068
}

2069
static unsigned long long testDomainGetMaxMemory(virDomainPtr domain) {
2070 2071
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2072
    unsigned long long ret = 0;
2073

2074
    testDriverLock(privconn);
2075 2076
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2077
    testDriverUnlock(privconn);
2078 2079

    if (privdom == NULL) {
2080
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2081
        goto cleanup;
2082
    }
2083

2084
    ret = privdom->def->mem.max_balloon;
2085 2086

cleanup:
2087
    if (privdom)
2088
        virObjectUnlock(privdom);
2089
    return ret;
2090 2091
}

2092 2093
static int testDomainSetMaxMemory(virDomainPtr domain,
                                  unsigned long memory)
2094
{
2095 2096
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2097
    int ret = -1;
2098

2099
    testDriverLock(privconn);
2100 2101
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2102
    testDriverUnlock(privconn);
2103 2104

    if (privdom == NULL) {
2105
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2106
        goto cleanup;
2107
    }
2108 2109

    /* XXX validate not over host memory wrt to other domains */
2110
    privdom->def->mem.max_balloon = memory;
2111 2112 2113
    ret = 0;

cleanup:
2114
    if (privdom)
2115
        virObjectUnlock(privdom);
2116
    return ret;
2117 2118
}

2119 2120
static int testDomainSetMemory(virDomainPtr domain,
                               unsigned long memory)
2121
{
2122 2123
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2124
    int ret = -1;
2125

2126
    testDriverLock(privconn);
2127 2128
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2129
    testDriverUnlock(privconn);
2130 2131

    if (privdom == NULL) {
2132
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2133
        goto cleanup;
2134
    }
2135

2136
    if (memory > privdom->def->mem.max_balloon) {
2137
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2138
        goto cleanup;
2139
    }
2140

2141
    privdom->def->mem.cur_balloon = memory;
2142 2143 2144
    ret = 0;

cleanup:
2145
    if (privdom)
2146
        virObjectUnlock(privdom);
2147
    return ret;
2148 2149
}

2150 2151
static int
testDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
C
Cole Robinson 已提交
2152
{
2153 2154 2155 2156 2157
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;

2158 2159
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2160 2161 2162
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    testDriverLock(privconn);
2163
    vm = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
2164 2165 2166 2167 2168
    testDriverUnlock(privconn);

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

2174
    if (virDomainLiveConfigHelperMethod(privconn->caps, privconn->xmlopt,
2175
                                        vm, &flags, &def) < 0)
2176
        goto cleanup;
2177

2178
    if (flags & VIR_DOMAIN_AFFECT_LIVE)
2179 2180 2181 2182 2183 2184
        def = vm->def;

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

cleanup:
    if (vm)
2185
        virObjectUnlock(vm);
2186
    return ret;
C
Cole Robinson 已提交
2187 2188
}

2189 2190 2191
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
2192
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2193 2194 2195 2196 2197 2198 2199
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

static int
testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
                        unsigned int flags)
{
2200
    testConnPtr privconn = domain->conn->privateData;
2201
    virDomainObjPtr privdom = NULL;
2202
    virDomainDefPtr persistentDef;
C
Cole Robinson 已提交
2203 2204
    int ret = -1, maxvcpus;

2205 2206
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2207 2208 2209 2210
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
2211 2212 2213
    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)) {
2214 2215
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
2216 2217
        return -1;
    }
2218
    if (!nrCpus || (maxvcpus = testConnectGetMaxVcpus(domain->conn, NULL)) < nrCpus) {
2219 2220
        virReportError(VIR_ERR_INVALID_ARG,
                       _("argument out of range: %d"), nrCpus);
2221 2222
        return -1;
    }
2223

2224
    testDriverLock(privconn);
2225
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
2226 2227 2228
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2229
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2230
        goto cleanup;
2231
    }
2232

2233
    if (!virDomainObjIsActive(privdom) && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
2234 2235
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot hotplug vcpus for an inactive domain"));
C
Cole Robinson 已提交
2236 2237 2238
        goto cleanup;
    }

2239 2240
    /* We allow more cpus in guest than host, but not more than the
     * domain's starting limit.  */
C
Cole Robinson 已提交
2241 2242
    if (!(flags & (VIR_DOMAIN_VCPU_MAXIMUM)) &&
        privdom->def->maxvcpus < maxvcpus)
2243
        maxvcpus = privdom->def->maxvcpus;
C
Cole Robinson 已提交
2244

C
Cole Robinson 已提交
2245
    if (nrCpus > maxvcpus) {
2246 2247 2248
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested cpu amount exceeds maximum (%d > %d)"),
                       nrCpus, maxvcpus);
2249
        goto cleanup;
2250
    }
2251

2252
    if (!(persistentDef = virDomainObjGetPersistentDef(privconn->caps,
2253
                                                       privconn->xmlopt,
2254 2255 2256
                                                       privdom)))
        goto cleanup;

2257
    switch (flags) {
2258
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_CONFIG:
2259 2260 2261
        persistentDef->maxvcpus = nrCpus;
        if (nrCpus < persistentDef->vcpus)
            persistentDef->vcpus = nrCpus;
2262 2263
        ret = 0;
        break;
2264

2265
    case VIR_DOMAIN_AFFECT_CONFIG:
2266
        persistentDef->vcpus = nrCpus;
2267 2268 2269
        ret = 0;
        break;

2270
    case VIR_DOMAIN_AFFECT_LIVE:
2271 2272 2273
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
        break;

2274
    case VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG:
2275
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
2276 2277 2278
        if (ret == 0) {
            persistentDef->vcpus = nrCpus;
        }
2279 2280
        break;
    }
2281 2282

cleanup:
2283
    if (privdom)
2284
        virObjectUnlock(privdom);
2285
    return ret;
2286 2287
}

2288
static int
2289
testDomainSetVcpus(virDomainPtr domain, unsigned int nrCpus)
2290
{
2291
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2292 2293
}

C
Cole Robinson 已提交
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
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);
2309
    privdom = virDomainObjListFindByName(privconn->domains, domain->name);
C
Cole Robinson 已提交
2310 2311 2312
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2313
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2314 2315 2316 2317
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2318 2319
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",_("cannot list vcpus for an inactive domain"));
C
Cole Robinson 已提交
2320 2321 2322 2323 2324 2325
        goto cleanup;
    }

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
2326
        virReportSystemError(errno,
C
Cole Robinson 已提交
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
                             "%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)
2378
        virObjectUnlock(privdom);
C
Cole Robinson 已提交
2379 2380 2381
    return ret;
}

C
Cole Robinson 已提交
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
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);
2395
    privdom = virDomainObjListFindByName(privconn->domains, domain->name);
C
Cole Robinson 已提交
2396 2397 2398
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2399
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2400 2401 2402 2403
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2404 2405
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",_("cannot pin vcpus on an inactive domain"));
C
Cole Robinson 已提交
2406 2407 2408 2409
        goto cleanup;
    }

    if (vcpu > privdom->def->vcpus) {
2410 2411
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("requested vcpu is higher than allocated vcpus"));
C
Cole Robinson 已提交
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434
        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)
2435
        virObjectUnlock(privdom);
C
Cole Robinson 已提交
2436 2437 2438
    return ret;
}

2439
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2440
{
2441
    testConnPtr privconn = domain->conn->privateData;
2442
    virDomainDefPtr def;
2443
    virDomainObjPtr privdom;
2444 2445
    char *ret = NULL;

2446 2447
    /* Flags checked by virDomainDefFormat */

2448
    testDriverLock(privconn);
2449 2450
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2451 2452 2453
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2454
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2455
        goto cleanup;
2456
    }
2457

2458 2459
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2460

2461
    ret = virDomainDefFormat(def,
2462 2463 2464
                             flags);

cleanup:
2465
    if (privdom)
2466
        virObjectUnlock(privdom);
2467
    return ret;
2468
}
2469

2470
static int testConnectNumOfDefinedDomains(virConnectPtr conn) {
2471
    testConnPtr privconn = conn->privateData;
2472
    int count;
2473

2474
    testDriverLock(privconn);
2475
    count = virDomainObjListNumOfDomains(privconn->domains, 0);
2476
    testDriverUnlock(privconn);
2477

2478
    return count;
2479 2480
}

2481 2482 2483
static int testConnectListDefinedDomains(virConnectPtr conn,
                                         char **const names,
                                         int maxnames) {
2484

2485
    testConnPtr privconn = conn->privateData;
2486
    int n;
2487

2488
    testDriverLock(privconn);
2489
    memset(names, 0, sizeof(*names)*maxnames);
2490
    n = virDomainObjListGetInactiveNames(privconn->domains, names, maxnames);
2491
    testDriverUnlock(privconn);
2492

2493
    return n;
2494 2495
}

2496
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
2497
                                        const char *xml) {
2498
    testConnPtr privconn = conn->privateData;
2499
    virDomainPtr ret = NULL;
2500
    virDomainDefPtr def;
2501
    virDomainObjPtr dom = NULL;
2502
    virDomainEventPtr event = NULL;
2503
    virDomainDefPtr oldDef = NULL;
2504

2505
    testDriverLock(privconn);
2506 2507
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
                                       1 << VIR_DOMAIN_VIRT_TEST,
2508
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
2509
        goto cleanup;
2510

2511
    if (testDomainGenerateIfnames(def) < 0)
2512
        goto cleanup;
2513
    if (!(dom = virDomainObjListAdd(privconn->domains,
2514
                                    def,
2515
                                    privconn->xmlopt,
2516 2517
                                    0,
                                    &oldDef)))
2518
        goto cleanup;
2519
    def = NULL;
2520
    dom->persistent = 1;
2521

2522 2523
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
2524
                                     !oldDef ?
2525 2526
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2527

2528
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2529
    if (ret)
2530
        ret->id = dom->def->id;
2531 2532 2533

cleanup:
    virDomainDefFree(def);
2534
    virDomainDefFree(oldDef);
2535
    if (dom)
2536
        virObjectUnlock(dom);
2537 2538
    if (event)
        testDomainEventQueue(privconn, event);
2539
    testDriverUnlock(privconn);
2540
    return ret;
2541 2542
}

2543 2544 2545
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
2546
    testConnPtr privconn = conn->privateData;
2547
    int i, j;
2548
    int ret = -1;
2549

2550
    testDriverLock(privconn);
2551
    if (startCell > privconn->numCells) {
2552 2553
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("Range exceeds available cells"));
2554
        goto cleanup;
2555 2556 2557 2558 2559 2560 2561
    }

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

2564
cleanup:
2565
    testDriverUnlock(privconn);
2566
    return ret;
2567 2568 2569
}


2570
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags) {
2571 2572
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2573
    virDomainEventPtr event = NULL;
2574
    int ret = -1;
2575

2576 2577
    virCheckFlags(0, -1);

2578
    testDriverLock(privconn);
2579 2580
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2581 2582

    if (privdom == NULL) {
2583
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2584
        goto cleanup;
2585
    }
2586

J
Jiri Denemark 已提交
2587
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
2588 2589
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Domain '%s' is already running"), domain->name);
2590
        goto cleanup;
2591 2592
    }

J
Jiri Denemark 已提交
2593 2594
    if (testDomainStartState(domain->conn, privdom,
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
2595 2596 2597
        goto cleanup;
    domain->id = privdom->def->id;

2598 2599 2600
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2601
    ret = 0;
2602

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

2612 2613 2614 2615
static int testDomainCreate(virDomainPtr domain) {
    return testDomainCreateWithFlags(domain, 0);
}

2616 2617 2618
static int testDomainUndefineFlags(virDomainPtr domain,
                                   unsigned int flags)
{
2619 2620
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2621
    virDomainEventPtr event = NULL;
2622
    int ret = -1;
2623

2624 2625
    virCheckFlags(0, -1);

2626
    testDriverLock(privconn);
2627 2628
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2629 2630

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

2635 2636 2637
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2638 2639
    if (virDomainObjIsActive(privdom)) {
        privdom->persistent = 0;
2640
    } else {
2641 2642
        virDomainObjListRemove(privconn->domains,
                               privdom);
2643 2644 2645
        privdom = NULL;
    }

2646
    ret = 0;
2647

2648
cleanup:
2649
    if (privdom)
2650
        virObjectUnlock(privdom);
2651 2652
    if (event)
        testDomainEventQueue(privconn, event);
2653
    testDriverUnlock(privconn);
2654
    return ret;
2655 2656
}

2657 2658 2659 2660 2661
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

2662 2663 2664
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2665 2666
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2667
    int ret = -1;
2668

2669
    testDriverLock(privconn);
2670 2671
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2672
    testDriverUnlock(privconn);
2673 2674

    if (privdom == NULL) {
2675
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2676
        goto cleanup;
2677 2678
    }

2679
    *autostart = privdom->autostart;
2680 2681 2682
    ret = 0;

cleanup:
2683
    if (privdom)
2684
        virObjectUnlock(privdom);
2685
    return ret;
2686 2687 2688 2689 2690 2691
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2692 2693
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2694
    int ret = -1;
2695

2696
    testDriverLock(privconn);
2697 2698
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2699
    testDriverUnlock(privconn);
2700 2701

    if (privdom == NULL) {
2702
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2703
        goto cleanup;
2704 2705
    }

2706
    privdom->autostart = autostart ? 1 : 0;
2707 2708 2709
    ret = 0;

cleanup:
2710
    if (privdom)
2711
        virObjectUnlock(privdom);
2712
    return ret;
2713
}
2714

2715
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
2716 2717
                                        int *nparams)
{
2718 2719
    char *type = NULL;

2720 2721 2722
    if (nparams)
        *nparams = 1;

2723
    type = strdup("fair");
2724
    if (!type)
2725
        virReportOOMError();
2726

2727 2728 2729
    return type;
}

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

2740 2741
    virCheckFlags(0, -1);

2742
    testDriverLock(privconn);
2743 2744
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2745
    testDriverUnlock(privconn);
2746 2747

    if (privdom == NULL) {
2748
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2749
        goto cleanup;
2750 2751
    }

2752 2753
    if (virTypedParameterAssign(params, VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, 50) < 0)
2754
        goto cleanup;
2755 2756
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
2757 2758

    *nparams = 1;
2759 2760 2761
    ret = 0;

cleanup:
2762
    if (privdom)
2763
        virObjectUnlock(privdom);
2764
    return ret;
2765
}
2766

2767
static int
2768 2769 2770
testDomainGetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int *nparams)
2771
{
2772
    return testDomainGetSchedulerParametersFlags(domain, params, nparams, 0);
2773
}
2774

2775
static int
2776 2777 2778 2779
testDomainSetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int nparams,
                                      unsigned int flags)
2780
{
2781 2782
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2783
    int ret = -1, i;
2784

2785
    virCheckFlags(0, -1);
2786 2787 2788 2789 2790
    if (virTypedParameterArrayValidate(params, nparams,
                                       VIR_DOMAIN_SCHEDULER_WEIGHT,
                                       VIR_TYPED_PARAM_UINT,
                                       NULL) < 0)
        return -1;
2791

2792
    testDriverLock(privconn);
2793 2794
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2795
    testDriverUnlock(privconn);
2796 2797

    if (privdom == NULL) {
2798
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2799
        goto cleanup;
2800 2801
    }

2802
    for (i = 0; i < nparams; i++) {
2803 2804 2805
        if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_WEIGHT)) {
            /* XXX */
            /*privdom->weight = params[i].value.ui;*/
2806
        }
2807
    }
2808

2809 2810 2811
    ret = 0;

cleanup:
2812
    if (privdom)
2813
        virObjectUnlock(privdom);
2814
    return ret;
2815 2816
}

2817
static int
2818 2819 2820
testDomainSetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int nparams)
2821
{
2822
    return testDomainSetSchedulerParametersFlags(domain, params, nparams, 0);
2823 2824
}

2825 2826 2827 2828 2829 2830 2831 2832
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;
2833
    int ret = -1;
2834 2835

    testDriverLock(privconn);
2836 2837
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2838 2839 2840
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2841
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2842 2843 2844
        goto error;
    }

2845
    if (virDomainDiskIndexByName(privdom->def, path, false) < 0) {
2846 2847
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path: %s"), path);
2848 2849 2850 2851
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2852
        virReportSystemError(errno,
2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867
                             "%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)
2868
        virObjectUnlock(privdom);
2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882
    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);
2883 2884
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2885 2886 2887
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2888
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2889 2890 2891 2892 2893
        goto error;
    }

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

    if (!found) {
2901 2902
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path, '%s' is not a known interface"), path);
2903 2904 2905 2906
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2907
        virReportSystemError(errno,
2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925
                             "%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)
2926
        virObjectUnlock(privdom);
2927 2928 2929
    return ret;
}

2930
static virDrvOpenStatus testNetworkOpen(virConnectPtr conn,
2931
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
2932 2933 2934 2935
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

2936 2937 2938 2939 2940 2941 2942
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

2943
static int testNetworkClose(virConnectPtr conn) {
2944 2945 2946 2947 2948
    conn->networkPrivateData = NULL;
    return 0;
}


2949 2950
static virNetworkPtr testNetworkLookupByUUID(virConnectPtr conn,
                                             const unsigned char *uuid)
2951
{
2952 2953
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
2954
    virNetworkPtr ret = NULL;
2955

2956 2957 2958 2959 2960
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
2961
        virReportError(VIR_ERR_NO_NETWORK, NULL);
2962
        goto cleanup;
2963 2964
    }

2965 2966 2967
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2968 2969
    if (net)
        virNetworkObjUnlock(net);
2970
    return ret;
2971
}
2972

2973
static virNetworkPtr testNetworkLookupByName(virConnectPtr conn,
2974
                                             const char *name)
2975
{
2976
    testConnPtr privconn = conn->privateData;
2977 2978
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
2979

2980 2981 2982 2983 2984
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
2985
        virReportError(VIR_ERR_NO_NETWORK, NULL);
2986
        goto cleanup;
2987 2988
    }

2989 2990 2991
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2992 2993
    if (net)
        virNetworkObjUnlock(net);
2994
    return ret;
2995 2996 2997
}


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

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

3011
    return numActive;
3012 3013
}

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

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

3031 3032 3033
    return n;

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

3041
static int testConnectNumOfDefinedNetworks(virConnectPtr conn) {
3042
    testConnPtr privconn = conn->privateData;
3043
    int numInactive = 0, i;
3044

3045 3046 3047
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3048
        if (!virNetworkObjIsActive(privconn->networks.objs[i]))
3049
            numInactive++;
3050 3051 3052
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3053

3054
    return numInactive;
3055 3056
}

3057
static int testConnectListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
3058
    testConnPtr privconn = conn->privateData;
3059
    int n = 0, i;
3060

3061
    testDriverLock(privconn);
3062
    memset(names, 0, sizeof(*names)*nnames);
3063 3064
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3065
        if (!virNetworkObjIsActive(privconn->networks.objs[i]) &&
3066 3067
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
3068
            goto no_memory;
3069 3070 3071 3072
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3073

3074 3075 3076
    return n;

no_memory:
3077
    virReportOOMError();
3078 3079
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3080
    testDriverUnlock(privconn);
3081
    return -1;
3082 3083
}

3084
static int
3085
testConnectListAllNetworks(virConnectPtr conn,
3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099
                           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;
}
3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110

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) {
3111
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131
        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) {
3132
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
        goto cleanup;
    }
    ret = obj->persistent;

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


3144
static virNetworkPtr testNetworkCreateXML(virConnectPtr conn, const char *xml) {
3145
    testConnPtr privconn = conn->privateData;
3146
    virNetworkDefPtr def;
3147
    virNetworkObjPtr net = NULL;
3148
    virNetworkPtr ret = NULL;
3149

3150
    testDriverLock(privconn);
3151
    if ((def = virNetworkDefParseString(xml)) == NULL)
3152
        goto cleanup;
3153

3154
    if (!(net = virNetworkAssignDef(&privconn->networks, def, false)))
3155 3156
        goto cleanup;
    def = NULL;
3157
    net->active = 1;
3158

3159
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3160

3161 3162
cleanup:
    virNetworkDefFree(def);
3163 3164 3165
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3166
    return ret;
3167 3168
}

3169
static
3170
virNetworkPtr testNetworkDefineXML(virConnectPtr conn, const char *xml)
3171
{
3172
    testConnPtr privconn = conn->privateData;
3173
    virNetworkDefPtr def;
3174
    virNetworkObjPtr net = NULL;
3175
    virNetworkPtr ret = NULL;
3176

3177
    testDriverLock(privconn);
3178
    if ((def = virNetworkDefParseString(xml)) == NULL)
3179
        goto cleanup;
3180

3181
    if (!(net = virNetworkAssignDef(&privconn->networks, def, false)))
3182 3183
        goto cleanup;
    def = NULL;
3184
    net->persistent = 1;
3185

3186
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3187 3188 3189

cleanup:
    virNetworkDefFree(def);
3190 3191 3192
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3193
    return ret;
3194 3195 3196
}

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

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

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

D
Daniel P. Berrange 已提交
3210
    if (virNetworkObjIsActive(privnet)) {
3211 3212
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is still running"), network->name);
3213
        goto cleanup;
3214 3215
    }

3216 3217
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
3218
    privnet = NULL;
3219
    ret = 0;
3220

3221
cleanup:
3222 3223 3224
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3225
    return ret;
3226 3227
}

3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275
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;
}

3276
static int testNetworkCreate(virNetworkPtr network) {
3277 3278
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3279
    int ret = -1;
3280

3281
    testDriverLock(privconn);
3282 3283
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3284
    testDriverUnlock(privconn);
3285 3286

    if (privnet == NULL) {
3287
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3288
        goto cleanup;
3289
    }
3290

D
Daniel P. Berrange 已提交
3291
    if (virNetworkObjIsActive(privnet)) {
3292 3293
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is already running"), network->name);
3294
        goto cleanup;
3295 3296
    }

3297
    privnet->active = 1;
3298
    ret = 0;
3299

3300
cleanup:
3301 3302
    if (privnet)
        virNetworkObjUnlock(privnet);
3303
    return ret;
3304 3305 3306
}

static int testNetworkDestroy(virNetworkPtr network) {
3307 3308
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3309
    int ret = -1;
3310

3311
    testDriverLock(privconn);
3312 3313 3314 3315
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3316
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3317
        goto cleanup;
3318
    }
3319

3320 3321 3322 3323
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
3324
        privnet = NULL;
3325
    }
3326 3327 3328
    ret = 0;

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

3335
static char *testNetworkGetXMLDesc(virNetworkPtr network,
E
Eric Blake 已提交
3336
                                   unsigned int flags)
3337
{
3338 3339
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3340
    char *ret = NULL;
3341

E
Eric Blake 已提交
3342 3343
    virCheckFlags(0, NULL);

3344
    testDriverLock(privconn);
3345 3346
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3347
    testDriverUnlock(privconn);
3348 3349

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

3354
    ret = virNetworkDefFormat(privnet->def, flags);
3355 3356

cleanup:
3357 3358
    if (privnet)
        virNetworkObjUnlock(privnet);
3359
    return ret;
3360 3361 3362
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3363
    testConnPtr privconn = network->conn->privateData;
3364
    char *bridge = NULL;
3365 3366
    virNetworkObjPtr privnet;

3367
    testDriverLock(privconn);
3368 3369
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3370
    testDriverUnlock(privconn);
3371 3372

    if (privnet == NULL) {
3373
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3374
        goto cleanup;
3375 3376
    }

3377
    if (!(privnet->def->bridge)) {
3378 3379 3380
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("network '%s' does not have a bridge name."),
                       privnet->def->name);
3381 3382 3383 3384
        goto cleanup;
    }

    if (!(bridge = strdup(privnet->def->bridge))) {
3385
        virReportOOMError();
3386
        goto cleanup;
3387
    }
3388 3389

cleanup:
3390 3391
    if (privnet)
        virNetworkObjUnlock(privnet);
3392 3393 3394 3395 3396
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
3397 3398
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3399
    int ret = -1;
3400

3401
    testDriverLock(privconn);
3402 3403
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3404
    testDriverUnlock(privconn);
3405 3406

    if (privnet == NULL) {
3407
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3408
        goto cleanup;
3409 3410
    }

3411
    *autostart = privnet->autostart;
3412 3413 3414
    ret = 0;

cleanup:
3415 3416
    if (privnet)
        virNetworkObjUnlock(privnet);
3417
    return ret;
3418 3419 3420 3421
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
3422 3423
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3424
    int ret = -1;
3425

3426
    testDriverLock(privconn);
3427 3428
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3429
    testDriverUnlock(privconn);
3430 3431

    if (privnet == NULL) {
3432
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3433
        goto cleanup;
3434 3435
    }

3436
    privnet->autostart = autostart ? 1 : 0;
3437 3438 3439
    ret = 0;

cleanup:
3440 3441
    if (privnet)
        virNetworkObjUnlock(privnet);
3442
    return ret;
3443
}
3444

C
Cole Robinson 已提交
3445

L
Laine Stump 已提交
3446 3447 3448 3449
/*
 * Physical host interface routines
 */

3450
static virDrvOpenStatus testInterfaceOpen(virConnectPtr conn,
L
Laine Stump 已提交
3451
                                          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3452
                                          unsigned int flags)
L
Laine Stump 已提交
3453
{
E
Eric Blake 已提交
3454 3455
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

L
Laine Stump 已提交
3456 3457 3458 3459 3460 3461 3462
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

3463
static int testInterfaceClose(virConnectPtr conn)
L
Laine Stump 已提交
3464 3465 3466 3467 3468 3469
{
    conn->interfacePrivateData = NULL;
    return 0;
}


3470
static int testConnectNumOfInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3471 3472 3473 3474 3475 3476 3477
{
    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 已提交
3478
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3479 3480 3481 3482 3483 3484 3485 3486
            count++;
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

3487
static int testConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
L
Laine Stump 已提交
3488 3489 3490 3491 3492 3493 3494 3495
{
    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 已提交
3496
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508
            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:
3509
    virReportOOMError();
L
Laine Stump 已提交
3510 3511 3512 3513 3514 3515
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

3516
static int testConnectNumOfDefinedInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3517 3518 3519 3520 3521 3522 3523
{
    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 已提交
3524
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3525 3526 3527 3528 3529 3530 3531 3532
            count++;
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

3533
static int testConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
L
Laine Stump 已提交
3534 3535 3536 3537 3538 3539 3540 3541
{
    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 已提交
3542
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554
            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:
3555
    virReportOOMError();
L
Laine Stump 已提交
3556 3557 3558 3559 3560 3561
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

3562
static virInterfacePtr testInterfaceLookupByName(virConnectPtr conn,
L
Laine Stump 已提交
3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573
                                                 const char *name)
{
    testConnPtr privconn = conn->privateData;
    virInterfaceObjPtr iface;
    virInterfacePtr ret = NULL;

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

    if (iface == NULL) {
3574
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585
        goto cleanup;
    }

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

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

3586
static virInterfacePtr testInterfaceLookupByMACString(virConnectPtr conn,
L
Laine Stump 已提交
3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598
                                                      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) {
3599
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3600 3601 3602 3603
        goto cleanup;
    }

    if (ifacect > 1) {
3604
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615
        goto cleanup;
    }

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

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

3616 3617 3618 3619 3620 3621 3622 3623 3624 3625
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) {
3626
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
3627 3628 3629 3630 3631 3632 3633 3634 3635 3636
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

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

3637
static int testInterfaceChangeBegin(virConnectPtr conn,
E
Eric Blake 已提交
3638
                                    unsigned int flags)
3639 3640 3641 3642
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3643 3644
    virCheckFlags(0, -1);

3645 3646
    testDriverLock(privconn);
    if (privconn->transaction_running) {
3647
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3648
                       _("there is another transaction running."));
3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664
        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 已提交
3665
                                     unsigned int flags)
3666 3667 3668 3669
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3670 3671
    virCheckFlags(0, -1);

3672 3673 3674
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3675
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3676 3677
                       _("no transaction running, "
                         "nothing to be committed."));
3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692
        goto cleanup;
    }

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

    ret = 0;

cleanup:
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
E
Eric Blake 已提交
3693
                                       unsigned int flags)
3694 3695 3696 3697
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

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

3700 3701 3702
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3703
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3704 3705
                       _("no transaction running, "
                         "nothing to rollback."));
3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722
        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;
}
3723

L
Laine Stump 已提交
3724
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
E
Eric Blake 已提交
3725
                                     unsigned int flags)
L
Laine Stump 已提交
3726 3727 3728 3729 3730
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

E
Eric Blake 已提交
3731 3732
    virCheckFlags(0, NULL);

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

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

3743
    ret = virInterfaceDefFormat(privinterface->def);
L
Laine Stump 已提交
3744 3745 3746 3747 3748 3749 3750 3751 3752

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


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
E
Eric Blake 已提交
3753
                                              unsigned int flags)
L
Laine Stump 已提交
3754 3755 3756 3757 3758 3759
{
    testConnPtr privconn = conn->privateData;
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
3760 3761
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3762
    testDriverLock(privconn);
3763
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3764 3765
        goto cleanup;

3766
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790
        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) {
3791
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804
        goto cleanup;
    }

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

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
E
Eric Blake 已提交
3805
                               unsigned int flags)
L
Laine Stump 已提交
3806 3807 3808 3809 3810
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3811 3812
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3813 3814 3815 3816 3817
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3818
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3819 3820 3821 3822
        goto cleanup;
    }

    if (privinterface->active != 0) {
3823
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

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

static int testInterfaceDestroy(virInterfacePtr iface,
E
Eric Blake 已提交
3838
                                unsigned int flags)
L
Laine Stump 已提交
3839 3840 3841 3842 3843
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3844 3845
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3846 3847 3848 3849 3850
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3851
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3852 3853 3854 3855
        goto cleanup;
    }

    if (privinterface->active == 0) {
3856
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

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



C
Cole Robinson 已提交
3872 3873 3874 3875
/*
 * Storage Driver routines
 */

3876

3877
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3878 3879 3880 3881 3882 3883 3884

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

    pool->configFile = strdup("\0");
    if (!pool->configFile) {
3885
        virReportOOMError();
C
Cole Robinson 已提交
3886 3887 3888 3889 3890 3891
        return -1;
    }

    return 0;
}

3892 3893
static virDrvOpenStatus testStorageOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3894 3895 3896 3897
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909
    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;
}

3910

C
Cole Robinson 已提交
3911 3912 3913
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
3914
    testConnPtr privconn = conn->privateData;
3915 3916
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3917

3918
    testDriverLock(privconn);
3919
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
3920
    testDriverUnlock(privconn);
3921 3922

    if (pool == NULL) {
3923
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
3924
        goto cleanup;
C
Cole Robinson 已提交
3925 3926
    }

3927 3928
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
3929 3930

cleanup:
3931 3932
    if (pool)
        virStoragePoolObjUnlock(pool);
3933
    return ret;
C
Cole Robinson 已提交
3934 3935 3936 3937 3938
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
3939
    testConnPtr privconn = conn->privateData;
3940 3941
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3942

3943
    testDriverLock(privconn);
3944
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
3945
    testDriverUnlock(privconn);
3946 3947

    if (pool == NULL) {
3948
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
3949
        goto cleanup;
C
Cole Robinson 已提交
3950 3951
    }

3952 3953
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
3954 3955

cleanup:
3956 3957
    if (pool)
        virStoragePoolObjUnlock(pool);
3958
    return ret;
C
Cole Robinson 已提交
3959 3960 3961 3962 3963 3964 3965 3966
}

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

static int
3967
testConnectNumOfStoragePools(virConnectPtr conn) {
3968
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3969 3970
    int numActive = 0, i;

3971
    testDriverLock(privconn);
C
Cole Robinson 已提交
3972 3973 3974
    for (i = 0 ; i < privconn->pools.count ; i++)
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
3975
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3976 3977 3978 3979 3980

    return numActive;
}

static int
3981 3982 3983
testConnectListStoragePools(virConnectPtr conn,
                            char **const names,
                            int nnames) {
3984
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3985 3986
    int n = 0, i;

3987
    testDriverLock(privconn);
C
Cole Robinson 已提交
3988
    memset(names, 0, sizeof(*names)*nnames);
3989 3990
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3991
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3992 3993
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3994
            goto no_memory;
3995 3996 3997 3998
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3999 4000 4001 4002

    return n;

no_memory:
4003
    virReportOOMError();
C
Cole Robinson 已提交
4004 4005
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
4006
    testDriverUnlock(privconn);
4007
    return -1;
C
Cole Robinson 已提交
4008 4009 4010
}

static int
4011
testConnectNumOfDefinedStoragePools(virConnectPtr conn) {
4012
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4013 4014
    int numInactive = 0, i;

4015 4016 4017
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->pools.count ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4018 4019
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
4020 4021 4022
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4023 4024 4025 4026 4027

    return numInactive;
}

static int
4028 4029 4030
testConnectListDefinedStoragePools(virConnectPtr conn,
                                   char **const names,
                                   int nnames) {
4031
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4032 4033
    int n = 0, i;

4034
    testDriverLock(privconn);
C
Cole Robinson 已提交
4035
    memset(names, 0, sizeof(*names)*nnames);
4036 4037
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4038
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
4039 4040
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4041
            goto no_memory;
4042 4043 4044 4045
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4046 4047 4048 4049

    return n;

no_memory:
4050
    virReportOOMError();
C
Cole Robinson 已提交
4051 4052
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
4053
    testDriverUnlock(privconn);
4054
    return -1;
C
Cole Robinson 已提交
4055 4056
}

4057
static int
4058 4059 4060
testConnectListAllStoragePools(virConnectPtr conn,
                               virStoragePoolPtr **pools,
                               unsigned int flags)
4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072
{
    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 已提交
4073

4074 4075 4076 4077 4078 4079 4080 4081 4082 4083
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) {
4084
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104
        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) {
4105
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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



C
Cole Robinson 已提交
4118
static int
4119 4120
testStoragePoolCreate(virStoragePoolPtr pool,
                      unsigned int flags)
E
Eric Blake 已提交
4121
{
4122 4123
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4124
    int ret = -1;
4125

E
Eric Blake 已提交
4126 4127
    virCheckFlags(0, -1);

4128
    testDriverLock(privconn);
4129 4130
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4131
    testDriverUnlock(privconn);
4132 4133

    if (privpool == NULL) {
4134
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4135
        goto cleanup;
4136 4137
    }

4138
    if (virStoragePoolObjIsActive(privpool)) {
4139 4140
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4141 4142
        goto cleanup;
    }
C
Cole Robinson 已提交
4143 4144

    privpool->active = 1;
4145
    ret = 0;
C
Cole Robinson 已提交
4146

4147
cleanup:
4148 4149
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4150
    return ret;
C
Cole Robinson 已提交
4151 4152 4153
}

static char *
4154 4155 4156 4157
testConnectFindStoragePoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type,
                                  const char *srcSpec,
                                  unsigned int flags)
C
Cole Robinson 已提交
4158
{
4159 4160 4161 4162
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4163 4164
    virCheckFlags(0, NULL);

4165 4166
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4167 4168
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
4169 4170 4171 4172
        goto cleanup;
    }

    if (srcSpec) {
4173
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4174 4175 4176 4177 4178 4179 4180 4181 4182
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
        ret = strdup(defaultPoolSourcesLogicalXML);
        if (!ret)
4183
            virReportOOMError();
4184 4185 4186
        break;

    case VIR_STORAGE_POOL_NETFS:
4187
        if (!source || !source->hosts[0].name) {
4188 4189
            virReportError(VIR_ERR_INVALID_ARG,
                           "%s", _("hostname must be specified for netfs sources"));
4190 4191 4192 4193
            goto cleanup;
        }

        if (virAsprintf(&ret, defaultPoolSourcesNetFSXML,
4194
                        source->hosts[0].name) < 0)
4195
            virReportOOMError();
4196 4197 4198
        break;

    default:
4199 4200
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4201 4202 4203 4204 4205
    }

cleanup:
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4206 4207 4208 4209
}


static virStoragePoolPtr
4210 4211 4212
testStoragePoolCreateXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4213
{
4214
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4215
    virStoragePoolDefPtr def;
4216
    virStoragePoolObjPtr pool = NULL;
4217
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4218

E
Eric Blake 已提交
4219 4220
    virCheckFlags(0, NULL);

4221
    testDriverLock(privconn);
4222
    if (!(def = virStoragePoolDefParseString(xml)))
4223
        goto cleanup;
C
Cole Robinson 已提交
4224

4225 4226 4227 4228
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4229 4230
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("storage pool already exists"));
4231
        goto cleanup;
C
Cole Robinson 已提交
4232 4233
    }

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

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

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

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

static virStoragePoolPtr
4257 4258 4259
testStoragePoolDefineXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4260
{
4261
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4262
    virStoragePoolDefPtr def;
4263
    virStoragePoolObjPtr pool = NULL;
4264
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4265

E
Eric Blake 已提交
4266 4267
    virCheckFlags(0, NULL);

4268
    testDriverLock(privconn);
4269
    if (!(def = virStoragePoolDefParseString(xml)))
4270
        goto cleanup;
C
Cole Robinson 已提交
4271 4272 4273 4274 4275

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

4276
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4277 4278
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4279

4280
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4281
        virStoragePoolObjRemove(&privconn->pools, pool);
4282 4283
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4284 4285
    }

4286 4287
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4288 4289 4290

cleanup:
    virStoragePoolDefFree(def);
4291 4292 4293
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4294
    return ret;
C
Cole Robinson 已提交
4295 4296 4297
}

static int
4298 4299 4300
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4301
    int ret = -1;
4302

4303
    testDriverLock(privconn);
4304 4305 4306 4307
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4308
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4309
        goto cleanup;
4310 4311
    }

4312
    if (virStoragePoolObjIsActive(privpool)) {
4313 4314
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4315 4316
        goto cleanup;
    }
C
Cole Robinson 已提交
4317 4318

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

4321
cleanup:
4322 4323 4324
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4325
    return ret;
C
Cole Robinson 已提交
4326 4327 4328
}

static int
4329
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4330 4331
                     unsigned int flags)
{
4332 4333
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4334
    int ret = -1;
4335

E
Eric Blake 已提交
4336 4337
    virCheckFlags(0, -1);

4338
    testDriverLock(privconn);
4339 4340
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4341
    testDriverUnlock(privconn);
4342 4343

    if (privpool == NULL) {
4344
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4345
        goto cleanup;
4346 4347
    }

4348
    if (virStoragePoolObjIsActive(privpool)) {
4349 4350
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4351 4352
        goto cleanup;
    }
4353
    ret = 0;
C
Cole Robinson 已提交
4354

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


static int
4363 4364 4365
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4366
    int ret = -1;
4367

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

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

    if (!virStoragePoolObjIsActive(privpool)) {
4378 4379
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4380
        goto cleanup;
4381
    }
C
Cole Robinson 已提交
4382 4383 4384

    privpool->active = 0;

4385
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4386
        virStoragePoolObjRemove(&privconn->pools, privpool);
4387 4388
        privpool = NULL;
    }
4389
    ret = 0;
C
Cole Robinson 已提交
4390

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


static int
4400
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4401 4402
                      unsigned int flags)
{
4403 4404
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4405
    int ret = -1;
4406

E
Eric Blake 已提交
4407 4408
    virCheckFlags(0, -1);

4409
    testDriverLock(privconn);
4410 4411
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4412
    testDriverUnlock(privconn);
4413 4414

    if (privpool == NULL) {
4415
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4416 4417 4418 4419
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
4420 4421
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4422
        goto cleanup;
4423 4424
    }

4425
    ret = 0;
C
Cole Robinson 已提交
4426

4427
cleanup:
4428 4429
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4430
    return ret;
C
Cole Robinson 已提交
4431 4432 4433 4434
}


static int
4435
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4436 4437
                       unsigned int flags)
{
4438 4439
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4440
    int ret = -1;
4441

E
Eric Blake 已提交
4442 4443
    virCheckFlags(0, -1);

4444
    testDriverLock(privconn);
4445 4446
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4447
    testDriverUnlock(privconn);
4448 4449

    if (privpool == NULL) {
4450
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4451
        goto cleanup;
4452 4453 4454
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4455 4456
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4457
        goto cleanup;
4458
    }
4459
    ret = 0;
C
Cole Robinson 已提交
4460

4461
cleanup:
4462 4463
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4464
    return ret;
C
Cole Robinson 已提交
4465 4466 4467 4468
}


static int
4469
testStoragePoolGetInfo(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4470
                       virStoragePoolInfoPtr info) {
4471 4472
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4473
    int ret = -1;
4474

4475
    testDriverLock(privconn);
4476 4477
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4478
    testDriverUnlock(privconn);
4479 4480

    if (privpool == NULL) {
4481
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4482
        goto cleanup;
4483
    }
C
Cole Robinson 已提交
4484 4485 4486 4487 4488 4489 4490 4491 4492

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

4495
cleanup:
4496 4497
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4498
    return ret;
C
Cole Robinson 已提交
4499 4500 4501
}

static char *
4502
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4503 4504
                          unsigned int flags)
{
4505 4506
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4507
    char *ret = NULL;
4508

E
Eric Blake 已提交
4509 4510
    virCheckFlags(0, NULL);

4511
    testDriverLock(privconn);
4512 4513
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4514
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4515

4516
    if (privpool == NULL) {
4517
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4518
        goto cleanup;
4519 4520
    }

4521
    ret = virStoragePoolDefFormat(privpool->def);
4522 4523

cleanup:
4524 4525
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4526
    return ret;
C
Cole Robinson 已提交
4527 4528 4529
}

static int
4530
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4531
                            int *autostart) {
4532 4533
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4534
    int ret = -1;
4535

4536
    testDriverLock(privconn);
4537 4538
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4539
    testDriverUnlock(privconn);
4540 4541

    if (privpool == NULL) {
4542
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4543
        goto cleanup;
4544
    }
C
Cole Robinson 已提交
4545 4546 4547 4548 4549 4550

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

4553
cleanup:
4554 4555
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4556
    return ret;
C
Cole Robinson 已提交
4557 4558 4559
}

static int
4560
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4561
                            int autostart) {
4562 4563
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4564
    int ret = -1;
4565

4566
    testDriverLock(privconn);
4567 4568
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4569
    testDriverUnlock(privconn);
4570 4571

    if (privpool == NULL) {
4572
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4573
        goto cleanup;
4574
    }
C
Cole Robinson 已提交
4575 4576

    if (!privpool->configFile) {
4577 4578
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
4579
        goto cleanup;
C
Cole Robinson 已提交
4580 4581 4582 4583
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4584 4585 4586
    ret = 0;

cleanup:
4587 4588
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4589
    return ret;
C
Cole Robinson 已提交
4590 4591 4592 4593
}


static int
4594
testStoragePoolNumOfVolumes(virStoragePoolPtr pool) {
4595 4596
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4597
    int ret = -1;
4598

4599
    testDriverLock(privconn);
4600 4601
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4602
    testDriverUnlock(privconn);
4603 4604

    if (privpool == NULL) {
4605
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4606
        goto cleanup;
4607 4608 4609
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4610 4611
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4612
        goto cleanup;
4613
    }
C
Cole Robinson 已提交
4614

4615 4616 4617
    ret = privpool->volumes.count;

cleanup:
4618 4619
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4620
    return ret;
C
Cole Robinson 已提交
4621 4622 4623
}

static int
4624
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4625 4626
                           char **const names,
                           int maxnames) {
4627 4628
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
C
Cole Robinson 已提交
4629 4630
    int i = 0, n = 0;

4631
    memset(names, 0, maxnames * sizeof(*names));
4632 4633

    testDriverLock(privconn);
4634 4635
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4636
    testDriverUnlock(privconn);
4637 4638

    if (privpool == NULL) {
4639
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4640
        goto cleanup;
4641 4642 4643 4644
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4645 4646
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4647
        goto cleanup;
4648 4649
    }

C
Cole Robinson 已提交
4650 4651
    for (i = 0 ; i < privpool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(privpool->volumes.objs[i]->name)) == NULL) {
4652
            virReportOOMError();
C
Cole Robinson 已提交
4653 4654 4655 4656
            goto cleanup;
        }
    }

4657
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4658 4659 4660 4661 4662 4663
    return n;

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

4664
    memset(names, 0, maxnames * sizeof(*names));
4665 4666
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4667 4668 4669
    return -1;
}

4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713
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,
4714 4715
                                     pool->volumes.objs[i]->key,
                                     NULL, NULL)))
4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729
            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]);
        }
4730
        VIR_FREE(tmp_vols);
4731 4732 4733 4734 4735 4736 4737
    }

    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}
C
Cole Robinson 已提交
4738 4739

static virStorageVolPtr
4740 4741
testStorageVolLookupByName(virStoragePoolPtr pool,
                           const char *name ATTRIBUTE_UNUSED) {
4742 4743 4744
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4745
    virStorageVolPtr ret = NULL;
4746

4747
    testDriverLock(privconn);
4748 4749
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4750
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4751

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


    if (!virStoragePoolObjIsActive(privpool)) {
4759 4760
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4761
        goto cleanup;
4762 4763 4764 4765 4766
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4767 4768
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"), name);
4769
        goto cleanup;
C
Cole Robinson 已提交
4770 4771
    }

4772
    ret = virGetStorageVol(pool->conn, privpool->def->name,
4773 4774
                           privvol->name, privvol->key,
                           NULL, NULL);
4775 4776

cleanup:
4777 4778
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4779
    return ret;
C
Cole Robinson 已提交
4780 4781 4782 4783
}


static virStorageVolPtr
4784 4785
testStorageVolLookupByKey(virConnectPtr conn,
                          const char *key) {
4786
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4787
    unsigned int i;
4788
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4789

4790
    testDriverLock(privconn);
C
Cole Robinson 已提交
4791
    for (i = 0 ; i < privconn->pools.count ; i++) {
4792
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4793
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4794
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4795 4796
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4797 4798 4799 4800
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
4801 4802
                                       privvol->key,
                                       NULL, NULL);
4803
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4804 4805
                break;
            }
C
Cole Robinson 已提交
4806
        }
4807
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4808
    }
4809
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4810

4811
    if (!ret)
4812 4813
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
4814 4815

    return ret;
C
Cole Robinson 已提交
4816 4817 4818
}

static virStorageVolPtr
4819 4820
testStorageVolLookupByPath(virConnectPtr conn,
                           const char *path) {
4821
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4822
    unsigned int i;
4823
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4824

4825
    testDriverLock(privconn);
C
Cole Robinson 已提交
4826
    for (i = 0 ; i < privconn->pools.count ; i++) {
4827
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4828
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4829
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4830 4831
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

4832 4833 4834 4835
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
4836 4837
                                       privvol->key,
                                       NULL, NULL);
4838
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4839 4840
                break;
            }
C
Cole Robinson 已提交
4841
        }
4842
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4843
    }
4844
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4845

4846
    if (!ret)
4847 4848
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
4849 4850

    return ret;
C
Cole Robinson 已提交
4851 4852 4853
}

static virStorageVolPtr
4854 4855 4856
testStorageVolCreateXML(virStoragePoolPtr pool,
                        const char *xmldesc,
                        unsigned int flags)
E
Eric Blake 已提交
4857
{
4858 4859
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4860 4861
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
4862

E
Eric Blake 已提交
4863 4864
    virCheckFlags(0, NULL);

4865
    testDriverLock(privconn);
4866 4867
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4868
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4869

4870
    if (privpool == NULL) {
4871
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4872
        goto cleanup;
4873 4874 4875
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4876 4877
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4878
        goto cleanup;
4879
    }
C
Cole Robinson 已提交
4880

4881
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4882
    if (privvol == NULL)
4883
        goto cleanup;
4884 4885

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4886 4887
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
4888
        goto cleanup;
C
Cole Robinson 已提交
4889 4890 4891
    }

    /* Make sure enough space */
4892
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
4893
         privpool->def->capacity) {
4894 4895 4896
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
4897
        goto cleanup;
C
Cole Robinson 已提交
4898 4899 4900 4901
    }

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4902
        virReportOOMError();
4903
        goto cleanup;
C
Cole Robinson 已提交
4904 4905
    }

4906 4907 4908
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4909
        virReportOOMError();
4910
        goto cleanup;
C
Cole Robinson 已提交
4911 4912
    }

4913 4914
    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4915
        virReportOOMError();
4916
        goto cleanup;
C
Cole Robinson 已提交
4917 4918
    }

4919
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
4920 4921 4922
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

4925
    ret = virGetStorageVol(pool->conn, privpool->def->name,
4926 4927
                           privvol->name, privvol->key,
                           NULL, NULL);
4928
    privvol = NULL;
4929 4930 4931

cleanup:
    virStorageVolDefFree(privvol);
4932 4933
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4934
    return ret;
C
Cole Robinson 已提交
4935 4936
}

4937
static virStorageVolPtr
4938 4939 4940 4941
testStorageVolCreateXMLFrom(virStoragePoolPtr pool,
                            const char *xmldesc,
                            virStorageVolPtr clonevol,
                            unsigned int flags)
E
Eric Blake 已提交
4942
{
4943 4944 4945 4946 4947
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
4948 4949
    virCheckFlags(0, NULL);

4950 4951 4952 4953 4954 4955
    testDriverLock(privconn);
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
    testDriverUnlock(privconn);

    if (privpool == NULL) {
4956
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4957 4958 4959 4960
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4961 4962
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4963 4964 4965
        goto cleanup;
    }

4966
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4967 4968 4969 4970
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4971 4972
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
4973 4974 4975 4976 4977
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
4978 4979 4980
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
4981 4982 4983 4984 4985 4986
        goto cleanup;
    }

    /* Make sure enough space */
    if ((privpool->def->allocation + privvol->allocation) >
         privpool->def->capacity) {
4987 4988 4989
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
4990 4991 4992 4993 4994 4995 4996
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4997
        virReportOOMError();
4998 4999 5000
        goto cleanup;
    }

5001 5002 5003
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
5004
        virReportOOMError();
5005 5006 5007 5008 5009
        goto cleanup;
    }

    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
5010
        virReportOOMError();
5011 5012 5013 5014 5015 5016 5017 5018 5019 5020
        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,
5021 5022
                           privvol->name, privvol->key,
                           NULL, NULL);
5023 5024 5025 5026 5027 5028 5029 5030 5031
    privvol = NULL;

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

C
Cole Robinson 已提交
5032
static int
5033 5034
testStorageVolDelete(virStorageVolPtr vol,
                     unsigned int flags)
E
Eric Blake 已提交
5035
{
5036 5037 5038
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
5039
    int i;
5040
    int ret = -1;
C
Cole Robinson 已提交
5041

E
Eric Blake 已提交
5042 5043
    virCheckFlags(0, -1);

5044
    testDriverLock(privconn);
5045 5046
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5047
    testDriverUnlock(privconn);
5048 5049

    if (privpool == NULL) {
5050
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5051
        goto cleanup;
5052 5053 5054 5055 5056 5057
    }


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

    if (privvol == NULL) {
5058 5059 5060
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5061
        goto cleanup;
5062 5063 5064
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5065 5066
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5067
        goto cleanup;
5068 5069 5070
    }


C
Cole Robinson 已提交
5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093
    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;
        }
    }
5094
    ret = 0;
C
Cole Robinson 已提交
5095

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


static int testStorageVolumeTypeForPool(int pooltype) {

5105
    switch (pooltype) {
C
Cole Robinson 已提交
5106 5107 5108 5109 5110 5111 5112 5113 5114 5115
        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
5116 5117
testStorageVolGetInfo(virStorageVolPtr vol,
                      virStorageVolInfoPtr info) {
5118 5119 5120
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5121
    int ret = -1;
5122

5123
    testDriverLock(privconn);
5124 5125
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5126
    testDriverUnlock(privconn);
5127 5128

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

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

    if (privvol == NULL) {
5136 5137 5138
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5139
        goto cleanup;
5140 5141 5142
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5143 5144
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5145
        goto cleanup;
5146
    }
C
Cole Robinson 已提交
5147 5148 5149 5150 5151

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

5154
cleanup:
5155 5156
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5157
    return ret;
C
Cole Robinson 已提交
5158 5159 5160
}

static char *
5161 5162
testStorageVolGetXMLDesc(virStorageVolPtr vol,
                         unsigned int flags)
E
Eric Blake 已提交
5163
{
5164 5165 5166
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5167
    char *ret = NULL;
5168

E
Eric Blake 已提交
5169 5170
    virCheckFlags(0, NULL);

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

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

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

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

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

5196
    ret = virStorageVolDefFormat(privpool->def, privvol);
5197 5198

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

static char *
5205
testStorageVolGetPath(virStorageVolPtr vol) {
5206 5207 5208
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5209
    char *ret = NULL;
5210

5211
    testDriverLock(privconn);
5212 5213
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5214
    testDriverUnlock(privconn);
5215 5216

    if (privpool == NULL) {
5217
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5218
        goto cleanup;
5219 5220 5221 5222 5223
    }

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

    if (privvol == NULL) {
5224 5225 5226
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5227
        goto cleanup;
5228 5229 5230
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5231 5232
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5233
        goto cleanup;
5234 5235
    }

C
Cole Robinson 已提交
5236
    ret = strdup(privvol->target.path);
5237
    if (ret == NULL)
5238
        virReportOOMError();
5239 5240

cleanup:
5241 5242
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5243 5244 5245
    return ret;
}

5246

5247
/* Node device implementations */
5248 5249 5250
static virDrvOpenStatus testNodeDeviceOpen(virConnectPtr conn,
                                           virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                           unsigned int flags)
E
Eric Blake 已提交
5251 5252 5253
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5254 5255 5256
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5257
    conn->nodeDevicePrivateData = conn->privateData;
5258 5259 5260
    return VIR_DRV_OPEN_SUCCESS;
}

5261 5262
static int testNodeDeviceClose(virConnectPtr conn) {
    conn->nodeDevicePrivateData = NULL;
5263 5264 5265
    return 0;
}

5266 5267 5268
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5269
                     unsigned int flags)
5270 5271 5272 5273 5274
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
5275 5276
    virCheckFlags(0, -1);

5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291
    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 已提交
5292
                    unsigned int flags)
5293 5294 5295 5296 5297
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
5298 5299
    virCheckFlags(0, -1);

5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335
    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) {
5336
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

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

static char *
5349
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5350
                         unsigned int flags)
5351 5352 5353 5354 5355
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5356 5357
    virCheckFlags(0, NULL);

5358 5359 5360 5361 5362
    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
5363 5364 5365
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5366 5367 5368
        goto cleanup;
    }

5369
    ret = virNodeDeviceDefFormat(obj->def);
5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388

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) {
5389 5390 5391
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5392 5393 5394 5395 5396 5397
        goto cleanup;
    }

    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
5398
            virReportOOMError();
5399
    } else {
5400 5401
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5402 5403 5404 5405 5406 5407 5408 5409
    }

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

5410

5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424
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) {
5425 5426 5427
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455
        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) {
5456 5457 5458
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479
        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;
}

5480 5481 5482
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
E
Eric Blake 已提交
5483
                        unsigned int flags)
5484 5485 5486 5487 5488 5489 5490 5491 5492
{
    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 已提交
5493 5494
    virCheckFlags(0, NULL);

5495 5496
    testDriverLock(driver);

5497
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, NULL);
5498 5499 5500 5501 5502
    if (def == NULL) {
        goto cleanup;
    }

    /* We run these next two simply for validation */
5503
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1) {
5504 5505 5506
        goto cleanup;
    }

5507
    if (virNodeDeviceGetParentHost(&driver->devs,
5508 5509 5510 5511 5512 5513 5514 5515 5516 5517
                                   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))) {
5518
        virReportOOMError();
5519 5520 5521 5522 5523 5524 5525 5526 5527 5528
        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;

5529
        caps->data.scsi_host.host = virRandomBits(10);
5530 5531 5532 5533
        caps = caps->next;
    }


5534
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def))) {
5535 5536 5537 5538 5539 5540 5541 5542
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
cleanup:
    testDriverUnlock(driver);
5543
    virNodeDeviceDefFree(def);
5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562
    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) {
5563
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5564 5565 5566
        goto out;
    }

5567
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
5568 5569 5570 5571 5572
        goto out;
    }

    parent_name = strdup(obj->def->parent);
    if (parent_name == NULL) {
5573
        virReportOOMError();
5574 5575 5576 5577 5578 5579 5580 5581 5582 5583
        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 */
5584
    if (virNodeDeviceGetParentHost(&driver->devs,
5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603
                                   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;
}

5604 5605

/* Domain event implementations */
5606
static int
5607 5608 5609 5610
testConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
5611 5612 5613 5614 5615
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5616 5617 5618
    ret = virDomainEventStateRegister(conn,
                                      driver->domainEventState,
                                      callback, opaque, freecb);
5619 5620 5621 5622 5623
    testDriverUnlock(driver);

    return ret;
}

5624

5625
static int
5626 5627
testConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
5628 5629 5630 5631 5632
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5633 5634 5635
    ret = virDomainEventStateDeregister(conn,
                                        driver->domainEventState,
                                        callback);
5636 5637 5638 5639 5640
    testDriverUnlock(driver);

    return ret;
}

5641 5642

static int
5643 5644 5645 5646 5647 5648
testConnectDomainEventRegisterAny(virConnectPtr conn,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback callback,
                                  void *opaque,
                                  virFreeCallback freecb)
5649 5650 5651 5652 5653
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5654 5655 5656 5657
    if (virDomainEventStateRegisterID(conn,
                                      driver->domainEventState,
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
5658
        ret = -1;
5659 5660 5661 5662 5663 5664
    testDriverUnlock(driver);

    return ret;
}

static int
5665 5666
testConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
5667 5668 5669 5670 5671
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5672 5673 5674
    ret = virDomainEventStateDeregisterID(conn,
                                          driver->domainEventState,
                                          callbackID);
5675 5676 5677 5678 5679 5680
    testDriverUnlock(driver);

    return ret;
}


5681 5682 5683 5684
/* driver must be locked before calling */
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event)
{
5685
    virDomainEventStateQueue(driver->domainEventState, event);
5686 5687
}

5688 5689
static virDrvOpenStatus testSecretOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5690 5691 5692 5693
                                       unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5694 5695 5696
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5697
    conn->secretPrivateData = conn->privateData;
5698 5699 5700 5701 5702 5703 5704
    return VIR_DRV_OPEN_SUCCESS;
}

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

5706 5707 5708

static virDrvOpenStatus testNWFilterOpen(virConnectPtr conn,
                                         virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5709 5710 5711 5712
                                         unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5713 5714 5715
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5716
    conn->nwfilterPrivateData = conn->privateData;
5717 5718 5719 5720 5721 5722 5723 5724
    return VIR_DRV_OPEN_SUCCESS;
}

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

5725 5726 5727
static int testConnectListAllDomains(virConnectPtr conn,
                                     virDomainPtr **domains,
                                     unsigned int flags)
5728 5729 5730 5731
{
    testConnPtr privconn = conn->privateData;
    int ret;

O
Osier Yang 已提交
5732
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
5733 5734

    testDriverLock(privconn);
5735
    ret = virDomainObjListExport(privconn->domains, conn, domains, flags);
5736 5737 5738 5739 5740
    testDriverUnlock(privconn);

    return ret;
}

5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770
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;
}

5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

    if (!(ret = strdup("image/png"))) {
        virReportOOMError();
        return NULL;
    }

    if (virFDStreamOpenFile(st, PKGDATADIR "/libvirtLogo.png", 0, 0, O_RDONLY < 0))
        VIR_FREE(ret);

    return ret;
}

5792

5793
static virDriver testDriver = {
5794 5795
    .no = VIR_DRV_TEST,
    .name = "Test",
5796 5797 5798
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
5799
    .connectGetHostname = virGetHostname, /* 0.6.3 */
5800
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
5801
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
5802 5803 5804 5805
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
5806
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820
    .domainLookupByID = testDomainLookupByID, /* 0.1.1 */
    .domainLookupByUUID = testDomainLookupByUUID, /* 0.1.1 */
    .domainLookupByName = testDomainLookupByName, /* 0.1.1 */
    .domainSuspend = testDomainSuspend, /* 0.1.1 */
    .domainResume = testDomainResume, /* 0.1.1 */
    .domainShutdown = testDomainShutdown, /* 0.1.1 */
    .domainShutdownFlags = testDomainShutdownFlags, /* 0.9.10 */
    .domainReboot = testDomainReboot, /* 0.1.1 */
    .domainDestroy = testDomainDestroy, /* 0.1.1 */
    .domainGetOSType = testDomainGetOSType, /* 0.1.9 */
    .domainGetMaxMemory = testDomainGetMaxMemory, /* 0.1.4 */
    .domainSetMaxMemory = testDomainSetMaxMemory, /* 0.1.1 */
    .domainSetMemory = testDomainSetMemory, /* 0.1.4 */
    .domainGetInfo = testDomainGetInfo, /* 0.1.1 */
5821 5822
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
5823
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
5824
    .domainRestore = testDomainRestore, /* 0.3.2 */
5825
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
5826
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
5827
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
5828 5829 5830 5831 5832 5833
    .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 */
5834 5835
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
5836 5837 5838 5839
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
5840
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
5841 5842 5843
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
5844 5845 5846 5847
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
5848 5849 5850
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
5851 5852 5853 5854
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
5855 5856 5857
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
5858 5859 5860
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
5861
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
5862
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
5863 5864 5865 5866
};

static virNetworkDriver testNetworkDriver = {
    "Test",
5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877
    .networkOpen = testNetworkOpen, /* 0.3.2 */
    .networkClose = testNetworkClose, /* 0.3.2 */
    .connectNumOfNetworks = testConnectNumOfNetworks, /* 0.3.2 */
    .connectListNetworks = testConnectListNetworks, /* 0.3.2 */
    .connectNumOfDefinedNetworks = testConnectNumOfDefinedNetworks, /* 0.3.2 */
    .connectListDefinedNetworks = testConnectListDefinedNetworks, /* 0.3.2 */
    .connectListAllNetworks = testConnectListAllNetworks, /* 0.10.2 */
    .networkLookupByUUID = testNetworkLookupByUUID, /* 0.3.2 */
    .networkLookupByName = testNetworkLookupByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreateXML, /* 0.3.2 */
    .networkDefineXML = testNetworkDefineXML, /* 0.3.2 */
5878
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
5879
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
5880
    .networkCreate = testNetworkCreate, /* 0.3.2 */
5881 5882 5883 5884 5885 5886 5887
    .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 */
5888 5889
};

L
Laine Stump 已提交
5890 5891
static virInterfaceDriver testInterfaceDriver = {
    "Test",                     /* name */
5892 5893 5894 5895 5896 5897 5898 5899
    .interfaceOpen = testInterfaceOpen, /* 0.7.0 */
    .interfaceClose = testInterfaceClose, /* 0.7.0 */
    .connectNumOfInterfaces = testConnectNumOfInterfaces, /* 0.7.0 */
    .connectListInterfaces = testConnectListInterfaces, /* 0.7.0 */
    .connectNumOfDefinedInterfaces = testConnectNumOfDefinedInterfaces, /* 0.7.0 */
    .connectListDefinedInterfaces = testConnectListDefinedInterfaces, /* 0.7.0 */
    .interfaceLookupByName = testInterfaceLookupByName, /* 0.7.0 */
    .interfaceLookupByMACString = testInterfaceLookupByMACString, /* 0.7.0 */
5900 5901 5902 5903 5904 5905
    .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 */
5906 5907 5908
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
5909 5910 5911
};


5912 5913
static virStorageDriver testStorageDriver = {
    .name = "Test",
5914 5915
    .storageOpen = testStorageOpen, /* 0.4.1 */
    .storageClose = testStorageClose, /* 0.4.1 */
5916

5917 5918 5919 5920 5921 5922
    .connectNumOfStoragePools = testConnectNumOfStoragePools, /* 0.5.0 */
    .connectListStoragePools = testConnectListStoragePools, /* 0.5.0 */
    .connectNumOfDefinedStoragePools = testConnectNumOfDefinedStoragePools, /* 0.5.0 */
    .connectListDefinedStoragePools = testConnectListDefinedStoragePools, /* 0.5.0 */
    .connectListAllStoragePools = testConnectListAllStoragePools, /* 0.10.2 */
    .connectFindStoragePoolSources = testConnectFindStoragePoolSources, /* 0.5.0 */
5923 5924 5925
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
5926 5927
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
5928 5929
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
5930
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
5931 5932 5933 5934 5935 5936 5937
    .storagePoolDestroy = testStoragePoolDestroy, /* 0.5.0 */
    .storagePoolDelete = testStoragePoolDelete, /* 0.5.0 */
    .storagePoolRefresh = testStoragePoolRefresh, /* 0.5.0 */
    .storagePoolGetInfo = testStoragePoolGetInfo, /* 0.5.0 */
    .storagePoolGetXMLDesc = testStoragePoolGetXMLDesc, /* 0.5.0 */
    .storagePoolGetAutostart = testStoragePoolGetAutostart, /* 0.5.0 */
    .storagePoolSetAutostart = testStoragePoolSetAutostart, /* 0.5.0 */
5938
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
5939 5940 5941
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

5942 5943 5944 5945 5946 5947 5948 5949 5950
    .storageVolLookupByName = testStorageVolLookupByName, /* 0.5.0 */
    .storageVolLookupByKey = testStorageVolLookupByKey, /* 0.5.0 */
    .storageVolLookupByPath = testStorageVolLookupByPath, /* 0.5.0 */
    .storageVolCreateXML = testStorageVolCreateXML, /* 0.5.0 */
    .storageVolCreateXMLFrom = testStorageVolCreateXMLFrom, /* 0.6.4 */
    .storageVolDelete = testStorageVolDelete, /* 0.5.0 */
    .storageVolGetInfo = testStorageVolGetInfo, /* 0.5.0 */
    .storageVolGetXMLDesc = testStorageVolGetXMLDesc, /* 0.5.0 */
    .storageVolGetPath = testStorageVolGetPath, /* 0.5.0 */
5951 5952
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
5953 5954
};

5955
static virNodeDeviceDriver testNodeDeviceDriver = {
5956
    .name = "Test",
5957 5958
    .nodeDeviceOpen = testNodeDeviceOpen, /* 0.6.0 */
    .nodeDeviceClose = testNodeDeviceClose, /* 0.6.0 */
5959 5960 5961 5962 5963 5964 5965 5966 5967 5968

    .nodeNumOfDevices = testNodeNumOfDevices, /* 0.7.2 */
    .nodeListDevices = testNodeListDevices, /* 0.7.2 */
    .nodeDeviceLookupByName = testNodeDeviceLookupByName, /* 0.7.2 */
    .nodeDeviceGetXMLDesc = testNodeDeviceGetXMLDesc, /* 0.7.2 */
    .nodeDeviceGetParent = testNodeDeviceGetParent, /* 0.7.2 */
    .nodeDeviceNumOfCaps = testNodeDeviceNumOfCaps, /* 0.7.2 */
    .nodeDeviceListCaps = testNodeDeviceListCaps, /* 0.7.2 */
    .nodeDeviceCreateXML = testNodeDeviceCreateXML, /* 0.7.3 */
    .nodeDeviceDestroy = testNodeDeviceDestroy, /* 0.7.3 */
5969 5970
};

5971 5972
static virSecretDriver testSecretDriver = {
    .name = "Test",
5973 5974
    .secretOpen = testSecretOpen, /* 0.7.1 */
    .secretClose = testSecretClose, /* 0.7.1 */
5975
};
5976 5977


5978 5979
static virNWFilterDriver testNWFilterDriver = {
    .name = "Test",
5980 5981
    .nwfilterOpen = testNWFilterOpen, /* 0.8.0 */
    .nwfilterClose = testNWFilterClose, /* 0.8.0 */
5982 5983
};

5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
5996 5997
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
5998 5999
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
6000
    if (virRegisterNodeDeviceDriver(&testNodeDeviceDriver) < 0)
6001
        return -1;
6002 6003
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
6004 6005
    if (virRegisterNWFilterDriver(&testNWFilterDriver) < 0)
        return -1;
6006

6007 6008
    return 0;
}