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 27 28
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
29 30
#include <fcntl.h>
#include <unistd.h>
31 32
#include <sys/stat.h>

33
#include "test.h"
34
#include "buf.h"
35
#include "util.h"
36
#include "uuid.h"
37
#include "capabilities.h"
38
#include "memory.h"
39
#include "network_conf.h"
40 41
#include "domain_conf.h"
#include "xml.h"
42

43 44 45 46 47 48 49 50 51 52 53
#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
54

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

68
#define TEST_MODEL "i686"
69
#define TEST_MODEL_WORDSIZE 32
70
#define TEST_EMULATOR "/usr/bin/test-hv"
71

72
static const virNodeInfo defaultNodeInfo = {
73
    TEST_MODEL,
74 75 76 77 78 79 80
    1024*1024*3, /* 3 GB */
    16,
    1400,
    2,
    2,
    2,
    2,
81 82
};

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

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

111
#define GET_CONNECTION(conn)                                            \
112 113 114 115 116
    testConnPtr privconn;                                               \
                                                                        \
    privconn = (testConnPtr)conn->privateData;


117 118
static void
testError(virConnectPtr con,
119
          virDomainPtr dom,
120
          virNetworkPtr net,
121 122
          virErrorNumber error,
          const char *info)
123
{
124
    const char *errmsg;
125

126 127
    if (error == VIR_ERR_OK)
        return;
128

129
    errmsg = __virErrorMsg(error, info);
130
    __virRaiseError(con, dom, net, VIR_FROM_TEST, error, VIR_ERR_ERROR,
131
                    errmsg, info, NULL, 0, 0, errmsg, info, 0);
132 133
}

134 135 136 137 138 139 140
static virCapsPtr
testBuildCapabilities(virConnectPtr conn) {
    virCapsPtr caps;
    virCapsGuestPtr guest;
    const char *const guest_types[] = { "hvm", "xen" };
    int i;
    GET_CONNECTION(conn);
141

142 143
    if ((caps = virCapabilitiesNew(TEST_MODEL, 0, 0)) == NULL)
        goto no_memory;
144

145 146 147 148
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
        goto no_memory;
    if (virCapabilitiesAddHostFeature(caps ,"nonpae") < 0)
        goto no_memory;
149

150 151 152 153
    for (i = 0; i < privconn->numCells; i++) {
        if (virCapabilitiesAddHostNUMACell(caps, i, privconn->cells[i].numCpus,
                                           privconn->cells[i].cpus) < 0)
            goto no_memory;
154 155
    }

156 157 158 159 160 161 162 163 164 165
    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;
166

167 168 169 170 171 172 173
        if (virCapabilitiesAddGuestDomain(guest,
                                          "test",
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
            goto no_memory;
174

175 176 177 178
        if (virCapabilitiesAddGuestFeature(guest, "pae", 1, 1) == NULL)
            goto no_memory;
        if (virCapabilitiesAddGuestFeature(guest ,"nonpae", 1, 1) == NULL)
            goto no_memory;
179 180
    }

181
    return caps;
182

183 184 185 186
no_memory:
    testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
    virCapabilitiesFree(caps);
    return NULL;
187 188
}

189

190 191 192 193 194 195 196 197 198 199
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>";
200 201


202 203 204 205 206 207 208 209 210 211 212
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>";
213 214


215
static int testOpenDefault(virConnectPtr conn) {
216 217
    int u;
    struct timeval tv;
218
    testConnPtr privconn;
219 220 221 222
    virDomainDefPtr domdef = NULL;
    virDomainObjPtr domobj = NULL;
    virNetworkDefPtr netdef = NULL;
    virNetworkObjPtr netobj = NULL;
223

224
    if (VIR_ALLOC(privconn) < 0) {
225 226 227
        testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "testConn");
        return VIR_DRV_OPEN_ERROR;
    }
228
    conn->privateData = privconn;
229 230

    if (gettimeofday(&tv, NULL) < 0) {
231
        testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
232
        goto error;
233 234
    }

235
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
236

237 238 239 240 241 242 243 244 245 246
    // 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;
    }

247 248 249 250 251 252 253 254 255 256 257
    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;
    }
258
    domobj->def->id = privconn->nextDomID++;
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    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;
280 281 282 283
}


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

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

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

    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;
327 328

    if ((fd = open(file, O_RDONLY)) < 0) {
329 330
        testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("loading host definition file"));
        goto error;
331 332
    }

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

342 343
    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
344
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node"));
345
        goto error;
346 347
    }

348 349
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
350
        testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("creating xpath context"));
351
        goto error;
352
    }
353

354
    privconn->nextDomID = 1;
355
    privconn->numCells = 0;
356 357 358 359 360
    strncpy(privconn->path, file, PATH_MAX-1);
    privconn->path[PATH_MAX-1] = '\0';
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

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

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

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

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

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

411
    str = virXPathString(conn, "string(/node/cpu/model[1])", ctxt);
