test.c 102.2 KB
Newer Older
1 2 3
/*
 * test.c: A "mock" hypervisor for use by application unit tests
 *
4
 * Copyright (C) 2006-2008 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.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"
44
#include "domain_conf.h"
45 46
#include "domain_event.h"
#include "event.h"
C
Cole Robinson 已提交
47
#include "storage_conf.h"
48
#include "xml.h"
49
#include "threads.h"
50
#include "logging.h"
51

52 53
#define VIR_FROM_THIS VIR_FROM_TEST

54 55 56 57 58 59 60 61 62 63 64
#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
65

66
struct _testConn {
67
    virMutex lock;
68

69 70
    char path[PATH_MAX];
    int nextDomID;
71
    virCapsPtr caps;
72
    virNodeInfo nodeInfo;
73
    virDomainObjList domains;
74
    virNetworkObjList networks;
C
Cole Robinson 已提交
75
    virStoragePoolObjList pools;
76 77
    int numCells;
    testCell cells[MAX_CELLS];
78 79 80 81 82 83 84


    /* An array of callbacks */
    virDomainEventCallbackListPtr domainEventCallbacks;
    virDomainEventQueuePtr domainEventQueue;
    int domainEventTimer;
    int domainEventDispatching;
85 86 87
};
typedef struct _testConn testConn;
typedef struct _testConn *testConnPtr;
88

89
#define TEST_MODEL "i686"
90
#define TEST_MODEL_WORDSIZE 32
91
#define TEST_EMULATOR "/usr/bin/test-hv"
92

93
static const virNodeInfo defaultNodeInfo = {
94
    TEST_MODEL,
95 96 97 98 99 100 101
    1024*1024*3, /* 3 GB */
    16,
    1400,
    2,
    2,
    2,
    2,
102 103
};

104

105
#define testError(conn, code, fmt...)                               \
106
        virReportErrorHelper(conn, VIR_FROM_TEST, code, __FILE__, \
107
                               __FUNCTION__, __LINE__, fmt)
108

109 110 111 112 113 114
static int testClose(virConnectPtr conn);
static void testDomainEventFlush(int timer, void *opaque);
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event);


115 116
static void testDriverLock(testConnPtr driver)
{
117
    virMutexLock(&driver->lock);
118 119 120 121
}

static void testDriverUnlock(testConnPtr driver)
{
122
    virMutexUnlock(&driver->lock);
123 124
}

125 126
static virCapsPtr
testBuildCapabilities(virConnectPtr conn) {
127
    testConnPtr privconn = conn->privateData;
128 129 130 131
    virCapsPtr caps;
    virCapsGuestPtr guest;
    const char *const guest_types[] = { "hvm", "xen" };
    int i;
132

133 134
    if ((caps = virCapabilitiesNew(TEST_MODEL, 0, 0)) == NULL)
        goto no_memory;
135

136 137 138 139
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
        goto no_memory;
    if (virCapabilitiesAddHostFeature(caps ,"nonpae") < 0)
        goto no_memory;
140

141 142 143 144
    for (i = 0; i < privconn->numCells; i++) {
        if (virCapabilitiesAddHostNUMACell(caps, i, privconn->cells[i].numCpus,
                                           privconn->cells[i].cpus) < 0)
            goto no_memory;
145 146
    }

147 148 149 150 151 152 153 154 155 156
    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;
157

158 159 160 161 162 163 164
        if (virCapabilitiesAddGuestDomain(guest,
                                          "test",
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
            goto no_memory;
165

166 167 168 169
        if (virCapabilitiesAddGuestFeature(guest, "pae", 1, 1) == NULL)
            goto no_memory;
        if (virCapabilitiesAddGuestFeature(guest ,"nonpae", 1, 1) == NULL)
            goto no_memory;
170 171
    }

172
    return caps;
173

174
no_memory:
175
    virReportOOMError(conn);
176 177
    virCapabilitiesFree(caps);
    return NULL;
178 179
}

180

181 182 183 184 185 186 187 188 189 190
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>";
191 192


193 194 195 196 197 198 199 200 201 202 203
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>";
204

C
Cole Robinson 已提交
205 206 207 208 209 210 211 212
static const char *defaultPoolXML =
"<pool type='dir'>"
"  <name>default-pool</name>"
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
"</pool>";

213
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
C
Cole Robinson 已提交
214 215
static const unsigned long long defaultPoolAlloc = 0;

216
static int testStoragePoolObjSetDefaults(virConnectPtr conn, virStoragePoolObjPtr pool);
217

218
static int testOpenDefault(virConnectPtr conn) {
219 220
    int u;
    struct timeval tv;
221
    testConnPtr privconn;
222 223 224 225
    virDomainDefPtr domdef = NULL;
    virDomainObjPtr domobj = NULL;
    virNetworkDefPtr netdef = NULL;
    virNetworkObjPtr netobj = NULL;
C
Cole Robinson 已提交
226 227
    virStoragePoolDefPtr pooldef = NULL;
    virStoragePoolObjPtr poolobj = NULL;
228

229
    if (VIR_ALLOC(privconn) < 0) {
230
        virReportOOMError(conn);
231 232
        return VIR_DRV_OPEN_ERROR;
    }
233 234 235 236 237 238 239
    if (virMutexInit(&privconn->lock) < 0) {
        testError(conn, VIR_ERR_INTERNAL_ERROR,
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

240
    testDriverLock(privconn);
241
    conn->privateData = privconn;
242 243

    if (gettimeofday(&tv, NULL) < 0) {
244 245
        virReportSystemError(conn, errno,
                             "%s", _("getting time of day"));
246
        goto error;
247 248
    }

249
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
250

251 252 253 254 255 256 257 258 259 260
    // Numa setup
    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;
    }

261 262 263 264 265
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;

    privconn->nextDomID = 1;

266 267 268
    if (!(domdef = virDomainDefParseString(conn, privconn->caps,
                                           defaultDomainXML,
                                           VIR_DOMAIN_XML_INACTIVE)))
269 270 271 272 273
        goto error;
    if (!(domobj = virDomainAssignDef(conn, &privconn->domains, domdef))) {
        virDomainDefFree(domdef);
        goto error;
    }
274
    domobj->def->id = privconn->nextDomID++;
275 276
    domobj->state = VIR_DOMAIN_RUNNING;
    domobj->persistent = 1;
277
    virDomainObjUnlock(domobj);
278 279 280 281 282 283 284 285 286

    if (!(netdef = virNetworkDefParseString(conn, defaultNetworkXML)))
        goto error;
    if (!(netobj = virNetworkAssignDef(conn, &privconn->networks, netdef))) {
        virNetworkDefFree(netdef);
        goto error;
    }
    netobj->active = 1;
    netobj->persistent = 1;
287
    virNetworkObjUnlock(netobj);
288

C
Cole Robinson 已提交
289 290 291 292 293 294 295 296
    if (!(pooldef = virStoragePoolDefParse(conn, defaultPoolXML, NULL)))
        goto error;

    if (!(poolobj = virStoragePoolObjAssignDef(conn, &privconn->pools,
                                               pooldef))) {
        virStoragePoolDefFree(pooldef);
        goto error;
    }
297

298
    if (testStoragePoolObjSetDefaults(conn, poolobj) == -1) {
299
        virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
300
        goto error;
301
    }
C
Cole Robinson 已提交
302
    poolobj->active = 1;
303
    virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
304

305
    testDriverUnlock(privconn);
306

307 308 309
    return VIR_DRV_OPEN_SUCCESS;

error:
310
    virDomainObjListFree(&privconn->domains);
311
    virNetworkObjListFree(&privconn->networks);
C
Cole Robinson 已提交
312
    virStoragePoolObjListFree(&privconn->pools);
313
    virCapabilitiesFree(privconn->caps);
314
    testDriverUnlock(privconn);
315
    conn->privateData = NULL;
316 317
    VIR_FREE(privconn);
    return VIR_DRV_OPEN_ERROR;
318 319 320 321
}


static char *testBuildFilename(const char *relativeTo,
322 323 324 325 326 327 328 329
                               const char *filename) {
    char *offset;
    int baseLen;
    if (!filename || filename[0] == '\0')
        return (NULL);
    if (filename[0] == '/')
        return strdup(filename);

330
    offset = strrchr(relativeTo, '/');
331
    if ((baseLen = (offset-relativeTo+1))) {
332 333 334
        char *absFile;
        if (VIR_ALLOC_N(absFile, baseLen + strlen(filename) + 1) < 0)
            return NULL;
335 336 337 338 339 340 341
        strncpy(absFile, relativeTo, baseLen);
        absFile[baseLen] = '\0';
        strcat(absFile, filename);
        return absFile;
    } else {
        return strdup(filename);
    }
342 343 344
}

static int testOpenFromFile(virConnectPtr conn,
345
                            const char *file) {
346
    int fd = -1, i, ret;
347 348
    long l;
    char *str;
349
    xmlDocPtr xml = NULL;
350
    xmlNodePtr root = NULL;
C
Cole Robinson 已提交
351
    xmlNodePtr *domains = NULL, *networks = NULL, *pools = NULL;
352 353
    xmlXPathContextPtr ctxt = NULL;
    virNodeInfoPtr nodeInfo;
354 355
    virNetworkObjPtr net;
    virDomainObjPtr dom;
356 357
    testConnPtr privconn;
    if (VIR_ALLOC(privconn) < 0) {
358
        virReportOOMError(conn);
359 360
        return VIR_DRV_OPEN_ERROR;
    }
361 362 363 364 365 366 367
    if (virMutexInit(&privconn->lock) < 0) {
        testError(conn, VIR_ERR_INTERNAL_ERROR,
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

368
    testDriverLock(privconn);
369
    conn->privateData = privconn;
370 371 372

    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;
373 374

    if ((fd = open(file, O_RDONLY)) < 0) {
375 376 377
        virReportSystemError(NULL, errno,
                             _("loading host definition file '%s'"),
                             file);
378
        goto error;
379 380
    }

381 382 383
    if (!(xml = xmlReadFd(fd, file, NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
J
Jim Meyering 已提交
384
        testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("host"));
385
        goto error;
386
    }
387 388
    close(fd);
    fd = -1;
389

390 391
    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
J
Jim Meyering 已提交
392
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node"));
393
        goto error;
394 395
    }

396 397
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
C
Cole Robinson 已提交
398 399
        testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s",
                  _("creating xpath context"));
400
        goto error;
401
    }
402

403
    privconn->nextDomID = 1;
404
    privconn->numCells = 0;
405 406 407 408 409
    strncpy(privconn->path, file, PATH_MAX-1);
    privconn->path[PATH_MAX-1] = '\0';
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    nodeInfo = &privconn->nodeInfo;
410
    ret = virXPathLong(conn, "string(/node/cpu/nodes[1])", ctxt, &l);
411 412 413
    if (ret == 0) {
        nodeInfo->nodes = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
414
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu numa nodes"));
415
        goto error;
416
    }
417

418
    ret = virXPathLong(conn, "string(/node/cpu/sockets[1])", ctxt, &l);
419 420 421
    if (ret == 0) {
        nodeInfo->sockets = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
422
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu sockets"));
423
        goto error;
424
    }
425

426
    ret = virXPathLong(conn, "string(/node/cpu/cores[1])", ctxt, &l);
427 428 429
    if (ret == 0) {
        nodeInfo->cores = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
430
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu cores"));
431
        goto error;
432 433
    }

434
    ret = virXPathLong(conn, "string(/node/cpu/threads[1])", ctxt, &l);
435 436 437
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
438
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu threads"));
439
        goto error;
440
    }
441

442
    nodeInfo->cpus = nodeInfo->cores * nodeInfo->threads * nodeInfo->sockets * nodeInfo->nodes;
443
    ret = virXPathLong(conn, "string(/node/cpu/active[1])", ctxt, &l);
444 445
    if (ret == 0) {
        if (l < nodeInfo->cpus) {
446 447
            nodeInfo->cpus = l;
        }
448
    } else if (ret == -2) {
J
Jim Meyering 已提交
449
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node active cpu"));
450
        goto error;
451
    }
452
    ret = virXPathLong(conn, "string(/node/cpu/mhz[1])", ctxt, &l);
453 454 455
    if (ret == 0) {
        nodeInfo->mhz = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
456
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu mhz"));
457
        goto error;
458 459
    }

460
    str = virXPathString(conn, "string(/node/cpu/model[1])", ctxt);
461 462
    if (str != NULL) {
        strncpy(nodeInfo->model, str, sizeof(nodeInfo->model)-1);
463
        nodeInfo->model[sizeof(nodeInfo->model)-1] = '\0';
464
        VIR_FREE(str);
465 466
    }

467
    ret = virXPathLong(conn, "string(/node/memory[1])", ctxt, &l);
468 469 470
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
471
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node memory"));
472
        goto error;
473
    }
474

475
    ret = virXPathNodeSet(conn, "/node/domain", ctxt, &domains);
476
    if (ret < 0) {
J
Jim Meyering 已提交
477
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node domain list"));
478
        goto error;
479
    }
480

481
    for (i = 0 ; i < ret ; i++) {
482 483 484 485 486 487
        virDomainDefPtr def;
        char *relFile = virXMLPropString(domains[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
J
Jim Meyering 已提交
488
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("resolving domain filename"));
489 490
                goto error;
            }
491 492
            def = virDomainDefParseFile(conn, privconn->caps, absFile,
                                        VIR_DOMAIN_XML_INACTIVE);
493
            VIR_FREE(absFile);
494 495 496
            if (!def)
                goto error;
        } else {
497 498
            if ((def = virDomainDefParseNode(conn, privconn->caps, xml, domains[i],
                                   VIR_DOMAIN_XML_INACTIVE)) == NULL)
499 500 501 502 503
                goto error;
        }

        if (!(dom = virDomainAssignDef(conn, &privconn->domains, def))) {
            virDomainDefFree(def);
504 505
            goto error;
        }
506 507 508 509

        dom->state = VIR_DOMAIN_RUNNING;
        dom->def->id = privconn->nextDomID++;
        dom->persistent = 1;
510
        virDomainObjUnlock(dom);
511
    }
