Skip to content

Commit 260fb94

Browse files
committed
Fix import statement for Hardware in fact.py and update test_fact.py
1 parent d91e645 commit 260fb94

File tree

3 files changed

+54
-50
lines changed

3 files changed

+54
-50
lines changed

ci/prodbin/Dockerfile.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ RUN <<EOT
104104
set -eux
105105

106106
# Install python 2 with latest security fixes from Ubuntu 22.04
107-
BASE_URL="launchpad.net/ubuntu/+archive/primary/+files/"
107+
BASE_URL="http://security.ubuntu.com/ubuntu/pool/universe/p/python2.7"
108108

109109
# Manually download libssl1.1 from Jammy as it is required for Python 2.7's _ssl module
110-
wget -c "https://${BASE_URL}/libssl1.1_1.1.1f-1ubuntu2.24_amd64.deb"
110+
wget -c "http://security.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2.24_amd64.deb"
111111
dpkg -i libssl1.1_1.1.1f-1ubuntu2.24_amd64.deb
112112
rm libssl1.1_1.1.1f-1ubuntu2.24_amd64.deb
113113

src/Products/Zing/fact.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from Products.ZenModel.System import System
2424
from Products.ZenModel.Location import Location
2525
from Products.ZenRelations.ZenPropertyManager import iszprop, iscustprop
26-
from Products.ZenModel.Hardware import Hardware
2726

2827
from .interfaces import IImpactRelationshipsFactProvider
2928
from .shortid import shortid
@@ -224,6 +223,7 @@ def device_info_fact(device):
224223
hwTag = None
225224
hwSN = None
226225

226+
from Products.ZenModel.Hardware import Hardware
227227
if isinstance(device, Hardware):
228228
hwProductKey = device.getProductKey()
229229
hwTag = getattr(device, "tag", None)

src/Products/Zing/tests/test_fact.py

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,12 @@
1010
from __future__ import absolute_import
1111

1212
from unittest import TestCase
13+
from mock import Mock
1314

15+
from Products.ZenModel.Device import Device
1416
from Products.ZenModel.Hardware import Hardware
1517
from 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

6320
class 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

Comments
 (0)