Skip to content

Commit 2d1f200

Browse files
authored
Create user so ping succeeds in DNS seedlist tests (#750)
1 parent 9f39ed4 commit 2d1f200

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

Tests/MongoSwiftTests/DNSSeedlistTests.swift

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,30 @@ final class DNSSeedlistTests: MongoSwiftTestCase {
120120

121121
print("Running test file \(fileName)...")
122122

123+
// create the user that the test attempts to authenticate as.
124+
if let username = testCase.parsedOptions?["user"]?.stringValue,
125+
let password = testCase.parsedOptions?["password"]?.stringValue
126+
{
127+
let dbName = testCase.parsedOptions?["db"]?.stringValue ?? "admin"
128+
try self.withTestClient { client in
129+
let db = client.db(dbName)
130+
// ignore errors, since the user might not exist already.
131+
_ = try? db.runCommand([
132+
"dropUser": .string(username)
133+
]).wait()
134+
135+
_ = try db.runCommand([
136+
"createUser": .string(username),
137+
"pwd": .string(password),
138+
"mechanisms": [
139+
"SCRAM-SHA-1",
140+
"SCRAM-SHA-256"
141+
],
142+
"roles": []
143+
]).wait()
144+
}
145+
}
146+
123147
let topologyWatcher = TopologyDescriptionWatcher()
124148

125149
let opts: MongoClientOptions?
@@ -143,9 +167,12 @@ final class DNSSeedlistTests: MongoSwiftTestCase {
143167
// eventually matches the list of hosts."
144168
// This needs to be done before the client leaves scope to ensure the SDAM machinery
145169
// keeps running.
146-
if let expectedHosts = testCase.hosts {
147-
expect(topologyWatcher.getLastDescription()?.servers.map { $0.address })
148-
.toEventually(equal(expectedHosts), timeout: 5)
170+
if let expectedHosts = testCase.hosts?.sorted(by: { $0.description < $1.description }) {
171+
expect(
172+
topologyWatcher.getLastDescription()?
173+
.servers.map { $0.address }
174+
.sorted { $0.description < $1.description }
175+
).toEventually(equal(expectedHosts), timeout: 5)
149176
} else if let expectedNumHosts = testCase.numHosts {
150177
expect(topologyWatcher.getLastDescription()?.servers)
151178
.toEventually(haveCount(expectedNumHosts), timeout: 5)

0 commit comments

Comments
 (0)