import logging
import logging.config
+import time
from abc import ABC, abstractmethod
from typing import List
from onapsdk.configuration import settings
from onapsdk.aai.business import Customer
-from .reports_collection import ReportsCollection
+from .reports_collection import Report, ReportsCollection, ReportStepStatus
class BaseStep(ABC):
"""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
+
+ """
+
@classmethod
def store_state(cls, fun):
def wrapper(self, *args, **kwargs):
try:
+ start_time: float = time.time()
ret = fun(self, *args, **kwargs)
- self.reports_collection.put({self.name: "PASS"})
+ execution_status: ReportStepStatus = ReportStepStatus.PASS
return ret
except Exception:
- self.reports_collection.put({self.name: "FAIL"})
+ execution_status: ReportStepStatus = ReportStepStatus.FAIL
raise
+ finally:
+ self.reports_collection.put(
+ Report(
+ step_description=f"[{self.component}] {self.name}: {self.description}",
+ step_execution_status=execution_status,
+ step_execution_duration=time.time() - start_time
+ )
+ )
return wrapper
def execute(self) -> None:
class ComplexCreateStep(BaseStep):
"""Complex creation step."""
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Create complex."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "AAI"
+
@BaseStep.store_state
def execute(self):
"""Create complex.
self.add_step(LinkCloudRegionToComplexStep(cleanup=cleanup))
self.add_step(CustomerServiceSubscriptionCreateStep(cleanup=cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Connect service subscription with cloud region."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "AAI"
+
@BaseStep.store_state
def execute(self):
"""Connect service subsription to cloud region and tenant.
class CustomerCreateStep(BaseStep):
"""Customer creation step."""
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Create customer."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "AAI"
+
@BaseStep.store_state
def execute(self):
"""Create cutomer.
super().__init__(cleanup=cleanup)
self.add_step(CustomerCreateStep(cleanup=cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Create customer's service subscription."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "AAI"
+
@BaseStep.store_state
def execute(self):
"""Create customer service subsription.
class K8SConnectivityInfoStep(BaseStep):
"""CreateConnnectivityInfoStep."""
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Create K8S connectivity info."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "K8S plugin"
+
@BaseStep.store_state
def execute(self):
"""Creation k8s connectivity information
super().__init__(cleanup=cleanup)
self.add_step(ComplexCreateStep(cleanup=cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Connect cloud region with complex."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "AAI"
+
@BaseStep.store_state
def execute(self):
"""Link cloud region to complex.
class RegisterCloudRegionStep(BaseStep):
"""Cloud region registration step."""
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Register cloud region."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "AAI"
+
@BaseStep.store_state
def execute(self):
"""Register cloud region
self._service_instance: ServiceInstance = None
self.add_step(YamlTemplateVnfAlaCarteInstantiateStep(cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Create K8S profile."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "K8S plugin"
+
@property
def yaml_template(self) -> dict:
"""Step YAML template.
self.add_step(ServiceOnboardStep(cleanup))
self.add_step(ConnectServiceSubToCloudRegionStep(cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Instantiate service using SO a'la carte method."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "SO"
+
@BaseStep.store_state
def execute(self):
"""Instantiate service.
self.add_step(YamlTemplateServiceOnboardStep(cleanup))
self.add_step(ConnectServiceSubToCloudRegionStep(cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Instantiate service described in YAML using SO a'la carte method."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "SO"
+
@property
def yaml_template(self) -> dict:
"""Step YAML template.
else:
self.add_step(YamlTemplateVnfAlaCarteInstantiateStep(cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Instantiate VF module described in YAML using SO a'la carte method."
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "SO"
@property
def yaml_template(self) -> dict:
self._service_instance: ServiceInstance = None
self.add_step(YamlTemplateServiceAlaCarteInstantiateStep(cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Instantiate network link described in YAML using SO a'la carte method."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "SO"
+
@property
def yaml_template(self) -> dict:
"""Step YAML template.
self._service_instance: ServiceInstance = None
self.add_step(YamlTemplateServiceAlaCarteInstantiateStep(cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Instantiate vnf described in YAML using SO a'la carte method."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "SO"
+
@property
def yaml_template(self) -> dict:
"""Step YAML template.
super().__init__(cleanup=cleanup)
self.add_step(VfOnboardStep(cleanup=cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Onboard service in SDC."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "SDC"
+
@BaseStep.store_state
def execute(self):
"""Onboard service.
self._yaml_template: dict = None
self.add_step(YamlTemplateVfOnboardStep(cleanup=cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Onboard service described in YAML file in SDC."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "SDC"
+
@property
def yaml_template(self) -> dict:
"""Step YAML template.
class VendorOnboardStep(BaseStep):
"""Vendor onboard step."""
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Onboard vendor in SDC."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "SDC"
+
@BaseStep.store_state
def execute(self):
"""Onboard vendor.
super().__init__(cleanup=cleanup)
self.add_step(VspOnboardStep(cleanup=cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Onboard vf in SDC."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "SDC"
+
@BaseStep.store_state
def execute(self):
"""Onboard Vf.
super().__init__(cleanup=cleanup)
self.add_step(YamlTemplateVspOnboardStep(cleanup=cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Onboard vf described in YAML file in SDC."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "SDC"
+
@property
def yaml_template(self) -> dict:
"""YAML template.
super().__init__(cleanup=cleanup)
self.add_step(VendorOnboardStep(cleanup=cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Onboard vsp in SDC."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "SDC"
+
@BaseStep.store_state
def execute(self):
"""Onboard Vsp.
super().__init__(cleanup=cleanup)
self.add_step(VendorOnboardStep(cleanup=cleanup))
+ @property
+ def description(self) -> str:
+ """Step description."""
+ return "Onboard vsp described in YAML file in SDC."
+
+ @property
+ def component(self) -> str:
+ """Component name."""
+ return "SDC"
+
@property
def yaml_template(self) -> dict:
"""YAML template.
import sys
-from typing import Dict
+from dataclasses import dataclass
+from enum import Enum
+from typing import List
from jinja2 import Environment, FileSystemLoader, select_autoescape
from onapsdk.configuration import settings
+
+class ReportStepStatus(Enum):
+ """Enum which stores steps execution statuses."""
+ PASS = "PASS"
+ FAIL = "FAIL"
+
+
+@dataclass
+class Report:
+ """Step execution report."""
+ step_description: str
+ step_execution_status: ReportStepStatus
+ step_execution_duration: float
+
+
class ReportsCollection:
"""Collection to store steps execution statuses."""
"""Initialize collection."""
self._collection: list = []
- def put(self, item: Dict[str, str]) -> None:
+ def put(self, item: Report) -> None:
"""Put execution status dictionary.
Args:
- item (Dict[str, str]): Step name with status dictionary
+ item (Report): Step report
"""
- self._collection.append(item)
+ self._collection.insert(0, item)
@property
- def report(self) -> Dict[str, str]:
+ def report(self) -> List[Report]:
"""Get report.
Build a dictionary with execution statuses.
Returns:
- Dict[str, str]: Steps name with status dictionary
+ List[str, str]: Steps name with status dictionary
+
+ """
+ return self._collection
+
+ @property
+ def failed_steps_num(self) -> int:
+ """Number of failed steps in report.
+
+ Returns:
+ int: How many steps failed
"""
- report: Dict[str, str] = {}
- for element in self._collection[::-1]:
- report.update(element)
- return report
+ return sum((1 for step_report in self.report if \
+ step_report.step_execution_status == ReportStepStatus.FAIL))
def generate_report(self) -> None:
- step_list = self.report
- failing_steps = {}
- for step,status in step_list.items():
- if 'FAIL' in status:
- failing_steps[step] = status
usecase = settings.SERVICE_NAME
try:
details = settings.SERVICE_DETAILS
loader=FileSystemLoader(sys.path[-1] + '/onaptests/templates/reporting'))
jinja_env.get_template('reporting.html.j2').stream(
- failing_steps=failing_steps,
- steps=step_list,
+ report=self,
usecase=usecase,
details=details,
components=components,
{% block title %}Summary{% endblock %}
{% block content %}
{{ summary('Results', "", [
- { 'title': 'Pythonsdk Tests', 'failing': (failing_steps | length), 'total': (steps | length)},
+ { 'title': 'Pythonsdk Tests', 'failing': report.failed_steps_num, 'total': (report.report | length)},
])
}}
<tr>
<th>Name</th>
<th>Status</th>
+ <th>Duration (seconds)</th>
</tr>
</thead>
<tbody>
- {% for step,value in steps.items() %}
- <tr {% if value == 'FAIL' %} class="has-background-danger" {% else %} class="has-background-success-light" {% endif %}>
+ {% for step_report in report.report %}
+ <tr {% if step_report.step_execution_status.value == 'FAIL' %} class="has-background-danger" {% else %} class="has-background-success-light" {% endif %}>
<td>
- {{ step }}
+ {{ step_report.step_description }}
</td>
<td>
- {{ value }}
+ {{ step_report.step_execution_status.value }}
+ </td>
+ <td>
+ {{ step_report.step_execution_duration | round(2) }}
</td>
</tr>
{% endfor %}
-from onaptests.steps.reports_collection import ReportsCollection
+from onaptests.steps.reports_collection import Report, ReportsCollection, ReportStepStatus
def test_reports_collection():
rc = ReportsCollection()
- assert rc.report == {}
+ assert rc.report == []
- rc.put({"a": "b"})
- assert rc.report == {"a": "b"}
+ rc.put(Report(
+ "test",
+ ReportStepStatus.PASS,
+ 0.0
+ ))
+ assert len(rc.report) == 1
+
+
+def test_reports_collection_failed_steps_num():
+
+ rc = ReportsCollection()
+ assert rc.failed_steps_num == 0
+
+ rc.put(Report(
+ "test",
+ ReportStepStatus.PASS,
+ 0.0
+ ))
+ assert rc.failed_steps_num == 0
+
+ rc.put(Report(
+ "test",
+ ReportStepStatus.FAIL,
+ 0.0
+ ))
+ assert rc.failed_steps_num == 1
import pytest
+
from onaptests.steps.base import BaseStep
+
class TestStep(BaseStep):
@BaseStep.store_state
def execute(self):
return super().execute()
+ @property
+ def description(self):
+ return "Test pass step"
+
+ @property
+ def component(self) -> str:
+ return "Test"
+
class TestFailStep(BaseStep):
super().execute()
raise Exception
+ @property
+ def description(self):
+ return "Test fail step"
+
+ @property
+ def component(self) -> str:
+ return "Test"
+
def test_store_state():
ts = TestStep()
ts.execute()
- assert ts.reports_collection.report == {"TestStep": "PASS"}
+ assert len(ts.reports_collection.report) == 1
+ rep = ts.reports_collection.report[0]
+ assert rep.step_description == "[Test] TestStep: Test pass step"
+ assert rep.step_execution_status.value == "PASS"
+ assert rep.step_execution_duration != 0
fs = TestFailStep()
fs.add_step(TestStep())
with pytest.raises(Exception):
fs.execute()
- fs.reports_collection.report == {"TestFailStep": "FAIL", "TestStep": "PASS"}
+ rep_f, rep_s = fs.reports_collection.report
+ assert rep_f.step_description == "[Test] TestFailStep: Test fail step"
+ assert rep_f.step_execution_status.value == "FAIL"
+ assert rep_f.step_execution_duration != 0
+
+ assert rep_s.step_description == "[Test] TestStep: Test pass step"
+ assert rep_s.step_execution_status.value == "PASS"
+ assert rep_s.step_execution_duration != 0