Merge "Ensure api can work as expected with boolean query params"

This commit is contained in:
Jenkins 2017-01-13 15:31:22 +00:00 committed by Gerrit Code Review
commit 9b8eea4ed1
5 changed files with 82 additions and 5 deletions

View File

@ -284,6 +284,12 @@ class ContainersController(rest.RestController):
new_container = objects.Container(context, **container_dict)
new_container.create(context)
try:
run = strutils.bool_from_string(run, strict=True)
except ValueError:
msg = _('Valid run values are true, false, 0, 1, yes and no')
raise exception.InvalidValue(msg)
if run:
compute_api.container_run(context, new_container)
else:
@ -334,7 +340,11 @@ class ContainersController(rest.RestController):
"""
container = _get_container(container_id)
check_policy_on_container(container.as_dict(), "container:delete")
force = strutils.bool_from_string(force, strict=True)
try:
force = strutils.bool_from_string(force, strict=True)
except ValueError:
msg = _('Valid force values are true, false, 0, 1, yes and no')
raise exception.InvalidValue(msg)
if not force:
utils.validate_container_state(container, 'delete')
context = pecan.request.context

View File

@ -11,6 +11,7 @@
# under the License.
from oslo_log import log as logging
from oslo_utils import strutils
from oslo_utils import timeutils
import pecan
from pecan import rest
@ -217,5 +218,11 @@ class ImagesController(rest.RestController):
action="image:search")
LOG.debug('Calling compute.image_search with %s' %
image)
try:
exact_match = strutils.bool_from_string(exact_match, strict=True)
except ValueError:
msg = _("Valid exact_match values are true,"
" false, 0, 1, yes and no")
raise exception.InvalidValue(msg)
return pecan.request.compute_api.image_search(context, image,
exact_match)

View File

@ -52,10 +52,10 @@ class ZunClient(rest_client.RestClient):
return resp, model_type.from_json(body)
@classmethod
def containers_uri(cls, action=None):
def containers_uri(cls, params=None):
url = "/containers/"
if action:
url = "{0}/{1}".format(url, action)
if params:
url = cls.add_params(url, params)
return url
@classmethod
@ -105,7 +105,7 @@ class ZunClient(rest_client.RestClient):
def run_container(self, model, **kwargs):
resp, body = self.post(
self.containers_uri(action='run'),
self.containers_uri(params={'run': True}),
body=model.to_json(), **kwargs)
return self.deserialize(resp, body, container_model.ContainerEntity)

View File

@ -40,6 +40,33 @@ class TestContainerController(api_base.FunctionalTest):
self.assertEqual(202, response.status_int)
self.assertTrue(mock_container_run.called)
@patch('zun.compute.rpcapi.API.container_run')
@patch('zun.compute.rpcapi.API.image_search')
def test_run_container_with_false(self, mock_search,
mock_container_run):
mock_container_run.side_effect = lambda x, y: y
params = ('{"name": "MyDocker", "image": "ubuntu",'
'"command": "env", "memory": "512",'
'"environment": {"key1": "val1", "key2": "val2"}}')
response = self.app.post('/v1/containers?run=false',
params=params,
content_type='application/json')
self.assertEqual(202, response.status_int)
self.assertFalse(mock_container_run.called)
@patch('zun.compute.rpcapi.API.container_run')
@patch('zun.compute.rpcapi.API.image_search')
def test_run_container_with_wrong(self, mock_search,
mock_container_run):
mock_container_run.side_effect = exception.InvalidValue
params = ('{"name": "MyDocker", "image": "ubuntu",'
'"command": "env", "memory": "512",'
'"environment": {"key1": "val1", "key2": "val2"}}')
self.assertRaises(AppError, self.app.post, '/v1/containers?run=wrong',
params=params, content_type='application/json')
self.assertTrue(mock_container_run.not_called)
@patch('zun.compute.api.API.container_create')
@patch('zun.compute.api.API.image_search')
def test_create_container(self, mock_search, mock_container_create):
@ -713,6 +740,16 @@ class TestContainerController(api_base.FunctionalTest):
test_object.uuid))
self.assertEqual(204, response.status_int)
@patch('zun.compute.api.API.container_delete')
def test_delete_by_uuid_with_force_wrong(self, mock_delete):
uuid = uuidutils.generate_uuid()
test_object = utils.create_test_container(context=self.context,
uuid=uuid)
mock_delete.side_effect = exception.InvalidValue
self.assertRaises(AppError, self.app.delete,
'/v1/containers/%s?force=wrong' % test_object.uuid)
self.assertTrue(mock_delete.not_called)
@patch('zun.common.utils.validate_container_state')
@patch('zun.compute.api.API.container_delete')
@patch('zun.objects.Container.get_by_name')

View File

@ -179,6 +179,29 @@ class TestImageController(api_base.FunctionalTest):
mock_image_search.assert_called_once_with(
mock.ANY, 'redis', False)
@patch('zun.compute.rpcapi.API.image_search')
def test_search_image_with_exact_match_true(self, mock_image_search):
mock_image_search.return_value = {'name': 'redis', 'stars': 2000}
response = self.app.get('/v1/images/redis/search?exact_match=true')
self.assertEqual(200, response.status_int)
mock_image_search.assert_called_once_with(
mock.ANY, 'redis', True)
@patch('zun.compute.rpcapi.API.image_search')
def test_search_image_with_exact_match_false(self, mock_image_search):
mock_image_search.return_value = {'name': 'redis', 'stars': 2000}
response = self.app.get('/v1/images/redis/search?exact_match=false')
self.assertEqual(200, response.status_int)
mock_image_search.assert_called_once_with(
mock.ANY, 'redis', False)
@patch('zun.compute.api.API.image_search')
def test_search_image_with_exact_match_wrong(self, mock_image_search):
mock_image_search.side_effect = exception.InvalidValue
self.assertRaises(AppError, self.app.get,
'/v1/images/redis/search?exact_match=wrong')
self.assertTrue(mock_image_search.not_called)
class TestImageEnforcement(api_base.FunctionalTest):