summaryrefslogtreecommitdiff
path: root/jstests/replsets/auth3.js
blob: b15edf448d53f9753f3323c08a6da7c6ffb0ab0c (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
if ( !_isWindows() ) { // SERVER-5024
var path = "jstests/libs/";

var rs = new ReplSetTest({"nodes" : {node0 : {}, node1 : {}, arbiter : {}}, keyFile : path+"key1"});
rs.startSet();
rs.initiate();

master = rs.getMaster();
print("adding user");
master.getDB("admin").addUser("foo", "bar");

var checkValidState = function(i) {
    assert.soon(function() {
        var result = rs.nodes[i].getDB("admin").runCommand({isMaster : 1});
        printjson(result);
        return result.secondary || result.ismaster;
    });
};

var safeInsert = function() {
    master = rs.getMaster();
    master.getDB("admin").auth("foo", "bar");
    master.getDB("foo").bar.insert({x:1});
    var insertWorked = master.getDB("foo").runCommand({getlasterror:1});
    printjson(insertWorked);
    assert.eq(insertWorked.ok, 1);
}

print("authing");
for (var i=0; i<2; i++) {
    checkValidState(i);

    // if this is run before initial sync finishes, we won't be logged in
    rs.nodes[i].getDB("admin").auth("foo", "bar");
}

print("make common point");

safeInsert();
rs.awaitReplication();

print("write stuff to 0&2")
rs.stop(1);

master = rs.getMaster();
master.getDB("foo").bar.drop();
print("last op: "+tojson(master.getDB("local").oplog.rs.find().sort({$natural:-1}).limit(1).next()));

print("write stuff to 1&2")
rs.stop(0);
rs.restart(1);

safeInsert();
print("last op: "+tojson(master.getDB("local").oplog.rs.find().sort({$natural:-1}).limit(1).next()));

rs.restart(0);

print("doing rollback!");
assert.soon(function() {
    var result = rs.nodes[0].getDB("admin").runCommand({isMaster : 1});
    printjson(result);
    return !result.secondary && !result.ismaster;
});

print("make sure 0 successfully rolls back");
m = rs.nodes[0];

checkValidState(0);
checkValidState(1);
} // !_isWindows()