Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions .azure-pipelines/test_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,31 @@ def cancel(self, test_plan_id):
print(f"Result of cancelling test plan at {tp_url}:")
print(str(resp["data"]))

def cancel_pr(self, pr_id):
tp_url = f"{self.scheduler_url}/test_plan/pr/{pr_id}"
cancel_url = f"{tp_url}/cancel"

print(f"Cancelling old PR testplans at {cancel_url}")

payload = json.dumps({})
headers = {
"Authorization": f"Bearer {self.get_token()}",
"Content-Type": "application/json"
}

raw_resp = {}
try:
raw_resp = requests.post(cancel_url, headers=headers, data=payload, timeout=10)
resp = raw_resp.json()
except Exception as exception:
raise Exception(f"HTTP execute failure, url: {cancel_url}, raw_resp: {str(raw_resp)}, "
f"exception: {str(exception)}")
if not resp["success"]:
raise Exception(f"Cancel test PR failed with error: {resp['errmsg']}")

print(f"Result of cancelling PR testplans at {tp_url}:")
print(str(resp["data"]))

def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expected_result=None):
print(f"Polling progress and status of test plan at {self.frontend_url}/scheduler/testplan/{test_plan_id}")
print(f"Polling interval: {interval} seconds")
Expand All @@ -461,6 +486,7 @@ def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expecte
start_time = time.time()
poll_retry_times = 0
while timeout < 0 or (time.time() - start_time) < timeout:

resp = None
try:
resp = requests.get(poll_url, headers=headers, timeout=10).json()
Expand Down Expand Up @@ -1038,6 +1064,7 @@ def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expecte

parser_poll = subparsers.add_parser("poll", help="Poll test plan status.")
parser_cancel = subparsers.add_parser("cancel", help="Cancel running test plan.")
parser_cancel_pr = subparsers.add_parser("cancel_pr", help="Cancel test plans for a PR.")

for p in [parser_cancel, parser_poll]:
p.add_argument(
Expand Down Expand Up @@ -1118,9 +1145,10 @@ def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expecte
env["SONIC_AUTOMATION_UMI"]
)

pr_id = os.environ.get("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER") or os.environ.get(
"SYSTEM_PULLREQUEST_PULLREQUESTID")

if args.action == "create":
pr_id = os.environ.get("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER") or os.environ.get(
"SYSTEM_PULLREQUEST_PULLREQUESTID")
build_repo_provider = os.environ.get("BUILD_REPOSITORY_PROVIDER")
build_reason = args.build_reason if args.build_reason else os.environ.get("BUILD_REASON")
build_id = os.environ.get("BUILD_BUILDID")
Expand Down Expand Up @@ -1208,6 +1236,8 @@ def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expecte
tp.poll(args.test_plan_id, args.interval, args.timeout, args.expected_state, args.expected_result)
elif args.action == "cancel":
tp.cancel(args.test_plan_id)
elif args.action == "cancel_pr":
tp.cancel_pr(pr_id=pr_id)
sys.exit(0)
except PollTimeoutException as e:
print(f"Polling test plan failed with exception: {repr(e)}")
Expand Down
Loading