From d585382d196cba40bac4333d2ed5cd60c8eb13b5 Mon Sep 17 00:00:00 2001 From: AllForNothing Date: Wed, 6 Dec 2023 10:25:48 +0800 Subject: [PATCH] Update the script for tag-retention APIs test case 1. Make the code logic stronger Signed-off-by: AllForNothing --- .../python/test_project_permission.py | 89 ++++++++++--------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/tests/apitests/python/test_project_permission.py b/tests/apitests/python/test_project_permission.py index e15a9f6c3126..a6b24cd18fb1 100644 --- a/tests/apitests/python/test_project_permission.py +++ b/tests/apitests/python/test_project_permission.py @@ -5,7 +5,7 @@ import urllib3 import os -admin_name = os.environ.get("ADMIN_NAME") +admin_name = os.environ.get("ADMIN_USER_NAME") admin_password = os.environ.get("ADMIN_PASSWORD") user_name = os.environ.get("USER_NAME") password = os.environ.get("PASSWORD") @@ -257,45 +257,50 @@ def call(self): } -def get_retention_id() -> str: - # create retention rule fist - # this request can be failed(retention rule existed) or succeeded, but we can finally get the retention id - requests.request("POST", "{}/retentions".format(harbor_base_url), - data=json.dumps(tag_retention_rule_payload), verify=False, - auth=(admin_name, admin_password), headers={"Content-Type": "application/json"}) - response1 = requests.request("GET", "{}/projects/{}/metadatas/retention_id".format(harbor_base_url, project_id), - data=None, verify=False, - auth=(admin_name, admin_password), headers={"Content-Type": "application/json"}) - retention_id = project_id - if "retention_id" in json.loads(response1.text): - retention_id = json.loads(response1.text)["retention_id"] - return retention_id - - -# because get_retention_id() has been called, so the expected status code is 400 -create_tag_retention_rule = Permission("{}/retentions".format(harbor_base_url), "POST", - 400, - tag_retention_rule_payload) - -update_retention_payload = copy.deepcopy(tag_retention_rule_payload) -update_retention_payload["rules"][0]["disabled"] = True -read_tag_retention = Permission("{}/retentions/{}".format(harbor_base_url, get_retention_id()), "GET", 200) -update_tag_retention = Permission("{}/retentions/{}".format(harbor_base_url, get_retention_id()), "PUT", 200, - update_retention_payload) -delete_tag_retention = Permission("{}/retentions/{}".format(harbor_base_url, get_retention_id()), "DELETE", 200) -execute_tag_retention = Permission("{}/retentions/{}/executions".format(harbor_base_url, get_retention_id()), "POST", - 201) -list_tag_retention_execution = Permission("{}/retentions/{}/executions".format(harbor_base_url, get_retention_id()), - "GET", - 200) -stop_tag_retention = Permission("{}/retentions/{}/executions/0".format(harbor_base_url, get_retention_id()), "PATCH", - 404, - {"action": "stop"}) -list_tag_retention_tasks = Permission("{}/retentions/{}/executions/0/tasks".format(harbor_base_url, get_retention_id()), - "GET", 404) -read_tag_retention_tasks = Permission( - "{}/retentions/{}/executions/0/tasks/0".format(harbor_base_url, get_retention_id()), - "GET", 404) +def init_tag_retention(): + if resource == "tag-retention": + # create retention rule first + # this request can be failed(retention rule existed) or succeeded, but we can finally get the retention id + requests.request("POST", "{}/retentions".format(harbor_base_url), + data=json.dumps(tag_retention_rule_payload), verify=False, + auth=(admin_name, admin_password), headers={"Content-Type": "application/json"}) + get_retention_response = requests.request("GET", "{}/projects/{}/metadatas/retention_id".format(harbor_base_url, + project_id), + data=None, verify=False, + auth=(admin_name, admin_password), + headers={"Content-Type": "application/json"}) + retention_id = None + if "retention_id" in json.loads(get_retention_response.text): + retention_id = json.loads(get_retention_response.text)["retention_id"] + # because the retention rule exists, so the expected status code is 400 + create_tag_retention_rule = Permission("{}/retentions".format(harbor_base_url, retention_id), "POST", + 400, + tag_retention_rule_payload) + + update_retention_payload = copy.deepcopy(tag_retention_rule_payload) + update_retention_payload["rules"][0]["disabled"] = True + read_tag_retention = Permission("{}/retentions/{}".format(harbor_base_url, retention_id), "GET", 200) + update_tag_retention = Permission("{}/retentions/{}".format(harbor_base_url, retention_id), "PUT", 200, + update_retention_payload) + delete_tag_retention = Permission("{}/retentions/{}".format(harbor_base_url, retention_id), "DELETE", 200) + execute_tag_retention = Permission("{}/retentions/{}/executions".format(harbor_base_url, retention_id), "POST", + 201) + list_tag_retention_execution = Permission("{}/retentions/{}/executions".format(harbor_base_url, retention_id), + "GET", + 200) + stop_tag_retention = Permission("{}/retentions/{}/executions/0".format(harbor_base_url, retention_id), "PATCH", + 404, + {"action": "stop"}) + list_tag_retention_tasks = Permission( + "{}/retentions/{}/executions/0/tasks".format(harbor_base_url, retention_id), + "GET", 404) + read_tag_retention_tasks = Permission( + "{}/retentions/{}/executions/0/tasks/0".format(harbor_base_url, retention_id), + "GET", 404) + return [create_tag_retention_rule, read_tag_retention, update_tag_retention, execute_tag_retention, + list_tag_retention_execution, stop_tag_retention, list_tag_retention_tasks, + read_tag_retention_tasks, delete_tag_retention] + # 15. Resource log actions: ['list'] list_log = Permission("{}/projects/{}/logs".format(harbor_base_url, project_name), "GET", 200) @@ -355,9 +360,7 @@ def get_retention_id() -> str: delete_preheat_policy], "immutable-tag": [create_immutable_tag_rule, list_immutable_tag_rule, update_immutable_tag_rule, delete_immutable_tag_rule], - "tag-retention": [create_tag_retention_rule, read_tag_retention, update_tag_retention, execute_tag_retention, - list_tag_retention_execution, stop_tag_retention, list_tag_retention_tasks, - read_tag_retention_tasks, delete_tag_retention], + "tag-retention": init_tag_retention(), "log": [list_log], "notification-policy": [create_webhook, list_webhook, read_webhook, update_webhook, delete_webhook, list_webhook_executions, list_webhook_executions_tasks, read_webhook_executions_tasks,