test.c 46.0 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
#ifdef WITH_TEST
27

28 29 30
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
31 32
#include <fcntl.h>
#include <unistd.h>
33 34
#include <sys/stat.h>

35
#include "socketcompat.h"
36 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 45
#include "domain_conf.h"
#include "xml.h"
46

47 48 49 50 51 52 53 54 55 56 57
#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
58

59 60 61
struct _testConn {
    char path[PATH_MAX];
    int nextDomID;
62
    virCapsPtr caps;
63
    virNodeInfo nodeInfo;
64
    virDomainObjPtr domains;
65
    virNetworkObjPtr networks;
66 67
    int numCells;
    testCell cells[MAX_CELLS];
68 69 70
};
typedef struct _testConn testConn;
typedef struct _testConn *testConnPtr;
71

72
#define TEST_MODEL "i686"
73
#define TEST_MODEL_WORDSIZE 32
74
#define TEST_EMULATOR "/usr/bin/test-hv"
75

76
static const virNodeInfo defaultNodeInfo = {
77
    TEST_MODEL,
78 79 80 81 82 83 84
    1024*1024*3, /* 3 GB */
    16,
    1400,
    2,
    2,
    2,
    2,
85 86
};

87 88
#define GET_DOMAIN(dom, ret)                                            \
    testConnPtr privconn;                                               \
89
    virDomainObjPtr privdom;                                            \
90 91
                                                                        \
    privconn = (testConnPtr)dom->conn->privateData;                     \
92 93 94 95 96 97 98 99
    do {                                                                \
        if ((privdom = virDomainFindByName(privconn->domains,           \
                                            (dom)->name)) == NULL) {    \
            testError((dom)->conn, (dom), NULL, VIR_ERR_INVALID_ARG,    \
                      __FUNCTION__);                                    \
            return (ret);                                               \
        }                                                               \
    } while (0)
100 101 102

#define GET_NETWORK(net, ret)                                           \
    testConnPtr privconn;                                               \
103
    virNetworkObjPtr privnet;                                           \
104 105
                                                                        \
    privconn = (testConnPtr)net->conn->privateData;                     \
106 107 108 109 110 111 112 113
    do {                                                                \
        if ((privnet = virNetworkFindByName(privconn->networks,         \
                                            (net)->name)) == NULL) {    \
            testError((net)->conn, NULL, (net), VIR_ERR_INVALID_ARG,    \
                      __FUNCTION__);                                    \
            return (ret);                                               \
        }                                                               \
    } while (0)
114

115
#define GET_CONNECTION(conn)                                            \
116 117 118 119 120
    testConnPtr privconn;                                               \
                                                                        \
    privconn = (testConnPtr)conn->privateData;


121 122
static void
testError(virConnectPtr con,
123
          virDomainPtr dom,
124
          virNetworkPtr net,
125 126
          virErrorNumber error,
          const char *info)
127
{
128
    const char *errmsg;
129

130 131
    if (error == VIR_ERR_OK)
        return;
132

133
    errmsg = __virErrorMsg(error, info);
134
    __virRaiseError(con, dom, net, VIR_FROM_TEST, error, VIR_ERR_ERROR,
135
                    errmsg, info, NULL, 0, 0, errmsg, info, 0);
136 137
}

138 139 140 141 142 143 144
static virCapsPtr
testBuildCapabilities(virConnectPtr conn) {
    virCapsPtr caps;
    virCapsGuestPtr guest;
    const char *const guest_types[] = { "hvm", "xen" };
    int i;
    GET_CONNECTION(conn);
145

146 147
    if ((caps = virCapabilitiesNew(TEST_MODEL, 0, 0)) == NULL)
        goto no_memory;
148

149 150 151 152
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
        goto no_memory;
    if (virCapabilitiesAddHostFeature(caps ,"nonpae") < 0)
        goto no_memory;
153

154 155 156 157
    for (i = 0; i < privconn->numCells; i++) {
        if (virCapabilitiesAddHostNUMACell(caps, i, privconn->cells[i].numCpus,
                                           privconn->cells[i].cpus) < 0)
            goto no_memory;
158 159
    }

160 161 162 163 164 165 166 167 168 169
    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;
170

171 172 173 174 175 176 177
        if (virCapabilitiesAddGuestDomain(guest,
                                          "test",
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
            goto no_memory;
178

179 180 181 182
        if (virCapabilitiesAddGuestFeature(guest, "pae", 1, 1) == NULL)
            goto no_memory;
        if (virCapabilitiesAddGuestFeature(guest ,"nonpae", 1, 1) == NULL)
            goto no_memory;
183 184
    }

185
    return caps;
186

187 188 189 190
no_memory:
    testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
    virCapabilitiesFree(caps);
    return NULL;
191 192
}

193

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


206 207 208 209 210 211 212 213 214 215 216
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>";
217 218


219
static int testOpenDefault(virConnectPtr conn) {
220 221
    int u;
    struct timeval tv;
222
    testConnPtr privconn;
223 224 225 226
    virDomainDefPtr domdef = NULL;
    virDomainObjPtr domobj = NULL;
    virNetworkDefPtr netdef = NULL;
    virNetworkObjPtr netobj = NULL;
227

228
    if (VIR_ALLOC(privconn) < 0) {
229 230 231
        testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "testConn");
        return VIR_DRV_OPEN_ERROR;
    }
232
    conn->privateData = privconn;
233 234

    if (gettimeofday(&tv, NULL) < 0) {
235
        testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
236
        goto error;
237 238
    }

239
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
240

241 242 243 244 245 246 247 248 249 250
    // 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;
    }

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;

    privconn->nextDomID = 1;

    if (!(domdef = virDomainDefParseString(conn, privconn->caps, defaultDomainXML)))
        goto error;
    if (!(domobj = virDomainAssignDef(conn, &privconn->domains, domdef))) {
        virDomainDefFree(domdef);
        goto error;
    }
    domobj->def->id = 1;
    domobj->state = VIR_DOMAIN_RUNNING;
    domobj->persistent = 1;

    if (!(netdef = virNetworkDefParseString(conn, defaultNetworkXML)))
        goto error;
    if (!(netobj = virNetworkAssignDef(conn, &privconn->networks, netdef))) {
        virNetworkDefFree(netdef);
        goto error;
    }

    netobj->active = 1;
    netobj->persistent = 1;

    return VIR_DRV_OPEN_SUCCESS;

