diff --git a/tests/apitests/python/test_project_quota.py b/tests/apitests/python/test_project_quota.py index 207985afe..92629ec59 100644 --- a/tests/apitests/python/test_project_quota.py +++ b/tests/apitests/python/test_project_quota.py @@ -1,35 +1,22 @@ from __future__ import absolute_import import unittest -from testutils import harbor_server -from testutils import TEARDOWN +from testutils import harbor_server, created_project, created_user 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): - self.project= Project() - self.user= User() - self.repo= Repository() - self.system = System() + def setUp(cls): + cls.repo = Repository(api_type='repository') + cls.system = System() @classmethod - def tearDown(self): + def tearDown(cls): 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: @@ -40,43 +27,46 @@ class TestProjects(unittest.TestCase): 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. + 6. Push the image with another tag to project(PA) by user(UA) + 7. Check quota not changed + 8. Delete repository(RA) by user(UA); + 9. 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) + #1. Create a new user(UA); + with created_user(user_001_password) as (user_id, user_name): + #2. Create a new private project(PA) by user(UA); + #3. Add user(UA) as a member of project(PA) with project-admin role; + with created_project(metadata={"public": "false"}, user_id=user_id) as (project_id, project_name): + #4. Push an image to project(PA) by user(UA), then check the project quota usage; -- {"count": 1, "storage": 2791709} + image, tag = "goharbor/alpine", "3.10" + push_image_to_project(project_name, harbor_server, user_name, user_001_password, image, tag) - #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) + #5. Get project quota + quota = self.system.get_project_quota("project", project_id, **ADMIN_CLIENT) + self.assertEqual(quota[0].used["count"], 1) + self.assertEqual(quota[0].used["storage"], 2789002) - #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) + #6. Push the image with another tag to project(PA) by user(UA), the check the project quota usage; -- {"count": 1, "storage": 2791709} + push_image_to_project(project_name, harbor_server, user_name, user_001_password, image, tag) - #4.Push an image to project(PA) by user(UA), then check the project quota usage; -- {"count": 1, "storage": 2791709} - image = "goharbor/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) + #7. Get project quota + quota = self.system.get_project_quota("project", project_id, **ADMIN_CLIENT) + self.assertEqual(quota[0].used["count"], 1) + self.assertEqual(quota[0].used["storage"], 2789002) - #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"], 2789002) - - #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) + #8. Delete repository(RA) by user(UA); + self.repo.delete_repoitory(project_name, image, **ADMIN_CLIENT) + #9. Quota should be 0 + quota = self.system.get_project_quota("project", project_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/apitests/python/testutils.py b/tests/apitests/python/testutils.py index 94662d906..aff96ad07 100644 --- a/tests/apitests/python/testutils.py +++ b/tests/apitests/python/testutils.py @@ -53,3 +53,34 @@ class TestResult(object): for each_err_msg in self.error_message: print "Error message:", each_err_msg raise Exception(r"Test case failed with {} errors.".format(self.num_errors)) + +from contextlib import contextmanager + +@contextmanager +def created_user(password): + from library.user import User + + api = User() + + user_id, user_name = api.create_user(user_password=password, **ADMIN_CLIENT) + try: + yield (user_id, user_name) + finally: + if TEARDOWN: + api.delete_user(user_id, **ADMIN_CLIENT) + +@contextmanager +def created_project(name=None, metadata=None, user_id=None, member_role_id=None): + from library.project import Project + + api = Project() + + project_id, project_name = api.create_project(name=None, metadata=None, **ADMIN_CLIENT) + if user_id: + api.add_project_members(project_id, user_id, member_role_id=member_role_id, **ADMIN_CLIENT) + + try: + yield (project_id, project_name) + finally: + if TEARDOWN: + api.delete_project(project_id, **ADMIN_CLIENT) diff --git a/tests/robot-cases/Group0-BAT/API_DB.robot b/tests/robot-cases/Group0-BAT/API_DB.robot index e4166e908..c770a78ba 100644 --- a/tests/robot-cases/Group0-BAT/API_DB.robot +++ b/tests/robot-cases/Group0-BAT/API_DB.robot @@ -57,9 +57,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 -# TODO uncomment this after making quota work with OCI registry -# Test Case - Project Quota -# Harbor API Test ./tests/apitests/python/test_project_quota.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