test_driver.c 156.2 KB
Newer Older
1 2 3
/*
 * test.c: A "mock" hypervisor for use by application unit tests
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2006-2011 Red Hat, Inc.
5
 * Copyright (C) 2006 Daniel P. Berrange
6
 *
7 8 9 10 11 12 13 14 15 16 17 18 19
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
20 21 22 23
 *
 * Daniel Berrange <berrange@redhat.com>
 */

24
#include <config.h>
25

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

34 35

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

54 55
#define VIR_FROM_THIS VIR_FROM_TEST

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

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

65 66 67 68 69 70 71 72 73 74 75
#define MAX_CPUS 128

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

#define MAX_CELLS 128
76

77
struct _testConn {
78
    virMutex lock;
79

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

94
    virDomainEventStatePtr domainEventState;
95 96 97
};
typedef struct _testConn testConn;
typedef struct _testConn *testConnPtr;
98

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

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

114

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

119 120 121 122 123 124
static int testClose(virConnectPtr conn);
static void testDomainEventFlush(int timer, void *opaque);
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 156
static virCapsPtr
testBuildCapabilities(virConnectPtr conn) {
157
    testConnPtr privconn = conn->privateData;
158 159 160 161
    virCapsPtr caps;
    virCapsGuestPtr guest;
    const char *const guest_types[] = { "hvm", "xen" };
    int i;
162

163 164
    if ((caps = virCapabilitiesNew(TEST_MODEL, 0, 0)) == NULL)
        goto no_memory;
165

166 167 168 169
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
        goto no_memory;
    if (virCapabilitiesAddHostFeature(caps ,"nonpae") < 0)
        goto no_memory;
170

171 172 173 174
    for (i = 0; i < privconn->numCells; i++) {
        if (virCapabilitiesAddHostNUMACell(caps, i, privconn->cells[i].numCpus,
                                           privconn->cells[i].cpus) < 0)
            goto no_memory;
175 176
    }

177 178 179 180 181 182 183 184 185 186
    for (i = 0; i < ARRAY_CARDINALITY(guest_types) ; i++) {
        if ((guest = virCapabilitiesAddGuest(caps,
                                             guest_types[i],
                                             TEST_MODEL,
                                             TEST_MODEL_WORDSIZE,
                                             TEST_EMULATOR,
                                             NULL,
                                             0,
                                             NULL)) == NULL)
            goto no_memory;
187

188 189 190 191 192 193 194
        if (virCapabilitiesAddGuestDomain(guest,
                                          "test",
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
            goto no_memory;
195

196 197 198 199
        if (virCapabilitiesAddGuestFeature(guest, "pae", 1, 1) == NULL)
            goto no_memory;
        if (virCapabilitiesAddGuestFeature(guest ,"nonpae", 1, 1) == NULL)
            goto no_memory;
200 201
    }

202 203 204
    caps->privateDataAllocFunc = testDomainObjPrivateAlloc;
    caps->privateDataFreeFunc = testDomainObjPrivateFree;

205 206 207 208 209 210 211 212
    caps->host.secModel.model = strdup("testSecurity");
    if (!caps->host.secModel.model)
        goto no_memory;

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

213
    return caps;
214

215
no_memory:
216
    virReportOOMError();
217 218
    virCapabilitiesFree(caps);
    return NULL;
219 220
}

221

222 223 224 225 226 227 228 229 230 231
static const char *defaultDomainXML =
"<domain type='test'>"
"  <name>test</name>"
"  <memory>8388608</memory>"
"  <currentMemory>2097152</currentMemory>"
"  <vcpu>2</vcpu>"
"  <os>"
"    <type>hvm</type>"
"  </os>"
"</domain>";
232 233


234 235 236 237 238 239 240 241 242 243 244
static const char *defaultNetworkXML =
"<network>"
"  <name>default</name>"
"  <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>";
245

L
Laine Stump 已提交
246 247 248 249 250 251 252 253 254 255 256
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 已提交
257 258 259 260 261 262 263 264
static const char *defaultPoolXML =
"<pool type='dir'>"
"  <name>default-pool</name>"
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
"</pool>";

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
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";

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

306
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
C
Cole Robinson 已提交
307 308
static const unsigned long long defaultPoolAlloc = 0;

309
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool);
310
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
311

312
static char *
313
testDomainGenerateIfname(virDomainDefPtr domdef) {
314 315 316 317 318 319 320 321
    int maxif = 1024;
    int ifctr, i;

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

        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0) {
322
            virReportOOMError();
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
            return NULL;
        }

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

        if (!found)
            return ifname;
    }

339
    testError(VIR_ERR_INTERNAL_ERROR,
340 341 342 343
              _("Exceeded max iface limit %d"), maxif);
    return NULL;
}

344
static int
345
testDomainGenerateIfnames(virDomainDefPtr domdef)
346 347 348 349 350 351 352 353
{
    int i = 0;

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

354
        ifname = testDomainGenerateIfname(domdef);
355
        if (!ifname)
356
            return -1;
357 358 359 360

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

361
    return 0;
362 363
}

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
/* Helper to update info for a single VCPU */
static int
testDomainUpdateVCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
                     virDomainObjPtr dom,
                     int vcpu,
                     int maplen,
                     int maxcpu)
{
    testDomainObjPrivatePtr privdata = dom->privateData;
    virVcpuInfoPtr info = &privdata->vcpu_infos[vcpu];
    unsigned char *cpumap = VIR_GET_CPUMAP(privdata->cpumaps, maplen, vcpu);
    int j;

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

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

    if (dom->def->cpumask) {
        for (j = 0; j < maxcpu && j < VIR_DOMAIN_CPUMASK_LEN; ++j) {
            if (dom->def->cpumask[j]) {
                VIR_USE_CPU(cpumap, j);
                info->cpu = j;
            }
        }
    } else {
        for (j = 0; j < maxcpu; ++j) {
            if ((j % 3) == 0) {
                /* Mark of every third CPU as usable */
                VIR_USE_CPU(cpumap, j);
                info->cpu = j;
            }
        }
    }

    return 0;
}

/*
 * Update domain VCPU amount and info
 *
 * @conn: virConnectPtr
 * @dom : domain needing updates
 * @nvcpus: New amount of vcpus for the domain
 * @clear_all: If true, rebuild info for ALL vcpus, not just newly added vcpus
 */
static int
testDomainUpdateVCPUs(virConnectPtr conn,
                      virDomainObjPtr dom,
                      int nvcpus,
                      unsigned int clear_all)
{
    testConnPtr privconn = conn->privateData;
    testDomainObjPrivatePtr privdata = dom->privateData;
    int i, ret = -1;
    int cpumaplen, maxcpu;

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

    if (VIR_REALLOC_N(privdata->vcpu_infos, nvcpus) < 0) {
428
        virReportOOMError();
429 430 431 432
        goto cleanup;
    }

    if (VIR_REALLOC_N(privdata->cpumaps, nvcpus * cpumaplen) < 0) {
433
        virReportOOMError();
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
        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;
    }

450
    dom->def->vcpus = nvcpus;
451 452 453 454 455
    ret = 0;
cleanup:
    return ret;
}

456 457
static void
testDomainShutdownState(virDomainPtr domain,
J
Jiri Denemark 已提交
458 459
                        virDomainObjPtr privdom,
                        virDomainShutoffReason reason)
460 461 462 463 464 465 466
{
    if (privdom->newDef) {
        virDomainDefFree(privdom->def);
        privdom->def = privdom->newDef;
        privdom->newDef = NULL;
    }

J
Jiri Denemark 已提交
467
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF, reason);
468 469 470 471 472
    privdom->def->id = -1;
    if (domain)
        domain->id = -1;
}

473
/* Set up domain runtime state */
474 475
static int
testDomainStartState(virConnectPtr conn,
J
Jiri Denemark 已提交
476 477
                     virDomainObjPtr dom,
                     virDomainRunningReason reason)
478 479
{
    testConnPtr privconn = conn->privateData;
480
    int ret = -1;
481

482 483 484
    if (testDomainUpdateVCPUs(conn, dom, dom->def->vcpus, 1) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
485
    virDomainObjSetState(dom, VIR_DOMAIN_RUNNING, reason);
486 487
    dom->def->id = privconn->nextDomID++;

488
    if (virDomainObjSetDefTransient(privconn->caps, dom, false) < 0) {
489 490 491
        goto cleanup;
    }

492 493
    ret = 0;
cleanup:
494
    if (ret < 0)
J
Jiri Denemark 已提交
495
        testDomainShutdownState(NULL, dom, VIR_DOMAIN_SHUTOFF_FAILED);
496
    return ret;
497
}
498

499
static int testOpenDefault(virConnectPtr conn) {
500
    int u;
501
    testConnPtr privconn;
502 503 504 505
    virDomainDefPtr domdef = NULL;
    virDomainObjPtr domobj = NULL;
    virNetworkDefPtr netdef = NULL;
    virNetworkObjPtr netobj = NULL;
L
Laine Stump 已提交
506 507
    virInterfaceDefPtr interfacedef = NULL;
    virInterfaceObjPtr interfaceobj = NULL;
C
Cole Robinson 已提交
508 509
    virStoragePoolDefPtr pooldef = NULL;
    virStoragePoolObjPtr poolobj = NULL;
510 511
    virNodeDeviceDefPtr nodedef = NULL;
    virNodeDeviceObjPtr nodeobj = NULL;
512

513
    if (VIR_ALLOC(privconn) < 0) {
514
        virReportOOMError();
515 516
        return VIR_DRV_OPEN_ERROR;
    }
517
    if (virMutexInit(&privconn->lock) < 0) {
518
        testError(VIR_ERR_INTERNAL_ERROR,
519 520 521 522 523
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

524
    testDriverLock(privconn);
525
    conn->privateData = privconn;
526

527 528 529
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

530
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
531

532
    /* Numa setup */
533 534 535 536 537 538 539 540 541
    privconn->numCells = 2;
    for (u = 0; u < 2; ++u) {
        privconn->cells[u].numCpus = 8;
        privconn->cells[u].mem = (u + 1) * 2048 * 1024;
    }
    for (u = 0 ; u < 16 ; u++) {
        privconn->cells[u % 2].cpus[(u / 2)] = u;
    }

542 543 544 545 546
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;

    privconn->nextDomID = 1;

547
    if (!(domdef = virDomainDefParseString(privconn->caps,
548 549
                                           defaultDomainXML,
                                           VIR_DOMAIN_XML_INACTIVE)))
550
        goto error;
551
    if (testDomainGenerateIfnames(domdef) < 0)
552
        goto error;
553
    if (!(domobj = virDomainAssignDef(privconn->caps,
554
                                      &privconn->domains, domdef, false)))
555 556
        goto error;
    domdef = NULL;
557

558
    domobj->persistent = 1;
J
Jiri Denemark 已提交
559
    if (testDomainStartState(conn, domobj, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
560 561 562 563
        virDomainObjUnlock(domobj);
        goto error;
    }

564
    virDomainObjUnlock(domobj);
565

566
    if (!(netdef = virNetworkDefParseString(defaultNetworkXML)))
567
        goto error;
568
    if (!(netobj = virNetworkAssignDef(&privconn->networks, netdef))) {
569 570 571 572 573
        virNetworkDefFree(netdef);
        goto error;
    }
    netobj->active = 1;
    netobj->persistent = 1;
574
    virNetworkObjUnlock(netobj);
575

576
    if (!(interfacedef = virInterfaceDefParseString(defaultInterfaceXML)))
L
Laine Stump 已提交
577
        goto error;
578
    if (!(interfaceobj = virInterfaceAssignDef(&privconn->ifaces, interfacedef))) {
L
Laine Stump 已提交
579 580 581 582 583 584
        virInterfaceDefFree(interfacedef);
        goto error;
    }
    interfaceobj->active = 1;
    virInterfaceObjUnlock(interfaceobj);

585
    if (!(pooldef = virStoragePoolDefParseString(defaultPoolXML)))
C
Cole Robinson 已提交
586 587
        goto error;

588
    if (!(poolobj = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
589 590 591 592
                                               pooldef))) {
        virStoragePoolDefFree(pooldef);
        goto error;
    }
593

594
    if (testStoragePoolObjSetDefaults(poolobj) == -1) {
595
        virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
596
        goto error;
597
    }
C
Cole Robinson 已提交
598
    poolobj->active = 1;
599
    virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
600

601
    /* Init default node device */
602
    if (!(nodedef = virNodeDeviceDefParseString(defaultNodeXML, 0)))
603
        goto error;
604
    if (!(nodeobj = virNodeDeviceAssignDef(&privconn->devs,
605 606 607 608 609 610
                                           nodedef))) {
        virNodeDeviceDefFree(nodedef);
        goto error;
    }
    virNodeDeviceObjUnlock(nodeobj);

611
    testDriverUnlock(privconn);
612

613 614 615
    return VIR_DRV_OPEN_SUCCESS;

error:
616
    virDomainObjListDeinit(&privconn->domains);
617
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
618
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
619
    virStoragePoolObjListFree(&privconn->pools);
620
    virNodeDeviceObjListFree(&privconn->devs);
621
    virCapabilitiesFree(privconn->caps);
622
    testDriverUnlock(privconn);
623
    conn->privateData = NULL;
624
    VIR_FREE(privconn);
625
    virDomainDefFree(domdef);
626
    return VIR_DRV_OPEN_ERROR;
627 628 629 630
}


static char *testBuildFilename(const char *relativeTo,
631 632 633 634 635 636 637 638
                               const char *filename) {
    char *offset;
    int baseLen;
    if (!filename || filename[0] == '\0')
        return (NULL);
    if (filename[0] == '/')
        return strdup(filename);

639
    offset = strrchr(relativeTo, '/');
640
    if ((baseLen = (offset-relativeTo+1))) {
641
        char *absFile;
C
Chris Lalancette 已提交
642 643
        int totalLen = baseLen + strlen(filename) + 1;
        if (VIR_ALLOC_N(absFile, totalLen) < 0)
644
            return NULL;
C
Chris Lalancette 已提交
645 646 647 648
        if (virStrncpy(absFile, relativeTo, baseLen, totalLen) == NULL) {
            VIR_FREE(absFile);
            return NULL;
        }
649 650 651 652 653
        strcat(absFile, filename);
        return absFile;
    } else {
        return strdup(filename);
    }
654 655
}

656
static int testOpenVolumesForPool(xmlDocPtr xml,
657 658 659 660 661 662 663
                                  xmlXPathContextPtr ctxt,
                                  const char *file,
                                  virStoragePoolObjPtr pool,
                                  int poolidx) {
    char *vol_xpath;
    int i, ret, func_ret = -1;
    xmlNodePtr *vols = NULL;
664
    virStorageVolDefPtr def = NULL;
665 666 667

    /* Find storage volumes */
    if (virAsprintf(&vol_xpath, "/node/pool[%d]/volume", poolidx) < 0) {
668
        virReportOOMError();
669 670 671
        goto error;
    }

672
    ret = virXPathNodeSet(vol_xpath, ctxt, &vols);
673 674 675 676 677 678 679 680 681 682 683
    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) {
684
                testError(VIR_ERR_INTERNAL_ERROR, "%s",
685 686 687 688
                          _("resolving volume filename"));
                goto error;
            }

689
            def = virStorageVolDefParseFile(pool->def, absFile);
690 691 692 693
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
694
            if ((def = virStorageVolDefParseNode(pool->def, xml,
695 696 697 698 699 700 701
                                                 vols[i])) == NULL) {
                goto error;
            }
        }

        if (VIR_REALLOC_N(pool->volumes.objs,
                          pool->volumes.count+1) < 0) {
702
            virReportOOMError();
703 704 705
            goto error;
        }

706 707 708 709 710 711 712
        if (def->target.path == NULL) {
            if (virAsprintf(&def->target.path, "%s/%s",
                            pool->def->target.path,
                            def->name) == -1) {
                virReportOOMError();
                goto error;
            }
713 714 715
        }

        if (def->key == NULL) {
716 717 718 719 720
            def->key = strdup(def->target.path);
            if (def->key == NULL) {
                virReportOOMError();
                goto error;
            }
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
        }

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

738
static int testOpenFromFile(virConnectPtr conn,
739
                            const char *file) {
740
    int i, ret;
741 742
    long l;
    char *str;
743
    xmlDocPtr xml = NULL;
744
    xmlNodePtr root = NULL;
745 746
    xmlNodePtr *domains = NULL, *networks = NULL, *ifaces = NULL,
               *pools = NULL, *devs = NULL;
747 748
    xmlXPathContextPtr ctxt = NULL;
    virNodeInfoPtr nodeInfo;
749
    virNetworkObjPtr net;
L
Laine Stump 已提交
750
    virInterfaceObjPtr iface;
751
    virDomainObjPtr dom;
752 753
    testConnPtr privconn;
    if (VIR_ALLOC(privconn) < 0) {
754
        virReportOOMError();
755 756
        return VIR_DRV_OPEN_ERROR;
    }
757
    if (virMutexInit(&privconn->lock) < 0) {
758
        testError(VIR_ERR_INTERNAL_ERROR,
759 760 761 762 763
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

764
    testDriverLock(privconn);
765
    conn->privateData = privconn;
766

767 768 769
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

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

773
    if (!(xml = virXMLParseFile(file))) {
774
        goto error;
775 776
    }

777 778
    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
779
        testError(VIR_ERR_XML_ERROR, "%s",
780
                  _("Root element is not 'node'"));
781
        goto error;
782 783
    }

784 785
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
786
        testError(VIR_ERR_INTERNAL_ERROR, "%s",
C
Cole Robinson 已提交
787
                  _("creating xpath context"));
788
        goto error;
789
    }
790

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

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

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

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

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

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

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

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

876
    ret = virXPathNodeSet("/node/domain", ctxt, &domains);
877
    if (ret < 0) {
878
        goto error;
879
    }
880

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

902
        if (testDomainGenerateIfnames(def) < 0 ||
903
            !(dom = virDomainAssignDef(privconn->caps,
904
                                       &privconn->domains, def, false))) {
905
            virDomainDefFree(def);
906 907
            goto error;
        }
908

909
        dom->persistent = 1;
J
Jiri Denemark 已提交
910
        if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
911 912 913 914
            virDomainObjUnlock(dom);
            goto error;
        }

915
        virDomainObjUnlock(dom);
916
    }
917
    VIR_FREE(domains);
918

919
    ret = virXPathNodeSet("/node/network", ctxt, &networks);
920 921 922 923 924 925 926 927 928
    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);
