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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
# Copyright 2015 Tesora Inc.
# 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.
import json
from proboscis import SkipTest
from trove.tests.config import CONFIG
from trove.tests.scenario.helpers.test_helper import DataType
from trove.tests.scenario.runners.test_runners import CheckInstance
from trove.tests.scenario.runners.test_runners import InstanceTestInfo
from trove.tests.scenario.runners.test_runners import TestRunner
class InstanceCreateRunner(TestRunner):
def __init__(self):
super(InstanceCreateRunner, self).__init__()
self.init_inst_info = None
self.init_inst_dbs = None
self.init_inst_users = None
self.init_inst_host = None
self.init_inst_data = None
self.init_inst_config_group_id = None
self.config_group_id = None
def run_empty_instance_create(
self, expected_states=['BUILD', 'ACTIVE'], expected_http_code=200):
name = self.instance_info.name
flavor = self.get_instance_flavor()
volume_size = self.instance_info.volume_size
instance_info = self.assert_instance_create(
name, flavor, volume_size, [], [], None, None,
CONFIG.dbaas_datastore, CONFIG.dbaas_datastore_version,
expected_states, expected_http_code, create_helper_user=True,
locality='affinity')
# Update the shared instance info.
self.instance_info.id = instance_info.id
self.instance_info.name = instance_info.name
self.instance_info.databases = instance_info.databases
self.instance_info.users = instance_info.users
self.instance_info.dbaas_datastore = instance_info.dbaas_datastore
self.instance_info.dbaas_datastore_version = (
instance_info.dbaas_datastore_version)
self.instance_info.dbaas_flavor_href = instance_info.dbaas_flavor_href
self.instance_info.volume = instance_info.volume
self.instance_info.srv_grp_id = self.assert_server_group_exists(
self.instance_info.id)
self.instance_info.helper_user = instance_info.helper_user
self.instance_info.helper_database = instance_info.helper_database
def run_initial_configuration_create(self, expected_http_code=200):
dynamic_config = self.test_helper.get_dynamic_group()
non_dynamic_config = self.test_helper.get_non_dynamic_group()
values = dynamic_config or non_dynamic_config
if values:
json_def = json.dumps(values)
client = self.auth_client
result = client.configurations.create(
'initial_configuration_for_instance_create',
json_def,
"Configuration group used by instance create tests.",
datastore=self.instance_info.dbaas_datastore,
datastore_version=self.instance_info.dbaas_datastore_version)
self.assert_client_code(client, expected_http_code)
self.config_group_id = result.id
else:
raise SkipTest("No groups defined.")
def run_initialized_instance_create(
self, with_dbs=True, with_users=True, configuration_id=None,
expected_states=['BUILD', 'ACTIVE'], expected_http_code=200,
create_helper_user=True, name_suffix='_init'):
if self.is_using_existing_instance:
# The user requested to run the tests using an existing instance.
# We therefore skip any scenarios that involve creating new
# test instances.
raise SkipTest("Using an existing instance.")
configuration_id = configuration_id or self.config_group_id
name = self.instance_info.name + name_suffix
flavor = self.get_instance_flavor()
volume_size = self.instance_info.volume_size
self.init_inst_dbs = (self.test_helper.get_valid_database_definitions()
if with_dbs else [])
self.init_inst_users = (self.test_helper.get_valid_user_definitions()
if with_users else [])
self.init_inst_config_group_id = configuration_id
if (self.init_inst_dbs or self.init_inst_users or configuration_id):
info = self.assert_instance_create(
name, flavor, volume_size,
self.init_inst_dbs, self.init_inst_users,
configuration_id, None,
CONFIG.dbaas_datastore, CONFIG.dbaas_datastore_version,
expected_states, expected_http_code,
create_helper_user=create_helper_user)
self.init_inst_info = info
else:
# There is no need to run this test as it's effectively the same as
# the empty instance test.
raise SkipTest("No testable initial properties provided.")
def assert_instance_create(
self, name, flavor, trove_volume_size,
database_definitions, user_definitions,
configuration_id, root_password, datastore, datastore_version,
expected_states, expected_http_code, create_helper_user=False,
locality=None):
"""This assert method executes a 'create' call and verifies the server
response. It neither waits for the instance to become available
nor it performs any other validations itself.
It has been designed this way to increase test granularity
(other tests may run while the instance is building) and also to allow
its reuse in other runners.
"""
databases = database_definitions
users = [{'name': item['name'], 'password': item['password']}
for item in user_definitions]
instance_info = InstanceTestInfo()
# Here we add helper user/database if any.
if create_helper_user:
helper_db_def, helper_user_def, root_def = self.build_helper_defs()
if helper_db_def:
self.report.log(
"Appending a helper database '%s' to the instance "
"definition." % helper_db_def['name'])
databases.append(helper_db_def)
instance_info.helper_database = helper_db_def
if helper_user_def:
self.report.log(
"Appending a helper user '%s:%s' to the instance "
"definition."
% (helper_user_def['name'], helper_user_def['password']))
users.append(helper_user_def)
instance_info.helper_user = helper_user_def
instance_info.name = name
instance_info.databases = databases
instance_info.users = users
instance_info.dbaas_datastore = CONFIG.dbaas_datastore
instance_info.dbaas_datastore_version = CONFIG.dbaas_datastore_version
instance_info.dbaas_flavor_href = self.get_flavor_href(flavor)
if self.VOLUME_SUPPORT:
instance_info.volume = {'size': trove_volume_size}
else:
instance_info.volume = None
instance_info.nics = self.instance_info.nics
self.report.log("Testing create instance: %s"
% {'name': name,
'flavor': flavor.id,
'volume': trove_volume_size,
'nics': instance_info.nics,
'databases': databases,
'users': users,
'configuration': configuration_id,
'root password': root_password,
'datastore': datastore,
'datastore version': datastore_version})
instance = self.get_existing_instance()
if instance:
self.report.log("Using an existing instance: %s" % instance.id)
self.assert_equal(expected_states[-1], instance.status,
"Given instance is in a bad state.")
instance_info.name = instance.name
else:
self.report.log("Creating a new instance.")
client = self.auth_client
instance = client.instances.create(
instance_info.name,
instance_info.dbaas_flavor_href,
instance_info.volume,
instance_info.databases,
instance_info.users,
nics=instance_info.nics,
configuration=configuration_id,
availability_zone="nova",
datastore=instance_info.dbaas_datastore,
datastore_version=instance_info.dbaas_datastore_version,
locality=locality)
self.assert_client_code(client, expected_http_code)
self.assert_instance_action(instance.id, expected_states[0:1])
instance_info.id = instance.id
with CheckInstance(instance._info) as check:
check.flavor()
check.datastore()
check.links(instance._info['links'])
if self.VOLUME_SUPPORT:
check.volume()
self.assert_equal(trove_volume_size,
instance._info['volume']['size'],
"Unexpected Trove volume size")
self.assert_equal(instance_info.name, instance._info['name'],
"Unexpected instance name")
self.assert_equal(flavor.id,
int(instance._info['flavor']['id']),
"Unexpected instance flavor")
self.assert_equal(instance_info.dbaas_datastore,
instance._info['datastore']['type'],
"Unexpected instance datastore version")
self.assert_equal(instance_info.dbaas_datastore_version,
instance._info['datastore']['version'],
"Unexpected instance datastore version")
self.assert_configuration_group(instance_info.id, configuration_id)
if locality:
self.assert_equal(locality, instance._info['locality'],
"Unexpected locality")
return instance_info
def run_wait_for_created_instances(
self, expected_states=['BUILD', 'ACTIVE']):
instances = [self.instance_info.id]
if self.init_inst_info:
instances.append(self.init_inst_info.id)
self.assert_all_instance_states(instances, expected_states)
# It may take some time before the initial users/databases
# appear on the instance after it went ACTIVE.
# We need to wait for that before we can proceed with data operations.
self.wait_for_test_helpers(self.instance_info)
if self.init_inst_info:
self.wait_for_test_helpers(self.init_inst_info)
def wait_for_test_helpers(self, inst_info):
self.report.log("Waiting for helper users and databases to be "
"created on instance: %s" % inst_info.id)
client = self.auth_client
if inst_info.helper_user:
self.wait_for_user_create(client, inst_info.id,
[inst_info.helper_user])
if inst_info.helper_database:
self.wait_for_database_create(client, inst_info.id,
[inst_info.helper_database])
self.report.log("Test helpers are ready.")
def run_add_initialized_instance_data(self):
self.init_inst_data = DataType.small
self.init_inst_host = self.get_instance_host(self.init_inst_info.id)
self.test_helper.add_data(self.init_inst_data, self.init_inst_host)
def run_validate_initialized_instance(self):
if self.init_inst_info:
self.assert_instance_properties(
self.init_inst_info.id, self.init_inst_dbs,
self.init_inst_users, self.init_inst_config_group_id,
self.init_inst_data)
def assert_instance_properties(
self, instance_id, expected_dbs_definitions,
expected_user_definitions, expected_config_group_id,
expected_data_type):
if expected_dbs_definitions:
self.assert_database_list(instance_id, expected_dbs_definitions)
else:
self.report.log("No databases to validate for instance: %s"
% instance_id)
if expected_user_definitions:
self.assert_user_list(instance_id, expected_user_definitions)
else:
self.report.log("No users to validate for instance: %s"
% instance_id)
self.assert_configuration_group(instance_id, expected_config_group_id)
if self.init_inst_host:
self.test_helper.verify_data(
expected_data_type, self.init_inst_host)
else:
self.report.log("No data to validate for instance: %s"
% instance_id)
def assert_configuration_group(self, instance_id, expected_group_id):
instance = self.get_instance(instance_id)
if expected_group_id:
self.assert_equal(expected_group_id, instance.configuration['id'],
"Wrong configuration group attached")
else:
self.assert_false(hasattr(instance, 'configuration'),
"No configuration group expected")
def assert_database_list(self, instance_id, expected_databases):
self.wait_for_database_create(self.auth_client,
instance_id, expected_databases)
def _get_names(self, definitions):
return [item['name'] for item in definitions]
def assert_user_list(self, instance_id, expected_users):
self.wait_for_user_create(self.auth_client,
instance_id, expected_users)
# Verify that user definitions include only created databases.
all_databases = self._get_names(
self.test_helper.get_valid_database_definitions())
for user in expected_users:
if 'databases' in user:
self.assert_is_sublist(
self._get_names(user['databases']), all_databases,
"Definition of user '%s' specifies databases not included "
"in the list of initial databases." % user['name'])
def run_initialized_instance_delete(self, expected_http_code=202):
if self.init_inst_info:
client = self.auth_client
client.instances.delete(self.init_inst_info.id)
self.assert_client_code(client, expected_http_code)
else:
raise SkipTest("Cleanup is not required.")
def run_wait_for_init_delete(self, expected_states=['SHUTDOWN']):
delete_ids = []
if self.init_inst_info:
delete_ids.append(self.init_inst_info.id)
if delete_ids:
self.assert_all_gone(delete_ids, expected_states[-1])
else:
raise SkipTest("Cleanup is not required.")
self.init_inst_info = None
self.init_inst_dbs = None
self.init_inst_users = None
self.init_inst_host = None
self.init_inst_data = None
self.init_inst_config_group_id = None
def run_initial_configuration_delete(self, expected_http_code=202):
if self.config_group_id:
client = self.auth_client
client.configurations.delete(self.config_group_id)
self.assert_client_code(client, expected_http_code)
else:
raise SkipTest("Cleanup is not required.")
self.config_group_id = None
|