Refactor _gen_resource_inventory

This refactors _gen_resource_inventory as suggested in [1], simplifying
by removing unused fields which duplicated defaults that are set by
placement itself.

This patch introduces the previously absent test suite for conductor
manager.

[1] I0aac84a1fc6b00b5523927bee27f7491daecd786

Change-Id: I79379f55f3ea0690c58a7835d1e8c31117245626
This commit is contained in:
Eric Fried 2019-11-27 13:28:36 -06:00
parent 672aa7eb1e
commit 59b8047ea1
3 changed files with 84 additions and 18 deletions
cyborg
conductor
tests/unit/conductor

@ -251,10 +251,8 @@ class ConductorManager(object):
rp_uuid = self.get_rp_uuid_from_obj(new_driver_dep_obj)
attrs = new_driver_dep_obj.attribute_list
resource_class = [i.value for i in attrs if i.key == 'rc'][0]
inv_data = self._gen_resource_inventory(
resource_class,
total=dep_obj.num_accelerators,
max=dep_obj.num_accelerators)
inv_data = _gen_resource_inventory(
resource_class, dep_obj.num_accelerators)
self.placement_client.update_inventory(rp_uuid, inv_data)
# diff the internal layer: driver_attribute_list
new_attribute_list = []
@ -395,10 +393,7 @@ class ConductorManager(object):
sub_pr_uuid = self._get_sub_provider(
context, parent, name)
result = self._gen_resource_inventory(
resource_class,
total=total,
max=total)
result = _gen_resource_inventory(resource_class, total)
self.placement_client.update_inventory(sub_pr_uuid, result)
# traits = ["CUSTOM_FPGA_INTEL", "CUSTOM_FPGA_INTEL_ARRIA10",
# "CUSTOM_FPGA_INTEL_REGION_UUID",
@ -422,16 +417,6 @@ class ConductorManager(object):
dep_obj["rp_uuid"] = rp_uuid
dep_obj.save(context)
def _gen_resource_inventory(self, name, total=0, max=1, min=1, step=1):
result = {}
result[name] = {
'total': total,
'min_unit': min,
'max_unit': max,
'step_size': step,
}
return result
def get_rp_uuid_from_obj(self, obj):
return str(uuid.uuid3(uuid.NAMESPACE_DNS, six.ensure_str(obj.name)))
@ -445,3 +430,12 @@ class ConductorManager(object):
{"rp_uuid": rp["uuid"]})
if rp["uuid"] == rp_uuid:
break
def _gen_resource_inventory(resource_class, total):
return {
resource_class: {
'total': total,
'max_unit': total,
},
}

@ -0,0 +1,72 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import fixtures
import mock
from cyborg.conductor import manager
from cyborg.tests import base
class ConductorManagerTest(base.TestCase):
def setUp(self):
super(ConductorManagerTest, self).setUp()
self.placement_mock = self.useFixture(fixtures.MockPatch(
'cyborg.common.placement_client.PlacementClient')
).mock.return_value
self.cm = manager.ConductorManager(
mock.sentinel.topic, mock.sentinel.host)
def test__gen_resource_inventory(self):
expected = {
'CUSTOM_FOO': {
'total': 42,
'max_unit': 42,
},
}
actual = manager._gen_resource_inventory('CUSTOM_FOO', 42)
self.assertEqual(expected, actual)
@mock.patch('cyborg.conductor.manager.ConductorManager._get_sub_provider')
def test_provider_report(self, mock_get_sub):
rc = 'CUSTOM_ACCELERATOR'
traits = ["CUSTOM_FPGA_INTEL",
"CUSTOM_FPGA_INTEL_ARRIA10",
"CUSTOM_FPGA_INTEL_REGION_UUID",
"CUSTOM_FPGA_FUNCTION_ID_INTEL_UUID",
"CUSTOM_PROGRAMMABLE",
"CUSTOM_FPGA_NETWORK"]
total = 42
expected_inv = {
rc: {
'total': total,
'max_unit': total,
},
}
# Test this exception path, since it doesn't actually change the flow.
# Use a random exception, because it doesn't matter.
self.placement_mock.ensure_resource_classes.side_effect = ValueError(
'fail')
actual = self.cm.provider_report(
mock.sentinel.context, mock.sentinel.name, rc, traits, total,
mock.sentinel.parent)
self.placement_mock.ensure_resource_classes.assert_called_once_with(
mock.sentinel.context, [rc])
mock_get_sub.assert_called_once_with(
mock.sentinel.context, mock.sentinel.parent, mock.sentinel.name)
sub_pr_uuid = mock_get_sub.return_value
self.placement_mock.update_inventory.assert_called_once_with(
sub_pr_uuid, expected_inv)
self.placement_mock.add_traits_to_rp.assert_called_once_with(
sub_pr_uuid, traits)
self.assertEqual(sub_pr_uuid, actual)