aboutsummaryrefslogtreecommitdiffstats
path: root/src/onaptests/steps/base.py
blob: d788fd499d0f5f7364d6de50cae08e388c3346c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import functools
import itertools
import logging
import logging.config
import os
import time
from abc import ABC, abstractmethod
from typing import Iterator, List, Optional

from onapsdk.aai.business import Customer, ServiceInstance, ServiceSubscription
from onapsdk.configuration import settings
from onapsdk.exceptions import SDKException, SettingsError

from onaptests.steps.reports_collection import (Report, ReportsCollection,
                                                ReportStepStatus)
from onaptests.utils.exceptions import (OnapTestException,
                                        OnapTestExceptionGroup,
                                        SubstepExecutionException,
                                        SubstepExecutionExceptionGroup,
                                        TestConfigurationException)

# pylint: disable=protected-access
IF_FORCE_CLEANUP = "PYTHON_SDK_TESTS_FORCE_CLEANUP"


class StoreStateHandler(ABC):
    """Decorator for storing the state of executed test step."""

    @classmethod
    def store_state(cls, fun=None, *, cleanup=False):  # noqa
        if fun is None:
            return functools.partial(cls.store_state, cleanup=cleanup)

        @functools.wraps(fun)
        def wrapper(self, *args, **kwargs):
            if (cleanup and self._state_clean) or (not cleanup and self._state_execute):
                raise RuntimeError("%s step executed twice" % self._step_title(cleanup))
            if cleanup:
                self._state_clean = True
            else:
                self._state_execute = True
            initial_exception = None
            try:
                execution_status: Optional[ReportStepStatus] = ReportStepStatus.FAIL
                if cleanup:
                    self._start_cleanup_time = time.time()
                    try:
                        if (self._cleanup and self._state_execute and
                                (self._is_validation_only or
                                    self.check_preconditions(cleanup=True))):
                            self._log_execution_state("START", cleanup)
                            if not self._is_validation_only or self._is_force_cleanup:
                                fun(self, *args, **kwargs)
                            self._cleaned_up = True
                            execution_status = ReportStepStatus.PASS
                        else:
                            execution_status = ReportStepStatus.NOT_EXECUTED
                    except (OnapTestException, SDKException) as test_exc:
                        initial_exception = test_exc
                    finally:
                        self._log_execution_state(execution_status.name, cleanup)
                        self._cleanup_substeps()
                    if initial_exception:
                        new_exception = initial_exception
                        initial_exception = None
                        raise new_exception
                else:
                    if self._is_validation_only or self.check_preconditions():
                        self._log_execution_state("START", cleanup)
                        self._execute_substeps()
                        if not self._is_validation_only:
                            fun(self, *args, **kwargs)
                        execution_status = ReportStepStatus.PASS
                        self._executed = True
                    else:
                        execution_status = ReportStepStatus.NOT_EXECUTED
            except SubstepExecutionException as substep_exc:
                if not cleanup:
                    execution_status = ReportStepStatus.NOT_EXECUTED
                if initial_exception:
                    substep_exc = OnapTestExceptionGroup("Cleanup Exceptions",
                                                         [initial_exception, substep_exc])
                raise substep_exc
            except (OnapTestException, SDKException) as test_exc:
                if initial_exception:
                    test_exc = OnapTestExceptionGroup("Cleanup Exceptions",
                                                      [initial_exception, test_exc])
                raise test_exc
            finally:
                if not cleanup:
                    self._log_execution_state(execution_status.name, cleanup)
                if cleanup:
                    self._cleanup_report = Report(
                        step_description=self._step_title(cleanup),
                        step_execution_status=execution_status,
                        step_execution_duration=time.time() - self._start_cleanup_time,
                        step_component=self.component
                    )
                else:
                    if not self._start_execution_time:
                        if execution_status != ReportStepStatus.NOT_EXECUTED:
                            self._logger.error("No execution start time saved for %s step. "
                                               "Fix it by call `super.execute()` "
                                               "in step class `execute()` method definition",
                                               self.name)
                        self._start_execution_time = time.time()
                    self._execution_report = Report(
                        step_description=self._step_title(cleanup),
                        step_execution_status=(execution_status if execution_status else
                                               ReportStepStatus.FAIL),
                        step_execution_duration=time.time() - self._start_execution_time,
                        step_component=self.component
                    )
            wrapper._is_wrapped = True
        return wrapper


