Skip to content

Commit bad38ab

Browse files
marc-grP1llus
andauthored
[Beats][pytest] Asserting if filebeat logs include errors (#20999) (#21554)
First iteration on tackling this issue, allowing to assert on errors in filebeat, since any test will fail afterwards anyway, and the logs should not include errors. This could be anything from Elasticsearch not being available to pipeline failing to install. (cherry picked from commit 4dd8061) Co-authored-by: Marius Iversen <pillus@chasenet.org>
1 parent 065c92a commit bad38ab

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

filebeat/tests/system/test_modules.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,29 @@ def run_on_file(self, module, fileset, test_file, cfgfile):
139139
cmd.append("{module}.{fileset}.var.format=json".format(module=module, fileset=fileset))
140140

141141
output_path = os.path.join(self.working_dir)
142-
output = open(os.path.join(output_path, "output.log"), "ab")
143-
output.write(bytes(" ".join(cmd) + "\n", "utf-8"))
144-
145-
# Use a fixed timezone so results don't vary depending on the environment
146-
# Don't use UTC to avoid hiding that non-UTC timezones are not being converted as needed,
147-
# this can happen because UTC uses to be the default timezone in date parsers when no other
148-
# timezone is specified.
149-
local_env = os.environ.copy()
150-
local_env["TZ"] = 'Etc/GMT+2'
151-
152-
subprocess.Popen(cmd,
153-
env=local_env,
154-
stdin=None,
155-
stdout=output,
156-
stderr=subprocess.STDOUT,
157-
bufsize=0).wait()
142+
# Runs inside a with block to ensure file is closed afterwards
143+
with open(os.path.join(output_path, "output.log"), "ab") as output:
144+
output.write(bytes(" ".join(cmd) + "\n", "utf-8"))
145+
146+
# Use a fixed timezone so results don't vary depending on the environment
147+
# Don't use UTC to avoid hiding that non-UTC timezones are not being converted as needed,
148+
# this can happen because UTC uses to be the default timezone in date parsers when no other
149+
# timezone is specified.
150+
local_env = os.environ.copy()
151+
local_env["TZ"] = 'Etc/GMT+2'
152+
153+
subprocess.Popen(cmd,
154+
env=local_env,
155+
stdin=None,
156+
stdout=output,
157+
stderr=subprocess.STDOUT,
158+
bufsize=0).wait()
159+
160+
# List of errors to check in filebeat output logs
161+
errors = ["Error loading pipeline for fileset"]
162+
# Checks if the output of filebeat includes errors
163+
contains_error, error_line = file_contains(os.path.join(output_path, "output.log"), errors)
164+
assert contains_error is False, "Error found in log:{}".format(error_line)
158165

159166
# Make sure index exists
160167
self.wait_until(lambda: self.es.indices.exists(self.index_name))
@@ -313,5 +320,14 @@ def delete_key(obj, key):
313320
del obj[key]
314321

315322

323+
def file_contains(filepath, strings):
324+
with open(filepath, 'r') as file:
325+
for line in file:
326+
for string in strings:
327+
if string in line:
328+
return True, line
329+
return False, None
330+
331+
316332
def pretty_json(obj):
317333
return json.dumps(obj, indent=2, separators=(',', ': '))

0 commit comments

Comments
 (0)