error:
    virDomainObjFree(privconn->domains);
    virNetworkObjFree(privconn->networks);
    virCapabilitiesFree(privconn->caps);
    VIR_FREE(privconn);
    return VIR_DRV_OPEN_ERROR;
284 285 286 287
}


static char *testBuildFilename(const char *relativeTo,
288 289 290 291 292 293 294 295
                               const char *filename) {
    char *offset;
    int baseLen;
    if (!filename || filename[0] == '\0')
        return (NULL);
    if (filename[0] == '/')
        return strdup(filename);

296
    offset = strrchr(relativeTo, '/');
297
    if ((baseLen = (offset-relativeTo+1))) {
298 299 300
        char *absFile;
        if (VIR_ALLOC_N(absFile, baseLen + strlen(filename) + 1) < 0)
            return NULL;
301 302 303 304 305 306 307
        strncpy(absFile, relativeTo, baseLen);
        absFile[baseLen] = '\0';
        strcat(absFile, filename);
        return absFile;
    } else {
        return strdup(filename);
    }
308 309 310
}

static int testOpenFromFile(virConnectPtr conn,
311
                            const char *file) {
312
    int fd = -1, i, ret;
313 314
    long l;
    char *str;
315
    xmlDocPtr xml = NULL;
316
    xmlNodePtr root = NULL;
317
    xmlNodePtr *domains, *networks = NULL;
318 319
    xmlXPathContextPtr ctxt = NULL;
    virNodeInfoPtr nodeInfo;
320 321
    virNetworkObjPtr net;
    virDomainObjPtr dom;
322 323
    testConnPtr privconn;
    if (VIR_ALLOC(privconn) < 0) {
324 325 326
        testError(NULL, NULL, NULL, VIR_ERR_NO_MEMORY, "testConn");
        return VIR_DRV_OPEN_ERROR;
    }
327 328 329 330
    conn->privateData = privconn;

    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;
331 332

    if ((fd = open(file, O_RDONLY)) < 0) {
333 334
        testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("loading host definition file"));
        goto error;
335 336
    }

337 338 339
    if (!(xml = xmlReadFd(fd, file, NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
340
        testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("host"));
341
        goto error;
342
    }
343 344
    close(fd);
    fd = -1;
345

346 347
    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
348
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node"));
349
        goto error;
350 351
    }

352 353
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
354
        testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("creating xpath context"));
355
        goto error;
356
    }
357

358
    privconn->nextDomID = 1;
359
    privconn->numCells = 0;
360 361 362 363 364
    strncpy(privconn->path, file, PATH_MAX-1);
    privconn->path[PATH_MAX-1] = '\0';
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    nodeInfo = &privconn->nodeInfo;
365 366 367 368
    ret = virXPathLong("string(/node/cpu/nodes[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->nodes = l;
    } else if (ret == -2) {
369 370
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu numa nodes"));
        goto error;
371
    }
372

373 374 375 376
    ret = virXPathLong("string(/node/cpu/sockets[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->sockets = l;
    } else if (ret == -2) {
377 378
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu sockets"));
        goto error;
379
    }
380

381 382 383 384
    ret = virXPathLong("string(/node/cpu/cores[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->cores = l;
    } else if (ret == -2) {
385 386
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu cores"));
        goto error;
387 388
    }

389 390 391 392
    ret = virXPathLong("string(/node/cpu/threads[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
393 394
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu threads"));
        goto error;
395
    }
396

397
    nodeInfo->cpus = nodeInfo->cores * nodeInfo->threads * nodeInfo->sockets * nodeInfo->nodes;
398 399 400
    ret = virXPathLong("string(/node/cpu/active[1])", ctxt, &l);
    if (ret == 0) {
        if (l < nodeInfo->cpus) {
401 402
            nodeInfo->cpus = l;
        }
403
    } else if (ret == -2) {
404 405
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node active cpu"));
        goto error;
406 407 408 409 410
    }
    ret = virXPathLong("string(/node/cpu/mhz[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->mhz = l;
    } else if (ret == -2) {
411 412
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu mhz"));
        goto error;
413 414 415 416 417
    }

    str = virXPathString("string(/node/cpu/model[1])", ctxt);
    if (str != NULL) {
        strncpy(nodeInfo->model, str, sizeof(nodeInfo->model)-1);
418
        nodeInfo->model[sizeof(nodeInfo->model)-1] = '\0';
419
        VIR_FREE(str);
420 421
    }