929
            if (!absFile) {
930
                testError(VIR_ERR_INTERNAL_ERROR, "%s", _("resolving network filename"));
931 932
                goto error;
            }
933

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

L
Laine Stump 已提交
953
    /* Parse interface definitions */
954
    ret = virXPathNodeSet("/node/interface", ctxt, &ifaces);
L
Laine Stump 已提交
955 956 957 958 959 960 961 962 963 964
    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) {
965
                testError(VIR_ERR_INTERNAL_ERROR, "%s", _("resolving interface filename"));
L
Laine Stump 已提交
966 967 968
                goto error;
            }

969
            def = virInterfaceDefParseFile(absFile);
L
Laine Stump 已提交
970 971 972 973
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
974
            if ((def = virInterfaceDefParseNode(xml, ifaces[i])) == NULL)
L
Laine Stump 已提交
975 976
                goto error;
        }
977

978
        if (!(iface = virInterfaceAssignDef(&privconn->ifaces, def))) {
L
Laine Stump 已提交
979 980 981
            virInterfaceDefFree(def);
            goto error;
        }
982 983

        iface->active = 1;
L
Laine Stump 已提交
984 985 986 987
        virInterfaceObjUnlock(iface);
    }
    VIR_FREE(ifaces);

C
Cole Robinson 已提交
988
    /* Parse Storage Pool list */
989
    ret = virXPathNodeSet("/node/pool", ctxt, &pools);
C
Cole Robinson 已提交
990 991 992 993 994 995 996 997 998 999 1000
    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) {
1001
                testError(VIR_ERR_INTERNAL_ERROR, "%s",
C
Cole Robinson 已提交
1002 1003 1004 1005
                          _("resolving pool filename"));
                goto error;
            }

1006
            def = virStoragePoolDefParseFile(absFile);
C
Cole Robinson 已提交
1007 1008 1009 1010
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
1011
            if ((def = virStoragePoolDefParseNode(xml,
1012
                                                  pools[i])) == NULL) {
C
Cole Robinson 已提交
1013 1014 1015 1016
                goto error;
            }
        }

1017
        if (!(pool = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
1018 1019 1020 1021 1022
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

1023
        if (testStoragePoolObjSetDefaults(pool) == -1) {
1024
            virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1025
            goto error;
1026
        }
C
Cole Robinson 已提交
1027
        pool->active = 1;
1028 1029

        /* Find storage volumes */
1030
        if (testOpenVolumesForPool(xml, ctxt, file, pool, i+1) < 0) {
1031 1032 1033 1034
            virStoragePoolObjUnlock(pool);
            goto error;
        }

1035
        virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1036
    }
1037
    VIR_FREE(pools);
C
Cole Robinson 已提交
1038

1039
    ret = virXPathNodeSet("/node/device", ctxt, &devs);
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
    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) {
1053
                testError(VIR_ERR_INTERNAL_ERROR, "%s",
1054 1055 1056 1057
                          _("resolving device filename"));
                goto error;
            }

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


J
Jim Meyering 已提交
1075
    xmlXPathFreeContext(ctxt);
1076
    xmlFreeDoc(xml);
1077
    testDriverUnlock(privconn);
1078

1079
    return (0);
1080 1081

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

1099

1100
static virDrvOpenStatus testOpen(virConnectPtr conn,
1101 1102
                                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                 unsigned int flags ATTRIBUTE_UNUSED)
1103
{
1104
    int ret;
1105
    testConnPtr privconn;
1106

1107
    if (!conn->uri)
1108
        return VIR_DRV_OPEN_DECLINED;
1109

1110
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1111
        return VIR_DRV_OPEN_DECLINED;
1112

1113
    /* Remote driver should handle these. */
1114
    if (conn->uri->server)
1115 1116
        return VIR_DRV_OPEN_DECLINED;

1117
    /* From this point on, the connection is for us. */
1118 1119 1120
    if (!conn->uri->path
        || conn->uri->path[0] == '\0'
        || (conn->uri->path[0] == '/' && conn->uri->path[1] == '\0')) {
1121 1122
        testError(VIR_ERR_INVALID_ARG,
                  "%s", _("testOpen: supply a path or use test:///default"));
1123 1124
        return VIR_DRV_OPEN_ERROR;
    }
1125

1126
    if (STREQ(conn->uri->path, "/default"))
1127 1128
        ret = testOpenDefault(conn);
    else
1129
        ret = testOpenFromFile(conn,
1130
                               conn->uri->path);
1131

1132 1133 1134 1135 1136
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

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

1138 1139 1140 1141 1142
    privconn->domainEventState = virDomainEventStateNew(testDomainEventFlush,
                                                        privconn,
                                                        NULL,
                                                        false);
    if (!privconn->domainEventState) {
1143
        testDriverUnlock(privconn);
1144 1145
        testClose(conn);
        return VIR_DRV_OPEN_ERROR;
1146 1147
    }

1148 1149 1150
    testDriverUnlock(privconn);

    return VIR_DRV_OPEN_SUCCESS;
1151 1152
}

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

1166
    testDriverUnlock(privconn);
1167
    virMutexDestroy(&privconn->lock);
1168

1169
    VIR_FREE (privconn);
1170
    conn->privateData = NULL;
1171
    return 0;
1172 1173
}

1174 1175
static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                          unsigned long *hvVer)
1176
{
1177 1178
    *hvVer = 2;
    return (0);
1179 1180
}

1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
static int testIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 1;
}

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

1191 1192 1193 1194 1195 1196 1197 1198
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1199
{
1200
    testConnPtr privconn = conn->privateData;
1201
    testDriverLock(privconn);
1202
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1203
    testDriverUnlock(privconn);
1204
    return (0);
1205 1206
}

1207
static char *testGetCapabilities (virConnectPtr conn)
1208
{
1209
    testConnPtr privconn = conn->privateData;
1210
    char *xml;
1211
    testDriverLock(privconn);
1212
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL)
1213
        virReportOOMError();
1214
    testDriverUnlock(privconn);
1215
    return xml;
1216 1217
}

1218
static int testNumOfDomains(virConnectPtr conn)
1219
{
1220
    testConnPtr privconn = conn->privateData;
1221
    int count;
1222

1223
    testDriverLock(privconn);
1224
    count = virDomainObjListNumOfDomains(&privconn->domains, 1);
1225
    testDriverUnlock(privconn);
1226

1227
    return count;
1228 1229
}

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
static int testDomainIsActive(virDomainPtr dom)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virDomainFindByUUID(&privconn->domains, dom->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
1240
        testError(VIR_ERR_NO_DOMAIN, NULL);
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
        goto cleanup;
    }
    ret = virDomainObjIsActive(obj);

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

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

    testDriverLock(privconn);
    obj = virDomainFindByUUID(&privconn->domains, dom->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
1261
        testError(VIR_ERR_NO_DOMAIN, NULL);
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
        goto cleanup;
    }
    ret = obj->persistent;

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

1272 1273 1274 1275 1276
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

1277
static virDomainPtr
1278
testDomainCreateXML(virConnectPtr conn, const char *xml,
1279
                      unsigned int flags)
1280
{
1281
    testConnPtr privconn = conn->privateData;
1282
    virDomainPtr ret = NULL;
1283
    virDomainDefPtr def;
1284
    virDomainObjPtr dom = NULL;
1285
    virDomainEventPtr event = NULL;
1286

1287 1288
    virCheckFlags(0, NULL);

1289
    testDriverLock(privconn);
1290
    if ((def = virDomainDefParseString(privconn->caps, xml,
1291
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1292
        goto cleanup;
1293

1294 1295 1296
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1297
    if (testDomainGenerateIfnames(def) < 0)
1298
        goto cleanup;
1299
    if (!(dom = virDomainAssignDef(privconn->caps,
1300
                                   &privconn->domains, def, false)))
1301 1302
        goto cleanup;
    def = NULL;
1303

J
Jiri Denemark 已提交
1304
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0)
1305
        goto cleanup;
1306

1307 1308 1309 1310
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1311
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1312
    if (ret)
1313
        ret->id = dom->def->id;
1314 1315

cleanup:
1316 1317
    if (dom)
        virDomainObjUnlock(dom);
1318 1319
    if (event)
        testDomainEventQueue(privconn, event);
1320
    virDomainDefFree(def);
1321
    testDriverUnlock(privconn);
1322
    return ret;
1323 1324 1325
}


1326 1327
static virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                         int id)
1328
{
1329
    testConnPtr privconn = conn->privateData;
1330 1331
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1332

1333 1334 1335 1336 1337
    testDriverLock(privconn);
    dom = virDomainFindByID(&privconn->domains, id);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1338
        testError(VIR_ERR_NO_DOMAIN, NULL);
1339
        goto cleanup;
1340 1341
    }

1342
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1343 1344 1345 1346
    if (ret)
        ret->id = dom->def->id;

cleanup:
1347 1348
    if (dom)
        virDomainObjUnlock(dom);
1349
    return ret;
1350 1351
}

1352 1353
static virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
1354
{
1355
    testConnPtr privconn = conn->privateData;
1356 1357
    virDomainPtr ret = NULL;
    virDomainObjPtr dom ;
1358

1359 1360 1361 1362 1363
    testDriverLock(privconn);
    dom = virDomainFindByUUID(&privconn->domains, uuid);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1364
        testError(VIR_ERR_NO_DOMAIN, NULL);
1365
        goto cleanup;
1366
    }
1367

1368
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1369 1370 1371 1372
    if (ret)
        ret->id = dom->def->id;

cleanup:
1373 1374
    if (dom)
        virDomainObjUnlock(dom);
1375
    return ret;
1376 1377
}

