test.c 8.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
/*
 * test.c: A "mock" hypervisor for use by application unit tests
 *
 * Copyright (C) 2006 Red Hat, Inc.
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Berrange <berrange@redhat.com>
 */

#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <libxml/uri.h>

#include "internal.h"
#include "test.h"

static virDriver testDriver = {
  "Test",
  NULL, /* init */
  testOpen, /* open */
  testClose, /* close */
  NULL, /* type */
  testGetVersion, /* version */
  testNodeGetInfo, /* nodeGetInfo */
  testListDomains, /* listDomains */
  testNumOfDomains, /* numOfDomains */
  NULL, /* domainCreateLinux */
  testLookupDomainByID, /* domainLookupByID */
  testLookupDomainByUUID, /* domainLookupByUUID */
  testLookupDomainByName, /* domainLookupByName */
  testPauseDomain, /* domainSuspend */
  testResumeDomain, /* domainResume */
  NULL, /* domainShutdown */
  NULL, /* domainReboot */
  testDestroyDomain, /* domainDestroy */
  NULL, /* domainFree */
  NULL, /* domainGetName */
  NULL, /* domainGetID */
  NULL, /* domainGetUUID */
  NULL, /* domainGetOSType */
  NULL, /* domainGetMaxMemory */
  testSetMaxMemory, /* domainSetMaxMemory */
  NULL, /* domainSetMemory */
  testGetDomainInfo, /* domainGetInfo */
  NULL, /* domainSave */
  NULL /* domainRestore */
};

typedef struct _testDev {
  char name[20];
  virDeviceMode mode;
} testDev;

#define MAX_DEVICES 10

typedef struct _testDom {
  int active;
  char name[20];
  unsigned char uuid[16];
  virDomainKernel kernel;
  virDomainInfo info;
  virDomainRestart onRestart;
  int numDevices;
  testDev devices[MAX_DEVICES];
} testDom;

#define MAX_DOMAINS 20

typedef struct _testCon {
  int active;
  int numDomains;
  testDom domains[MAX_DOMAINS];
} testCon;

#define MAX_CONNECTIONS 5

typedef struct _testNode {
  int numConnections;
  testCon connections[MAX_CONNECTIONS];
} 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;

static virNodeInfo nodeInfo = {
  "i86",
  1024*1024*3, /* 3 GB */
  16,
  1400,
  2,
  2,
  2,
  2,
};

static void
testError(virConnectPtr con,
	  virDomainPtr dom,
	  virErrorNumber error,
	  const char *info)
{
  const char *errmsg;

  if (error == VIR_ERR_OK)
    return;

  errmsg = __virErrorMsg(error, info);
  __virRaiseError(con, dom, VIR_FROM_XEN, error, VIR_ERR_ERROR,
		  errmsg, info, NULL, 0, 0, errmsg, info, 0);
}


/**
 * testRegister:
 *
 * Registers the test driver
 */
void testRegister(void)
{
  virRegisterDriver(&testDriver);
}


int testOpen(virConnectPtr conn,
             const char *name,
             int flags)
{
  xmlURIPtr uri;
  int i, j;

  if (!name) {
    return -1;
  }

  uri = xmlParseURI(name);
  if (uri == NULL) {
    if (!(flags & VIR_DRV_OPEN_QUIET))
      testError(conn, NULL, VIR_ERR_NO_SUPPORT, name);
    return(-1);
  }

  if (!uri->scheme ||
      strcmp(uri->scheme, "test") ||
      !uri->path ||
      strcmp(uri->path, "/default")) {
    xmlFreeURI(uri);
    return -1;
  }


  xmlFreeURI(uri);

  if (node == NULL) {
    node = calloc(1, sizeof(testNode));
    if (!node) {
      testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, "cannot allocate memory");
      return -1;
    }
  }

  for (i = 0 ; i < MAX_CONNECTIONS ; i++) {
    if (!node->connections[i].active) {
      struct timeval tv;

      if (gettimeofday(&tv, NULL) < 0) {
	testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, "cannot get timeofday");
	return -1;
      }

      conn->handle = i;
      node->connections[i].active = 1;

      node->connections[i].numDomains = 1;
      node->connections[i].domains[0].active = 1;
      strcpy(node->connections[i].domains[0].name, "Domain-0");
      for (j = 0 ; j < 16 ; j++) {
	node->connections[i].domains[0].uuid[j] = (j * 75)%255;
      }
      node->connections[i].domains[0].info.maxMem = 8192 * 1024;
      node->connections[i].domains[0].info.memory = 2048 * 1024;
      node->connections[i].domains[0].info.state = VIR_DOMAIN_RUNNING;
      node->connections[i].domains[0].info.nrVirtCpu = 2;
      node->connections[i].domains[0].info.cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
      return 0;
    }
  }


  testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, "too make connections");
  return -1;
}