512
    if (domains != NULL)
513
        VIR_FREE(domains);
514

515
    ret = virXPathNodeSet(conn, "/node/network", ctxt, &networks);
516
    if (ret < 0) {
J
Jim Meyering 已提交
517
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node network list"));
518 519 520 521 522 523 524 525
        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);
526
            if (!absFile) {
J
Jim Meyering 已提交
527
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("resolving network filename"));
528 529
                goto error;
            }
530 531

            def = virNetworkDefParseFile(conn, absFile);
532
            VIR_FREE(absFile);
533 534 535 536 537
            if (!def)
                goto error;
        } else {
            if ((def = virNetworkDefParseNode(conn, xml, networks[i])) == NULL)
                goto error;
538
        }
539 540 541 542
        if (!(net = virNetworkAssignDef(conn, &privconn->networks,
                                        def))) {
            virNetworkDefFree(def);
            goto error;
543
        }
544
        net->persistent = 1;
545
        virNetworkObjUnlock(net);
546
    }
547
    if (networks != NULL)
548
        VIR_FREE(networks);
549

C
Cole Robinson 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    /* Parse Storage Pool list */
    ret = virXPathNodeSet(conn, "/node/pool", ctxt, &pools);
    if (ret < 0) {
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node pool list"));
        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) {
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s",
                          _("resolving pool filename"));
                goto error;
            }

            def = virStoragePoolDefParse(conn, NULL, absFile);
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
            xmlBufferPtr buf;
            xmlSaveCtxtPtr sctxt;

            buf = xmlBufferCreate();
            sctxt = xmlSaveToBuffer(buf, NULL, 0);
            xmlSaveTree(sctxt, pools[i]);
            xmlSaveClose(sctxt);
            if ((def = virStoragePoolDefParse(conn,
                                              (const char *) buf->content,
                                              NULL)) == NULL) {
                xmlBufferFree(buf);
                goto error;
            }
        }

        if (!(pool = virStoragePoolObjAssignDef(conn, &privconn->pools,
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

595
        if (testStoragePoolObjSetDefaults(conn, pool) == -1) {
596
            virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
597
            goto error;
598
        }
C
Cole Robinson 已提交
599
        pool->active = 1;
600
        virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
601 602 603 604
    }
    if (pools != NULL)
        VIR_FREE(pools);

J
Jim Meyering 已提交
605
    xmlXPathFreeContext(ctxt);
606
    xmlFreeDoc(xml);
607
    testDriverUnlock(privconn);
608

609
    return (0);
610 611

 error:
J
Jim Meyering 已提交
612
    xmlXPathFreeContext(ctxt);
613
    xmlFreeDoc(xml);
614 615
    VIR_FREE(domains);
    VIR_FREE(networks);
C
Cole Robinson 已提交
616
    VIR_FREE(pools);
617 618
    if (fd != -1)
        close(fd);
619
    virDomainObjListFree(&privconn->domains);
620
    virNetworkObjListFree(&privconn->networks);
C
Cole Robinson 已提交
621
    virStoragePoolObjListFree(&privconn->pools);
622
    testDriverUnlock(privconn);
623
    VIR_FREE(privconn);
624
    conn->privateData = NULL;
625
    return VIR_DRV_OPEN_ERROR;
626 627
}

628

629
static virDrvOpenStatus testOpen(virConnectPtr conn,
630
                    virConnectAuthPtr auth ATTRIBUTE_UNUSED,
631
                    int flags ATTRIBUTE_UNUSED)
632
{
633
    int ret;
634

635
    if (!conn->uri)
636
        return VIR_DRV_OPEN_DECLINED;
637

638
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
639
        return VIR_DRV_OPEN_DECLINED;
640

641
    /* Remote driver should handle these. */
642
    if (conn->uri->server)
643 644
        return VIR_DRV_OPEN_DECLINED;

645
    if (conn->uri->server)
646 647
        return VIR_DRV_OPEN_DECLINED;

648
    /* From this point on, the connection is for us. */
649 650 651
    if (!conn->uri->path
        || conn->uri->path[0] == '\0'
        || (conn->uri->path[0] == '/' && conn->uri->path[1] == '\0')) {
652
        testError (NULL, VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
653
                   "%s", _("testOpen: supply a path or use test:///default"));
654 655
        return VIR_DRV_OPEN_ERROR;
    }
656

657
    if (STREQ(conn->uri->path, "/default"))
658 659
        ret = testOpenDefault(conn);
    else
660
        ret = testOpenFromFile(conn,
661
                               conn->uri->path);
662

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
    if (ret == VIR_DRV_OPEN_SUCCESS) {
        testConnPtr privconn = conn->privateData;
        /* Init callback list */
        if (VIR_ALLOC(privconn->domainEventCallbacks) < 0 ||
            !(privconn->domainEventQueue = virDomainEventQueueNew())) {
            virReportOOMError(NULL);
            testClose(conn);
            return VIR_DRV_OPEN_ERROR;
        }

        if ((privconn->domainEventTimer =
             virEventAddTimeout(-1, testDomainEventFlush, privconn, NULL)) < 0)
            DEBUG0("virEventAddTimeout failed: No addTimeoutImpl defined. "
                   "continuing without events.");
    }

679
    return (ret);
680 681
}

682
static int testClose(virConnectPtr conn)
683
{
684
    testConnPtr privconn = conn->privateData;
685
    testDriverLock(privconn);
686
    virCapabilitiesFree(privconn->caps);
687
    virDomainObjListFree(&privconn->domains);
688
    virNetworkObjListFree(&privconn->networks);
C
Cole Robinson 已提交
689
    virStoragePoolObjListFree(&privconn->pools);
690 691 692 693 694 695 696

    virDomainEventCallbackListFree(privconn->domainEventCallbacks);
    virDomainEventQueueFree(privconn->domainEventQueue);

    if (privconn->domainEventTimer != -1)
        virEventRemoveTimeout(privconn->domainEventTimer);

697
    testDriverUnlock(privconn);
698
    virMutexDestroy(&privconn->lock);
699

700
    VIR_FREE (privconn);
701
    conn->privateData = NULL;
702
    return 0;
703 704
}

705 706
static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                          unsigned long *hvVer)
707
{
708 709
    *hvVer = 2;
    return (0);
710 711
}

712
static char *testGetHostname (virConnectPtr conn)
713
{
714
    char *result;
715

716 717
    result = virGetHostname();
    if (result == NULL) {
718 719
        virReportSystemError(conn, errno,
                             "%s", _("cannot lookup hostname"));
720 721
        return NULL;
    }
722 723
    /* Caller frees this string. */
    return result;
724 725
}

726 727 728 729 730 731 732 733
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
734
{
735
    testConnPtr privconn = conn->privateData;
736
    testDriverLock(privconn);
737
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
738
    testDriverUnlock(privconn);
739
    return (0);
740 741
}

742
static char *testGetCapabilities (virConnectPtr conn)
743
{
744
    testConnPtr privconn = conn->privateData;
745
    char *xml;
746
    testDriverLock(privconn);
747
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL)
748
        virReportOOMError(conn);
749
    testDriverUnlock(privconn);
750
    return xml;
751 752
}

753
static int testNumOfDomains(virConnectPtr conn)
754
{
755
    testConnPtr privconn = conn->privateData;
756
    unsigned int numActive = 0, i;
757

758
    testDriverLock(privconn);
759 760
    for (i = 0 ; i < privconn->domains.count ; i++)
        if (virDomainIsActive(privconn->domains.objs[i]))
761
            numActive++;
762
    testDriverUnlock(privconn);
763

764
    return numActive;
765 766
}

767
static virDomainPtr
768
testDomainCreateXML(virConnectPtr conn, const char *xml,
769
                      unsigned int flags ATTRIBUTE_UNUSED)
