-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker-mocha-run.js
executable file
·726 lines (635 loc) · 21.8 KB
/
docker-mocha-run.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#!/usr/bin/env node
const path = require("path");
const async = require("async");
const fs = require("fs");
const yaml = require("js-yaml");
const DockerMocha = require("./src/DockerMocha");
const DockerManager = require("./src/DockerManager");
const Utils = require("./src/utils");
const Queue = require('better-queue');
const ExportToCsv = require('export-to-csv').ExportToCsv;
const Mocha = require('mocha');
const defaultFileName = "tests.json";
const defaultFile = path.join(process.cwd(), defaultFileName);
let overrideFile = null;
let composeFile = null;
let entrypoint = null;
const vanillaString = "";
const SLEEP = 100;
const MAX_TIMEOUT = 600000;
const MAX_RETRIES = 0;
const DEFAULT_THREAD = 4;
let threadsNumber = DEFAULT_THREAD;
let defaultExists = false;
let overrideExists = false;
let testFile = false;
let setupFile = false;
const dockerMocha = new DockerMocha();
let passedTests = 0;
let failedTests = 0;
let failedTestsArray = [];
let passedTestsArray = [];
let startTime;
let finishTime;
let queue;
const csvOptions = {
fieldSeparator: ',',
quoteStrings: '"',
decimalSeparator: '.',
showLabels: true,
showTitle: false,
// title: 'Docker-Mocha Details',
useTextFile: false,
useBom: true,
useKeysAsHeaders: true,
};
const csvExporter = new ExportToCsv(csvOptions);
let data = [];
const timeStamp = Utils.getTimeStamp();
const logFileName = "docker-mocha_" + timeStamp + ".log";
for(let i in process.argv)
{
//Check if file flag exists
if(process.argv[i] === "-f" || process.argv[i] === "--file")
{
if((Number(i) + 1) < process.argv.length)
{
overrideFile = path.join(process.cwd(), process.argv[Number(i) + 1]);
}
}
//Check if threads flag exists
if(process.argv[i] === "-t" || process.argv[i] === "--threads")
{
if((Number(i) + 1) < process.argv.length && Number.isInteger(Number(process.argv[Number(i) + 1])))
{
threadsNumber = Number(process.argv[Number(i) + 1])
}
}
//Check if alternate docker compose exists
if(process.argv[i] === "-c" || process.argv[i] === "--compose")
{
if((Number(i) + 1) < process.argv.length)
{
composeFile = path.join(process.cwd(), process.argv[Number(i) + 1]);
}
}
//Check if test entrypoint specified
if(process.argv[i] === "-e" || process.argv[i] === "--entrypoint")
{
if((Number(i) + 1) < process.argv.length)
{
entrypoint = process.argv[Number(i) + 1];
}
}
//check if port
if(process.argv[i] === "-p" || process.argv[i] === "--port")
{
if((Number(i) + 1) < process.argv.length)
{
let port = parseInt(process.argv[Number(i) + 1]);
if(!isNaN(port))
dockerMocha.port = port;
}
}
if(process.argv[i] === "--config")
{
if((Number(i) + 1) < process.argv.length)
{
dockerMocha.deployment_config = process.argv[Number(i) + 1];
}
}
if(process.argv[i] === "--no-checkpoint")
{
dockerMocha.noCheckpoint = true;
}
if(process.argv[i] === "--no-delete")
{
dockerMocha.noDelete = true;
}
if(process.argv[i] === "--no-docker")
{
dockerMocha.noDocker = true;
}
if(process.argv[i] === "--testFile")
{
testFile = process.argv[Number(i) + 1];
}
if(process.argv[i] === "--setupFile")
{
setupFile = process.argv[Number(i) + 1];
}
}
if(!setupFile && !testFile) // manager
{
// console.log("NEM NADA Setup and test file");
try
{
//verify if the other *.json tests file exists
if(overrideFile != null && fs.existsSync(overrideFile))
overrideExists = true;
}
catch(err) {
overrideExists = false;}
try
{
//verify if the default *.json tests file exists
if(defaultFile != null && fs.existsSync(defaultFile))
defaultExists = true;
}
catch(err)
{
defaultExists = false;}
try
{
//verify if compose file exists
if(composeFile != null)
fs.existsSync(composeFile)
}
catch (err)
{
console.warn("WARN: Specified Compose file not found, using default");
composeFile = null;
}
if(!defaultExists && !overrideExists)
{
console.error("ERROR: No Default file or Override file found");
process.exit(1);
}
else
{
let fileToUse = null;
if(!overrideExists)
{
fileToUse = defaultFile;
console.warn("WARN: Override file not found using default File");
}
else
{
fileToUse = overrideFile;
console.info("INFO: Using override file: " + overrideFile);
}
//Loading compose File
if(Utils.isNull(composeFile))
{
try
{
let composeContents = yaml.safeLoad(fs.readFileSync("docker-compose.yml"));
composeFile = path.join(process.cwd(), "docker-compose.yml");
dockerMocha.addComposeFile(composeFile, composeContents);
console.info("INFO: Using default docker-compose.yml");
}
catch (e)
{
console.error("ERROR: No default docker-compose.yml");
process.exit(1);
}
}
else
{
try
{
let composeContents = yaml.safeLoad(fs.readFileSync(composeFile));
dockerMocha.addComposeFile(composeFile, composeContents);
console.info("INFO: Using specified compose file: " + composeFile);
}
catch (e)
{
console.error("ERROR: Could not load file: " + composeFile);
process.exit(1);
}
}
if(Utils.isNull(entrypoint))
{
try
{
const packageJson = JSON.parse(fs.readFileSync(path.join(process.cwd(), "package.json"), 'utf8'));
entrypoint = packageJson.name;
dockerMocha.entrypoint = entrypoint;
}
catch (e) {
console.error("ERROR: Unable to retrieve entrypoint service, please provide a package json or specify with -e flag");
process.exit(1);
}
}
else
dockerMocha.entrypoint = entrypoint;
//Informing threads number
console.info("INFO: Using " + threadsNumber + " thread(s)");
//Parsing tests from *.json tests file
const testsFile = JSON.parse(fs.readFileSync(fileToUse, 'utf8'));
const tests = testsFile["tests"];
const states = testsFile["states"];
if(Utils.isNull(tests))
{
console.error("ERROR: no section 'tests' specified in the test file");
process.exit(1);
}
else if(Utils.isNull(states))
{
console.error("ERROR: no section 'states' specified in the test file");
process.exit(1);
}
//Parsing states
dockerMocha.statesMap = states;
let rootTests = 0;
for(let state in states)
{
if (states.hasOwnProperty(state))
{
if(Utils.isNull(states[state]["depends_on"]))
{
dockerMocha.rootState = state;
rootTests++;
}
dockerMocha.addState(state);
}
}
if(rootTests !== 1)
{
console.error("ERROR: Multiple root states detected, only one is allowed: " + rootTests);
process.exit(1);
}
dockerMocha.verifyAndWarnStates();
//Parsing tests
//For each test verify if the test file and setup file exist. Add to docker mocha if positive
let totalTests = 0;
let badTests = 0;
for(let test in tests)
{
if(tests.hasOwnProperty(test))
{
if(!Utils.isNull(tests[test]["state"]) && dockerMocha.statesMap.hasOwnProperty(tests[test]["state"]))
{
dockerMocha.addTest(test, tests[test]);
}
else
{
console.warn("WARN: Ignored: " + JSON.stringify(tests[test]));
badTests++;
}
}
totalTests++;
}
if(badTests > 0)
{
console.warn("WARN: Some tests have been ignored: " + badTests);
console.log("INFO: Added " + (totalTests-badTests) + " tests");
}
else
{
console.log("INFO: All tests added: " + (totalTests));
}
//Initialize DockerManager
DockerManager();
startTime = new Date();
if(dockerMocha.noDelete)
{
manager();
}
else if(dockerMocha.noDocker)
{
require('./src/NoDocker').noDocker(dockerMocha);
}
else
{
DockerManager.removeAllVolumes(() =>
{
DockerManager.deleteAllStates(dockerMocha, () =>
{
manager();
});
});
}
}
}
else if(testFile && setupFile) // run a test. Needs the init function of the setup file and the testFile to run the test itself
{
// console.log("Setup and test file");
const loaderClass = Utils.requireFile(setupFile);
const testFilePath = Utils.getAbsPath(testFile);
console.log(`Running Tests on ${testFile} after running init present in ${setupFile}`);
let testsFailed = 1;
const taskList = [
loaderClass.init,
function(callback)
{
console.log("Ran INIT of " + loaderClass.name + " before test " + testFile);
callback(null);
},
function(callback)
{
if(dockerMocha.port)
{
console.log("Waiting for server on port " + dockerMocha.port + " to be available, as specified by the -p argument of docker-mocha.");
Utils.checkConnectivityOnPort(dockerMocha.port, callback);
}
else
{
callback(null);
}
},
function(callback)
{
// Instantiate a Mocha instance.
const mocha = new Mocha();
mocha.addFile(
testFilePath
);
// Run the tests.
mocha.run(function(failures) {
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
testsFailed = process.exitCode;
callback();
});
},
function(callback)
{
console.log("DOCKER-MOCHA: Started shutdown of " + loaderClass.name);
loaderClass.shutdown(function()
{
console.log("DOCKER-MOCHA: Finished shutdown of " + loaderClass.name + " with error: " + testsFailed);
callback(null);
});
},
function()
{
console.log("Exiting after running shutdown of " + loaderClass.name);
process.exit(testsFailed);
}
];
Utils.runSync(taskList);
console.log("After Run Sync, test + setup");
}
else if(setupFile) // run a setup. Needs the init, load and shutdown methods of the setupFile
{
// console.log("Setup file only");
const loaderClass = Utils.requireFile(setupFile);
console.log(`Setting up state ${loaderClass.name} from file ${setupFile}`);
const taskList = [
loaderClass.init,
function(callback)
{
if(dockerMocha.port)
{
console.log("Waiting for server on port " + dockerMocha.port + " to be available, as specified by the -p argument of docker-mocha.");
Utils.checkConnectivityOnPort(dockerMocha.port, callback);
}
else
{
console.log("Skipping wait for service bootup as no -p argument was specified.");
callback(null);
}
},
function(callback)
{
console.log("Ran INIT of " + loaderClass.name);
callback(null);
},
loaderClass.load,
function(callback)
{
console.log("Ran LOAD of " + loaderClass.name);
callback(null);
},
loaderClass.shutdown,
function(callback)
{
console.log("Exiting after running shutdown of " + loaderClass.name);
process.exit(0);
}
];
Utils.runSync(taskList);
console.log("After Run Sync, setup");
}
function manager()
{
if(Utils.isNull(dockerMocha.rootState))
{
console.error("ERROR: No root state specified, please provide exactly one state with no parent dependency");
process.exit(1);
return;
}
queue = new Queue(function (task, callback)
{
if(task["type"] === "state")
{
//In case no Checkpoint flag, immediately unlock
if (dockerMocha.noCheckpoint)
{
if(Utils.isNull(dockerMocha.dependencyMap[task["name"]])) //in case it does no have dependencies
{
callback(null);
}
else
{
const childTests = dockerMocha.dependencyMap[task["name"]]["child_tests"];
const childStates = dockerMocha.dependencyMap[task["name"]]["child_states"];
//unlock all the child states
for (const i in childStates)
{
const state = childStates[i];
queue.push({"type": "state", "name": state});
}
//unlock all the child tests
for (const j in childTests)
{
const test = childTests[j];
queue.push({"type": "test", "name": test});
}
callback(null);
}
}
else
{
createState(task["name"], (err) =>
{
if (err > 0)
{
callback(1);
}
else
{
if(Utils.isNull(dockerMocha.dependencyMap[task["name"]]))
{
callback(null);
}
else
{
const childTests = dockerMocha.dependencyMap[task["name"]]["child_tests"];
const childStates = dockerMocha.dependencyMap[task["name"]]["child_states"];
//unlock all the child tests
for (const j in childTests)
{
const test = childTests[j];
queue.push({"type": "test", "name": test});
}
//unlock all the child states
for (const i in childStates)
{
const state = childStates[i];
queue.push({"type": "state", "name": state});
}
callback(null);
}
}
})
}
}
else if(task["type"] === "test")
{
runTest(task["name"], (err) =>
{
if(err)
{
failedTests++;
callback(1);
}
else
{
passedTests++;
callback(null);
}
})
}
},
{concurrent: threadsNumber, maxRetries: MAX_RETRIES, maxTimeout: MAX_TIMEOUT});
queue.push({"type":"state","name":dockerMocha.rootState});
queue.on('drain', function ()
{
finishTime = new Date();
const res = Math.abs(finishTime - startTime) / 1000;
const hours = Math.floor(res / 3600) % 24;
const minutes = Math.floor(res / 60) % 60;
const seconds = res % 60;
console.log("Docker Mocha finished with " + passedTests + "/" + (passedTests+failedTests) + " passed tests");
console.log("Docker Mocha executed " + (passedTests+failedTests) + "/" + Object.keys(dockerMocha.testsMap).length);
//console.log("Docker Mocha skipped " + ((Object.keys(dockerMocha.testsMap).length) - (passedTests+failedTests)) + " tests");
console.log("Execution finished in " + hours + " hour(s), " + minutes + " minute(s) and " + seconds + " seconds");
const csvData = csvExporter.generateCsv(data, true);
fs.writeFileSync('data_' + timeStamp + '.csv', csvData);
fs.appendFileSync(logFileName, "Execution started with Timestamp: " + timeStamp + "\n");
fs.appendFileSync(logFileName, "Execution finished in " + hours + " hour(s), " + minutes + " minute(s) and " + seconds + " seconds" + "\n");
fs.appendFileSync(logFileName, "Docker Mocha finished with " + passedTests + "/" + (passedTests+failedTests) + " passed tests" + "\n");
fs.appendFileSync(logFileName, "Docker Mocha executed " + (passedTests+failedTests) + "/" + Object.keys(dockerMocha.testsMap).length + "\n");
fs.appendFileSync(logFileName, "\nPASSED TESTS:\n");
for(let j in passedTestsArray)
{
try
{
fs.appendFileSync(logFileName, passedTestsArray[j] + "\n");
} catch (err) {}
}
fs.appendFileSync(logFileName, "\n\nFAILED TESTS:\n");
for(let i in failedTestsArray)
{
try
{
fs.appendFileSync(logFileName, failedTestsArray[i] + "\n");
} catch (err) {}
}
process.exit(0);
})
}
function createState(state, callback)
{
console.log("Creating State: " + state);
let stateParent = dockerMocha.getStateParent(state);
const startDateState = new Date();
DockerManager.checkIfStateExists(state, dockerMocha, (exists) =>
{
if(exists)
{
data.push({
timestamp: Utils.convertTimestamp(startDateState),
state: state,
event: "state_reused",
test: '',
time_taken: 0
});
callback(0);
}
else
{
data.push({
timestamp: Utils.convertTimestamp(startDateState),
state: state,
event: "state_create_start",
test: '',
time_taken: 0
});
DockerManager.StopAndRemoveContainers(state, dockerMocha, () =>
{
DockerManager.RemoveNetworks(state, dockerMocha, () =>
{
DockerManager.restoreState(state, state, dockerMocha, (info) =>
{
DockerManager.logEntrypoint(info.entrypoint, dockerMocha, () =>
{
DockerManager.stopEnvironment(state, stateParent, dockerMocha, (err) =>
{
const stopDateState = new Date();
data.push({
timestamp: Utils.convertTimestamp(stopDateState),
state: state,
event: "state_create_complete",
test: '',
time_taken: Math.abs(stopDateState - startDateState) / 1000,
});
callback(err);
});
});
});
})
})
}
})
}
function runTest(test, callback)
{
console.log("Running Test: " + test);
const state = dockerMocha.getTestState(test);
const testPath = dockerMocha.getTestPath(test);
let exitState;
if(dockerMocha.noCheckpoint)
exitState = null;
else
exitState = state;
const startDateTest = new Date();
DockerManager.StopAndRemoveContainers(state, dockerMocha, () =>
{
DockerManager.RemoveNetworks(test, dockerMocha, () =>
{
DockerManager.restoreState(state, test, dockerMocha, (info) =>
{
DockerManager.runTest(info.entrypoint, test, testPath, dockerMocha, (err, result) =>
{
if (err)
{
console.error("Test Failed: " + test);
failedTestsArray.push(testPath);
}
else
{
passedTestsArray.push(testPath);
console.info("Test Passed: " + test);
}
DockerManager.logEntrypoint(info.entrypoint, dockerMocha, () =>
{
DockerManager.stopEnvironment(test, exitState, dockerMocha, () =>
{
const stopDateTest = new Date();
data.push({
timestamp: Utils.convertTimestamp(stopDateTest),
state: state,
event: "test_concluded",
test: test,
time_taken: Math.abs(stopDateTest - startDateTest) / 1000,
});
callback(err);
});
});
})
})
})
})
}