Add tese case of manage project memebers and modify (#6304)

Move admin_client to testutil.py as global parameter.
Change create_repository function name to push_image_to_project to make it reasonable.
Signed-off-by: danfengliu <danfengl@vmware.com>
This commit is contained in:
danfengliu 2018-11-20 14:24:13 +08:00 committed by GitHub
parent 9a46d6b3ac
commit 5ceebcbb10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 229 additions and 69 deletions

View File

@ -4,6 +4,17 @@ import base
import swagger_client import swagger_client
from swagger_client.rest import ApiException from swagger_client.rest import ApiException
def is_member_exist_in_project(members, member_user_name, expected_member_role_id = None):
result = False
for member in members:
if member.entity_name == member_user_name:
if expected_member_role_id != None:
if member.role_id == expected_member_role_id:
return True
else:
return True
return result
class Project(base.Base): class Project(base.Base):
def create_project(self, name=None, metadata=None, expect_status_code = 201, expect_response_body = None, **kwargs): def create_project(self, name=None, metadata=None, expect_status_code = 201, expect_response_body = None, **kwargs):
if name is None: if name is None:
@ -49,10 +60,16 @@ class Project(base.Base):
404: False, 404: False,
}.get(status_code,'error') }.get(status_code,'error')
def get_project(self, project_id, **kwargs): def get_project(self, project_id, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs) client = self._get_client(**kwargs)
data, status_code, _ = client.projects_project_id_get_with_http_info(project_id) try:
base._assert_status_code(200, status_code) data, status_code, _ = client.projects_project_id_get_with_http_info(project_id)
except ApiException as e:
if e.status == expect_status_code:
return e.reason, e.body
else:
raise Exception(r"Get project result is not as expected {} actual status is {}.".format(expect_status_code, e.status))
base._assert_status_code(expect_status_code, status_code)
return data return data
def update_project(self, project_id, metadata, **kwargs): def update_project(self, project_id, metadata, **kwargs):
@ -80,12 +97,47 @@ class Project(base.Base):
}.get(meta_name,'error') }.get(meta_name,'error')
def get_project_members(self, project_id, **kwargs): def get_project_members(self, project_id, **kwargs):
client = self._get_client(**kwargs)
return client.projects_project_id_members_get(project_id)
def get_project_member(self, project_id, member_id, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs) client = self._get_client(**kwargs)
data = [] data = []
data, status_code, _ = client.projects_project_id_members_get_with_http_info(project_id) try:
base._assert_status_code(200, status_code) data, status_code, _ = client.projects_project_id_members_mid_get_with_http_info(project_id, member_id,)
except ApiException as e:
if e.status == expect_status_code:
return e.reason, e.body
else:
raise Exception(r"Get project member result is not as expected {} actual status is {}.".format(expect_status_code, e.status))
base._assert_status_code(expect_status_code, status_code)
return data return data
def check_project_member_not_exist(self, project_id, member_user_name, **kwargs):
members = self.get_project_members(project_id, **kwargs)
result = is_member_exist_in_project(list(members), member_user_name)
if result == True:
raise Exception(r"User {} should not be a member of project with ID {}.".format(member_user_name, project_id))
def check_project_members_exist(self, project_id, member_user_name, expected_member_role_id = None, **kwargs):
members = self.get_project_members(project_id, **kwargs)
result = is_member_exist_in_project(members, member_user_name, expected_member_role_id = expected_member_role_id)
if result == False:
raise Exception(r"User {} should be a member of project with ID {}.".format(member_user_name, project_id))
def update_project_member_role(self, project_id, member_id, member_role_id, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs)
role = swagger_client.Role(role_id = member_role_id)
data = []
data, status_code, _ = client.projects_project_id_members_mid_put_with_http_info(project_id, member_id, role = role)
base._assert_status_code(expect_status_code, status_code)
return data
def delete_project_member(self, project_id, member_id, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs)
_, status_code, _ = client.projects_project_id_members_mid_delete_with_http_info(project_id, member_id)
base._assert_status_code(expect_status_code, status_code)
def add_project_members(self, project_id, user_id, member_role_id = None, expect_status_code = 201, **kwargs): def add_project_members(self, project_id, user_id, member_role_id = None, expect_status_code = 201, **kwargs):
if member_role_id is None: if member_role_id is None:
member_role_id = 1 member_role_id = 1
@ -93,6 +145,7 @@ class Project(base.Base):
projectMember = swagger_client.ProjectMember(member_role_id, member_user = _member_user) projectMember = swagger_client.ProjectMember(member_role_id, member_user = _member_user)
client = self._get_client(**kwargs) client = self._get_client(**kwargs)
data = [] data = []
data, status_code, _ = client.projects_project_id_members_post_with_http_info(project_id, project_member = projectMember) data, status_code, header = client.projects_project_id_members_post_with_http_info(project_id, project_member = projectMember)
base._assert_status_code(201, status_code) base._assert_status_code(201, status_code)
return data return base._get_id_from_header(header)

