Merge "Validate keypair when create a bay"
This commit is contained in:
commit
534e44c546
|
@ -12,6 +12,7 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from novaclient import exceptions as nova_exception
|
||||
import six
|
||||
|
||||
from magnum.common import clients
|
||||
|
@ -43,9 +44,10 @@ def validate_flavor(cli, flavor):
|
|||
def validate_keypair(cli, keypair):
|
||||
"""Validate keypair"""
|
||||
|
||||
# TODO(houming):this method implement will be added after this
|
||||
# first pathch for bay's OpenStack resources validation is merged.
|
||||
pass
|
||||
try:
|
||||
cli.nova().keypairs.get(keypair)
|
||||
except nova_exception.NotFound:
|
||||
raise exception.KeyPairNotFound(keypair=keypair)
|
||||
|
||||
|
||||
def validate_external_network(cli, external_network):
|
||||
|
|
|
@ -576,8 +576,7 @@ class TestPost(api_base.FunctionalTest):
|
|||
self.assertEqual(201, response.status_int)
|
||||
|
||||
@mock.patch('magnum.api.attr_validator.validate_os_resources')
|
||||
def test_create_bay_with_invalid_flavor(self,
|
||||
mock_valid_os_res):
|
||||
def test_create_bay_with_invalid_flavor(self, mock_valid_os_res):
|
||||
bdict = apiutils.bay_post_data()
|
||||
mock_valid_os_res.side_effect = exception.FlavorNotFound('test-flavor')
|
||||
response = self.post_json('/bays', bdict, expect_errors=True)
|
||||
|
@ -586,8 +585,7 @@ class TestPost(api_base.FunctionalTest):
|
|||
self.assertEqual(404, response.status_int)
|
||||
|
||||
@mock.patch('magnum.api.attr_validator.validate_os_resources')
|
||||
def test_create_bay_with_invalid_ext_network(self,
|
||||
mock_valid_os_res):
|
||||
def test_create_bay_with_invalid_ext_network(self, mock_valid_os_res):
|
||||
bdict = apiutils.bay_post_data()
|
||||
mock_valid_os_res.side_effect = exception.NetworkNotFound('test-net')
|
||||
response = self.post_json('/bays', bdict, expect_errors=True)
|
||||
|
@ -595,6 +593,15 @@ class TestPost(api_base.FunctionalTest):
|
|||
self.assertTrue(mock_valid_os_res.called)
|
||||
self.assertEqual(404, response.status_int)
|
||||
|
||||
@mock.patch('magnum.api.attr_validator.validate_os_resources')
|
||||
def test_create_bay_with_invalid_keypair(self, mock_valid_os_res):
|
||||
bdict = apiutils.bay_post_data()
|
||||
mock_valid_os_res.side_effect = exception.KeyPairNotFound('test-key')
|
||||
response = self.post_json('/bays', bdict, expect_errors=True)
|
||||
self.assertEqual('application/json', response.content_type)
|
||||
self.assertTrue(mock_valid_os_res.called)
|
||||
self.assertEqual(404, response.status_int)
|
||||
|
||||
|
||||
class TestDelete(api_base.FunctionalTest):
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
|
||||
import mock
|
||||
from novaclient import exceptions as nova_exc
|
||||
|
||||
from magnum.api import attr_validator
|
||||
from magnum.common import exception
|
||||
|
@ -68,6 +69,24 @@ class TestAttrValidator(base.BaseTestCase):
|
|||
attr_validator.validate_external_network,
|
||||
mock_os_cli, 'test_ext_net')
|
||||
|
||||
def test_validate_keypair_with_valid_keypair(self):
|
||||
mock_keypair = mock.MagicMock()
|
||||
mock_keypair.id = 'test-keypair'
|
||||
mock_nova = mock.MagicMock()
|
||||
mock_nova.keypairs.get.return_value = mock_keypair
|
||||
mock_os_cli = mock.MagicMock()
|
||||
mock_os_cli.nova.return_value = mock_nova
|
||||
attr_validator.validate_keypair(mock_os_cli, 'test-keypair')
|
||||
|
||||
def test_validate_keypair_with_invalid_keypair(self):
|
||||
mock_nova = mock.MagicMock()
|
||||
mock_nova.keypairs.get.side_effect = nova_exc.NotFound('test-keypair')
|
||||
mock_os_cli = mock.MagicMock()
|
||||
mock_os_cli.nova.return_value = mock_nova
|
||||
self.assertRaises(exception.KeyPairNotFound,
|
||||
attr_validator.validate_keypair,
|
||||
mock_os_cli, 'test_keypair')
|
||||
|
||||
@mock.patch('magnum.objects.baymodel.BayModel.get_by_uuid')
|
||||
@mock.patch('magnum.common.clients.OpenStackClients')
|
||||
def test_validate_os_resources_with_invalid_flavor(self,
|
||||
|
|
Loading…
Reference in New Issue