Skip to content

Commit

Permalink
add auto scaler service
Browse files Browse the repository at this point in the history
  • Loading branch information
annemarie35 authored and emeric-martineau committed Sep 1, 2023
1 parent c25d9e3 commit e8ffb4b
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 1 deletion.
14 changes: 14 additions & 0 deletions build/services/auto-scaler-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function createAutoscaller() {
return async () => {
return true
};
}

async function scalingoClient({ scalingoToken, scalingoApiUrl }) {
const client = await scalingo.clientFromToken(scalingoToken, { apiUrl: scalingoApiUrl });
}

module.exports = {
createAutoscaller,
scalingoClient
};
12 changes: 12 additions & 0 deletions common/services/scalingo-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ class ScalingoClient {
throw new Error(`Impossible to create ${app.name}, ${e.name}`);
}
}

async updateAutoscaler(appname, updateParams) {
const autoscalers = await this.client.Autoscalers.for(appname);

if (!Array.isArray(autoscalers) || !autoscalers.length) {
throw new Error(`Aucun autoscaller trouvé pour l'application '${appname}'`)
}

const autoscaler = autoscalers[0];

await this.client.Autoscalers.update(appname, autoscaler.id, updateParams);
}
}

async function _isUrlReachable(url) {
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const server = require('./server');
const { createCronJob } = require('./common/services/cron-job');
const githubServices = require('./common/services/github');
const { deploy } = require('./run/services/deploy');
const ecoModeService = require('./build/services/eco-mode-service');
const = require('./build/services/eco-mode-service');
const logger = require('./common/services/logger');

const init = async () => {
Expand Down
13 changes: 13 additions & 0 deletions test/unit/build/services/auto-scaler_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { expect, sinon } = require('../../../test-helper');
const { createAutoscaller } = require('../../../../build/services/auto-scaler-service.js');
describe('Unit | Services | AutoScaler ', function () {
describe('#startAutoScaler', function () {
it.only('should start the job', async function () {
// given
// when
const ret = createAutoscaller({});
// then
expect(await ret()).to.be.true;
});
});
});
145 changes: 145 additions & 0 deletions test/unit/common/services/scalingo-client_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,149 @@ describe('Scalingo client', () => {
expect(actual.message).to.equal('Impossible to create pix-application-recette, foo');
});
});

describe('#Scalingo.updateAutoscaler', () => {
it('should update autoscaller for one application', async () => {
// given
const forAutoscallerStub = sinon.stub();
const updateAutoscallerStub = sinon.stub();

forAutoscallerStub.resolves([
{
id: 'au-123456789'
}
])

updateAutoscallerStub.resolves()

const clientStub = {
clientFromToken: async () => {
return { Autoscalers: { for: forAutoscallerStub, update: updateAutoscallerStub } };
},
};

const scalingoClient = await ScalingoClient.getInstance('recette', clientStub);

const autoscalerUpdateParams = {
min_containers: 1,
max_containers: 2,
};

// when
await scalingoClient.updateAutoscaler('pix-application-recette', autoscalerUpdateParams);

// then
expect(forAutoscallerStub.calledOnceWith('pix-application-recette')).to.be.true;
expect(updateAutoscallerStub.calledOnceWith(
'pix-application-recette',
'au-123456789',
autoscalerUpdateParams)).to.be.true;
});

it('should throw when autoscaler not found', async () => {
// given
const forAutoscallerStub = sinon.stub();
const updateAutoscallerStub = sinon.stub();

forAutoscallerStub.resolves([])

updateAutoscallerStub.resolves()

const clientStub = {
clientFromToken: async () => {
return { Autoscalers: { for: forAutoscallerStub, update: updateAutoscallerStub } };
},
};

const scalingoClient = await ScalingoClient.getInstance('recette', clientStub);

const autoscalerUpdateParams = {
min_containers: 1,
max_containers: 2,
};

// when
let actual;
try {
await scalingoClient.updateAutoscaler('pix-application-recette', autoscalerUpdateParams);
} catch (error) {
actual = error;
}

// then
expect(actual.message).to.equal("Aucun autoscaller trouvé pour l'application 'pix-application-recette'");
});

it('should throw when autoscaler api return undefined', async () => {
// given
const forAutoscallerStub = sinon.stub();
const updateAutoscallerStub = sinon.stub();

forAutoscallerStub.resolves(undefined)

updateAutoscallerStub.resolves()

const clientStub = {
clientFromToken: async () => {
return { Autoscalers: { for: forAutoscallerStub, update: updateAutoscallerStub } };
},
};

const scalingoClient = await ScalingoClient.getInstance('recette', clientStub);

const autoscalerUpdateParams = {
min_containers: 1,
max_containers: 2,
};

// when
let actual;
try {
await scalingoClient.updateAutoscaler('pix-application-recette', autoscalerUpdateParams);
} catch (error) {
actual = error;
}

// then
expect(actual.message).to.equal("Aucun autoscaller trouvé pour l'application 'pix-application-recette'");
});

it('should throw when autoscaler api return null', async () => {
// given
const forAutoscallerStub = sinon.stub();
const updateAutoscallerStub = sinon.stub();

forAutoscallerStub.resolves(null)

updateAutoscallerStub.resolves()

const clientStub = {
clientFromToken: async () => {
return { Autoscalers: { for: forAutoscallerStub, update: updateAutoscallerStub } };
},
};

const scalingoClient = await ScalingoClient.getInstance('recette', clientStub);

const autoscalerUpdateParams = {
min_containers: 1,
max_containers: 2,
};

// when
let actual;
try {
await scalingoClient.updateAutoscaler('pix-application-recette', autoscalerUpdateParams);
} catch (error) {
actual = error;
}

// then
expect(actual.message).to.equal("Aucun autoscaller trouvé pour l'application 'pix-application-recette'");
});

it('should throw when update autoscaler', async () => {

});
});
});

0 comments on commit e8ffb4b

Please sign in to comment.