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
19 import keystoneclient.v2_0.client as ksclient
20 import glanceclient.client as glclient
21 import novaclient.client as nvclient
22 from neutronclient.v2_0 import client as ntclient
24 import functest.utils.functest_logger as ft_logger
25 import functest.utils.functest_utils as functest_utils
26 import functest.utils.openstack_utils as openstack_utils
28 parser = argparse.ArgumentParser()
30 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
31 parser.add_argument("-r", "--report",
32 help="Create json result file",
34 args = parser.parse_args()
36 with open('/home/opnfv/functest/conf/config_functest.yaml') as f:
37 functest_yaml = yaml.safe_load(f)
39 dirs = functest_yaml.get('general').get('directories')
40 FUNCTEST_REPO = dirs.get('dir_repo_functest')
41 PROMISE_REPO = dirs.get('dir_repo_promise')
42 TEST_DB = functest_yaml.get('results').get('test_db_url')
44 TENANT_NAME = functest_yaml.get('promise').get('general').get('tenant_name')
45 TENANT_DESCRIPTION = functest_yaml.get('promise').get(
46 'general').get('tenant_description')
47 USER_NAME = functest_yaml.get('promise').get('general').get('user_name')
48 USER_PWD = functest_yaml.get('promise').get('general').get('user_pwd')
49 IMAGE_NAME = functest_yaml.get('promise').get('general').get('image_name')
50 FLAVOR_NAME = functest_yaml.get('promise').get('general').get('flavor_name')
51 FLAVOR_VCPUS = functest_yaml.get('promise').get('general').get('flavor_vcpus')
52 FLAVOR_RAM = functest_yaml.get('promise').get('general').get('flavor_ram')
53 FLAVOR_DISK = functest_yaml.get('promise').get('general').get('flavor_disk')
56 GLANCE_IMAGE_FILENAME = functest_yaml.get('general').get('openstack').get(
58 GLANCE_IMAGE_FORMAT = functest_yaml.get('general').get('openstack').get(
60 GLANCE_IMAGE_PATH = functest_yaml.get('general').get('directories').get(
61 'dir_functest_data') + "/" + GLANCE_IMAGE_FILENAME
63 """ logging configuration """
64 logger = ft_logger.Logger("promise").getLogger()
68 ks_creds = openstack_utils.get_credentials("keystone")
69 nv_creds = openstack_utils.get_credentials("nova")
70 nt_creds = openstack_utils.get_credentials("neutron")
72 keystone = ksclient.Client(**ks_creds)
74 user_id = openstack_utils.get_user_id(keystone, ks_creds['username'])
76 logger.error("Error : Failed to get id of %s user" %
80 logger.info("Creating tenant '%s'..." % TENANT_NAME)
81 tenant_id = openstack_utils.create_tenant(
82 keystone, TENANT_NAME, TENANT_DESCRIPTION)
84 logger.error("Error : Failed to create %s tenant" % TENANT_NAME)
86 logger.debug("Tenant '%s' created successfully." % 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'..." % (role_id, TENANT_NAME))
99 if not openstack_utils.add_role_user(keystone, user_id,
101 logger.error("Error : Failed to add %s on tenant %s" %
102 (ks_creds['username'], TENANT_NAME))
104 logger.debug("Role added successfully.")
106 logger.info("Creating user '%s'..." % USER_NAME)
107 user_id = openstack_utils.create_user(
108 keystone, USER_NAME, USER_PWD, None, tenant_id)
111 logger.error("Error : Failed to create %s user" % USER_NAME)
113 logger.debug("User '%s' created successfully." % USER_NAME)
115 logger.info("Updating OpenStack credentials...")
117 "username": TENANT_NAME,
118 "password": TENANT_NAME,
119 "tenant_name": TENANT_NAME,
123 "tenant_name": TENANT_NAME,
127 "project_id": TENANT_NAME,
130 glance_endpoint = keystone.service_catalog.url_for(
131 service_type='image', endpoint_type='publicURL')
132 glance = glclient.Client(1, glance_endpoint, token=keystone.auth_token)
133 nova = nvclient.Client("2", **nv_creds)
135 logger.info("Creating image '%s' from '%s'..." % (IMAGE_NAME,
137 image_id = openstack_utils.create_glance_image(glance,
141 logger.error("Failed to create the Glance image...")
143 logger.debug("Image '%s' with ID '%s' created successfully." % (IMAGE_NAME,
145 flavor_id = openstack_utils.get_flavor_id(nova, FLAVOR_NAME)
147 logger.info("Creating flavor '%s'..." % FLAVOR_NAME)
148 flavor_id = openstack_utils.create_flavor(nova,
154 logger.error("Failed to create the Flavor...")
156 logger.debug("Flavor '%s' with ID '%s' created successfully." %
157 (FLAVOR_NAME, flavor_id))
159 logger.debug("Using existing flavor '%s' with ID '%s'..."
160 % (FLAVOR_NAME, flavor_id))
162 neutron = ntclient.Client(**nt_creds)
163 private_net = openstack_utils.get_private_net(neutron)
164 if private_net is None:
165 logger.error("There is no private network in the deployment."
168 logger.debug("Using private network '%s' (%s)." % (private_net['name'],
171 logger.info("Exporting environment variables...")
172 os.environ["NODE_ENV"] = "functest"
173 os.environ["OS_TENANT_NAME"] = TENANT_NAME
174 os.environ["OS_USERNAME"] = USER_NAME
175 os.environ["OS_PASSWORD"] = USER_PWD
176 os.environ["OS_TEST_IMAGE"] = image_id
177 os.environ["OS_TEST_FLAVOR"] = flavor_id
178 os.environ["OS_TEST_NETWORK"] = private_net['id']
180 os.chdir(PROMISE_REPO)
181 results_file_name = 'promise-results.json'
182 results_file = open(results_file_name, 'w+')
183 cmd = 'npm run -s test -- --reporter json'
185 logger.info("Running command: %s" % cmd)
186 ret = subprocess.call(cmd, shell=True, stdout=results_file,
187 stderr=subprocess.STDOUT)
191 logger.info("The test succeeded.")
194 logger.info("The command '%s' failed." % cmd)
195 # test_status = "Failed"
197 # Print output of file
198 with open(results_file_name, 'r') as results_file:
199 data = results_file.read()
200 logger.debug("\n%s" % data)
201 json_data = json.loads(data)
203 suites = json_data["stats"]["suites"]
204 tests = json_data["stats"]["tests"]
205 passes = json_data["stats"]["passes"]
206 pending = json_data["stats"]["pending"]
207 failures = json_data["stats"]["failures"]
208 start_time = json_data["stats"]["start"]
209 end_time = json_data["stats"]["end"]
210 duration = float(json_data["stats"]["duration"]) / float(1000)
213 "****************************************\n"
214 " Promise test report\n\n"
215 "****************************************\n"
224 "****************************************\n\n"
225 % (suites, tests, passes, pending, failures,
226 start_time, end_time, duration))
229 pod_name = functest_utils.get_pod_name(logger)
230 installer = functest_utils.get_installer_type(logger)
231 scenario = functest_utils.get_scenario(logger)
232 version = functest_utils.get_version(logger)
233 build_tag = functest_utils.get_build_tag(logger)
234 # git_version = functest_utils.get_git_branch(PROMISE_REPO)
235 url = TEST_DB + "/results"
237 json_results = {"timestart": start_time, "duration": duration,
238 "tests": int(tests), "failures": int(failures)}
239 logger.debug("Results json: " + str(json_results))
241 # criteria for Promise in Release B was 100% of tests OK
243 if int(tests) > 32 and int(failures) < 1:
246 params = {"project_name": "promise", "case_name": "promise",
247 "pod_name": str(pod_name), 'installer': installer,
248 "version": version, "scenario": scenario,
249 "criteria": status, "build_tag": build_tag,
250 'details': json_results}
251 headers = {'Content-Type': 'application/json'}
253 logger.info("Pushing results to DB...")
254 r = requests.post(url, data=json.dumps(params), headers=headers)
258 if __name__ == '__main__':