2 * Copyright (C) 2009-2010 Red Hat Inc.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (* Please read generator/README first. *)
26 open Generator_docstrings
27 open Generator_optgroups
28 open Generator_actions
29 open Generator_structs
31 (* Generate the tests. *)
32 let rec generate_tests () =
33 generate_header CStyle GPLv2plus;
40 #include <sys/types.h>
43 #include \"guestfs.h\"
44 #include \"guestfs-internal.h\"
47 static int suppress_error = 0;
49 static void print_error (guestfs_h *g, void *data, const char *msg)
52 fprintf (stderr, \"%%s\\n\", msg);
55 /* FIXME: nearly identical code appears in fish.c */
56 static void print_strings (char *const *argv)
60 for (argc = 0; argv[argc] != NULL; ++argc)
61 printf (\"\\t%%s\\n\", argv[argc]);
65 static void print_table (char const *const *argv)
69 for (i = 0; argv[i] != NULL; i += 2)
70 printf (\"%%s: %%s\\n\", argv[i], argv[i+1]);
75 is_available (const char *group)
77 const char *groups[] = { group, NULL };
81 r = guestfs_available (g, (char **) groups);
88 incr (guestfs_h *g, void *iv)
94 /* Get md5sum of the named file. */
96 md5sum (const char *filename, char *result)
99 snprintf (cmd, sizeof cmd, \"md5sum %%s\", filename);
100 FILE *pp = popen (cmd, \"r\");
105 if (fread (result, 1, 32, pp) != 32) {
106 perror (\"md5sum: fread\");
109 if (pclose (pp) == -1) {
118 (* Generate a list of commands which are not tested anywhere. *)
119 pr "static void no_test_warnings (void)\n";
122 let hash : (string, bool) Hashtbl.t = Hashtbl.create 13 in
124 fun (_, _, _, _, tests, _, _) ->
125 let tests = filter_map (
127 | (_, (Always|If _|Unless _|IfAvailable _), test) -> Some test
128 | (_, Disabled, _) -> None
130 let seq = List.concat (List.map seq_of_test tests) in
131 let cmds_tested = List.map List.hd seq in
132 List.iter (fun cmd -> Hashtbl.replace hash cmd true) cmds_tested
136 fun (name, _, _, _, _, _, _) ->
137 if not (Hashtbl.mem hash name) then
138 pr " fprintf (stderr, \"warning: \\\"guestfs_%s\\\" has no tests\\n\");\n" name
144 (* Generate the actual tests. Note that we generate the tests
145 * in reverse order, deliberately, so that (in general) the
146 * newest tests run first. This makes it quicker and easier to
151 fun (name, _, _, flags, tests, _, _) ->
152 mapi (generate_one_test name flags) tests
153 ) (List.rev all_functions) in
154 let test_names = List.concat test_names in
155 let nr_tests = List.length test_names in
158 int main (int argc, char *argv[])
161 unsigned long int n_failed = 0;
162 const char *filename;
164 int nr_tests, test_num = 0;
166 setbuf (stdout, NULL);
170 g = guestfs_create ();
172 printf (\"guestfs_create FAILED\\n\");
176 guestfs_set_error_handler (g, print_error, NULL);
178 guestfs_set_path (g, \"../appliance\");
180 filename = \"test1.img\";
181 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
186 if (lseek (fd, %d, SEEK_SET) == -1) {
192 if (write (fd, &c, 1) == -1) {
198 if (close (fd) == -1) {
203 if (guestfs_add_drive (g, filename) == -1) {
204 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
208 filename = \"test2.img\";
209 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
214 if (lseek (fd, %d, SEEK_SET) == -1) {
220 if (write (fd, &c, 1) == -1) {
226 if (close (fd) == -1) {
231 if (guestfs_add_drive (g, filename) == -1) {
232 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
236 filename = \"test3.img\";
237 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
242 if (lseek (fd, %d, SEEK_SET) == -1) {
248 if (write (fd, &c, 1) == -1) {
254 if (close (fd) == -1) {
259 if (guestfs_add_drive (g, filename) == -1) {
260 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
264 if (guestfs_add_drive_ro (g, \"../images/test.iso\") == -1) {
265 printf (\"guestfs_add_drive_ro ../images/test.iso FAILED\\n\");
269 /* Set a timeout in case qemu hangs during launch (RHBZ#505329). */
272 if (guestfs_launch (g) == -1) {
273 printf (\"guestfs_launch FAILED\\n\");
277 /* Cancel previous alarm. */
282 " (500 * 1024 * 1024) (50 * 1024 * 1024) (10 * 1024 * 1024) nr_tests;
287 pr " if (guestfs_get_verbose (g))\n";
288 pr " printf (\"-------------------------------------------------------------------------------\\n\");\n";
289 pr " printf (\"%%3d/%%3d %s\\n\", test_num, nr_tests);\n" test_name;
290 pr " if (%s () == -1) {\n" test_name;
291 pr " printf (\"%s FAILED\\n\");\n" test_name;
297 pr " /* Check close callback is called. */
298 int close_sentinel = 1;
299 guestfs_set_close_callback (g, incr, &close_sentinel);
303 if (close_sentinel != 2) {
304 fprintf (stderr, \"close callback was not called\\n\");
308 unlink (\"test1.img\");
309 unlink (\"test2.img\");
310 unlink (\"test3.img\");
314 pr " if (n_failed > 0) {\n";
315 pr " printf (\"***** %%lu / %%d tests FAILED *****\\n\", n_failed, nr_tests);\n";
316 pr " exit (EXIT_FAILURE);\n";
320 pr " exit (EXIT_SUCCESS);\n";
323 and generate_one_test name flags i (init, prereq, test) =
324 let test_name = sprintf "test_%s_%d" name i in
327 static int %s_skip (void)
331 str = getenv (\"TEST_ONLY\");
333 return strstr (str, \"%s\") == NULL;
334 str = getenv (\"SKIP_%s\");
335 if (str && STREQ (str, \"1\")) return 1;
336 str = getenv (\"SKIP_TEST_%s\");
337 if (str && STREQ (str, \"1\")) return 1;
341 " test_name name (String.uppercase test_name) (String.uppercase name);
344 | Disabled | Always | IfAvailable _ -> ()
345 | If code | Unless code ->
346 pr "static int %s_prereq (void)\n" test_name;
357 printf (\" %%s skipped (reason: environment variable set)\\n\", \"%s\");
361 " test_name test_name test_name;
363 (* Optional functions should only be tested if the relevant
364 * support is available in the daemon.
369 pr " if (!is_available (\"%s\")) {\n" group;
370 pr " printf (\" %%s skipped (reason: group %%s not available in daemon)\\n\", \"%s\", \"%s\");\n" test_name group;
378 pr " printf (\" %%s skipped (reason: test disabled in generator)\\n\", \"%s\");\n" test_name
380 pr " if (! %s_prereq ()) {\n" test_name;
381 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
385 generate_one_test_body name i test_name init test;
387 pr " if (%s_prereq ()) {\n" test_name;
388 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
392 generate_one_test_body name i test_name init test;
393 | IfAvailable group ->
394 pr " if (!is_available (\"%s\")) {\n" group;
395 pr " printf (\" %%s skipped (reason: %%s not available)\\n\", \"%s\", \"%s\");\n" test_name group;
399 generate_one_test_body name i test_name init test;
401 generate_one_test_body name i test_name init test
409 and generate_one_test_body name i test_name init test =
411 | InitNone (* XXX at some point, InitNone and InitEmpty became
412 * folded together as the same thing. Really we should
413 * make InitNone do nothing at all, but the tests may
414 * need to be checked to make sure this is OK.
417 pr " /* InitNone|InitEmpty for %s */\n" test_name;
418 List.iter (generate_test_command_call test_name)
419 [["blockdev_setrw"; "/dev/sda"];
423 pr " /* InitPartition for %s: create /dev/sda1 */\n" test_name;
424 List.iter (generate_test_command_call test_name)
425 [["blockdev_setrw"; "/dev/sda"];
428 ["part_disk"; "/dev/sda"; "mbr"]]
430 pr " /* InitBasicFS for %s: create ext2 on /dev/sda1 */\n" test_name;
431 List.iter (generate_test_command_call test_name)
432 [["blockdev_setrw"; "/dev/sda"];
435 ["part_disk"; "/dev/sda"; "mbr"];
436 ["mkfs"; "ext2"; "/dev/sda1"];
437 ["mount_options"; ""; "/dev/sda1"; "/"]]
438 | InitBasicFSonLVM ->
439 pr " /* InitBasicFSonLVM for %s: create ext2 on /dev/VG/LV */\n"
441 List.iter (generate_test_command_call test_name)
442 [["blockdev_setrw"; "/dev/sda"];
445 ["part_disk"; "/dev/sda"; "mbr"];
446 ["pvcreate"; "/dev/sda1"];
447 ["vgcreate"; "VG"; "/dev/sda1"];
448 ["lvcreate"; "LV"; "VG"; "8"];
449 ["mkfs"; "ext2"; "/dev/VG/LV"];
450 ["mount_options"; ""; "/dev/VG/LV"; "/"]]
452 pr " /* InitISOFS for %s */\n" test_name;
453 List.iter (generate_test_command_call test_name)
454 [["blockdev_setrw"; "/dev/sda"];
457 ["mount_ro"; "/dev/sdd"; "/"]]
460 let get_seq_last = function
462 failwithf "%s: you cannot use [] (empty list) when expecting a command"
465 let seq = List.rev seq in
466 List.rev (List.tl seq), List.hd seq
471 pr " /* TestRun for %s (%d) */\n" name i;
472 List.iter (generate_test_command_call test_name) seq
473 | TestOutput (seq, expected) ->
474 pr " /* TestOutput for %s (%d) */\n" name i;
475 pr " const char *expected = \"%s\";\n" (c_quote expected);
476 let seq, last = get_seq_last seq in
478 pr " if (STRNEQ (r, expected)) {\n";
479 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
483 List.iter (generate_test_command_call test_name) seq;
484 generate_test_command_call ~test test_name last
485 | TestOutputList (seq, expected) ->
486 pr " /* TestOutputList for %s (%d) */\n" name i;
487 let seq, last = get_seq_last seq in
491 pr " if (!r[%d]) {\n" i;
492 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
493 pr " print_strings (r);\n";
497 pr " const char *expected = \"%s\";\n" (c_quote str);
498 pr " if (STRNEQ (r[%d], expected)) {\n" i;
499 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
504 pr " if (r[%d] != NULL) {\n" (List.length expected);
505 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
507 pr " print_strings (r);\n";
511 List.iter (generate_test_command_call test_name) seq;
512 generate_test_command_call ~test test_name last
513 | TestOutputListOfDevices (seq, expected) ->
514 pr " /* TestOutputListOfDevices for %s (%d) */\n" name i;
515 let seq, last = get_seq_last seq in
519 pr " if (!r[%d]) {\n" i;
520 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
521 pr " print_strings (r);\n";
525 pr " const char *expected = \"%s\";\n" (c_quote str);
526 pr " r[%d][5] = 's';\n" i;
527 pr " if (STRNEQ (r[%d], expected)) {\n" i;
528 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
533 pr " if (r[%d] != NULL) {\n" (List.length expected);
534 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
536 pr " print_strings (r);\n";
540 List.iter (generate_test_command_call test_name) seq;
541 generate_test_command_call ~test test_name last
542 | TestOutputInt (seq, expected) ->
543 pr " /* TestOutputInt for %s (%d) */\n" name i;
544 let seq, last = get_seq_last seq in
546 pr " if (r != %d) {\n" expected;
547 pr " fprintf (stderr, \"%s: expected %d but got %%d\\n\","
553 List.iter (generate_test_command_call test_name) seq;
554 generate_test_command_call ~test test_name last
555 | TestOutputIntOp (seq, op, expected) ->
556 pr " /* TestOutputIntOp for %s (%d) */\n" name i;
557 let seq, last = get_seq_last seq in
559 pr " if (! (r %s %d)) {\n" op expected;
560 pr " fprintf (stderr, \"%s: expected %s %d but got %%d\\n\","
561 test_name op expected;
566 List.iter (generate_test_command_call test_name) seq;
567 generate_test_command_call ~test test_name last
568 | TestOutputTrue seq ->
569 pr " /* TestOutputTrue for %s (%d) */\n" name i;
570 let seq, last = get_seq_last seq in
573 pr " fprintf (stderr, \"%s: expected true, got false\\n\");\n"
578 List.iter (generate_test_command_call test_name) seq;
579 generate_test_command_call ~test test_name last
580 | TestOutputFalse seq ->
581 pr " /* TestOutputFalse for %s (%d) */\n" name i;
582 let seq, last = get_seq_last seq in
585 pr " fprintf (stderr, \"%s: expected false, got true\\n\");\n"
590 List.iter (generate_test_command_call test_name) seq;
591 generate_test_command_call ~test test_name last
592 | TestOutputLength (seq, expected) ->
593 pr " /* TestOutputLength for %s (%d) */\n" name i;
594 let seq, last = get_seq_last seq in
597 pr " for (j = 0; j < %d; ++j)\n" expected;
598 pr " if (r[j] == NULL) {\n";
599 pr " fprintf (stderr, \"%s: short list returned\\n\");\n"
601 pr " print_strings (r);\n";
604 pr " if (r[j] != NULL) {\n";
605 pr " fprintf (stderr, \"%s: long list returned\\n\");\n"
607 pr " print_strings (r);\n";
611 List.iter (generate_test_command_call test_name) seq;
612 generate_test_command_call ~test test_name last
613 | TestOutputBuffer (seq, expected) ->
614 pr " /* TestOutputBuffer for %s (%d) */\n" name i;
615 pr " const char *expected = \"%s\";\n" (c_quote expected);
616 let seq, last = get_seq_last seq in
617 let len = String.length expected in
619 pr " if (size != %d) {\n" len;
620 pr " fprintf (stderr, \"%s: returned size of buffer wrong, expected %d but got %%zu\\n\", size);\n" test_name len;
623 pr " if (STRNEQLEN (r, expected, size)) {\n";
624 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
628 List.iter (generate_test_command_call test_name) seq;
629 generate_test_command_call ~test test_name last
630 | TestOutputStruct (seq, checks) ->
631 pr " /* TestOutputStruct for %s (%d) */\n" name i;
632 let seq, last = get_seq_last seq in
636 | CompareWithInt (field, expected) ->
637 pr " if (r->%s != %d) {\n" field expected;
638 pr " fprintf (stderr, \"%s: %s was %%d, expected %d\\n\",\n"
639 test_name field expected;
640 pr " (int) r->%s);\n" field;
643 | CompareWithIntOp (field, op, expected) ->
644 pr " if (!(r->%s %s %d)) {\n" field op expected;
645 pr " fprintf (stderr, \"%s: %s was %%d, expected %s %d\\n\",\n"
646 test_name field op expected;
647 pr " (int) r->%s);\n" field;
650 | CompareWithString (field, expected) ->
651 pr " if (STRNEQ (r->%s, \"%s\")) {\n" field expected;
652 pr " fprintf (stderr, \"%s: %s was \"%%s\", expected \"%s\"\\n\",\n"
653 test_name field expected;
654 pr " r->%s);\n" field;
657 | CompareFieldsIntEq (field1, field2) ->
658 pr " if (r->%s != r->%s) {\n" field1 field2;
659 pr " fprintf (stderr, \"%s: %s (%%d) <> %s (%%d)\\n\",\n"
660 test_name field1 field2;
661 pr " (int) r->%s, (int) r->%s);\n" field1 field2;
664 | CompareFieldsStrEq (field1, field2) ->
665 pr " if (STRNEQ (r->%s, r->%s)) {\n" field1 field2;
666 pr " fprintf (stderr, \"%s: %s (\"%%s\") <> %s (\"%%s\")\\n\",\n"
667 test_name field1 field2;
668 pr " r->%s, r->%s);\n" field1 field2;
673 List.iter (generate_test_command_call test_name) seq;
674 generate_test_command_call ~test test_name last
675 | TestOutputFileMD5 (seq, filename) ->
676 pr " /* TestOutputFileMD5 for %s (%d) */\n" name i;
677 pr " char expected[33];\n";
678 pr " md5sum (\"%s\", expected);\n" filename;
679 let seq, last = get_seq_last seq in
681 pr " if (STRNEQ (r, expected)) {\n";
682 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
686 List.iter (generate_test_command_call test_name) seq;
687 generate_test_command_call ~test test_name last
688 | TestOutputDevice (seq, expected) ->
689 pr " /* TestOutputDevice for %s (%d) */\n" name i;
690 pr " const char *expected = \"%s\";\n" (c_quote expected);
691 let seq, last = get_seq_last seq in
694 pr " if (STRNEQ (r, expected)) {\n";
695 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
699 List.iter (generate_test_command_call test_name) seq;
700 generate_test_command_call ~test test_name last
701 | TestLastFail seq ->
702 pr " /* TestLastFail for %s (%d) */\n" name i;
703 let seq, last = get_seq_last seq in
704 List.iter (generate_test_command_call test_name) seq;
705 generate_test_command_call test_name ~expect_error:true last
707 (* Generate the code to run a command, leaving the result in 'r'.
708 * If you expect to get an error then you should set expect_error:true.
710 and generate_test_command_call ?(expect_error = false) ?test test_name cmd =
714 (* Look up the command to find out what args/ret it has. *)
717 let _, style, _, _, _, _, _ =
718 List.find (fun (n, _, _, _, _, _, _) -> n = name) all_functions in
721 failwithf "%s: in test, command %s was not found" test_name name in
723 (* If the call has optional args, fold them all together. We cannot
724 * test partial optional args yet.
727 let ret, args, optargs = style in
730 if List.length (snd style) <> List.length args then
731 failwithf "%s: in test, wrong number of args given to %s"
738 | OptString n, "NULL" -> ()
745 pr " const char *%s = \"%s\";\n" n (c_quote arg);
747 pr " const char *%s = \"%s\";\n" n (c_quote arg);
748 pr " size_t %s_size = %d;\n" n (String.length arg)
752 | FileIn _, _ | FileOut _, _ -> ()
753 | StringList n, "" | DeviceList n, "" ->
754 pr " const char *const %s[1] = { NULL };\n" n
755 | StringList n, arg | DeviceList n, arg ->
756 let strs = string_split " " arg in
759 pr " const char *%s_%d = \"%s\";\n" n i (c_quote str);
761 pr " const char *const %s[] = {\n" n;
763 fun i _ -> pr " %s_%d,\n" n i
767 ) (List.combine (snd style) args);
771 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
772 | RInt64 _ -> pr " int64_t r;\n"; "-1"
773 | RConstString _ | RConstOptString _ ->
774 pr " const char *r;\n"; "NULL"
775 | RString _ -> pr " char *r;\n"; "NULL"
776 | RStringList _ | RHashtable _ ->
780 | RStruct (_, typ) ->
781 pr " struct guestfs_%s *r;\n" typ; "NULL"
782 | RStructList (_, typ) ->
783 pr " struct guestfs_%s_list *r;\n" typ; "NULL"
786 pr " size_t size;\n";
789 pr " suppress_error = %d;\n" (if expect_error then 1 else 0);
790 pr " r = guestfs_%s (g" name;
792 (* Generate the parameters. *)
795 | OptString _, "NULL" -> pr ", NULL"
797 | Device n, _ | Dev_or_Path n, _
803 pr ", %s, %s_size" n n
804 | FileIn _, arg | FileOut _, arg ->
805 pr ", \"%s\"" (c_quote arg)
806 | StringList n, _ | DeviceList n, _ ->
807 pr ", (char **) %s" n
810 try int_of_string arg
811 with Failure "int_of_string" ->
812 failwithf "%s: expecting an int, but got '%s'" test_name arg in
816 try Int64.of_string arg
817 with Failure "int_of_string" ->
818 failwithf "%s: expecting an int64, but got '%s'" test_name arg in
821 let b = bool_of_string arg in pr ", %d" (if b then 1 else 0)
822 ) (List.combine (snd style) args);
824 (match fst style with
825 | RBufferOut _ -> pr ", &size"
831 if not expect_error then
832 pr " if (r == %s)\n" error_code
834 pr " if (r != %s)\n" error_code;
837 (* Insert the test code. *)
843 (match fst style with
844 | RErr | RInt _ | RInt64 _ | RBool _
845 | RConstString _ | RConstOptString _ -> ()
846 | RString _ | RBufferOut _ -> pr " free (r);\n"
847 | RStringList _ | RHashtable _ ->
848 pr " for (i = 0; r[i] != NULL; ++i)\n";
849 pr " free (r[i]);\n";
851 | RStruct (_, typ) ->
852 pr " guestfs_free_%s (r);\n" typ
853 | RStructList (_, typ) ->
854 pr " guestfs_free_%s_list (r);\n" typ