test.c 45.9 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

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

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

        net->persistent = 1;
    }
499
    if (networks != NULL)
500
        VIR_FREE(networks);
501

J
Jim Meyering 已提交
502
    xmlXPathFreeContext(ctxt);
503
    xmlFreeDoc(xml);
504

505
    return (0);
506 507

 error:
J
Jim Meyering 已提交
508
    xmlXPathFreeContext(ctxt);
509
    xmlFreeDoc(xml);
510 511
    VIR_FREE(domains);
    VIR_FREE(networks);
512 513
    if (fd != -1)
        close(fd);
514 515 516 517 518 519 520 521 522 523 524 525
    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;
    }
526
    VIR_FREE(privconn);
527
    conn->privateData = NULL;
528
    return VIR_DRV_OPEN_ERROR;
529 530
}

531

532
static int testOpen(virConnectPtr conn,
533
                    xmlURIPtr uri,
534
                    virConnectAuthPtr auth ATTRIBUTE_UNUSED,
535
                    int flags ATTRIBUTE_UNUSED)
536
{
537
    int ret;
538

539
    if (!uri)
540
        return VIR_DRV_OPEN_DECLINED;
541

542
    if (!uri->scheme || STRNEQ(uri->scheme, "test"))
543
        return VIR_DRV_OPEN_DECLINED;
544

545
    /* Remote driver should handle these. */
546
    if (uri->server)
547 548
        return VIR_DRV_OPEN_DECLINED;

549
    if (uri->server)
550 551
        return VIR_DRV_OPEN_DECLINED;

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

561 562 563
    if (STREQ(uri->path, "/default"))
        ret = testOpenDefault(conn);
    else
564 565 566 567
        ret = testOpenFromFile(conn,
                               uri->path);

    return (ret);
568 569
}

570
static int testClose(virConnectPtr conn)
571
{
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
    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;
    }
588
    VIR_FREE (privconn);
589
    conn->privateData = conn;
590
    return 0;
591 592
}

593 594
static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                          unsigned long *hvVer)
595
{
596 597
    *hvVer = 2;
    return (0);
598 599
}

600
static char *testGetHostname (virConnectPtr conn)
601 602 603 604 605 606
{
    int r;
    char hostname [HOST_NAME_MAX+1], *str;

    r = gethostname (hostname, HOST_NAME_MAX+1);
    if (r == -1) {
607
        testError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, strerror (errno));
608 609 610 611
        return NULL;
    }
    str = strdup (hostname);
    if (str == NULL) {
612
        testError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, strerror (errno));
613 614 615 616 617
        return NULL;
    }
    return str;
}

618
static char * testGetURI (virConnectPtr conn)
619 620
{
    char *uri;
621
    GET_CONNECTION(conn);
622

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

630 631 632 633 634 635 636 637
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
638
{
639
    GET_CONNECTION(conn);
640
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
641
    return (0);
642 643
}

644
static char *testGetCapabilities (virConnectPtr conn)
645
{
646
    char *xml;
647
    GET_CONNECTION(conn);
648

649 650 651
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL) {
        testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
        return NULL;
652
    }
653 654

    return xml;
655 656
}

657
static int testNumOfDomains(virConnectPtr conn)
658
{
659 660 661
    int numActive = 0;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
662

663 664 665 666 667
    dom = privconn->domains;
    while (dom) {
        if (virDomainIsActive(dom))
            numActive++;
        dom = dom->next;
668
    }
669
    return numActive;
670 671
}

672
static virDomainPtr
673
testDomainCreateLinux(virConnectPtr conn, const char *xml,
674
                      unsigned int flags ATTRIBUTE_UNUSED)
675
{
676 677 678 679
    virDomainPtr ret;
    virDomainDefPtr def;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
680

681 682
    if ((def = virDomainDefParseString(conn, privconn->caps, xml)) == NULL)
        return NULL;
683

684 685 686 687
    if ((dom = virDomainAssignDef(conn, &privconn->domains,
                                  def)) == NULL) {
        virDomainDefFree(def);
        return NULL;
688
    }
689 690
    dom->state = VIR_DOMAIN_RUNNING;
    dom->def->id = privconn->nextDomID++;
691

692 693 694 695 696
    ret = virGetDomain(conn, def->name, def->uuid);
    if (!ret)
        return NULL;
    ret->id = def->id;
    return ret;
697 698 699
}


