From 77f319afc73b0644d709d2d3e94396ab98bacbe6 Mon Sep 17 00:00:00 2001 From: Michael Klein Date: Mon, 31 Jul 2017 10:39:57 +0200 Subject: [PATCH] Fix fetchRevisions-hooks When bucket was empty the fetchRevisions hooks would fail. --- index.js | 71 +++++++++++++++++++++++++++------------------ tests/index-test.js | 60 ++++++++++++++++++++++++-------------- tests/runner.js | 3 +- 3 files changed, 83 insertions(+), 51 deletions(-) diff --git a/index.js b/index.js index 61e05f4..137f0d1 100644 --- a/index.js +++ b/index.js @@ -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 = { @@ -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'); diff --git a/tests/index-test.js b/tests/index-test.js index 31ff93c..35f6ef0 100644 --- a/tests/index-test.js +++ b/tests/index-test.js @@ -1,4 +1,5 @@ /* eslint-env node */ +/* global afterEach */ 'use strict'; const fs = require('fs'); @@ -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) => { @@ -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'; @@ -158,7 +158,6 @@ 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); @@ -166,11 +165,21 @@ describe('fastboot-app-server-aws plugin', function() { 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); @@ -178,12 +187,21 @@ describe('fastboot-app-server-aws plugin', function() { 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' }; diff --git a/tests/runner.js b/tests/runner.js index 848237a..42e1756 100644 --- a/tests/runner.js +++ b/tests/runner.js @@ -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];