class BaseStep(StoreStateHandler, ABC):
    """Base step class."""

    # Indicates that Step has no dedicated cleanup method
    HAS_NO_CLEANUP = False

    _logger: logging.Logger = logging.getLogger("")

    def __init_subclass__(cls):
        """Subclass initialization.

        Add _logger property for any BaseStep subclass
        """
        super().__init_subclass__()
        cls._logger: logging.Logger = logging.getLogger("")
        logging.config.dictConfig(settings.LOG_CONFIG)
        # Setup Proxy if SOCK_HTTP is defined in settings
        try:
            cls.set_proxy(settings.SOCK_HTTP)
        except SettingsError:
            pass

    def __init__(self, cleanup: bool = False, break_on_error=True) -> None:
        """Step initialization.

        Args:
            cleanup(bool, optional): Determines if cleanup action should be called.
            break_on_error(bool, optional): Determines if fail on execution should
                result with continuation of further steps

        """
        self._steps: List["BaseStep"] = []
        self._cleanup: bool = cleanup
        self._parent: "BaseStep" = None
        self._reports_collection: ReportsCollection = None
        self._start_execution_time: float = None
        self._start_cleanup_time: float = None
        self._execution_report: ReportStepStatus = None
        self._cleanup_report: ReportStepStatus = None
        self._executed: bool = False
        self._cleaned_up: bool = False
        self._state_execute: bool = False
        self._state_clean: bool = False
        self._nesting_level: int = 0
        self._break_on_error: bool = break_on_error
        self._is_validation_only = settings.IF_VALIDATION
        self._is_force_cleanup = os.environ.get(IF_FORCE_CLEANUP) is not None

    def add_step(self, step: "BaseStep") -> None:
        """Add substep.

        Add substep and mark step as a substep parent.

        Args:
            step (BaseStep): Step object
        """
        self._steps.append(step)
        step._parent: "BaseStep" = self
        step._update_nesting_level()

    def _update_nesting_level(self) -> None:
        """Update step nesting level.

        Step nesting level allows to display relatino of steps during validation
        """
        self._nesting_level = 1 + self._parent._nesting_level
        for step in self._steps:
            step._update_nesting_level()

    @property
    def parent(self) -> "BaseStep":
        """Step parent.

        If parent is not set the step is a root one.
        """
        return self._parent

    @property
    def is_executed(self) -> bool:
        """Is step executed.

        Step is executed if execute() method was completed without errors

        Returns:
            bool: True if step is executed, False otherwise

        """
        return self._executed

    @property
    def is_root(self) -> bool:
        """Is a root step.

        Step is a root if has no parent

        Returns:
            bool: True if step is a root step, False otherwise

        """
        return self._parent is None

    @property
    def reports_collection(self) -> ReportsCollection:
        """Collection to store step reports.

        Store there if step result is "PASS" or "FAIL"

        Returns:
            Queue: Thread safe collection to store reports

        """
        if not self.is_root:
            return self.parent.reports_collection
        if not self._reports_collection:
            self._reports_collection = ReportsCollection(self._component_list())
            for step_report in itertools.chain(self.execution_reports, self.cleanup_reports):
                self._reports_collection.put(step_report)
        return self._reports_collection

    @property
    def execution_reports(self) -> Iterator[ReportsCollection]:
        """Execution reports generator.

        Steps tree postorder traversal

        Yields:
            Iterator[ReportsCollection]: Step execution report

        """
        for step in self._steps:
            yield from step.execution_reports
        if self._execution_report:
            yield self._execution_report

    @property
    def cleanup_reports(self) -> Iterator[ReportsCollection]:
        """Cleanup reports generator.

        Steps tree preorder traversal

        Yields:
            Iterator[ReportsCollection]: Step cleanup report

        """
        if self._cleanup:
            if self._cleanup_report:
                yield self._cleanup_report
        for step in reversed(self._steps):
            yield from step.cleanup_reports

    @property
    def name(self) -> str:
        """Step name."""
        return self.__class__.__name__

    @property
    @abstractmethod
    def description(self) -> str:
        """Step description.

        Used for reports

        Returns:
            str: Step description

        """

    @property
    @abstractmethod
    def component(self) -> str:
        """Component name.

        Name of component which step is related with.
            Most is the name of ONAP component.

        Returns:
            str: Component name

        """

    def _component_list(self, components: dict = None):
        if not components:
            components = {}
        for step in self._steps:
            components[step.component] = step.component
            step._component_list(components)
        if not self.is_root or not components:
            components[self.component] = self.component
        return list(components)

    def _step_title(self, cleanup=False):
        cleanup_label = " Cleanup:" if cleanup else ":"
        return f"[{self.component}] {self.name}{cleanup_label} {self.description}"

    def _log_execution_state(self, state: str, cleanup=False):
        nesting_label = "" + "  " * self._nesting_level
        description = f"| {state} {self._step_title(cleanup)} |"
        self._logger.info(nesting_label + "*" * len(description))
        self._logger.info(nesting_label + description)
        self._logger.info(nesting_label + "*" * len(description))

    def check_preconditions(self, cleanup=False) -> bool:
        """Check preconditions.

        Check if step preconditions are satisfied. If not, step is skipped
        without further consequences. If yes, execution is initiated

        Returns:
            bool: True if preconditions are satisfied, False otherwise

        """
        return True

    def _execute_substeps(self) -> None:
        """Step's action execution.

        Run all substeps action before it's own action.
        Override this method and remember to call `super().execute()` before.

        """
        substep_error = False
        for step in self._steps:
            try:
                step.execute()
            except (OnapTestException, SDKException) as substep_err:
                substep_error = True
                if step._break_on_error:
                    raise SubstepExecutionException from substep_err
                self._logger.exception(substep_err)
        if self._steps:
            if substep_error and self._break_on_error:
                raise SubstepExecutionException("Cannot continue due to failed substeps")
            self._log_execution_state("CONTINUE")
        self._start_execution_time = time.time()

    def _cleanup_substeps(self) -> None:
        """Substeps' cleanup.

        Substeps are cleaned-up in reversed order.
        We also try to cleanup steps if others failed

        """
        exceptions_to_raise = []
        for step in reversed(self._steps):
            try:
                if step._cleanup:
                    step.cleanup()
                else:
                    step._default_cleanup_handler()
            except (OnapTestException, SDKException) as substep_err:
                try:
                    raise SubstepExecutionException from substep_err
                except Exception as e:
                    exceptions_to_raise.append(e)
        if len(exceptions_to_raise) > 0:
            if len(exceptions_to_raise) == 1:
                raise exceptions_to_raise[0]
            raise SubstepExecutionExceptionGroup("Substep Exceptions", exceptions_to_raise)

    def execute(self) -> None:
        """Step's execute.

        Must be implemented in the steps with store_state decorator

        """

    def cleanup(self) -> None:
        """Step's cleanup.

        Not all steps has to have cleanup method

        """
        # Step itself was cleaned-up, now time for children
        if not self._cleanup:
            # in this case we just make sure that store_state is run
            self._default_cleanup_handler()

    @StoreStateHandler.store_state(cleanup=True)
    def _default_cleanup_handler(self):
        pass

    @classmethod
    def set_proxy(cls, sock_http):
        """Set sock proxy."""
        onap_proxy = {}
        onap_proxy['http'] = sock_http
        onap_proxy['https'] = sock_http
        Customer.set_proxy(onap_proxy)

    def validate_step_implementation(self):
        """Validate is step addes store_state decorators."""

        if not getattr(self.execute, "_is_wrapped", False):
            raise TestConfigurationException(
                f"{self._step_title()} - store_state decorator not present in execute() method")
        if self._cleanup and not getattr(self.cleanup, "_is_wrapped", False):
            raise TestConfigurationException(
                f"{self._step_title()} - store_state decorator not present in cleanup() method")
        for step in self._steps:
            step.validate_step_implementation()

    def validate_execution(self):
        """Validate if each step was executed by decorator."""

        if self._is_validation_only:
            self._log_execution_state(f"VALIDATE EXECUTION [{self._state_execute}]")
            if not self._state_execute:
                raise TestConfigurationException(
                    f"{self._step_title()} - Execute decorator was not called")
            for step in self._steps:
                step.validate_execution()

    def validate_cleanup(self):
        """Validate if each step was cleaned by decorator."""

        if self._is_validation_only:
            for step in reversed(self._steps):
                step.validate_cleanup()
            if self._cleanup:
                self._log_execution_state(
                    f"VALIDATE CLEANUP [{self._state_clean}, {self._cleanup}]")
                if not self._state_clean:
                    raise TestConfigurationException(
                        f"{self._step_title()} - Cleanup decorator was not called")


