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 a28903f commit 46f8aef
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 498 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,58 @@ parameters:
default: ""

steps:
#- script: |
# # Check if azure cli is installed. If not, try to install it
# if ! command -v az; then
# echo "Azure CLI is not installed. Trying to install it..."
# curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
# else
# echo "Azure CLI is already installed"
# fi
#
# sudo apt-get update && sudo apt-get install -y jq
# displayName: "Install azure-cli"
- script: |
# Check if azure cli is installed. If not, try to install it
if ! command -v az; then
echo "Azure CLI is not installed. Trying to install it..."
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
else
echo "Azure CLI is already installed"
fi
sudo apt-get update && sudo apt-get install -y jq
displayName: "Install azure-cli"

- script: |
set -x
sudo apt-get update && sudo apt-get install -y jq
TEST_SCRIPTS=$(echo '$(TEST_SCRIPTS)' | jq -r -c '.${{ parameters.TOPOLOGY }}')
TEST_SCRIPTS=$(echo '$(TEST_SCRIPTS)' | jq -r -c '."${{ parameters.TOPOLOGY }}"')
SCRIPTS=$(echo "$TEST_SCRIPTS" | jq -r '. | join(",")')
echo -n "##vso[task.setvariable variable=SCRIPTS]$SCRIPTS"
displayName: "Get ${{ parameters.TOPOLOGY }} test scripts"

#- task: AzureCLI@2
# displayName: "Calculate instance number"
# inputs:
# azureSubscription: "SONiC-Automation"
# scriptType: 'bash'
# scriptLocation: 'inlineScript'
# inlineScript: |
# set -x
#
# pip install azure-kusto-data
# pip install azure-kusto-data azure-identity
#
## accessToken=$(az account get-access-token --resource https://api.kusto.windows.net --query accessToken -o tsv)
## export ACCESS_TOKEN=$accessToken
#
# INSTANCE_NUMBER=$(python ./.azure-pipelines/impacted_area_testing/calculate_instance_number.py --scripts $(SCRIPTS) --topology ${{ parameters.TOPOLOGY }} --branch ${{ parameters.BUILD_BRANCH }})
# echo "$INSTANCE_NUMBER"
# echo -n "##vso[task.setvariable variable=INSTANCE_NUMBER]$INSTANCE_NUMBER"
- task: AzureCLI@2
displayName: "Calculate instance number"
continueOnError: false
inputs:
azureSubscription: "SONiC-Automation"
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
set -x
- script: |
set -x
pip install azure-kusto-data
pip install azure-kusto-data azure-identity
pip install azure-kusto-data
pip install azure-kusto-data azure-identity
accessToken=$(az account get-access-token --resource https://api.kusto.windows.net --query accessToken -o tsv)
export ACCESS_TOKEN=$accessToken
# accessToken=$(az account get-access-token --resource https://api.kusto.windows.net --query accessToken -o tsv)
# export ACCESS_TOKEN=$accessToken
INSTANCE_NUMBER=$(python ./.azure-pipelines/impacted_area_testing/calculate_instance_number.py --scripts $(SCRIPTS) --topology ${{ parameters.TOPOLOGY }} --branch ${{ parameters.BUILD_BRANCH }})
echo "$INSTANCE_NUMBER"
echo -n "##vso[task.setvariable variable=INSTANCE_NUMBER]$INSTANCE_NUMBER"
INSTANCE_NUMBER=$(python ./.azure-pipelines/impacted_area_testing/calculate_instance_number.py --scripts $(SCRIPTS) --topology ${{ parameters.TOPOLOGY }} --branch ${{ parameters.BUILD_BRANCH }})
echo "$INSTANCE_NUMBER"
echo -n "##vso[task.setvariable variable=INSTANCE_NUMBER]$INSTANCE_NUMBER"
displayName: "Calculate instance number"
#- script: |
# set -x
#
# pip install azure-kusto-data
# pip install azure-kusto-data azure-identity
#
# # accessToken=$(az account get-access-token --resource https://api.kusto.windows.net --query accessToken -o tsv)
# # export ACCESS_TOKEN=$accessToken
#
# INSTANCE_NUMBER=$(python ./.azure-pipelines/impacted_area_testing/calculate_instance_number.py --scripts $(SCRIPTS) --topology ${{ parameters.TOPOLOGY }} --branch ${{ parameters.BUILD_BRANCH }})
# echo "$INSTANCE_NUMBER"
# echo -n "##vso[task.setvariable variable=INSTANCE_NUMBER]$INSTANCE_NUMBER"
# displayName: "Calculate instance number"
143 changes: 72 additions & 71 deletions .azure-pipelines/impacted_area_testing/calculate_instance_number.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import os
import argparse
import math
import subprocess
import json
from datetime import datetime, timezone
from constant import PR_CHECKER_TOPOLOGY_NAME, MAX_INSTANCE_NUMBER, MAX_GET_TOKEN_RETRY_TIMES
from constant import PR_CHECKER_TOPOLOGY_NAME, MAX_INSTANCE_NUMBER
from azure.kusto.data import KustoConnectionStringBuilder, KustoClient


Expand All @@ -21,75 +18,78 @@ def parse_list_from_str(s):
if single_str.strip()]


