summaryrefslogtreecommitdiff
path: root/jstests/free_mon/libs/free_mon.js
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/free_mon/libs/free_mon.js')
-rw-r--r--jstests/free_mon/libs/free_mon.js157
1 files changed, 157 insertions, 0 deletions
diff --git a/jstests/free_mon/libs/free_mon.js b/jstests/free_mon/libs/free_mon.js
new file mode 100644
index 00000000000..5e507ce3641
--- /dev/null
+++ b/jstests/free_mon/libs/free_mon.js
@@ -0,0 +1,157 @@
+
+/**
+ * Control the Free Monitoring Mock Webserver.
+ */
+
+// These faults must match the list of faults in mock_http_server.py, see the
+// SUPPORTED_FAULT_TYPES list in mock_http_server.py
+const FAULT_FAIL_REGISTER = "fail_register";
+const FAULT_INVALID_REGISTER = "invalid_register";
+
+class FreeMonWebServer {
+ /**
+ * Create a new webserver.
+ *
+ * @param {string} fault_type
+ */
+ constructor(fault_type) {
+ this.python = "/opt/mongodbtoolchain/v2/bin/python3";
+ this.fault_type = fault_type;
+
+ 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/free_mon/libs/mock_http_server.py";
+ this.control_py = "jstests/free_mon/libs/mock_http_control.py";
+
+ this.pid = undefined;
+ this.port = -1;
+ }
+
+ /**
+ * Get the Port.
+ *
+ * @return {number} port number of http server
+ */
+ getPort() {
+ return port;
+ }
+
+ /**
+ * Get the URL.
+ *
+ * @return {string} url of http server
+ */
+ getURL() {
+ return "http://localhost:" + this.port;
+ }
+
+ /**
+ * Start the Mock HTTP Server.
+ */
+ start() {
+ this.port = allocatePort();
+ print("Mock Web server is listening on port: " + this.port);
+
+ let args = [this.python, "-u", this.web_server_py, "--port=" + this.port];
+ if (this.fault_type) {
+ args.push("--fault=" + this.fault_type);
+ }
+
+ 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);
+ }
+
+ /**
+ * Query the HTTP server.
+ *
+ * @param {string} query type
+ *
+ * @return {object} Object representation of JSON from the server.
+ */
+ query(query) {
+ const out_file = "out_" + this.port + ".txt";
+ const python_command = this.python + " -u " + this.control_py + " --port=" + this.port +
+ " --query=" + query + " > " + out_file;
+
+ let ret = 0;
+ if (_isWindows()) {
+ ret = runProgram('cmd.exe', '/c', python_command);
+ } else {
+ ret = runProgram('/bin/sh', '-c', python_command);
+ }
+
+ assert.eq(ret, 0);
+
+ const result = cat(out_file);
+
+ try {
+ return JSON.parse(result);
+ } catch (e) {
+ jsTestLog("Failed to parse: " + result + "\n" + result);
+ throw e;
+ }
+ }
+
+ /**
+ * Query the stats page for the HTTP server.
+ *
+ * @return {object} Object representation of JSON from the server.
+ */
+ queryStats() {
+ return this.query("stats");
+ }
+
+ /**
+ * Wait for N register calls to be received by web server.
+ *
+ * @throws assert.soon() exception
+ */
+ waitRegisters(count) {
+ const qs = this.queryStats.bind(this);
+ // Wait for registration to occur
+ assert.soon(function() {
+ const stats = qs();
+ print("QS : " + tojson(stats));
+ return stats.registers == count;
+ }, "Failed to web server register", 60 * 1000);
+ }
+}
+
+/**
+ * Wait for registration information to be populated in the database.
+ *
+ * @param {object} conn
+ */
+function WaitForRegistration(conn) {
+ const admin = conn.getDB("admin");
+
+ // Wait for registration to occur
+ assert.soon(function() {
+ const docs = admin.system.version.find({_id: "free_monitoring"});
+ const da = docs.toArray();
+ return da.length != 0;
+ }, "Failed to register", 60 * 1000);
+}