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
20 import keystoneclient.v2_0.client as ksclient
21 import glanceclient.client as glclient
22 import novaclient.client as nvclient
23 from neutronclient.v2_0 import client as ntclient
26 import 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'). \
57 get('openstack').get('image_file_name')
58 GLANCE_IMAGE_FORMAT = functest_yaml.get('general'). \
59 get('openstack').get('image_disk_format')
60 GLANCE_IMAGE_PATH = functest_yaml.get('general'). \
61 get('directories').get('dir_functest_data') + "/" + GLANCE_IMAGE_FILENAME
63 """ logging configuration """
64 logger = logging.getLogger('Promise')
65 logger.setLevel(logging.DEBUG)
67 ch = logging.StreamHandler()
70 ch.setLevel(logging.DEBUG)
72 ch.setLevel(logging.INFO)
74 formatter = logging.Formatter('%(asctime)s - %(name)s'
75 '- %(levelname)s - %(message)s')
77 ch.setFormatter(formatter)
82 ks_creds = openstack_utils.get_credentials("keystone")
83 nv_creds = openstack_utils.get_credentials("nova")
84 nt_creds = openstack_utils.get_credentials("neutron")
86 keystone = ksclient.Client(**ks_creds)
88 user_id = openstack_utils.get_user_id(keystone, ks_creds['username'])
90 logger.error("Error : Failed to get id of %s user" %
94 logger.info("Creating tenant '%s'..." % TENANT_NAME)
95 tenant_id = openstack_utils.create_tenant(
96 keystone, TENANT_NAME, TENANT_DESCRIPTION)
98 logger.error("Error : Failed to create %s tenant" % TENANT_NAME)
100 logger.debug("Tenant '%s' created successfully." % TENANT_NAME)
102 roles_name = ["admin", "Admin"]
104 for role_name in roles_name:
106 role_id = openstack_utils.get_role_id(keystone, role_name)
109 logger.error("Error : Failed to get id for %s role" % role_name)
112 logger.info("Adding role '%s' to tenant '%s'..." % (role_id, TENANT_NAME))
113 if not openstack_utils.add_role_user(keystone, user_id,
115 logger.error("Error : Failed to add %s on tenant %s" %
116 (ks_creds['username'], TENANT_NAME))
118 logger.debug("Role added successfully.")
120 logger.info("Creating user '%s'..." % USER_NAME)
121 user_id = openstack_utils.create_user(
122 keystone, USER_NAME, USER_PWD, None, tenant_id)
125 logger.error("Error : Failed to create %s user" % USER_NAME)
127 logger.debug("User '%s' created successfully." % USER_NAME)
129 logger.info("Updating OpenStack credentials...")
131 "username": TENANT_NAME,
132 "password": TENANT_NAME,
133 "tenant_name": TENANT_NAME,
137 "tenant_name": TENANT_NAME,
141 "project_id": TENANT_NAME,
144 glance_endpoint = keystone.\
145 service_catalog.url_for(service_type='image',
146 endpoint_type='publicURL')
147 glance = glclient.Client(1, glance_endpoint, token=keystone.auth_token)
148 nova = nvclient.Client("2", **nv_creds)
150 logger.info("Creating image '%s' from '%s'..." % (IMAGE_NAME,
152 image_id = openstack_utils.create_glance_image(glance,
156 logger.error("Failed to create the Glance image...")
158 logger.debug("Image '%s' with ID '%s' created successfully." % (IMAGE_NAME,
160 flavor_id = openstack_utils.get_flavor_id(nova, FLAVOR_NAME)
162 logger.info("Creating flavor '%s'..." % FLAVOR_NAME)
163 flavor_id = openstack_utils.create_flavor(nova,
169 logger.error("Failed to create the Flavor...")
171 logger.debug("Flavor '%s' with ID '%s' created successfully." %
172 (FLAVOR_NAME, flavor_id))
174 logger.debug("Using existing flavor '%s' with ID '%s'..."
175 % (FLAVOR_NAME, flavor_id))
177 neutron = ntclient.Client(**nt_creds)
178 private_net = openstack_utils.get_private_net(neutron)
179 if private_net is None:
180 logger.error("There is no private network in the deployment."
183 logger.debug("Using private network '%s' (%s)." % (private_net['name'],
186 logger.info("Exporting environment variables...")
187 os.environ["NODE_ENV"] = "functest"
188 os.environ["OS_TENANT_NAME"] = TENANT_NAME
189 os.environ["OS_USERNAME"] = USER_NAME
190 os.environ["OS_PASSWORD"] = USER_PWD
191 os.environ["OS_TEST_IMAGE"] = image_id
192 os.environ["OS_TEST_FLAVOR"] = flavor_id
193 os.environ["OS_TEST_NETWORK"] = private_net['id']
195 os.chdir(PROMISE_REPO)
196 results_file_name = 'promise-results.json'
197 results_file = open(results_file_name, 'w+')
198 cmd = 'npm run -s test -- --reporter json'
200 logger.info("Running command: %s" % cmd)
201 ret = subprocess.call(cmd, shell=True, stdout=results_file,
202 stderr=subprocess.STDOUT)
206 logger.info("The test succeeded.")
209 logger.info("The command '%s' failed." % cmd)
210 # test_status = "Failed"
212 # Print output of file
213 with open(results_file_name, 'r') as results_file:
214 data = results_file.read()
215 logger.debug("\n%s" % data)
216 json_data = json.loads(data)
218 suites = json_data["stats"]["suites"]
219 tests = json_data["stats"]["tests"]
220 passes = json_data["stats"]["passes"]
221 pending = json_data["stats"]["pending"]
222 failures = json_data["stats"]["failures"]
223 start_time = json_data["stats"]["start"]
224 end_time = json_data["stats"]["end"]
225 duration = float(json_data["stats"]["duration"]) / float(1000)
228 "****************************************\n"
229 " Promise test report\n\n"
230 "****************************************\n"
239 "****************************************\n\n"
240 % (suites, tests, passes, pending, failures,
241 start_time, end_time, duration))
244 pod_name = functest_utils.get_pod_name(logger)
245 installer = functest_utils.get_installer_type(logger)
246 scenario = functest_utils.get_scenario(logger)
247 build_tag = functest_utils.get_build_tag(logger)
248 # git_version = functest_utils.get_git_branch(PROMISE_REPO)
249 url = TEST_DB + "/results"
251 json_results = {"timestart": start_time, "duration": duration,
252 "tests": int(tests), "failures": int(failures)}
253 logger.debug("Results json: " + str(json_results))
255 # criteria for Promise in Release B was 100% of tests OK
257 if int(tests) > 32 and int(failures) < 1:
260 params = {"project_name": "promise", "case_name": "promise",
261 "pod_name": str(pod_name), 'installer': installer,
262 "version": scenario, "scenario": scenario,
263 "criteria": status, "build_tag": build_tag,
264 'details': json_results}
265 headers = {'Content-Type': 'application/json'}
267 logger.info("Pushing results to DB...")
268 r = requests.post(url, data=json.dumps(params), headers=headers)
272 if __name__ == '__main__':