Fix bug: report exist trait exception

check the trait first before create trait to placement

Closes-Bug: #2029890
Change-Id: I37cddd13c12536bc20bd6039127d348e6f9949da
This commit is contained in:
songwenping 2023-08-04 18:21:07 +08:00 committed by sonwenping
parent 57fa0d442d
commit 867931fb83
2 changed files with 15 additions and 8 deletions

View File

@ -36,7 +36,7 @@ class PlacementClient(object):
def get(self, url, version=None, global_request_id=None): def get(self, url, version=None, global_request_id=None):
res = self._client.get(url, microversion=version, res = self._client.get(url, microversion=version,
global_request_id=global_request_id) global_request_id=global_request_id)
if res.status_code >= 500: if res and res.status_code >= 500:
raise exception.PlacementServerError( raise exception.PlacementServerError(
"Placement Server has some error at this time.") "Placement Server has some error at this time.")
LOG.debug('Successfully get resources from placement: %s', url) LOG.debug('Successfully get resources from placement: %s', url)
@ -85,16 +85,19 @@ class PlacementClient(object):
def _ensure_traits(self, trait_names): def _ensure_traits(self, trait_names):
# TODO(Xinran): maintain a reference count of how many RPs use # TODO(Xinran): maintain a reference count of how many RPs use
# this trait and do the deletion only when the last RP is deleted. # this trait and do the deletion only when the last RP is deleted.
for trait in trait_names: for trait_name in trait_names:
resp = self.put("/traits/%s" % trait, None, version='1.6') trait = self.get("/traits/%s" % trait_name, version='1.6')
if trait:
LOG.info("Trait %(trait)s already existed",
{"trait": trait_name})
continue
resp = self.put("/traits/%s" % trait_name, None, version='1.6')
if resp.status_code == 201: if resp.status_code == 201:
LOG.info("Created trait %(trait)s", {"trait": trait}) LOG.info("Created trait %(trait)s", {"trait": trait_name})
elif resp.status_code == 204:
LOG.info("Trait %(trait)s already existed", {"trait": trait})
else: else:
raise Exception( raise Exception(
"Failed to create trait %s: HTTP %d: %s" % "Failed to create trait %s: HTTP %d: %s" %
(trait, resp.status_code, resp.text)) (trait_name, resp.status_code, resp.text))
def _put_rp_traits(self, rp_uuid, traits_json): def _put_rp_traits(self, rp_uuid, traits_json):
generation = self.get_resource_provider( generation = self.get_resource_provider(

View File

@ -106,13 +106,17 @@ class PlacementAPITest(base.TestCase):
def test_ensure_traits(self): def test_ensure_traits(self):
self.mock_sdk.put.return_value = mock.Mock(status_code=201) self.mock_sdk.put.return_value = mock.Mock(status_code=201)
placement = placement_client.PlacementClient() placement = placement_client.PlacementClient()
self.mock_sdk.get.return_value = None
placement._ensure_traits([mock.ANY]) placement._ensure_traits([mock.ANY])
self.assertEqual(2, self.mock_log_debug.call_count)
msg = 'Successfully update resources from placement: %s' msg = 'Successfully update resources from placement: %s'
self.mock_log_debug.assert_called_once_with(msg, mock.ANY) # first call/arg
self.assertIn(msg, self.mock_log_debug.call_args_list[1][0][0])
def test_ensure_traits_exception(self): def test_ensure_traits_exception(self):
placement = placement_client.PlacementClient() placement = placement_client.PlacementClient()
mock_ret = mock.Mock(status_code=500) mock_ret = mock.Mock(status_code=500)
self.mock_sdk.get.return_value = None
self.mock_sdk.put.return_value = mock_ret self.mock_sdk.put.return_value = mock_ret
self.assertRaises(exception.PlacementServerError, self.assertRaises(exception.PlacementServerError,
placement._ensure_traits, [mock.ANY]) placement._ensure_traits, [mock.ANY])