412 413
    if (str != NULL) {
        strncpy(nodeInfo->model, str, sizeof(nodeInfo->model)-1);
414
        nodeInfo->model[sizeof(nodeInfo->model)-1] = '\0';
415
        VIR_FREE(str);
416 417
    }

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

426
    ret = virXPathNodeSet(conn, "/node/domain", ctxt, &domains);
427
    if (ret < 0) {
428
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node domain list"));
429
        goto error;
430
    }
431

432
    for (i = 0 ; i < ret ; i++) {
433 434 435 436 437 438 439 440 441 442
        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);
443
            VIR_FREE(absFile);
444 445 446 447 448 449 450 451 452
            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);
453 454
            goto error;
        }
455 456 457 458

        dom->state = VIR_DOMAIN_RUNNING;
        dom->def->id = privconn->nextDomID++;
        dom->persistent = 1;
459
    }
460
    if (domains != NULL)
461
        VIR_FREE(domains);
462

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

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

        net->persistent = 1;
    }
495
    if (networks != NULL)
496
        VIR_FREE(networks);
497

J
Jim Meyering 已提交
498
    xmlXPathFreeContext(ctxt);
499
    xmlFreeDoc(xml);
500

501
    return (0);
502 503

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

527

528
static int testOpen(virConnectPtr conn,
529
                    xmlURIPtr uri,
530
                    virConnectAuthPtr auth ATTRIBUTE_UNUSED,
531
                    int flags ATTRIBUTE_UNUSED)
532
{
533
    int ret;
534

535
    if (!uri)
536
        return VIR_DRV_OPEN_DECLINED;
537

538
    if (!uri->scheme || STRNEQ(uri->scheme, "test"))
539
        return VIR_DRV_OPEN_DECLINED;
540

541
    /* Remote driver should handle these. */
542
    if (uri->server)
543 544
        return VIR_DRV_OPEN_DECLINED;

545
    if (uri->server)
546 547
        return VIR_DRV_OPEN_DECLINED;

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

557 558 559
    if (STREQ(uri->path, "/default"))
        ret = testOpenDefault(conn);
    else
560 561 562 563
        ret = testOpenFromFile(conn,
                               uri->path);

    return (ret);
564 565
}

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

589 590
static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                          unsigned long *hvVer)
591
{
592 593
    *hvVer = 2;
    return (0);
594 595
}

596
static char *testGetHostname (virConnectPtr conn)
597 598 599 600 601 602
{
    int r;
    char hostname [HOST_NAME_MAX+1], *str;

    r = gethostname (hostname, HOST_NAME_MAX+1);
    if (r == -1) {
603
        testError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, strerror (errno));
604 605 606 607
        return NULL;
    }
    str = strdup (hostname);
    if (str == NULL) {
608
        testError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, strerror (errno));
609 610 611 612 613
        return NULL;
    }
    return str;
}

614
static char * testGetURI (virConnectPtr conn)
615 616
{
    char *uri;
617
    GET_CONNECTION(conn);
618

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

626 627 628 629 630 631 632 633
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
634
{
635
    GET_CONNECTION(conn);
636
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
637
    return (0);
638 639
}

640
static char *testGetCapabilities (virConnectPtr conn)
641
{
642
    char *xml;
643
    GET_CONNECTION(conn);
644

645 646 647
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL) {
        testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
        return NULL;
648
    }
649 650

    return xml;
651 652
}

653
static int testNumOfDomains(virConnectPtr conn)
654
{
655 656 657
    int numActive = 0;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
658

659 660 661 662 663
    dom = privconn->domains;
    while (dom) {
        if (virDomainIsActive(dom))
            numActive++;
        dom = dom->next;
664
    }
665
    return numActive;
666 667
}

668
static virDomainPtr
669
testDomainCreateLinux(virConnectPtr conn, const char *xml,
670
                      unsigned int flags ATTRIBUTE_UNUSED)
671
{
672 673 674 675
    virDomainPtr ret;
    virDomainDefPtr def;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
676

677 678
    if ((def = virDomainDefParseString(conn, privconn->caps, xml)) == NULL)
        return NULL;
679

680 681 682 683
    if ((dom = virDomainAssignDef(conn, &privconn->domains,
                                  def)) == NULL) {
        virDomainDefFree(def);
        return NULL;
684
    }
685 686
    dom->state = VIR_DOMAIN_RUNNING;
    dom->def->id = privconn->nextDomID++;
687

688 689 690 691 692
    ret = virGetDomain(conn, def->name, def->uuid);
    if (!ret)
        return NULL;
    ret->id = def->id;
    return ret;
693 694 695
}


