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
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 dirs = ft_utils.get_functest_config('general.directories')
36 PROMISE_REPO = dirs.get('dir_repo_promise')
37 RESULTS_DIR = ft_utils.get_functest_config('general.directories.dir_results')
39 TENANT_NAME = ft_utils.get_functest_config('promise.tenant_name')
40 TENANT_DESCRIPTION = \
41 ft_utils.get_functest_config('promise.tenant_description')
42 USER_NAME = ft_utils.get_functest_config('promise.user_name')
43 USER_PWD = ft_utils.get_functest_config('promise.user_pwd')
44 IMAGE_NAME = ft_utils.get_functest_config('promise.image_name')
45 FLAVOR_NAME = ft_utils.get_functest_config('promise.flavor_name')
46 FLAVOR_VCPUS = ft_utils.get_functest_config('promise.flavor_vcpus')
47 FLAVOR_RAM = ft_utils.get_functest_config('promise.flavor_ram')
48 FLAVOR_DISK = ft_utils.get_functest_config('promise.flavor_disk')
51 GLANCE_IMAGE_FILENAME = \
52 ft_utils.get_functest_config('general.openstack.image_file_name')
53 GLANCE_IMAGE_FORMAT = \
54 ft_utils.get_functest_config('general.openstack.image_disk_format')
56 ft_utils.get_functest_config('general.directories.dir_functest_data') + \
57 "/" + GLANCE_IMAGE_FILENAME
59 NET_NAME = ft_utils.get_functest_config('promise.network_name')
60 SUBNET_NAME = ft_utils.get_functest_config('promise.subnet_name')
61 SUBNET_CIDR = ft_utils.get_functest_config('promise.subnet_cidr')
62 ROUTER_NAME = ft_utils.get_functest_config('promise.router_name')
65 """ logging configuration """
66 logger = ft_logger.Logger("promise").getLogger()
71 start_time = time.time()
72 ks_creds = openstack_utils.get_credentials("keystone")
73 nv_creds = openstack_utils.get_credentials("nova")
74 nt_creds = openstack_utils.get_credentials("neutron")
76 keystone = ksclient.Client(**ks_creds)
78 user_id = openstack_utils.get_user_id(keystone, ks_creds['username'])
80 logger.error("Error : Failed to get id of %s user" %
84 logger.info("Creating tenant '%s'..." % TENANT_NAME)
85 tenant_id = openstack_utils.create_tenant(
86 keystone, TENANT_NAME, TENANT_DESCRIPTION)
88 logger.error("Error : Failed to create %s tenant" % TENANT_NAME)
90 logger.debug("Tenant '%s' created successfully." % TENANT_NAME)
92 roles_name = ["admin", "Admin"]
94 for role_name in roles_name:
96 role_id = openstack_utils.get_role_id(keystone, role_name)
99 logger.error("Error : Failed to get id for %s role" % role_name)
102 logger.info("Adding role '%s' to tenant '%s'..." % (role_id, TENANT_NAME))
103 if not openstack_utils.add_role_user(keystone, user_id,
105 logger.error("Error : Failed to add %s on tenant %s" %
106 (ks_creds['username'], TENANT_NAME))
108 logger.debug("Role added successfully.")
110 logger.info("Creating user '%s'..." % USER_NAME)
111 user_id = openstack_utils.create_user(
112 keystone, USER_NAME, USER_PWD, None, tenant_id)
115 logger.error("Error : Failed to create %s user" % USER_NAME)
117 logger.debug("User '%s' created successfully." % USER_NAME)
119 logger.info("Updating OpenStack credentials...")
121 "username": TENANT_NAME,
122 "password": TENANT_NAME,
123 "tenant_name": TENANT_NAME,
127 "tenant_name": TENANT_NAME,
131 "project_id": TENANT_NAME,
134 glance = openstack_utils.get_glance_client()
135 nova = nvclient.Client("2", **nv_creds)
137 logger.info("Creating image '%s' from '%s'..." % (IMAGE_NAME,
139 image_id = openstack_utils.create_glance_image(glance,
143 logger.error("Failed to create the Glance image...")
145 logger.debug("Image '%s' with ID '%s' created successfully." % (IMAGE_NAME,
147 flavor_id = openstack_utils.get_flavor_id(nova, FLAVOR_NAME)
149 logger.info("Creating flavor '%s'..." % FLAVOR_NAME)
150 flavor_id = openstack_utils.create_flavor(nova,
156 logger.error("Failed to create the Flavor...")
158 logger.debug("Flavor '%s' with ID '%s' created successfully." %
159 (FLAVOR_NAME, flavor_id))
161 logger.debug("Using existing flavor '%s' with ID '%s'..."
162 % (FLAVOR_NAME, flavor_id))
164 neutron = ntclient.Client(**nt_creds)
166 network_dic = openstack_utils.create_network_full(neutron,
172 logger.error("Failed to create the private network...")
175 logger.info("Exporting environment variables...")
176 os.environ["NODE_ENV"] = "functest"
177 os.environ["OS_TENANT_NAME"] = TENANT_NAME
178 os.environ["OS_USERNAME"] = USER_NAME
179 os.environ["OS_PASSWORD"] = USER_PWD
180 os.environ["OS_TEST_IMAGE"] = image_id
181 os.environ["OS_TEST_FLAVOR"] = flavor_id
182 os.environ["OS_TEST_NETWORK"] = network_dic["net_id"]
184 os.chdir(PROMISE_REPO)
185 results_file_name = RESULTS_DIR + '/' + 'promise-results.json'
186 results_file = open(results_file_name, 'w+')
187 cmd = 'npm run -s test -- --reporter json'
189 logger.info("Running command: %s" % cmd)
190 ret = subprocess.call(cmd, shell=True, stdout=results_file,
191 stderr=subprocess.STDOUT)
195 logger.info("The test succeeded.")
198 logger.info("The command '%s' failed." % cmd)
199 # test_status = "Failed"
201 # Print output of file
202 with open(results_file_name, 'r') as results_file:
203 data = results_file.read()
204 logger.debug("\n%s" % data)
205 json_data = json.loads(data)
207 suites = json_data["stats"]["suites"]
208 tests = json_data["stats"]["tests"]
209 passes = json_data["stats"]["passes"]
210 pending = json_data["stats"]["pending"]
211 failures = json_data["stats"]["failures"]
212 start_time_json = json_data["stats"]["start"]
213 end_time = json_data["stats"]["end"]
214 duration = float(json_data["stats"]["duration"]) / float(1000)
217 "****************************************\n"
218 " Promise test report\n\n"
219 "****************************************\n"
228 "****************************************\n\n"
229 % (suites, tests, passes, pending, failures,
230 start_time_json, end_time, duration))
233 stop_time = time.time()
234 json_results = {"timestart": start_time, "duration": duration,
235 "tests": int(tests), "failures": int(failures)}
236 logger.debug("Promise Results json: " + str(json_results))
238 # criteria for Promise in Release B was 100% of tests OK
240 if int(tests) > 32 and int(failures) < 1:
244 ft_utils.push_results_to_db("promise",
254 if __name__ == '__main__':