View File

@ -5,7 +5,7 @@ import base
import swagger_client import swagger_client
from docker_api import DockerAPI from docker_api import DockerAPI
def create_repository(project_name, registry, username, password, image, tag): def push_image_to_project(project_name, registry, username, password, image, tag):
_docker_api = DockerAPI() _docker_api = DockerAPI()
_docker_api.docker_login(registry, username, password) _docker_api.docker_login(registry, username, password)
time.sleep(2) time.sleep(2)

View File

@ -3,7 +3,7 @@ from __future__ import absolute_import
import unittest import unittest
from testutils import CLIENT from testutils import ADMIN_CLIENT
from library.project import Project from library.project import Project
from library.user import User from library.user import User
@ -31,12 +31,9 @@ class TestProjects(unittest.TestCase):
4. Login harbor as admin, then to add user(UA) in project(PA); 4. Login harbor as admin, then to add user(UA) in project(PA);
5. Login harbor as user(UA), then to get all private project, there must be project(PA) only. 5. Login harbor as user(UA), then to get all private project, there must be project(PA) only.
""" """
url = CLIENT["endpoint"] url = ADMIN_CLIENT["endpoint"]
user_001_password = "Aa123456" user_001_password = "Aa123456"
admin_user = "admin"
admin_pwd = "Harbor12345"
ADMIN_CLIENT=dict(endpoint = url, username = admin_user, password = admin_pwd)
#1. Create user-001 #1. Create user-001
user_001_id, user_001_name = self.user.create_user(user_password = user_001_password, **ADMIN_CLIENT) user_001_id, user_001_name = self.user.create_user(user_password = user_001_password, **ADMIN_CLIENT)
self.assertNotEqual(user_001_id, None, msg="Failed to create user, return user is {}".format(user_001_id)) self.assertNotEqual(user_001_id, None, msg="Failed to create user, return user is {}".format(user_001_id))

View File