422 423 424 425
    ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
426 427
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node memory"));
        goto error;
428
    }
429

430 431
    ret = virXPathNodeSet("/node/domain", ctxt, &domains);
    if (ret < 0) {
432
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node domain list"));
433
        goto error;
434
    }
435

436
    for (i = 0 ; i < ret ; i++) {
437 438 439 440 441 442 443 444 445 446
        virDomainDefPtr def;
        char *relFile = virXMLPropString(domains[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
                testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("resolving domain filename"));
                goto error;
            }
            def = virDomainDefParseFile(conn, privconn->caps, absFile);
447
            VIR_FREE(absFile);
448 449 450 451 452 453 454 455 456
            if (!def)
                goto error;
        } else {
            if ((def = virDomainDefParseNode(conn, privconn->caps, xml, domains[i])) == NULL)
                goto error;
        }

        if (!(dom = virDomainAssignDef(conn, &privconn->domains, def))) {
            virDomainDefFree(def);
457 458
            goto error;
        }
459 460 461 462

        dom->state = VIR_DOMAIN_RUNNING;
        dom->def->id = privconn->nextDomID++;
        dom->persistent = 1;
463
    }
464
    if (domains != NULL) {
465
        VIR_FREE(domains);
466 467 468 469
        domains = NULL;
    }

    ret = virXPathNodeSet("/node/network", ctxt, &networks);
470 471 472 473 474 475 476 477 478 479
    if (ret < 0) {
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node network list"));
        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);
480 481 482 483
            if (!absFile) {
                testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("resolving network filename"));
                goto error;
            }
484 485

            def = virNetworkDefParseFile(conn, absFile);
486
            VIR_FREE(absFile);
487 488 489 490 491
            if (!def)
                goto error;
        } else {
            if ((def = virNetworkDefParseNode(conn, xml, networks[i])) == NULL)
                goto error;
492
        }
493 494 495 496
        if (!(net = virNetworkAssignDef(conn, &privconn->networks,
                                        def))) {
            virNetworkDefFree(def);
            goto error;
497
        }
498 499 500 501 502 503

        net->persistent = 1;
    }
    if (networks != NULL) {
        VIR_FREE(networks);
        networks = NULL;
504
    }
505

J
Jim Meyering 已提交
506
    xmlXPathFreeContext(ctxt);
507
    xmlFreeDoc(xml);
508

509
    return (0);
510 511

 error:
J
Jim Meyering 已提交
512
    xmlXPathFreeContext(ctxt);
513
    xmlFreeDoc(xml);
514 515
    VIR_FREE(domains);
    VIR_FREE(networks);
516 517
    if (fd != -1)
        close(fd);
518 519 520 521 522 523 524 525 526 527 528 529
    dom = privconn->domains;
    while (dom) {
        virDomainObjPtr tmp = dom->next;
        virDomainObjFree(dom);
        dom = tmp;
    }
    net = privconn->networks;
    while (net) {
        virNetworkObjPtr tmp = net->next;
        virNetworkObjFree(net);
        net = tmp;
    }
530
    VIR_FREE(privconn);
531
    conn->privateData = NULL;
532
    return VIR_DRV_OPEN_ERROR;
533 534
}

535

536
static int testOpen(virConnectPtr conn,
537
                    xmlURIPtr uri,
538
                    virConnectAuthPtr auth ATTRIBUTE_UNUSED,
539
                    int flags ATTRIBUTE_UNUSED)
540
{
541
    int ret;
542

543
    if (!uri)
544
        return VIR_DRV_OPEN_DECLINED;
545

546
    if (!uri->scheme || STRNEQ(uri->scheme, "test"))
547
        return VIR_DRV_OPEN_DECLINED;
548

549
    /* Remote driver should handle these. */
550
    if (uri->server)
551 552
        return VIR_DRV_OPEN_DECLINED;

553
    if (uri->server)
554 555
        return VIR_DRV_OPEN_DECLINED;

556 557 558 559
    /* From this point on, the connection is for us. */
    if (!uri->path
        || uri->path[0] == '\0'
        || (uri->path[0] == '/' && uri->path[1] == '\0')) {
560
        testError (NULL, NULL, NULL, VIR_ERR_INVALID_ARG,
561 562 563
                   _("testOpen: supply a path or use test:///default"));
        return VIR_DRV_OPEN_ERROR;
    }
564

565 566 567
    if (STREQ(uri->path, "/default"))
        ret = testOpenDefault(conn);
    else
568 569 570 571
        ret = testOpenFromFile(conn,
                               uri->path);

    return (ret);
572 573
}

574
static int testClose(virConnectPtr conn)
575
{
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    virDomainObjPtr dom;
    virNetworkObjPtr net;
    GET_CONNECTION(conn);
    virCapabilitiesFree(privconn->caps);
    dom = privconn->domains;
    while (dom) {
        virDomainObjPtr tmp = dom->next;
        virDomainObjFree(dom);
        dom = tmp;
    }
    net = privconn->networks;
    while (net) {
        virNetworkObjPtr tmp = net->next;
        virNetworkObjFree(net);
        net = tmp;
    }
592
    VIR_FREE (privconn);
593
    conn->privateData = conn;
594
    return 0;
595 596
}

