Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
annemarie35 authored and mickaelalibert committed Sep 4, 2023
1 parent 4f9ea92 commit ddd500f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
4 changes: 2 additions & 2 deletions run/services/schedule-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ const tasks = [
enabled: config.autoScaleEnabled,
schedule: config.scheduleAutoScaleUp,
task: async () => {
await taskAutoScaleWeb.run();
await taskAutoScaleWeb.run({ applicationName, region, autoScalingParameters });
},
},
{
name: 'eveningAutoScale',
enabled: config.autoScaleEnabled,
schedule: config.scheduleAutoScaleDown,
task: async () => {
await taskAutoScaleWeb.run();
await taskAutoScaleWeb.run({ applicationName, region, autoScalingParameters });
},
},
];
Expand Down
7 changes: 5 additions & 2 deletions run/services/task-autoscale-web.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const logger = require('../../common/services/logger');
const ScalingoClient = require('../../common/services/scalingo-client');

async function task() {
async function task({ applicationName, region, autoScalingParameters }, injectedScalingoClient = ScalingoClient) {
try {
const client = await injectedScalingoClient.getInstance(region);
await client.updateAutoscaler(applicationName, autoScalingParameters);
logger.info('running');
} catch (error) {
logger.info({});
throw new Error(`Scalingo APIError: ${error.message}`);
}
}

Expand Down
45 changes: 41 additions & 4 deletions test/integration/run/services/task-autoscale-web_test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,52 @@
const taskAutoscaleWeb = require('../../../../run/services/task-autoscale-web');
const { expect, sinon } = require('../../../test-helper');
const { expect, sinon, catchErr } = require('../../../test-helper');

describe('#task-autoscale-web', function () {
it('should call autoscale on web container for specified application', async function () {
// given
const applicationName = 'pix-api-test';
const region = 'recette';
const autoScalingParameters = { min: 2, max: 4 };

const scalingoClientStub = sinon.stub();
const autoScalerStub = sinon.stub();
const updateAutoscalerStub = sinon.stub();

const getInstanceStub = sinon.stub().resolves({
updateAutoscaler: updateAutoscalerStub,
});
scalingoClientStub.getInstance = getInstanceStub;

// when
await taskAutoscaleWeb.run({ applicationName, region, autoScalingParameters }, scalingoClientStub);

// then
expect(getInstanceStub.calledOnceWithExactly(region)).to.be.true;
expect(updateAutoscalerStub.calledOnceWithExactly(applicationName, autoScalingParameters)).to.be.true;
});
it('should throw an error on scalingo errors', async function () {
// given
const applicationName = 'pix-api-test';
const region = 'recette';
const autoScalingParameters = { min: 2, max: 4 };

const scalingoClientStub = sinon.stub();
const updateAutoscalerStub = sinon.stub().rejects(new Error('Cannot configure autoscaler'));

const getInstanceStub = sinon.stub().resolves({
updateAutoscaler: updateAutoscalerStub,
});

scalingoClientStub.getInstance = getInstanceStub;

// when
await taskAutoscaleWeb.run(scalingoClientStub);
const result = await catchErr(taskAutoscaleWeb.run)(
{ applicationName, region, autoScalingParameters },
scalingoClientStub,
);

// then
expect(autoScalerStub.called).to.be.true;
expect(getInstanceStub.calledOnceWithExactly(region)).to.be.true;
expect(result).to.be.instanceOf(Error);
expect(result.message).to.be.equal('Scalingo APIError: Cannot configure autoscaler');
});
});

0 comments on commit ddd500f

Please sign in to comment.