summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/test/qa-tests/jstests/oplog/all_ops_tests.js
blob: dd3b97c2923dc79a874eb31350ce4096f2dc7745 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * This test creates a fake oplog and uses it to test correct behavior of
 * all possible op codes.
 */
(function() {
  if (typeof getToolTest === 'undefined') {
    load('jstests/configs/plain_28.config.js');
  }
  load('jstests/libs/extended_assert.js');
  var assert = extendedAssert;

  var OPLOG_INSERT_CODE = 'i';
  var OPLOG_COMMAND_CODE = 'c';
  var OPLOG_UPDATE_CODE = 'u';
  var OPLOG_REMOVE_CODE = 'd';
  var OPLOG_NOOP_CODE = 'n';
  var CURRENT_OPLOG_VERSION = 2;

  var toolTest = getToolTest('applyAllOpsTest');
  var commonToolArgs = getCommonToolArguments();

  // Get the db that we'll insert the fake oplog into
  var db = toolTest.db.getSiblingDB('foo');
  db.dropDatabase();
  db.getSiblingDB('rs').dropDatabase();

  // Create capped collection
  db.getSiblingDB('rs').createCollection('rs_test', {capped: true, size: 4});

  // Add a bunch of operations to the fake oplog

  // Create a collection to drop
  db.getSiblingDB('rs').rs_test.insert({
    ts: new Timestamp(),
    v: CURRENT_OPLOG_VERSION,
    op: OPLOG_COMMAND_CODE,
    ns: "foo.$cmd",
    o: {create: "baz"}
  });

  // Insert a doc
  db.getSiblingDB('rs').rs_test.insert({
    ts: new Timestamp(),
    h: 0,
    v: CURRENT_OPLOG_VERSION,
    op: OPLOG_INSERT_CODE,
    o: {
      _id: 0
    },
    ns: 'foo.baz'
  });

  // Drop the doc's database
  db.getSiblingDB('rs').rs_test.insert({
    ts: new Timestamp(),
    h: 1,
    v: CURRENT_OPLOG_VERSION,
    op: OPLOG_COMMAND_CODE,
    o: {
      dropDatabase: 1
    },
    ns: 'foo.$cmd'
  });

  // Create the collection
  db.getSiblingDB('rs').rs_test.insert({
    ts: new Timestamp(),
    v: CURRENT_OPLOG_VERSION,
    op: OPLOG_COMMAND_CODE,
    ns: "foo.$cmd",
    o: {create: "bar"}
  });

  // Insert 2 docs
  db.getSiblingDB('rs').rs_test.insert({
    ts: new Timestamp(),
    h: 2,
    v: CURRENT_OPLOG_VERSION,
    op: OPLOG_INSERT_CODE,
    o: {
      _id: 1
    },
    ns: 'foo.bar'
  });

  db.getSiblingDB('rs').rs_test.insert({
    ts: new Timestamp(),
    h: 3,
    v: CURRENT_OPLOG_VERSION,
    op: OPLOG_INSERT_CODE,
    o: {
      _id: 2
    },
    ns: 'foo.bar'
  });

  // Remove first doc
  db.getSiblingDB('rs').rs_test.insert({
    ts: new Timestamp(),
    h: 4,
    b: true,
    v: CURRENT_OPLOG_VERSION,
    op: OPLOG_REMOVE_CODE,
    o: {
      _id: 1
    },
    ns: 'foo.bar'
  });

  // Update the second doc
  db.getSiblingDB('rs').rs_test.insert({
    ts: new Timestamp(),
    h: 5,
    b: true,
    v: CURRENT_OPLOG_VERSION,
    op: OPLOG_UPDATE_CODE,
    o2: {
      _id: 2
    },
    o: {
      _id: 2,
      x: 1
    },
    ns: 'foo.bar'
  });

  // Noop
  db.getSiblingDB('rs').rs_test.insert({
    ts: new Timestamp(),
    h: 6,
    op: OPLOG_NOOP_CODE,
    ns: 'foo.bar',
    o: {x: 'noop'}
  });

  var args = ['oplog', '--oplogns', 'rs.rs_test',
    '--from', '127.0.0.1:' + toolTest.port].concat(commonToolArgs);

  if (toolTest.isSharded) {
    // When applying ops to a sharded cluster,
    assert(toolTest.runTool.apply(toolTest, args) !== 0,
      'mongooplog should fail when running applyOps on a sharded cluster');

    var expectedError =
      'error applying ops: applyOps not allowed through mongos';
    assert.strContains.soon(expectedError, rawMongoProgramOutput,
      'mongooplog crash should output the correct error message');

    assert.eq(0, db.bar.count({}),
      'mongooplog should not have applied any operations');
  } else {
    // Running with default --seconds should apply all operations
    assert.eq(toolTest.runTool.apply(toolTest, args), 0,
      'mongooplog should succeed');

    assert.eq(1, db.bar.count({}),
      'mongooplog should apply all operations');
    assert.eq(0, db.baz.count({}), 'mongooplog should have dropped db');
    assert.eq(1, db.bar.count({_id: 2}),
      'mongooplog should have applied correct ops');
  }

  toolTest.stop();
}());