Refactor core VNF class
[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.tenant_created = False
47         self.user_created = False
48         self.tenant_name = CONST.__getattribute__(
49             'vnf_{}_tenant_name'.format(self.case_name))
50         self.tenant_description = CONST.__getattribute__(
51             'vnf_{}_tenant_description'.format(self.case_name))
52
53     def run(self, **kwargs):
54         """
55         Run of the VNF test case:
56
57             * Deploy an orchestrator if needed (e.g. heat, cloudify, ONAP),
58             * Deploy the VNF,
59             * Perform tests on the VNF
60
61           A VNF test case is successfull when the 3 steps are PASS
62           If one of the step is FAIL, the test case is FAIL
63
64           Returns:
65             TestCase.EX_OK if result is 'PASS'.
66             TestCase.EX_TESTCASE_FAILED otherwise.
67         """
68         self.start_time = time.time()
69
70         try:
71             self.prepare()
72             if (self.deploy_orchestrator() and
73                     self.deploy_vnf() and
74                     self.test_vnf()):
75                 self.stop_time = time.time()
76                 # Calculation with different weight depending on the steps TODO
77                 self.result = 100
78                 return base.TestCase.EX_OK
79             else:
80                 self.result = 0
81                 return base.TestCase.EX_TESTCASE_FAILED
82         except Exception:  # pylint: disable=broad-except
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             self.__logger.info("Prepare VNF: %s, description: %s",
100                                self.tenant_name, self.tenant_description)
101             admin_creds = os_utils.get_credentials()
102             keystone_client = os_utils.get_keystone_client()
103             self.tenant_created = os_utils.get_or_create_tenant_for_vnf(
104                 keystone_client, self.tenant_name, self.tenant_description)
105             self.user_created = os_utils.get_or_create_user_for_vnf(
106                 keystone_client, self.tenant_name)
107             creds = admin_creds.copy()
108             creds.update({
109                 "tenant": self.tenant_name,
110                 "username": self.tenant_name,
111                 "password": self.tenant_name
112                 })
113             return base.TestCase.EX_OK
114         except Exception:  # pylint: disable=broad-except
115             self.__logger.exception("Exception raised during VNF preparation")
116             raise VnfPreparationException
117
118     def deploy_orchestrator(self):
119         """
120         Deploy an orchestrator (optional).
121
122         If function overwritten
123         raise orchestratorDeploymentException if error during orchestrator
124         deployment
125         """
126         self.__logger.info("Deploy orchestrator (if necessary)")
127         return True
128
129     def deploy_vnf(self):
130         """
131         Deploy the VNF
132
133         This function MUST be implemented by vnf test cases.
134         The details section MAY be updated in the vnf test cases.
135
136         The deployment can be executed via a specific orchestrator
137         or using nuild-in orchestrators such as:
138
139             * heat, openbaton, cloudify (available on all scenario),
140             * open-o (on open-o scenarios)
141
142         Returns:
143             True if the VNF is properly deployed
144             False if the VNF is not deployed
145
146         Raise VnfDeploymentException if error during VNF deployment
147         """
148         self.__logger.error("VNF must be deployed")
149         raise VnfDeploymentException
150
151     def test_vnf(self):
152         """
153         Test the VNF
154
155         This function MUST be implemented by vnf test cases.
156         The details section MAY be updated in the vnf test cases.
157
158         Once a VNF is deployed, it is assumed that specific test suite can be
159         run to validate the VNF.
160         Please note that the same test suite can be used on several test case
161         (e.g. clearwater test suite can be used whatever the orchestrator used
162         for the deployment)
163
164         Returns:
165             True if VNF tests are PASS
166             False if test suite is FAIL
167
168         Raise VnfTestException if error during VNF test
169         """
170         self.__logger.error("VNF must be tested")
171         raise VnfTestException
172
173     def clean(self):
174         """
175         Clean VNF test case.
176
177         It is up to the test providers to delete resources used for the tests.
178         By default we clean:
179
180             * the user,
181             * the tenant
182         """
183         self.__logger.info("test cleaning")
184         keystone_client = os_utils.get_keystone_client()
185         if self.tenant_created:
186             os_utils.delete_tenant(keystone_client, self.tenant_name)
187         if self.user_created:
188             os_utils.delete_user(keystone_client, self.tenant_name)