Added netpipe test.

Signed-off-by: Bryce Boe <bboe@google.com>



git-svn-id: http://test.kernel.org/svn/autotest/trunk@2136 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/client/tests/netpipe/netpipe.py b/client/tests/netpipe/netpipe.py
new file mode 100644
index 0000000..f3af388
--- /dev/null
+++ b/client/tests/netpipe/netpipe.py
@@ -0,0 +1,110 @@
+import os, time
+from autotest_lib.client.bin import test, autotest_utils
+from autotest_lib.client.common_lib import utils, error
+
+
+class netpipe(test.test):
+    version = 1
+    NP_FILE = '/tmp/np.out'
+
+    # http://www.scl.ameslab.gov/netpipe/code/NetPIPE-3.7.1.tar.gz
+    def setup(self, tarball='NetPIPE-3.7.1.tar.gz'):
+        tarball = utils.unmap_url(self.bindir, tarball, self.tmpdir)
+        autotest_utils.extract_tarball_to_dir(tarball, self.srcdir)
+        os.chdir(self.srcdir)
+        utils.system('make')
+
+
+    def initialize(self):
+        self.job.require_gcc()
+
+        # Add arguments later
+        self.server_path = '%s %%s' % os.path.join(self.srcdir, 'NPtcp')
+        # Add server_ip and arguments later
+        base_path = os.path.join(self.srcdir, 'NPtcp -h')
+        self.client_path = '%s %%s -o %s %%s' % (base_path, self.NP_FILE)
+        self.results = []
+
+    def cleanup(self):
+        # Just in case...
+        utils.system('killall -9 NPtcp', ignore_status=True)
+
+
+    def run_once(self, server_ip, client_ip, role, bidirectional=False,
+                 buffer_size=None, upper_bound=None,
+                 perturbation_size=3):
+        self.role = role
+
+        # Any arguments used must be the same on both the client and the server
+        args = '-p %d ' % perturbation_size
+        if bidirectional:
+            args += '-2 '
+        if buffer_size:
+            args += '-b %d ' % buffer_size
+        if upper_bound:
+            args += '-u %d ' % upper_bound
+
+
+        server_tag = server_ip + '#netpipe-server'
+        client_tag = client_ip + '#netpipe-client'
+        all = [server_tag, client_tag]
+
+        if role == 'server':
+            # Wait up to five minutes for both to reach this point.
+            self.job.barrier(server_tag, 'start', 300).rendevous(*all)
+            self.server_start(args)
+            # Both the client and server should be closed so just to make
+            # sure they are both at the same point wait at most a minute.
+            self.job.barrier(server_tag, 'stop', 60).rendevous(*all)
+        elif role == 'client':
+            # Wait up to five minutes for the server to start
+            self.job.barrier(client_tag, 'start', 300).rendevous(*all)
+            # Sleep 10 seconds to make sure the server is started
+            time.sleep(10)
+            self.client(server_ip, args)
+            # Wait up to 1 minute for the server to also reach this point
+            self.job.barrier(client_tag, 'stop', 60).rendevous(*all)
+        else:
+            raise error.TestError('invalid role specified')
+
+
+    def server_start(self, args):
+        cmd = self.server_path % args
+        self.results.append(utils.system_output(cmd, retain_output=True))
+
+
+    def client(self, server_ip, args):
+        cmd = self.client_path % (server_ip, args)
+
+        try:
+            # We don't care about the actual output since the important stuff
+            # goes to self.NP_FILE
+            utils.system(cmd)
+        except error.CmdError, e:
+            """ Catch errors due to timeout, but raise others
+            The actual error string is:
+              "Command did not complete within %d seconds"
+            called in function join_bg_job in the file common_lib/utils.py
+
+            Looking for 'within' is probably not the best way to do this but
+            works for now"""
+
+            if ('within' in e.additional_text
+                or 'non-zero' in e.additional_text):
+                print e.additional_text
+            else:
+                raise
+
+
+    def postprocess(self):
+        if self.role == 'client':
+            try:
+                output = open(self.NP_FILE)
+                for line in output.readlines():
+                    buff, bandwidth, latency = line.split()
+                    attr = {'buffer_size':buff}
+                    keyval = {'bandwidth':bandwidth, 'latency':latency}
+                    self.write_iteration_keyval(attr, keyval)
+            finally:
+                output.close()
+                os.remove(self.NP_FILE)