2019-03-11 18:17:29 -04:00
|
|
|
// @tags: [
|
|
|
|
|
// requires_fastcount,
|
|
|
|
|
// requires_non_retryable_writes,
|
|
|
|
|
// uses_multiple_connections,
|
2020-04-16 12:52:36 -04:00
|
|
|
// uses_parallel_shell,
|
2022-05-27 14:40:28 +00:00
|
|
|
// # This test has statements that do not support non-local read concern.
|
|
|
|
|
// does_not_support_causal_consistency,
|
2023-05-10 15:13:57 +00:00
|
|
|
// # DB.prototype.loadServerScripts does not behave as expected in module mode, and the SELinux
|
|
|
|
|
// # test runner loads scripts with dynamic load.
|
2024-03-18 11:34:55 +01:00
|
|
|
// no_selinux,
|
|
|
|
|
// requires_system_dot_js_stored_functions,
|
2024-06-11 16:27:44 +02:00
|
|
|
// # system.js stored functions only work for collections that live on the db-primary shard so
|
|
|
|
|
// # we have to make sure it wont be moved anywhere by the balancer
|
|
|
|
|
// assumes_balancer_off,
|
2019-03-11 18:17:29 -04:00
|
|
|
// ]
|
2012-07-18 14:52:37 -07:00
|
|
|
|
|
|
|
|
// Test db.loadServerScripts()
|
|
|
|
|
|
2025-08-28 15:11:44 -04:00
|
|
|
let testdb = db.getSiblingDB("loadserverscripts");
|
2023-11-18 08:19:11 +00:00
|
|
|
const systemJsColl = testdb.getCollection("system.js");
|
2012-07-18 14:52:37 -07:00
|
|
|
|
|
|
|
|
jsTest.log("testing db.loadServerScripts()");
|
2025-08-28 15:11:44 -04:00
|
|
|
let x;
|
2012-07-18 14:52:37 -07:00
|
|
|
|
|
|
|
|
// clear out any data from old tests
|
2023-11-18 08:19:11 +00:00
|
|
|
systemJsColl.remove({});
|
2012-07-18 14:52:37 -07:00
|
|
|
|
2023-11-18 08:19:11 +00:00
|
|
|
x = systemJsColl.findOne();
|
2012-07-18 14:52:37 -07:00
|
|
|
assert.isnull(x, "Test for empty collection");
|
|
|
|
|
|
|
|
|
|
// User functions should not be defined yet
|
2016-03-09 12:17:50 -05:00
|
|
|
assert.eq(typeof myfunc, "undefined", "Checking that myfunc() is undefined");
|
|
|
|
|
assert.eq(typeof myfunc2, "undefined", "Checking that myfunc2() is undefined");
|
2012-07-18 14:52:37 -07:00
|
|
|
|
|
|
|
|
// Insert a function in the context of this process: make sure it's in the collection
|
2023-11-18 08:19:11 +00:00
|
|
|
systemJsColl.insert({
|
2016-03-09 12:17:50 -05:00
|
|
|
_id: "myfunc",
|
2025-08-21 10:17:44 -07:00
|
|
|
"value": function () {
|
2016-03-09 12:17:50 -05:00
|
|
|
return "myfunc";
|
2025-08-21 10:17:44 -07:00
|
|
|
},
|
2016-03-09 12:17:50 -05:00
|
|
|
});
|
2023-11-18 08:19:11 +00:00
|
|
|
systemJsColl.insert({_id: "mystring", "value": "var root = this;"});
|
|
|
|
|
systemJsColl.insert({_id: "changeme", "value": false});
|
2015-09-09 15:53:45 -04:00
|
|
|
|
2023-11-18 08:19:11 +00:00
|
|
|
x = systemJsColl.count();
|
2016-03-09 12:17:50 -05:00
|
|
|
assert.eq(x, 3, "Should now be one function in the system.js collection");
|
2015-09-09 15:53:45 -04:00
|
|
|
|
|
|
|
|
// Set a global variable that will be over-written
|
2016-02-04 12:29:01 -05:00
|
|
|
var changeme = true;
|
2012-07-18 14:52:37 -07:00
|
|
|
|
|
|
|
|
// Load that function
|
2012-07-24 16:30:09 -07:00
|
|
|
testdb.loadServerScripts();
|
2016-03-09 12:17:50 -05:00
|
|
|
assert.eq(typeof myfunc, "function", "Checking that myfunc() loaded correctly");
|
|
|
|
|
assert.eq(typeof mystring, "string", "Checking that mystring round-tripped correctly");
|
|
|
|
|
assert.eq(changeme, false, "Checking that global var was overwritten");
|
2012-07-18 14:52:37 -07:00
|
|
|
|
|
|
|
|
// Make sure it works
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
|
x = myfunc();
|
|
|
|
|
assert.eq(x, "myfunc", "Checking that myfunc() returns the correct value");
|
|
|
|
|
|
|
|
|
|
// Insert value into collection from another process
|
2025-08-28 15:11:44 -04:00
|
|
|
let coproc = startParallelShell(
|
2025-08-21 10:17:44 -07:00
|
|
|
'db.getSiblingDB("loadserverscripts").getCollection("system.js").insert' +
|
|
|
|
|
' ( {_id: "myfunc2", "value": function(){ return "myfunc2"; } } );',
|
|
|
|
|
);
|
2012-07-18 14:52:37 -07:00
|
|
|
// wait for results
|
|
|
|
|
coproc();
|
|
|
|
|
|
|
|
|
|
// Make sure the collection's been updated
|
2023-11-18 08:19:11 +00:00
|
|
|
x = systemJsColl.count();
|
2016-03-09 12:17:50 -05:00
|
|
|
assert.eq(x, 4, "Should now be two functions in the system.js collection");
|
2012-07-18 14:52:37 -07:00
|
|
|
|
|
|
|
|
// Load the new functions: test them as above
|
2012-07-24 16:30:09 -07:00
|
|
|
testdb.loadServerScripts();
|
2016-03-09 12:17:50 -05:00
|
|
|
assert.eq(typeof myfunc2, "function", "Checking that myfunc2() loaded correctly");
|
2012-07-18 14:52:37 -07:00
|
|
|
// eslint-disable-next-line
|
|
|
|
|
x = myfunc2();
|
|
|
|
|
assert.eq(x, "myfunc2", "Checking that myfunc2() returns the correct value");
|
|
|
|
|
|
|
|
|
|
jsTest.log("completed test of db.loadServerScripts()");
|