1010from __future__ import absolute_import
1111
1212from unittest import TestCase
13+ from mock import Mock
1314
15+ from Products .ZenModel .Device import Device
1416from Products .ZenModel .Hardware import Hardware
1517from Products .Zing .fact import device_info_fact , MetadataKeys
1618
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-
6219
6320class TestFactGeneration (TestCase ):
6421
@@ -67,7 +24,31 @@ def test_device_hardware_extraction(self):
6724 Verify that device_info_fact correctly extracts hardware properties
6825 from a standard Device object using the fallback methods.
6926 """
70- device = DummyDevice ()
27+ # Create a flexible mock without a strict spec to avoid AttributeError
28+ # for dynamically acquired Zope methods.
29+ device = Mock ()
30+ device .id = "test_device"
31+ device .title = "Test Device"
32+ device .meta_type = "Device"
33+
34+ # Explicitly set __class__ to bypass isinstance(device, Hardware)
35+ device .__class__ = Device
36+
37+ # Stub the specific methods called during fact generation
38+ device .titleOrId .return_value = "Test Device"
39+ device ._propertyMap .return_value = []
40+ device .device .return_value = None
41+ device .getProductionState .return_value = 1000
42+ device .getPriorityConversions .return_value = {}
43+ device .getProdStateConversions .return_value = {}
44+ device .convertProdState .return_value = "Production"
45+ device .getOSProductKey .return_value = ("os_model" , "os_manufacturer" )
46+
47+ # Mock the hardware fallback methods for a non-Hardware object
48+ device .getHWProductKey .return_value = ("device_hw_model" , "device_hw_manufacturer" )
49+ device .getHWTag .return_value = "device_hw_tag"
50+ device .getHWSerialNumber .return_value = "device_hw_sn"
51+
7152 fact = device_info_fact (device )
7253
7354 self .assertEqual (fact .data .get (MetadataKeys .HW_MODEL ), "device_hw_model" )
@@ -81,7 +62,30 @@ def test_hardware_component_extraction(self):
8162 a component that inherits from Hardware, rather than falling back to the
8263 parent device methods.
8364 """
84- hw_comp = DummyHardwareComponent ()
65+ # Create a flexible mock Hardware component
66+ hw_comp = Mock ()
67+ hw_comp .id = "test_hw_comp"
68+ hw_comp .title = "Test HW Comp"
69+ hw_comp .meta_type = "HWComponent"
70+
71+ # Explicitly set __class__ to Hardware so isinstance(device, Hardware) is True
72+ hw_comp .__class__ = Hardware
73+
74+ # Stub the specific methods called during fact generation
75+ hw_comp .titleOrId .return_value = "Test HW Comp"
76+ hw_comp ._propertyMap .return_value = []
77+ hw_comp .device .return_value = None
78+ hw_comp .getProductionState .return_value = 1000
79+ hw_comp .getPriorityConversions .return_value = {}
80+ hw_comp .getProdStateConversions .return_value = {}
81+ hw_comp .convertProdState .return_value = "Production"
82+ hw_comp .getOSProductKey .return_value = ("os_model" , "os_manufacturer" )
83+
84+ # Its own hardware properties (which should be used because it's a Hardware instance)
85+ hw_comp .getProductKey .return_value = ("comp_hw_model" , "comp_hw_manufacturer" )
86+ hw_comp .tag = "comp_hw_tag"
87+ hw_comp .serialNumber = "comp_hw_sn"
88+
8589 fact = device_info_fact (hw_comp )
8690
8791 self .assertEqual (fact .data .get (MetadataKeys .HW_MODEL ), "comp_hw_model" )
0 commit comments