Skip to content

Commit 7cd0434

Browse files
committed
hw-mgmt: scripts: Cleanup and beautify *.py scripts
Cleanup and beautify *.py scripts Signed-off-by: Oleksandr Shamray <oleksandrs@nvidia.com>
1 parent 6b5b570 commit 7cd0434

4 files changed

Lines changed: 188 additions & 143 deletions

File tree

unittest/hw_mgmgt_sync/module_populate/run_all_tests.py

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import random
1212
import argparse
1313

14+
1415
def setup_import_path(hw_mgmt_path=None):
1516
"""Setup import path for hw_management_sync module"""
1617
if hw_mgmt_path:
@@ -26,80 +27,83 @@ def setup_import_path(hw_mgmt_path=None):
2627
hw_mgmt_dir = './bin'
2728
else:
2829
raise FileNotFoundError("Cannot find hw_management_sync.py")
29-
30+
3031
hw_mgmt_dir = os.path.abspath(hw_mgmt_dir)
3132
if hw_mgmt_dir not in sys.path:
3233
sys.path.insert(0, hw_mgmt_dir)
3334
return hw_mgmt_dir
3435

36+
3537
def test_basic_functionality():
3638
"""Test basic function imports and constants"""
3739
print("🧪 Testing basic functionality...")
38-
40+
3941
try:
4042
from hw_management_sync import CONST, sdk_temp2degree, module_temp_populate
41-
43+
4244
# Test constants
4345
assert CONST.SDK_FW_CONTROL == 0, f"SDK_FW_CONTROL should be 0, got {CONST.SDK_FW_CONTROL}"
4446
assert CONST.SDK_SW_CONTROL == 1, f"SDK_SW_CONTROL should be 1, got {CONST.SDK_SW_CONTROL}"
4547
print("✅ Constants test PASSED")
46-
48+
4749
# Test function existence
4850
assert callable(module_temp_populate), "module_temp_populate should be callable"
4951
assert callable(sdk_temp2degree), "sdk_temp2degree should be callable"
5052
print("✅ Function existence test PASSED")
51-
53+
5254
return True
5355
except Exception as e:
5456
print(f"❌ Basic functionality test FAILED: {e}")
5557
return False
5658

59+
5760
def test_temperature_conversion():
5861
"""Test sdk_temp2degree function with various inputs"""
5962
print("🧪 Testing temperature conversion...")
60-
63+
6164
try:
6265
from hw_management_sync import sdk_temp2degree
63-
66+
6467
test_cases = [
6568
(0, 0, "Zero temperature"),
6669
(25, 3125, "Normal positive temperature"),
67-
(-10, 0xffff + (-10) + 1, "Normal negative temperature"),
70+
(-10, 0xffff + (-10) + 1, "Normal negative temperature"),
6871
]
69-
72+
7073
passed = 0
7174
total = len(test_cases)
72-
75+
7376
for input_temp, expected, description in test_cases:
7477
result = sdk_temp2degree(input_temp)
7578
if result == expected:
7679
print(f" ✅ {description}: sdk_temp2degree({input_temp}) = {result}")
7780
passed += 1
7881
else:
7982
print(f" ❌ {description}: sdk_temp2degree({input_temp}) = {result}, expected {expected}")
80-
83+
8184
if passed == total:
8285
print(f"✅ Temperature conversion test PASSED ({passed}/{total})")
8386
return True
8487
else:
8588
print(f"❌ Temperature conversion test FAILED ({passed}/{total})")
8689
return False
87-
90+
8891
except Exception as e:
8992
print(f"❌ Temperature conversion test FAILED: {e}")
9093
return False
9194

