-
Notifications
You must be signed in to change notification settings - Fork 36
/
cli.ts
275 lines (265 loc) · 8.32 KB
/
cli.ts
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
#!/usr/bin/env node
/**
* MIT License
*
* Copyright (c) 2020-present, Elastic NV
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*
*/
import { program, Option } from 'commander';
import { cwd } from 'process';
import { CliArgs, PushOptions } from './common_types';
import { reporters } from './reporters';
import {
normalizeOptions,
parseThrottling,
getCommonCommandOpts,
} from './options';
import { globalSetup } from './loader';
import { run } from './';
import { runner } from './core';
import { SyntheticsLocations } from './dsl/monitor';
import { push, loadSettings, validatePush, warnIfThrottled } from './push';
import {
formatLocations,
getLocations,
renderLocations,
LocationCmdOptions,
} from './locations';
import { resolve } from 'path';
import { Generator } from './generator';
import { error } from './helpers';
import { LocationsMap } from './locations/public-locations';
import { createLightweightMonitors } from './push/monitor';
import { getVersion } from './push/kibana_api';
import { installTransform } from './core/transform';
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const { name, version } = require('../package.json');
const {
params,
pattern,
playwrightOpts,
auth,
authMandatory,
configOpt,
tags,
match,
} = getCommonCommandOpts();
program
.name(`npx ${name}`)
.usage('[options] [dir] [files] file')
.addOption(configOpt)
.addOption(pattern)
.addOption(tags)
.addOption(match)
.addOption(params)
.addOption(
new Option('--reporter <value>', `output reporter format`).choices(
Object.keys(reporters)
)
)
.option('--inline', 'read journeys from stdin instead of reading from files')
.option('-r, --require <modules...>', 'module(s) to preload')
.option('--sandbox', 'enable chromium sand-boxing')
.option(
'--rich-events',
'preset flag used when running monitors directly via Heartbeat'
)
.option('--no-headless', 'run with the browser in headful mode')
.option(
'--capability <features...>',
'Enable capabilities through feature flags'
)
.addOption(
new Option(
'--screenshots [flag]',
'Control whether to capture screenshots at the end of each step'
).choices(['on', 'off', 'only-on-failure'])
)
.option(
'--dry-run',
"don't actually execute anything, report only registered journeys"
)
.option(
'--outfd <fd>',
'specify a file descriptor for logs. Default is stdout',
parseInt
)
.option(
'--ws-endpoint <endpoint>',
'Browser WebSocket endpoint to connect to'
)
.option(
'--pause-on-error',
'pause on error until a keypress is made in the console. Useful during development'
)
.option(
'--ignore-https-errors',
'ignores any HTTPS errors in sites being tested, including ones related to unrecognized certs or signatures. This can be insecure!'
)
.option(
'--quiet-exit-code',
'always return 0 as an exit code status, regardless of test pass / fail. Only return > 0 exit codes on internal errors where the suite could not be run'
)
.option(
'--throttling <config>',
'JSON object to throttle network conditions for download and upload throughput in megabits/second and latency in milliseconds. Ex: { "download": 10, "upload": 5, "latency": 200 }.',
parseThrottling
)
.option(
'--no-throttling',
'Turns off default network throttling.',
parseThrottling
)
.addOption(playwrightOpts)
.version(version)
.description('Run synthetic tests')
.action(async (cliArgs: CliArgs) => {
const tearDown = await globalSetup(cliArgs, program.args);
try {
const options = await normalizeOptions(cliArgs, 'run');
const results = await run(options);
/**
* Exit with error status if any journey fails
*/
if (!options.quietExitCode) {
for (const result of Object.values(results)) {
if (result.status === 'failed') {
process.exit(1);
}
}
}
} catch (e) {
console.error(e);
process.exit(1);
} finally {
tearDown();
}
});
// Push command
program
.command('push')
.description(
'Push all journeys in the current directory to create monitors within the Kibana monitor management UI'
)
.addOption(authMandatory)
.option(
'--schedule <time-in-minutes>',
"schedule in minutes for the pushed monitors. Setting `10`, for example, configures monitors which don't have an interval defined to run every 10 minutes.",
parseInt
)
.addOption(
new Option(
'--locations <locations...>',
'default list of locations from which your monitors will run.'
).choices(SyntheticsLocations)
)
.option(
'--private-locations <locations...>',
'default list of private locations from which your monitors will run.'
)
.option('--url <url>', 'Kibana URL to upload the project monitors')
.option(
'--id <id>',
'project id that will be used for logically grouping monitors'
)
.option(
'--space <space>',
'the target Kibana spaces for the pushed monitors — spaces help you organise pushed monitors.'
)
.option('-y, --yes', 'skip all questions and run non-interactively')
.addOption(pattern)
.addOption(tags)
.addOption(match)
.addOption(params)
.addOption(playwrightOpts)
.addOption(configOpt)
.action(async cmdOpts => {
cmdOpts = { ...cmdOpts, ...program.opts() };
const workDir = cwd();
const tearDown = await globalSetup({ inline: false, ...cmdOpts }, [
workDir,
]);
try {
const settings = await loadSettings(cmdOpts.config);
const options = (await normalizeOptions(
{
...settings,
...cmdOpts,
},
'push'
)) as PushOptions;
await validatePush(options, settings);
const monitors = runner.buildMonitors(options);
if ((options as CliArgs).throttling == null) {
warnIfThrottled(monitors);
}
options.kibanaVersion = await getVersion(options);
monitors.push(...(await createLightweightMonitors(workDir, options)));
await push(monitors, options);
} catch (e) {
e && console.error(e);
process.exit(1);
} finally {
tearDown();
}
});
// Init command
program
.command('init [dir]')
.description('Initialize Elastic synthetics project')
.action(async (dir = '') => {
try {
const generator = await new Generator(resolve(process.cwd(), dir));
await generator.setup();
} catch (e) {
e && error(e);
process.exit(1);
}
});
// Locations command
program
.command('locations')
.description(
`List all locations to run the synthetics monitors. Pass optional '--url' and '--auth' to list private locations.`
)
.option('--url <url>', 'Kibana URL to fetch all public and private locations')
.addOption(auth)
.action(async (cmdOpts: LocationCmdOptions) => {
const revert = installTransform();
const url = cmdOpts.url ?? (await loadSettings(null, true))?.url;
try {
if (url && cmdOpts.auth) {
const allLocations = await getLocations({
url,
auth: cmdOpts.auth,
});
renderLocations(formatLocations(allLocations));
} else {
renderLocations({ publicLocations: Object.keys(LocationsMap) });
}
} catch (e) {
e && error(e);
process.exit(1);
} finally {
revert();
}
});
program.parse(process.argv);