Skip to content

Commit

Permalink
update some tests, but they still need work
Browse files Browse the repository at this point in the history
  • Loading branch information
JaniAnttonen committed Apr 27, 2020
1 parent e2b559f commit fbbd59f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "winston-loki",
"version": "6.0.0",
"version": "6.0.0-rc.1",
"description": "A Winston transport for Grafana Loki",
"keywords": [
"winston",
Expand Down
36 changes: 18 additions & 18 deletions test/batcher.json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,30 @@ describe('Batcher tests with JSON transport', function () {
await stub.mockReturnValue(() => call())
const spy = jest.fn(call)

batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[0]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[0]))

function call () {
expect(spy).toHaveBeenCalledTimes(1)
}
await stub.mockRestore()
})
it('Should add same items in the same stream', function () {
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[0]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[0]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[0]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[0]))
expect(batcher.batch.streams.length).toBe(1)
})
it('Should add items with same labels in the same stream', function () {
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[1]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[2]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[1]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[2]))
expect(batcher.batch.streams.length).toBe(1)
})
it('Should add items with different labels in separate streams', function () {
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[0]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[2]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[0]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[2]))
expect(batcher.batch.streams.length).toBe(2)
})
it('Should replace timestamps with Date.now() if replaceTimestamp is enabled', function () {
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[1]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[1]))

expect(batcher.batch.streams[0].entries[0].ts).toBe(
fixtures.logs[1].timestamp
Expand All @@ -84,15 +84,15 @@ describe('Batcher tests with JSON transport', function () {
options.replaceTimestamp = true
batcher = new Batcher(options)

batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[1]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[1]))

expect(batcher.batch.streams[0].entries[0].ts).not.toBe(
fixtures.logs[1].timestamp
)
})
it('Should be able to clear the batch of streams', function () {
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[0]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[2]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[0]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[2]))
expect(batcher.batch.streams.length).toBe(2)
batcher.clearBatch()
expect(batcher.batch.streams.length).toBe(0)
Expand All @@ -108,10 +108,10 @@ describe('Batcher tests with JSON transport', function () {
}
}
req.post.mockResolvedValue(responseObject)
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[1]))
batcher.pushLogEntry(fixtures.logs[1])

expect(req.post.mock.calls[0][req.post.mock.calls[0].length - 1]).toBe(
JSON.stringify({ streams: [JSON.parse(fixtures.logs_mapped[1])] })
JSON.stringify({ streams: [JSON.parse(fixtures.logs_mapped_after[1])] })
)
})
it('Should clear batch and resolve on successful send', async function () {
Expand All @@ -122,14 +122,14 @@ describe('Batcher tests with JSON transport', function () {
}
}
req.post.mockResolvedValue(responseObject)
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[0]))
batcher.pushLogEntry(fixtures.logs[0])

expect(batcher.batch.streams.length).toBe(1)

await batcher.sendBatchToLoki()

expect(req.post.mock.calls[0][req.post.mock.calls[0].length - 1]).toBe(
JSON.stringify({ streams: [JSON.parse(fixtures.logs_mapped[0])] })
JSON.stringify({ streams: [JSON.parse(fixtures.logs_mapped_after[0])] })
)
expect(batcher.batch.streams.length).toBe(0)
})
Expand All @@ -138,7 +138,7 @@ describe('Batcher tests with JSON transport', function () {
statusCode: 404
}
req.post.mockRejectedValue(errorObject)
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[0]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[0]))
expect(batcher.batch.streams.length).toBe(1)
try {
await batcher.sendBatchToLoki()
Expand All @@ -156,7 +156,7 @@ describe('Batcher tests with JSON transport', function () {
statusCode: 404
}
req.post.mockRejectedValue(errorObject)
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[0]))
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[0]))
expect(batcher.batch.streams.length).toBe(1)

try {
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('Batcher tests with JSON transport', function () {

batcher.circuitBreakerInterval = circuitBreakerInterval
batcher.run()
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[0]))
batcher.pushLogEntry(fixtures.logs[0])

expect(batcher.batch.streams.length).toBe(1)
expect(batcher.interval).toBe(fixtures.options_json.interval * 1000)
Expand Down
9 changes: 4 additions & 5 deletions test/batcher.protobuf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,13 @@ describe('Batcher tests with Protobuf + gRPC transport', function () {
}
}
req.post.mockResolvedValue(responseObject)
await batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped_before[1]))
await batcher.pushLogEntry(fixtures.logs[1])

const logEntryConverted = createProtoTimestamps(
JSON.parse(fixtures.logs_mapped[1])
fixtures.logs[1]
)
const buffer = logproto.PushRequest.encode({
streams: [logEntryConverted]
}).finish()
const preparedLogEntry = prepareProtoBatch({ streams: [ logEntryConverted ] })
const buffer = logproto.PushRequest.encode(preparedLogEntry).finish()

