test.c 44.0 KB
Newer Older
1 2 3
/*
 * test.c: A "mock" hypervisor for use by application unit tests
 *
4 5
 * Copyright (C) 2006-2007 Red Hat, Inc.
 * 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
#ifdef WITH_TEST
25 26 27

#define _GNU_SOURCE /* for asprintf */

28 29 30
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
31
#include <errno.h>
32 33 34
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
35
#include <libxml/uri.h>
36 37
#include <fcntl.h>
#include <unistd.h>
38 39 40

#include "internal.h"
#include "test.h"
41
#include "xml.h"
42
#include "buf.h"
43

44 45 46 47 48 49
int testOpen(virConnectPtr conn,
             const char *name,
             int flags);
int testClose  (virConnectPtr conn);
int testGetVersion(virConnectPtr conn,
                   unsigned long *hvVer);
50 51
char *testGetHostname (virConnectPtr conn);
char *testGetURI (virConnectPtr conn);
52 53
int testNodeGetInfo(virConnectPtr conn,
                    virNodeInfoPtr info);
54
char *testGetCapabilities (virConnectPtr conn);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
int testNumOfDomains(virConnectPtr conn);
int testListDomains(virConnectPtr conn,
                    int *ids,
                    int maxids);
char *testGetOSType(virDomainPtr dom);
virDomainPtr
testDomainCreateLinux(virConnectPtr conn, const char *xmlDesc,
                      unsigned int flags ATTRIBUTE_UNUSED);
virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                  int id);
virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                    const unsigned char *uuid);
virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                    const char *name);
int testDestroyDomain(virDomainPtr domain);
int testResumeDomain(virDomainPtr domain);
int testPauseDomain(virDomainPtr domain);
int testShutdownDomain (virDomainPtr domain);
int testRebootDomain (virDomainPtr domain,
                      virDomainRestart action);
int testGetDomainInfo(virDomainPtr domain,
                      virDomainInfoPtr info);
unsigned long testGetMaxMemory(virDomainPtr domain);
int testSetMaxMemory(virDomainPtr domain,
                     unsigned long memory);
int testSetMemory(virDomainPtr domain,
                  unsigned long memory);
int testSetVcpus(virDomainPtr domain,
                 unsigned int nrCpus);
char * testDomainDumpXML(virDomainPtr domain, int flags);

int testNumOfDefinedDomains(virConnectPtr conn);
int testListDefinedDomains(virConnectPtr conn,
88
                           char **const names,
89 90 91 92 93 94 95 96 97
                           int maxnames);

virDomainPtr testDomainDefineXML(virConnectPtr conn,
                                 const char *xml);

int testDomainCreate(virDomainPtr dom);

int testDomainUndefine(virDomainPtr dom);

98
static virDriver testDriver = {
99 100 101 102 103 104 105
    VIR_DRV_TEST,
    "Test",
    LIBVIR_VERSION_NUMBER,
    testOpen, /* open */
    testClose, /* close */
    NULL, /* type */
    testGetVersion, /* version */
106 107
    testGetHostname, /* hostname */
    testGetURI, /* URI */
108
    NULL, /* getMaxVcpus */
109
    testNodeGetInfo, /* nodeGetInfo */
110
    testGetCapabilities, /* getCapabilities */
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    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 */
    NULL, /* domainSave */
    NULL, /* domainRestore */
    NULL, /* domainCoreDump */
    testSetVcpus, /* domainSetVcpus */
    NULL, /* domainPinVcpu */
    NULL, /* domainGetVcpus */
133
    NULL, /* domainGetMaxVcpus */
134 135 136 137 138 139 140 141
    testDomainDumpXML, /* domainDumpXML */
    testListDefinedDomains, /* listDefinedDomains */
    testNumOfDefinedDomains, /* numOfDefinedDomains */
    testDomainCreate, /* domainCreate */
    testDomainDefineXML, /* domainDefineXML */
    testDomainUndefine, /* domainUndefine */
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
142 143
    NULL, /* domainGetAutostart */
    NULL, /* domainSetAutostart */
144 145 146
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
147 148
};

149 150 151
/* Per-connection private data. */
struct _testPrivate {
    int handle;
152
    char *path;
153 154 155
};
typedef struct _testPrivate *testPrivatePtr;

156
typedef struct _testDev {
157 158
    char name[20];
    virDeviceMode mode;
159 160 161 162 163
} testDev;

#define MAX_DEVICES 10

typedef struct _testDom {
164
    int active;
165
    int id;
166
    char name[20];
167
    unsigned char uuid[VIR_UUID_BUFLEN];
168 169 170 171 172 173 174 175
    virDomainInfo info;
    unsigned int maxVCPUs;
    virDomainRestart onRestart; /* What to do at end of current shutdown procedure */
    virDomainRestart onReboot;
    virDomainRestart onPoweroff;
    virDomainRestart onCrash;
    int numDevices;
    testDev devices[MAX_DEVICES];
176 177 178 179 180
} testDom;

#define MAX_DOMAINS 20

typedef struct _testCon {
181 182 183 184
    int active;
    virNodeInfo nodeInfo;
    int numDomains;
    testDom domains[MAX_DOMAINS];
185 186 187 188 189
} testCon;

#define MAX_CONNECTIONS 5

typedef struct _testNode {
190 191
    int numConnections;
    testCon connections[MAX_CONNECTIONS];
192 193 194 195 196 197 198 199
} testNode;

/* XXX, how about we stuff this in a SHM
   segment so multiple apps can run tests
   against the mock hypervisor concurrently.
   Would need a pthread process shared mutex
   too probably */
static testNode *node = NULL;
200
static int nextDomID = 1;
201

202 203 204
#define TEST_MODEL "i686"
#define TEST_MODEL_WORDSIZE "32"