1378 1379
static virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                           const char *name)
1380
{
1381
    testConnPtr privconn = conn->privateData;
1382 1383
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1384

1385 1386 1387 1388 1389
    testDriverLock(privconn);
    dom = virDomainFindByName(&privconn->domains, name);
    testDriverUnlock(privconn);

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

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

cleanup:
1399 1400
    if (dom)
        virDomainObjUnlock(dom);
1401
    return ret;
1402 1403
}

1404 1405 1406
static int testListDomains (virConnectPtr conn,
                            int *ids,
                            int maxids)
1407
{
1408
    testConnPtr privconn = conn->privateData;
1409
    int n;
1410

1411
    testDriverLock(privconn);
1412
    n = virDomainObjListGetActiveIDs(&privconn->domains, ids, maxids);
1413
    testDriverUnlock(privconn);
1414

1415
    return n;
1416 1417
}

1418
static int testDestroyDomain (virDomainPtr domain)
1419
{
1420 1421
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1422
    virDomainEventPtr event = NULL;
1423
    int ret = -1;
1424

1425
    testDriverLock(privconn);
1426 1427 1428 1429
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1430
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1431
        goto cleanup;
1432
    }
1433

J
Jiri Denemark 已提交
1434
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1435 1436 1437
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1438

1439 1440 1441
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1442
        privdom = NULL;
1443
    }
1444 1445 1446

    ret = 0;
cleanup:
1447 1448
    if (privdom)
        virDomainObjUnlock(privdom);
1449 1450
    if (event)
        testDomainEventQueue(privconn, event);
1451
    testDriverUnlock(privconn);
1452
    return ret;
1453 1454
}

1455
static int testResumeDomain (virDomainPtr domain)
1456
{
1457 1458
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1459
    virDomainEventPtr event = NULL;
1460
    int ret = -1;
1461

1462
    testDriverLock(privconn);
1463 1464
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1465
    testDriverUnlock(privconn);
1466 1467

    if (privdom == NULL) {
1468
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1469
        goto cleanup;
1470
    }
1471

J
Jiri Denemark 已提交
1472
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_PAUSED) {
1473
        testError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
1474
                  domain->name);
1475
        goto cleanup;
1476
    }
1477

J
Jiri Denemark 已提交
1478 1479
    virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                         VIR_DOMAIN_RUNNING_UNPAUSED);
1480 1481 1482
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1483 1484 1485
    ret = 0;

cleanup:
1486 1487
    if (privdom)
        virDomainObjUnlock(privdom);
1488 1489 1490 1491 1492
    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1493
    return ret;
1494 1495
}

1496
static int testPauseDomain (virDomainPtr domain)
1497
{
1498 1499
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1500
    virDomainEventPtr event = NULL;
1501
    int ret = -1;
J
Jiri Denemark 已提交
1502
    int state;
1503

1504
    testDriverLock(privconn);
1505 1506
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1507
    testDriverUnlock(privconn);
1508 1509

    if (privdom == NULL) {
1510
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1511
        goto cleanup;
1512
    }
1513

J
Jiri Denemark 已提交
1514 1515
    state = virDomainObjGetState(privdom, NULL);
    if (state == VIR_DOMAIN_SHUTOFF || state == VIR_DOMAIN_PAUSED) {
1516
        testError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
1517
                  domain->name);
1518
        goto cleanup;
1519
    }
1520

J
Jiri Denemark 已提交
1521
    virDomainObjSetState(privdom, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
1522 1523 1524
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1525 1526 1527
    ret = 0;

cleanup:
1528 1529
    if (privdom)
        virDomainObjUnlock(privdom);
1530 1531 1532 1533 1534 1535

    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1536
    return ret;
1537 1538
}

1539
static int testShutdownDomain (virDomainPtr domain)
1540
{
1541 1542
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1543
    virDomainEventPtr event = NULL;
1544
    int ret = -1;
1545

1546
    testDriverLock(privconn);
1547 1548 1549 1550
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1551
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1552
        goto cleanup;
1553
    }
1554

J
Jiri Denemark 已提交
1555
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
1556
        testError(VIR_ERR_INTERNAL_ERROR,
1557
                  _("domain '%s' not running"), domain->name);
1558
        goto cleanup;
1559
    }
1560

J
Jiri Denemark 已提交
1561
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1562 1563 1564
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1565

1566 1567 1568 1569 1570
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
        privdom = NULL;
    }
1571

1572
    ret = 0;
1573
cleanup:
1574 1575
    if (privdom)
        virDomainObjUnlock(privdom);
1576 1577
    if (event)
        testDomainEventQueue(privconn, event);
1578
    testDriverUnlock(privconn);
1579
    return ret;
1580 1581 1582
}

/* Similar behaviour as shutdown */
1583 1584
static int testRebootDomain (virDomainPtr domain,
                             unsigned int action ATTRIBUTE_UNUSED)
1585
{
1586 1587
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1588
    virDomainEventPtr event = NULL;
1589
    int ret = -1;
1590

1591
    testDriverLock(privconn);
1592 1593 1594 1595
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1596
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1597
        goto cleanup;
1598
    }
1599

J
Jiri Denemark 已提交
1600 1601 1602
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

1603 1604
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
J
Jiri Denemark 已提交
1605 1606
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1607 1608
        break;

1609
    case VIR_DOMAIN_LIFECYCLE_RESTART:
J
Jiri Denemark 已提交
1610 1611
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1612 1613
        break;

1614
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
J
Jiri Denemark 已提交
1615 1616
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1617 1618
        break;

1619
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
J
Jiri Denemark 已提交
1620 1621
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1622
        break;
1623

1624
    default:
J
Jiri Denemark 已提交
1625 1626
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1627 1628
        break;
    }
1629

J
Jiri Denemark 已提交
1630 1631
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1632 1633 1634
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1635

1636 1637 1638 1639 1640
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1641 1642
    }

1643 1644
    ret = 0;
cleanup:
1645 1646
    if (privdom)
        virDomainObjUnlock(privdom);
1647 1648
    if (event)
        testDomainEventQueue(privconn, event);
1649
    testDriverUnlock(privconn);
1650
    return ret;
1651 1652
}

1653 1654
static int testGetDomainInfo (virDomainPtr domain,
                              virDomainInfoPtr info)
1655
{
1656
    testConnPtr privconn = domain->conn->privateData;
1657
    struct timeval tv;
1658
    virDomainObjPtr privdom;
1659
    int ret = -1;
1660

1661
    testDriverLock(privconn);
1662 1663
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1664
    testDriverUnlock(privconn);
1665 1666

    if (privdom == NULL) {
1667
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1668
        goto cleanup;
1669
    }
1670 1671

    if (gettimeofday(&tv, NULL) < 0) {
1672
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1673
                  "%s", _("getting time of day"));
1674
        goto cleanup;
1675 1676
    }

J
Jiri Denemark 已提交
1677
    info->state = virDomainObjGetState(privdom, NULL);
1678 1679
    info->memory = privdom->def->mem.cur_balloon;
    info->maxMem = privdom->def->mem.max_balloon;
1680 1681
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1682 1683 1684
    ret = 0;

cleanup:
1685 1686
    if (privdom)
        virDomainObjUnlock(privdom);
1687
    return ret;
1688 1689
}

1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
static int
testDomainGetState(virDomainPtr domain,
                   int *state,
                   int *reason,
                   unsigned int flags)
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    int ret = -1;

    virCheckFlags(0, -1);

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

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

J
Jiri Denemark 已提交
1712
    *state = virDomainObjGetState(privdom, reason);
1713 1714 1715 1716 1717 1718 1719 1720
    ret = 0;

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

1721 1722 1723 1724 1725
#define TEST_SAVE_MAGIC "TestGuestMagic"

static int testDomainSave(virDomainPtr domain,
                          const char *path)
{
1726
    testConnPtr privconn = domain->conn->privateData;
1727 1728 1729
    char *xml = NULL;
    int fd = -1;
    int len;
1730
    virDomainObjPtr privdom;
1731
    virDomainEventPtr event = NULL;
1732
    int ret = -1;
1733

1734
    testDriverLock(privconn);
1735 1736 1737 1738
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1739
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1740
        goto cleanup;
1741
    }
1742

1743
    xml = virDomainDefFormat(privdom->def,
C
Cole Robinson 已提交
1744 1745
                             VIR_DOMAIN_XML_SECURE);

1746
    if (xml == NULL) {
1747
        virReportSystemError(errno,
1748 1749
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
1750
        goto cleanup;
1751
    }
1752 1753

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1754
        virReportSystemError(errno,
1755 1756
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
1757
        goto cleanup;
1758
    }
1759
    len = strlen(xml);
1760
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1761
        virReportSystemError(errno,
1762 1763
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1764
        goto cleanup;
1765
    }
1766
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
1767
        virReportSystemError(errno,
1768 1769
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1770
        goto cleanup;
1771
    }
1772
    if (safewrite(fd, xml, len) < 0) {
1773
        virReportSystemError(errno,
1774 1775
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1776
        goto cleanup;
1777
    }
1778

1779
    if (VIR_CLOSE(fd) < 0) {
1780
        virReportSystemError(errno,
1781 1782
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1783
        goto cleanup;
1784
    }
1785 1786
    fd = -1;

J
Jiri Denemark 已提交
1787
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
1788 1789 1790
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1791

1792 1793 1794
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1795
        privdom = NULL;
1796
    }
1797

1798
    ret = 0;
1799 1800 1801 1802 1803 1804 1805
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) {
1806
        VIR_FORCE_CLOSE(fd);
1807 1808
        unlink(path);
    }
1809 1810
    if (privdom)
        virDomainObjUnlock(privdom);
1811 1812
    if (event)
        testDomainEventQueue(privconn, event);
1813
    testDriverUnlock(privconn);
1814
    return ret;
1815 1816
}

1817 1818
static int testDomainRestore(virConnectPtr conn,
                             const char *path)
1819
{
1820
    testConnPtr privconn = conn->privateData;
1821
    char *xml = NULL;
1822
    char magic[15];
1823 1824 1825
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
1826
    virDomainObjPtr dom = NULL;
1827
    virDomainEventPtr event = NULL;
1828
    int ret = -1;
1829 1830

    if ((fd = open(path, O_RDONLY)) < 0) {
1831
        virReportSystemError(errno,
1832 1833
                             _("cannot read domain image '%s'"),
                             path);
1834
        goto cleanup;
1835
    }
1836
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
1837
        virReportSystemError(errno,
1838 1839
                             _("incomplete save header in '%s'"),
                             path);
1840
        goto cleanup;
1841
    }
1842
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
1843
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1844
                  "%s", _("mismatched header magic"));
1845
        goto cleanup;
1846
    }
1847
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
1848
        virReportSystemError(errno,
1849 1850
                             _("failed to read metadata length in '%s'"),
                             path);
1851
        goto cleanup;
1852 1853
    }
    if (len < 1 || len > 8192) {
1854
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1855
                  "%s", _("length of metadata out of range"));
1856
        goto cleanup;
1857
    }
1858
    if (VIR_ALLOC_N(xml, len+1) < 0) {
1859
        virReportOOMError();
1860
        goto cleanup;
1861
    }
1862
    if (saferead(fd, xml, len) != len) {
1863
        virReportSystemError(errno,
1864
                             _("incomplete metdata in '%s'"), path);
1865
        goto cleanup;
1866 1867
    }
    xml[len] = '\0';
1868

1869
    testDriverLock(privconn);
1870
    def = virDomainDefParseString(privconn->caps, xml,
1871
                                  VIR_DOMAIN_XML_INACTIVE);
1872
    if (!def)
1873
        goto cleanup;
1874

1875 1876 1877
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1878
    if (testDomainGenerateIfnames(def) < 0)
1879
        goto cleanup;
1880
    if (!(dom = virDomainAssignDef(privconn->caps,
1881
                                   &privconn->domains, def, true)))
1882 1883
        goto cleanup;
    def = NULL;
1884

J
Jiri Denemark 已提交
1885
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_RESTORED) < 0)
1886 1887
        goto cleanup;

1888 1889 1890
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1891
    ret = 0;
1892 1893 1894 1895

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
1896
    VIR_FORCE_CLOSE(fd);
1897 1898
    if (dom)
        virDomainObjUnlock(dom);
1899 1900
    if (event)
        testDomainEventQueue(privconn, event);
1901
    testDriverUnlock(privconn);
1902
    return ret;
1903 1904
}

1905 1906
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
1907
                              unsigned int flags ATTRIBUTE_UNUSED)
1908
{
1909
    testConnPtr privconn = domain->conn->privateData;
1910
    int fd = -1;
1911
    virDomainObjPtr privdom;
1912
    virDomainEventPtr event = NULL;
1913
    int ret = -1;
1914

1915
    testDriverLock(privconn);
1916 1917 1918 1919
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1920
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1921
        goto cleanup;
1922
    }
1923 1924

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1925
        virReportSystemError(errno,
1926 1927
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
1928
        goto cleanup;
1929
    }
1930
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1931
        virReportSystemError(errno,
1932 1933
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
1934
        goto cleanup;
1935
    }
1936
    if (VIR_CLOSE(fd) < 0) {
1937
        virReportSystemError(errno,
1938 1939
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
1940
        goto cleanup;
1941
    }
1942

1943
    if (flags & VIR_DUMP_CRASH) {
J
Jiri Denemark 已提交
1944
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_CRASHED);
1945 1946 1947 1948 1949 1950 1951 1952
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1953
    }
1954

1955
    ret = 0;
1956
cleanup:
1957
    VIR_FORCE_CLOSE(fd);
1958 1959
    if (privdom)
        virDomainObjUnlock(privdom);
1960 1961
    if (event)
        testDomainEventQueue(privconn, event);
1962
    testDriverUnlock(privconn);
1963
    return ret;
1964 1965
}

1966
static char *testGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
1967 1968
    char *ret = strdup("linux");
    if (!ret)
1969
        virReportOOMError();
1970
    return ret;
1971 1972 1973
}

