Merge "Replace all the loggers by the functest logger module"
[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 os
15 import requests
16 import subprocess
17 import yaml
18
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
23
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
27
28 parser = argparse.ArgumentParser()
29
30 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
31 parser.add_argument("-r", "--report",
32                     help="Create json result file",
33                     action="store_true")
34 args = parser.parse_args()
35
36 with open('/home/opnfv/functest/conf/config_functest.yaml') as f:
37     functest_yaml = yaml.safe_load(f)
38
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')
43
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')
54
55
56 GLANCE_IMAGE_FILENAME = functest_yaml.get('general').get('openstack').get(
57     'image_file_name')
58 GLANCE_IMAGE_FORMAT = functest_yaml.get('general').get('openstack').get(
59     'image_disk_format')
60 GLANCE_IMAGE_PATH = functest_yaml.get('general').get('directories').get(
61     'dir_functest_data') + "/" + GLANCE_IMAGE_FILENAME
62
63 """ logging configuration """
64 logger = ft_logger.Logger("promise").getLogger()
65
66
67 def main():
68     ks_creds = openstack_utils.get_credentials("keystone")
69     nv_creds = openstack_utils.get_credentials("nova")
70     nt_creds = openstack_utils.get_credentials("neutron")
71
72     keystone = ksclient.Client(**ks_creds)
73
74     user_id = openstack_utils.get_user_id(keystone, ks_creds['username'])
75     if user_id == '':
76         logger.error("Error : Failed to get id of %s user" %
77                      ks_creds['username'])
78         exit(-1)
79
80     logger.info("Creating tenant '%s'..." % TENANT_NAME)
81     tenant_id = openstack_utils.create_tenant(
82         keystone, TENANT_NAME, TENANT_DESCRIPTION)
83     if tenant_id == '':
84         logger.error("Error : Failed to create %s tenant" % TENANT_NAME)
85         exit(-1)
86     logger.debug("Tenant '%s' created successfully." % TENANT_NAME)
87
88     roles_name = ["admin", "Admin"]
89     role_id = ''
90     for role_name in roles_name:
91         if role_id == '':
92             role_id = openstack_utils.get_role_id(keystone, role_name)
93
94     if role_id == '':
95         logger.error("Error : Failed to get id for %s role" % role_name)
96         exit(-1)
97
98     logger.info("Adding role '%s' to tenant '%s'..." % (role_id, TENANT_NAME))
99     if not openstack_utils.add_role_user(keystone, user_id,
100                                          role_id, tenant_id):
101         logger.error("Error : Failed to add %s on tenant %s" %
102                      (ks_creds['username'], TENANT_NAME))
103         exit(-1)
104     logger.debug("Role added successfully.")
105
106     logger.info("Creating user '%s'..." % USER_NAME)
107     user_id = openstack_utils.create_user(
108         keystone, USER_NAME, USER_PWD, None, tenant_id)
109
110     if user_id == '':
111         logger.error("Error : Failed to create %s user" % USER_NAME)
112         exit(-1)
113     logger.debug("User '%s' created successfully." % USER_NAME)
114
115     logger.info("Updating OpenStack credentials...")
116     ks_creds.update({
117         "username": TENANT_NAME,
118         "password": TENANT_NAME,
119         "tenant_name": TENANT_NAME,
120     })
121
122     nt_creds.update({
123         "tenant_name": TENANT_NAME,
124     })
125
126     nv_creds.update({
127         "project_id": TENANT_NAME,
128     })
129
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)
134
135     logger.info("Creating image '%s' from '%s'..." % (IMAGE_NAME,
136                                                       GLANCE_IMAGE_PATH))
137     image_id = openstack_utils.create_glance_image(glance,
138                                                    IMAGE_NAME,
139                                                    GLANCE_IMAGE_PATH)
140     if not image_id:
141         logger.error("Failed to create the Glance image...")
142         exit(-1)
143     logger.debug("Image '%s' with ID '%s' created successfully." % (IMAGE_NAME,
144                                                                     image_id))
145     flavor_id = openstack_utils.get_flavor_id(nova, FLAVOR_NAME)
146     if flavor_id == '':
147         logger.info("Creating flavor '%s'..." % FLAVOR_NAME)
148         flavor_id = openstack_utils.create_flavor(nova,
149                                                   FLAVOR_NAME,
150                                                   FLAVOR_RAM,
151                                                   FLAVOR_DISK,
152                                                   FLAVOR_VCPUS)
153         if not flavor_id:
154             logger.error("Failed to create the Flavor...")
155             exit(-1)
156         logger.debug("Flavor '%s' with ID '%s' created successfully." %
157                      (FLAVOR_NAME, flavor_id))
158     else:
159         logger.debug("Using existing flavor '%s' with ID '%s'..."
160                      % (FLAVOR_NAME, flavor_id))
161
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."
166                      "Aborting...")
167         exit(-1)
168     logger.debug("Using private network '%s' (%s)." % (private_net['name'],
169                                                        private_net['id']))
170
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']
179
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'
184
185     logger.info("Running command: %s" % cmd)
186     ret = subprocess.call(cmd, shell=True, stdout=results_file,
187                           stderr=subprocess.STDOUT)
188     results_file.close()
189
190     if ret == 0:
191         logger.info("The test succeeded.")
192         # test_status = 'OK'
193     else:
194         logger.info("The command '%s' failed." % cmd)
195         # test_status = "Failed"
196
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)
202
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)
211
212     logger.info("\n"
213                 "****************************************\n"
214                 "          Promise test report\n\n"
215                 "****************************************\n"
216                 " Suites:  \t%s\n"
217                 " Tests:   \t%s\n"
218                 " Passes:  \t%s\n"
219                 " Pending: \t%s\n"
220                 " Failures:\t%s\n"
221                 " Start:   \t%s\n"
222                 " End:     \t%s\n"
223                 " Duration:\t%s\n"
224                 "****************************************\n\n"
225                 % (suites, tests, passes, pending, failures,
226                    start_time, end_time, duration))
227
228     if args.report:
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"
236
237         json_results = {"timestart": start_time, "duration": duration,
238                         "tests": int(tests), "failures": int(failures)}
239         logger.debug("Results json: " + str(json_results))
240
241         # criteria for Promise in Release B was 100% of tests OK
242         status = "failed"
243         if int(tests) > 32 and int(failures) < 1:
244             status = "passed"
245
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'}
252
253         logger.info("Pushing results to DB...")
254         r = requests.post(url, data=json.dumps(params), headers=headers)
255         logger.debug(r)
256
257
258 if __name__ == '__main__':
259     main()