205
static const virNodeInfo defaultNodeInfo = {
206
    TEST_MODEL,
207 208 209 210 211 212 213
    1024*1024*3, /* 3 GB */
    16,
    1400,
    2,
    2,
    2,
    2,
214 215 216 217
};

static void
testError(virConnectPtr con,
218 219 220
          virDomainPtr dom,
          virErrorNumber error,
          const char *info)
221
{
222
    const char *errmsg;
223

224 225
    if (error == VIR_ERR_OK)
        return;
226

227
    errmsg = __virErrorMsg(error, info);
228
    __virRaiseError(con, dom, NULL, VIR_FROM_TEST, error, VIR_ERR_ERROR,
229
                    errmsg, info, NULL, 0, 0, errmsg, info, 0);
230 231
}

232
static int testRestartStringToFlag(const char *str) {
233 234 235 236 237 238 239 240 241 242 243
    if (!strcmp(str, "restart")) {
        return VIR_DOMAIN_RESTART;
    } else if (!strcmp(str, "destroy")) {
        return VIR_DOMAIN_DESTROY;
    } else if (!strcmp(str, "preserve")) {
        return VIR_DOMAIN_PRESERVE;
    } else if (!strcmp(str, "rename-restart")) {
        return VIR_DOMAIN_RENAME_RESTART;
    } else {
        return (0);
    }
244 245 246
}

static const char *testRestartFlagToString(int flag) {
247 248 249 250 251 252 253 254 255 256 257
    switch (flag) {
    case VIR_DOMAIN_RESTART:
        return "restart";
    case VIR_DOMAIN_DESTROY:
        return "destroy";
    case VIR_DOMAIN_PRESERVE:
        return "preserve";
    case VIR_DOMAIN_RENAME_RESTART:
        return "rename-restart";
    }
    return (NULL);
258
}
259 260 261 262 263 264

/**
 * testRegister:
 *
 * Registers the test driver
 */
265 266
int
testRegister(void)
267
{
268
    return virRegisterDriver(&testDriver);
269 270
}

271
static int testLoadDomain(virConnectPtr conn,
272 273 274 275 276
                          int domid,
                          xmlDocPtr xml) {
    xmlNodePtr root = NULL;
    xmlXPathContextPtr ctxt = NULL;
    char *name = NULL;
277
    unsigned char rawuuid[VIR_UUID_BUFLEN];
278 279 280 281 282 283
    char *dst_uuid;
    testCon *con;
    struct timeval tv;
    unsigned long memory = 0;
    unsigned long maxMem = 0;
    int nrVirtCpu;
284 285 286
    char *str;
    int handle = -1, i, ret;
    long l;
287 288 289
    virDomainRestart onReboot = VIR_DOMAIN_RESTART;
    virDomainRestart onPoweroff = VIR_DOMAIN_DESTROY;
    virDomainRestart onCrash = VIR_DOMAIN_RENAME_RESTART;
290
    testPrivatePtr priv;
291 292 293 294

    if (gettimeofday(&tv, NULL) < 0) {
        testError(conn, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
        return (-1);
295 296
    }

297 298 299 300
    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "domain"))) {
        testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain"));
        goto error;
301
    }
302 303 304 305 306 307 308

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        testError(conn, NULL, VIR_ERR_INTERNAL_ERROR, _("creating xpath context"));
        goto error;
    }

309 310
    name = virXPathString("string(/domain/name[1])", ctxt);
    if (name == NULL) {
311 312 313
        testError(conn, NULL, VIR_ERR_INTERNAL_ERROR, _("domain name"));
        goto error;
    }
314

315 316
    str = virXPathString("string(/domain/uuid[1])", ctxt);
    if (str == NULL) {
317 318 319 320
        testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain uuid"));
        goto error;
    }
    dst_uuid = (char *) &rawuuid[0];
321
    if (!(virParseUUID((char **)&dst_uuid, str))) {
322 323
        testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain uuid"));
        goto error;
324
    }
325
    free(str);
326

327 328 329

    ret = virXPathLong("string(/domain/memory[1])", ctxt, &l);
    if (ret != 0) {
330 331
        testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain memory"));
        goto error;
332
    }
333
    maxMem = l;
334

335 336
    ret = virXPathLong("string(/domain/currentMemory[1])", ctxt, &l);
    if (ret == -1) {
337
        memory = maxMem;
338 339 340
    } else if (ret == -2) {
	testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain current memory"));
	goto error;
341
    } else {
342
        memory = l;
343 344
    }

345 346
    ret = virXPathLong("string(/domain/vcpu[1])", ctxt, &l);
    if (ret == -1) {
347
        nrVirtCpu = 1;
348 349 350
    } else if (ret == -2) {
	testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain vcpus"));
	goto error;
351
    } else {
352
        nrVirtCpu = l;
353 354
    }

355 356 357
    str = virXPathString("string(/domain/on_reboot[1])", ctxt);
    if (str != NULL) {
        if (!(onReboot = testRestartStringToFlag(str))) {
358
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain reboot behaviour"));
359
	    free(str);
360 361
            goto error;
        }
362
	free(str);
363 364
    }

365 366 367
    str = virXPathString("string(/domain/on_poweroff[1])", ctxt);
    if (str != NULL) {
        if (!(onReboot = testRestartStringToFlag(str))) {
368
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain poweroff behaviour"));
369
	    free(str);
370 371
            goto error;
        }
372
	free(str);
373 374
    }

375 376 377
    str = virXPathString("string(/domain/on_crash[1])", ctxt);
    if (str != NULL) {
        if (!(onReboot = testRestartStringToFlag(str))) {
378
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain crash behaviour"));
379
	    free(str);
380 381
            goto error;
        }
382
	free(str);
383
    }
