Use python json parser to get the results form the Promise json report
[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 logging
15 import os
16 import subprocess
17 import sys
18 import time
19 import yaml
20
21 import keystoneclient.v2_0.client as ksclient
22 import glanceclient.client as glclient
23 import novaclient.client as nvclient
24 from neutronclient.v2_0 import client as ntclient
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 with open('/home/opnfv/functest/conf/config_functest.yaml') as f:
35     functest_yaml = yaml.safe_load(f)
36
37 dirs = functest_yaml.get('general').get('directories')
38 FUNCTEST_REPO = dirs.get('dir_repo_functest')
39 PROMISE_REPO = dirs.get('dir_repo_promise')
40 TEST_DB = functest_yaml.get('results').get('test_db_url')
41
42 TENANT_NAME = functest_yaml.get('promise').get('general').get('tenant_name')
43 TENANT_DESCRIPTION = functest_yaml.get('promise').get(
44     'general').get('tenant_description')
45 USER_NAME = functest_yaml.get('promise').get('general').get('user_name')
46 USER_PWD = functest_yaml.get('promise').get('general').get('user_pwd')
47 IMAGE_NAME = functest_yaml.get('promise').get('general').get('image_name')
48 FLAVOR_NAME = functest_yaml.get('promise').get('general').get('flavor_name')
49 FLAVOR_VCPUS = functest_yaml.get('promise').get('general').get('flavor_vcpus')
50 FLAVOR_RAM = functest_yaml.get('promise').get('general').get('flavor_ram')
51 FLAVOR_DISK = functest_yaml.get('promise').get('general').get('flavor_disk')
52
53
54 GLANCE_IMAGE_FILENAME = functest_yaml.get('general'). \
55     get('openstack').get('image_file_name')
56 GLANCE_IMAGE_FORMAT = functest_yaml.get('general'). \
57     get('openstack').get('image_disk_format')
58 GLANCE_IMAGE_PATH = functest_yaml.get('general'). \
59     get('directories').get('dir_functest_data') + "/" + GLANCE_IMAGE_FILENAME
60
61 sys.path.append('%s/testcases' % FUNCTEST_REPO)
62 import functest_utils
63
64 """ logging configuration """
65 logger = logging.getLogger('Promise')
66 logger.setLevel(logging.DEBUG)
67
68 ch = logging.StreamHandler()
69
70 if args.debug:
71     ch.setLevel(logging.DEBUG)
72 else:
73     ch.setLevel(logging.INFO)
74
75 formatter = logging.Formatter('%(asctime)s - %(name)s'
76                               '- %(levelname)s - %(message)s')
77
78 ch.setFormatter(formatter)
79 logger.addHandler(ch)
80
81
82
83 def create_image(glance_client, name):
84
85     return image_id
86
87
88 def main():
89     ks_creds = functest_utils.get_credentials("keystone")
90     nv_creds = functest_utils.get_credentials("nova")
91     nt_creds = functest_utils.get_credentials("neutron")
92
93     keystone = ksclient.Client(**ks_creds)
94
95     user_id = functest_utils.get_user_id(keystone, ks_creds['username'])
96     if user_id == '':
97         logger.error("Error : Failed to get id of %s user" %
98                      ks_creds['username'])
99         exit(-1)
100
101     logger.info("Creating tenant '%s'..." % TENANT_NAME)
102     tenant_id = functest_utils.create_tenant(
103         keystone, TENANT_NAME, TENANT_DESCRIPTION)
104     if tenant_id == '':
105         logger.error("Error : Failed to create %s tenant" % TENANT_NAME)
106         exit(-1)
107     logger.debug("Tenant '%s' created successfully." % TENANT_NAME)
108
109     roles_name = ["admin", "Admin"]
110     role_id = ''
111     for role_name in roles_name:
112         if role_id == '':
113             role_id = functest_utils.get_role_id(keystone, role_name)
114
115     if role_id == '':
116         logger.error("Error : Failed to get id for %s role" % role_name)
117         exit(-1)
118
119     logger.info("Adding role '%s' to tenant '%s'..." % (role_id,TENANT_NAME))
120     if not functest_utils.add_role_user(keystone, user_id, role_id, tenant_id):
121         logger.error("Error : Failed to add %s on tenant %s" %
122                      (ks_creds['username'],TENANT_NAME))
123         exit(-1)
124     logger.debug("Role added successfully.")
125
126     logger.info("Creating user '%s'..." % USER_NAME)
127     user_id = functest_utils.create_user(
128         keystone, USER_NAME, USER_PWD, None, tenant_id)
129
130     if user_id == '':
131         logger.error("Error : Failed to create %s user" % USER_NAME)
132         exit(-1)
133     logger.debug("User '%s' created successfully." % USER_NAME)
134
135     logger.info("Updating OpenStack credentials...")
136     ks_creds.update({
137         "username": TENANT_NAME,
138         "password": TENANT_NAME,
139         "tenant_name": TENANT_NAME,
140     })
141
142     nt_creds.update({
143         "tenant_name": TENANT_NAME,
144     })
145
146     nv_creds.update({
147         "project_id": TENANT_NAME,
148     })
149
150     glance_endpoint = keystone.service_catalog.url_for(service_type='image',
151                                                        endpoint_type='publicURL')
152     glance = glclient.Client(1, glance_endpoint, token=keystone.auth_token)
153     nova = nvclient.Client("2", **nv_creds)
154
155
156     logger.info("Creating image '%s' from '%s'..." % (IMAGE_NAME,
157                                                        GLANCE_IMAGE_PATH))
158     image_id = functest_utils.create_glance_image(glance,
159                                                   IMAGE_NAME,
160                                                   GLANCE_IMAGE_PATH)
161     if not image_id:
162         logger.error("Failed to create the Glance image...")
163         exit(-1)
164     logger.debug("Image '%s' with ID '%s' created successfully." % (IMAGE_NAME,
165                                                                     image_id))
166     flavor_id = functest_utils.get_flavor_id(nova, FLAVOR_NAME)
167     if flavor_id == '':
168         logger.info("Creating flavor '%s'..." % FLAVOR_NAME)
169         flavor_id = functest_utils.create_flavor(nova,
170                                                  FLAVOR_NAME,
171                                                  FLAVOR_RAM,
172                                                  FLAVOR_DISK,
173                                                  FLAVOR_VCPUS)
174         if not flavor_id:
175             logger.error("Failed to create the Flavor...")
176             exit(-1)
177         logger.debug("Flavor '%s' with ID '%s' created successfully." %
178                                             (FLAVOR_NAME, flavor_id))
179     else:
180         logger.debug("Using existing flavor '%s' with ID '%s'..." % (FLAVOR_NAME,
181                                                                     flavor_id))
182
183
184     neutron = ntclient.Client(**nt_creds)
185     private_net=functest_utils.get_private_net(neutron)
186     if private_net == None:
187         logger.error("There is no private network in the deployment. Aborting...")
188         exit(-1)
189     logger.debug("Using private network '%s' (%s)." % (private_net['name'],
190                                                        private_net['id']))
191
192     logger.info("Exporting environment variables...")
193     os.environ["NODE_ENV"] = "functest"
194     os.environ["OS_TENANT_NAME"] = TENANT_NAME
195     os.environ["OS_USERNAME"] = USER_NAME
196     os.environ["OS_PASSWORD"] = USER_PWD
197     os.environ["OS_TEST_IMAGE"] = image_id
198     os.environ["OS_TEST_FLAVOR"] = flavor_id
199     os.environ["OS_TEST_NETWORK"] = private_net['id']
200
201
202     os.chdir(PROMISE_REPO)
203     results_file_name='promise-results.json'
204     results_file=open(results_file_name,'w+')
205     cmd = 'npm run -s test -- --reporter json'
206
207     logger.info("Running command: %s" % cmd)
208     ret = subprocess.call(cmd, shell=True, stdout=results_file, \
209                     stderr=subprocess.STDOUT)
210     results_file.close()
211
212     if ret == 0:
213         logger.info("The test succeeded.")
214         test_status = 'OK'
215     else:
216         logger.info("The command '%s' failed." % cmd)
217         test_status = "Failed"
218
219     # Print output of file
220     with open(results_file_name,'r') as results_file:
221         data = results_file.read()
222         logger.debug("\n%s" % data)
223         json_data = json.loads(data)
224
225         suites = json_data["stats"]["suites"]
226         tests = json_data["stats"]["tests"]
227         passes = json_data["stats"]["passes"]
228         pending = json_data["stats"]["pending"]
229         failures = json_data["stats"]["failures"]
230         start_time = json_data["stats"]["start"]
231         end_time = json_data["stats"]["end"]
232         duration = float(json_data["stats"]["duration"])/float(1000)
233
234     logger.info("\n" \
235     "****************************************\n"\
236     "          Promise test report\n\n"\
237     "****************************************\n"\
238     " Suites:  \t%s\n"\
239     " Tests:   \t%s\n"\
240     " Passes:  \t%s\n"\
241     " Pending: \t%s\n"\
242     " Failures:\t%s\n"\
243     " Start:   \t%s\n"\
244     " End:     \t%s\n"\
245     " Duration:\t%s\n"\
246     "****************************************\n\n"\
247     % (suites, tests, passes, pending, failures, start_time, end_time, duration))
248
249
250     if args.report:
251         pod_name = functest_utils.get_pod_name(logger)
252         installer = functest_utils.get_installer_type(logger)
253         scenario = functest_utils.get_scenario(logger)
254         git_version = functest_utils.get_git_branch(PROMISE_REPO)
255         url = TEST_DB + "/results"
256
257         json_results = {"timestart": start_time, "duration": duration,
258                         "tests": int(tests), "failures": int(failures)}
259         logger.debug("Results json: "+str(json_results))
260
261         params = {"project_name": "promise", "case_name": "promise",
262                   "pod_name": str(pod_name), 'installer': installer,
263                   "version": scenario, 'details': json_results}
264         headers = {'Content-Type': 'application/json'}
265
266         logger.info("Pushing results to DB...")
267         r = requests.post(url, data=json.dumps(params), headers=headers)
268         logger.debug(r)
269
270
271 if __name__ == '__main__':
272     main()