From 8985b6565dc567e2148a980b46606d8345ff0373 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Thu, 11 Oct 2007 19:20:41 -0700 Subject: [PATCH] cli: add test coverage --- src/allmydata/scripts/cli.py | 12 +++-- src/allmydata/scripts/tahoe_get.py | 9 ++-- src/allmydata/scripts/tahoe_ls.py | 6 +-- src/allmydata/scripts/tahoe_put.py | 9 ++-- src/allmydata/scripts/tahoe_rm.py | 6 +-- src/allmydata/test/test_system.py | 76 ++++++++++++++++++++++++++++++ 6 files changed, 101 insertions(+), 17 deletions(-) diff --git a/src/allmydata/scripts/cli.py b/src/allmydata/scripts/cli.py index 7b2a7d32..99e2fa47 100644 --- a/src/allmydata/scripts/cli.py +++ b/src/allmydata/scripts/cli.py @@ -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 = { diff --git a/src/allmydata/scripts/tahoe_get.py b/src/allmydata/scripts/tahoe_get.py index c3fb9fdd..642d9693 100644 --- a/src/allmydata/scripts/tahoe_get.py +++ b/src/allmydata/scripts/tahoe_get.py @@ -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 diff --git a/src/allmydata/scripts/tahoe_ls.py b/src/allmydata/scripts/tahoe_ls.py index f36883fc..bd68c0dd 100644 --- a/src/allmydata/scripts/tahoe_ls.py +++ b/src/allmydata/scripts/tahoe_ls.py @@ -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) diff --git a/src/allmydata/scripts/tahoe_put.py b/src/allmydata/scripts/tahoe_put.py index f88aae04..e2b0feee 100644 --- a/src/allmydata/scripts/tahoe_put.py +++ b/src/allmydata/scripts/tahoe_put.py @@ -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(): diff --git a/src/allmydata/scripts/tahoe_rm.py b/src/allmydata/scripts/tahoe_rm.py index 06a99271..ee2acfe6 100644 --- a/src/allmydata/scripts/tahoe_rm.py +++ b/src/allmydata/scripts/tahoe_rm.py @@ -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(): diff --git a/src/allmydata/test/test_system.py b/src/allmydata/test/test_system.py index 7a3d3701..649dc04f 100644 --- a/src/allmydata/test/test_system.py +++ b/src/allmydata/test/test_system.py @@ -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 + -- 2.45.2