384

385 386
    priv = (testPrivatePtr) conn->privateData;
    con = &node->connections[priv->handle];
387

388 389 390 391 392 393 394 395 396 397
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
        if (!con->domains[i].active) {
            handle = i;
            break;
        }
    }
    if (handle < 0)
        return (-1);

    con->domains[handle].active = 1;
398
    con->domains[handle].id = domid;
399
    strncpy(con->domains[handle].name, name, sizeof(con->domains[handle].name));
400
    free(name);
401 402 403 404 405
    name = NULL;

    if (memory > maxMem)
        memory = maxMem;

406
    memmove(con->domains[handle].uuid, rawuuid, VIR_UUID_BUFLEN);
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    con->domains[handle].info.maxMem = maxMem;
    con->domains[handle].info.memory = memory;
    con->domains[handle].info.state = domid < 0 ? VIR_DOMAIN_SHUTOFF : VIR_DOMAIN_RUNNING;
    con->domains[handle].info.nrVirtCpu = nrVirtCpu;
    con->domains[handle].info.cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
    con->domains[handle].maxVCPUs = nrVirtCpu;

    con->domains[handle].onReboot = onReboot;
    con->domains[handle].onPoweroff = onPoweroff;
    con->domains[handle].onCrash = onCrash;

    return (0);

 error:
    if (name)
        free(name);
    return (-1);
424 425 426
}

static int testLoadDomainFromDoc(virConnectPtr conn,
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
                                 int domid,
                                 const char *doc) {
    int ret;
    xmlDocPtr xml;
    if (!(xml = xmlReadDoc(BAD_CAST doc, "domain.xml", NULL,
                           XML_PARSE_NOENT | XML_PARSE_NONET |
                           XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
        testError(NULL, NULL, VIR_ERR_XML_ERROR, _("domain"));
        return (-1);
    }

    ret = testLoadDomain(conn, domid, xml);

    xmlFreeDoc(xml);

    return (ret);
443 444 445
}

static int testLoadDomainFromFile(virConnectPtr conn,
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
                                  int domid,
                                  const char *file) {
    int ret, fd;
    xmlDocPtr xml;

    if ((fd = open(file, O_RDONLY)) < 0) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("load domain definition file"));
        return (-1);
    }

    if (!(xml = xmlReadFd(fd, file, NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
        testError(NULL, NULL, VIR_ERR_XML_ERROR, _("domain"));
        close(fd);
        return (-1);
    }
463 464
    close(fd);

465
    ret = testLoadDomain(conn, domid, xml);
466

467
    xmlFreeDoc(xml);
468

469
    return (ret);
470 471 472 473
}


static int testOpenDefault(virConnectPtr conn,
474 475 476
                           int connid) {
    int u;
    struct timeval tv;
477
    testPrivatePtr priv = (testPrivatePtr) conn->privateData;
478 479 480

    if (gettimeofday(&tv, NULL) < 0) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
481
        return VIR_DRV_OPEN_ERROR;
482 483
    }

484
    priv->handle = connid;
485 486 487 488 489
    node->connections[connid].active = 1;
    memmove(&node->connections[connid].nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    node->connections[connid].numDomains = 1;
    node->connections[connid].domains[0].active = 1;
490
    node->connections[connid].domains[0].id = nextDomID++;
491 492 493 494
    node->connections[connid].domains[0].onReboot = VIR_DOMAIN_RESTART;
    node->connections[connid].domains[0].onCrash = VIR_DOMAIN_RESTART;
    node->connections[connid].domains[0].onPoweroff = VIR_DOMAIN_DESTROY;
    strcpy(node->connections[connid].domains[0].name, "test");
495
    for (u = 0 ; u < VIR_UUID_BUFLEN ; u++) {
496 497 498 499 500 501 502 503
        node->connections[connid].domains[0].uuid[u] = (u * 75)%255;
    }
    node->connections[connid].domains[0].info.maxMem = 8192 * 1024;
    node->connections[connid].domains[0].info.memory = 2048 * 1024;
    node->connections[connid].domains[0].info.state = VIR_DOMAIN_RUNNING;
    node->connections[connid].domains[0].info.nrVirtCpu = 2;
    node->connections[connid].domains[0].info.cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
    return (0);
504 505 506 507
}


static char *testBuildFilename(const char *relativeTo,
508 509 510 511 512 513 514 515
                               const char *filename) {
    char *offset;
    int baseLen;
    if (!filename || filename[0] == '\0')
        return (NULL);
    if (filename[0] == '/')
        return strdup(filename);

516
    offset = strrchr(relativeTo, '/');
517 518 519 520 521 522 523 524 525
    if ((baseLen = (offset-relativeTo+1))) {
        char *absFile = malloc(baseLen + strlen(filename) + 1);
        strncpy(absFile, relativeTo, baseLen);
        absFile[baseLen] = '\0';
        strcat(absFile, filename);
        return absFile;
    } else {
        return strdup(filename);
    }
526 527 528
}

static int testOpenFromFile(virConnectPtr conn,
529 530
                            int connid,
                            const char *file) {
531 532 533
    int fd, i, ret;
    long l;
    char *str;
534 535
    xmlDocPtr xml;
    xmlNodePtr root = NULL;
536
    xmlNodePtr *domains;
537 538
    xmlXPathContextPtr ctxt = NULL;
    virNodeInfoPtr nodeInfo;
539
    testPrivatePtr priv = (testPrivatePtr) conn->privateData;
540 541 542

    if ((fd = open(file, O_RDONLY)) < 0) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("loading host definition file"));
543
        return VIR_DRV_OPEN_ERROR;
544 545
    }

546 547 548 549 550
    if (!(xml = xmlReadFd(fd, file, NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("host"));
        goto error;
551
    }
552 553
    close(fd);
    fd = -1;
554

555 556 557 558
    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
        testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node"));
        goto error;
559 560
    }

561 562 563 564
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("creating xpath context"));
        goto error;
