-
Notifications
You must be signed in to change notification settings - Fork 5
/
dingo_test.js
259 lines (227 loc) · 9.41 KB
/
dingo_test.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
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
// dingo_test.js
//
// Class for encapsulating test information.
"use strict";
var fs = require('fs');
var dingoJobTypes = require('./dingo_job_types');
var dingoUtils = require('./dingo_utils');
var DingoJob = require('./dingo_job').DingoJob;
var DingoJobPipeline = require('./dingo_job_pipeline').DingoJobPipeline;
const DingoTest = class DingoTest {
constructor(tenantId, subscriptionId, clientId, clientSecret, duration, delay, randomOrder) {
this.tenantId = tenantId;
this.subscriptionId = subscriptionId;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.testDuration = 0;
this.testDelay = 0;
this.testRandomOrder = false;
if(duration) {
this.testDuration = duration;
}
if(delay) {
this.testDelay = delay;
}
if(randomOrder) {
this.testRandomOrder = true;
}
this.jobs = [];
}
}
// Add a job to the test.
DingoTest.prototype.addJob = function(job) {
this.jobs.push(job);
}
// Display test details
DingoTest.prototype.dumpTest = function() {
console.log("Test defined:");
console.log(" Tenant Id: " + this.tenantId);
console.log(" Subcription Id: " + this.subscriptionId);
console.log(" Client Id: " + this.clientId);
console.log(" Client Secret: " + this.clientSecret);
console.log("");
console.log("Global properties:");
console.log(" Test duration: " + this.testDuration);
console.log(" Test delay: " + this.testDelay);
console.log(" Random Order: " + this.testRandomOrder);
console.log("");
console.log("Jobs:");
for(var i = 0; i < this.jobs.length; i++) {
this.jobs[i].dumpJob(4);
}
}
// Parse the command line and create the test.
DingoTest.generateTest = function(args) {
if(!args) {
throw new Error('Unable to generate test. No arguments supplied.');
}
// command line overrides settings in a test file
var tenantId = args.tenant;
var subscriptionId = args.subscription;
var clientId = args.client;
var clientSecret = args.password;
var testDuration = dingoUtils.parse_argument('int-range', args.testduration, 60);
var testDelay = dingoUtils.parse_argument('int-range', args.testdelay, 60);
var randomOrder = args.testrandom;
var jobs = [];
if(args.testfile) {
if(!args.testfile) {
throw new Error('Unable to generate test. No test file specified.');
}
var testJson;
try {
var testfile = fs.readFileSync(args.testfile);
testJson = JSON.parse(testfile);
} catch(ex) {
throw new Error('Unable to generate test. Failure reading test file. File: ' + args.testfile + ', Error: ' + ex);
}
// validate json content, if items missing or if a command line option exists, that takes precedence
if(!tenantId) { tenantId = testJson.tenantId; }
if(!subscriptionId) { subscriptionId = testJson.subscriptionId; }
if(!clientId) { clientId = testJson.clientId; }
if(!clientSecret) { clientSecret = testJson.clientSecret; }
// Flags for how to run test jobs. Default is run each test once.
if(!args.testduration && testJson.testDuration) {
testDuration = dingoUtils.parse_argument('int-range', testJson.testDuration, 0);
}
if(!args.testdelay && testJson.testDelay) {
testDelay = dingoUtils.parse_argument('int-range', testJson.testDelay, 0);
}
if(!randomOrder) { randomOrder = testJson.randomOrder ? testJson.randomOrder : false; }
if((typeof testJson.jobs != 'object') && !(testJson.jobs instanceof Array)) {
throw new Error('\'jobs\' section of test file required and conform to the valid format.');
}
var jobList = testJson.jobs;
for(var i = 0; i < jobList.length; i++) {
var j = jobList[i];
jobs.push({
type: j.type,
operation: j.operation,
resourceGroup: (args.resourcegrp ? args.resourcegrp : j.resourceGroup),
resource: (args.resource ? (args.resource == '*' ? null : args.resource) : (j.resource == "*" ? null : j.resource)),
resourceMatch: (args.resourcematch ? args.resourcematch : j.resourceMatch),
randomResource: (args.randomresource ? true : (j.resource == '*' ? true : false)),
duration: dingoUtils.parse_argument('int-range', (args.duration ? args.duration : (j.duration ? j.duration : 60)), 60)
});
}
} else {
// job arguments
var type = args.resourcetype;
var operation = args.operation;
var resourceGroup = args.resourcegrp;
var resource = (args.resource == '*' ? null : args.resource);
var resourceMatch = args.resourcematch;
var randomResource = (args.randomresource ? true : (resource == null ? true : false));
var duration = dingoUtils.parse_argument('int-range', args.duration, 60);
if(!type || !operation || !resourceGroup || (!resource && !resourceMatch )) {
var msg = 'One or more equired parameters were not specified. You must provide:\n'
if (!type) {
msg = msg + '\tResource Type (-v, --resource-type';
}
if (!operation) {
msg = msg + '\tOperation (-o, --operation';
}
if (!resourceGroup) {
msg = msg + '\tResource Group (-g, --resourcegrp';
}
if (!resource && !resourceMatch ) {
msg = msg + '\tEither a resource name (-r, --resource) or a regular expression to match resources (-m, --resourcematch)';
}
console.log(msg);
process.exit(1)
}
jobs.push({
type: type,
operation: operation,
resourceGroup: resourceGroup,
resource: resource,
resourceMatch: resourceMatch,
randomResource: randomResource,
duration: duration
});
}
if(!tenantId) { throw new Error('Required parameter \'tenantId\' not specified.'); }
if(!subscriptionId) { throw new Error('Required parameter \'subscriptionId\' not specified.'); }
if(!clientId) { throw new Error('Required parameter \'clientId\' not specified.'); }
if(!clientSecret) { throw new Error('Required parameter \'clientSecret\' not specified.'); }
if(jobs.length == 0) {
throw new Error('No jobs specified.');
}
var test = new DingoTest(tenantId, subscriptionId, clientId, clientSecret, testDuration, testDelay, randomOrder);
for(var i = 0; i < jobs.length; i++) {
var j = jobs[i];
var job = DingoJob.generateJob(j.type, j.operation, j.resourceGroup, j.resource, j.resourceMatch, j.randomResource, j.duration);
test.addJob(job);
}
test.dumpTest();
return test;
}
function randomArrayOfN(n) {
var result = [];
while(result.length < n) {
var r = Math.floor(Math.random() * n);
var found = false;
for(var i = 0; i < result.length; i++) {
if(r == result[i]) {
found = true;
break;
}
}
if(!found) {
result.push(r);
}
}
return result;
}
DingoTest.processJob = function(manager, jobSequence, startTime, currentIndex) {
var pipeline = new DingoJobPipeline(manager.tenantId,
manager.subscriptionId,
manager.clientId,
manager.clientSecret,
manager.jobs[jobSequence[currentIndex]])
pipeline.go(function(err, result) {
if(err) {
throw new Error('Attempt to process job failed. Error: ' + err);
} else {
if(manager.testDuration) {
var now = Date.now();
if(now - startTime >= (manager.testDuration * 1000)) {
console.log('Total test time exceeded. Shutting down.');
process.exit(0);
}
} else if(currentIndex + 1 >= jobSequence.length) {
console.log('All tests run.');
process.exit(0);
}
if(manager.jobs[jobSequence[currentIndex]].randomOperation) {
var updatedJob = manager.jobs[jobSequence[currentIndex]].regenerateJob();
manager.jobs[jobSequence[currentIndex]] = updatedJob;
}
if(currentIndex + 1 == jobSequence.length) {
if(manager.testRandomOrder) {
jobSequence = randomArrayOfN(manager.jobs.length);
}
currentIndex = 0;
} else {
currentIndex++;
}
setTimeout(function() {
DingoTest.processJob(manager, jobSequence, startTime, currentIndex)
}, manager.testDelay * 1000);
}
});
}
DingoTest.prototype.go = function() {
var manager = this;
var startTime = Date.now();
// test details
var jobSequence;
if(manager.testRandomOrder) {
jobSequence = randomArrayOfN(manager.jobs.length);
} else {
jobSequence = [];
for(var i = 0; i < manager.jobs.length; i++) { jobSequence.push(i); }
}
DingoTest.processJob(manager, jobSequence, startTime, 0);
}
module.exports = { DingoTest };