1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 from oslo_config import cfg
13 from doctor_tests.identity_auth import get_session
14 from doctor_tests.os_clients import keystone_client
15 from doctor_tests.os_clients import nova_client
19 cfg.StrOpt('doctor_user',
21 help='the name of test user',
23 cfg.StrOpt('doctor_passwd',
25 help='the password of test user',
27 cfg.StrOpt('doctor_project',
29 help='the name of test project',
31 cfg.StrOpt('doctor_role',
33 help='the role of test user',
35 cfg.IntOpt('quota_instances',
36 default=os.environ.get('VM_COUNT', 1),
37 help='the quota of instances in test user',
39 cfg.IntOpt('quota_cores',
40 default=os.environ.get('VM_COUNT', 1),
41 help='the quota of cores in test user',
48 def __init__(self, conf, log):
52 keystone_client(get_session())
54 nova_client(conf.nova_version, get_session())
58 self.roles_for_user = {}
59 self.roles_for_admin = {}
62 """create test user, project and etc"""
63 self.log.info('user create start......')
65 self._create_project()
68 self._add_user_role_in_project(is_admin=False)
69 self._add_user_role_in_project(is_admin=True)
71 self.log.info('user create end......')
73 def _create_project(self):
74 """create test project"""
75 self.projects = {project.name: project
76 for project in self.keystone.tenants.list()}
77 if self.conf.doctor_project not in self.projects:
79 self.keystone.tenants.create(self.conf.doctor_project)
80 self.projects[test_project.name] = test_project
82 def _create_user(self):
83 """create test user"""
84 project = self.projects.get(self.conf.doctor_project)
85 self.users = {user.name: user for user in self.keystone.users.list()}
86 if self.conf.doctor_user not in self.users:
87 test_user = self.keystone.users.create(
88 self.conf.doctor_user,
89 password=self.conf.doctor_passwd,
91 self.users[test_user.name] = test_user
93 def _create_role(self):
94 """create test role"""
95 self.roles = {role.name: role for role in self.keystone.roles.list()}
96 if self.conf.doctor_role not in self.roles:
97 test_role = self.keystone.roles.create(self.conf.doctor_role)
98 self.roles[test_role.name] = test_role
100 def _add_user_role_in_project(self, is_admin=False):
101 """add test user with test role in test project"""
102 project = self.projects.get(self.conf.doctor_project)
104 user_name = 'admin' if is_admin else self.conf.doctor_user
105 user = self.users.get(user_name)
107 role_name = 'admin' if is_admin else self.conf.doctor_role
108 role = self.roles.get(role_name)
110 roles_for_user = self.roles_for_admin \
111 if is_admin else self.roles_for_user
114 {role.name: role for role in
115 self.keystone.roles.roles_for_user(user, tenant=project)}
116 if role_name not in roles_for_user:
117 self.keystone.roles.add_user_role(user, role, tenant=project)
118 roles_for_user[role_name] = role
121 """delete the test user, project and role"""
122 self.log.info('user delete start......')
124 project = self.projects.get(self.conf.doctor_project)
125 user = self.users.get(self.conf.doctor_user)
126 role = self.roles.get(self.conf.doctor_role)
129 if 'admin' in self.roles_for_admin:
130 self.keystone.roles.remove_user_role(
136 if role and self.conf.doctor_role in self.roles_for_user:
137 self.keystone.roles.remove_user_role(
138 user, role, tenant=project)
139 self.keystone.roles.delete(role)
140 self.keystone.users.delete(user)
142 self.keystone.tenants.delete(project)
143 self.log.info('user delete end......')
145 def update_quota(self):
146 self.log.info('user quota update start......')
147 project = self.projects.get(self.conf.doctor_project)
148 user = self.users.get(self.conf.doctor_user)
151 self.quota = self.nova.quotas.get(project.id,
153 if self.conf.quota_instances > self.quota.instances:
154 self.nova.quotas.update(project.id,
155 instances=self.conf.quota_instances,
157 if self.conf.quota_cores > self.quota.cores:
158 self.nova.quotas.update(project.id,
159 cores=self.conf.quota_cores,
161 self.log.info('user quota update end......')
163 raise Exception('No project or role for update quota')