when looking for latest fastly VCL to clone, use only active VCLs

This commit is contained in:
Ben Wheeler 2019-09-19 12:36:25 -04:00
parent 2e7a687547
commit de5e712305
3 changed files with 55 additions and 4 deletions

View file

@ -24,7 +24,7 @@ var routes = routeJson.map(function (route) {
async.auto({ async.auto({
version: function (cb) { version: function (cb) {
fastly.getLatestVersion(function (err, response) { fastly.getLatestActiveVersion(function (err, response) {
if (err) return cb(err); if (err) return cb(err);
// Validate latest version before continuing // Validate latest version before continuing
if (response.active || response.locked) { if (response.active || response.locked) {

View file

@ -24,11 +24,11 @@ module.exports = function (apiKey, serviceId) {
}; };
/* /*
* getLatestVersion: Get the most recent version for the configured service * getLatestActiveVersion: Get the most recent version for the configured service
* *
* @param {callback} Callback with signature *err, latestVersion) * @param {callback} Callback with signature *err, latestVersion)
*/ */
fastly.getLatestVersion = function (cb) { fastly.getLatestActiveVersion = function (cb) {
if (!this.serviceId) { if (!this.serviceId) {
return cb('Failed to get latest version. No serviceId configured'); return cb('Failed to get latest version. No serviceId configured');
} }
@ -39,7 +39,14 @@ module.exports = function (apiKey, serviceId) {
} }
var latestVersion = versions.reduce(function (lateVersion, version) { var latestVersion = versions.reduce(function (lateVersion, version) {
if (!lateVersion) return version; if (!lateVersion) return version;
if (version.number > lateVersion.number) return version; // if versions we're comparing are both active, or both inactive, prefer
// whichever has a higher version number
if (lateVersion.active === version.active) {
if (version.number > lateVersion.number) return version;
return lateVersion;
}
// if only one of the versions is active, prefer that one
if (version.active === true) return version;
return lateVersion; return lateVersion;
}); });
return cb(null, latestVersion); return cb(null, latestVersion);

View file

@ -0,0 +1,44 @@
describe('fastly library', () => {
test('getLatestActiveVersion returns largest active VCL number', done => {
const mockedFastlyRequest = jest.fn((method, url, cb) => {
cb(null, [
{
number: 4,
active: false
},
{
number: 1,
active: true
},
{
number: 2,
active: true
},
{
number: 3,
active: false
}
]);
});
jest.mock('fastly', () => (() => ({
request: mockedFastlyRequest
})));
const fastlyExtended = require('../../../bin/lib/fastly-extended'); // eslint-disable-line global-require
const fastlyInstance = fastlyExtended('api_key', 'service_id');
fastlyInstance.getLatestActiveVersion((err, response) => {
expect(err).toBe(null);
expect(response).toEqual({
number: 2,
active: true
});
expect(mockedFastlyRequest).toHaveBeenCalledWith(
'GET', '/service/service_id/version', expect.any(Function)
);
done();
});
});
});