-
Notifications
You must be signed in to change notification settings - Fork 883
/
connstring_spec_test.go
160 lines (128 loc) · 3.63 KB
/
connstring_spec_test.go
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
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package connstring_test
import (
"encoding/json"
"io/ioutil"
"path"
"testing"
"github.com/mongodb/mongo-go-driver/core/connstring"
"github.com/mongodb/mongo-go-driver/internal/testutil/helpers"
"github.com/stretchr/testify/require"
)
type host struct {
Type string
Host string
Port json.Number
}
type auth struct {
Username string
Password *string
DB string
}
type testCase struct {
Description string
URI string
Valid bool
Hosts []host
Auth *auth
Options map[string]interface{}
}
type testContainer struct {
Tests []testCase
}
const connstringTestsDir = "../../data/connection-string/"
// Note a test supporting the deprecated gssapiServiceName property was removed from data/auth/auth_tests.json
const authTestsDir = "../../data/auth/"
func (h *host) toString() string {
switch h.Type {
case "unix":
return h.Host
case "ip_literal":
if len(h.Port) == 0 {
return "[" + h.Host + "]"
} else {
return "[" + h.Host + "]" + ":" + string(h.Port)
}
case "ipv4":
fallthrough
case "hostname":
if len(h.Port) == 0 {
return h.Host
} else {
return h.Host + ":" + string(h.Port)
}
}
return ""
}
func hostsToStrings(hosts []host) []string {
out := make([]string, len(hosts))
for i, host := range hosts {
out[i] = host.toString()
}
return out
}
func runTestsInFile(t *testing.T, dirname string, filename string) {
filepath := path.Join(dirname, filename)
content, err := ioutil.ReadFile(filepath)
require.NoError(t, err)
var container testContainer
require.NoError(t, json.Unmarshal(content, &container))
// Remove ".json" from filename.
filename = filename[:len(filename)-5]
for _, testCase := range container.Tests {
runTest(t, filename, &testCase)
}
}
func runTest(t *testing.T, filename string, test *testCase) {
testName := filename + ":" + test.Description
t.Run(testName, func(t *testing.T) {
cs, err := connstring.Parse(test.URI)
if test.Valid {
require.NoError(t, err)
} else {
require.Error(t, err)
return
}
require.Equal(t, test.URI, cs.Original)
if test.Hosts != nil {
require.Equal(t, hostsToStrings(test.Hosts), cs.Hosts)
}
if test.Auth != nil {
require.Equal(t, test.Auth.Username, cs.Username)
if test.Auth.Password == nil {
require.False(t, cs.PasswordSet)
} else {
require.True(t, cs.PasswordSet)
require.Equal(t, *test.Auth.Password, cs.Password)
}
if test.Auth.DB != cs.Database {
require.Equal(t, test.Auth.DB, cs.AuthSource)
} else {
require.Equal(t, test.Auth.DB, cs.Database)
}
}
// Check that all options are present.
testhelpers.VerifyConnStringOptions(t, cs, test.Options)
// Check that non-present options are unset. This will be redundant with the above checks
// for options that are present.
var ok bool
_, ok = test.Options["maxconnsperhost"]
require.Equal(t, ok, cs.MaxConnsPerHostSet)
_, ok = test.Options["maxidleconnsperhost"]
require.Equal(t, ok, cs.MaxIdleConnsPerHostSet)
require.Equal(t, test.Auth != nil && test.Auth.Password != nil, cs.PasswordSet)
})
}
// Test case for all connection string spec tests.
func TestConnStringSpec(t *testing.T) {
for _, file := range testhelpers.FindJSONFilesInDir(t, connstringTestsDir) {
runTestsInFile(t, connstringTestsDir, file)
}
for _, file := range testhelpers.FindJSONFilesInDir(t, authTestsDir) {
runTestsInFile(t, authTestsDir, file)
}
}