-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
283 lines (226 loc) · 8.26 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Copyright 2023 IBM Corp. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Utility functions for end2end tests.
"""
import os
import re
import subprocess
import random
import warnings
import pytest
import zhmcclient
# Prefix used for names of resources that are created during tests
TEST_PREFIX = 'zhmcclient_tests_end2end'
def env2bool(name):
"""
Evaluate the (string) value of the specified environment variable as a
boolean value and return the result as a bool.
The following variable values are considered True: 'true', 'yes', '1'.
Any other value or if the variable is not set, is considered False.
"""
return os.getenv(name, 'false').lower() in ('true', 'yes', '1')
class End2endTestWarning(UserWarning):
"""
Python warning indicating an issue with an end2end test.
"""
pass
def zhmc_session_args(hmc_definition): # noqa: F811
# pylint: disable=redefined-outer-name
"""
Return the session related command line options for zhmc from an HMC
definition.
Parameters:
hmc_definition(zhmcclient.testutils.HMCDefiniton): HMC definition.
Returns:
list of str: zhmc session related command line options.
"""
ret = []
if not hmc_definition.verify:
ret.append('-n')
ret.extend(['-h', hmc_definition.host])
ret.extend(['-u', hmc_definition.userid])
ret.extend(['-p', hmc_definition.password])
if hmc_definition.ca_certs:
ret.extend(['-c', hmc_definition.ca_certs])
return ret
def create_hmc_session(hmc_definition): # noqa: F811
# pylint: disable=redefined-outer-name
"""
Create a valid HMC session.
Returns the session ID of the HMC session.
Raises zhmcclient exceptions if the HMC logon fails.
"""
host = hmc_definition.host
userid = hmc_definition.userid
password = hmc_definition.password
if not hmc_definition.verify:
verify_cert = False
elif hmc_definition.ca_certs:
verify_cert = hmc_definition.ca_certs
else:
verify_cert = True
session = zhmcclient.Session(host, userid, password,
verify_cert=verify_cert)
session.logon()
return session.session_id
def delete_hmc_session(hmc_definition, session_id):
"""
Delete a valid HMC session.
Raises zhmcclient exceptions if the session ID is not valid.
"""
host = hmc_definition.host
userid = hmc_definition.userid
if not hmc_definition.verify:
verify_cert = False
elif hmc_definition.ca_certs:
verify_cert = hmc_definition.ca_certs
else:
verify_cert = True
session = zhmcclient.Session(host, userid, session_id=session_id,
verify_cert=verify_cert)
session.logoff()
def is_valid_hmc_session(hmc_definition, session_id):
"""
Return a boolean indicating whether an HMC session is valid.
Raises zhmcclient exceptions if the validity cannot be determined.
"""
host = hmc_definition.host
userid = hmc_definition.userid
if not hmc_definition.verify:
verify_cert = False
elif hmc_definition.ca_certs:
verify_cert = hmc_definition.ca_certs
else:
verify_cert = True
session = zhmcclient.Session(host, userid, session_id=session_id,
verify_cert=verify_cert)
try:
# This simply performs the GET with the session header set to the
# session_id.
session.get('/api/cpcs', logon_required=False, renew_session=False)
except zhmcclient.ServerAuthError as exc:
if re.search(r'x-api-session header did not map to a known session',
str(exc)):
return False
raise
return True
def run_zhmc(args, env=None, pdb_=False, log=False):
"""
Run the zhmc command and return its exit code, stdout and stderr.
Parameters:
args(list/tuple of str): List of command line arguments, not including
the 'zhmc' command name itself.
env(dict of str/str): Environment variables to be used instead of the
current process' variables.
pdb_(bool): If True, debug the zhmc command. This is done by inserting
the '--pdb' option to the command arguments, and by not capturing the
stdout/stderr.
If both log and pdb_ are set, only pdb_ is performed.
log(bool): If True, enable HMC logging for the zhmc command. This is done
by inserting the '--log hmc=debug' option to the command arguments,
and by not capturing the stdout/stderr.
If both log and pdb_ are set, only pdb_ is performed.
Returns:
tuple (rc, stdout, stderr) as follows:
- rc(int): zhmc exit code
- stdout(str): stdout string, or None if debugging the zhmc command
- stderr(str): stderr string, or None if debugging the zhmc command
"""
assert isinstance(args, (list, tuple))
if env is not None:
assert isinstance(env, dict)
p_args = ['zhmc'] + args
# Set up output capturing, dependent on pdb_ flag
if pdb_:
kwargs = {}
p_args.insert(1, '--pdb')
else:
if log:
p_args.insert(1, 'all=debug')
p_args.insert(1, '--log')
kwargs = {
'stdout': subprocess.PIPE,
'stderr': subprocess.PIPE,
}
# pylint: disable=consider-using-with
proc = subprocess.Popen(p_args, env=env, **kwargs)
stdout, stderr = proc.communicate()
rc = proc.returncode
if not pdb_:
stdout = stdout.decode()
stderr = stderr.decode()
return rc, stdout, stderr
def _res_name(item):
"""Return the resource name, used by pick_test_resources()"""
if isinstance(item, (tuple, list)):
return item[0].name
return item.name
def pick_test_resources(res_list):
"""
Return the list of resources to be tested.
The env.var "TESTRESOURCES" controls which resources are picked for the
test, as follows:
* 'random': (default) one random choice from the input list of resources.
* 'all': the complete input list of resources.
* '<pattern>': The resources with names matching the regexp pattern.
Parameters:
res_list (list of zhmcclient.BaseResource or tuple thereof):
List of resources to pick from. Tuple items are a resource and its
parent resources.
Returns:
list of zhmcclient.BaseResource: Picked list of resources.
"""
test_res = os.getenv('TESTRESOURCES', 'random')
if test_res == 'random':
return [random.choice(res_list)] # nosec: B311
if test_res == 'all':
return sorted(res_list, key=_res_name)
# match the pattern
ret_list = []
for item in res_list:
if re.search(test_res, _res_name(item)):
ret_list.append(item)
return sorted(ret_list, key=_res_name)
def skipif_no_storage_mgmt_feature(cpc):
"""
Skip the test if the "DPM Storage Management" feature is not enabled for
the specified CPC, or if the CPC does not yet support it.
"""
try:
smf = cpc.feature_enabled('dpm-storage-management')
except ValueError:
smf = False
if not smf:
skip_warn("DPM Storage Mgmt feature not enabled or not supported "
"on CPC {c}".format(c=cpc.name))
def skipif_storage_mgmt_feature(cpc):
"""
Skip the test if the "DPM Storage Management" feature is enabled for
the specified CPC.
"""
try:
smf = cpc.feature_enabled('dpm-storage-management')
except ValueError:
smf = False
if smf:
skip_warn("DPM Storage Mgmt feature enabled on CPC {c}".
format(c=cpc.name))
def skip_warn(msg):
"""
Issue an End2endTestWarning and skip the current pytest testcase with the
specified message.
"""
warnings.warn(msg, End2endTestWarning, stacklevel=2)
pytest.skip(msg)