test(quota): enable quota test case in API robot (#11039)

Signed-off-by: He Weiwei <hweiwei@vmware.com>
This commit is contained in:
He Weiwei 2020-03-12 11:50:30 +08:00 committed by GitHub
parent b597d9d59a
commit 28dcb5ad59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 46 deletions

View File

@ -1,35 +1,22 @@
from __future__ import absolute_import from __future__ import absolute_import
import unittest import unittest
from testutils import harbor_server from testutils import harbor_server, created_project, created_user
from testutils import TEARDOWN
from testutils import ADMIN_CLIENT from testutils import ADMIN_CLIENT
from library.project import Project
from library.user import User
from library.repository import Repository from library.repository import Repository
from library.repository import push_image_to_project from library.repository import push_image_to_project
from library.system import System from library.system import System
class TestProjects(unittest.TestCase): class TestProjects(unittest.TestCase):
@classmethod @classmethod
def setUp(self): def setUp(cls):
self.project= Project() cls.repo = Repository(api_type='repository')
self.user= User() cls.system = System()
self.repo= Repository()
self.system = System()
@classmethod @classmethod
def tearDown(self): def tearDown(cls):
print "Case completed" 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): def testProjectQuota(self):
""" """
Test case: Test case:
@ -40,43 +27,46 @@ class TestProjects(unittest.TestCase):
3. Add user(UA) as a member of project(PA) with project-admin role; 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; 4. Push an image to project(PA) by user(UA), then check the project quota usage;
5. Check quota change 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: Tear down:
1. Delete repository(RA) by user(UA); 1. Delete repository(RA) by user(UA);
2. Delete project(PA); 2. Delete project(PA);
3. Delete user(UA); 3. Delete user(UA);
""" """
url = ADMIN_CLIENT["endpoint"]
user_001_password = "Aa123456" user_001_password = "Aa123456"
#1. Create user-001 #1. Create a new user(UA);
TestProjects.user_test_quota_id, user_test_quota_name = self.user.create_user(user_password = user_001_password, **ADMIN_CLIENT) with created_user(user_001_password) as (user_id, user_name):
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);
#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); #5. Get project quota
TestProjects.project_test_quota_id, project_test_quota_name = self.project.create_project(metadata = {"public": "false"}, **ADMIN_CLIENT) 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; #6. Push the image with another tag to project(PA) by user(UA), the check the project quota usage; -- {"count": 1, "storage": 2791709}
self.project.add_project_members(TestProjects.project_test_quota_id, TestProjects.user_test_quota_id, **ADMIN_CLIENT) 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} #7. Get project quota
image = "goharbor/alpine" quota = self.system.get_project_quota("project", project_id, **ADMIN_CLIENT)
src_tag = "3.10" self.assertEqual(quota[0].used["count"], 1)
TestProjects.repo_name, _ = push_image_to_project(project_test_quota_name, harbor_server, user_test_quota_name, user_001_password, image, src_tag) self.assertEqual(quota[0].used["storage"], 2789002)
#5. Get project quota #8. Delete repository(RA) by user(UA);
quota = self.system.get_project_quota("project", TestProjects.project_test_quota_id, **ADMIN_CLIENT) self.repo.delete_repoitory(project_name, image, **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)
#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__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -53,3 +53,34 @@ class TestResult(object):
for each_err_msg in self.error_message: for each_err_msg in self.error_message:
print "Error message:", each_err_msg print "Error message:", each_err_msg
raise Exception(r"Test case failed with {} errors.".format(self.num_errors)) 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)

View File

@ -57,9 +57,8 @@ Test Case - Robot Account
Harbor API Test ./tests/apitests/python/test_robot_account.py Harbor API Test ./tests/apitests/python/test_robot_account.py
Test Case - Sign A Image Test Case - Sign A Image
Harbor API Test ./tests/apitests/python/test_sign_image.py Harbor API Test ./tests/apitests/python/test_sign_image.py
# TODO uncomment this after making quota work with OCI registry Test Case - Project Quota
# Test Case - Project Quota Harbor API Test ./tests/apitests/python/test_project_quota.py
# Harbor API Test ./tests/apitests/python/test_project_quota.py
Test Case - System Level CVE Whitelist Test Case - System Level CVE Whitelist
Harbor API Test ./tests/apitests/python/test_sys_cve_whitelists.py Harbor API Test ./tests/apitests/python/test_sys_cve_whitelists.py
Test Case - Project Level CVE Whitelist Test Case - Project Level CVE Whitelist