[libvirt] [PATCH 3/7] tests: Create test for virCommandDoAsyncIO

Michal Privoznik mprivozn at redhat.com
Wed Jan 23 09:41:29 UTC 2013


This is just a basic test, so we don't break virCommand in the
future. A "Hello world\n" string is written to commanhelper,
which copies input to stdout and stderr where we read it from.
Then the read strings are compared with expected values.
---
 tests/commandtest.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/tests/commandtest.c b/tests/commandtest.c
index 00d2eac..f4e335f 100644
--- a/tests/commandtest.c
+++ b/tests/commandtest.c
@@ -851,6 +851,54 @@ static const char *const newenv[] = {
     NULL
 };
 
+static int test21(const void *unused ATTRIBUTE_UNUSED)
+{
+    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
+    int ret = -1;
+    const char *wrbuf = "Hello world\n";
+    char *outbuf = NULL, *errbuf = NULL;
+    const char *outbufExpected="BEGIN STDOUT\n"
+        "Hello world\n"
+        "END STDOUT\n";
+    const char *errbufExpected="BEGIN STDERR\n"
+        "Hello world\n"
+        "END STDERR\n";
+
+    virCommandSetInputBuffer(cmd, wrbuf);
+    virCommandSetOutputBuffer(cmd, &outbuf);
+    virCommandSetErrorBuffer(cmd, &errbuf);
+    virCommandDoAsyncIO(cmd);
+
+    if (virCommandRunAsync(cmd, NULL) < 0) {
+        virErrorPtr err = virGetLastError();
+        printf("Cannot run child %s\n", err->message);
+        goto cleanup;
+    }
+
+    if (virCommandWait(cmd, NULL) < 0)
+        goto cleanup;
+
+    if (virTestGetVerbose())
+        printf("STDOUT:%s\nSTDERR:%s\n", NULLSTR(outbuf), NULLSTR(errbuf));
+
+    if (STRNEQ(outbuf, outbufExpected)) {
+        virtTestDifference(stderr, outbufExpected, outbuf);
+        goto cleanup;
+    }
+
+    if (STRNEQ(errbuf, errbufExpected)) {
+        virtTestDifference(stderr, errbufExpected, errbuf);
+        goto cleanup;
+    }
+
+    ret = 0;
+cleanup:
+    VIR_FREE(outbuf);
+    VIR_FREE(errbuf);
+    virCommandFree(cmd);
+    return ret;
+}
+
 static void virCommandThreadWorker(void *opaque)
 {
     virCommandTestDataPtr test = opaque;
@@ -983,6 +1031,7 @@ mymain(void)
     DO_TEST(test18);
     DO_TEST(test19);
     DO_TEST(test20);
+    DO_TEST(test21);
 
     virMutexLock(&test->lock);
     if (test->running) {
-- 
1.8.0.2




More information about the libvir-list mailing list