Browse Source

Fix creating plan with invalid provider and no parameters

When we create a plan with using invalid provider, it return
success if we do not specify parameters. This patch will fix
it.
Closes-Bug: #1829163

Change-Id: I9755949343b73702fd00f5407b5d01b82c4a0963
changes/59/659259/2
jiaopengju 2 years ago
parent
commit
9206e58333
  1. 6
      karbor/api/v1/plans.py
  2. 16
      karbor/tests/unit/api/v1/test_plans.py

6
karbor/api/v1/plans.py

@ -384,9 +384,6 @@ class PlansController(wsgi.Controller):
raise exception.InvalidInput(reason=msg)
def validate_plan_parameters(self, context, plan):
parameters = plan["parameters"]
if not parameters:
return
try:
provider = self.protection_api.show_provider(
context, plan["provider_id"])
@ -398,6 +395,9 @@ class PlansController(wsgi.Controller):
if options_schema is None:
msg = _("The option_schema of plugin must be provided.")
raise exc.HTTPBadRequest(explanation=msg)
parameters = plan["parameters"]
if not parameters:
return
for resource_key, parameter_value in parameters.items():
if "#" in resource_key:
resource_type, resource_id = resource_key.split("#")

16
karbor/tests/unit/api/v1/test_plans.py

@ -106,6 +106,17 @@ class PlanApiTest(base.TestCase):
self.assertRaises(exc.HTTPBadRequest, self.controller.create,
req, body=body)
@mock.patch(
'karbor.services.protection.rpcapi.ProtectionAPI.show_provider')
def test_plan_create_InvalidProvider_and_no_parameters_specify(
self, mock_provider):
plan = self._plan_in_request_body(parameters={})
body = {"plan": plan}
req = fakes.HTTPRequest.blank('/v1/plans')
mock_provider.side_effect = exception.NotFound()
self.assertRaises(exc.HTTPBadRequest, self.controller.create,
req, body=body)
@mock.patch(
'karbor.api.v1.plans.PlansController._plan_get')
@mock.patch(
@ -179,7 +190,10 @@ class PlanApiTest(base.TestCase):
self.controller.index,
req)
def test_plan_create_empty_dict(self):
@mock.patch(
'karbor.services.protection.rpcapi.ProtectionAPI.show_provider')
def test_plan_create_empty_dict(self, mock_provider):
mock_provider.return_value = fakes.PROVIDER_OS
plan = self._plan_in_request_body(parameters={})
body = {"plan": plan}
req = fakes.HTTPRequest.blank('/v1/plans')

Loading…
Cancel
Save