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
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Maintainer : jose.lausuch@ericsson.com
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
26 parser = argparse.ArgumentParser()
28 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
29 parser.add_argument("-r", "--report",
30 help="Create json result file",
32 args = parser.parse_args()
35 PROMISE_REPO_DIR = ft_constants.PROMISE_REPO_DIR
36 RESULTS_DIR = ft_constants.FUNCTEST_RESULTS_DIR
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
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)
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
60 """ logging configuration """
61 logger = ft_logger.Logger("promise").getLogger()
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")
71 keystone = ksclient.Client(**ks_creds)
73 user_id = openstack_utils.get_user_id(keystone, ks_creds['username'])
75 logger.error("Error : Failed to get id of %s user" %
79 logger.info("Creating tenant '%s'..." % PROMISE_TENANT_NAME)
80 tenant_id = openstack_utils.create_tenant(
81 keystone, PROMISE_TENANT_NAME, TENANT_DESCRIPTION)
83 logger.error("Error : Failed to create %s tenant"
84 % PROMISE_TENANT_NAME)
86 logger.debug("Tenant '%s' created successfully." % PROMISE_TENANT_NAME)
88 roles_name = ["admin", "Admin"]
90 for role_name in roles_name:
92 role_id = openstack_utils.get_role_id(keystone, role_name)
95 logger.error("Error : Failed to get id for %s role" % role_name)
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,
102 logger.error("Error : Failed to add %s on tenant %s" %
103 (ks_creds['username'], PROMISE_TENANT_NAME))
105 logger.debug("Role added successfully.")
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)
112 logger.error("Error : Failed to create %s user" % PROMISE_USER_NAME)
114 logger.debug("User '%s' created successfully." % PROMISE_USER_NAME)
116 logger.info("Updating OpenStack credentials...")
118 "username": PROMISE_TENANT_NAME,
119 "password": PROMISE_TENANT_NAME,
120 "tenant_name": PROMISE_TENANT_NAME,
124 "tenant_name": PROMISE_TENANT_NAME,
128 "project_id": PROMISE_TENANT_NAME,
131 glance = openstack_utils.get_glance_client()
132 nova = nvclient.Client("2", **nv_creds)
134 logger.info("Creating image '%s' from '%s'..." % (PROMISE_IMAGE_NAME,
136 image_id = openstack_utils.create_glance_image(glance,
140 logger.error("Failed to create the Glance image...")
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)
146 logger.info("Creating flavor '%s'..." % PROMISE_FLAVOR_NAME)
147 flavor_id = openstack_utils.create_flavor(nova,
151 PROMISE_FLAVOR_VCPUS)
153 logger.error("Failed to create the Flavor...")
155 logger.debug("Flavor '%s' with ID '%s' created successfully." %
156 (PROMISE_FLAVOR_NAME, flavor_id))
158 logger.debug("Using existing flavor '%s' with ID '%s'..."
159 % (PROMISE_FLAVOR_NAME, flavor_id))
161 neutron = ntclient.Client(**nt_creds)
163 network_dic = openstack_utils.create_network_full(neutron,
169 logger.error("Failed to create the private network...")
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
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'
186 logger.info("Running command: %s" % cmd)
187 ret = subprocess.call(cmd, shell=True, stdout=results_file,
188 stderr=subprocess.STDOUT)
192 logger.info("The test succeeded.")
195 logger.info("The command '%s' failed." % cmd)
196 # test_status = "Failed"
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)
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)
214 "****************************************\n"
215 " Promise test report\n\n"
216 "****************************************\n"
225 "****************************************\n\n"
226 % (suites, tests, passes, pending, failures,
227 start_time_json, end_time, duration))
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))
235 # criteria for Promise in Release B was 100% of tests OK
237 if int(tests) > 32 and int(failures) < 1:
241 ft_utils.push_results_to_db("promise",
251 if __name__ == '__main__':