const snappy = require('snappy')
const data = snappy.compressSync(buffer)
Expand Down
8 changes: 5 additions & 3 deletions test/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@
"timestamp": 1546977515858,
"message": "you broke everything but not quite",
"level": "error",
"jeejee": "ebon"
"labels": {
"jeejee": "ebon"
}
}
],
"logs_mapped_before": [
"{\"labels\":\"{job=\\\"test\\\", level=\\\"info\\\"}\",\"entries\":[{\"ts\":1546977515828,\"line\":\"testings \"}]}",
"{\"labels\":\"{job=\\\"test\\\", level=\\\"error\\\"}\",\"entries\":[{\"ts\":1546977615848,\"line\":\"you broke everything \"} ]}",
"{\"labels\":\"{job=\\\"test\\\", level=\\\"error\\\"}\",\"entries\":[{\"ts\":1546977615848,\"line\":\"you broke everything but not quite \"}]}",
"{\"labels\":\"{job=\\\"test\\\", level=\\\"error\\\"}\",\"entries\":[{\"ts\":1546977515858,\"line\":\"you broke everything but not quite {\\\"jeejee\\\":\\\"ebon\\\"}\"}]}"],
"{\"labels\":\"{job=\\\"test\\\", level=\\\"error\\\", jeejee=\\\"ebon\\\"}\",\"entries\":[{\"ts\":1546977515858,\"line\":\"you broke everything but not quite \"]}"],
"logs_mapped_after": [
"{\"stream\":\"{job=\\\"test\\\", level=\\\"info\\\"}\",\"values\":[1546977515828,\"testings \"]}",
"{\"stream\":\"{job=\\\"test\\\", level=\\\"error\\\"}\",\"values\":[1546977615848,\"you broke everything \"]}",
"{\"stream\":\"{job=\\\"test\\\", level=\\\"error\\\"}\",\"values\":[1546977615848,\"you broke everything but not quite \"]}",
"{\"stream\":\"{job=\\\"test\\\", level=\\\"error\\\"}\",\"values\":[1546977515858,\"you broke everything but not quite {\\\"jeejee\\\":\\\"ebon\\\"}\"]}"
"{\"stream\":\"{job=\\\"test\\\", level=\\\"error\\\", jeejee=\\\"ebon\\\"}\",\"values\":[1546977515858,\"you broke everything but not quite \"]}"
],
"incorrect_mapping": "{ \"labelings\": \"{jobbings=\\\"test\\\", levelings=\\\"info\\\"}\", \"entries\": [ { \"tisisnotit\": 1546977515828, \"dontdodisline\": \"testings {}\" } ] }"
}
8 changes: 4 additions & 4 deletions test/transport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ describe('Integration tests', function () {
const lokiTransport = new LokiTransport(fixtures.options_json)
const spy = jest.spyOn(lokiTransport.batcher, 'pushLogEntry')
lokiTransport.log(fixtures.logs[0], () => {})
expect(spy).toHaveBeenCalledWith(JSON.parse(fixtures.logs_mapped[0]))
expect(spy).toHaveBeenCalledWith(JSON.parse(fixtures.logs_mapped_before[0]))
})
it('LokiTransport should map logs correctly from Winston to Grafana Loki format', function () {
const lokiTransport = new LokiTransport(fixtures.options_json)
lokiTransport.log(fixtures.logs[0], () => {})
expect(lokiTransport.batcher.batch.streams.length).toBe(1)
expect(lokiTransport.batcher.batch.streams[0]).toEqual(
JSON.parse(fixtures.logs_mapped[0])
fixtures.logs[0]
)
})
it("LokiTransport should append anything else than the message after it in the log's entry", function () {
Expand All @@ -55,14 +55,14 @@ describe('Integration tests', function () {
expect(lokiTransport.batcher.batch.streams.length).toBe(1)
expect(
JSON.stringify(lokiTransport.batcher.batch.streams[0]).replace(/\s/g, '')
).toEqual(fixtures.logs_mapped[3].replace(/\s/g, ''))
).toEqual(fixtures.logs_mapped_before[3].replace(/\s/g, ''))
})
it('LokiTransport should not append anything else after the message if there are no extra keys in the log object', function () {
const lokiTransport = new LokiTransport(fixtures.options_json)
lokiTransport.log(fixtures.logs[2], () => {})
expect(lokiTransport.batcher.batch.streams.length).toBe(1)
expect(
JSON.stringify(lokiTransport.batcher.batch.streams[0]).replace(/\s/g, '')
).toEqual(fixtures.logs_mapped[2].replace(/\s/g, ''))
).toEqual(fixtures.logs_mapped_before[2].replace(/\s/g, ''))
})
})

0 comments on commit fbbd59f

Please sign in to comment.