Skip to content

Commit

Permalink
Enhance tests for ingest failure
Browse files Browse the repository at this point in the history
  • Loading branch information
ikemsley committed Jan 16, 2018
1 parent 3da1f3a commit f6947d1
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 20 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package-lock.json
node_modules/
cwe/test/report.xml
test/report.xml
#idea
.idea/
nbproject/
.nyc_output/
al-cwe-collector.zip
coverage/
test/ide_test.js
.env
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ function getKinesisData(event, callback) {
function filterGDEvents(cwEvents, callback) {
async.filter(cwEvents,
function(cwEvent, filterCallback){
return filterCallback(null, cwEvent.source && cwEvent.source == 'aws.guardduty');
return filterCallback(null, cwEvent.source &&
cwEvent.source === 'aws.guardduty');
},
callback
);
Expand Down Expand Up @@ -225,7 +226,7 @@ function processKinesisRecords(event, context) {
});
},
function(aimsC, collectedData, callback) {
if (collectedData != '') {
if (collectedData !== '') {
sendToIngest(event, context, aimsC, collectedData, callback);
} else {
return callback(null);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
"repository": {},
"private": true,
"scripts": {
"start": "node index.js",
"lint": "jshint --exclude \"./node_modules/*\" **/*.js",
"test": "JUNIT_REPORT_PATH=./test/report.xml nyc --reporter=cobertura mocha --colors --reporter mocha-jenkins-reporter"
},
"devDependencies": {
"aws-sdk": "*",
"aws-sdk-mock": "*",
"dotenv": "*",
"clone": "*",
"jshint": "^2.9.5",
"mocha": "^3.5.3",
Expand Down
10 changes: 5 additions & 5 deletions test/cwe_checkin_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('CWE Checkin Tests', function() {

before(function() {
setAzcollectStub();
});
});

beforeEach(function() {
rewireProcessCheckin = cweRewire.__get__('processCheckin');
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('CWE Checkin Tests', function() {

it('checkin waterfall flow error - getDecryptedCredentials()', function(done) {
var context = {
fail : (reason) => { if (reason == 'decryption_error') done(); }
fail : (reason) => { if (reason === 'decryption_error') done(); }
};
rewireGetDecryptedCredentials = cweRewire.__set__(
{getDecryptedCredentials: function(callback) { callback('decryption_error'); }}
Expand All @@ -83,7 +83,7 @@ describe('CWE Checkin Tests', function() {

it('checkin waterfall flow error - getAlAuth()', function(done) {
var context = {
fail : (reason) => { if (reason == 'test error') done(); }
fail : (reason) => { if (reason === 'test error') done(); }
};
rewireGetAlAuth = cweRewire.__set__(
{getAlAuth: function(callback) { callback('test error'); }}
Expand All @@ -93,7 +93,7 @@ describe('CWE Checkin Tests', function() {

it('checkin waterfall flow error - checkHealth()', function(done) {
var context = {
fail : (reason) => { if (reason == 'check_health_error') done(); }
fail : (reason) => { if (reason === 'check_health_error') done(); }
};
checkinStub = sinon.stub(cweCheckin, 'checkHealth').callsFake(
function fakeFn(event, context, callback) {
Expand Down Expand Up @@ -368,7 +368,7 @@ describe('CWE Checkin Tests', function() {
var rewireGetDecryptedCredentials;
var rewireGetAlAuth;
var rewireProcessCheckin;
var rewireCheckHealth;
var checkHealthStub;
var sendCheckinStub;

before(function() {
Expand Down
14 changes: 13 additions & 1 deletion test/cwe_mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const AIMS_TEST_CREDS = {
secret_key: 'test-secret-key'
};

const TEST_AL_API = 'api.test.product.dev.alertlogic.com';
const INGEST_TEST_URL = '/data/secmsgs';
const CHECKIN_TEST_FUNCTION_NAME = 'test-CollectLambdaFunction-1JNNKQIPOTEST';
const CHECKIN_TEST_URL = '/aws/cwe/checkin/353333894008/us-east-1/' + encodeURIComponent(CHECKIN_TEST_FUNCTION_NAME);
const FUNCTION_ARN = 'arn:aws:lambda:us-east-1:352283894008:function:test-guardduty-01-CollectLambdaFunction-2CWNLPPW5XO8';
Expand Down Expand Up @@ -392,7 +394,15 @@ const CLOUDWATCH_GET_METRIC_STATS_OK = {
'Label': 'Invocations'
};

const TEST_COLLECTED_BATCH = {
collected_batch: {
source_id: FUNCTION_ARN,
collected_messages: [GD_EVENT]
}
};

module.exports = {
TEST_AL_API : TEST_AL_API,
AIMS_TEST_CREDS : AIMS_TEST_CREDS,
CHECKIN_TEST_FUNCTION_NAME : CHECKIN_TEST_FUNCTION_NAME,
CHECKIN_TEST_URL : CHECKIN_TEST_URL,
Expand All @@ -415,5 +425,7 @@ module.exports = {
CWE_RULE_NAME : CWE_RULE_NAME,
DEFAULT_LAMBDA_CONTEXT : DEFAULT_LAMBDA_CONTEXT,
LAMBDA_LIST_EVENTSOURCE_MAPPINGS_OK : LAMBDA_LIST_EVENTSOURCE_MAPPINGS_OK,
CLOUDWATCH_GET_METRIC_STATS_OK : CLOUDWATCH_GET_METRIC_STATS_OK
CLOUDWATCH_GET_METRIC_STATS_OK : CLOUDWATCH_GET_METRIC_STATS_OK,
TEST_COLLECTED_BATCH : TEST_COLLECTED_BATCH,
INGEST_TEST_URL : INGEST_TEST_URL
};
105 changes: 94 additions & 11 deletions test/cwe_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('CWE Unit Tests', function() {
it('registration waterfall flow OK - sendRegistration()', function(done) {
var context = {
functionName : cweMock.REGISTRATION_TEST_FUNCTION_NAME,
fail : (reason) => { if (reason == 'test error') done(); },
fail : (reason) => { if (reason === 'test error') done(); },
done : () => { done(); }
};
rewireProcessRegistration(cweMock.REGISTRATION_TEST_EVENT, context, true);
Expand All @@ -112,7 +112,7 @@ describe('CWE Unit Tests', function() {

it('registration waterfall flow error - getAlAuth()', function(done) {
var context = {
fail : (reason) => { if (reason == 'test error') done(); }
fail : (reason) => { if (reason === 'test error') done(); }
};
rewireGetAlAuth = cweRewire.__set__(
{getAlAuth: function(callback) { callback('test error'); }}
Expand Down Expand Up @@ -166,7 +166,7 @@ describe('CWE Unit Tests', function() {
it('deregistration waterfall flow OK - sendRegistration()', function(done) {
var context = {
functionName : cweMock.REGISTRATION_TEST_FUNCTION_NAME,
fail : (reason) => { if (reason == 'test error') done(); },
fail : (reason) => { if (reason === 'test error') done(); },
done : () => { done(); }
};
rewireProcessRegistration(cweMock.REGISTRATION_TEST_EVENT, context, false);
Expand All @@ -186,7 +186,7 @@ describe('CWE Unit Tests', function() {

it('registration waterfall flow error - getAlAuth()', function(done) {
var context = {
fail : (reason) => { if (reason == 'test error') done(); }
fail : (reason) => { if (reason === 'test error') done(); }
};
rewireGetAlAuth = cweRewire.__set__(
{getAlAuth: function(callback) { callback('test error'); }}
Expand Down Expand Up @@ -235,7 +235,7 @@ describe('CWE Unit Tests', function() {
});

it('waterfall flow error - getDecryptedCredentials()', function(done) {
var context = {fail : (reason) => { if (reason == 'decryption_error') done(); } };
var context = {fail : (reason) => { if (reason === 'decryption_error') done(); } };
rewireGetDecryptedCredentials = cweRewire.__set__(
{getDecryptedCredentials: function(callback) { callback('decryption_error'); }}
);
Expand All @@ -244,8 +244,9 @@ describe('CWE Unit Tests', function() {

it('waterfall flow error - getAlAuth()', function(done) {
var context = {
fail : (reason) => { if (reason == 'test error') done(); }
fail : (reason) => { if (reason === 'test error') done(); }
};
rewireGetAlAuth();
rewireGetAlAuth = cweRewire.__set__(
{getAlAuth: function(callback) { callback('test error'); }}
);
Expand All @@ -254,7 +255,7 @@ describe('CWE Unit Tests', function() {

it('waterfall flow error - formatMessages()', function(done) {
var context = {
fail : (reason) => { if (reason == 'test error') done(); }
fail : (reason) => { if (reason === 'test error') done(); }
};
rewireFormatMessages = cweRewire.__set__(
{formatMessages: (event, context, callback) => { callback('test error'); }}
Expand All @@ -264,15 +265,97 @@ describe('CWE Unit Tests', function() {

it('waterfall flow error - sendToIngest()', function(done) {
var context = {
fail : (reason) => { if (reason == 'test error') done(); }
fail : (reason) => { if (reason === 'test error') done(); }
};
rewireSendToIngest();
rewireSendToIngest = cweRewire.__set__(
{sendToIngest: (event, context, aimsC, message, callback) => { callback('test error'); }}
);
rewireProcessKinesisRecords(cweMock.GD_ONLY_KINESIS_TEST_EVENT, context);
});
});

describe('sendToIngest()', function() {
var rewireGetDecryptedCredentials;
var rewireGetAlAuth;
var rewireSendToIngest;
var aimscStub;
var responseStub;
var ingestStub;

before(function() {
aimscStub = sinon.stub(m_aimsc.prototype, 'authenticate').
callsFake(
function fake(aa,bb) {
return new Promise(function(resolve, reject) {
resolve();
});
}
);
responseStub = sinon.stub(m_response, 'send').callsFake(
function fakeFn() {
});
});

beforeEach(function() {
rewireSendToIngest = cweRewire.__get__('sendToIngest');
rewireGetDecryptedCredentials = cweRewire.__set__(
{getDecryptedCredentials: (callback) => { callback(null); }}
);
rewireGetAlAuth = cweRewire.__set__(
{getAlAuth: (callback) => { callback(null, {}); }}
);
});

afterEach(function() {
rewireGetDecryptedCredentials();
rewireGetAlAuth();
ingestStub.restore();
});

after(function() {
responseStub.restore();
aimscStub.restore();
});

it('waterfall send to ingest OK', function(done) {
ingestStub = sinon.stub(m_servicec.AlServiceC.prototype, 'post').callsFake(
function fakeFn(path, extraOptions) {
assert.equal(cweMock.INGEST_TEST_URL, path);
return new Promise(function(resolve, reject) {
resolve();
});
});

var context = {};
var collectedBatch = JSON.stringify(cweMock.TEST_COLLECTED_BATCH);
rewireSendToIngest(cweMock.GD_ONLY_KINESIS_TEST_EVENT, context,
aimscStub, collectedBatch,
function(arg) {
done();
});
});

it('waterfall send to ingest Error', function(done) {
ingestStub = sinon.stub(m_servicec.AlServiceC.prototype, 'post').callsFake(
function fakeFn(path, extraOptions) {
assert.equal(cweMock.INGEST_TEST_URL, path);
return new Promise(function(resolve, reject) {
throw 'exception';
});
});

var context = {};
var collectedBatch = JSON.stringify(cweMock.TEST_COLLECTED_BATCH);
rewireSendToIngest(cweMock.GD_ONLY_KINESIS_TEST_EVENT, context,
aimscStub, collectedBatch,
function(exception) {
assert.equal('Unable to send to Ingest exception', exception);
done();
});
});

});

describe('getAlAuth()', function() {
var rewireGetAlAuth;
Expand Down Expand Up @@ -314,20 +397,20 @@ describe('CWE Unit Tests', function() {
}
);

rewireGetAlAuth(function(err) { if (err == 'reject') done(); });
rewireGetAlAuth(function(err) { if (err === 'reject') done(); });
});

it('aims throws exception', function(done) {
stub = sinon.stub(m_aimsc.prototype, 'authenticate').
callsFake(
function fake(aa,bb) {
return new Promise(function(resolve, reject) {
throw('exception');
throw 'exception';
});
}
);

rewireGetAlAuth(function(err) { if (err == 'exception') done(); });
rewireGetAlAuth(function(err) { if (err === 'exception') done(); });
});
});

Expand Down

0 comments on commit f6947d1

Please sign in to comment.