565
    }
566

567
    priv->handle = connid;
568 569 570 571 572
    node->connections[connid].active = 1;
    node->connections[connid].numDomains = 0;
    memmove(&node->connections[connid].nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    nodeInfo = &node->connections[connid].nodeInfo;
573 574 575 576
    ret = virXPathLong("string(/node/cpu/nodes[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->nodes = l;
    } else if (ret == -2) {
577
	testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu numa nodes"));
578
	goto error;
579
    }
580

581 582 583 584
    ret = virXPathLong("string(/node/cpu/sockets[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->sockets = l;
    } else if (ret == -2) {
585
	testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu sockets"));
586
	goto error;
587
    }
588

589 590 591 592
    ret = virXPathLong("string(/node/cpu/cores[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->cores = l;
    } else if (ret == -2) {
593
	testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu cores"));
594
	goto error;
595 596
    }

597 598 599 600
    ret = virXPathLong("string(/node/cpu/threads[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
601
	testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu threads"));
602
	goto error;
603
    }
604

605
    nodeInfo->cpus = nodeInfo->cores * nodeInfo->threads * nodeInfo->sockets * nodeInfo->nodes;
606 607 608 609 610 611
    ret = virXPathLong("string(/node/cpu/active[1])", ctxt, &l);
    if (ret == 0) {
        if (l < nodeInfo->cpus) {
	    nodeInfo->cpus = l;
	}
    } else if (ret == -2) {
612
	testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node active cpu"));
613 614 615 616 617 618
	goto error;
    }
    ret = virXPathLong("string(/node/cpu/mhz[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->mhz = l;
    } else if (ret == -2) {
619
        testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu mhz"));
620 621 622 623 624 625
	goto error;
    }

    str = virXPathString("string(/node/cpu/model[1])", ctxt);
    if (str != NULL) {
        strncpy(nodeInfo->model, str, sizeof(nodeInfo->model)-1);
626
        nodeInfo->model[sizeof(nodeInfo->model)-1] = '\0';
627
        free(str);
628 629
    }

630 631 632 633
    ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
634
        testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node memory"));
635
	goto error;
636
    }
637

638 639
    ret = virXPathNodeSet("/node/domain", ctxt, &domains);
    if (ret < 0) {
640 641
        testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node domain list"));
        goto error;
642
    }
643

644 645
    for (i = 0 ; i < ret ; i++) {
        xmlChar *domFile = xmlGetProp(domains[i], BAD_CAST "file");
646 647 648 649 650 651 652 653 654 655 656 657 658
        char *absFile = testBuildFilename(file, (const char *)domFile);
        int domid = nextDomID++;
        free(domFile);
        if (!absFile) {
            testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("resolving domain filename"));
            goto error;
        }
        if (testLoadDomainFromFile(conn, domid, absFile) != 0) {
            free(absFile);
            goto error;
        }
        free(absFile);
        node->connections[connid].numDomains++;
659
    }
660 661
    if (domains != NULL)
        free(domains);
662

663
    xmlFreeDoc(xml);
664

665
    return (0);
666 667

 error:
668 669 670 671 672 673
    if (node->connections[connid].active) {
        for (i = 0 ; i <node->connections[connid].numDomains ; i++) {
            node->connections[connid].domains[i].active = 0;
        }
        node->connections[connid].numDomains = 0;
        node->connections[connid].active = 0;
674
    }
675 676 677 678
    if (xml)
        xmlFreeDoc(xml);
    if (fd != -1)
        close(fd);
679
    return VIR_DRV_OPEN_ERROR;
680 681 682
}

static int getNextConnection(void) {
683 684 685 686 687 688 689 690 691 692 693 694 695 696
    int i;
    if (node == NULL) {
        node = calloc(1, sizeof(testNode));
        nextDomID = 1;
        if (!node) {
            testError(NULL, NULL, VIR_ERR_NO_MEMORY, _("allocating node"));
            return (-1);
        }
    }

    for (i = 0 ; i < MAX_CONNECTIONS ; i++) {
        if (!node->connections[i].active) {
            return (i);
        }
697
    }
698 699
    return (-1);
}
700

701 702 703
static int getDomainIndex(virDomainPtr domain) {
    int i;
    testCon *con;
704 705 706
    testPrivatePtr priv = (testPrivatePtr) domain->conn->privateData;

    con = &node->connections[priv->handle];
707
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
708 709
        if (domain->id >= 0) {
            if (domain->id == con->domains[i].id)
710 711 712 713 714
                return (i);
        } else {
            if (!strcmp(domain->name, con->domains[i].name))
                return (i);
        }
715
    }
716
    return (-1);
717
}
718 719 720

int testOpen(virConnectPtr conn,
             const char *name,
721
             int flags ATTRIBUTE_UNUSED)
722
{
723 724
    xmlURIPtr uri;
    int ret, connid;
725
    testPrivatePtr priv;
726

727 728
    if (!name)
        return VIR_DRV_OPEN_DECLINED;
729

730 731
    uri = xmlParseURI(name);
    if (uri == NULL) {
732
        return VIR_DRV_OPEN_DECLINED;
733
    }
734

735
    if (!uri->scheme || strcmp(uri->scheme, "test") != 0) {
736
        xmlFreeURI(uri);
737
        return VIR_DRV_OPEN_DECLINED;
738
    }
739

740 741 742 743 744 745
    /* Remote driver should handle these. */
    if (uri->server) {
        xmlFreeURI(uri);
        return VIR_DRV_OPEN_DECLINED;
    }

746 747 748 749 750
    if (uri->server) {
        xmlFreeURI(uri);
        return VIR_DRV_OPEN_DECLINED;
    }

751 752 753 754
    /* From this point on, the connection is for us. */
    if (!uri->path
        || uri->path[0] == '\0'
        || (uri->path[0] == '/' && uri->path[1] == '\0')) {
755
        testError (NULL, NULL, VIR_ERR_INVALID_ARG,
756 757 758
                   _("testOpen: supply a path or use test:///default"));
        return VIR_DRV_OPEN_ERROR;
    }
759

760 761
    if ((connid = getNextConnection()) < 0) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("too many connections"));
