make test: Add QUIC external echo transfer test

- Add namespace configuration to echo_client
- Add external echo transfer testcase
- Refactor quic test class to reduce unnecessary method args
- Increase default fifo size to 64 and data transfer to 10 mbytes
- Remove sleep() between starting internal server & client apps

Change-Id: Ia9de46a314d58e7809f005f6a5211760e63789bf
Signed-off-by: Dave Wallace <dwallacelf@gmail.com>
diff --git a/test/test_quic.py b/test/test_quic.py
index ee6143d..aed3408 100644
--- a/test/test_quic.py
+++ b/test/test_quic.py
@@ -1,11 +1,24 @@
 #!/usr/bin/env python
+""" Vpp QUIC tests """
 
 import unittest
 import os
-from framework import VppTestCase, VppTestRunner, running_extended_tests
+import subprocess
+import signal
+from framework import VppTestCase, VppTestRunner, running_extended_tests, \
+    Worker
 from vpp_ip_route import VppIpTable, VppIpRoute, VppRoutePath
 
 
+class QUICAppWorker(Worker):
+    """ QUIC Test Application Worker """
+
+    def __init__(self, build_dir, appname, args, logger, env={}):
+        app = "%s/vpp/bin/%s" % (build_dir, appname)
+        self.args = [app] + args
+        super(QUICAppWorker, self).__init__(self.args, logger, env)
+
+
 class QUICTestCase(VppTestCase):
     """ QUIC Test Case """
 
@@ -36,12 +49,10 @@
 
         self.create_loopback_interfaces(2)
         self.uri = "quic://%s/1234" % self.loop0.local_ip4
-        common_args = ["uri", self.uri, "fifo-size", "4"]
+        common_args = ["uri", self.uri, "fifo-size", "64"]
         self.server_echo_test_args = common_args + ["appns", "server"]
         self.client_echo_test_args = common_args + ["appns", "client",
-                                                    "bytes", "1024",
-                                                    "test-bytes",
-                                                    "no-output"]
+                                                    "test-bytes"]
         table_id = 1
         for i in self.lo_interfaces:
             i.admin_up()
@@ -97,10 +108,59 @@
 
     def internal_ipv4_transfer_test(self, server_args, client_args):
         self.start_internal_echo_server(server_args)
-        self.sleep(self.pre_test_sleep)
         self.start_internal_echo_client(client_args)
+
+    def start_external_echo_server(self, args):
+        self.worker_server = QUICAppWorker(self.build_dir, "quic_echo",
+                                           args, self.logger)
+        self.worker_server.start()
+
+    def start_external_echo_client(self, args):
+        self.client_echo_test_args += "use-svm-api"
+        self.worker_client = QUICAppWorker(self.build_dir, "quic_echo",
+                                           args, self.logger)
+        self.worker_client.start()
+        self.worker_client.join(self.timeout)
+        try:
+            self.validateExternalTestResults()
+        except Exception as error:
+            self.fail("Failed with %s" % error)
+
+    def external_ipv4_transfer_test(self, server_args, client_args):
+        self.start_external_echo_server(server_args)
+        self.sleep(self.pre_test_sleep)
+        self.start_external_echo_client(client_args)
         self.sleep(self.post_test_sleep)
 
+    def validateExternalTestResults(self):
+        if os.path.isdir('/proc/{}'.format(self.worker_server.process.pid)):
+            self.logger.info("Killing server worker process (pid %d)" %
+                             self.worker_server.process.pid)
+            os.killpg(os.getpgid(self.worker_server.process.pid),
+                      signal.SIGTERM)
+            self.worker_server.join()
+        self.logger.info("Client worker result is `%s'" %
+                         self.worker_client.result)
+        error = False
+        if self.worker_client.result is None:
+            try:
+                error = True
+                self.logger.error(
+                    "Timeout: %ss! Killing client worker process (pid %d)" %
+                    (self.timeout, self.worker_client.process.pid))
+                os.killpg(os.getpgid(self.worker_client.process.pid),
+                          signal.SIGKILL)
+                self.worker_client.join()
+            except OSError:
+                self.logger.debug(
+                    "Couldn't kill client worker process")
+                raise
+        if error:
+            raise Exception(
+                "Timeout! Client worker did not finish in %ss" % timeout)
+        self.assert_equal(self.worker_client.result, 0,
+                          "Binary test return code")
+
 
 class QUICInternalEchoIPv4TestCase(QUICTestCase):
     """ QUIC Internal Echo IPv4 Transfer Test Cases """
@@ -129,7 +189,8 @@
         """ QUIC internal echo client/server transfer """
 
         self.internal_ipv4_transfer_test(self.server_echo_test_args,
-                                         self.client_echo_test_args)
+                                         self.client_echo_test_args +
+                                         ["no-output", "mbytes", "10"])
 
 
 class QUICInternalEchoIPv4MultiStreamTestCase(QUICTestCase):
@@ -160,7 +221,42 @@
 
         self.internal_ipv4_transfer_test(self.server_echo_test_args,
                                          self.client_echo_test_args +
-                                         ["quic-streams", "10"])
+                                         ["quic-streams", "10",
+                                          "mbytes", "1",
+                                          "no-output"])
+
+
+class QUICExternalEchoIPv4TestCase(QUICTestCase):
+    """ QUIC External Echo IPv4 Transfer Test Cases """
+
+    @classmethod
+    def setUpClass(cls):
+        super(QUICExternalEchoIPv4TestCase, cls).setUpClass()
+
+    @classmethod
+    def tearDownClass(cls):
+        super(QUICExternalEchoIPv4TestCase, cls).tearDownClass()
+
+    def setUp(self):
+        super(QUICExternalEchoIPv4TestCase, self).setUp()
+        self.thru_host_stack_ipv4_setup()
+
+    def tearDown(self):
+        super(QUICExternalEchoIPv4TestCase, self).tearDown()
+        self.thru_host_stack_ipv4_tear_down()
+
+    def show_commands_at_teardown(self):
+        self.logger.debug(self.vapi.cli("show session verbose 2"))
+
+    @unittest.skipUnless(running_extended_tests, "part of extended tests")
+    def test_quic_external_transfer(self):
+        """ QUIC external echo client/server transfer """
+
+        self.external_ipv4_transfer_test(self.server_echo_test_args +
+                                         ["socket-name", self.api_sock],
+                                         self.client_echo_test_args +
+                                         ["socket-name", self.api_sock,
+                                          "mbytes", "10"])
 
 
 if __name__ == '__main__':