Encoding changes in tests for py3

In many cases we do need to be explicit about bytes vs. strings for
python 3 compatibility.

Change-Id: I9cbc5c73004d03f711f8a6e5752a0865ae55fb9f
This commit is contained in:
Clint Byrum 2017-05-10 20:53:12 -07:00
parent 1d0c7d1941
commit f322fe2eee
7 changed files with 38 additions and 30 deletions

View File

@ -981,7 +981,7 @@ class FakeStatsd(threading.Thread):
return
def stop(self):
os.write(self.wake_write, '1\n')
os.write(self.wake_write, b'1\n')
class FakeBuild(object):
@ -1265,7 +1265,7 @@ class FakeGearmanServer(gear.Server):
for queue in [self.high_queue, self.normal_queue, self.low_queue]:
for job in queue:
if not hasattr(job, 'waiting'):
if job.name.startswith('executor:execute'):
if job.name.startswith(b'executor:execute'):
job.waiting = self.hold_jobs_in_queue
else:
job.waiting = False
@ -1383,7 +1383,7 @@ class FakeNodepool(object):
path = self.REQUEST_ROOT + '/' + oid
try:
data, stat = self.client.get(path)
data = json.loads(data)
data = json.loads(data.decode('utf8'))
data['_oid'] = oid
reqs.append(data)
except kazoo.exceptions.NoNodeError:
@ -1399,7 +1399,7 @@ class FakeNodepool(object):
for oid in sorted(nodeids):
path = self.NODE_ROOT + '/' + oid
data, stat = self.client.get(path)
data = json.loads(data)
data = json.loads(data.decode('utf8'))
data['_oid'] = oid
try:
lockfiles = self.client.get_children(path + '/lock')
@ -1431,7 +1431,7 @@ class FakeNodepool(object):
image_id=None,
host_keys=["fake-key1", "fake-key2"],
executor='fake-nodepool')
data = json.dumps(data)
data = json.dumps(data).encode('utf8')
path = self.client.create(path, data,
makepath=True,
sequence=True)
@ -1460,7 +1460,7 @@ class FakeNodepool(object):
request['state_time'] = time.time()
path = self.REQUEST_ROOT + '/' + oid
data = json.dumps(request)
data = json.dumps(request).encode('utf8')
self.log.debug("Fulfilling node request: %s %s" % (oid, data))
self.client.set(path, data)
@ -2202,8 +2202,9 @@ class ZuulTestCase(BaseTestCase):
if build.url is None:
self.log.debug("%s has not reported start" % build)
return False
# using internal ServerJob which offers no Text interface
worker_build = self.executor_server.job_builds.get(
server_job.unique)
server_job.unique.decode('utf8'))
if worker_build:
if worker_build.isWaiting():
continue
@ -2307,7 +2308,7 @@ class ZuulTestCase(BaseTestCase):
start = time.time()
while time.time() < (start + 5):
for stat in self.statsd.stats:
k, v = stat.split(':')
k, v = stat.decode('utf-8').split(':')
if key == k:
if value is None and kind is None:
return

View File

@ -41,14 +41,14 @@ class TestEncryption(BaseTestCase):
def test_pkcs1_oaep(self):
"Verify encryption and decryption"
orig_plaintext = "some text to encrypt"
orig_plaintext = b"some text to encrypt"
ciphertext = encryption.encrypt_pkcs1_oaep(orig_plaintext, self.public)
plaintext = encryption.decrypt_pkcs1_oaep(ciphertext, self.private)
self.assertEqual(orig_plaintext, plaintext)
def test_openssl_pkcs1_oaep(self):
"Verify that we can decrypt something encrypted with OpenSSL"
orig_plaintext = "some text to encrypt"
orig_plaintext = b"some text to encrypt"
pem_public = encryption.serialize_rsa_public_key(self.public)
public_file = tempfile.NamedTemporaryFile(delete=False)
try:

View File

