@friggframework/admin-scripts 2.0.0--canary.517.41839c5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +9 -0
- package/index.js +66 -0
- package/package.json +53 -0
- package/src/adapters/__tests__/aws-scheduler-adapter.test.js +322 -0
- package/src/adapters/__tests__/local-scheduler-adapter.test.js +325 -0
- package/src/adapters/__tests__/scheduler-adapter-factory.test.js +257 -0
- package/src/adapters/__tests__/scheduler-adapter.test.js +103 -0
- package/src/adapters/aws-scheduler-adapter.js +138 -0
- package/src/adapters/local-scheduler-adapter.js +103 -0
- package/src/adapters/scheduler-adapter-factory.js +69 -0
- package/src/adapters/scheduler-adapter.js +64 -0
- package/src/application/__tests__/admin-frigg-commands.test.js +643 -0
- package/src/application/__tests__/admin-script-base.test.js +273 -0
- package/src/application/__tests__/dry-run-http-interceptor.test.js +313 -0
- package/src/application/__tests__/dry-run-repository-wrapper.test.js +257 -0
- package/src/application/__tests__/schedule-management-use-case.test.js +276 -0
- package/src/application/__tests__/script-factory.test.js +381 -0
- package/src/application/__tests__/script-runner.test.js +202 -0
- package/src/application/admin-frigg-commands.js +242 -0
- package/src/application/admin-script-base.js +138 -0
- package/src/application/dry-run-http-interceptor.js +296 -0
- package/src/application/dry-run-repository-wrapper.js +261 -0
- package/src/application/schedule-management-use-case.js +230 -0
- package/src/application/script-factory.js +161 -0
- package/src/application/script-runner.js +254 -0
- package/src/builtins/__tests__/integration-health-check.test.js +598 -0
- package/src/builtins/__tests__/oauth-token-refresh.test.js +344 -0
- package/src/builtins/index.js +28 -0
- package/src/builtins/integration-health-check.js +279 -0
- package/src/builtins/oauth-token-refresh.js +221 -0
- package/src/infrastructure/__tests__/admin-auth-middleware.test.js +148 -0
- package/src/infrastructure/__tests__/admin-script-router.test.js +701 -0
- package/src/infrastructure/admin-auth-middleware.js +49 -0
- package/src/infrastructure/admin-script-router.js +311 -0
- package/src/infrastructure/script-executor-handler.js +75 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Left Hook Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @friggframework/admin-scripts
|
|
3
|
+
*
|
|
4
|
+
* Admin Script Runner for Frigg - Execute maintenance and operational scripts
|
|
5
|
+
* in hosted environments with VPC/KMS secured database connections.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Application Services
|
|
9
|
+
const { ScriptFactory, getScriptFactory, createScriptFactory } = require('./src/application/script-factory');
|
|
10
|
+
const { AdminScriptBase } = require('./src/application/admin-script-base');
|
|
11
|
+
const { AdminFriggCommands, createAdminFriggCommands } = require('./src/application/admin-frigg-commands');
|
|
12
|
+
const { ScriptRunner, createScriptRunner } = require('./src/application/script-runner');
|
|
13
|
+
|
|
14
|
+
// Infrastructure
|
|
15
|
+
const { adminAuthMiddleware } = require('./src/infrastructure/admin-auth-middleware');
|
|
16
|
+
const { router, app, handler: routerHandler } = require('./src/infrastructure/admin-script-router');
|
|
17
|
+
const { handler: executorHandler } = require('./src/infrastructure/script-executor-handler');
|
|
18
|
+
|
|
19
|
+
// Built-in Scripts
|
|
20
|
+
const {
|
|
21
|
+
OAuthTokenRefreshScript,
|
|
22
|
+
IntegrationHealthCheckScript,
|
|
23
|
+
builtinScripts,
|
|
24
|
+
registerBuiltinScripts,
|
|
25
|
+
} = require('./src/builtins');
|
|
26
|
+
|
|
27
|
+
// Adapters
|
|
28
|
+
const { SchedulerAdapter } = require('./src/adapters/scheduler-adapter');
|
|
29
|
+
const { AWSSchedulerAdapter } = require('./src/adapters/aws-scheduler-adapter');
|
|
30
|
+
const { LocalSchedulerAdapter } = require('./src/adapters/local-scheduler-adapter');
|
|
31
|
+
const {
|
|
32
|
+
createSchedulerAdapter,
|
|
33
|
+
detectSchedulerAdapterType,
|
|
34
|
+
} = require('./src/adapters/scheduler-adapter-factory');
|
|
35
|
+
|
|
36
|
+
module.exports = {
|
|
37
|
+
// Application layer
|
|
38
|
+
AdminScriptBase,
|
|
39
|
+
ScriptFactory,
|
|
40
|
+
getScriptFactory,
|
|
41
|
+
createScriptFactory,
|
|
42
|
+
AdminFriggCommands,
|
|
43
|
+
createAdminFriggCommands,
|
|
44
|
+
ScriptRunner,
|
|
45
|
+
createScriptRunner,
|
|
46
|
+
|
|
47
|
+
// Infrastructure layer
|
|
48
|
+
adminAuthMiddleware,
|
|
49
|
+
router,
|
|
50
|
+
app,
|
|
51
|
+
routerHandler,
|
|
52
|
+
executorHandler,
|
|
53
|
+
|
|
54
|
+
// Built-in scripts
|
|
55
|
+
OAuthTokenRefreshScript,
|
|
56
|
+
IntegrationHealthCheckScript,
|
|
57
|
+
builtinScripts,
|
|
58
|
+
registerBuiltinScripts,
|
|
59
|
+
|
|
60
|
+
// Adapters
|
|
61
|
+
SchedulerAdapter,
|
|
62
|
+
AWSSchedulerAdapter,
|
|
63
|
+
LocalSchedulerAdapter,
|
|
64
|
+
createSchedulerAdapter,
|
|
65
|
+
detectSchedulerAdapterType,
|
|
66
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@friggframework/admin-scripts",
|
|
3
|
+
"prettier": "@friggframework/prettier-config",
|
|
4
|
+
"version": "2.0.0--canary.517.41839c5.0",
|
|
5
|
+
"description": "Admin Script Runner for Frigg - Execute maintenance and operational scripts in hosted environments",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@aws-sdk/client-scheduler": "^3.588.0",
|
|
8
|
+
"@friggframework/core": "2.0.0--canary.517.41839c5.0",
|
|
9
|
+
"bcryptjs": "^2.4.3",
|
|
10
|
+
"express": "^4.18.2",
|
|
11
|
+
"lodash": "4.17.21",
|
|
12
|
+
"mongoose": "6.11.6",
|
|
13
|
+
"serverless-http": "^3.2.0",
|
|
14
|
+
"uuid": "^9.0.1"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@friggframework/eslint-config": "2.0.0--canary.517.41839c5.0",
|
|
18
|
+
"@friggframework/prettier-config": "2.0.0--canary.517.41839c5.0",
|
|
19
|
+
"@friggframework/test": "2.0.0--canary.517.41839c5.0",
|
|
20
|
+
"chai": "^4.3.6",
|
|
21
|
+
"eslint": "^8.22.0",
|
|
22
|
+
"jest": "^29.7.0",
|
|
23
|
+
"prettier": "^2.7.1",
|
|
24
|
+
"sinon": "^16.1.1",
|
|
25
|
+
"supertest": "^7.1.4"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"lint:fix": "prettier --write --loglevel error . && eslint . --fix",
|
|
29
|
+
"test": "jest --passWithNoTests"
|
|
30
|
+
},
|
|
31
|
+
"author": "",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"main": "index.js",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/friggframework/frigg.git"
|
|
37
|
+
},
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/friggframework/frigg/issues"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/friggframework/frigg#readme",
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"keywords": [
|
|
46
|
+
"frigg",
|
|
47
|
+
"admin",
|
|
48
|
+
"scripts",
|
|
49
|
+
"maintenance",
|
|
50
|
+
"operations"
|
|
51
|
+
],
|
|
52
|
+
"gitHead": "41839c5520f3b4a0991017021e54596bbb8c01da"
|
|
53
|
+
}
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
const { AWSSchedulerAdapter } = require('../aws-scheduler-adapter');
|
|
2
|
+
const { SchedulerAdapter } = require('../scheduler-adapter');
|
|
3
|
+
|
|
4
|
+
// Mock AWS SDK
|
|
5
|
+
jest.mock('@aws-sdk/client-scheduler', () => {
|
|
6
|
+
const mockSend = jest.fn();
|
|
7
|
+
|
|
8
|
+
return {
|
|
9
|
+
SchedulerClient: jest.fn(() => ({
|
|
10
|
+
send: mockSend,
|
|
11
|
+
})),
|
|
12
|
+
CreateScheduleCommand: jest.fn((params) => ({ _type: 'CreateScheduleCommand', params })),
|
|
13
|
+
DeleteScheduleCommand: jest.fn((params) => ({ _type: 'DeleteScheduleCommand', params })),
|
|
14
|
+
GetScheduleCommand: jest.fn((params) => ({ _type: 'GetScheduleCommand', params })),
|
|
15
|
+
UpdateScheduleCommand: jest.fn((params) => ({ _type: 'UpdateScheduleCommand', params })),
|
|
16
|
+
ListSchedulesCommand: jest.fn((params) => ({ _type: 'ListSchedulesCommand', params })),
|
|
17
|
+
_mockSend: mockSend,
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('AWSSchedulerAdapter', () => {
|
|
22
|
+
let adapter;
|
|
23
|
+
let mockSend;
|
|
24
|
+
let originalEnv;
|
|
25
|
+
|
|
26
|
+
beforeAll(() => {
|
|
27
|
+
originalEnv = { ...process.env };
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
jest.clearAllMocks();
|
|
32
|
+
|
|
33
|
+
// Reset environment variables
|
|
34
|
+
process.env.AWS_REGION = 'us-east-1';
|
|
35
|
+
process.env.SCHEDULE_GROUP_NAME = 'test-schedule-group';
|
|
36
|
+
process.env.SCHEDULER_ROLE_ARN = 'arn:aws:iam::123456789012:role/test-role';
|
|
37
|
+
process.env.ADMIN_SCRIPT_LAMBDA_ARN = 'arn:aws:lambda:us-east-1:123456789012:function:test-executor';
|
|
38
|
+
|
|
39
|
+
const sdk = require('@aws-sdk/client-scheduler');
|
|
40
|
+
mockSend = sdk._mockSend;
|
|
41
|
+
|
|
42
|
+
adapter = new AWSSchedulerAdapter({
|
|
43
|
+
targetLambdaArn: 'arn:aws:lambda:us-east-1:123456789012:function:admin-script-executor',
|
|
44
|
+
scheduleGroupName: 'frigg-admin-scripts',
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
afterAll(() => {
|
|
49
|
+
process.env = originalEnv;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('Inheritance', () => {
|
|
53
|
+
it('should extend SchedulerAdapter', () => {
|
|
54
|
+
expect(adapter).toBeInstanceOf(SchedulerAdapter);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should have correct adapter name', () => {
|
|
58
|
+
expect(adapter.getName()).toBe('aws-eventbridge-scheduler');
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('Constructor', () => {
|
|
63
|
+
it('should use provided configuration', () => {
|
|
64
|
+
const customAdapter = new AWSSchedulerAdapter({
|
|
65
|
+
region: 'eu-west-1',
|
|
66
|
+
targetLambdaArn: 'arn:aws:lambda:eu-west-1:123456789012:function:custom',
|
|
67
|
+
scheduleGroupName: 'custom-group',
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(customAdapter.region).toBe('eu-west-1');
|
|
71
|
+
expect(customAdapter.targetLambdaArn).toBe('arn:aws:lambda:eu-west-1:123456789012:function:custom');
|
|
72
|
+
expect(customAdapter.scheduleGroupName).toBe('custom-group');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should use environment variables as fallback', () => {
|
|
76
|
+
const envAdapter = new AWSSchedulerAdapter();
|
|
77
|
+
|
|
78
|
+
expect(envAdapter.region).toBe('us-east-1');
|
|
79
|
+
expect(envAdapter.targetLambdaArn).toBe('arn:aws:lambda:us-east-1:123456789012:function:test-executor');
|
|
80
|
+
expect(envAdapter.scheduleGroupName).toBe('test-schedule-group');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should use defaults when no config or env vars', () => {
|
|
84
|
+
delete process.env.AWS_REGION;
|
|
85
|
+
delete process.env.SCHEDULE_GROUP_NAME;
|
|
86
|
+
delete process.env.ADMIN_SCRIPT_LAMBDA_ARN;
|
|
87
|
+
|
|
88
|
+
const defaultAdapter = new AWSSchedulerAdapter();
|
|
89
|
+
|
|
90
|
+
expect(defaultAdapter.region).toBe('us-east-1');
|
|
91
|
+
expect(defaultAdapter.scheduleGroupName).toBe('frigg-admin-scripts');
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('createSchedule()', () => {
|
|
96
|
+
it('should create a schedule with required fields', async () => {
|
|
97
|
+
mockSend.mockResolvedValue({
|
|
98
|
+
ScheduleArn: 'arn:aws:scheduler:us-east-1:123456789012:schedule/frigg-admin-scripts/frigg-script-test-script',
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const result = await adapter.createSchedule({
|
|
102
|
+
scriptName: 'test-script',
|
|
103
|
+
cronExpression: 'cron(0 0 * * ? *)',
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
expect(result).toEqual({
|
|
107
|
+
scheduleArn: 'arn:aws:scheduler:us-east-1:123456789012:schedule/frigg-admin-scripts/frigg-script-test-script',
|
|
108
|
+
scheduleName: 'frigg-script-test-script',
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
expect(mockSend).toHaveBeenCalledTimes(1);
|
|
112
|
+
const command = mockSend.mock.calls[0][0];
|
|
113
|
+
expect(command._type).toBe('CreateScheduleCommand');
|
|
114
|
+
expect(command.params.Name).toBe('frigg-script-test-script');
|
|
115
|
+
expect(command.params.ScheduleExpression).toBe('cron(0 0 * * ? *)');
|
|
116
|
+
expect(command.params.ScheduleExpressionTimezone).toBe('UTC');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should create a schedule with all optional fields', async () => {
|
|
120
|
+
mockSend.mockResolvedValue({
|
|
121
|
+
ScheduleArn: 'arn:aws:scheduler:us-east-1:123456789012:schedule/frigg-admin-scripts/frigg-script-test-script',
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
await adapter.createSchedule({
|
|
125
|
+
scriptName: 'test-script',
|
|
126
|
+
cronExpression: 'cron(0 12 * * ? *)',
|
|
127
|
+
timezone: 'America/New_York',
|
|
128
|
+
input: { key: 'value' },
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const command = mockSend.mock.calls[0][0];
|
|
132
|
+
expect(command.params.ScheduleExpressionTimezone).toBe('America/New_York');
|
|
133
|
+
|
|
134
|
+
const targetInput = JSON.parse(command.params.Target.Input);
|
|
135
|
+
expect(targetInput).toEqual({
|
|
136
|
+
scriptName: 'test-script',
|
|
137
|
+
trigger: 'SCHEDULED',
|
|
138
|
+
params: { key: 'value' },
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should configure target with Lambda ARN and role', async () => {
|
|
143
|
+
mockSend.mockResolvedValue({
|
|
144
|
+
ScheduleArn: 'arn:aws:scheduler:us-east-1:123456789012:schedule/frigg-admin-scripts/frigg-script-test-script',
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
await adapter.createSchedule({
|
|
148
|
+
scriptName: 'test-script',
|
|
149
|
+
cronExpression: 'cron(0 0 * * ? *)',
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const command = mockSend.mock.calls[0][0];
|
|
153
|
+
expect(command.params.Target.Arn).toBe('arn:aws:lambda:us-east-1:123456789012:function:admin-script-executor');
|
|
154
|
+
expect(command.params.Target.RoleArn).toBe('arn:aws:iam::123456789012:role/test-role');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('should enable schedule by default', async () => {
|
|
158
|
+
mockSend.mockResolvedValue({
|
|
159
|
+
ScheduleArn: 'arn:aws:scheduler:us-east-1:123456789012:schedule/frigg-admin-scripts/frigg-script-test-script',
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
await adapter.createSchedule({
|
|
163
|
+
scriptName: 'test-script',
|
|
164
|
+
cronExpression: 'cron(0 0 * * ? *)',
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const command = mockSend.mock.calls[0][0];
|
|
168
|
+
expect(command.params.State).toBe('ENABLED');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('should set flexible time window to OFF', async () => {
|
|
172
|
+
mockSend.mockResolvedValue({
|
|
173
|
+
ScheduleArn: 'arn:aws:scheduler:us-east-1:123456789012:schedule/frigg-admin-scripts/frigg-script-test-script',
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
await adapter.createSchedule({
|
|
177
|
+
scriptName: 'test-script',
|
|
178
|
+
cronExpression: 'cron(0 0 * * ? *)',
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const command = mockSend.mock.calls[0][0];
|
|
182
|
+
expect(command.params.FlexibleTimeWindow).toEqual({ Mode: 'OFF' });
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe('deleteSchedule()', () => {
|
|
187
|
+
it('should delete a schedule', async () => {
|
|
188
|
+
mockSend.mockResolvedValue({});
|
|
189
|
+
|
|
190
|
+
await adapter.deleteSchedule('test-script');
|
|
191
|
+
|
|
192
|
+
expect(mockSend).toHaveBeenCalledTimes(1);
|
|
193
|
+
const command = mockSend.mock.calls[0][0];
|
|
194
|
+
expect(command._type).toBe('DeleteScheduleCommand');
|
|
195
|
+
expect(command.params.Name).toBe('frigg-script-test-script');
|
|
196
|
+
expect(command.params.GroupName).toBe('frigg-admin-scripts');
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
describe('setScheduleEnabled()', () => {
|
|
201
|
+
beforeEach(() => {
|
|
202
|
+
// Mock GetScheduleCommand response
|
|
203
|
+
mockSend.mockImplementation((command) => {
|
|
204
|
+
if (command._type === 'GetScheduleCommand') {
|
|
205
|
+
return Promise.resolve({
|
|
206
|
+
Name: 'frigg-script-test-script',
|
|
207
|
+
GroupName: 'frigg-admin-scripts',
|
|
208
|
+
ScheduleExpression: 'cron(0 0 * * ? *)',
|
|
209
|
+
ScheduleExpressionTimezone: 'UTC',
|
|
210
|
+
FlexibleTimeWindow: { Mode: 'OFF' },
|
|
211
|
+
Target: {
|
|
212
|
+
Arn: 'arn:aws:lambda:us-east-1:123456789012:function:admin-script-executor',
|
|
213
|
+
RoleArn: 'arn:aws:iam::123456789012:role/test-role',
|
|
214
|
+
Input: '{"scriptName":"test-script","trigger":"SCHEDULED","params":{}}',
|
|
215
|
+
},
|
|
216
|
+
State: 'ENABLED',
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
return Promise.resolve({});
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('should disable a schedule', async () => {
|
|
224
|
+
await adapter.setScheduleEnabled('test-script', false);
|
|
225
|
+
|
|
226
|
+
expect(mockSend).toHaveBeenCalledTimes(2); // GET then UPDATE
|
|
227
|
+
const updateCommand = mockSend.mock.calls[1][0];
|
|
228
|
+
expect(updateCommand._type).toBe('UpdateScheduleCommand');
|
|
229
|
+
expect(updateCommand.params.State).toBe('DISABLED');
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('should enable a schedule', async () => {
|
|
233
|
+
await adapter.setScheduleEnabled('test-script', true);
|
|
234
|
+
|
|
235
|
+
expect(mockSend).toHaveBeenCalledTimes(2); // GET then UPDATE
|
|
236
|
+
const updateCommand = mockSend.mock.calls[1][0];
|
|
237
|
+
expect(updateCommand._type).toBe('UpdateScheduleCommand');
|
|
238
|
+
expect(updateCommand.params.State).toBe('ENABLED');
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('should preserve schedule configuration when updating state', async () => {
|
|
242
|
+
await adapter.setScheduleEnabled('test-script', false);
|
|
243
|
+
|
|
244
|
+
const updateCommand = mockSend.mock.calls[1][0];
|
|
245
|
+
expect(updateCommand.params.ScheduleExpression).toBe('cron(0 0 * * ? *)');
|
|
246
|
+
expect(updateCommand.params.ScheduleExpressionTimezone).toBe('UTC');
|
|
247
|
+
expect(updateCommand.params.FlexibleTimeWindow).toEqual({ Mode: 'OFF' });
|
|
248
|
+
expect(updateCommand.params.Target).toBeDefined();
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
describe('listSchedules()', () => {
|
|
253
|
+
it('should list all schedules', async () => {
|
|
254
|
+
const mockSchedules = [
|
|
255
|
+
{ Name: 'frigg-script-script-1', State: 'ENABLED' },
|
|
256
|
+
{ Name: 'frigg-script-script-2', State: 'DISABLED' },
|
|
257
|
+
];
|
|
258
|
+
|
|
259
|
+
mockSend.mockResolvedValue({ Schedules: mockSchedules });
|
|
260
|
+
|
|
261
|
+
const result = await adapter.listSchedules();
|
|
262
|
+
|
|
263
|
+
expect(result).toEqual(mockSchedules);
|
|
264
|
+
expect(mockSend).toHaveBeenCalledTimes(1);
|
|
265
|
+
const command = mockSend.mock.calls[0][0];
|
|
266
|
+
expect(command._type).toBe('ListSchedulesCommand');
|
|
267
|
+
expect(command.params.GroupName).toBe('frigg-admin-scripts');
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it('should return empty array when no schedules exist', async () => {
|
|
271
|
+
mockSend.mockResolvedValue({ Schedules: undefined });
|
|
272
|
+
|
|
273
|
+
const result = await adapter.listSchedules();
|
|
274
|
+
|
|
275
|
+
expect(result).toEqual([]);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
describe('getSchedule()', () => {
|
|
280
|
+
it('should get schedule details', async () => {
|
|
281
|
+
const mockSchedule = {
|
|
282
|
+
Name: 'frigg-script-test-script',
|
|
283
|
+
GroupName: 'frigg-admin-scripts',
|
|
284
|
+
ScheduleExpression: 'cron(0 0 * * ? *)',
|
|
285
|
+
ScheduleExpressionTimezone: 'UTC',
|
|
286
|
+
State: 'ENABLED',
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
mockSend.mockResolvedValue(mockSchedule);
|
|
290
|
+
|
|
291
|
+
const result = await adapter.getSchedule('test-script');
|
|
292
|
+
|
|
293
|
+
expect(result).toEqual(mockSchedule);
|
|
294
|
+
expect(mockSend).toHaveBeenCalledTimes(1);
|
|
295
|
+
const command = mockSend.mock.calls[0][0];
|
|
296
|
+
expect(command._type).toBe('GetScheduleCommand');
|
|
297
|
+
expect(command.params.Name).toBe('frigg-script-test-script');
|
|
298
|
+
expect(command.params.GroupName).toBe('frigg-admin-scripts');
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
describe('Lazy SDK loading', () => {
|
|
303
|
+
it('should load AWS SDK on first client access', () => {
|
|
304
|
+
const newAdapter = new AWSSchedulerAdapter({
|
|
305
|
+
targetLambdaArn: 'arn:aws:lambda:us-east-1:123456789012:function:test',
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
expect(newAdapter.scheduler).toBeNull();
|
|
309
|
+
|
|
310
|
+
newAdapter.getSchedulerClient();
|
|
311
|
+
|
|
312
|
+
expect(newAdapter.scheduler).toBeDefined();
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('should reuse client after first creation', () => {
|
|
316
|
+
const client1 = adapter.getSchedulerClient();
|
|
317
|
+
const client2 = adapter.getSchedulerClient();
|
|
318
|
+
|
|
319
|
+
expect(client1).toBe(client2);
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
});
|