static unsigned long testGetMaxMemory(virDomainPtr domain) {
1974 1975
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1976
    unsigned long ret = 0;
1977

1978
    testDriverLock(privconn);
1979 1980
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1981
    testDriverUnlock(privconn);
1982 1983

    if (privdom == NULL) {
1984
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1985
        goto cleanup;
1986
    }
1987

1988
    ret = privdom->def->mem.max_balloon;
1989 1990

cleanup:
1991 1992
    if (privdom)
        virDomainObjUnlock(privdom);
1993
    return ret;
1994 1995 1996 1997 1998
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
1999 2000
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2001
    int ret = -1;
2002

2003
    testDriverLock(privconn);
2004 2005
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2006
    testDriverUnlock(privconn);
2007 2008

    if (privdom == NULL) {
2009
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2010
        goto cleanup;
2011
    }
2012 2013

    /* XXX validate not over host memory wrt to other domains */
2014
    privdom->def->mem.max_balloon = memory;
2015 2016 2017
    ret = 0;

cleanup:
2018 2019
    if (privdom)
        virDomainObjUnlock(privdom);
2020
    return ret;
2021 2022
}

2023 2024 2025
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
2026 2027
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2028
    int ret = -1;
2029

2030
    testDriverLock(privconn);
2031 2032
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2033
    testDriverUnlock(privconn);
2034 2035

    if (privdom == NULL) {
2036
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2037
        goto cleanup;
2038
    }
2039

2040
    if (memory > privdom->def->mem.max_balloon) {
2041
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2042
        goto cleanup;
2043
    }
2044

2045
    privdom->def->mem.cur_balloon = memory;
2046 2047 2048
    ret = 0;

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

2054 2055
static int
testDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
C
Cole Robinson 已提交
2056
{
2057 2058 2059 2060 2061
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;

2062 2063
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2064 2065 2066
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* Exactly one of LIVE or CONFIG must be set.  */
2067
    if (!(flags & VIR_DOMAIN_AFFECT_LIVE) == !(flags & VIR_DOMAIN_AFFECT_CONFIG)) {
2068 2069
        testError(VIR_ERR_INVALID_ARG,
                  _("invalid flag combination: (0x%x)"), flags);
2070 2071 2072
        return -1;
    }

2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
    testDriverLock(privconn);
    vm = virDomainFindByUUID(&privconn->domains, domain->uuid);
    testDriverUnlock(privconn);

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

2085
    if (flags & VIR_DOMAIN_AFFECT_LIVE) {
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
        if (!virDomainObjIsActive(vm)) {
            testError(VIR_ERR_OPERATION_INVALID, "%s",
                      _("domain not active"));
            goto cleanup;
        }
        def = vm->def;
    } else {
        def = vm->newDef ? vm->newDef : vm->def;
    }

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

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

2104 2105 2106
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
2107
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2108 2109 2110 2111 2112 2113 2114
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

static int
testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
                        unsigned int flags)
{
2115
    testConnPtr privconn = domain->conn->privateData;
2116
    virDomainObjPtr privdom = NULL;
2117
    virDomainDefPtr persistentDef;
C
Cole Robinson 已提交
2118 2119
    int ret = -1, maxvcpus;

2120 2121
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2122 2123 2124 2125
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
2126 2127 2128
    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)) {
2129 2130 2131 2132 2133 2134 2135
        testError(VIR_ERR_INVALID_ARG,
                  _("invalid flag combination: (0x%x)"), flags);
        return -1;
    }
    if (!nrCpus || (maxvcpus = testGetMaxVCPUs(domain->conn, NULL)) < nrCpus) {
        testError(VIR_ERR_INVALID_ARG,
                  _("argument out of range: %d"), nrCpus);
2136 2137
        return -1;
    }
2138

2139
    testDriverLock(privconn);
2140
    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
2141 2142 2143
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2144
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2145
        goto cleanup;
2146
    }
2147

2148
    if (!virDomainObjIsActive(privdom) && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
2149
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2150 2151 2152 2153
                  "%s", _("cannot hotplug vcpus for an inactive domain"));
        goto cleanup;
    }

2154 2155
    /* We allow more cpus in guest than host, but not more than the
     * domain's starting limit.  */
C
Cole Robinson 已提交
2156 2157
    if (!(flags & (VIR_DOMAIN_VCPU_MAXIMUM)) &&
        privdom->def->maxvcpus < maxvcpus)
2158
        maxvcpus = privdom->def->maxvcpus;
C
Cole Robinson 已提交
2159

C
Cole Robinson 已提交
2160
    if (nrCpus > maxvcpus) {
2161
        testError(VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
2162 2163
                  "requested cpu amount exceeds maximum (%d > %d)",
                  nrCpus, maxvcpus);
2164
        goto cleanup;
2165
    }
2166

2167 2168 2169 2170
    if (!(persistentDef = virDomainObjGetPersistentDef(privconn->caps,
                                                       privdom)))
        goto cleanup;

2171
    switch (flags) {
2172
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_CONFIG:
2173 2174 2175
        persistentDef->maxvcpus = nrCpus;
        if (nrCpus < persistentDef->vcpus)
            persistentDef->vcpus = nrCpus;
2176 2177
        ret = 0;
        break;
2178

2179
    case VIR_DOMAIN_AFFECT_CONFIG:
2180
        persistentDef->vcpus = nrCpus;
2181 2182 2183
        ret = 0;
        break;

2184
    case VIR_DOMAIN_AFFECT_LIVE:
2185 2186 2187
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
        break;

2188
    case VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG:
2189
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
2190 2191 2192
        if (ret == 0) {
            persistentDef->vcpus = nrCpus;
        }
2193 2194
        break;
    }
2195 2196

cleanup:
2197 2198
    if (privdom)
        virDomainObjUnlock(privdom);
2199
    return ret;
2200 2201
}

2202 2203 2204
static int
testSetVcpus(virDomainPtr domain, unsigned int nrCpus)
{
2205
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2206 2207
}

C
Cole Robinson 已提交
2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
static int testDomainGetVcpus(virDomainPtr domain,
                              virVcpuInfoPtr info,
                              int maxinfo,
                              unsigned char *cpumaps,
                              int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
    int i, v, maxcpu, hostcpus;
    int ret = -1;
    struct timeval tv;
    unsigned long long statbase;

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

    if (privdom == NULL) {
2227
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2228 2229 2230 2231
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2232
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2233 2234 2235 2236 2237 2238 2239
                  "%s",_("cannot list vcpus for an inactive domain"));
        goto cleanup;
    }

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
2240
        virReportSystemError(errno,
C
Cole Robinson 已提交
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
                             "%s", _("getting time of day"));
        goto cleanup;
    }

    statbase = (tv.tv_sec * 1000UL * 1000UL) + tv.tv_usec;


    hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
    maxcpu = maplen * 8;
    if (maxcpu > hostcpus)
        maxcpu = hostcpus;

    /* Clamp to actual number of vcpus */
    if (maxinfo > privdom->def->vcpus)
        maxinfo = privdom->def->vcpus;

    /* Populate virVcpuInfo structures */
    if (info != NULL) {
        memset(info, 0, sizeof(*info) * maxinfo);

        for (i = 0 ; i < maxinfo ; i++) {
            virVcpuInfo privinfo = privdomdata->vcpu_infos[i];

            info[i].number = privinfo.number;
            info[i].state = privinfo.state;
            info[i].cpu = privinfo.cpu;

            /* Fake an increasing cpu time value */
            info[i].cpuTime = statbase / 10;
        }
    }

    /* Populate cpumaps */
    if (cpumaps != NULL) {
        int privmaplen = VIR_CPU_MAPLEN(hostcpus);
        memset(cpumaps, 0, maplen * maxinfo);

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

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

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

C
Cole Robinson 已提交
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
static int testDomainPinVcpu(virDomainPtr domain,
                             unsigned int vcpu,
                             unsigned char *cpumap,
                             int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
    unsigned char *privcpumap;
    int i, maxcpu, hostcpus, privmaplen;
    int ret = -1;

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

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

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

    if (vcpu > privdom->def->vcpus) {
2324
        testError(VIR_ERR_INVALID_ARG, "%s",
C
Cole Robinson 已提交
2325 2326 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
                  _("requested vcpu is higher than allocated vcpus"));
        goto cleanup;
    }

    privdomdata = privdom->privateData;
    hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
    privmaplen = VIR_CPU_MAPLEN(hostcpus);

    maxcpu = maplen * 8;
    if (maxcpu > hostcpus)
        maxcpu = hostcpus;

    privcpumap = VIR_GET_CPUMAP(privdomdata->cpumaps, privmaplen, vcpu);
    memset(privcpumap, 0, privmaplen);

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

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

2353
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2354
{
2355
    testConnPtr privconn = domain->conn->privateData;
2356
    virDomainDefPtr def;
2357
    virDomainObjPtr privdom;
2358 2359
    char *ret = NULL;

2360 2361 2362 2363 2364 2365
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2366
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2367
        goto cleanup;
2368
    }
2369

2370 2371
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2372

2373
    ret = virDomainDefFormat(def,
2374 2375 2376
                             flags);

cleanup:
2377 2378
    if (privdom)
        virDomainObjUnlock(privdom);
2379
    return ret;
2380
}
2381

2382
static int testNumOfDefinedDomains(virConnectPtr conn) {
2383
    testConnPtr privconn = conn->privateData;
2384
    int count;
2385

2386
    testDriverLock(privconn);
2387
    count = virDomainObjListNumOfDomains(&privconn->domains, 0);
2388
    testDriverUnlock(privconn);
2389

2390
    return count;
2391 2392
}

2393 2394 2395
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
2396

2397
    testConnPtr privconn = conn->privateData;
2398
    int n;
2399

2400
    testDriverLock(privconn);
2401
    memset(names, 0, sizeof(*names)*maxnames);
2402
    n = virDomainObjListGetInactiveNames(&privconn->domains, names, maxnames);
2403
    testDriverUnlock(privconn);
2404

2405
    return n;
2406 2407
}

2408
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
2409
                                        const char *xml) {
2410
    testConnPtr privconn = conn->privateData;
2411
    virDomainPtr ret = NULL;
2412
    virDomainDefPtr def;
2413
    virDomainObjPtr dom = NULL;
2414
    virDomainEventPtr event = NULL;
2415
    int dupVM;
2416

2417
    testDriverLock(privconn);
2418
    if ((def = virDomainDefParseString(privconn->caps, xml,
2419
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
2420
        goto cleanup;
2421

2422 2423 2424
    if ((dupVM = virDomainObjIsDuplicate(&privconn->domains, def, 0)) < 0)
        goto cleanup;

2425
    if (testDomainGenerateIfnames(def) < 0)
2426
        goto cleanup;
2427
    if (!(dom = virDomainAssignDef(privconn->caps,
2428
                                   &privconn->domains, def, false)))
2429
        goto cleanup;
2430
    def = NULL;
2431
    dom->persistent = 1;
2432

2433 2434
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
2435 2436 2437
                                     !dupVM ?
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2438

2439
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2440
    if (ret)
2441
        ret->id = dom->def->id;
2442 2443 2444

cleanup:
    virDomainDefFree(def);
2445 2446
    if (dom)
        virDomainObjUnlock(dom);
2447 2448
    if (event)
        testDomainEventQueue(privconn, event);
2449
    testDriverUnlock(privconn);
2450
    return ret;
2451 2452
}

2453 2454 2455
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
2456
    testConnPtr privconn = conn->privateData;
2457
    int i, j;
2458
    int ret = -1;
2459

2460
    testDriverLock(privconn);
2461
    if (startCell > privconn->numCells) {
2462
        testError(VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
2463
                  "%s", _("Range exceeds available cells"));
2464
        goto cleanup;
2465 2466 2467 2468 2469 2470 2471
    }

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

2474
cleanup:
2475
    testDriverUnlock(privconn);
2476
    return ret;
2477 2478 2479
}


2480
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags) {
2481 2482
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2483
    virDomainEventPtr event = NULL;
2484
    int ret = -1;
2485

2486 2487
    virCheckFlags(0, -1);

2488
    testDriverLock(privconn);
2489 2490 2491 2492
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2493
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2494
        goto cleanup;
2495
    }
2496

J
Jiri Denemark 已提交
2497
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
2498
        testError(VIR_ERR_INTERNAL_ERROR,
2499
                  _("Domain '%s' is already running"), domain->name);
2500
        goto cleanup;
2501 2502
    }

J
Jiri Denemark 已提交
2503 2504
    if (testDomainStartState(domain->conn, privdom,
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
2505 2506 2507
        goto cleanup;
    domain->id = privdom->def->id;

2508 2509 2510
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2511
    ret = 0;
2512

2513
cleanup:
2514 2515
    if (privdom)
        virDomainObjUnlock(privdom);
2516 2517
    if (event)
        testDomainEventQueue(privconn, event);
2518
    testDriverUnlock(privconn);
2519
    return ret;
2520 2521
}

2522 2523 2524 2525
static int testDomainCreate(virDomainPtr domain) {
    return testDomainCreateWithFlags(domain, 0);
}

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

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

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

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

2547 2548 2549
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2550 2551
    virDomainRemoveInactive(&privconn->domains,
                            privdom);
2552
    privdom = NULL;
2553
    ret = 0;
2554

2555
cleanup:
2556 2557
    if (privdom)
        virDomainObjUnlock(privdom);
2558 2559
    if (event)
        testDomainEventQueue(privconn, event);
2560
    testDriverUnlock(privconn);
2561
    return ret;
2562 2563
}

2564 2565 2566
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2567 2568
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2569
    int ret = -1;
2570

2571
    testDriverLock(privconn);
2572 2573
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2574
    testDriverUnlock(privconn);
2575 2576

    if (privdom == NULL) {
2577
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2578
        goto cleanup;
2579 2580
    }

2581
    *autostart = privdom->autostart;
2582 2583 2584
    ret = 0;

cleanup:
2585 2586
    if (privdom)
        virDomainObjUnlock(privdom);
2587
    return ret;
2588 2589 2590 2591 2592 2593
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2594 2595
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2596
    int ret = -1;
2597

2598
    testDriverLock(privconn);
2599 2600
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2601
    testDriverUnlock(privconn);