@ -1,7 +1,7 @@
from __future__ import absolute_import from __future__ import absolute_import
import unittest import unittest
from testutils import CLIENT from testutils import ADMIN_CLIENT
from testutils import TEARDOWN from testutils import TEARDOWN
from library.project import Project from library.project import Project
from library.user import User from library.user import User
@ -32,16 +32,16 @@ class TestProjects(unittest.TestCase):
def test_ClearData(self): def test_ClearData(self):
#1. Delete rule(RA); #1. Delete rule(RA);
for rule_id in TestProjects.rule_id_list: for rule_id in TestProjects.rule_id_list:
self.replication.delete_replication_rule(rule_id, **TestProjects.ADMIN_CLIENT) self.replication.delete_replication_rule(rule_id, **ADMIN_CLIENT)
#2. Delete target(TA); #2. Delete target(TA);
self.target.delete_target(TestProjects.target_id, **TestProjects.ADMIN_CLIENT) self.target.delete_target(TestProjects.target_id, **ADMIN_CLIENT)
#3. Delete project(PA); #3. Delete project(PA);
self.project.delete_project(TestProjects.project_add_rule_id, **TestProjects.USER_add_rule_CLIENT) self.project.delete_project(TestProjects.project_add_rule_id, **TestProjects.USER_add_rule_CLIENT)
#4. Delete user(UA); #4. Delete user(UA);
self.user.delete_user(TestProjects.user_add_rule_id, **TestProjects.ADMIN_CLIENT) self.user.delete_user(TestProjects.user_add_rule_id, **ADMIN_CLIENT)
def testAddSysLabelToRepo(self): def testAddSysLabelToRepo(self):
""" """
@ -59,14 +59,11 @@ class TestProjects(unittest.TestCase):
3. Delete project(PA); 3. Delete project(PA);
4. Delete user(UA). 4. Delete user(UA).
""" """
admin_user = "admin" url = ADMIN_CLIENT["endpoint"]
admin_pwd = "Harbor12345"
url = CLIENT["endpoint"]
user_add_rule_password = "Aa123456" user_add_rule_password = "Aa123456"
TestProjects.ADMIN_CLIENT=dict(endpoint = url, username = admin_user, password = admin_pwd)
#1. Create user(UA) #1. Create user(UA)
TestProjects.user_add_rule_id, user_add_rule_name = self.user.create_user_success(user_password = user_add_rule_password, **TestProjects.ADMIN_CLIENT) TestProjects.user_add_rule_id, user_add_rule_name = self.user.create_user_success(user_password = user_add_rule_password, **ADMIN_CLIENT)
TestProjects.USER_add_rule_CLIENT=dict(endpoint = url, username = user_add_rule_name, password = user_add_rule_password) TestProjects.USER_add_rule_CLIENT=dict(endpoint = url, username = user_add_rule_name, password = user_add_rule_password)
@ -78,7 +75,7 @@ class TestProjects(unittest.TestCase):
expected_project_id = TestProjects.project_add_rule_id, **TestProjects.USER_add_rule_CLIENT) expected_project_id = TestProjects.project_add_rule_id, **TestProjects.USER_add_rule_CLIENT)
#3. Create a new target(TA)/registry #3. Create a new target(TA)/registry
TestProjects.target_id, _ = self.target.create_target(**TestProjects.ADMIN_CLIENT) TestProjects.target_id, _ = self.target.create_target(**ADMIN_CLIENT)
print "TestProjects.target_id:", TestProjects.target_id print "TestProjects.target_id:", TestProjects.target_id
TestProjects.rule_id_list = [] TestProjects.rule_id_list = []
@ -87,11 +84,11 @@ class TestProjects(unittest.TestCase):
for value in trigger_values_to_set: for value in trigger_values_to_set:
#4. Create a new rule for project(PA) and target(TA) #4. Create a new rule for project(PA) and target(TA)
rule_id, rule_name = self.replication.create_replication_rule([TestProjects.project_add_rule_id], rule_id, rule_name = self.replication.create_replication_rule([TestProjects.project_add_rule_id],
[TestProjects.target_id], trigger=swagger_client.RepTrigger(kind=value), **TestProjects.ADMIN_CLIENT) [TestProjects.target_id], trigger=swagger_client.RepTrigger(kind=value), **ADMIN_CLIENT)
TestProjects.rule_id_list.append(rule_id) TestProjects.rule_id_list.append(rule_id)
#5. Check rule should be exist #5. Check rule should be exist
self.replication.check_replication_rule_should_exist(rule_id, rule_name, expect_trigger = value, **TestProjects.ADMIN_CLIENT) self.replication.check_replication_rule_should_exist(rule_id, rule_name, expect_trigger = value, **ADMIN_CLIENT)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,13 +1,14 @@
from __future__ import absolute_import from __future__ import absolute_import
import unittest import unittest
from testutils import CLIENT
from testutils import harbor_server from testutils import harbor_server
from testutils import TEARDOWN from testutils import TEARDOWN
from testutils import ADMIN_CLIENT
from library.project import Project from library.project import Project
from library.user import User from library.user import User
from library.repository import Repository from library.repository import Repository
from library.repository import create_repository from library.repository import push_image_to_project
from library.label import Label from library.label import Label
class TestProjects(unittest.TestCase): class TestProjects(unittest.TestCase):
@ -38,10 +39,10 @@ class TestProjects(unittest.TestCase):
self.project.delete_project(TestProjects.project_add_g_lbl_id, **TestProjects.USER_add_g_lbl_CLIENT) self.project.delete_project(TestProjects.project_add_g_lbl_id, **TestProjects.USER_add_g_lbl_CLIENT)
#3. Delete user(UA); #3. Delete user(UA);
self.user.delete_user(TestProjects.user_add_g_lbl_id, **TestProjects.ADMIN_CLIENT) self.user.delete_user(TestProjects.user_add_g_lbl_id, **ADMIN_CLIENT)
#4. Delete label(LA). #4. Delete label(LA).
self.label.delete_label(TestProjects.label_id, **TestProjects.ADMIN_CLIENT) self.label.delete_label(TestProjects.label_id, **ADMIN_CLIENT)
def testAddSysLabelToRepo(self): def testAddSysLabelToRepo(self):
""" """
@ -61,32 +62,29 @@ class TestProjects(unittest.TestCase):
3. Delete user(UA); 3. Delete user(UA);
4. Delete label(LA). 4. Delete label(LA).
""" """
admin_user = "admin" url = ADMIN_CLIENT["endpoint"]
admin_pwd = "Harbor12345"
url = CLIENT["endpoint"]
user_001_password = "Aa123456" user_001_password = "Aa123456"
TestProjects.ADMIN_CLIENT=dict(endpoint = url, username = admin_user, password = admin_pwd)
#1. Create user-001 #1. Create user-001
TestProjects.user_add_g_lbl_id, user_add_g_lbl_name = self.user.create_user_success(user_password = user_001_password, **TestProjects.ADMIN_CLIENT) TestProjects.user_add_g_lbl_id, user_add_g_lbl_name = self.user.create_user_success(user_password = user_001_password, **ADMIN_CLIENT)
TestProjects.USER_add_g_lbl_CLIENT=dict(endpoint = url, username = user_add_g_lbl_name, password = user_001_password) TestProjects.USER_add_g_lbl_CLIENT=dict(endpoint = url, username = user_add_g_lbl_name, password = user_001_password)
#2. Create private project-001 #2. Create private project-001
TestProjects.project_add_g_lbl_id, project_add_g_lbl_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.ADMIN_CLIENT) TestProjects.project_add_g_lbl_id, project_add_g_lbl_name = self.project.create_project(metadata = {"public": "false"}, **ADMIN_CLIENT)
#3. Add user-001 as a member of project-001 with project-admin role #3. Add user-001 as a member of project-001 with project-admin role
self.project.add_project_members(TestProjects.project_add_g_lbl_id, TestProjects.user_add_g_lbl_id, **TestProjects.ADMIN_CLIENT) self.project.add_project_members(TestProjects.project_add_g_lbl_id, TestProjects.user_add_g_lbl_id, **ADMIN_CLIENT)
#4. Get private project of user(UA), user(UA) can see only one private project which is project(PA); #4. Get private project of user(UA), user(UA) can see only one private project which is project(PA);
self.project.projects_should_exist(dict(public=False), expected_count = 1, self.project.projects_should_exist(dict(public=False), expected_count = 1,
expected_project_id = TestProjects.project_add_g_lbl_id, **TestProjects.USER_add_g_lbl_CLIENT) expected_project_id = TestProjects.project_add_g_lbl_id, **TestProjects.USER_add_g_lbl_CLIENT)
#5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA); #5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA);
TestProjects.repo_name, tag = create_repository(project_add_g_lbl_name, harbor_server, user_add_g_lbl_name, user_001_password, "hello-world", "latest") TestProjects.repo_name, tag = push_image_to_project(project_add_g_lbl_name, harbor_server, user_add_g_lbl_name, user_001_password, "hello-world", "latest")
#6. Create a new label(LA) in project(PA) by admin; #6. Create a new label(LA) in project(PA) by admin;
TestProjects.label_id, _ = self.label.create_label(**TestProjects.ADMIN_CLIENT) TestProjects.label_id, _ = self.label.create_label(**ADMIN_CLIENT)
#7. Add this system global label to repository(RA)/tag(TA). #7. Add this system global label to repository(RA)/tag(TA).
self.repo.add_label_to_tag(TestProjects.repo_name, tag, int(TestProjects.label_id), **TestProjects.USER_add_g_lbl_CLIENT) self.repo.add_label_to_tag(TestProjects.repo_name, tag, int(TestProjects.label_id), **TestProjects.USER_add_g_lbl_CLIENT)

