summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/libs/configExpand/lib.js
blob: e2b98bce55404952a792b8dc83c2076032da30e7 (plain)
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
/**
 * Control the config file expansion mock web server.
 */

class ConfigExpandRestServer {
    /**
    * Create a new webserver.
    */
    constructor() {
        this.python = "/opt/mongodbtoolchain/v2/bin/python3";

        if (_isWindows()) {
            const paths = ["c:\\python36\\python.exe", "c:\\python\\python36\\python.exe"];
            for (let p of paths) {
                if (fileExists(p)) {
                    this.python = p;
                }
            }
        }

        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 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',
        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',
        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) {
        _stopMongoProgram(mongod);
    }
    removeFile(configFile);
}