762
        return VIR_DRV_OPEN_ERROR;
763 764
    }

765
    /* Allocate per-connection private data. */
766
    priv = conn->privateData = calloc (1, sizeof (struct _testPrivate));
767
    if (!priv) {
768
        testError(NULL, NULL, VIR_ERR_NO_MEMORY, _("allocating private data"));
769 770 771
        return VIR_DRV_OPEN_ERROR;
    }
    priv->handle = -1;
772 773 774 775 776
    priv->path = strdup (uri->path);
    if (!priv->path) {
        testError (NULL, NULL, VIR_ERR_NO_MEMORY, _("allocating path"));
        return VIR_DRV_OPEN_ERROR;
    }
777

778
    if (strcmp(uri->path, "/default") == 0) {
779 780 781 782 783 784 785 786 787 788
        ret = testOpenDefault(conn,
                              connid);
    } else {
        ret = testOpenFromFile(conn,
                               connid,
                               uri->path);
    }

    xmlFreeURI(uri);

789 790
    if (ret < 0) free (conn->privateData);

791
    return (ret);
792 793 794 795
}

int testClose(virConnectPtr conn)
{
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
    testPrivatePtr priv;

    if (!conn) {
        testError (NULL, NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return -1;
    }

    priv = (testPrivatePtr) conn->privateData;
    if (!priv) {
        testError (NULL, NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return -1;
    }

    if (priv->handle >= 0) {
        testCon *con = &node->connections[priv->handle];
        con->active = 0;
        memset (con, 0, sizeof *con); // RWMJ - why?
    }

815
    free (priv->path);
816 817
    free (priv);
    return 0;
818 819
}

820
int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
821 822
                   unsigned long *hvVer)
{
823 824
    *hvVer = 2;
    return (0);
825 826
}

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
char *
testGetHostname (virConnectPtr conn)
{
    int r;
    char hostname [HOST_NAME_MAX+1], *str;

    r = gethostname (hostname, HOST_NAME_MAX+1);
    if (r == -1) {
        testError (conn, NULL, VIR_ERR_SYSTEM_ERROR, strerror (errno));
        return NULL;
    }
    str = strdup (hostname);
    if (str == NULL) {
        testError (conn, NULL, VIR_ERR_SYSTEM_ERROR, strerror (errno));
        return NULL;
    }
    return str;
}

char *
testGetURI (virConnectPtr conn)
{
    testPrivatePtr priv = (testPrivatePtr) conn->privateData;
    char *uri;

    if (asprintf (&uri, "test://%s", priv->path) == -1) {
        testError (conn, NULL, VIR_ERR_SYSTEM_ERROR, strerror (errno));
        return NULL;
    }
    return uri;
}

859
int testNodeGetInfo(virConnectPtr conn,
860 861
                    virNodeInfoPtr info)
{
862 863
    testPrivatePtr priv = (testPrivatePtr) conn->privateData;
    testCon *con = &node->connections[priv->handle];
864 865
    memcpy(info, &con->nodeInfo, sizeof(virNodeInfo));
    return (0);
866 867
}

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
char *
testGetCapabilities (virConnectPtr conn)
{
    static char caps[] = "\
<capabilities>\n\
  <host>\n\
    <cpu>\n\
      <arch>" TEST_MODEL "</arch>\n\
      <features>\n\
        <pae/>\n\
        <nonpae/>\n\
      </features>\n\
    </cpu>\n\
  </host>\n\
\n\
  <guest>\n\
    <os_type>linux</os_type>\n\
    <arch name=\"" TEST_MODEL "\">\n\
      <wordsize>" TEST_MODEL_WORDSIZE "</wordsize>\n\
      <domain type=\"test\"/>\n\
    </arch>\n\
    <features>\n\
      <pae/>\n\
      <nonpae/>\n\
    </features>\n\
  </guest>\n\
</capabilities>\n\
";

    char *caps_copy = strdup (caps);
    if (!caps_copy) {
        testError(conn, NULL, VIR_ERR_NO_MEMORY, __FUNCTION__);
        return NULL;
    }
    return caps_copy;
}

905 906
int testNumOfDomains(virConnectPtr conn)
{
907
    int numActive = 0, i;
908 909
    testPrivatePtr priv = (testPrivatePtr) conn->privateData;
    testCon *con = &node->connections[priv->handle];
910 911 912 913 914 915 916
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
        if (!con->domains[i].active ||
            con->domains[i].info.state == VIR_DOMAIN_SHUTOFF)
            continue;
        numActive++;
    }
    return (numActive);
917 918
}

919 920
virDomainPtr
testDomainCreateLinux(virConnectPtr conn, const char *xmlDesc,
921
                      unsigned int flags ATTRIBUTE_UNUSED)