2602 2603

    if (privdom == NULL) {
2604
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2605
        goto cleanup;
2606 2607
    }

2608
    privdom->autostart = autostart ? 1 : 0;
2609 2610 2611
    ret = 0;

cleanup:
2612 2613
    if (privdom)
        virDomainObjUnlock(privdom);
2614
    return ret;
2615
}
2616

2617
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
2618 2619
                                        int *nparams)
{
2620 2621
    char *type = NULL;

2622 2623 2624
    if (nparams)
        *nparams = 1;

2625
    type = strdup("fair");
2626
    if (!type)
2627
        virReportOOMError();
2628

2629 2630 2631
    return type;
}

2632 2633 2634 2635 2636
static int
testDomainGetSchedulerParamsFlags(virDomainPtr domain,
                                  virTypedParameterPtr params,
                                  int *nparams,
                                  unsigned int flags)
2637
{
2638 2639
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2640
    int ret = -1;
2641

2642 2643
    virCheckFlags(0, -1);

2644
    testDriverLock(privconn);
2645 2646
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2647
    testDriverUnlock(privconn);
2648 2649

    if (privdom == NULL) {
2650
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2651
        goto cleanup;
2652 2653
    }

2654 2655
    if (*nparams < 1) {
        testError(VIR_ERR_INVALID_ARG, "%s", _("Invalid parameter count"));
2656
        goto cleanup;
2657
    }
2658
    strcpy(params[0].field, "weight");
2659
    params[0].type = VIR_TYPED_PARAM_UINT;
2660 2661 2662
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
    params[0].value.ui = 50;
2663 2664

    *nparams = 1;
2665 2666 2667
    ret = 0;

cleanup:
2668 2669
    if (privdom)
        virDomainObjUnlock(privdom);
2670
    return ret;
2671
}
2672

2673 2674 2675 2676 2677 2678 2679
static int
testDomainGetSchedulerParams(virDomainPtr domain,
                             virTypedParameterPtr params,
                             int *nparams)
{
    return testDomainGetSchedulerParamsFlags(domain, params, nparams, 0);
}
2680

2681 2682 2683 2684 2685
static int
testDomainSetSchedulerParamsFlags(virDomainPtr domain,
                                  virTypedParameterPtr params,
                                  int nparams,
                                  unsigned int flags)
2686
{
2687 2688
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2689
    int ret = -1, i;
2690

2691 2692
    virCheckFlags(0, -1);

2693
    testDriverLock(privconn);
2694 2695
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2696
    testDriverUnlock(privconn);
2697 2698

    if (privdom == NULL) {
2699
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2700
        goto cleanup;
2701 2702
    }

2703 2704 2705 2706 2707
    for (i = 0; i < nparams; i++) {
        if (STRNEQ(params[i].field, "weight")) {
            testError(VIR_ERR_INVALID_ARG, "field");
            goto cleanup;
        }
2708
        if (params[i].type != VIR_TYPED_PARAM_UINT) {
2709 2710 2711 2712 2713
            testError(VIR_ERR_INVALID_ARG, "type");
            goto cleanup;
        }
        /* XXX */
        /*privdom->weight = params[i].value.ui;*/
2714
    }
2715

2716 2717 2718
    ret = 0;

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

2724 2725 2726 2727 2728 2729 2730 2731
static int
testDomainSetSchedulerParams(virDomainPtr domain,
                             virTypedParameterPtr params,
                             int nparams)
{
    return testDomainSetSchedulerParamsFlags(domain, params, nparams, 0);
}

2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747
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;
    int i, found = 0, ret = -1;

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

    if (privdom == NULL) {
2748
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759
        goto error;
    }

    for (i = 0 ; i < privdom->def->ndisks ; i++) {
        if (STREQ(path, privdom->def->disks[i]->dst)) {
            found = 1;
            break;
        }
    }

    if (!found) {
2760
        testError(VIR_ERR_INVALID_ARG,
2761 2762 2763 2764 2765
                  _("invalid path: %s"), path);
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2766
        virReportSystemError(errno,
2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
                             "%s", _("getting time of day"));
        goto error;
    }

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

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

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

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

    if (privdom == NULL) {
2802
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814
        goto error;
    }

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

    if (!found) {
2815
        testError(VIR_ERR_INVALID_ARG,
2816 2817 2818 2819 2820
                  _("invalid path, '%s' is not a known interface"), path);
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2821
        virReportSystemError(errno,
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843
                             "%s", _("getting time of day"));
        goto error;
    }

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

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

2844
static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
2845
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
2846
                                        unsigned int flags ATTRIBUTE_UNUSED) {
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

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


static virNetworkPtr testLookupNetworkByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
{
2863 2864
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
2865
    virNetworkPtr ret = NULL;
2866

2867 2868 2869 2870 2871
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
2872
        testError(VIR_ERR_NO_NETWORK, NULL);
2873
        goto cleanup;
2874 2875
    }

2876 2877 2878
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2879 2880
    if (net)
        virNetworkObjUnlock(net);
2881
    return ret;
2882
}
2883

2884
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
2885
                                             const char *name)
2886
{
2887
    testConnPtr privconn = conn->privateData;
2888 2889
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
2890

2891 2892 2893 2894 2895
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
2896
        testError(VIR_ERR_NO_NETWORK, NULL);
2897
        goto cleanup;
2898 2899
    }

2900 2901 2902
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2903 2904
    if (net)
        virNetworkObjUnlock(net);
2905
    return ret;
2906 2907 2908 2909
}


static int testNumNetworks(virConnectPtr conn) {
2910
    testConnPtr privconn = conn->privateData;
2911
    int numActive = 0, i;
2912

2913 2914 2915
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2916
        if (virNetworkObjIsActive(privconn->networks.objs[i]))
2917
            numActive++;
2918 2919 2920
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2921

2922
    return numActive;
2923 2924 2925
}

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

2929
    testDriverLock(privconn);
2930
    memset(names, 0, sizeof(*names)*nnames);
2931 2932
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2933
        if (virNetworkObjIsActive(privconn->networks.objs[i]) &&
2934 2935
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2936
            goto no_memory;
2937 2938 2939 2940
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2941

2942 2943 2944
    return n;

no_memory:
2945
    virReportOOMError();
2946 2947
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2948
    testDriverUnlock(privconn);
2949
    return -1;
2950 2951 2952
}

static int testNumDefinedNetworks(virConnectPtr conn) {
2953
    testConnPtr privconn = conn->privateData;
2954
    int numInactive = 0, i;
2955

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

2965
    return numInactive;
2966 2967 2968
}

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

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

2985 2986 2987
    return n;

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

2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005

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) {
3006
        testError(VIR_ERR_NO_NETWORK, NULL);
3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026
        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) {
3027
        testError(VIR_ERR_NO_NETWORK, NULL);
3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
        goto cleanup;
    }
    ret = obj->persistent;

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


3039
static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
3040
    testConnPtr privconn = conn->privateData;
3041
    virNetworkDefPtr def;
3042
    virNetworkObjPtr net = NULL;
3043
    virNetworkPtr ret = NULL;
3044

3045
    testDriverLock(privconn);
3046
    if ((def = virNetworkDefParseString(xml)) == NULL)
3047
        goto cleanup;
3048

3049
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
3050 3051
        goto cleanup;
    def = NULL;
3052
    net->active = 1;
3053

3054
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3055

3056 3057
cleanup:
    virNetworkDefFree(def);
3058 3059 3060
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3061
    return ret;
3062 3063 3064
}

static virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml) {
3065
    testConnPtr privconn = conn->privateData;
3066
    virNetworkDefPtr def;
3067
    virNetworkObjPtr net = NULL;
3068
    virNetworkPtr ret = NULL;
3069

3070
    testDriverLock(privconn);
3071
    if ((def = virNetworkDefParseString(xml)) == NULL)
3072
        goto cleanup;
3073

3074
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
3075 3076
        goto cleanup;
    def = NULL;
3077
    net->persistent = 1;
3078

3079
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3080 3081 3082

cleanup:
    virNetworkDefFree(def);
3083 3084 3085
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3086
    return ret;
3087 3088 3089
}

static int testNetworkUndefine(virNetworkPtr network) {
3090 3091
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3092
    int ret = -1;
3093

3094
    testDriverLock(privconn);
3095 3096 3097 3098
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3099
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3100
        goto cleanup;
3101
    }
3102

D
Daniel P. Berrange 已提交
3103
    if (virNetworkObjIsActive(privnet)) {
3104
        testError(VIR_ERR_OPERATION_INVALID,
3105
                  _("Network '%s' is still running"), network->name);
3106
        goto cleanup;
3107 3108
    }

3109 3110
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
3111
    privnet = NULL;
3112
    ret = 0;
3113

3114
cleanup:
3115 3116 3117
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3118
    return ret;
3119 3120 3121
}

static int testNetworkStart(virNetworkPtr network) {
3122 3123
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3124
    int ret = -1;
3125

3126
    testDriverLock(privconn);
3127 3128
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3129
    testDriverUnlock(privconn);
3130 3131

    if (privnet == NULL) {
3132
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3133
        goto cleanup;
3134
    }
3135

D
Daniel P. Berrange 已提交
3136
    if (virNetworkObjIsActive(privnet)) {
3137
        testError(VIR_ERR_OPERATION_INVALID,
3138
                  _("Network '%s' is already running"), network->name);
3139
        goto cleanup;
3140 3141
    }

3142
    privnet->active = 1;
3143
    ret = 0;
3144

3145
cleanup:
3146 3147
    if (privnet)
        virNetworkObjUnlock(privnet);
3148
    return ret;
3149 3150 3151
}

static int testNetworkDestroy(virNetworkPtr network) {
3152 3153
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3154
    int ret = -1;
3155

3156
    testDriverLock(privconn);
3157 3158 3159 3160
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3161
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3162
        goto cleanup;
3163
    }
3164

3165 3166 3167 3168
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
3169
        privnet = NULL;
3170
    }
3171 3172 3173
    ret = 0;

cleanup:
3174 3175 3176
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3177
    return ret;
3178 3179
}

3180 3181 3182
static char *testNetworkGetXMLDesc(virNetworkPtr network,
                                   unsigned int flags ATTRIBUTE_UNUSED)
{
3183 3184
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3185
    char *ret = NULL;
3186

3187
    testDriverLock(privconn);
3188 3189
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3190
    testDriverUnlock(privconn);
3191 3192

    if (privnet == NULL) {
3193
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3194
        goto cleanup;
3195
    }
3196

3197
    ret = virNetworkDefFormat(privnet->def);
3198 3199

cleanup:
3200 3201
    if (privnet)
        virNetworkObjUnlock(privnet);
3202
    return ret;
3203 3204 3205
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3206
    testConnPtr privconn = network->conn->privateData;
3207
    char *bridge = NULL;
3208 3209
    virNetworkObjPtr privnet;

3210
    testDriverLock(privconn);
3211 3212
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3213
    testDriverUnlock(privconn);
3214 3215

    if (privnet == NULL) {
3216
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3217
        goto cleanup;
3218 3219
    }

3220
    if (!(privnet->def->bridge)) {
3221
        testError(VIR_ERR_INTERNAL_ERROR,
3222 3223 3224 3225 3226 3227
                  _("network '%s' does not have a bridge name."),
                  privnet->def->name);
        goto cleanup;
    }

    if (!(bridge = strdup(privnet->def->bridge))) {
3228
        virReportOOMError();
3229
        goto cleanup;
3230
    }
3231 3232

cleanup:
3233 3234
    if (privnet)
        virNetworkObjUnlock(privnet);
3235 3236 3237 3238 3239
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
3240 3241
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3242
    int ret = -1;
3243

3244
    testDriverLock(privconn);
3245 3246
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3247
    testDriverUnlock(privconn);
3248 3249

    if (privnet == NULL) {
3250
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3251
        goto cleanup;
3252 3253
    }

3254
    *autostart = privnet->autostart;
3255 3256 3257
    ret = 0;

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

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
3265 3266
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3267
    int ret = -1;
3268

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

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

3279
    privnet->autostart = autostart ? 1 : 0;
3280 3281 3282
    ret = 0;

cleanup:
3283 3284
    if (privnet)
        virNetworkObjUnlock(privnet);
3285
    return ret;
3286
}
3287

C
Cole Robinson 已提交
3288

L
Laine Stump 已提交
3289 3290 3291 3292 3293 3294
/*
 * Physical host interface routines
 */

static virDrvOpenStatus testOpenInterface(virConnectPtr conn,
                                          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
3295
                                          unsigned int flags ATTRIBUTE_UNUSED)
