Skip to content

Commit

Permalink
health check tests
Browse files Browse the repository at this point in the history
  • Loading branch information
constantology committed Sep 26, 2017
1 parent a25a322 commit 54cdaa8
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 0 deletions.
77 changes: 77 additions & 0 deletions test/health/db-backups.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';

const path = require('path');

const chai = require('chai');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');

const AWS = require('aws-sdk');
const moment = require('moment');

const nHealthStatus = require('n-health/src/checks/status');

const {
DB
} = require('config');

const { expect } = chai;

chai.use(sinonChai);

const __proto__ = Object.getPrototypeOf(new AWS.S3({}));

const MODULE_ID = path.relative(`${process.cwd()}/test`, module.id) || require(path.resolve('./package.json')).name;

describe(MODULE_ID, function () {
let S3Stub;
let underTest;
let time;

before(function () {
time = moment().isSameOrAfter(moment().startOf('hour').add(10, 'minutes')) ? moment() : moment().subtract(1, 'hour');

S3Stub = sinon.stub(__proto__, 'listObjectsV2').callsFake((params, cb) => cb(null, {
IsTruncated: false,
Contents: [ {
Key: `production/syndication.${time.format(DB.BACKUP.date_format)}.zip`,
LastModified: time.toJSON(),
ETag: '"167e9de0f286d5d771a89b864c053ea8-1"',
Size: 95089,
StorageClass: 'STANDARD'
} ],
Name: 'next-syndication-db-backups',
Prefix: `production/syndication.${time.format(DB.BACKUP.date_format)}`,
MaxKeys: 5,
CommonPrefixes: [],
KeyCount: 1
}));

underTest = proxyquire('../../health/db-backups', {
moment: moment
});
});

after(function () {
S3Stub.restore();
});

describe('#tick', function () {
it('calls S3.listObectsV2 to try and find the last back up file', async function () {
await underTest.tick();

expect(S3Stub).to.have.been.calledWith({
Bucket: DB.BACKUP.bucket.id,
MaxKeys: 5,
Prefix: `${DB.BACKUP.bucket.directory}/${DB.BACKUP.schema}.${time.format(DB.BACKUP.date_format)}`
});
});

it('sets the status to PASSED if a backup file is found', async function () {
await underTest.tick();

expect(underTest.status).to.equal(nHealthStatus.PASSED);
});
});
});
87 changes: 87 additions & 0 deletions test/health/db-sync-state.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'use strict';

const path = require('path');

const chai = require('chai');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');

const nHealthStatus = require('n-health/src/checks/status');

const {
TEST: { FIXTURES_DIRECTORY }
} = require('config');

const { expect } = chai;

chai.use(sinonChai);

const MODULE_ID = path.relative(`${process.cwd()}/test`, module.id) || require(path.resolve('./package.json')).name;

describe(MODULE_ID, function () {
const { initDB } = require(path.resolve(`${FIXTURES_DIRECTORY}/massive`))();

let db;
let underTest;

before(function () {
db = initDB();

db.syndication.get_health_contracts.resolves([{
get_health_contracts: {
contract_asset_data: 0,
contract_assets: 0,
contract_data: 0,
contracts: 0
}
}]);

db.syndication.get_health_downloads.resolves([{
get_health_downloads: {
save_history: 0,
saved_items: 0
}
}]);

db.syndication.get_health_saved_items.resolves([{
get_health_saved_items: {
download_history: 0,
downloads: 0
}
}]);

underTest = proxyquire('../../health/db-sync-state', {
'../db/pg': sinon.stub().resolves(db)
});
});

after(function () {
});

describe('#tick', function () {
it('calls syndication.get_health_contracts to determine the integrity of contracts', async function () {
await underTest.tick();

expect(db.syndication.get_health_contracts).to.have.been.called;
});

it('calls syndication.get_health_downloads to determine the integrity of downloads', async function () {
await underTest.tick();

expect(db.syndication.get_health_downloads).to.have.been.called;
});

it('calls syndication.get_health_saved_items to determine the integrity of saved_items', async function () {
await underTest.tick();

expect(db.syndication.get_health_saved_items).to.have.been.called;
});

it('sets the status to PASSED if all counts are zero', async function () {
await underTest.tick();

expect(underTest.status).to.equal(nHealthStatus.PASSED);
});
});
});
81 changes: 81 additions & 0 deletions test/health/sqs.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';

const fs = require('fs');
const path = require('path');

const chai = require('chai');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');

const AWS = require('aws-sdk');
const moment = require('moment');

const {
HEALTH_CHECK_HISTORY,
SYNDICATION_DOWNLOAD_SQS_URL: DEFAULT_QUEUE_URL
} = require('config');

const { expect } = chai;

chai.use(sinonChai);

const __proto__ = Object.getPrototypeOf(new AWS.SQS({}));

const MODULE_ID = path.relative(`${process.cwd()}/test`, module.id) || require(path.resolve('./package.json')).name;

describe(MODULE_ID, function () {
let fsStub;
let sqsStub;
let underTest;

before(function () {
fsStub = sinon.stub(fs, 'writeFile').callsFake((x, y, z, cb) => cb(null, {}));

sqsStub = sinon.stub(__proto__, 'getQueueAttributesAsync').resolves({
Attributes: {
ApproximateNumberOfMessages: '0',
ApproximateNumberOfMessagesDelayed: '0'
}
});

underTest = proxyquire('../../health/sqs', {
fs: {
writeFile: fsStub
}
});
});

after(function () {
fsStub.restore();
sqsStub.restore();
});

describe('#tick', function () {
it('calls SQS.getQueueAttributes to get the current queue count', async function () {
await underTest.tick();

expect(sqsStub).to.have.been.calledWith({
AttributeNames: [
'ApproximateNumberOfMessages',
'ApproximateNumberOfMessagesDelayed'
],
QueueUrl: DEFAULT_QUEUE_URL
});
});

it('writes a file so it can compare queue history', async function () {
await underTest.tick();

const file_path = path.resolve(HEALTH_CHECK_HISTORY.directory, 'sqs', moment().format(HEALTH_CHECK_HISTORY.file_date_format));

const data = JSON.stringify({
ApproximateNumberOfMessages: 0,
ApproximateNumberOfMessagesDelayed: 0,
total: 0
}, null, 4);

expect(fsStub).to.have.been.calledWith(file_path, data, 'utf8');
});
});
});

0 comments on commit 54cdaa8

Please sign in to comment.