Added Unit Tests for ci/run_tests
[functest.git] / functest / core / vnf_base.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 import time
11
12 import inspect
13
14 import functest.utils.functest_logger as ft_logger
15 import functest.utils.openstack_utils as os_utils
16 import functest.utils.functest_utils as ft_utils
17 import testcase_base as base
18 from functest.utils.constants import CONST
19
20
21 class VnfOnBoardingBase(base.TestcaseBase):
22
23     logger = ft_logger.Logger(__name__).getLogger()
24
25     def __init__(self, project='functest', case='', repo='', cmd=''):
26         super(VnfOnBoardingBase, self).__init__()
27         self.repo = repo
28         self.project_name = project
29         self.case_name = case
30         self.cmd = cmd
31         self.details = {}
32         self.data_dir = CONST.dir_functest_data
33         self.details['orchestrator'] = {}
34         self.details['vnf'] = {}
35         self.details['test_vnf'] = {}
36         self.images = {}
37         try:
38             self.tenant_name = CONST.__getattribute__(
39                 'vnf_{}_tenant_name'.format(self.case_name))
40             self.tenant_description = CONST.__getattribute__(
41                 'vnf_{}_tenant_description'.format(self.case_name))
42         except:
43             # raise Exception("Unknown VNF case=" + self.case_name)
44             self.logger.error("Unknown VNF case={}".format(self.case_name))
45
46         try:
47             self.images = CONST.__getattribute__(
48                 'vnf_{}_tenant_images'.format(self.case_name))
49         except:
50             self.logger.warn("No tenant image defined for this VNF")
51
52     def execute(self):
53         self.start_time = time.time()
54         # Prepare the test (Create Tenant, User, ...)
55         self.logger.info("Create VNF Onboarding environment")
56         self.prepare()
57
58         # Deploy orchestrator
59         try:
60             self.logger.info("Deploy orchestrator (if necessary)")
61             orchestrator_ready_time = time.time()
62             res_orchestrator = self.deploy_orchestrator()
63             # orchestrator is not mandatory
64             if res_orchestrator is not None:
65                 self.details['orchestrator']['status'] = (
66                     res_orchestrator['status'])
67                 self.details['orchestrator']['result'] = (
68                     res_orchestrator['result'])
69                 self.details['orchestrator']['duration'] = round(
70                     orchestrator_ready_time - self.start_time, 1)
71         except Exception:
72             self.logger.warn("Problem with the Orchestrator", exc_info=True)
73
74         # Deploy VNF
75         try:
76             self.logger.info("Deploy VNF " + self.case_name)
77             res_deploy_vnf = self.deploy_vnf()
78             vnf_ready_time = time.time()
79             self.details['vnf']['status'] = res_deploy_vnf['status']
80             self.details['vnf']['result'] = res_deploy_vnf['result']
81             self.details['vnf']['duration'] = round(
82                 vnf_ready_time - orchestrator_ready_time, 1)
83         except Exception:
84             self.logger.error("Error during VNF deployment", exc_info=True)
85             return base.TestcaseBase.EX_TESTCASE_FAILED
86
87         # Test VNF
88         try:
89             self.logger.info("Test VNF")
90             res_test_vnf = self.test_vnf()
91             test_vnf_done_time = time.time()
92             self.details['test_vnf']['status'] = res_test_vnf['status']
93             self.details['test_vnf']['result'] = res_test_vnf['result']
94             self.details['test_vnf']['duration'] = round(
95                 test_vnf_done_time - vnf_ready_time, 1)
96         except Exception:
97             self.logger.error("Error when running VNF tests", exc_info=True)
98             return base.TestcaseBase.EX_TESTCASE_FAILED
99
100         # Clean the system
101         self.clean()
102         self.stop_time = time.time()
103
104         exit_code = self.parse_results()
105         self.log_results()
106         return exit_code
107
108     # prepare state could consist in the creation of the resources
109     # a dedicated user
110     # a dedictaed tenant
111     # dedicated images
112     def prepare(self):
113         self.creds = os_utils.get_credentials()
114         self.keystone_client = os_utils.get_keystone_client()
115
116         self.logger.info("Prepare OpenStack plateform(create tenant and user)")
117         admin_user_id = os_utils.get_user_id(self.keystone_client,
118                                              self.creds['username'])
119         if admin_user_id == '':
120             self.step_failure("Failed to get id of " +
121                               self.creds['username'])
122
123         tenant_id = os_utils.create_tenant(
124             self.keystone_client, self.tenant_name, self.tenant_description)
125         if not tenant_id:
126             self.step_failure("Failed to create " +
127                               self.tenant_name + " tenant")
128
129         roles_name = ["admin", "Admin"]
130         role_id = ''
131         for role_name in roles_name:
132             if role_id == '':
133                 role_id = os_utils.get_role_id(self.keystone_client, role_name)
134
135         if role_id == '':
136             self.logger.error("Failed to get id for %s role" % role_name)
137             self.step_failure("Failed to get role id of " + role_name)
138
139         if not os_utils.add_role_user(self.keystone_client, admin_user_id,
140                                       role_id, tenant_id):
141             self.logger.error("Failed to add %s on tenant" %
142                               self.creds['username'])
143             self.step_failure("Failed to add %s on tenant" %
144                               self.creds['username'])
145
146         user_id = os_utils.create_user(self.keystone_client,
147                                        self.tenant_name,
148                                        self.tenant_name,
149                                        None,
150                                        tenant_id)
151         if not user_id:
152             self.logger.error("Failed to create %s user" % self.tenant_name)
153             self.step_failure("Failed to create user ")
154
155         if not os_utils.add_role_user(self.keystone_client, user_id,
156                                       role_id, tenant_id):
157             self.logger.error("Failed to add %s on tenant" %
158                               self.tenant_name)
159             self.step_failure("Failed to add %s on tenant" %
160                               self.tenant_name)
161
162         self.logger.info("Update OpenStack creds informations")
163         self.admin_creds = self.creds.copy()
164         self.admin_creds.update({
165             "tenant": self.tenant_name
166         })
167         self.neutron_client = os_utils.get_neutron_client(self.admin_creds)
168         self.nova_client = os_utils.get_nova_client(self.admin_creds)
169         self.creds.update({
170             "tenant": self.tenant_name,
171             "username": self.tenant_name,
172             "password": self.tenant_name,
173         })
174
175     # orchestrator is not mandatory to dpeloy and test VNF
176     def deploy_orchestrator(self, **kwargs):
177         pass
178
179     # TODO see how to use built-in exception from releng module
180     def deploy_vnf(self):
181         self.logger.error("VNF must be deployed")
182         return base.TestcaseBase.EX_TESTCASE_FAILED
183
184     def test_vnf(self):
185         self.logger.error("VNF must be tested")
186         return base.TestcaseBase.EX_TESTCASE_FAILED
187
188     def clean(self):
189         self.logger.info("test cleaning")
190
191         self.logger.info("Removing %s tenant .." % self.tenant_name)
192         tenant_id = os_utils.get_tenant_id(self.keystone_client,
193                                            self.tenant_name)
194         if tenant_id == '':
195             self.logger.error("Error : Failed to get id of %s tenant" %
196                               self.tenant_name)
197         else:
198             if not os_utils.delete_tenant(self.keystone_client, tenant_id):
199                 self.logger.error("Error : Failed to remove %s tenant" %
200                                   self.tenant_name)
201
202         self.logger.info("Removing %s user .." % self.tenant_name)
203         user_id = os_utils.get_user_id(
204             self.keystone_client, self.tenant_name)
205         if user_id == '':
206             self.logger.error("Error : Failed to get id of %s user" %
207                               self.tenant_name)
208         else:
209             if not os_utils.delete_user(self.keystone_client, user_id):
210                 self.logger.error("Error : Failed to remove %s user" %
211                                   self.tenant_name)
212
213     def parse_results(self):
214         exit_code = self.EX_OK
215         self.criteria = "PASS"
216         self.logger.info(self.details)
217         # The 2 VNF steps must be OK to get a PASS result
218         if (self.details['vnf']['status'] is not "PASS" or
219                 self.details['test_vnf']['status'] is not "PASS"):
220             exit_code = self.EX_RUN_ERROR
221             self.criteria = "FAIL"
222         return exit_code
223
224     def log_results(self):
225         ft_utils.logger_test_results(self.project_name,
226                                      self.case_name,
227                                      self.criteria,
228                                      self.details)
229
230     def step_failure(self, error_msg):
231         part = inspect.stack()[1][3]
232         self.details[part]['status'] = 'FAIL'
233         self.details[part]['result'] = error_msg
234         self.logger.error("Step failure:{}".format(error_msg))
235         return base.TestcaseBase.EX_TESTCASE_FAILED