L
Laine Stump 已提交
3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318
{
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

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


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

    testDriverLock(privconn);
    for (i = 0 ; (i < privconn->ifaces.count); i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3319
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336
            count++;
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

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

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
    for (i = 0 ; (i < privconn->ifaces.count) && (n < nnames); i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3337
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349
            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:
3350
    virReportOOMError();
L
Laine Stump 已提交
3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

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

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

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

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
    for (i = 0 ; (i < privconn->ifaces.count) && (n < nnames); i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3383
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395
            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:
3396
    virReportOOMError();
L
Laine Stump 已提交
3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

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

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

    if (iface == NULL) {
3415
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439
        goto cleanup;
    }

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

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

static virInterfacePtr testLookupInterfaceByMACString(virConnectPtr conn,
                                                      const char *mac)
{
    testConnPtr privconn = conn->privateData;
    virInterfaceObjPtr iface;
    int ifacect;
    virInterfacePtr ret = NULL;

    testDriverLock(privconn);
    ifacect = virInterfaceFindByMACString(&privconn->ifaces, mac, &iface, 1);
    testDriverUnlock(privconn);

    if (ifacect == 0) {
3440
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3441 3442 3443 3444
        goto cleanup;
    }

    if (ifacect > 1) {
3445
        testError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456
        goto cleanup;
    }

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

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

3457 3458 3459 3460 3461 3462 3463 3464 3465 3466
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) {
3467
        testError(VIR_ERR_NO_INTERFACE, NULL);
3468 3469 3470 3471 3472 3473 3474 3475 3476 3477
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

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

3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555
static int testInterfaceChangeBegin(virConnectPtr conn,
                                    unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    testDriverLock(privconn);
    if (privconn->transaction_running) {
        testError(VIR_ERR_OPERATION_INVALID,
                  _("there is another transaction running."));
        goto cleanup;
    }

    privconn->transaction_running = true;

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

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

static int testInterfaceChangeCommit(virConnectPtr conn,
                                     unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    testDriverLock(privconn);

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

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

    ret = 0;

cleanup:
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
                                       unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    testDriverLock(privconn);

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

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

    privconn->transaction_running = false;

    ret = 0;

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

L
Laine Stump 已提交
3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
                                     unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

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

    if (privinterface == NULL) {
3570
        testError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
3571 3572 3573
        goto cleanup;
    }

3574
    ret = virInterfaceDefFormat(privinterface->def);
L
Laine Stump 已提交
3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591

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


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
                                              unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr privconn = conn->privateData;
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

    testDriverLock(privconn);
3592
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3593 3594
        goto cleanup;

3595
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619
        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) {
3620
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644
        goto cleanup;
    }

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

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
                               unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

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

    if (privinterface == NULL) {
3645
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3646 3647 3648 3649
        goto cleanup;
    }

    if (privinterface->active != 0) {
3650
        testError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

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

static int testInterfaceDestroy(virInterfacePtr iface,
                                unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

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

    if (privinterface == NULL) {
3676
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3677 3678 3679 3680
        goto cleanup;
    }

    if (privinterface->active == 0) {
3681
        testError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

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



C
Cole Robinson 已提交
3697 3698 3699 3700
/*
 * Storage Driver routines
 */

3701

3702
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3703 3704 3705 3706 3707 3708 3709

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

    pool->configFile = strdup("\0");
    if (!pool->configFile) {
3710
        virReportOOMError();
C
Cole Robinson 已提交
3711 3712 3713 3714 3715 3716
        return -1;
    }

    return 0;
}

3717 3718
static virDrvOpenStatus testStorageOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
3719
                                        unsigned int flags ATTRIBUTE_UNUSED) {
3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731
    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;
}

3732

C
Cole Robinson 已提交
3733 3734 3735
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
3736
    testConnPtr privconn = conn->privateData;
3737 3738
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3739

3740
    testDriverLock(privconn);
3741
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
3742
    testDriverUnlock(privconn);
3743 3744

    if (pool == NULL) {
3745
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3746
        goto cleanup;
C
Cole Robinson 已提交
3747 3748
    }

3749 3750 3751
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3752 3753
    if (pool)
        virStoragePoolObjUnlock(pool);
3754
    return ret;
C
Cole Robinson 已提交
3755 3756 3757 3758 3759
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
3760
    testConnPtr privconn = conn->privateData;
3761 3762
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3763

3764
    testDriverLock(privconn);
3765
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
3766
    testDriverUnlock(privconn);
3767 3768

    if (pool == NULL) {
3769
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3770
        goto cleanup;
C
Cole Robinson 已提交
3771 3772
    }

3773 3774 3775
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3776 3777
    if (pool)
        virStoragePoolObjUnlock(pool);
3778
    return ret;
C
Cole Robinson 已提交
3779 3780 3781 3782 3783 3784 3785 3786 3787
}

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

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

3791
    testDriverLock(privconn);
C
Cole Robinson 已提交
3792 3793 3794
    for (i = 0 ; i < privconn->pools.count ; i++)
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
3795
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3796 3797 3798 3799 3800 3801 3802 3803

    return numActive;
}

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

3807
    testDriverLock(privconn);
C
Cole Robinson 已提交
3808
    memset(names, 0, sizeof(*names)*nnames);
3809 3810
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3811
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3812 3813
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3814
            goto no_memory;
3815 3816 3817 3818
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3819 3820 3821 3822

    return n;

no_memory:
3823
    virReportOOMError();
C
Cole Robinson 已提交
3824 3825
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3826
    testDriverUnlock(privconn);
3827
    return -1;
C
Cole Robinson 已提交
3828 3829 3830 3831
}

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

3835 3836 3837
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->pools.count ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3838 3839
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
3840 3841 3842
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3843 3844 3845 3846 3847 3848 3849 3850

    return numInactive;
}

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

3854
    testDriverLock(privconn);
C
Cole Robinson 已提交
3855
    memset(names, 0, sizeof(*names)*nnames);
3856 3857
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3858
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3859 3860
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3861
            goto no_memory;
3862 3863 3864 3865
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3866 3867 3868 3869

    return n;

no_memory:
3870
    virReportOOMError();
C
Cole Robinson 已提交
3871 3872
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3873
    testDriverUnlock(privconn);
3874
    return -1;
C
Cole Robinson 已提交
3875 3876 3877
}


3878 3879 3880 3881 3882 3883 3884 3885 3886 3887
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) {
3888
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908
        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) {
3909
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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



C
Cole Robinson 已提交
3922
static int
3923
testStoragePoolStart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3924
                     unsigned int flags ATTRIBUTE_UNUSED) {
3925 3926
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3927
    int ret = -1;
3928

3929
    testDriverLock(privconn);
3930 3931
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3932
    testDriverUnlock(privconn);
3933 3934

    if (privpool == NULL) {
3935
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3936
        goto cleanup;
3937 3938
    }

3939
    if (virStoragePoolObjIsActive(privpool)) {
3940
        testError(VIR_ERR_OPERATION_INVALID,
3941 3942 3943
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
3944 3945

    privpool->active = 1;
3946
    ret = 0;
C
Cole Robinson 已提交
3947

3948
cleanup:
3949 3950
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3951
    return ret;
C
Cole Robinson 已提交
3952 3953 3954
}

static char *
3955
testStorageFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
3956 3957
                           const char *type,
                           const char *srcSpec,
C
Cole Robinson 已提交
3958 3959
                           unsigned int flags ATTRIBUTE_UNUSED)
{
3960 3961 3962 3963 3964 3965
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
3966
        testError(VIR_ERR_INTERNAL_ERROR,
3967 3968 3969 3970 3971
                  _("unknown storage pool type %s"), type);
        goto cleanup;
    }

    if (srcSpec) {
3972
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
3973 3974 3975 3976 3977 3978 3979 3980 3981
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
        ret = strdup(defaultPoolSourcesLogicalXML);
        if (!ret)
3982
            virReportOOMError();
3983 3984 3985 3986
        break;

    case VIR_STORAGE_POOL_NETFS:
        if (!source || !source->host.name) {
3987
            testError(VIR_ERR_INVALID_ARG,
3988 3989 3990 3991 3992 3993
                      "%s", "hostname must be specified for netfs sources");
            goto cleanup;
        }

        if (virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                        source->host.name) < 0)
3994
            virReportOOMError();
3995 3996 3997
        break;

    default:
3998
        testError(VIR_ERR_NO_SUPPORT,
3999 4000 4001 4002 4003
                  _("pool type '%s' does not support source discovery"), type);
    }

cleanup:
    virStoragePoolSourceFree(source);
4004
    VIR_FREE(source);
4005
    return ret;
C
Cole Robinson 已提交
4006 4007 4008 4009 4010 4011 4012
}


static virStoragePoolPtr
testStoragePoolCreate(virConnectPtr conn,
                      const char *xml,
                      unsigned int flags ATTRIBUTE_UNUSED) {
4013
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4014
    virStoragePoolDefPtr def;
4015
    virStoragePoolObjPtr pool = NULL;
4016
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4017

4018
    testDriverLock(privconn);
4019
    if (!(def = virStoragePoolDefParseString(xml)))
4020
        goto cleanup;
C
Cole Robinson 已提交
4021

4022 4023 4024 4025
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4026
        testError(VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
4027
                  "%s", _("storage pool already exists"));
4028
        goto cleanup;
C
Cole Robinson 已提交
4029 4030
    }

4031
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4032
        goto cleanup;
4033
    def = NULL;
C
Cole Robinson 已提交
4034

4035
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4036
        virStoragePoolObjRemove(&privconn->pools, pool);
4037 4038
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4039 4040 4041
    }
    pool->active = 1;

4042 4043 4044 4045
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
4046 4047 4048
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4049
    return ret;
C
Cole Robinson 已提交
4050 4051 4052 4053 4054 4055
}

static virStoragePoolPtr
testStoragePoolDefine(virConnectPtr conn,
                      const char *xml,
                      unsigned int flags ATTRIBUTE_UNUSED) {
4056
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4057
    virStoragePoolDefPtr def;
4058
    virStoragePoolObjPtr pool = NULL;
4059
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4060

4061
    testDriverLock(privconn);
4062
    if (!(def = virStoragePoolDefParseString(xml)))
4063
        goto cleanup;
C
Cole Robinson 已提交
4064 4065 4066 4067 4068

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

4069
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4070 4071
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4072

4073
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4074
        virStoragePoolObjRemove(&privconn->pools, pool);
4075 4076
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4077 4078
    }

4079 4080 4081 4082
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
4083 4084 4085
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4086
    return ret;
C
Cole Robinson 已提交
4087 4088 4089
}

static int
4090 4091 4092
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4093
    int ret = -1;
4094

4095
    testDriverLock(privconn);
4096 4097 4098 4099
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4100
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4101
        goto cleanup;
4102 4103
    }

4104
    if (virStoragePoolObjIsActive(privpool)) {
4105
        testError(VIR_ERR_OPERATION_INVALID,
4106 4107 4108
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
4109 4110

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

4113
cleanup:
4114 4115 4116
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4117
    return ret;
C
Cole Robinson 已提交
4118 4119 4120
}

static int
4121
testStoragePoolBuild(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4122
                     unsigned int flags ATTRIBUTE_UNUSED) {
4123 4124
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4125
    int ret = -1;
4126

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

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

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

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


static int
4152 4153 4154
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4155
    int ret = -1;
4156

4157
    testDriverLock(privconn);
4158 4159 4160 4161
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4162
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4163
        goto cleanup;
4164 4165 4166
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4167
        testError(VIR_ERR_OPERATION_INVALID,
4168
                  _("storage pool '%s' is not active"), pool->name);
4169
        goto cleanup;
4170
    }
C
Cole Robinson 已提交
4171 4172 4173

    privpool->active = 0;

4174
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4175
        virStoragePoolObjRemove(&privconn->pools, privpool);
4176 4177
        privpool = NULL;
    }
4178
    ret = 0;
C
Cole Robinson 已提交
4179

4180
cleanup:
4181 4182 4183
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4184
    return ret;
C
Cole Robinson 已提交
4185 4186 4187 4188
}


static int
4189
testStoragePoolDelete(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4190
                      unsigned int flags ATTRIBUTE_UNUSED) {
4191 4192
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4193
    int ret = -1;
4194

4195
    testDriverLock(privconn);
4196 4197
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4198
    testDriverUnlock(privconn);
4199 4200

    if (privpool == NULL) {
4201
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4202 4203 4204 4205
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
4206
        testError(VIR_ERR_OPERATION_INVALID,
4207 4208
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
4209 4210
    }

4211
    ret = 0;
C
Cole Robinson 已提交
4212

4213
cleanup:
4214 4215
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4216
    return ret;
C
Cole Robinson 已提交
4217 4218 4219 4220
}


static int
4221
testStoragePoolRefresh(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4222
                       unsigned int flags ATTRIBUTE_UNUSED) {
4223 4224
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4225
    int ret = -1;
4226

4227
    testDriverLock(privconn);
4228 4229
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4230
    testDriverUnlock(privconn);
4231 4232

    if (privpool == NULL) {
4233
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4234
        goto cleanup;
4235 4236 4237
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4238
        testError(VIR_ERR_OPERATION_INVALID,
4239
                  _("storage pool '%s' is not active"), pool->name);
4240
        goto cleanup;
4241
    }
4242
    ret = 0;
C
Cole Robinson 已提交
4243

4244
cleanup:
4245 4246
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4247
    return ret;
C
Cole Robinson 已提交
4248 4249 4250 4251
}


static int
4252
testStoragePoolGetInfo(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4253
                       virStoragePoolInfoPtr info) {
4254 4255
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4256
    int ret = -1;
4257

4258
    testDriverLock(privconn);
4259 4260
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4261
    testDriverUnlock(privconn);
4262 4263

    if (privpool == NULL) {
4264
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4265
        goto cleanup;
4266
    }
C
Cole Robinson 已提交
4267 4268 4269 4270 4271 4272 4273 4274 4275

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

4278
cleanup:
4279 4280
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4281
    return ret;
C
Cole Robinson 已提交
4282 4283 4284
}

static char *
4285 4286
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
                          unsigned int flags ATTRIBUTE_UNUSED) {
4287 4288
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4289
    char *ret = NULL;
4290

4291
    testDriverLock(privconn);
4292 4293
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4294
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4295

4296
    if (privpool == NULL) {
4297
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4298
        goto cleanup;
4299 4300
    }

4301
    ret = virStoragePoolDefFormat(privpool->def);
4302 4303

cleanup:
4304 4305
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4306
    return ret;
C
Cole Robinson 已提交
4307 4308 4309
}

static int
4310
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4311
                            int *autostart) {
4312 4313
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4314
    int ret = -1;
4315

4316
    testDriverLock(privconn);
4317 4318
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4319
    testDriverUnlock(privconn);
4320 4321

    if (privpool == NULL) {
4322
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4323
        goto cleanup;
4324
    }
C
Cole Robinson 已提交
4325 4326 4327 4328 4329 4330

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

4333
cleanup:
4334 4335
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4336
    return ret;
C
Cole Robinson 已提交
4337 4338 4339
}

