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