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("/","!"))
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)
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
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):
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():
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
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():
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
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
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
+