Last active
October 24, 2019 08:52
-
-
Save adriansr/4077630402c4d9ba6465cb9f13a1d70f to your computer and use it in GitHub Desktop.
Diff filebeat module fileset test failure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Helper to find what's wrong when a Filebeat's fileset | |
# fails with the error: | |
# | |
# The following expected object was not found: | |
# { | |
# [...] | |
# } | |
# Searched in: | |
# [ | |
# [...] | |
# ] | |
# | |
# Usage: | |
# python fileset_error_event_compare.py <file> | |
# | |
# Where file contains the whole error above. | |
import json | |
import sys | |
def expect(f, expected, name): | |
line = f.readline().strip() | |
if line != expected: | |
print '{} not found'.format(name) | |
print 'Expected: ', expected | |
print 'Got:', line | |
sys.exit(1) | |
def readjson(f, start, end, name): | |
js = f.readline().strip() | |
if js != start: | |
print 'Start of {} is not {}, but {}'.format(name, start, js) | |
sys.exit(1) | |
line = f.readline().strip() | |
while line != end: | |
js += line | |
line = f.readline().rstrip() | |
js += line.lstrip() | |
try: | |
return json.loads(js) | |
except: | |
print 'Error parsing:', js | |
raise | |
def flatten(d): | |
out = dict() | |
for k,v in d.iteritems(): | |
if isinstance(v, dict): | |
out.update(dict((k + '.' + xk, xv) for xk,xv in flatten(v).iteritems())) | |
else: | |
out[k] = v | |
return out | |
def print_doc(name, d): | |
print name | |
w = max(map(len, d.keys())) | |
keys = d.keys() | |
keys.sort() | |
for k in keys: | |
pad = ' '*(w-len(k)) | |
print k,pad,'=',repr(d[k]) | |
def diff(expected, actual, display=False): | |
ek = set(expected.keys()) | |
ak = set(actual.keys()) | |
missing = ek - ak | |
added = ak - ek | |
changed = filter(lambda k: expected[k] != actual[k], ek & ak) | |
if display: | |
print 'Diff {} added {} missing {} changed:'.format(len(added), len(missing), len(changed)) | |
for k in missing: | |
print '-',k | |
for k in added: | |
print '+',k | |
for k in changed: | |
print '|',k | |
print '<',repr(expected[k]) | |
print '>',repr(actual[k]) | |
return len(ek) + len(ak) -len(added) - len(missing) - 2*len(changed) | |
f = open(sys.argv[1], 'rb') | |
expect(f, 'The following expected object was not found:', 'First line') | |
expected = flatten(readjson(f, '{', '}', 'expected document')) | |
expect(f, 'Searched in:', 'Separator line') | |
got = map(flatten, readjson(f, '[', ']', 'Actual documents')) | |
print_doc('Expected:', expected) | |
scores = sorted( [ (idx, doc, diff(expected, doc)) for (idx, doc) in enumerate(got) ], key=lambda x: -x[2]) | |
for (idx, doc, score) in scores: | |
print_doc('Document #{} score {}:'.format(idx, score), doc) | |
diff(expected, doc, display=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment