Check realization status when getting id by search

Change-Id: I912b51933f2ad1e9fa2b1750c42301b1a1a70c25
This commit is contained in:
asarfaty 2020-10-13 08:55:17 +02:00 committed by Adit Sarfaty
parent c1d44e0046
commit b408b2a55c
3 changed files with 55 additions and 2 deletions

View File

@ -114,7 +114,9 @@ def get_default_nsxlib_config(allow_passthrough=True):
plugin_ver=PLUGIN_VER,
dns_nameservers=DNS_NAMESERVERS,
dns_domain=DNS_DOMAIN,
allow_passthrough=allow_passthrough
allow_passthrough=allow_passthrough,
realization_max_attempts=3,
realization_wait_sec=0.2
)

View File

@ -3089,6 +3089,30 @@ class TestPolicyTier1(NsxPolicyLibTestCase):
tier1_id, tenant=TEST_TENANT,
max_attempts=5, sleep=0.1)
def test_get_realized_id(self):
# Get realized ID using search
tier1_id = '111'
logical_router_id = 'realized_111'
info = {'results': [{'status': {'state': 'success'},
'id': logical_router_id}]}
with mock.patch.object(self.resourceApi.nsx_api, "search_by_tags",
return_value=info):
realized_id = self.resourceApi.get_realized_id(tier1_id)
self.assertEqual(logical_router_id, realized_id)
def test_get_realized_id_failed(self):
# Get realized ID using search
tier1_id = '111'
logical_router_id = 'realized_111'
info = {'results': [{'status': {'state': 'in_progress'},
'id': logical_router_id}]}
with mock.patch.object(self.resourceApi.nsx_api, "search_by_tags",
return_value=info),\
mock.patch.object(self.resourceApi.policy_api,
"get_realized_entities"):
self.assertRaises(nsxlib_exc.RealizationTimeoutError,
self.resourceApi.get_realized_id, tier1_id)
def test_get_realized_downlink_port(self):
tier1_id = '111'
segment_id = '222'
@ -3510,6 +3534,30 @@ class TestPolicyTier1NoPassthrough(TestPolicyTier1):
self.resourceApi.set_dhcp_relay(tier1_id, segment_id, relay_id)
realization.assert_not_called()
def test_get_realized_id(self):
# Get realized ID using policy api
tier1_id = '111'
logical_router_id = 'realized_111'
result = [{'state': constants.STATE_REALIZED,
'entity_type': 'RealizedLogicalRouter',
'realization_specific_identifier': logical_router_id}]
with mock.patch.object(self.resourceApi.policy_api,
"get_realized_entities",
return_value=result):
realized_id = self.resourceApi.get_realized_id(tier1_id)
self.assertEqual(logical_router_id, realized_id)
def test_get_realized_id_failed(self):
# Get realized ID using policy api
tier1_id = '111'
result = [{'state': constants.STATE_UNREALIZED,
'entity_type': 'RealizedLogicalRouter'}]
with mock.patch.object(self.resourceApi.policy_api,
"get_realized_entities",
return_value=result):
realized_id = self.resourceApi.get_realized_id(tier1_id)
self.assertEqual(None, realized_id)
class TestPolicyTier0NatRule(NsxPolicyLibTestCase):

View File

@ -311,7 +311,10 @@ class NsxPolicyResourceBase(object):
resources = self.nsx_api.search_by_tags(
tags=tag, resource_type=mp_resource_type)['results']
if resources:
return resources[0]['id']
# If status exists, make sure the state is successful
if (not resources[0].get('status') or
resources[0]['status'].get('state') == 'success'):
return resources[0]['id']
# From time to time also check the Policy realization state,
# as if it is in ERROR waiting should be avoided.