[EXCEPTIONS] Distinguish onaptests and onapsdk exception
[testsuite/pythonsdk-tests.git] / src / onaptests / scenario / basic_vm_macro.py
1 """Instantiate basic vm using SO macro flow."""
2 import logging
3 import time
4 from yaml import load
5
6 from onapsdk.configuration import settings
7 from onapsdk.exceptions import SDKException
8 from xtesting.core import testcase
9
10 from onaptests.steps.base import YamlTemplateBaseStep
11 from onaptests.steps.onboard.cds import CbaPublishStep
12 from onaptests.utils.exceptions import OnapTestException
13 from onaptests.steps.instantiate.service_macro import YamlTemplateServiceMacroInstantiateStep
14
15
16 class BasicVmMacroStep(YamlTemplateBaseStep):
17
18     def __init__(self, cleanup=False):
19         """Initialize step.
20
21         Substeps:
22             - CbaPublishStep
23             - YamlTemplateServiceAlaCarteInstantiateStep.
24         """
25         super().__init__(cleanup=cleanup)
26         self._yaml_template: dict = None
27         self.add_step(CbaPublishStep(
28             cleanup=settings.CLEANUP_FLAG
29         ))
30         self.add_step(YamlTemplateServiceMacroInstantiateStep(
31             cleanup=settings.CLEANUP_FLAG
32         ))
33
34     @property
35     def description(self) -> str:
36         """Step description.
37
38         Used for reports
39
40         Returns:
41             str: Step description
42
43         """
44         return "Basic VM macro scenario step"
45
46     @property
47     def component(self) -> str:
48         """Component name.
49
50         Name of component which step is related with.
51             Most is the name of ONAP component.
52
53         Returns:
54             str: Component name
55
56         """
57         return "PythonSDK-tests"
58
59     @property
60     def yaml_template(self) -> dict:
61         """YAML template abstract property.
62
63         Every YAML template step need to implement that property.
64
65         Returns:
66             dict: YAML template
67
68         """
69         if not self._yaml_template:
70             with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template:
71                 self._yaml_template: dict = load(yaml_template)
72         return self._yaml_template
73
74     @property
75     def service_name(self) -> dict:
76         """Service name.
77
78         Get from YAML template.
79
80         Returns:
81             str: Service name
82
83         """
84         return next(iter(self.yaml_template.keys()))
85
86     @property
87     def service_instance_name(self) -> str:
88         """Service instance name.
89
90         Returns:
91             str: Service instance name
92
93         """
94         return settings.SERVICE_INSTANCE_NAME
95
96
97 class BasicVmMacro(testcase.TestCase):
98     """Instantiate a basic vm macro."""
99
100     __logger = logging.getLogger(__name__)
101
102     def __init__(self, **kwargs):
103         """Init Basic Macro use case."""
104         if "case_name" not in kwargs:
105             kwargs["case_name"] = 'basic_vm_macro'
106         super().__init__(**kwargs)
107         self.__logger.debug("Basic VM macro init started")
108         self.test = BasicVmMacroStep(cleanup=settings.CLEANUP_FLAG)
109
110     def run(self):
111         """Run basic vm macro test."""
112         self.start_time = time.time()
113         try:
114             self.test.execute()
115             self.test.cleanup()
116             self.result = 100
117         except OnapTestException as exc:
118             self.result = 0
119             self.__logger.error(exc.error_message)
120         except SDKException:
121             self.result = 0
122             self.__logger.error("SDK Exception")
123         finally:
124             self.stop_time = time.time()
125
126     def clean(self):
127         """Generate report."""
128         self.test.reports_collection.generate_report()