Skip to content

Commit ca19afb

Browse files
ausphammssonicbld
authored andcommitted
feat: add cancel_pr for testplan.py (sonic-net#23101)
What is the motivation for this PR? Currently, when running a new testplan, there is no safety check to cancel all the previous dangling job. This add the ability to test_plan.py to cancel all related job with PR number using cancel_pr. However this PR does not add the call, only the implementation. The real call will be implemented in sonic-net#22861 How did you do it? Added a cancel_pr method that hits Elastictest endpoint API to cancel all previous job for the same PR How did you verify/test it? Verified locally and manually using CI Signed-off-by: Austin Pham <austinpham@microsoft.com> Signed-off-by: mssonicbld <sonicbld@microsoft.com>
1 parent 9f80d60 commit ca19afb

1 file changed

Lines changed: 32 additions & 2 deletions

File tree

.azure-pipelines/test_plan.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,31 @@ def cancel(self, test_plan_id):
423423
print(f"Result of cancelling test plan at {tp_url}:")
424424
print(str(resp["data"]))
425425

426+
def cancel_pr(self, pr_id):
427+
tp_url = f"{self.scheduler_url}/test_plan/pr/{pr_id}"
428+
cancel_url = f"{tp_url}/cancel"
429+
430+
print(f"Cancelling old PR testplans at {cancel_url}")
431+
432+
payload = json.dumps({})
433+
headers = {
434+
"Authorization": f"Bearer {self.get_token()}",
435+
"Content-Type": "application/json"
436+
}
437+
438+
raw_resp = {}
439+
try:
440+
raw_resp = requests.post(cancel_url, headers=headers, data=payload, timeout=10)
441+
resp = raw_resp.json()
442+
except Exception as exception:
443+
raise Exception(f"HTTP execute failure, url: {cancel_url}, raw_resp: {str(raw_resp)}, "
444+
f"exception: {str(exception)}")
445+
if not resp["success"]:
446+
raise Exception(f"Cancel test PR failed with error: {resp['errmsg']}")
447+
448+
print(f"Result of cancelling PR testplans at {tp_url}:")
449+
print(str(resp["data"]))
450+
426451
def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expected_result=None):
427452
print(f"Polling progress and status of test plan at {self.frontend_url}/scheduler/testplan/{test_plan_id}")
428453
print(f"Polling interval: {interval} seconds")
@@ -437,6 +462,7 @@ def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expecte
437462
start_time = time.time()
438463
poll_retry_times = 0
439464
while timeout < 0 or (time.time() - start_time) < timeout:
465+
440466
resp = None
441467
try:
442468
resp = requests.get(poll_url, headers=headers, timeout=10).json()
@@ -931,6 +957,7 @@ def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expecte
931957

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

935962
for p in [parser_cancel, parser_poll]:
936963
p.add_argument(
@@ -1011,9 +1038,10 @@ def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expecte
10111038
env["SONIC_AUTOMATION_UMI"]
10121039
)
10131040

1041+
pr_id = os.environ.get("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER") or os.environ.get(
1042+
"SYSTEM_PULLREQUEST_PULLREQUESTID")
1043+
10141044
if args.action == "create":
1015-
pr_id = os.environ.get("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER") or os.environ.get(
1016-
"SYSTEM_PULLREQUEST_PULLREQUESTID")
10171045
build_repo_provider = os.environ.get("BUILD_REPOSITORY_PROVIDER")
10181046
build_reason = args.build_reason if args.build_reason else os.environ.get("BUILD_REASON")
10191047
build_id = os.environ.get("BUILD_BUILDID")
@@ -1093,6 +1121,8 @@ def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expecte
10931121
tp.poll(args.test_plan_id, args.interval, args.timeout, args.expected_state, args.expected_result)
10941122
elif args.action == "cancel":
10951123
tp.cancel(args.test_plan_id)
1124+
elif args.action == "cancel_pr":
1125+
tp.cancel_pr(pr_id=pr_id)
10961126
sys.exit(0)
10971127
except PollTimeoutException as e:
10981128
print(f"Polling test plan failed with exception: {repr(e)}")

0 commit comments

Comments
 (0)