summaryrefslogtreecommitdiff
path: root/jstests/multiVersion/copydb_helper.js
blob: 25c60f2552a650df7406ff8916e8ceb46788dc7a (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
// SERVER-36438 Ensure the 4.2 copyDatabase() shell helper still successfully executes the copyDB
// command on a 4.0 server, now that the copyDB command has been removed as of 4.2.
(function() {
"use strict";
const oldVersion = "4.0";

let runTest = function(useAuth) {
    let conn;
    if (useAuth) {
        conn = MongoRunner.runMongod({auth: "", binVersion: oldVersion});
    } else {
        conn = MongoRunner.runMongod({binVersion: oldVersion});
    }

    let fromDB = conn.getDB("copydb2-test-a");
    let toDB = conn.getDB("copydb2-test-b");
    let adminDB = conn.getDB("admin");

    if (useAuth) {
        adminDB.createUser({user: "root", pwd: "root", roles: ["root"]});
        adminDB.auth("root", "root");
        fromDB.createUser(
            {user: "chevy", pwd: "chase", roles: ["read", {role: "readWrite", db: toDB._name}]});
    }

    assert.commandWorked(fromDB.foo.insert({a: 1}));
    assert.commandWorked(fromDB.foo.createIndex({a: 1}));

    if (useAuth) {
        assert.commandWorked(toDB.getSiblingDB("admin").logout());
        fromDB.auth("chevy", "chase");
    }

    assert.eq(1, fromDB.foo.count());
    assert.eq(0, toDB.foo.count());

    assert.commandWorked(fromDB.copyDatabase(fromDB._name, toDB._name));
    assert.eq(1, fromDB.foo.count());
    assert.eq(1, toDB.foo.count());
    assert.eq(fromDB.foo.getIndexes().length, toDB.foo.getIndexes().length);
    MongoRunner.stopMongod(conn);
};

runTest(/*useAuth*/ false);

// Authenticating as multiple users on multiple databases results in an error.
if (!jsTest.options().auth) {
    runTest(/*useAuth*/ true);
}
})();