770
{
771
    testConnPtr privconn = conn->privateData;
772
    virDomainPtr ret = NULL;
773
    virDomainDefPtr def;
774
    virDomainObjPtr dom = NULL;
775
    virDomainEventPtr event = NULL;
776

777
    testDriverLock(privconn);
778 779
    if ((def = virDomainDefParseString(conn, privconn->caps, xml,
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
780
        goto cleanup;
781

782 783 784
    if ((dom = virDomainAssignDef(conn, &privconn->domains,
                                  def)) == NULL) {
        virDomainDefFree(def);
785
        goto cleanup;
786
    }
787 788
    dom->state = VIR_DOMAIN_RUNNING;
    dom->def->id = privconn->nextDomID++;
789

790 791 792 793
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

794
    ret = virGetDomain(conn, def->name, def->uuid);
795 796 797 798
    if (ret)
        ret->id = def->id;

cleanup:
799 800
    if (dom)
        virDomainObjUnlock(dom);
801 802
    if (event)
        testDomainEventQueue(privconn, event);
803
    testDriverUnlock(privconn);
804
    return ret;
805 806 807
}


808 809
static virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                         int id)
810
{
811
    testConnPtr privconn = conn->privateData;
812 813
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
814

815 816 817 818 819
    testDriverLock(privconn);
    dom = virDomainFindByID(&privconn->domains, id);
    testDriverUnlock(privconn);

    if (dom == NULL) {
820
        testError (conn, VIR_ERR_NO_DOMAIN, NULL);
821
        goto cleanup;
822 823
    }

824
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
825 826 827 828
    if (ret)
        ret->id = dom->def->id;

cleanup:
829 830
    if (dom)
        virDomainObjUnlock(dom);
831
    return ret;
832 833
}

834 835
static virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
836
{
837
    testConnPtr privconn = conn->privateData;
838 839
    virDomainPtr ret = NULL;
    virDomainObjPtr dom ;
840

841 842 843 844 845
    testDriverLock(privconn);
    dom = virDomainFindByUUID(&privconn->domains, uuid);
    testDriverUnlock(privconn);

    if (dom == NULL) {
846
        testError (conn, VIR_ERR_NO_DOMAIN, NULL);
847
        goto cleanup;
848
    }
849

850
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
851 852 853 854
    if (ret)
        ret->id = dom->def->id;

cleanup:
855 856
    if (dom)
        virDomainObjUnlock(dom);
857
    return ret;
858 859
}

860 861
static virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                           const char *name)
862
{
863
    testConnPtr privconn = conn->privateData;
864 865
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
866

867 868 869 870 871
    testDriverLock(privconn);
    dom = virDomainFindByName(&privconn->domains, name);
    testDriverUnlock(privconn);

    if (dom == NULL) {
872
        testError (conn, VIR_ERR_NO_DOMAIN, NULL);
873
        goto cleanup;
874
    }
875

876
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
877 878 879 880
    if (ret)
        ret->id = dom->def->id;

cleanup:
881 882
    if (dom)
        virDomainObjUnlock(dom);
883
    return ret;
884 885
}

886 887 888
static int testListDomains (virConnectPtr conn,
                            int *ids,
                            int maxids)
889
{
890
    testConnPtr privconn = conn->privateData;
891
    unsigned int n = 0, i;
892

893 894 895
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->domains.count && n < maxids ; i++) {
        virDomainObjLock(privconn->domains.objs[i]);
896 897
        if (virDomainIsActive(privconn->domains.objs[i]))
            ids[n++] = privconn->domains.objs[i]->def->id;
898 899 900
        virDomainObjUnlock(privconn->domains.objs[i]);
    }
    testDriverUnlock(privconn);
901

902
    return n;
903 904
}

905
static int testDestroyDomain (virDomainPtr domain)
906
{
907 908
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
909
    virDomainEventPtr event = NULL;
910
    int ret = -1;
911

912
    testDriverLock(privconn);
913 914 915 916 917
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
918
        goto cleanup;
919
    }
920

921
    privdom->state = VIR_DOMAIN_SHUTOFF;
922 923
    privdom->def->id = -1;
    domain->id = -1;
924 925 926
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
927 928 929
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
930
        privdom = NULL;
931
    }
932

933

934 935
    ret = 0;
cleanup:
936 937
    if (privdom)
        virDomainObjUnlock(privdom);
938 939
    if (event)
        testDomainEventQueue(privconn, event);
940
    testDriverUnlock(privconn);
941
    return ret;
942 943
}

944
static int testResumeDomain (virDomainPtr domain)
945
{
946 947
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
948
    virDomainEventPtr event = NULL;
949
    int ret = -1;
950

951
    testDriverLock(privconn);
952 953
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
954
    testDriverUnlock(privconn);
955 956 957

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
958
        goto cleanup;
959
    }
960

961
    if (privdom->state != VIR_DOMAIN_PAUSED) {
962 963 964
        testError(domain->conn,
                  VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
                  domain->name);
965
        goto cleanup;
966
    }
967

968
    privdom->state = VIR_DOMAIN_RUNNING;
969 970 971
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
972 973 974
    ret = 0;

cleanup:
975 976
    if (privdom)
        virDomainObjUnlock(privdom);
977 978 979 980 981
    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
982
    return ret;
983 984
}

985
static int testPauseDomain (virDomainPtr domain)
986
{
987 988
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
989
    virDomainEventPtr event = NULL;
990
    int ret = -1;
991

992
    testDriverLock(privconn);
993 994
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
995
    testDriverUnlock(privconn);
996 997 998

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
999
        goto cleanup;
1000
    }
1001

1002 1003
    if (privdom->state == VIR_DOMAIN_SHUTOFF ||
        privdom->state == VIR_DOMAIN_PAUSED) {
1004 1005 1006
        testError(domain->conn,
                  VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
                  domain->name);
1007
        goto cleanup;
1008
    }
1009

1010
    privdom->state = VIR_DOMAIN_PAUSED;
1011 1012 1013
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1014 1015 1016
    ret = 0;

cleanup:
1017 1018
    if (privdom)
        virDomainObjUnlock(privdom);
1019 1020 1021 1022 1023 1024

    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1025
    return ret;
1026 1027
}

1028
static int testShutdownDomain (virDomainPtr domain)
1029
{
1030 1031
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1032
    virDomainEventPtr event = NULL;
1033
    int ret = -1;
1034

1035
    testDriverLock(privconn);
1036 1037 1038 1039 1040
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1041
        goto cleanup;
1042
    }
1043

1044
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
1045 1046
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
                  _("domain '%s' not running"), domain->name);
1047
        goto cleanup;
1048
    }
1049

1050
    privdom->state = VIR_DOMAIN_SHUTOFF;
1051
    domain->id = -1;
1052
    privdom->def->id = -1;
1053 1054 1055
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1056 1057 1058 1059 1060
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
        privdom = NULL;
    }
1061
    ret = 0;
1062

1063
cleanup:
1064 1065
    if (privdom)
        virDomainObjUnlock(privdom);
1066 1067
    if (event)
        testDomainEventQueue(privconn, event);
1068
    testDriverUnlock(privconn);
1069
    return ret;
1070 1071 1072
}

/* Similar behaviour as shutdown */
1073 1074
static int testRebootDomain (virDomainPtr domain,
                             unsigned int action ATTRIBUTE_UNUSED)
1075
{
1076 1077
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1078
    virDomainEventPtr event = NULL;
1079
    int ret = -1;
1080

1081
    testDriverLock(privconn);
1082 1083 1084 1085 1086
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1087
        goto cleanup;
1088
    }
1089

1090 1091 1092 1093
    privdom->state = VIR_DOMAIN_SHUTDOWN;
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
        privdom->state = VIR_DOMAIN_SHUTOFF;
1094
        domain->id = -1;
1095
        privdom->def->id = -1;
1096 1097
        break;

1098 1099
    case VIR_DOMAIN_LIFECYCLE_RESTART:
        privdom->state = VIR_DOMAIN_RUNNING;
1100 1101
        break;

1102 1103
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
        privdom->state = VIR_DOMAIN_SHUTOFF;
1104
        domain->id = -1;
1105
        privdom->def->id = -1;
1106 1107
        break;

1108 1109
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
        privdom->state = VIR_DOMAIN_RUNNING;
1110
        break;
1111

1112
    default:
1113
        privdom->state = VIR_DOMAIN_SHUTOFF;
1114
        domain->id = -1;
1115
        privdom->def->id = -1;
1116 1117
        break;
    }
1118

1119 1120 1121 1122 1123 1124 1125 1126 1127
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1128 1129
    }

1130 1131 1132
    ret = 0;

cleanup:
1133 1134
    if (privdom)
        virDomainObjUnlock(privdom);
1135 1136
    if (event)
        testDomainEventQueue(privconn, event);
1137
    testDriverUnlock(privconn);
1138
    return ret;
1139 1140
}

1141 1142
static int testGetDomainInfo (virDomainPtr domain,
                              virDomainInfoPtr info)
1143
{
1144
    testConnPtr privconn = domain->conn->privateData;
1145
    struct timeval tv;
1146
    virDomainObjPtr privdom;
1147
    int ret = -1;
1148

1149
    testDriverLock(privconn);
1150 1151
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1152
    testDriverUnlock(privconn);
1153 1154 1155

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1156
        goto cleanup;
1157
    }
1158 1159

    if (gettimeofday(&tv, NULL) < 0) {
1160
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1161
                  "%s", _("getting time of day"));
1162
        goto cleanup;
1163 1164
    }

1165 1166 1167 1168 1169
    info->state = privdom->state;
    info->memory = privdom->def->memory;
    info->maxMem = privdom->def->maxmem;
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1170 1171 1172
    ret = 0;

cleanup:
1173 1174
    if (privdom)
        virDomainObjUnlock(privdom);
1175
    return ret;
1176 1177
}

1178
static char *testDomainDumpXML(virDomainPtr domain, int flags);
1179

1180 1181 1182 1183 1184
#define TEST_SAVE_MAGIC "TestGuestMagic"

static int testDomainSave(virDomainPtr domain,
                          const char *path)
{
1185
    testConnPtr privconn = domain->conn->privateData;
1186 1187 1188
    char *xml = NULL;
    int fd = -1;
    int len;
1189
    virDomainObjPtr privdom;
1190
    virDomainEventPtr event = NULL;
1191
    int ret = -1;
1192

1193
    testDriverLock(privconn);
1194 1195 1196 1197 1198
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1199
        goto cleanup;
1200
    }
1201

1202
    xml = testDomainDumpXML(domain, 0);
1203
    if (xml == NULL) {
1204 1205 1206
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
1207
        goto cleanup;
1208
    }
1209 1210

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1211 1212 1213
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
1214
        goto cleanup;
1215
    }
1216
    len = strlen(xml);
1217
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1218 1219 1220
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1221
        goto cleanup;
1222
    }
1223
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
1224 1225 1226
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1227
        goto cleanup;
1228
    }
1229
    if (safewrite(fd, xml, len) < 0) {
1230 1231 1232
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1233
        goto cleanup;
1234
    }
1235

