virshtest.c 8.7 KB
Newer Older
1
#include <config.h>
2 3 4 5

#include <stdio.h>
#include <string.h>
#include <unistd.h>
6

7
#include "internal.h"
8 9 10 11
#include "xml.h"
#include "testutils.h"

static char *progname;
12
static char *abs_srcdir;
13 14 15
#define MAX_FILE 4096

static int testFilterLine(char *buffer,
16
                          const char *toRemove) {
17 18 19 20 21 22 23 24 25 26 27 28 29 30
  char *start;
  char *end;

  if (!(start = strstr(buffer, toRemove)))
    return -1;

  if (!(end = strstr(start+1, "\n"))) {
    *start = '\0';
  } else {
    memmove(start, end, strlen(end)+1);
  }
  return 0;
}

31 32
static int testCompareOutput(const char *expect_rel, const char *filter,
                             const char *const argv[]) {
33 34 35 36
  char expectData[MAX_FILE];
  char actualData[MAX_FILE];
  char *expectPtr = &(expectData[0]);
  char *actualPtr = &(actualData[0]);
37 38
  char expect[PATH_MAX];

39
  snprintf(expect, sizeof expect - 1, "%s/%s", abs_srcdir, expect_rel);
40 41 42 43 44 45 46 47 48 49 50

  if (virtTestLoadFile(expect, &expectPtr, MAX_FILE) < 0)
    return -1;

  if (virtTestCaptureProgramOutput(argv, &actualPtr, MAX_FILE) < 0)
    return -1;

  if (filter)
    if (testFilterLine(actualData, filter) < 0)
      return -1;

51 52 53 54
  if (STRNEQ(expectData, actualData)) {
      virtTestDifference(stderr, expectData, actualData);
      return -1;
  }
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

  return 0;
}


#define VIRSH_DEFAULT     "../src/virsh", \
    "--connect", \
    "test:///default"

static char *custom_uri;

#define VIRSH_CUSTOM     "../src/virsh", \
    "--connect", \
    custom_uri



72
static int testCompareListDefault(const void *data ATTRIBUTE_UNUSED) {
73 74 75 76 77 78
  const char *const argv[] = {
    VIRSH_DEFAULT,
    "list",
    NULL
  };
  return testCompareOutput("virshdata/list-default.txt",
79 80
                           NULL,
                           argv);
81 82
}

83
static int testCompareListCustom(const void *data ATTRIBUTE_UNUSED) {
84 85 86 87 88 89
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "list",
    NULL
  };
  return testCompareOutput("virshdata/list-custom.txt",
90 91
                           NULL,
                           argv);
92 93 94
}


95
static int testCompareNodeinfoDefault(const void *data ATTRIBUTE_UNUSED) {
96 97 98 99 100 101
  const char *const argv[] = {
    VIRSH_DEFAULT,
    "nodeinfo",
    NULL
  };
  return testCompareOutput("virshdata/nodeinfo-default.txt",
102 103
                           NULL,
                           argv);
104 105
}

106
static int testCompareNodeinfoCustom(const void *data ATTRIBUTE_UNUSED) {
107 108 109 110 111 112
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "nodeinfo",
    NULL
  };
  return testCompareOutput("virshdata/nodeinfo-custom.txt",
113 114
                           NULL,
                           argv);
115 116
}

117
static int testCompareDominfoByID(const void *data ATTRIBUTE_UNUSED) {
118 119 120
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "dominfo",
D
Daniel P. Berrange 已提交
121
    "2",
122 123 124
    NULL
  };
  return testCompareOutput("virshdata/dominfo-fc4.txt",
125 126
                           "\nCPU time:",
                           argv);
127 128 129
}


130
static int testCompareDominfoByUUID(const void *data ATTRIBUTE_UNUSED) {
131 132 133 134 135 136 137
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "dominfo",
    "ef861801-45b9-11cb-88e3-afbfe5370493",
    NULL
  };
  return testCompareOutput("virshdata/dominfo-fc4.txt",
138 139
                           "\nCPU time:",
                           argv);
140 141 142
}


143
static int testCompareDominfoByName(const void *data ATTRIBUTE_UNUSED) {
144 145 146 147 148 149 150
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "dominfo",
    "fc4",
    NULL
  };
  return testCompareOutput("virshdata/dominfo-fc4.txt",
151 152
                           "\nCPU time:",
                           argv);
153 154 155
}


156
static int testCompareDomuuidByID(const void *data ATTRIBUTE_UNUSED) {
157 158 159
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "domuuid",
D
Daniel P. Berrange 已提交
160
    "2",
161 162 163
    NULL
  };
  return testCompareOutput("virshdata/domuuid-fc4.txt",
164 165
                           NULL,
                           argv);
166 167
}

168
static int testCompareDomuuidByName(const void *data ATTRIBUTE_UNUSED) {
169 170 171 172 173 174 175
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "domuuid",
    "fc4",
    NULL
  };
  return testCompareOutput("virshdata/domuuid-fc4.txt",
176 177
                           NULL,
                           argv);
178 179
}

180
static int testCompareDomidByName(const void *data ATTRIBUTE_UNUSED) {
181 182 183 184 185 186 187
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "domid",
    "fc4",
    NULL
  };
  return testCompareOutput("virshdata/domid-fc4.txt",
188 189
                           NULL,
                           argv);
190 191 192
}


193
static int testCompareDomidByUUID(const void *data ATTRIBUTE_UNUSED) {
194 195 196 197 198 199 200
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "domid",
    "ef861801-45b9-11cb-88e3-afbfe5370493",
    NULL
  };
  return testCompareOutput("virshdata/domid-fc4.txt",
201 202
                           NULL,
                           argv);
