From 6b5fd36bb35e3cc1c6ae7ffe817a1199662ad62f Mon Sep 17 00:00:00 2001 From: Wang Yan Date: Wed, 11 Sep 2019 13:34:23 +0800 Subject: [PATCH] add e2e test case for project quota 1. Create a new user(UA); 2. Create a new private project(PA) by user(UA); 3. Add user(UA) as a member of project(PA) with project-admin role; 4. Push an image to project(PA) by user(UA), then check the project quota usage; 5. Check quota change 6. Delete image, the quota should be changed to 0. Signed-off-by: wang yan --- docs/swagger.yaml | 1 - tests/apitests/python/library/docker_api.py | 2 +- tests/apitests/python/library/system.py | 10 +++ tests/apitests/python/test_project_quota.py | 88 +++++++++++++++++++++ tests/robot-cases/Group0-BAT/API_DB.robot | 2 + 5 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 tests/apitests/python/test_project_quota.py diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 687f92d15..83cb717eb 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -3600,7 +3600,6 @@ paths: description: List quotas tags: - Products - - Quota parameters: - name: reference in: query diff --git a/tests/apitests/python/library/docker_api.py b/tests/apitests/python/library/docker_api.py index aa5a0e0db..715799a4c 100644 --- a/tests/apitests/python/library/docker_api.py +++ b/tests/apitests/python/library/docker_api.py @@ -25,7 +25,7 @@ class DockerAPI(object): if str(err).lower().find(expected_error_message.lower()) < 0: raise Exception(r"Docker login: Return message {} is not as expected {}".format(str(err), expected_error_message)) else: - raise Exception(r" Docker login {} failed, error is [{}]".format (image, err.message)) + raise Exception(r" Docker login failed, error is [{}]".format (err.message)) def docker_image_pull(self, image, tag = None, expected_error_message = None): if tag is not None: diff --git a/tests/apitests/python/library/system.py b/tests/apitests/python/library/system.py index 570d277e3..c67ad5434 100644 --- a/tests/apitests/python/library/system.py +++ b/tests/apitests/python/library/system.py @@ -182,3 +182,13 @@ class System(base.Base): def get_cve_whitelist(self, **kwargs): client = self._get_client(**kwargs) return client.system_cve_whitelist_get() + + def get_project_quota(self, reference, reference_id, **kwargs): + params={} + params['reference'] = reference + params['reference_id'] = reference_id + + client = self._get_client(**kwargs) + data, status_code, _ = client.quotas_get_with_http_info(**params) + base._assert_status_code(200, status_code) + return data \ No newline at end of file diff --git a/tests/apitests/python/test_project_quota.py b/tests/apitests/python/test_project_quota.py new file mode 100644 index 000000000..8ab8c842b --- /dev/null +++ b/tests/apitests/python/test_project_quota.py @@ -0,0 +1,88 @@ +from __future__ import absolute_import +import unittest + +from testutils import harbor_server +from testutils import TEARDOWN +from testutils import ADMIN_CLIENT +from library.project import Project +from library.user import User +from library.repository import Repository +from library.repository import push_image_to_project +from library.system import System + +class TestProjects(unittest.TestCase): + @classmethod + def setUp(self): + project = Project() + self.project= project + + user = User() + self.user= user + + repo = Repository() + self.repo= repo + + self.system = System() + + @classmethod + def tearDown(self): + print "Case completed" + + @unittest.skipIf(TEARDOWN == False, "Test data won't be erased.") + def test_ClearData(self): + #1. Delete project(PA); + self.project.delete_project(TestProjects.project_test_quota_id, **ADMIN_CLIENT) + + #2. Delete user(UA); + self.user.delete_user(TestProjects.user_test_quota_id, **ADMIN_CLIENT) + + def testProjectQuota(self): + """ + Test case: + Project Quota + Test step and expected result: + 1. Create a new user(UA); + 2. Create a new private project(PA) by user(UA); + 3. Add user(UA) as a member of project(PA) with project-admin role; + 4. Push an image to project(PA) by user(UA), then check the project quota usage; + 5. Check quota change + 6. Delete image, the quota should be changed to 0. + Tear down: + 1. Delete repository(RA) by user(UA); + 2. Delete project(PA); + 3. Delete user(UA); + """ + url = ADMIN_CLIENT["endpoint"] + user_001_password = "Aa123456" + + #1. Create user-001 + TestProjects.user_test_quota_id, user_test_quota_name = self.user.create_user(user_password = user_001_password, **ADMIN_CLIENT) + TestProjects.USER_TEST_QUOTA_CLIENT=dict(endpoint = url, username = user_test_quota_name, password = user_001_password) + + #2. Create a new private project(PA) by user(UA); + TestProjects.project_test_quota_id, project_test_quota_name = self.project.create_project(metadata = {"public": "false"}, **ADMIN_CLIENT) + + #3. Add user(UA) as a member of project(PA) with project-admin role; + self.project.add_project_members(TestProjects.project_test_quota_id, TestProjects.user_test_quota_id, **ADMIN_CLIENT) + + #4.Push an image to project(PA) by user(UA), then check the project quota usage; -- {"count": 1, "storage": 2791709} + image = "alpine" + src_tag = "3.10" + TestProjects.repo_name, _ = push_image_to_project(project_test_quota_name, harbor_server, user_test_quota_name, user_001_password, image, src_tag) + + #5. Get project quota + quota = self.system.get_project_quota("project", TestProjects.project_test_quota_id, **ADMIN_CLIENT) + self.assertEqual(quota[0].used["count"], 1) + self.assertEqual(quota[0].used["storage"], 2791709) + + #6. Delete repository(RA) by user(UA); + self.repo.delete_repoitory(TestProjects.repo_name, **ADMIN_CLIENT) + + #6. Quota should be 0 + quota = self.system.get_project_quota("project", TestProjects.project_test_quota_id, **ADMIN_CLIENT) + self.assertEqual(quota[0].used["count"], 0) + self.assertEqual(quota[0].used["storage"], 0) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/tests/robot-cases/Group0-BAT/API_DB.robot b/tests/robot-cases/Group0-BAT/API_DB.robot index 0324e476c..c2ee1d071 100644 --- a/tests/robot-cases/Group0-BAT/API_DB.robot +++ b/tests/robot-cases/Group0-BAT/API_DB.robot @@ -49,6 +49,8 @@ Test Case - Robot Account Harbor API Test ./tests/apitests/python/test_robot_account.py Test Case - Sign A Image Harbor API Test ./tests/apitests/python/test_sign_image.py +Test Case - Project Quota + Harbor API Test ./tests/apitests/python/test_project_quota.py Test Case - System Level CVE Whitelist Harbor API Test ./tests/apitests/python/test_sys_cve_whitelists.py Test Case - Project Level CVE Whitelist