summaryrefslogtreecommitdiff
path: root/jstests/libs/parallel_shell_helpers.js
blob: 60df28094c615ecc760deac943e60c7811c5fcf4 (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
/**
 * Allows passing arguments to the function executed by startParallelShell.
 */
const funWithArgs = (fn, ...args) =>
    "(" + fn.toString() + ")(" + args.map(x => tojson(x)).reduce((x, y) => x + ", " + y) + ")";

/**
 * Internal function used by _doAssertCommandInParallelShell().
 */
const _parallelShellRunCommand = function(
    dbName, cmdObj, expectedCode, checkResultFn, checkResultArgs) {
    const expectedCodeStr =
        (expectedCode === undefined ? '' : '; expectedCode: ' + tojson(expectedCode));
    jsTestLog('Starting command in parallel shell - host: ' + db.getMongo().host +
              '; db: ' + dbName + '; command: ' + tojson(cmdObj) + expectedCodeStr);
    const testDB = db.getSiblingDB(dbName);
    const result = testDB.runCommand(cmdObj);
    jsTestLog('Finished running command in parallel shell - host: ' + db.getMongo().host +
              '; db: ' + dbName + '; command: ' + tojson(cmdObj) + expectedCodeStr +
              '; result: ' + tojson(result));
    if (expectedCode === undefined) {
        assert.commandWorked(result);
    } else {
        assert.commandFailedWithCode(result, expectedCode);
    }
    if (!checkResultFn) {
        return;
    }
    jsTestLog('Checking command result in parallel shell - host: ' + db.getMongo().host +
              '; db: ' + dbName + '; command: ' + tojson(cmdObj) + expectedCodeStr +
              '; result: ' + tojson(result) + '; checkResultFn: ' + checkResultFn +
              '; checkResultArgs: ' + tojson(checkResultArgs));
    checkResultFn(result, checkResultArgs);
    jsTestLog('Successfully verified command result in parallel shell - host: ' +
              db.getMongo().host + '; db: ' + dbName + '; command: ' + tojson(cmdObj) +
              expectedCodeStr + '; result: ' + tojson(result));
};

/**
 * Internal function used by assertCommandWorkedInParallelShell() and
 * assertCommandFailedWithCodeInParallelShell().
 */
const _doAssertCommandInParallelShell = function(
    conn, db, cmdObj, expectedCode, checkResultFn, checkResultArgs) {
    // Return joinable object to caller.
    return startParallelShell(funWithArgs(_parallelShellRunCommand,
                                          db.getName(),
                                          cmdObj,
                                          expectedCode,
                                          checkResultFn,
                                          checkResultArgs),
                              conn.port);
};

/**
 * Starts command in a parallel shell.
 * Provides similar behavior to assert.commandWorked().
 */
const assertCommandWorkedInParallelShell = function(
    conn, db, cmdObj, checkResultFn, checkResultArgs) {
    return _doAssertCommandInParallelShell(conn, db, cmdObj, checkResultFn, checkResultArgs);
};

/**
 * Starts command in a parallel shell.
 * Provides similar behavior to assert.commandFailedWithCode().
 */
const assertCommandFailedWithCodeInParallelShell = function(
    conn, db, cmdObj, expectedCode, checkResultFn, checkResultArgs) {
    assert(expectedCode,
           'expected error code(s) must be provided to run command in parallel shell - host: ' +
               db.getMongo().host + '; db: ' + db.getName() + '; command: ' + tojson(cmdObj));
    return _doAssertCommandInParallelShell(
        conn, db, cmdObj, expectedCode, checkResultFn, checkResultArgs);
};