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