From c5d37435aa9e257fac1c2d517fadc7630fa0d869 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Mon, 20 Apr 2009 13:14:07 +0100 Subject: [PATCH] Add tests for the upload and download commands. --- .gitignore | 1 + src/generator.ml | 12 ++++- tests.c | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 168 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 174489e..e41c076 100644 --- a/.gitignore +++ b/.gitignore @@ -69,6 +69,7 @@ ruby/ext/guestfs/mkmf.log pod2htm?.tmp stamp-h1 test*.img +test*.tmp tests update-initramfs.sh vmlinuz.* diff --git a/src/generator.ml b/src/generator.ml index 191b94d..ae1dfe9 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -1273,7 +1273,10 @@ Reread the partition table on C. This uses the L command."); ("upload", (RErr, [FileIn "filename"; String "remotefilename"]), 66, [], - [], + [InitBasicFS, TestOutput ( + (* Pick a file from cwd which isn't likely to change. *) + [["upload"; "COPYING.LIB"; "/COPYING.LIB"]; + ["checksum"; "md5"; "/COPYING.LIB"]], "e3eda01d9815f8d24aae2dbd89b68b06")], "upload a file from the local machine", "\ Upload local file C to C on the @@ -1284,7 +1287,12 @@ C can also be a named pipe. See also C."); ("download", (RErr, [String "remotefilename"; FileOut "filename"]), 67, [], - [], + [InitBasicFS, TestOutput ( + (* Pick a file from cwd which isn't likely to change. *) + [["upload"; "COPYING.LIB"; "/COPYING.LIB"]; + ["download"; "/COPYING.LIB"; "testdownload.tmp"]; + ["upload"; "testdownload.tmp"; "/upload"]; + ["checksum"; "md5"; "/upload"]], "e3eda01d9815f8d24aae2dbd89b68b06")], "download a file to the local machine", "\ Download file C and save it as C diff --git a/tests.c b/tests.c index 9744575..876cc29 100644 --- a/tests.c +++ b/tests.c @@ -101,8 +101,6 @@ static void no_test_warnings (void) fprintf (stderr, "warning: \"guestfs_command_lines\" has no tests\n"); fprintf (stderr, "warning: \"guestfs_tune2fs_l\" has no tests\n"); fprintf (stderr, "warning: \"guestfs_blockdev_setbsz\" has no tests\n"); - fprintf (stderr, "warning: \"guestfs_upload\" has no tests\n"); - fprintf (stderr, "warning: \"guestfs_download\" has no tests\n"); } static int test_checksum_0 (void) @@ -614,6 +612,150 @@ static int test_checksum_7 (void) return 0; } +static int test_download_0 (void) +{ + /* InitBasicFS for download (0): create ext2 on /dev/sda1 */ + { + int r; + suppress_error = 0; + r = guestfs_umount_all (g); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_lvm_remove_all (g); + if (r == -1) + return -1; + } + { + char *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestOutput for download (0) */ + { + int r; + suppress_error = 0; + r = guestfs_upload (g, "COPYING.LIB", "/COPYING.LIB"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_download (g, "/COPYING.LIB", "testdownload.tmp"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_upload (g, "testdownload.tmp", "/upload"); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_checksum (g, "md5", "/upload"); + if (r == NULL) + return -1; + if (strcmp (r, "e3eda01d9815f8d24aae2dbd89b68b06") != 0) { + fprintf (stderr, "test_download_0: expected \"e3eda01d9815f8d24aae2dbd89b68b06\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + +static int test_upload_0 (void) +{ + /* InitBasicFS for upload (0): create ext2 on /dev/sda1 */ + { + int r; + suppress_error = 0; + r = guestfs_umount_all (g); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_lvm_remove_all (g); + if (r == -1) + return -1; + } + { + char *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestOutput for upload (0) */ + { + int r; + suppress_error = 0; + r = guestfs_upload (g, "COPYING.LIB", "/COPYING.LIB"); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_checksum (g, "md5", "/COPYING.LIB"); + if (r == NULL) + return -1; + if (strcmp (r, "e3eda01d9815f8d24aae2dbd89b68b06") != 0) { + fprintf (stderr, "test_upload_0: expected \"e3eda01d9815f8d24aae2dbd89b68b06\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + static int test_blockdev_rereadpt_0 (void) { /* InitEmpty for blockdev_rereadpt (0) */ @@ -5017,7 +5159,7 @@ int main (int argc, char *argv[]) exit (1); } - nr_tests = 71; + nr_tests = 73; test_num++; printf ("%3d/%3d test_checksum_0\n", test_num, nr_tests); @@ -5068,6 +5210,18 @@ int main (int argc, char *argv[]) failed++; } test_num++; + printf ("%3d/%3d test_download_0\n", test_num, nr_tests); + if (test_download_0 () == -1) { + printf ("test_download_0 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_upload_0\n", test_num, nr_tests); + if (test_upload_0 () == -1) { + printf ("test_upload_0 FAILED\n"); + failed++; + } + test_num++; printf ("%3d/%3d test_blockdev_rereadpt_0\n", test_num, nr_tests); if (test_blockdev_rereadpt_0 () == -1) { printf ("test_blockdev_rereadpt_0 FAILED\n"); -- 1.8.3.1