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
21 import keystoneclient.v2_0.client as ksclient
22 import glanceclient.client as glclient
23 import novaclient.client as nvclient
24 from neutronclient.v2_0 import client as ntclient
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()
34 with open('/home/opnfv/functest/conf/config_functest.yaml') as f:
35 functest_yaml = yaml.safe_load(f)
37 dirs = functest_yaml.get('general').get('directories')
38 FUNCTEST_REPO = dirs.get('dir_repo_functest')
39 PROMISE_REPO = dirs.get('dir_repo_promise')
40 TEST_DB = functest_yaml.get('results').get('test_db_url')
42 TENANT_NAME = functest_yaml.get('promise').get('general').get('tenant_name')
43 TENANT_DESCRIPTION = functest_yaml.get('promise').get(
44 'general').get('tenant_description')
45 USER_NAME = functest_yaml.get('promise').get('general').get('user_name')
46 USER_PWD = functest_yaml.get('promise').get('general').get('user_pwd')
47 IMAGE_NAME = functest_yaml.get('promise').get('general').get('image_name')
48 FLAVOR_NAME = functest_yaml.get('promise').get('general').get('flavor_name')
49 FLAVOR_VCPUS = functest_yaml.get('promise').get('general').get('flavor_vcpus')
50 FLAVOR_RAM = functest_yaml.get('promise').get('general').get('flavor_ram')
51 FLAVOR_DISK = functest_yaml.get('promise').get('general').get('flavor_disk')
54 GLANCE_IMAGE_FILENAME = functest_yaml.get('general'). \
55 get('openstack').get('image_file_name')
56 GLANCE_IMAGE_FORMAT = functest_yaml.get('general'). \
57 get('openstack').get('image_disk_format')
58 GLANCE_IMAGE_PATH = functest_yaml.get('general'). \
59 get('directories').get('dir_functest_data') + "/" + GLANCE_IMAGE_FILENAME
61 sys.path.append('%s/testcases' % FUNCTEST_REPO)
64 """ logging configuration """
65 logger = logging.getLogger('Promise')
66 logger.setLevel(logging.DEBUG)
68 ch = logging.StreamHandler()
71 ch.setLevel(logging.DEBUG)
73 ch.setLevel(logging.INFO)
75 formatter = logging.Formatter('%(asctime)s - %(name)s'
76 '- %(levelname)s - %(message)s')
78 ch.setFormatter(formatter)
82 def create_image(glance_client, name):
88 ks_creds = functest_utils.get_credentials("keystone")
89 nv_creds = functest_utils.get_credentials("nova")
90 nt_creds = functest_utils.get_credentials("neutron")
92 keystone = ksclient.Client(**ks_creds)
94 user_id = functest_utils.get_user_id(keystone, ks_creds['username'])
96 logger.error("Error : Failed to get id of %s user" %
100 logger.info("Creating tenant '%s'..." % TENANT_NAME)
101 tenant_id = functest_utils.create_tenant(
102 keystone, TENANT_NAME, TENANT_DESCRIPTION)
104 logger.error("Error : Failed to create %s tenant" % TENANT_NAME)
106 logger.debug("Tenant '%s' created successfully." % TENANT_NAME)
108 roles_name = ["admin", "Admin"]
110 for role_name in roles_name:
112 role_id = functest_utils.get_role_id(keystone, role_name)
115 logger.error("Error : Failed to get id for %s role" % role_name)
118 logger.info("Adding role '%s' to tenant '%s'..." % (role_id, TENANT_NAME))
119 if not functest_utils.add_role_user(keystone, user_id, role_id, tenant_id):
120 logger.error("Error : Failed to add %s on tenant %s" %
121 (ks_creds['username'], TENANT_NAME))
123 logger.debug("Role added successfully.")
125 logger.info("Creating user '%s'..." % USER_NAME)
126 user_id = functest_utils.create_user(
127 keystone, USER_NAME, USER_PWD, None, tenant_id)
130 logger.error("Error : Failed to create %s user" % USER_NAME)
132 logger.debug("User '%s' created successfully." % USER_NAME)
134 logger.info("Updating OpenStack credentials...")
136 "username": TENANT_NAME,
137 "password": TENANT_NAME,
138 "tenant_name": TENANT_NAME,
142 "tenant_name": TENANT_NAME,
146 "project_id": TENANT_NAME,
149 glance_endpoint = keystone.service_catalog.url_for(service_type='image',
150 endpoint_type='publicURL')
151 glance = glclient.Client(1, glance_endpoint, token=keystone.auth_token)
152 nova = nvclient.Client("2", **nv_creds)
154 logger.info("Creating image '%s' from '%s'..." % (IMAGE_NAME,
156 image_id = functest_utils.create_glance_image(glance,
160 logger.error("Failed to create the Glance image...")
162 logger.debug("Image '%s' with ID '%s' created successfully." % (IMAGE_NAME,
164 flavor_id = functest_utils.get_flavor_id(nova, FLAVOR_NAME)
166 logger.info("Creating flavor '%s'..." % FLAVOR_NAME)
167 flavor_id = functest_utils.create_flavor(nova,
173 logger.error("Failed to create the Flavor...")
175 logger.debug("Flavor '%s' with ID '%s' created successfully." %
176 (FLAVOR_NAME, flavor_id))
178 logger.debug("Using existing flavor '%s' with ID '%s'..." % (FLAVOR_NAME,
181 neutron = ntclient.Client(**nt_creds)
182 private_net = functest_utils.get_private_net(neutron)
183 if private_net is None:
184 logger.error("There is no private network in the deployment. Aborting...")
186 logger.debug("Using private network '%s' (%s)." % (private_net['name'],
189 logger.info("Exporting environment variables...")
190 os.environ["NODE_ENV"] = "functest"
191 os.environ["OS_TENANT_NAME"] = TENANT_NAME
192 os.environ["OS_USERNAME"] = USER_NAME
193 os.environ["OS_PASSWORD"] = USER_PWD
194 os.environ["OS_TEST_IMAGE"] = image_id
195 os.environ["OS_TEST_FLAVOR"] = flavor_id
196 os.environ["OS_TEST_NETWORK"] = private_net['id']
198 os.chdir(PROMISE_REPO)
199 results_file_name = 'promise-results.json'
200 results_file = open(results_file_name, 'w+')
201 cmd = 'npm run -s test -- --reporter json'
203 logger.info("Running command: %s" % cmd)
204 ret = subprocess.call(cmd, shell=True, stdout=results_file, \
205 stderr=subprocess.STDOUT)
209 logger.info("The test succeeded.")
212 logger.info("The command '%s' failed." % cmd)
213 test_status = "Failed"
215 # Print output of file
216 with open(results_file_name, 'r') as results_file:
217 data = results_file.read()
218 logger.debug("\n%s" % data)
219 json_data = json.loads(data)
221 suites = json_data["stats"]["suites"]
222 tests = json_data["stats"]["tests"]
223 passes = json_data["stats"]["passes"]
224 pending = json_data["stats"]["pending"]
225 failures = json_data["stats"]["failures"]
226 start_time = json_data["stats"]["start"]
227 end_time = json_data["stats"]["end"]
228 duration = float(json_data["stats"]["duration"])/float(1000)
231 "****************************************\n"\
232 " Promise test report\n\n"\
233 "****************************************\n"\
242 "****************************************\n\n"\
243 % (suites, tests, passes, pending, failures, start_time, end_time, duration))
246 pod_name = functest_utils.get_pod_name(logger)
247 installer = functest_utils.get_installer_type(logger)
248 scenario = functest_utils.get_scenario(logger)
249 build_tag = functest_utils.get_build_tag(logger)
250 git_version = functest_utils.get_git_branch(PROMISE_REPO)
251 url = TEST_DB + "/results"
253 json_results = {"timestart": start_time, "duration": duration,
254 "tests": int(tests), "failures": int(failures)}
255 logger.debug("Results json: "+str(json_results))
257 # criteria for Promise in Release B was 100% of tests OK
259 if int(tests) > 32 and int(failures) < 1:
262 params = {"project_name": "promise", "case_name": "promise",
263 "pod_name": str(pod_name), 'installer': installer,
264 "version": scenario, "scenario": scenario,
265 "criteria": status, "build_tag": build_tag,
266 'details': json_results}
267 headers = {'Content-Type': 'application/json'}
269 logger.info("Pushing results to DB...")
270 r = requests.post(url, data=json.dumps(params), headers=headers)
274 if __name__ == '__main__':