diff --git a/testing/unit/ntp/ntp_module_test.py b/testing/unit/ntp/ntp_module_test.py index 52bd32aa9..a6ac5d3fd 100644 --- a/testing/unit/ntp/ntp_module_test.py +++ b/testing/unit/ntp/ntp_module_test.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. """Module run all the NTP related unit tests""" +import unittest.mock from ntp_module import NTPModule import unittest from scapy.all import rdpcap, NTP, wrpcap @@ -26,9 +27,15 @@ OUTPUT_DIR = os.path.join(TEST_FILES_DIR,'output/') REPORTS_DIR = os.path.join(TEST_FILES_DIR,'reports/') CAPTURES_DIR = os.path.join(TEST_FILES_DIR,'captures/') +MODULE_FILES_DIR = 'modules/test/' + MODULE +TEMPLATE_SOURCE = os.path.join(MODULE_FILES_DIR, 'resources') +TEMPLATE_DIR = os.path.join(TEST_FILES_DIR, 'templates') +TEMPLATE_FILE = 'report_template.jinja2' +BASE_TEMPLATE_FILE = 'module_report_base.jinja2' +BASE_TEMPLATE_SOURCE = os.path.join('resources/report', BASE_TEMPLATE_FILE) -LOCAL_REPORT = os.path.join(REPORTS_DIR,'ntp_report_local.html') -LOCAL_REPORT_NO_NTP = os.path.join(REPORTS_DIR,'ntp_report_local_no_ntp.html') +LOCAL_REPORT = os.path.join(REPORTS_DIR,'ntp_report_local.jinja2') +LOCAL_REPORT_NO_NTP = os.path.join(REPORTS_DIR,'ntp_report_local_no_ntp.jinja2') # Define the capture files to be used for the test NTP_SERVER_CAPTURE_FILE = os.path.join(CAPTURES_DIR,'ntp.pcap') @@ -44,6 +51,23 @@ def setUpClass(cls): # Create the output directories and ignore errors if it already exists os.makedirs(OUTPUT_DIR, exist_ok=True) os.environ['DEVICE_MAC'] = '38:d1:35:09:01:8e' + # Copy template files + if not os.path.isdir(TEMPLATE_DIR): + os.makedirs(TEMPLATE_DIR) + shutil.copy( + os.path.join(TEMPLATE_SOURCE, TEMPLATE_FILE), + os.path.join(TEMPLATE_DIR, TEMPLATE_FILE) + ) + shutil.copy( + BASE_TEMPLATE_SOURCE, + os.path.join(TEMPLATE_DIR, BASE_TEMPLATE_FILE) + ) + + @classmethod + def tearDownClass(cls): + # Delete templates dir + if os.path.isdir(TEMPLATE_DIR): + shutil.rmtree(TEMPLATE_DIR) # Test the module report generation def ntp_module_report_test(self): @@ -52,6 +76,8 @@ def ntp_module_report_test(self): ntp_server_capture_file=NTP_SERVER_CAPTURE_FILE, startup_capture_file=STARTUP_CAPTURE_FILE, monitor_capture_file=MONITOR_CAPTURE_FILE) + setattr(ntp_module, '_report_template_folder', TEMPLATE_DIR) + setattr(ntp_module, '_base_template_file', BASE_TEMPLATE_FILE) report_out_path = ntp_module.generate_module_report() @@ -64,10 +90,9 @@ def ntp_module_report_test(self): report_local = file.read() # Copy the generated html report to a new file - new_report_name = 'ntp_local.html' + new_report_name = 'ntp_local.jinja2' new_report_path = os.path.join(OUTPUT_DIR, new_report_name) shutil.copy(report_out_path, new_report_path) - self.assertEqual(report_out, report_local) # Test the module report generation if no DNS traffic @@ -100,11 +125,14 @@ def ntp_module_report_no_ntp_test(self): wrpcap(startup_cap_file, packets_startup) wrpcap(monitor_cap_file, packets_monitor) - ntp_module = NTPModule(module='dns', + ntp_module = NTPModule(module=MODULE, results_dir=OUTPUT_DIR, ntp_server_capture_file=ntp_server_cap_file, startup_capture_file=startup_cap_file, monitor_capture_file=monitor_cap_file) + + setattr(ntp_module, '_report_template_folder', TEMPLATE_DIR) + setattr(ntp_module, '_base_template_file', BASE_TEMPLATE_FILE) report_out_path = ntp_module.generate_module_report() @@ -117,7 +145,7 @@ def ntp_module_report_no_ntp_test(self): report_local = file.read() # Copy the generated html report to a new file - new_report_name = 'ntp_no_ntp.html' + new_report_name = 'ntp_no_ntp.jinja2' new_report_path = os.path.join(OUTPUT_DIR, new_report_name) shutil.copy(report_out_path, new_report_path) diff --git a/testing/unit/ntp/reports/ntp_report_local.html b/testing/unit/ntp/reports/ntp_report_local.html deleted file mode 100644 index 1fe5e3f3a..000000000 --- a/testing/unit/ntp/reports/ntp_report_local.html +++ /dev/null @@ -1,115 +0,0 @@ -

NTP Module

- - - - - - - - - - - - - - - - - -
Requests to local NTP serverRequests to external NTP serversTotal NTP requestsTotal NTP responses
6338101104
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SourceDestinationTypeVersionCountSync Request Average
10.10.10.15216.239.35.12Client4837.942 seconds
216.239.35.1210.10.10.15Server48N/A
10.10.10.15216.239.35.4Client4837.834 seconds
216.239.35.410.10.10.15Server48N/A
10.10.10.15216.239.35.8Client4838.056 seconds
216.239.35.810.10.10.15Server48N/A
10.10.10.15216.239.35.0Client41420.601 seconds
216.239.35.010.10.10.15Server417N/A
10.10.10.1510.10.10.5Client46313.057 seconds
10.10.10.510.10.10.15Server463N/A
- \ No newline at end of file diff --git a/testing/unit/ntp/reports/ntp_report_local.jinja2 b/testing/unit/ntp/reports/ntp_report_local.jinja2 new file mode 100644 index 000000000..4f8364cb5 --- /dev/null +++ b/testing/unit/ntp/reports/ntp_report_local.jinja2 @@ -0,0 +1,179 @@ + +
+
+
+ {# Badge #} +

+ {% if json_data['device']['test_pack'] == 'Device Qualification' %} + + Device Qualification + {% else %} + + Pilot Assessment + {% endif %} +

+ {{ title }} +
+ + {{ device['manufacturer'] }} + {{ device['model']}} + + Testrun +
+ +
+ +

NTP Module

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Requests to local NTP serverRequests to external NTP serversTotal NTP requestsTotal NTP responses
6338101104
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SourceDestinationTypeVersionCountSync Request Average
10.10.10.15216.239.35.12Client4837.942 seconds
216.239.35.1210.10.10.15Server48N/A
10.10.10.15216.239.35.4Client4837.834 seconds
216.239.35.410.10.10.15Server48N/A
10.10.10.15216.239.35.8Client4838.056 seconds
216.239.35.810.10.10.15Server48N/A
10.10.10.15216.239.35.0Client41420.601 seconds
216.239.35.010.10.10.15Server417N/A
10.10.10.1510.10.10.5Client46313.057 seconds
10.10.10.510.10.10.15Server463N/A
+ + +
+ + +
+
diff --git a/testing/unit/ntp/reports/ntp_report_local_no_ntp.html b/testing/unit/ntp/reports/ntp_report_local_no_ntp.html deleted file mode 100644 index c93ab885f..000000000 --- a/testing/unit/ntp/reports/ntp_report_local_no_ntp.html +++ /dev/null @@ -1,24 +0,0 @@ -

NTP Module

- - - - - - - - - - - - - - - - - -
Requests to local NTP serverRequests to external NTP serversTotal NTP requestsTotal NTP responses
0000
- -
-
- No NTP traffic detected from the device -
\ No newline at end of file diff --git a/testing/unit/ntp/reports/ntp_report_local_no_ntp.jinja2 b/testing/unit/ntp/reports/ntp_report_local_no_ntp.jinja2 new file mode 100644 index 000000000..6f3878bd8 --- /dev/null +++ b/testing/unit/ntp/reports/ntp_report_local_no_ntp.jinja2 @@ -0,0 +1,71 @@ + +
+
+
+ {# Badge #} +

+ {% if json_data['device']['test_pack'] == 'Device Qualification' %} + + Device Qualification + {% else %} + + Pilot Assessment + {% endif %} +

+ {{ title }} +
+ + {{ device['manufacturer'] }} + {{ device['model']}} + + Testrun +
+ +
+ +

NTP Module

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Requests to local NTP serverRequests to external NTP serversTotal NTP requestsTotal NTP responses
0000
+ + +
+
+ No NTP traffic detected from the device +
+ + +
+ + +
+