X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=generator%2Fgenerator_capitests.ml;h=196b1fb8cc6679828a499baf68737133ac9df847;hb=2cac52000a6a96a583af72e289a4296c596047d5;hp=f6c71be7c87fdb3278b95310e5361ad9d467762f;hpb=04d8209077d2227eb1d42695ba71147f78987050;p=libguestfs.git diff --git a/generator/generator_capitests.ml b/generator/generator_capitests.ml index f6c71be..196b1fb 100644 --- a/generator/generator_capitests.ml +++ b/generator/generator_capitests.ml @@ -91,6 +91,28 @@ incr (guestfs_h *g, void *iv) (*i)++; } +/* Get md5sum of the named file. */ +static void +md5sum (const char *filename, char *result) +{ + char cmd[256]; + snprintf (cmd, sizeof cmd, \"md5sum %%s\", filename); + FILE *pp = popen (cmd, \"r\"); + if (pp == NULL) { + perror (cmd); + exit (EXIT_FAILURE); + } + if (fread (result, 1, 32, pp) != 32) { + perror (\"md5sum: fread\"); + exit (EXIT_FAILURE); + } + if (pclose (pp) == -1) { + perror (\"pclose\"); + exit (EXIT_FAILURE); + } + result[32] = '\\0'; +} + "; (* Generate a list of commands which are not tested anywhere. *) @@ -153,22 +175,14 @@ int main (int argc, char *argv[]) guestfs_set_error_handler (g, print_error, NULL); - guestfs_set_path (g, \"../appliance\"); - filename = \"test1.img\"; - fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666); + fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666); if (fd == -1) { perror (filename); exit (EXIT_FAILURE); } - if (lseek (fd, %d, SEEK_SET) == -1) { - perror (\"lseek\"); - close (fd); - unlink (filename); - exit (EXIT_FAILURE); - } - if (write (fd, &c, 1) == -1) { - perror (\"write\"); + if (ftruncate (fd, %d) == -1) { + perror (\"ftruncate\"); close (fd); unlink (filename); exit (EXIT_FAILURE); @@ -184,19 +198,13 @@ int main (int argc, char *argv[]) } filename = \"test2.img\"; - fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666); + fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666); if (fd == -1) { perror (filename); exit (EXIT_FAILURE); } - if (lseek (fd, %d, SEEK_SET) == -1) { - perror (\"lseek\"); - close (fd); - unlink (filename); - exit (EXIT_FAILURE); - } - if (write (fd, &c, 1) == -1) { - perror (\"write\"); + if (ftruncate (fd, %d) == -1) { + perror (\"ftruncate\"); close (fd); unlink (filename); exit (EXIT_FAILURE); @@ -212,19 +220,13 @@ int main (int argc, char *argv[]) } filename = \"test3.img\"; - fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666); + fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666); if (fd == -1) { perror (filename); exit (EXIT_FAILURE); } - if (lseek (fd, %d, SEEK_SET) == -1) { - perror (\"lseek\"); - close (fd); - unlink (filename); - exit (EXIT_FAILURE); - } - if (write (fd, &c, 1) == -1) { - perror (\"write\"); + if (ftruncate (fd, %d) == -1) { + perror (\"ftruncate\"); close (fd); unlink (filename); exit (EXIT_FAILURE); @@ -255,6 +257,16 @@ int main (int argc, char *argv[]) /* Cancel previous alarm. */ alarm (0); + /* Create ext2 filesystem on /dev/sdb1 partition. */ + if (guestfs_part_disk (g, \"/dev/sdb\", \"mbr\") == -1) { + printf (\"guestfs_part_disk FAILED\\n\"); + exit (EXIT_FAILURE); + } + if (guestfs_mkfs (g, \"ext2\", \"/dev/sdb1\") == -1) { + printf (\"guestfs_mkfs (/dev/sdb1) FAILED\\n\"); + exit (EXIT_FAILURE); + } + nr_tests = %d; " (500 * 1024 * 1024) (50 * 1024 * 1024) (10 * 1024 * 1024) nr_tests; @@ -433,6 +445,13 @@ and generate_one_test_body name i test_name init test = ["umount_all"]; ["lvm_remove_all"]; ["mount_ro"; "/dev/sdd"; "/"]] + | InitScratchFS -> + pr " /* InitScratchFS for %s */\n" test_name; + List.iter (generate_test_command_call test_name) + [["blockdev_setrw"; "/dev/sda"]; + ["umount_all"]; + ["lvm_remove_all"]; + ["mount_options"; ""; "/dev/sdb1"; "/"]] ); let get_seq_last = function @@ -650,6 +669,32 @@ and generate_one_test_body name i test_name init test = in List.iter (generate_test_command_call test_name) seq; generate_test_command_call ~test test_name last + | TestOutputFileMD5 (seq, filename) -> + pr " /* TestOutputFileMD5 for %s (%d) */\n" name i; + pr " char expected[33];\n"; + pr " md5sum (\"%s\", expected);\n" filename; + let seq, last = get_seq_last seq in + let test () = + pr " if (STRNEQ (r, expected)) {\n"; + pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name; + pr " return -1;\n"; + pr " }\n" + in + List.iter (generate_test_command_call test_name) seq; + generate_test_command_call ~test test_name last + | TestOutputDevice (seq, expected) -> + pr " /* TestOutputDevice for %s (%d) */\n" name i; + pr " const char *expected = \"%s\";\n" (c_quote expected); + let seq, last = get_seq_last seq in + let test () = + pr " r[5] = 's';\n"; + pr " if (STRNEQ (r, expected)) {\n"; + pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name; + pr " return -1;\n"; + pr " }\n" + in + List.iter (generate_test_command_call test_name) seq; + generate_test_command_call ~test test_name last | TestLastFail seq -> pr " /* TestLastFail for %s (%d) */\n" name i; let seq, last = get_seq_last seq in @@ -664,7 +709,7 @@ and generate_test_command_call ?(expect_error = false) ?test test_name cmd = | [] -> assert false | name :: args -> (* Look up the command to find out what args/ret it has. *) - let style = + let style_ret, style_args, style_optargs = try let _, style, _, _, _, _, _ = List.find (fun (n, _, _, _, _, _, _) -> n = name) all_functions in @@ -672,9 +717,24 @@ and generate_test_command_call ?(expect_error = false) ?test test_name cmd = with Not_found -> failwithf "%s: in test, command %s was not found" test_name name in - if List.length (snd style) <> List.length args then - failwithf "%s: in test, wrong number of args given to %s" - test_name name; + (* Match up the arguments strings and argument types. *) + let args, optargs = + let rec loop argts args = + match argts, args with + | (t::ts), (s::ss) -> + let args, rest = loop ts ss in + ((t, s) :: args), rest + | [], ss -> [], ss + | ts, [] -> + failwithf "%s: in test, too few args given to function %s" + test_name name + in + let args, optargs = loop style_args args in + let optargs, rest = loop style_optargs optargs in + if rest <> [] then + failwithf "%s: in test, too many args given to function %s" + test_name name; + args, optargs in pr " {\n"; @@ -709,30 +769,52 @@ and generate_test_command_call ?(expect_error = false) ?test test_name cmd = ) strs; pr " NULL\n"; pr " };\n"; - ) (List.combine (snd style) args); - - let error_code = - match fst style with - | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1" - | RInt64 _ -> pr " int64_t r;\n"; "-1" - | RConstString _ | RConstOptString _ -> - pr " const char *r;\n"; "NULL" - | RString _ -> pr " char *r;\n"; "NULL" - | RStringList _ | RHashtable _ -> - pr " char **r;\n"; - pr " size_t i;\n"; - "NULL" - | RStruct (_, typ) -> - pr " struct guestfs_%s *r;\n" typ; "NULL" - | RStructList (_, typ) -> - pr " struct guestfs_%s_list *r;\n" typ; "NULL" - | RBufferOut _ -> - pr " char *r;\n"; - pr " size_t size;\n"; - "NULL" in + | Pointer _, _ -> + (* Difficult to make these pointers in order to run a test. *) + assert false + ) args; + + (* Currently can only deal with a complete, in-order list of optargs. *) + if optargs <> [] then ( + pr " struct guestfs_%s_argv optargs;\n" name; + let len = List.length style_optargs in + let bitmask = Int64.pred (Int64.shift_left 1L len) in + pr " optargs.bitmask = UINT64_C(0x%Lx);\n" bitmask; + List.iter ( + function + | Bool n, arg + | Int n, arg + | Int64 n, arg -> + pr " optargs.%s = %s;\n" n arg + | String n, arg -> + pr " optargs.%s = \"%s\";\n" n (c_quote arg); + | _ -> assert false + ) optargs; + ); + + (match style_ret with + | RErr | RInt _ | RBool _ -> pr " int r;\n" + | RInt64 _ -> pr " int64_t r;\n" + | RConstString _ | RConstOptString _ -> + pr " const char *r;\n" + | RString _ -> pr " char *r;\n" + | RStringList _ | RHashtable _ -> + pr " char **r;\n"; + pr " size_t i;\n" + | RStruct (_, typ) -> + pr " struct guestfs_%s *r;\n" typ + | RStructList (_, typ) -> + pr " struct guestfs_%s_list *r;\n" typ + | RBufferOut _ -> + pr " char *r;\n"; + pr " size_t size;\n" + ); pr " suppress_error = %d;\n" (if expect_error then 1 else 0); - pr " r = guestfs_%s (g" name; + if optargs = [] then + pr " r = guestfs_%s (g" name + else + pr " r = guestfs_%s_argv (g" name; (* Generate the parameters. *) List.iter ( @@ -764,20 +846,34 @@ and generate_test_command_call ?(expect_error = false) ?test test_name cmd = pr ", %Ld" i | Bool _, arg -> let b = bool_of_string arg in pr ", %d" (if b then 1 else 0) - ) (List.combine (snd style) args); + | Pointer _, _ -> assert false + ) args; - (match fst style with + (match style_ret with | RBufferOut _ -> pr ", &size" | _ -> () ); + if optargs <> [] then + pr ", &optargs"; + pr ");\n"; - if not expect_error then - pr " if (r == %s)\n" error_code - else - pr " if (r != %s)\n" error_code; - pr " return -1;\n"; + (match errcode_of_ret style_ret, expect_error with + | `CannotReturnError, _ -> () + | `ErrorIsMinusOne, false -> + pr " if (r == -1)\n"; + pr " return -1;\n"; + | `ErrorIsMinusOne, true -> + pr " if (r != -1)\n"; + pr " return -1;\n"; + | `ErrorIsNULL, false -> + pr " if (r == NULL)\n"; + pr " return -1;\n"; + | `ErrorIsNULL, true -> + pr " if (r != NULL)\n"; + pr " return -1;\n"; + ); (* Insert the test code. *) (match test with @@ -785,7 +881,7 @@ and generate_test_command_call ?(expect_error = false) ?test test_name cmd = | Some f -> f () ); - (match fst style with + (match style_ret with | RErr | RInt _ | RInt64 _ | RBool _ | RConstString _ | RConstOptString _ -> () | RString _ | RBufferOut _ -> pr " free (r);\n"