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 all 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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [v1.0.1] - 2018-03-08
### Added

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

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

Initial release

[Unreleased]: https://github.com/cumulus-nasa/cumulus-cumulus-message-adapter-js/compare/v1.0.0...HEAD
[Unreleased]: https://github.com/cumulus-nasa/cumulus-cumulus-message-adapter-js/compare/v1.0.1...HEAD
[v1.0.1]: https://github.com/cumulus-nasa/cumulus-cumulus-message-adapter-js/compare/v1.0.0...v1.0.1
[v1.0.0]: https://github.com/cumulus-nasa/cumulus-message-adapter-js/tree/v1.0.0
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cumulus/cumulus-message-adapter-js",
"version": "1.0.0",
"version": "1.0.1",
"description": "Cumulus message adapter",
"main": "index.js",
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"require-jsdoc": "off"
}
}
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)
51 changes: 51 additions & 0 deletions test/test-index-with-dir-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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);
});
2 changes: 0 additions & 2 deletions test/test-index-with-env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable require-jsdoc */

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

Expand Down
2 changes: 0 additions & 2 deletions test/test-index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable require-jsdoc */

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

Expand Down