Skip to content

Commit

Permalink
Merge pull request #643 from Zooz/set-timeout-chaos
Browse files Browse the repository at this point in the history
feat: implement running experiments on setTimeout
  • Loading branch information
GuyAb authored Oct 1, 2023
2 parents a9a22e5 + b4ce0d3 commit 0834846
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/chaos-experiments/models/chaosExperimentsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ module.exports.insertChaosJobExperiment = async (jobExperimentId, jobId, experim
};

module.exports.runChaosExperiment = async (kubernetesChaosConfig, jobExperimentId) => {
await kubernetesConnector.runChaosExperiment(kubernetesChaosConfig);
await databaseConnector.setChaosJobExperimentTriggered(jobExperimentId, true);
try {
await kubernetesConnector.runChaosExperiment(kubernetesChaosConfig);
await databaseConnector.setChaosJobExperimentTriggered(jobExperimentId, true);
} catch (error){
logger.error(error, `Error while running chaos job experiment ${jobExperimentId}`);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const kubernetesConfig = require('../../../config/kubernetesConfig');
const logger = require('../../../common/logger');
const requestSender = require('../../../common/requestSender');
const kubernetesUrl = kubernetesConfig.kubernetesUrl;
const kubernetesNamespace = kubernetesConfig.kubernetesNamespace;

const TOKEN_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/token';
const headers = {};
Expand All @@ -24,7 +23,8 @@ if (kubernetesConfig.kubernetesToken) {

module.exports.runChaosExperiment = async (kubernetesExperimentConfig) => {
const resourceKindName = kubernetesExperimentConfig.kind.toLowerCase();
const url = util.format('%s/apis/chaos-mesh.org/v1/namespaces/%s/%s', kubernetesUrl, kubernetesNamespace, resourceKindName);
const kubernetesNamespace = kubernetesExperimentConfig.metadata.namespace;
const url = util.format('%s/apis/chaos-mesh.org/v1alpha1/namespaces/%s/%s', kubernetesUrl, kubernetesNamespace, resourceKindName);
const options = {
url,
method: 'POST',
Expand Down
2 changes: 1 addition & 1 deletion src/jobs/models/jobExperimentsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function setSingleJobExperiment(experimentRequest, experimentsFromDb, base
const jobExperimentId = uuid();
await chaosExperimentManager.insertChaosJobExperiment(jobExperimentId, jobId, experiment.id, startTime, endTime);
const kubeObject = experiment.kubeObject;
kubeObject.name = kubeObject.metadata.name.concat(`_${jobExperimentId}`);
kubeObject.metadata.name = kubeObject.metadata.name.concat(`-${jobExperimentId}`);
const timeout = setTimeout(() => chaosExperimentManager.runChaosExperiment(kubeObject, jobExperimentId), experimentRequest.start_after);
jobExperimentsIdToTimeout.set(jobExperimentId, timeout);
} catch (error){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Chaos experiments manager tests', function () {
apiVersion: 'chaos-mesh.org/v1alpha1',
metadata: {
namespace: 'apps',
name: 'first pod fault',
name: 'firstPodChaos',
annotations: {}
},
spec: {
Expand All @@ -41,11 +41,11 @@ describe('Chaos experiments manager tests', function () {
}
}
};
requestSenderSendStub.resolves({ metadata: { name: 'firstPodChaos', uid: 'some_uuid' }, namespace: 'default' });
requestSenderSendStub.resolves({ metadata: { name: 'firstPodChaos', uid: 'some_uuid' }, namespace: 'apps' });
await chaosExperimentConnector.runChaosExperiment(kubernetesJobConfig);
requestSenderSendStub.callCount.should.eql(1);
requestSenderSendStub.args[0][0].should.eql({
url: 'localhost:80/apis/chaos-mesh.org/v1/namespaces/default/podchaos',
url: 'localhost:80/apis/chaos-mesh.org/v1alpha1/namespaces/apps/podchaos',
method: 'POST',
body: kubernetesJobConfig,
headers: {}
Expand All @@ -58,7 +58,7 @@ describe('Chaos experiments manager tests', function () {
apiVersion: 'chaos-mesh.org/v1alpha1',
metadata: {
namespace: 'apps',
name: 'first http fault',
name: 'firstHttpChaos',
annotations: {}
},
spec: {
Expand All @@ -73,11 +73,11 @@ describe('Chaos experiments manager tests', function () {
}
}
};
requestSenderSendStub.resolves({ metadata: { name: 'firstHttpChaos', uid: 'some_uuid' }, namespace: 'default' });
requestSenderSendStub.resolves({ metadata: { name: 'firstHttpChaos', uid: 'some_uuid' }, namespace: 'apps' });
await chaosExperimentConnector.runChaosExperiment(kubernetesJobConfig);
requestSenderSendStub.callCount.should.eql(1);
requestSenderSendStub.args[0][0].should.eql({
url: 'localhost:80/apis/chaos-mesh.org/v1/namespaces/default/httpchaos',
url: 'localhost:80/apis/chaos-mesh.org/v1alpha1/namespaces/apps/httpchaos',
method: 'POST',
body: kubernetesJobConfig,
headers: {}
Expand Down
2 changes: 2 additions & 0 deletions tests/unit-tests/jobs/models/jobExperimentsHandler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('Job experiments handler tests', function () {
it('set chaos experiments with 2 experiments', async () => {
const firstExperiment = generateExperiment();
const secondExperiment = generateExperiment();
const firstExperimentName = firstExperiment.kubeObject.metadata.name;
experimentsManagerInsertStub.resolves();
experimentsManagerGetStub.resolves([firstExperiment, secondExperiment]);
experimentsManagerRunJobStub.resolves();
Expand All @@ -64,6 +65,7 @@ describe('Job experiments handler tests', function () {
await jobExperimentHandler.setChaosExperimentsIfExist(jobId, jobExperiments);
clock.tick(3000);
experimentsManagerRunJobStub.callCount.should.eql(2);
experimentsManagerRunJobStub.args[0][0].metadata.name.should.eql(`${firstExperimentName}-${experimentsManagerRunJobStub.args[0][1]}`);
experimentsManagerGetStub.callCount.should.eql(1);
experimentsManagerInsertStub.callCount.should.eql(2);
experimentsManagerInsertStub.args[0][1].should.eql(jobId);
Expand Down

0 comments on commit 0834846

Please sign in to comment.