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