From 6b678be0ae601923dfaa68cfc5e47e8f573c86a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Fri, 8 Mar 2019 12:22:32 +0100 Subject: [PATCH] Detect if the configured and the migrated registry paths are the same (#10982) (#11085) Cherry-pick of PR #10982 to 6.7 branch. Original message: The E2E test of starting Journalbeat with an existing registry broke when I have added support and tests for `include_matches`. Thus, when I added the migration of the registry file, the issue was not detected. Now both the code and the test are fixed. --- journalbeat/checkpoint/checkpoint.go | 8 ++++++++ journalbeat/tests/system/test_base.py | 21 ++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/journalbeat/checkpoint/checkpoint.go b/journalbeat/checkpoint/checkpoint.go index 2d0d108a590..ff8e702de1c 100644 --- a/journalbeat/checkpoint/checkpoint.go +++ b/journalbeat/checkpoint/checkpoint.go @@ -140,6 +140,14 @@ func (c *Checkpoint) findRegistryFile() error { return fmt.Errorf("error accessing previous registry file: %+v", err) } + // if two files are the same, do not do anything + migratedFs, err := os.Stat(migratedPath) + if err == nil { + if os.SameFile(fs, migratedFs) { + return nil + } + } + f, err := os.Open(c.file) if err != nil { return err diff --git a/journalbeat/tests/system/test_base.py b/journalbeat/tests/system/test_base.py index 96deb5ee7ad..4da33f26ebc 100644 --- a/journalbeat/tests/system/test_base.py +++ b/journalbeat/tests/system/test_base.py @@ -4,6 +4,7 @@ import sys import unittest import time +import yaml class Test(BaseTest): @@ -114,10 +115,14 @@ def test_read_events_with_existing_registry(self): Journalbeat is able to follow reading a from a journal with an existing registry file. """ + registry_path = self.beat_path + "/tests/system/input/test.registry" + input_path = self.beat_path + "/tests/system/input/test.journal" + self._prepare_registry_file(registry_path, input_path) + self.render_config_template( - journal_path=self.beat_path + "/tests/system/input/test.journal", + journal_path=input_path, seek_method="cursor", - registry_file=self.beat_path + "/tests/system/input/test.registry", + registry_file=registry_path, path=os.path.abspath(self.working_dir) + "/log/*", ) journalbeat_proc = self.start_beat() @@ -140,7 +145,7 @@ def test_read_events_with_existing_registry(self): assert exit_code == 0 @unittest.skipUnless(sys.platform.startswith("linux"), "Journald only on Linux") - def test_read_events_with_existing_registry(self): + def test_read_events_with_include_matches(self): """ Journalbeat is able to pass matchers to the journal reader and read filtered messages. """ @@ -172,6 +177,16 @@ def test_read_events_with_existing_registry(self): exit_code = journalbeat_proc.kill_and_wait() assert exit_code == 0 + def _prepare_registry_file(self, registry_path, journal_path): + lines = [] + with open(registry_path, "r") as registry_file: + lines = registry_file.readlines() + lines[2] = "- path: " + journal_path + "\n" + + with open(registry_path, "w") as registry_file: + for line in lines: + registry_file.write(line) + if __name__ == '__main__': unittest.main()