Validate that the specified LB does exist

If we use the specified loadbalancer does exist, we should validate it
in the validate() function. Otherwise, it will attach to a cluster
failed.

To fix this problem,I add some statements in validate() to check the
loadbalancer.

Change-Id: I12ff134da0355701934707d98712bae2281af339
Closes-Bug: 1715070
This commit is contained in:
TingtingYu 2017-09-07 11:18:49 +08:00
parent 921fde986a
commit 6007a2aaed
2 changed files with 26 additions and 0 deletions

View File

@ -316,6 +316,16 @@ class LoadBalancingPolicy(base.Policy):
) % {'key': self.VIP_SUBNET, 'value': name_or_id}
raise exc.InvalidSpec(message=msg)
# validate loadbalancer
if self.lb:
name_or_id = self.lb
try:
nc.loadbalancer_get(name_or_id)
except exc.InternalError:
msg = _("The specified %(key)s '%(value)s' could not be found."
) % {'key': self.LOADBALANCER, 'value': name_or_id}
raise exc.InvalidSpec(message=msg)
def attach(self, cluster, enabled=True):
"""Routine to be invoked when policy is to be attached to a cluster.

View File

@ -207,6 +207,22 @@ class TestLoadBalancingPolicy(base.SenlinTestCase):
self.assertEqual("The specified subnet 'external-subnet' could not "
"be found.", six.text_type(ex))
@mock.patch.object(policy_base.Policy, 'validate')
def test_validate_loadbalancer_notfund(self, mock_validate):
self.spec['properties']['loadbalancer'] = "LB_ID"
policy = lb_policy.LoadBalancingPolicy('test-policy', self.spec)
policy._networkclient = self.net_driver
ctx = mock.Mock(user='user1', project='project1')
self.net_driver.loadbalancer_get = mock.Mock(
side_effect=exc.InternalError(code='404', message='not found'))
ex = self.assertRaises(exc.InvalidSpec, policy.validate, ctx, True)
mock_validate.assert_called_with(ctx, True)
self.net_driver.loadbalancer_get.assert_called_once_with('LB_ID')
self.assertEqual("The specified loadbalancer 'LB_ID' could not "
"be found.", six.text_type(ex))
@mock.patch.object(lb_policy.LoadBalancingPolicy, '_build_policy_data')
@mock.patch.object(policy_base.Policy, 'attach')
@mock.patch.object(no.Node, 'update')