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