1236
    if (close(fd) < 0) {
1237 1238 1239
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1240
        goto cleanup;
1241
    }
1242 1243
    fd = -1;

1244
    privdom->state = VIR_DOMAIN_SHUTOFF;
1245 1246 1247
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1248 1249 1250
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1251
        privdom = NULL;
1252
    }
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
    ret = 0;

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) {
        if (fd != -1)
            close(fd);
        unlink(path);
    }
1266 1267
    if (privdom)
        virDomainObjUnlock(privdom);
1268 1269
    if (event)
        testDomainEventQueue(privconn, event);
1270
    testDriverUnlock(privconn);
1271
    return ret;
1272 1273
}

1274 1275
static int testDomainRestore(virConnectPtr conn,
                             const char *path)
1276
{
1277
    testConnPtr privconn = conn->privateData;
1278
    char *xml = NULL;
1279
    char magic[15];
1280 1281 1282
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
1283
    virDomainObjPtr dom = NULL;
1284
    virDomainEventPtr event = NULL;
1285
    int ret = -1;
1286 1287

    if ((fd = open(path, O_RDONLY)) < 0) {
1288 1289 1290
        virReportSystemError(conn, errno,
                             _("cannot read domain image '%s'"),
                             path);
1291
        goto cleanup;
1292
    }
1293 1294 1295 1296
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
        virReportSystemError(conn, errno,
                             _("incomplete save header in '%s'"),
                             path);
1297
        goto cleanup;
1298
    }
1299
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
1300
        testError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1301
                  "%s", _("mismatched header magic"));
1302
        goto cleanup;
1303
    }
1304 1305 1306 1307
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
        virReportSystemError(conn, errno,
                             _("failed to read metadata length in '%s'"),
                             path);
1308
        goto cleanup;
1309 1310
    }
    if (len < 1 || len > 8192) {
1311
        testError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1312
                  "%s", _("length of metadata out of range"));
1313
        goto cleanup;
1314
    }
1315
    if (VIR_ALLOC_N(xml, len+1) < 0) {
1316
        virReportOOMError(conn);
1317
        goto cleanup;
1318
    }
1319 1320 1321
    if (saferead(fd, xml, len) != len) {
        virReportSystemError(conn, errno,
                             _("incomplete metdata in '%s'"), path);
1322
        goto cleanup;
1323 1324
    }
    xml[len] = '\0';
1325

1326
    testDriverLock(privconn);
1327 1328
    def = virDomainDefParseString(conn, privconn->caps, xml,
                                  VIR_DOMAIN_XML_INACTIVE);
1329
    if (!def)
1330
        goto cleanup;
1331 1332

    if ((dom = virDomainAssignDef(conn, &privconn->domains,
1333 1334 1335
                                  def)) == NULL)
        goto cleanup;

1336 1337
    dom->state = VIR_DOMAIN_RUNNING;
    dom->def->id = privconn->nextDomID++;
1338
    def = NULL;
1339 1340 1341
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1342 1343 1344 1345 1346 1347 1348
    ret = dom->def->id;

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
    if (fd != -1)
        close(fd);
1349 1350
    if (dom)
        virDomainObjUnlock(dom);
1351 1352
    if (event)
        testDomainEventQueue(privconn, event);
1353
    testDriverUnlock(privconn);
1354
    return ret;
1355 1356
}

1357 1358 1359
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
                              int flags ATTRIBUTE_UNUSED)
1360
{
1361
    testConnPtr privconn = domain->conn->privateData;
1362
    int fd = -1;
1363
    virDomainObjPtr privdom;
1364
    virDomainEventPtr event = NULL;
1365
    int ret = -1;
1366

1367
    testDriverLock(privconn);
1368 1369 1370 1371 1372
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1373
        goto cleanup;
1374
    }
1375 1376

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1377 1378 1379
        virReportSystemError(domain->conn, errno,
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
1380
        goto cleanup;
1381
    }
1382
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1383 1384 1385
        virReportSystemError(domain->conn, errno,
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
1386
        goto cleanup;
1387
    }
1388
    if (close(fd) < 0) {
1389 1390 1391
        virReportSystemError(domain->conn, errno,
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
1392
        goto cleanup;
1393
    }
1394
    privdom->state = VIR_DOMAIN_SHUTOFF;
1395 1396 1397
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_CRASHED);
1398 1399 1400
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1401
        privdom = NULL;
1402
    }
1403 1404 1405 1406 1407
    ret = 0;

cleanup:
    if (fd != -1)
        close(fd);
1408 1409
    if (privdom)
        virDomainObjUnlock(privdom);
1410 1411
    if (event)
        testDomainEventQueue(privconn, event);
1412
    testDriverUnlock(privconn);
1413
    return ret;
1414 1415
}

1416 1417 1418
static char *testGetOSType(virDomainPtr dom) {
    char *ret = strdup("linux");
    if (!ret)
1419
        virReportOOMError(dom->conn);
1420
    return ret;
1421 1422 1423
}

static unsigned long testGetMaxMemory(virDomainPtr domain) {
1424 1425
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1426
    unsigned long ret = 0;
1427

1428
    testDriverLock(privconn);
1429 1430
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1431
    testDriverUnlock(privconn);
1432 1433 1434

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1435
        goto cleanup;
1436
    }
1437

1438 1439 1440
    ret = privdom->def->maxmem;

cleanup:
1441 1442
    if (privdom)
        virDomainObjUnlock(privdom);
1443
    return ret;
1444 1445 1446 1447 1448
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
1449 1450
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1451
    int ret = -1;
1452

1453
    testDriverLock(privconn);
1454 1455
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1456
    testDriverUnlock(privconn);
1457 1458 1459

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1460
        goto cleanup;
1461
    }
1462 1463

    /* XXX validate not over host memory wrt to other domains */
1464
    privdom->def->maxmem = memory;
1465 1466 1467
    ret = 0;

cleanup:
1468 1469
    if (privdom)
        virDomainObjUnlock(privdom);
1470
    return ret;
1471 1472
}

1473 1474 1475
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
1476 1477
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1478
    int ret = -1;
1479

1480
    testDriverLock(privconn);
1481 1482
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1483
    testDriverUnlock(privconn);
1484 1485 1486

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1487
        goto cleanup;
1488
    }
1489

1490
    if (memory > privdom->def->maxmem) {
1491
        testError(domain->conn,
1492
                  VIR_ERR_INVALID_ARG, __FUNCTION__);
1493
        goto cleanup;
1494
    }
1495

1496
    privdom->def->memory = memory;
1497 1498 1499
    ret = 0;

cleanup:
1500 1501
    if (privdom)
        virDomainObjUnlock(privdom);
1502
    return ret;
1503 1504 1505 1506
}

static int testSetVcpus(virDomainPtr domain,
                        unsigned int nrCpus) {
1507 1508
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1509 1510
    int ret = -1;

1511 1512 1513 1514 1515 1516
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
1517
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1518
        goto cleanup;
1519
    }
1520

1521 1522
    /* We allow more cpus in guest than host */
    if (nrCpus > 32) {
1523
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1524
        goto cleanup;
1525
    }
1526

1527
    privdom->def->vcpus = nrCpus;
1528 1529 1530
    ret = 0;

cleanup:
1531 1532
    if (privdom)
        virDomainObjUnlock(privdom);
1533
    return ret;
1534 1535
}

1536
static char *testDomainDumpXML(virDomainPtr domain, int flags)
1537
{
1538
    testConnPtr privconn = domain->conn->privateData;
1539
    virDomainDefPtr def;
1540
    virDomainObjPtr privdom;
1541 1542
    char *ret = NULL;

1543 1544 1545 1546 1547 1548
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
1549
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1550
        goto cleanup;
1551
    }
1552

1553 1554
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
1555

1556 1557 1558 1559 1560
    ret = virDomainDefFormat(domain->conn,
                             def,
                             flags);

cleanup:
1561 1562
    if (privdom)
        virDomainObjUnlock(privdom);
1563
    return ret;
1564
}
1565

1566
static int testNumOfDefinedDomains(virConnectPtr conn) {
1567
    testConnPtr privconn = conn->privateData;
1568
    unsigned int numInactive = 0, i;
1569

1570 1571 1572
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->domains.count ; i++) {
        virDomainObjLock(privconn->domains.objs[i]);
1573
        if (!virDomainIsActive(privconn->domains.objs[i]))
1574
            numInactive++;
1575 1576 1577
        virDomainObjUnlock(privconn->domains.objs[i]);
    }
    testDriverUnlock(privconn);
1578

1579
    return numInactive;
1580 1581
}

1582 1583 1584
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
1585
    testConnPtr privconn = conn->privateData;
1586
    unsigned int n = 0, i;
1587

1588
    testDriverLock(privconn);
1589
    memset(names, 0, sizeof(*names)*maxnames);
1590 1591
    for (i = 0 ; i < privconn->domains.count && n < maxnames ; i++) {
        virDomainObjLock(privconn->domains.objs[i]);
1592
        if (!virDomainIsActive(privconn->domains.objs[i]) &&
1593 1594
            !(names[n++] = strdup(privconn->domains.objs[i]->def->name))) {
            virDomainObjUnlock(privconn->domains.objs[i]);
1595
            goto no_memory;
1596 1597 1598 1599
        }
        virDomainObjUnlock(privconn->domains.objs[i]);
    }
    testDriverUnlock(privconn);
1600

1601 1602 1603
    return n;

no_memory:
1604
    virReportOOMError(conn);
1605 1606
    for (n = 0 ; n < maxnames ; n++)
        VIR_FREE(names[n]);
1607
    testDriverUnlock(privconn);
1608
    return -1;
1609 1610
}

1611
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
1612
                                        const char *xml) {
1613
    testConnPtr privconn = conn->privateData;
1614
    virDomainPtr ret = NULL;
1615
    virDomainDefPtr def;
1616
    virDomainObjPtr dom = NULL;
1617
    virDomainEventPtr event = NULL;
1618

1619
    testDriverLock(privconn);
1620 1621
    if ((def = virDomainDefParseString(conn, privconn->caps, xml,
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1622
        goto cleanup;
1623

1624 1625
    if ((dom = virDomainAssignDef(conn, &privconn->domains,
                                  def)) == NULL) {
1626
        goto cleanup;
1627 1628 1629
    }
    dom->persistent = 1;
    dom->def->id = -1;
1630 1631 1632
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED);
1633

1634
    ret = virGetDomain(conn, def->name, def->uuid);
1635 1636 1637 1638 1639 1640
    def = NULL;
    if (ret)
        ret->id = -1;

cleanup:
    virDomainDefFree(def);
1641 1642
    if (dom)
        virDomainObjUnlock(dom);
1643 1644
    if (event)
        testDomainEventQueue(privconn, event);
1645
    testDriverUnlock(privconn);
1646
    return ret;
1647 1648
}

1649 1650 1651
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
1652
    testConnPtr privconn = conn->privateData;
1653
    int i, j;
1654
    int ret = -1;
1655

1656
    testDriverLock(privconn);
1657
    if (startCell > privconn->numCells) {
1658
        testError(conn, VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
1659
                  "%s", _("Range exceeds available cells"));
1660
        goto cleanup;
1661 1662 1663 1664 1665 1666 1667
    }

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

1670
cleanup:
1671
    testDriverUnlock(privconn);
1672
    return ret;
1673 1674 1675
}


1676
static int testDomainCreate(virDomainPtr domain) {
1677 1678
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1679
    virDomainEventPtr event = NULL;
1680
    int ret = -1;
1681

1682
    testDriverLock(privconn);
1683 1684 1685 1686 1687
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1688
        goto cleanup;
1689
    }
1690

1691
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
1692 1693
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Domain '%s' is already running"), domain->name);
1694
        goto cleanup;
1695 1696
    }

