f38490a741085e377de7acc3742c391b4af244a3
[libguestfs.git] / test-tool / test-tool.c
1 /* libguestfs-test-tool
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <inttypes.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <getopt.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/wait.h>
32
33 #include <guestfs.h>
34
35 #ifdef HAVE_GETTEXT
36 #include "gettext.h"
37 #define _(str) dgettext(PACKAGE, (str))
38 #define N_(str) dgettext(PACKAGE, (str))
39 #else
40 #define _(str) str
41 #define N_(str) str
42 #endif
43
44 #define STREQ(a,b) (strcmp((a),(b)) == 0)
45 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
46 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
47 #define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
48 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
49 #define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
50 #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
51 #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
52 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
53
54 #define DEFAULT_TIMEOUT 120
55
56 static const char *helper = DEFAULT_HELPER;
57 static int timeout = DEFAULT_TIMEOUT;
58 static char tmpf[] = "/tmp/libguestfs-test-tool-sda-XXXXXX";
59 static char isof[] = "/tmp/libguestfs-test-tool-iso-XXXXXX";
60 static guestfs_h *g;
61
62 static void preruncheck (void);
63 static void make_files (void);
64 static void set_qemu (const char *path, int use_wrapper);
65
66 static void
67 usage (void)
68 {
69   printf (_("libguestfs-test-tool: interactive test tool\n"
70             "Copyright (C) 2009 Red Hat Inc.\n"
71             "Usage:\n"
72             "  libguestfs-test-tool [--options]\n"
73             "Options:\n"
74             "  --help         Display usage\n"
75             "  --helper libguestfs-test-tool-helper\n"
76             "                 Helper program (default: %s)\n"
77             "  --qemudir dir  Specify QEMU source directory\n"
78             "  --qemu qemu    Specify QEMU binary\n"
79             "  --timeout n\n"
80             "  -t n           Set launch timeout (default: %d seconds)\n"
81             ),
82           DEFAULT_HELPER, DEFAULT_TIMEOUT);
83 }
84
85 int
86 main (int argc, char *argv[])
87 {
88   static const char *options = "t:?";
89   static const struct option long_options[] = {
90     { "help", 0, 0, '?' },
91     { "helper", 1, 0, 0 },
92     { "qemu", 1, 0, 0 },
93     { "qemudir", 1, 0, 0 },
94     { "timeout", 1, 0, 't' },
95     { 0, 0, 0, 0 }
96   };
97   int c;
98   int option_index;
99   extern char **environ;
100   int i;
101   struct guestfs_version *vers;
102   char *sfdisk_lines[] = { ",", NULL };
103   char *str;
104   /* XXX This is wrong if the user renames the helper. */
105   char *helper_args[] = { "/iso/libguestfs-test-tool-helper", NULL };
106
107   for (;;) {
108     c = getopt_long (argc, argv, options, long_options, &option_index);
109     if (c == -1) break;
110
111     switch (c) {
112     case 0:                     /* options which are long only */
113       if (STREQ (long_options[option_index].name, "helper"))
114         helper = optarg;
115       else if (STREQ (long_options[option_index].name, "qemu"))
116         set_qemu (optarg, 0);
117       else if (STREQ (long_options[option_index].name, "qemudir"))
118         set_qemu (optarg, 1);
119       else {
120         fprintf (stderr,
121                  _("libguestfs-test-tool: unknown long option: %s (%d)\n"),
122                  long_options[option_index].name, option_index);
123         exit (EXIT_FAILURE);
124       }
125       break;
126
127     case 't':
128       if (sscanf (optarg, "%d", &timeout) != 1 || timeout < 0) {
129         fprintf (stderr,
130                  _("libguestfs-test-tool: invalid timeout: %s\n"),
131                  optarg);
132         exit (EXIT_FAILURE);
133       }
134       break;
135
136     case '?':
137       usage ();
138       exit (EXIT_SUCCESS);
139
140     default:
141       fprintf (stderr,
142                _("libguestfs-test-tool: unexpected command line option 0x%x\n"),
143                c);
144       exit (EXIT_FAILURE);
145     }
146   }
147
148   preruncheck ();
149   make_files ();
150
151   printf ("===== Test starts here =====\n");
152
153   /* Must set LIBGUESTFS_DEBUG=1 */
154   setenv ("LIBGUESTFS_DEBUG", "1", 1);
155
156   /* Print out any environment variables which may relate to this test. */
157   for (i = 0; environ[i] != NULL; ++i)
158     if (STREQLEN (environ[i], "LIBGUESTFS_", 11))
159       printf ("%s\n", environ[i]);
160
161   /* Create the handle and configure it. */
162   g = guestfs_create ();
163   if (g == NULL) {
164     fprintf (stderr,
165              _("libguestfs-test-tool: failed to create libguestfs handle\n"));
166     exit (EXIT_FAILURE);
167   }
168   if (guestfs_add_drive (g, tmpf) == -1) {
169     fprintf (stderr,
170              _("libguestfs-test-tool: failed to add drive '%s'\n"),
171              tmpf);
172     exit (EXIT_FAILURE);
173   }
174   if (guestfs_add_drive (g, isof) == -1) {
175     fprintf (stderr,
176              _("libguestfs-test-tool: failed to add drive '%s'\n"),
177              isof);
178     exit (EXIT_FAILURE);
179   }
180
181   /* Print any version info etc. */
182   vers = guestfs_version (g);
183   if (vers == NULL) {
184     fprintf (stderr, _("libguestfs-test-tool: guestfs_version failed\n"));
185     exit (EXIT_FAILURE);
186   }
187   printf ("library version: %"PRIi64".%"PRIi64".%"PRIi64"%s\n",
188           vers->major, vers->minor, vers->release, vers->extra);
189   guestfs_free_version (vers);
190
191   printf ("guestfs_get_append: %s\n", guestfs_get_append (g) ? : "(null)");
192   printf ("guestfs_get_autosync: %d\n", guestfs_get_autosync (g));
193   printf ("guestfs_get_memsize: %d\n", guestfs_get_memsize (g));
194   printf ("guestfs_get_path: %s\n", guestfs_get_path (g));
195   printf ("guestfs_get_qemu: %s\n", guestfs_get_qemu (g));
196   printf ("guestfs_get_verbose: %d\n", guestfs_get_verbose (g));
197
198   /* Launch the guest handle. */
199   printf ("Launching appliance, timeout set to %d seconds.\n", timeout);
200   fflush (stdout);
201
202   alarm (timeout);
203
204   if (guestfs_launch (g) == -1) {
205     fprintf (stderr,
206              _("libguestfs-test-tool: failed to launch appliance\n"));
207     exit (EXIT_FAILURE);
208   }
209
210   alarm (0);
211
212   printf ("Guest launched OK.\n");
213   fflush (stdout);
214
215   /* Create the filesystem and mount everything. */
216   if (guestfs_sfdiskM (g, "/dev/sda", sfdisk_lines) == -1) {
217     fprintf (stderr,
218              _("libguestfs-test-tool: failed to run sfdisk\n"));
219     exit (EXIT_FAILURE);
220   }
221
222   if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1) {
223     fprintf (stderr,
224              _("libguestfs-test-tool: failed to mkfs.ext2\n"));
225     exit (EXIT_FAILURE);
226   }
227
228   if (guestfs_mount (g, "/dev/sda1", "/") == -1) {
229     fprintf (stderr,
230              _("libguestfs-test-tool: failed to mount /dev/sda1 on /\n"));
231     exit (EXIT_FAILURE);
232   }
233
234   if (guestfs_mkdir (g, "/iso") == -1) {
235     fprintf (stderr,
236              _("libguestfs-test-tool: failed to mkdir /iso\n"));
237     exit (EXIT_FAILURE);
238   }
239
240   if (guestfs_mount (g, "/dev/sdb", "/iso") == -1) {
241     fprintf (stderr,
242              _("libguestfs-test-tool: failed to mount /dev/sdb on /iso\n"));
243     exit (EXIT_FAILURE);
244   }
245
246   /* Let's now run some simple tests using the helper program. */
247   str = guestfs_command (g, helper_args);
248   if (str == NULL) {
249     fprintf (stderr,
250              _("libguestfs-test-tool: could not run helper program, or helper failed\n"));
251     exit (EXIT_FAILURE);
252   }
253   free (str);
254
255   printf ("===== TEST FINISHED OK =====\n");
256   exit (EXIT_SUCCESS);
257 }
258
259 static char qemuwrapper[] = "/tmp/libguestfs-test-tool-wrapper-XXXXXX";
260
261 static void
262 cleanup_wrapper (void)
263 {
264   unlink (qemuwrapper);
265 }
266
267 /* Handle the --qemu and --qemudir parameters.  use_wrapper is true
268  * in the --qemudir (source directory) case, where we have to create
269  * a wrapper shell script.
270  */
271 static void
272 set_qemu (const char *path, int use_wrapper)
273 {
274   char buffer[PATH_MAX];
275   struct stat statbuf;
276   int fd;
277   FILE *fp;
278
279   if (getenv ("LIBGUESTFS_QEMU")) {
280     fprintf (stderr,
281     _("LIBGUESTFS_QEMU environment variable is already set, so\n"
282       "--qemu/--qemudir options cannot be used.\n"));
283     exit (EXIT_FAILURE);
284   }
285
286   if (!use_wrapper) {
287     if (access (path, X_OK) == -1) {
288       fprintf (stderr,
289                _("Binary '%s' does not exist or is not executable\n"),
290                path);
291       exit (EXIT_FAILURE);
292     }
293
294     setenv ("LIBGUESTFS_QEMU", path, 1);
295     return;
296   }
297
298   /* This should be a source directory, so check it. */
299   snprintf (buffer, sizeof buffer, "%s/pc-bios", path);
300   if (stat (buffer, &statbuf) == -1 ||
301       !S_ISDIR (statbuf.st_mode)) {
302     fprintf (stderr,
303              _("%s: does not look like a qemu source directory\n"),
304              path);
305     exit (EXIT_FAILURE);
306   }
307
308   /* Make a wrapper script. */
309   fd = mkstemp (qemuwrapper);
310   if (fd == -1) {
311     perror (qemuwrapper);
312     exit (EXIT_FAILURE);
313   }
314
315   fchmod (fd, 0700);
316
317   fp = fdopen (fd, "w");
318   fprintf (fp,
319            "#!/bin/sh -\n"
320            "qemudir='%s'\n"
321            "\"$qemudir\"/",
322            path);
323
324   /* Select the right qemu binary for the wrapper script. */
325 #ifdef __i386__
326   fprintf (fp, "i386-softmmu/qemu");
327 #else
328   fprintf (fp, host_cpu "-softmmu/qemu-system-" host_cpu);
329 #endif
330
331   fprintf (fp, " -L \"$qemudir\"/pc-bios \"$@\"\n");
332
333   fclose (fp);
334
335   setenv ("LIBGUESTFS_QEMU", qemuwrapper, 1);
336   atexit (cleanup_wrapper);
337 }
338
339 /* After getting the command line args, but before running
340  * anything, we check everything is in place to do the tests.
341  */
342 static void
343 preruncheck (void)
344 {
345   int r;
346   FILE *fp;
347   char cmd[256];
348   char buffer[1024];
349
350   if (access (helper, R_OK) == -1) {
351     fprintf (stderr,
352     _("Test tool helper program 'libguestfs-test-tool-helper' is not\n"
353       "available.  Expected to find it in '%s'\n"
354       "\n"
355       "Use the --helper option to specify the location of this program.\n"),
356              helper);
357     exit (EXIT_FAILURE);
358   }
359
360   snprintf (cmd, sizeof cmd, "file '%s'", helper);
361   fp = popen (cmd, "r");
362   if (fp == NULL) {
363     perror (cmd);
364     exit (EXIT_FAILURE);
365   }
366   r = fread (buffer, 1, sizeof buffer - 1, fp);
367   if (r == 0) {
368     fprintf (stderr, _("command failed: %s"), cmd);
369     exit (EXIT_FAILURE);
370   }
371   pclose (fp);
372   buffer[r] = '\0';
373
374   if (strstr (buffer, "statically linked") == NULL) {
375     fprintf (stderr,
376     _("Test tool helper program %s\n"
377       "is not statically linked.  This is a build error when this test tool\n"
378       "was built.\n"),
379              helper);
380     exit (EXIT_FAILURE);
381   }
382 }
383
384 static void
385 cleanup_tmpfiles (void)
386 {
387   unlink (tmpf);
388   unlink (isof);
389 }
390
391 static void
392 make_files (void)
393 {
394   int fd, r;
395   char cmd[256];
396
397   /* Make the ISO which will contain the helper program. */
398   fd = mkstemp (isof);
399   if (fd == -1) {
400     perror (isof);
401     exit (EXIT_FAILURE);
402   }
403   close (fd);
404
405   snprintf (cmd, sizeof cmd, "mkisofs -quiet -rJT -o '%s' '%s'",
406             isof, helper);
407   r = system (cmd);
408   if (r == -1 || WEXITSTATUS(r) != 0) {
409     fprintf (stderr,
410              _("mkisofs command failed: %s\n"), cmd);
411     exit (EXIT_FAILURE);
412   }
413
414   /* Allocate the sparse file for /dev/sda. */
415   fd = mkstemp (tmpf);
416   if (fd == -1) {
417     perror (tmpf);
418     unlink (isof);
419     exit (EXIT_FAILURE);
420   }
421
422   if (lseek (fd, 100 * 1024 * 1024 - 1, SEEK_SET) == -1) {
423     perror ("lseek");
424     close (fd);
425     unlink (tmpf);
426     unlink (isof);
427     exit (EXIT_FAILURE);
428   }
429
430   if (write (fd, "\0", 1) == -1) {
431     perror ("write");
432     close (fd);
433     unlink (tmpf);
434     unlink (isof);
435     exit (EXIT_FAILURE);
436   }
437
438   close (fd);
439
440   atexit (cleanup_tmpfiles);    /* Removes tmpf and isof. */
441 }