summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmit Shah <amit.shah@redhat.com>2009-10-26 14:47:19 +0530
committerAmit Shah <amit.shah@redhat.com>2009-10-27 15:36:00 +0530
commit008ded2aaefb795f249bee1d3c1c7fc5f7e6a31c (patch)
tree0c7271688ed905abd11bb8b2711f5f03f9012d6b
parented66b2d7d901b96ed53987d27d78118c3824bc0f (diff)
downloadtest-virtserial-008ded2aaefb795f249bee1d3c1c7fc5f7e6a31c.tar.gz
test-virtserial-008ded2aaefb795f249bee1d3c1c7fc5f7e6a31c.tar.xz
test-virtserial-008ded2aaefb795f249bee1d3c1c7fc5f7e6a31c.zip
auto-test: run a post-test cleanup routine after each test
We don't necessarily consume all the data that's written to ports which can cause that data to linger around in case buffer caching is enabled (enabled by default, only disabled for port 3 here). To ensure this data doesn't cause any side-effects to the other tests that get run on the same port later, just read() all the data so that each port is in a clean state when a new test starts. This was done in per-test functions so far, move it to a common function. This patch also introduces a run_test() function that is passed the function pointer to the test that's to be run and the post- test function is called from run_test(). A pre-test function can also be added, but that's not needed as of now. Signed-off-by: Amit Shah <amit.shah@redhat.com>
-rw-r--r--auto-virtserial.c112
1 files changed, 63 insertions, 49 deletions
diff --git a/auto-virtserial.c b/auto-virtserial.c
index f2db046..4c248d8 100644
--- a/auto-virtserial.c
+++ b/auto-virtserial.c
@@ -534,10 +534,6 @@ static int test_guest_throttle(int nr)
fail(__func__, "throttle");
debug("%s: ret = %d, copied = %zu\n", __func__, ret, copied);
}
- /* Cleanup - free all the buffers that were sent */
- while (guest_poll(nr, 0) & POLLIN) {
- guest_read(nr, BUF_LENGTH);
- }
guest_close_port(nr);
host_close_chardev(nr);
free(buf);
@@ -546,8 +542,6 @@ static int test_guest_throttle(int nr)
static int test_host_throttle(int nr)
{
- char *buf;
- struct pollfd pollfds[1];
size_t copied;
int ret;
@@ -568,19 +562,8 @@ static int test_host_throttle(int nr)
fail(__func__, "throttle");
debug("%s: ret = %d, copied = %zu\n", __func__, ret, copied);
}
-
- host_connect_chardev(nr);
- buf = malloc(BUF_LENGTH);
- if (!buf)
- error(ENOMEM, ENOMEM, "%s", __func__);
- pollfds[0].fd = chardevs[nr].sock;
- pollfds[0].events = POLLIN;
- while ((poll(pollfds, 1, 0) == 1) && (pollfds[0].revents & POLLIN))
- read(chardevs[nr].sock, buf, BUF_LENGTH);
- free(buf);
-
- host_close_chardev(nr);
guest_close_port(nr);
+ host_close_chardev(nr);
return ret;
}
@@ -790,7 +773,6 @@ static int test_file_send(int nr)
{
char buf[BUF_LENGTH];
char csum[BUF_LENGTH];
- struct pollfd pollfds[1];
struct guest_packet gpkt;
int ret, fd, csum_fd;
@@ -812,7 +794,7 @@ static int test_file_send(int nr)
if (ret < 0) {
fail(__func__, "open bigfile");
debug("%s: open_bigfile: ret = %d\n", __func__, ret);
- goto out_clean;
+ goto out_close;
}
guest_set_length(nr, BUF_LENGTH);
@@ -831,14 +813,14 @@ static int test_file_send(int nr)
fail(__func__, "read/write");
debug("%s: read/write: ret = %d\n", __func__, -errno);
fd = -errno;
- goto out_clean;
+ goto out_close;
}
get_guest_response(&gpkt);
if (gpkt.key != KEY_RESULT || gpkt.value != 0) {
fail(__func__, "guest response");
debug("%s: guest responded with %d(%d)\n",
__func__, gpkt.key, gpkt.value);
- goto out_clean;
+ goto out_close;
}
host_connect_chardev(nr);
gpkt.key = KEY_CSUM;
@@ -849,17 +831,17 @@ static int test_file_send(int nr)
if (ret == -1) {
fail(__func__, "csum");
ret = -errno;
- goto out_clean;
+ goto out_close;
}
if (WIFEXITED(ret) != true) {
fail(__func__, "csum2");
ret = -1;
- goto out_clean;
+ goto out_close;
}
if (WEXITSTATUS(ret) != 0) {
fail(__func__, "csum3");
ret = -WEXITSTATUS(ret);
- goto out_clean;
+ goto out_close;
}
csum_fd = open("/tmp/amit/csumfile", O_RDONLY);
if (!csum_fd) {
@@ -873,7 +855,7 @@ static int test_file_send(int nr)
fail(__func__, "guest csum");
debug("%s: guest responded with %d(%d)\n",
__func__, gpkt.key, gpkt.value);
- goto out_clean;
+ goto out_close;
}
/* Guest sent its computed checksum on the same port */
read(chardevs[nr].sock, buf, gpkt.value);
@@ -884,24 +866,49 @@ static int test_file_send(int nr)
debug("%s: guest csum: %s, host csum: %s\n",
__func__, buf, csum);
}
-out_clean:
- /*
- * If read/write above fails, we would have an outstanding
- * guest response to look after.
- */
+out_close:
+ guest_close_port(nr);
+out:
+ return ret;
+}
+
+static void post_test_cleanup(int nr)
+{
+ char buf[BUF_LENGTH];
+ struct pollfd pollfds[1];
+ int ret;
+
+ /* Flush out any data that was left in the guest port */
+ if (!guest_ok)
+ goto skip_guest;
+ ret = guest_open_port(nr);
+ if (ret < 0)
+ goto skip_guest;
+ while ((ret = guest_poll(nr, 0))) {
+ if ((ret > 0) && (ret & POLLIN))
+ guest_read(nr, BUF_LENGTH);
+ else
+ break;
+ }
+ guest_close_port(nr);
+
+skip_guest:
+ /* Flush out any data that was left in the host chardev */
+ host_connect_chardev(nr);
pollfds[0].fd = chardevs[nr].sock;
pollfds[0].events = POLLIN;
- while (poll(pollfds, 1, 0) > 0)
- read(chardevs[1].sock, buf, BUF_LENGTH);
+ while ((poll(pollfds, 1, 0) > 0) && (pollfds[0].revents & POLLIN))
+ read(chardevs[nr].sock, buf, BUF_LENGTH);
+ host_close_chardev(nr);
+}
- while(guest_poll(nr, 0) & POLLIN)
- guest_read(nr, BUF_LENGTH);
+static int run_test(int (test)(int nr), int nr)
+{
+ int ret;
- while (poll(pollfds, 1, 0) > 0)
- read(chardevs[nr].sock, buf, BUF_LENGTH);
+ ret = (test)(nr);
+ post_test_cleanup(nr);
- guest_close_port(nr);
-out:
return ret;
}
@@ -924,31 +931,38 @@ static int start_tests(void)
test_poll(2);
+ /*
+ * Use run_test when for later tests as poll() and
+ * read() calls are used in the post_test_cleanup()
+ * routine. We should make sure they work as expected
+ * before using them.
+ */
+
/* Throttling is not enabled on this port */
- test_guest_throttle(2);
+ run_test(test_guest_throttle, 2);
/* Throttling is enabled on this port */
- test_guest_throttle(4);
+ run_test(test_guest_throttle, 4);
/* Throttling is not enabled on this port */
- test_host_throttle(2);
+ run_test(test_host_throttle, 2);
/* Throttling is enabled on this port */
- test_host_throttle(4);
+ run_test(test_host_throttle, 4);
/* Caching is enabled on this port */
- test_guest_caching(2);
+ run_test(test_guest_caching, 2);
/* Caching is not enabled on this port */
- test_guest_caching(3);
+ run_test(test_guest_caching, 3);
/* Caching is enabled on this port */
- test_host_caching(2);
+ run_test(test_host_caching, 2);
/* Caching is not enabled on this port */
- test_host_caching(3);
+ run_test(test_host_caching, 3);
/* Sends a big file across, compares sha1sums */
- test_file_send(3);
+ run_test(test_file_send, 2);
}
/* The console test should work in any case. */
- test_console(0);
+ run_test(test_console, 0);
return 0;
}