-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_replication_sslcon.nim
198 lines (182 loc) · 6.08 KB
/
test_replication_sslcon.nim
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# WIP replication test
# TODO:
# 1. [Done] Run local mongod processes for replication setup
# 2. [Done] Manage the replication setup by initializing it first
# ref: https://docs.mongodb.com/manual/tutorial/deploy-replica-set-for-testing/
# 3. [Done] Fix all nodes status to be able to elect the PRIMARY, current problem
# all nodes are SECONDARY and this disability to elect the PRIMARY cannot
# to do any write operation
# 4. Fix weird `auto` enabling slave during the test and it should be throwing
# MongoError with reason `not enabled slave`.
# 5. [Done] Cleanup all produced artifacts such as temporary dbpath directories and
# created self-signing key, certificate, and pem file.
# 6. [Done w ReadPreference.primary?] Since the test purposely choose
# the ReadPreference.secondary, testing to read the database entry
# could result in disaster because of eventual synchronization.
import utils_test
{.warning[UnusedImport]: off.}
const
testReplication {.booldefine.} = false
when testReplication and defined(ssl):
import unittest
from threadpool import spawn, sync
from times import now, toTime
from os import sleep
from strformat import `&`
from osproc import Process, running
from threadpool import spawn
from sequtils import allIt, all, anyIt
from sugar import dump
import utils_replica
import anonimongo
suite "Replication, SSL, and SRV DNS seedlist lookup (mongodb+srv) tests":
test "Initial test setup":
require createMongoTemp()
test "Create self-signing SSL key certificate":
require createSSLCert()
var processes: seq[Process]
test "Run the local replication set db":
processes = setupMongoReplication()
require processes.allIt( it != nil )
require processes.all running
var mongo: Mongo[TheSock]
var db: Database[TheSock]
test "Catch error without SSL for SSL/TLS required connection":
expect(IOError):
var m = newMongo[TheSock](
MongoUri &"mongodb://{mongoServer}:{replicaPortStart}/admin",
poolconn = utils_test.poolconn)
when anoSocketSync:
check m.connect()
else:
check waitfor m.connect()
m.close()
test "Connect single uri":
mongo = newMongo[TheSock](MongoUri uriSettingRepl,
poolconn = utils_test.poolconn,
dnsserver = mongoServer,
dnsport = dnsport)
require mongo != nil
when anoSocketSync:
require mongo.connect()
else:
require waitfor mongo.connect()
db = mongo["admin"]
require db != nil
test "Setting up replication set":
var config = bson({
"_id": rsetName,
members: [
{ "_id": 0, host: &"{mongoServer}:{replicaPortStart}", priority: 2 },
{ "_id": 1, host: &"{mongoServer}:{replicaPortStart+1}" },
{ "_id": 2, host: &"{mongoServer}:{replicaPortStart+2}" },
]
})
var reply: BsonDocument
try:
when anoSocketSync:
reply = db.replSetInitiate(config)
else:
reply = waitfor db.replSetInitiate(config)
except MongoError:
checkpoint(getCurrentExceptionMsg())
fail()
reply.reasonedCheck("replSetInitiate")
try:
when anoSocketSync:
reply = db.replSetGetStatus
else:
reply = waitfor db.replSetGetStatus
except MongoError:
checkpoint(getCurrentExceptionMsg())
fail()
when utils_test.verbose: dump reply
reply.reasonedCheck("replSetGetStatus")
check reply["set"] == rsetName
let members = reply["members"].ofArray
check members.len == 3
sleep 15_000 # waiting the replica set to elect primary
test "Connect with manual multi uri connections":
mongo = newMongo[TheSock](
MongoUri uriMultiManual,
poolconn = utils_test.poolconn
)
require mongo != nil
when anoSocketSync:
check mongo.connect
else:
check waitfor mongo.connect
db = mongo["admin"]
when anoSocketSync:
let cfg = db.replSetGetStatus
else:
let cfg = waitfor db.replSetGetStatus
let members = cfg["members"].ofArray
check members.len == 3
check members.anyIt( it["stateStr"] == "PRIMARY" )
mongo.close
spawn fakeDnsServer()
test "Check newMongo mongodb+srv scheme connection":
try:
mongo = newMongo[TheSock](
MongoUri uriSrv,
poolconn = utils_test.poolconn,
dnsserver = mongoServer,
dnsport = dnsport
)
except RangeError:
checkpoint(getCurrentExceptionMsg())
fail()
require mongo != nil
when anoSocketSync:
require mongo.connect
else:
require waitfor mongo.connect
db = mongo["temptest"]
require db != nil
sync()
var tempcoll = db["test"]
let
currtime = now().toTime
msg = "こんにちは、isekai"
truthy = true
embedobj = bson({
"type": "kawaii",
name: "Est",
form: "Sword",
})
test "Reconnect to enable replication set writing":
skip()
mongo.slaveOk
#require waitfor mongo.connect
db = mongo["temptest"]
require db != nil
#sync()
#mongo.slaveOk
tempcoll = db["test"]
check true
test "Retry inserting to database":
tempcoll = db["test"]
let b = bson({
entry: currtime,
msg: msg,
truthness: truthy,
embedded: embedobj,
})
when anoSocketSync:
let wr = tempcoll.insert(@[b])
else:
let wr = waitfor tempcoll.insert(@[b])
wr.success.reasonedCheck("Retry tempcoll.insert", wr.reason)
# apparently in some mongodb version, there's this problem
# https://dba.stackexchange.com/questions/179616/mongodb-hangs-up-on-shutdown
# if the problem persists, this replication action test would be disabled.
when anoSocketSync:
discard mongo.shutdown(timeout = 10, force = true)
else:
discard waitfor mongo.shutdown(timeout = 10, force = true)
mongo.close
processes.cleanup
sleep 3000
cleanupSSL()
cleanMongoTemp()