700 701
static virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                         int id)
702
{
703 704 705
    virDomainObjPtr dom = NULL;
    virDomainPtr ret;
    GET_CONNECTION(conn);
706

707
    if ((dom = virDomainFindByID(privconn->domains, id)) == NULL) {
708
        testError (conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
709
        return NULL;
710 711
    }

712 713 714 715 716
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (!ret)
        return NULL;
    ret->id = dom->def->id;
    return ret;
717 718
}

719 720
static virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
721
{
722 723 724
    virDomainPtr ret;
    virDomainObjPtr dom = NULL;
    GET_CONNECTION(conn);
725

726
    if ((dom = virDomainFindByUUID(privconn->domains, uuid)) == NULL) {
727
        testError (conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
728
        return NULL;
729
    }
730

731 732 733 734 735
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (!ret)
        return NULL;
    ret->id = dom->def->id;
    return ret;
736 737
}

738 739
static virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                           const char *name)
740
{
741 742 743
    virDomainPtr ret;
    virDomainObjPtr dom = NULL;
    GET_CONNECTION(conn);
744

745
    if ((dom = virDomainFindByName(privconn->domains, name)) == NULL) {
746
        testError (conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
747
        return NULL;
748
    }
749

750 751 752 753 754
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (!ret)
        return NULL;
    ret->id = dom->def->id;
    return ret;
755 756
}

757 758 759
static int testListDomains (virConnectPtr conn,
                            int *ids,
                            int maxids)
760
{
761 762 763
    int n = 0;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
764

765 766 767 768 769
    dom = privconn->domains;
    while (dom && n < maxids) {
        if (virDomainIsActive(dom))
            ids[n++] = dom->def->id;
        dom = dom->next;
770
    }
771
    return n;
772 773
}

774
static int testDestroyDomain (virDomainPtr domain)
775
{
776
    GET_DOMAIN(domain, -1);
777

778 779 780 781
    privdom->state = VIR_DOMAIN_SHUTOFF;
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
782
    }
783
    return (0);
784 785
}

786
static int testResumeDomain (virDomainPtr domain)
787
{
788
    GET_DOMAIN(domain, -1);
789

790 791 792
    if (privdom->state != VIR_DOMAIN_PAUSED) {
        testError(domain->conn, domain, NULL,
                  VIR_ERR_INTERNAL_ERROR, _("domain not paused"));
793 794
        return -1;
    }
795

796
    privdom->state = VIR_DOMAIN_RUNNING;
797
    return (0);
798 799
}

800
static int testPauseDomain (virDomainPtr domain)
801
{
802
    GET_DOMAIN(domain, -1);
803

804 805 806 807
    if (privdom->state == VIR_DOMAIN_SHUTOFF ||
        privdom->state == VIR_DOMAIN_PAUSED) {
        testError(domain->conn, domain, NULL,
                  VIR_ERR_INTERNAL_ERROR, _("domain not running"));
808 809
        return -1;
    }
810

811
    privdom->state = VIR_DOMAIN_PAUSED;
812
    return (0);
813 814
}

815
static int testShutdownDomain (virDomainPtr domain)
816
{
817
    GET_DOMAIN(domain, -1);
818

819
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
820 821
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, "domain not running");
        return -1;
822
    }
823

824
    privdom->state = VIR_DOMAIN_SHUTOFF;
825
    domain->id = -1;
826
    privdom->def->id = -1;
827 828

    return (0);
829 830 831
}

/* Similar behaviour as shutdown */
832 833
static int testRebootDomain (virDomainPtr domain,
                             unsigned int action ATTRIBUTE_UNUSED)