922
{
923 924 925
    testCon *con;
    int domid, handle = -1, i;
    virDomainPtr dom;
926
    testPrivatePtr priv;
927

928 929 930 931 932 933 934 935 936 937 938 939
    if (!VIR_IS_CONNECT(conn)) {
        testError(conn, NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (xmlDesc == NULL) {
        testError(conn, NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }
    if (conn->flags & VIR_CONNECT_RO) {
        testError(conn, NULL, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (NULL);
    }
940 941

    priv = (testPrivatePtr) conn->privateData;
942
  
943
    con = &node->connections[priv->handle];
944 945 946 947 948 949 950 951 952 953

    if (con->numDomains == MAX_DOMAINS) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("too many domains"));
        return (NULL);
    }

    domid = nextDomID++;
    if (testLoadDomainFromDoc(conn, domid, xmlDesc) < 0)
        return (NULL);
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
954
        if (con->domains[i].id == domid) {
955 956 957 958 959 960 961 962 963 964 965
            handle = i;
            break;
        }
    }
    dom = virGetDomain(conn, con->domains[handle].name, con->domains[handle].uuid);
    if (dom == NULL) {
        testError(conn, NULL, VIR_ERR_NO_MEMORY, _("allocating domain"));
        return (NULL);
    }
    con->numDomains++;
    return (dom);
966 967 968
}


969 970 971
virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                  int id)
{
972 973
    testPrivatePtr priv = (testPrivatePtr) conn->privateData;
    testCon *con = &node->connections[priv->handle];
974 975 976 977 978
    virDomainPtr dom;
    int i, idx = -1;

    for (i = 0 ; i < MAX_DOMAINS ; i++) {
        if (con->domains[i].active &&
979
            con->domains[i].id == id) {
980 981 982 983 984 985
            idx = i;
            break;
        }
    }

    if (idx < 0) {
986
        testError (conn, NULL, VIR_ERR_NO_DOMAIN, NULL);
987 988 989 990 991 992 993 994
        return(NULL);
    }

    dom = virGetDomain(conn, con->domains[idx].name, con->domains[idx].uuid);
    if (dom == NULL) {
        testError(conn, NULL, VIR_ERR_NO_MEMORY, _("allocating domain"));
        return(NULL);
    }
995
    dom->id = id;
996
    return (dom);
997 998 999 1000 1001
}

virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                    const unsigned char *uuid)
{
1002 1003
    testPrivatePtr priv = (testPrivatePtr) conn->privateData;
    testCon *con = &node->connections[priv->handle];
1004
    virDomainPtr dom;
1005
    int i, idx = -1;
1006

1007 1008
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
        if (con->domains[i].active &&
1009
            memcmp(uuid, con->domains[i].uuid, VIR_UUID_BUFLEN) == 0) {
1010 1011 1012
            idx = i;
            break;
        }
1013
    }
1014 1015 1016 1017

    if (idx < 0) {
        testError (conn, NULL, VIR_ERR_NO_DOMAIN, NULL);
        return NULL;
1018
    }
1019 1020 1021 1022 1023 1024 1025 1026 1027

    dom = virGetDomain(conn, con->domains[idx].name, con->domains[idx].uuid);
    if (dom == NULL) {
        testError(conn, NULL, VIR_ERR_NO_MEMORY, _("allocating domain"));
        return NULL;
    }
    dom->id = con->domains[idx].id;

    return dom;
1028 1029 1030 1031 1032
}

virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                    const char *name)
{
1033 1034
    testPrivatePtr priv = (testPrivatePtr) conn->privateData;
    testCon *con = &node->connections[priv->handle];
1035
    virDomainPtr dom;
1036
    int i, idx = -1;
1037

1038 1039 1040 1041 1042 1043
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
        if (con->domains[i].active &&
            strcmp(name, con->domains[i].name) == 0) {
            idx = i;
            break;
        }
1044
    }
1045 1046 1047 1048

    if (idx < 0) {
        testError (conn, NULL, VIR_ERR_NO_DOMAIN, NULL);
        return NULL;
1049
    }
1050 1051 1052 1053 1054 1055 1056 1057 1058

    dom = virGetDomain(conn, con->domains[idx].name, con->domains[idx].uuid);
    if (dom == NULL) {
        testError(conn, NULL, VIR_ERR_NO_MEMORY, _("allocating domain"));
        return NULL;
    }
    dom->id = con->domains[idx].id;

    return dom;
1059 1060 1061 1062 1063 1064
}

int testListDomains (virConnectPtr conn,
                     int *ids,
                     int maxids)
{
1065 1066
    testPrivatePtr priv = (testPrivatePtr) conn->privateData;
    testCon *con = &node->connections[priv->handle];
1067 1068 1069 1070 1071
    int n, i;

    for (i = 0, n = 0 ; i < MAX_DOMAINS && n < maxids ; i++) {
        if (con->domains[i].active &&
            con->domains[i].info.state != VIR_DOMAIN_SHUTOFF) {
1072
            ids[n++] = con->domains[i].id;
1073
        }
1074
    }
1075
    return (n);
1076 1077 1078 1079
}

int testDestroyDomain (virDomainPtr domain)
{
1080 1081
    testCon *con;
    int domidx;
1082 1083
    testPrivatePtr priv;

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }

1095 1096 1097
    priv = (testPrivatePtr) domain->conn->privateData;

    con = &node->connections[priv->handle];
1098 1099
    con->domains[domidx].active = 0;
    return (0);
1100 1101 1102 1103
}

int testResumeDomain (virDomainPtr domain)
{
1104 1105
    testCon *con;
    int domidx;
1106 1107
    testPrivatePtr priv;

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
1118

1119 1120 1121
    priv = (testPrivatePtr) domain->conn->privateData;

    con = &node->connections[priv->handle];
1122 1123
    con->domains[domidx].info.state = VIR_DOMAIN_RUNNING;
    return (0);
1124 1125 1126 1127
}

int testPauseDomain (virDomainPtr domain)
{
1128 1129
    testCon *con;\
    int domidx;
1130 1131
    testPrivatePtr priv;

1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
1142

1143 1144 1145
    priv = (testPrivatePtr) domain->conn->privateData;

    con = &node->connections[priv->handle];
1146 1147
    con->domains[domidx].info.state = VIR_DOMAIN_PAUSED;
    return (0);
1148 1149
}