597 598
static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                          unsigned long *hvVer)
599
{
600 601
    *hvVer = 2;
    return (0);
602 603
}

604
static char *testGetHostname (virConnectPtr conn)
605 606 607 608 609 610
{
    int r;
    char hostname [HOST_NAME_MAX+1], *str;

    r = gethostname (hostname, HOST_NAME_MAX+1);
    if (r == -1) {
611
        testError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, strerror (errno));
612 613 614 615
        return NULL;
    }
    str = strdup (hostname);
    if (str == NULL) {
616
        testError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, strerror (errno));
617 618 619 620 621
        return NULL;
    }
    return str;
}

622
static char * testGetURI (virConnectPtr conn)
623 624
{
    char *uri;
625
    GET_CONNECTION(conn);
626

627 628
    if (asprintf (&uri, "test://%s", privconn->path) == -1) {
        testError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, strerror (errno));
629 630 631 632 633
        return NULL;
    }
    return uri;
}

634 635 636 637 638 639 640 641
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
642
{
643
    GET_CONNECTION(conn);
644
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
645
    return (0);
646 647
}

648
static char *testGetCapabilities (virConnectPtr conn)
649
{
650
    char *xml;
651
    GET_CONNECTION(conn);
652

653 654 655
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL) {
        testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
        return NULL;
656
    }
657 658

    return xml;
659 660
}

661
static int testNumOfDomains(virConnectPtr conn)
662
{
663 664 665
    int numActive = 0;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
666

667 668 669 670 671
    dom = privconn->domains;
    while (dom) {
        if (virDomainIsActive(dom))
            numActive++;
        dom = dom->next;
672
    }
673
    return numActive;
674 675
}

676
static virDomainPtr
677
testDomainCreateLinux(virConnectPtr conn, const char *xml,
678
                      unsigned int flags ATTRIBUTE_UNUSED)
679
{
680 681 682 683
    virDomainPtr ret;
    virDomainDefPtr def;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
684

685 686
    if ((def = virDomainDefParseString(conn, privconn->caps, xml)) == NULL)
        return NULL;
687

688 689 690 691
    if ((dom = virDomainAssignDef(conn, &privconn->domains,
                                  def)) == NULL) {
        virDomainDefFree(def);
        return NULL;
692
    }
693 694
    dom->state = VIR_DOMAIN_RUNNING;
    dom->def->id = privconn->nextDomID++;
695

696 697 698 699 700
    ret = virGetDomain(conn, def->name, def->uuid);
    if (!ret)
        return NULL;
    ret->id = def->id;
    return ret;
701 702 703
}


704 705
static virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                         int id)
706
{
707 708 709
    virDomainObjPtr dom = NULL;
    virDomainPtr ret;
    GET_CONNECTION(conn);
710

711
    if ((dom = virDomainFindByID(privconn->domains, id)) == NULL) {
712
        testError (conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
713
        return NULL;
714 715
    }

716 717 718 719 720
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (!ret)
        return NULL;
    ret->id = dom->def->id;
    return ret;
721 722
}

723 724
static virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
725
{
726 727 728
    virDomainPtr ret;
    virDomainObjPtr dom = NULL;
    GET_CONNECTION(conn);
729

730
    if ((dom = virDomainFindByUUID(privconn->domains, uuid)) == NULL) {
731
        testError (conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
732
        return NULL;
733
    }
734

735 736 737 738 739
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (!ret)
        return NULL;
    ret->id = dom->def->id;
    return ret;
740 741
}

742 743
static virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                           const char *name)
744
{
745 746 747
    virDomainPtr ret;
    virDomainObjPtr dom = NULL;
    GET_CONNECTION(conn);
748

749
    if ((dom = virDomainFindByName(privconn->domains, name)) == NULL) {
750
        testError (conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
751
        return NULL;
752
    }
753

754 755 756 757 758
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (!ret)
        return NULL;
    ret->id = dom->def->id;
    return ret;
759 760
}

761 762 763
static int testListDomains (virConnectPtr conn,
                            int *ids,
                            int maxids)
764
{
765 766 767
    int n = 0;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
768

769 770 771 772 773
    dom = privconn->domains;
    while (dom && n < maxids) {
        if (virDomainIsActive(dom))
            ids[n++] = dom->def->id;
        dom = dom->next;
774
    }
775
    return n;
776 777
}

778
static int testDestroyDomain (virDomainPtr domain)
779
{
780
    GET_DOMAIN(domain, -1);
781

782 783 784 785
    privdom->state = VIR_DOMAIN_SHUTOFF;
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
786
    }
787
    return (0);
788 789
}

790
static int testResumeDomain (virDomainPtr domain)
791
{
792
    GET_DOMAIN(domain, -1);
793

794 795 796
    if (privdom->state != VIR_DOMAIN_PAUSED) {
        testError(domain->conn, domain, NULL,
                  VIR_ERR_INTERNAL_ERROR, _("domain not paused"));
797 798
        return -1;
    }
799

800
    privdom->state = VIR_DOMAIN_RUNNING;
801
    return (0);
802 803
}

804
static int testPauseDomain (virDomainPtr domain)
805
{
806
    GET_DOMAIN(domain, -1);
807

808 809 810 811
    if (privdom->state == VIR_DOMAIN_SHUTOFF ||
        privdom->state == VIR_DOMAIN_PAUSED) {
        testError(domain->conn, domain, NULL,
                  VIR_ERR_INTERNAL_ERROR, _("domain not running"));
812 813
        return -1;
    }
814

815
    privdom->state = VIR_DOMAIN_PAUSED;
816
    return (0);
817 818
}

819
static int testShutdownDomain (virDomainPtr domain)
820
{
821
    GET_DOMAIN(domain, -1);
822

823
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
824 825
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, "domain not running");
        return -1;
826
    }
