-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathrunFunctionalTests.js
162 lines (142 loc) · 4.88 KB
/
runFunctionalTests.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
// Helper script to orchestrate starting Functional Tests
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
let perfMode = false;
function help() {
console.log(
"Usage: node RunFunctionalTests.js [PathToAISDK]\n\n"+
"PathToAISDK must be an absolute path to a tgz archive"+
" of the compiled AI SDK,\nor an NPM package reference "+
"(eg. applicationinsights@0.22.0)\n\nA present AI SDK tgz "+
"in the root repo directory will be used by default");
return 0;
}
function findDefaultPath() {
const rootDir = path.resolve(__dirname, "../../");
const files = fs.readdirSync(rootDir);
for(let i = 0; i < files.length; i++) {
const file = path.join(rootDir, files[i]);
const stat = fs.lstatSync(file);
if (!stat.isDirectory()) {
if (file.indexOf("applicationinsights") === rootDir.length + 1 &&
file.indexOf(".tgz") === file.length - 4) {
return path.resolve(file);
}
}
}
return null;
}
function run(cmd, workingDir) {
const proc = childProcess.spawnSync(cmd, {
shell: true,
cwd: workingDir && path.resolve(__dirname, workingDir)
});
return {
code: proc.status,
output: proc.output.map(v => String.fromCharCode.apply(null, v)).join("")
}
}
function runLive(cmd, workingDir) {
const proc = childProcess.spawnSync(cmd, {
shell: true,
cwd: workingDir && path.resolve(__dirname, workingDir),
stdio: 'inherit'
});
return {
code: proc.status,
output: proc.output.map(v => String.fromCharCode.apply(null, v)).join("")
}
}
function runAsync(cmd, workingDir) {
const proc = childProcess.spawn(cmd, [], {
shell: true,
cwd: workingDir && path.resolve(__dirname, workingDir)
});
return proc;
}
function startDocker() {
const tasks = [
run("docker run -d -p 27017:27017 --name ainjsmongo mongo"),
run("docker run -e MYSQL_ROOT_PASSWORD=dummypw -e MYSQL_DATABASE=testdb -d -p 33060:3306 --name ainjsmysql mysql:5"),
run("docker run -d -p 63790:6379 --name ainjsredis redis:alpine"),
run("docker run -e POSTGRES_PASSWORD=dummypw -d -p 54320:5432 --name ainjspostgres postgres:alpine")
];
for(let i = 0; i < tasks.length; i++) {
if (tasks[i].code !== 0) {
console.error("Failed to start container!");
console.error(tasks[i].output);
return false;
}
}
return true;
}
function cleanUpDocker() {
run("docker stop ainjsmongo");
run("docker stop ainjsmysql");
run("docker stop ainjsredis");
run("docker stop ainjspostgres");
run("docker rm ainjsmongo");
run("docker rm ainjsmysql");
run("docker rm ainjsredis");
run("docker rm ainjspostgres");
}
function main() {
// Find the SDK TGZ archive
let path = null;
if (process.argv.length > 2) {
path = process.argv[2]
if (path === "-h" || path === "--help") {
return help();
}
if (process.argv.indexOf("-perfmode") !== -1) {
perfMode = true;
}
}
if (path === null || path.indexOf("-") === 0) {
path = findDefaultPath();
}
if (path === null) {
console.error("Could not find path for AI SDK!");
help();
return 1;
}
console.log("Using SDK package at " + path);
// Validate docker is present on the box
if (run("docker --version").code !== 0) {
console.error("Docker not installed!");
return 1;
}
// Prepare docker
console.log("Spinning up Docker containers...");
cleanUpDocker(); // Just in case the script failed in a previous run
if (!startDocker()) {
console.error("Could not spin up containers!");
return 1;
}
// Prepare runner and testapp
console.log("Installing Runner and TestApp dependencies...");
if (run("npm install", "./runner").code !== 0 || run("npm install", "./testApp").code !== 0) {
console.error("Could not install dependencies!");
return 1;
}
console.log("Installing " + path);
run("npm uninstall applicationinsights", "./testApp");
if (run("npm install --no-save " + path, "./testApp").code !== 0) {
console.error("Could not install SDK!");
return 1;
}
// Run tests
console.log("Running functional tests...");
console.log("=======================\n");
const testApp = runAsync("node --use_strict main.js", "./testApp");
const runnerStatus = runLive("node --use_strict main.js" + (perfMode ? " -perfmode": ""), "./runner").code;
console.log("\n=======================");
// Clean up
console.log("Killing TestApp...");
testApp.kill();
console.log("Spinning down and deleting Docker containers...");
cleanUpDocker();
return runnerStatus;
}
process.exit(main());