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