827

828
    privdom->state = VIR_DOMAIN_SHUTOFF;
829
    domain->id = -1;
830
    privdom->def->id = -1;
831 832

    return (0);
833 834 835
}

/* Similar behaviour as shutdown */
836 837
static int testRebootDomain (virDomainPtr domain,
                             unsigned int action ATTRIBUTE_UNUSED)
838
{
839
    GET_DOMAIN(domain, -1);
840

841 842 843 844
    privdom->state = VIR_DOMAIN_SHUTDOWN;
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
        privdom->state = VIR_DOMAIN_SHUTOFF;
845
        domain->id = -1;
846
        privdom->def->id = -1;
847 848
        break;

849 850
    case VIR_DOMAIN_LIFECYCLE_RESTART:
        privdom->state = VIR_DOMAIN_RUNNING;
851 852
        break;

853 854
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
        privdom->state = VIR_DOMAIN_SHUTOFF;
855
        domain->id = -1;
856
        privdom->def->id = -1;
857 858
        break;

859 860
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
        privdom->state = VIR_DOMAIN_RUNNING;
861
        break;
862

863
    default:
864
        privdom->state = VIR_DOMAIN_SHUTOFF;
865
        domain->id = -1;
866
        privdom->def->id = -1;
867 868
        break;
    }
869

870
    return (0);
871 872
}

873 874
static int testGetDomainInfo (virDomainPtr domain,
                              virDomainInfoPtr info)
875
{
876
    struct timeval tv;
877
    GET_DOMAIN(domain, -1);
878 879

    if (gettimeofday(&tv, NULL) < 0) {
880
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
881 882 883
        return (-1);
    }

884 885 886 887 888
    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));
889
    return (0);
890 891
}

892
static char *testDomainDumpXML(virDomainPtr domain, int flags);
893

894 895 896 897 898 899 900 901
#define TEST_SAVE_MAGIC "TestGuestMagic"

static int testDomainSave(virDomainPtr domain,
                          const char *path)
{
    char *xml;
    int fd, len;
    GET_DOMAIN(domain, -1);
902

903
    xml = testDomainDumpXML(domain, 0);
904 905
    if (xml == NULL) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
906
                  _("cannot allocate space for metadata"));
907 908
        return (-1);
    }
909 910 911

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
912
                  _("cannot save domain"));
913 914
        return (-1);
    }
915
    len = strlen(xml);
916
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
917
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
918
                  _("cannot write header"));
919 920 921
        close(fd);
        return (-1);
    }
922
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
923
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
924
                  _("cannot write metadata length"));
925 926 927
        close(fd);
        return (-1);
    }
928
    if (safewrite(fd, xml, len) < 0) {
929
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
930
                  _("cannot write metadata"));
931
        VIR_FREE(xml);
932 933 934
        close(fd);
        return (-1);
    }
935
    VIR_FREE(xml);
936 937
    if (close(fd) < 0) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
938
                  _("cannot save domain data"));
939 940 941
        close(fd);
        return (-1);
    }
942 943 944 945
    privdom->state = VIR_DOMAIN_SHUTOFF;
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
946 947
    }
    return 0;
948 949
}

950 951
static int testDomainRestore(virConnectPtr conn,
                             const char *path)
952
{
953 954
    char *xml;
    char magic[15];
955 956 957 958
    int fd, len;
    virDomainDefPtr def;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
959 960 961

    if ((fd = open(path, O_RDONLY)) < 0) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
962
                  _("cannot read domain image"));
963 964
        return (-1);
    }
965 966
    if (read(fd, magic, sizeof(magic)) != sizeof(magic)) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
967
                  _("incomplete save header"));
968
        close(fd);
969 970
        return (-1);
    }
971 972
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
973
                  _("mismatched header magic"));
974 975 976 977 978
        close(fd);
        return (-1);
    }
    if (read(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
979
                  _("failed to read metadata length"));
980 981 982 983 984
        close(fd);
        return (-1);
    }
    if (len < 1 || len > 8192) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
985
                  _("length of metadata out of range"));
986 987 988
        close(fd);
        return (-1);
    }
989
    if (VIR_ALLOC_N(xml, len+1) < 0) {
990 991 992 993 994 995
        testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "xml");
        close(fd);
        return (-1);
    }
    if (read(fd, xml, len) != len) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
996
                  _("incomplete metdata"));
997 998 999 1000 1001
        close(fd);
        return (-1);
    }
    xml[len] = '\0';
    close(fd);
