Skip to content

Commit bcf4b3c

Browse files
author
Dean Troyer
committed
Update use of open() in object API
* Switch to use io.open() for py3 compatibility and simpler testing. * Open files in 'rb' mode to avoid translation on Windows Previously tests simply relied on files that were present in the repository to run tests using open(). Change the filenames to ensure that no longer happens. requests_mock doesn't have a way to match against the request body for PUT/POST; an attempt to add a new Matcher to do that worked but it needs to subclass the currently private adapter._Matcher class or duplicate most of its functionality. Change-Id: I8c30b41db20af8ecafe67e760e872fc08adec905
1 parent 49c7422 commit bcf4b3c

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎openstackclient/api/object_store_v1.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
"""Object Store v1 API Library"""
1515

16+
import io
1617
import os
1718
import six
1819

@@ -187,7 +188,12 @@ def object_create(
187188
return {}
188189

189190
full_url = "%s/%s" % (container, object)
190-
response = self.create(full_url, method='PUT', data=open(object))
191+
with io.open(object, 'rb') as f:
192+
response = self.create(
193+
full_url,
194+
method='PUT',
195+
data=f,
196+
)
191197
url_parts = urlparse(self.endpoint)
192198
data = {
193199
'account': url_parts.path.split('/')[-1],

‎openstackclient/tests/api/test_object_store_v1.py‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
"""Object Store v1 API Library Tests"""
1515

16+
import mock
17+
1618
from requests_mock.contrib import fixture
1719

1820
from keystoneclient import session
@@ -175,30 +177,41 @@ class TestObject(TestObjectAPIv1):
175177
def setUp(self):
176178
super(TestObject, self).setUp()
177179

178-
def test_object_create(self):
180+
@mock.patch('openstackclient.api.object_store_v1.io.open')
181+
def base_object_create(self, file_contents, mock_open):
182+
mock_open.read.return_value = file_contents
183+
179184
headers = {
180185
'etag': 'youreit',
181186
'x-trans-id': '1qaz2wsx',
182187
}
188+
# TODO(dtroyer): When requests_mock gains the ability to
189+
# match against request.body add this check
190+
# https://review.openstack.org/127316
183191
self.requests_mock.register_uri(
184192
'PUT',
185-
FAKE_URL + '/qaz/requirements.txt',
193+
FAKE_URL + '/qaz/counter.txt',
186194
headers=headers,
195+
# body=file_contents,
187196
status_code=201,
188197
)
189198
ret = self.api.object_create(
190199
container='qaz',
191-
object='requirements.txt',
200+
object='counter.txt',
192201
)
193202
data = {
194203
'account': FAKE_ACCOUNT,
195204
'container': 'qaz',
196-
'object': 'requirements.txt',
205+
'object': 'counter.txt',
197206
'etag': 'youreit',
198207
'x-trans-id': '1qaz2wsx',
199208
}
200209
self.assertEqual(data, ret)
201210

211+
def test_object_create(self):
212+
self.base_object_create('111\n222\n333\n')
213+
self.base_object_create(bytes([0x31, 0x00, 0x0d, 0x0a, 0x7f, 0xff]))
214+
202215
def test_object_delete(self):
203216
self.requests_mock.register_uri(
204217
'DELETE',

0 commit comments

Comments
 (0)