Skip to content

Commit

Permalink
Merge branch 'master' into UpdateQueuePdrsTask
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Huffnagle committed Mar 5, 2018
2 parents e7a9bc8 + 60256eb commit 8f1780c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added
- Added a `jlog` function to `common/test-utils` to aid in test debugging
- Integration test package
- Integration test package with command line tool [CUMULUS-200] by @laurenfrederick

### Updated
- The `queue-pdrs` task now uses the [cumulus-message-adapter-js](https://github.com/cumulus-nasa/cumulus-message-adapter-js)
Expand Down
2 changes: 2 additions & 0 deletions packages/integration-tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const uuidv4 = require('uuid/v4');
const fs = require('fs-extra');
const { s3, sfn } = require('@cumulus/common/aws');
const lambda = require('./lambda');

const executionStatusNumRetries = 20;
const waitPeriodMs = 5000;
Expand Down Expand Up @@ -159,3 +160,4 @@ async function testWorkflow(stackName, bucketName, workflowName, inputFile) {

exports.testWorkflow = testWorkflow;
exports.executeWorkflow = executeWorkflow;
exports.getLambdaOutput = lambda.getLambdaOutputPayload;
89 changes: 89 additions & 0 deletions packages/integration-tests/lambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict';

const { sfn } = require('@cumulus/common/aws');

const lambdaScheduleEvents = [
'LambdaFunctionScheduled',
'LambdaFunctionScheduleFailed'
];

const lambdaStartedEvents = [
'LambdaFunctionStartFailed',
'LambdaFunctionStarted'
];

const lambdaCompletedEvents = [
'LambdaFunctionFailed',
'LambdaFunctionSucceeded',
'LambdaFunctionTimedOut'
];

/**
* Get the events for the lambda execution for the given workflow execution.
* This function currently assumes one execution of the given lambda per workflow.
*
* @param {string} workflowExecutionArn - Arn of the workflow execution
* @param {string} lambdaName - name of the lambda
* @returns {Object} an object containing a schedule event, start event, and complete
* event if exist, null if cannot find the lambda
*/
async function getLambdaExecution(workflowExecutionArn, lambdaName) {
const executionHistory = (
await sfn().getExecutionHistory({ executionArn: workflowExecutionArn }).promise()
);

// Get the event where the lambda was scheduled
const scheduleEvent = executionHistory.events.find((event) => (
lambdaScheduleEvents.includes(event.type)) &&
(event.lambdaFunctionScheduledEventDetails.resource.includes(lambdaName))
);

if (scheduleEvent === null) {
console.log(`Could not find lambda ${lambdaName} in execution.`);
return null;
}

let startEvent = null;
let completeEvent = null;

if (scheduleEvent.type !== 'LambdaFunctionScheduleFailed') {
startEvent = executionHistory.events.find((event) =>
(lambdaStartedEvents.includes(event.type)) &&
(event.previousEventId === scheduleEvent.id));

if (startEvent !== null && startEvent.type !== 'LambdaFunctionStartFailed') {
completeEvent = executionHistory.events.find((event) =>
(lambdaCompletedEvents.includes(event.type)) &&
(event.previousEventId === startEvent.id));
}
}

return { scheduleEvent, startEvent, completeEvent };
}

/**
* Get the output payload from the lambda, if the lambda succeeds
*
* @param {string} workflowExecutionArn - Arn of the workflow execution
* @param {string} lambdaName - name of the lambda
* @returns {Object} object containing the payload, null if error
*/
async function getLambdaOutputPayload(workflowExecutionArn, lambdaName) {
const lambdaExecution = await getLambdaExecution(workflowExecutionArn, lambdaName);

if (lambdaExecution === null) {
console.log(`Could not find lambda ${lambdaName} in execution.`);
return null;
}

if (lambdaExecution.completeEvent === null ||
lambdaExecution.completeEvent.type !== 'LambdaFunctionSucceeded') {
console.log(`Lambda ${lambdaName} was not successful.`);
return null;
}

const succeededDetails = JSON.parse(lambdaExecution.completeEvent.lambdaFunctionSucceededEventDetails.output.toString());
return succeededDetails.payload;
}

exports.getLambdaOutputPayload = getLambdaOutputPayload;

0 comments on commit 8f1780c

Please sign in to comment.