Skip to content

Commit

Permalink
Fix fetchRevisions-hooks
Browse files Browse the repository at this point in the history
When bucket was empty the fetchRevisions hooks would fail.
  • Loading branch information
LevelbossMike committed Jul 31, 2017
1 parent 075da1e commit 77f319a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 51 deletions.
71 changes: 42 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,50 @@ function _list(opts) {
let listObjects = RSVP.denodeify(client.listObjects.bind(client));
let getObject = RSVP.denodeify(client.getObject.bind(client));

return RSVP.hash({
revisions: listObjects({ Bucket: bucket, Prefix: archivePrefix }),
current: getObject({ Bucket: bucket, Key: manifestKey })
})
.then((result) => {
let revisionsData = result.revisions;
let current = result.current;
let data = revisionsData.Contents;
let body = current.Body;

let manifestData = JSON.parse(body);

let revisions = data.sort(function(a, b) {
return new Date(b.LastModified) - new Date(a.LastModified);
})
.map((d) => {
let match = d.Key.match(new RegExp(archivePrefix+'([^.]*)\\.zip'));
if (!match) {
return; // ignore files that are no zipped app builds
}
let revisionsResults;

let revision = match[1];
return {
revision,
timestamp: d.LastModified,
active: d.Key === manifestData.key
return listObjects({ Bucket: bucket, Prefix: archivePrefix })
.then((results) => {
revisionsResults = results;
return getObject({ Bucket: bucket, Key: manifestKey });
})
.then((current) => {
return { revisions: revisionsResults, current };
})
.catch(() => {
return { revisions: revisionsResults, current: { Body: '{}'} };
})
.then((result) => {
if (result.revisions.length < 1) {
return { revisions: [] };
}
}).filter((d) => d); // filter out empty values

return { revisions };
});
let revisionsData = result.revisions;
let current = result.current;
let data = revisionsData.Contents;
let body = current.Body;

let manifestData = JSON.parse(body);

let revisions = data.sort(function(a, b) {
return new Date(b.LastModified) - new Date(a.LastModified);
})
.map((d) => {
let match = d.Key.match(new RegExp(archivePrefix+'([^.]*)\\.zip'));
if (!match) {
return; // ignore files that are no zipped app builds
}

let revision = match[1];
return {
revision,
timestamp: d.LastModified,
active: d.Key === manifestData.key
}
}).filter((d) => d); // filter out empty values

return { revisions };
});
}

module.exports = {
Expand Down Expand Up @@ -143,7 +156,7 @@ module.exports = {
fetchRevisions: function() {
let accessKeyId = this.readConfig('accessKeyId');
let secretAccessKey = this.readConfig('secretAccessKey');
let archivePrefix = this.readConfig('archivePrefix');
let archivePrefix = this.readConfig('archivePrefix');
let bucket = this.readConfig('bucket');
let region = this.readConfig('region');
let manifestKey = this.readConfig('manifestKey');
Expand Down
60 changes: 39 additions & 21 deletions tests/index-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env node */
/* global afterEach */
'use strict';

const fs = require('fs');
Expand All @@ -14,24 +15,25 @@ const del = RSVP.denodeify(client.deleteObjects.bind(client));
const put = RSVP.denodeify(client.putObject.bind(client));
const all = RSVP.all;

function setupTestData() {
function cleanBucket() {
return list({ Bucket: process.env.TEST_BUCKET })
.then((data) => data.Contents.map((d) => { return { Key: d.Key }; }))
.then((objects) => {
if (!objects.length) {
return;
}

return del({
Bucket: process.env.TEST_BUCKET,
Delete: {
Objects: objects
}
});
function cleanBucket() {
return list({ Bucket: process.env.TEST_BUCKET })
.then((data) => data.Contents.map((d) => { return { Key: d.Key }; }))
.then((objects) => {
if (!objects.length) {
return;
}

return del({
Bucket: process.env.TEST_BUCKET,
Delete: {
Objects: objects
}
});
}
});
}

function setupTestData() {
function addTestData() {
let existingDists = ['dist-12.zip', 'dist-34.zip', 'dist-56.zip'];
let promises = existingDists.map((n) => {
Expand Down Expand Up @@ -133,8 +135,6 @@ describe('fastboot-app-server-aws plugin', function() {
});

it('uploads whatever is in `context.fastbootArchivePath` to S3', function() {
this.timeout(5000);

let FILE_NAME = 'dist-78.zip';
let CONTENT = 'testtest';

Expand All @@ -158,32 +158,50 @@ describe('fastboot-app-server-aws plugin', function() {

describe('#fetchRevisions', function() {
it('returns a list of available revisions and the current active one', function() {
this.timeout(5000);
return plugin.fetchRevisions(context)
.then((data) => {
let revisions = data.revisions.map((d) => d.revision);
assert.deepEqual(revisions, ['12', '34', '56']);
assert.isTrue(data.revisions[1].active, 'revision 34 marked current');
});
});

it('does not fail when bucket is empty', function() {
return cleanBucket()
.then(() => {
return plugin.fetchRevisions(context);
})
.then((data) => {
let revisions = data.revisions.map((d) => d.revision);
assert.deepEqual(revisions, []);
});
});
});

describe('#fetchInitialRevisions', function() {
it('returns a list of available revisions and the current active one', function() {
this.timeout(5000);
return plugin.fetchInitialRevisions(context)
.then((data) => {
let revisions = data.initialRevisions.map((d) => d.revision);
assert.deepEqual(revisions, ['12', '34', '56']);
assert.isTrue(data.initialRevisions[1].active, 'revision 34 marked current');
});
});

it('does not fail when bucket is empty', function() {
return cleanBucket()
.then(() => {
return plugin.fetchInitialRevisions(context);
})
.then((data) => {
let revisions = data.initialRevisions.map((d) => d.revision);
assert.deepEqual(revisions, []);
});
});
});

describe('#activate', function() {
it('takes a manifest file and uploads it to S3', function() {
this.timeout(5000);

context.commandOptions = {
revision: '56'
};
Expand Down
3 changes: 2 additions & 1 deletion tests/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var glob = require('glob');
var Mocha = require('mocha');

var mocha = new Mocha({
reporter: 'spec'
reporter: 'spec',
timeout: 5000
});

var arg = process.argv[2];
Expand Down

0 comments on commit 77f319a

Please sign in to comment.