1697 1698
    domain->id = privdom->def->id = privconn->nextDomID++;
    privdom->state = VIR_DOMAIN_RUNNING;
1699 1700 1701
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
1702
    ret = 0;
1703

1704
cleanup:
1705 1706
    if (privdom)
        virDomainObjUnlock(privdom);
1707 1708
    if (event)
        testDomainEventQueue(privconn, event);
1709
    testDriverUnlock(privconn);
1710
    return ret;
1711 1712 1713
}

static int testDomainUndefine(virDomainPtr domain) {
1714 1715
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1716
    virDomainEventPtr event = NULL;
1717
    int ret = -1;
1718

1719
    testDriverLock(privconn);
1720 1721 1722 1723 1724
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1725
        goto cleanup;
1726
    }
1727

1728
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
1729 1730
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Domain '%s' is still running"), domain->name);
1731
        goto cleanup;
1732 1733
    }

1734
    privdom->state = VIR_DOMAIN_SHUTOFF;
1735 1736 1737
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
1738 1739
    virDomainRemoveInactive(&privconn->domains,
                            privdom);
1740
    privdom = NULL;
1741
    ret = 0;
1742

1743
cleanup:
1744 1745
    if (privdom)
        virDomainObjUnlock(privdom);
1746 1747
    if (event)
        testDomainEventQueue(privconn, event);
1748
    testDriverUnlock(privconn);
1749
    return ret;
1750 1751
}

1752 1753 1754
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
1755 1756
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1757
    int ret = -1;
1758

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

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1766
        goto cleanup;
1767 1768
    }

1769
    *autostart = privdom->autostart;
1770 1771 1772
    ret = 0;

cleanup:
1773 1774
    if (privdom)
        virDomainObjUnlock(privdom);
1775
    return ret;
1776 1777 1778 1779 1780 1781
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
1782 1783
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1784
    int ret = -1;
1785

1786
    testDriverLock(privconn);
1787 1788
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1789
    testDriverUnlock(privconn);
1790 1791 1792

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1793
        goto cleanup;
1794 1795
    }

1796
    privdom->autostart = autostart ? 1 : 0;
1797 1798 1799
    ret = 0;

cleanup:
1800 1801
    if (privdom)
        virDomainObjUnlock(privdom);
1802
    return ret;
1803
}
1804

1805 1806 1807
static char *testDomainGetSchedulerType(virDomainPtr domain,
                                        int *nparams)
{
1808 1809
    char *type = NULL;

1810 1811
    *nparams = 1;
    type = strdup("fair");
1812
    if (!type)
1813
        virReportOOMError(domain->conn);
1814

1815 1816 1817 1818 1819 1820 1821
    return type;
}

static int testDomainGetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int *nparams)
{
1822 1823
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1824
    int ret = -1;
1825

1826
    testDriverLock(privconn);
1827 1828
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1829
    testDriverUnlock(privconn);
1830 1831 1832

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1833
        goto cleanup;
1834 1835
    }

1836
    if (*nparams != 1) {
1837
        testError(domain->conn, VIR_ERR_INVALID_ARG, "nparams");
1838
        goto cleanup;
1839
    }
1840 1841
    strcpy(params[0].field, "weight");
    params[0].type = VIR_DOMAIN_SCHED_FIELD_UINT;
1842 1843 1844
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
    params[0].value.ui = 50;
1845 1846 1847
    ret = 0;

cleanup:
1848 1849
    if (privdom)
        virDomainObjUnlock(privdom);
1850
    return ret;
1851
}
1852 1853


1854 1855 1856 1857
static int testDomainSetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int nparams)
{
1858 1859
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1860
    int ret = -1;
1861

1862
    testDriverLock(privconn);
1863 1864
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1865
    testDriverUnlock(privconn);
1866 1867 1868

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1869
        goto cleanup;
1870 1871
    }

1872
    if (nparams != 1) {
1873
        testError(domain->conn, VIR_ERR_INVALID_ARG, "nparams");
1874
        goto cleanup;
1875
    }
1876
    if (STRNEQ(params[0].field, "weight")) {
1877
        testError(domain->conn, VIR_ERR_INVALID_ARG, "field");
1878
        goto cleanup;
1879 1880
    }
    if (params[0].type != VIR_DOMAIN_SCHED_FIELD_UINT) {
1881
        testError(domain->conn, VIR_ERR_INVALID_ARG, "type");
1882
        goto cleanup;
1883
    }
1884 1885
    /* XXX */
    /*privdom->weight = params[0].value.ui;*/
1886 1887 1888
    ret = 0;

cleanup:
1889 1890
    if (privdom)
        virDomainObjUnlock(privdom);
1891
    return ret;
1892 1893 1894
}

static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
1895
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
                                        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)
{
1913 1914
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
1915
    virNetworkPtr ret = NULL;
1916

1917 1918 1919 1920 1921
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
1922
        testError (conn, VIR_ERR_NO_NETWORK, NULL);
1923
        goto cleanup;
1924 1925
    }

1926 1927 1928
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
1929 1930
    if (net)
        virNetworkObjUnlock(net);
1931
    return ret;
1932
}
1933

1934
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
1935
                                             const char *name)
1936
{
1937
    testConnPtr privconn = conn->privateData;
1938 1939
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
1940

1941 1942 1943 1944 1945
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
1946
        testError (conn, VIR_ERR_NO_NETWORK, NULL);
1947
        goto cleanup;
1948 1949
    }

1950 1951 1952
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
1953 1954
    if (net)
        virNetworkObjUnlock(net);
1955
    return ret;
1956 1957 1958 1959
}


static int testNumNetworks(virConnectPtr conn) {
1960
    testConnPtr privconn = conn->privateData;
1961
    int numActive = 0, i;
1962

1963 1964 1965
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
1966
        if (virNetworkIsActive(privconn->networks.objs[i]))
1967
            numActive++;
1968 1969 1970
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
1971

1972
    return numActive;
1973 1974 1975
}

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

1979
    testDriverLock(privconn);
1980
    memset(names, 0, sizeof(*names)*nnames);
1981 1982
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
1983
        if (virNetworkIsActive(privconn->networks.objs[i]) &&
1984 1985
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
1986
            goto no_memory;
1987 1988 1989 1990
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
1991

1992 1993 1994
    return n;

no_memory:
1995
    virReportOOMError(conn);
1996 1997
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
1998
    testDriverUnlock(privconn);
1999
    return -1;
2000 2001 2002
}

static int testNumDefinedNetworks(virConnectPtr conn) {
2003
    testConnPtr privconn = conn->privateData;
2004
    int numInactive = 0, i;
2005

2006 2007 2008
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
2009
        if (!virNetworkIsActive(privconn->networks.objs[i]))
2010
            numInactive++;
2011 2012 2013
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2014

2015
    return numInactive;
2016 2017 2018
}

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

2022
    testDriverLock(privconn);
2023
    memset(names, 0, sizeof(*names)*nnames);
2024 2025
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
2026
        if (!virNetworkIsActive(privconn->networks.objs[i]) &&
2027 2028
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2029
            goto no_memory;
2030 2031 2032 2033
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2034

2035 2036 2037
    return n;

no_memory:
2038
    virReportOOMError(conn);
2039 2040
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2041
    testDriverUnlock(privconn);
2042
    return -1;
2043 2044 2045
}

static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
2046
    testConnPtr privconn = conn->privateData;
2047
    virNetworkDefPtr def;
2048
    virNetworkObjPtr net = NULL;
2049
    virNetworkPtr ret = NULL;
2050

2051
    testDriverLock(privconn);
2052
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
2053
        goto cleanup;
2054

2055 2056
    if ((net = virNetworkAssignDef(conn, &privconn->networks,
                                   def)) == NULL) {
2057
        goto cleanup;
2058
    }
2059
    net->active = 1;
2060 2061 2062
    def = NULL;

    ret = virGetNetwork(conn, def->name, def->uuid);
2063

2064 2065
cleanup:
    virNetworkDefFree(def);
2066 2067 2068
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
2069
    return ret;
2070 2071 2072
}

static virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml) {
2073
    testConnPtr privconn = conn->privateData;
2074
    virNetworkDefPtr def;
2075
    virNetworkObjPtr net = NULL;
2076
    virNetworkPtr ret = NULL;
2077

2078
    testDriverLock(privconn);
2079
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
2080
        goto cleanup;
2081

2082 2083
    if ((net = virNetworkAssignDef(conn, &privconn->networks,
                                   def)) == NULL) {
2084
        goto cleanup;
2085
    }
2086
    net->persistent = 1;
2087
    def = NULL;
2088

2089 2090 2091 2092
    ret = virGetNetwork(conn, def->name, def->uuid);

cleanup:
    virNetworkDefFree(def);
2093 2094 2095
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
2096
    return ret;
2097 2098 2099
}

static int testNetworkUndefine(virNetworkPtr network) {
2100 2101
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2102
    int ret = -1;
2103

2104
    testDriverLock(privconn);
2105 2106 2107 2108 2109
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2110
        goto cleanup;
2111
    }
2112

2113
    if (virNetworkIsActive(privnet)) {
2114 2115
        testError(network->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Network '%s' is still running"), network->name);
2116
        goto cleanup;
2117 2118
    }

2119 2120
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
2121
    privnet = NULL;
2122
    ret = 0;
2123

2124
cleanup:
2125 2126 2127
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
2128
    return ret;
2129 2130 2131
}

static int testNetworkStart(virNetworkPtr network) {
2132 2133
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2134
    int ret = -1;
2135

2136
    testDriverLock(privconn);
2137 2138
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2139
    testDriverUnlock(privconn);
2140 2141 2142

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2143
        goto cleanup;
2144
    }
2145

2146
    if (virNetworkIsActive(privnet)) {
2147 2148
        testError(network->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Network '%s' is already running"), network->name);
2149
        goto cleanup;
2150 2151
    }

2152
    privnet->active = 1;
2153
    ret = 0;
2154

2155
cleanup:
2156 2157
    if (privnet)
        virNetworkObjUnlock(privnet);
2158
    return ret;
2159 2160 2161
}

static int testNetworkDestroy(virNetworkPtr network) {
2162 2163
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2164
    int ret = -1;
2165

2166
    testDriverLock(privconn);
2167 2168 2169 2170 2171
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2172
        goto cleanup;
2173
    }
