Skip to content

Commit

Permalink
move mock store to own container
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronkvanmeerten committed Dec 2, 2024
1 parent f6f0164 commit 9986849
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 65 deletions.
20 changes: 1 addition & 19 deletions src/test/instance_tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import assert from 'node:assert';
import test, { afterEach, describe, mock } from 'node:test';

import { InstanceTracker } from '../instance_tracker';
import { mockStore } from './mock_store';

describe('InstanceTracker', () => {
let context = {
Expand Down Expand Up @@ -50,25 +51,6 @@ describe('InstanceTracker', () => {
},
};

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: mockStore,
metricsStore: mockStore,
Expand Down
20 changes: 20 additions & 0 deletions src/test/mock_store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { mock } from 'node:test';
import { Context } from '../context';
import { InstanceState } from '../instance_store';

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

writeInstanceMetric: mock.fn(),

fetchInstanceStates: mock.fn(() => []),
filterOutAndTrimExpiredStates: mock.fn((ctx: Context, group: string, states: InstanceState[]) => states),

getShutdownStatuses: mock.fn(() => [false]),
getShutdownStatus: mock.fn(() => false),
getShutdownConfirmation: mock.fn(() => false),
getShutdownConfirmations: mock.fn(() => [false]),
};
63 changes: 17 additions & 46 deletions src/test/shutdown_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import assert from 'node:assert';
import test, { afterEach, describe, mock } from 'node:test';

import RedisStore from '../redis';
import { mockStore } from './mock_store';

import ShutdownManager from '../shutdown_manager';

describe('ShutdownManager', () => {
Expand All @@ -17,42 +18,18 @@ describe('ShutdownManager', () => {
},
};

let _keys = [];

const mockPipeline = {
get: mock.fn((key) => _keys.push(key)),
set: mock.fn(),
exec: mock.fn(() => Promise.resolve(_keys.map(() => [null, null]))),
};

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(),
get: mock.fn(),
pipeline: mock.fn(() => mockPipeline),
};

const instanceStore = new RedisStore({ redisClient });
const audit = {
log: mock.fn(),
};

const shutdownManager = new ShutdownManager({
instanceStore,
instanceStore: mockStore,
audit,
shutdownTTL: 86400,
});

afterEach(() => {
_keys = [];
mockPipeline.exec.mock.resetCalls();
mockPipeline.get.mock.resetCalls();
mockStore.getShutdownConfirmations.mock.resetCalls();
context = {
logger: {
info: mock.fn(),
Expand All @@ -67,21 +44,21 @@ describe('ShutdownManager', () => {
// these tests are for the shutdown confirmation statuses
describe('shutdownConfirmationStatuses', () => {
test('read non-existent shutdown confirmation status', async () => {
redisClient.get.mock.mockImplementationOnce(() => null);
const result = await shutdownManager.getShutdownConfirmation(context, 'instanceId');
assert.equal(result, false, 'expect no shutdown confirmation when no key exists');
});

test('read existing shutdown confirmation status', async () => {
const shutdownConfirmation = new Date().toISOString();
redisClient.get.mock.mockImplementationOnce(() => shutdownConfirmation);
mockStore.getShutdownConfirmation.mock.mockImplementationOnce(() => shutdownConfirmation);
const result = await shutdownManager.getShutdownConfirmation(context, 'instanceId');
assert.ok(result, 'expect ok result');
assert.equal(result, shutdownConfirmation, 'expect shutdown confirmation to match mock date');
});

test('read multiple non-existent shutdown confirmation statuses', async () => {
const instances = ['instanceId', 'instanceId2'];
mockStore.getShutdownConfirmations.mock.mockImplementationOnce(() => [false, false]);
const result = await shutdownManager.getShutdownConfirmations(context, instances);
assert.ok(result, 'expect ok result');
assert.equal(result.length, instances.length, 'expect confirmation length to match instances length');
Expand All @@ -93,17 +70,17 @@ describe('ShutdownManager', () => {
const shutdownConfirmation = new Date().toISOString();

const instances = ['instanceId', 'instanceId2'];
mockPipeline.exec.mock.mockImplementationOnce(() =>
Promise.resolve(instances.map(() => [null, shutdownConfirmation])),
);
mockStore.getShutdownConfirmations.mock.mockImplementationOnce(() => [
shutdownConfirmation,
shutdownConfirmation,
]);

const result = await shutdownManager.getShutdownConfirmations(context, instances);
assert.ok(result, 'expect ok result');
assert.equal(mockPipeline.exec.mock.callCount(), 1, 'expect exec to be called once');
assert.equal(
mockPipeline.get.mock.callCount(),
instances.length,
'expect get to be called once per instance',
mockStore.getShutdownConfirmations.mock.callCount(),
1,
'expect getShutdownConfirmations to be called once',
);
assert.equal(result.length, instances.length, 'expect confirmation length to match instances length');
assert.equal(result[0], shutdownConfirmation, 'expect first confirmation to match mock date');
Expand All @@ -114,20 +91,14 @@ describe('ShutdownManager', () => {
const shutdownConfirmation = new Date().toISOString();

const instances = ['instanceId', 'instanceId2'];
mockPipeline.exec.mock.mockImplementationOnce(() =>
Promise.resolve([
[null, null],
[null, shutdownConfirmation],
]),
);
mockStore.getShutdownConfirmations.mock.mockImplementationOnce(() => [false, shutdownConfirmation]);

const result = await shutdownManager.getShutdownConfirmations(context, instances);
assert.ok(result, 'expect ok result');
assert.equal(mockPipeline.exec.mock.callCount(), 1, 'expect exec to be called once');
assert.equal(
mockPipeline.get.mock.callCount(),
instances.length,
'expect get to be called once per instance',
mockStore.getShutdownConfirmations.mock.callCount(),
1,
'expect getShutdownConfirmations to be called once',
);
assert.equal(result.length, instances.length, 'expect confirmation length to match instances length');
assert.equal(result[0], false, 'expect first confirmation to be false');
Expand Down

0 comments on commit 9986849

Please sign in to comment.