ONAP Python SDK tests should generate JSON artifact (next to HTML) 53/134553/18
authorpawel.denst <pawel.denst@external.t-mobile.pl>
Wed, 10 May 2023 10:50:58 +0000 (10:50 +0000)
committerpawel.denst <pawel.denst@external.t-mobile.pl>
Tue, 30 May 2023 22:36:16 +0000 (22:36 +0000)
Generation of report file in JSON format

Issue-ID: INT-2235
Signed-off-by: pawel.denst <pawel.denst@external.t-mobile.pl>
Signed-off-by: Lukasz Rajewski <lukasz.rajewski@t-mobile.pl>
Change-Id: Ib99a0cb8bf317633e19a4a2e1c41d611a3b9f343

src/onaptests/configuration/settings.py
src/onaptests/steps/reports_collection.py
tests/test_generate_json.py [new file with mode: 0644]

index 74bc840..436e82c 100644 (file)
@@ -40,7 +40,9 @@ LOG_CONFIG = {
 CLEANUP_FLAG = False
 SDC_CLEANUP = False
 
-REPORTING_FILE_PATH = "/tmp/reporting.html"
+REPORTING_FILE_DIRECTORY = "/tmp/"
+HTML_REPORTING_FILE_NAME = "reporting.html"
+JSON_REPORTING_FILE_NAME = "reporting.json"
 K8S_REGION_TYPE = "k8s"
 TILLER_HOST = "localhost"
 K8S_CONFIG = None  # None means it will use default config (~/.kube/config)
index 4b7f79b..0e5076f 100644 (file)
@@ -1,5 +1,7 @@
 from dataclasses import dataclass
 from enum import Enum
+import json
+from pathlib import Path
 from typing import List
 from jinja2 import Environment, FileSystemLoader, select_autoescape
 from onapsdk.configuration import settings
@@ -83,4 +85,20 @@ class ReportsCollection:
             details=details,
             components=components,
             log_path="./pythonsdk.debug.log").dump(
-            settings.REPORTING_FILE_PATH)
+                str(Path(settings.REPORTING_FILE_DIRECTORY).joinpath(settings.HTML_REPORTING_FILE_NAME)))
+
+        report_dict = {
+            'usecase': usecase,
+            'details': details,
+            'components': components,
+            'steps': [
+                {
+                    'description': step_report.step_description,
+                    'status': step_report.step_execution_status.value,
+                    'duration': step_report.step_execution_duration
+                }
+                for step_report in reversed(self.report)
+            ]
+        }
+        with (Path(settings.REPORTING_FILE_DIRECTORY).joinpath(settings.JSON_REPORTING_FILE_NAME)).open('w') as file:
+            json.dump(report_dict, file, indent=4)
\ No newline at end of file
diff --git a/tests/test_generate_json.py b/tests/test_generate_json.py
new file mode 100644 (file)
index 0000000..b5bc48c
--- /dev/null
@@ -0,0 +1,64 @@
+import unittest
+import os
+import json
+from unittest import mock
+from onaptests.steps.reports_collection import ReportsCollection, Report, ReportStepStatus
+
+
+class TestReportsCollection(unittest.TestCase):
+
+    def setUp(self):
+        self.collection = ReportsCollection()
+
+    @mock.patch("onaptests.steps.reports_collection.settings")
+    def test_generate_report_json(self, settings):
+        settings.SERVICE_NAME = "Status Check"
+        settings.SERVICE_DETAILS = "Checks status of all k8s resources in the selected namespace"
+        settings.SERVICE_COMPONENTS = "ALL"
+        settings.REPORTING_FILE_DIRECTORY = "/tmp/"
+        settings.HTML_REPORTING_FILE_NAME = "reporting.html"
+        settings.JSON_REPORTING_FILE_NAME = "reporting.json"
+
+        self.collection.put(Report("Step 1", ReportStepStatus.PASS, 10.0))
+        self.collection.put(Report("Step 2", ReportStepStatus.FAIL, 5.0))
+        self.collection.put(Report("Step 3", ReportStepStatus.NOT_EXECUTED, 0.0))
+        self.collection.put(Report("Step 10", ReportStepStatus.NOT_EXECUTED, 0.0))
+        self.collection.put(Report("Step 12", ReportStepStatus.PASS, 20.0))
+        self.collection.put(Report("Step 21", ReportStepStatus.FAIL, 15.0))
+
+        report_dict = self.collection.generate_report()
+        report_json_path = os.path.join(settings.REPORTING_FILE_DIRECTORY, settings.JSON_REPORTING_FILE_NAME)
+        self.assertTrue(os.path.exists(report_json_path))
+  
+        with open(report_json_path, 'r') as file:
+            report_dict = json.load(file)
+
+        self.assertEqual(report_dict['usecase'], 'Status Check')
+        self.assertEqual(report_dict['details'], 'Checks status of all k8s resources in the selected namespace')
+        self.assertEqual(report_dict['components'], 'ALL')
+        self.assertEqual(len(report_dict['steps']), 6)
+        step1 = report_dict['steps'][0]
+        step2 = report_dict['steps'][1]
+        step3 = report_dict['steps'][2]
+        step10 = report_dict['steps'][3]
+        step12 = report_dict['steps'][4]
+        step21 = report_dict['steps'][5]
+        self.assertEqual(step1['description'], 'Step 1')
+        self.assertEqual(step1['duration'], 10.0)
+        self.assertEqual(step2['description'], 'Step 2')
+        self.assertEqual(step2['duration'], 5.0)
+        self.assertEqual(step3['description'], 'Step 3')
+        self.assertEqual(step3['duration'], 0.0)
+        self.assertEqual(step10['description'], 'Step 10')
+        self.assertEqual(step10['duration'], 0.0)
+        self.assertEqual(step12['description'], 'Step 12')
+        self.assertEqual(step12['duration'], 20.0)
+        self.assertEqual(step21['description'], 'Step 21')
+        self.assertEqual(step21['duration'], 15.0)
+        report_json_path = os.path.join(settings.REPORTING_FILE_DIRECTORY, settings.JSON_REPORTING_FILE_NAME)
+        self.assertTrue(os.path.exists(report_json_path))
+        self.assertEqual(report_json_path,'/tmp/reporting.json')
+        os.remove(report_json_path)
+
+if __name__ == '__main__':
+    unittest.main()