]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
cli: add test coverage
authorBrian Warner <warner@allmydata.com>
Fri, 12 Oct 2007 02:20:41 +0000 (19:20 -0700)
committerBrian Warner <warner@allmydata.com>
Fri, 12 Oct 2007 02:20:41 +0000 (19:20 -0700)
src/allmydata/scripts/cli.py
src/allmydata/scripts/tahoe_get.py
src/allmydata/scripts/tahoe_ls.py
src/allmydata/scripts/tahoe_put.py
src/allmydata/scripts/tahoe_rm.py
src/allmydata/test/test_system.py

index 7b2a7d322abe4c916ffeab9574b75011337448c2..99e2fa479c5b06962edc8fcab9957e770480b98f 100644 (file)
@@ -106,7 +106,8 @@ def list(config, stdout, stderr):
     from allmydata.scripts import tahoe_ls
     rc = tahoe_ls.list(config['node-url'],
                        config['root-uri'],
-                       config['vdrive_pathname'])
+                       config['vdrive_pathname'],
+                       stdout, stderr)
     return rc
 
 def get(config, stdout, stderr):
@@ -116,7 +117,8 @@ def get(config, stdout, stderr):
     rc = tahoe_get.get(config['node-url'],
                        config['root-uri'],
                        vdrive_filename,
-                       local_filename)
+                       local_filename,
+                       stdout, stderr)
     if rc == 0:
         if local_filename is None or local_filename == "-":
             # be quiet, since the file being written to stdout should be
@@ -140,7 +142,8 @@ def put(config, stdout, stderr):
                        config['root-uri'],
                        local_filename,
                        vdrive_filename,
-                       verbosity)
+                       verbosity,
+                       stdout, stderr)
     return rc
 
 def rm(config, stdout, stderr):