95+
9296
def test_random_module_states():
9397
"""Test with randomized module states as requested"""
9498
print("🧪 Testing random module states (5 modules as requested)...")
95-
99+
96100
try:
97101
from hw_management_sync import CONST
98-
102+
99103
# Generate 5 random module states as requested
100104
random.seed(42) # For reproducible results
101105
module_states = []
102-
106+
103107
for i in range(5):
104108
state = {
105109
'present': random.choice([0, 1]),
@@ -108,103 +112,106 @@ def test_random_module_states():
108112
'threshold_hi': random.randint(60, 80)
109113
}
110114
module_states.append(state)
111-
115+
112116
mode_str = "SDK_SW_CONTROL" if state['mode'] == CONST.SDK_SW_CONTROL else "SDK_FW_CONTROL"
113117
present_str = "Present" if state['present'] else "Not Present"
114-
print(f" Module {i+1}: {mode_str}, {present_str}, Temp={state['temperature']}")
115-
118+
print(f" Module {i + 1}: {mode_str}, {present_str}, Temp={state['temperature']}")
119+
116120
# Test the logic expectations
117121
fw_control_count = sum(1 for state in module_states if state['mode'] == CONST.SDK_FW_CONTROL)
118122
sw_control_count = sum(1 for state in module_states if state['mode'] == CONST.SDK_SW_CONTROL)
119123
present_count = sum(1 for state in module_states if state['present'] == 1)
120-
124+
121125
print(f" 📊 Summary: {fw_control_count} FW control, {sw_control_count} SW control, {present_count} present")
122126
print("✅ Random module states test PASSED")
123127
return True
124-
128+
125129
except Exception as e:
126130
print(f"❌ Random module states test FAILED: {e}")
127131
return False
128132

133+
129134
def test_folder_agnostic_functionality(hw_mgmt_dir):
130135
"""Test that the folder-agnostic import worked correctly"""
131136
print("🧪 Testing folder-agnostic functionality...")
132-
137+
133138
try:
134139
import hw_management_sync
135140
module_file = hw_management_sync.__file__
136141
expected_dir = os.path.abspath(hw_mgmt_dir)
137142
actual_dir = os.path.dirname(os.path.abspath(module_file))
138-
143+
139144
assert actual_dir == expected_dir, f"Module loaded from {actual_dir}, expected {expected_dir}"
140-
145+
141146
print(f" ✅ Module loaded from: {actual_dir}")
142147
print("✅ Folder-agnostic functionality test PASSED")
143148
return True
144-
149+
145150
except Exception as e:
146151
print(f"❌ Folder-agnostic functionality test FAILED: {e}")
147152
return False
148153

154+
149155
def main():
150156
"""Main test runner"""
151157
parser = argparse.ArgumentParser(description='Comprehensive test runner for module_temp_populate')
152158
parser.add_argument('--hw-mgmt-path', help='Path to hw_management_sync.py')
153159
args = parser.parse_args()
154-
160+
155161
print("=" * 70)
156162
print("🚀 MODULE_TEMP_POPULATE TEST SUITE")
157163
print("=" * 70)
158164
print(f"Python version: {sys.version}")
159-
165+
160166
try:
161167
# Setup import path
162168
hw_mgmt_dir = setup_import_path(args.hw_mgmt_path)
163169
print(f"📂 Using hw_management_sync.py from: {hw_mgmt_dir}")
164170
print("=" * 70)
165-
171+
166172
# Run all tests
167173
tests = [
168174
("Basic Functionality", test_basic_functionality),
169-
("Temperature Conversion", test_temperature_conversion),
175+
("Temperature Conversion", test_temperature_conversion),
170176
("Random Module States (5 modules)", test_random_module_states),
171177
("Folder-Agnostic Functionality", lambda: test_folder_agnostic_functionality(hw_mgmt_dir)),
172178
]
173-
179+
174180
passed = 0
175181
total = len(tests)
176-
182+
177183
for test_name, test_func in tests:
178184
print(f"\n🔬 Running: {test_name}")
179185
print("-" * 40)
180186
if test_func():
181187
passed += 1
182188
print()
183-
189+
184190
# Final results
185191
print("=" * 70)
186192
print("📋 FINAL TEST RESULTS")
187193
print("=" * 70)
188-
194+
189195
for i, (test_name, _) in enumerate(tests):
190196
status = "✅ PASSED" if i < passed else "❌ FAILED"
191197
print(f" {status} - {test_name}")
192-
198+
193199
print(f"\n🏆 Tests Passed: {passed}/{total}")
194-
200+
195201
if passed == total:
196202
print("🎉 ALL TESTS PASSED!")
197203
print("✅ The module_temp_populate test suite is working correctly!")
198204
print("✅ Folder-agnostic functionality confirmed!")
199205
print("✅ 5 modules with random parameters tested!")
200206
else:
201207
print("⚠️ Some tests failed. Please check the output above.")
202-
208+
203209
return 0 if passed == total else 1
204-
210+
205211
except Exception as e:
206212
print(f"❌ Critical error: {e}")
207213
return 1
208214

