Debian Codegraph @

mistralclient/tests/functional/cli/base.py

raw symbols SCIP
1 # Copyright (c) 2014 Mirantis, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import os
16
17 from openstack import config as occ
18 from tempest.lib.cli import base
19
20
21 CLI_DIR = os.environ.get(
22 'OS_MISTRALCLIENT_EXEC_DIR',
23 os.path.join(os.path.abspath('.'), '.tox/functional/bin')
24 )
25
26
27 def credentials(cloud='devstack-admin'):
28 """Retrieves credentials to run functional tests
29
30 Credentials are either read via os-client-config from the environment
31 or from a config file ('clouds.yaml'). Environment variables override
32 those from the config file.
33 devstack produces a clouds.yaml with two named clouds - one named
34 'devstack' which has user privs and one named 'devstack-admin' which
35 has admin privs. This function will default to getting the devstack-admin
36 cloud as that is the current expected behavior.
37 """
38 return get_cloud_config(cloud=cloud).get_auth_args()
39
40
41 def get_cloud_config(cloud='devstack-admin'):
42 return occ.OpenStackConfig().get_one(cloud=cloud)
43
44
45 class MistralCLIAuth(base.ClientTestBase):
46
47 _mistral_url = None
48
49 def _get_admin_clients(self):
50 creds = credentials()
51
52 clients = base.CLIClient(
53 username=creds['username'],
54 password=creds['password'],
55 tenant_name=creds['project_name'],
56 project_name=creds['project_name'],
57 user_domain_id=creds['user_domain_id'],
58 project_domain_id=creds['project_domain_id'],
59 uri=creds['auth_url'],
60 cli_dir=CLI_DIR
61 )
62
63 return clients
64
65 def _get_clients(self):
66 return self._get_admin_clients()
67
68 def mistral(self, action, flags='', params='', fail_ok=False):
69 """Executes Mistral command."""
70 mistral_url_op = "--os-mistral-url %s" % self._mistral_url
71 flags = "{} --insecure".format(flags)
72
73 if 'WITHOUT_AUTH' in os.environ:
74 return base.execute(
75 'mistral %s' % mistral_url_op,
76 action,
77 flags,
78 params,
79 fail_ok,
80 merge_stderr=False,
81 cli_dir=''
82 )
83 else:
84 return self.clients.cmd_with_auth(
85 'mistral %s' % mistral_url_op,
86 action,
87 flags,
88 params,
89 fail_ok
90 )
91
92 def get_project_id(self, project_name='admin'):
93 admin_clients = self._get_clients()
94
95 projects = self.parser.listing(
96 admin_clients.openstack(
97 'project show',
98 params=project_name,
99 flags='--os-identity-api-version 3 --insecure'
100 )
101 )
102
103 return [o['Value'] for o in projects if o['Field'] == 'id'][0]
104
105
106 class MistralCLIAltAuth(base.ClientTestBase):
107
108 _mistral_url = None
109
110 def _get_alt_clients(self):
111 creds = credentials('devstack-alt-member')
112
113 clients = base.CLIClient(
114 username=creds['username'],
115 password=creds['password'],
116 project_name=creds['project_name'],
117 tenant_name=creds['project_name'],
118 user_domain_id=creds['user_domain_id'],
119 project_domain_id=creds['project_domain_id'],
120 uri=creds['auth_url'],
121 cli_dir=CLI_DIR
122 )
123
124 return clients
125
126 def _get_clients(self):
127 return self._get_alt_clients()
128
129 def mistral_alt(self, action, flags='', params='', mode='alt_user'):
130 """Executes Mistral command for alt_user from alt_tenant."""
131 mistral_url_op = "--os-mistral-url %s" % self._mistral_url
132 flags = "{} --insecure".format(flags)
133
134 return self.clients.cmd_with_auth(
135 'mistral %s' % mistral_url_op, action, flags, params)