1002 1003

    def = virDomainDefParseString(conn, privconn->caps, xml);
1004
    VIR_FREE(xml);
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
    if (!def)
        return -1;

    if ((dom = virDomainAssignDef(conn, &privconn->domains,
                                  def)) == NULL) {
        virDomainDefFree(def);
        return -1;
    }
    dom->state = VIR_DOMAIN_RUNNING;
    dom->def->id = privconn->nextDomID++;
    return dom->def->id;
1016 1017
}

1018 1019 1020
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
                              int flags ATTRIBUTE_UNUSED)
1021
{
1022 1023 1024 1025 1026
    int fd;
    GET_DOMAIN(domain, -1);

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
1027
                  _("cannot save domain core"));
1028 1029
        return (-1);
    }
1030
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1031
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
1032
                  _("cannot write header"));
1033
        close(fd);
1034 1035
        return (-1);
    }
1036 1037
    if (close(fd) < 0) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
1038
                  _("cannot save domain data"));
1039
        close(fd);
1040 1041
        return (-1);
    }
1042 1043 1044 1045
    privdom->state = VIR_DOMAIN_SHUTOFF;
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1046 1047 1048 1049
    }
    return 0;
}

1050 1051 1052 1053 1054
static char *testGetOSType(virDomainPtr dom) {
    char *ret = strdup("linux");
    if (!ret)
        testError(dom->conn, dom, NULL, VIR_ERR_NO_MEMORY, NULL);
    return ret;
1055 1056 1057 1058
}

static unsigned long testGetMaxMemory(virDomainPtr domain) {
    GET_DOMAIN(domain, -1);
1059

1060
    return privdom->def->maxmem;
1061 1062 1063 1064 1065 1066 1067 1068
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
    GET_DOMAIN(domain, -1);

    /* XXX validate not over host memory wrt to other domains */
1069
    privdom->def->maxmem = memory;
1070
    return (0);
1071 1072
}

1073 1074 1075 1076
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
    GET_DOMAIN(domain, -1);
1077

1078 1079 1080
    if (memory > privdom->def->maxmem) {
        testError(domain->conn, domain, NULL,
                  VIR_ERR_INVALID_ARG, __FUNCTION__);
1081 1082
        return (-1);
    }
1083

1084
    privdom->def->memory = memory;
1085 1086 1087 1088 1089 1090
    return (0);
}

static int testSetVcpus(virDomainPtr domain,
                        unsigned int nrCpus) {
    GET_DOMAIN(domain, -1);
1091

1092 1093
    /* We allow more cpus in guest than host */
    if (nrCpus > 32) {
1094
        testError(domain->conn, domain, NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
1095 1096
        return (-1);
    }
1097

1098
    privdom->def->vcpus = nrCpus;
1099
    return (0);
1100 1101
}

1102
static char *testDomainDumpXML(virDomainPtr domain, int flags)
1103
{
1104
    virDomainDefPtr def;
1105
    GET_DOMAIN(domain, NULL);
1106

1107 1108
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
1109

1110 1111 1112
    return virDomainDefFormat(domain->conn,
                              def,
                              flags);
1113
}
1114

1115
static int testNumOfDefinedDomains(virConnectPtr conn) {
1116 1117 1118
    int numInactive = 0;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
1119

1120 1121 1122 1123 1124
    dom = privconn->domains;
    while (dom) {
        if (!virDomainIsActive(dom))
            numInactive++;
        dom = dom->next;
1125
    }
1126
    return numInactive;
1127 1128
}

1129 1130 1131
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
    int n = 0;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);

    dom = privconn->domains;
    memset(names, 0, sizeof(*names)*maxnames);
    while (dom && n < maxnames) {
        if (virDomainIsActive(dom) &&
            !(names[n++] = strdup(dom->def->name)))
            goto no_memory;
        dom = dom->next;
1143
    }
1144 1145 1146 1147 1148 1149 1150
    return n;

no_memory:
    testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
    for (n = 0 ; n < maxnames ; n++)
        VIR_FREE(names[n]);
    return -1;
1151 1152
}

1153
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
1154 1155 1156 1157 1158
                                        const char *xml) {
    virDomainPtr ret;
    virDomainDefPtr def;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
1159

1160 1161
    if ((def = virDomainDefParseString(conn, privconn->caps, xml)) == NULL)
        return NULL;
1162

1163 1164 1165 1166 1167 1168 1169
    if ((dom = virDomainAssignDef(conn, &privconn->domains,
                                  def)) == NULL) {
        virDomainDefFree(def);
        return NULL;
    }
    dom->persistent = 1;
    dom->def->id = -1;
1170

1171 1172 1173 1174 1175
    ret = virGetDomain(conn, def->name, def->uuid);
    if (!ret)
        return NULL;
    ret->id = -1;
    return ret;
1176 1177
}

1178 1179 1180 1181 1182
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
    int i, j;

1183
    GET_CONNECTION(conn);
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200

    if (startCell > privconn->numCells) {
        testError(conn, NULL, NULL, VIR_ERR_INVALID_ARG,
                  _("Range exceeds available cells"));
        return -1;
    }

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

    return j;
}


1201 1202
static int testDomainCreate(virDomainPtr domain) {
    GET_DOMAIN(domain, -1);
1203

1204
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
1205 1206
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Domain is already running"));
1207 1208 1209
        return (-1);
    }

