-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin-tester.ts
More file actions
281 lines (231 loc) · 9.1 KB
/
Copy pathplugin-tester.ts
File metadata and controls
281 lines (231 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import {
ImportResponseData,
PlanResponseData,
ResourceConfig,
ResourceOperation,
} from '@codifycli/schemas';
import chalk from 'chalk';
import unionBy from 'lodash.unionby';
import { PluginProcess } from './plugin-process.js';
import { getPlatformOs, splitUserConfig } from './utils.js';
export class PluginTester {
static async fullTest(
pluginPath: string,
configs: ResourceConfig[],
options?: {
skipUninstall?: boolean,
skipImport?: boolean,
validatePlan?: (plans: PlanResponseData[]) => Promise<void> | void
validateApply?: (plans: PlanResponseData[]) => Promise<void> | void,
validateDestroy?: (plans: PlanResponseData[]) => Promise<void> | void,
validateImport?: (importResults: (ImportResponseData['result'][0])[]) => Promise<void> | void,
testModify?: {
modifiedConfigs: ResourceConfig[],
validateModify?: (plans: PlanResponseData[]) => Promise<void> | void,
}
}): Promise<void> {
configs = configs.filter((c) => !c.os || c.os.includes(getPlatformOs()));
const ids = configs
.map((c) => `${c.type}${c.name ? `.${c.name}` : ''}`)
.join(', ')
console.info(chalk.cyan(`Starting full test of [ ${ids} ]...`));
const {
skipUninstall = false,
} = options ?? {}
const plugin = new PluginProcess(pluginPath);
try {
console.info(chalk.cyan('Testing initialization...'))
const initializeResult = await plugin.initialize();
const unsupportedConfigs = configs.filter((c) =>
!initializeResult.resourceDefinitions.some((rd) => rd.type === c.type)
)
if (unsupportedConfigs.length > 0) {
throw new Error(`The plugin does not support the following configs supplied:\n ${JSON.stringify(unsupportedConfigs, null, 2)}\n Initialize result: ${JSON.stringify(initializeResult)}`)
}
// configs = configs.filter((c) => initializeResult.resourceDefinitions.find((rd) => rd.type === c.type)?.operatingSystems?.includes(os.platform() as OS));
console.info(chalk.cyan('Testing validate...'))
const validate = await plugin.validate({
configs: configs.map((c) => {
const { coreParameters, parameters } = splitUserConfig(c)
return { core: coreParameters, parameters };
})
});
const invalidConfigs = validate.resourceValidations.filter((v) => !v.isValid)
if (invalidConfigs.length > 0) {
throw new Error(`The following configs did not validate:\n ${JSON.stringify(invalidConfigs, null, 2)}`)
}
console.info(chalk.cyan('Testing plan...'))
const plans = [];
for (const config of configs) {
const { coreParameters, parameters } = splitUserConfig(config);
plans.push(await plugin.plan({
core: coreParameters,
desired: parameters,
isStateful: false,
state: undefined,
}));
}
if (options?.validatePlan) {
await options.validatePlan(plans);
}
console.info(chalk.cyan('Testing apply...'))
for (const plan of plans) {
await plugin.apply({
planId: plan.planId
});
}
if (options?.validateApply) {
await options.validateApply(plans);
}
} finally {
plugin.kill();
}
if (!options?.skipImport) {
const importPlugin = new PluginProcess(pluginPath);
try {
await importPlugin.initialize();
console.info(chalk.cyan('Testing import...'))
const importResults = [];
for (const config of configs) {
const { coreParameters, parameters } = splitUserConfig(config);
const importResult = await importPlugin.import({ core: coreParameters, parameters })
importResults.push(importResult);
}
if (options?.validateImport) {
await options.validateImport(importResults.map((r) => r.result[0]));
}
} finally {
importPlugin.kill();
}
}
if (options?.testModify) {
const modifyPlugin = new PluginProcess(pluginPath);
try {
await modifyPlugin.initialize();
console.info(chalk.cyan('Testing modify...'))
const modifyPlans = [];
for (const config of options.testModify.modifiedConfigs) {
const { coreParameters, parameters } = splitUserConfig(config);
modifyPlans.push(await modifyPlugin.plan({
core: coreParameters,
desired: parameters,
isStateful: false,
state: undefined,
}));
}
if (modifyPlans.some((p) => p.operation !== ResourceOperation.MODIFY)) {
throw new Error(`Error while testing modify. Non-modify results were found in the plan:
${JSON.stringify(modifyPlans, null, 2)}`)
}
for (const plan of modifyPlans) {
await modifyPlugin.apply({
planId: plan.planId
});
}
if (options.testModify.validateModify) {
await options.testModify.validateModify(modifyPlans);
}
} finally {
modifyPlugin.kill();
}
}
if (!skipUninstall) {
// We need to add unique names to multiple configs with the same type or else it breaks the unionBy below.
const configsWithNames = this.addNamesToConfigs(configs);
const modifiedConfigs = this.addNamesToConfigs(options?.testModify?.modifiedConfigs ?? [])
const id = (config: ResourceConfig) => config.type + (config.name ? `.${config.name}` : '')
const configsToDestroy = unionBy(modifiedConfigs, configsWithNames, id);
await this.uninstall(pluginPath, configsToDestroy.toReversed(), options);
}
}
static async install(pluginPath: string, configs: ResourceConfig[]) {
const plugin = new PluginProcess(pluginPath);
try {
console.info(chalk.cyan('Testing initialization...'))
const initializeResult = await plugin.initialize();
const unsupportedConfigs = configs.filter((c) =>
!initializeResult.resourceDefinitions.some((rd) => rd.type === c.type)
)
if (unsupportedConfigs.length > 0) {
throw new Error(`The plugin does not support the following configs supplied:\n ${JSON.stringify(unsupportedConfigs, null, 2)}\n Initialize result: ${JSON.stringify(initializeResult)}`)
}
// configs = configs.filter((c) => initializeResult.resourceDefinitions.find((rd) => rd.type === c.type)?.operatingSystems?.includes(os.platform() as OS));
console.info(chalk.cyan('Testing validate...'))
const validate = await plugin.validate({
configs: configs.map((c) => {
const { coreParameters, parameters } = splitUserConfig(c)
return { core: coreParameters, parameters };
})
});
const invalidConfigs = validate.resourceValidations.filter((v) => !v.isValid)
if (invalidConfigs.length > 0) {
throw new Error(`The following configs did not validate:\n ${JSON.stringify(invalidConfigs, null, 2)}`)
}
console.info(chalk.cyan('Testing plan...'))
const plans = [];
for (const config of configs) {
const { coreParameters, parameters } = splitUserConfig(config);
plans.push(await plugin.plan({
core: coreParameters,
desired: parameters,
isStateful: false,
state: undefined,
}));
}
console.info(chalk.cyan('Testing apply...'))
for (const plan of plans) {
await plugin.apply({
planId: plan.planId
});
}
} finally {
plugin.kill();
}
}
static async uninstall(pluginPath: string, configs: ResourceConfig[], options?: {
validateDestroy?: (plans: PlanResponseData[]) => Promise<void> | void
}) {
const destroyPlugin = new PluginProcess(pluginPath);
try {
await destroyPlugin.initialize();
console.info(chalk.cyan('Testing destroy...'))
const plans = [];
for (const config of configs) {
const { coreParameters, parameters } = splitUserConfig(config);
plans.push(await destroyPlugin.plan({
core: coreParameters,
isStateful: true,
state: parameters,
desired: undefined
}))
}
for (const plan of plans) {
if (plan.operation !== ResourceOperation.DESTROY && plan.operation !== ResourceOperation.NOOP) {
throw new Error(`Expect resource operation to be 'destroy' but instead received plan: \n ${JSON.stringify(plans, null, 2)}`)
}
await destroyPlugin.apply({
planId: plan.planId
});
}
if (options?.validateDestroy) {
await options.validateDestroy(plans);
}
} finally {
destroyPlugin.kill();
}
}
private static addNamesToConfigs(configs: ResourceConfig[]): ResourceConfig[] {
const configsWithNames = new Array<ResourceConfig>();
const typeSet = new Set(configs.map((c) => c.type));
for (const type of typeSet) {
const sameTypeConfigs = configs.filter((c) => c.type === type);
if (sameTypeConfigs.length > 1) {
sameTypeConfigs.forEach((c, idx) => {
c.name = c.name ?? idx.toString()
});
}
configsWithNames.push(...sameTypeConfigs);
}
return configsWithNames;
}
}