Best Python code snippet using localstack_python
test_assign_role_to_ldap_group.py
Source:test_assign_role_to_ldap_group.py
1# coding: utf-82"""3 Harbor API4 These APIs provide services for manipulating Harbor project. 5 OpenAPI spec version: 1.4.06 7 Generated by: https://github.com/swagger-api/swagger-codegen.git8"""9from __future__ import absolute_import10import os11import sys12sys.path.append(os.environ["SWAGGER_CLIENT_PATH"])13import unittest14import testutils15import docker16import swagger_client17from swagger_client.models.project import Project 18from swagger_client.models.project_req import ProjectReq 19from swagger_client.models.project_metadata import ProjectMetadata 20from swagger_client.models.project_member import ProjectMember 21from swagger_client.models.user_group import UserGroup 22from swagger_client.models.configurations import Configurations 23from swagger_client.rest import ApiException24from pprint import pprint25#Testcase26#3-07-LDAP usergroup manage project group members27class TestAssignRoleToLdapGroup(unittest.TestCase):28 harbor_host = os.environ["HARBOR_HOST"]29 """AssignRoleToLdapGroup unit test stubs"""30 product_api = testutils.GetProductApi("admin", "Harbor12345")31 project_id = 032 docker_client = docker.from_env()33 34 def setUp(self):35 #login with admin, create a project and assign role to ldap group36 result = self.product_api.configurations_put(configurations=Configurations(ldap_filter="", ldap_group_attribute_name="cn", ldap_group_base_dn="ou=groups,dc=example,dc=com", ldap_group_search_filter="objectclass=groupOfNames", ldap_group_search_scope=2))37 pprint(result)38 cfgs = self.product_api.configurations_get()39 pprint(cfgs)40 req = ProjectReq() 41 req.project_name = "ldap_group_test_prj"42 req.metadata = ProjectMetadata(public="false")43 result = self.product_api.projects_post(req)44 pprint(result)45 projs = self.product_api.projects_get(name="ldap_group_test_prj")46 if projs.count>0 :47 project = projs[0]48 self.project_id = project.project_id49 50 # asign role to project with dn51 group_dn = "cn=harbor_admin,ou=groups,dc=example,dc=com"52 projectmember = ProjectMember()53 projectmember.role_id = 154 projectmember.member_group = UserGroup(ldap_group_dn=group_dn)55 result = self.product_api.projects_project_id_members_post( project_id=self.project_id, project_member=projectmember )56 pprint(result)57 group_dn = "cn=harbor_dev,ou=groups,dc=example,dc=com"58 projectmember = ProjectMember()59 projectmember.role_id = 260 projectmember.member_group = UserGroup(ldap_group_dn=group_dn)61 result = self.product_api.projects_project_id_members_post( project_id=self.project_id, project_member=projectmember )62 pprint(result)63 group_dn = "cn=harbor_guest,ou=groups,dc=example,dc=com"64 projectmember = ProjectMember()65 projectmember.role_id = 366 projectmember.member_group = UserGroup(ldap_group_dn=group_dn)67 result = self.product_api.projects_project_id_members_post( project_id=self.project_id, project_member=projectmember )68 pprint(result)69 pass70 def tearDown(self):71 #delete images in project72 result = self.product_api.repositories_repo_name_delete(repo_name="ldap_group_test_prj/busybox")73 pprint(result)74 result = self.product_api.repositories_repo_name_delete(repo_name="ldap_group_test_prj/busyboxdev")75 pprint(result)76 if self.project_id > 0 :77 self.product_api.projects_project_id_delete(self.project_id)78 pass79 def testAssignRoleToLdapGroup(self):80 """Test AssignRoleToLdapGroup"""81 admin_product_api = testutils.GetProductApi(username="admin_user", password="zhu88jie")82 projects = admin_product_api.projects_get(name="ldap_group_test_prj")83 self.assertTrue(projects.count > 1)84 self.assertEqual(1, projects[0].current_user_role_id)85 86 dev_product_api = testutils.GetProductApi("dev_user", "zhu88jie")87 projects = dev_product_api.projects_get(name="ldap_group_test_prj")88 self.assertTrue(projects.count > 1)89 self.assertEqual(2, projects[0].current_user_role_id)90 guest_product_api = testutils.GetProductApi("guest_user", "zhu88jie")91 projects = guest_product_api.projects_get(name="ldap_group_test_prj")92 self.assertTrue(projects.count > 1)93 self.assertEqual(3, projects[0].current_user_role_id) 94 self.dockerCmdLoginAdmin(username="admin_user", password="zhu88jie")95 self.dockerCmdLoginDev(username="dev_user", password="zhu88jie")96 self.dockerCmdLoginGuest(username="guest_user", password="zhu88jie")97 self.assertTrue(self.queryUserLogs(username="admin_user", password="zhu88jie")>0, "admin user can see logs")98 self.assertTrue(self.queryUserLogs(username="dev_user", password="zhu88jie")>0, "dev user can see logs")99 self.assertTrue(self.queryUserLogs(username="guest_user", password="zhu88jie")>0, "guest user can see logs")100 self.assertTrue(self.queryUserLogs(username="test", password="123456")==0, "test user can not see any logs")101 pass102 # admin user can push, pull images103 def dockerCmdLoginAdmin(self, username, password):104 pprint(self.docker_client.info())105 self.docker_client.login(username=username, password=password, registry=self.harbor_host) 106 self.docker_client.images.pull("busybox:latest")107 image = self.docker_client.images.get("busybox:latest")108 image.tag(repository=self.harbor_host+"/ldap_group_test_prj/busybox", tag="latest")109 output = self.docker_client.images.push(repository=self.harbor_host+"/ldap_group_test_prj/busybox", tag="latest") 110 if output.find("error")>0 :111 self.fail("Should not fail to push image for admin_user")112 self.docker_client.images.pull(repository=self.harbor_host+"/ldap_group_test_prj/busybox", tag="latest")113 pass114 # dev user can push, pull images115 def dockerCmdLoginDev(self, username, password, harbor_server=harbor_host):116 self.docker_client.login(username=username, password=password, registry=self.harbor_host) 117 self.docker_client.images.pull("busybox:latest")118 image = self.docker_client.images.get("busybox:latest")119 image.tag(repository=self.harbor_host+"/ldap_group_test_prj/busyboxdev", tag="latest")120 output = self.docker_client.images.push(repository=self.harbor_host+"/ldap_group_test_prj/busyboxdev", tag="latest") 121 if output.find("error") >0 :122 self.fail("Should not fail to push images for dev_user")123 pass124 # guest user can pull images125 def dockerCmdLoginGuest(self, username, password, harbor_server=harbor_host):126 self.docker_client.login(username=username, password=password, registry=self.harbor_host) 127 self.docker_client.images.pull("busybox:latest")128 image = self.docker_client.images.get("busybox:latest")129 image.tag(repository=self.harbor_host+"/ldap_group_test_prj/busyboxguest", tag="latest")130 output = self.docker_client.images.push(repository=self.harbor_host+"1/ldap_group_test_prj/busyboxguest", tag="latest") 131 if output.find("error")<0 :132 self.fail("Should failed to push image for guest user")133 self.docker_client.images.pull(repository=self.harbor_host+"/ldap_group_test_prj/busybox", tag="latest")134 pass 135 # check can see his log in current project136 def queryUserLogs(self, username, password, harbor_host=harbor_host):137 client_product_api = testutils.GetProductApi(username=username, password=password)138 logs = client_product_api.logs_get(repository="ldap_group_test_prj", username=username)139 if logs == None:140 return 0141 else:142 return logs.count143if __name__ == '__main__':...
conftest.py
Source:conftest.py
...12 s.mount('http://', HTTPAdapter(max_retries=retries))13 s.mount('https://', HTTPAdapter(max_retries=retries))14 return s15@pytest.fixture(scope='session')16def docker_client():17 """Docker client configured based on the host environment"""18 return docker.from_env()19@pytest.fixture(scope='session')20def image_name():21 """Image name to test"""22 return 'rholbrook/minimal-gpu-notebook'23class TrackedContainer(object):24 """Wrapper that collects docker container configuration and delays25 container creation/execution.26 Parameters27 ----------28 docker_client: docker.DockerClient29 Docker client instance30 image_name: str...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!