forked from openstack-charmers/zaza-openstack-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
298 lines (264 loc) · 11.1 KB
/
tests.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python3
# Copyright 2019 Canonical Ltd.
#
# 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.
"""Encapsulate Cinder testing."""
import logging
import zaza.model
import zaza.openstack.charm_tests.test_utils as test_utils
import zaza.openstack.utilities.openstack as openstack_utils
import zaza.openstack.charm_tests.glance.setup as glance_setup
from tenacity import (
Retrying,
stop_after_attempt,
wait_exponential,
)
class CinderTests(test_utils.OpenStackBaseTest):
"""Encapsulate Cinder tests."""
RESOURCE_PREFIX = 'zaza-cindertests'
@classmethod
def setUpClass(cls):
"""Run class setup for running tests."""
super(CinderTests, cls).setUpClass(application_name='cinder')
cls.application_name = 'cinder'
cls.lead_unit = zaza.model.get_lead_unit_name(
"cinder", model_name=cls.model_name)
cls.cinder_client = openstack_utils.get_cinder_session_client(
cls.keystone_session)
cls.nova_client = openstack_utils.get_nova_session_client(
cls.keystone_session)
@classmethod
def tearDown(cls):
"""Remove test resources."""
logging.info('Running teardown')
for attempt in Retrying(
stop=stop_after_attempt(8),
wait=wait_exponential(multiplier=1, min=2, max=60)):
with attempt:
volumes = list(cls.cinder_client.volumes.list())
snapped_volumes = [v for v in volumes
if v.name.endswith("-from-snap")]
if snapped_volumes:
logging.info("Removing volumes from snapshot")
cls._remove_volumes(snapped_volumes)
volumes = list(cls.cinder_client.volumes.list())
snapshots = list(cls.cinder_client.volume_snapshots.list())
if snapshots:
logging.info("tearDown - snapshots: {}".format(
", ".join(s.name for s in snapshots)))
cls._remove_snapshots(snapshots)
if volumes:
logging.info("tearDown - volumes: {}".format(
", ".join(v.name for v in volumes)))
cls._remove_volumes(volumes)
@classmethod
def _remove_snapshots(cls, snapshots):
"""Remove snapshots passed as param.
:param volumes: the snapshots to delete
:type volumes: List[snapshot objects]
"""
for snapshot in snapshots:
if snapshot.name.startswith(cls.RESOURCE_PREFIX):
logging.info("removing snapshot: {}".format(snapshot.name))
try:
openstack_utils.delete_resource(
cls.cinder_client.volume_snapshots,
snapshot.id,
msg="snapshot")
except Exception as e:
logging.error("error removing snapshot: {}".format(str(e)))
raise
@classmethod
def _remove_volumes(cls, volumes):
"""Remove volumes passed as param.
:param volumes: the volumes to delete
:type volumes: List[volume objects]
"""
for volume in volumes:
if volume.name.startswith(cls.RESOURCE_PREFIX):
logging.info("removing volume: {}".format(volume.name))
try:
openstack_utils.delete_resource(
cls.cinder_client.volumes,
volume.id,
msg="volume")
except Exception as e:
logging.error("error removing volume: {}".format(str(e)))
raise
def test_100_volume_create_extend_delete(self):
"""Test creating, extending a volume."""
vol_new = self.cinder_client.volumes.create(
name='{}-100-vol'.format(self.RESOURCE_PREFIX),
size='1')
openstack_utils.resource_reaches_status(
self.cinder_client.volumes,
vol_new.id,
wait_iteration_max_time=1200,
stop_after_attempt=20,
expected_status="available",
msg="Volume status wait")
self.cinder_client.volumes.extend(
vol_new.id,
'2')
openstack_utils.resource_reaches_status(
self.cinder_client.volumes,
vol_new.id,
wait_iteration_max_time=1200,
stop_after_attempt=20,
expected_status="available",
msg="Volume status wait")
def test_105_volume_create_from_img(self):
"""Test creating a volume from an image."""
logging.debug("finding image {} ..."
.format(glance_setup.LTS_IMAGE_NAME))
image = self.nova_client.glance.find_image(
glance_setup.LTS_IMAGE_NAME)
logging.debug("using cinder_client to create volume from image {}"
.format(image.id))
vol_img = self.cinder_client.volumes.create(
name='{}-105-vol-from-img'.format(self.RESOURCE_PREFIX),
size=3,
imageRef=image.id)
logging.debug("now waiting for volume {} to reach available"
.format(vol_img.id))
openstack_utils.resource_reaches_status(
self.cinder_client.volumes,
vol_img.id,
wait_iteration_max_time=1200,
stop_after_attempt=20,
expected_status="available",
msg="Volume status wait")
def test_110_volume_snap_clone(self):
"""Test creating snapshot and build a volume from it."""
# Create a 1GB
vol_new = self.cinder_client.volumes.create(
name='{}-110-vol'.format(self.RESOURCE_PREFIX),
size='1')
openstack_utils.resource_reaches_status(
self.cinder_client.volumes,
vol_new.id,
wait_iteration_max_time=1200,
stop_after_attempt=20,
expected_status="available",
msg="Volume status wait")
# Snapshot the volume
snap_new = self.cinder_client.volume_snapshots.create(
volume_id=vol_new.id,
name='{}-110-snap'.format(self.RESOURCE_PREFIX))
openstack_utils.resource_reaches_status(
self.cinder_client.volume_snapshots,
snap_new.id,
wait_iteration_max_time=1200,
stop_after_attempt=20,
expected_status="available",
msg="Volume status wait")
# Create a volume from the snapshot
vol_from_snap = self.cinder_client.volumes.create(
name='{}-110-vol-from-snap'.format(self.RESOURCE_PREFIX),
size=snap_new.size,
snapshot_id=snap_new.id)
openstack_utils.resource_reaches_status(
self.cinder_client.volumes,
vol_from_snap.id,
wait_iteration_max_time=1200,
stop_after_attempt=20,
expected_status="available",
msg="Volume status wait")
def test_120_volume_force_delete(self):
"""Test force deleting a volume."""
vol_new = self.cinder_client.volumes.create(
name='{}-120-vol'.format(self.RESOURCE_PREFIX),
size='1')
openstack_utils.resource_reaches_status(
self.cinder_client.volumes,
vol_new.id,
wait_iteration_max_time=1200,
stop_after_attempt=20,
expected_status="available",
msg="Volume status wait")
vol_new.force_delete()
openstack_utils.resource_removed(
self.cinder_client.volumes,
vol_new.id,
msg="Volume")
@property
def services(self):
"""Return a list services for the selected OpenStack release."""
services = ['cinder-scheduler', 'cinder-volume']
if (openstack_utils.get_os_release() >=
openstack_utils.get_os_release('xenial_ocata')):
services.append('apache2')
else:
services.append('cinder-api')
return services
def test_900_restart_on_config_change(self):
"""Checking restart happens on config change.
Change debug mode and assert that change propagates to the correct
file and that services are restarted as a result
"""
# Config file affected by juju set config change
conf_file = '/etc/cinder/cinder.conf'
# Make config change, check for service restarts
logging.debug('Setting debug mode...')
self.restart_on_changed_debug_oslo_config_file(
conf_file,
self.services)
def test_901_pause_resume(self):
"""Run pause and resume tests.
Pause service and check services are stopped then resume and check
they are started
"""
services = ['cinder-scheduler', 'cinder-volume']
if (openstack_utils.get_os_release() >=
openstack_utils.get_os_release('xenial_ocata')):
services.append('apache2')
else:
services.append('cinder-api')
with self.pause_resume(services):
logging.info("Testing pause resume")
class SecurityTests(test_utils.OpenStackBaseTest):
"""Keystone security tests tests."""
@classmethod
def setUpClass(cls):
"""Run class setup for running Keystone aa-tests."""
super(SecurityTests, cls).setUpClass()
def test_security_checklist(self):
"""Verify expected state with security-checklist."""
# Changes fixing the below expected failures will be made following
# this initial work to get validation in. There will be bugs targeted
# to each one and resolved independently where possible.
expected_failures = [
'check-max-request-body-size',
'is-volume-encryption-enabled',
'uses-tls-for-glance',
'uses-tls-for-nova',
'validate-uses-tls-for-keystone',
]
expected_passes = [
'validate-file-ownership',
'validate-file-permissions',
'validate-nas-uses-secure-environment',
'validate-uses-keystone',
]
for unit in zaza.model.get_units('cinder', model_name=self.model_name):
logging.info('Running `security-checklist` action'
' on unit {}'.format(unit.entity_id))
test_utils.audit_assertions(
zaza.model.run_action(
unit.entity_id,
'security-checklist',
model_name=self.model_name,
action_params={}),
expected_passes,
expected_failures,
expected_to_pass=False)