Merge "First update of Functest user guide for Danube"
[functest.git] / functest / opnfv_tests / openstack / tempest / conf_utils.py
1 #!/usr/bin/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 import ConfigParser
11 import os
12 import re
13 import shutil
14
15 import opnfv.utils.constants as releng_constants
16
17 from functest.utils.constants import CONST
18 import functest.utils.functest_utils as ft_utils
19 import functest.utils.openstack_utils as os_utils
20
21
22 IMAGE_ID_ALT = None
23 FLAVOR_ID_ALT = None
24 REPO_PATH = CONST.dir_repo_functest
25 GLANCE_IMAGE_PATH = os.path.join(CONST.dir_functest_data,
26                                  CONST.openstack_image_file_name)
27 TEMPEST_TEST_LIST_DIR = CONST.dir_tempest_cases
28 TEMPEST_RESULTS_DIR = os.path.join(CONST.dir_results,
29                                    'tempest')
30 TEMPEST_CUSTOM = os.path.join(REPO_PATH, TEMPEST_TEST_LIST_DIR,
31                               'test_list.txt')
32 TEMPEST_BLACKLIST = os.path.join(REPO_PATH, TEMPEST_TEST_LIST_DIR,
33                                  'blacklist.txt')
34 TEMPEST_DEFCORE = os.path.join(REPO_PATH, TEMPEST_TEST_LIST_DIR,
35                                'defcore_req.txt')
36 TEMPEST_RAW_LIST = os.path.join(TEMPEST_RESULTS_DIR, 'test_raw_list.txt')
37 TEMPEST_LIST = os.path.join(TEMPEST_RESULTS_DIR, 'test_list.txt')
38
39 CI_INSTALLER_TYPE = CONST.INSTALLER_TYPE
40 CI_INSTALLER_IP = CONST.INSTALLER_IP
41
42
43 def configure_tempest(logger, deployment_dir, IMAGE_ID=None, FLAVOR_ID=None):
44     """
45     Add/update needed parameters into tempest.conf file generated by Rally
46     """
47     tempest_conf_file = os.path.join(deployment_dir, "tempest.conf")
48     if os.path.isfile(tempest_conf_file):
49         logger.debug("Verifier is already configured.")
50         logger.debug("Reconfiguring the current verifier...")
51         cmd = "rally verify configure-verifier --reconfigure"
52     else:
53         logger.info("Configuring the verifier...")
54         cmd = "rally verify configure-verifier"
55     ft_utils.execute_command(cmd)
56
57     logger.debug("Looking for tempest.conf file...")
58     if not os.path.isfile(tempest_conf_file):
59         logger.error("Tempest configuration file %s NOT found."
60                      % tempest_conf_file)
61         return releng_constants.EXIT_RUN_ERROR
62
63     logger.debug("Updating selected tempest.conf parameters...")
64     config = ConfigParser.RawConfigParser()
65     config.read(tempest_conf_file)
66     config.set(
67         'compute',
68         'fixed_network_name',
69         CONST.tempest_private_net_name)
70     if CONST.tempest_use_custom_images:
71         if IMAGE_ID is not None:
72             config.set('compute', 'image_ref', IMAGE_ID)
73         if IMAGE_ID_ALT is not None:
74             config.set('compute', 'image_ref_alt', IMAGE_ID_ALT)
75     if CONST.tempest_use_custom_flavors:
76         if FLAVOR_ID is not None:
77             config.set('compute', 'flavor_ref', FLAVOR_ID)
78         if FLAVOR_ID_ALT is not None:
79             config.set('compute', 'flavor_ref_alt', FLAVOR_ID_ALT)
80     config.set('identity', 'tenant_name', CONST.tempest_identity_tenant_name)
81     config.set('identity', 'username', CONST.tempest_identity_user_name)
82     config.set('identity', 'password', CONST.tempest_identity_user_password)
83     config.set(
84         'validation', 'ssh_timeout', CONST.tempest_validation_ssh_timeout)
85     config.set('object-storage', 'operator_role',
86                CONST.tempest_object_storage_operator_role)
87
88     if CONST.OS_ENDPOINT_TYPE is not None:
89         services_list = ['compute',
90                          'volume',
91                          'image',
92                          'network',
93                          'data-processing',
94                          'object-storage',
95                          'orchestration']
96         sections = config.sections()
97         for service in services_list:
98             if service not in sections:
99                 config.add_section(service)
100             config.set(service, 'endpoint_type',
101                        CONST.OS_ENDPOINT_TYPE)
102
103     with open(tempest_conf_file, 'wb') as config_file:
104         config.write(config_file)
105
106     # Copy tempest.conf to /home/opnfv/functest/results/tempest/
107     shutil.copyfile(tempest_conf_file,
108                     os.path.join(TEMPEST_RESULTS_DIR, 'tempest.conf'))
109
110     return releng_constants.EXIT_OK
111
112
113 def configure_tempest_multisite(logger, deployment_dir):
114     """
115     Add/update needed parameters into tempest.conf file generated by Rally
116     """
117     logger.debug("configure the tempest")
118     configure_tempest(logger, deployment_dir)
119
120     logger.debug("Finding tempest.conf file...")
121     tempest_conf_old = os.path.join(deployment_dir, 'tempest.conf')
122     if not os.path.isfile(tempest_conf_old):
123         logger.error("Tempest configuration file %s NOT found."
124                      % tempest_conf_old)
125         return releng_constants.EXIT_RUN_ERROR
126
127     # Copy tempest.conf to /home/opnfv/functest/results/tempest/
128     cur_path = os.path.split(os.path.realpath(__file__))[0]
129     tempest_conf_file = os.path.join(cur_path, 'tempest_multisite.conf')
130     shutil.copyfile(tempest_conf_old, tempest_conf_file)
131
132     logger.debug("Updating selected tempest.conf parameters...")
133     config = ConfigParser.RawConfigParser()
134     config.read(tempest_conf_file)
135
136     config.set('service_available', 'kingbird', 'true')
137     # cmd = ("openstack endpoint show kingbird | grep publicurl |"
138     #       "awk '{print $4}' | awk -F '/' '{print $4}'")
139     # kingbird_api_version = os.popen(cmd).read()
140     kingbird_api_version = os_utils.get_endpoint(service_type='kingbird')
141
142     if CI_INSTALLER_TYPE == 'fuel':
143         # For MOS based setup, the service is accessible
144         # via bind host
145         kingbird_conf_path = "/etc/kingbird/kingbird.conf"
146         installer_type = CI_INSTALLER_TYPE
147         installer_ip = CI_INSTALLER_IP
148         installer_username = CONST.__getattribute__(
149             'multisite_{}_installer_username'.format(installer_type))
150         installer_password = CONST.__getattribute__(
151             'multisite_{}_installer_password'.format(installer_type))
152
153         ssh_options = ("-o UserKnownHostsFile=/dev/null -o "
154                        "StrictHostKeyChecking=no")
155
156         # Get the controller IP from the fuel node
157         cmd = 'sshpass -p %s ssh 2>/dev/null %s %s@%s \
158                 \'fuel node --env 1| grep controller | grep "True\|  1" \
159                 | awk -F\| "{print \$5}"\'' % (installer_password,
160                                                ssh_options,
161                                                installer_username,
162                                                installer_ip)
163         multisite_controller_ip = "".join(os.popen(cmd).read().split())
164
165         # Login to controller and get bind host details
166         cmd = 'sshpass -p %s ssh 2>/dev/null  %s %s@%s "ssh %s \\" \
167             grep -e "^bind_" %s  \\""' % (installer_password,
168                                           ssh_options,
169                                           installer_username,
170                                           installer_ip,
171                                           multisite_controller_ip,
172                                           kingbird_conf_path)
173         bind_details = os.popen(cmd).read()
174         bind_details = "".join(bind_details.split())
175         # Extract port number from the bind details
176         bind_port = re.findall(r"\D(\d{4})", bind_details)[0]
177         # Extract ip address from the bind details
178         bind_host = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",
179                                bind_details)[0]
180         kingbird_endpoint_url = "http://%s:%s/" % (bind_host, bind_port)
181     else:
182         # cmd = "openstack endpoint show kingbird | grep publicurl |\
183         #       awk '{print $4}' | awk -F '/' '{print $3}'"
184         # kingbird_endpoint_url = os.popen(cmd).read()
185         kingbird_endpoint_url = os_utils.get_endpoint(service_type='kingbird')
186
187     try:
188         config.add_section("kingbird")
189     except Exception:
190         logger.info('kingbird section exist')
191     config.set('kingbird', 'endpoint_type', 'publicURL')
192     config.set('kingbird', 'TIME_TO_SYNC', '20')
193     config.set('kingbird', 'endpoint_url', kingbird_endpoint_url)
194     config.set('kingbird', 'api_version', kingbird_api_version)
195     with open(tempest_conf_file, 'wb') as config_file:
196         config.write(config_file)
197
198     return releng_constants.EXIT_OK