Merge "Update rally version"
[functest.git] / testcases / features / promise.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 # Maintainer : jose.lausuch@ericsson.com
11 #
12 import argparse
13 import json
14 import os
15 import subprocess
16 import time
17
18 import functest.utils.functest_logger as ft_logger
19 import functest.utils.functest_utils as ft_utils
20 import functest.utils.openstack_utils as openstack_utils
21 import keystoneclient.v2_0.client as ksclient
22 from neutronclient.v2_0 import client as ntclient
23 import novaclient.client as nvclient
24
25
26 parser = argparse.ArgumentParser()
27
28 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
29 parser.add_argument("-r", "--report",
30                     help="Create json result file",
31                     action="store_true")
32 args = parser.parse_args()
33
34
35 dirs = ft_utils.get_functest_config('general.directories')
36 PROMISE_REPO = dirs.get('dir_repo_promise')
37 RESULTS_DIR = ft_utils.get_functest_config('general.directories.dir_results')
38
39 TENANT_NAME = ft_utils.get_functest_config('promise.tenant_name')
40 TENANT_DESCRIPTION = \
41     ft_utils.get_functest_config('promise.tenant_description')
42 USER_NAME = ft_utils.get_functest_config('promise.user_name')
43 USER_PWD = ft_utils.get_functest_config('promise.user_pwd')
44 IMAGE_NAME = ft_utils.get_functest_config('promise.image_name')
45 FLAVOR_NAME = ft_utils.get_functest_config('promise.flavor_name')
46 FLAVOR_VCPUS = ft_utils.get_functest_config('promise.flavor_vcpus')
47 FLAVOR_RAM = ft_utils.get_functest_config('promise.flavor_ram')
48 FLAVOR_DISK = ft_utils.get_functest_config('promise.flavor_disk')
49
50
51 GLANCE_IMAGE_FILENAME = \
52     ft_utils.get_functest_config('general.openstack.image_file_name')
53 GLANCE_IMAGE_FORMAT = \
54     ft_utils.get_functest_config('general.openstack.image_disk_format')
55 GLANCE_IMAGE_PATH = \
56     ft_utils.get_functest_config('general.directories.dir_functest_data') + \
57     "/" + GLANCE_IMAGE_FILENAME
58
59 NET_NAME = ft_utils.get_functest_config('promise.network_name')
60 SUBNET_NAME = ft_utils.get_functest_config('promise.subnet_name')
61 SUBNET_CIDR = ft_utils.get_functest_config('promise.subnet_cidr')
62 ROUTER_NAME = ft_utils.get_functest_config('promise.router_name')
63
64
65 """ logging configuration """
66 logger = ft_logger.Logger("promise").getLogger()
67
68
69 def main():
70     exit_code = -1
71     start_time = time.time()
72     ks_creds = openstack_utils.get_credentials("keystone")
73     nv_creds = openstack_utils.get_credentials("nova")
74     nt_creds = openstack_utils.get_credentials("neutron")
75
76     keystone = ksclient.Client(**ks_creds)
77
78     user_id = openstack_utils.get_user_id(keystone, ks_creds['username'])
79     if user_id == '':
80         logger.error("Error : Failed to get id of %s user" %
81                      ks_creds['username'])
82         exit(-1)
83
84     logger.info("Creating tenant '%s'..." % TENANT_NAME)
85     tenant_id = openstack_utils.create_tenant(
86         keystone, TENANT_NAME, TENANT_DESCRIPTION)
87     if not tenant_id:
88         logger.error("Error : Failed to create %s tenant" % TENANT_NAME)
89         exit(-1)
90     logger.debug("Tenant '%s' created successfully." % TENANT_NAME)
91
92     roles_name = ["admin", "Admin"]
93     role_id = ''
94     for role_name in roles_name:
95         if role_id == '':
96             role_id = openstack_utils.get_role_id(keystone, role_name)
97
98     if role_id == '':
99         logger.error("Error : Failed to get id for %s role" % role_name)
100         exit(-1)
101
102     logger.info("Adding role '%s' to tenant '%s'..." % (role_id, TENANT_NAME))
103     if not openstack_utils.add_role_user(keystone, user_id,
104                                          role_id, tenant_id):
105         logger.error("Error : Failed to add %s on tenant %s" %
106                      (ks_creds['username'], TENANT_NAME))
107         exit(-1)
108     logger.debug("Role added successfully.")
109
110     logger.info("Creating user '%s'..." % USER_NAME)
111     user_id = openstack_utils.create_user(
112         keystone, USER_NAME, USER_PWD, None, tenant_id)
113
114     if not user_id:
115         logger.error("Error : Failed to create %s user" % USER_NAME)
116         exit(-1)
117     logger.debug("User '%s' created successfully." % USER_NAME)
118
119     logger.info("Updating OpenStack credentials...")
120     ks_creds.update({
121         "username": TENANT_NAME,
122         "password": TENANT_NAME,
123         "tenant_name": TENANT_NAME,
124     })
125
126     nt_creds.update({
127         "tenant_name": TENANT_NAME,
128     })
129
130     nv_creds.update({
131         "project_id": TENANT_NAME,
132     })
133
134     glance = openstack_utils.get_glance_client()
135     nova = nvclient.Client("2", **nv_creds)
136
137     logger.info("Creating image '%s' from '%s'..." % (IMAGE_NAME,
138                                                       GLANCE_IMAGE_PATH))
139     image_id = openstack_utils.create_glance_image(glance,
140                                                    IMAGE_NAME,
141                                                    GLANCE_IMAGE_PATH)
142     if not image_id:
143         logger.error("Failed to create the Glance image...")
144         exit(-1)
145     logger.debug("Image '%s' with ID '%s' created successfully." % (IMAGE_NAME,
146                                                                     image_id))
147     flavor_id = openstack_utils.get_flavor_id(nova, FLAVOR_NAME)
148     if flavor_id == '':
149         logger.info("Creating flavor '%s'..." % FLAVOR_NAME)
150         flavor_id = openstack_utils.create_flavor(nova,
151                                                   FLAVOR_NAME,
152                                                   FLAVOR_RAM,
153                                                   FLAVOR_DISK,
154                                                   FLAVOR_VCPUS)
155         if not flavor_id:
156             logger.error("Failed to create the Flavor...")
157             exit(-1)
158         logger.debug("Flavor '%s' with ID '%s' created successfully." %
159                      (FLAVOR_NAME, flavor_id))
160     else:
161         logger.debug("Using existing flavor '%s' with ID '%s'..."
162                      % (FLAVOR_NAME, flavor_id))
163
164     neutron = ntclient.Client(**nt_creds)
165
166     network_dic = openstack_utils.create_network_full(neutron,
167                                                       NET_NAME,
168                                                       SUBNET_NAME,
169                                                       ROUTER_NAME,
170                                                       SUBNET_CIDR)
171     if not network_dic:
172         logger.error("Failed to create the private network...")
173         exit(-1)
174
175     logger.info("Exporting environment variables...")
176     os.environ["NODE_ENV"] = "functest"
177     os.environ["OS_TENANT_NAME"] = TENANT_NAME
178     os.environ["OS_USERNAME"] = USER_NAME
179     os.environ["OS_PASSWORD"] = USER_PWD
180     os.environ["OS_TEST_IMAGE"] = image_id
181     os.environ["OS_TEST_FLAVOR"] = flavor_id
182     os.environ["OS_TEST_NETWORK"] = network_dic["net_id"]
183
184     os.chdir(PROMISE_REPO)
185     results_file_name = RESULTS_DIR + '/' + 'promise-results.json'
186     results_file = open(results_file_name, 'w+')
187     cmd = 'npm run -s test -- --reporter json'
188
189     logger.info("Running command: %s" % cmd)
190     ret = subprocess.call(cmd, shell=True, stdout=results_file,
191                           stderr=subprocess.STDOUT)
192     results_file.close()
193
194     if ret == 0:
195         logger.info("The test succeeded.")
196         # test_status = 'OK'
197     else:
198         logger.info("The command '%s' failed." % cmd)
199         # test_status = "Failed"
200
201     # Print output of file
202     with open(results_file_name, 'r') as results_file:
203         data = results_file.read()
204         logger.debug("\n%s" % data)
205         json_data = json.loads(data)
206
207         suites = json_data["stats"]["suites"]
208         tests = json_data["stats"]["tests"]
209         passes = json_data["stats"]["passes"]
210         pending = json_data["stats"]["pending"]
211         failures = json_data["stats"]["failures"]
212         start_time_json = json_data["stats"]["start"]
213         end_time = json_data["stats"]["end"]
214         duration = float(json_data["stats"]["duration"]) / float(1000)
215
216     logger.info("\n"
217                 "****************************************\n"
218                 "          Promise test report\n\n"
219                 "****************************************\n"
220                 " Suites:  \t%s\n"
221                 " Tests:   \t%s\n"
222                 " Passes:  \t%s\n"
223                 " Pending: \t%s\n"
224                 " Failures:\t%s\n"
225                 " Start:   \t%s\n"
226                 " End:     \t%s\n"
227                 " Duration:\t%s\n"
228                 "****************************************\n\n"
229                 % (suites, tests, passes, pending, failures,
230                    start_time_json, end_time, duration))
231
232     if args.report:
233         stop_time = time.time()
234         json_results = {"timestart": start_time, "duration": duration,
235                         "tests": int(tests), "failures": int(failures)}
236         logger.debug("Promise Results json: " + str(json_results))
237
238         # criteria for Promise in Release B was 100% of tests OK
239         status = "FAIL"
240         if int(tests) > 32 and int(failures) < 1:
241             status = "PASS"
242             exit_code = 0
243
244         ft_utils.push_results_to_db("promise",
245                                     "promise",
246                                     start_time,
247                                     stop_time,
248                                     status,
249                                     json_results)
250
251     exit(exit_code)
252
253
254 if __name__ == '__main__':
255     main()