1210 1211
    domain->id = privdom->def->id = privconn->nextDomID++;
    privdom->state = VIR_DOMAIN_RUNNING;
1212

1213 1214 1215 1216 1217 1218
    return (0);
}

static int testDomainUndefine(virDomainPtr domain) {
    GET_DOMAIN(domain, -1);

1219
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
1220 1221
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Domain is still running"));
1222 1223 1224
        return (-1);
    }

1225 1226 1227
    privdom->state = VIR_DOMAIN_SHUTOFF;
    virDomainRemoveInactive(&privconn->domains,
                            privdom);
1228 1229 1230 1231

    return (0);
}

1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
    GET_DOMAIN(domain, -1);
    *autostart = privdom->autostart;
    return (0);
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
    GET_DOMAIN(domain, -1);
    privdom->autostart = autostart ? 1 : 0;
    return (0);
}
1248

1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
static char *testDomainGetSchedulerType(virDomainPtr domain,
                                        int *nparams)
{
    char *type;
    *nparams = 1;
    type = strdup("fair");
    if (!type) {
        testError(domain->conn, domain, NULL, VIR_ERR_NO_MEMORY, "schedular");
        return (NULL);
    }
    return type;
}

static int testDomainGetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int *nparams)
{
    GET_DOMAIN(domain, -1);
    if (*nparams != 1) {
        testError(domain->conn, domain, NULL, VIR_ERR_INVALID_ARG, "nparams");
1269 1270
        return (-1);
    }
1271 1272
    strcpy(params[0].field, "weight");
    params[0].type = VIR_DOMAIN_SCHED_FIELD_UINT;
1273 1274 1275
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
    params[0].value.ui = 50;
1276 1277
    return 0;
}
1278 1279


1280 1281 1282 1283 1284 1285 1286
static int testDomainSetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int nparams)
{
    GET_DOMAIN(domain, -1);
    if (nparams != 1) {
        testError(domain->conn, domain, NULL, VIR_ERR_INVALID_ARG, "nparams");
1287 1288
        return (-1);
    }
1289 1290 1291 1292 1293 1294 1295 1296
    if (STRNEQ(params[0].field, "weight")) {
        testError(domain->conn, domain, NULL, VIR_ERR_INVALID_ARG, "field");
        return (-1);
    }
    if (params[0].type != VIR_DOMAIN_SCHED_FIELD_UINT) {
        testError(domain->conn, domain, NULL, VIR_ERR_INVALID_ARG, "type");
        return (-1);
    }
1297 1298
    /* XXX */
    /*privdom->weight = params[0].value.ui;*/
1299 1300 1301 1302
    return 0;
}

static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
1303
                                        xmlURIPtr uri ATTRIBUTE_UNUSED,
1304
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
                                        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)
{
1322
    virNetworkObjPtr net = NULL;
1323
    GET_CONNECTION(conn);
1324

1325
    if ((net = virNetworkFindByUUID(privconn->networks, uuid)) == NULL) {
1326 1327 1328 1329
        testError (conn, NULL, NULL, VIR_ERR_NO_NETWORK, NULL);
        return NULL;
    }

1330
    return virGetNetwork(conn, net->def->name, net->def->uuid);
1331
}
1332

1333
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
1334
                                             const char *name)
1335
{
1336
    virNetworkObjPtr net = NULL;
1337
    GET_CONNECTION(conn);
1338

1339
    if ((net = virNetworkFindByName(privconn->networks, name)) == NULL) {
1340 1341 1342 1343
        testError (conn, NULL, NULL, VIR_ERR_NO_NETWORK, NULL);
        return NULL;
    }

1344
    return virGetNetwork(conn, net->def->name, net->def->uuid);
1345 1346 1347 1348
}


static int testNumNetworks(virConnectPtr conn) {
1349 1350
    int numActive = 0;
    virNetworkObjPtr net;
1351
    GET_CONNECTION(conn);
1352

1353 1354 1355 1356 1357
    net = privconn->networks;
    while (net) {
        if (virNetworkIsActive(net))
            numActive++;
        net = net->next;
1358
    }
1359
    return numActive;
1360 1361 1362
}

static int testListNetworks(virConnectPtr conn, char **const names, int nnames) {
1363 1364
    int n = 0;
    virNetworkObjPtr net;
1365
    GET_CONNECTION(conn);
1366

1367 1368 1369 1370 1371 1372 1373
    net = privconn->networks;
    memset(names, 0, sizeof(*names)*nnames);
    while (net && n < nnames) {
        if (virNetworkIsActive(net) &&
            !(names[n++] = strdup(net->def->name)))
            goto no_memory;
        net = net->next;
1374
    }
1375 1376 1377 1378 1379 1380 1381
    return n;

no_memory:
    testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
    return (-1);
1382 1383 1384
}

static int testNumDefinedNetworks(virConnectPtr conn) {
1385 1386
    int numInactive = 0;
    virNetworkObjPtr net;
1387
    GET_CONNECTION(conn);
1388

1389 1390 1391 1392 1393
    net = privconn->networks;
    while (net) {
        if (!virNetworkIsActive(net))
            numInactive++;
        net = net->next;
1394
    }
1395
    return numInactive;
1396 1397 1398
}

