From 3eb967784bbe1c1668d22fdb8cee65dc2417103c Mon Sep 17 00:00:00 2001 From: EdLeafe Date: Fri, 3 Feb 2017 15:32:55 +0000 Subject: [PATCH] Add check for invalid inventory amounts This patch adds sane minimum and maximums to the fields for an inventory posting, which will quickly return a 400 error if invalid values are passed instead of proceeding, only to fail at the DB layer. Conflicts: nova/tests/functional/api/openstack/placement/gabbits/inventory.yaml Partial-Bug: #1673227 Change-Id: I6296cee6b8a4be1dd53c52f6290ebda926cf6465 (cherry picked from commit 0b2d7c4d028104d3a2a0d5851b194eface707ec3) --- .../openstack/placement/handlers/inventory.py | 19 +++-- .../placement/gabbits/inventory.yaml | 84 ++++++++++++++++++- 2 files changed, 94 insertions(+), 9 deletions(-) diff --git a/nova/api/openstack/placement/handlers/inventory.py b/nova/api/openstack/placement/handlers/inventory.py index cf1fa1b7c20c..0995f29f3515 100644 --- a/nova/api/openstack/placement/handlers/inventory.py +++ b/nova/api/openstack/placement/handlers/inventory.py @@ -33,23 +33,28 @@ BASE_INVENTORY_SCHEMA = { }, "total": { "type": "integer", - "maximum": db.MAX_INT + "maximum": db.MAX_INT, + "minimum": 1, }, "reserved": { "type": "integer", - "maximum": db.MAX_INT + "maximum": db.MAX_INT, + "minimum": 0, }, "min_unit": { "type": "integer", - "maximum": db.MAX_INT + "maximum": db.MAX_INT, + "minimum": 1 }, "max_unit": { "type": "integer", - "maximum": db.MAX_INT + "maximum": db.MAX_INT, + "minimum": 1 }, "step_size": { "type": "integer", - "maximum": db.MAX_INT + "maximum": db.MAX_INT, + "minimum": 1 }, "allocation_ratio": { "type": "number", @@ -104,8 +109,8 @@ OUTPUT_INVENTORY_FIELDS = [ ] INVENTORY_DEFAULTS = { 'reserved': 0, - 'min_unit': 0, - 'max_unit': 0, + 'min_unit': 1, + 'max_unit': db.MAX_INT, 'step_size': 1, 'allocation_ratio': 1.0 } diff --git a/nova/tests/functional/api/openstack/placement/gabbits/inventory.yaml b/nova/tests/functional/api/openstack/placement/gabbits/inventory.yaml index f7ecbe8be3d4..0bcb2257272c 100644 --- a/nova/tests/functional/api/openstack/placement/gabbits/inventory.yaml +++ b/nova/tests/functional/api/openstack/placement/gabbits/inventory.yaml @@ -41,7 +41,18 @@ tests: response_strings: - Unable to create inventory for resource provider -- name: post an invalid inventory +- name: post an inventory with no total specified + POST: /resource_providers/$ENVIRON['RP_UUID']/inventories + request_headers: + content-type: application/json + data: + resource_class: DISK_GB + status: 400 + response_strings: + - JSON does not validate + - "'total' is a required property" + +- name: post a negative inventory POST: /resource_providers/$ENVIRON['RP_UUID']/inventories request_headers: content-type: application/json @@ -50,7 +61,76 @@ tests: total: -1 status: 400 response_strings: - - Bad inventory DISK_GB for resource provider $ENVIRON['RP_UUID'] + - JSON does not validate + - -1 is less than the minimum of 1 + +- name: post an inventory with invalid total + POST: /resource_providers/$ENVIRON['RP_UUID']/inventories + request_headers: + content-type: application/json + data: + resource_class: DISK_GB + total: 0 + reserved: 512 + min_unit: 1 + max_unit: 1024 + step_size: 10 + allocation_ratio: 1.0 + status: 400 + response_strings: + - "JSON does not validate: 0 is less than the minimum of 1" + - "Failed validating 'minimum' in schema['properties']['total']" + +- name: post an inventory invalid min_unit + POST: /resource_providers/$ENVIRON['RP_UUID']/inventories + request_headers: + content-type: application/json + data: + resource_class: DISK_GB + total: 2048 + reserved: 512 + min_unit: 0 + max_unit: 1024 + step_size: 10 + allocation_ratio: 1.0 + status: 400 + response_strings: + - "JSON does not validate: 0 is less than the minimum of 1" + - "Failed validating 'minimum' in schema['properties']['min_unit']" + +- name: post an inventory invalid max_unit + POST: /resource_providers/$ENVIRON['RP_UUID']/inventories + request_headers: + content-type: application/json + data: + resource_class: DISK_GB + total: 2048 + reserved: 512 + min_unit: 10 + max_unit: 0 + step_size: 10 + allocation_ratio: 1.0 + status: 400 + response_strings: + - "JSON does not validate: 0 is less than the minimum of 1" + - "Failed validating 'minimum' in schema['properties']['max_unit']" + +- name: post an inventory invalid step_size + POST: /resource_providers/$ENVIRON['RP_UUID']/inventories + request_headers: + content-type: application/json + data: + resource_class: DISK_GB + total: 2048 + reserved: 512 + min_unit: 10 + max_unit: 1024 + step_size: 0 + allocation_ratio: 1.0 + status: 400 + response_strings: + - "JSON does not validate: 0 is less than the minimum of 1" + - "Failed validating 'minimum' in schema['properties']['step_size']" - name: post an inventory POST: /resource_providers/$ENVIRON['RP_UUID']/inventories