test_driver.c 156.0 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 47
#include "domain_event.h"
#include "event.h"
C
Cole Robinson 已提交
48
#include "storage_conf.h"
49
#include "node_device_conf.h"
50
#include "xml.h"
51
#include "threads.h"
52
#include "logging.h"
53
#include "files.h"
54

55 56
#define VIR_FROM_THIS VIR_FROM_TEST

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

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

66 67 68 69 70 71 72 73 74 75 76
#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
77

78
struct _testConn {
79
    virMutex lock;
80

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

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

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

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

115

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

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


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

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

136 137 138 139 140 141 142 143 144 145 146 147 148 149
static void *testDomainObjPrivateAlloc(void)
{
    testDomainObjPrivatePtr priv;

    if (VIR_ALLOC(priv) < 0)
        return NULL;

    return priv;
}

static void testDomainObjPrivateFree(void *data)
{
    testDomainObjPrivatePtr priv = data;

D
Daniel P. Berrange 已提交
150
    VIR_FREE(priv->vcpu_infos);
151 152 153 154 155
    VIR_FREE(priv->cpumaps);
    VIR_FREE(priv);
}


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

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

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

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

178 179 180 181 182 183 184 185 186 187
    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;
188

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

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

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

206 207 208 209 210 211 212 213
    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;

214
    return caps;
215

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

222

223 224 225 226 227 228 229 230 231 232
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>";
233 234


235 236 237 238 239 240 241 242 243 244 245
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>";
246

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

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

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

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

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

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

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

        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0) {
323
            virReportOOMError();
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
            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;
    }

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

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

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

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

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

362
    return 0;
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 428
/* 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) {
429
        virReportOOMError();
430 431 432 433
        goto cleanup;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

533
    /* Numa setup */
534 535 536 537 538 539 540 541 542
    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;
    }

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

    privconn->nextDomID = 1;

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

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

565
    virDomainObjUnlock(domobj);
566

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

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

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

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

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

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

612
    testDriverUnlock(privconn);
613

614 615 616
    return VIR_DRV_OPEN_SUCCESS;

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

792
    privconn->nextDomID = 1;
793
    privconn->numCells = 0;
C
Chris Lalancette 已提交
794
    if (virStrcpyStatic(privconn->path, file) == NULL) {
795
        testError(VIR_ERR_INTERNAL_ERROR,
C
Chris Lalancette 已提交
796 797 798
                  _("Path %s too big for destination"), file);
        goto error;
    }
799 800 801
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

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

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

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

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

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

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

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

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

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

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

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

917
        virDomainObjUnlock(dom);
918
    }
919
    VIR_FREE(domains);
920

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

1081
    return (0);
1082 1083

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

1100

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

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

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

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

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

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

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

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

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

1149 1150 1151
    testDriverUnlock(privconn);

    return VIR_DRV_OPEN_SUCCESS;
1152 1153
}

