|
| 1 | +############################################################################## |
| 2 | +# |
| 3 | +# Copyright (C) Zenoss, Inc. 2026, all rights reserved. |
| 4 | +# |
| 5 | +# This content is made available according to terms specified in |
| 6 | +# License.zenoss under the directory where your Zenoss product is installed. |
| 7 | +# |
| 8 | +############################################################################## |
| 9 | + |
| 10 | +from __future__ import absolute_import |
| 11 | + |
| 12 | +from unittest import TestCase |
| 13 | + |
| 14 | +from Products.ZenModel.Hardware import Hardware |
| 15 | +from Products.Zing.fact import device_info_fact, MetadataKeys |
| 16 | + |
| 17 | +class DummyDevice(object): |
| 18 | + id = "dummy_device" |
| 19 | + title = "Dummy Device" |
| 20 | + |
| 21 | + def _propertyMap(self): return [] |
| 22 | + def device(self): return None |
| 23 | + def getProductionState(self): return 1000 |
| 24 | + def getPriorityConversions(self): return {} |
| 25 | + def getProdStateConversions(self): return {} |
| 26 | + def convertProdState(self, state): return "Production" |
| 27 | + |
| 28 | + def getOSProductKey(self): return ("os_model", "os_manufacturer") |
| 29 | + def getHWProductKey(self): return ("device_hw_model", "device_hw_manufacturer") |
| 30 | + def getHWTag(self): return "device_hw_tag" |
| 31 | + def getHWSerialNumber(self): return "device_hw_sn" |
| 32 | + |
| 33 | + |
| 34 | +class DummyHardwareComponent(Hardware): |
| 35 | + id = "dummy_hw_comp" |
| 36 | + title = "Dummy HW Component" |
| 37 | + |
| 38 | + tag = "comp_hw_tag" |
| 39 | + serialNumber = "comp_hw_sn" |
| 40 | + |
| 41 | + def __init__(self): |
| 42 | + # Prevent calling MEProduct.__init__ which relies on ZODB infrastructure |
| 43 | + pass |
| 44 | + |
| 45 | + def _propertyMap(self): return [] |
| 46 | + def device(self): return None |
| 47 | + def getProductionState(self): return 1000 |
| 48 | + def getPriorityConversions(self): return {} |
| 49 | + def getProdStateConversions(self): return {} |
| 50 | + def convertProdState(self, state): return "Production" |
| 51 | + |
| 52 | + def getOSProductKey(self): return ("os_model", "os_manufacturer") |
| 53 | + |
| 54 | + # Its own hardware properties |
| 55 | + def getProductKey(self): return ("comp_hw_model", "comp_hw_manufacturer") |
| 56 | + |
| 57 | + # Inherited device properties (fallback) |
| 58 | + def getHWProductKey(self): return ("device_hw_model", "device_hw_manufacturer") |
| 59 | + def getHWTag(self): return "device_hw_tag" |
| 60 | + def getHWSerialNumber(self): return "device_hw_sn" |
| 61 | + |
| 62 | + |
| 63 | +class TestFactGeneration(TestCase): |
| 64 | + |
| 65 | + def test_device_hardware_extraction(self): |
| 66 | + """ |
| 67 | + Verify that device_info_fact correctly extracts hardware properties |
| 68 | + from a standard Device object using the fallback methods. |
| 69 | + """ |
| 70 | + device = DummyDevice() |
| 71 | + fact = device_info_fact(device) |
| 72 | + |
| 73 | + self.assertEqual(fact.data.get(MetadataKeys.HW_MODEL), "device_hw_model") |
| 74 | + self.assertEqual(fact.data.get(MetadataKeys.HW_MANUFACTURER), "device_hw_manufacturer") |
| 75 | + self.assertEqual(fact.data.get(MetadataKeys.HW_TAG), "device_hw_tag") |
| 76 | + self.assertEqual(fact.data.get(MetadataKeys.HW_SERIAL_NUMBER), "device_hw_sn") |
| 77 | + |
| 78 | + def test_hardware_component_extraction(self): |
| 79 | + """ |
| 80 | + Verify that device_info_fact extracts hardware properties directly from |
| 81 | + a component that inherits from Hardware, rather than falling back to the |
| 82 | + parent device methods. |
| 83 | + """ |
| 84 | + hw_comp = DummyHardwareComponent() |
| 85 | + fact = device_info_fact(hw_comp) |
| 86 | + |
| 87 | + self.assertEqual(fact.data.get(MetadataKeys.HW_MODEL), "comp_hw_model") |
| 88 | + self.assertEqual(fact.data.get(MetadataKeys.HW_MANUFACTURER), "comp_hw_manufacturer") |
| 89 | + self.assertEqual(fact.data.get(MetadataKeys.HW_TAG), "comp_hw_tag") |
| 90 | + self.assertEqual(fact.data.get(MetadataKeys.HW_SERIAL_NUMBER), "comp_hw_sn") |
0 commit comments