Log exception stack trace on test failure
[testsuite/pythonsdk-tests.git] / src / onaptests / scenario / basic_cnf_macro.py
1 """Instantiate basic cnf using SO macro flow."""
2 import logging
3 import time
4 from yaml import load, SafeLoader
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 BasicCnfMacroStep(YamlTemplateBaseStep):
17
18     def __init__(self, cleanup=False):
19         """Initialize step.
20
21         Substeps:
22             - CbaPublishStep
23             - YamlTemplateServiceMacroInstantiateStep.
24         """
25         super().__init__(cleanup=cleanup)
26         self._yaml_template: dict = None
27         self.add_step(CbaPublishStep(
28             cleanup=cleanup
29         ))
30         self.add_step(YamlTemplateServiceMacroInstantiateStep(
31             cleanup=cleanup
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 CNF 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, SafeLoader)
72         return self._yaml_template
73
74     @property
75     def model_yaml_template(self) -> dict:
76         return {}
77
78     @property
79     def service_name(self) -> dict:
80         """Service name.
81
82         Get from YAML template.
83
84         Returns:
85             str: Service name
86
87         """
88         return next(iter(self.yaml_template.keys()))
89
90     @property
91     def service_instance_name(self) -> str:
92         """Service instance name.
93
94         Returns:
95             str: Service instance name
96
97         """
98         return settings.SERVICE_INSTANCE_NAME
99
100
101 class BasicCnfMacro(testcase.TestCase):
102     """Instantiate a basic cnf macro."""
103
104     __logger = logging.getLogger()
105
106     def __init__(self, **kwargs):
107         """Init Basic Cnf Macro use case."""
108         if "case_name" not in kwargs:
109             kwargs["case_name"] = 'basic_cnf_macro'
110         super().__init__(**kwargs)
111         self.__logger.debug("Basic Cnf macro init started")
112         self.test = BasicCnfMacroStep(cleanup=settings.CLEANUP_FLAG)
113
114     def run(self):
115         """Run basic cnf macro test."""
116         self.start_time = time.time()
117         try:
118             self.test.execute()
119             self.test.cleanup()
120             self.result = 100
121         except OnapTestException as exc:
122             self.result = 0
123             self.__logger.exception(exc.error_message)
124         except SDKException:
125             self.result = 0
126             self.__logger.exception("SDK Exception")
127         finally:
128             self.stop_time = time.time()
129
130     def clean(self):
131         """Generate report."""
132         self.test.reports_collection.generate_report()