33import os
44import pytest
55import random
6+ import re
67import time
78from tests .common .helpers .port_utils import get_common_supported_speeds
89
@@ -316,15 +317,77 @@ def test_collect_pfc_pause_delay_params(duthosts, tbinfo):
316317 logger .warning ('Unable to create file {}: {}' .format (filepath , e ))
317318
318319
319- def test_update_saithrift_ptf (request , ptfhost ):
320+ def test_update_saithrift_ptf (request , ptfhost , duthosts , enum_dut_hostname ):
320321 '''
321322 Install the correct python saithrift package on the ptf
322323 '''
323324 py_saithrift_url = request .config .getoption ("--py_saithrift_url" )
324325 if not py_saithrift_url :
325326 pytest .skip ("No URL specified for python saithrift package" )
327+
328+ duthost = duthosts [enum_dut_hostname ]
329+ output = duthost .shell ("show version" , module_ignore_errors = True )['stdout' ]
330+ version_reg = re .compile (r"sonic software version: +([^\s]+)\s" , re .IGNORECASE )
331+ asic_reg = re .compile (r"asic: +([^\s]+)\s" , re .IGNORECASE )
332+ # sample value: SONiC.20240510.33, SONiC.20250505.07, SONiC.internal.129741107-8524154c2d,
333+ # SONiC.master.882522-695c23859
334+ version = version_reg .findall (output )[0 ] if version_reg .search (output ) else ""
335+ # only broadcom, cisco-8000, mellanox support qos sai tests
336+ asic = asic_reg .findall (output )[0 ] if asic_reg .search (output ) else ""
337+
338+ if "master" in version :
339+ branch_name = "master"
340+ elif "internal" in version :
341+ branch_name = "internal"
342+ else :
343+ # Extract year/month from version string to determine branch
344+ date_match = re .search (r'(\d{4})(\d{2})' , version )
345+ if date_match :
346+ year , month = date_match .groups ()
347+ branch_name = f"internal-{ year } { month } "
348+ else :
349+ pytest .fail ("Unable to parse or recognize version format: {}" .format (version ))
350+
351+ # Apply special codename overrides for specific internal branches
352+ if branch_name == "internal-202411" :
353+ # internal-202411 has saithrift URL hardcoded to bullseye
354+ debian_codename = "bullseye"
355+ elif (branch_name .startswith ("internal-" ) and branch_name < "internal-202405" ):
356+ # For internal branches older than 202405, use the original URL without modification
357+ # No need to get debian_codename as URL won't be modified
358+ debian_codename = None
359+ else :
360+ # Get debian codename from syncd container (not host OS)
361+ # This applies to: master branch and internal branches >= 202405 (except 202411)
362+ try :
363+ # Try to get codename from syncd container
364+ syncd_codename_cmd = ("docker exec syncd grep VERSION_CODENAME /etc/os-release | "
365+ "cut -d= -f2 | tr -d '\" '" )
366+ syncd_codename_result = duthost .shell (syncd_codename_cmd , module_ignore_errors = True )
367+ if syncd_codename_result ['rc' ] == 0 and syncd_codename_result ['stdout' ].strip ():
368+ debian_codename = syncd_codename_result ['stdout' ].strip ()
369+ else :
370+ pytest .fail ("Failed to get debian codename from syncd container. RC: {}, Output: '{}'" .format (
371+ syncd_codename_result ['rc' ], syncd_codename_result ['stdout' ]))
372+ except Exception as e :
373+ pytest .fail ("Exception while getting debian codename from syncd container: {}" .format (str (e )))
374+
326375 pkg_name = py_saithrift_url .split ("/" )[- 1 ]
376+ ip_addr = py_saithrift_url .split ("/" )[2 ]
327377 ptfhost .shell ("rm -f {}" .format (pkg_name ))
378+
379+ if branch_name .startswith ("internal-" ) and branch_name < "internal-202405" :
380+ # For internal branches older than 202405, use the original URL without modification
381+ pass
382+ elif branch_name == "master" :
383+ py_saithrift_url = (f"http://{ ip_addr } /mssonic-public-pipelines/"
384+ f"Azure.sonic-buildimage.official.{ asic } /master/{ asic } /"
385+ f"latest/target/debs/{ debian_codename } /{ pkg_name } " )
386+ else :
387+ # For internal branches newer than 202405 and other branches
388+ py_saithrift_url = (f"http://{ ip_addr } /pipelines/Networking-acs-buildimage-Official/"
389+ f"{ asic } /{ branch_name } /latest/target/debs/{ debian_codename } /{ pkg_name } " )
390+
328391 # Retry download of saithrift library
329392 retry_count = 5
330393 while retry_count > 0 :
@@ -335,7 +398,7 @@ def test_update_saithrift_ptf(request, ptfhost):
335398 retry_count -= 1
336399
337400 if result ["failed" ] or "OK" not in result ["msg" ]:
338- pytest .skip ("Download failed/error while installing python saithrift package" )
401+ pytest .fail ("Download failed/error while installing python saithrift package: {}" . format ( py_saithrift_url ) )
339402 ptfhost .shell ("dpkg -i {}" .format (os .path .join ("/root" , pkg_name )))
340403 # In 202405 branch, the switch_sai_thrift package is inside saithrift-0.9-py3.11.egg
341404 # We need to move it out to the correct location
0 commit comments