Merge "add bash script to config pip and easy_install"
[functest.git] / testcases / features / promise.py
1 #!/usr/bin/python
2 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Maintainer : jose.lausuch@ericsson.com
11 #
12 import argparse
13 import json
14 import logging
15 import os
16 import requests
17 import subprocess
18 import sys
19 import yaml
20
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
25
26 parser = argparse.ArgumentParser()
27
28 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
29 parser.add_argument("-r", "--report",
30                     help="Create json result file",
31                     action="store_true")
32 args = parser.parse_args()
33
34 with open('/home/opnfv/functest/conf/config_functest.yaml') as f:
35     functest_yaml = yaml.safe_load(f)
36
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')
41
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')
52
53
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
60
61 sys.path.append('%s/testcases' % FUNCTEST_REPO)
62 import functest_utils
63 import openstack_utils
64
65 """ logging configuration """
66 logger = logging.getLogger('Promise')
67 logger.setLevel(logging.DEBUG)
68
69 ch = logging.StreamHandler()
70
71 if args.debug:
72     ch.setLevel(logging.DEBUG)
73 else:
74     ch.setLevel(logging.INFO)
75
76 formatter = logging.Formatter('%(asctime)s - %(name)s'
77                               '- %(levelname)s - %(message)s')
78
79 ch.setFormatter(formatter)
80 logger.addHandler(ch)
81
82
83 def create_image(glance_client, name):
84
85     return image_id
86
87
88 def main():
89     ks_creds = openstack_utils.get_credentials("keystone")
90     nv_creds = openstack_utils.get_credentials("nova")
91     nt_creds = openstack_utils.get_credentials("neutron")
92
93     keystone = ksclient.Client(**ks_creds)
94
95     user_id = openstack_utils.get_user_id(keystone, ks_creds['username'])
96     if user_id == '':
97         logger.error("Error : Failed to get id of %s user" %
98                      ks_creds['username'])
99         exit(-1)
100
101     logger.info("Creating tenant '%s'..." % TENANT_NAME)
102     tenant_id = openstack_utils.create_tenant(
103         keystone, TENANT_NAME, TENANT_DESCRIPTION)
104     if tenant_id == '':
105         logger.error("Error : Failed to create %s tenant" % TENANT_NAME)
106         exit(-1)
107     logger.debug("Tenant '%s' created successfully." % TENANT_NAME)
108
109     roles_name = ["admin", "Admin"]
110     role_id = ''
111     for role_name in roles_name:
112         if role_id == '':
113             role_id = openstack_utils.get_role_id(keystone, role_name)
114
115     if role_id == '':
116         logger.error("Error : Failed to get id for %s role" % role_name)
117         exit(-1)
118
119     logger.info("Adding role '%s' to tenant '%s'..." % (role_id, TENANT_NAME))
120     if not openstack_utils.add_role_user(keystone, user_id, role_id, tenant_id):
121         logger.error("Error : Failed to add %s on tenant %s" %
122                      (ks_creds['username'], TENANT_NAME))
123         exit(-1)
124     logger.debug("Role added successfully.")
125
126     logger.info("Creating user '%s'..." % USER_NAME)
127     user_id = openstack_utils.create_user(
128         keystone, USER_NAME, USER_PWD, None, tenant_id)
129
130     if user_id == '':
131         logger.error("Error : Failed to create %s user" % USER_NAME)
132         exit(-1)
133     logger.debug("User '%s' created successfully." % USER_NAME)
134
135     logger.info("Updating OpenStack credentials...")
136     ks_creds.update({
137         "username": TENANT_NAME,
138         "password": TENANT_NAME,
139         "tenant_name": TENANT_NAME,
140     })
141
142     nt_creds.update({
143         "tenant_name": TENANT_NAME,
144     })
145
146     nv_creds.update({
147         "project_id": TENANT_NAME,
148     })
149
150     glance_endpoint = keystone.service_catalog.url_for(service_type='image',
151                                                        endpoint_type='publicURL')
152     glance = glclient.Client(1, glance_endpoint, token=keystone.auth_token)
153     nova = nvclient.Client("2", **nv_creds)
154
155     logger.info("Creating image '%s' from '%s'..." % (IMAGE_NAME,
156                                                       GLANCE_IMAGE_PATH))
157     image_id = openstack_utils.create_glance_image(glance,
158                                                   IMAGE_NAME,
159                                                   GLANCE_IMAGE_PATH)
160     if not image_id:
161         logger.error("Failed to create the Glance image...")
162         exit(-1)
163     logger.debug("Image '%s' with ID '%s' created successfully." % (IMAGE_NAME,
164                                                                     image_id))
165     flavor_id = openstack_utils.get_flavor_id(nova, FLAVOR_NAME)
166     if flavor_id == '':
167         logger.info("Creating flavor '%s'..." % FLAVOR_NAME)
168         flavor_id = openstack_utils.create_flavor(nova,
169                                                  FLAVOR_NAME,
170                                                  FLAVOR_RAM,
171                                                  FLAVOR_DISK,
172                                                  FLAVOR_VCPUS)
173         if not flavor_id:
174             logger.error("Failed to create the Flavor...")
175             exit(-1)
176         logger.debug("Flavor '%s' with ID '%s' created successfully." %
177                      (FLAVOR_NAME, flavor_id))
178     else:
179         logger.debug("Using existing flavor '%s' with ID '%s'..." % (FLAVOR_NAME,
180                                                                      flavor_id))
181
182     neutron = ntclient.Client(**nt_creds)
183     private_net = openstack_utils.get_private_net(neutron)
184     if private_net is None:
185         logger.error("There is no private network in the deployment. Aborting...")
186         exit(-1)
187     logger.debug("Using private network '%s' (%s)." % (private_net['name'],
188                                                        private_net['id']))
189
190     logger.info("Exporting environment variables...")
191     os.environ["NODE_ENV"] = "functest"
192     os.environ["OS_TENANT_NAME"] = TENANT_NAME
193     os.environ["OS_USERNAME"] = USER_NAME
194     os.environ["OS_PASSWORD"] = USER_PWD
195     os.environ["OS_TEST_IMAGE"] = image_id
196     os.environ["OS_TEST_FLAVOR"] = flavor_id
197     os.environ["OS_TEST_NETWORK"] = private_net['id']
198
199     os.chdir(PROMISE_REPO)
200     results_file_name = 'promise-results.json'
201     results_file = open(results_file_name, 'w+')
202     cmd = 'npm run -s test -- --reporter json'
203
204     logger.info("Running command: %s" % cmd)
205     ret = subprocess.call(cmd, shell=True, stdout=results_file, \
206                           stderr=subprocess.STDOUT)
207     results_file.close()
208
209     if ret == 0:
210         logger.info("The test succeeded.")
211         test_status = 'OK'
212     else:
213         logger.info("The command '%s' failed." % cmd)
214         test_status = "Failed"
215
216     # Print output of file
217     with open(results_file_name, 'r') as results_file:
218         data = results_file.read()
219         logger.debug("\n%s" % data)
220         json_data = json.loads(data)
221
222         suites = json_data["stats"]["suites"]
223         tests = json_data["stats"]["tests"]
224         passes = json_data["stats"]["passes"]
225         pending = json_data["stats"]["pending"]
226         failures = json_data["stats"]["failures"]
227         start_time = json_data["stats"]["start"]
228         end_time = json_data["stats"]["end"]
229         duration = float(json_data["stats"]["duration"])/float(1000)
230
231     logger.info("\n" \
232                 "****************************************\n"\
233                 "          Promise test report\n\n"\
234                 "****************************************\n"\
235                 " Suites:  \t%s\n"\
236                 " Tests:   \t%s\n"\
237                 " Passes:  \t%s\n"\
238                 " Pending: \t%s\n"\
239                 " Failures:\t%s\n"\
240                 " Start:   \t%s\n"\
241                 " End:     \t%s\n"\
242                 " Duration:\t%s\n"\
243                 "****************************************\n\n"\
244                 % (suites, tests, passes, pending, failures, start_time, end_time, duration))
245
246     if args.report:
247         pod_name = functest_utils.get_pod_name(logger)
248         installer = functest_utils.get_installer_type(logger)
249         scenario = functest_utils.get_scenario(logger)
250         build_tag = functest_utils.get_build_tag(logger)
251         git_version = functest_utils.get_git_branch(PROMISE_REPO)
252         url = TEST_DB + "/results"
253
254         json_results = {"timestart": start_time, "duration": duration,
255                         "tests": int(tests), "failures": int(failures)}
256         logger.debug("Results json: "+str(json_results))
257
258         # criteria for Promise in Release B was 100% of tests OK
259         status = "failed"
260         if int(tests) > 32 and int(failures) < 1:
261             status = "passed"
262
263         params = {"project_name": "promise", "case_name": "promise",
264                   "pod_name": str(pod_name), 'installer': installer,
265                   "version": scenario, "scenario": scenario,
266                   "criteria": status, "build_tag": build_tag,
267                   'details': json_results}
268         headers = {'Content-Type': 'application/json'}
269
270         logger.info("Pushing results to DB...")
271         r = requests.post(url, data=json.dumps(params), headers=headers)
272         logger.debug(r)
273
274
275 if __name__ == '__main__':
276     main()