Tests pass on Python 2 and Python 3.

This commit is contained in:
Itamar Turner-Trauring 2021-08-26 14:39:41 -04:00
parent 7fa180176e
commit e23767db1b
2 changed files with 22 additions and 19 deletions

View File

@ -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 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 from six import ensure_binary, ensure_str
import json
try: try:
from allmydata.scripts.types_ import SubCommands 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.cli import _default_nodedir
from allmydata.scripts.common import BaseOptions from allmydata.scripts.common import BaseOptions
from allmydata.util.encodingutil import argv_to_abspath from allmydata.util.encodingutil import argv_to_abspath
from allmydata.util import jsonbytes
class GenerateKeypairOptions(BaseOptions): class GenerateKeypairOptions(BaseOptions):
@ -143,7 +142,7 @@ def add_grid_manager_cert(options):
config = read_config(nd, "portnum") config = read_config(nd, "portnum")
cert_fname = "{}.cert".format(options['name']) cert_fname = "{}.cert".format(options['name'])
cert_path = FilePath(config.get_config_path(cert_fname)) 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'] cert_name = options['name']
if cert_path.exists(): if cert_path.exists():

View File

@ -1,7 +1,11 @@
import json from future.utils import PY3
from io import ( from six import ensure_str
BytesIO,
) # 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 ( from twisted.python.usage import (
UsageError, UsageError,
@ -16,6 +20,7 @@ from allmydata.scripts.admin import (
from allmydata.scripts.runner import ( from allmydata.scripts.runner import (
Options, Options,
) )
from allmydata.util import jsonbytes as json
from ..common import ( from ..common import (
SyncTestCase, SyncTestCase,
) )
@ -31,7 +36,6 @@ class AddCertificateOptions(SyncTestCase):
""" """
Tests for 'tahoe admin add-grid-manager-cert' option validation Tests for 'tahoe admin add-grid-manager-cert' option validation
""" """
def setUp(self): def setUp(self):
self.tahoe = Options() self.tahoe = Options()
return super(AddCertificateOptions, self).setUp() return super(AddCertificateOptions, self).setUp()
@ -40,8 +44,8 @@ class AddCertificateOptions(SyncTestCase):
""" """
When no data is passed to stdin an error is produced When no data is passed to stdin an error is produced
""" """
self.tahoe.stdin = BytesIO(b"") self.tahoe.stdin = StringIO(ensure_str(""))
self.tahoe.stderr = BytesIO() # suppress message self.tahoe.stderr = StringIO() # suppress message
with self.assertRaises(UsageError) as ctx: with self.assertRaises(UsageError) as ctx:
self.tahoe.parseOptions( self.tahoe.parseOptions(
@ -63,7 +67,7 @@ class AddCertificateOptions(SyncTestCase):
""" """
tmp = self.mktemp() tmp = self.mktemp()
with open(tmp, "w") as f: with open(tmp, "w") as f:
json.dump(fake_cert, f) f.write(json.dumps(fake_cert))
# certificate should be loaded # certificate should be loaded
self.tahoe.parseOptions( self.tahoe.parseOptions(
@ -83,8 +87,8 @@ class AddCertificateOptions(SyncTestCase):
""" """
Unparseable data produces an error Unparseable data produces an error
""" """
self.tahoe.stdin = BytesIO(b"{}") self.tahoe.stdin = StringIO(ensure_str("{}"))
self.tahoe.stderr = BytesIO() # suppress message self.tahoe.stderr = StringIO() # suppress message
with self.assertRaises(UsageError) as ctx: with self.assertRaises(UsageError) as ctx:
self.tahoe.parseOptions( self.tahoe.parseOptions(
@ -111,15 +115,15 @@ class AddCertificateCommand(SyncTestCase):
self.node_path = FilePath(self.mktemp()) self.node_path = FilePath(self.mktemp())
self.node_path.makedirs() self.node_path.makedirs()
with self.node_path.child("tahoe.cfg").open("w") as f: 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() return super(AddCertificateCommand, self).setUp()
def test_add_one(self): def test_add_one(self):
""" """
Adding a certificate succeeds Adding a certificate succeeds
""" """
self.tahoe.stdin = BytesIO(json.dumps(fake_cert)) self.tahoe.stdin = StringIO(json.dumps(fake_cert))
self.tahoe.stderr = BytesIO() self.tahoe.stderr = StringIO()
self.tahoe.parseOptions( self.tahoe.parseOptions(
[ [
"--node-directory", self.node_path.path, "--node-directory", self.node_path.path,
@ -145,8 +149,8 @@ class AddCertificateCommand(SyncTestCase):
An error message is produced when adding a certificate with a An error message is produced when adding a certificate with a
duplicate name. duplicate name.
""" """
self.tahoe.stdin = BytesIO(json.dumps(fake_cert)) self.tahoe.stdin = StringIO(json.dumps(fake_cert))
self.tahoe.stderr = BytesIO() self.tahoe.stderr = StringIO()
self.tahoe.parseOptions( self.tahoe.parseOptions(
[ [
"--node-directory", self.node_path.path, "--node-directory", self.node_path.path,
@ -158,7 +162,7 @@ class AddCertificateCommand(SyncTestCase):
rc = add_grid_manager_cert(self.tahoe.subOptions.subOptions) rc = add_grid_manager_cert(self.tahoe.subOptions.subOptions)
self.assertEqual(rc, 0) self.assertEqual(rc, 0)
self.tahoe.stdin = BytesIO(json.dumps(fake_cert)) self.tahoe.stdin = StringIO(json.dumps(fake_cert))
self.tahoe.parseOptions( self.tahoe.parseOptions(
[ [
"--node-directory", self.node_path.path, "--node-directory", self.node_path.path,