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