Forbid multipart upload if google storage
[functest-xtesting.git] / xtesting / 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 import uuid
15
16 from xtesting.core import testcase
17
18 __author__ = ("Morgan Richomme <morgan.richomme@orange.com>, "
19               "Valentin Boucher <valentin.boucher@orange.com>")
20
21
22 class VnfPreparationException(Exception):
23     """Raise when VNF preparation cannot be executed."""
24
25
26 class OrchestratorDeploymentException(Exception):
27     """Raise when orchestrator cannot be deployed."""
28
29
30 class VnfDeploymentException(Exception):
31     """Raise when VNF cannot be deployed."""
32
33
34 class VnfTestException(Exception):
35     """Raise when VNF cannot be tested."""
36
37
38 class VnfOnBoarding(testcase.TestCase):
39     # pylint: disable=too-many-instance-attributes
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.uuid = uuid.uuid4()
47         self.user_name = "{}-{}".format(self.case_name, self.uuid)
48         self.tenant_name = "{}-{}".format(self.case_name, self.uuid)
49         self.snaps_creds = {}
50         self.created_object = []
51         self.os_project = None
52         self.tenant_description = "Created by OPNFV Functest: {}".format(
53             self.case_name)
54
55     def run(self, **kwargs):
56         """
57         Run of the VNF test case:
58
59             * Deploy an orchestrator if needed (e.g. heat, cloudify, ONAP,...),
60             * Deploy the VNF,
61             * Perform tests on the VNF
62
63           A VNF test case is successfull when the 3 steps are PASS
64           If one of the step is FAIL, the test case is FAIL
65
66         Returns:
67           TestCase.EX_OK if result is 'PASS'.
68           TestCase.EX_TESTCASE_FAILED otherwise.
69         """
70         self.start_time = time.time()
71
72         try:
73             self.prepare()
74             if (self.deploy_orchestrator() and
75                     self.deploy_vnf() and
76                     self.test_vnf()):
77                 self.stop_time = time.time()
78                 # Calculation with different weight depending on the steps TODO
79                 self.result = 100
80                 return testcase.TestCase.EX_OK
81             self.result = 0
82             self.stop_time = time.time()
83             return testcase.TestCase.EX_TESTCASE_FAILED
84         except Exception:  # pylint: disable=broad-except
85             self.stop_time = time.time()
86             self.__logger.exception("Exception on VNF testing")
87             return testcase.TestCase.EX_TESTCASE_FAILED
88
89     def prepare(self):
90         """
91         Prepare the environment for VNF testing:
92
93         Returns base.TestCase.EX_OK if preparation is successfull
94
95         Raise VnfPreparationException in case of problem
96         """
97         self.__logger.exception("VNF must be prepared")
98         raise VnfPreparationException
99
100     def deploy_orchestrator(self):
101         """
102         Deploy an orchestrator (optional).
103
104         If this method is overriden then raise orchestratorDeploymentException
105         if error during orchestrator deployment
106         """
107         self.__logger.info("Deploy orchestrator (if necessary)")
108         return True
109
110     def deploy_vnf(self):
111         """
112         Deploy the VNF
113
114         This function MUST be implemented by vnf test cases.
115         The details section MAY be updated in the vnf test cases.
116
117         The deployment can be executed via a specific orchestrator
118         or using build-in orchestrators such as heat, OpenBaton, cloudify,
119         juju, onap, ...
120
121         Returns:
122             True if the VNF is properly deployed
123             False if the VNF is not deployed
124
125         Raise VnfDeploymentException if error during VNF deployment
126         """
127         self.__logger.error("VNF must be deployed")
128         raise VnfDeploymentException
129
130     def test_vnf(self):
131         """
132         Test the VNF
133
134         This function MUST be implemented by vnf test cases.
135         The details section MAY be updated in the vnf test cases.
136
137         Once a VNF is deployed, it is assumed that specific test suite can be
138         run to validate the VNF.
139         Please note that the same test suite can be used on several test case
140         (e.g. clearwater test suite can be used whatever the orchestrator used
141         for the deployment)
142
143         Returns:
144             True if VNF tests are PASS
145             False if test suite is FAIL
146
147         Raise VnfTestException if error during VNF test
148         """
149         self.__logger.error("VNF must be tested")
150         raise VnfTestException
151
152     def clean(self):
153         """
154         Clean VNF test case.
155
156         It is up to the test providers to delete resources used for the tests.
157         By default we clean:
158
159             * the user,
160             * the tenant
161         """
162         self.__logger.info('Removing the VNF resources ..')
163         for creator in reversed(self.created_object):
164             try:
165                 creator.clean()
166             except Exception as exc:  # pylint: disable=broad-except
167                 self.__logger.error('Unexpected error cleaning - %s', exc)