Merge "Bugfix: fix the execution way in copper"
[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
86     if CONST.OS_ENDPOINT_TYPE is not None:
87         services_list = ['compute',
88                          'volume',
89                          'image',
90                          'network',
91                          'data-processing',
92                          'object-storage',
93                          'orchestration']
94         sections = config.sections()
95         for service in services_list:
96             if service not in sections:
97                 config.add_section(service)
98             config.set(service, 'endpoint_type',
99                        CONST.OS_ENDPOINT_TYPE)
100
101     with open(tempest_conf_file, 'wb') as config_file:
102         config.write(config_file)
103
104     # Copy tempest.conf to /home/opnfv/functest/results/tempest/
105     shutil.copyfile(tempest_conf_file,
106                     os.path.join(TEMPEST_RESULTS_DIR, 'tempest.conf'))
107
108     return releng_constants.EXIT_OK
109
110
111 def configure_tempest_multisite(logger, deployment_dir):
112     """
113     Add/update needed parameters into tempest.conf file generated by Rally
114     """
115     logger.debug("configure the tempest")
116     configure_tempest(logger, deployment_dir)
117
118     logger.debug("Finding tempest.conf file...")
119     tempest_conf_old = os.path.join(deployment_dir, 'tempest.conf')
120     if not os.path.isfile(tempest_conf_old):
121         logger.error("Tempest configuration file %s NOT found."
122                      % tempest_conf_old)
123         return releng_constants.EXIT_RUN_ERROR
124
125     # Copy tempest.conf to /home/opnfv/functest/results/tempest/
126     cur_path = os.path.split(os.path.realpath(__file__))[0]
127     tempest_conf_file = os.path.join(cur_path, 'tempest_multisite.conf')
128     shutil.copyfile(tempest_conf_old, tempest_conf_file)
129
130     logger.debug("Updating selected tempest.conf parameters...")
131     config = ConfigParser.RawConfigParser()
132     config.read(tempest_conf_file)
133
134     config.set('service_available', 'kingbird', 'true')
135     # cmd = ("openstack endpoint show kingbird | grep publicurl |"
136     #       "awk '{print $4}' | awk -F '/' '{print $4}'")
137     # kingbird_api_version = os.popen(cmd).read()
138     kingbird_api_version = os_utils.get_endpoint(service_type='kingbird')
139
140     if CI_INSTALLER_TYPE == 'fuel':
141         # For MOS based setup, the service is accessible
142         # via bind host
143         kingbird_conf_path = "/etc/kingbird/kingbird.conf"
144         installer_type = CI_INSTALLER_TYPE
145         installer_ip = CI_INSTALLER_IP
146         installer_username = CONST.__getattribute__(
147             'multisite_{}_installer_username'.format(installer_type))
148         installer_password = CONST.__getattribute__(
149             'multisite_{}_installer_password'.format(installer_type))
150
151         ssh_options = ("-o UserKnownHostsFile=/dev/null -o "
152                        "StrictHostKeyChecking=no")
153
154         # Get the controller IP from the fuel node
155         cmd = 'sshpass -p %s ssh 2>/dev/null %s %s@%s \
156                 \'fuel node --env 1| grep controller | grep "True\|  1" \
157                 | awk -F\| "{print \$5}"\'' % (installer_password,
158                                                ssh_options,
159                                                installer_username,
160                                                installer_ip)
161         multisite_controller_ip = "".join(os.popen(cmd).read().split())
162
163         # Login to controller and get bind host details
164         cmd = 'sshpass -p %s ssh 2>/dev/null  %s %s@%s "ssh %s \\" \
165             grep -e "^bind_" %s  \\""' % (installer_password,
166                                           ssh_options,
167                                           installer_username,
168                                           installer_ip,
169                                           multisite_controller_ip,
170                                           kingbird_conf_path)
171         bind_details = os.popen(cmd).read()
172         bind_details = "".join(bind_details.split())
173         # Extract port number from the bind details
174         bind_port = re.findall(r"\D(\d{4})", bind_details)[0]
175         # Extract ip address from the bind details
176         bind_host = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",
177                                bind_details)[0]
178         kingbird_endpoint_url = "http://%s:%s/" % (bind_host, bind_port)
179     else:
180         # cmd = "openstack endpoint show kingbird | grep publicurl |\
181         #       awk '{print $4}' | awk -F '/' '{print $3}'"
182         # kingbird_endpoint_url = os.popen(cmd).read()
183         kingbird_endpoint_url = os_utils.get_endpoint(service_type='kingbird')
184
185     try:
186         config.add_section("kingbird")
187     except Exception:
188         logger.info('kingbird section exist')
189     config.set('kingbird', 'endpoint_type', 'publicURL')
190     config.set('kingbird', 'TIME_TO_SYNC', '20')
191     config.set('kingbird', 'endpoint_url', kingbird_endpoint_url)
192     config.set('kingbird', 'api_version', kingbird_api_version)
193     with open(tempest_conf_file, 'wb') as config_file:
194         config.write(config_file)
195
196     return releng_constants.EXIT_OK