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 filename = \"test1.img\";
179 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666);
184 if (ftruncate (fd, %d) == -1) {
185 perror (\"ftruncate\");
190 if (close (fd) == -1) {
195 if (guestfs_add_drive (g, filename) == -1) {
196 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
200 filename = \"test2.img\";
201 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666);
206 if (ftruncate (fd, %d) == -1) {
207 perror (\"ftruncate\");
212 if (close (fd) == -1) {
217 if (guestfs_add_drive (g, filename) == -1) {
218 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
222 filename = \"test3.img\";
223 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666);
228 if (ftruncate (fd, %d) == -1) {
229 perror (\"ftruncate\");
234 if (close (fd) == -1) {
239 if (guestfs_add_drive (g, filename) == -1) {
240 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
244 if (guestfs_add_drive_ro (g, \"../images/test.iso\") == -1) {
245 printf (\"guestfs_add_drive_ro ../images/test.iso FAILED\\n\");
249 /* Set a timeout in case qemu hangs during launch (RHBZ#505329). */
252 if (guestfs_launch (g) == -1) {
253 printf (\"guestfs_launch FAILED\\n\");
257 /* Cancel previous alarm. */
262 " (500 * 1024 * 1024) (50 * 1024 * 1024) (10 * 1024 * 1024) nr_tests;
267 pr " if (guestfs_get_verbose (g))\n";
268 pr " printf (\"-------------------------------------------------------------------------------\\n\");\n";
269 pr " printf (\"%%3d/%%3d %s\\n\", test_num, nr_tests);\n" test_name;
270 pr " if (%s () == -1) {\n" test_name;
271 pr " printf (\"%s FAILED\\n\");\n" test_name;
277 pr " /* Check close callback is called. */
278 int close_sentinel = 1;
279 guestfs_set_close_callback (g, incr, &close_sentinel);
283 if (close_sentinel != 2) {
284 fprintf (stderr, \"close callback was not called\\n\");
288 unlink (\"test1.img\");
289 unlink (\"test2.img\");
290 unlink (\"test3.img\");
294 pr " if (n_failed > 0) {\n";
295 pr " printf (\"***** %%lu / %%d tests FAILED *****\\n\", n_failed, nr_tests);\n";
296 pr " exit (EXIT_FAILURE);\n";
300 pr " exit (EXIT_SUCCESS);\n";
303 and generate_one_test name flags i (init, prereq, test) =
304 let test_name = sprintf "test_%s_%d" name i in
307 static int %s_skip (void)
311 str = getenv (\"TEST_ONLY\");
313 return strstr (str, \"%s\") == NULL;
314 str = getenv (\"SKIP_%s\");
315 if (str && STREQ (str, \"1\")) return 1;
316 str = getenv (\"SKIP_TEST_%s\");
317 if (str && STREQ (str, \"1\")) return 1;
321 " test_name name (String.uppercase test_name) (String.uppercase name);
324 | Disabled | Always | IfAvailable _ -> ()
325 | If code | Unless code ->
326 pr "static int %s_prereq (void)\n" test_name;
337 printf (\" %%s skipped (reason: environment variable set)\\n\", \"%s\");
341 " test_name test_name test_name;
343 (* Optional functions should only be tested if the relevant
344 * support is available in the daemon.
349 pr " if (!is_available (\"%s\")) {\n" group;
350 pr " printf (\" %%s skipped (reason: group %%s not available in daemon)\\n\", \"%s\", \"%s\");\n" test_name group;
358 pr " printf (\" %%s skipped (reason: test disabled in generator)\\n\", \"%s\");\n" test_name
360 pr " if (! %s_prereq ()) {\n" test_name;
361 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
365 generate_one_test_body name i test_name init test;
367 pr " if (%s_prereq ()) {\n" test_name;
368 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
372 generate_one_test_body name i test_name init test;
373 | IfAvailable group ->
374 pr " if (!is_available (\"%s\")) {\n" group;
375 pr " printf (\" %%s skipped (reason: %%s not available)\\n\", \"%s\", \"%s\");\n" test_name group;
379 generate_one_test_body name i test_name init test;
381 generate_one_test_body name i test_name init test
389 and generate_one_test_body name i test_name init test =
391 | InitNone (* XXX at some point, InitNone and InitEmpty became
392 * folded together as the same thing. Really we should
393 * make InitNone do nothing at all, but the tests may
394 * need to be checked to make sure this is OK.
397 pr " /* InitNone|InitEmpty for %s */\n" test_name;
398 List.iter (generate_test_command_call test_name)
399 [["blockdev_setrw"; "/dev/sda"];
403 pr " /* InitPartition for %s: create /dev/sda1 */\n" test_name;
404 List.iter (generate_test_command_call test_name)
405 [["blockdev_setrw"; "/dev/sda"];
408 ["part_disk"; "/dev/sda"; "mbr"]]
410 pr " /* InitBasicFS for %s: create ext2 on /dev/sda1 */\n" test_name;
411 List.iter (generate_test_command_call test_name)
412 [["blockdev_setrw"; "/dev/sda"];
415 ["part_disk"; "/dev/sda"; "mbr"];
416 ["mkfs"; "ext2"; "/dev/sda1"];
417 ["mount_options"; ""; "/dev/sda1"; "/"]]
418 | InitBasicFSonLVM ->
419 pr " /* InitBasicFSonLVM for %s: create ext2 on /dev/VG/LV */\n"
421 List.iter (generate_test_command_call test_name)
422 [["blockdev_setrw"; "/dev/sda"];
425 ["part_disk"; "/dev/sda"; "mbr"];
426 ["pvcreate"; "/dev/sda1"];
427 ["vgcreate"; "VG"; "/dev/sda1"];
428 ["lvcreate"; "LV"; "VG"; "8"];
429 ["mkfs"; "ext2"; "/dev/VG/LV"];
430 ["mount_options"; ""; "/dev/VG/LV"; "/"]]
432 pr " /* InitISOFS for %s */\n" test_name;
433 List.iter (generate_test_command_call test_name)
434 [["blockdev_setrw"; "/dev/sda"];
437 ["mount_ro"; "/dev/sdd"; "/"]]
440 let get_seq_last = function
442 failwithf "%s: you cannot use [] (empty list) when expecting a command"
445 let seq = List.rev seq in
446 List.rev (List.tl seq), List.hd seq
451 pr " /* TestRun for %s (%d) */\n" name i;
452 List.iter (generate_test_command_call test_name) seq
453 | TestOutput (seq, expected) ->
454 pr " /* TestOutput for %s (%d) */\n" name i;
455 pr " const char *expected = \"%s\";\n" (c_quote expected);
456 let seq, last = get_seq_last seq in
458 pr " if (STRNEQ (r, expected)) {\n";
459 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
463 List.iter (generate_test_command_call test_name) seq;
464 generate_test_command_call ~test test_name last
465 | TestOutputList (seq, expected) ->
466 pr " /* TestOutputList for %s (%d) */\n" name i;
467 let seq, last = get_seq_last seq in
471 pr " if (!r[%d]) {\n" i;
472 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
473 pr " print_strings (r);\n";
477 pr " const char *expected = \"%s\";\n" (c_quote str);
478 pr " if (STRNEQ (r[%d], expected)) {\n" i;
479 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
484 pr " if (r[%d] != NULL) {\n" (List.length expected);
485 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
487 pr " print_strings (r);\n";
491 List.iter (generate_test_command_call test_name) seq;
492 generate_test_command_call ~test test_name last
493 | TestOutputListOfDevices (seq, expected) ->
494 pr " /* TestOutputListOfDevices for %s (%d) */\n" name i;
495 let seq, last = get_seq_last seq in
499 pr " if (!r[%d]) {\n" i;
500 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
501 pr " print_strings (r);\n";
505 pr " const char *expected = \"%s\";\n" (c_quote str);
506 pr " r[%d][5] = 's';\n" i;
507 pr " if (STRNEQ (r[%d], expected)) {\n" i;
508 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
513 pr " if (r[%d] != NULL) {\n" (List.length expected);
514 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
516 pr " print_strings (r);\n";
520 List.iter (generate_test_command_call test_name) seq;
521 generate_test_command_call ~test test_name last
522 | TestOutputInt (seq, expected) ->
523 pr " /* TestOutputInt for %s (%d) */\n" name i;
524 let seq, last = get_seq_last seq in
526 pr " if (r != %d) {\n" expected;
527 pr " fprintf (stderr, \"%s: expected %d but got %%d\\n\","
533 List.iter (generate_test_command_call test_name) seq;
534 generate_test_command_call ~test test_name last
535 | TestOutputIntOp (seq, op, expected) ->
536 pr " /* TestOutputIntOp for %s (%d) */\n" name i;
537 let seq, last = get_seq_last seq in
539 pr " if (! (r %s %d)) {\n" op expected;
540 pr " fprintf (stderr, \"%s: expected %s %d but got %%d\\n\","
541 test_name op expected;
546 List.iter (generate_test_command_call test_name) seq;
547 generate_test_command_call ~test test_name last
548 | TestOutputTrue seq ->
549 pr " /* TestOutputTrue for %s (%d) */\n" name i;
550 let seq, last = get_seq_last seq in
553 pr " fprintf (stderr, \"%s: expected true, got false\\n\");\n"
558 List.iter (generate_test_command_call test_name) seq;
559 generate_test_command_call ~test test_name last
560 | TestOutputFalse seq ->
561 pr " /* TestOutputFalse for %s (%d) */\n" name i;
562 let seq, last = get_seq_last seq in
565 pr " fprintf (stderr, \"%s: expected false, got true\\n\");\n"
570 List.iter (generate_test_command_call test_name) seq;
571 generate_test_command_call ~test test_name last
572 | TestOutputLength (seq, expected) ->
573 pr " /* TestOutputLength for %s (%d) */\n" name i;
574 let seq, last = get_seq_last seq in
577 pr " for (j = 0; j < %d; ++j)\n" expected;
578 pr " if (r[j] == NULL) {\n";
579 pr " fprintf (stderr, \"%s: short list returned\\n\");\n"
581 pr " print_strings (r);\n";
584 pr " if (r[j] != NULL) {\n";
585 pr " fprintf (stderr, \"%s: long list returned\\n\");\n"
587 pr " print_strings (r);\n";
591 List.iter (generate_test_command_call test_name) seq;
592 generate_test_command_call ~test test_name last
593 | TestOutputBuffer (seq, expected) ->
594 pr " /* TestOutputBuffer for %s (%d) */\n" name i;
595 pr " const char *expected = \"%s\";\n" (c_quote expected);
596 let seq, last = get_seq_last seq in
597 let len = String.length expected in
599 pr " if (size != %d) {\n" len;
600 pr " fprintf (stderr, \"%s: returned size of buffer wrong, expected %d but got %%zu\\n\", size);\n" test_name len;
603 pr " if (STRNEQLEN (r, expected, size)) {\n";
604 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
608 List.iter (generate_test_command_call test_name) seq;
609 generate_test_command_call ~test test_name last
610 | TestOutputStruct (seq, checks) ->
611 pr " /* TestOutputStruct for %s (%d) */\n" name i;
612 let seq, last = get_seq_last seq in
616 | CompareWithInt (field, expected) ->
617 pr " if (r->%s != %d) {\n" field expected;
618 pr " fprintf (stderr, \"%s: %s was %%d, expected %d\\n\",\n"
619 test_name field expected;
620 pr " (int) r->%s);\n" field;
623 | CompareWithIntOp (field, op, expected) ->
624 pr " if (!(r->%s %s %d)) {\n" field op expected;
625 pr " fprintf (stderr, \"%s: %s was %%d, expected %s %d\\n\",\n"
626 test_name field op expected;
627 pr " (int) r->%s);\n" field;
630 | CompareWithString (field, expected) ->
631 pr " if (STRNEQ (r->%s, \"%s\")) {\n" field expected;
632 pr " fprintf (stderr, \"%s: %s was \"%%s\", expected \"%s\"\\n\",\n"
633 test_name field expected;
634 pr " r->%s);\n" field;
637 | CompareFieldsIntEq (field1, field2) ->
638 pr " if (r->%s != r->%s) {\n" field1 field2;
639 pr " fprintf (stderr, \"%s: %s (%%d) <> %s (%%d)\\n\",\n"
640 test_name field1 field2;
641 pr " (int) r->%s, (int) r->%s);\n" field1 field2;
644 | CompareFieldsStrEq (field1, field2) ->
645 pr " if (STRNEQ (r->%s, r->%s)) {\n" field1 field2;
646 pr " fprintf (stderr, \"%s: %s (\"%%s\") <> %s (\"%%s\")\\n\",\n"
647 test_name field1 field2;
648 pr " r->%s, r->%s);\n" field1 field2;
653 List.iter (generate_test_command_call test_name) seq;
654 generate_test_command_call ~test test_name last
655 | TestOutputFileMD5 (seq, filename) ->
656 pr " /* TestOutputFileMD5 for %s (%d) */\n" name i;
657 pr " char expected[33];\n";
658 pr " md5sum (\"%s\", expected);\n" filename;
659 let seq, last = get_seq_last seq in
661 pr " if (STRNEQ (r, expected)) {\n";
662 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
666 List.iter (generate_test_command_call test_name) seq;
667 generate_test_command_call ~test test_name last
668 | TestOutputDevice (seq, expected) ->
669 pr " /* TestOutputDevice for %s (%d) */\n" name i;
670 pr " const char *expected = \"%s\";\n" (c_quote expected);
671 let seq, last = get_seq_last seq in
674 pr " if (STRNEQ (r, expected)) {\n";
675 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
679 List.iter (generate_test_command_call test_name) seq;
680 generate_test_command_call ~test test_name last
681 | TestLastFail seq ->
682 pr " /* TestLastFail for %s (%d) */\n" name i;
683 let seq, last = get_seq_last seq in
684 List.iter (generate_test_command_call test_name) seq;
685 generate_test_command_call test_name ~expect_error:true last
687 (* Generate the code to run a command, leaving the result in 'r'.
688 * If you expect to get an error then you should set expect_error:true.
690 and generate_test_command_call ?(expect_error = false) ?test test_name cmd =
694 (* Look up the command to find out what args/ret it has. *)
697 let _, style, _, _, _, _, _ =
698 List.find (fun (n, _, _, _, _, _, _) -> n = name) all_functions in
701 failwithf "%s: in test, command %s was not found" test_name name in
703 (* If the call has optional args, fold them all together. We cannot
704 * test partial optional args yet.
707 let ret, args, optargs = style in
710 if List.length (snd style) <> List.length args then
711 failwithf "%s: in test, wrong number of args given to %s"
718 | OptString n, "NULL" -> ()
725 pr " const char *%s = \"%s\";\n" n (c_quote arg);
727 pr " const char *%s = \"%s\";\n" n (c_quote arg);
728 pr " size_t %s_size = %d;\n" n (String.length arg)
732 | FileIn _, _ | FileOut _, _ -> ()
733 | StringList n, "" | DeviceList n, "" ->
734 pr " const char *const %s[1] = { NULL };\n" n
735 | StringList n, arg | DeviceList n, arg ->
736 let strs = string_split " " arg in
739 pr " const char *%s_%d = \"%s\";\n" n i (c_quote str);
741 pr " const char *const %s[] = {\n" n;
743 fun i _ -> pr " %s_%d,\n" n i
747 ) (List.combine (snd style) args);
751 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
752 | RInt64 _ -> pr " int64_t r;\n"; "-1"
753 | RConstString _ | RConstOptString _ ->
754 pr " const char *r;\n"; "NULL"
755 | RString _ -> pr " char *r;\n"; "NULL"
756 | RStringList _ | RHashtable _ ->
760 | RStruct (_, typ) ->
761 pr " struct guestfs_%s *r;\n" typ; "NULL"
762 | RStructList (_, typ) ->
763 pr " struct guestfs_%s_list *r;\n" typ; "NULL"
766 pr " size_t size;\n";
769 pr " suppress_error = %d;\n" (if expect_error then 1 else 0);
770 pr " r = guestfs_%s (g" name;
772 (* Generate the parameters. *)
775 | OptString _, "NULL" -> pr ", NULL"
777 | Device n, _ | Dev_or_Path n, _
783 pr ", %s, %s_size" n n
784 | FileIn _, arg | FileOut _, arg ->
785 pr ", \"%s\"" (c_quote arg)
786 | StringList n, _ | DeviceList n, _ ->
787 pr ", (char **) %s" n
790 try int_of_string arg
791 with Failure "int_of_string" ->
792 failwithf "%s: expecting an int, but got '%s'" test_name arg in
796 try Int64.of_string arg
797 with Failure "int_of_string" ->
798 failwithf "%s: expecting an int64, but got '%s'" test_name arg in
801 let b = bool_of_string arg in pr ", %d" (if b then 1 else 0)
802 ) (List.combine (snd style) args);
804 (match fst style with
805 | RBufferOut _ -> pr ", &size"
811 if not expect_error then
812 pr " if (r == %s)\n" error_code
814 pr " if (r != %s)\n" error_code;
817 (* Insert the test code. *)
823 (match fst style with
824 | RErr | RInt _ | RInt64 _ | RBool _
825 | RConstString _ | RConstOptString _ -> ()
826 | RString _ | RBufferOut _ -> pr " free (r);\n"
827 | RStringList _ | RHashtable _ ->
828 pr " for (i = 0; r[i] != NULL; ++i)\n";
829 pr " free (r[i]);\n";
831 | RStruct (_, typ) ->
832 pr " guestfs_free_%s (r);\n" typ
833 | RStructList (_, typ) ->
834 pr " guestfs_free_%s_list (r);\n" typ