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