Adapt Parser test case to the new template
[functest.git] / functest / opnfv_tests / openstack / tempest / gen_tempest_conf.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 # Execute Multisite Tempest test cases
11 ##
12
13 import ConfigParser
14 import os
15 import re
16 import shutil
17 import functest.utils.functest_utils as ft_utils
18 import functest.utils.functest_logger as ft_logger
19 from run_tempest import configure_tempest
20 from run_tempest import TEMPEST_RESULTS_DIR
21 import functest.utils.functest_constants as ft_constants
22
23 logger = ft_logger.Logger("gen_tempest_conf").getLogger()
24
25 CI_INSTALLER_TYPE = ft_constants.CI_INSTALLER_TYPE
26 CI_INSTALLER_IP = ft_constants.CI_INSTALLER_IP
27
28
29 def configure_tempest_multisite(deployment_dir):
30     """
31     Add/update needed parameters into tempest.conf file generated by Rally
32     """
33     logger.debug("configure the tempest")
34     configure_tempest(deployment_dir)
35
36     logger.debug("Finding tempest.conf file...")
37     tempest_conf_old = os.path.join(deployment_dir, '/tempest.conf')
38     if not os.path.isfile(tempest_conf_old):
39         logger.error("Tempest configuration file %s NOT found."
40                      % tempest_conf_old)
41         exit(-1)
42
43     # Copy tempest.conf to /home/opnfv/functest/results/tempest/
44     cur_path = os.path.split(os.path.realpath(__file__))[0]
45     tempest_conf_file = os.path.join(cur_path, '/tempest_multisite.conf')
46     shutil.copyfile(tempest_conf_old, tempest_conf_file)
47
48     logger.debug("Updating selected tempest.conf parameters...")
49     config = ConfigParser.RawConfigParser()
50     config.read(tempest_conf_file)
51
52     config.set('service_available', 'kingbird', 'true')
53     cmd = "openstack endpoint show kingbird | grep publicurl |\
54            awk '{print $4}' | awk -F '/' '{print $4}'"
55     kingbird_api_version = os.popen(cmd).read()
56     if CI_INSTALLER_TYPE == 'fuel':
57         # For MOS based setup, the service is accessible
58         # via bind host
59         kingbird_conf_path = "/etc/kingbird/kingbird.conf"
60         installer_type = CI_INSTALLER_TYPE
61         installer_ip = CI_INSTALLER_IP
62         installer_username = ft_utils.get_functest_config(
63             "multisite." + installer_type +
64             "_environment.installer_username")
65         installer_password = ft_utils.get_functest_config(
66             "multisite." + installer_type +
67             "_environment.installer_password")
68
69         ssh_options = "-o UserKnownHostsFile=/dev/null -o \
70             StrictHostKeyChecking=no"
71
72         # Get the controller IP from the fuel node
73         cmd = 'sshpass -p %s ssh 2>/dev/null %s %s@%s \
74                 \'fuel node --env 1| grep controller | grep "True\|  1" \
75                 | awk -F\| "{print \$5}"\'' % (installer_password,
76                                                ssh_options,
77                                                installer_username,
78                                                installer_ip)
79         multisite_controller_ip = \
80             "".join(os.popen(cmd).read().split())
81
82         # Login to controller and get bind host details
83         cmd = 'sshpass -p %s ssh 2>/dev/null  %s %s@%s "ssh %s \\" \
84             grep -e "^bind_" %s  \\""' % (installer_password,
85                                           ssh_options,
86                                           installer_username,
87                                           installer_ip,
88                                           multisite_controller_ip,
89                                           kingbird_conf_path)
90         bind_details = os.popen(cmd).read()
91         bind_details = "".join(bind_details.split())
92         # Extract port number from the bind details
93         bind_port = re.findall(r"\D(\d{4})", bind_details)[0]
94         # Extract ip address from the bind details
95         bind_host = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",
96                                bind_details)[0]
97         kingbird_endpoint_url = "http://" + bind_host + ":" + bind_port + \
98                                 "/"
99     else:
100         cmd = "openstack endpoint show kingbird | grep publicurl |\
101                awk '{print $4}' | awk -F '/' '{print $3}'"
102         kingbird_endpoint_url = os.popen(cmd).read()
103
104     try:
105         config.add_section("kingbird")
106     except Exception:
107         logger.info('kingbird section exist')
108     config.set('kingbird', 'endpoint_type', 'publicURL')
109     config.set('kingbird', 'TIME_TO_SYNC', '20')
110     config.set('kingbird', 'endpoint_url', kingbird_endpoint_url)
111     config.set('kingbird', 'api_version', kingbird_api_version)
112     with open(tempest_conf_file, 'wb') as config_file:
113         config.write(config_file)
114
115     return True
116
117
118 def main():
119
120     if not os.path.exists(TEMPEST_RESULTS_DIR):
121         os.makedirs(TEMPEST_RESULTS_DIR)
122
123     deployment_dir = ft_utils.get_deployment_dir()
124     configure_tempest_multisite(deployment_dir)
125
126
127 if __name__ == '__main__':
128     main()