| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
import random |
| 14 |
import socket |
| 15 |
import string |
| 16 |
import struct |
| 17 |
|
| 18 |
from tempest.lib.common.utils import data_utils |
| 19 |
|
| 20 |
from magnum_tempest_plugin.common import config |
| 21 |
from magnum_tempest_plugin.tests.api.v1.models import cert_model |
| 22 |
from magnum_tempest_plugin.tests.api.v1.models import cluster_model |
| 23 |
from magnum_tempest_plugin.tests.api.v1.models import cluster_template_model |
| 24 |
from magnum_tempest_plugin.tests.api.v1.models import cluster_templatepatch_model # noqa |
| 25 |
from magnum_tempest_plugin.tests.api.v1.models import clusterpatch_model |
| 26 |
|
| 27 |
|
| 28 |
def random_int(min_int=1, max_int=100): |
| 29 |
return random.randrange(min_int, max_int) |
| 30 |
|
| 31 |
|
| 32 |
def gen_coe_dep_network_driver(coe): |
| 33 |
allowed_driver_types = { |
| 34 |
'kubernetes': ['flannel', 'calico', None], |
| 35 |
'swarm': ['docker', 'flannel', None], |
| 36 |
'swarm-mode': ['docker', None], |
| 37 |
'mesos': ['docker', None], |
| 38 |
} |
| 39 |
driver_types = allowed_driver_types[coe] |
| 40 |
return driver_types[random.randrange(0, len(driver_types))] |
| 41 |
|
| 42 |
|
| 43 |
def gen_coe_dep_volume_driver(coe): |
| 44 |
allowed_driver_types = { |
| 45 |
'kubernetes': ['cinder', None], |
| 46 |
'swarm': ['rexray', None], |
| 47 |
'swarm-mode': ['rexray', None], |
| 48 |
'mesos': ['rexray', None], |
| 49 |
} |
| 50 |
driver_types = allowed_driver_types[coe] |
| 51 |
return driver_types[random.randrange(0, len(driver_types))] |
| 52 |
|
| 53 |
|
| 54 |
def gen_random_port(): |
| 55 |
return random_int(49152, 65535) |
| 56 |
|
| 57 |
|
| 58 |
def gen_docker_volume_size(min_int=3, max_int=5): |
| 59 |
return random_int(min_int, max_int) |
| 60 |
|
| 61 |
|
| 62 |
def gen_fake_ssh_pubkey(): |
| 63 |
chars = "".join( |
| 64 |
random.choice(string.ascii_uppercase |
| 65 |
+ string.ascii_letters + string.digits + '/+=') |
| 66 |
for _ in range(372)) |
| 67 |
return "ssh-rsa " + chars |
| 68 |
|
| 69 |
|
| 70 |
def gen_random_ip(): |
| 71 |
return socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff))) |
| 72 |
|
| 73 |
|
| 74 |
def gen_url(scheme="http", domain="example.com", port=80): |
| 75 |
return "%s://%s:%s" % (scheme, domain, port) |
| 76 |
|
| 77 |
|
| 78 |
def gen_http_proxy(): |
| 79 |
return gen_url(port=gen_random_port()) |
| 80 |
|
| 81 |
|
| 82 |
def gen_https_proxy(): |
| 83 |
return gen_url(scheme="https", port=gen_random_port()) |
| 84 |
|
| 85 |
|
| 86 |
def gen_no_proxy(): |
| 87 |
return ",".join(gen_random_ip() for x in range(3)) |
| 88 |
|
| 89 |
|
| 90 |
def cert_data(cluster_uuid, csr_data): |
| 91 |
data = { |
| 92 |
"cluster_uuid": cluster_uuid, |
| 93 |
"csr": csr_data} |
| 94 |
|
| 95 |
model = cert_model.CertEntity.from_dict(data) |
| 96 |
|
| 97 |
return model |
| 98 |
|
| 99 |
|
| 100 |
def cluster_template_data(**kwargs): |
| 101 |
"""Generates random cluster_template data |
| 102 |
|
| 103 |
Keypair and image id cannot be random for the cluster_template to be valid |
| 104 |
due to validations for the presence of keypair and image id prior to |
| 105 |
cluster_template creation. |
| 106 |
|
| 107 |
:param keypair_id: keypair name |
| 108 |
:param image_id: image id or name |
| 109 |
:returns: ClusterTemplateEntity with generated data |
| 110 |
""" |
| 111 |
|
| 112 |
data = { |
| 113 |
"name": data_utils.rand_name('cluster'), |
| 114 |
"coe": "kubernetes", |
| 115 |
"tls_disabled": False, |
| 116 |
"network_driver": None, |
| 117 |
"volume_driver": None, |
| 118 |
"labels": {}, |
| 119 |
"public": False, |
| 120 |
"dns_nameserver": "8.8.8.8", |
| 121 |
"flavor_id": data_utils.rand_name('cluster'), |
| 122 |
"master_flavor_id": data_utils.rand_name('cluster'), |
| 123 |
"external_network_id": config.Config.nic_id, |
| 124 |
"keypair_id": data_utils.rand_name('cluster'), |
| 125 |
"image_id": data_utils.rand_name('cluster') |
| 126 |
} |
| 127 |
|
| 128 |
data.update(kwargs) |
| 129 |
model = cluster_template_model.ClusterTemplateEntity.from_dict(data) |
| 130 |
|
| 131 |
return model |
| 132 |
|
| 133 |
|
| 134 |
def cluster_template_replace_patch_data(path, |
| 135 |
value=data_utils.rand_name('cluster')): |
| 136 |
"""Generates random ClusterTemplate patch data |
| 137 |
|
| 138 |
:param path: path to replace |
| 139 |
:param value: value to replace in patch |
| 140 |
:returns: ClusterTemplatePatchCollection with generated data |
| 141 |
""" |
| 142 |
|
| 143 |
data = [{ |
| 144 |
"path": path, |
| 145 |
"value": value, |
| 146 |
"op": "replace" |
| 147 |
}] |
| 148 |
collection = cluster_templatepatch_model.ClusterTemplatePatchCollection |
| 149 |
return collection.from_dict(data) |
| 150 |
|
| 151 |
|
| 152 |
def cluster_template_remove_patch_data(path): |
| 153 |
"""Generates ClusterTemplate patch data by removing value |
| 154 |
|
| 155 |
:param path: path to remove |
| 156 |
:returns: ClusterTemplatePatchCollection with generated data |
| 157 |
""" |
| 158 |
|
| 159 |
data = [{ |
| 160 |
"path": path, |
| 161 |
"op": "remove" |
| 162 |
}] |
| 163 |
collection = cluster_templatepatch_model.ClusterTemplatePatchCollection |
| 164 |
return collection.from_dict(data) |
| 165 |
|
| 166 |
|
| 167 |
def cluster_template_name_patch_data(name=data_utils.rand_name('cluster')): |
| 168 |
"""Generates random cluster_template patch data |
| 169 |
|
| 170 |
:param name: name to replace in patch |
| 171 |
:returns: ClusterTemplatePatchCollection with generated data |
| 172 |
""" |
| 173 |
|
| 174 |
data = [{ |
| 175 |
"path": "/name", |
| 176 |
"value": name, |
| 177 |
"op": "replace" |
| 178 |
}] |
| 179 |
collection = cluster_templatepatch_model.ClusterTemplatePatchCollection |
| 180 |
return collection.from_dict(data) |
| 181 |
|
| 182 |
|
| 183 |
def cluster_template_flavor_patch_data(flavor=data_utils.rand_name('cluster')): |
| 184 |
"""Generates random cluster_template patch data |
| 185 |
|
| 186 |
:param flavor: flavor to replace in patch |
| 187 |
:returns: ClusterTemplatePatchCollection with generated data |
| 188 |
""" |
| 189 |
|
| 190 |
data = [{ |
| 191 |
"path": "/flavor_id", |
| 192 |
"value": flavor, |
| 193 |
"op": "replace" |
| 194 |
}] |
| 195 |
collection = cluster_templatepatch_model.ClusterTemplatePatchCollection |
| 196 |
return collection.from_dict(data) |
| 197 |
|
| 198 |
|
| 199 |
def cluster_template_data_with_valid_keypair_image_flavor(): |
| 200 |
"""Generates random clustertemplate data with valid data |
| 201 |
|
| 202 |
:returns: ClusterTemplateEntity with generated data |
| 203 |
""" |
| 204 |
master_flavor = config.Config.master_flavor_id |
| 205 |
return cluster_template_data( |
| 206 |
keypair_id=config.Config.keypair_name, |
| 207 |
image_id=config.Config.image_id, |
| 208 |
flavor_id=config.Config.flavor_id, |
| 209 |
master_flavor_id=master_flavor, |
| 210 |
network_driver=config.Config.network_driver, |
| 211 |
docker_storage_driver=config.Config.docker_storage_driver, |
| 212 |
labels=config.Config.labels) |
| 213 |
|
| 214 |
|
| 215 |
def cluster_template_data_with_missing_image(): |
| 216 |
"""Generates random cluster_template data with missing image |
| 217 |
|
| 218 |
:returns: ClusterTemplateEntity with generated data |
| 219 |
""" |
| 220 |
|
| 221 |
return cluster_template_data( |
| 222 |
keypair_id=config.Config.keypair_name, |
| 223 |
flavor_id=config.Config.flavor_id, |
| 224 |
master_flavor_id=config.Config.master_flavor_id) |
| 225 |
|
| 226 |
|
| 227 |
def cluster_template_data_with_missing_flavor(): |
| 228 |
"""Generates random cluster_template data with missing flavor |
| 229 |
|
| 230 |
:returns: ClusterTemplateEntity with generated data |
| 231 |
""" |
| 232 |
|
| 233 |
return cluster_template_data(keypair_id=config.Config.keypair_name, |
| 234 |
image_id=config.Config.image_id) |
| 235 |
|
| 236 |
|
| 237 |
def cluster_template_data_with_missing_keypair(): |
| 238 |
"""Generates random cluster_template data with missing keypair |
| 239 |
|
| 240 |
:returns: ClusterTemplateEntity with generated data |
| 241 |
""" |
| 242 |
|
| 243 |
return cluster_template_data( |
| 244 |
image_id=config.Config.image_id, |
| 245 |
flavor_id=config.Config.flavor_id, |
| 246 |
master_flavor_id=config.Config.master_flavor_id) |
| 247 |
|
| 248 |
|
| 249 |
def cluster_template_valid_data_with_specific_coe(coe): |
| 250 |
"""Generates random cluster_template data with valid keypair and image |
| 251 |
|
| 252 |
:param coe: coe |
| 253 |
:returns: ClusterTemplateEntity with generated data |
| 254 |
""" |
| 255 |
|
| 256 |
return cluster_template_data(keypair_id=config.Config.keypair_name, |
| 257 |
image_id=config.Config.image_id, coe=coe) |
| 258 |
|
| 259 |
|
| 260 |
def valid_cluster_template(is_public=False): |
| 261 |
"""Generates a valid swarm-mode cluster_template with valid data |
| 262 |
|
| 263 |
:returns: ClusterTemplateEntity with generated data |
| 264 |
""" |
| 265 |
master_flavor_id = config.Config.master_flavor_id |
| 266 |
return cluster_template_data( |
| 267 |
image_id=config.Config.image_id, |
| 268 |
flavor_id=config.Config.flavor_id, |
| 269 |
public=is_public, |
| 270 |
dns_nameserver=config.Config.dns_nameserver, |
| 271 |
master_flavor_id=master_flavor_id, |
| 272 |
coe=config.Config.coe, |
| 273 |
cluster_distro=None, |
| 274 |
external_network_id=config.Config.nic_id, |
| 275 |
http_proxy=None, https_proxy=None, |
| 276 |
no_proxy=None, |
| 277 |
network_driver=config.Config.network_driver, |
| 278 |
volume_driver=None, |
| 279 |
docker_storage_driver=config.Config.docker_storage_driver, |
| 280 |
tls_disabled=False, |
| 281 |
labels=config.Config.labels) |
| 282 |
|
| 283 |
|
| 284 |
def cluster_data(name=data_utils.rand_name('cluster'), |
| 285 |
cluster_template_id=data_utils.rand_uuid(), |
| 286 |
node_count=random_int(1, 5), discovery_url=gen_random_ip(), |
| 287 |
create_timeout=None, |
| 288 |
master_count=random_int(1, 5)): |
| 289 |
"""Generates random cluster data |
| 290 |
|
| 291 |
cluster_template_id cannot be random for the cluster to be valid due to |
| 292 |
validations for the presence of clustertemplate prior to clustertemplate |
| 293 |
creation. |
| 294 |
|
| 295 |
:param name: cluster name (must be unique) |
| 296 |
:param cluster_template_id: clustertemplate unique id (must already exist) |
| 297 |
:param node_count: number of agents for cluster |
| 298 |
:param discovery_url: url provided for node discovery |
| 299 |
:param create_timeout: timeout in minutes for cluster create |
| 300 |
:param master_count: number of master nodes for the cluster |
| 301 |
:returns: ClusterEntity with generated data |
| 302 |
""" |
| 303 |
|
| 304 |
timeout = create_timeout or config.Config.cluster_creation_timeout |
| 305 |
|
| 306 |
data = { |
| 307 |
"name": name, |
| 308 |
"cluster_template_id": cluster_template_id, |
| 309 |
"keypair": config.Config.keypair_name, |
| 310 |
"node_count": node_count, |
| 311 |
"discovery_url": None, |
| 312 |
"create_timeout": timeout, |
| 313 |
"master_count": master_count |
| 314 |
} |
| 315 |
model = cluster_model.ClusterEntity.from_dict(data) |
| 316 |
|
| 317 |
return model |
| 318 |
|
| 319 |
|
| 320 |
def valid_cluster_data(cluster_template_id, |
| 321 |
name=data_utils.rand_name('cluster'), |
| 322 |
node_count=1, master_count=1, create_timeout=None): |
| 323 |
"""Generates random cluster data with valid |
| 324 |
|
| 325 |
:param cluster_template_id: clustertemplate unique id that already exists |
| 326 |
:param name: cluster name (must be unique) |
| 327 |
:param node_count: number of agents for cluster |
| 328 |
:returns: ClusterEntity with generated data |
| 329 |
""" |
| 330 |
|
| 331 |
return cluster_data(cluster_template_id=cluster_template_id, name=name, |
| 332 |
master_count=master_count, node_count=node_count, |
| 333 |
create_timeout=create_timeout) |
| 334 |
|
| 335 |
|
| 336 |
def cluster_name_patch_data(name=data_utils.rand_name('cluster')): |
| 337 |
"""Generates random clustertemplate patch data |
| 338 |
|
| 339 |
:param name: name to replace in patch |
| 340 |
:returns: ClusterPatchCollection with generated data |
| 341 |
""" |
| 342 |
|
| 343 |
data = [{ |
| 344 |
"path": "/name", |
| 345 |
"value": name, |
| 346 |
"op": "replace" |
| 347 |
}] |
| 348 |
return clusterpatch_model.ClusterPatchCollection.from_dict(data) |
| 349 |
|
| 350 |
|
| 351 |
def cluster_api_addy_patch_data(address='0.0.0.0'): |
| 352 |
"""Generates random cluster patch data |
| 353 |
|
| 354 |
:param name: name to replace in patch |
| 355 |
:returns: ClusterPatchCollection with generated data |
| 356 |
""" |
| 357 |
|
| 358 |
data = [{ |
| 359 |
"path": "/api_address", |
| 360 |
"value": address, |
| 361 |
"op": "replace" |
| 362 |
}] |
| 363 |
return clusterpatch_model.ClusterPatchCollection.from_dict(data) |
| 364 |
|
| 365 |
|
| 366 |
def cluster_node_count_patch_data(node_count=2): |
| 367 |
"""Generates random cluster patch data |
| 368 |
|
| 369 |
:param name: name to replace in patch |
| 370 |
:returns: ClusterPatchCollection with generated data |
| 371 |
""" |
| 372 |
|
| 373 |
data = [{ |
| 374 |
"path": "/node_count", |
| 375 |
"value": node_count, |
| 376 |
"op": "replace" |
| 377 |
}] |
| 378 |
return clusterpatch_model.ClusterPatchCollection.from_dict(data) |