Add validation for compose node api

Partially-Implements: blueprint validation
Change-Id: I020114d21e07f0b08c5744d60bcae14669fff692
This commit is contained in:
Anusha Ramineni 2017-04-07 14:04:21 +05:30
parent d0b9f60bba
commit a42b5a688f
5 changed files with 72 additions and 12 deletions

View File

@ -19,6 +19,7 @@ from six.moves import http_client
from valence.common import utils
from valence.controller import nodes
from valence.validation import validator
class Nodes(Resource):
@ -27,6 +28,7 @@ class Nodes(Resource):
return utils.make_response(
http_client.OK, nodes.Node.list_composed_nodes())
@validator.check_input('compose_node_schema')
def post(self):
return utils.make_response(
http_client.OK, nodes.Node.compose_node(request.get_json()))

View File

@ -77,9 +77,6 @@ class Node(object):
"processor": {}
}
if not("name" in request_body and request_body["name"].strip()):
raise exception.BadRequest(
detail="Please specify a name of the node to compose")
name = request_body["name"]
# "description" is optional
description = request_body.get("description", "")

View File

@ -73,14 +73,6 @@ class TestAPINodes(unittest.TestCase):
requirements)
self.assertEqual(expected, result)
def test_compose_node_with_wrong_parameters(self):
"""Test compose node with no name input"""
with self.assertRaises(exception.BadRequest) as context:
nodes.Node.compose_node({"no_name": "fake_value"})
self.assertTrue("Please specify a name of the node to compose"
in context.exception.detail)
@mock.patch("valence.db.api.Connection.create_composed_node")
@mock.patch("valence.common.utils.generate_uuid")
@mock.patch("valence.controller.nodes.Node.list_composed_nodes")

View File

@ -113,3 +113,50 @@ class TestPodmanagerApi(TestApiValidation):
response = json.loads(response.data.decode())
self.assertEqual(400, response['status'])
self.assertEqual('ValidationError', response['code'])
class TestNodeApi(TestApiValidation):
@mock.patch('valence.controller.nodes.Node.compose_node')
def test_compose_request_using_properties(self, mock_compose):
req = {
"name": "test_request",
"properties": {
"memory": {
"capacity_mib": "4000",
"type": "DDR3"
},
"processor": {
"model": "Intel",
"total_cores": "4"
}
}
}
mock_compose.return_value = req
resp = self.app.post('/v1/nodes',
content_type='application/json',
data=json.dumps(req))
self.assertEqual(200, resp.status_code)
@mock.patch('valence.controller.nodes.Node.compose_node')
def test_compose_request_using_flavor(self, mock_compose):
req = {
"name": "test_request1",
"flavor_id": "test_flavor"
}
mock_compose.return_value = req
resp = self.app.post('/v1/nodes',
content_type='application/json',
data=json.dumps(req))
self.assertEqual(200, resp.status_code)
def test_compose_request_invalid_params(self):
req = {
"name": "test_request1",
}
resp = self.app.post('/v1/nodes',
content_type='application/json',
data=json.dumps(req))
response = json.loads(resp.data.decode())
self.assertEqual(400, response['status'])
self.assertEqual('ValidationError', response['code'])

View File

@ -70,5 +70,27 @@ podmanager_schema = {
jsonschema.Draft4Validator.check_schema(podmanager_schema)
compose_node_with_flavor = {
'type': 'object',
'properties': {
'name': {'type': 'string'},
'description': {'type': 'string'},
'flavor_id': {'type': 'string'}
},
'required': ['name', 'flavor_id'],
'additionalProperties': False,
}
compose_node_schema = {
'anyOf': [
compose_node_with_flavor,
flavor_schema,
]
}
jsonschema.Draft4Validator.check_schema(compose_node_schema)
SCHEMAS = {'flavor_schema': flavor_schema,
'podmanager_schema': podmanager_schema, }
'podmanager_schema': podmanager_schema,
'compose_node_schema': compose_node_schema, }