-
Notifications
You must be signed in to change notification settings - Fork 16
/
cli.js
executable file
·194 lines (172 loc) · 5.05 KB
/
cli.js
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
#!/usr/bin/env node
var fs = require( 'fs' ),
path = require( 'path' ),
program = require( 'commander' ),
spawn = require( 'child_process' ).spawn,
tsbs = require( './testswarm-browserstack' ),
util = require( './util' ),
cliConfig,
config,
child;
function loadAndParseConfigFile( filePath ) {
return ( filePath && fs.existsSync( filePath ) ) ?
JSON.parse( fs.readFileSync( path.resolve( filePath ), 'utf-8' ) ) :
false;
}
function confContains( conf, props, prefix ) {
for ( var i = 0; i < props.length; i += 1 ) {
if ( !conf || conf[ props[ i ] ] === undefined ) {
console.log(
'Configuration file invalid or missing required parameter:',
( prefix || '' ) + props[ i ]
);
return false;
}
}
return true;
}
function runLoop() {
if ( config.verbose ) {
console.log( 'Starting run loop...' );
}
child = spawn( 'node', [
__filename,
'--run',
'--config', program.config,
program.verbose ? '--verbose' : '',
program.dryRun ? '--dry-run' : ''
] );
child.stdout.on( 'data', function( data ) {
process.stdout.write( String( data ) );
} );
child.stderr.on( 'data', function( data ) {
util.log.fatal( String( data ).trim() );
} );
child.on( 'exit', function() {
if ( config.verbose ) {
console.log( 'Next iteration in ' + program.runLoop + ' seconds...' );
}
setTimeout( runLoop, program.runLoop * 1000 );
} );
}
program
.version( loadAndParseConfigFile( __dirname + '/../package.json' ).version )
.option( '--config [path]',
'path to config file with options (defaults to ./config.json)',
'./config.json'
)
.option( '--run',
'Retrieve TestSwarm state and spawn/terminate BrowserStack workers as needed'
)
.option( '--run-loop <timeout>',
'Execute --run in a non-overlapping loop with set timeout (in seconds) between iterations',
Number
)
.option( '--ua2bs <id>',
'Get BrowserStack worker template from TestSwarm useragent id ("*" to show all)'
)
.option( '--listWorkers', 'List all active BrowserStack workers' )
.option( '--worker <id>', 'Get info about a specific BrowserStack worker', Number )
.option( '--terminate <id>', 'Terminate a specific BrowserStack worker', Number )
.option( '--terminateAll', 'Terminate all BrowserStack workers' )
.option( '--spawn <uaId>',
'Spwawn a BrowserStack worker by swarm useragent id (joining the swarm)'
)
.option( '-v, --verbose', 'Output debug information (through console.log)' )
.option( '--dry, --dry-run', 'Simulate spawning and termination of browserstack workers' )
.parse( process.argv );
if ( !program.run && !program.runLoop && !program.ua2bs && !program.listWorkers &&
!program.worker && !program.terminate && !program.terminateAll && !program.spawn
) {
console.log( program.helpInformation() );
return;
}
// Process configuration
cliConfig = loadAndParseConfigFile( program.config );
if ( !cliConfig ) {
console.error(
'Configuration file missing or invalid.\n' +
'Specify the file path in the --config parameter ' +
'(defaults to ./config.json, use config-sample.json as template).'
);
process.exit( 1 );
}
tsbs.extendConfig( cliConfig );
tsbs.init( function( _err, tsbs ) {
config = tsbs.getConfig();
if ( program.verbose ) {
config.verbose = true;
}
if ( program.dryRun ) {
config.browserstack.dryRun = true;
}
if ( !confContains( config.browserstack, [ 'user', 'pass' ], 'browserstack.' ) ) {
process.exit( 1 );
}
// Execute actions
if ( program.ua2bs === '*' ) {
tsbs.getMap( function( err, map ) {
console.log( '[getMap]', err || map.uaID2Browser );
} );
} else if ( program.ua2bs ) {
console.log(
'[getBrowserFromUaID] ' + program.ua2bs,
tsbs.getBrowserFromUaID( program.ua2bs ) || false
);
}
if ( program.worker ) {
tsbs.browserstack.getWorker( program.worker, function( err, worker ) {
if ( err ) {
console.error( 'Could not get worker info from browserstack', err );
return;
}
if ( !worker ) {
console.log( 'No worker info available' );
} else {
console.log( 'Worker #' + program.worker + ':\n', worker );
}
} );
}
if ( program.listWorkers ) {
tsbs.browserstack.getWorkers( function( err, workers ) {
if ( err ) {
console.error( 'Could not get worker list from browserstack', err );
return;
}
if ( !workers || workers.length < 1 ) {
console.log( 'No workers running or queued' );
} else {
console.log( workers );
}
} );
}
if ( program.terminateAll ) {
tsbs.browserstack.terminateAll( function( err, workers ) {
if ( err ) {
console.error( 'Could not get workers from browserstack' );
}
if ( !workers || workers.length < 1 ) {
console.log( 'No workers running or queued' );
}
} );
} else if ( program.terminate ) {
tsbs.browserstack.terminateWorker( { id: program.terminate } );
}
if ( program.spawn ) {
tsbs.spawnWorkerByUa( program.spawn, function( err ) {
if ( err ) {
console.error( err );
}
} );
}
if ( program.run || program.runLoop ) {
if ( !confContains( config.testswarm, [ 'root', 'runUrl' ], 'testswarm.' ) ) {
process.exit( 1 );
}
if ( program.runLoop ) {
runLoop();
} else {
tsbs.run();
}
}
} );