From e33b2984ce1a9954056d757b7fc0088a4b1d75a1 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Mon, 16 Mar 2020 15:46:43 +0800 Subject: [PATCH] Add create/delete tag API test case Add create/delete tag API test case Signed-off-by: Wenkai Yin --- tests/apitests/python/library/artifact.py | 13 +++ .../apitests/python/test_create_delete_tag.py | 99 +++++++++++++++++++ tests/robot-cases/Group0-BAT/API_DB.robot | 5 +- 3 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 tests/apitests/python/test_create_delete_tag.py diff --git a/tests/apitests/python/library/artifact.py b/tests/apitests/python/library/artifact.py index a671466aa..5c3f05850 100644 --- a/tests/apitests/python/library/artifact.py +++ b/tests/apitests/python/library/artifact.py @@ -10,6 +10,8 @@ class Artifact(base.Base): params = {} if "with_signature" in kwargs: params["with_signature"] = kwargs["with_signature"] + if "with_tag" in kwargs: + params["with_tag"] = kwargs["with_tag"] return client.get_artifact_with_http_info(project_name, repo_name, reference, **params ) def add_label_to_reference(self, project_name, repo_name, reference, label_id, **kwargs): @@ -31,3 +33,14 @@ class Artifact(base.Base): base._assert_status_code(expect_status_code, status_code) base._assert_status_code(201, status_code) return data + + def create_tag(self, project_name, repo_name, reference, tag_name, expect_status_code = 201, **kwargs): + client = self._get_client(**kwargs) + tag = v2_swagger_client.Tag(name = tag_name) + _, status_code, _ = client.create_tag_with_http_info(project_name, repo_name, reference, tag) + base._assert_status_code(expect_status_code, status_code) + + def delete_tag(self, project_name, repo_name, reference, tag_name, expect_status_code = 200, **kwargs): + client = self._get_client(**kwargs) + _, status_code, _ = client.delete_tag_with_http_info(project_name, repo_name, reference, tag_name) + base._assert_status_code(expect_status_code, status_code) diff --git a/tests/apitests/python/test_create_delete_tag.py b/tests/apitests/python/test_create_delete_tag.py new file mode 100644 index 000000000..2e828941c --- /dev/null +++ b/tests/apitests/python/test_create_delete_tag.py @@ -0,0 +1,99 @@ +from __future__ import absolute_import + + +import unittest + +import library.repository +import library.docker_api +from library.base import _assert_status_code +from testutils import ADMIN_CLIENT +from testutils import harbor_server + +from testutils import TEARDOWN +from library.project import Project +from library.user import User +from library.repository import Repository +from library.artifact import Artifact +from library.repository import push_image_to_project +from library.repository import pull_harbor_image + +class TestProjects(unittest.TestCase): + @classmethod + def setUpClass(self): + self.project= Project() + self.user= User() + self.artifact = Artifact(api_type='artifact') + self.repo= Repository(api_type='repository') + self.url = ADMIN_CLIENT["endpoint"] + self.user_password = "Aa123456" + self.repo_name = "hello-world" + + @classmethod + def tearDownClass(self): + print "Case completed" + + @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.") + def test_ClearData(self): + #1. Delete repository(RA,IA) by user(UA); + self.repo.delete_repoitory(TestProjects.project_name, self.repo_name, **TestProjects.USER_CLIENT) + + #2. Delete project(PA); + self.project.delete_project(TestProjects.project_id, **TestProjects.USER_CLIENT) + + #3. Delete user(UA). + self.user.delete_user(TestProjects.user_id, **ADMIN_CLIENT) + + def testCreateDeleteTag(self): + """ + Test case: + Create/Delete tag + Test step and expected result: + 1. Create a new user(UA); + 2. Create a new project(PA) by user(UA); + 3. Push an image(IA) to Harbor by docker successfully; + 4. Create a tag(1.0) for the image(IA); + 5. Get the image(latest) from Harbor successfully; + 6. Verify the image(IA) contains tag named 1.0; + 7. Delete the tag(1.0) from image(IA); + 8. Get the image(IA) from Harbor successfully; + 9. Verify the image(IA) contains no tag named 1.0; + Tear down: + 1. Delete repository(RA,IA) by user(UA); + 2. Delete project(PA); + 3. Delete user(UA). + """ + #1. Create a new user(UA); + TestProjects.user_id, user_name = self.user.create_user(user_password = self.user_password, **ADMIN_CLIENT) + + TestProjects.USER_CLIENT=dict(with_tag = True, endpoint = self.url, username = user_name, password = self.user_password) + + #2. Create a new project(PA) by user(UA); + TestProjects.project_id, TestProjects.project_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_CLIENT) + + #3. Push an image(IA) to Harbor by docker successfully; + repo_name, tag = push_image_to_project(TestProjects.project_name, harbor_server, 'admin', 'Harbor12345', self.repo_name, "latest") + + #4. Create a tag(1.0) for the image(IA) + self.artifact.create_tag(TestProjects.project_name, self.repo_name, tag, "1.0",**TestProjects.USER_CLIENT) + + #5. Get the image(IA) from Harbor successfully; + artifact = self.artifact.get_reference_info(TestProjects.project_name, self.repo_name, tag, **TestProjects.USER_CLIENT) + + #6. Verify the image(IA) contains tag named 1.0; + self.assertEqual(artifact[0].tags[0].name, tag) + self.assertEqual(artifact[0].tags[1].name, "1.0") + + #7. Delete the tag(1.0) from image(IA); + self.artifact.delete_tag(TestProjects.project_name, self.repo_name, tag, "1.0",**TestProjects.USER_CLIENT) + + #8. Get the image(latest) from Harbor successfully; + artifact = self.artifact.get_reference_info(TestProjects.project_name, self.repo_name, tag, **TestProjects.USER_CLIENT) + + #9. Verify the image(IA) contains no tag named 1.0; + self.assertEqual(artifact[0].tags[0].name, tag) + + + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/robot-cases/Group0-BAT/API_DB.robot b/tests/robot-cases/Group0-BAT/API_DB.robot index 8af75ffba..5895797fb 100644 --- a/tests/robot-cases/Group0-BAT/API_DB.robot +++ b/tests/robot-cases/Group0-BAT/API_DB.robot @@ -16,10 +16,7 @@ ${SERVER_URL} https://${SERVER} ${SERVER_API_ENDPOINT} ${SERVER_URL}/api &{SERVER_CONFIG} endpoint=${SERVER_API_ENDPOINT} verify_ssl=False -# TODO the cases commented by "###" can be uncommented after implementing the repository python library based on new API - *** Test Cases *** -# TODO uncomment this after re-implement the case Test Case - Garbage Collection Harbor API Test ./tests/apitests/python/test_garbage_collection.py Test Case - Add Private Project Member and Check User Can See It @@ -73,4 +70,6 @@ Test Case - Push Index By Docker Manifest Harbor API Test ./tests/apitests/python/test_push_chart_by_helm3_chart_cli.py Test Case - Push Cnab Bundle Harbor API Test ./tests/apitests/python/test_push_cnab_bundle.py +Test Case - Create/Delete tag + Harbor API Test ./tests/apitests/python/test_create_delete_tag.py