Skip to content

Commit feaa251

Browse files
authored
Updated pretest routine to not overwrite the testbed metadata. (sonic-net#21834)
Needed for the switch+dpu pretest combo runs. Modified the test_pretest.py file to update the testbed metadata on the subsequent calls. This resolved the pretest conflicts between the switch and the dpu pretests. What is the motivation for this PR? Enable pretest for both switch and dpu in any order/place of the test sequence. How did you do it? Modified the code to update JSON instead of overwriting it How did you verify/test it? Ran multiple switch and DPI regressions Signed-off-by: nmirin <nikolay.a.mirin@gmail.com>
1 parent 378e3bb commit feaa251

1 file changed

Lines changed: 38 additions & 9 deletions

File tree

tests/test_pretest.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,41 @@ def collect_dut_info(dut, metadata):
142142
metadata[dut.hostname] = dut_info
143143

144144

145+
def update_testbed_metadata(metadata, tbname, filepath):
146+
"""Update or create testbed metadata JSON file.
147+
148+
Reads existing metadata file (if present), updates or adds testbed metadata,
149+
and writes back to file. Handles missing files and JSON decode errors gracefully.
150+
151+
Args:
152+
metadata: Dictionary containing DUT metadata to be stored.
153+
tbname: Testbed name used as key in the metadata file.
154+
filepath: Path to the metadata JSON file.
155+
156+
Returns:
157+
None.
158+
"""
159+
try:
160+
with open(filepath, 'r') as yf:
161+
info = json.load(yf)
162+
try:
163+
info[tbname].update(metadata)
164+
except KeyError:
165+
logger.info(f"The testbed '{tbname}' is not in the file '{filepath}', adding it.")
166+
info[tbname] = metadata
167+
except FileNotFoundError:
168+
logger.info(f"The testbed metadata file '{filepath}' was not found, creating new file.")
169+
info = {tbname: metadata}
170+
except json.JSONDecodeError as e:
171+
logger.warning(f"Error: Failed to decode JSON from the file '{filepath}': {e}, recreating the file.")
172+
info = {tbname: metadata}
173+
try:
174+
with open(filepath, 'w') as yf:
175+
json.dump(info, yf, indent=4)
176+
except IOError as e:
177+
logger.warning('Unable to create file {}: {}'.format(filepath, e))
178+
179+
145180
def test_update_testbed_metadata(duthosts, tbinfo, fanouthosts):
146181
metadata = {}
147182
tbname = tbinfo['conf-name']
@@ -151,17 +186,11 @@ def test_update_testbed_metadata(duthosts, tbinfo, fanouthosts):
151186
for duthost in duthosts:
152187
executor.submit(collect_dut_info, duthost, metadata)
153188

154-
info = {tbname: metadata}
155189
folder = 'metadata'
190+
if not os.path.exists(folder):
191+
os.mkdir(folder)
156192
filepath = os.path.join(folder, tbname + '.json')
157-
try:
158-
if not os.path.exists(folder):
159-
os.mkdir(folder)
160-
with open(filepath, 'w') as yf:
161-
json.dump(info, yf, indent=4)
162-
except IOError as e:
163-
logger.warning('Unable to create file {}: {}'.format(filepath, e))
164-
193+
update_testbed_metadata(metadata, tbname, filepath)
165194
prepare_autonegtest_params(duthosts, fanouthosts)
166195

167196

0 commit comments

Comments
 (0)