diff --git a/azure/activity_logs_monitoring/index.js b/azure/activity_logs_monitoring/index.js index 40053cae7..189edde6e 100644 --- a/azure/activity_logs_monitoring/index.js +++ b/azure/activity_logs_monitoring/index.js @@ -3,9 +3,9 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). // Copyright 2020 Datadog, Inc. -var tls = require('tls'); +var https = require('https'); -const VERSION = '0.1.1'; +const VERSION = '0.1.2'; const STRING = 'string'; // example: 'some message' const STRING_ARRAY = 'string-array'; // example: ['one message', 'two message', ...] @@ -15,196 +15,247 @@ const JSON_STRING = 'json-string'; // example: '{"key": "value"}' const JSON_STRING_ARRAY = 'json-string-array'; // example: ['{"records": [{}, {}]}'] or ['{"key": "value"}'] const INVALID = 'invalid'; +const JSON_TYPE = 'json'; +const STRING_TYPE = 'string'; + const DD_API_KEY = process.env.DD_API_KEY || ''; const DD_SITE = process.env.DD_SITE || 'datadoghq.com'; -const DD_URL = process.env.DD_URL || 'functions-intake.logs.' + DD_SITE; -const DD_PORT = process.env.DD_PORT || DD_SITE === 'datadoghq.eu' ? 443 : 10516; +const DD_URL = process.env.DD_URL || 'http-intake.logs.' + DD_SITE; +const DD_PORT = process.env.DD_PORT || 443; const DD_TAGS = process.env.DD_TAGS || ''; // Replace '' by your comma-separated list of tags const DD_SERVICE = process.env.DD_SERVICE || 'azure'; const DD_SOURCE = process.env.DD_SOURCE || 'azure'; const DD_SOURCE_CATEGORY = process.env.DD_SOURCE_CATEGORY || 'azure'; -module.exports = function(context, eventHubMessages) { - if (!DD_API_KEY || DD_API_KEY === '') { - context.log.error( - 'You must configure your API key before starting this function (see ## Parameters section)' - ); - return; +class EventhubLogForwarder { + constructor(context) { + this.context = context; + this.options = { + hostname: DD_URL, + port: 443, + path: '/v1/input', + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'DD-API-KEY': DD_API_KEY + }, + timeout: 2000 + }; } - var socket = getSocket(context); - var sender = tagger => record => { - record = tagger(record, context); - if (!send(socket, record)) { - // Retry once - socket = getSocket(context); - send(socket, record); + formatLogAndSend(messageType, record) { + if (messageType == JSON_TYPE) { + record = this.addTagsToJsonLog(record); + } else { + record = this.addTagsToStringLog(record); } - }; - - handleLogs(sender, eventHubMessages, context); - - socket.end(); - context.done(); -}; + return this.sendWithRetry(record); + } -function getSocket(context) { - var socket = tls.connect({ port: DD_PORT, host: DD_URL }); - socket.on('error', err => { - context.log.error(err.toString()); - socket.end(); - }); + sendWithRetry(record) { + return new Promise((resolve, reject) => { + return this.send(record) + .then(res => { + resolve(); + }) + .catch(err => { + setTimeout(() => { + this.send(record) + .then(resolve) + .catch(err => { + this.context.log.error( + `unable to send request after 2 tries, err: ${err}` + ); + reject(); + }); + }, 1000); + }); + }); + } - return socket; -} + send(record) { + return new Promise((resolve, reject) => { + const req = https + .request(this.options, resp => { + if (resp.statusCode < 200 || resp.statusCode > 299) { + reject(`invalid status code ${resp.statusCode}`); + } else { + resolve(); + } + }) + .on('error', error => { + reject(error); + }); + req.write(JSON.stringify(record)); + req.end(); + }); + } -function send(socket, record) { - return socket.write(DD_API_KEY + ' ' + JSON.stringify(record) + '\n'); -} + handleLogs(logs) { + var promises = []; + var logsType = this.getLogFormat(logs); + switch (logsType) { + case STRING: + promises.push(this.formatLogAndSend(STRING_TYPE, logs)); + break; + case JSON_STRING: + logs = JSON.parse(logs); + promises.push(this.formatLogAndSend(JSON_TYPE, logs)); + break; + case JSON_OBJECT: + promises.push(this.formatLogAndSend(JSON_TYPE, logs)); + break; + case STRING_ARRAY: + logs.forEach(log => + promises.push(this.formatLogAndSend(STRING_TYPE, log)) + ); + break; + case JSON_ARRAY: + promises = this.handleJSONArrayLogs(logs, JSON_ARRAY); + break; + case JSON_STRING_ARRAY: + promises = this.handleJSONArrayLogs(logs, JSON_STRING_ARRAY); + break; + case INVALID: + default: + this.context.log.warn('logs format is invalid'); + break; + } + return promises; + } -function handleLogs(sender, logs, context) { - var logsType = getLogFormat(logs); - switch (logsType) { - case STRING: - sender(addTagsToStringLog)(logs); - break; - case JSON_STRING: - logs = JSON.parse(logs); - sender(addTagsToJsonLog)(logs); - break; - case JSON_OBJECT: - sender(addTagsToJsonLog)(logs); - break; - case STRING_ARRAY: - logs.forEach(sender(addTagsToStringLog)); - break; - case JSON_ARRAY: - handleJSONArrayLogs(sender, context, logs, JSON_ARRAY); - break; - case JSON_STRING_ARRAY: - handleJSONArrayLogs(sender, context, logs, JSON_STRING_ARRAY); - break; - case INVALID: - default: - context.log.warn('logs format is invalid'); - break; + handleJSONArrayLogs(logs, logsType) { + var promises = []; + logs.forEach(message => { + if (logsType == JSON_STRING_ARRAY) { + try { + message = JSON.parse(message); + } catch (err) { + this.context.log.warn( + 'log is malformed json, sending as string' + ); + promises.push(this.formatLogAndSend(STRING_TYPE, message)); + return; + } + } + if (message.records != undefined) { + message.records.forEach(message => + promises.push(this.formatLogAndSend(JSON_TYPE, message)) + ); + } else { + this.formatLogAndSend(JSON_TYPE, message); + } + }); + return promises; } -} -function handleJSONArrayLogs(sender, context, logs, logsType) { - logs.forEach(message => { - if (logsType == JSON_STRING_ARRAY) { - try { - message = JSON.parse(message); - } catch (err) { - context.log.warn('log is malformed json, sending as string'); - sender(addTagsToStringLog)(message); - return; + getLogFormat(logs) { + if (typeof logs === 'string') { + if (this.isJsonString(logs)) { + return JSON_STRING; } + return STRING; } - if (message.records != undefined) { - message.records.forEach(sender(addTagsToJsonLog)); - } else { - sender(addTagsToJsonLog)(message); + if (!Array.isArray(logs) && typeof logs === 'object' && logs !== null) { + return JSON_OBJECT; } - }); -} - -function getLogFormat(logs) { - if (typeof logs === 'string') { - if (isJsonString(logs)) { - return JSON_STRING; + if (!Array.isArray(logs)) { + return INVALID; + } + if (typeof logs[0] === 'object') { + return JSON_ARRAY; + } + if (typeof logs[0] === 'string') { + if (this.isJsonString(logs[0])) { + return JSON_STRING_ARRAY; + } else { + return STRING_ARRAY; + } } - return STRING; - } - if (!Array.isArray(logs) && typeof logs === 'object' && logs !== null) { - return JSON_OBJECT; - } - if (!Array.isArray(logs)) { return INVALID; } - if (typeof logs[0] === 'object') { - return JSON_ARRAY; - } - if (typeof logs[0] === 'string') { - if (isJsonString(logs[0])) { - return JSON_STRING_ARRAY; - } else { - return STRING_ARRAY; + + isJsonString(record) { + try { + JSON.parse(record); + return true; + } catch (err) { + return false; } } - return INVALID; -} -function isJsonString(record) { - try { - JSON.parse(record); - return true; - } catch (err) { - return false; + addTagsToJsonLog(record) { + var metadata = this.extractResourceId(record); + record['ddsource'] = metadata.source || DD_SOURCE; + record['ddsourcecategory'] = DD_SOURCE_CATEGORY; + record['service'] = DD_SERVICE; + record['ddtags'] = metadata.tags + .concat([ + DD_TAGS, + 'forwardername:' + this.context.executionContext.functionName + ]) + .filter(Boolean) + .join(','); + return record; } -} - -function addTagsToJsonLog(record, context) { - metadata = extractResourceId(record); - record['ddsource'] = metadata.source || DD_SOURCE; - record['ddsourcecategory'] = DD_SOURCE_CATEGORY; - record['service'] = DD_SERVICE; - record['ddtags'] = metadata.tags - .concat([ - DD_TAGS, - 'forwardername:' + context.executionContext.functionName - ]) - .filter(Boolean) - .join(','); - return record; -} -function addTagsToStringLog(stringLog, context) { - jsonLog = { message: stringLog }; - return addTagsToJsonLog(jsonLog, context); -} + addTagsToStringLog(stringLog) { + var jsonLog = { message: stringLog }; + return this.addTagsToJsonLog(jsonLog); + } -function extractResourceId(record) { - metadata = { tags: [], source: '' }; - if ( - record.resourceId === undefined || - typeof record.resourceId !== 'string' - ) { - return metadata; - } else if (record.resourceId.toLowerCase().startsWith('/subscriptions/')) { - var resourceId = record.resourceId.toLowerCase().split('/'); - if (resourceId.length > 2) { - metadata.tags.push('subscription_id:' + resourceId[2]); - } - if (resourceId.length > 4) { - metadata.tags.push('resource_group:' + resourceId[4]); - } - if (resourceId.length > 6 && resourceId[6]) { - metadata.source = resourceId[6].replace('microsoft.', 'azure.'); - } - return metadata; - } else if (record.resourceId.toLowerCase().startsWith('/tenants/')) { - var resourceId = record.resourceId.toLowerCase().split('/'); - if (resourceId.length > 4 && resourceId[4]) { - metadata.tags.push('tenant:' + resourceId[2]); - metadata.source = resourceId[4] - .replace('microsoft.', 'azure.') - .replace('aadiam', 'activedirectory'); + extractResourceId(record) { + var metadata = { tags: [], source: '' }; + if ( + record.resourceId === undefined || + typeof record.resourceId !== 'string' + ) { + return metadata; + } else if ( + record.resourceId.toLowerCase().startsWith('/subscriptions/') + ) { + var resourceId = record.resourceId.toLowerCase().split('/'); + if (resourceId.length > 2) { + metadata.tags.push('subscription_id:' + resourceId[2]); + } + if (resourceId.length > 4) { + metadata.tags.push('resource_group:' + resourceId[4]); + } + if (resourceId.length > 6 && resourceId[6]) { + metadata.source = resourceId[6].replace('microsoft.', 'azure.'); + } + return metadata; + } else if (record.resourceId.toLowerCase().startsWith('/tenants/')) { + var resourceId = record.resourceId.toLowerCase().split('/'); + if (resourceId.length > 4 && resourceId[4]) { + metadata.tags.push('tenant:' + resourceId[2]); + metadata.source = resourceId[4] + .replace('microsoft.', 'azure.') + .replace('aadiam', 'activedirectory'); + } + return metadata; + } else { + return metadata; } - return metadata; - } else { - return metadata; } } +module.exports = async function(context, eventHubMessages) { + if (!DD_API_KEY || DD_API_KEY === '') { + context.log.error( + 'You must configure your API key before starting this function (see ## Parameters section)' + ); + return; + } + var promises = new EventhubLogForwarder(context).handleLogs( + eventHubMessages + ); + + return Promise.allSettled(promises); +}; + module.exports.forTests = { - getLogFormat, - extractResourceId, - handleLogs, - isJsonString, - addTagsToStringLog, - addTagsToJsonLog, + EventhubLogForwarder, constants: { STRING, STRING_ARRAY, diff --git a/azure/test/client.test.js b/azure/test/client.test.js index eb0534775..1ca5b460b 100644 --- a/azure/test/client.test.js +++ b/azure/test/client.test.js @@ -1,180 +1,292 @@ var assert = require('assert'); var client = require('../activity_logs_monitoring').forTests; -var constants = client.constants -var sinon = require('sinon') +var constants = client.constants; +var sinon = require('sinon'); +function fakeContext() { + // create a fake context object to pass into handleLogs + contextSpy = sinon.spy(); + contextSpy.log = sinon.spy(); + contextSpy.log.error = function(x) {}; // do nothing + contextSpy.log.warn = function(x) {}; // do nothing + + 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(); + +EventhubLogForwarderInstance.addTagsToJsonLog = handleJsonLogsSpy; +EventhubLogForwarderInstance.addTagsToStringLog = handleStringLogsSpy; describe('Azure Log Monitoring', function() { - describe('#getLogFormat', function() { - it('should return string', function() { - eventHubMessages = ''; - assert.equal(constants.STRING, client.getLogFormat(eventHubMessages)); - eventHubMessages = 'foobar'; - assert.equal(constants.STRING, client.getLogFormat(eventHubMessages)); - }); - it('should return string array', function() { - eventHubMessages = ['', 'foobar']; - assert.equal(constants.STRING_ARRAY, client.getLogFormat(eventHubMessages)); - }); - it('should return json object', function() { - eventHubMessages = {'key': 'value', 'otherkey':'othervalue'}; - assert.equal(constants.JSON_OBJECT, client.getLogFormat(eventHubMessages)); + describe('#getLogFormat', function() { + it('should return string', function() { + eventHubMessages = ''; + assert.equal( + constants.STRING, + EventhubLogForwarderInstance.getLogFormat(eventHubMessages) + ); + eventHubMessages = 'foobar'; + assert.equal( + constants.STRING, + EventhubLogForwarderInstance.getLogFormat(eventHubMessages) + ); + }); + it('should return string array', function() { + eventHubMessages = ['', 'foobar']; + assert.equal( + constants.STRING_ARRAY, + EventhubLogForwarderInstance.getLogFormat(eventHubMessages) + ); + }); + it('should return json object', function() { + eventHubMessages = { key: 'value', otherkey: 'othervalue' }; + assert.equal( + constants.JSON_OBJECT, + EventhubLogForwarderInstance.getLogFormat(eventHubMessages) + ); + }); + it('should return json array when there are no records', function() { + eventHubMessages = [ + { key: 'value', otherkey: 'othervalue' }, + { key: 'value', otherkey: 'othervalue' } + ]; + assert.equal( + constants.JSON_ARRAY, + EventhubLogForwarderInstance.getLogFormat(eventHubMessages) + ); + }); + it('should return invalid', function() { + eventHubMessages = 1; + assert.equal( + constants.INVALID, + EventhubLogForwarderInstance.getLogFormat(eventHubMessages) + ); + eventHubMessages = () => {}; + assert.equal( + constants.INVALID, + EventhubLogForwarderInstance.getLogFormat(eventHubMessages) + ); + eventHubMessages = true; + assert.equal( + constants.INVALID, + EventhubLogForwarderInstance.getLogFormat(eventHubMessages) + ); + eventHubMessages = null; + assert.equal( + constants.INVALID, + EventhubLogForwarderInstance.getLogFormat(eventHubMessages) + ); + eventHubMessages = undefined; + assert.equal( + constants.INVALID, + EventhubLogForwarderInstance.getLogFormat(eventHubMessages) + ); + }); }); - it('should return json no records', function() { - eventHubMessages = [{'key': 'value', 'otherkey':'othervalue'}, {'key': 'value', 'otherkey':'othervalue'}]; - assert.equal(constants.JSON_ARRAY, client.getLogFormat(eventHubMessages)); - }); - it('should return invalid', function() { - eventHubMessages = 1; - assert.equal(constants.INVALID, client.getLogFormat(eventHubMessages)); - eventHubMessages = () => {}; - assert.equal(constants.INVALID, client.getLogFormat(eventHubMessages)); - eventHubMessages = true; - assert.equal(constants.INVALID, client.getLogFormat(eventHubMessages)); - eventHubMessages = null; - assert.equal(constants.INVALID, client.getLogFormat(eventHubMessages)); - eventHubMessages = undefined; - assert.equal(constants.INVALID, client.getLogFormat(eventHubMessages)); - }); - }); - describe('#extractResourceId', function() { - it('should parse a valid record', function() { - record = {'resourceId': '/SUBSCRIPTIONS/12345678-1234-ABCD-1234-1234567890AB/RESOURCEGROUPS/SOME-RESOURCE-GROUP/PROVIDERS/MICROSOFT.COMPUTE/VIRTUALMACHINES/SOME-VM'} - expectedMetadata = {'tags': ["subscription_id:12345678-1234-abcd-1234-1234567890ab","resource_group:some-resource-group"], 'source': 'azure.compute'} - assert.deepEqual(expectedMetadata, client.extractResourceId(record)) - }); - it('should parse a valid record without provider', function() { - record = {'resourceId': '/SUBSCRIPTIONS/12345678-1234-ABCD-1234-1234567890AB/RESOURCEGROUPS/SOME-RESOURCE-GROUP'} - expectedMetadata = {'tags': ["subscription_id:12345678-1234-abcd-1234-1234567890ab","resource_group:some-resource-group"], 'source': ''} - assert.deepEqual(expectedMetadata, client.extractResourceId(record)) - }); - it('should parse a valid record without provider and resource group', function() { - record = {'resourceId': '/SUBSCRIPTIONS/12345678-1234-ABCD-1234-1234567890AB'} - expectedMetadata = {'tags': ["subscription_id:12345678-1234-abcd-1234-1234567890ab"], 'source': ''} - assert.deepEqual(expectedMetadata, client.extractResourceId(record)) - }); - it('should not fail on record without resourceId', function() { - record = {'key':'value'} - expectedMetadata = {'tags': [], 'source': ''} - assert.deepEqual(expectedMetadata, client.extractResourceId(record)) - }); - it('should not fail on string record', function() { - record = {'key':'value'} - expectedMetadata = {'tags': [], 'source': ''} - assert.deepEqual(expectedMetadata, client.extractResourceId(record)) + describe('#extractResourceId', function() { + it('should parse a valid record', function() { + record = { + resourceId: + '/SUBSCRIPTIONS/12345678-1234-ABCD-1234-1234567890AB/RESOURCEGROUPS/SOME-RESOURCE-GROUP/PROVIDERS/MICROSOFT.COMPUTE/VIRTUALMACHINES/SOME-VM' + }; + expectedMetadata = { + tags: [ + 'subscription_id:12345678-1234-abcd-1234-1234567890ab', + 'resource_group:some-resource-group' + ], + source: 'azure.compute' + }; + assert.deepEqual( + expectedMetadata, + EventhubLogForwarderInstance.extractResourceId(record) + ); + }); + it('should parse a valid record without provider', function() { + record = { + resourceId: + '/SUBSCRIPTIONS/12345678-1234-ABCD-1234-1234567890AB/RESOURCEGROUPS/SOME-RESOURCE-GROUP' + }; + expectedMetadata = { + tags: [ + 'subscription_id:12345678-1234-abcd-1234-1234567890ab', + 'resource_group:some-resource-group' + ], + source: '' + }; + assert.deepEqual( + expectedMetadata, + EventhubLogForwarderInstance.extractResourceId(record) + ); + }); + it('should parse a valid record without provider and resource group', function() { + record = { + resourceId: + '/SUBSCRIPTIONS/12345678-1234-ABCD-1234-1234567890AB' + }; + expectedMetadata = { + tags: ['subscription_id:12345678-1234-abcd-1234-1234567890ab'], + source: '' + }; + assert.deepEqual( + expectedMetadata, + EventhubLogForwarderInstance.extractResourceId(record) + ); + }); + it('should not fail on record without resourceId', function() { + record = { key: 'value' }; + expectedMetadata = { tags: [], source: '' }; + assert.deepEqual( + expectedMetadata, + EventhubLogForwarderInstance.extractResourceId(record) + ); + }); + it('should not fail on string record', function() { + record = { key: 'value' }; + expectedMetadata = { tags: [], source: '' }; + assert.deepEqual( + expectedMetadata, + EventhubLogForwarderInstance.extractResourceId(record) + ); + }); + it('should not fail on improper resourceId', function() { + record = { resourceId: 'foo/bar' }; + expectedMetadata = { tags: [], source: '' }; + assert.deepEqual( + expectedMetadata, + EventhubLogForwarderInstance.extractResourceId(record) + ); + }); + it('should not fail with an invalid source', function() { + record = { + resourceId: + '/SUBSCRIPTIONS/12345678-1234-ABCD-1234-1234567890AB/RESOURCEGROUPS/SOME-RESOURCE-GROUP/PROVIDERS/////' + }; + expectedMetadata = { + tags: [ + 'subscription_id:12345678-1234-abcd-1234-1234567890ab', + 'resource_group:some-resource-group' + ], + source: '' + }; + assert.deepEqual( + expectedMetadata, + EventhubLogForwarderInstance.extractResourceId(record) + ); + }); }); - it('should not fail on improper resourceId', function() { - record = {'resourceId': 'foo/bar'} - expectedMetadata = {'tags': [], 'source': ''} - assert.deepEqual(expectedMetadata, client.extractResourceId(record)) - }); - it('should not fail with an invalid source', function() { - record = {'resourceId': '/SUBSCRIPTIONS/12345678-1234-ABCD-1234-1234567890AB/RESOURCEGROUPS/SOME-RESOURCE-GROUP/PROVIDERS/////'} - expectedMetadata = {'tags': ["subscription_id:12345678-1234-abcd-1234-1234567890ab","resource_group:some-resource-group"], 'source': ''} - assert.deepEqual(expectedMetadata, client.extractResourceId(record)) - }); - }) - function fakeContext() { - // create a fake context object to pass into handleLogs - contextSpy = sinon.spy() - contextSpy.log = sinon.spy() - contextSpy.log.error = function(x) {} // do nothing - contextSpy.log.warn = function(x) {} // do nothing - - return contextSpy - } - - function testHandleLogs(logs, expected, assertJson) { - // create a spy to mock the sender call - var handleJsonLogsSpy = sinon.spy(); - var handleStringLogsSpy = sinon.spy(); - - sender = function(tagger) { - if (tagger === client.addTagsToJsonLog) { - return handleJsonLogsSpy; - } else { - return handleStringLogsSpy; - } + 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); + }); + } } - client.handleLogs(sender, record, fakeContext()); - if (assertJson == true) { - expected.forEach(message => { - sinon.assert.calledWith(handleJsonLogsSpy, message) - }); - } else { - expected.forEach(message => { - sinon.assert.calledWith(handleStringLogsSpy, message) - }) - } - } - - describe('#handleLogs', function() { - it('should handle string properly', function() { - record = 'hello' - expected = ['hello'] - assert.equal(client.getLogFormat(record), constants.STRING) - testHandleLogs(record, expected, false) - }); + describe('#handleLogs', function() { + it('should handle string properly', function() { + record = 'hello'; + expected = ['hello']; + assert.equal( + EventhubLogForwarderInstance.getLogFormat(record), + constants.STRING + ); + testHandleLogs(record, expected, false); + }); - it('should handle json-string properly', function() { - record = '{"hello": "there"}' - expected = [{'hello': 'there'}] - assert.equal(client.getLogFormat(record), constants.JSON_STRING) - testHandleLogs(record, expected, true) - }); + it('should handle json-string properly', function() { + record = '{"hello": "there"}'; + expected = [{ hello: 'there' }]; + assert.equal( + EventhubLogForwarderInstance.getLogFormat(record), + constants.JSON_STRING + ); + testHandleLogs(record, expected, true); + }); - it('should handle json-object properly', function() { - record = {'hello': 'there'} - expected = [{'hello': 'there'}] - assert.equal(client.getLogFormat(record), constants.JSON_OBJECT) - testHandleLogs(record, expected, true) - }); + it('should handle json-object properly', function() { + record = { hello: 'there' }; + expected = [{ hello: 'there' }]; + assert.equal( + EventhubLogForwarderInstance.getLogFormat(record), + constants.JSON_OBJECT + ); + testHandleLogs(record, expected, true); + }); - it('should handle string-array properly', function() { - record = ['one message', 'two message'] - expected = ['one message', 'two message'] - assert.equal(client.getLogFormat(record), constants.STRING_ARRAY) - testHandleLogs(record, expected, false) - }); + it('should handle string-array properly', function() { + record = ['one message', 'two message']; + expected = ['one message', 'two message']; + assert.equal( + EventhubLogForwarderInstance.getLogFormat(record), + constants.STRING_ARRAY + ); + testHandleLogs(record, expected, false); + }); - it('should handle json-records properly', function() { - record = [{"records": [{"hello": "there"}, {"goodbye": "now"}]}] - expected = [{"hello": "there"}, {"goodbye": "now"}] - assert.equal(client.getLogFormat(record), constants.JSON_ARRAY) //JSON_RECORDS - testHandleLogs(record, expected, true) - }); + it('should handle json-records properly', function() { + record = [{ records: [{ hello: 'there' }, { goodbye: 'now' }] }]; + expected = [{ hello: 'there' }, { goodbye: 'now' }]; + assert.equal( + EventhubLogForwarderInstance.getLogFormat(record), + constants.JSON_ARRAY + ); //JSON_RECORDS + testHandleLogs(record, expected, true); + }); - it('should handle json-array properly', function() { - record = [{"hello": "there"}, {"goodbye": "now"}] - expected = [{"hello": "there"}, {"goodbye": "now"}] - assert.equal(client.getLogFormat(record), constants.JSON_ARRAY) - testHandleLogs(record, expected, true) - }); + it('should handle json-array properly', function() { + record = [{ hello: 'there' }, { goodbye: 'now' }]; + expected = [{ hello: 'there' }, { goodbye: 'now' }]; + assert.equal( + EventhubLogForwarderInstance.getLogFormat(record), + constants.JSON_ARRAY + ); + testHandleLogs(record, expected, true); + }); - it('should handle json-string-array properly records', function() { - record = ['{"records": [{ "time": "xyz"}, {"time": "abc"}]}'] - expected = [{"time": "xyz"}] - assert.equal(client.getLogFormat(record), constants.JSON_STRING_ARRAY) - testHandleLogs(record, expected, true) - }); + it('should handle json-string-array properly records', function() { + record = ['{"records": [{ "time": "xyz"}, {"time": "abc"}]}']; + expected = [{ time: 'xyz' }]; + assert.equal( + EventhubLogForwarderInstance.getLogFormat(record), + constants.JSON_STRING_ARRAY + ); + testHandleLogs(record, expected, true); + }); - it('should handle json-string-array properly no records', function() { - record = ['{"time": "xyz"}'] - expected = [{"time": "xyz"}] - assert.equal(client.getLogFormat(record), constants.JSON_STRING_ARRAY) - testHandleLogs(record, expected, true) - }); + it('should handle json-string-array properly no records', function() { + record = ['{"time": "xyz"}']; + expected = [{ time: 'xyz' }]; + assert.equal( + EventhubLogForwarderInstance.getLogFormat(record), + constants.JSON_STRING_ARRAY + ); + testHandleLogs(record, expected, true); + }); - it('should handle json-string-array with malformed string', function() { - record = ['{"time": "xyz"}', '{"time": "xy'] - expected = ['{"time": "xy'] - assert.equal(client.getLogFormat(record), 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) + it('should handle json-string-array with malformed string', function() { + record = ['{"time": "xyz"}', '{"time": "xy']; + expected = ['{"time": "xy']; + assert.equal( + EventhubLogForwarderInstance.getLogFormat(record), + 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); + }); }); - }) });