Skip to content

Commit 3228713

Browse files
Eric Friedmelwitt
authored andcommitted
Fix functional tests for py3
Fix various things so the functional tests will work under python3: - A hashlib.md5() can only be update()d with an encoded string in py3. - There's no dict.iteritems(), change to dict.items() (which is already an iterator). - Open temp files with 'w+' mode rather than the default 'w+b' (as an alternative to encoding all the write and expected-read payloads as bytes). - (This is a weird one) Explicitly raise SkipTest from unittest (rather than unittest2, which is where cls.skipException landed). Not sure why this is busted, but this moves the ball. Conflict/issue with raising SkipTest on this branch. (cherry picked from commit f1d742f) (cherry picked from commit b866202) Includes squash of: Before writing object data to stdout, re-open it in binary mode Otherwise, you can hit TypeErrors on Python3. Closes-Bug: 1775482 (cherry picked from commit 415b480) Conflicts: openstackclient/tests/functional/identity/v3/common.py NOTE(melwitt): The conflicts are due to the following changes not in Queens: Id8377363f7a3248b45aeeba21d2acc02684a0305 I7c44bbb60557378b66c5c43a7ba917f40dc2b633 Change-Id: Ic9b2b47848a600e87a3674289ae7ae8c3e091fee (cherry picked from commit 47f0277)
1 parent d2e809f commit 3228713

7 files changed

Lines changed: 35 additions & 10 deletions

File tree

openstackclient/api/object_store_v1.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,9 @@ def object_save(
378378
)
379379
if response.status_code == 200:
380380
if file == '-':
381-
for chunk in response.iter_content(64 * 1024):
382-
sys.stdout.write(chunk)
381+
with os.fdopen(sys.stdout.fileno(), 'wb') as f:
382+
for chunk in response.iter_content(64 * 1024):
383+
f.write(chunk)
383384
else:
384385
if not os.path.exists(os.path.dirname(file)):
385386
if len(os.path.dirname(file)) > 0:

openstackclient/tests/functional/common/test_help.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_commands_help_no_auth(self):
8080
"""Check help commands without auth info."""
8181
# Pop all auth info. os.environ will be changed in loop, so do not
8282
# replace os.environ.keys() to os.environ
83-
for key in os.environ.keys():
83+
for key in list(os.environ):
8484
if key.startswith('OS_'):
8585
self.useFixture(fixtures.EnvironmentVariable(key, None))
8686

openstackclient/tests/functional/compute/v2/test_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class ComputeAgentTests(base.TestCase):
2121

2222
# Generate two different md5hash
2323
MD5HASH1 = hashlib.md5()
24-
MD5HASH1.update('agent_1')
24+
MD5HASH1.update('agent_1'.encode('utf-8'))
2525
MD5HASH1 = MD5HASH1.hexdigest()
2626
MD5HASH2 = hashlib.md5()
27-
MD5HASH2.update('agent_2')
27+
MD5HASH2.update('agent_2'.encode('utf-8'))
2828
MD5HASH2 = MD5HASH2.hexdigest()
2929

3030
def test_compute_agent_delete(self):

openstackclient/tests/functional/compute/v2/test_keypair.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_keypair_create_public_key(self):
8888
1) Create keypair with given public key
8989
2) Delete keypair
9090
"""
91-
with tempfile.NamedTemporaryFile() as f:
91+
with tempfile.NamedTemporaryFile(mode='w+') as f:
9292
f.write(self.PUBLIC_KEY)
9393
f.flush()
9494

@@ -108,7 +108,7 @@ def test_keypair_create_private_key(self):
108108
1) Create keypair with private key file
109109
2) Delete keypair
110110
"""
111-
with tempfile.NamedTemporaryFile() as f:
111+
with tempfile.NamedTemporaryFile(mode='w+') as f:
112112
cmd_output = json.loads(self.openstack(
113113
'keypair create -f json --private-key %s tmpkey' % f.name,
114114
))

openstackclient/tests/functional/object/v1/test_object.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def setUp(self):
3333
self.skipTest("No object-store service present")
3434

3535
def test_object(self):
36-
with tempfile.NamedTemporaryFile() as f:
36+
with tempfile.NamedTemporaryFile(mode='w+') as f:
3737
f.write('test content')
3838
f.flush()
3939
self._test_object(f.name)

openstackclient/tests/unit/object/v1/test_object_all.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,25 @@ def test_save_to_stdout(self):
241241

242242
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
243243

244-
with mock.patch('sys.stdout', new=six.BytesIO()) as fake_stdout:
244+
class FakeStdout(six.BytesIO):
245+
def __init__(self):
246+
six.BytesIO.__init__(self)
247+
self.context_manager_calls = []
248+
249+
def __enter__(self):
250+
self.context_manager_calls.append('__enter__')
251+
return self
252+
253+
def __exit__(self, *a):
254+
self.context_manager_calls.append('__exit__')
255+
256+
with mock.patch('sys.stdout') as fake_stdout, mock.patch(
257+
'os.fdopen', return_value=FakeStdout()) as fake_fdopen:
258+
fake_stdout.fileno.return_value = 123
245259
self.cmd.take_action(parsed_args)
246260

247-
self.assertEqual(fake_stdout.getvalue(), object_fakes.object_1_content)
261+
self.assertEqual(fake_fdopen.return_value.getvalue(),
262+
object_fakes.object_1_content)
263+
self.assertEqual(fake_fdopen.mock_calls, [mock.call(123, 'wb')])
264+
self.assertEqual(fake_fdopen.return_value.context_manager_calls,
265+
['__enter__', '__exit__'])
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Re-open stdout in binary mode before writing object data in
5+
``object save --file -`` command.
6+
[Bug `1775482 <https://bugs.launchpad.net/python-openstackclient/+bug/1775482>`_]

0 commit comments

Comments
 (0)