Skip to content

Commit

Permalink
fix instance tracker tests to be provider agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronkvanmeerten committed Dec 2, 2024
1 parent 59dfac7 commit f6f0164
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions src/test/instance_tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import assert from 'node:assert';
import test, { afterEach, describe, mock } from 'node:test';

import { InstanceTracker } from '../instance_tracker';
import RedisStore from '../redis';

describe('InstanceTracker', () => {
let context = {
Expand All @@ -17,17 +16,6 @@ describe('InstanceTracker', () => {
},
};

const redisClient = {
expire: mock.fn(),
zremrangebyscore: mock.fn(() => 0),
hgetall: mock.fn(),
hset: mock.fn(),
hdel: mock.fn(),
del: mock.fn(),
scan: mock.fn(),
zrange: mock.fn(),
};

const shutdownManager = {
shutdown: mock.fn(),
};
Expand Down Expand Up @@ -62,19 +50,30 @@ describe('InstanceTracker', () => {
},
};

const redisStore = new RedisStore({ redisClient });
const mockStore = {
fetchInstanceMetrics: mock.fn(() => [
{ value: 0.5, instanceId: 'i-0a1b2c3d4e5f6g7h8', timestamp: Date.now() - 350 },
]),

cleanInstanceMetrics: mock.fn(),

writeInstanceMetric: mock.fn(),

fetchInstanceStates: mock.fn((ctx: Context, group: string) => {
switch (provider) {
default:
// redis
return redisStore.fetchInstanceStates(ctx, group);
}
}),
filterOutAndTrimExpiredStates: mock.fn((ctx: Context, group: string, states: InstanceState[]) => states),
};

const instanceTracker = new InstanceTracker({
instanceStore: redisStore,
metricsStore: redisStore,
redisScanCount: 100,
instanceStore: mockStore,
metricsStore: mockStore,
shutdownManager,
audit,
idleTTL: 300,
metricTTL: 3600,
provisioningTTL: 900,
shutdownStatusTTL: 86400,
groupRelatedDataTTL: 172800,
});

afterEach(() => {
Expand Down Expand Up @@ -112,7 +111,7 @@ describe('InstanceTracker', () => {
hfGroupDetails.scalingOptions.scaleDownPeriodsCount,
hfGroupDetails.scalingOptions.scaleUpPeriodsCount,
);
redisClient.zrange.mock.mockImplementationOnce(() => metricInventory.map(JSON.stringify));
mockStore.fetchInstanceMetrics.mock.mockImplementationOnce(() => metricInventory);

const metricInventoryPerPeriod = await instanceTracker.getMetricInventoryPerPeriod(
context,
Expand All @@ -138,7 +137,7 @@ describe('InstanceTracker', () => {
hfGroupDetails.scalingOptions.scaleDownPeriodsCount,
hfGroupDetails.scalingOptions.scaleUpPeriodsCount,
);
redisClient.zrange.mock.mockImplementationOnce(() => metricInventory.map(JSON.stringify));
mockStore.fetchInstanceMetrics.mock.mockImplementationOnce(() => metricInventory);

const metricInventoryPerPeriod = await instanceTracker.getMetricInventoryPerPeriod(
context,
Expand All @@ -162,7 +161,7 @@ describe('InstanceTracker', () => {
hfGroupDetails.scalingOptions.scaleDownPeriodsCount,
hfGroupDetails.scalingOptions.scaleUpPeriodsCount,
);
redisClient.zrange.mock.mockImplementationOnce(() => metricInventory.map(JSON.stringify));
mockStore.fetchInstanceMetrics.mock.mockImplementationOnce(() => metricInventory);

const metricInventoryPerPeriod = await instanceTracker.getMetricInventoryPerPeriod(
context,
Expand All @@ -179,10 +178,10 @@ describe('InstanceTracker', () => {
});

assert.deepEqual(
context.logger.info.mock.calls[1].arguments[0],
context.logger.info.mock.calls[0].arguments[0],
`Filling in for missing metric from previous period`,
);
assert.deepEqual(context.logger.info.mock.calls[1].arguments[1], {
assert.deepEqual(context.logger.info.mock.calls[0].arguments[1], {
group: groupName,
instanceId: 'i-0a1b2c3d4e5f6g7h8',
periodIdx: 0,
Expand All @@ -206,7 +205,7 @@ describe('InstanceTracker', () => {
groupDetails.scalingOptions.scaleDownPeriodsCount,
groupDetails.scalingOptions.scaleUpPeriodsCount,
);
redisClient.zrange.mock.mockImplementationOnce(() => metricInventory.map(JSON.stringify));
mockStore.fetchInstanceMetrics.mock.mockImplementationOnce(() => metricInventory);
const metricInventoryPerPeriod = await instanceTracker.getMetricInventoryPerPeriod(
context,
groupDetails,
Expand Down Expand Up @@ -236,7 +235,7 @@ describe('InstanceTracker', () => {
groupDetails.scalingOptions.scaleDownPeriodsCount,
groupDetails.scalingOptions.scaleUpPeriodsCount,
);
redisClient.zrange.mock.mockImplementationOnce(() => metricInventory.map(JSON.stringify));
mockStore.fetchInstanceMetrics.mock.mockImplementationOnce(() => metricInventory);
const metricInventoryPerPeriod = await instanceTracker.getMetricInventoryPerPeriod(
context,
groupDetails,
Expand Down Expand Up @@ -266,7 +265,7 @@ describe('InstanceTracker', () => {
groupDetails.scalingOptions.scaleUpPeriodsCount,
);

redisClient.zrange.mock.mockImplementationOnce(() => metricInventory.map(JSON.stringify));
mockStore.fetchInstanceMetrics.mock.mockImplementationOnce(() => metricInventory);
const metricInventoryPerPeriod = await instanceTracker.getMetricInventoryPerPeriod(
context,
groupDetails,
Expand Down

0 comments on commit f6f0164

Please sign in to comment.