696 697
static virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                         int id)
698
{
699 700 701
    virDomainObjPtr dom = NULL;
    virDomainPtr ret;
    GET_CONNECTION(conn);
702

703
    if ((dom = virDomainFindByID(privconn->domains, id)) == NULL) {
704
        testError (conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
705
        return NULL;
706 707
    }

708 709 710 711 712
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (!ret)
        return NULL;
    ret->id = dom->def->id;
    return ret;
713 714
}

715 716
static virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
717
{
718 719 720
    virDomainPtr ret;
    virDomainObjPtr dom = NULL;
    GET_CONNECTION(conn);
721

722
    if ((dom = virDomainFindByUUID(privconn->domains, uuid)) == NULL) {
723
        testError (conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
724
        return NULL;
725
    }
726

727 728 729 730 731
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (!ret)
        return NULL;
    ret->id = dom->def->id;
    return ret;
732 733
}

734 735
static virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                           const char *name)
736
{
737 738 739
    virDomainPtr ret;
    virDomainObjPtr dom = NULL;
    GET_CONNECTION(conn);
740

741
    if ((dom = virDomainFindByName(privconn->domains, name)) == NULL) {
742
        testError (conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
743
        return NULL;
744
    }
745

746 747 748 749 750
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
    if (!ret)
        return NULL;
    ret->id = dom->def->id;
    return ret;
751 752
}

753 754 755
static int testListDomains (virConnectPtr conn,
                            int *ids,
                            int maxids)
756
{
757 758 759
    int n = 0;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);
760

761 762 763 764 765
    dom = privconn->domains;
    while (dom && n < maxids) {
        if (virDomainIsActive(dom))
            ids[n++] = dom->def->id;
        dom = dom->next;
766
    }
767
    return n;
768 769
}

770
static int testDestroyDomain (virDomainPtr domain)
771
{
772
    GET_DOMAIN(domain, -1);
773

774
    privdom->state = VIR_DOMAIN_SHUTOFF;
775 776
    privdom->def->id = -1;
    domain->id = -1;
777 778 779
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
780
    }
781
    return (0);
782 783
}

784
static int testResumeDomain (virDomainPtr domain)
785
{
786
    GET_DOMAIN(domain, -1);
787

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

794
    privdom->state = VIR_DOMAIN_RUNNING;
795
    return (0);
796 797
}

798
static int testPauseDomain (virDomainPtr domain)
799
{
800
    GET_DOMAIN(domain, -1);
801

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

809
    privdom->state = VIR_DOMAIN_PAUSED;
810
    return (0);
811 812
}

813
static int testShutdownDomain (virDomainPtr domain)
814
{
815
    GET_DOMAIN(domain, -1);
816

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

822
    privdom->state = VIR_DOMAIN_SHUTOFF;
823
    domain->id = -1;
824
    privdom->def->id = -1;
825 826

    return (0);
827 828 829
}

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

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

843 844
    case VIR_DOMAIN_LIFECYCLE_RESTART:
        privdom->state = VIR_DOMAIN_RUNNING;
845 846
        break;

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

853 854
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
        privdom->state = VIR_DOMAIN_RUNNING;
855
        break;
856

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

864
    return (0);
865 866
}

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

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

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

886
static char *testDomainDumpXML(virDomainPtr domain, int flags);
887

888 889 890 891 892 893 894 895
#define TEST_SAVE_MAGIC "TestGuestMagic"

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

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

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

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

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

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

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

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

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

1054
    return privdom->def->maxmem;
1055 1056 1057 1058 1059 1060 1061 1062
}

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

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

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

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

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

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

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

1092
    privdom->def->vcpus = nrCpus;
1093
    return (0);
1094 1095
}

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

1101 1102
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
1103

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

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

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

1123 1124 1125
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
1126 1127 1128 1129 1130 1131 1132
    int n = 0;
    virDomainObjPtr dom;
    GET_CONNECTION(conn);

    dom = privconn->domains;
    memset(names, 0, sizeof(*names)*maxnames);
    while (dom && n < maxnames) {
1133
        if (!virDomainIsActive(dom) &&
1134 1135 1136
            !(names[n++] = strdup(dom->def->name)))
            goto no_memory;
        dom = dom->next;
1137
    }
1138 1139 1140 1141 1142 1143 1144
    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;
1145 1146
}

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

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

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

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

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

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

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


1195 1196
static int testDomainCreate(virDomainPtr domain) {
    GET_DOMAIN(domain, -1);
1197

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

1204 1205
    domain->id = privdom->def->id = privconn->nextDomID++;
    privdom->state = VIR_DOMAIN_RUNNING;
1206

1207 1208 1209 1210 1211 1212
    return (0);
}

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

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

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

    return (0);
}

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

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


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

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

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

1324
    return virGetNetwork(conn, net->def->name, net->def->uuid);
1325
}
1326

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

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

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


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

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

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

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

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

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

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

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

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

1419 1420
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
        return NULL;
1421

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

1429
    return virGetNetwork(conn, def->name, def->uuid);
1430 1431 1432
}

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

1437 1438
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
        return NULL;
1439

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

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

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

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

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

    return (0);
}

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

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

1474
    privnet->active = 1;
1475 1476 1477 1478 1479 1480 1481

    return (0);
}

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

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

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

1493
    return virNetworkDefFormat(network->conn, privnet->def);
1494 1495 1496
}

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

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

1537 1538 1539 1540 1541

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

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


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

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