Merged
【26fa9037a485ed0829baf6961f7990bc】fix: 修复未注册器件类型导致的一组对象添加失败问题 #5
lishengxiaoAdd on Sep 16
【26fa9037a485ed0829baf6961f7990bc】fix: 修复未注册器件类型导致的一组对象添加失败问题 #5
Merged
Total: 11 files changed+200-13
| @@ -1,3 +1,4 @@ | |||
| 1 | **/conanbase.py | 1 | **/conanbase.py |
| 2 | test/.dbus | 2 | test/.dbus |
| 3 | test/.test_temp_data/ | 3 | test/.test_temp_data/ |
| 4 | +/temp/ | ||
| @@ -1,3 +1,6 @@ | |||
| 1 | +--version: 1.130.23 2026.9.16 | ||
| 2 | +* 修复未注册器件类型导致的对象添加失败问题,补充对应UT与IT用例 | ||
| 3 | + | ||
| 1 | --version: 1.130.22 2026.9.12 | 4 | --version: 1.130.22 2026.9.12 |
| 2 | * 更新版本号 | 5 | * 更新版本号 |
| 3 | 6 | ||
| @@ -2,7 +2,7 @@ | |||
| 2 | "name": "hwproxy", | 2 | "name": "hwproxy", |
| 3 | "type": "application", | 3 | "type": "application", |
| 4 | "deployConfig": "framework.service", | 4 | "deployConfig": "framework.service", |
| 5 | - "version": "1.130.22", | 5 | + "version": "1.130.23", |
| 6 | "author": "Huawei", | 6 | "author": "Huawei", |
| 7 | "license": "Mulan PSL v2", | 7 | "license": "Mulan PSL v2", |
| 8 | "description": "hardware proxy component", | 8 | "description": "hardware proxy component", |
| @@ -278,8 +278,13 @@ function dispatch:generate_topo_in_order(service, buses, chip_objs, mux_data, to | |||
| 278 | return | 278 | return |
| 279 | end | 279 | end |
| 280 | 280 | ||
| 281 | - -- 获取前级器件类型 | 281 | + -- 获取前级器件类型,前级器件类型未注册时无法确定其下级处理方式,直接跳过 |
| 282 | - local pre_type = object_defs[get_class_type(pre_device)].type | 282 | + local pre_def = object_defs[get_class_type(pre_device)] |
| 283 | + if not pre_def then | ||
| 284 | + log:error('unknown pre_device class, pre_device: %s, position: %s', pre_device, position) | ||
| 285 | + return | ||
| 286 | + end | ||
| 287 | + local pre_type = pre_def.type | ||
| 283 | if self.bus_object_name then | 288 | if self.bus_object_name then |
| 284 | -- 总线是否与当前work服务的bus匹配 | 289 | -- 总线是否与当前work服务的bus匹配 |
| 285 | if pre_type == 'Bus' and self.bus_object_name ~= pre_device then | 290 | if pre_type == 'Bus' and self.bus_object_name ~= pre_device then |
| @@ -348,7 +353,13 @@ function dispatch:remove_topo_in_order(chips, topo_data, pre_device) | |||
| 348 | return | 353 | return |
| 349 | end | 354 | end |
| 350 | 355 | ||
| 351 | - local pre_type = object_defs[get_class_type(pre_device)].type | 356 | + -- 前级器件类型未注册时无法确定其下级处理方式,直接跳过 |
| 357 | + local pre_def = object_defs[get_class_type(pre_device)] | ||
| 358 | + if not pre_def then | ||
| 359 | + log:error('unknown pre_device class, pre_device: %s', pre_device) | ||
| 360 | + return | ||
| 361 | + end | ||
| 362 | + local pre_type = pre_def.type | ||
| 352 | if self.bus_object_name then | 363 | if self.bus_object_name then |
| 353 | -- 总线是否与当前work服务的bus匹配 | 364 | -- 总线是否与当前work服务的bus匹配 |
| 354 | if pre_type == 'Bus' and self.bus_object_name ~= pre_device then | 365 | if pre_type == 'Bus' and self.bus_object_name ~= pre_device then |
| @@ -74,10 +74,8 @@ function hwproxy_app:init() | |||
| 74 | end | 74 | end |
| 75 | 75 | ||
| 76 | local function is_support_chip_model(class_name, object) | 76 | local function is_support_chip_model(class_name, object) |
| 77 | - if | 77 | + local def = object_defs[class_name] |
| 78 | - not object_defs[class_name].support_chip_module | 78 | + if not def or not def.support_chip_module or def.support_chip_module[object.ChipModel] then |
| 79 | - or object_defs[class_name].support_chip_module[object.ChipModel] | ||
| 80 | - then | ||
| 81 | return true | 79 | return true |
| 82 | end | 80 | end |
| 83 | 81 | ||
| @@ -86,7 +84,8 @@ end | |||
| 86 | 84 | ||
| 87 | function hwproxy_app:object_mgmt_call_back_register() | 85 | function hwproxy_app:object_mgmt_call_back_register() |
| 88 | object_manage.on_add_object(self.bus, function(class_name, object, position) | 86 | object_manage.on_add_object(self.bus, function(class_name, object, position) |
| 89 | - if not object_defs[class_name].add_call_back then | 87 | + local def = object_defs[class_name] |
| 88 | + if not def or not def.add_call_back then | ||
| 90 | log:mcf_error('Not support object, class name: %s, object name: %s', class_name, object.name) | 89 | log:mcf_error('Not support object, class name: %s, object name: %s', class_name, object.name) |
| 91 | return | 90 | return |
| 92 | end | 91 | end |
| @@ -94,7 +93,7 @@ function hwproxy_app:object_mgmt_call_back_register() | |||
| 94 | log:error('Not support chip model, object name: %s, chip model: %s', object.name, object.ChipModel) | 93 | log:error('Not support chip model, object name: %s, chip model: %s', object.name, object.ChipModel) |
| 95 | return | 94 | return |
| 96 | end | 95 | end |
| 97 | - local add_call_back = object_defs[class_name].add_call_back | 96 | + local add_call_back = def.add_call_back |
| 98 | self.hwproxy_objects[add_call_back](self.hwproxy_objects, class_name, object, position) | 97 | self.hwproxy_objects[add_call_back](self.hwproxy_objects, class_name, object, position) |
| 99 | end, function() | 98 | end, function() |
| 100 | -- 取消自动上树 | 99 | -- 取消自动上树 |
| @@ -12,13 +12,17 @@ local class = require('mc.class') | |||
| 12 | local stream = require('stream.stream') | 12 | local stream = require('stream.stream') |
| 13 | local object_defs = require('object_defs') | 13 | local object_defs = require('object_defs') |
| 14 | 14 | ||
| 15 | +local DEFAULT_SCAN_INTERVAL <const> = 200 | ||
| 16 | + | ||
| 15 | local bus = class() | 17 | local bus = class() |
| 16 | 18 | ||
| 17 | function bus:ctor(object) | 19 | function bus:ctor(object) |
| 18 | self.name = object.name | 20 | self.name = object.name |
| 19 | self.stream = (stream[object.class_name] or stream.None).new(object, object.name) | 21 | self.stream = (stream[object.class_name] or stream.None).new(object, object.name) |
| 20 | - self.need_scan = object_defs[object.class_name].need_scan or false | 22 | + -- 未注册的器件类型按不需要扫描处理,避免object_defs查表失败 |
| 21 | - self.scan_interval = object_defs[object.class_name].scan_interval or 200 | 23 | + local obj_def = object_defs[object.class_name] or {} |
| 24 | + self.need_scan = obj_def.need_scan or false | ||
| 25 | + self.scan_interval = obj_def.scan_interval or DEFAULT_SCAN_INTERVAL | ||
| 22 | end | 26 | end |
| 23 | 27 | ||
| 24 | function bus:set_high_task_cb(high_task_cb) | 28 | function bus:set_high_task_cb(high_task_cb) |
| @@ -54,7 +54,8 @@ | |||
| 54 | "Eeprom_EXU", | 54 | "Eeprom_EXU", |
| 55 | "Pca9555_IT", | 55 | "Pca9555_IT", |
| 56 | "Ina_238_1", | 56 | "Ina_238_1", |
| 57 | - "Chip_same" | 57 | + "Chip_same", |
| 58 | + "Foo_1" | ||
| 58 | ] | 59 | ] |
| 59 | }, | 60 | }, |
| 60 | "I2c_7": { | 61 | "I2c_7": { |
| @@ -327,6 +328,13 @@ | |||
| 327 | "OffsetWidth": 2, | 328 | "OffsetWidth": 2, |
| 328 | "AddrWidth": 1 | 329 | "AddrWidth": 1 |
| 329 | }, | 330 | }, |
| 331 | + "Foo_1": { | ||
| 332 | + "Address": 80, | ||
| 333 | + "OffsetWidth": 1, | ||
| 334 | + "AddrWidth": 1, | ||
| 335 | + "WriteTmout": 100, | ||
| 336 | + "ReadTmout": 100 | ||
| 337 | + }, | ||
| 330 | "Eeprom_1": { | 338 | "Eeprom_1": { |
| 331 | "Address": 174, | 339 | "Address": 174, |
| 332 | "OffsetWidth": 2, | 340 | "OffsetWidth": 2, |
| @@ -729,6 +729,29 @@ local function test_add_delete_card(bus) | |||
| 729 | wait_value_equal(smc_scanner_obj, 'Value', 1, 3000) | 729 | wait_value_equal(smc_scanner_obj, 'Value', 1, 3000) |
| 730 | end | 730 | end |
| 731 | 731 | ||
| 732 | +-- SR中配置了未注册到object_defs.lua的器件类型(Foo_1)时: | ||
| 733 | +-- 1. 未注册类型不应导致该组对象添加失败(原实现会命中 object_defs[类名] 取字段报错,中断对象添加流程) | ||
| 734 | +-- 2. 未注册类型的器件不应出现在资源树/拓扑中 | ||
| 735 | +-- 3. 同一条链路上的正常器件仍可正常访问(证明该组对象添加成功、主服务与工作服务工作正常) | ||
| 736 | +-- 测试数据来源:test/integration/test_data/sr/14100513_EXU_01.sr 的 I2c_2 总线下的 Foo_1, | ||
| 737 | +-- 该器件的类名 Foo 未在 src/lualib/object_defs.lua 中注册。 | ||
| 738 | +local function test_unknown_class_device_in_topology(bus) | ||
| 739 | + -- 未注册类型的器件不应出现在资源树中 | ||
| 740 | + local ok = pcall(mdb.get_object, bus, '/bmc/kepler/Chip/Foo/Foo_1_0101', 'bmc.kepler.Chip') | ||
| 741 | + lu.assertIsFalse(ok) | ||
| 742 | + | ||
| 743 | + -- 同一板卡上的正常器件仍可访问 | ||
| 744 | + local eeprom_obj = get_object(bus, '/bmc/kepler/Chip/Eeprom/Eeprom_1_0101', 'bmc.kepler.Chip.BlockIO') | ||
| 745 | + local buffer = eeprom_obj:Read(context.new(), 0, 4) | ||
| 746 | + lu.assertNotIsNil(buffer) | ||
| 747 | + | ||
| 748 | + -- 等待若干个心跳周期后再次访问:若未知器件类型导致该组对象添加失败、工作服务处理异常,这里会失败 | ||
| 749 | + skynet.sleep(PERF_DELAY * 5) | ||
| 750 | + eeprom_obj = get_object(bus, '/bmc/kepler/Chip/Eeprom/Eeprom_1_0101', 'bmc.kepler.Chip.BlockIO') | ||
| 751 | + buffer = eeprom_obj:Read(context.new(), 0, 4) | ||
| 752 | + lu.assertNotIsNil(buffer) | ||
| 753 | +end | ||
| 754 | + | ||
| 732 | local function test_add_delete_card_frequent(bus) | 755 | local function test_add_delete_card_frequent(bus) |
| 733 | -- 板卡频繁加载与卸载测试 | 756 | -- 板卡频繁加载与卸载测试 |
| 734 | for i = 1, 5 do | 757 | for i = 1, 5 do |
| @@ -1471,6 +1494,7 @@ local function test_hwproxy(bus) | |||
| 1471 | wrap(test_chip_block_write_read_by_protocol, 'test_chip_block_write_read_by_protocol', bus) | 1494 | wrap(test_chip_block_write_read_by_protocol, 'test_chip_block_write_read_by_protocol', bus) |
| 1472 | wrap(test_chip_error_with_retry_times, 'test_chip_error_with_retry_times', bus) | 1495 | wrap(test_chip_error_with_retry_times, 'test_chip_error_with_retry_times', bus) |
| 1473 | wrap(test_add_delete_card, 'test_add_delete_card', bus) | 1496 | wrap(test_add_delete_card, 'test_add_delete_card', bus) |
| 1497 | + wrap(test_unknown_class_device_in_topology, 'test_unknown_class_device_in_topology', bus) | ||
| 1474 | wrap(test_add_delete_card_frequent, 'test_add_delete_card_frequent', bus) | 1498 | wrap(test_add_delete_card_frequent, 'test_add_delete_card_frequent', bus) |
| 1475 | wrap(test_chip_combo_write_read, 'test_chip_combo_write_read', bus) | 1499 | wrap(test_chip_combo_write_read, 'test_chip_combo_write_read', bus) |
| 1476 | wrap(test_chip_stop_and_restart_accesss, 'test_chip_stop_and_restart_accesss', bus) | 1500 | wrap(test_chip_stop_and_restart_accesss, 'test_chip_stop_and_restart_accesss', bus) |
| @@ -151,3 +151,90 @@ function TestDispatch:test_predevice_bus_handle() | |||
| 151 | lu.assertIsTrue(ret) | 151 | lu.assertIsTrue(ret) |
| 152 | lu.assertNotIsNil(service.chips[chip_name].aggregate_sections) | 152 | lu.assertNotIsNil(service.chips[chip_name].aggregate_sections) |
| 153 | end | 153 | end |
| 154 | + | ||
| 155 | +-- 前级器件类型未在object_defs注册时,generate_topo_in_order应跳过该链路而不是报错中断 | ||
| 156 | +function TestDispatch:test_generate_topo_unknown_pre_device_class() | ||
| 157 | + local service = { new_bus = function() end, chips = {}, chip_topo = {} } | ||
| 158 | + local topo_data = { ['Foo_1'] = { [=[Eeprom_1]=] }, Anchor = { [=[Foo_1]=] } } | ||
| 159 | + | ||
| 160 | + local ok, err = pcall( | ||
| 161 | + dispatch_instance_main.generate_topo_in_order, | ||
| 162 | + dispatch_instance_main, | ||
| 163 | + service, | ||
| 164 | + {}, | ||
| 165 | + {}, | ||
| 166 | + {}, | ||
| 167 | + topo_data, | ||
| 168 | + 'Foo_1', | ||
| 169 | + '01' | ||
| 170 | + ) | ||
| 171 | + | ||
| 172 | + lu.assertIsTrue(ok, '未注册的前级器件类型不应导致拓扑生成报错, err: ' .. tostring(err)) | ||
| 173 | + lu.assertIsNil(service.chips['Eeprom_1'], '未知前级链路不应创建chip') | ||
| 174 | +end | ||
| 175 | + | ||
| 176 | +-- 前级器件名不含下划线时get_class_type返回nil,同样不应报错中断 | ||
| 177 | +function TestDispatch:test_generate_topo_pre_device_without_underscore() | ||
| 178 | + local service = { new_bus = function() end, chips = {}, chip_topo = {} } | ||
| 179 | + local topo_data = { ['VirtualChip'] = { [=[Eeprom_1]=] }, Anchor = { [=[VirtualChip]=] } } | ||
| 180 | + | ||
| 181 | + local ok, err = pcall( | ||
| 182 | + dispatch_instance_main.generate_topo_in_order, | ||
| 183 | + dispatch_instance_main, | ||
| 184 | + service, | ||
| 185 | + {}, | ||
| 186 | + {}, | ||
| 187 | + {}, | ||
| 188 | + topo_data, | ||
| 189 | + 'VirtualChip', | ||
| 190 | + '01' | ||
| 191 | + ) | ||
| 192 | + | ||
| 193 | + lu.assertIsTrue(ok, '前级器件名不含下划线时不应报错中断, err: ' .. tostring(err)) | ||
| 194 | +end | ||
| 195 | + | ||
| 196 | +-- 删除链路时前级器件类型未注册,remove_topo_in_order应跳过而不是报错中断 | ||
| 197 | +function TestDispatch:test_remove_topo_unknown_pre_device_class() | ||
| 198 | + local topo_data = { ['Foo_1'] = { [=[Eeprom_1]=] }, Anchor = { [=[Foo_1]=] } } | ||
| 199 | + | ||
| 200 | + local ok, err = pcall(dispatch_instance_main.remove_topo_in_order, dispatch_instance_main, {}, topo_data, 'Foo_1') | ||
| 201 | + | ||
| 202 | + lu.assertIsTrue(ok, '删除未注册前级器件链路不应报错中断, err: ' .. tostring(err)) | ||
| 203 | +end | ||
| 204 | + | ||
| 205 | +-- 正常总线链路行为保持不变(回归保护) | ||
| 206 | +function TestDispatch:test_generate_topo_normal_bus_still_creates_chip() | ||
| 207 | + local added_chips = {} | ||
| 208 | + -- 主进程实例的bus_object_name为nil,不进行总线过滤 | ||
| 209 | + local dispatch_instance = dispatch.new(true, nil) | ||
| 210 | + local service = { new_bus = function() end, chips = {}, chip_topo = {} } | ||
| 211 | + local buses = { I2c_1 = { name = 'I2c_1', need_scan = false } } | ||
| 212 | + local chip_objs = { | ||
| 213 | + Eeprom_1 = { name = 'Eeprom_1', class_name = 'Eeprom', Address = 0xae }, | ||
| 214 | + } | ||
| 215 | + local topo_data = { ['I2c_1'] = { [=[Eeprom_1]=] }, Anchor = { [=[I2c_1]=] } } | ||
| 216 | + dispatch_instance.scans = { | ||
| 217 | + I2c_1 = { | ||
| 218 | + add_chip = function(_, chip_obj) | ||
| 219 | + added_chips[#added_chips + 1] = chip_obj.name | ||
| 220 | + end, | ||
| 221 | + }, | ||
| 222 | + } | ||
| 223 | + | ||
| 224 | + local ok, err = pcall( | ||
| 225 | + dispatch_instance.generate_topo_in_order, | ||
| 226 | + dispatch_instance, | ||
| 227 | + service, | ||
| 228 | + buses, | ||
| 229 | + chip_objs, | ||
| 230 | + {}, | ||
| 231 | + topo_data, | ||
| 232 | + 'I2c_1', | ||
| 233 | + '01' | ||
| 234 | + ) | ||
| 235 | + | ||
| 236 | + lu.assertIsTrue(ok, tostring(err)) | ||
| 237 | + lu.assertEquals(#added_chips, 1, '正常总线链路应创建1个chip') | ||
| 238 | + lu.assertEquals(added_chips[1], 'Eeprom_1') | ||
| 239 | + lu.assertNotIsNil(service.chips['Eeprom_1']) | ||
| 240 | +end | ||
| @@ -8,6 +8,7 @@ | |||
| 8 | -- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | 8 | -- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. |
| 9 | -- See the Mulan PSL v2 for more details. | 9 | -- See the Mulan PSL v2 for more details. |
| 10 | 10 | ||
| 11 | +local lu = require('luaunit') | ||
| 11 | local hwproxy_app = require('hwproxy_app') | 12 | local hwproxy_app = require('hwproxy_app') |
| 12 | local log = require('mc.logging') | 13 | local log = require('mc.logging') |
| 13 | 14 | ||
| @@ -47,3 +48,32 @@ function TestHwproxyapp:test_trace_chip() | |||
| 47 | input.funcname = 'write' | 48 | input.funcname = 'write' |
| 48 | hwproxy_app:trace_chip(input, nil) | 49 | hwproxy_app:trace_chip(input, nil) |
| 49 | end | 50 | end |
| 51 | + | ||
| 52 | +-- AddObject回调收到未注册的器件类型时应只记录日志并跳过,不中断该组对象添加流程 | ||
| 53 | +function TestHwproxyapp:test_object_mgmt_call_back_unknown_class() | ||
| 54 | + local object_manage = require('mc.mdb.object_manage') | ||
| 55 | + local old_on_add_object = object_manage.on_add_object | ||
| 56 | + local captured | ||
| 57 | + object_manage.on_add_object = function(_, cb) | ||
| 58 | + captured = cb | ||
| 59 | + end | ||
| 60 | + | ||
| 61 | + local handled = false | ||
| 62 | + local service = { | ||
| 63 | + bus = {}, | ||
| 64 | + hwproxy_objects = { | ||
| 65 | + chip_object_handle = function() | ||
| 66 | + handled = true | ||
| 67 | + end, | ||
| 68 | + }, | ||
| 69 | + } | ||
| 70 | + hwproxy_app.object_mgmt_call_back_register(service) | ||
| 71 | + object_manage.on_add_object = old_on_add_object | ||
| 72 | + | ||
| 73 | + lu.assertNotIsNil(captured, 'AddObject回调应已注册') | ||
| 74 | + | ||
| 75 | + local ok, err = pcall(captured, 'NoSuchClass', { name = 'NoSuchClass_1' }, '01') | ||
| 76 | + | ||
| 77 | + lu.assertIsTrue(ok, '未注册器件类型不应导致回调报错, err: ' .. tostring(err)) | ||
| 78 | + lu.assertIsFalse(handled, '未注册器件类型不应被分发处理') | ||
| 79 | +end | ||
| @@ -53,4 +53,24 @@ function TestWorkBus:test_notify_secondary_task_without_callback() | |||
| 53 | lu.assertEquals(ok, true) | 53 | lu.assertEquals(ok, true) |
| 54 | end | 54 | end |
| 55 | 55 | ||
| 56 | +-- 器件类型未在object_defs注册时,构造bus不应报错,按不扫描处理 | ||
| 57 | +function TestWorkBus:test_unknown_class_name_no_error() | ||
| 58 | + local ok, err = pcall(bus.new, { | ||
| 59 | + name = 'test_bus', | ||
| 60 | + class_name = 'UnknownBusWithoutDef', | ||
| 61 | + }) | ||
| 62 | + | ||
| 63 | + lu.assertIsTrue(ok, '未注册的器件类型不应导致构造报错, err: ' .. tostring(err)) | ||
| 64 | +end | ||
| 65 | + | ||
| 66 | +function TestWorkBus:test_unknown_class_name_uses_default_scan_config() | ||
| 67 | + local test_bus = bus.new({ | ||
| 68 | + name = 'test_bus', | ||
| 69 | + class_name = 'UnknownBusWithoutDef', | ||
| 70 | + }) | ||
| 71 | + | ||
| 72 | + lu.assertEquals(test_bus.need_scan, false, '未注册类型默认不扫描') | ||
| 73 | + lu.assertEquals(test_bus.scan_interval, 200, '未注册类型使用默认扫描周期') | ||
| 74 | +end | ||
| 75 | + | ||
| 56 | return TestWorkBus | 76 | return TestWorkBus |