static int
4340
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4341
                            int autostart) {
4342 4343
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4344
    int ret = -1;
4345

4346
    testDriverLock(privconn);
4347 4348
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4349
    testDriverUnlock(privconn);
4350 4351

    if (privpool == NULL) {
4352
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4353
        goto cleanup;
4354
    }
C
Cole Robinson 已提交
4355 4356

    if (!privpool->configFile) {
4357
        testError(VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
4358
                  "%s", _("pool has no config file"));
4359
        goto cleanup;
C
Cole Robinson 已提交
4360 4361 4362 4363
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4364 4365 4366
    ret = 0;

cleanup:
4367 4368
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4369
    return ret;
C
Cole Robinson 已提交
4370 4371 4372 4373
}


static int
4374 4375 4376
testStoragePoolNumVolumes(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4377
    int ret = -1;
4378

4379
    testDriverLock(privconn);
4380 4381
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4382
    testDriverUnlock(privconn);
4383 4384

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

    if (!virStoragePoolObjIsActive(privpool)) {
4390
        testError(VIR_ERR_OPERATION_INVALID,
4391
                  _("storage pool '%s' is not active"), pool->name);
4392
        goto cleanup;
4393
    }
C
Cole Robinson 已提交
4394

4395 4396 4397
    ret = privpool->volumes.count;

cleanup:
4398 4399
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4400
    return ret;
C
Cole Robinson 已提交
4401 4402 4403
}

static int
4404
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4405 4406
                           char **const names,
                           int maxnames) {
4407 4408
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
C
Cole Robinson 已提交
4409 4410
    int i = 0, n = 0;

4411
    memset(names, 0, maxnames * sizeof(*names));
4412 4413

    testDriverLock(privconn);
4414 4415
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4416
    testDriverUnlock(privconn);
4417 4418

    if (privpool == NULL) {
4419
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4420
        goto cleanup;
4421 4422 4423 4424
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4425
        testError(VIR_ERR_OPERATION_INVALID,
4426
                  _("storage pool '%s' is not active"), pool->name);
4427
        goto cleanup;
4428 4429
    }

C
Cole Robinson 已提交
4430 4431
    for (i = 0 ; i < privpool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(privpool->volumes.objs[i]->name)) == NULL) {
4432
            virReportOOMError();
C
Cole Robinson 已提交
4433 4434 4435 4436
            goto cleanup;
        }
    }

4437
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4438 4439 4440 4441 4442 4443
    return n;

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

4444
    memset(names, 0, maxnames * sizeof(*names));
4445 4446
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4447 4448 4449 4450 4451
    return -1;
}


static virStorageVolPtr
4452
testStorageVolumeLookupByName(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4453
                              const char *name ATTRIBUTE_UNUSED) {
4454 4455 4456
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4457
    virStorageVolPtr ret = NULL;
4458

4459
    testDriverLock(privconn);
4460 4461
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4462
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4463

4464
    if (privpool == NULL) {
4465
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4466
        goto cleanup;
4467 4468 4469 4470
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4471
        testError(VIR_ERR_OPERATION_INVALID,
4472
                  _("storage pool '%s' is not active"), pool->name);
4473
        goto cleanup;
4474 4475 4476 4477 4478
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4479
        testError(VIR_ERR_NO_STORAGE_VOL,
C
Cole Robinson 已提交
4480
                  _("no storage vol with matching name '%s'"), name);
4481
        goto cleanup;
C
Cole Robinson 已提交
4482 4483
    }

4484 4485 4486 4487
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);

cleanup:
4488 4489
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4490
    return ret;
C
Cole Robinson 已提交
4491 4492 4493 4494 4495 4496
}


static virStorageVolPtr
testStorageVolumeLookupByKey(virConnectPtr conn,
                             const char *key) {
4497
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4498
    unsigned int i;
4499
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4500

4501
    testDriverLock(privconn);
C
Cole Robinson 已提交
4502
    for (i = 0 ; i < privconn->pools.count ; i++) {
4503
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4504
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4505
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4506 4507
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4508 4509 4510 4511 4512
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4513
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4514 4515
                break;
            }
C
Cole Robinson 已提交
4516
        }
4517
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4518
    }
4519
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4520

4521
    if (!ret)
4522
        testError(VIR_ERR_NO_STORAGE_VOL,
4523 4524 4525
                  _("no storage vol with matching key '%s'"), key);

    return ret;
C
Cole Robinson 已提交
4526 4527 4528 4529 4530
}

static virStorageVolPtr
testStorageVolumeLookupByPath(virConnectPtr conn,
                              const char *path) {
4531
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4532
    unsigned int i;
4533
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4534

4535
    testDriverLock(privconn);
C
Cole Robinson 已提交
4536
    for (i = 0 ; i < privconn->pools.count ; i++) {
4537
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4538
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4539
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4540 4541
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

4542 4543 4544 4545 4546
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4547
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4548 4549
                break;
            }
C
Cole Robinson 已提交
4550
        }
4551
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4552
    }
4553
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4554

4555
    if (!ret)
4556
        testError(VIR_ERR_NO_STORAGE_VOL,
4557 4558 4559
                  _("no storage vol with matching path '%s'"), path);

    return ret;
C
Cole Robinson 已提交
4560 4561 4562
}

static virStorageVolPtr
4563
testStorageVolumeCreateXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4564 4565
                           const char *xmldesc,
                           unsigned int flags ATTRIBUTE_UNUSED) {
4566 4567
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4568 4569
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
4570

4571
    testDriverLock(privconn);
4572 4573
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4574
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4575

4576
    if (privpool == NULL) {
4577
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4578
        goto cleanup;
4579 4580 4581
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4582
        testError(VIR_ERR_OPERATION_INVALID,
4583
                  _("storage pool '%s' is not active"), pool->name);
4584
        goto cleanup;
4585
    }
C
Cole Robinson 已提交
4586

4587
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4588
    if (privvol == NULL)
4589
        goto cleanup;
4590 4591

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4592
        testError(VIR_ERR_OPERATION_FAILED,
C
Cole Robinson 已提交
4593
                  "%s", _("storage vol already exists"));
4594
        goto cleanup;
C
Cole Robinson 已提交
4595 4596 4597
    }

    /* Make sure enough space */
4598
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
4599
         privpool->def->capacity) {
4600
        testError(VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
4601
                  _("Not enough free space in pool for volume '%s'"),
4602
                  privvol->name);
4603
        goto cleanup;
C
Cole Robinson 已提交
4604 4605 4606 4607
    }

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4608
        virReportOOMError();
4609
        goto cleanup;
C
Cole Robinson 已提交
4610 4611
    }

4612 4613 4614
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4615
        virReportOOMError();
4616
        goto cleanup;
C
Cole Robinson 已提交
4617 4618
    }

4619 4620
    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4621
        virReportOOMError();
4622
        goto cleanup;
C
Cole Robinson 已提交
4623 4624
    }

4625
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
4626 4627 4628
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

4631 4632
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);
4633
    privvol = NULL;
4634 4635 4636

cleanup:
    virStorageVolDefFree(privvol);
4637 4638
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4639
    return ret;
C
Cole Robinson 已提交
4640 4641
}

4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657
static virStorageVolPtr
testStorageVolumeCreateXMLFrom(virStoragePoolPtr pool,
                               const char *xmldesc,
                               virStorageVolPtr clonevol,
                               unsigned int flags ATTRIBUTE_UNUSED) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

    testDriverLock(privconn);
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
    testDriverUnlock(privconn);

    if (privpool == NULL) {
4658
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4659 4660 4661 4662
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4663
        testError(VIR_ERR_OPERATION_INVALID,
4664 4665 4666 4667
                  _("storage pool '%s' is not active"), pool->name);
        goto cleanup;
    }

4668
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4669 4670 4671 4672
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4673
        testError(VIR_ERR_OPERATION_FAILED,
4674 4675 4676 4677 4678 4679
                  "%s", _("storage vol already exists"));
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
4680
        testError(VIR_ERR_NO_STORAGE_VOL,
4681 4682 4683 4684 4685 4686 4687 4688
                  _("no storage vol with matching name '%s'"),
                  clonevol->name);
        goto cleanup;
    }

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

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

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

    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4712
        virReportOOMError();
4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732
        goto cleanup;
    }

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

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

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

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

C
Cole Robinson 已提交
4733
static int
4734
testStorageVolumeDelete(virStorageVolPtr vol,
C
Cole Robinson 已提交
4735
                        unsigned int flags ATTRIBUTE_UNUSED) {
4736 4737 4738
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
4739
    int i;
4740
    int ret = -1;
C
Cole Robinson 已提交
4741

4742
    testDriverLock(privconn);
4743 4744
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4745
    testDriverUnlock(privconn);
4746 4747

    if (privpool == NULL) {
4748
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4749
        goto cleanup;
4750 4751 4752 4753 4754 4755
    }


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

    if (privvol == NULL) {
4756
        testError(VIR_ERR_NO_STORAGE_VOL,
4757 4758
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4759
        goto cleanup;
4760 4761 4762
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4763
        testError(VIR_ERR_OPERATION_INVALID,
4764
                  _("storage pool '%s' is not active"), vol->pool);
4765
        goto cleanup;
4766 4767 4768
    }


C
Cole Robinson 已提交
4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791
    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;
        }
    }
4792
    ret = 0;
C
Cole Robinson 已提交
4793

4794
cleanup:
4795 4796
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4797
    return ret;
C
Cole Robinson 已提交
4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813
}


static int testStorageVolumeTypeForPool(int pooltype) {

    switch(pooltype) {
        case VIR_STORAGE_POOL_DIR:
        case VIR_STORAGE_POOL_FS:
        case VIR_STORAGE_POOL_NETFS:
            return VIR_STORAGE_VOL_FILE;
        default:
            return VIR_STORAGE_VOL_BLOCK;
    }
}

static int
4814
testStorageVolumeGetInfo(virStorageVolPtr vol,
C
Cole Robinson 已提交
4815
                         virStorageVolInfoPtr info) {
4816 4817 4818
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4819
    int ret = -1;
4820

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

    if (privpool == NULL) {
4827
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4828
        goto cleanup;
4829 4830 4831 4832 4833
    }

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

    if (privvol == NULL) {
4834
        testError(VIR_ERR_NO_STORAGE_VOL,
4835 4836
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4837
        goto cleanup;
4838 4839 4840
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4841
        testError(VIR_ERR_OPERATION_INVALID,
4842
                  _("storage pool '%s' is not active"), vol->pool);
4843
        goto cleanup;
4844
    }
C
Cole Robinson 已提交
4845 4846 4847 4848 4849

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

4852
cleanup:
4853 4854
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4855
    return ret;
C
Cole Robinson 已提交
4856 4857 4858
}

static char *
4859
testStorageVolumeGetXMLDesc(virStorageVolPtr vol,
C
Cole Robinson 已提交
4860
                            unsigned int flags ATTRIBUTE_UNUSED) {
4861 4862 4863
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4864
    char *ret = NULL;
4865

4866
    testDriverLock(privconn);
4867 4868
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4869
    testDriverUnlock(privconn);
4870 4871

    if (privpool == NULL) {
4872
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4873
        goto cleanup;
4874 4875 4876 4877 4878
    }

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

    if (privvol == NULL) {
4879
        testError(VIR_ERR_NO_STORAGE_VOL,
4880 4881
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4882
        goto cleanup;
4883
    }
C
Cole Robinson 已提交
4884

4885
    if (!virStoragePoolObjIsActive(privpool)) {
4886
        testError(VIR_ERR_OPERATION_INVALID,
4887
                  _("storage pool '%s' is not active"), vol->pool);
4888
        goto cleanup;
4889 4890
    }

4891
    ret = virStorageVolDefFormat(privpool->def, privvol);
4892 4893

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

static char *
4900 4901 4902 4903
testStorageVolumeGetPath(virStorageVolPtr vol) {
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4904
    char *ret = NULL;
4905

4906
    testDriverLock(privconn);
4907 4908
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4909
    testDriverUnlock(privconn);
4910 4911

    if (privpool == NULL) {
4912
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4913
        goto cleanup;
4914 4915 4916 4917 4918
    }

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

    if (privvol == NULL) {
4919
        testError(VIR_ERR_NO_STORAGE_VOL,
4920 4921
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4922
        goto cleanup;
4923 4924 4925
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4926
        testError(VIR_ERR_OPERATION_INVALID,
4927
                  _("storage pool '%s' is not active"), vol->pool);
4928
        goto cleanup;
4929 4930
    }

C
Cole Robinson 已提交
4931
    ret = strdup(privvol->target.path);
4932
    if (ret == NULL)
4933
        virReportOOMError();
4934 4935

cleanup:
4936 4937
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4938 4939 4940
    return ret;
}

4941

4942
/* Node device implementations */
4943 4944
static virDrvOpenStatus testDevMonOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
4945
                                       unsigned int flags ATTRIBUTE_UNUSED) {
4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

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

4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
                     unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

    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,
                    unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

    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) {
5024
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

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

static char *
5037 5038
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
                         unsigned int flags ATTRIBUTE_UNUSED)
5039 5040 5041 5042 5043 5044 5045 5046 5047 5048
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

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

    if (!obj) {
5049 5050
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
5051 5052 5053 5054
                                 dev->name);
        goto cleanup;
    }

5055
    ret = virNodeDeviceDefFormat(obj->def);
5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074

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) {
5075
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
5076 5077 5078 5079 5080 5081 5082 5083
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
5084
            virReportOOMError();
5085
    } else {
5086
        virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
5087 5088 5089 5090 5091 5092 5093 5094 5095
                                 "%s", _("no parent for this device"));
    }

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

5096

5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110
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) {
5111 5112
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141
                                 dev->name);
        goto cleanup;
    }

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

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


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

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

    if (!obj) {
5142
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

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

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

5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
                        unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr driver = conn->privateData;
    virNodeDeviceDefPtr def = NULL;
    virNodeDeviceObjPtr obj = NULL;
    char *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;
    virNodeDevicePtr dev = NULL;
    virNodeDevCapsDefPtr caps;

    testDriverLock(driver);

5181
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE);
5182 5183 5184 5185 5186
    if (def == NULL) {
        goto cleanup;
    }

    /* We run these next two simply for validation */
5187
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1) {
5188 5189 5190
        goto cleanup;
    }

5191
    if (virNodeDeviceGetParentHost(&driver->devs,
5192 5193 5194 5195 5196 5197 5198 5199 5200 5201
                                   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))) {
5202
        virReportOOMError();
5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217
        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;

        caps->data.scsi_host.host = virRandom(1024);
        caps = caps->next;
    }


