← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~pappacena/turnip:py3-pack-test_git into turnip:master

 

Thiago F. Pappacena has proposed merging ~pappacena/turnip:py3-pack-test_git into turnip:master.

Commit message:
Making turnip.pack.tests.test_git compatible with python 3

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~pappacena/turnip/+git/turnip/+merge/388080
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~pappacena/turnip:py3-pack-test_git into turnip:master.
diff --git a/turnip/pack/git.py b/turnip/pack/git.py
index 794a1c3..7f6bb0a 100644
--- a/turnip/pack/git.py
+++ b/turnip/pack/git.py
@@ -13,6 +13,7 @@ __metaclass__ = type
 
 import uuid
 
+import six
 from twisted.internet import (
     defer,
     error,
@@ -221,9 +222,9 @@ class PackServerProtocol(PackProxyProtocol):
     def createAuthParams(self, params):
         auth_params = {}
         for key, value in params.items():
+            key = six.ensure_binary(key)
             if key.startswith(b'turnip-authenticated-'):
-                decoded_key = key[len(b'turnip-authenticated-'):].decode(
-                    'utf-8')
+                decoded_key = key[len(b'turnip-authenticated-'):]
                 auth_params[decoded_key] = value
         if 'uid' in auth_params:
             auth_params['uid'] = int(auth_params['uid'])
@@ -445,7 +446,8 @@ class PackBackendProtocol(PackServerProtocol):
                 clone_from = params.get('clone_from')
                 yield self._createRepo(raw_pathname, clone_from, auth_params)
             except Exception as e:
-                self.die(b'Could not create repository: %s' % e)
+                self.die(b'Could not create repository: %s'
+                         % six.ensure_binary(str(e)))
             self.expectNextCommand()
             return
 
diff --git a/turnip/pack/helpers.py b/turnip/pack/helpers.py
index bf79c53..9cf411d 100644
--- a/turnip/pack/helpers.py
+++ b/turnip/pack/helpers.py
@@ -107,7 +107,8 @@ def encode_request(command, pathname, params):
     bits = [pathname]
     for name in sorted(params):
         value = params[name]
-        value = value if value is not None else b''
+        value = six.ensure_binary(value) if value is not None else b''
+        name = six.ensure_binary(name)
         if b'=' in name or b'\0' in name + value:
             raise ValueError('Metacharacter in arguments')
         bits.append(name + b'=' + value)
diff --git a/turnip/pack/tests/fake_servers.py b/turnip/pack/tests/fake_servers.py
index d9ab901..606dab1 100644
--- a/turnip/pack/tests/fake_servers.py
+++ b/turnip/pack/tests/fake_servers.py
@@ -11,6 +11,7 @@ from collections import defaultdict
 import hashlib
 
 from lazr.sshserver.auth import NoSuchPersonWithName
+import six
 from six.moves import xmlrpc_client
 from twisted.web import xmlrpc
 
@@ -49,6 +50,17 @@ class FakeAuthServerService(xmlrpc.XMLRPC):
             }
 
 
+def xmlrpc_binary_content(data):
+    """Compatibility method to get binary data content for fake XML-RPC server.
+
+    On Python3, binary data is represented as xmlrpc.client.Binary object
+    instead of a simple byte string.
+    """
+    if six.PY3:
+        return data.data
+    return data
+
+
 class FakeVirtInfoService(xmlrpc.XMLRPC):
     """A trivial virt information XML-RPC service.
 
@@ -89,6 +101,8 @@ class FakeVirtInfoService(xmlrpc.XMLRPC):
         if self.require_auth and 'user' not in auth_params:
             raise xmlrpc.Fault(3, "Unauthorized")
 
+        pathname = xmlrpc_binary_content(pathname)
+
         self.translations.append((pathname, permission, auth_params))
         writable = pathname.startswith(b'/+rw')
 
@@ -127,7 +141,9 @@ class FakeVirtInfoService(xmlrpc.XMLRPC):
             return self.merge_proposal_url
 
     def xmlrpc_confirmRepoCreation(self, pathname, auth_params):
+        pathname = xmlrpc_binary_content(pathname)
         self.confirm_repo_creation_call_args.append((pathname, ))
 
     def xmlrpc_abortRepoCreation(self, pathname, auth_params):
+        pathname = xmlrpc_binary_content(pathname)
         self.abort_repo_creation_call_args.append((pathname, ))
diff --git a/turnip/pack/tests/test_git.py b/turnip/pack/tests/test_git.py
index be492ba..e71e811 100644
--- a/turnip/pack/tests/test_git.py
+++ b/turnip/pack/tests/test_git.py
@@ -226,7 +226,7 @@ class TestPackBackendProtocol(TestCase):
         self.assertEqual([], store.delete_repo.call_args_list)
 
         self.assertEqual(
-            [('foo.git', )], self.virtinfo.confirm_repo_creation_call_args)
+            [(b'foo.git', )], self.virtinfo.confirm_repo_creation_call_args)
 
         self.assertEqual(
             (b'git', [b'git', b'upload-pack', full_path], {}),
@@ -429,8 +429,9 @@ class TestPackVirtServerProtocol(TestCase):
         clone_digest = hashlib.sha256(b'foo-repo').hexdigest()
         ANY = mock.ANY
         self.assertEqual(
-            [('turnip-create-repo', digest, {'clone_from': clone_digest}, ANY),
-             ('git-upload-pack', digest, {}, ANY)],
+            [(b'turnip-create-repo', digest,
+              {'clone_from': clone_digest}, ANY),
+             (b'git-upload-pack', digest, {}, ANY)],
             self.proto.requests)
 
     def test_translatePath_timeout(self):