mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-02-27 03:37:17 +00:00
fix some test-ability problems and add tests for 'tahoe admin add-grid-manager-cert'
This commit is contained in:
parent
019772a2c2
commit
a34093ed0e
@ -115,8 +115,11 @@ def parse_options(argv, config=None):
|
||||
config.parseOptions(argv) # may raise usage.error
|
||||
return config
|
||||
|
||||
def parse_or_exit_with_explanation(argv, stdout=sys.stdout):
|
||||
def parse_or_exit_with_explanation(argv, stdout=sys.stdout, stderr=sys.stderr, stdin=sys.stdin):
|
||||
config = Options()
|
||||
config.stdout = stdout
|
||||
config.stdin = stdin
|
||||
config.stderr = stderr
|
||||
try:
|
||||
parse_options(argv, config=config)
|
||||
except usage.error as e:
|
||||
@ -141,6 +144,7 @@ def dispatch(config,
|
||||
so.stdout = stdout
|
||||
so.stderr = stderr
|
||||
so.stdin = stdin
|
||||
config.stdin = stdin
|
||||
|
||||
if command in create_dispatch:
|
||||
f = create_dispatch[command]
|
||||
|
@ -2,13 +2,27 @@
|
||||
import os
|
||||
import json
|
||||
|
||||
from ..common import SyncTestCase
|
||||
from ..common import (
|
||||
SyncTestCase,
|
||||
AsyncTestCase,
|
||||
)
|
||||
from allmydata.cli.grid_manager import (
|
||||
grid_manager,
|
||||
)
|
||||
|
||||
import click.testing
|
||||
|
||||
# these imports support the tests for `tahoe *` subcommands
|
||||
from ..common_util import (
|
||||
run_cli,
|
||||
)
|
||||
from twisted.internet.defer import (
|
||||
inlineCallbacks,
|
||||
)
|
||||
from twisted.python.filepath import (
|
||||
FilePath,
|
||||
)
|
||||
|
||||
|
||||
class GridManagerCommandLine(SyncTestCase):
|
||||
"""
|
||||
@ -77,3 +91,77 @@ class GridManagerCommandLine(SyncTestCase):
|
||||
|
||||
result = self.runner.invoke(grid_manager, ["--config", "foo", "list"])
|
||||
self.assertEqual(result.output.strip(), "")
|
||||
|
||||
|
||||
# note: CLITestMixin can't function without also GridTestMixin ... :/
|
||||
class TahoeAddGridManagerCert(AsyncTestCase):
|
||||
"""
|
||||
Test `tahoe admin add-grid-manager-cert` subcommand
|
||||
"""
|
||||
|
||||
@inlineCallbacks
|
||||
def test_help(self):
|
||||
"""
|
||||
some kind of help is printed
|
||||
"""
|
||||
code, out, err = yield run_cli("admin", "add-grid-manager-cert")
|
||||
self.assertEqual(err, "")
|
||||
self.assertNotEqual(0, code)
|
||||
|
||||
@inlineCallbacks
|
||||
def test_no_name(self):
|
||||
"""
|
||||
error to miss --name option
|
||||
"""
|
||||
code, out, err = yield run_cli(
|
||||
"admin", "add-grid-manager-cert", "--filename", "-",
|
||||
stdin="the cert",
|
||||
)
|
||||
self.assertIn(
|
||||
"Must provide --name",
|
||||
out
|
||||
)
|
||||
|
||||
@inlineCallbacks
|
||||
def test_no_filename(self):
|
||||
"""
|
||||
error to miss --name option
|
||||
"""
|
||||
code, out, err = yield run_cli(
|
||||
"admin", "add-grid-manager-cert", "--name", "foo",
|
||||
stdin="the cert",
|
||||
)
|
||||
self.assertIn(
|
||||
"Must provide --filename",
|
||||
out
|
||||
)
|
||||
|
||||
@inlineCallbacks
|
||||
def test_add_one(self):
|
||||
"""
|
||||
we can add a certificate
|
||||
"""
|
||||
nodedir = self.mktemp()
|
||||
fake_cert = """{"certificate": "", "signature": ""}"""
|
||||
|
||||
code, out, err = yield run_cli(
|
||||
"--node-directory", nodedir,
|
||||
"admin", "add-grid-manager-cert", "-f", "-", "--name", "foo",
|
||||
stdin=fake_cert,
|
||||
ignore_stderr=True,
|
||||
)
|
||||
nodepath = FilePath(nodedir)
|
||||
with nodepath.child("tahoe.cfg").open("r") as f:
|
||||
config_data = f.read()
|
||||
|
||||
self.assertIn("tahoe.cfg", nodepath.listdir())
|
||||
self.assertIn(
|
||||
"foo = foo.cert",
|
||||
config_data,
|
||||
)
|
||||
self.assertIn("foo.cert", nodepath.listdir())
|
||||
with nodepath.child("foo.cert").open("r") as f:
|
||||
self.assertEqual(
|
||||
json.load(f),
|
||||
json.loads(fake_cert)
|
||||
)
|
||||
|
@ -40,14 +40,17 @@ def run_cli(verb, *args, **kwargs):
|
||||
"arguments to do_cli must be strs -- convert using unicode_to_argv", args=args)
|
||||
nodeargs = kwargs.get("nodeargs", [])
|
||||
argv = nodeargs + [verb] + list(args)
|
||||
stdin = kwargs.get("stdin", "")
|
||||
stdin = StringIO(kwargs.get("stdin", ""))
|
||||
stdout = StringIO()
|
||||
stderr = StringIO()
|
||||
d = defer.succeed(argv)
|
||||
d.addCallback(runner.parse_or_exit_with_explanation, stdout=stdout)
|
||||
d.addCallback(runner.dispatch,
|
||||
stdin=StringIO(stdin),
|
||||
stdout=stdout, stderr=stderr)
|
||||
d.addCallback(runner.parse_or_exit_with_explanation, stdout=stdout, stderr=stderr, stdin=stdin)
|
||||
d.addCallback(
|
||||
runner.dispatch,
|
||||
stdin=stdin,
|
||||
stdout=stdout,
|
||||
stderr=stderr,
|
||||
)
|
||||
def _done(rc):
|
||||
return 0, stdout.getvalue(), stderr.getvalue()
|
||||
def _err(f):
|
||||
|
Loading…
x
Reference in New Issue
Block a user