View File

@ -4,14 +4,14 @@ from __future__ import absolute_import
import unittest import unittest
from library.base import _assert_status_code from library.base import _assert_status_code
from testutils import CLIENT from testutils import ADMIN_CLIENT
from testutils import harbor_server from testutils import harbor_server
from testutils import TEARDOWN from testutils import TEARDOWN
from library.project import Project from library.project import Project
from library.user import User from library.user import User
from library.repository import Repository from library.repository import Repository
from library.repository import create_repository from library.repository import push_image_to_project
class TestProjects(unittest.TestCase): class TestProjects(unittest.TestCase):
@classmethod @classmethod
@ -36,7 +36,7 @@ class TestProjects(unittest.TestCase):
self.project.delete_project(TestProjects.project_del_repo_id, **TestProjects.USER_del_repo_CLIENT) self.project.delete_project(TestProjects.project_del_repo_id, **TestProjects.USER_del_repo_CLIENT)
#2. Delete user(UA). #2. Delete user(UA).
self.user.delete_user(TestProjects.user_del_repo_id, **TestProjects.ADMIN_CLIENT) self.user.delete_user(TestProjects.user_del_repo_id, **ADMIN_CLIENT)
def testDelRepo(self): def testDelRepo(self):
""" """
@ -53,14 +53,11 @@ class TestProjects(unittest.TestCase):
1. Delete project(PA); 1. Delete project(PA);
2. Delete user(UA). 2. Delete user(UA).
""" """
admin_user = "admin" url = ADMIN_CLIENT["endpoint"]
admin_pwd = "Harbor12345"
url = CLIENT["endpoint"]
user_del_repo_password = "Aa123456" user_del_repo_password = "Aa123456"
TestProjects.ADMIN_CLIENT=dict(endpoint = url, username = admin_user, password = admin_pwd)
#1. Create a new user(UA); #1. Create a new user(UA);
TestProjects.user_del_repo_id, user_del_repo_name = self.user.create_user_success(user_password = user_del_repo_password, **TestProjects.ADMIN_CLIENT) TestProjects.user_del_repo_id, user_del_repo_name = self.user.create_user_success(user_password = user_del_repo_password, **ADMIN_CLIENT)
TestProjects.USER_del_repo_CLIENT=dict(endpoint = url, username = user_del_repo_name, password = user_del_repo_password) TestProjects.USER_del_repo_CLIENT=dict(endpoint = url, username = user_del_repo_name, password = user_del_repo_password)
@ -68,7 +65,7 @@ class TestProjects(unittest.TestCase):
TestProjects.project_del_repo_id, project_del_repo_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_del_repo_CLIENT) TestProjects.project_del_repo_id, project_del_repo_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_del_repo_CLIENT)
#3. Create a new repository(RA) in project(PA) by user(UA); #3. Create a new repository(RA) in project(PA) by user(UA);
repo_name, _ = create_repository(project_del_repo_name, harbor_server, 'admin', 'Harbor12345', "hello-world", "latest") repo_name, _ = push_image_to_project(project_del_repo_name, harbor_server, 'admin', 'Harbor12345', "hello-world", "latest")
#4. Get repository in project(PA), there should be one repository which was created by user(UA); #4. Get repository in project(PA), there should be one repository which was created by user(UA);
repo_data = self.repo.get_repository(TestProjects.project_del_repo_id, **TestProjects.USER_del_repo_CLIENT) repo_data = self.repo.get_repository(TestProjects.project_del_repo_id, **TestProjects.USER_del_repo_CLIENT)

