Skip to content

Commit

Permalink
basic scaling tests completed
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronkvanmeerten committed Feb 29, 2024
1 parent 1528617 commit 6f1266e
Showing 1 changed file with 229 additions and 4 deletions.
233 changes: 229 additions & 4 deletions src/test/autoscaler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,120 @@ describe('AutoscaleProcessor', () => {
assert.strictEqual(result, true);
});
test('will choose to increase desired count to maximum and not higher', async () => {
// do something
const scalableGroup = {
...groupDetails,
scalingOptions: {
...groupDetails.scalingOptions,
maxDesired: 2,
scaleUpThreshold: 0.8,
scaleUpQuantity: 5,
},
};
instanceGroupManager.getInstanceGroup.mock.mockImplementationOnce(() => {
return scalableGroup;
});
const inventory = [{ instance_id: 'i-0a1b2c3d4e5f6g7h8' }];

instanceTracker.trimCurrent.mock.mockImplementationOnce(() => inventory);
instanceTracker.getMetricInventoryPerPeriod.mock.mockImplementationOnce(() => {
return [
[{ value: 1, instanceId: 'i-0a1b2c3d4e5f6g7h8' }],
[{ value: 1, instanceId: 'i-0a1b2c3d4e5f6g7h8' }],
[{ value: 1, instanceId: 'i-0a1b2c3d4e5f6g7h8' }],
];
});

const currentDesired = scalableGroup.scalingOptions.desiredCount;
const expectedDesired = 2;
const result = await autoscaleProcessor.processAutoscalingByGroup(context, groupName);

// assert inventory was read
assert.deepEqual(instanceTracker.trimCurrent.mock.calls.length, 1);
// assert metric inventory was read
assert.deepEqual(instanceTracker.getMetricInventoryPerPeriod.mock.calls.length, 1);
// assert one action items was saved
assert.deepEqual(audit.saveAutoScalerActionItem.mock.calls.length, 1);

assert.deepEqual(
context.logger.debug.mock.calls[0].arguments[0],
`[AutoScaler] Begin desired count adjustments for group ${groupName} with 1 instances and current desired count ${currentDesired}`,
);

assert.deepEqual(
context.logger.info.mock.calls[1].arguments[0],
`[AutoScaler] Evaluating scale up choice for group ${groupName} with 1 instances and current desired count ${currentDesired}`,
);

assert.deepEqual(
context.logger.info.mock.calls[2].arguments[0],
`[AutoScaler] Increasing desired count to ${expectedDesired} for group ${groupName} with ${inventory.length} instances`,
);

assert.deepEqual(context.logger.info.mock.calls[2].arguments[1], { desiredCount: expectedDesired });
// one update to the instance group is expected
assert.deepEqual(instanceGroupManager.upsertInstanceGroup.mock.calls.length, 1);

assert.deepEqual(audit.updateLastAutoScalerRun.mock.callCount(), 1);
assert.deepEqual(audit.updateLastAutoScalerRun.mock.calls[0].arguments[1], groupName);
// assert process ended with success
assert.strictEqual(result, true);
});
test('would choose to increase desired count but maximum was reached', async () => {
// do something
const scalableGroup = {
...groupDetails,
scalingOptions: { ...groupDetails.scalingOptions, scaleUpThreshold: 0.8 },
};
instanceGroupManager.getInstanceGroup.mock.mockImplementationOnce(() => {
return scalableGroup;
});
const inventory = [{ instance_id: 'i-0a1b2c3d4e5f6g7h8' }];

instanceTracker.trimCurrent.mock.mockImplementationOnce(() => inventory);
instanceTracker.getMetricInventoryPerPeriod.mock.mockImplementationOnce(() => {
return [
[{ value: 1, instanceId: 'i-0a1b2c3d4e5f6g7h8' }],
[{ value: 1, instanceId: 'i-0a1b2c3d4e5f6g7h8' }],
[{ value: 1, instanceId: 'i-0a1b2c3d4e5f6g7h8' }],
];
});

const currentDesired = scalableGroup.scalingOptions.desiredCount;
const result = await autoscaleProcessor.processAutoscalingByGroup(context, groupName);

// assert inventory was read
assert.deepEqual(instanceTracker.trimCurrent.mock.calls.length, 1);
// assert metric inventory was read
assert.deepEqual(instanceTracker.getMetricInventoryPerPeriod.mock.calls.length, 1);
// assert no action items were saved
assert.deepEqual(audit.saveAutoScalerActionItem.mock.calls.length, 0);

assert.deepEqual(
context.logger.debug.mock.calls[0].arguments[0],
`[AutoScaler] Begin desired count adjustments for group ${groupName} with 1 instances and current desired count ${currentDesired}`,
);

assert.deepEqual(
context.logger.info.mock.calls[1].arguments[0],
`[AutoScaler] Evaluating scale up choice for group ${groupName} with 1 instances and current desired count ${currentDesired}`,
);

assert.deepEqual(
context.logger.info.mock.calls[2].arguments[0],
`[AutoScaler] Evaluating scale down choice for group ${groupName} with 1 instances and current desired count ${currentDesired}`,
);

assert.deepEqual(
context.logger.info.mock.calls[3].arguments[0],
`[AutoScaler] No desired count adjustments needed for group ${groupName} with 1 instances`,
);

// no updates to the instance group is expected
assert.deepEqual(instanceGroupManager.upsertInstanceGroup.mock.calls.length, 0);

assert.deepEqual(audit.updateLastAutoScalerRun.mock.callCount(), 1);
assert.deepEqual(audit.updateLastAutoScalerRun.mock.calls[0].arguments[1], groupName);
// assert process ended with success
assert.strictEqual(result, true);
});
test('will choose to decrease desired count', async () => {
const scalableGroup = {
Expand Down Expand Up @@ -320,10 +430,125 @@ describe('AutoscaleProcessor', () => {
assert.strictEqual(result, true);
});
test('will choose to increase desired count to minimum and not lower', async () => {
// do something
const scalableGroup = {
...groupDetails,
scalingOptions: {
...groupDetails.scalingOptions,
minDesired: 0,
scaleDownThreshold: 0.3,
scaleDownQuantity: 5,
},
};
instanceGroupManager.getInstanceGroup.mock.mockImplementationOnce(() => {
return scalableGroup;
});
const inventory = [{ instance_id: 'i-0a1b2c3d4e5f6g7h8' }];

instanceTracker.trimCurrent.mock.mockImplementationOnce(() => inventory);
instanceTracker.getMetricInventoryPerPeriod.mock.mockImplementationOnce(() => {
return [
[{ value: 0.1, instanceId: 'i-0a1b2c3d4e5f6g7h8' }],
[{ value: 0.1, instanceId: 'i-0a1b2c3d4e5f6g7h8' }],
[{ value: 0.1, instanceId: 'i-0a1b2c3d4e5f6g7h8' }],
];
});

const currentDesired = scalableGroup.scalingOptions.desiredCount;
const expectedDesired = 0;
const result = await autoscaleProcessor.processAutoscalingByGroup(context, groupName);

// assert inventory was read
assert.deepEqual(instanceTracker.trimCurrent.mock.calls.length, 1);
// assert metric inventory was read
assert.deepEqual(instanceTracker.getMetricInventoryPerPeriod.mock.calls.length, 1);
// assert no action items were saved
assert.deepEqual(audit.saveAutoScalerActionItem.mock.calls.length, 1);

assert.deepEqual(
context.logger.debug.mock.calls[0].arguments[0],
`[AutoScaler] Begin desired count adjustments for group ${groupName} with 1 instances and current desired count ${currentDesired}`,
);

assert.deepEqual(
context.logger.info.mock.calls[1].arguments[0],
`[AutoScaler] Evaluating scale up choice for group ${groupName} with 1 instances and current desired count ${currentDesired}`,
);

assert.deepEqual(
context.logger.info.mock.calls[2].arguments[0],
`[AutoScaler] Evaluating scale down choice for group ${groupName} with 1 instances and current desired count ${currentDesired}`,
);

assert.deepEqual(
context.logger.info.mock.calls[3].arguments[0],
`[AutoScaler] Reducing desired count to ${expectedDesired} for group ${groupName} with ${inventory.length} instances`,
);

assert.deepEqual(context.logger.info.mock.calls[3].arguments[1], { desiredCount: expectedDesired });
// one update to the instance group is expected
assert.deepEqual(instanceGroupManager.upsertInstanceGroup.mock.calls.length, 1);

assert.deepEqual(audit.updateLastAutoScalerRun.mock.callCount(), 1);
assert.deepEqual(audit.updateLastAutoScalerRun.mock.calls[0].arguments[1], groupName);
// assert process ended with success
assert.strictEqual(result, true);
});
test('would choose to decrease desired count but minimum was reached', async () => {
// do something
const scalableGroup = {
...groupDetails,
scalingOptions: { ...groupDetails.scalingOptions, scaleDownThreshold: 0.3 },
};
instanceGroupManager.getInstanceGroup.mock.mockImplementationOnce(() => {
return scalableGroup;
});
const inventory = [{ instance_id: 'i-0a1b2c3d4e5f6g7h8' }];

instanceTracker.trimCurrent.mock.mockImplementationOnce(() => inventory);
instanceTracker.getMetricInventoryPerPeriod.mock.mockImplementationOnce(() => {
return [
[{ value: 0.1, instanceId: 'i-0a1b2c3d4e5f6g7h8' }],
[{ value: 0.1, instanceId: 'i-0a1b2c3d4e5f6g7h8' }],
[{ value: 0.1, instanceId: 'i-0a1b2c3d4e5f6g7h8' }],
];
});

const currentDesired = scalableGroup.scalingOptions.desiredCount;
const result = await autoscaleProcessor.processAutoscalingByGroup(context, groupName);

// assert inventory was read
assert.deepEqual(instanceTracker.trimCurrent.mock.calls.length, 1);
// assert metric inventory was read
assert.deepEqual(instanceTracker.getMetricInventoryPerPeriod.mock.calls.length, 1);
// assert no action items were saved
assert.deepEqual(audit.saveAutoScalerActionItem.mock.calls.length, 0);

assert.deepEqual(
context.logger.debug.mock.calls[0].arguments[0],
`[AutoScaler] Begin desired count adjustments for group ${groupName} with 1 instances and current desired count ${currentDesired}`,
);

assert.deepEqual(
context.logger.info.mock.calls[1].arguments[0],
`[AutoScaler] Evaluating scale up choice for group ${groupName} with 1 instances and current desired count ${currentDesired}`,
);

assert.deepEqual(
context.logger.info.mock.calls[2].arguments[0],
`[AutoScaler] Evaluating scale down choice for group ${groupName} with 1 instances and current desired count ${currentDesired}`,
);

assert.deepEqual(
context.logger.info.mock.calls[3].arguments[0],
`[AutoScaler] No desired count adjustments needed for group ${groupName} with 1 instances`,
);

// no updates to the instance group is expected
assert.deepEqual(instanceGroupManager.upsertInstanceGroup.mock.calls.length, 0);

assert.deepEqual(audit.updateLastAutoScalerRun.mock.callCount(), 1);
assert.deepEqual(audit.updateLastAutoScalerRun.mock.calls[0].arguments[1], groupName);
// assert process ended with success
assert.strictEqual(result, true);
});
});

Expand Down

0 comments on commit 6f1266e

Please sign in to comment.