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."""
16 from snaps.config.user import UserConfig
17 from snaps.config.project import ProjectConfig
18 from snaps.openstack.create_user import OpenStackUser
19 from snaps.openstack.create_project import OpenStackProject
20 from snaps.openstack.utils import keystone_utils
21 from snaps.openstack.tests import openstack_tests
23 from functest.core import testcase
24 from functest.utils import constants
26 __author__ = ("Morgan Richomme <morgan.richomme@orange.com>, "
27 "Valentin Boucher <valentin.boucher@orange.com>")
30 class VnfPreparationException(Exception):
31 """Raise when VNF preparation cannot be executed."""
34 class OrchestratorDeploymentException(Exception):
35 """Raise when orchestrator cannot be deployed."""
38 class VnfDeploymentException(Exception):
39 """Raise when VNF cannot be deployed."""
42 class VnfTestException(Exception):
43 """Raise when VNF cannot be tested."""
46 class VnfOnBoarding(testcase.TestCase):
47 # pylint: disable=too-many-instance-attributes
48 """Base model for VNF test cases."""
50 __logger = logging.getLogger(__name__)
52 def __init__(self, **kwargs):
53 super(VnfOnBoarding, self).__init__(**kwargs)
54 self.uuid = uuid.uuid4()
55 self.user_name = "{}-{}".format(self.case_name, self.uuid)
56 self.tenant_name = "{}-{}".format(self.case_name, self.uuid)
58 self.created_object = []
59 self.os_project = None
60 self.tenant_description = "Created by OPNFV Functest: {}".format(
63 def run(self, **kwargs):
65 Run of the VNF test case:
67 * Deploy an orchestrator if needed (e.g. heat, cloudify, ONAP,...),
69 * Perform tests on the VNF
71 A VNF test case is successfull when the 3 steps are PASS
72 If one of the step is FAIL, the test case is FAIL
75 TestCase.EX_OK if result is 'PASS'.
76 TestCase.EX_TESTCASE_FAILED otherwise.
78 self.start_time = time.time()
82 if (self.deploy_orchestrator() and
85 self.stop_time = time.time()
86 # Calculation with different weight depending on the steps TODO
88 return testcase.TestCase.EX_OK
90 self.stop_time = time.time()
91 return testcase.TestCase.EX_TESTCASE_FAILED
92 except Exception: # pylint: disable=broad-except
93 self.stop_time = time.time()
94 self.__logger.exception("Exception on VNF testing")
95 return testcase.TestCase.EX_TESTCASE_FAILED
99 Prepare the environment for VNF testing:
101 * Creation of a user,
102 * Creation of a tenant,
103 * Allocation admin role to the user on this tenant
105 Returns base.TestCase.EX_OK if preparation is successfull
107 Raise VnfPreparationException in case of problem
111 "Prepare VNF: %s, description: %s", self.case_name,
112 self.tenant_description)
113 snaps_creds = openstack_tests.get_credentials(
114 os_env_file=constants.ENV_FILE)
116 self.os_project = OpenStackProject(
119 name=self.tenant_name,
120 description=self.tenant_description,
121 domain=snaps_creds.project_domain_name
123 self.os_project.create()
124 self.created_object.append(self.os_project)
126 snaps_creds.project_domain_id = \
127 self.os_project.get_project().domain_id
128 snaps_creds.user_domain_id = \
129 self.os_project.get_project().domain_id
131 for role in ['admin', 'Admin']:
132 if keystone_utils.get_role_by_name(
133 keystone_utils.keystone_client(snaps_creds), role):
137 user_creator = OpenStackUser(
141 password=str(uuid.uuid4()),
142 project_name=self.tenant_name,
143 domain_name=snaps_creds.user_domain_name,
144 roles={admin_role: self.tenant_name}))
145 user_creator.create()
146 self.created_object.append(user_creator)
147 self.snaps_creds = user_creator.get_os_creds(self.tenant_name)
149 return testcase.TestCase.EX_OK
150 except Exception: # pylint: disable=broad-except
151 self.__logger.exception("Exception raised during VNF preparation")
152 raise VnfPreparationException
154 def deploy_orchestrator(self):
156 Deploy an orchestrator (optional).
158 If this method is overriden then raise orchestratorDeploymentException
159 if error during orchestrator deployment
161 self.__logger.info("Deploy orchestrator (if necessary)")
164 def deploy_vnf(self):
168 This function MUST be implemented by vnf test cases.
169 The details section MAY be updated in the vnf test cases.
171 The deployment can be executed via a specific orchestrator
172 or using build-in orchestrators such as heat, OpenBaton, cloudify,
176 True if the VNF is properly deployed
177 False if the VNF is not deployed
179 Raise VnfDeploymentException if error during VNF deployment
181 self.__logger.error("VNF must be deployed")
182 raise VnfDeploymentException
188 This function MUST be implemented by vnf test cases.
189 The details section MAY be updated in the vnf test cases.
191 Once a VNF is deployed, it is assumed that specific test suite can be
192 run to validate the VNF.
193 Please note that the same test suite can be used on several test case
194 (e.g. clearwater test suite can be used whatever the orchestrator used
198 True if VNF tests are PASS
199 False if test suite is FAIL
201 Raise VnfTestException if error during VNF test
203 self.__logger.error("VNF must be tested")
204 raise VnfTestException
210 It is up to the test providers to delete resources used for the tests.
216 self.__logger.info('Removing the VNF resources ..')
217 for creator in reversed(self.created_object):
220 except Exception as exc: # pylint: disable=broad-except
221 self.__logger.error('Unexpected error cleaning - %s', exc)