203 204 205
}


206
static int testCompareDomnameByID(const void *data ATTRIBUTE_UNUSED) {
207 208 209
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "domname",
D
Daniel P. Berrange 已提交
210
    "2",
211 212 213
    NULL
  };
  return testCompareOutput("virshdata/domname-fc4.txt",
214 215
                           NULL,
                           argv);
216 217 218
}


219
static int testCompareDomnameByUUID(const void *data ATTRIBUTE_UNUSED) {
220 221 222 223 224 225 226
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "domname",
    "ef861801-45b9-11cb-88e3-afbfe5370493",
    NULL
  };
  return testCompareOutput("virshdata/domname-fc4.txt",
227 228
                           NULL,
                           argv);
229 230
}

231
static int testCompareDomstateByID(const void *data ATTRIBUTE_UNUSED) {
232 233 234
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "domstate",
D
Daniel P. Berrange 已提交
235
    "2",
236 237 238
    NULL
  };
  return testCompareOutput("virshdata/domstate-fc4.txt",
239 240
                           NULL,
                           argv);
241 242 243
}


244
static int testCompareDomstateByUUID(const void *data ATTRIBUTE_UNUSED) {
245 246 247 248 249 250 251
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "domstate",
    "ef861801-45b9-11cb-88e3-afbfe5370493",
    NULL
  };
  return testCompareOutput("virshdata/domstate-fc4.txt",
252 253
                           NULL,
                           argv);
254 255
}

256
static int testCompareDomstateByName(const void *data ATTRIBUTE_UNUSED) {
257 258 259 260 261 262 263
  const char *const argv[] = {
    VIRSH_CUSTOM,
    "domstate",
    "fc4",
    NULL
  };
  return testCompareOutput("virshdata/domstate-fc4.txt",
264 265
                           NULL,
                           argv);
266 267 268
}


A
Atsushi SAKAI 已提交
269
#ifndef WIN32
270 271
static int
mymain(int argc, char **argv)
272 273 274
{
    int ret = 0;
    char buffer[PATH_MAX];
275
    char cwd[PATH_MAX];
276

277 278 279
    abs_srcdir = getenv("abs_srcdir");
    if (!abs_srcdir)
        abs_srcdir = getcwd(cwd, sizeof(cwd));
280

281
    snprintf(buffer, PATH_MAX-1, "test://%s/../docs/testnode.xml", abs_srcdir);
282 283 284
    buffer[PATH_MAX-1] = '\0';
    progname = argv[0];
    custom_uri = buffer;
285

286
    if (argc > 1) {
287
        fprintf(stderr, "Usage: %s\n", progname);
288
        return(EXIT_FAILURE);
289
    }
290

291
    if (virtTestRun("virsh list (default)",
292
                    1, testCompareListDefault, NULL) != 0)
293 294 295
        ret = -1;

    if (virtTestRun("virsh list (custom)",
296
                    1, testCompareListCustom, NULL) != 0)
297 298 299
        ret = -1;

    if (virtTestRun("virsh nodeinfo (default)",
300
                    1, testCompareNodeinfoDefault, NULL) != 0)
301 302 303
        ret = -1;

    if (virtTestRun("virsh nodeinfo (custom)",
304
                    1, testCompareNodeinfoCustom, NULL) != 0)
305 306 307
        ret = -1;

    if (virtTestRun("virsh dominfo (by id)",
308
                    1, testCompareDominfoByID, NULL) != 0)
309 310 311
        ret = -1;

    if (virtTestRun("virsh dominfo (by uuid)",
312
                    1, testCompareDominfoByUUID, NULL) != 0)
313 314 315
        ret = -1;

    if (virtTestRun("virsh dominfo (by name)",
316
                    1, testCompareDominfoByName, NULL) != 0)
317 318 319
        ret = -1;

    if (virtTestRun("virsh domid (by name)",
320
                    1, testCompareDomidByName, NULL) != 0)
321 322 323
        ret = -1;

    if (virtTestRun("virsh domid (by uuid)",
324
                    1, testCompareDomidByUUID, NULL) != 0)
325 326 327
        ret = -1;

    if (virtTestRun("virsh domuuid (by id)",
328
                    1, testCompareDomuuidByID, NULL) != 0)
329 330 331
        ret = -1;

    if (virtTestRun("virsh domuuid (by name)",
332
                    1, testCompareDomuuidByName, NULL) != 0)
333 334 335
        ret = -1;

    if (virtTestRun("virsh domname (by id)",
336
                    1, testCompareDomnameByID, NULL) != 0)
337 338 339
        ret = -1;

    if (virtTestRun("virsh domname (by uuid)",
340
                    1, testCompareDomnameByUUID, NULL) != 0)
341 342 343
        ret = -1;

    if (virtTestRun("virsh domstate (by id)",
344
                    1, testCompareDomstateByID, NULL) != 0)
345 346 347
        ret = -1;

    if (virtTestRun("virsh domstate (by uuid)",
348
                    1, testCompareDomstateByUUID, NULL) != 0)
349 350 351
        ret = -1;

    if (virtTestRun("virsh domstate (by name)",
352
                    1, testCompareDomstateByName, NULL) != 0)
353 354
        ret = -1;

355
    return(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
356
}
A
Atsushi SAKAI 已提交
357 358 359 360 361
#else /* ! WIN32 */

static int mymain (void) { exit (77); /* means 'test skipped' for automake */ }

#endif /* WIN32 */
362 363

VIRT_TEST_MAIN(mymain)