Skip to content

Commit 874dc59

Browse files
ausphamvrajeshe
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 <[email protected]> Signed-off-by: Venkata Gouri Rajesh Etla <[email protected]>
1 parent 4027f7f commit 874dc59

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
@@ -450,6 +450,31 @@ def cancel(self, test_plan_id):
450450
print(f"Result of cancelling test plan at {tp_url}:")
451451
print(str(resp["data"]))
452452

453+
def cancel_pr(self, pr_id):
454+
tp_url = f"{self.scheduler_url}/test_plan/pr/{pr_id}"
455+
cancel_url = f"{tp_url}/cancel"
456+
457+
print(f"Cancelling old PR testplans at {cancel_url}")
458+
459+
payload = json.dumps({})
460+
headers = {
461+
"Authorization": f"Bearer {self.get_token()}",
462+
"Content-Type": "application/json"
463+
}
464+
465+
raw_resp = {}
466+
try:
467+
raw_resp = requests.post(cancel_url, headers=headers, data=payload, timeout=10)
468+
resp = raw_resp.json()
469+
except Exception as exception:
470+
raise Exception(f"HTTP execute failure, url: {cancel_url}, raw_resp: {str(raw_resp)}, "
471+
f"exception: {str(exception)}")
472+
if not resp["success"]:
473+
raise Exception(f"Cancel test PR failed with error: {resp['errmsg']}")
474+
475+
print(f"Result of cancelling PR testplans at {tp_url}:")
476+
print(str(resp["data"]))
477+
453478
def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expected_result=None):
454479
print(f"Polling progress and status of test plan at {self.frontend_url}/scheduler/testplan/{test_plan_id}")
455480
print(f"Polling interval: {interval} seconds")
@@ -464,6 +489,7 @@ def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expecte
464489
start_time = time.time()
465490
poll_retry_times = 0
466491
while timeout < 0 or (time.time() - start_time) < timeout:
492+
467493
resp = None
468494
try:
469495
resp = requests.get(poll_url, headers=headers, timeout=10).json()
@@ -1041,6 +1067,7 @@ def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expecte
10411067

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

10451072
for p in [parser_cancel, parser_poll]:
10461073
p.add_argument(
@@ -1121,9 +1148,10 @@ def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expecte
11211148
env["SONIC_AUTOMATION_UMI"]
11221149
)
11231150

1151+
pr_id = os.environ.get("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER") or os.environ.get(
1152+
"SYSTEM_PULLREQUEST_PULLREQUESTID")
1153+
11241154
if args.action == "create":
1125-
pr_id = os.environ.get("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER") or os.environ.get(
1126-
"SYSTEM_PULLREQUEST_PULLREQUESTID")
11271155
build_repo_provider = os.environ.get("BUILD_REPOSITORY_PROVIDER")
11281156
build_reason = args.build_reason if args.build_reason else os.environ.get("BUILD_REASON")
11291157
build_id = os.environ.get("BUILD_BUILDID")
@@ -1211,6 +1239,8 @@ def poll(self, test_plan_id, interval=60, timeout=-1, expected_state="", expecte
12111239
tp.poll(args.test_plan_id, args.interval, args.timeout, args.expected_state, args.expected_result)
12121240
elif args.action == "cancel":
12131241
tp.cancel(args.test_plan_id)
1242+
elif args.action == "cancel_pr":
1243+
tp.cancel_pr(pr_id=pr_id)
12141244
sys.exit(0)
12151245
except PollTimeoutException as e:
12161246
print(f"Polling test plan failed with exception: {repr(e)}")

0 commit comments

Comments
 (0)