Skip to content

Instantly share code, notes, and snippets.

Last active October 24, 2019 08:52
Show Gist options
  • Save adriansr/4077630402c4d9ba6465cb9f13a1d70f to your computer and use it in GitHub Desktop.
Save adriansr/4077630402c4d9ba6465cb9f13a1d70f to your computer and use it in GitHub Desktop.
Diff filebeat module fileset test failure
# 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 <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
def readjson(f, start, end, name):
js = f.readline().strip()
if js != start:
print 'Start of {} is not {}, but {}'.format(name, start, js)
line = f.readline().strip()
while line != end:
js += line
line = f.readline().rstrip()
js += line.lstrip()
return json.loads(js)
print 'Error parsing:', js
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()))
out[k] = v
return out
def print_doc(name, d):
print name
w = max(map(len, d.keys()))
keys = d.keys()
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