Skip to content

Commit 3735454

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

File tree

3 files changed

+40
-50
lines changed

3 files changed

+40
-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="https://launchpad.net/ubuntu/+archive/primary/+files"
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 "${BASE_URL}/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: 37 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,24 @@ 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 mock Device. Using spec=Device ensures isinstance(device, Device) works.
28+
# It automatically provides dummy callable methods for titleOrId, getProductionState, etc.
29+
device = Mock(spec=Device)
30+
device.id = "test_device"
31+
device.title = "Test Device"
32+
device.meta_type = "Device"
33+
34+
# Stub the specific methods called during fact generation
35+
device.titleOrId.return_value = "Test Device"
36+
device._propertyMap.return_value = []
37+
device.device.return_value = None
38+
device.getOSProductKey.return_value = ("os_model", "os_manufacturer")
39+
40+
# Mock the hardware fallback methods for a non-Hardware object
41+
device.getHWProductKey.return_value = ("device_hw_model", "device_hw_manufacturer")
42+
device.getHWTag.return_value = "device_hw_tag"
43+
device.getHWSerialNumber.return_value = "device_hw_sn"
44+
7145
fact = device_info_fact(device)
7246

7347
self.assertEqual(fact.data.get(MetadataKeys.HW_MODEL), "device_hw_model")
@@ -81,7 +55,23 @@ def test_hardware_component_extraction(self):
8155
a component that inherits from Hardware, rather than falling back to the
8256
parent device methods.
8357
"""
84-
hw_comp = DummyHardwareComponent()
58+
# Create a mock Hardware component. spec=Hardware makes isinstance(device, Hardware) True
59+
hw_comp = Mock(spec=Hardware)
60+
hw_comp.id = "test_hw_comp"
61+
hw_comp.title = "Test HW Comp"
62+
hw_comp.meta_type = "HWComponent"
63+
64+
# Stub the specific methods called during fact generation
65+
hw_comp.titleOrId.return_value = "Test HW Comp"
66+
hw_comp._propertyMap.return_value = []
67+
hw_comp.device.return_value = None
68+
hw_comp.getOSProductKey.return_value = ("os_model", "os_manufacturer")
69+
70+
# Its own hardware properties (which should be used because it's a Hardware instance)
71+
hw_comp.getProductKey.return_value = ("comp_hw_model", "comp_hw_manufacturer")
72+
hw_comp.tag = "comp_hw_tag"
73+
hw_comp.serialNumber = "comp_hw_sn"
74+
8575
fact = device_info_fact(hw_comp)
8676

8777
self.assertEqual(fact.data.get(MetadataKeys.HW_MODEL), "comp_hw_model")

0 commit comments

Comments
 (0)