Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cumulus 200 #223

Merged
merged 8 commits into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions packages/integration-tests/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-console": "off"
}
}
47 changes: 47 additions & 0 deletions packages/integration-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# @cumulus/integration-tests

[![CircleCI](https://circleci.com/gh/cumulus-nasa/cumulus.svg?style=svg)](https://circleci.com/gh/cumulus-nasa/cumulus)

@cumulus/integration-tests provides a CLI and functions for testing Cumulus workflow executions in a Cumulus deployment.

## What is Cumulus?

Cumulus is a cloud-based data ingest, archive, distribution and management prototype for NASA's future Earth science data streams.

[Cumulus Documentation](https://cumulus-nasa.github.io/)

## Installation

```
npm install @cumulus/integration-tests
```

## Usage

```
Usage: cumulus-test TYPE COMMAND [options]


Options:

-V, --version output the version number
-s, --stack-name <stackName> AWS Cloud Formation stack name (default: null)
-b, --bucket-name <bucketName> AWS S3 internal bucket name (default: null)
-w, --workflow <workflow> Workflow name (default: null)
-i, --input-file <inputFile> Workflow input JSON file (default: null)
-h, --help output usage information


Commands:

workflow Execute a workflow and determine if the workflow completes successfully
```
i.e. to test the HelloWorld workflow:

`cumulus-test workflow --stack-name helloworld-cumulus --bucket-name cumulus-bucket-internal --workflow HelloWorldWorkflow --input-file ./helloWorldInput.json`



## Contributing

See [Cumulus README](https://github.com/cumulus-nasa/cumulus/blob/master/README.md#installing-and-deploying)
27 changes: 27 additions & 0 deletions packages/integration-tests/bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node

'use strict';

const pckg = require('../package.json');
const testRunner = require('../index');
const program = require('commander');

program.version(pckg.version);

program
.usage('TYPE COMMAND [options]')
.option('-s, --stack-name <stackName>', 'AWS Cloud Formation stack name', null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this command will fail if any of these options are not set, that should be detected and handled. Right now there is nothing checking to see if these options (all of which are necessary) are set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

.option('-b, --bucket-name <bucketName>', 'AWS S3 internal bucket name', null)
.option('-w, --workflow <workflow>', 'Workflow name', null)
.option('-i, --input-file <inputFile>', 'Workflow input JSON file', null);

program
.command('workflow')
.description('Execute a workflow and determine if the workflow completes successfully')
.action(() => {
testRunner.testWorkflow(program.stackName, program.bucketName,
program.workflow, program.inputFile);
});

program
.parse(process.argv);
172 changes: 172 additions & 0 deletions packages/integration-tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
'use strict';

const uuidv4 = require('uuid/v4');
const fs = require('fs-extra');
const { s3, sfn } = require('@cumulus/common/aws');

const executionStatusNumRetries = 20;
const waitPeriodMs = 5000;

/**
* Wait for the defined number of milliseconds
*
* @param {integer} waitPeriod - number of milliseconds to wait
* @returns {Promise.<undefined>} - promise resolves after a given time period
*/
function timeout(waitPeriod) {
return new Promise((resolve) => setTimeout(resolve, waitPeriod));
}

/**
* Get the list of workflows for the stack
*
* @param {string} stackName - Cloud formation stack name
* @param {string} bucketName - S3 internal bucket name
* @returns {Object} list as a JSON object
*/
async function getWorkflowList(stackName, bucketName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is never used and should be removed.

const key = `${stackName}/workflows/list.json`;

return s3().getObject({ Bucket: bucketName, Key: key }).promise()
.then((workflowJson) => JSON.parse(workflowJson.Body.toString()));
}

/**
* Get the template JSON from S3 for the workflow
*
* @param {string} stackName - Cloud formation stack name
* @param {string} bucketName - S3 internal bucket name
* @param {string} workflowName - workflow name
* @returns {Object} template as a JSON object
*/
async function getWorkflowTemplate(stackName, bucketName, workflowName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an async function, await is never used.

const key = `${stackName}/workflows/${workflowName}.json`;
return s3().getObject({ Bucket: bucketName, Key: key }).promise()
.then((templateJson) => JSON.parse(templateJson.Body.toString()));
}

/**
* Get the workflow ARN for the given workflow from the
* template stored on S3
*
* @param {string} stackName - Cloud formation stack name
* @param {string} bucketName - S3 internal bucket name
* @param {string} workflowName - workflow name
* @returns {string} - workflow arn
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns a promise. @returns {Promise.<string>} - workflow arn

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this change.

*/
async function getWorkflowArn(stackName, bucketName, workflowName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an async function. await is never used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

return getWorkflowTemplate(stackName, bucketName, workflowName)
.then((template) => template.cumulus_meta.state_machine);
}

/**
* Get the execution status (i.e. running, completed, etc)
* for the given execution
*
* @param {string} executionArn - ARN of the execution
* @returns {string} status
*/
async function getExecutionStatus(executionArn) {
return sfn().describeExecution({ executionArn }).promise()
.then((status) => status.status);
}

/**
* Wait for a given execution to complete, then return the status
*
* @param {string} executionArn - ARN of the execution
* @returns {string} status
*/
async function waitForCompletedExecution(executionArn) {
let executionStatus = await getExecutionStatus(executionArn);
let statusCheckCount = 0;

// While execution is running, check status on a time interval
while (executionStatus === 'RUNNING' && statusCheckCount < executionStatusNumRetries) {
await timeout(waitPeriodMs);
executionStatus = await getExecutionStatus(executionArn);
statusCheckCount++;
}

if (executionStatus === 'RUNNING' && statusCheckCount === executionStatusNumRetries) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for safety, statusCheckCount >= executionStatusNumRetries

console.log(`Execution status check timed out, exceeded ${executionStatusNumRetries} status checks.`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add // eslint-disable-line max-len at the end of this line so it doesn't complain that the line is too long. In the case of long strings, exceeding the max line length isn't a problem.

http://airbnb.io/javascript/#strings--line-length

}

return executionStatus;
}

/**
* Kick off a workflow execution
*
* @param {string} workflowArn - ARN for the workflow
* @param {string} inputFile - path to input JSON
* @returns {Promise.<Object>} execution details: {executionArn, startDate}
*/
async function startWorkflowExecution(workflowArn, inputFile) {
const rawInput = await fs.readFile(inputFile, 'utf8');

const parsedInput = JSON.parse(rawInput);

// Give this execution a unique name
parsedInput.cumulus_meta.execution_name = uuidv4();
parsedInput.cumulus_meta.workflow_start_time = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this being set to null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk because I am a dum dum.


const workflowParams = {
stateMachineArn: workflowArn,
input: JSON.stringify(parsedInput),
name: parsedInput.cumulus_meta.execution_name
};

return sfn().startExecution(workflowParams).promise();
}

/**
* Execute the given workflow.
* Wait for workflow to complete to get the status
* Return the execution arn and the workflow status.
*
* @param {string} stackName - Cloud formation stack name
* @param {string} bucketName - S3 internal bucket name
* @param {string} workflowName - workflow name
* @param {string} inputFile - path to input JSON file
* @returns {Object} - {executionArn: <arn>, status: <status>}
*/
async function executeWorkflow(stackName, bucketName, workflowName, inputFile) {
const workflowArn = await getWorkflowArn(stackName, bucketName, workflowName);
const execution = await startWorkflowExecution(workflowArn, inputFile);
const executionArn = execution.executionArn;

console.log(`Executing workflow: ${workflowName}. Execution ARN ${executionArn}`);

// Wait for the execution to complete to get the status
const status = await waitForCompletedExecution(executionArn);

return { status, executionArn };
}

/**
* Test the given workflow and report whether the workflow failed or succeeded
*
* @param {string} stackName - Cloud formation stack name
* @param {string} bucketName - S3 internal bucket name
* @param {string} workflowName - workflow name
* @param {string} inputFile - path to input JSON file
* @returns {*} undefined
*/
async function testWorkflow(stackName, bucketName, workflowName, inputFile) {
try {
const workflowStatus = await executeWorkflow(stackName, bucketName, workflowName, inputFile);

if (workflowStatus.status === 'SUCCEEDED') {
console.log(`Workflow ${workflowName} execution succeeded.`);
}
else {
console.log(`Workflow ${workflowName} execution failed with state: ${workflowStatus.status}`);
}
}
catch (err) {
console.log(`Error executing workflow ${workflowName}. Error: ${err}`);
}
}

exports.testWorkflow = testWorkflow;
41 changes: 41 additions & 0 deletions packages/integration-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@cumulus/integration-tests",
"version": "1.0.0",
"description": "Integration tests",
"bin": {
"cumulus-test": "./bin/cli.js"
},
"repository": {
"type": "git",
"url": "https://github.com/cumulus-nasa/cumulus"
},
"scripts": {
"build": "webpack --progress",
"watch": "webpack --progress -w"
},
"publishConfig": {
"access": "public"
},
"babel": {
"presets": [
"es2017"
],
"plugins": [
"transform-async-to-generator"
]
},
"author": "Cumulus Authors",
"license": "Apache-2.0",
"dependencies": {
"@cumulus/common": "^1.0.0-beta.19",
"babel-core": "^6.25.0",
"babel-loader": "^6.2.4",
"babel-plugin-transform-async-to-generator": "^6.24.1",
"babel-polyfill": "^6.23.0",
"babel-preset-es2017": "^6.24.1",
"commander": "^2.9.0",
"fs-extra": "^5.0.0",
"uuid": "^3.2.1",
"webpack": "^1.12.13"
}
}
22 changes: 22 additions & 0 deletions packages/integration-tests/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
entry: ['babel-polyfill', './index.js'],
output: {
libraryTarget: 'commonjs2',
filename: 'dist/index.js'
},
externals: [
'electron'
],
target: 'node',
devtool: 'sourcemap',
module: {
loaders: [{
test: /\.js?$/,
exclude: /node_modules(?!\/@cumulus\/)/,
loader: 'babel'
}, {
test: /\.json$/,
loader: 'json'
}]
}
};