412e131567f84d52e2372a43ee2268a0a35305e9
[functest.git] / functest / core / vnf.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 """Define the parent class of all VNF TestCases."""
11
12 import logging
13 import time
14 import uuid
15
16 from snaps.config.user import UserConfig
17 from snaps.config.project import ProjectConfig
18 from snaps.openstack.create_user import OpenStackUser
19 from snaps.openstack.create_project import OpenStackProject
20 from snaps.openstack.utils import keystone_utils
21 from snaps.openstack.tests import openstack_tests
22
23 from functest.core import testcase
24 from functest.utils import constants
25
26 __author__ = ("Morgan Richomme <morgan.richomme@orange.com>, "
27               "Valentin Boucher <valentin.boucher@orange.com>")
28
29
30 class VnfPreparationException(Exception):
31     """Raise when VNF preparation cannot be executed."""
32
33
34 class OrchestratorDeploymentException(Exception):
35     """Raise when orchestrator cannot be deployed."""
36
37
38 class VnfDeploymentException(Exception):
39     """Raise when VNF cannot be deployed."""
40
41
42 class VnfTestException(Exception):
43     """Raise when VNF cannot be tested."""
44
45
46 class VnfOnBoarding(testcase.TestCase):
47     # pylint: disable=too-many-instance-attributes
48     """Base model for VNF test cases."""
49
50     __logger = logging.getLogger(__name__)
51
52     def __init__(self, **kwargs):
53         super(VnfOnBoarding, self).__init__(**kwargs)
54         self.uuid = uuid.uuid4()
55         self.user_name = "{}-{}".format(self.case_name, self.uuid)
56         self.tenant_name = "{}-{}".format(self.case_name, self.uuid)
57         self.snaps_creds = {}
58         self.created_object = []
59         self.os_project = None
60         self.tenant_description = "Created by OPNFV Functest: {}".format(
61             self.case_name)
62
63     def run(self, **kwargs):
64         """
65         Run of the VNF test case:
66
67             * Deploy an orchestrator if needed (e.g. heat, cloudify, ONAP,...),
68             * Deploy the VNF,
69             * Perform tests on the VNF
70
71           A VNF test case is successfull when the 3 steps are PASS
72           If one of the step is FAIL, the test case is FAIL
73
74         Returns:
75           TestCase.EX_OK if result is 'PASS'.
76           TestCase.EX_TESTCASE_FAILED otherwise.
77         """
78         self.start_time = time.time()
79
80         try:
81             self.prepare()
82             if (self.deploy_orchestrator() and
83                     self.deploy_vnf() and
84                     self.test_vnf()):
85                 self.stop_time = time.time()
86                 # Calculation with different weight depending on the steps TODO
87                 self.result = 100
88                 return testcase.TestCase.EX_OK
89             self.result = 0
90             self.stop_time = time.time()
91             return testcase.TestCase.EX_TESTCASE_FAILED
92         except Exception:  # pylint: disable=broad-except
93             self.stop_time = time.time()
94             self.__logger.exception("Exception on VNF testing")
95             return testcase.TestCase.EX_TESTCASE_FAILED
96
97     def prepare(self):
98         """
99         Prepare the environment for VNF testing:
100
101             * Creation of a user,
102             * Creation of a tenant,
103             * Allocation admin role to the user on this tenant
104
105         Returns base.TestCase.EX_OK if preparation is successfull
106
107         Raise VnfPreparationException in case of problem
108         """
109         try:
110             self.__logger.info(
111                 "Prepare VNF: %s, description: %s", self.case_name,
112                 self.tenant_description)
113             snaps_creds = openstack_tests.get_credentials(
114                 os_env_file=constants.ENV_FILE)
115
116             self.os_project = OpenStackProject(
117                 snaps_creds,
118                 ProjectConfig(
119                     name=self.tenant_name,
120                     description=self.tenant_description,
121                     domain=snaps_creds.project_domain_name
122                 ))
123             self.os_project.create()
124             self.created_object.append(self.os_project)
125
126             snaps_creds.project_domain_id = \
127                 self.os_project.get_project().domain_id
128             snaps_creds.user_domain_id = \
129                 self.os_project.get_project().domain_id
130
131             for role in ['admin', 'Admin']:
132                 if keystone_utils.get_role_by_name(
133                         keystone_utils.keystone_client(snaps_creds), role):
134                     admin_role = role
135                     break
136
137             user_creator = OpenStackUser(
138                 snaps_creds,
139                 UserConfig(
140                     name=self.user_name,
141                     password=str(uuid.uuid4()),
142                     project_name=self.tenant_name,
143                     domain_name=snaps_creds.user_domain_name,
144                     roles={admin_role: self.tenant_name}))
145             user_creator.create()
146             self.created_object.append(user_creator)
147             self.snaps_creds = user_creator.get_os_creds(self.tenant_name)
148
149             return testcase.TestCase.EX_OK
150         except Exception:  # pylint: disable=broad-except
151             self.__logger.exception("Exception raised during VNF preparation")
152             raise VnfPreparationException
153
154     def deploy_orchestrator(self):
155         """
156         Deploy an orchestrator (optional).
157
158         If this method is overriden then raise orchestratorDeploymentException
159         if error during orchestrator deployment
160         """
161         self.__logger.info("Deploy orchestrator (if necessary)")
162         return True
163
164     def deploy_vnf(self):
165         """
166         Deploy the VNF
167
168         This function MUST be implemented by vnf test cases.
169         The details section MAY be updated in the vnf test cases.
170
171         The deployment can be executed via a specific orchestrator
172         or using build-in orchestrators such as heat, OpenBaton, cloudify,
173         juju, onap, ...
174
175         Returns:
176             True if the VNF is properly deployed
177             False if the VNF is not deployed
178
179         Raise VnfDeploymentException if error during VNF deployment
180         """
181         self.__logger.error("VNF must be deployed")
182         raise VnfDeploymentException
183
184     def test_vnf(self):
185         """
186         Test the VNF
187
188         This function MUST be implemented by vnf test cases.
189         The details section MAY be updated in the vnf test cases.
190
191         Once a VNF is deployed, it is assumed that specific test suite can be
192         run to validate the VNF.
193         Please note that the same test suite can be used on several test case
194         (e.g. clearwater test suite can be used whatever the orchestrator used
195         for the deployment)
196
197         Returns:
198             True if VNF tests are PASS
199             False if test suite is FAIL
200
201         Raise VnfTestException if error during VNF test
202         """
203         self.__logger.error("VNF must be tested")
204         raise VnfTestException
205
206     def clean(self):
207         """
208         Clean VNF test case.
209
210         It is up to the test providers to delete resources used for the tests.
211         By default we clean:
212
213             * the user,
214             * the tenant
215         """
216         self.__logger.info('Removing the VNF resources ..')
217         for creator in reversed(self.created_object):
218             try:
219                 creator.clean()
220             except Exception as exc:  # pylint: disable=broad-except
221                 self.__logger.error('Unexpected error cleaning - %s', exc)