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