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

Add schema validation support for tests #215

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
4 changes: 3 additions & 1 deletion cumulus/tasks/discover-granules/schemas/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"title": "DiscoverGranulesConfig",
"description": "Describes the config used by the discover-granules task",
"type": "object",
"required": [ "provider" ],
"properties": {
"useQueue": {
"type": "boolean"
},
"provider": {
"type": "object",
"required": [ "host", "protocol" ],
"properties": {
"id": { "type": "string" },
"username": { "type": "string" },
Expand Down Expand Up @@ -56,4 +58,4 @@
}
}
}
}
}
13 changes: 12 additions & 1 deletion cumulus/tasks/discover-granules/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ const test = require('ava');
const mur = require('./fixtures/mur.json');
const { cloneDeep } = require('lodash');
const { recursivelyDeleteS3Bucket, s3, sqs } = require('@cumulus/common/aws');
const { createQueue, randomString } = require('@cumulus/common/test-utils');
const {
createQueue,
randomString,
validateConfig,
validateOutput
} = require('@cumulus/common/test-utils');
const { discoverGranules } = require('../index');

async function uploadMessageTemplate(Bucket) {
Expand Down Expand Up @@ -32,8 +37,11 @@ test('test discovering mur granules', async (t) => {
const event = cloneDeep(mur);
event.config.useQueue = false;

await validateConfig(t, event.config);

try {
const output = await discoverGranules(event);
await validateOutput(t, output);
t.is(output.granules.length, 3);
t.is(output.granules[0].files.length, 2);
}
Expand All @@ -60,8 +68,11 @@ test('test discovering mur granules over FTP with queue', async (t) => {
event.config.templateUri = await uploadMessageTemplate(messageTemplateBucket);
event.config.useQueue = true;

await validateConfig(t, event.config);

try {
const output = await discoverGranules(event);
await validateOutput(t, output);
t.is(output.granules.length, 3);
}
catch (e) {
Expand Down
1 change: 1 addition & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"webpack-node-externals": "^1.5.4"
},
"devDependencies": {
"ajv": "^5.5.2",
"ava": "^0.25.0"
}
}
82 changes: 82 additions & 0 deletions packages/common/test-utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

const Ajv = require('ajv');
const crypto = require('crypto');
const url = require('url');
const aws = require('./aws');
const { readFile } = require('fs');

/**
* Generate a 40-character random string
Expand Down Expand Up @@ -107,3 +109,83 @@ async function createQueue() {
return url.format(returnedQueueUrl);
}
exports.createQueue = createQueue;

/**
* Read a file and return a promise with the data
*
* Takes the same parameters as fs.readFile:
*
* https://nodejs.org/docs/v6.10.3/api/fs.html#fs_fs_readfile_file_options_callback
*
* @param {string|Buffer|integer} file - filename or file descriptor
* @param {any} options - encoding and flag options
* @returns {Promise} - the contents of the file
*/
function promisedReadFile(file, options) {
return new Promise((resolve, reject) => {
readFile(file, options, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
}

/**
* Validate an object using json-schema
*
* Issues a test failure if there were validation errors
*
* @param {Object} t - an ava test
* @param {string} schemaFilename - the filename of the schema
* @param {Object} data - the object to be validated
* @returns {boolean} - whether the object is valid or not
*/
async function validateJSON(t, schemaFilename, data) {
const schema = await promisedReadFile(schemaFilename, 'utf8').then(JSON.parse);
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if the schema file does not exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The assumption is that you would only add this check to your test if the schema file exists.

Copy link
Contributor

Choose a reason for hiding this comment

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

It might be nice to just comment the expected behavior or spit out an error in that case. I don't love that the schema locations are assumed, but think it's fine for now and we can make updates if we run into a different scenario.

const ajv = new Ajv();
const valid = (new Ajv()).validate(schema, data);
if (!valid) t.fail(`input validation failed: ${ajv.errorsText()}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

input validation failed: ${ajv.errorsText()} could be ${schemaFilename} validation failed: ${ajv.errorsText()}

return valid;
}

/**
* Validate a task input object using json-schema
*
* Issues a test failure if there were validation errors
*
* @param {Object} t - an ava test
* @param {Object} data - the object to be validated
* @returns {boolean} - whether the object is valid or not
*/
async function validateInput(t, data) {
return validateJSON(t, './schemas/input.json', data);
}
exports.validateInput = validateInput;

/**
* Validate a task config object using json-schema
*
* Issues a test failure if there were validation errors
*
* @param {Object} t - an ava test
* @param {Object} data - the object to be validated
* @returns {boolean} - whether the object is valid or not
*/
async function validateConfig(t, data) {
return validateJSON(t, './schemas/config.json', data);
}
exports.validateConfig = validateConfig;

/**
* Validate a task output object using json-schema
*
* Issues a test failure if there were validation errors
*
* @param {Object} t - an ava test
* @param {Object} data - the object to be validated
* @returns {boolean} - whether the object is valid or not
*/
async function validateOutput(t, data) {
return validateJSON(t, './schemas/output.json', data);
}
exports.validateOutput = validateOutput;