View File

@ -1,7 +1,7 @@
from __future__ import absolute_import from __future__ import absolute_import
import unittest import unittest
from testutils import CLIENT from testutils import ADMIN_CLIENT
from testutils import TEARDOWN from testutils import TEARDOWN
from library.project import Project from library.project import Project
from library.user import User from library.user import User
@ -30,7 +30,7 @@ class TestProjects(unittest.TestCase):
self.project.delete_project(TestProjects.project_edit_project_creation_id, **TestProjects.USER_edit_project_creation_CLIENT) self.project.delete_project(TestProjects.project_edit_project_creation_id, **TestProjects.USER_edit_project_creation_CLIENT)
#2. Delete user(UA); #2. Delete user(UA);
self.user.delete_user(TestProjects.user_edit_project_creation_id, **TestProjects.ADMIN_CLIENT) self.user.delete_user(TestProjects.user_edit_project_creation_id, **ADMIN_CLIENT)
def testEditProjectCreation(self): def testEditProjectCreation(self):
""" """
@ -46,26 +46,23 @@ class TestProjects(unittest.TestCase):
1. Delete project(PA); 1. Delete project(PA);
2. Delete user(UA); 2. Delete user(UA);
""" """
admin_user = "admin" url = ADMIN_CLIENT["endpoint"]
admin_pwd = "Harbor12345"
url = CLIENT["endpoint"]
user_edit_project_creation_password = "Aa123456" user_edit_project_creation_password = "Aa123456"
TestProjects.ADMIN_CLIENT=dict(endpoint = url, username = admin_user, password = admin_pwd)
#1. Create a new user(UA); #1. Create a new user(UA);
TestProjects.user_edit_project_creation_id, user_edit_project_creation_name = self.user.create_user_success(user_password = user_edit_project_creation_password, **TestProjects.ADMIN_CLIENT) TestProjects.user_edit_project_creation_id, user_edit_project_creation_name = self.user.create_user_success(user_password = user_edit_project_creation_password, **ADMIN_CLIENT)
TestProjects.USER_edit_project_creation_CLIENT=dict(endpoint = url, username = user_edit_project_creation_name, password = user_edit_project_creation_password) TestProjects.USER_edit_project_creation_CLIENT=dict(endpoint = url, username = user_edit_project_creation_name, password = user_edit_project_creation_password)
#2. Set project creation to "admin only"; #2. Set project creation to "admin only";
self.conf.set_configurations_of_project_creation_restriction_success("adminonly", **TestProjects.ADMIN_CLIENT) self.conf.set_configurations_of_project_creation_restriction_success("adminonly", **ADMIN_CLIENT)
#3. Create a new project(PA) by user(UA), and fail to create a new project; #3. Create a new project(PA) by user(UA), and fail to create a new project;
self.project.create_project(metadata = {"public": "false"}, expect_status_code = 403, self.project.create_project(metadata = {"public": "false"}, expect_status_code = 403,
expect_response_body = "Only system admin can create project", **TestProjects.USER_edit_project_creation_CLIENT) expect_response_body = "Only system admin can create project", **TestProjects.USER_edit_project_creation_CLIENT)
#4. Set project creation to "everyone"; #4. Set project creation to "everyone";
self.conf.set_configurations_of_project_creation_restriction_success("everyone", **TestProjects.ADMIN_CLIENT) self.conf.set_configurations_of_project_creation_restriction_success("everyone", **ADMIN_CLIENT)
#5. Create a new project(PA) by user(UA), success to create a project. #5. Create a new project(PA) by user(UA), success to create a project.
TestProjects.project_edit_project_creation_id, _ = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_edit_project_creation_CLIENT) TestProjects.project_edit_project_creation_id, _ = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_edit_project_creation_CLIENT)