2174

2175 2176 2177 2178
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
2179
        privnet = NULL;
2180
    }
2181 2182 2183
    ret = 0;

cleanup:
2184 2185 2186
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
2187
    return ret;
2188 2189 2190
}

static char *testNetworkDumpXML(virNetworkPtr network, int flags ATTRIBUTE_UNUSED) {
2191 2192
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2193
    char *ret = NULL;
2194

2195
    testDriverLock(privconn);
2196 2197
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2198
    testDriverUnlock(privconn);
2199 2200 2201

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2202
        goto cleanup;
2203
    }
2204

2205 2206 2207
    ret = virNetworkDefFormat(network->conn, privnet->def);

cleanup:
2208 2209
    if (privnet)
        virNetworkObjUnlock(privnet);
2210
    return ret;
2211 2212 2213
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
2214
    testConnPtr privconn = network->conn->privateData;
2215
    char *bridge = NULL;
2216 2217
    virNetworkObjPtr privnet;

2218
    testDriverLock(privconn);
2219 2220
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2221
    testDriverUnlock(privconn);
2222 2223 2224

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2225
        goto cleanup;
2226 2227
    }

2228 2229
    if (privnet->def->bridge &&
        !(bridge = strdup(privnet->def->bridge))) {
2230
        virReportOOMError(network->conn);
2231
        goto cleanup;
2232
    }
2233 2234

cleanup:
2235 2236
    if (privnet)
        virNetworkObjUnlock(privnet);
2237 2238 2239 2240 2241
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
2242 2243
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2244
    int ret = -1;
2245

2246
    testDriverLock(privconn);
2247 2248
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2249
    testDriverUnlock(privconn);
2250 2251 2252

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2253
        goto cleanup;
2254 2255
    }

2256
    *autostart = privnet->autostart;
2257 2258 2259
    ret = 0;

cleanup:
2260 2261
    if (privnet)
        virNetworkObjUnlock(privnet);
2262
    return ret;
2263 2264 2265 2266
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
2267 2268
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2269
    int ret = -1;
2270

2271
    testDriverLock(privconn);
2272 2273
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2274
    testDriverUnlock(privconn);
2275 2276 2277

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2278
        goto cleanup;
2279 2280
    }

2281
    privnet->autostart = autostart ? 1 : 0;
2282 2283 2284
    ret = 0;

cleanup:
2285 2286
    if (privnet)
        virNetworkObjUnlock(privnet);
2287
    return ret;
2288
}
2289

C
Cole Robinson 已提交
2290 2291 2292 2293 2294

/*
 * Storage Driver routines
 */

2295 2296
static int testStoragePoolObjSetDefaults(virConnectPtr conn,
                                         virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
2297 2298 2299 2300 2301 2302 2303

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

    pool->configFile = strdup("\0");
    if (!pool->configFile) {
2304
        virReportOOMError(conn);
C
Cole Robinson 已提交
2305 2306 2307 2308 2309 2310
        return -1;
    }

    return 0;
}

2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
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;
}

C
Cole Robinson 已提交
2326 2327 2328
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
2329
    testConnPtr privconn = conn->privateData;
2330 2331
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
2332

2333
    testDriverLock(privconn);
2334
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
2335
    testDriverUnlock(privconn);
2336 2337

    if (pool == NULL) {
C
Cole Robinson 已提交
2338
        testError (conn, VIR_ERR_NO_STORAGE_POOL, NULL);
2339
        goto cleanup;
C
Cole Robinson 已提交
2340 2341
    }

2342 2343 2344
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
2345 2346
    if (pool)
        virStoragePoolObjUnlock(pool);
2347
    return ret;
C
Cole Robinson 已提交
2348 2349 2350 2351 2352
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
2353
    testConnPtr privconn = conn->privateData;
2354 2355
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
2356

2357
    testDriverLock(privconn);
2358
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
2359
    testDriverUnlock(privconn);
2360 2361

    if (pool == NULL) {
C
Cole Robinson 已提交
2362
        testError (conn, VIR_ERR_NO_STORAGE_POOL, NULL);
2363
        goto cleanup;
C
Cole Robinson 已提交
2364 2365
    }

2366 2367 2368
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
2369 2370
    if (pool)
        virStoragePoolObjUnlock(pool);
2371
    return ret;
C
Cole Robinson 已提交
2372 2373 2374 2375 2376 2377 2378 2379 2380
}

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

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

2384
    testDriverLock(privconn);
C
Cole Robinson 已提交
2385 2386 2387
    for (i = 0 ; i < privconn->pools.count ; i++)
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
2388
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
2389 2390 2391 2392 2393 2394 2395 2396

    return numActive;
}

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

2400
    testDriverLock(privconn);
C
Cole Robinson 已提交
2401
    memset(names, 0, sizeof(*names)*nnames);
2402 2403
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
2404
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
2405 2406
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
2407
            goto no_memory;
2408 2409 2410 2411
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
2412 2413 2414 2415

    return n;

no_memory:
2416
    virReportOOMError(conn);
C
Cole Robinson 已提交
2417 2418
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2419
    testDriverUnlock(privconn);
2420
    return -1;
C
Cole Robinson 已提交
2421 2422 2423 2424
}

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

2428 2429 2430
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->pools.count ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
2431 2432
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
2433 2434 2435
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
2436 2437 2438 2439 2440 2441 2442 2443

    return numInactive;
}

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

2447
    testDriverLock(privconn);
C
Cole Robinson 已提交
2448
    memset(names, 0, sizeof(*names)*nnames);
2449 2450
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
2451
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
2452 2453
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
2454
            goto no_memory;
2455 2456 2457 2458
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
2459 2460 2461 2462

    return n;

no_memory:
2463
    virReportOOMError(conn);
C
Cole Robinson 已提交
2464 2465
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2466
    testDriverUnlock(privconn);
2467
    return -1;
C
Cole Robinson 已提交
2468 2469 2470 2471
}


static int
2472
testStoragePoolStart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
2473
                     unsigned int flags ATTRIBUTE_UNUSED) {
2474 2475
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
2476
    int ret = -1;
2477

2478
    testDriverLock(privconn);
2479 2480
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
2481
    testDriverUnlock(privconn);
2482 2483 2484

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2485
        goto cleanup;
2486 2487
    }

2488 2489 2490 2491 2492
    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
2493 2494

    privpool->active = 1;
2495
    ret = 0;
C
Cole Robinson 已提交
2496

2497
cleanup:
2498 2499
    if (privpool)
        virStoragePoolObjUnlock(privpool);
2500
    return ret;
C
Cole Robinson 已提交
2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
}

static char *
testStorageFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED,
                           const char *srcSpec ATTRIBUTE_UNUSED,
                           unsigned int flags ATTRIBUTE_UNUSED)
{
    return NULL;
}


static virStoragePoolPtr
testStoragePoolCreate(virConnectPtr conn,
                      const char *xml,
                      unsigned int flags ATTRIBUTE_UNUSED) {
2517
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
2518
    virStoragePoolDefPtr def;
2519
    virStoragePoolObjPtr pool = NULL;
2520
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
2521

2522
    testDriverLock(privconn);
C
Cole Robinson 已提交
2523
    if (!(def = virStoragePoolDefParse(conn, xml, NULL)))
2524
        goto cleanup;
C
Cole Robinson 已提交
2525

2526 2527 2528 2529
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
C
Cole Robinson 已提交
2530 2531
        testError(conn, VIR_ERR_INTERNAL_ERROR,
                  "%s", _("storage pool already exists"));
2532
        goto cleanup;
C
Cole Robinson 已提交
2533 2534 2535
    }

    if (!(pool = virStoragePoolObjAssignDef(conn, &privconn->pools, def))) {
2536
        goto cleanup;
C
Cole Robinson 已提交
2537
    }
2538
    def = NULL;
C
Cole Robinson 已提交
2539

2540
    if (testStoragePoolObjSetDefaults(conn, pool) == -1) {
C
Cole Robinson 已提交
2541
        virStoragePoolObjRemove(&privconn->pools, pool);
2542 2543
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
2544 2545 2546
    }
    pool->active = 1;

2547 2548 2549 2550
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
2551 2552 2553
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
2554
    return ret;
C
Cole Robinson 已提交
2555 2556 2557 2558 2559 2560
}

static virStoragePoolPtr
testStoragePoolDefine(virConnectPtr conn,
                      const char *xml,
                      unsigned int flags ATTRIBUTE_UNUSED) {
2561
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
2562
    virStoragePoolDefPtr def;
2563
    virStoragePoolObjPtr pool = NULL;
2564
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
2565

2566
    testDriverLock(privconn);
C
Cole Robinson 已提交
2567
    if (!(def = virStoragePoolDefParse(conn, xml, NULL)))
2568
        goto cleanup;
C
Cole Robinson 已提交
2569 2570 2571 2572 2573 2574

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

    if (!(pool = virStoragePoolObjAssignDef(conn, &privconn->pools, def))) {
2575
        goto cleanup;
C
Cole Robinson 已提交
2576
    }
2577
    def = NULL;
C
Cole Robinson 已提交
2578

2579
    if (testStoragePoolObjSetDefaults(conn, pool) == -1) {
C
Cole Robinson 已提交
2580
        virStoragePoolObjRemove(&privconn->pools, pool);
2581 2582
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
2583 2584
    }

2585 2586 2587 2588
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
2589 2590 2591
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
2592
    return ret;
C
Cole Robinson 已提交
2593 2594 2595
}

static int
2596 2597 2598
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
2599
    int ret = -1;
2600

2601
    testDriverLock(privconn);
2602 2603 2604 2605 2606
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2607
        goto cleanup;
2608 2609
    }

2610 2611 2612 2613 2614
    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
2615 2616

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

2619
cleanup:
2620 2621 2622
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
2623
    return ret;
C
Cole Robinson 已提交
2624 2625 2626
}

static int
2627
testStoragePoolBuild(virStoragePoolPtr pool,
C
Cole Robinson 已提交
2628
                     unsigned int flags ATTRIBUTE_UNUSED) {
2629 2630
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
2631
    int ret = -1;
2632

2633
    testDriverLock(privconn);
2634 2635
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
2636
    testDriverUnlock(privconn);
2637 2638 2639

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2640
        goto cleanup;
2641 2642
    }

2643 2644 2645 2646 2647
    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
2648
    ret = 0;
C
Cole Robinson 已提交
2649

2650
cleanup:
2651 2652
    if (privpool)
        virStoragePoolObjUnlock(privpool);
2653
    return ret;
C
Cole Robinson 已提交
2654 2655 2656 2657
}


static int
2658 2659 2660
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
2661
    int ret = -1;
2662

2663
    testDriverLock(privconn);
2664 2665 2666 2667 2668
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2669
        goto cleanup;
2670 2671 2672 2673 2674
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), pool->name);
2675
        goto cleanup;
2676
    }
C
Cole Robinson 已提交
2677 2678 2679

    privpool->active = 0;