@ -530,8 +530,8 @@ class TestScheduler(ZuulTestCase):
queue = self.gearman_server.getQueue()
self.assertEqual(len(self.builds), 0)
self.assertEqual(len(queue), 1)
self.assertEqual(queue[0].name, 'executor:execute')
job_args = json.loads(queue[0].arguments)
self.assertEqual(queue[0].name, b'executor:execute')
job_args = json.loads(queue[0].arguments.decode('utf8'))
self.assertEqual(job_args['job'], 'project-merge')
self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
@ -547,17 +547,23 @@ class TestScheduler(ZuulTestCase):
self.assertEqual(len(queue), 6)
self.assertEqual(
json.loads(queue[0].arguments)['job'], 'project-test1')
json.loads(queue[0].arguments.decode('utf8'))['job'],
'project-test1')
self.assertEqual(
json.loads(queue[1].arguments)['job'], 'project-test2')
json.loads(queue[1].arguments.decode('utf8'))['job'],
'project-test2')
self.assertEqual(
json.loads(queue[2].arguments)['job'], 'project-test1')
json.loads(queue[2].arguments.decode('utf8'))['job'],
'project-test1')
self.assertEqual(
json.loads(queue[3].arguments)['job'], 'project-test2')
json.loads(queue[3].arguments.decode('utf8'))['job'],
'project-test2')
self.assertEqual(
json.loads(queue[4].arguments)['job'], 'project-test1')
json.loads(queue[4].arguments.decode('utf8'))['job'],
'project-test1')
self.assertEqual(
json.loads(queue[5].arguments)['job'], 'project-test2')
json.loads(queue[5].arguments.decode('utf8'))['job'],
'project-test2')
self.release(queue[0])
self.waitUntilSettled()
@ -2227,7 +2233,7 @@ class TestScheduler(ZuulTestCase):
self.assertIn('Cache-Control', headers)
self.assertIn('Last-Modified', headers)
self.assertIn('Expires', headers)
data = f.read()
data = f.read().decode('utf8')
self.executor_server.hold_jobs_in_build = False
self.executor_server.release()
@ -2712,7 +2718,7 @@ class TestScheduler(ZuulTestCase):
req = urllib.request.Request(
"http://localhost:%s/tenant-one/status" % port)
f = urllib.request.urlopen(req)
data = f.read()
data = f.read().decode('utf8')
self.executor_server.hold_jobs_in_build = False
# Stop queuing timer triggered jobs so that the assertions

View File

@ -405,7 +405,7 @@ class ExecutorServer(object):
def runCommand(self):
while self._command_running:
try:
command = self.command_socket.get()
command = self.command_socket.get().decode('utf8')
if command != '_stop':
self.command_map[command]()
except Exception:
@ -460,7 +460,8 @@ class ExecutorServer(object):
job.sendWorkFail()
except Exception:
self.log.exception("Exception while running job")
job.sendWorkException(traceback.format_exc())
job.sendWorkException(
traceback.format_exc().encode('utf8'))
except gear.InterruptedError:
pass
except Exception:

View File

@ -46,14 +46,14 @@ class CommandSocket(object):
self.running = False
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(self.path)
s.sendall('_stop\n')
s.sendall(b'_stop\n')
# The command '_stop' will be ignored by our listener, so
# directly inject it into the queue so that consumers of this
# class which are waiting in .get() are awakened. They can
# either handle '_stop' or just ignore the unknown command and
# then check to see if they should continue to run before
# re-entering their loop.
self.queue.put('_stop')
self.queue.put(b'_stop')
self.socket_thread.join()
def _socketListener(self):
@ -61,10 +61,10 @@ class CommandSocket(object):
try:
s, addr = self.socket.accept()
self.log.debug("Accepted socket connection %s" % (s,))
buf = ''
buf = b''
while True:
buf += s.recv(1)
if buf[-1] == '\n':
if buf[-1:] == b'\n':
break
buf = buf.strip()
self.log.debug("Received %s from socket" % (buf,))
@ -72,7 +72,7 @@ class CommandSocket(object):
# Because we use '_stop' internally to wake up a
# waiting thread, don't allow it to actually be
# injected externally.
if buf != '_stop':
if buf != b'_stop':
self.queue.put(buf)
except Exception:
self.log.exception("Exception in socket handler")

View File

@ -199,7 +199,7 @@ class Repo(object):
tree = repo.commit(commit).tree
for fn in files:
if fn in tree:
ret[fn] = tree[fn].data_stream.read()
ret[fn] = tree[fn].data_stream.read().decode('utf8')
else:
ret[fn] = None
return ret

View File

@ -59,10 +59,10 @@ class ZooKeeper(object):
self._became_lost = False
def _dictToStr(self, data):
return json.dumps(data)
return json.dumps(data).encode('utf8')
def _strToDict(self, data):
return json.loads(data)
return json.loads(data.decode('utf8'))
def _connection_listener(self, state):
'''