834
{
835
    GET_DOMAIN(domain, -1);
836

837 838 839 840
    privdom->state = VIR_DOMAIN_SHUTDOWN;
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
        privdom->state = VIR_DOMAIN_SHUTOFF;
841
        domain->id = -1;
842
        privdom->def->id = -1;
843 844
        break;

845 846
    case VIR_DOMAIN_LIFECYCLE_RESTART:
        privdom->state = VIR_DOMAIN_RUNNING;
847 848
        break;

849 850
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
        privdom->state = VIR_DOMAIN_SHUTOFF;
851
        domain->id = -1;
852
        privdom->def->id = -1;
853 854
        break;

855 856
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
        privdom->state = VIR_DOMAIN_RUNNING;
857
        break;
858

859
    default:
860
        privdom->state = VIR_DOMAIN_SHUTOFF;
861
        domain->id = -1;
862
        privdom->def->id = -1;
863 864
        break;
    }
865

866
    return (0);
867 868
}

869 870
static int testGetDomainInfo (virDomainPtr domain,
                              virDomainInfoPtr info)
871
{
872
    struct timeval tv;
873
    GET_DOMAIN(domain, -1);
874 875

    if (gettimeofday(&tv, NULL) < 0) {
876
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
877 878 879
        return (-1);
    }

880 881 882 883 884
    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));
885
    return (0);
886 887
}

888
static char *testDomainDumpXML(virDomainPtr domain, int flags);
889

890 891 892 893 894 895 896 897
#define TEST_SAVE_MAGIC "TestGuestMagic"

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

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

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

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

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

    def = virDomainDefParseString(conn, privconn->caps, xml);
1000
    VIR_FREE(xml);
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
    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;
1012 1013
}

1014 1015 1016
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
                              int flags ATTRIBUTE_UNUSED)
1017
{
1018 1019 1020 1021 1022
    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,
1023
                  _("cannot save domain core"));
1024 1025
        return (-1);
    }
1026
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1027
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
1028
                  _("cannot write header"));
1029
        close(fd);
1030 1031
        return (-1);
    }
1032 1033
    if (close(fd) < 0) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
1034
                  _("cannot save domain data"));
1035
        close(fd);
1036 1037
        return (-1);
    }
1038 1039 1040 1041
    privdom->state = VIR_DOMAIN_SHUTOFF;
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1042 1043 1044 1045
    }
    return 0;
}

1046 1047 1048 1049 1050
static char *testGetOSType(virDomainPtr dom) {
    char *ret = strdup("linux");
    if (!ret)
        testError(dom->conn, dom, NULL, VIR_ERR_NO_MEMORY, NULL);
    return ret;
1051 1052 1053 1054
}

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

1056
    return privdom->def->maxmem;
1057 1058 1059 1060 1061 1062 1063 1064
}

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

    /* XXX validate not over host memory wrt to other domains */
1065
    privdom->def->maxmem = memory;
1066
    return (0);
1067 1068
}

1069 1070 1071 1072
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
    GET_DOMAIN(domain, -1);
1073

1074 1075 1076
    if (memory > privdom->def->maxmem) {
        testError(domain->conn, domain, NULL,
                  VIR_ERR_INVALID_ARG, __FUNCTION__);
1077 1078
        return (-1);
    }
1079

1080
    privdom->def->memory = memory;
1081 1082 1083 1084 1085 1086
    return (0);
}

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

1088 1089
    /* We allow more cpus in guest than host */
    if (nrCpus > 32) {
1090
        testError(domain->conn, domain, NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
1091 1092
        return (-1);
    }
1093

1094
    privdom->def->vcpus = nrCpus;
1095
    return (0);
1096 1097
}

1098
static char *testDomainDumpXML(virDomainPtr domain, int flags)
1099
{
1100
    virDomainDefPtr def;
1101
    GET_DOMAIN(domain, NULL);
1102

1103 1104
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
1105

1106 1107 1108
    return virDomainDefFormat(domain->conn,
                              def,
                              flags);
1109
}
1110

1111
static int testNumOfDefinedDomains(virConnectPtr conn) {
1112 1113 1114
    int numInactive = 0;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
1115

1116 1117 1118 1119 1120
    dom = privconn->domains;
    while (dom) {
        if (!virDomainIsActive(dom))
            numInactive++;
        dom = dom->next;
1121
    }
1122
    return numInactive;
1123 1124
}

