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