Merge "Update use of open() in object API"

This commit is contained in:
Jenkins 2014-10-24 06:38:22 +00:00 committed by Gerrit Code Review
commit 6a4b673e88
2 changed files with 24 additions and 5 deletions

View File

@ -13,6 +13,7 @@
"""Object Store v1 API Library""" """Object Store v1 API Library"""
import io
import os import os
import six import six
@ -187,7 +188,12 @@ class APIv1(api.BaseAPI):
return {} return {}
full_url = "%s/%s" % (container, object) full_url = "%s/%s" % (container, object)
response = self.create(full_url, method='PUT', data=open(object)) with io.open(object, 'rb') as f:
response = self.create(
full_url,
method='PUT',
data=f,
)
url_parts = urlparse(self.endpoint) url_parts = urlparse(self.endpoint)
data = { data = {
'account': url_parts.path.split('/')[-1], 'account': url_parts.path.split('/')[-1],

View File

@ -13,6 +13,8 @@
"""Object Store v1 API Library Tests""" """Object Store v1 API Library Tests"""
import mock
from requests_mock.contrib import fixture from requests_mock.contrib import fixture
from keystoneclient import session from keystoneclient import session
@ -175,30 +177,41 @@ class TestObject(TestObjectAPIv1):
def setUp(self): def setUp(self):
super(TestObject, self).setUp() super(TestObject, self).setUp()
def test_object_create(self): @mock.patch('openstackclient.api.object_store_v1.io.open')
def base_object_create(self, file_contents, mock_open):
mock_open.read.return_value = file_contents
headers = { headers = {
'etag': 'youreit', 'etag': 'youreit',
'x-trans-id': '1qaz2wsx', 'x-trans-id': '1qaz2wsx',
} }
# TODO(dtroyer): When requests_mock gains the ability to
# match against request.body add this check
# https://review.openstack.org/127316
self.requests_mock.register_uri( self.requests_mock.register_uri(
'PUT', 'PUT',
FAKE_URL + '/qaz/requirements.txt', FAKE_URL + '/qaz/counter.txt',
headers=headers, headers=headers,
# body=file_contents,
status_code=201, status_code=201,
) )
ret = self.api.object_create( ret = self.api.object_create(
container='qaz', container='qaz',
object='requirements.txt', object='counter.txt',
) )
data = { data = {
'account': FAKE_ACCOUNT, 'account': FAKE_ACCOUNT,
'container': 'qaz', 'container': 'qaz',
'object': 'requirements.txt', 'object': 'counter.txt',
'etag': 'youreit', 'etag': 'youreit',
'x-trans-id': '1qaz2wsx', 'x-trans-id': '1qaz2wsx',
} }
self.assertEqual(data, ret) self.assertEqual(data, ret)
def test_object_create(self):
self.base_object_create('111\n222\n333\n')
self.base_object_create(bytes([0x31, 0x00, 0x0d, 0x0a, 0x7f, 0xff]))
def test_object_delete(self): def test_object_delete(self):
self.requests_mock.register_uri( self.requests_mock.register_uri(
'DELETE', 'DELETE',