2680
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
2681
        virStoragePoolObjRemove(&privconn->pools, privpool);
2682 2683
        privpool = NULL;
    }
2684
    ret = 0;
C
Cole Robinson 已提交
2685

2686
cleanup:
2687 2688 2689
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
2690
    return ret;
C
Cole Robinson 已提交
2691 2692 2693 2694
}


static int
2695
testStoragePoolDelete(virStoragePoolPtr pool,
C
Cole Robinson 已提交
2696
                      unsigned int flags ATTRIBUTE_UNUSED) {
2697 2698
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
2699
    int ret = -1;
2700

2701
    testDriverLock(privconn);
2702 2703
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
2704
    testDriverUnlock(privconn);
2705 2706 2707

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2708 2709 2710 2711 2712 2713 2714
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
2715 2716
    }

2717
    ret = 0;
C
Cole Robinson 已提交
2718

2719
cleanup:
2720 2721
    if (privpool)
        virStoragePoolObjUnlock(privpool);
2722
    return ret;
C
Cole Robinson 已提交
2723 2724 2725 2726
}


static int
2727
testStoragePoolRefresh(virStoragePoolPtr pool,
C
Cole Robinson 已提交
2728
                       unsigned int flags ATTRIBUTE_UNUSED) {
2729 2730
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
2731
    int ret = -1;
2732

2733
    testDriverLock(privconn);
2734 2735
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
2736
    testDriverUnlock(privconn);
2737 2738 2739

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2740
        goto cleanup;
2741 2742 2743 2744 2745
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), pool->name);
2746
        goto cleanup;
2747
    }
2748
    ret = 0;
C
Cole Robinson 已提交
2749

2750
cleanup:
2751 2752
    if (privpool)
        virStoragePoolObjUnlock(privpool);
2753
    return ret;
C
Cole Robinson 已提交
2754 2755 2756 2757
}


static int
2758
testStoragePoolGetInfo(virStoragePoolPtr pool,
C
Cole Robinson 已提交
2759
                       virStoragePoolInfoPtr info) {
2760 2761
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
2762
    int ret = -1;
2763

2764
    testDriverLock(privconn);
2765 2766
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
2767
    testDriverUnlock(privconn);
2768 2769 2770

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2771
        goto cleanup;
2772
    }
C
Cole Robinson 已提交
2773 2774 2775 2776 2777 2778 2779 2780 2781

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

2784
cleanup:
2785 2786
    if (privpool)
        virStoragePoolObjUnlock(privpool);
2787
    return ret;
C
Cole Robinson 已提交
2788 2789 2790
}

static char *
2791
testStoragePoolDumpXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
2792
                       unsigned int flags ATTRIBUTE_UNUSED) {
2793 2794
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
2795
    char *ret = NULL;
2796

2797
    testDriverLock(privconn);
2798 2799
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
2800
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
2801

2802 2803
    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2804
        goto cleanup;
2805 2806
    }

2807 2808 2809
    ret = virStoragePoolDefFormat(pool->conn, privpool->def);

cleanup:
2810 2811
    if (privpool)
        virStoragePoolObjUnlock(privpool);
2812
    return ret;
C
Cole Robinson 已提交
2813 2814 2815
}

static int
2816
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
2817
                            int *autostart) {
2818 2819
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
2820
    int ret = -1;
2821

2822
    testDriverLock(privconn);
2823 2824
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
2825
    testDriverUnlock(privconn);
2826 2827 2828

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2829
        goto cleanup;
2830
    }
C
Cole Robinson 已提交
2831 2832 2833 2834 2835 2836

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

2839
cleanup:
2840 2841
    if (privpool)
        virStoragePoolObjUnlock(privpool);
2842
    return ret;
C
Cole Robinson 已提交
2843 2844 2845
}

static int
2846
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
2847
                            int autostart) {
2848 2849
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
2850
    int ret = -1;
2851

2852
    testDriverLock(privconn);
2853 2854
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
2855
    testDriverUnlock(privconn);
2856 2857 2858

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2859
        goto cleanup;
2860
    }
C
Cole Robinson 已提交
2861 2862

    if (!privpool->configFile) {
2863
        testError(pool->conn, VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
2864
                  "%s", _("pool has no config file"));
2865
        goto cleanup;
C
Cole Robinson 已提交
2866 2867 2868 2869
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
2870 2871 2872
    ret = 0;

cleanup:
2873 2874
    if (privpool)
        virStoragePoolObjUnlock(privpool);
2875
    return ret;
C
Cole Robinson 已提交
2876 2877 2878 2879
}


static int
2880 2881 2882
testStoragePoolNumVolumes(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
2883
    int ret = -1;
2884

2885
    testDriverLock(privconn);
2886 2887
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
2888
    testDriverUnlock(privconn);
2889 2890 2891

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2892
        goto cleanup;
2893 2894 2895 2896 2897
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), pool->name);
2898
        goto cleanup;
2899
    }
C
Cole Robinson 已提交
2900

2901 2902 2903
    ret = privpool->volumes.count;

cleanup:
2904 2905
    if (privpool)
        virStoragePoolObjUnlock(privpool);
2906
    return ret;
C
Cole Robinson 已提交
2907 2908 2909
}

static int
2910
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
2911 2912
                           char **const names,
                           int maxnames) {
2913 2914
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
C
Cole Robinson 已提交
2915 2916
    int i = 0, n = 0;

2917
    memset(names, 0, maxnames * sizeof(*names));
2918 2919

    testDriverLock(privconn);
2920 2921
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
2922
    testDriverUnlock(privconn);
2923 2924 2925

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2926
        goto cleanup;
2927 2928 2929 2930 2931 2932
    }


    if (!virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), pool->name);
2933
        goto cleanup;
2934 2935
    }

C
Cole Robinson 已提交
2936 2937
    for (i = 0 ; i < privpool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(privpool->volumes.objs[i]->name)) == NULL) {
2938
            virReportOOMError(pool->conn);
C
Cole Robinson 已提交
2939 2940 2941 2942
            goto cleanup;
        }
    }

2943
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
2944 2945 2946 2947 2948 2949
    return n;

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

2950
    memset(names, 0, maxnames * sizeof(*names));
2951 2952
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
2953 2954 2955 2956 2957
    return -1;
}


static virStorageVolPtr
2958
testStorageVolumeLookupByName(virStoragePoolPtr pool,
C
Cole Robinson 已提交
2959
                              const char *name ATTRIBUTE_UNUSED) {
2960 2961 2962
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
2963
    virStorageVolPtr ret = NULL;
2964

2965
    testDriverLock(privconn);
2966 2967
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
2968
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
2969

2970 2971
    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2972
        goto cleanup;
2973 2974 2975 2976 2977 2978
    }


    if (!virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), pool->name);
2979
        goto cleanup;
2980 2981 2982 2983 2984 2985
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
        testError(pool->conn, VIR_ERR_INVALID_STORAGE_VOL,
C
Cole Robinson 已提交
2986
                  _("no storage vol with matching name '%s'"), name);
2987
        goto cleanup;
C
Cole Robinson 已提交
2988 2989
    }

2990 2991 2992 2993
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);

cleanup:
2994 2995
    if (privpool)
        virStoragePoolObjUnlock(privpool);
2996
    return ret;
C
Cole Robinson 已提交
2997 2998 2999 3000 3001 3002
}


static virStorageVolPtr
testStorageVolumeLookupByKey(virConnectPtr conn,
                             const char *key) {
3003
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3004
    unsigned int i;
3005
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
3006

3007
    testDriverLock(privconn);
C
Cole Robinson 已提交
3008
    for (i = 0 ; i < privconn->pools.count ; i++) {
3009
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3010
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
3011
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
3012 3013
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

3014 3015 3016 3017 3018
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
3019
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
3020 3021
                break;
            }
C
Cole Robinson 已提交
3022
        }
3023
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3024
    }
3025
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3026

3027 3028 3029 3030 3031
    if (!ret)
        testError(conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching key '%s'"), key);

    return ret;
C
Cole Robinson 已提交
3032 3033 3034 3035 3036
}

static virStorageVolPtr
testStorageVolumeLookupByPath(virConnectPtr conn,
                              const char *path) {
3037
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3038
    unsigned int i;
3039
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
3040

3041
    testDriverLock(privconn);
C
Cole Robinson 已提交
3042
    for (i = 0 ; i < privconn->pools.count ; i++) {
3043
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3044
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
3045
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
3046 3047
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

3048 3049 3050 3051 3052
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
3053
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
3054 3055
                break;
            }
C
Cole Robinson 已提交
3056
        }
3057
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3058
    }
3059
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3060

3061 3062 3063 3064 3065
    if (!ret)
        testError(conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching path '%s'"), path);

    return ret;
C
Cole Robinson 已提交
3066 3067 3068
}

static virStorageVolPtr
3069
testStorageVolumeCreateXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3070 3071
                           const char *xmldesc,
                           unsigned int flags ATTRIBUTE_UNUSED) {
3072 3073
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3074 3075
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
3076

3077
    testDriverLock(privconn);
3078 3079
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3080
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3081

3082 3083
    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3084
        goto cleanup;
3085 3086 3087 3088 3089 3090
    }


    if (!virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), pool->name);
3091
        goto cleanup;
3092
    }
C
Cole Robinson 已提交
3093

3094 3095 3096

    privvol = virStorageVolDefParse(pool->conn, privpool->def, xmldesc, NULL);
    if (privvol == NULL)
3097
        goto cleanup;
3098 3099 3100

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
        testError(pool->conn, VIR_ERR_INVALID_STORAGE_POOL,
C
Cole Robinson 已提交
3101
                  "%s", _("storage vol already exists"));
3102
        goto cleanup;
C
Cole Robinson 已提交
3103 3104 3105
    }

    /* Make sure enough space */
3106
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
3107
         privpool->def->capacity) {
3108
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
3109
                  _("Not enough free space in pool for volume '%s'"),
3110
                  privvol->name);
3111
        goto cleanup;
C
Cole Robinson 已提交
3112 3113 3114 3115 3116 3117
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
3118
        virReportOOMError(pool->conn);
3119
        goto cleanup;
C
Cole Robinson 已提交
3120 3121
    }

3122 3123 3124
    if (VIR_ALLOC_N(privvol->target.path,
                    strlen(privpool->def->target.path) +
                    1 + strlen(privvol->name) + 1) < 0) {
3125
        virReportOOMError(pool->conn);
3126
        goto cleanup;
C
Cole Robinson 已提交
3127 3128
    }

3129 3130 3131 3132 3133
    strcpy(privvol->target.path, privpool->def->target.path);
    strcat(privvol->target.path, "/");
    strcat(privvol->target.path, privvol->name);
    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
3134
        virReportOOMError(pool->conn);
3135
        goto cleanup;
C
Cole Robinson 已提交
3136 3137
    }

3138
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
3139 3140 3141
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

3144 3145
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);
3146
    privvol = NULL;
3147 3148 3149

cleanup:
    virStorageVolDefFree(privvol);
3150 3151
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3152
    return ret;