int testClose(virConnectPtr conn)
{
  testCon *con = &node->connections[conn->handle];
  con->active = 0;
  conn->handle = -1;
  memset(con, 0, sizeof(testCon));
  return 0;
}

int testGetVersion(virConnectPtr conn,
                   unsigned long *hvVer)
{
  *hvVer = 1;
  return 0;
}

int testNodeGetInfo(virConnectPtr conn,
                    virNodeInfoPtr info)
{
  memcpy(info, &nodeInfo, sizeof(nodeInfo));
  return 0;
}

int testNumOfDomains(virConnectPtr conn)
{
  testCon *con = &node->connections[conn->handle];
  return con->numDomains;
}

virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                  int id)
{
  testCon *con = &node->connections[conn->handle];
  virDomainPtr dom;
  if (!con->domains[id].active) {
    return NULL;
  }
  dom = virGetDomain(conn, con->domains[id].name, con->domains[id].uuid);
  if (dom == NULL) {
    testError(conn, NULL, VIR_ERR_NO_MEMORY, "Allocating domain");
    return(NULL);
  }
  dom->handle = id;
  return dom;
}

virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                    const unsigned char *uuid)
{
  testCon *con = &node->connections[conn->handle];
  virDomainPtr dom = NULL;
  int i, id = -1;
  for (i = 0 ; i < MAX_DOMAINS ; i++) {
    if (con->domains[i].active &&
	memcmp(uuid, con->domains[i].uuid, 16) == 0) {
      id = i;
      break;
    }
  }
  if (id >= 0) {
    dom = virGetDomain(conn, con->domains[id].name, con->domains[id].uuid);
    if (dom == NULL) {
      testError(conn, NULL, VIR_ERR_NO_MEMORY, "Allocating domain");
      return(NULL);
    }
    dom->handle = id;
  }
  return dom;
}

virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                    const char *name)
{
  testCon *con = &node->connections[conn->handle];
  virDomainPtr dom = NULL;
  int i, id = -1;
  for (i = 0 ; i < MAX_DOMAINS ; i++) {
    if (con->domains[i].active &&
	strcmp(name, con->domains[i].name) == 0) {
      id = i;
      break;
    }
  }
  if (id >= 0) {
    dom = virGetDomain(conn, con->domains[id].name, con->domains[id].uuid);
    if (dom == NULL) {
      testError(conn, NULL, VIR_ERR_NO_MEMORY, "Allocating domain");
      return(NULL);
    }
    dom->handle = id;
  }
  return dom;
}

int testListDomains (virConnectPtr conn,
                     int *ids,
                     int maxids)
{
  testCon *con = &node->connections[conn->handle];
  int n, i;

  for (i = 0, n = 0 ; i < MAX_DOMAINS && n < maxids ; i++) {
    if (con->domains[i].active) {
      ids[n++] = i;
    }
  }
  return n;
}

int testDestroyDomain (virDomainPtr domain)
{
  testCon *con = &node->connections[domain->conn->handle];
  con->domains[domain->handle].active = 0;
  return 0;
}

int testResumeDomain (virDomainPtr domain)
{
  testCon *con = &node->connections[domain->conn->handle];
  con->domains[domain->handle].info.state = VIR_DOMAIN_RUNNING;
  return 0;
}

int testPauseDomain (virDomainPtr domain)
{
  testCon *con = &node->connections[domain->conn->handle];
  con->domains[domain->handle].info.state = VIR_DOMAIN_PAUSED;
  return 0;
}

int testGetDomainInfo (virDomainPtr domain,
                       virDomainInfoPtr info)
{
  testCon *con = &node->connections[domain->conn->handle];
  struct timeval tv;
  if (gettimeofday(&tv, NULL) < 0) {
    testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, "cannot get timeofday");
    return -1;
  }

  con->domains[domain->handle].info.cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
  memcpy(info, &con->domains[domain->handle].info, sizeof(virDomainInfo));
  return 0;
}

int testSetMaxMemory (virDomainPtr domain,
                      unsigned long memory)
{
  testCon *con = &node->connections[domain->conn->handle];
  con->domains[domain->handle].info.maxMem = memory;
  return 0;
}