Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 82 additions & 73 deletions azure/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,44 @@ function fakeContext() {
return contextSpy;
}

var EventhubLogForwarderInstance = new client.EventhubLogForwarder(
fakeContext()
);
EventhubLogForwarderInstance.sendWithRetry = function(record) {}; // do nothing
console.log(EventhubLogForwarderInstance.getLogFormat);
var handleJsonLogsSpy = sinon.spy();
var handleStringLogsSpy = sinon.spy();
function setUp() {
var forwarder = new client.EventhubLogForwarder(fakeContext());
forwarder.sendWithRetry = function(record) {}; // do nothing

EventhubLogForwarderInstance.addTagsToJsonLog = handleJsonLogsSpy;
EventhubLogForwarderInstance.addTagsToStringLog = handleStringLogsSpy;
forwarder.addTagsToJsonLog = sinon.spy();
forwarder.addTagsToStringLog = sinon.spy();
return forwarder;
}

describe('Azure Log Monitoring', function() {
describe('#getLogFormat', function() {
beforeEach(function() {
this.forwarder = setUp();
});
it('should return string', function() {
eventHubMessages = '';
assert.equal(
constants.STRING,
EventhubLogForwarderInstance.getLogFormat(eventHubMessages)
this.forwarder.getLogFormat(eventHubMessages)
);
eventHubMessages = 'foobar';
assert.equal(
constants.STRING,
EventhubLogForwarderInstance.getLogFormat(eventHubMessages)
this.forwarder.getLogFormat(eventHubMessages)
);
});
it('should return string array', function() {
eventHubMessages = ['', 'foobar'];
assert.equal(
constants.STRING_ARRAY,
EventhubLogForwarderInstance.getLogFormat(eventHubMessages)
this.forwarder.getLogFormat(eventHubMessages)
);
});
it('should return json object', function() {
eventHubMessages = { key: 'value', otherkey: 'othervalue' };
assert.equal(
constants.JSON_OBJECT,
EventhubLogForwarderInstance.getLogFormat(eventHubMessages)
this.forwarder.getLogFormat(eventHubMessages)
);
});
it('should return json array when there are no records', function() {
Expand All @@ -59,39 +60,42 @@ describe('Azure Log Monitoring', function() {
];
assert.equal(
constants.JSON_ARRAY,
EventhubLogForwarderInstance.getLogFormat(eventHubMessages)
this.forwarder.getLogFormat(eventHubMessages)
);
});
it('should return invalid', function() {
eventHubMessages = 1;
assert.equal(
constants.INVALID,
EventhubLogForwarderInstance.getLogFormat(eventHubMessages)
this.forwarder.getLogFormat(eventHubMessages)
);
eventHubMessages = () => {};
assert.equal(
constants.INVALID,
EventhubLogForwarderInstance.getLogFormat(eventHubMessages)
this.forwarder.getLogFormat(eventHubMessages)
);
eventHubMessages = true;
assert.equal(
constants.INVALID,
EventhubLogForwarderInstance.getLogFormat(eventHubMessages)
this.forwarder.getLogFormat(eventHubMessages)
);
eventHubMessages = null;
assert.equal(
constants.INVALID,
EventhubLogForwarderInstance.getLogFormat(eventHubMessages)
this.forwarder.getLogFormat(eventHubMessages)
);
eventHubMessages = undefined;
assert.equal(
constants.INVALID,
EventhubLogForwarderInstance.getLogFormat(eventHubMessages)
this.forwarder.getLogFormat(eventHubMessages)
);
});
});

describe('#extractResourceId', function() {
beforeEach(function() {
this.forwarder = setUp();
});
it('should parse a valid record', function() {
record = {
resourceId:
Expand All @@ -106,7 +110,7 @@ describe('Azure Log Monitoring', function() {
};
assert.deepEqual(
expectedMetadata,
EventhubLogForwarderInstance.extractResourceId(record)
this.forwarder.extractResourceId(record)
);
});
it('should parse a valid record without provider', function() {
Expand All @@ -123,7 +127,7 @@ describe('Azure Log Monitoring', function() {
};
assert.deepEqual(
expectedMetadata,
EventhubLogForwarderInstance.extractResourceId(record)
this.forwarder.extractResourceId(record)
);
});
it('should parse a valid record without provider and resource group', function() {
Expand All @@ -137,31 +141,31 @@ describe('Azure Log Monitoring', function() {
};
assert.deepEqual(
expectedMetadata,
EventhubLogForwarderInstance.extractResourceId(record)
this.forwarder.extractResourceId(record)
);
});
it('should not fail on record without resourceId', function() {
record = { key: 'value' };
expectedMetadata = { tags: [], source: '' };
assert.deepEqual(
expectedMetadata,
EventhubLogForwarderInstance.extractResourceId(record)
this.forwarder.extractResourceId(record)
);
});
it('should not fail on string record', function() {
record = { key: 'value' };
expectedMetadata = { tags: [], source: '' };
assert.deepEqual(
expectedMetadata,
EventhubLogForwarderInstance.extractResourceId(record)
this.forwarder.extractResourceId(record)
);
});
it('should not fail on improper resourceId', function() {
record = { resourceId: 'foo/bar' };
expectedMetadata = { tags: [], source: '' };
assert.deepEqual(
expectedMetadata,
EventhubLogForwarderInstance.extractResourceId(record)
this.forwarder.extractResourceId(record)
);
});
it('should not fail with an invalid source', function() {
Expand All @@ -178,145 +182,150 @@ describe('Azure Log Monitoring', function() {
};
assert.deepEqual(
expectedMetadata,
EventhubLogForwarderInstance.extractResourceId(record)
this.forwarder.extractResourceId(record)
);
});
});

function testHandleLogs(logs, expected, assertJson) {
EventhubLogForwarderInstance.handleLogs(record);
if (assertJson == true) {
expected.forEach(message => {
sinon.assert.calledWith(handleJsonLogsSpy, message);
});
} else {
expected.forEach(message => {
sinon.assert.calledWith(handleStringLogsSpy, message);
});
}
function testHandleJSONLogs(forwarder, logs, expected) {
forwarder.handleLogs(logs);
expected.forEach(message => {
sinon.assert.calledWith(forwarder.addTagsToJsonLog, message);
});
}

function testHandleStringLogs(forwarder, logs, expected) {
forwarder.handleLogs(logs);
expected.forEach(message => {
sinon.assert.calledWith(forwarder.addTagsToStringLog, message);
});
}

describe('#handleLogs', function() {
beforeEach(function() {
this.forwarder = setUp();
});

it('should handle string properly', function() {
record = 'hello';
log = 'hello';
expected = ['hello'];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
this.forwarder.getLogFormat(log),
constants.STRING
);
testHandleLogs(record, expected, false);
testHandleStringLogs(this.forwarder, log, expected);
});

it('should handle json-string properly', function() {
record = '{"hello": "there"}';
log = '{"hello": "there"}';
expected = [{ hello: 'there' }];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
this.forwarder.getLogFormat(log),
constants.JSON_STRING
);
testHandleLogs(record, expected, true);
testHandleJSONLogs(this.forwarder, log, expected);
});

it('should handle json-object properly', function() {
record = { hello: 'there' };
log = { hello: 'there' };
expected = [{ hello: 'there' }];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
this.forwarder.getLogFormat(log),
constants.JSON_OBJECT
);
testHandleLogs(record, expected, true);
testHandleJSONLogs(this.forwarder, log, expected);
});

it('should handle string-array properly', function() {
record = ['one message', 'two message'];
log = ['one message', 'two message'];
expected = ['one message', 'two message'];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
this.forwarder.getLogFormat(log),
constants.STRING_ARRAY
);
testHandleLogs(record, expected, false);
testHandleStringLogs(this.forwarder, log, expected);
});

it('should handle json-records properly', function() {
record = [{ records: [{ hello: 'there' }, { goodbye: 'now' }] }];
log = [{ records: [{ hello: 'there' }, { goodbye: 'now' }] }];
expected = [{ hello: 'there' }, { goodbye: 'now' }];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
this.forwarder.getLogFormat(log),
constants.JSON_ARRAY
); //JSON_RECORDS
testHandleLogs(record, expected, true);
testHandleJSONLogs(this.forwarder, log, expected);
});

it('should handle json-array properly', function() {
record = [{ hello: 'there' }, { goodbye: 'now' }];
log = [{ hello: 'there' }, { goodbye: 'now' }];
expected = [{ hello: 'there' }, { goodbye: 'now' }];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
this.forwarder.getLogFormat(log),
constants.JSON_ARRAY
);
testHandleLogs(record, expected, true);
testHandleJSONLogs(this.forwarder, log, expected);
});

it('should handle buffer array properly', function() {
record = [Buffer.from('{"records": [{ "test": "testing"}]}')];
log = [Buffer.from('{"records": [{ "test": "testing"}]}')];
expected = [{ test: 'testing' }];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
this.forwarder.getLogFormat(log),
constants.BUFFER_ARRAY
);
testHandleLogs(record, expected, true);
testHandleJSONLogs(this.forwarder, log, expected);
});

it('should handle buffer array without records properly', function() {
record = [Buffer.from('{ "test": "example"}')];
log = [Buffer.from('{ "test": "example"}')];
expected = [{ test: 'example' }];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
this.forwarder.getLogFormat(log),
constants.BUFFER_ARRAY
);
testHandleLogs(record, expected, true);
testHandleJSONLogs(this.forwarder, log, expected);
});

it('should handle buffer array with malformed string', function() {
record = [Buffer.from('{"time": "xy')];
log = [Buffer.from('{"time": "xy')];
expected = ['{"time": "xy'];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
this.forwarder.getLogFormat(log),
constants.BUFFER_ARRAY
);
testHandleLogs(record, expected, false);
testHandleStringLogs(this.forwarder, log, expected);
});

it('should handle json-string-array properly records', function() {
record = ['{"records": [{ "time": "xyz"}, {"time": "abc"}]}'];
log = ['{"records": [{ "time": "xyz"}, {"time": "abc"}]}'];
expected = [{ time: 'xyz' }];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
this.forwarder.getLogFormat(log),
constants.JSON_STRING_ARRAY
);
testHandleLogs(record, expected, true);
testHandleJSONLogs(this.forwarder, log, expected);
});

it('should handle json-string-array properly no records', function() {
record = ['{"time": "xyz"}'];
log = ['{"time": "xyz"}'];
expected = [{ time: 'xyz' }];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
this.forwarder.getLogFormat(log),
constants.JSON_STRING_ARRAY
);
testHandleLogs(record, expected, true);
testHandleJSONLogs(this.forwarder, log, expected);
});

it('should handle json-string-array with malformed string', function() {
record = ['{"time": "xyz"}', '{"time": "xy'];
log = ['{"time": "xyz"}', '{"time": "xy'];
expected = ['{"time": "xy'];
assert.equal(
EventhubLogForwarderInstance.getLogFormat(record),
this.forwarder.getLogFormat(log),
constants.JSON_STRING_ARRAY
);
// just assert that the string method is called for the second message,
// we don't care about the first one for this test
testHandleLogs(record, expected, false);
testHandleStringLogs(this.forwarder, log, expected);
});
});
});