Convert image tests to httpretty

Change-Id: I3abc51ba4dcc641b72e3ac5e09955e4b22718451
blueprint: httpretty-testing
This commit is contained in:
Jamie Lennox 2014-05-26 11:24:11 +10:00
parent 817ec0764c
commit 935219a6eb
3 changed files with 157 additions and 38 deletions
novaclient/tests

@ -0,0 +1,119 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import httpretty
from novaclient.openstack.common import jsonutils
from novaclient.tests import fakes
from novaclient.tests.fixture_data import base
class V1(base.Fixture):
base_url = 'images'
def setUp(self):
super(V1, self).setUp()
get_images = {
'images': [
{'id': 1, 'name': 'CentOS 5.2'},
{'id': 2, 'name': 'My Server Backup'}
]
}
httpretty.register_uri(httpretty.GET, self.url(),
body=jsonutils.dumps(get_images),
content_type='application/json')
image_1 = {
'id': 1,
'name': 'CentOS 5.2',
"updated": "2010-10-10T12:00:00Z",
"created": "2010-08-10T12:00:00Z",
"status": "ACTIVE",
"metadata": {
"test_key": "test_value",
},
"links": {},
}
image_2 = {
"id": 2,
"name": "My Server Backup",
"serverId": 1234,
"updated": "2010-10-10T12:00:00Z",
"created": "2010-08-10T12:00:00Z",
"status": "SAVING",
"progress": 80,
"links": {},
}
get_images_detail = {'images': [image_1, image_2]}
httpretty.register_uri(httpretty.GET, self.url('detail'),
body=jsonutils.dumps(get_images_detail),
content_type='application/json')
get_images_1 = {'image': image_1}
httpretty.register_uri(httpretty.GET, self.url(1),
body=jsonutils.dumps(get_images_1),
content_type='application/json')
get_images_2 = {'image': image_2}
httpretty.register_uri(httpretty.GET, self.url(2),
body=jsonutils.dumps(get_images_2),
content_type='application/json')
httpretty.register_uri(httpretty.GET, self.url(456),
body=jsonutils.dumps(get_images_2),
content_type='application/json')
def post_images(request, url, headers):
body = jsonutils.loads(request.body.decode('utf-8'))
assert list(body) == ['image']
fakes.assert_has_keys(body['image'], required=['serverId', 'name'])
return 202, headers, jsonutils.dumps(images_1)
httpretty.register_uri(httpretty.POST, self.url(),
body=post_images,
content_type='application/json')
def post_images_1_metadata(request, url, headers):
body = jsonutils.loads(request.body.decode('utf-8'))
assert list(body) == ['metadata']
fakes.assert_has_keys(body['metadata'], required=['test_key'])
data = jsonutils.dumps({'metadata': image_1['metadata']})
return 200, headers, data
httpretty.register_uri(httpretty.POST, self.url(1, 'metadata'),
body=post_images_1_metadata,
content_type='application/json')
for u in (1, 2, '1/metadata/test_key'):
httpretty.register_uri(httpretty.DELETE, self.url(u),
status=204)
httpretty.register_uri(httpretty.HEAD, self.url(1), status=200,
x_image_meta_id=1,
x_image_meta_name='CentOS 5.2',
x_image_meta_updated='2010-10-10T12:00:00Z',
x_image_meta_created='2010-10-10T12:00:00Z',
x_image_meta_status='ACTIVE',
x_image_meta_property_test_key='test_value')
class V3(V1):
base_url = 'v1/images'

