| 1 |
# Copyright 2016, 2018 Red Hat, Inc. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
""" |
| 16 |
This script will generate the etc/tempest.conf file by applying a series of |
| 17 |
specified options in the following order: |
| 18 |
|
| 19 |
1. Default values provided by the tool. |
| 20 |
|
| 21 |
2. Values using the file provided by the --deployer-input argument to the |
| 22 |
script. |
| 23 |
Some required options differ among deployed clouds but the right values cannot |
| 24 |
be discovered by the user. The file used here could be created by an installer, |
| 25 |
or manually if necessary. |
| 26 |
|
| 27 |
3. Values provided in client's cloud config file or as an environment |
| 28 |
variables, see documentation of openstacksdk |
| 29 |
https://docs.openstack.org/openstacksdk/latest/ |
| 30 |
|
| 31 |
4. Values provided on the command line. These override all other values. |
| 32 |
|
| 33 |
5. Discovery. Values that have not been provided in steps [2-4] will be |
| 34 |
obtained by querying the cloud. |
| 35 |
""" |
| 36 |
|
| 37 |
import argparse |
| 38 |
import logging |
| 39 |
import os |
| 40 |
import re |
| 41 |
import secrets |
| 42 |
import string |
| 43 |
import sys |
| 44 |
|
| 45 |
import openstack |
| 46 |
from oslo_config import cfg |
| 47 |
from six.moves import configparser |
| 48 |
|
| 49 |
from config_tempest import accounts |
| 50 |
from config_tempest.clients import ClientManager |
| 51 |
from config_tempest import constants as C |
| 52 |
from config_tempest.constants import LOG |
| 53 |
from config_tempest.credentials import Credentials |
| 54 |
from config_tempest.flavors import Flavors |
| 55 |
from config_tempest import profile |
| 56 |
from config_tempest.services.services import Services |
| 57 |
from config_tempest.tempest_conf import TempestConf |
| 58 |
from config_tempest.users import Users |
| 59 |
|
| 60 |
|
| 61 |
def set_logging(debug, verbose): |
| 62 |
"""Set logging based on the arguments. |
| 63 |
|
| 64 |
:type debug: Boolean |
| 65 |
:type verbose: Boolean |
| 66 |
""" |
| 67 |
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' |
| 68 |
logging.basicConfig(format=log_format) |
| 69 |
if debug: |
| 70 |
LOG.setLevel(logging.DEBUG) |
| 71 |
elif verbose: |
| 72 |
LOG.setLevel(logging.INFO) |
| 73 |
|
| 74 |
|
| 75 |
def load_basic_defaults(conf): |
| 76 |
"""Load basic default options into conf file. |
| 77 |
|
| 78 |
:type conf: TempestConf object |
| 79 |
""" |
| 80 |
LOG.debug("Setting basic default values") |
| 81 |
default_values = { |
| 82 |
"DEFAULT": [ |
| 83 |
("debug", "true"), |
| 84 |
("use_stderr", "false"), |
| 85 |
("log_file", "tempest.log") |
| 86 |
], |
| 87 |
"identity": [ |
| 88 |
("username", "demo_tempestconf"), |
| 89 |
("password", "secrete"), |
| 90 |
("project_name", "demo"), |
| 91 |
("project_domain_name", "Default"), |
| 92 |
("user_domain_name", "Default"), |
| 93 |
("alt_username", "alt_demo_tempestconf"), |
| 94 |
("alt_password", "secrete"), |
| 95 |
("alt_project_name", "alt_demo") |
| 96 |
], |
| 97 |
"auth": [ |
| 98 |
("tempest_roles", "member"), |
| 99 |
("admin_username", "admin"), |
| 100 |
("admin_project_name", "admin"), |
| 101 |
("admin_domain_name", "Default"), |
| 102 |
("admin_project_domain_name", "Default"), |
| 103 |
("admin_user_domain_name", "Default") |
| 104 |
], |
| 105 |
"object-storage": [ |
| 106 |
("reseller_admin_role", "ResellerAdmin") |
| 107 |
], |
| 108 |
"oslo-concurrency": [ |
| 109 |
("lock_path", "/tmp") |
| 110 |
], |
| 111 |
"compute-feature-enabled": [ |
| 112 |
("preserve_ports", "true") |
| 113 |
], |
| 114 |
"network-feature-enabled": [ |
| 115 |
("ipv6_subnet_attributes", "true") |
| 116 |
], |
| 117 |
"scenario": [ |
| 118 |
# Since cirros version 0.6.0, dhcpcd dhcp client is the default |
| 119 |
# and because tempestconf sets default cirros >= 0.6.0 by default, |
| 120 |
# we can also set the dhcp client to dhcpcd by default |
| 121 |
("dhcp_client", "dhcpcd") |
| 122 |
]} |
| 123 |
|
| 124 |
for section in default_values.keys(): |
| 125 |
if section != "DEFAULT" and not conf.has_section(section): |
| 126 |
conf.add_section(section) |
| 127 |
for key, value in default_values[section]: |
| 128 |
if not conf.has_option(section, key): |
| 129 |
conf.set(section, key, value) |
| 130 |
|
| 131 |
|
| 132 |
def read_deployer_input(deployer_input_file, conf): |
| 133 |
"""Read deployer-input file and set values in conf accordingly. |
| 134 |
|
| 135 |
:param deployer_input_file: Path to the deployer inut file |
| 136 |
:type deployer_input_file: String |
| 137 |
:param conf: TempestConf object |
| 138 |
""" |
| 139 |
LOG.info("Adding options from deployer-input file '%s'", |
| 140 |
deployer_input_file) |
| 141 |
deployer_input = configparser.ConfigParser() |
| 142 |
# the following transforms the option names as found in an input file, |
| 143 |
# the default implementation returns a lower-case version of the |
| 144 |
# option names, however, we have at least one option that contains |
| 145 |
# capitalized letters - RBAC_test_type from octavia-tempest-plugin |
| 146 |
deployer_input.optionxform = str |
| 147 |
deployer_input.read(deployer_input_file) |
| 148 |
for section in deployer_input.sections(): |
| 149 |
# There are no deployer input options in DEFAULT |
| 150 |
for (key, value) in deployer_input.items(section): |
| 151 |
conf.set(section, key, value, priority=True) |
| 152 |
|
| 153 |
|
| 154 |
def set_options(conf, deployer_input, non_admin, image_path, overrides=[], |
| 155 |
accounts_path=None, cloud_creds=None, |
| 156 |
no_default_deployer=False): |
| 157 |
"""Set options in conf provided by different source. |
| 158 |
|
| 159 |
1. read the default values |
| 160 |
2. read a file provided by --deployer-input argument |
| 161 |
3. read default DEPLOYER_INPUT if --no-deployer-input is False and no |
| 162 |
deployer_input was passed |
| 163 |
4. set values from client's config (openstacksdk support) if provided |
| 164 |
5. set overrides - may override values which were set in the steps above |
| 165 |
|
| 166 |
:param conf: TempestConf object |
| 167 |
:param deployer_input: Path to the deployer inut file |
| 168 |
:type deployer_input: string |
| 169 |
:type non_admin: boolean |
| 170 |
:param image_path: An image to be uploaded to glance |
| 171 |
:type image_path: string |
| 172 |
:param overrides: list of tuples: [(section, key, value)] |
| 173 |
:type overrides: list |
| 174 |
:param accounts_path: A path where accounts.yaml is or will be created. |
| 175 |
:type accounts_path: string |
| 176 |
:param cloud_creds: Cloud credentials from client's config |
| 177 |
:type cloud_creds: dict |
| 178 |
""" |
| 179 |
load_basic_defaults(conf) |
| 180 |
# image.image_path is a python-tempestconf option which defines which |
| 181 |
# image will be uploaded to glance |
| 182 |
conf.set('image', 'image_path', image_path) |
| 183 |
|
| 184 |
if deployer_input and os.path.isfile(deployer_input): |
| 185 |
LOG.info("Reading deployer input from file %s", deployer_input) |
| 186 |
read_deployer_input(deployer_input, conf) |
| 187 |
elif os.path.isfile(C.DEPLOYER_INPUT) and not no_default_deployer: |
| 188 |
LOG.info("Reading deployer input from file %s", C.DEPLOYER_INPUT) |
| 189 |
read_deployer_input(C.DEPLOYER_INPUT, conf) |
| 190 |
|
| 191 |
if non_admin: |
| 192 |
# non admin, so delete auth admin values which were set |
| 193 |
# in load_basic_defaults method |
| 194 |
conf.set("auth", "admin_username", "") |
| 195 |
conf.set("auth", "admin_project_name", "") |
| 196 |
conf.set("auth", "admin_password", "") |
| 197 |
conf.set("auth", "use_dynamic_credentials", "False", priority=True) |
| 198 |
|
| 199 |
# get and set auth data from client's config |
| 200 |
if cloud_creds: |
| 201 |
set_cloud_config_values(non_admin, cloud_creds, conf) |
| 202 |
|
| 203 |
if accounts_path: |
| 204 |
# new way for running using accounts file |
| 205 |
conf.set("auth", "use_dynamic_credentials", "False", priority=True) |
| 206 |
conf.set("auth", "test_accounts_file", |
| 207 |
os.path.abspath(accounts_path)) |
| 208 |
|
| 209 |
# set overrides - values specified in CLI |
| 210 |
for section, key, value in overrides: |
| 211 |
conf.set(section, key, value, priority=True) |
| 212 |
|
| 213 |
uri = conf.get("identity", "uri") |
| 214 |
if "v3" in uri: |
| 215 |
conf.set("identity", "uri_v3", uri) |
| 216 |
else: |
| 217 |
# TODO(arxcruz) make a check if v3 is enabled |
| 218 |
conf.set("identity", "uri_v3", uri.replace("v2.0", "v3")) |
| 219 |
|
| 220 |
|
| 221 |
def get_arg_parser(): |
| 222 |
parser = argparse.ArgumentParser(__doc__) |
| 223 |
cloud_config = openstack.config.OpenStackConfig() |
| 224 |
cloud_config.register_argparse_arguments(parser, sys.argv) |
| 225 |
parser.add_argument('--create', action='store_true', default=False, |
| 226 |
help="""Create Tempest resources |
| 227 |
Make *python-tempestconf* to create Tempest |
| 228 |
resources such as flavors needed for running |
| 229 |
Tempest tests.""") |
| 230 |
parser.add_argument('--out', default="etc/tempest.conf", |
| 231 |
help="""Output file |
| 232 |
A name of the file where the discovered Tempest |
| 233 |
configuration will be written to.""") |
| 234 |
parser.add_argument('--deployer-input', default=None, |
| 235 |
help="""Path to deployer file |
| 236 |
A file in the format of tempest.conf that will |
| 237 |
override the default values. It is usually |
| 238 |
created by an installer and contains |
| 239 |
environment specific options. |
| 240 |
|
| 241 |
The deployer-input file is an alternative to |
| 242 |
providing key/value pairs. If there are also |
| 243 |
key/value pairs they will be applied after the |
| 244 |
deployer-input file. |
| 245 |
|
| 246 |
If the option is **not defined** and |
| 247 |
**--no-default-deployer** is **not used**, |
| 248 |
python-tempestconf **will try** to look for the |
| 249 |
file in `$HOME/tempest-deployer-input.conf` |
| 250 |
location.""") |
| 251 |
parser.add_argument('--no-default-deployer', action='store_true', |
| 252 |
default=False, |
| 253 |
help="""Do not check for the default deployer input in |
| 254 |
`$HOME/tempest-deployer-input.conf`""") |
| 255 |
parser.add_argument('overrides', nargs='*', default=[], |
| 256 |
help="""Override options |
| 257 |
Key value pairs used to hardcode values in |
| 258 |
`tempest.conf`. The key is a section.key where |
| 259 |
section is a section header in the conf file. |
| 260 |
For example: |
| 261 |
$ discover-tempest-config \\ |
| 262 |
identity.username myname \\ |
| 263 |
identity.password mypass""") |
| 264 |
parser.add_argument('--password-regex', default=None, |
| 265 |
help="""Define the password regex required to |
| 266 |
generate a password that meets these |
| 267 |
conditions. NOTE: In regular expression use |
| 268 |
double curly brackets `{{`instead of single |
| 269 |
curly bracket `{` to make the command pass. |
| 270 |
""") |
| 271 |
parser.add_argument('--password-length', default=8, |
| 272 |
help="""Specify the password length in combination |
| 273 |
with the --password-regex option to generate |
| 274 |
a password that meets these conditions. |
| 275 |
""") |
| 276 |
parser.add_argument('--debug', action='store_true', default=False, |
| 277 |
help='Print debugging information.') |
| 278 |
parser.add_argument('--verbose', '-v', action='store_true', default=False, |
| 279 |
help='Print more information about the execution.') |
| 280 |
parser.add_argument('--no-rng', action='store_true', default=False, |
| 281 |
help="""Create new flavors and upload images without |
| 282 |
random number generator device.""") |
| 283 |
parser.add_argument('--non-admin', action='store_true', default=False, |
| 284 |
help="""Simulate non-admin credentials. |
| 285 |
When True, the credentials are used as |
| 286 |
non-admin ones. No resources are created.""") |
| 287 |
parser.add_argument('--test-accounts', default=None, metavar='PATH', |
| 288 |
help="""Tempest accounts.yaml file |
| 289 |
Defines a path to a Tempest accounts.yaml |
| 290 |
file. |
| 291 |
For example: |
| 292 |
--test-accounts $HOME/tempest/accounts.yaml |
| 293 |
""") |
| 294 |
parser.add_argument('--create-accounts-file', default=None, |
| 295 |
metavar='PATH', |
| 296 |
help="""Generate Tempest accounts file |
| 297 |
Minimal accounts file will be created in the |
| 298 |
specified path. |
| 299 |
For example: |
| 300 |
--create-accounts-file $HOME/accounts.yaml |
| 301 |
""") |
| 302 |
parser.add_argument('--profile', default=None, metavar='PATH', |
| 303 |
help="""python-tempestconf's profile.yaml file |
| 304 |
A file which contains definition of |
| 305 |
python-tempestconf's arguments. |
| 306 |
NOTE: If this argument is used, other |
| 307 |
arguments cannot be defined!""") |
| 308 |
parser.add_argument('--generate-profile', default=None, |
| 309 |
metavar='PATH', |
| 310 |
help="""Generate a sample profile.yaml file. |
| 311 |
A sample profile.yaml will be generated in the |
| 312 |
specified path. After that python-tempestconf |
| 313 |
ends. |
| 314 |
For example: |
| 315 |
--generate-profile $HOME/profile.yaml |
| 316 |
""") |
| 317 |
parser.add_argument('--image-disk-format', default=C.DEFAULT_IMAGE_FORMAT, |
| 318 |
help="""A format of an image to be uploaded to glance. |
| 319 |
Default is '%s'""" % C.DEFAULT_IMAGE_FORMAT) |
| 320 |
parser.add_argument('--image', default=C.DEFAULT_IMAGE, |
| 321 |
help="""An image name/path/url to be uploaded to |
| 322 |
glance if it's not already there. The name of |
| 323 |
the image is the leaf name of the path. Default |
| 324 |
is '%s'""" % C.DEFAULT_IMAGE) |
| 325 |
parser.add_argument('--retry-image', default=False, action='store_true', |
| 326 |
help="""Allow tempestconf to retry download an image, |
| 327 |
in case of failure, from these urls: '%s' |
| 328 |
""" % C.DEFAULT_IMAGES) |
| 329 |
parser.add_argument('--flavor-min-mem', default=C.DEFAULT_FLAVOR_RAM, |
| 330 |
type=int, help="""Specify minimum memory for new |
| 331 |
flavours, default is '%s'.""" % C.DEFAULT_FLAVOR_RAM) |
| 332 |
parser.add_argument('--flavor-min-disk', default=C.DEFAULT_FLAVOR_DISK, |
| 333 |
type=int, help="""Specify minimum disk size for new |
| 334 |
flavours, default is '%s'.""" % C.DEFAULT_FLAVOR_DISK) |
| 335 |
parser.add_argument('--convert-to-raw', action='store_true', default=False, |
| 336 |
help="""Convert images to raw format before uploading |
| 337 |
to glance.""") |
| 338 |
parser.add_argument('--network-id', |
| 339 |
help="""Specify which network with external |
| 340 |
connectivity should be used by the tests.""") |
| 341 |
parser.add_argument('--network', |
| 342 |
help="""Specify which network (id/name) with external |
| 343 |
connectivity should be used by the tests.""") |
| 344 |
parser.add_argument('--append', action='append', default=[], |
| 345 |
metavar="SECTION.KEY=VALUE[,VALUE]", |
| 346 |
help="""Append values to tempest.conf |
| 347 |
Key value pair to be appended to the |
| 348 |
configuration file. |
| 349 |
NOTE: Multiple values are supposed to be |
| 350 |
divided by a COLON only, WITHOUT spaces. |
| 351 |
For example: |
| 352 |
$ discover-tempest-config \\ |
| 353 |
--append features.ext=tag[,tag-ext] \\ |
| 354 |
--append section.ext=ext[,another-ext] |
| 355 |
""") |
| 356 |
parser.add_argument('--remove', action='append', default=[], |
| 357 |
metavar="SECTION.KEY=VALUE[,VALUE]", |
| 358 |
help="""Remove values from tempest.conf |
| 359 |
Key value pair to be removed from the |
| 360 |
configuration file. |
| 361 |
NOTE: Multiple values are supposed to be |
| 362 |
divided by a COLON only, WITHOUT spaces. |
| 363 |
For example: |
| 364 |
$ discover-tempest-config \\ |
| 365 |
--remove identity.username=myname \\ |
| 366 |
--remove feature-enabled.api_ext=http[,https] |
| 367 |
""") |
| 368 |
return parser |
| 369 |
|
| 370 |
|
| 371 |
def parse_arguments(): |
| 372 |
parser = get_arg_parser() |
| 373 |
args = parser.parse_args() |
| 374 |
if args.create and args.non_admin: |
| 375 |
raise Exception("Options '--create' and '--non-admin' cannot be used" |
| 376 |
" together, since creating" " resources requires" |
| 377 |
" admin rights") |
| 378 |
if args.test_accounts and args.create_accounts_file: |
| 379 |
raise Exception("Options '--test-accounts' and " |
| 380 |
"'--create-accounts-file' can't be used together.") |
| 381 |
if args.network_id and args.network: |
| 382 |
raise Exception("--network-id is deprecated and will be replaced with" |
| 383 |
" --network. Please, use only --network argument.") |
| 384 |
if args.network_id: |
| 385 |
LOG.warning("The --network-id is deprecated, please, use --network") |
| 386 |
args.network = args.network_id |
| 387 |
args.overrides = parse_overrides(args.overrides) |
| 388 |
return args |
| 389 |
|
| 390 |
|
| 391 |
def parse_values_to_remove(options): |
| 392 |
"""Manual parsing of remove arguments. |
| 393 |
|
| 394 |
:param options: list of arguments following --remove argument |
| 395 |
:return: dictionary containing key paths with values to be removed |
| 396 |
:rtype: dict |
| 397 |
EXAMPLE: {'identity.username': [myname], |
| 398 |
'identity-feature-enabled.api_extensions': [http, https]} |
| 399 |
""" |
| 400 |
parsed = {} |
| 401 |
for argument in options: |
| 402 |
if len(argument.split('=')) == 2: |
| 403 |
section, values = argument.split('=') |
| 404 |
if len(section.split('.')) != 2: |
| 405 |
raise Exception("Missing dot. The option --remove has to " |
| 406 |
"come in the format 'section.key=value[,value" |
| 407 |
"]', but got '%s'." % argument) |
| 408 |
parsed[section] = values.split(',') |
| 409 |
else: |
| 410 |
# missing equal sign, all values in section.key will be deleted |
| 411 |
parsed[argument] = [] |
| 412 |
return parsed |
| 413 |
|
| 414 |
|
| 415 |
def parse_values_to_append(options): |
| 416 |
"""Manual parsing of --append arguments. |
| 417 |
|
| 418 |
:param options: list of arguments following --append argument. |
| 419 |
:return: dictionary containing key paths with values to be added |
| 420 |
:rtype: dict |
| 421 |
""" |
| 422 |
parsed = {} |
| 423 |
for argument in options: |
| 424 |
if len(argument.split('=')) == 2: |
| 425 |
section, values = argument.split('=') |
| 426 |
if len(section.split('.')) != 2: |
| 427 |
raise Exception("Missing dot. The option --append has to " |
| 428 |
"come in the format 'section.key=value[,value" |
| 429 |
"]', but got '%s'." % argument) |
| 430 |
if values == '': |
| 431 |
raise Exception("No values to append specified. The option " |
| 432 |
"--append has to come in the format " |
| 433 |
"'section.key=value[, value]', but got " |
| 434 |
"'%s'" % values) |
| 435 |
parsed[section] = values.split(',') |
| 436 |
else: |
| 437 |
# missing equal sign, no values to add were specified, if a user |
| 438 |
# wants to just create a section, it can be done so via overrides |
| 439 |
raise Exception("Missing equal sign or more than just one found.") |
| 440 |
return parsed |
| 441 |
|
| 442 |
|
| 443 |
def parse_overrides(overrides): |
| 444 |
"""Manual parsing of positional arguments. |
| 445 |
|
| 446 |
:param overrides: list of section.keys and values to override, example: |
| 447 |
['section.key', 'value', 'section.key', 'value'] |
| 448 |
:return: list of tuples, example: [('section', 'key', 'value'), ...] |
| 449 |
:rtype: list |
| 450 |
""" |
| 451 |
if len(overrides) % 2 != 0: |
| 452 |
raise Exception("An odd number of override options was found. The" |
| 453 |
" overrides have to be in 'section.key value' format.") |
| 454 |
i = 0 |
| 455 |
new_overrides = [] |
| 456 |
while i < len(overrides): |
| 457 |
section_key = overrides[i].split('.') |
| 458 |
value = overrides[i + 1] |
| 459 |
if len(section_key) != 2: |
| 460 |
raise Exception("Missing dot. The option overrides has to come in" |
| 461 |
" the format 'section.key value', but got '%s'." |
| 462 |
% (overrides[i] + ' ' + value)) |
| 463 |
section, key = section_key |
| 464 |
new_overrides.append((section, key, value)) |
| 465 |
i += 2 |
| 466 |
return new_overrides |
| 467 |
|
| 468 |
|
| 469 |
def generate_password_by_regex(regex, length): |
| 470 |
"""Generate a new password that meets the required regex and length. |
| 471 |
|
| 472 |
:type regex: string |
| 473 |
:type length: int |
| 474 |
""" |
| 475 |
characters = string.ascii_letters + string.digits + string.punctuation |
| 476 |
regex = regex.replace('{{', '{').replace('}}', '}') |
| 477 |
|
| 478 |
try: |
| 479 |
regex_object = re.compile(regex) |
| 480 |
except re.error: |
| 481 |
raise Exception("The provided regex %s is invalid." % regex) |
| 482 |
|
| 483 |
for _ in range(200): |
| 484 |
password = ''.join(secrets.choice(characters) for i in range(length)) |
| 485 |
if regex_object.fullmatch(password): |
| 486 |
return password |
| 487 |
|
| 488 |
raise Exception("Could not generate a password matching the specified" |
| 489 |
" regex. Check if the regex %s is possible with the" |
| 490 |
" given length %d." % (regex, length)) |
| 491 |
|
| 492 |
|
| 493 |
def set_cloud_config_values(non_admin, cloud_creds, conf): |
| 494 |
"""Set values from client's cloud config file. |
| 495 |
|
| 496 |
Set admin and non-admin credentials and uri from cloud credentials. |
| 497 |
Note: the values may be later overridden by values specified in CLI. |
| 498 |
|
| 499 |
:type non_admin: Boolean |
| 500 |
:param cloud_creds: auth data from openstacksdk |
| 501 |
:type cloud_creds: dict |
| 502 |
:param conf: TempestConf object |
| 503 |
""" |
| 504 |
try: |
| 505 |
if non_admin: |
| 506 |
# Tempest doesn't have non-admin credentials, but we're gonna |
| 507 |
# keep them under identity for future usage |
| 508 |
conf.set('identity', 'username', cloud_creds['username']) |
| 509 |
conf.set('identity', |
| 510 |
'project_name', |
| 511 |
cloud_creds['project_name']) |
| 512 |
conf.set('identity', 'password', cloud_creds['password']) |
| 513 |
for cred in ['project_domain_name', 'user_domain_name']: |
| 514 |
if cred in cloud_creds: |
| 515 |
conf.set('auth', cred, cloud_creds[cred]) |
| 516 |
else: |
| 517 |
# admin credentials are under auth section |
| 518 |
conf.set('auth', 'admin_username', cloud_creds['username']) |
| 519 |
conf.set('auth', |
| 520 |
'admin_project_name', |
| 521 |
cloud_creds['project_name']) |
| 522 |
conf.set('auth', 'admin_password', cloud_creds['password']) |
| 523 |
for cred in [ |
| 524 |
'domain_name', 'project_domain_name', 'user_domain_name' |
| 525 |
]: |
| 526 |
if cred in cloud_creds: |
| 527 |
conf.set('auth', f'admin_{cred}', cloud_creds[cred]) |
| 528 |
|
| 529 |
conf.set('identity', 'uri', cloud_creds['auth_url']) |
| 530 |
|
| 531 |
if 'region_name' in cloud_creds: |
| 532 |
conf.set('identity', 'region', cloud_creds['region_name']) |
| 533 |
except cfg.NoSuchOptError: |
| 534 |
LOG.warning( |
| 535 |
'Could not load some identity options from cloud config file') |
| 536 |
|
| 537 |
|
| 538 |
def get_cloud_creds(args_namespace): |
| 539 |
"""Get cloud credentials based on argument namespace. |
| 540 |
|
| 541 |
If args contains --os-cloud argument, the method returns cloud |
| 542 |
credentials related to that cloud, otherwise, returns credentials |
| 543 |
of the current cloud. |
| 544 |
|
| 545 |
:type args_namespace: argparse.Namespace |
| 546 |
:return: cloud credentials |
| 547 |
:rtype: dict |
| 548 |
EXAMPLE: {'username': 'demo', 'project_name': 'demo', |
| 549 |
'user_domain_name': 'Default', |
| 550 |
'auth_url': 'http://172.16.52.8:5000/v3', |
| 551 |
'password': 'f0921edc3c2b4fc8', 'project_domain_name': 'Default'} |
| 552 |
""" |
| 553 |
if args_namespace.os_cloud: |
| 554 |
cloud = openstack.connect(cloud=args_namespace.os_cloud) |
| 555 |
else: |
| 556 |
cloud = openstack.connect(**vars(args_namespace)) |
| 557 |
|
| 558 |
cloud_creds = cloud.config.get_auth_args() |
| 559 |
region_name = cloud.config.config['region_name'] |
| 560 |
if region_name: |
| 561 |
cloud_creds['region_name'] = region_name |
| 562 |
|
| 563 |
request_args = cloud.config.get_requests_verify_args() |
| 564 |
cloud_creds['request_args'] = { |
| 565 |
'verify': request_args[0], |
| 566 |
'cert': request_args[1], |
| 567 |
} |
| 568 |
|
| 569 |
return cloud_creds |
| 570 |
|
| 571 |
|
| 572 |
def config_tempest(**kwargs): |
| 573 |
# convert a list of remove values to a dict |
| 574 |
remove = parse_values_to_remove(kwargs.get('remove', [])) |
| 575 |
add = parse_values_to_append(kwargs.get('append', [])) |
| 576 |
set_logging(kwargs.get('debug', False), kwargs.get('verbose', False)) |
| 577 |
|
| 578 |
accounts_path = kwargs.get('test_accounts') |
| 579 |
if kwargs.get('create_accounts_file') is not None: |
| 580 |
accounts_path = kwargs.get('create_accounts_file') |
| 581 |
conf = TempestConf(write_credentials=accounts_path is None) |
| 582 |
set_options(conf, kwargs.get('deployer_input'), |
| 583 |
kwargs.get('non_admin', False), |
| 584 |
kwargs.get('image_path', C.DEFAULT_IMAGE), |
| 585 |
kwargs.get('overrides', []), |
| 586 |
accounts_path, |
| 587 |
kwargs.get('cloud_creds')) |
| 588 |
|
| 589 |
request_args = kwargs.get('cloud_creds', {}).get('request_args', {}) |
| 590 |
credentials = Credentials(conf, not kwargs.get('non_admin', False), |
| 591 |
**request_args) |
| 592 |
clients = ClientManager(conf, credentials) |
| 593 |
|
| 594 |
if kwargs.get('create', False) and kwargs.get('test_accounts') is None: |
| 595 |
if kwargs.get('password_regex') is not None: |
| 596 |
password_regex = kwargs.get('password_regex') |
| 597 |
password_length = kwargs.get('password_length') |
| 598 |
new_password = generate_password_by_regex(password_regex, |
| 599 |
int(password_length)) |
| 600 |
conf.set('identity', 'password', new_password) |
| 601 |
users = Users(clients.projects, clients.roles, clients.users, conf) |
| 602 |
users.create_tempest_users() |
| 603 |
|
| 604 |
services = Services(clients, conf, credentials) |
| 605 |
|
| 606 |
if services.is_service(**{"type": "compute"}): |
| 607 |
flavors = Flavors(clients.flavors, kwargs.get('create', False), conf, |
| 608 |
kwargs.get('flavor_min_mem', C.DEFAULT_FLAVOR_RAM), |
| 609 |
kwargs.get('flavor_min_disk', C.DEFAULT_FLAVOR_DISK), |
| 610 |
no_rng=kwargs.get('no_rng', False)) |
| 611 |
flavors.create_tempest_flavors() |
| 612 |
|
| 613 |
if services.is_service(**{"type": "image"}): |
| 614 |
image = services.get_service('image') |
| 615 |
image.set_image_preferences(kwargs.get('image_disk_format', |
| 616 |
C.DEFAULT_IMAGE_FORMAT), |
| 617 |
kwargs.get('non_admin', False), |
| 618 |
no_rng=kwargs.get('no_rng', False), |
| 619 |
convert=kwargs.get('convert_to_raw', |
| 620 |
False)) |
| 621 |
retry_alt = kwargs.get('retry_image', False) |
| 622 |
image.create_tempest_images(conf, retry_alt=retry_alt) |
| 623 |
|
| 624 |
if services.is_service(**{"type": "network"}): |
| 625 |
network = services.get_service("network") |
| 626 |
network.create_tempest_networks(conf, kwargs.get('network')) |
| 627 |
|
| 628 |
if services.is_service(**{"type": "load-balancer"}): |
| 629 |
load_balancer = services.get_service("load-balancer") |
| 630 |
load_balancer.get_test_server_application(conf) |
| 631 |
|
| 632 |
services.post_configuration() |
| 633 |
services.set_supported_api_versions() |
| 634 |
services.set_service_extensions() |
| 635 |
|
| 636 |
if accounts_path is not None and kwargs.get('test_accounts') is None: |
| 637 |
LOG.info("Creating an accounts.yaml file in: %s", accounts_path) |
| 638 |
accounts.create_accounts_file(kwargs.get('create', False), |
| 639 |
accounts_path, |
| 640 |
conf) |
| 641 |
|
| 642 |
# remove all unwanted values if were specified |
| 643 |
if remove != {}: |
| 644 |
LOG.info("Removing configuration: %s", str(remove)) |
| 645 |
conf.remove_values(remove) |
| 646 |
if add != {}: |
| 647 |
LOG.info("Adding configuration: %s", str(add)) |
| 648 |
conf.append_values(add) |
| 649 |
out_path = kwargs.get('out', 'etc/tempest.conf') |
| 650 |
conf.write(out_path) |
| 651 |
|
| 652 |
|
| 653 |
def main(): |
| 654 |
args = parse_arguments() |
| 655 |
if args.generate_profile: |
| 656 |
profile.generate_profile(args, args.generate_profile) |
| 657 |
sys.exit(0) |
| 658 |
if args.profile: |
| 659 |
profile_args = profile.read_profile_file(args.profile) |
| 660 |
# update default args by values gained from the profile |
| 661 |
# Namespace can't be updated, so translate it to a dict first |
| 662 |
args_dict = vars(args) |
| 663 |
args_dict.update(profile_args) |
| 664 |
args = argparse.Namespace(**args_dict) |
| 665 |
cloud_creds = get_cloud_creds(args) |
| 666 |
config_tempest( |
| 667 |
append=args.append, |
| 668 |
cloud_creds=cloud_creds, |
| 669 |
convert_to_raw=args.convert_to_raw, |
| 670 |
create=args.create, |
| 671 |
create_accounts_file=args.create_accounts_file, |
| 672 |
debug=args.debug, |
| 673 |
deployer_input=args.deployer_input, |
| 674 |
flavor_min_mem=args.flavor_min_mem, |
| 675 |
flavor_min_disk=args.flavor_min_disk, |
| 676 |
image_disk_format=args.image_disk_format, |
| 677 |
image_path=args.image, |
| 678 |
network=args.network, |
| 679 |
non_admin=args.non_admin, |
| 680 |
no_rng=args.no_rng, |
| 681 |
os_cloud=args.os_cloud, |
| 682 |
out=args.out, |
| 683 |
overrides=args.overrides, |
| 684 |
password_regex=args.password_regex, |
| 685 |
password_length=args.password_length, |
| 686 |
remove=args.remove, |
| 687 |
test_accounts=args.test_accounts, |
| 688 |
verbose=args.verbose, |
| 689 |
retry_image=args.retry_image |
| 690 |
) |
| 691 |
|
| 692 |
|
| 693 |
if __name__ == "__main__": |
| 694 |
main() |