From e23767db1b6921038ae8e46e60e954c0dcab23a0 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Thu, 26 Aug 2021 14:39:41 -0400 Subject: [PATCH] Tests pass on Python 2 and Python 3. --- src/allmydata/scripts/admin.py | 5 ++-- src/allmydata/test/cli/test_admin.py | 36 +++++++++++++++------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/allmydata/scripts/admin.py b/src/allmydata/scripts/admin.py index 178701f84..7bce914cc 100644 --- a/src/allmydata/scripts/admin.py +++ b/src/allmydata/scripts/admin.py @@ -11,7 +11,6 @@ if PY2: from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401 from six import ensure_binary, ensure_str -import json try: from allmydata.scripts.types_ import SubCommands @@ -28,7 +27,7 @@ from allmydata.grid_manager import ( from allmydata.scripts.cli import _default_nodedir from allmydata.scripts.common import BaseOptions from allmydata.util.encodingutil import argv_to_abspath - +from allmydata.util import jsonbytes class GenerateKeypairOptions(BaseOptions): @@ -143,7 +142,7 @@ def add_grid_manager_cert(options): config = read_config(nd, "portnum") cert_fname = "{}.cert".format(options['name']) cert_path = FilePath(config.get_config_path(cert_fname)) - cert_bytes = json.dumps(options.certificate_data, indent=4) + '\n' + cert_bytes = jsonbytes.dumps_bytes(options.certificate_data, indent=4) + b'\n' cert_name = options['name'] if cert_path.exists(): diff --git a/src/allmydata/test/cli/test_admin.py b/src/allmydata/test/cli/test_admin.py index 37f733f07..1ac047acb 100644 --- a/src/allmydata/test/cli/test_admin.py +++ b/src/allmydata/test/cli/test_admin.py @@ -1,7 +1,11 @@ -import json -from io import ( - BytesIO, -) +from future.utils import PY3 +from six import ensure_str + +# We're going to override stdin/stderr, so want to match their behavior on respective Python versions. +if PY3: + from io import StringIO +else: + from StringIO import StringIO from twisted.python.usage import ( UsageError, @@ -16,6 +20,7 @@ from allmydata.scripts.admin import ( from allmydata.scripts.runner import ( Options, ) +from allmydata.util import jsonbytes as json from ..common import ( SyncTestCase, ) @@ -31,7 +36,6 @@ class AddCertificateOptions(SyncTestCase): """ Tests for 'tahoe admin add-grid-manager-cert' option validation """ - def setUp(self): self.tahoe = Options() return super(AddCertificateOptions, self).setUp() @@ -40,8 +44,8 @@ class AddCertificateOptions(SyncTestCase): """ When no data is passed to stdin an error is produced """ - self.tahoe.stdin = BytesIO(b"") - self.tahoe.stderr = BytesIO() # suppress message + self.tahoe.stdin = StringIO(ensure_str("")) + self.tahoe.stderr = StringIO() # suppress message with self.assertRaises(UsageError) as ctx: self.tahoe.parseOptions( @@ -63,7 +67,7 @@ class AddCertificateOptions(SyncTestCase): """ tmp = self.mktemp() with open(tmp, "w") as f: - json.dump(fake_cert, f) + f.write(json.dumps(fake_cert)) # certificate should be loaded self.tahoe.parseOptions( @@ -83,8 +87,8 @@ class AddCertificateOptions(SyncTestCase): """ Unparseable data produces an error """ - self.tahoe.stdin = BytesIO(b"{}") - self.tahoe.stderr = BytesIO() # suppress message + self.tahoe.stdin = StringIO(ensure_str("{}")) + self.tahoe.stderr = StringIO() # suppress message with self.assertRaises(UsageError) as ctx: self.tahoe.parseOptions( @@ -111,15 +115,15 @@ class AddCertificateCommand(SyncTestCase): self.node_path = FilePath(self.mktemp()) self.node_path.makedirs() with self.node_path.child("tahoe.cfg").open("w") as f: - f.write("# minimal test config\n") + f.write(b"# minimal test config\n") return super(AddCertificateCommand, self).setUp() def test_add_one(self): """ Adding a certificate succeeds """ - self.tahoe.stdin = BytesIO(json.dumps(fake_cert)) - self.tahoe.stderr = BytesIO() + self.tahoe.stdin = StringIO(json.dumps(fake_cert)) + self.tahoe.stderr = StringIO() self.tahoe.parseOptions( [ "--node-directory", self.node_path.path, @@ -145,8 +149,8 @@ class AddCertificateCommand(SyncTestCase): An error message is produced when adding a certificate with a duplicate name. """ - self.tahoe.stdin = BytesIO(json.dumps(fake_cert)) - self.tahoe.stderr = BytesIO() + self.tahoe.stdin = StringIO(json.dumps(fake_cert)) + self.tahoe.stderr = StringIO() self.tahoe.parseOptions( [ "--node-directory", self.node_path.path, @@ -158,7 +162,7 @@ class AddCertificateCommand(SyncTestCase): rc = add_grid_manager_cert(self.tahoe.subOptions.subOptions) self.assertEqual(rc, 0) - self.tahoe.stdin = BytesIO(json.dumps(fake_cert)) + self.tahoe.stdin = StringIO(json.dumps(fake_cert)) self.tahoe.parseOptions( [ "--node-directory", self.node_path.path,