@ -11,56 +11,56 @@
# License for the specific language governing permissions and limitations
# under the License.
from novaclient.tests.fixture_data import client
from novaclient.tests.fixture_data import images as data
from novaclient.tests import utils
from novaclient.tests.v1_1 import fakes
from novaclient.v1_1 import images
cs = fakes.FakeClient()
class ImagesTest(utils.FixturedTestCase):
class ImagesTest(utils.TestCase):
client_fixture_class = client.V1
data_fixture_class = data.V1
def test_list_images(self):
il = cs.images.list()
cs.assert_called('GET', '/images/detail')
il = self.cs.images.list()
self.assert_called('GET', '/images/detail')
[self.assertIsInstance(i, images.Image) for i in il]
def test_list_images_undetailed(self):
il = cs.images.list(detailed=False)
cs.assert_called('GET', '/images')
il = self.cs.images.list(detailed=False)
self.assert_called('GET', '/images')
[self.assertIsInstance(i, images.Image) for i in il]
def test_list_images_with_limit(self):
cs.images.list(limit=4)
cs.assert_called('GET', '/images/detail?limit=4')
self.cs.images.list(limit=4)
self.assert_called('GET', '/images/detail?limit=4')
def test_get_image_details(self):
i = cs.images.get(1)
cs.assert_called('GET', '/images/1')
i = self.cs.images.get(1)
self.assert_called('GET', '/images/1')
self.assertIsInstance(i, images.Image)
self.assertEqual(i.id, 1)
self.assertEqual(i.name, 'CentOS 5.2')
def test_delete_image(self):
cs.images.delete(1)
cs.assert_called('DELETE', '/images/1')
self.cs.images.delete(1)
self.assert_called('DELETE', '/images/1')
def test_delete_meta(self):
cs.images.delete_meta(1, {'test_key': 'test_value'})
cs.assert_called('DELETE', '/images/1/metadata/test_key')
self.cs.images.delete_meta(1, {'test_key': 'test_value'})
self.assert_called('DELETE', '/images/1/metadata/test_key')
def test_set_meta(self):
cs.images.set_meta(1, {'test_key': 'test_value'})
cs.assert_called('POST', '/images/1/metadata',
self.cs.images.set_meta(1, {'test_key': 'test_value'})
self.assert_called('POST', '/images/1/metadata',
{"metadata": {'test_key': 'test_value'}})
def test_find(self):
i = cs.images.find(name="CentOS 5.2")
i = self.cs.images.find(name="CentOS 5.2")
self.assertEqual(i.id, 1)
cs.assert_called('GET', '/images', pos=-2)
cs.assert_called('GET', '/images/1', pos=-1)
self.assert_called('GET', '/images/1')
iml = cs.images.findall(status='SAVING')
iml = self.cs.images.findall(status='SAVING')
self.assertEqual(len(iml), 1)
self.assertEqual(iml[0].name, 'My Server Backup')

@ -13,45 +13,45 @@
# under the License.
from novaclient.tests.fixture_data import client
from novaclient.tests.fixture_data import images as data
from novaclient.tests import utils
from novaclient.tests.v3 import fakes
from novaclient.v3 import images
cs = fakes.FakeClient()
class ImagesTest(utils.FixturedTestCase):
class ImagesTest(utils.TestCase):
client_fixture_class = client.V3
data_fixture_class = data.V3
def test_list_images(self):
il = cs.images.list()
cs.assert_called('GET', '/v1/images/detail')
il = self.cs.images.list()
self.assert_called('GET', '/v1/images/detail')
for i in il:
self.assertIsInstance(i, images.Image)
def test_list_images_undetailed(self):
il = cs.images.list(detailed=False)
cs.assert_called('GET', '/v1/images')
il = self.cs.images.list(detailed=False)
self.assert_called('GET', '/v1/images')
for i in il:
self.assertIsInstance(i, images.Image)
def test_list_images_with_limit(self):
cs.images.list(limit=4)
cs.assert_called('GET', '/v1/images/detail?limit=4')
self.cs.images.list(limit=4)
self.assert_called('GET', '/v1/images/detail?limit=4')
def test_get_image_details(self):
i = cs.images.get(1)
cs.assert_called('HEAD', '/v1/images/1')
i = self.cs.images.get(1)
self.assert_called('HEAD', '/v1/images/1')
self.assertIsInstance(i, images.Image)
self.assertEqual(i.id, '1')
self.assertEqual(i.name, 'CentOS 5.2')
def test_find(self):
i = cs.images.find(name="CentOS 5.2")
i = self.cs.images.find(name="CentOS 5.2")
self.assertEqual(i.id, '1')
cs.assert_called('GET', '/v1/images', pos=-2)
cs.assert_called('HEAD', '/v1/images/1', pos=-1)
self.assert_called('HEAD', '/v1/images/1')
iml = cs.images.findall(status='SAVING')
iml = self.cs.images.findall(status='SAVING')
self.assertEqual(len(iml), 1)
self.assertEqual(iml[0].name, 'My Server Backup')