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.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))
53 def run(self, **kwargs):
55 Run of the VNF test case:
57 * Deploy an orchestrator if needed (e.g. heat, cloudify, ONAP),
59 * Perform tests on the VNF
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
65 TestCase.EX_OK if result is 'PASS'.
66 TestCase.EX_TESTCASE_FAILED otherwise.
68 self.start_time = time.time()
72 if (self.deploy_orchestrator() and
75 self.stop_time = time.time()
76 # Calculation with different weight depending on the steps TODO
78 return base.TestCase.EX_OK
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
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 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()
109 "tenant": self.tenant_name,
110 "username": self.tenant_name,
111 "password": self.tenant_name
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
118 def deploy_orchestrator(self):
120 Deploy an orchestrator (optional).
122 If function overwritten
123 raise orchestratorDeploymentException if error during orchestrator
126 self.__logger.info("Deploy orchestrator (if necessary)")
129 def deploy_vnf(self):
133 This function MUST be implemented by vnf test cases.
134 The details section MAY be updated in the vnf test cases.
136 The deployment can be executed via a specific orchestrator
137 or using nuild-in orchestrators such as:
139 * heat, openbaton, cloudify (available on all scenario),
140 * open-o (on open-o scenarios)
143 True if the VNF is properly deployed
144 False if the VNF is not deployed
146 Raise VnfDeploymentException if error during VNF deployment
148 self.__logger.error("VNF must be deployed")
149 raise VnfDeploymentException
155 This function MUST be implemented by vnf test cases.
156 The details section MAY be updated in the vnf test cases.
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
165 True if VNF tests are PASS
166 False if test suite is FAIL
168 Raise VnfTestException if error during VNF test
170 self.__logger.error("VNF must be tested")
171 raise VnfTestException
177 It is up to the test providers to delete resources used for the tests.
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)