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
|
/**
* Control the config file expansion mock web server.
*/
class ConfigExpandRestServer {
/**
* Create a new webserver.
*/
constructor() {
load('jstests/libs/python.js');
this.python = getPython3Binary();
print("Using python interpreter: " + this.python);
this.web_server_py = "jstests/noPassthrough/libs/configExpand/rest_server.py";
this.pid = undefined;
this.port = undefined;
}
/**
* Get the Port.
*
* @return {number} port number of http server
*/
getPort() {
return this.port;
}
/**
* Get the URL.
*
* @return {string} url of http server
*/
getURL() {
return "http://localhost:" + this.port;
}
/**
* Construct a string reflection URL.
*
* @param {string} string to be reflected
* @param {object} Options, any combination of:
* {
* sleep: int, // seconds to sleep during request
* }
*/
getStringReflectionURL(str, options = {}) {
let url = this.getURL() + '/reflect/string?string=' + encodeURI(str);
if (options.sleep !== undefined) {
url += '&sleep=' + encodeURI(options.sleep);
}
return url;
}
/**
* Start the Mock HTTP Server.
*/
start() {
this.port = allocatePort();
print("Mock Web server is listening on port: " + this.port);
const args = [this.python, "-u", this.web_server_py, "--port=" + this.port];
this.pid = _startMongoProgram({args: args});
assert(checkProgram(this.pid));
// Wait for the web server to start
assert.soon(function() {
return rawMongoProgramOutput().search("Mock Web Server Listening") !== -1;
});
print("Mock HTTP Server sucessfully started.");
}
/**
* Stop the Mock HTTP Server.
*/
stop() {
stopMongoProgramByPid(this.pid);
}
}
function makeReflectionCmd(arg, opts = {}) {
'use strict';
load('jstests/libs/python.js');
let cmd = getPython3Binary();
if (_isWindows()) {
cmd = '"' + cmd + '"';
}
cmd += ' jstests/noPassthrough/libs/configExpand/reflect.py';
if (opts.sleep && (opts.sleep > 0)) {
cmd += ' -s ' + Number(opts.sleep);
}
// Escape arguments to the shell by wrapping in OS appropriate quotes.
if (_isWindows()) {
cmd += ' ' + arg.split('"').map(v => '"' + v + '"').join('\\"');
} else {
cmd += ' ' + arg.split("'").map(v => "'" + v + "'").join("\\'");
}
return cmd;
}
function jsToYaml(config, toplevel = true) {
if (typeof config === 'object') {
if (Array.isArray(config)) {
let delim = '';
let yaml = '';
config.forEach(function(v) {
yaml += delim + jsToYaml(v, false);
delim = ',';
});
return '[' + yaml + ']';
} else {
let delim = '';
let yaml = '';
for (let k in config) {
yaml += delim + k + ": " + jsToYaml(config[k], false);
delim = toplevel ? "\n" : ',';
}
if (toplevel) {
return yaml;
} else {
return '{' + yaml + '}';
}
}
} else if (typeof config === 'string') {
return "'" + config.replace(/'/g, "''") + "'";
} else {
// Simple scalar JSON types are close enough to YAML types.
return JSON.stringify(config);
}
}
function configExpandSuccess(config, test = null, opts = {}) {
const configFile = MongoRunner.dataPath + '/configExpand.conf';
writeFile(configFile, jsToYaml(config));
const mongod = MongoRunner.runMongod(Object.assign({
configExpand: 'rest,exec',
config: configFile,
},
opts));
assert(mongod, "Mongod failed to start up with config: " + cat(configFile));
removeFile(configFile);
if (test) {
test(mongod.getDB('admin'));
}
MongoRunner.stopMongod(mongod);
}
function configExpandFailure(config, test = null, opts = {}) {
const configFile = MongoRunner.dataPath + '/configExpand.conf';
writeFile(configFile, jsToYaml(config));
const options = Object.assign({
configExpand: 'rest,exec',
config: configFile,
port: allocatePort(),
},
opts);
let args = [MongoRunner.mongodPath];
for (let k in options) {
args.push('--' + k);
if (options[k] != '') {
args.push(String(options[k]));
}
}
clearRawMongoProgramOutput();
const mongod = _startMongoProgram({args: args});
assert.soon(function() {
return rawMongoProgramOutput().match(test);
});
if (mongod) {
stopMongoProgramByPid(mongod);
}
removeFile(configFile);
}
|