Fix is_using_neutron

Recently we moved to use SessionClient with
the client plugins and it seems, just
creating the client does not ensure that the
service is available. We should check for
the endpoint.

Change-Id: Iae9fa8758314a4b04224127a181eb1054aa72d8a
Closes-Bug: #1606951
This commit is contained in:
Rabi Mihsra 2016-07-27 16:59:59 +03:00 committed by Rabi Mishra
parent cd125f328e
commit 74815dafd9
6 changed files with 25 additions and 7 deletions

View File

@ -2154,8 +2154,8 @@ class Resource(object):
def is_using_neutron(self):
try:
self.client('neutron')
if not self.client('neutron').get_endpoint():
return False
except Exception:
return False
else:
return True
return True

View File

@ -15,7 +15,6 @@ import collections
import copy
import mock
from keystoneclient import exceptions as keystone_exc
from neutronclient.common import exceptions as neutron_exc
from neutronclient.v2_0 import client as neutronclient
from novaclient.v2 import security_group_rules as nova_sgr
@ -25,6 +24,7 @@ from heat.common import exception
from heat.common import short_id
from heat.common import template_format
from heat.engine.clients.os import nova
from heat.engine import resource
from heat.engine.resources.aws.ec2 import security_group
from heat.engine import rsrc_defn
from heat.engine import scheduler
@ -149,11 +149,12 @@ Resources:
neutronclient.Client, 'delete_security_group_rule')
self.m.StubOutWithMock(neutronclient.Client, 'delete_security_group')
self.m.StubOutWithMock(neutronclient.Client, 'update_security_group')
self.patchobject(resource.Resource, 'is_using_neutron',
return_value=True)
def mock_no_neutron(self):
mock_create = self.patch(
'heat.engine.clients.os.neutron.NeutronClientPlugin._create')
mock_create.side_effect = keystone_exc.EndpointNotFound()
self.patchobject(resource.Resource, 'is_using_neutron',
return_value=False)
def create_stack(self, templ):
self.stack = self.parse_stack(template_format.parse(templ))

View File

@ -193,6 +193,8 @@ class ServersTest(common.HeatTestCase):
self.limits.absolute = self._limits_absolute()
self.mock_flavor = mock.Mock(ram=4, disk=4)
self.mock_image = mock.Mock(min_ram=1, min_disk=1, status='active')
self.patchobject(resource.Resource, 'is_using_neutron',
return_value=True)
def flavor_side_effect(*args):
return 2 if args[0] == 'm1.small' else 1
@ -3710,6 +3712,8 @@ class ServerInternalPortTest(common.HeatTestCase):
'delete_port')
self.port_show = self.patchobject(neutronclient.Client,
'show_port')
self.patchobject(resource.Resource, 'is_using_neutron',
return_value=True)
def flavor_side_effect(*args):
return 2 if args[0] == 'm1.small' else 1

View File

@ -104,6 +104,8 @@ class OSDBInstanceTest(common.HeatTestCase):
self.nova = self.m.CreateMockAnything()
self.m.StubOutWithMock(trove.TroveClientPlugin, '_create')
self.stub_TroveFlavorConstraint_validate()
self.patchobject(resource.Resource, 'is_using_neutron',
return_value=True)
def _setup_test_clouddbinstance(self, name, t):
stack_name = '%s_stack' % name

View File

@ -1497,6 +1497,14 @@ class ResourceTest(common.HeatTestCase):
mock_create.side_effect = Exception()
self.assertFalse(res.is_using_neutron())
client = mock.Mock()
self.patchobject(client, 'get_endpoint',
return_value=None)
self.patch(
'heat.engine.clients.os.neutron.NeutronClientPlugin._create',
return_value=client)
self.assertFalse(res.is_using_neutron())
def _test_skip_validation_if_custom_constraint(self, tmpl):
stack = parser.Stack(utils.dummy_context(), 'test', tmpl)
stack.store()

View File

@ -15,6 +15,7 @@ import uuid
from heat.common import exception
from heat.common import template_format
from heat.engine import resource
from heat.engine.resources.aws.ec2 import subnet as sn
from heat.engine import scheduler
from heat.engine import stack as parser
@ -581,6 +582,8 @@ Resources:
def mock_create_network_interface(
self, security_groups=['0389f747-7785-4757-b7bb-2ab07e4b09c3']):
self.patchobject(resource.Resource, 'is_using_neutron',
return_value=True)
self.nic_name = utils.PhysName('test_stack', 'the_nic')
port = {'network_id': 'aaaa',
'fixed_ips': [{