1150 1151 1152 1153 1154 1155
/* We don't do an immediate shutdown. We basically pretend that
   out shutdown sequence takes 'n' seconds to complete. SO, here
   we just set state to shutdown, and subsquent calls to getDomainInfo
   will check to see if shutdown ought to be marked complete. */
int testShutdownDomain (virDomainPtr domain)
{
1156 1157 1158
    testCon *con;
    int domidx;
    struct timeval tv;
1159 1160
    testPrivatePtr priv;

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
1171

1172 1173 1174
    priv = (testPrivatePtr) domain->conn->privateData;

    con = &node->connections[priv->handle];
1175

1176 1177 1178 1179
    if (gettimeofday(&tv, NULL) < 0) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
        return (-1);
    }
1180

1181
    con->domains[domidx].info.state = VIR_DOMAIN_SHUTOFF;
1182 1183
    domain->id = -1;
    con->domains[domidx].id = -1;
1184 1185

    return (0);
1186 1187 1188 1189 1190
}

/* Similar behaviour as shutdown */
int testRebootDomain (virDomainPtr domain, virDomainRestart action)
{
1191 1192 1193
    testCon *con;
    int domidx;
    struct timeval tv;
1194 1195
    testPrivatePtr priv;

1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
1206

1207 1208
    priv = (testPrivatePtr) domain->conn->privateData;
    con = &node->connections[priv->handle];
1209

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
    if (gettimeofday(&tv, NULL) < 0) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
        return (-1);
    }

    if (!action)
        action = VIR_DOMAIN_RESTART;

    con->domains[domidx].info.state = VIR_DOMAIN_SHUTDOWN;
    switch (action) {
    case VIR_DOMAIN_DESTROY:
        con->domains[domidx].info.state = VIR_DOMAIN_SHUTOFF;
        break;

    case VIR_DOMAIN_RESTART:
        con->domains[domidx].info.state = VIR_DOMAIN_RUNNING;
        break;

    case VIR_DOMAIN_PRESERVE:
        con->domains[domidx].info.state = VIR_DOMAIN_SHUTOFF;
        break;

    case VIR_DOMAIN_RENAME_RESTART:
        con->domains[domidx].info.state = VIR_DOMAIN_RUNNING;
        break;
1235

1236 1237 1238 1239
    default:
        con->domains[domidx].info.state = VIR_DOMAIN_SHUTOFF;
        break;
    }
1240 1241
    domain->id = -1;
    con->domains[domidx].id = -1;
1242

1243
    return (0);
1244 1245
}

1246 1247 1248
int testGetDomainInfo (virDomainPtr domain,
                       virDomainInfoPtr info)
{
1249 1250 1251
    struct timeval tv;
    testCon *con;
    int domidx;
1252 1253
    testPrivatePtr priv;

1254 1255 1256 1257 1258 1259 1260
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }

1261 1262
    priv = (testPrivatePtr) domain->conn->privateData;
    con = &node->connections[priv->handle];
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

    if (gettimeofday(&tv, NULL) < 0) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
        return (-1);
    }

    if (con->domains[domidx].info.state == VIR_DOMAIN_SHUTOFF) {
        con->domains[domidx].info.cpuTime = 0;
        con->domains[domidx].info.memory = 0;
    } else {
        con->domains[domidx].info.cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
    }
    memcpy(info, &con->domains[domidx].info, sizeof(virDomainInfo));
    return (0);
1277 1278
}

1279
char *testGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
1280
    return strdup("linux");
1281 1282
}

1283
unsigned long testGetMaxMemory(virDomainPtr domain) {
1284 1285
    testCon *con;
    int domidx;
1286 1287
    testPrivatePtr priv;

1288 1289 1290 1291 1292 1293 1294
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }

1295 1296
    priv = (testPrivatePtr) domain->conn->privateData;
    con = &node->connections[priv->handle];
1297
    return con->domains[domidx].info.maxMem;
1298 1299
}

1300 1301
int testSetMaxMemory(virDomainPtr domain,
                     unsigned long memory)
1302
{
1303 1304
    testCon *con;
    int domidx;
1305 1306
    testPrivatePtr priv;

1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
1317

1318 1319
    priv = (testPrivatePtr) domain->conn->privateData;
    con = &node->connections[priv->handle];
1320 1321 1322
    /* XXX validate not over host memory wrt to other domains */
    con->domains[domidx].info.maxMem = memory;
    return (0);
1323 1324
}

1325 1326
int testSetMemory(virDomainPtr domain,
                  unsigned long memory)
