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

allow setting cumulus-message-adapter directory with env var #13

Merged
merged 4 commits into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

- Add the `CUMULUS_MESSAGE_ADAPTER_DIR` environment variable to set the path where cumulus-message-adapter is located
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be under the ### Added section.


## [v1.0.0] - 2018-03-07

Initial release
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ NodeTest:
useMessageAdapter: true
```

## Environment variables

There are two environment variables that can be used with this library. Both are rarely needed for most use cases:

- `CUMULUS_MESSAGE_ADAPTER_DISABLED=true`
- Defaults to false. This env var disables Cumulus Message Adapter. This can be used to turn off the message adapter for tasks that adapt the message on their own, or for testing.
- `CUMULUS_MESSAGE_ADAPTER_DIR`
- The default directory for Cumulus Message Adapter is the root directory of the lambda function. There are rare cases (see [cumulus-ecs-task](https://github.com/cumulus-nasa/cumulus-ecs-task) for an example) where the adapter directory will be in a different location. This env var is unlikely to be useful for tasks, only for libraries that are working with Cumulus tasks at a higher level.

## Development

### Running Tests
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class CumulusMessageAdapterExecutionError extends Error {
*/
function callCumulusMessageAdapter(command, input) {
return new Promise((resolve, reject) => {
const cumulusMessageAdapter = cp.spawn('python', ['./cumulus-message-adapter', command]);
const adapterDir = process.env.CUMULUS_MESSAGE_ADAPTER_DIR || './cumulus-message-adapter';
const cumulusMessageAdapter = cp.spawn('python', [adapterDir, command]);

// Collect STDOUT
let cumulusMessageAdapterStdout = '';
Expand Down
4 changes: 4 additions & 0 deletions test/alternate-dir/cumulus-message-adapter/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import sys

for line in sys.stdin:
sys.stdout.write(line)
53 changes: 53 additions & 0 deletions test/test-index-with-dir-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-disable require-jsdoc */
Copy link
Contributor

Choose a reason for hiding this comment

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

Add this to test/.eslintrc.json so that other test files get the same config.


const path = require('path');

const test = require('ava').serial;
const cumulusMessageAdapter = require('../index');

test.cb('CUMULUS_MESSAGE_ADAPTER_DIR sets the location of the message adapter', (t) => {
const dir = path.join(__dirname, 'alternate-dir', 'cumulus-message-adapter');
process.env.CUMULUS_MESSAGE_ADAPTER_DIR = dir;

const businessLogicOutput = 42;
const businessLogic = () => businessLogicOutput;

const inputEvent = { a: 1 };

const expectedOutput = {
event: {
event: inputEvent,
schemas: null
},
handler_response: businessLogicOutput,
message_config: null,
schemas: null
};

function callback(err, data) {
t.is(err, null);
t.deepEqual(data, expectedOutput);
t.end();
}

return cumulusMessageAdapter.runCumulusTask(businessLogic, inputEvent, {}, callback);
});

test.cb('callback returns error if CUMULUS_MESSAGE_ADAPTER_DIR is incorrect', (t) => {
const dir = path.join(__dirname, 'wrong-dir', 'cumulus-message-adapter');
process.env.CUMULUS_MESSAGE_ADAPTER_DIR = dir;

const businessLogicOutput = 42;
const businessLogic = () => businessLogicOutput;

const inputEvent = { a: 1 };

function callback(err, data) {
t.is(typeof err, 'object');
t.is(err.name, 'CumulusMessageAdapterExecutionError');
t.is(data, undefined);
t.end();
}

cumulusMessageAdapter.runCumulusTask(businessLogic, inputEvent, {}, callback);
});