|
| 1 | +from filebeat import BaseTest |
| 2 | + |
| 3 | +import os |
| 4 | +import platform |
| 5 | +import time |
| 6 | +import shutil |
| 7 | +import json |
| 8 | +import stat |
| 9 | +from nose.plugins.skip import Skip, SkipTest |
| 10 | + |
| 11 | + |
| 12 | +class Test(BaseTest): |
| 13 | + |
| 14 | + def test_migration_non_windows(self): |
| 15 | + """ |
| 16 | + Tests if migration from old filebeat registry to new format works |
| 17 | + """ |
| 18 | + |
| 19 | + if os.name == "nt": |
| 20 | + raise SkipTest |
| 21 | + |
| 22 | + registry_file = self.working_dir + '/registry' |
| 23 | + |
| 24 | + # Write old registry file |
| 25 | + with open(registry_file, 'w') as f: |
| 26 | + f.write('{"logs/hello.log":{"source":"logs/hello.log","offset":4,"FileStateOS":{"inode":30178938,"device":16777220}},"logs/log2.log":{"source":"logs/log2.log","offset":6,"FileStateOS":{"inode":30178958,"device":16777220}}}') |
| 27 | + |
| 28 | + self.render_config_template( |
| 29 | + path=os.path.abspath(self.working_dir) + "/log/input*", |
| 30 | + clean_removed="false", |
| 31 | + clean_inactive="0", |
| 32 | + ) |
| 33 | + |
| 34 | + filebeat = self.start_beat() |
| 35 | + |
| 36 | + self.wait_until( |
| 37 | + lambda: self.log_contains("Old registry states found: 2"), |
| 38 | + max_timeout=15) |
| 39 | + |
| 40 | + self.wait_until( |
| 41 | + lambda: self.log_contains("Old states converted to new states and written to registrar: 2"), |
| 42 | + max_timeout=15) |
| 43 | + |
| 44 | + filebeat.check_kill_and_wait() |
| 45 | + |
| 46 | + # Check if content is same as above |
| 47 | + assert self.get_registry_entry_by_path("logs/hello.log")["offset"] == 4 |
| 48 | + assert self.get_registry_entry_by_path("logs/log2.log")["offset"] == 6 |
| 49 | + |
| 50 | + # Compare first entry |
| 51 | + oldJson = json.loads('{"source":"logs/hello.log","offset":4,"FileStateOS":{"inode":30178938,"device":16777220}}') |
| 52 | + newJson = self.get_registry_entry_by_path("logs/hello.log") |
| 53 | + del newJson["timestamp"] |
| 54 | + del newJson["ttl"] |
| 55 | + assert newJson == oldJson |
| 56 | + |
| 57 | + # Compare second entry |
| 58 | + oldJson = json.loads('{"source":"logs/log2.log","offset":6,"FileStateOS":{"inode":30178958,"device":16777220}}') |
| 59 | + newJson = self.get_registry_entry_by_path("logs/log2.log") |
| 60 | + del newJson["timestamp"] |
| 61 | + del newJson["ttl"] |
| 62 | + assert newJson == oldJson |
| 63 | + |
| 64 | + # Make sure the right number of entries is in |
| 65 | + data = self.get_registry() |
| 66 | + assert len(data) == 2 |
| 67 | + |
| 68 | + def test_migration_windows(self): |
| 69 | + """ |
| 70 | + Tests if migration from old filebeat registry to new format works |
| 71 | + """ |
| 72 | + |
| 73 | + if os.name != "nt": |
| 74 | + raise SkipTest |
| 75 | + |
| 76 | + registry_file = self.working_dir + '/registry' |
| 77 | + |
| 78 | + # Write old registry file |
| 79 | + with open(registry_file, 'w') as f: |
| 80 | + f.write('{"logs/hello.log":{"source":"logs/hello.log","offset":4,"FileStateOS":{"idxhi":1,"idxlo":12,"vol":34}},"logs/log2.log":{"source":"logs/log2.log","offset":6,"FileStateOS":{"idxhi":67,"idxlo":44,"vol":12}}}') |
| 81 | + |
| 82 | + self.render_config_template( |
| 83 | + path=os.path.abspath(self.working_dir) + "/log/input*", |
| 84 | + ) |
| 85 | + |
| 86 | + filebeat = self.start_beat() |
| 87 | + |
| 88 | + self.wait_until( |
| 89 | + lambda: self.log_contains("Old registry states found: 2"), |
| 90 | + max_timeout=15) |
| 91 | + |
| 92 | + self.wait_until( |
| 93 | + lambda: self.log_contains("Old states converted to new states and written to registrar: 2"), |
| 94 | + max_timeout=15) |
| 95 | + |
| 96 | + filebeat.check_kill_and_wait() |
| 97 | + |
| 98 | + # Check if content is same as above |
| 99 | + assert self.get_registry_entry_by_path("logs/hello.log")["offset"] == 4 |
| 100 | + assert self.get_registry_entry_by_path("logs/log2.log")["offset"] == 6 |
| 101 | + |
| 102 | + # Compare first entry |
| 103 | + oldJson = json.loads('{"source":"logs/hello.log","offset":4,"FileStateOS":{"idxhi":1,"idxlo":12,"vol":34}}') |
| 104 | + newJson = self.get_registry_entry_by_path("logs/hello.log") |
| 105 | + del newJson["timestamp"] |
| 106 | + del newJson["ttl"] |
| 107 | + assert newJson == oldJson |
| 108 | + |
| 109 | + # Compare second entry |
| 110 | + oldJson = json.loads('{"source":"logs/log2.log","offset":6,"FileStateOS":{"idxhi":67,"idxlo":44,"vol":12}}') |
| 111 | + newJson = self.get_registry_entry_by_path("logs/log2.log") |
| 112 | + del newJson["timestamp"] |
| 113 | + del newJson["ttl"] |
| 114 | + assert newJson == oldJson |
| 115 | + |
| 116 | + # Make sure the right number of entries is in |
| 117 | + data = self.get_registry() |
| 118 | + assert len(data) == 2 |
| 119 | + |
| 120 | + def test_migration_continue_reading(self): |
| 121 | + """ |
| 122 | + Tests if after the migration filebeat keeps reading the file |
| 123 | + """ |
| 124 | + |
| 125 | + os.mkdir(self.working_dir + "/log/") |
| 126 | + testfile1 = self.working_dir + "/log/test.log" |
| 127 | + |
| 128 | + with open(testfile1, 'w') as f: |
| 129 | + f.write("entry10\n") |
| 130 | + |
| 131 | + registry_file = self.working_dir + '/registry' |
| 132 | + |
| 133 | + self.render_config_template( |
| 134 | + path=os.path.abspath(self.working_dir) + "/log/*", |
| 135 | + output_file_filename="filebeat_1", |
| 136 | + ) |
| 137 | + |
| 138 | + # Run filebeat to create a registry |
| 139 | + filebeat = self.start_beat(output="filebeat1.log") |
| 140 | + self.wait_until( |
| 141 | + lambda: self.output_has(lines=1, output_file="output/filebeat_1"), |
| 142 | + max_timeout=10) |
| 143 | + filebeat.check_kill_and_wait() |
| 144 | + |
| 145 | + # Create old registry file out of the new one |
| 146 | + r = self.get_registry() |
| 147 | + registry_entry = r[0] |
| 148 | + del registry_entry["timestamp"] |
| 149 | + del registry_entry["ttl"] |
| 150 | + old_registry = {registry_entry["source"]: registry_entry} |
| 151 | + |
| 152 | + # Overwrite registry |
| 153 | + with open(registry_file, 'w') as f: |
| 154 | + json.dump(old_registry, f) |
| 155 | + |
| 156 | + |
| 157 | + self.render_config_template( |
| 158 | + path=os.path.abspath(self.working_dir) + "/log/*", |
| 159 | + output_file_filename="filebeat_2", |
| 160 | + ) |
| 161 | + |
| 162 | + filebeat = self.start_beat(output="filebeat2.log") |
| 163 | + |
| 164 | + # Wait until state is migrated |
| 165 | + self.wait_until( |
| 166 | + lambda: self.log_contains( |
| 167 | + "Old states converted to new states and written to registrar: 1", "filebeat2.log"), |
| 168 | + max_timeout=10) |
| 169 | + |
| 170 | + with open(testfile1, 'a') as f: |
| 171 | + f.write("entry12\n") |
| 172 | + |
| 173 | + # After restart new output file is created -> only 1 new entry |
| 174 | + self.wait_until( |
| 175 | + lambda: self.output_has(lines=1, output_file="output/filebeat_2"), |
| 176 | + max_timeout=10) |
| 177 | + |
| 178 | + filebeat.check_kill_and_wait() |
0 commit comments