1327
{
1328 1329
    testCon *con;
    int domidx;
1330 1331
    testPrivatePtr priv;

1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
1342

1343 1344
    priv = (testPrivatePtr) domain->conn->privateData;
    con = &node->connections[priv->handle];
1345

1346 1347 1348 1349
    if (memory > con->domains[domidx].info.maxMem) {
        testError(domain->conn, domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
1350

1351 1352
    con->domains[domidx].info.memory = memory;
    return (0);
1353 1354 1355
}

int testSetVcpus(virDomainPtr domain,
1356 1357 1358
                 unsigned int nrCpus) {
    testCon *con;
    int domidx;
1359 1360
    testPrivatePtr priv;

1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
1371

1372 1373
    priv = (testPrivatePtr) domain->conn->privateData;
    con = &node->connections[priv->handle];
1374

1375 1376 1377 1378 1379
    /* We allow more cpus in guest than host */
    if (nrCpus > 32) {
        testError(domain->conn, domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
1380

1381 1382
    con->domains[domidx].info.nrVirtCpu = nrCpus;
    return (0);
1383 1384 1385 1386
}

char * testDomainDumpXML(virDomainPtr domain, int flags ATTRIBUTE_UNUSED)
{
1387 1388 1389 1390 1391
    virBufferPtr buf;
    char *xml;
    unsigned char *uuid;
    testCon *con;
    int domidx;
1392 1393
    testPrivatePtr priv;

1394 1395 1396 1397 1398 1399 1400
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (NULL);
    }

1401 1402
    priv = (testPrivatePtr) domain->conn->privateData;
    con = &node->connections[priv->handle];
1403 1404

    if (!(buf = virBufferNew(4000))) {
1405
        testError(domain->conn, domain, VIR_ERR_NO_MEMORY, __FUNCTION__);
1406 1407 1408
        return (NULL);
    }

1409
    virBufferVSprintf(buf, "<domain type='test' id='%d'>\n", domain->id);
1410 1411 1412 1413 1414 1415 1416 1417 1418
    virBufferVSprintf(buf, "  <name>%s</name>\n", domain->name);
    uuid = domain->uuid;
    virBufferVSprintf(buf,
                      "  <uuid>%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x</uuid>\n",
                      uuid[0], uuid[1], uuid[2], uuid[3],
                      uuid[4], uuid[5], uuid[6], uuid[7],
                      uuid[8], uuid[9], uuid[10], uuid[11],
                      uuid[12], uuid[13], uuid[14], uuid[15]);

1419
    virBufferVSprintf(buf, "  <memory>%lu</memory>\n", con->domains[domidx].info.maxMem);
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
    virBufferVSprintf(buf, "  <vcpu>%d</vcpu>\n", con->domains[domidx].info.nrVirtCpu);
    virBufferVSprintf(buf, "  <on_reboot>%s</on_reboot>\n", testRestartFlagToString(con->domains[domidx].onReboot));
    virBufferVSprintf(buf, "  <on_poweroff>%s</on_poweroff>\n", testRestartFlagToString(con->domains[domidx].onPoweroff));
    virBufferVSprintf(buf, "  <on_crash>%s</on_crash>\n", testRestartFlagToString(con->domains[domidx].onCrash));

    virBufferAdd(buf, "</domain>\n", -1);

    xml = buf->content;
    free(buf);

    return (xml);
1431
}
1432 1433 1434 1435


int testNumOfDefinedDomains(virConnectPtr conn) {
    int numInactive = 0, i;
1436 1437
    testPrivatePtr priv = (testPrivatePtr) conn->privateData;
    testCon *con = &node->connections[priv->handle];
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
        if (!con->domains[i].active ||
            con->domains[i].info.state != VIR_DOMAIN_SHUTOFF)
            continue;
        numInactive++;
    }
    return (numInactive);
}

int testListDefinedDomains(virConnectPtr conn,
1448
                           char **const names,
1449
                           int maxnames) {
1450 1451
    testPrivatePtr priv = (testPrivatePtr) conn->privateData;
    testCon *con = &node->connections[priv->handle];
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
    int n = 0, i;

    for (i = 0, n = 0 ; i < MAX_DOMAINS && n < maxnames ; i++) {
        if (con->domains[i].active &&
            con->domains[i].info.state == VIR_DOMAIN_SHUTOFF) {
            names[n++] = strdup(con->domains[i].name);
        }
    }
    return (n);
}

virDomainPtr testDomainDefineXML(virConnectPtr conn,
                                 const char *doc) {
    int ret;
    xmlDocPtr xml;
    int domid;

    if (!(xml = xmlReadDoc(BAD_CAST doc, "domain.xml", NULL,
                           XML_PARSE_NOENT | XML_PARSE_NONET |
                           XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
        testError(NULL, NULL, VIR_ERR_XML_ERROR, _("domain"));
        return (NULL);
    }

    domid = nextDomID++;
    ret = testLoadDomain(conn, domid, xml);

    xmlFreeDoc(xml);

    if (ret < 0)
        return (NULL);

    return testLookupDomainByID(conn, domid);
}

int testDomainCreate(virDomainPtr domain) {
    testCon *con;
    int domidx;
1490 1491
    testPrivatePtr priv;

1492 1493 1494 1495 1496 1497 1498
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }

1499 1500
    priv = (testPrivatePtr) domain->conn->privateData;
    con = &node->connections[priv->handle];
1501 1502 1503 1504 1505 1506 1507

    if (con->domains[domidx].info.state != VIR_DOMAIN_SHUTOFF) {
        testError(domain->conn, domain, VIR_ERR_INTERNAL_ERROR,
                  _("Domain is already running"));
        return (-1);
    }

1508
    domain->id = con->domains[domidx].id = nextDomID++;
1509 1510 1511 1512 1513 1514 1515 1516
    con->domains[domidx].info.state = VIR_DOMAIN_RUNNING;

    return (0);
}

int testDomainUndefine(virDomainPtr domain) {
    testCon *con;
    int domidx;
1517 1518
    testPrivatePtr priv;

1519 1520 1521 1522 1523 1524 1525
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }

1526 1527
    priv = (testPrivatePtr) domain->conn->privateData;
    con = &node->connections[priv->handle];
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538

    if (con->domains[domidx].info.state != VIR_DOMAIN_SHUTOFF) {
        testError(domain->conn, domain, VIR_ERR_INTERNAL_ERROR,
                  _("Domain is still running"));
        return (-1);
    }

    con->domains[domidx].active = 0;

    return (0);
}
1539
#endif /* WITH_TEST */
1540

1541 1542 1543 1544 1545
/*
 * vim: set tabstop=4:
 * vim: set shiftwidth=4:
 * vim: set expandtab:
 */
1546 1547 1548 1549 1550 1551 1552 1553
/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */