From e753e77ed0b58f9ad467e1d465cda10510328d97 Mon Sep 17 00:00:00 2001 From: P1llus Date: Sun, 6 Sep 2020 21:17:27 +0200 Subject: [PATCH 1/5] first iteration on checking logfiles for errors --- filebeat/tests/system/test_modules.py | 49 ++++++++++++++++++--------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/filebeat/tests/system/test_modules.py b/filebeat/tests/system/test_modules.py index 94d775300ede..76ef55662304 100644 --- a/filebeat/tests/system/test_modules.py +++ b/filebeat/tests/system/test_modules.py @@ -1,6 +1,7 @@ from filebeat import BaseTest from beat.beat import INTEGRATION_TESTS import os +import sys import unittest import glob import subprocess @@ -131,22 +132,31 @@ def run_on_file(self, module, fileset, test_file, cfgfile): cmd.append("{module}.{fileset}.var.format=json".format(module=module, fileset=fileset)) output_path = os.path.join(self.working_dir) - output = open(os.path.join(output_path, "output.log"), "ab") - output.write(bytes(" ".join(cmd) + "\n", "utf-8")) - - # Use a fixed timezone so results don't vary depending on the environment - # Don't use UTC to avoid hiding that non-UTC timezones are not being converted as needed, - # this can happen because UTC uses to be the default timezone in date parsers when no other - # timezone is specified. - local_env = os.environ.copy() - local_env["TZ"] = 'Etc/GMT+2' - - subprocess.Popen(cmd, - env=local_env, - stdin=None, - stdout=output, - stderr=subprocess.STDOUT, - bufsize=0).wait() + # Runs inside a with loop to ensure file is closed afterwards + with open(os.path.join(output_path, "output.log"), "ab") as output: + output.write(bytes(" ".join(cmd) + "\n", "utf-8")) + + # Use a fixed timezone so results don't vary depending on the environment + # Don't use UTC to avoid hiding that non-UTC timezones are not being converted as needed, + # this can happen because UTC uses to be the default timezone in date parsers when no other + # timezone is specified. + local_env = os.environ.copy() + local_env["TZ"] = 'Etc/GMT+2' + + subprocess.Popen(cmd, + env=local_env, + stdin=None, + stdout=output, + stderr=subprocess.STDOUT, + bufsize=0).wait() + output.close() + + # Checks if the output of filebeat includes errors + contains_error, error_line = file_contains(os.path.join(output_path, "output.log"), "ERROR") + assert contains_error is False, "Error found in log:{}".format(error_line) + + # Ensure file is actually closed to ensure large amount of file handlers is not spawning + assert output.closed is True # Make sure index exists self.wait_until(lambda: self.es.indices.exists(self.index_name)) @@ -209,6 +219,13 @@ def _test_expected_events(self, test_file, objects): assert len(d) == 0, "The following expected object doesn't match:\n Diff:\n{}, full object: \n{}".format(d, obj) +def file_contains(filepath, string): + with open(filepath, 'r') as file: + for line in file: + if string in line: + return True, line + return False, None + def clean_keys(obj): # These keys are host dependent From afa5177dd83739d92600e3881a6084003a33ca5d Mon Sep 17 00:00:00 2001 From: P1llus Date: Sun, 6 Sep 2020 21:30:31 +0200 Subject: [PATCH 2/5] mage fmt update --- filebeat/tests/system/test_modules.py | 1 - 1 file changed, 1 deletion(-) diff --git a/filebeat/tests/system/test_modules.py b/filebeat/tests/system/test_modules.py index 76ef55662304..cc359f4c4619 100644 --- a/filebeat/tests/system/test_modules.py +++ b/filebeat/tests/system/test_modules.py @@ -1,7 +1,6 @@ from filebeat import BaseTest from beat.beat import INTEGRATION_TESTS import os -import sys import unittest import glob import subprocess From 04aaff021af6fbede327b59191123b9f7b5c1cf5 Mon Sep 17 00:00:00 2001 From: P1llus Date: Sun, 6 Sep 2020 21:43:40 +0200 Subject: [PATCH 3/5] moving function and another mage fmt update --- filebeat/tests/system/test_modules.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/filebeat/tests/system/test_modules.py b/filebeat/tests/system/test_modules.py index cc359f4c4619..66a9bd7a6b76 100644 --- a/filebeat/tests/system/test_modules.py +++ b/filebeat/tests/system/test_modules.py @@ -218,13 +218,6 @@ def _test_expected_events(self, test_file, objects): assert len(d) == 0, "The following expected object doesn't match:\n Diff:\n{}, full object: \n{}".format(d, obj) -def file_contains(filepath, string): - with open(filepath, 'r') as file: - for line in file: - if string in line: - return True, line - return False, None - def clean_keys(obj): # These keys are host dependent @@ -312,5 +305,13 @@ def delete_key(obj, key): del obj[key] +def file_contains(filepath, string): + with open(filepath, 'r') as file: + for line in file: + if string in line: + return True, line + return False, None + + def pretty_json(obj): return json.dumps(obj, indent=2, separators=(',', ': ')) From ba59d5a92c53570d5e5bb6be342142fb3609f76f Mon Sep 17 00:00:00 2001 From: P1llus Date: Sun, 6 Sep 2020 22:36:38 +0200 Subject: [PATCH 4/5] changing errors from string to array and adding more specific errors to ensure no false positives --- filebeat/tests/system/test_modules.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/filebeat/tests/system/test_modules.py b/filebeat/tests/system/test_modules.py index 66a9bd7a6b76..176013c670fa 100644 --- a/filebeat/tests/system/test_modules.py +++ b/filebeat/tests/system/test_modules.py @@ -150,8 +150,10 @@ def run_on_file(self, module, fileset, test_file, cfgfile): bufsize=0).wait() output.close() + # List of errors to check in filebeat output logs + errors = ["Error loading pipeline for fileset"] # Checks if the output of filebeat includes errors - contains_error, error_line = file_contains(os.path.join(output_path, "output.log"), "ERROR") + contains_error, error_line = file_contains(os.path.join(output_path, "output.log"), errors) assert contains_error is False, "Error found in log:{}".format(error_line) # Ensure file is actually closed to ensure large amount of file handlers is not spawning @@ -305,11 +307,12 @@ def delete_key(obj, key): del obj[key] -def file_contains(filepath, string): +def file_contains(filepath, strings): with open(filepath, 'r') as file: for line in file: - if string in line: - return True, line + for string in strings: + if string in line: + return True, line return False, None From ddec89524dc8a08350791257616ed6d7b2acd726 Mon Sep 17 00:00:00 2001 From: P1llus Date: Thu, 1 Oct 2020 17:24:11 +0200 Subject: [PATCH 5/5] updating code based on PR comments --- filebeat/tests/system/test_modules.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/filebeat/tests/system/test_modules.py b/filebeat/tests/system/test_modules.py index fccbe9380f44..fa3caa934755 100644 --- a/filebeat/tests/system/test_modules.py +++ b/filebeat/tests/system/test_modules.py @@ -131,7 +131,7 @@ def run_on_file(self, module, fileset, test_file, cfgfile): cmd.append("{module}.{fileset}.var.format=json".format(module=module, fileset=fileset)) output_path = os.path.join(self.working_dir) - # Runs inside a with loop to ensure file is closed afterwards + # Runs inside a with block to ensure file is closed afterwards with open(os.path.join(output_path, "output.log"), "ab") as output: output.write(bytes(" ".join(cmd) + "\n", "utf-8")) @@ -148,7 +148,6 @@ def run_on_file(self, module, fileset, test_file, cfgfile): stdout=output, stderr=subprocess.STDOUT, bufsize=0).wait() - output.close() # List of errors to check in filebeat output logs errors = ["Error loading pipeline for fileset"] @@ -156,9 +155,6 @@ def run_on_file(self, module, fileset, test_file, cfgfile): contains_error, error_line = file_contains(os.path.join(output_path, "output.log"), errors) assert contains_error is False, "Error found in log:{}".format(error_line) - # Ensure file is actually closed to ensure large amount of file handlers is not spawning - assert output.closed is True - # Make sure index exists self.wait_until(lambda: self.es.indices.exists(self.index_name))