1313import argparse
1414import base64
1515import json
16+ import os
1617import sys
1718
1819import lib .args
@@ -24,7 +25,7 @@ import lib.url
2425from lib .globals import STATE_CRIT , STATE_OK , STATE_UNKNOWN , STATE_WARN
2526
2627__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
27- __version__ = '2026042303 '
28+ __version__ = '2026042401 '
2829
2930DESCRIPTION = """Checks the state of all physical drives and their storage controllers in a
3031Redfish-compatible server via the Redfish API. Alerts when any drive or storage controller
@@ -112,6 +113,21 @@ def parse_args():
112113 return args
113114
114115
116+ def load_test_fixture (test_args , path ):
117+ # Replace the first element of args.TEST with the walk-specific
118+ # fixture path, read it via lib.lftest.test() and return the parsed
119+ # JSON. On a missing file or malformed JSON, exit STATE_UNKNOWN with
120+ # a helpful message instead of letting json.loads raise a traceback.
121+ if not os .path .isfile (path ):
122+ lib .base .cu (f'Test fixture not found: "{ path } ".' )
123+ test_args [0 ] = path
124+ stdout , _ , _ = lib .lftest .test (test_args )
125+ try :
126+ return json .loads (stdout )
127+ except (json .JSONDecodeError , ValueError ) as e :
128+ lib .base .cu (f'Test fixture "{ path } " does not contain valid JSON: { e } ' )
129+
130+
115131def main ():
116132 """The main function. This is where the magic happens."""
117133
@@ -146,9 +162,7 @@ def main():
146162 # file names describe what they contain (systems, system,
147163 # storages, storage, drive-N).
148164 test_base = args .TEST [0 ]
149- args .TEST [0 ] = f'{ test_base } -systems'
150- stdout , _ , _ = lib .lftest .test (args .TEST )
151- result = json .loads (stdout )
165+ result = load_test_fixture (args .TEST , f'{ test_base } -systems' )
152166 # "Members": [
153167 # {
154168 # "@odata.id": "/redfish/v1/Systems/437XR1138R2"
@@ -179,9 +193,7 @@ def main():
179193 )
180194 )
181195 else :
182- args .TEST [0 ] = f'{ test_base } -system'
183- stdout , _ , _ = lib .lftest .test (args .TEST )
184- systems = json .loads (stdout )
196+ systems = load_test_fixture (args .TEST , f'{ test_base } -system' )
185197 systems = lib .redfish .get_systems (systems )
186198 if systems ['Status_State' ] not in ['Enabled' , 'Quiesced' ]:
187199 continue
@@ -217,9 +229,7 @@ def main():
217229 )
218230 )
219231 else :
220- args .TEST [0 ] = f'{ test_base } -storages'
221- stdout , _ , _ = lib .lftest .test (args .TEST )
222- storages = json .loads (stdout )
232+ storages = load_test_fixture (args .TEST , f'{ test_base } -storages' )
223233 table_data = []
224234 table_data_drive = []
225235 for storage in storages .get ('Members' , []):
@@ -235,9 +245,7 @@ def main():
235245 )
236246 )
237247 else :
238- args .TEST [0 ] = f'{ test_base } -storage'
239- stdout , _ , _ = lib .lftest .test (args .TEST )
240- storage_data = json .loads (stdout )
248+ storage_data = load_test_fixture (args .TEST , f'{ test_base } -storage' )
241249
242250 # get drives attached to the storage member
243251 for drive_idx , drive in enumerate (storage_data .get ('Drives' , [])):
@@ -253,9 +261,10 @@ def main():
253261 )
254262 )
255263 else :
256- args .TEST [0 ] = f'{ test_base } -drive-{ drive_idx } '
257- stdout , _ , _ = lib .lftest .test (args .TEST )
258- drive_data = json .loads (stdout )
264+ drive_data = load_test_fixture (
265+ args .TEST ,
266+ f'{ test_base } -drive-{ drive_idx } ' ,
267+ )
259268 drive_data = lib .redfish .get_systems_storage_drives (drive_data )
260269 if drive_data ['Status_State' ] not in ['Enabled' , 'Quiesced' ]:
261270 continue
0 commit comments