bugfix: fix the filename of ssh tunnel
[doctor.git] / doctor_tests / user.py
1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
3 #
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 ##############################################################################
9 import os
10
11 from oslo_config import cfg
12
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
16 from keystoneclient import exceptions as ks_exceptions
17
18
19 OPTS = [
20     cfg.StrOpt('doctor_user',
21                default='doctor',
22                help='the name of test user',
23                required=True),
24     cfg.StrOpt('doctor_passwd',
25                default='doctor',
26                help='the password of test user',
27                required=True),
28     cfg.StrOpt('doctor_project',
29                default='doctor',
30                help='the name of test project',
31                required=True),
32     cfg.StrOpt('doctor_role',
33                default='_member_',
34                help='the role of test user',
35                required=True),
36     cfg.StrOpt('doctor_domain_id',
37                default=os.environ.get('OS_PROJECT_DOMAIN_ID', 'default'),
38                help='the domain id of the doctor project',
39                required=True),
40     cfg.IntOpt('quota_instances',
41                default=os.environ.get('VM_COUNT', 1),
42                help='the quota of instances in test user',
43                required=True),
44     cfg.IntOpt('quota_cores',
45                default=os.environ.get('VM_COUNT', 1),
46                help='the quota of cores in test user',
47                required=True),
48 ]
49
50
51 class User(object):
52
53     def __init__(self, conf, log):
54         self.conf = conf
55         self.log = log
56         self.keystone = keystone_client(
57             self.conf.keystone_version, get_session())
58         self.nova = \
59             nova_client(conf.nova_version, get_session())
60         self.users = {}
61         self.projects = {}
62         self.roles = {}
63         self.use_exist_role = False
64         self.roles_for_user = {}
65         self.roles_for_admin = {}
66
67     def create(self):
68         """create test user, project and etc"""
69         self.log.info('user create start......')
70
71         self._create_project()
72         self._create_user()
73         self._create_role()
74         self._add_user_role_in_project(is_admin=False)
75         self._add_user_role_in_project(is_admin=True)
76
77         self.log.info('user create end......')
78
79     def _create_project(self):
80         """create test project"""
81         self.projects = {project.name: project for project in
82                          self.keystone.projects.list(
83                              domain=self.conf.doctor_domain_id)}
84         if self.conf.doctor_project not in self.projects:
85             self.log.info('create project......')
86             test_project = \
87                 self.keystone.projects.create(
88                     self.conf.doctor_project,
89                     self.conf.doctor_domain_id)
90             self.projects[test_project.name] = test_project
91         else:
92             self.log.info('project %s already created......'
93                           % self.conf.doctor_project)
94         self.log.info('test project %s'
95                       % str(self.projects[self.conf.doctor_project]))
96
97     def _create_user(self):
98         """create test user"""
99         self.users = {user.name: user for user in
100                       self.keystone.users.list(
101                           domain=self.conf.doctor_domain_id)}
102         if self.conf.doctor_user not in self.users:
103             self.log.info('create user......')
104             test_user = self.keystone.users.create(
105                 self.conf.doctor_user,
106                 password=self.conf.doctor_passwd,
107                 domain=self.conf.doctor_domain_id)
108             self.users[test_user.name] = test_user
109         else:
110             self.log.info('user %s already created......'
111                           % self.conf.doctor_user)
112         self.log.info('test user %s'
113                       % str(self.users[self.conf.doctor_user]))
114
115     def _create_role(self):
116         """create test role"""
117         self.roles = {role.name: role for role in
118                       self.keystone.roles.list()}
119         if self.conf.doctor_role not in self.roles:
120             self.log.info('create role......')
121             test_role = self.keystone.roles.create(
122                 self.conf.doctor_role)
123             self.roles[test_role.name] = test_role
124         else:
125             self.use_exist_role = True
126             self.log.info('role %s already created......'
127                           % self.conf.doctor_role)
128         self.log.info('test role %s' % str(self.roles[self.conf.doctor_role]))
129
130     def _add_user_role_in_project(self, is_admin=False):
131         """add test user with test role in test project"""
132
133         project = self.projects.get(self.conf.doctor_project)
134
135         user_name = 'admin' if is_admin else self.conf.doctor_user
136         user = self.users.get(user_name)
137
138         role_name = 'admin' if is_admin else self.conf.doctor_role
139         role = self.roles.get(role_name)
140
141         roles_for_user = self.roles_for_admin \
142             if is_admin else self.roles_for_user
143
144         try:
145             self.keystone.roles.check(role, user=user, project=project)
146             self.log.info('Already grant a role:%s to user: %s on'
147                           ' project: %s'
148                           % (role_name, user_name,
149                              self.conf.doctor_project))
150         except ks_exceptions.NotFound:
151             self.keystone.roles.grant(role, user=user, project=project)
152             roles_for_user[role_name] = role
153
154     def delete(self):
155         """delete the test user, project and role"""
156         self.log.info('user delete start......')
157
158         project = self.projects.get(self.conf.doctor_project)
159         user = self.users.get(self.conf.doctor_user)
160         role = self.roles.get(self.conf.doctor_role)
161
162         if project:
163             if 'admin' in self.roles_for_admin:
164                 self.keystone.roles.revoke(
165                     self.roles['admin'],
166                     user=self.users['admin'],
167                     project=project)
168
169             if user:
170                 if role and self.conf.doctor_role in self.roles_for_user:
171                     self.keystone.roles.revoke(
172                         role, user=user, project=project)
173                     if not self.use_exist_role:
174                         self.keystone.roles.delete(role)
175                 self.keystone.users.delete(user)
176
177             self.keystone.projects.delete(project)
178         self.log.info('user delete end......')
179
180     def update_quota(self):
181         self.log.info('user quota update start......')
182         project = self.projects.get(self.conf.doctor_project)
183         user = self.users.get(self.conf.doctor_user)
184
185         if project and user:
186             self.quota = self.nova.quotas.get(project.id,
187                                               user_id=user.id)
188             if self.conf.quota_instances > self.quota.instances:
189                 self.nova.quotas.update(
190                     project.id,
191                     instances=self.conf.quota_instances,
192                     user_id=user.id)
193             if self.conf.quota_cores > self.quota.cores:
194                 self.nova.quotas.update(project.id,
195                                         cores=self.conf.quota_cores,
196                                         user_id=user.id)
197             self.log.info('user quota update end......')
198         else:
199             raise Exception('No project or role for update quota')