summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/test/qa-tests/jstests/common/topology_helper.js
blob: a3bd8773a9b40827c006048ad11af633b7e7787c (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
// topology_helper.js; contains utility functions to run tests

// auth related variables
var authUser = 'user';
var authPassword = 'password';
var authArgs = [
  '--authenticationDatabase', 'admin',
  '--authenticationMechanism', 'SCRAM-SHA-1',
  '-u', authUser,
  '-p', authPassword
];
var keyFile = 'jstests/libs/key1';

// topology startup settings
var auth = {
  name: 'auth',
  args: authArgs,
};

var plain = {
  name: 'plain',
  args: [],
};

/* exported passthroughs */
// passthroughs while running all tests
var passthroughs = [plain, auth];

/* helper functions */

// runAuthSetup creates a user with root role on the admin database
var runAuthSetup = function(topology) {
  jsTest.log('Running auth setup');

  var conn = topology.connection();
  var db = conn.getDB('test');

  db.getSiblingDB('admin').createUser({
    user: authUser,
    pwd: authPassword,
    roles: ['root'],
  });

  assert.eq(db.getSiblingDB('admin').auth(authUser, authPassword), 1, 'authentication failed');
};

// buildStartupArgs constructs the proper object to be passed as arguments in
// starting mongod
var buildStartupArgs = function(passthrough) {
  var startupArgs = {};
  if (passthrough.name === auth.name) {
    startupArgs.auth = '';
    startupArgs.keyFile = keyFile;
  }
  return startupArgs;
};

// requiresAuth returns a boolean indicating if the passthrough requires authentication
var requiresAuth = function(passthrough) {
  return passthrough.name === auth.name;
};

/* standalone topology */
/* exported standaloneTopology */
var standaloneTopology = {
  init: function(passthrough) {
    jsTest.log('Using standalone topology');

    passthrough = passthrough || [];
    var startupArgs = buildStartupArgs(passthrough);
    startupArgs.port = allocatePorts(1)[0];
    this.conn = MongoRunner.runMongod(startupArgs);

    // set up the auth user if needed
    if (requiresAuth(passthrough)) {
      runAuthSetup(this);
    }
    return this;
  },
  connection: function() {
    return this.conn;
  },
  stop: function() {
    MongoRunner.stopMongod(this.conn);
  },
};


/* replica set topology */
/* exported replicaSetTopology */
var replicaSetTopology = {
  init: function(passthrough) {
    jsTest.log('Using replica set topology');

    passthrough = passthrough || [];
    var startupArgs = buildStartupArgs(passthrough);
    startupArgs.name = testName;
    startupArgs.nodes = 2;
    this.replTest = new ReplSetTest(startupArgs);

    // start the replica set
    this.replTest.startSet();
    jsTest.log('Started replica set');

    // initiate the replica set with a default config
    this.replTest.initiate();
    jsTest.log('Initiated replica set');

    // block till the set is fully operational
    this.replTest.awaitSecondaryNodes();
    jsTest.log('Replica set fully operational');

    // set up the auth user if needed
    if (requiresAuth(passthrough)) {
      runAuthSetup(this);
    }
    return this;
  },
  connection: function() {
    return this.replTest.getPrimary();
  },
  stop: function() {
    this.replTest.stopSet();
  },
};


/* sharded cluster topology */
/* exported shardedClusterTopology */
var shardedClusterTopology = {
  init: function(passthrough) {
    jsTest.log('Using sharded cluster topology');

    passthrough = passthrough || [];
    var other = buildStartupArgs(passthrough);
    var startupArgs = {};
    startupArgs.name = testName;
    startupArgs.mongos = 1;
    startupArgs.shards = 1;

    // set up the auth user if needed
    if (requiresAuth(passthrough)) {
      startupArgs.keyFile = keyFile;
      startupArgs.other = {
        shardOptions: other,
      };
      this.shardingTest = new ShardingTest(startupArgs);
      runAuthSetup(this);
    } else {
      startupArgs.other = {
        shardOptions: other,
      };
      this.shardingTest = new ShardingTest(startupArgs);
    }
    return this;
  },
  connection: function() {
    return this.shardingTest.s;
  },
  stop: function() {
    this.shardingTest.stop();
  },
};