Convert keypair tests to httpretty

Change-Id: I6876d97dd6600a0a34b89d9f693078f495085622
blueprint: httpretty-testing
This commit is contained in:
Jamie Lennox 2014-05-26 11:44:37 +10:00
parent 935219a6eb
commit 9348128d35
3 changed files with 77 additions and 22 deletions
novaclient/tests

@ -0,0 +1,52 @@
# 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 = 'os-keypairs'
def setUp(self):
super(V1, self).setUp()
keypair = {'fingerprint': 'FAKE_KEYPAIR', 'name': 'test'}
httpretty.register_uri(httpretty.GET, self.url(),
body=jsonutils.dumps({'keypairs': [keypair]}),
content_type='application/json')
httpretty.register_uri(httpretty.GET, self.url('test'),
body=jsonutils.dumps({'keypair': keypair}),
content_type='application/json')
httpretty.register_uri(httpretty.DELETE, self.url('test'), status=202)
def post_os_keypairs(request, url, headers):
body = jsonutils.loads(request.body.decode('utf-8'))
assert list(body) == ['keypair']
fakes.assert_has_keys(body['keypair'], required=['name'])
return 202, headers, jsonutils.dumps({'keypair': keypair})
httpretty.register_uri(httpretty.POST, self.url(),
body=post_os_keypairs,
content_type='application/json')
class V3(V1):
base_url = 'keypairs'

@ -11,50 +11,54 @@
# 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 keypairs as data
from novaclient.tests import utils
from novaclient.tests.v1_1 import fakes
from novaclient.v1_1 import keypairs
class KeypairsTest(utils.TestCase):
class KeypairsTest(utils.FixturedTestCase):
client_fixture_class = client.V1
data_fixture_class = data.V1
def setUp(self):
super(KeypairsTest, self).setUp()
self.cs = self._get_fake_client()
self.keypair_type = self._get_keypair_type()
self.keypair_prefix = keypairs.KeypairManager.keypair_prefix
def _get_fake_client(self):
return fakes.FakeClient()
self.keypair_prefix = self._get_keypair_prefix()
def _get_keypair_type(self):
return keypairs.Keypair
def _get_keypair_prefix(self):
return keypairs.KeypairManager.keypair_prefix
def test_get_keypair(self):
kp = self.cs.keypairs.get('test')
self.cs.assert_called('GET', '/%s/test' % self.keypair_prefix)
self.assert_called('GET', '/%s/test' % self.keypair_prefix)
self.assertIsInstance(kp, keypairs.Keypair)
self.assertEqual(kp.name, 'test')
def test_list_keypairs(self):
kps = self.cs.keypairs.list()
self.cs.assert_called('GET', '/%s' % self.keypair_prefix)
self.assert_called('GET', '/%s' % self.keypair_prefix)
[self.assertIsInstance(kp, keypairs.Keypair) for kp in kps]
def test_delete_keypair(self):
kp = self.cs.keypairs.list()[0]
kp.delete()
self.cs.assert_called('DELETE', '/%s/test' % self.keypair_prefix)
self.assert_called('DELETE', '/%s/test' % self.keypair_prefix)
self.cs.keypairs.delete('test')
self.cs.assert_called('DELETE', '/%s/test' % self.keypair_prefix)
self.assert_called('DELETE', '/%s/test' % self.keypair_prefix)
self.cs.keypairs.delete(kp)
self.cs.assert_called('DELETE', '/%s/test' % self.keypair_prefix)
self.assert_called('DELETE', '/%s/test' % self.keypair_prefix)
def test_create_keypair(self):
kp = self.cs.keypairs.create("foo")
self.cs.assert_called('POST', '/%s' % self.keypair_prefix)
self.assert_called('POST', '/%s' % self.keypair_prefix)
self.assertIsInstance(kp, keypairs.Keypair)
def test_import_keypair(self):
kp = self.cs.keypairs.create("foo", "fake-public-key")
self.cs.assert_called('POST', '/%s' % self.keypair_prefix)
self.assert_called('POST', '/%s' % self.keypair_prefix)
self.assertIsInstance(kp, keypairs.Keypair)

@ -12,20 +12,19 @@
# 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 keypairs as data
from novaclient.tests.v1_1 import test_keypairs
from novaclient.tests.v3 import fakes
from novaclient.v3 import keypairs
class KeypairsTest(test_keypairs.KeypairsTest):
def setUp(self):
super(KeypairsTest, self).setUp()
self.cs = self._get_fake_client()
self.keypair_type = self._get_keypair_type()
self.keypair_prefix = keypairs.KeypairManager.keypair_prefix
def _get_fake_client(self):
return fakes.FakeClient()
client_fixture_class = client.V3
data_fixture_class = data.V3
def _get_keypair_type(self):
return keypairs.Keypair
def _get_keypair_prefix(self):
return keypairs.KeypairManager.keypair_prefix