1154
static int testClose(virConnectPtr conn)
1155
{
1156
    testConnPtr privconn = conn->privateData;
1157
    testDriverLock(privconn);
1158
    virCapabilitiesFree(privconn->caps);
1159
    virDomainObjListDeinit(&privconn->domains);
D
Daniel P. Berrange 已提交
1160
    virNodeDeviceObjListFree(&privconn->devs);
1161
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1162
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1163
    virStoragePoolObjListFree(&privconn->pools);
1164
    virDomainEventStateFree(privconn->domainEventState);
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 1907
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
                              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 2062 2063 2064 2065 2066 2067 2068 2069
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* Exactly one of LIVE or CONFIG must be set.  */
    if (!(flags & VIR_DOMAIN_VCPU_LIVE) == !(flags & VIR_DOMAIN_VCPU_CONFIG)) {
        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 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
    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;
    }

    if (flags & VIR_DOMAIN_VCPU_LIVE) {
        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 2107 2108 2109 2110 2111 2112 2113 2114
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_VCPU_LIVE |
                                            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 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
    if ((flags & (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG)) == 0 ||
        (flags & (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_LIVE)) ==
         (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_LIVE)) {
        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_VCPU_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 2172
    switch (flags) {
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_CONFIG:
2173 2174 2175
        persistentDef->maxvcpus = nrCpus;
        if (nrCpus < persistentDef->vcpus)
            persistentDef->vcpus = nrCpus;
2176 2177
        ret = 0;
        break;
2178

2179
    case VIR_DOMAIN_VCPU_CONFIG:
2180
        persistentDef->vcpus = nrCpus;
2181 2182 2183 2184 2185 2186 2187 2188 2189
        ret = 0;
        break;

    case VIR_DOMAIN_VCPU_LIVE:
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
        break;

    case VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG:
        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 2205 2206 2207
static int
testSetVcpus(virDomainPtr domain, unsigned int nrCpus)
{
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_VCPU_LIVE);
}

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, 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 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862
                                        int flags ATTRIBUTE_UNUSED) {
    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
static char *testNetworkGetXMLDesc(virNetworkPtr network, int flags ATTRIBUTE_UNUSED) {
3181 3182
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3183
    char *ret = NULL;
3184

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

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

3195
    ret = virNetworkDefFormat(privnet->def);
3196 3197

cleanup:
3198 3199
    if (privnet)
        virNetworkObjUnlock(privnet);
3200
    return ret;
3201 3202 3203
}

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

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

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

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

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

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

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

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

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

3252
    *autostart = privnet->autostart;
3253 3254 3255
    ret = 0;

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

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

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

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

3277
    privnet->autostart = autostart ? 1 : 0;
3278 3279 3280
    ret = 0;

cleanup:
3281 3282
    if (privnet)
        virNetworkObjUnlock(privnet);
3283
    return ret;
3284
}
3285

C
Cole Robinson 已提交
3286

L
Laine Stump 已提交
3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316
/*
 * Physical host interface routines
 */

static virDrvOpenStatus testOpenInterface(virConnectPtr conn,
                                          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                          int flags ATTRIBUTE_UNUSED)
{
    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 已提交
3317
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334
            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 已提交
3335
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347
            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:
3348
    virReportOOMError();
L
Laine Stump 已提交
3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362
    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 已提交
3363
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380
            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 已提交
3381
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393
            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:
3394
    virReportOOMError();
L
Laine Stump 已提交
3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412
    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) {
3413
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437
        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) {
3438
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3439 3440 3441 3442
        goto cleanup;
    }

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

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

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

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

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

3476 3477 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
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;
}
3554

L
Laine Stump 已提交
3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567
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) {
3568
        testError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
3569 3570 3571
        goto cleanup;
    }

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

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);
3590
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3591 3592
        goto cleanup;

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

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

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

    privinterface->active = 0;
    ret = 0;

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



C
Cole Robinson 已提交
3695 3696 3697 3698
/*
 * Storage Driver routines
 */

3699

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

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

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

    return 0;
}

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

3730

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

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

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

3747 3748 3749
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

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

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

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

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

3771 3772 3773
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

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

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

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

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

    return numActive;
}

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

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

    return n;

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

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

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

    return numInactive;
}

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

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

    return n;

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


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

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



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

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

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

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

    privpool->active = 1;
3944
    ret = 0;
C
Cole Robinson 已提交
3945

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

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

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

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

    switch (pool_type) {

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

    privpool->active = 0;

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

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


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

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

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

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

4209
    ret = 0;
C
Cole Robinson 已提交
4210

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


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

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

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

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

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


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

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

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

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

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

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

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

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

4299
    ret = virStoragePoolDefFormat(privpool->def);
4300 4301

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

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

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

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

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

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

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

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

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

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

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4362 4363 4364
    ret = 0;

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


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

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

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

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

4393 4394 4395
    ret = privpool->volumes.count;

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

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

4409
    memset(names, 0, maxnames * sizeof(*names));
4410 4411

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

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


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

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

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

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

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


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

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

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


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

    privvol = virStorageVolDefFindByName(privpool, name);

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

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

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


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

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

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

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

    return ret;
C
Cole Robinson 已提交
4524 4525 4526 4527 4528
}

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

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

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

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

    return ret;