View File

@ -0,0 +1,118 @@
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 push_image_to_project
from library.repository import Repository
class TestProjects(unittest.TestCase):
@classmethod
def setUp(self):
project = Project()
self.project= project
user = User()
self.user= user
repo = Repository()
self.repo= repo
@classmethod
def tearDown(self):
print "Case completed"
@unittest.skipIf(TEARDOWN == False, "Test data should be remain in the harbor.")
def test_ClearData(self):
#1. Delete repository(RA) by user(UA);
self.repo.delete_repoitory(TestProjects.repo_name, **ADMIN_CLIENT)
#3. Delete project(PA);
self.project.delete_project(TestProjects.project_alice_id, **ADMIN_CLIENT)
#4. Delete user(UA);
self.user.delete_user(TestProjects.user_alice_id, **ADMIN_CLIENT)
self.user.delete_user(TestProjects.user_bob_id, **ADMIN_CLIENT)
self.user.delete_user(TestProjects.user_carol_id, **ADMIN_CLIENT)
def testManageProjectMember(self):
"""
Test case:
Manage Project members
Test step and Expectation:
1. Create user Alice, Bob, Carol;
2. Create private project(PA) by Alice, Add a repository to project(PA) by Alice;
3. Bob is not a member of project(PA);
4. Alice Add Bob as a guest member of project(PA), Check Bob is a guest member of project(PA);
5. Update role of Bob to developer of project(PA), Check Bob is developer member of project(PA);
6. Update role of Bob to admin member of project(PA), Check Bob is admin member of project(PA);
7. Bob add Carol to project(PA) as a guest member, Carol is a member of project(PA) as a guest;
8. Alice delete Bob from project(PA),
Bob is no longer a member of project(PA) and Bob can see project(PA),
Carol is still a member of project(PA) as a guest.
"""
url = ADMIN_CLIENT["endpoint"]
user_alice_password = "Aa123456"
user_bob_password = "Test1@34"
user_carol_password = "Test1@34"
#1.1 Create user Alice
TestProjects.user_alice_id, user_alice_name = self.user.create_user(user_password = user_alice_password, **ADMIN_CLIENT)
USER_ALICE_CLIENT=dict(endpoint = url, username = user_alice_name, password = user_alice_password)
#1.2 Create user Bob
TestProjects.user_bob_id, user_bob_name = self.user.create_user(user_password = user_bob_password, **ADMIN_CLIENT)
USER_BOB_CLIENT=dict(endpoint = url, username = user_bob_name, password = user_bob_password)
#1.3 Create user Carol
TestProjects.user_carol_id, user_carol_name = self.user.create_user(user_password = user_carol_password, **ADMIN_CLIENT)
#2.1 Create private project(PA) by Alice
TestProjects.project_alice_id, project_alice_name = self.project.create_project(metadata = {"public": "false"}, **USER_ALICE_CLIENT)
#2.2 Add a repository to project(PA) by Alice
TestProjects.repo_name, _ = push_image_to_project(project_alice_name, harbor_server, user_alice_name, user_alice_password, "hello-world", "latest")
#3. Bob is not a member of project(PA);
self.project.check_project_member_not_exist(TestProjects.project_alice_id, user_bob_name, **USER_ALICE_CLIENT)
#4.1 Alice Add Bob as a guest member of project(PA)
member_id_bob = self.project.add_project_members(TestProjects.project_alice_id, TestProjects.user_bob_id, member_role_id = 3, **USER_ALICE_CLIENT)
#4.2 Check Bob is a guest member of project(PA)
self.project.check_project_members_exist(TestProjects.project_alice_id, user_bob_name, expected_member_role_id = 3, user_name = user_bob_name, user_password = user_bob_password, **USER_ALICE_CLIENT)
#5.1 Update role of Bob to developer of project(PA)
self.project.update_project_member_role(TestProjects.project_alice_id, member_id_bob, 2, **USER_ALICE_CLIENT)
#5.2 Check Bob is developer member of project(PA)
self.project.check_project_members_exist(TestProjects.project_alice_id, user_bob_name, expected_member_role_id = 2, user_name = user_bob_name, user_password = user_bob_password, **USER_ALICE_CLIENT)
#6.1 Update role of Bob to admin member of project(PA)
self.project.update_project_member_role(TestProjects.project_alice_id, member_id_bob, 1, **USER_ALICE_CLIENT)
#6.2 Check Bob is admin member of project(PA)
self.project.check_project_members_exist(TestProjects.project_alice_id, user_bob_name, expected_member_role_id = 1, user_name = user_bob_name, user_password = user_bob_password, **USER_ALICE_CLIENT)
#7.1 Bob add Carol to project(PA) as a guest member.
self.project.add_project_members(TestProjects.project_alice_id, TestProjects.user_carol_id, member_role_id = 3, **USER_BOB_CLIENT)
#7.2 Carol is a member of project(PA) as a guest.
self.project.check_project_members_exist(TestProjects.project_alice_id, user_carol_name, expected_member_role_id = 3, user_name = user_carol_name, user_password = user_carol_password, **USER_ALICE_CLIENT)
#8.1 Alice delete Bob from project(PA).
self.project.delete_project_member(TestProjects.project_alice_id, member_id_bob, **USER_ALICE_CLIENT)
#8.2 Bob is no longer a member of project(PA) and Bob can see project(PA).
self.project.check_project_member_not_exist(TestProjects.project_alice_id, user_bob_name, **USER_ALICE_CLIENT)
#8.3 Carol is still a member of project(PA) as a guest.
self.project.check_project_members_exist(TestProjects.project_alice_id, user_carol_name, expected_member_role_id = 3, user_name = user_carol_name, user_password = user_carol_password, **USER_ALICE_CLIENT)
if __name__ == '__main__':
unittest.main()