5218
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def))) {
5219 5220 5221 5222 5223 5224 5225 5226
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
cleanup:
    testDriverUnlock(driver);
5227
    virNodeDeviceDefFree(def);
5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246
    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) {
5247
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5248 5249 5250
        goto out;
    }

5251
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
5252 5253 5254 5255 5256
        goto out;
    }

    parent_name = strdup(obj->def->parent);
    if (parent_name == NULL) {
5257
        virReportOOMError();
5258 5259 5260 5261 5262 5263 5264 5265 5266 5267
        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 */
5268
    if (virNodeDeviceGetParentHost(&driver->devs,
5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287
                                   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;
}

5288 5289

/* Domain event implementations */
5290
static int
5291 5292 5293 5294
testDomainEventRegister(virConnectPtr conn,
                        virConnectDomainEventCallback callback,
                        void *opaque,
                        virFreeCallback freecb)
5295 5296 5297 5298 5299
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5300 5301
    ret = virDomainEventCallbackListAdd(conn,
                                        driver->domainEventState->callbacks,
5302 5303 5304 5305 5306 5307
                                        callback, opaque, freecb);
    testDriverUnlock(driver);

    return ret;
}

5308

5309
static int
5310 5311
testDomainEventDeregister(virConnectPtr conn,
                          virConnectDomainEventCallback callback)
5312 5313 5314 5315 5316
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5317 5318 5319
    ret = virDomainEventStateDeregister(conn,
                                        driver->domainEventState,
                                        callback);
5320 5321 5322 5323 5324
    testDriverUnlock(driver);

    return ret;
}

5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337

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

    testDriverLock(driver);
5338 5339
    ret = virDomainEventCallbackListAddID(conn,
                                          driver->domainEventState->callbacks,
5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354
                                          dom, eventID,
                                          callback, opaque, freecb);
    testDriverUnlock(driver);

    return ret;
}

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

    testDriverLock(driver);
5355 5356 5357
    ret = virDomainEventStateDeregisterAny(conn,
                                           driver->domainEventState,
                                           callbackID);
5358 5359 5360 5361 5362 5363
    testDriverUnlock(driver);

    return ret;
}


5364 5365
static void testDomainEventDispatchFunc(virConnectPtr conn,
                                        virDomainEventPtr event,
5366
                                        virConnectDomainEventGenericCallback cb,
5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382
                                        void *cbopaque,
                                        void *opaque)
{
    testConnPtr driver = opaque;

    /* Drop the lock whle dispatching, for sake of re-entrancy */
    testDriverUnlock(driver);
    virDomainEventDispatchDefaultFunc(conn, event, cb, cbopaque, NULL);
    testDriverLock(driver);
}

static void testDomainEventFlush(int timer ATTRIBUTE_UNUSED, void *opaque)
{
    testConnPtr driver = opaque;

    testDriverLock(driver);
5383 5384 5385
    virDomainEventStateFlush(driver->domainEventState,
                             testDomainEventDispatchFunc,
                             driver);
5386 5387 5388 5389 5390 5391 5392 5393
    testDriverUnlock(driver);
}


/* driver must be locked before calling */
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event)
{
5394
    virDomainEventStateQueue(driver->domainEventState, event);
5395 5396
}

5397 5398
static virDrvOpenStatus testSecretOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
5399
                                       unsigned int flags ATTRIBUTE_UNUSED) {
5400 5401 5402
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5403
    conn->secretPrivateData = conn->privateData;
5404 5405 5406 5407 5408 5409 5410
    return VIR_DRV_OPEN_SUCCESS;
}

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

5412 5413 5414

static virDrvOpenStatus testNWFilterOpen(virConnectPtr conn,
                                         virConnectAuthPtr auth ATTRIBUTE_UNUSED,
5415
                                         unsigned int flags ATTRIBUTE_UNUSED) {
5416 5417 5418
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5419
    conn->nwfilterPrivateData = conn->privateData;
5420 5421 5422 5423 5424 5425 5426 5427
    return VIR_DRV_OPEN_SUCCESS;
}

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

5428
static virDriver testDriver = {
5429 5430
    .no = VIR_DRV_TEST,
    .name = "Test",
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 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474
    .open = testOpen, /* 0.1.1 */
    .close = testClose, /* 0.1.1 */
    .version = testGetVersion, /* 0.1.1 */
    .getHostname = virGetHostname, /* 0.6.3 */
    .getMaxVcpus = testGetMaxVCPUs, /* 0.3.2 */
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
    .getCapabilities = testGetCapabilities, /* 0.2.1 */
    .listDomains = testListDomains, /* 0.1.1 */
    .numOfDomains = testNumOfDomains, /* 0.1.1 */
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
    .domainLookupByID = testLookupDomainByID, /* 0.1.1 */
    .domainLookupByUUID = testLookupDomainByUUID, /* 0.1.1 */
    .domainLookupByName = testLookupDomainByName, /* 0.1.1 */
    .domainSuspend = testPauseDomain, /* 0.1.1 */
    .domainResume = testResumeDomain, /* 0.1.1 */
    .domainShutdown = testShutdownDomain, /* 0.1.1 */
    .domainReboot = testRebootDomain, /* 0.1.1 */
    .domainDestroy = testDestroyDomain, /* 0.1.1 */
    .domainGetOSType = testGetOSType, /* 0.1.9 */
    .domainGetMaxMemory = testGetMaxMemory, /* 0.1.4 */
    .domainSetMaxMemory = testSetMaxMemory, /* 0.1.1 */
    .domainSetMemory = testSetMemory, /* 0.1.4 */
    .domainGetInfo = testGetDomainInfo, /* 0.1.1 */
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
    .domainRestore = testDomainRestore, /* 0.3.2 */
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
    .domainSetVcpus = testSetVcpus, /* 0.1.4 */
    .domainSetVcpusFlags = testDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = testDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = testDomainPinVcpu, /* 0.7.3 */
    .domainGetVcpus = testDomainGetVcpus, /* 0.7.3 */
    .domainGetMaxVcpus = testDomainGetMaxVcpus, /* 0.7.3 */
    .domainGetXMLDesc = testDomainGetXMLDesc, /* 0.1.4 */
    .listDefinedDomains = testListDefinedDomains, /* 0.1.11 */
    .numOfDefinedDomains = testNumOfDefinedDomains, /* 0.1.11 */
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
    .domainGetSchedulerParameters = testDomainGetSchedulerParams, /* 0.3.2 */
5475
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParamsFlags, /* 0.9.2 */
5476
    .domainSetSchedulerParameters = testDomainSetSchedulerParams, /* 0.3.2 */
5477
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParamsFlags, /* 0.9.2 */
5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
    .domainEventRegister = testDomainEventRegister, /* 0.6.0 */
    .domainEventDeregister = testDomainEventDeregister, /* 0.6.0 */
    .isEncrypted = testIsEncrypted, /* 0.7.3 */
    .isSecure = testIsSecure, /* 0.7.3 */
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
    .domainEventRegisterAny = testDomainEventRegisterAny, /* 0.8.0 */
    .domainEventDeregisterAny = testDomainEventDeregisterAny, /* 0.8.0 */
5490 5491 5492 5493
};

static virNetworkDriver testNetworkDriver = {
    "Test",
5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512
    .open = testOpenNetwork, /* 0.3.2 */
    .close = testCloseNetwork, /* 0.3.2 */
    .numOfNetworks = testNumNetworks, /* 0.3.2 */
    .listNetworks = testListNetworks, /* 0.3.2 */
    .numOfDefinedNetworks = testNumDefinedNetworks, /* 0.3.2 */
    .listDefinedNetworks = testListDefinedNetworks, /* 0.3.2 */
    .networkLookupByUUID = testLookupNetworkByUUID, /* 0.3.2 */
    .networkLookupByName = testLookupNetworkByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreate, /* 0.3.2 */
    .networkDefineXML = testNetworkDefine, /* 0.3.2 */
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
    .networkCreate = testNetworkStart, /* 0.3.2 */
    .networkDestroy = testNetworkDestroy, /* 0.3.2 */
    .networkGetXMLDesc = testNetworkGetXMLDesc, /* 0.3.2 */
    .networkGetBridgeName = testNetworkGetBridgeName, /* 0.3.2 */
    .networkGetAutostart = testNetworkGetAutostart, /* 0.3.2 */
    .networkSetAutostart = testNetworkSetAutostart, /* 0.3.2 */
    .networkIsActive = testNetworkIsActive, /* 0.7.3 */
    .networkIsPersistent = testNetworkIsPersistent, /* 0.7.3 */
5513 5514
};

L
Laine Stump 已提交
5515 5516
static virInterfaceDriver testInterfaceDriver = {
    "Test",                     /* name */
5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530
    .open = testOpenInterface, /* 0.7.0 */
    .close = testCloseInterface, /* 0.7.0 */
    .numOfInterfaces = testNumOfInterfaces, /* 0.7.0 */
    .listInterfaces = testListInterfaces, /* 0.7.0 */
    .numOfDefinedInterfaces = testNumOfDefinedInterfaces, /* 0.7.0 */
    .listDefinedInterfaces = testListDefinedInterfaces, /* 0.7.0 */
    .interfaceLookupByName = testLookupInterfaceByName, /* 0.7.0 */
    .interfaceLookupByMACString = testLookupInterfaceByMACString, /* 0.7.0 */
    .interfaceGetXMLDesc = testInterfaceGetXMLDesc, /* 0.7.0 */
    .interfaceDefineXML = testInterfaceDefineXML, /* 0.7.0 */
    .interfaceUndefine = testInterfaceUndefine, /* 0.7.0 */
    .interfaceCreate = testInterfaceCreate, /* 0.7.0 */
    .interfaceDestroy = testInterfaceDestroy, /* 0.7.0 */
    .interfaceIsActive = testInterfaceIsActive, /* 0.7.3 */
5531 5532 5533
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
5534 5535 5536
};


5537 5538
static virStorageDriver testStorageDriver = {
    .name = "Test",
5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575
    .open = testStorageOpen, /* 0.4.1 */
    .close = testStorageClose, /* 0.4.1 */

    .numOfPools = testStorageNumPools, /* 0.5.0 */
    .listPools = testStorageListPools, /* 0.5.0 */
    .numOfDefinedPools = testStorageNumDefinedPools, /* 0.5.0 */
    .listDefinedPools = testStorageListDefinedPools, /* 0.5.0 */
    .findPoolSources = testStorageFindPoolSources, /* 0.5.0 */
    .poolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .poolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .poolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
    .poolCreateXML = testStoragePoolCreate, /* 0.5.0 */
    .poolDefineXML = testStoragePoolDefine, /* 0.5.0 */
    .poolBuild = testStoragePoolBuild, /* 0.5.0 */
    .poolUndefine = testStoragePoolUndefine, /* 0.5.0 */
    .poolCreate = testStoragePoolStart, /* 0.5.0 */
    .poolDestroy = testStoragePoolDestroy, /* 0.5.0 */
    .poolDelete = testStoragePoolDelete, /* 0.5.0 */
    .poolRefresh = testStoragePoolRefresh, /* 0.5.0 */
    .poolGetInfo = testStoragePoolGetInfo, /* 0.5.0 */
    .poolGetXMLDesc = testStoragePoolGetXMLDesc, /* 0.5.0 */
    .poolGetAutostart = testStoragePoolGetAutostart, /* 0.5.0 */
    .poolSetAutostart = testStoragePoolSetAutostart, /* 0.5.0 */
    .poolNumOfVolumes = testStoragePoolNumVolumes, /* 0.5.0 */
    .poolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */

    .volLookupByName = testStorageVolumeLookupByName, /* 0.5.0 */
    .volLookupByKey = testStorageVolumeLookupByKey, /* 0.5.0 */
    .volLookupByPath = testStorageVolumeLookupByPath, /* 0.5.0 */
    .volCreateXML = testStorageVolumeCreateXML, /* 0.5.0 */
    .volCreateXMLFrom = testStorageVolumeCreateXMLFrom, /* 0.6.4 */
    .volDelete = testStorageVolumeDelete, /* 0.5.0 */
    .volGetInfo = testStorageVolumeGetInfo, /* 0.5.0 */
    .volGetXMLDesc = testStorageVolumeGetXMLDesc, /* 0.5.0 */
    .volGetPath = testStorageVolumeGetPath, /* 0.5.0 */
    .poolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .poolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
5576 5577
};

5578 5579
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591
    .open = testDevMonOpen, /* 0.6.0 */
    .close = testDevMonClose, /* 0.6.0 */

    .numOfDevices = testNodeNumOfDevices, /* 0.7.2 */
    .listDevices = testNodeListDevices, /* 0.7.2 */
    .deviceLookupByName = testNodeDeviceLookupByName, /* 0.7.2 */
    .deviceGetXMLDesc = testNodeDeviceGetXMLDesc, /* 0.7.2 */
    .deviceGetParent = testNodeDeviceGetParent, /* 0.7.2 */
    .deviceNumOfCaps = testNodeDeviceNumOfCaps, /* 0.7.2 */
    .deviceListCaps = testNodeDeviceListCaps, /* 0.7.2 */
    .deviceCreateXML = testNodeDeviceCreateXML, /* 0.7.3 */
    .deviceDestroy = testNodeDeviceDestroy, /* 0.7.3 */
5592 5593
};

5594 5595
static virSecretDriver testSecretDriver = {
    .name = "Test",
5596 5597
    .open = testSecretOpen, /* 0.7.1 */
    .close = testSecretClose, /* 0.7.1 */
5598
};
5599 5600


5601 5602
static virNWFilterDriver testNWFilterDriver = {
    .name = "Test",
5603 5604
    .open = testNWFilterOpen, /* 0.8.0 */
    .close = testNWFilterClose, /* 0.8.0 */
5605 5606
};

5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
5619 5620
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
5621 5622
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
5623 5624
    if (virRegisterDeviceMonitor(&testDevMonitor) < 0)
        return -1;
5625 5626
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
5627 5628
    if (virRegisterNWFilterDriver(&testNWFilterDriver) < 0)
        return -1;
5629

5630 5631
    return 0;
}