1125 1126 1127
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
    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;
1139
    }
1140 1141 1142 1143 1144 1145 1146
    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;
1147 1148
}

1149
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
1150 1151 1152 1153 1154
                                        const char *xml) {
    virDomainPtr ret;
    virDomainDefPtr def;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
1155

1156 1157
    if ((def = virDomainDefParseString(conn, privconn->caps, xml)) == NULL)
        return NULL;
1158

1159 1160 1161 1162 1163 1164 1165
    if ((dom = virDomainAssignDef(conn, &privconn->domains,
                                  def)) == NULL) {
        virDomainDefFree(def);
        return NULL;
    }
    dom->persistent = 1;
    dom->def->id = -1;
1166

1167 1168 1169 1170 1171
    ret = virGetDomain(conn, def->name, def->uuid);
    if (!ret)
        return NULL;
    ret->id = -1;
    return ret;
1172 1173
}

1174 1175 1176 1177 1178
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
    int i, j;

1179
    GET_CONNECTION(conn);
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196

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


1197 1198
static int testDomainCreate(virDomainPtr domain) {
    GET_DOMAIN(domain, -1);
1199

1200
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
1201 1202
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Domain is already running"));
1203 1204 1205
        return (-1);
    }

1206 1207
    domain->id = privdom->def->id = privconn->nextDomID++;
    privdom->state = VIR_DOMAIN_RUNNING;
1208

1209 1210 1211 1212 1213 1214
    return (0);
}

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

1215
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
1216 1217
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Domain is still running"));
1218 1219 1220
        return (-1);
    }

1221 1222 1223
    privdom->state = VIR_DOMAIN_SHUTOFF;
    virDomainRemoveInactive(&privconn->domains,
                            privdom);
1224 1225 1226 1227

    return (0);
}

1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
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);
}
1244

1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
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");
1265 1266
        return (-1);
    }
1267 1268
    strcpy(params[0].field, "weight");
    params[0].type = VIR_DOMAIN_SCHED_FIELD_UINT;
1269 1270 1271
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
    params[0].value.ui = 50;
1272 1273
    return 0;
}
1274 1275


1276 1277 1278 1279 1280 1281 1282
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");
1283 1284
        return (-1);
    }
1285 1286 1287 1288 1289 1290 1291 1292
    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);
    }
1293 1294
    /* XXX */
    /*privdom->weight = params[0].value.ui;*/
1295 1296 1297 1298
    return 0;
}

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

1321
    if ((net = virNetworkFindByUUID(privconn->networks, uuid)) == NULL) {
1322 1323 1324 1325
        testError (conn, NULL, NULL, VIR_ERR_NO_NETWORK, NULL);
        return NULL;
    }

1326
    return virGetNetwork(conn, net->def->name, net->def->uuid);
1327
}
1328

1329
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
1330
                                             const char *name)
1331
{
1332
    virNetworkObjPtr net = NULL;
1333
    GET_CONNECTION(conn);
1334

1335
    if ((net = virNetworkFindByName(privconn->networks, name)) == NULL) {
1336 1337 1338 1339
        testError (conn, NULL, NULL, VIR_ERR_NO_NETWORK, NULL);
        return NULL;
    }

1340
    return virGetNetwork(conn, net->def->name, net->def->uuid);
1341 1342 1343 1344
}


static int testNumNetworks(virConnectPtr conn) {
1345 1346
    int numActive = 0;
    virNetworkObjPtr net;
1347
    GET_CONNECTION(conn);
1348

1349 1350 1351 1352 1353
    net = privconn->networks;
    while (net) {
        if (virNetworkIsActive(net))
            numActive++;
        net = net->next;
1354
    }
1355
    return numActive;
1356 1357 1358
}

static int testListNetworks(virConnectPtr conn, char **const names, int nnames) {
1359 1360
    int n = 0;
    virNetworkObjPtr net;
1361
    GET_CONNECTION(conn);
1362

1363 1364 1365 1366 1367 1368 1369
    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;
1370
    }