C
Cole Robinson 已提交
3153 3154 3155
}

static int
3156
testStorageVolumeDelete(virStorageVolPtr vol,
C
Cole Robinson 已提交
3157
                        unsigned int flags ATTRIBUTE_UNUSED) {
3158 3159 3160
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
3161
    int i;
3162
    int ret = -1;
C
Cole Robinson 已提交
3163

3164
    testDriverLock(privconn);
3165 3166
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
3167
    testDriverUnlock(privconn);
3168 3169 3170

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3171
        goto cleanup;
3172 3173 3174 3175 3176 3177 3178 3179 3180
    }


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

    if (privvol == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching name '%s'"),
                  vol->name);
3181
        goto cleanup;
3182 3183 3184 3185 3186
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(vol->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), vol->pool);
3187
        goto cleanup;
3188 3189 3190
    }


C
Cole Robinson 已提交
3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213
    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;
        }
    }
3214
    ret = 0;
C
Cole Robinson 已提交
3215

3216
cleanup:
3217 3218
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3219
    return ret;
C
Cole Robinson 已提交
3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235
}


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
3236
testStorageVolumeGetInfo(virStorageVolPtr vol,
C
Cole Robinson 已提交
3237
                         virStorageVolInfoPtr info) {
3238 3239 3240
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
3241
    int ret = -1;
3242

3243
    testDriverLock(privconn);
3244 3245
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
3246
    testDriverUnlock(privconn);
3247 3248 3249

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3250
        goto cleanup;
3251 3252 3253 3254 3255 3256 3257 3258
    }

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

    if (privvol == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching name '%s'"),
                  vol->name);
3259
        goto cleanup;
3260 3261 3262 3263 3264
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(vol->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), vol->pool);
3265
        goto cleanup;
3266
    }
C
Cole Robinson 已提交
3267 3268 3269 3270 3271

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

3274
cleanup:
3275 3276
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3277
    return ret;
C
Cole Robinson 已提交
3278 3279 3280
}

static char *
3281
testStorageVolumeGetXMLDesc(virStorageVolPtr vol,
C
Cole Robinson 已提交
3282
                            unsigned int flags ATTRIBUTE_UNUSED) {
3283 3284 3285
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
3286
    char *ret = NULL;
3287

3288
    testDriverLock(privconn);
3289 3290
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
3291
    testDriverUnlock(privconn);
3292 3293 3294

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3295
        goto cleanup;
3296 3297 3298 3299 3300 3301 3302 3303
    }

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

    if (privvol == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching name '%s'"),
                  vol->name);
3304
        goto cleanup;
3305
    }
C
Cole Robinson 已提交
3306

3307 3308 3309
    if (!virStoragePoolObjIsActive(privpool)) {
        testError(vol->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), vol->pool);
3310
        goto cleanup;
3311 3312
    }

3313 3314 3315
    ret = virStorageVolDefFormat(vol->conn, privpool->def, privvol);

cleanup:
3316 3317
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3318
    return ret;
C
Cole Robinson 已提交
3319 3320 3321
}

static char *
3322 3323 3324 3325
testStorageVolumeGetPath(virStorageVolPtr vol) {
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
3326
    char *ret = NULL;
3327

3328
    testDriverLock(privconn);
3329 3330
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
3331
    testDriverUnlock(privconn);
3332 3333 3334

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3335
        goto cleanup;
3336 3337 3338 3339 3340 3341 3342 3343
    }

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

    if (privvol == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching name '%s'"),
                  vol->name);
3344
        goto cleanup;
3345 3346 3347 3348 3349
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(vol->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), vol->pool);
3350
        goto cleanup;
3351 3352
    }

C
Cole Robinson 已提交
3353
    ret = strdup(privvol->target.path);
3354
    if (ret == NULL)
3355
        virReportOOMError(vol->conn);
3356 3357

cleanup:
3358 3359
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
3360 3361 3362
    return ret;
}

3363

3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379
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;
}


3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476
static int
testDomainEventRegister (virConnectPtr conn,
                         virConnectDomainEventCallback callback,
                         void *opaque,
                         virFreeCallback freecb)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
    ret = virDomainEventCallbackListAdd(conn, driver->domainEventCallbacks,
                                        callback, opaque, freecb);
    testDriverUnlock(driver);

    return ret;
}

static int
testDomainEventDeregister (virConnectPtr conn,
                           virConnectDomainEventCallback callback)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
    if (driver->domainEventDispatching)
        ret = virDomainEventCallbackListMarkDelete(conn, driver->domainEventCallbacks,
                                                   callback);
    else
        ret = virDomainEventCallbackListRemove(conn, driver->domainEventCallbacks,
                                               callback);
    testDriverUnlock(driver);

    return ret;
}

static void testDomainEventDispatchFunc(virConnectPtr conn,
                                        virDomainEventPtr event,
                                        virConnectDomainEventCallback cb,
                                        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;
    virDomainEventQueue tempQueue;

    testDriverLock(driver);

    driver->domainEventDispatching = 1;

    /* Copy the queue, so we're reentrant safe */
    tempQueue.count = driver->domainEventQueue->count;
    tempQueue.events = driver->domainEventQueue->events;
    driver->domainEventQueue->count = 0;
    driver->domainEventQueue->events = NULL;

    virEventUpdateTimeout(driver->domainEventTimer, -1);
    virDomainEventQueueDispatch(&tempQueue,
                                driver->domainEventCallbacks,
                                testDomainEventDispatchFunc,
                                driver);

    /* Purge any deleted callbacks */
    virDomainEventCallbackListPurgeMarked(driver->domainEventCallbacks);

    driver->domainEventDispatching = 0;
    testDriverUnlock(driver);
}


/* driver must be locked before calling */
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event)
{
    if (driver->domainEventTimer < 0) {
        virDomainEventFree(event);
        return;
    }

    if (virDomainEventQueuePush(driver->domainEventQueue,
                                event) < 0)
        virDomainEventFree(event);

    if (driver->domainEventQueue->count == 1)
        virEventUpdateTimeout(driver->domainEventTimer, 0);
}


3477 3478 3479 3480 3481
static virDriver testDriver = {
    VIR_DRV_TEST,
    "Test",
    testOpen, /* open */
    testClose, /* close */
3482
    NULL, /* supports_feature */
3483 3484 3485
    NULL, /* type */
    testGetVersion, /* version */
    testGetHostname, /* hostname */
3486
    NULL, /* URI */
3487 3488 3489 3490 3491
    testGetMaxVCPUs, /* getMaxVcpus */
    testNodeGetInfo, /* nodeGetInfo */
    testGetCapabilities, /* getCapabilities */
    testListDomains, /* listDomains */
    testNumOfDomains, /* numOfDomains */
3492
    testDomainCreateXML, /* domainCreateXML */
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
    testLookupDomainByID, /* domainLookupByID */
    testLookupDomainByUUID, /* domainLookupByUUID */
    testLookupDomainByName, /* domainLookupByName */
    testPauseDomain, /* domainSuspend */
    testResumeDomain, /* domainResume */
    testShutdownDomain, /* domainShutdown */
    testRebootDomain, /* domainReboot */
    testDestroyDomain, /* domainDestroy */
    testGetOSType, /* domainGetOSType */
    testGetMaxMemory, /* domainGetMaxMemory */
    testSetMaxMemory, /* domainSetMaxMemory */
    testSetMemory, /* domainSetMemory */
    testGetDomainInfo, /* domainGetInfo */
    testDomainSave, /* domainSave */
    testDomainRestore, /* domainRestore */
    testDomainCoreDump, /* domainCoreDump */
    testSetVcpus, /* domainSetVcpus */
    NULL, /* domainPinVcpu */
    NULL, /* domainGetVcpus */
    NULL, /* domainGetMaxVcpus */
    testDomainDumpXML, /* domainDumpXML */
    testListDefinedDomains, /* listDefinedDomains */
    testNumOfDefinedDomains, /* numOfDefinedDomains */
    testDomainCreate, /* domainCreate */
    testDomainDefineXML, /* domainDefineXML */
    testDomainUndefine, /* domainUndefine */
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
    testDomainGetAutostart, /* domainGetAutostart */
    testDomainSetAutostart, /* domainSetAutostart */
    testDomainGetSchedulerType, /* domainGetSchedulerType */
    testDomainGetSchedulerParams, /* domainGetSchedulerParameters */
    testDomainSetSchedulerParams, /* domainSetSchedulerParameters */
3526 3527 3528
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
3529 3530
    NULL, /* domainBlockStats */
    NULL, /* domainInterfaceStats */
R
Richard W.M. Jones 已提交
3531
    NULL, /* domainBlockPeek */
R
Richard W.M. Jones 已提交
3532
    NULL, /* domainMemoryPeek */
3533
    testNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
3534
    NULL, /* getFreeMemory */
3535 3536
    testDomainEventRegister, /* domainEventRegister */
    testDomainEventDeregister, /* domainEventDeregister */
D
Daniel Veillard 已提交
3537 3538
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561
};

static virNetworkDriver testNetworkDriver = {
    "Test",
    testOpenNetwork, /* open */
    testCloseNetwork, /* close */
    testNumNetworks, /* numOfNetworks */
    testListNetworks, /* listNetworks */
    testNumDefinedNetworks, /* numOfDefinedNetworks */
    testListDefinedNetworks, /* listDefinedNetworks */
    testLookupNetworkByUUID, /* networkLookupByUUID */
    testLookupNetworkByName, /* networkLookupByName */
    testNetworkCreate, /* networkCreateXML */
    testNetworkDefine, /* networkDefineXML */
    testNetworkUndefine, /* networkUndefine */
    testNetworkStart, /* networkCreate */
    testNetworkDestroy, /* networkDestroy */
    testNetworkDumpXML, /* networkDumpXML */
    testNetworkGetBridgeName, /* networkGetBridgeName */
    testNetworkGetAutostart, /* networkGetAutostart */
    testNetworkSetAutostart, /* networkSetAutostart */
};

3562 3563 3564 3565
static virStorageDriver testStorageDriver = {
    .name = "Test",
    .open = testStorageOpen,
    .close = testStorageClose,
C
Cole Robinson 已提交
3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597

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

    .volLookupByName = testStorageVolumeLookupByName,
    .volLookupByKey = testStorageVolumeLookupByKey,
    .volLookupByPath = testStorageVolumeLookupByPath,
    .volCreateXML = testStorageVolumeCreateXML,
    .volDelete = testStorageVolumeDelete,
    .volGetInfo = testStorageVolumeGetInfo,
    .volGetXMLDesc = testStorageVolumeGetXMLDesc,
    .volGetPath = testStorageVolumeGetPath,
3598 3599
};

3600 3601 3602 3603 3604 3605 3606 3607
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
    .open = testDevMonOpen,
    .close = testDevMonClose,
};



3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
3620 3621
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
3622 3623 3624
    if (virRegisterDeviceMonitor(&testDevMonitor) < 0)
        return -1;

3625 3626
    return 0;
}