class YamlTemplateBaseStep(BaseStep, ABC):
    """Base YAML template step."""

    def __init__(self, cleanup: bool):
        """Initialize step."""

        super().__init__(cleanup=cleanup)
        self._service_instance: ServiceInstance = None
        self._service_subscription: ServiceSubscription = None
        self._customer: Customer = None

    def _load_customer_and_subscription(self, reload: bool = False):
        if self._customer is None:
            self._customer: Customer = \
                Customer.get_by_global_customer_id(settings.GLOBAL_CUSTOMER_ID)
        if self._service_subscription is None or reload:
            self._service_subscription: ServiceSubscription = \
                self._customer.get_service_subscription_by_service_type(self.service_name)

    def _load_service_instance(self):
        if self._service_instance is None:
            self._service_instance: ServiceInstance = \
                self._service_subscription.get_service_instance_by_name(self.service_instance_name)

    @property
    def service_name(self) -> str:
        """Service name.

        Get from YAML template if it's a root step, get from parent otherwise.

        Returns:
            str: Service name

        """
        if self.is_root:
            return next(iter(self.yaml_template.keys()))
        return self.parent.service_name

    @property
    def service_instance_name(self) -> str:
        """Service instance name.

        Generate service instance name.
        If not applicable None is returned

        Returns:
            str: Service instance name

        """
        return None

    @property
    @abstractmethod
    def yaml_template(self) -> dict:
        """YAML template abstract property.

        Every YAML template step need to implement that property.

        Returns:
            dict: YAML template

        """

    @property
    @abstractmethod
    def model_yaml_template(self) -> dict:
        """Model YAML template abstract property.

        Every YAML template step need to implement that property.

        Returns:
            dict: YAML template

        """