Skip to content

Commit

Permalink
Unit tests for app/routes/v2/cluster.js (#40)
Browse files Browse the repository at this point in the history
* initial unit test setup

* add test

* add test 'should return 205 if cluster does not exist and is dirty'

* Add test - updateClusterResources should return 400 if missing resource body

* add route security check getClusters to all /clusters routes

* add test updateClusterResources should return 200

* exclude coverage/* from coverage report

* add clusters->updateClusterResources->MODIFIED should return 200 test

* added ->updateClusterResources->MODIFIED different record should return 200 test

* Add updateClusterResources->should return 500 if missing resource malformed test

* add clusters->updateClusterResources->DELETED should return 200 test

* added updateClusterResources->updateClusterResources->should return 500 if unsupported event test

* added clusters->addUpdateCluster->POLLED should return 200 test

* add clusters->updateClusterResources->SYNC should return 200 test

* add clusters->addClusterMessages error tests

* add addClusterMessages->should return 200 test

* lint fix

* added clusters->addUpdateCluster->should return 500

* formatting

* Fix error handling tests
  • Loading branch information
geoffcorey authored Jun 4, 2019
1 parent d73b187 commit 620e0f4
Show file tree
Hide file tree
Showing 3 changed files with 558 additions and 49 deletions.
71 changes: 37 additions & 34 deletions app/routes/v2/clusters.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,47 @@ const objectHash = require('object-hash');
const _ = require('lodash');

const getBunyanConfig = require('../../utils/bunyan.js').getBunyanConfig;
const getCluster = require ('../../utils/cluster.js').getCluster;
const buildSearchableDataForResource = require ('../../utils/cluster.js').buildSearchableDataForResource;
const buildPushObj = require ('../../utils/cluster.js').buildPushObj;
const getCluster = require('../../utils/cluster.js').getCluster;
const buildSearchableDataForResource = require('../../utils/cluster.js').buildSearchableDataForResource;
const buildPushObj = require('../../utils/cluster.js').buildPushObj;

const addUpdateCluster = async(req,res,next) => {
const addUpdateCluster = async (req, res, next) => {
try {
const Clusters = req.db.collection('clusters');
const Stats = req.db.collection('resourceStats');
const cluster = await Clusters.findOne( { org_id: req.org._id, cluster_id: req.params.cluster_id } );
const cluster = await Clusters.findOne({ org_id: req.org._id, cluster_id: req.params.cluster_id });
const metadata = req.body;
if ( !cluster ) {
await Clusters.insertOne( { org_id: req.org._id, cluster_id: req.params.cluster_id, metadata, created: new Date(), updated: new Date() } );
Stats.updateOne( { org_id: req.org._id }, { $inc: { clusterCount: 1 }}, { upsert: true });
if (!cluster) {
await Clusters.insertOne({ org_id: req.org._id, cluster_id: req.params.cluster_id, metadata, created: new Date(), updated: new Date() });
Stats.updateOne({ org_id: req.org._id }, { $inc: { clusterCount: 1 } }, { upsert: true });
res.status(200).send('Welcome to Razee');
}
else {
if ( cluster.dirty ) {
await Clusters.updateOne( { org_id: req.org._id, cluster_id: req.params.cluster_id }, { $set: { metadata, updated: new Date(), dirty: false } } );
if (cluster.dirty) {
await Clusters.updateOne({ org_id: req.org._id, cluster_id: req.params.cluster_id }, { $set: { metadata, updated: new Date(), dirty: false } });
res.status(205).send('Please resync');
}
else {
await Clusters.updateOne( { org_id: req.org._id, cluster_id: req.params.cluster_id }, { $set: { metadata, updated: new Date() } } );
await Clusters.updateOne({ org_id: req.org._id, cluster_id: req.params.cluster_id }, { $set: { metadata, updated: new Date() } });
res.status(200).send('Thanks for the update');
}
}
} catch (error) {
next(error);
} catch (err) {
req.log.error(err.message);
next(err);
}
};

const updateClusterResources = async(req, res, next) => {
const updateClusterResources = async (req, res, next) => {
try {
const body = req.body;
if ( !body ) {
res.status(400).send( 'Missing resource body' );
if (!body) {
res.status(400).send('Missing resource body');
return;
}

let resources = body;
if ( !Array.isArray(resources) ) {
if (!Array.isArray(resources)) {
resources = [body];
}

Expand All @@ -82,20 +83,19 @@ const updateClusterResources = async(req, res, next) => {
case 'POLLED':
case 'MODIFIED':
case 'ADDED': {
const resourceHash = objectHash( resource.object );
const resourceHash = objectHash(resource.object);
const dataStr = JSON.stringify(resource.object);
const selfLink = resource.object.metadata.selfLink;
const key = {
org_id: req.org._id,
cluster_id: req.params.cluster_id,
selfLink: selfLink
};
const currentResource = await Resources.findOne( key );
const currentResource = await Resources.findOne(key);
const searchableDataObj = buildSearchableDataForResource(resource.object);

const pushCmd = buildPushObj(searchableDataObj, _.get(currentResource, 'searchableData', null));

if ( currentResource ) {
if (currentResource) {
if (resourceHash === currentResource.hash) {
await Resources.updateOne(
key,
Expand Down Expand Up @@ -126,7 +126,7 @@ const updateClusterResources = async(req, res, next) => {
},
{ upsert: true }
);
Stats.updateOne( { org_id: req.org._id }, { $inc: { deploymentCount: 1 }}, { upsert: true });
Stats.updateOne({ org_id: req.org._id }, { $inc: { deploymentCount: 1 } }, { upsert: true });
}
break;
}
Expand All @@ -139,10 +139,10 @@ const updateClusterResources = async(req, res, next) => {
selfLink: selfLink
};
const searchableDataObj = buildSearchableDataForResource(resource.object);
const currentResource = await Resources.findOne( key );
const currentResource = await Resources.findOne(key);
const pushCmd = buildPushObj(searchableDataObj, _.get(currentResource, 'searchableData', null));

if ( currentResource ) {
if (currentResource) {
await Resources.updateOne(
key, {
$set: { deleted: true, data: dataStr, searchableData: searchableDataObj },
Expand All @@ -154,20 +154,22 @@ const updateClusterResources = async(req, res, next) => {
break;
}
default: {
req.log.error( `Unsupported event ${resource.type}` );
throw new Error(`Unsupported event ${resource.type}`);
}
}
}
res.status(200).send('Thanks');
} catch (err) {
req.log.error(err.message);
next(err);
}
};

const addClusterMessages = async(req, res, next) => {
const addClusterMessages = async (req, res, next) => {
const body = req.body;
if ( !body ) {
res.status(400).send( 'Missing resource body' );
if (!body) {
res.status(400).send('Missing resource body');
return;
}

const clusterId = req.params.cluster_id;
Expand Down Expand Up @@ -201,9 +203,10 @@ const addClusterMessages = async(req, res, next) => {
const Messages = req.db.collection('messages');
await Messages.updateOne(key, { $set: data, $setOnInsert: insertData }, { upsert: true });
req.log.debug({ messagedata: data }, `${messageType} message data posted`);
res.status(200).send( `${messageType} message received` );
} catch (exception) {
next(exception);
res.status(200).send(`${messageType} message received`);
} catch (err) {
req.log.error(err.message);
next(err);
}
};

Expand All @@ -212,12 +215,12 @@ router.use(ebl(getBunyanConfig('razeedash-api/clusters')));
router.use(getCluster); // adds req.cluster object and validates org_id/cluster_id ownership

// /api/v2/clusters/:cluster_id
router.post('/:cluster_id', asyncHandler( addUpdateCluster));
router.post('/:cluster_id', asyncHandler(addUpdateCluster));

// /api/v2/clusters/:cluster_id/resources
router.post('/:cluster_id/resources', asyncHandler( updateClusterResources));
router.post('/:cluster_id/resources', asyncHandler(updateClusterResources));

// /api/v2/clusters/:cluster_id/messages
router.post('/:cluster_id/messages', asyncHandler( addClusterMessages ));
router.post('/:cluster_id/messages', asyncHandler(addClusterMessages));

module.exports = router;
Loading

0 comments on commit 620e0f4

Please sign in to comment.