22import importlib
33import logging
44import json
5+ import os
56
67script = importlib .import_module ("aci-preupgrade-validation-script" )
78AciVersion = script .AciVersion
9+ AciResult = script .AciResult
810
911
1012@pytest .fixture (autouse = True )
@@ -133,7 +135,7 @@ def _mock_get_target_version(arg_tversion):
133135 "ave_eol_check" ,
134136 {"username" : "admin" , "password" : "mypassword" , "cversion" : AciVersion ("6.0(4d)" ), "tversion" : AciVersion ("6.1(4a)" ), "sw_cversion" : AciVersion ("6.0(9d)" ), "vpc_node_ids" : ["101" , "102" ]},
135137 ),
136- # veresions are switch syntax
138+ # versions are switch syntax
137139 # The version `get_target_version()` is ignored.
138140 (
139141 {
@@ -147,7 +149,7 @@ def _mock_get_target_version(arg_tversion):
147149 "ave_eol_check" ,
148150 {"username" : "admin" , "password" : "mypassword" , "cversion" : AciVersion ("6.0(4d)" ), "tversion" : AciVersion ("6.1(4a)" ), "sw_cversion" : AciVersion ("6.0(9d)" ), "vpc_node_ids" : ["101" , "102" ]},
149151 ),
150- # veresions are switch or APIC syntax
152+ # versions are switch or APIC syntax
151153 # The version `get_target_version()` is ignored.
152154 (
153155 {
@@ -164,15 +166,31 @@ def _mock_get_target_version(arg_tversion):
164166 ],
165167)
166168def test_prepare (mock_icurl , api_only , arg_tversion , arg_cversion , debug_function , expected_result ):
169+ script .initialize ()
167170 checks = script .get_checks (api_only , debug_function )
168- inputs = script .prepare (api_only , arg_tversion , arg_cversion , len ( checks ) )
171+ inputs = script .prepare (api_only , arg_tversion , arg_cversion , checks )
169172 for key , value in expected_result .items ():
170- if "version" in key :
173+ if "version" in key : # cversion or tversion
171174 assert isinstance (inputs [key ], AciVersion )
172175 assert str (inputs [key ]) == str (value )
173176 else :
174177 assert inputs [key ] == value
175178
179+ result_files = os .listdir (script .JSON_DIR )
180+ # Result files should be created for all checks
181+ assert len (result_files ) == len (checks )
182+ for check in checks :
183+ # Rule name is known only through the wrapper `check_wrapper`.
184+ # Rule name content should be checked via another unit test.
185+ # Use AciResult class here just to get the filename from `check.__name__`.
186+ ar = AciResult (check .__name__ , "unknown_name" , "" )
187+ file_path = os .path .join (script .JSON_DIR , ar .filename )
188+ assert os .path .exists (file_path ), "Missing result file: {}" .format (file_path )
189+ with open (file_path , "r" ) as f :
190+ result = json .load (f )
191+ assert result ["ruleId" ] == check .__name__
192+ assert result ["ruleStatus" ] == AciResult .IN_PROGRESS
193+
176194 with open (script .META_FILE , "r" ) as f :
177195 meta = json .load (f )
178196 assert meta ["name" ] == "PreupgradeCheck"
@@ -191,13 +209,13 @@ def test_prepare(mock_icurl, api_only, arg_tversion, arg_cversion, debug_functio
191209def test_tversion_invald ():
192210 with pytest .raises (SystemExit ):
193211 with pytest .raises (ValueError ):
194- script .prepare (False , "invalid_version" , "6.0(1a)" , 1 )
212+ script .prepare (False , "invalid_version" , "6.0(1a)" , [] )
195213
196214
197215def test_cversion_invald ():
198216 with pytest .raises (SystemExit ):
199217 with pytest .raises (ValueError ):
200- script .prepare (False , "6.0(1a)" , "invalid_version" , 1 )
218+ script .prepare (False , "6.0(1a)" , "invalid_version" , [] )
201219
202220
203221@pytest .mark .parametrize (
@@ -264,7 +282,46 @@ def test_prepare_exception(capsys, caplog, mock_icurl, api_only, arg_tversion, a
264282 with pytest .raises (SystemExit ):
265283 with pytest .raises (Exception ):
266284 checks = script .get_checks (api_only , debug_function )
267- script .prepare (api_only , arg_tversion , arg_cversion , len ( checks ) )
285+ script .prepare (api_only , arg_tversion , arg_cversion , checks )
268286 captured = capsys .readouterr ()
269287 print (captured .out )
270288 assert captured .out .endswith (expected_result )
289+
290+
291+ # Unit test focusing only on the result file creation
292+ def test_prepare_initial_result_files (mock_icurl , icurl_outputs ):
293+ # Provide required API outputs used inside prepare()
294+ icurl_outputs .update ({
295+ "firmwareCtrlrRunning.json" : outputs ["cversion" ],
296+ "firmwareRunning.json" : outputs ["switch_version" ],
297+ "fabricNodePEp.json" : outputs ["vpc_nodes" ],
298+ })
299+
300+ # Create two simple checks with known titles
301+ @script .check_wrapper (check_title = "Prepare Check A" )
302+ def prep_check_a (** kwargs ):
303+ return script .Result (result = script .PASS )
304+
305+ @script .check_wrapper (check_title = "Prepare Check B" )
306+ def prep_check_b (** kwargs ):
307+ return script .Result (result = script .PASS )
308+
309+ checks = [prep_check_a , prep_check_b ]
310+
311+ # Run prepare which should only initialize result files
312+ script .prepare (api_only = False , arg_tversion = None , arg_cversion = None , checks = checks )
313+
314+ # Verify result files and contents
315+ expected = {
316+ "prep_check_a" : "Prepare Check A" ,
317+ "prep_check_b" : "Prepare Check B" ,
318+ }
319+ for func_name , title in expected .items ():
320+ ar = AciResult (func_name , title , "" )
321+ file_path = os .path .join (script .JSON_DIR , ar .filename )
322+ assert os .path .exists (file_path ), "Missing result file: {}" .format (file_path )
323+ with open (file_path , "r" ) as f :
324+ data = json .load (f )
325+ assert data ["ruleId" ] == func_name
326+ assert data ["name" ] == title
327+ assert data ["ruleStatus" ] == AciResult .IN_PROGRESS
0 commit comments