def run_cmd(cmd):
process = subprocess.Popen(
cmd.split(),
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
stdout, stderr = process.communicate()
return_code = process.returncode

if return_code != 0:
raise Exception(f'Command {cmd} execution failed, rc={return_code}, error={stderr}')
return stdout, stderr, return_code


def get_access_token():
managed_identity_id = os.environ.get("SONIC_AUTOMATION_UMI")

# 1. Run az login with re-try
az_login_cmd = f"az login --identity --username {managed_identity_id}"
az_login_attempts = 0
while az_login_attempts < MAX_GET_TOKEN_RETRY_TIMES:
try:
stdout, _, _ = run_cmd(az_login_cmd)
print(f"Az login successfully. Login time: {datetime.now(timezone.utc)}")
break
except Exception as exception:
az_login_attempts += 1
print(
f"Failed to az login with exception: {repr(exception)}. "
f"Retry {MAX_GET_TOKEN_RETRY_TIMES - az_login_attempts} times to login."
)

# If az login failed, return with exception
if az_login_attempts >= MAX_GET_TOKEN_RETRY_TIMES:
raise Exception(f"Failed to az login after {MAX_GET_TOKEN_RETRY_TIMES} attempts.")

# 2. Get access token with re-try
get_token_cmd = "az account get-access-token ---resource https://api.kusto.windows.net --query accessToken -o tsv"
get_token_attempts = 0
while get_token_attempts < MAX_GET_TOKEN_RETRY_TIMES:
try:
stdout, _, _ = run_cmd(get_token_cmd)

token = json.loads(stdout.decode("utf-8"))
access_token = token.get("accessToken", None)
if not access_token:
raise Exception("Parse token from stdout failed, accessToken is None.")

# Parse token expires time from string
token_expires_on = token.get("expiresOn", "")
if token_expires_on:
print(f"Get token successfully. Token will expire on {token_expires_on}.")

return access_token

except Exception as exception:
get_token_attempts += 1
print(f"Failed to get token with exception: {repr(exception)}.")

# If az get token failed, return with exception
if get_token_attempts >= MAX_GET_TOKEN_RETRY_TIMES:
raise Exception(f"Failed to get token after {MAX_GET_TOKEN_RETRY_TIMES} attempts")
pass
# def run_cmd(cmd):
# process = subprocess.Popen(
# cmd.split(),
# shell=False,
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE
# )
# stdout, stderr = process.communicate()
# return_code = process.returncode
#
# if return_code != 0:
# raise Exception(f'Command {cmd} execution failed, rc={return_code}, error={stderr}')
# return stdout, stderr, return_code

#
# def get_access_token():
# managed_identity_id = os.environ.get("SONIC_AUTOMATION_UMI")
# print(managed_identity_id)
#
# # 1. Run az login with re-try
# az_login_cmd = f"az login --identity --username {managed_identity_id}"
# print(az_login_cmd)
# az_login_attempts = 0
# while az_login_attempts < MAX_GET_TOKEN_RETRY_TIMES:
# try:
# os.popen(az_login_cmd)
# # print(result.read())
# print(f"Az login successfully. Login time: {datetime.now(timezone.utc)}")
# break
# except Exception as exception:
# az_login_attempts += 1
# print(
# f"Failed to az login with exception: {repr(exception)}. "
# f"Retry {MAX_GET_TOKEN_RETRY_TIMES - az_login_attempts} times to login."
# )
#
# # If az login failed, return with exception
# if az_login_attempts >= MAX_GET_TOKEN_RETRY_TIMES:
# raise Exception(f"Failed to az login after {MAX_GET_TOKEN_RETRY_TIMES} attempts.")
#
# # 2. Get access token with re-try
# get_token_cmd = "az account get-access-token ---resource https://api.kusto.windows.net --query accessToken -o tsv"
# get_token_attempts = 0
# while get_token_attempts < MAX_GET_TOKEN_RETRY_TIMES:
# try:
# result = os.popen(get_token_cmd)
# print(result.read())
# token = json.loads(result.read())
# access_token = token.get("accessToken", None)
# if not access_token:
# raise Exception("Parse token from stdout failed, accessToken is None.")
#
# # Parse token expires time from string
# token_expires_on = token.get("expiresOn", "")
# if token_expires_on:
# print(f"Get token successfully. Token will expire on {token_expires_on}.")
#
# return access_token
#
# except Exception as exception:
# get_token_attempts += 1
# print(f"Failed to get token with exception: {repr(exception)}.")
#
# # If az get token failed, return with exception
# if get_token_attempts >= MAX_GET_TOKEN_RETRY_TIMES:
# raise Exception(f"Failed to get token after {MAX_GET_TOKEN_RETRY_TIMES} attempts")
# pass


def main(scripts, topology, branch):
ingest_cluster = os.getenv("TEST_REPORT_QUERY_KUSTO_CLUSTER_BACKUP")
access_token = get_access_token()
access_token = os.getenv('ACCESS_TOKEN', None)

if not ingest_cluster or not access_token:
raise RuntimeError(
Expand Down Expand Up @@ -121,14 +121,15 @@ def main(scripts, topology, branch):
PR_CHECKER_TOPOLOGY_NAME[topology][1], script)
response = client.execute("SonicTestData", query)

average_running_time = 1800

for row in response.primary_results[0]:
# We have obtained the results of the most recent five times.
# To get the result for a single time, we need to divide by five
# If response.primary_results is None, which means where is no historical data in Kusto,
# we will use the default 1800s for a script.
average_running_time = row["sum_Runtime"] / 5
# There is no relevant records in Kusto
if average_running_time == 0:
average_running_time = 1800

total_running_time += average_running_time
scripts_running_time[script] = average_running_time
Expand Down
1 change: 0 additions & 1 deletion .azure-pipelines/impacted_area_testing/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@
}

MAX_INSTANCE_NUMBER = 25
MAX_GET_TOKEN_RETRY_TIMES = 3
26 changes: 19 additions & 7 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 All @@ -15,7 +22,6 @@ steps:
sudo apt-get install -y jq
FINAL_FEATURES=""
echo "$(DIFF_FOLDERS)"
IFS=' ' read -ra FEATURES_LIST <<< "$(DIFF_FOLDERS)"
for FEATURE in "${FEATURES_LIST[@]}"
do
Expand All @@ -25,7 +31,12 @@ steps:
break
# If changes only limited to specific feature, the scope of PR testing is impacted area.
elif [[ "$FEATURE" =~ \/tests\/.+\/.+\.py$ ]]; then
elif [[ "$FEATURE" =~ tests\/* ]]; then
# Cut the feature path
if [[ $FEATURE == */*/* ]]; then
FEATURE=$(echo "$FEATURE" | cut -d'/' -f1-2)
fi
if [[ -z "$FINAL_FEATURES" ]]; then
FINAL_FEATURES="${FEATURE#tests/}"
else
Expand All @@ -41,14 +52,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"
Loading

0 comments on commit 46f8aef

Please sign in to comment.