@@ -153,7 +156,8 @@ def rm(config, stdout, stderr):
     rc = tahoe_rm.rm(config['node-url'],
                      config['root-uri'],
                      vdrive_pathname,
-                     verbosity)
+                     verbosity,
+                     stdout, stderr)
     return rc
 
 dispatch = {
index c3fb9fddc6796c08b7511393e64bbfa126d80bbe..642d9693f711716a0330eef5177ffd06aaed66d4 100644 (file)
@@ -2,7 +2,7 @@
 
 import sys, urllib
 
-def get(nodeurl, root_uri, vdrive_fname, local_file):
+def get(nodeurl, root_uri, vdrive_fname, local_file, stdout, stderr):
     if nodeurl[-1] != "/":
         nodeurl += "/"
     url = nodeurl + "uri/%s/" % urllib.quote(root_uri.replace("/","!"))
@@ -10,16 +10,19 @@ def get(nodeurl, root_uri, vdrive_fname, local_file):
         url += vdrive_fname
 
     if local_file is None or local_file == "-":
-        outf = sys.stdout
+        outf = stdout
+        close_outf = False
     else:
         outf = open(local_file, "wb")
+        close_outf = True
     inf = urllib.urlopen(url)
     while True:
         data = inf.read(4096)
         if not data:
             break
         outf.write(data)
-    outf.close()
+    if close_outf:
+        outf.close()
 
     return 0
 
index f36883fc1fc0afca7bde08400761e21d61ea9736..bd68c0dd95c26f09f38e1818bb313037d3565110 100644 (file)
@@ -3,7 +3,7 @@
 import urllib
 import simplejson
 
-def list(nodeurl, root_uri, vdrive_pathname):
+def list(nodeurl, root_uri, vdrive_pathname, stdout, stderr):
     if nodeurl[-1] != "/":
         nodeurl += "/"
     url = nodeurl + "uri/%s/" % urllib.quote(root_uri.replace("/","!"))
@@ -20,11 +20,11 @@ def list(nodeurl, root_uri, vdrive_pathname):
             child = d['children'][name]
             childtype = child[0]
             if childtype == "dirnode":
-                print "%10s %s/" % ("", name)
+                print >>stdout, "%10s %s/" % ("", name)
             else:
                 assert childtype == "filenode"
                 size = child[1]['size']
-                print "%10s %s" % (size, name)
+                print >>stdout, "%10s %s" % (size, name)
 
 
 
index f88aae0406acc3fc501894c93ce08da4639a3bfc..e2b0feee14aa5b177401e259fdf9adcffe2aefb3 100644 (file)
@@ -4,7 +4,8 @@ import re, socket, urllib
 
 NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
 
-def put(nodeurl, root_uri, local_fname, vdrive_fname, verbosity):
+def put(nodeurl, root_uri, local_fname, vdrive_fname, verbosity,
+        stdout, stderr):
     """
     @param verbosity: 0, 1, or 2, meaning quiet, verbose, or very verbose
 
@@ -32,7 +33,7 @@ def put(nodeurl, root_uri, local_fname, vdrive_fname, verbosity):
         try:
             sent = so.send(data)
         except Exception, le:
-            print "got socket error: %s" % (le,)
+            print >>stderr, "got socket error: %s" % (le,)
             return -1
 
         if sent == len(data):
@@ -66,10 +67,10 @@ def put(nodeurl, root_uri, local_fname, vdrive_fname, verbosity):
         word = mo.group(2)
 
         if code in (200, 201,):
-            print "%s %s" % (code, word,)
+            print >>stdout, "%s %s" % (code, word,)
             return 0
     
-    print respstr[headerend:]
+    print >>stderr, respstr[headerend:]
     return 1
 
 def main():
index 06a9927118543f005fdf1ee0773171d5a69a23a1..ee2acfe65f1790ed6586f252d3e182b679169595 100644 (file)
@@ -4,7 +4,7 @@ import re, socket, urllib
 
 NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
 
-def rm(nodeurl, root_uri, vdrive_pathname, verbosity):
+def rm(nodeurl, root_uri, vdrive_pathname, verbosity, stdout, stderr):
     """
     @param verbosity: 0, 1, or 2, meaning quiet, verbose, or very verbose
 
@@ -51,10 +51,10 @@ def rm(nodeurl, root_uri, vdrive_pathname, verbosity):
         word = mo.group(2)
 
         if code == 200:
-            print "%s %s" % (code, word,)
+            print >>stdout, "%s %s" % (code, word,)
             return 0
     
-    print respstr[headerend:]
+    print >>stderr, respstr[headerend:]
     return 1
 
 def main():
index 7a3d37019b8bef4fd090766e5beaec42aaf51968..649dc04f0defd056fa2a73f32a8383a13855fe0e 100644 (file)
@@ -4,6 +4,7 @@ import os, sys
 from cStringIO import StringIO
 from twisted.trial import unittest
 from twisted.internet import defer, reactor
+from twisted.internet import threads # CLI tests use deferToThread
 from twisted.application import service
 from allmydata import client, uri, download, upload
 from allmydata.introducer_and_vdrive import IntroducerAndVdrive
@@ -289,6 +290,7 @@ class SystemTest(testutil.SignalMixin, unittest.TestCase):
         d.addCallback(self._test_web)
         d.addCallback(self._test_web_start)
         d.addCallback(self._test_control)
+        d.addCallback(self._test_cli)
         return d
     test_vdrive.timeout = 1100
 
@@ -690,3 +692,77 @@ class SystemTest(testutil.SignalMixin, unittest.TestCase):
         d.addCallback(lambda res: rref.callRemote("measure_peer_response_time"))
         return d
 
+    def _test_cli(self, res):
+        # run various CLI commands (in a thread, since they use blocking
+        # network calls)
+
+        private_uri = self.clients[0].getServiceNamed("vdrive")._private_uri
+        nodeargs = [
+            "--node-url", self.webish_url,
+            "--root-uri", private_uri,
+            ]
+
+        argv = ["ls"] + nodeargs
+        d = self._run_cli(argv)
+        def _check_ls((out,err)):
+            self.failUnless("personal" in out)
+            self.failUnless("s2-ro" in out)
+            self.failUnless("s2-rw" in out)
+            self.failUnlessEqual(err, "")
+        d.addCallback(_check_ls)
+
+        def _put(res):
+            tdir = self.getdir("cli_put")
+            fileutil.make_dirs(tdir)
+            fn = os.path.join(tdir, "upload_me")
+            f = open(fn, "w")
+            f.write("I will not write the same thing over and over.\n" * 100)
+            f.close()
+            argv = ["put"] + nodeargs + [fn, "test_put/upload.txt"]
+            return self._run_cli(argv)
+        d.addCallback(_put)
+        def _check_put((out,err)):
+            self.failUnless("200 OK" in out)
+            self.failUnlessEqual(err, "")
+            vdrive0 = self.clients[0].getServiceNamed("vdrive")
+            d = vdrive0.get_node_at_path("~/test_put/upload.txt")
+            d.addCallback(lambda filenode: filenode.download_to_data())
+            def _check_put2(res):
+                self.failUnless("I will not write" in res)
+            d.addCallback(_check_put2)
+            return d
+        d.addCallback(_check_put)
+        def _get(res):
+            argv = ["get"] + nodeargs + ["test_put/upload.txt"]
+            return self._run_cli(argv)
+        d.addCallback(_get)
+        def _check_get((out,err)):
+            self.failUnless("I will not write" in out)
+            self.failUnlessEqual(err, "")
+        d.addCallback(_check_get)
+
+        def _rm(res):
+            argv = ["rm"] + nodeargs + ["test_put/upload.txt"]
+            return self._run_cli(argv)
+        d.addCallback(_rm)
+        def _check_rm((out,err)):
+            self.failUnless("200 OK" in out)
+            self.failUnlessEqual(err, "")
+            vdrive0 = self.clients[0].getServiceNamed("vdrive")
+            d = defer.maybeDeferred(vdrive0.get_node_at_path,
+                                    "~/test_put/upload.txt")
+            d.addBoth(self.shouldFail, KeyError, "test_cli._check_rm",
+                      "unable to find child named 'upload.txt'")
+            return d
+        d.addCallback(_check_rm)
+        return d
+
+    def _run_cli(self, argv):
+        stdout, stderr = StringIO(), StringIO()
+        d = threads.deferToThread(runner.runner, argv, run_by_human=False,
+                                  stdout=stdout, stderr=stderr)
+        def _done(res):
+            return stdout.getvalue(), stderr.getvalue()
+        d.addCallback(_done)
+        return d
+