C
Cole Robinson 已提交
4558 4559 4560
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655
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) {
4656
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4657 4658 4659 4660
        goto cleanup;
    }

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

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

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

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

    /* Make sure enough space */
    if ((privpool->def->allocation + privvol->allocation) >
         privpool->def->capacity) {
4687
        testError(VIR_ERR_INTERNAL_ERROR,
4688 4689 4690 4691 4692 4693 4694 4695 4696
                  _("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) {
4697
        virReportOOMError();
4698 4699 4700
        goto cleanup;
    }

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

    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4710
        virReportOOMError();
4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730
        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 已提交
4731
static int
4732
testStorageVolumeDelete(virStorageVolPtr vol,
C
Cole Robinson 已提交
4733
                        unsigned int flags ATTRIBUTE_UNUSED) {
4734 4735 4736
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
4737
    int i;
4738
    int ret = -1;
C
Cole Robinson 已提交
4739

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

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


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

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

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


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

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


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
4812
testStorageVolumeGetInfo(virStorageVolPtr vol,
C
Cole Robinson 已提交
4813
                         virStorageVolInfoPtr info) {
4814 4815 4816
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4817
    int ret = -1;
4818

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

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

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

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

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

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

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

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

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

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

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

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

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

4889
    ret = virStorageVolDefFormat(privpool->def, privvol);
4890 4891

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

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

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

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

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

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

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

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

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

4939

4940
/* Node device implementations */
4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955
static virDrvOpenStatus testDevMonOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                       int flags ATTRIBUTE_UNUSED) {
    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;
}

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

    ret = virGetNodeDevice(conn, name);

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

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

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

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

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

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

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

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

5094

5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108
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) {
5109 5110
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
5111 5112 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
                                 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) {
5140
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163
                                _("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;
}

5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178
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);

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

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

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


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

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

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

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

5286 5287

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

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

    return ret;
}

5306

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

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

    return ret;
}

5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335

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

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

    return ret;
}

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

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

    return ret;
}


5362 5363
static void testDomainEventDispatchFunc(virConnectPtr conn,
                                        virDomainEventPtr event,
5364
                                        virConnectDomainEventGenericCallback cb,
5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380
                                        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);
5381 5382 5383
    virDomainEventStateFlush(driver->domainEventState,
                             testDomainEventDispatchFunc,
                             driver);
5384 5385 5386 5387 5388 5389 5390 5391
    testDriverUnlock(driver);
}


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

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

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

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

5410 5411 5412 5413 5414 5415 5416

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

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

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

5426
static virDriver testDriver = {
5427 5428
    .no = VIR_DRV_TEST,
    .name = "Test",
5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472
    .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 */
5473
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParamsFlags, /* 0.9.2 */
5474
    .domainSetSchedulerParameters = testDomainSetSchedulerParams, /* 0.3.2 */
5475
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParamsFlags, /* 0.9.2 */
5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487
    .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 */
5488 5489 5490 5491
};

static virNetworkDriver testNetworkDriver = {
    "Test",
5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510
    .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 */
5511 5512
};

L
Laine Stump 已提交
5513 5514
static virInterfaceDriver testInterfaceDriver = {
    "Test",                     /* name */
5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528
    .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 */
5529 5530 5531
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
5532 5533 5534
};


5535 5536
static virStorageDriver testStorageDriver = {
    .name = "Test",
5537 5538 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
    .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 */
5574 5575
};

5576 5577
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589
    .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 */
5590 5591
};

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


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

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

5628 5629
    return 0;
}