forked from xmrig/xmrig-proxy
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathrun_tests.js
More file actions
252 lines (217 loc) · 7.71 KB
/
Copy pathrun_tests.js
File metadata and controls
252 lines (217 loc) · 7.71 KB
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
#!/usr/bin/env node
"use strict";
const { spawnSync } = require("child_process");
const fs = require("fs");
const os = require("os");
const path = require("path");
const ROOT = __dirname;
const DEFAULT_TIMEOUT_MS = 15000;
function parseArgs(argv) {
const options = {
build: true,
buildDir: path.join(ROOT, "build"),
binary: null,
cmakeArgs: [],
cmakeGenerator: process.env.CMAKE_GENERATOR || "",
timeoutMs: DEFAULT_TIMEOUT_MS,
verbose: false
};
for (let i = 0; i < argv.length; ++i) {
const arg = argv[i];
const value = () => {
if (i + 1 >= argv.length) {
throw new Error(`missing value for ${arg}`);
}
return argv[++i];
};
if (arg === "--binary") {
options.binary = path.resolve(value());
}
else if (arg.startsWith("--binary=")) {
options.binary = path.resolve(arg.slice("--binary=".length));
}
else if (arg === "--build-dir") {
options.buildDir = path.resolve(value());
}
else if (arg.startsWith("--build-dir=")) {
options.buildDir = path.resolve(arg.slice("--build-dir=".length));
}
else if (arg === "--skip-build") {
options.build = false;
}
else if (arg === "--cmake-generator") {
options.cmakeGenerator = value();
}
else if (arg.startsWith("--cmake-generator=")) {
options.cmakeGenerator = arg.slice("--cmake-generator=".length);
}
else if (arg === "--cmake-arg") {
options.cmakeArgs.push(value());
}
else if (arg.startsWith("--cmake-arg=")) {
options.cmakeArgs.push(arg.slice("--cmake-arg=".length));
}
else if (arg === "--timeout-ms") {
options.timeoutMs = Number(value());
}
else if (arg.startsWith("--timeout-ms=")) {
options.timeoutMs = Number(arg.slice("--timeout-ms=".length));
}
else if (arg === "--verbose") {
options.verbose = true;
}
else if (arg === "--help" || arg === "-h") {
printHelp();
process.exit(0);
}
else {
throw new Error(`unknown argument: ${arg}`);
}
}
if (!Number.isFinite(options.timeoutMs) || options.timeoutMs <= 0) {
throw new Error("--timeout-ms must be a positive number");
}
return options;
}
function printHelp() {
console.log(`Usage: node run_tests.js [options]
Builds xmrig-proxy when needed, then runs offline integration suites with local
fake pools and fake miners. No external pool or miner connections are used.
Options:
--binary PATH use an existing xmrig-proxy binary
--build-dir PATH CMake build directory, default: build
--skip-build do not configure or build
--cmake-generator NAME CMake generator used when configuring
--cmake-arg ARG extra CMake configure argument, repeatable
--timeout-ms N operation timeout, default: ${DEFAULT_TIMEOUT_MS}
--verbose print proxy stdout/stderr while tests run
`);
}
function run(command, args, options = {}) {
console.log(`$ ${[command].concat(args).join(" ")}`);
const result = spawnSync(command, args, {
cwd: options.cwd || ROOT,
env: options.env || process.env,
stdio: "inherit",
windowsHide: true
});
if (result.error) {
throw result.error;
}
if (result.status !== 0) {
throw new Error(`${command} exited with status ${result.status}`);
}
}
function configureArgs(options) {
const args = ["-S", ROOT, "-B", options.buildDir, "-DCMAKE_BUILD_TYPE=Release"];
if (options.cmakeGenerator) {
args.push("-G", options.cmakeGenerator);
}
if (process.env.XMRIG_DEPS) {
args.push(`-DXMRIG_DEPS=${process.env.XMRIG_DEPS}`);
}
if (process.env.OPENSSL_ROOT_DIR) {
args.push(`-DOPENSSL_ROOT_DIR=${process.env.OPENSSL_ROOT_DIR}`);
}
return args.concat(options.cmakeArgs);
}
function findBinary(buildDir) {
const exe = process.platform === "win32" ? ".exe" : "";
const names = [`xmrig-proxy${exe}`, "xmrig-proxy", `xmrig-proxy-notls${exe}`, "xmrig-proxy-notls"];
const dirs = [
buildDir,
path.join(buildDir, "Release"),
path.join(buildDir, "RelWithDebInfo"),
path.join(buildDir, "Debug")
];
for (const dir of dirs) {
for (const name of names) {
const file = path.join(dir, name);
if (fs.existsSync(file)) {
return file;
}
}
}
throw new Error(`xmrig-proxy binary not found under ${buildDir}`);
}
function findNativeTargetBinary(proxyBinary) {
const name = process.platform === "win32" ? "native-target-tests.exe" : "native-target-tests";
const file = path.join(path.dirname(proxyBinary), name);
return fs.existsSync(file) ? file : null;
}
function findIpBanTestBinary(proxyBinary) {
const name = process.platform === "win32" ? "ip-ban-tests.exe" : "ip-ban-tests";
const file = path.join(path.dirname(proxyBinary), name);
return fs.existsSync(file) ? file : null;
}
function buildProxy(options) {
if (options.binary) {
if (!fs.existsSync(options.binary)) {
throw new Error(`binary not found: ${options.binary}`);
}
return options.binary;
}
if (options.build) {
if (!fs.existsSync(path.join(options.buildDir, "CMakeCache.txt"))) {
run("cmake", configureArgs(options));
}
const parallel = process.env.BUILD_PARALLEL || String(Math.max(1, os.cpus().length));
run("cmake", ["--build", options.buildDir, "--config", "Release", "--parallel", parallel]);
}
return findBinary(options.buildDir);
}
function runNodeTests(binary, nativeTargetBinary, ipBanTestBinary, options) {
const env = Object.assign({}, process.env, {
XMRIG_PROXY_TEST_BINARY: binary,
XMRIG_PROXY_TEST_TIMEOUT_MS: String(options.timeoutMs),
XMRIG_PROXY_TEST_VERBOSE: options.verbose ? "1" : "0"
});
if (nativeTargetBinary) {
env.XMRIG_PROXY_NATIVE_TARGET_BINARY = nativeTargetBinary;
}
else {
delete env.XMRIG_PROXY_NATIVE_TARGET_BINARY;
}
if (ipBanTestBinary) {
run(ipBanTestBinary, []);
}
run(process.execPath, [
"--test",
"--test-reporter=./tests/common/spec_reporter.js",
"--test-concurrency=1",
"tests/all.js"
], { env: env });
}
function main() {
const options = parseArgs(process.argv.slice(2));
const binary = buildProxy(options);
const nativeTargetBinary = findNativeTargetBinary(binary);
const ipBanTestBinary = findIpBanTestBinary(binary);
if (!nativeTargetBinary && options.build && !options.binary) {
throw new Error(`native-target-tests binary not found beside ${binary}`);
}
if (!ipBanTestBinary && options.build && !options.binary) {
throw new Error(`ip-ban-tests binary not found beside ${binary}`);
}
console.log(`testing ${binary}`);
if (nativeTargetBinary) {
console.log(`testing ${nativeTargetBinary}`);
}
else {
console.log("skipping native-target-tests: sibling binary not found (use --skip-build only when it is unavailable)");
}
if (ipBanTestBinary) {
console.log(`testing ${ipBanTestBinary}`);
}
else {
console.log("skipping ip-ban-tests: sibling binary not found (use --skip-build only when it is unavailable)");
}
runNodeTests(binary, nativeTargetBinary, ipBanTestBinary, options);
}
try {
main();
}
catch (error) {
console.error(error.stack || error.message || String(error));
process.exit(1);
}