Stop forcing admin_scope_domain = True
[functest.git] / functest / opnfv_tests / openstack / tempest / conf_utils.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2015 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10
11 """Tempest configuration utilities."""
12
13 import json
14 import logging
15 import os
16 import subprocess
17
18 import pkg_resources
19 from six.moves import configparser
20 import yaml
21
22 from functest.utils import config
23 from functest.utils import env
24 from functest.utils import functest_utils
25
26
27 GLANCE_IMAGE_PATH = os.path.join(
28     getattr(config.CONF, 'dir_functest_images'),
29     getattr(config.CONF, 'openstack_image_file_name'))
30 TEMPEST_CUSTOM = pkg_resources.resource_filename(
31     'functest', 'opnfv_tests/openstack/tempest/custom_tests/test_list.txt')
32 TEMPEST_BLACKLIST = pkg_resources.resource_filename(
33     'functest', 'opnfv_tests/openstack/tempest/custom_tests/blacklist.yaml')
34 TEMPEST_CONF_YAML = pkg_resources.resource_filename(
35     'functest', 'opnfv_tests/openstack/tempest/custom_tests/tempest_conf.yaml')
36
37 CI_INSTALLER_TYPE = env.get('INSTALLER_TYPE')
38
39 """ logging configuration """
40 LOGGER = logging.getLogger(__name__)
41
42
43 def create_verifier():
44     """Create new verifier"""
45     LOGGER.info("Create verifier from existing repo...")
46     cmd = ['rally', 'verify', 'delete-verifier',
47            '--id', str(getattr(config.CONF, 'tempest_verifier_name')),
48            '--force']
49     try:
50         output = subprocess.check_output(cmd)
51         LOGGER.info("%s\n%s", " ".join(cmd), output)
52     except subprocess.CalledProcessError:
53         pass
54
55     cmd = ['rally', 'verify', 'create-verifier',
56            '--source', str(getattr(config.CONF, 'dir_repo_tempest')),
57            '--name', str(getattr(config.CONF, 'tempest_verifier_name')),
58            '--type', 'tempest', '--system-wide']
59     output = subprocess.check_output(cmd)
60     LOGGER.info("%s\n%s", " ".join(cmd), output)
61     return get_verifier_id()
62
63
64 def get_verifier_id():
65     """
66     Returns verifier id for current Tempest
67     """
68     cmd = ("rally verify list-verifiers | awk '/" +
69            getattr(config.CONF, 'tempest_verifier_name') +
70            "/ {print $2}'")
71     proc = subprocess.Popen(cmd, shell=True,
72                             stdout=subprocess.PIPE,
73                             stderr=subprocess.STDOUT)
74     verifier_uuid = proc.stdout.readline().rstrip()
75     return verifier_uuid
76
77
78 def get_verifier_repo_dir(verifier_id):
79     """
80     Returns installed verifier repo directory for Tempest
81     """
82     return os.path.join(getattr(config.CONF, 'dir_rally_inst'),
83                         'verification',
84                         'verifier-{}'.format(verifier_id),
85                         'repo')
86
87
88 def get_verifier_deployment_dir(verifier_id, deployment_id):
89     """
90     Returns Rally deployment directory for current verifier
91     """
92     return os.path.join(getattr(config.CONF, 'dir_rally_inst'),
93                         'verification',
94                         'verifier-{}'.format(verifier_id),
95                         'for-deployment-{}'.format(deployment_id))
96
97
98 def update_tempest_conf_file(conf_file, rconfig):
99     """Update defined paramters into tempest config file"""
100     with open(TEMPEST_CONF_YAML) as yfile:
101         conf_yaml = yaml.safe_load(yfile)
102     if conf_yaml:
103         sections = rconfig.sections()
104         for section in conf_yaml:
105             if section not in sections:
106                 rconfig.add_section(section)
107             sub_conf = conf_yaml.get(section)
108             for key, value in sub_conf.items():
109                 rconfig.set(section, key, value)
110
111     with open(conf_file, 'wb') as config_file:
112         rconfig.write(config_file)
113
114
115 def configure_tempest_update_params(
116         tempest_conf_file, image_id=None, flavor_id=None,
117         compute_cnt=1, image_alt_id=None, flavor_alt_id=None,
118         admin_role_name='admin', cidr='192.168.120.0/24',
119         domain_id='default'):
120     # pylint: disable=too-many-branches,too-many-arguments,too-many-statements
121     """
122     Add/update needed parameters into tempest.conf file
123     """
124     LOGGER.debug("Updating selected tempest.conf parameters...")
125     rconfig = configparser.RawConfigParser()
126     rconfig.read(tempest_conf_file)
127     rconfig.set('compute', 'volume_device_name', env.get('VOLUME_DEVICE_NAME'))
128     if image_id is not None:
129         rconfig.set('compute', 'image_ref', image_id)
130     if image_alt_id is not None:
131         rconfig.set('compute', 'image_ref_alt', image_alt_id)
132     if flavor_id is not None:
133         rconfig.set('compute', 'flavor_ref', flavor_id)
134     if flavor_alt_id is not None:
135         rconfig.set('compute', 'flavor_ref_alt', flavor_alt_id)
136     if compute_cnt > 1:
137         # enable multinode tests
138         rconfig.set('compute', 'min_compute_nodes', compute_cnt)
139         rconfig.set('compute-feature-enabled', 'live_migration', True)
140     filters = ['RetryFilter', 'AvailabilityZoneFilter', 'ComputeFilter',
141                'ComputeCapabilitiesFilter', 'ImagePropertiesFilter',
142                'ServerGroupAntiAffinityFilter', 'ServerGroupAffinityFilter']
143     rconfig.set(
144         'compute-feature-enabled', 'scheduler_available_filters',
145         functest_utils.convert_list_to_ini(filters))
146     if os.environ.get('OS_REGION_NAME'):
147         rconfig.set('identity', 'region', os.environ.get('OS_REGION_NAME'))
148     if env.get("NEW_USER_ROLE").lower() != "member":
149         rconfig.set(
150             'auth', 'tempest_roles',
151             functest_utils.convert_list_to_ini([env.get("NEW_USER_ROLE")]))
152     if not json.loads(env.get("USE_DYNAMIC_CREDENTIALS").lower()):
153         rconfig.set('auth', 'use_dynamic_credentials', False)
154         account_file = os.path.join(
155             getattr(config.CONF, 'dir_functest_data'), 'accounts.yaml')
156         assert os.path.exists(
157             account_file), "{} doesn't exist".format(account_file)
158         rconfig.set('auth', 'test_accounts_file', account_file)
159     rconfig.set('identity', 'auth_version', 'v3')
160     rconfig.set('identity', 'admin_role', admin_role_name)
161     rconfig.set('identity', 'default_domain_id', domain_id)
162     if not rconfig.has_section('network'):
163         rconfig.add_section('network')
164     rconfig.set('network', 'default_network', cidr)
165     rconfig.set('network', 'project_network_cidr', cidr)
166     rconfig.set('network', 'project_networks_reachable', False)
167     rconfig.set(
168         'identity', 'v3_endpoint_type',
169         os.environ.get('OS_INTERFACE', 'public'))
170
171     sections = rconfig.sections()
172     services_list = [
173         'compute', 'volume', 'image', 'network', 'data-processing',
174         'object-storage', 'orchestration']
175     for service in services_list:
176         if service not in sections:
177             rconfig.add_section(service)
178         rconfig.set(
179             service, 'endpoint_type', os.environ.get('OS_INTERFACE', 'public'))
180
181     LOGGER.debug('Add/Update required params defined in tempest_conf.yaml '
182                  'into tempest.conf file')
183     update_tempest_conf_file(tempest_conf_file, rconfig)
184
185
186 def configure_verifier(deployment_dir):
187     """
188     Execute rally verify configure-verifier, which generates tempest.conf
189     """
190     cmd = ['rally', 'verify', 'configure-verifier', '--reconfigure',
191            '--id', str(getattr(config.CONF, 'tempest_verifier_name'))]
192     output = subprocess.check_output(cmd)
193     LOGGER.info("%s\n%s", " ".join(cmd), output)
194
195     LOGGER.debug("Looking for tempest.conf file...")
196     tempest_conf_file = os.path.join(deployment_dir, "tempest.conf")
197     if not os.path.isfile(tempest_conf_file):
198         LOGGER.error("Tempest configuration file %s NOT found.",
199                      tempest_conf_file)
200         return None
201     return tempest_conf_file