2023-09-07 05:44:19 -07:00
|
|
|
import Container from 'typedi';
|
|
|
|
import config from '@/config';
|
2023-10-06 04:58:11 -07:00
|
|
|
import { OrchestrationMainService } from '@/services/orchestration/main/orchestration.main.service';
|
2023-09-07 05:44:19 -07:00
|
|
|
import type { RedisServiceWorkerResponseObject } from '@/services/redis/RedisServiceCommands';
|
|
|
|
import { eventBus } from '@/eventbus';
|
|
|
|
import { RedisService } from '@/services/redis.service';
|
|
|
|
import { mockInstance } from '../../integration/shared/utils';
|
2023-10-06 04:58:11 -07:00
|
|
|
import { handleWorkerResponseMessageMain } from '@/services/orchestration/main/handleWorkerResponseMessageMain';
|
|
|
|
import { handleCommandMessageMain } from '@/services/orchestration/main/handleCommandMessageMain';
|
|
|
|
import { OrchestrationHandlerMainService } from '@/services/orchestration/main/orchestration.handler.main.service';
|
|
|
|
import * as helpers from '@/services/orchestration/helpers';
|
|
|
|
import { ExternalSecretsManager } from '@/ExternalSecrets/ExternalSecretsManager.ee';
|
2023-10-25 07:35:22 -07:00
|
|
|
import { Logger } from '@/Logger';
|
2023-09-07 05:44:19 -07:00
|
|
|
|
2023-10-06 04:58:11 -07:00
|
|
|
const os = Container.get(OrchestrationMainService);
|
|
|
|
const handler = Container.get(OrchestrationHandlerMainService);
|
2023-09-07 05:44:19 -07:00
|
|
|
|
2023-09-26 04:58:06 -07:00
|
|
|
let queueModeId: string;
|
|
|
|
|
2023-09-07 05:44:19 -07:00
|
|
|
function setDefaultConfig() {
|
|
|
|
config.set('executions.mode', 'queue');
|
2023-10-06 04:58:11 -07:00
|
|
|
config.set('generic.instanceType', 'main');
|
2023-09-07 05:44:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const workerRestartEventbusResponse: RedisServiceWorkerResponseObject = {
|
|
|
|
senderId: 'test',
|
|
|
|
workerId: 'test',
|
|
|
|
command: 'restartEventBus',
|
|
|
|
payload: {
|
|
|
|
result: 'success',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('Orchestration Service', () => {
|
2023-10-25 07:35:22 -07:00
|
|
|
const logger = mockInstance(Logger);
|
2023-09-07 05:44:19 -07:00
|
|
|
beforeAll(async () => {
|
|
|
|
mockInstance(RedisService);
|
2023-10-06 04:58:11 -07:00
|
|
|
mockInstance(ExternalSecretsManager);
|
2023-09-07 05:44:19 -07:00
|
|
|
jest.mock('ioredis', () => {
|
|
|
|
const Redis = require('ioredis-mock');
|
|
|
|
if (typeof Redis === 'object') {
|
|
|
|
// the first mock is an ioredis shim because ioredis-mock depends on it
|
|
|
|
// https://github.com/stipsan/ioredis-mock/blob/master/src/index.js#L101-L111
|
|
|
|
return {
|
|
|
|
Command: { _transformer: { argument: {}, reply: {} } },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// second mock for our code
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
return function (...args: any) {
|
|
|
|
return new Redis(args);
|
|
|
|
};
|
|
|
|
});
|
2023-10-25 07:35:22 -07:00
|
|
|
jest.mock('@/services/redis/RedisServicePubSubPublisher', () => {
|
2023-09-07 05:44:19 -07:00
|
|
|
return jest.fn().mockImplementation(() => {
|
|
|
|
return {
|
|
|
|
init: jest.fn(),
|
|
|
|
publishToEventLog: jest.fn(),
|
|
|
|
publishToWorkerChannel: jest.fn(),
|
|
|
|
destroy: jest.fn(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
2023-10-25 07:35:22 -07:00
|
|
|
jest.mock('@/services/redis/RedisServicePubSubSubscriber', () => {
|
2023-09-07 05:44:19 -07:00
|
|
|
return jest.fn().mockImplementation(() => {
|
|
|
|
return {
|
|
|
|
subscribeToCommandChannel: jest.fn(),
|
|
|
|
destroy: jest.fn(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
setDefaultConfig();
|
2023-09-26 04:58:06 -07:00
|
|
|
queueModeId = config.get('redis.queueModeId');
|
2023-09-07 05:44:19 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2023-10-25 07:35:22 -07:00
|
|
|
jest.mock('@/services/redis/RedisServicePubSubPublisher').restoreAllMocks();
|
|
|
|
jest.mock('@/services/redis/RedisServicePubSubSubscriber').restoreAllMocks();
|
2023-09-17 02:05:54 -07:00
|
|
|
await os.shutdown();
|
2023-09-07 05:44:19 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should initialize', async () => {
|
2023-09-26 04:58:06 -07:00
|
|
|
await os.init();
|
2023-09-28 03:57:35 -07:00
|
|
|
await handler.init();
|
2023-09-07 05:44:19 -07:00
|
|
|
expect(os.redisPublisher).toBeDefined();
|
2023-09-28 03:57:35 -07:00
|
|
|
expect(handler.redisSubscriber).toBeDefined();
|
2023-09-26 04:58:06 -07:00
|
|
|
expect(queueModeId).toBeDefined();
|
2023-09-07 05:44:19 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should handle worker responses', async () => {
|
2023-10-06 04:58:11 -07:00
|
|
|
const response = await handleWorkerResponseMessageMain(
|
2023-09-07 05:44:19 -07:00
|
|
|
JSON.stringify(workerRestartEventbusResponse),
|
|
|
|
);
|
|
|
|
expect(response.command).toEqual('restartEventBus');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should handle command messages from others', async () => {
|
2023-10-06 04:58:11 -07:00
|
|
|
const responseFalseId = await handleCommandMessageMain(
|
2023-09-17 02:05:54 -07:00
|
|
|
JSON.stringify({
|
|
|
|
senderId: 'test',
|
|
|
|
command: 'reloadLicense',
|
|
|
|
}),
|
2023-09-07 05:44:19 -07:00
|
|
|
);
|
|
|
|
expect(responseFalseId).toBeDefined();
|
2023-09-17 02:05:54 -07:00
|
|
|
expect(responseFalseId!.command).toEqual('reloadLicense');
|
2023-09-07 05:44:19 -07:00
|
|
|
expect(responseFalseId!.senderId).toEqual('test');
|
2023-10-25 07:35:22 -07:00
|
|
|
expect(logger.error).toHaveBeenCalled();
|
2023-09-07 05:44:19 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should reject command messages from iteslf', async () => {
|
|
|
|
jest.spyOn(eventBus, 'restart');
|
2023-10-06 04:58:11 -07:00
|
|
|
const response = await handleCommandMessageMain(
|
2023-09-26 04:58:06 -07:00
|
|
|
JSON.stringify({ ...workerRestartEventbusResponse, senderId: queueModeId }),
|
2023-09-07 05:44:19 -07:00
|
|
|
);
|
|
|
|
expect(response).toBeDefined();
|
|
|
|
expect(response!.command).toEqual('restartEventBus');
|
2023-09-26 04:58:06 -07:00
|
|
|
expect(response!.senderId).toEqual(queueModeId);
|
2023-09-07 05:44:19 -07:00
|
|
|
expect(eventBus.restart).not.toHaveBeenCalled();
|
|
|
|
jest.spyOn(eventBus, 'restart').mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should send command messages', async () => {
|
2023-10-06 04:58:11 -07:00
|
|
|
setDefaultConfig();
|
|
|
|
jest.spyOn(os.redisPublisher, 'publishToCommandChannel').mockImplementation(async () => {});
|
2023-09-07 05:44:19 -07:00
|
|
|
await os.getWorkerIds();
|
|
|
|
expect(os.redisPublisher.publishToCommandChannel).toHaveBeenCalled();
|
|
|
|
jest.spyOn(os.redisPublisher, 'publishToCommandChannel').mockRestore();
|
|
|
|
});
|
2023-10-06 04:58:11 -07:00
|
|
|
|
|
|
|
test('should prevent receiving commands too often', async () => {
|
|
|
|
setDefaultConfig();
|
|
|
|
jest.spyOn(helpers, 'debounceMessageReceiver');
|
|
|
|
const res1 = await handleCommandMessageMain(
|
|
|
|
JSON.stringify({
|
|
|
|
senderId: 'test',
|
|
|
|
command: 'reloadExternalSecretsProviders',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
const res2 = await handleCommandMessageMain(
|
|
|
|
JSON.stringify({
|
|
|
|
senderId: 'test',
|
|
|
|
command: 'reloadExternalSecretsProviders',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(helpers.debounceMessageReceiver).toHaveBeenCalledTimes(2);
|
|
|
|
expect(res1!.payload).toBeUndefined();
|
|
|
|
expect(res2!.payload!.result).toEqual('debounced');
|
|
|
|
});
|
2023-09-07 05:44:19 -07:00
|
|
|
});
|