3 # Copyright (c) 2016 Orange and others.
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
10 """Define the parent class of all VNF TestCases."""
15 import functest.core.testcase as base
16 from functest.utils.constants import CONST
17 import functest.utils.openstack_utils as os_utils
19 __author__ = ("Morgan Richomme <morgan.richomme@orange.com>, "
20 "Valentin Boucher <valentin.boucher@orange.com>")
23 class VnfPreparationException(Exception):
24 """Raise when VNF preparation cannot be executed."""
27 class OrchestratorDeploymentException(Exception):
28 """Raise when orchestrator cannot be deployed."""
31 class VnfDeploymentException(Exception):
32 """Raise when VNF cannot be deployed."""
35 class VnfTestException(Exception):
36 """Raise when VNF cannot be tested."""
39 class VnfOnBoarding(base.TestCase):
40 """Base model for VNF test cases."""
42 __logger = logging.getLogger(__name__)
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))
51 def run(self, **kwargs):
53 Run of the VNF test case:
55 * Deploy an orchestrator if needed (e.g. heat, cloudify, ONAP),
57 * Perform tests on the VNF
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
63 TestCase.EX_OK if result is 'PASS'.
64 TestCase.EX_TESTCASE_FAILED otherwise.
66 self.start_time = time.time()
70 if (self.deploy_orchestrator() and
73 self.stop_time = time.time()
74 # Calculation with different weight depending on the steps TODO
76 return base.TestCase.EX_OK
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
88 Prepare the environment for VNF testing:
91 * Creation of a tenant,
92 * Allocation admin role to the user on this tenant
94 Returns base.TestCase.EX_OK if preparation is successfull
96 Raise VnfPreparationException in case of problem
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(
109 self.exist_obj['user'] = not os_utils.get_or_create_user_for_vnf(
110 keystone_client, self.tenant_name)
112 "tenant": self.tenant_name,
113 "username": self.tenant_name,
114 "password": self.tenant_name,
115 "auth_url": os_utils.get_credentials()['auth_url']
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
122 def deploy_orchestrator(self):
124 Deploy an orchestrator (optional).
126 If function overwritten
127 raise orchestratorDeploymentException if error during orchestrator
130 self.__logger.info("Deploy orchestrator (if necessary)")
133 def deploy_vnf(self):
137 This function MUST be implemented by vnf test cases.
138 The details section MAY be updated in the vnf test cases.
140 The deployment can be executed via a specific orchestrator
141 or using nuild-in orchestrators such as:
143 * heat, openbaton, cloudify (available on all scenario),
144 * open-o (on open-o scenarios)
147 True if the VNF is properly deployed
148 False if the VNF is not deployed
150 Raise VnfDeploymentException if error during VNF deployment
152 self.__logger.error("VNF must be deployed")
153 raise VnfDeploymentException
159 This function MUST be implemented by vnf test cases.
160 The details section MAY be updated in the vnf test cases.
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
169 True if VNF tests are PASS
170 False if test suite is FAIL
172 Raise VnfTestException if error during VNF test
174 self.__logger.error("VNF must be tested")
175 raise VnfTestException
181 It is up to the test providers to delete resources used for the tests.
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)