static int testListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
1399 1400
    int n = 0;
    virNetworkObjPtr net;
1401
    GET_CONNECTION(conn);
1402

1403 1404 1405 1406 1407 1408 1409
    net = privconn->networks;
    memset(names, 0, sizeof(*names)*nnames);
    while (net && n < nnames) {
        if (!virNetworkIsActive(net) &&
            !(names[n++] = strdup(net->def->name)))
            goto no_memory;
        net = net->next;
1410
    }
1411 1412 1413 1414 1415 1416 1417
    return n;

no_memory:
    testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
    return (-1);
1418 1419 1420
}

static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
1421 1422
    virNetworkDefPtr def;
    virNetworkObjPtr net;
1423
    GET_CONNECTION(conn);
1424

1425 1426
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
        return NULL;
1427

1428 1429 1430 1431
    if ((net = virNetworkAssignDef(conn, &privconn->networks,
                                   def)) == NULL) {
        virNetworkDefFree(def);
        return NULL;
1432
    }
1433
    net->active = 1;
1434

1435
    return virGetNetwork(conn, def->name, def->uuid);
1436 1437 1438
}

static virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml) {
1439 1440
    virNetworkDefPtr def;
    virNetworkObjPtr net;
1441
    GET_CONNECTION(conn);
1442

1443 1444
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
        return NULL;
1445

1446 1447 1448 1449
    if ((net = virNetworkAssignDef(conn, &privconn->networks,
                                   def)) == NULL) {
        virNetworkDefFree(def);
        return NULL;
1450
    }
1451
    net->persistent = 1;
1452

1453
    return virGetNetwork(conn, def->name, def->uuid);
1454 1455 1456 1457 1458
}

static int testNetworkUndefine(virNetworkPtr network) {
    GET_NETWORK(network, -1);

1459
    if (virNetworkIsActive(privnet)) {
1460 1461 1462 1463 1464
        testError(network->conn, NULL, network, VIR_ERR_INTERNAL_ERROR,
                  _("Network is still running"));
        return (-1);
    }

1465 1466
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
1467 1468 1469 1470 1471 1472 1473

    return (0);
}

static int testNetworkStart(virNetworkPtr network) {
    GET_NETWORK(network, -1);

1474
    if (virNetworkIsActive(privnet)) {
1475 1476 1477 1478 1479
        testError(network->conn, NULL, network, VIR_ERR_INTERNAL_ERROR,
                  _("Network is already running"));
        return (-1);
    }

1480
    privnet->active = 1;
1481 1482 1483 1484 1485 1486 1487

    return (0);
}

static int testNetworkDestroy(virNetworkPtr network) {
    GET_NETWORK(network, -1);

1488 1489 1490 1491
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
1492 1493 1494 1495 1496 1497 1498
    }
    return (0);
}

static char *testNetworkDumpXML(virNetworkPtr network, int flags ATTRIBUTE_UNUSED) {
    GET_NETWORK(network, NULL);

1499
    return virNetworkDefFormat(network->conn, privnet->def);
1500 1501 1502
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
1503
    char *bridge = NULL;
1504
    GET_NETWORK(network, NULL);
1505 1506
    if (privnet->def->bridge &&
        !(bridge = strdup(privnet->def->bridge))) {
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
        testError(network->conn, NULL, network, VIR_ERR_NO_MEMORY, "network");
        return NULL;
    }
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
    GET_NETWORK(network, -1);
    *autostart = privnet->autostart;
    return (0);
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
    GET_NETWORK(network, -1);
    privnet->autostart = autostart ? 1 : 0;
1524 1525
    return (0);
}
1526

1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
static virDrvOpenStatus testStorageOpen(virConnectPtr conn,
                                        xmlURIPtr uri ATTRIBUTE_UNUSED,
                                        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;
}

1543 1544 1545 1546 1547

static virDriver testDriver = {
    VIR_DRV_TEST,
    "Test",
    LIBVIR_VERSION_NUMBER,
1548
    NULL, /* probe */
1549 1550
    testOpen, /* open */
    testClose, /* close */
1551
    NULL, /* supports_feature */
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
    NULL, /* type */
    testGetVersion, /* version */
    testGetHostname, /* hostname */
    testGetURI, /* URI */
    testGetMaxVCPUs, /* getMaxVcpus */
    testNodeGetInfo, /* nodeGetInfo */
    testGetCapabilities, /* getCapabilities */
    testListDomains, /* listDomains */
    testNumOfDomains, /* numOfDomains */
    testDomainCreateLinux, /* domainCreateLinux */
    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 */
1595 1596 1597
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
1598 1599
    NULL, /* domainBlockStats */
    NULL, /* domainInterfaceStats */
R
Richard W.M. Jones 已提交
1600
    NULL, /* domainBlockPeek */
R
Richard W.M. Jones 已提交
1601
    NULL, /* domainMemoryPeek */
1602
    testNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
1603
    NULL, /* getFreeMemory */
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
};

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


1628 1629 1630 1631 1632 1633
static virStorageDriver testStorageDriver = {
    .name = "Test",
    .open = testStorageOpen,
    .close = testStorageClose,
};

1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
1646 1647
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
1648 1649 1650
    return 0;
}

1651
#endif /* WITH_TEST */