Convert keypairs tests to requests_mock

Change-Id: I85a9d19959e750995fed20a882eb8a9cae9add64
This commit is contained in:
Monty Taylor 2017-06-16 18:10:34 -05:00
parent 70365c9f65
commit 587b41b0f6
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
2 changed files with 95 additions and 39 deletions

View File

@ -130,6 +130,17 @@ def make_fake_server(server_id, name, status='ACTIVE'):
"config_drive": "True"}
def make_fake_keypair(name):
# Note: this is literally taken from:
# https://developer.openstack.org/api-ref/compute/
return {
"fingerprint": "7e:eb:ab:24:ba:d1:e1:88:ae:9a:fb:66:53:df:d3:bd",
"name": name,
"type": "ssh",
"public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCkF3MX59OrlBs3dH5CU7lNmvpbrgZxSpyGjlnE8Flkirnc/Up22lpjznoxqeoTAwTW034k7Dz6aYIrZGmQwe2TkE084yqvlj45Dkyoj95fW/sZacm0cZNuL69EObEGHdprfGJQajrpz22NQoCD8TFB8Wv+8om9NH9Le6s+WPe98WC77KLw8qgfQsbIey+JawPWl4O67ZdL5xrypuRjfIPWjgy/VH85IXg/Z/GONZ2nxHgSShMkwqSFECAC5L3PHB+0+/12M/iikdatFSVGjpuHvkLOs3oe7m6HlOfluSJ85BzLWBbvva93qkGmLg4ZAc8rPh2O+YIsBUHNLLMM/oQp Generated-by-Nova\n", # flake8: noqa
}
def make_fake_stack(id, name, description=None, status='CREATE_COMPLETE'):
return {
'creation_time': '2017-03-23T23:57:12Z',

View File

@ -12,57 +12,102 @@
# limitations under the License.
import shade
from mock import patch
from novaclient import exceptions as nova_exc
from shade import exc
from shade import meta
from shade.tests import fakes
from shade.tests.unit import base
class TestKeypair(base.TestCase):
class TestKeypair(base.RequestsMockTestCase):
@patch.object(shade.OpenStackCloud, 'nova_client')
def test_create_keypair(self, mock_nova):
keyname = 'my_keyname'
pub_key = 'ssh-rsa BLAH'
key = fakes.FakeKeypair('keyid', keyname, pub_key)
mock_nova.keypairs.create.return_value = key
def setUp(self):
super(TestKeypair, self).setUp()
self.keyname = self.getUniqueString('key')
self.key = fakes.make_fake_keypair(self.keyname)
new_key = self.cloud.create_keypair(keyname, pub_key)
mock_nova.keypairs.create.assert_called_once_with(
name=keyname, public_key=pub_key
)
self.assertEqual(meta.obj_to_munch(key), new_key)
def test_create_keypair(self):
self.register_uris([
dict(method='POST',
uri=self.get_mock_url(
'compute', 'public', append=['os-keypairs']),
json={'keypair': self.key},
validate=dict(json={
'keypair': {
'name': self.key['name'],
'public_key': self.key['public_key']}})),
dict(method='GET',
uri=self.get_mock_url(
'compute', 'public',
append=['os-keypairs', self.keyname]),
json={'keypair': self.key}),
])
@patch.object(shade.OpenStackCloud, 'nova_client')
def test_create_keypair_exception(self, mock_nova):
mock_nova.keypairs.create.side_effect = Exception()
self.assertRaises(exc.OpenStackCloudException,
self.cloud.create_keypair, '', '')
new_key = self.cloud.create_keypair(
self.keyname, self.key['public_key'])
self.assertEqual(new_key['name'], self.keyname)
@patch.object(shade.OpenStackCloud, 'nova_client')
def test_delete_keypair(self, mock_nova):
self.assertTrue(self.cloud.delete_keypair('mykey'))
mock_nova.keypairs.delete.assert_called_once_with(
key='mykey'
)
self.assert_calls()
@patch.object(shade.OpenStackCloud, 'nova_client')
def test_delete_keypair_not_found(self, mock_nova):
mock_nova.keypairs.delete.side_effect = nova_exc.NotFound('')
self.assertFalse(self.cloud.delete_keypair('invalid'))
def test_create_keypair_exception(self):
self.register_uris([
dict(method='POST',
uri=self.get_mock_url(
'compute', 'public', append=['os-keypairs']),
status_code=400,
validate=dict(json={
'keypair': {
'name': self.key['name'],
'public_key': self.key['public_key']}})),
])
@patch.object(shade.OpenStackCloud, 'nova_client')
def test_list_keypairs(self, mock_nova):
self.assertRaises(
exc.OpenStackCloudException,
self.cloud.create_keypair,
self.keyname, self.key['public_key'])
self.assert_calls()
def test_delete_keypair(self):
self.register_uris([
dict(method='DELETE',
uri=self.get_mock_url(
'compute', 'public',
append=['os-keypairs', self.keyname]),
status_code=202),
])
self.assertTrue(self.cloud.delete_keypair(self.keyname))
self.assert_calls()
def test_delete_keypair_not_found(self):
self.register_uris([
dict(method='DELETE',
uri=self.get_mock_url(
'compute', 'public',
append=['os-keypairs', self.keyname]),
status_code=404),
])
self.assertFalse(self.cloud.delete_keypair(self.keyname))
self.assert_calls()
def test_list_keypairs(self):
self.register_uris([
dict(method='GET',
uri=self.get_mock_url(
'compute', 'public', append=['os-keypairs']),
json={'keypairs': [self.key]}),
])
self.cloud.list_keypairs()
mock_nova.keypairs.list.assert_called_once_with()
@patch.object(shade.OpenStackCloud, 'nova_client')
def test_list_keypairs_exception(self, mock_nova):
mock_nova.keypairs.list.side_effect = Exception()
def test_list_keypairs_exception(self):
self.register_uris([
dict(method='GET',
uri=self.get_mock_url(
'compute', 'public', append=['os-keypairs']),
status_code=400),
])
self.assertRaises(exc.OpenStackCloudException,
self.cloud.list_keypairs)
self.assert_calls()