From bcf4b3caece9edc6a04be3ce59697301e48ad34a Mon Sep 17 00:00:00 2001 From: Dean Troyer Date: Wed, 8 Oct 2014 23:22:24 -0500 Subject: [PATCH] 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 --- openstackclient/api/object_store_v1.py | 8 ++++++- .../tests/api/test_object_store_v1.py | 21 +++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/openstackclient/api/object_store_v1.py b/openstackclient/api/object_store_v1.py index 57db906398..c52eeb3ac9 100644 --- a/openstackclient/api/object_store_v1.py +++ b/openstackclient/api/object_store_v1.py @@ -13,6 +13,7 @@ """Object Store v1 API Library""" +import io import os import six @@ -187,7 +188,12 @@ class APIv1(api.BaseAPI): return {} 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) data = { 'account': url_parts.path.split('/')[-1], diff --git a/openstackclient/tests/api/test_object_store_v1.py b/openstackclient/tests/api/test_object_store_v1.py index 5a376a454b..b18a003db5 100644 --- a/openstackclient/tests/api/test_object_store_v1.py +++ b/openstackclient/tests/api/test_object_store_v1.py @@ -13,6 +13,8 @@ """Object Store v1 API Library Tests""" +import mock + from requests_mock.contrib import fixture from keystoneclient import session @@ -175,30 +177,41 @@ class TestObject(TestObjectAPIv1): def setUp(self): 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 = { 'etag': 'youreit', '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( 'PUT', - FAKE_URL + '/qaz/requirements.txt', + FAKE_URL + '/qaz/counter.txt', headers=headers, + # body=file_contents, status_code=201, ) ret = self.api.object_create( container='qaz', - object='requirements.txt', + object='counter.txt', ) data = { 'account': FAKE_ACCOUNT, 'container': 'qaz', - 'object': 'requirements.txt', + 'object': 'counter.txt', 'etag': 'youreit', 'x-trans-id': '1qaz2wsx', } 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): self.requests_mock.register_uri( 'DELETE',