1371 1372 1373 1374 1375 1376 1377
    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);
1378 1379 1380
}

static int testNumDefinedNetworks(virConnectPtr conn) {
1381 1382
    int numInactive = 0;
    virNetworkObjPtr net;
1383
    GET_CONNECTION(conn);
1384

1385 1386 1387 1388 1389
    net = privconn->networks;
    while (net) {
        if (!virNetworkIsActive(net))
            numInactive++;
        net = net->next;
1390
    }
1391
    return numInactive;
1392 1393 1394
}

static int testListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
1395 1396
    int n = 0;
    virNetworkObjPtr net;
1397
    GET_CONNECTION(conn);
1398

1399 1400 1401 1402 1403 1404 1405
    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;
1406
    }
1407 1408 1409 1410 1411 1412 1413
    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);
1414 1415 1416
}

static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
1417 1418
    virNetworkDefPtr def;
    virNetworkObjPtr net;
1419
    GET_CONNECTION(conn);
1420

1421 1422
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
        return NULL;
1423

1424 1425 1426 1427
    if ((net = virNetworkAssignDef(conn, &privconn->networks,
                                   def)) == NULL) {
        virNetworkDefFree(def);
        return NULL;
1428
    }
1429
    net->active = 1;
1430

1431
    return virGetNetwork(conn, def->name, def->uuid);
1432 1433 1434
}

static virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml) {
1435 1436
    virNetworkDefPtr def;
    virNetworkObjPtr net;
1437
    GET_CONNECTION(conn);
1438

1439 1440
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
        return NULL;
1441

1442 1443 1444 1445
    if ((net = virNetworkAssignDef(conn, &privconn->networks,
                                   def)) == NULL) {
        virNetworkDefFree(def);
        return NULL;
1446
    }
1447
    net->persistent = 1;
1448

1449
    return virGetNetwork(conn, def->name, def->uuid);
1450 1451 1452 1453 1454
}

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

1455
    if (virNetworkIsActive(privnet)) {
1456 1457 1458 1459 1460
        testError(network->conn, NULL, network, VIR_ERR_INTERNAL_ERROR,
                  _("Network is still running"));
        return (-1);
    }

1461 1462
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
1463 1464 1465 1466 1467 1468 1469

    return (0);
}

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

1470
    if (virNetworkIsActive(privnet)) {
1471 1472 1473 1474 1475
        testError(network->conn, NULL, network, VIR_ERR_INTERNAL_ERROR,
                  _("Network is already running"));
        return (-1);
    }

1476
    privnet->active = 1;
1477 1478 1479 1480 1481 1482 1483

    return (0);
}

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

1484 1485 1486 1487
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
1488 1489 1490 1491 1492 1493 1494
    }
    return (0);
}

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

1495
    return virNetworkDefFormat(network->conn, privnet->def);
1496 1497 1498
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
1499
    char *bridge = NULL;
1500
    GET_NETWORK(network, NULL);
1501 1502
    if (privnet->def->bridge &&
        !(bridge = strdup(privnet->def->bridge))) {
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
        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;
1520 1521
    return (0);
}
1522

1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
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;
}

1539 1540 1541 1542 1543

static virDriver testDriver = {
    VIR_DRV_TEST,
    "Test",
    LIBVIR_VERSION_NUMBER,
1544
    NULL, /* probe */
1545 1546
    testOpen, /* open */
    testClose, /* close */
1547
    NULL, /* supports_feature */
1548 1549 1550 1551 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
    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 */
1591 1592 1593
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
1594 1595
    NULL, /* domainBlockStats */
    NULL, /* domainInterfaceStats */
R
Richard W.M. Jones 已提交
1596
    NULL, /* domainBlockPeek */
R
Richard W.M. Jones 已提交
1597
    NULL, /* domainMemoryPeek */
1598
    testNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
1599
    NULL, /* getFreeMemory */
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
};

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


1624 1625 1626 1627 1628 1629
static virStorageDriver testStorageDriver = {
    .name = "Test",
    .open = testStorageOpen,
    .close = testStorageClose,
};

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

1647
#endif /* WITH_TEST */