Skip to content

Commit

Permalink
modify
Browse files Browse the repository at this point in the history
  • Loading branch information
yutongzhang-microsoft committed Dec 11, 2024
1 parent a45d80a commit 49019cd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ steps:

- task: AzureCLI@2
displayName: "Calculate instance number"
continueOnError: false
inputs:
azureSubscription: "SONiC-Automation"
scriptType: 'bash'
Expand Down
18 changes: 13 additions & 5 deletions .azure-pipelines/impacted_area_testing/get-impacted-area.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ steps:
- script: |
set -x
DIFF_FOLDERS=$(git diff HEAD^ HEAD --name-only | xargs -n1 dirname | sort -u | tr '\n' ' ') || ""
echo -n "##vso[task.setvariable variable=DIFF_FOLDERS]$DIFF_FOLDERS"
DIFF_FOLDERS=$(git diff HEAD^ HEAD --name-only | xargs -n1 dirname | sort -u | tr '\n' ' ')
if [[ -z "$DIFF_FOLDERS" ]]; then
echo "##vso[task.complete result=Failed;]Get diff folders fail."
exit 1
else
echo -n "##vso[task.setvariable variable=DIFF_FOLDERS]$DIFF_FOLDERS"
fi
continueOnError: false
displayName: "Get diff folders"

- script: |
Expand Down Expand Up @@ -50,14 +57,15 @@ steps:
done
if [[ -z "$FINAL_FEATURES" ]]; then
TEST_SCRIPTS=$(python ./.azure-pipelines/impacted_area_testing/get_test_scripts.py --features "" --location tests)
TEST_SCRIPTS=$(python ./.azure-pipelines/impacted_area_testing/get_test_scripts.py --features "" --location tests) || ""
else
TEST_SCRIPTS=$(python ./.azure-pipelines/impacted_area_testing/get_test_scripts.py --features ${FINAL_FEATURES} --location tests)
TEST_SCRIPTS=$(python ./.azure-pipelines/impacted_area_testing/get_test_scripts.py --features ${FINAL_FEATURES} --location tests) || ""
fi
PR_CHECKERS=$(echo "${TEST_SCRIPTS}" | jq -c 'keys')
PR_CHECKERS=$(echo "${TEST_SCRIPTS}" | jq -c 'keys') || ""
echo "##vso[task.setvariable variable=PR_CHECKERS;isOutput=true]$PR_CHECKERS"
echo "##vso[task.setvariable variable=TEST_SCRIPTS;isOutput=true]$TEST_SCRIPTS"
name: SetVariableTask
continueOnError: false
displayName: "Get impacted area"
32 changes: 16 additions & 16 deletions .azure-pipelines/impacted_area_testing/get_test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ def collect_all_scripts(features, location):
'''
# Recursively find all files starting with "test_" and ending with ".py"
# Note: The full path and name of files are stored in a list named "files"
files = []
scripts = []
for feature in features.split(","):
feature_path = os.path.join(location, feature)
for root, dirs, file in os.walk(feature_path):
for f in file:
if f.startswith("test_") and f.endswith(".py"):
files.append(os.path.join(root, f))
files = natsorted(files)
for root, dirs, script in os.walk(feature_path):
for s in script:
if s.startswith("test_") and s.endswith(".py"):
scripts.append(os.path.join(root, s))
scripts = natsorted(scripts)

# Open each file and search for regex pattern
pattern = re.compile(r"[^@]pytest\.mark\.topology\(([^\)]*)\)")
Expand All @@ -57,31 +57,31 @@ def collect_all_scripts(features, location):
for topology_type in PR_TOPOLOGY_TYPE:
test_scripts_per_topology_type[topology_type] = []

for f in files:
for s in scripts:
# Remove prefix from file name:
filename = f[len(location) + 1:]
if filename in EXCLUDE_TEST_SCRIPTS:
script_name = s[len(location) + 1:]
if script_name in EXCLUDE_TEST_SCRIPTS:
continue

try:
with open(f, 'r') as file:
for line in file:
with open(s, 'r') as script:
for line in script:
# Get topology type of script from mark `pytest.mark.topology`
match = pattern.search(line)
if match:
for topology in match.group(1).split(","):
topology_mark = topology.strip().strip('"').strip("'")
if topology_mark == "any":
for key in ["t0", "t1"]:
if filename not in test_scripts_per_topology_type[key]:
test_scripts_per_topology_type[key].append(filename)
if script_name not in test_scripts_per_topology_type[key]:
test_scripts_per_topology_type[key].append(script_name)
else:
topology_type = topo_name_to_type(topology_mark)
if topology_type in test_scripts_per_topology_type \
and filename not in test_scripts_per_topology_type[topology_type]:
test_scripts_per_topology_type[topology_type].append(filename)
and script_name not in test_scripts_per_topology_type[topology_type]:
test_scripts_per_topology_type[topology_type].append(script_name)
except Exception as e:
logging.error('Failed to load file {}, error {}'.format(f, e))
logging.error('Failed to load file {}, error {}'.format(s, e))

return {k: v for k, v in test_scripts_per_topology_type.items() if v}

Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ stages:
- job: get_impacted_area
displayName: "Get impacted area"
timeoutInMinutes: 240
continueOnError: true
continueOnError: false
# pool: sonic-ubuntu-1c
steps:
- template: .azure-pipelines/impacted_area_testing/get-impacted-area.yml
Expand Down

0 comments on commit 49019cd

Please sign in to comment.