View File

@ -1,13 +1,13 @@
from __future__ import absolute_import from __future__ import absolute_import
import unittest import unittest
from testutils import CLIENT
from testutils import harbor_server from testutils import harbor_server
from testutils import TEARDOWN from testutils import TEARDOWN
from testutils import ADMIN_CLIENT
from library.project import Project from library.project import Project
from library.user import User from library.user import User
from library.repository import Repository from library.repository import Repository
from library.repository import create_repository from library.repository import push_image_to_project
class TestProjects(unittest.TestCase): class TestProjects(unittest.TestCase):
@classmethod @classmethod
@ -34,7 +34,7 @@ class TestProjects(unittest.TestCase):
self.project.delete_project(TestProjects.project_scan_image_id, **TestProjects.USER_scan_image_CLIENT) self.project.delete_project(TestProjects.project_scan_image_id, **TestProjects.USER_scan_image_CLIENT)
#3. Delete user(UA); #3. Delete user(UA);
self.user.delete_user(TestProjects.user_scan_image_id, **TestProjects.ADMIN_CLIENT) self.user.delete_user(TestProjects.user_scan_image_id, **ADMIN_CLIENT)
def testScanImage(self): def testScanImage(self):
""" """
@ -52,22 +52,19 @@ class TestProjects(unittest.TestCase):
2. Delete project(PA); 2. Delete project(PA);
3. Delete user(UA); 3. Delete user(UA);
""" """
admin_user = "admin" url = ADMIN_CLIENT["endpoint"]
admin_pwd = "Harbor12345"
url = CLIENT["endpoint"]
user_001_password = "Aa123456" user_001_password = "Aa123456"
TestProjects.ADMIN_CLIENT=dict(endpoint = url, username = admin_user, password = admin_pwd)
#1. Create user-001 #1. Create user-001
TestProjects.user_scan_image_id, user_scan_image_name = self.user.create_user_success(user_password = user_001_password, **TestProjects.ADMIN_CLIENT) TestProjects.user_scan_image_id, user_scan_image_name = self.user.create_user_success(user_password = user_001_password, **ADMIN_CLIENT)
TestProjects.USER_scan_image_CLIENT=dict(endpoint = url, username = user_scan_image_name, password = user_001_password) TestProjects.USER_scan_image_CLIENT=dict(endpoint = url, username = user_scan_image_name, password = user_001_password)
#2. Create a new private project(PA) by user(UA); #2. Create a new private project(PA) by user(UA);
TestProjects.project_scan_image_id, project_scan_image_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.ADMIN_CLIENT) TestProjects.project_scan_image_id, project_scan_image_name = self.project.create_project(metadata = {"public": "false"}, **ADMIN_CLIENT)
#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;
self.project.add_project_members(TestProjects.project_scan_image_id, TestProjects.user_scan_image_id, **TestProjects.ADMIN_CLIENT) self.project.add_project_members(TestProjects.project_scan_image_id, TestProjects.user_scan_image_id, **ADMIN_CLIENT)
#4. Get private project of user(UA), user(UA) can see only one private project which is project(PA); #4. Get private project of user(UA), user(UA) can see only one private project which is project(PA);
self.project.projects_should_exist(dict(public=False), expected_count = 1, self.project.projects_should_exist(dict(public=False), expected_count = 1,
@ -79,7 +76,7 @@ class TestProjects(unittest.TestCase):
image = "pypy" image = "pypy"
src_tag = "latest" src_tag = "latest"
#5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA); #5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA);
TestProjects.repo_name, tag = create_repository(project_scan_image_name, harbor_server, user_scan_image_name, user_001_password, image, src_tag) TestProjects.repo_name, tag = push_image_to_project(project_scan_image_name, harbor_server, user_scan_image_name, user_001_password, image, src_tag)
#6. Send scan image command and get tag(TA) infomation to check scan result, it should be finished; #6. Send scan image command and get tag(TA) infomation to check scan result, it should be finished;
self.repo.scan_not_scanned_image_success(TestProjects.repo_name, tag, **TestProjects.USER_scan_image_CLIENT) self.repo.scan_not_scanned_image_success(TestProjects.repo_name, tag, **TestProjects.USER_scan_image_CLIENT)

View File

@ -7,8 +7,12 @@ from swagger_client.rest import ApiException
import swagger_client.models import swagger_client.models
from pprint import pprint from pprint import pprint
admin_user = "admin"
admin_pwd = "Harbor12345"
harbor_server = os.environ["HARBOR_HOST"] harbor_server = os.environ["HARBOR_HOST"]
CLIENT=dict(endpoint="https://"+harbor_server+"/api") #CLIENT=dict(endpoint="https://"+harbor_server+"/api")
ADMIN_CLIENT=dict(endpoint = "https://"+harbor_server+"/api", username = admin_user, password = admin_pwd)
USER_ROLE=dict(admin=0,normal=1) USER_ROLE=dict(admin=0,normal=1)
TEARDOWN = True TEARDOWN = True

View File

@ -28,4 +28,6 @@ Test Case - Add Replication Rule
Test Case - Edit Project Creation Test Case - Edit Project Creation
Harbor API Test ./tests/apitests/python/test_edit_project_creation.py Harbor API Test ./tests/apitests/python/test_edit_project_creation.py
Test Case - Scan Image Test Case - Scan Image
Harbor API Test ./tests/apitests/python/test_scan_image.py Harbor API Test ./tests/apitests/python/test_scan_image.py
Test Case - Manage Project Member
Harbor API Test ./tests/apitests/python/test_manage_project_member.py