215+
209216
if __name__ == '__main__':
210-
exit(main())
217+
exit(main())

unittest/hw_mgmgt_sync/module_populate/simple_test.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import argparse
77

8+
89
def setup_import_path(hw_mgmt_path=None):
910
"""Setup import path for hw_management_sync module"""
1011
if hw_mgmt_path:
@@ -20,12 +21,13 @@ def setup_import_path(hw_mgmt_path=None):
2021
hw_mgmt_dir = './bin'
2122
else:
2223
raise FileNotFoundError("Cannot find hw_management_sync.py")
23-
24+
2425
hw_mgmt_dir = os.path.abspath(hw_mgmt_dir)
2526
if hw_mgmt_dir not in sys.path:
2627
sys.path.insert(0, hw_mgmt_dir)
2728
return hw_mgmt_dir
2829

30+
2931
def main():
3032
parser = argparse.ArgumentParser(description='Simple test for folder-agnostic functionality')
3133
parser.add_argument('--hw-mgmt-path', help='Path to hw_management_sync.py')
@@ -34,36 +36,37 @@ def main():
3436
try:
3537
hw_mgmt_dir = setup_import_path(args.hw_mgmt_path)
3638
print(f"Found hw_management_sync.py in: {hw_mgmt_dir}")
37-
39+
3840
from hw_management_sync import CONST, sdk_temp2degree, module_temp_populate
3941
print("✅ Import successful!")
4042
print(f"✅ CONST.SDK_FW_CONTROL = {CONST.SDK_FW_CONTROL}")
4143
print(f"✅ CONST.SDK_SW_CONTROL = {CONST.SDK_SW_CONTROL}")
4244
print(f"✅ sdk_temp2degree(25) = {sdk_temp2degree(25)}")
4345
print(f"✅ sdk_temp2degree(-10) = {sdk_temp2degree(-10)}")
44-
46+
4547
# Test temperature conversion formula
4648
assert sdk_temp2degree(25) == 25 * 125, "Positive temperature conversion failed"
4749
assert sdk_temp2degree(-10) == 0xffff + (-10) + 1, "Negative temperature conversion failed"
4850
print("✅ Temperature conversion tests PASSED")
49-
51+
5052
# Test constants
5153
assert CONST.SDK_FW_CONTROL == 0, "SDK_FW_CONTROL should be 0"
5254
assert CONST.SDK_SW_CONTROL == 1, "SDK_SW_CONTROL should be 1"
5355
print("✅ Constants tests PASSED")
54-
56+
5557
# Test function exists
5658
assert callable(module_temp_populate), "module_temp_populate should be callable"
5759
print("✅ Function existence tests PASSED")
58-
60+
5961
print("✅ ALL TESTS PASSED!")
6062
print(f"✅ Successfully tested folder-agnostic import from: {hw_mgmt_dir}")
61-
63+
6264
except Exception as e:
6365
print(f"❌ Error: {e}")
6466
return 1
65-
67+
6668
return 0
6769

70+
6871
if __name__ == '__main__':
69-
exit(main())
72+
exit(main())

0 commit comments

Comments
 (0)