-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector_test.go
161 lines (149 loc) · 5.42 KB
/
collector_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
161
package schemedetector
import (
"os"
"sort"
"testing"
"gotest.tools/assert"
)
func testDetect(t *testing.T, result []*Scheme, answers []string) {
if answers == nil {
assert.Check(t, result == nil)
return
}
var resultStrings []string
for _, r := range result {
resultStrings = append(resultStrings, r.String())
}
sort.Strings(resultStrings)
sort.Strings(answers)
assert.DeepEqual(t, resultStrings, answers)
}
func TestCollectorMap(t *testing.T) {
testDetect(t, FromMap(map[string]string{
"API_ENDPOINT": "https://example.com/test",
}), []string{"https://example.com:443/test"})
testDetect(t, FromMap(map[string]string{
"API_ENDPOINT": "https://[email protected]/test",
}), []string{"https://[email protected]:443/test"})
testDetect(t, FromMap(map[string]string{
"DB": "pgsql://user:[email protected]:5432/test",
}), []string{"pgsql://user:[email protected]:5432/test"})
testDetect(t, FromMap(map[string]string{
"DB": "pgsql://[email protected]:5432/test",
}), []string{"pgsql://[email protected]:5432/test"})
testDetect(t, FromMap(map[string]string{
"DB": "pgsql://[email protected]:5432/test?option1=value1&option2=value2",
}), []string{"pgsql://[email protected]:5432/test?option1=value1&option2=value2"})
testDetect(t, FromMap(map[string]string{
"DATABASE_HOST": "127.0.0.1",
"DATABASE_USER": "user",
"DATABASE_PASS": "password",
"DATABASE_PORT": "5432",
"DATABASE_NAME": "test",
}), []string{"pgsql://user:[email protected]:5432/test"})
testDetect(t, FromMap(map[string]string{
"DATABASE_HOST": "127.0.0.1",
"DATABASE_USER": "user",
"DATABASE_PASS": "password",
"DATABASE_PORT": "5432",
"DATABASE_NAME": "test",
"DATABASE2_HOST": "127.0.0.2",
"DATABASE2_USER": "user2",
"DATABASE2_PASS": "password2",
"DATABASE2_PORT": "5432",
"DATABASE2_NAME": "test2",
"API_ENDPOINT": "https://example.com/test",
"API_USER": "api_user",
"API_PASS": "api_password",
}), []string{
"pgsql://user:[email protected]:5432/test",
"pgsql://user2:[email protected]:5432/test2",
"https://api_user:[email protected]:443/test",
})
// Unknown port => unknown schema => no result
testDetect(t, FromMap(map[string]string{
"DATABASE_HOST": "127.0.0.1",
"DATABASE_USER": "user",
"DATABASE_PASS": "password",
"DATABASE_PORT": "0",
"DATABASE_NAME": "test",
}), nil)
// Unknown schema => unknown port => no result
testDetect(t, FromMap(map[string]string{
"ENDPOINT_URL": "unknown://127.0.0.1/test",
}), nil)
// Overwrite is not allowed
testDetect(t, FromMap(map[string]string{
"DB_ADDRESS": "pgsql://user:[email protected]:5432/test",
"DB_PORT": "1234",
"DB_HOST": "localhost",
"DB_NAME": "db",
"DB_USER": "custom",
"DB_PASS": "newpassword",
}), []string{"pgsql://user:[email protected]:5432/test"})
}
func TestCollectorFromEnv(t *testing.T) {
os.Clearenv()
t.Setenv("API_URL", "https://postman:[email protected]/basic-auth")
t.Setenv("DB_ENDPOINT", "pgsql://[email protected]:5432/test?option1=value1&option2=value2")
testDetect(t, FromEnv(), []string{
"https://postman:[email protected]:443/basic-auth",
"pgsql://[email protected]:5432/test?option1=value1&option2=value2",
})
}
func TestCollectorHelpers(t *testing.T) {
os.Clearenv()
assert.Check(t, FromMap(map[string]string{
"DB": "pgsql://[email protected]:5432/test",
})[0].IsIP())
assert.Check(t, !FromMap(map[string]string{
"DB": "pgsql://[email protected]:5432/test",
})[0].IsDNSName())
assert.Check(t, FromMap(map[string]string{
"DB": "pgsql://user@localhost:5432/test",
})[0].IsDNSName())
assert.Check(t, !FromMap(map[string]string{
"DB": "pgsql://user@localhost:5432/test",
})[0].IsIP())
}
func TestComplicated(t *testing.T) {
os.Clearenv()
testDetect(t, FromMap(map[string]string{
"EXAMPLE_API_ENDPOINT": "http://api.example.com",
"EXAMPLE_API_PASSWORD": "123pass",
"EXAMPLE_API_USERNAME": "api_username",
"DATABASE_MAIN_HOST": "base-master.default.svc.cluster.local",
"DATABASE_MAIN_NAME": "api",
"DATABASE_MAIN_PASSWORD": "MAIN_base_PASS",
"DATABASE_MAIN_PORT": "6432",
"DATABASE_MAIN_SCHEMA": "pgsql",
"DATABASE_MAIN_USERNAME": "api_user_main",
"DATABASE_REPLICA_URL": "pgsql://api_user_main:[email protected]:5432/api",
"DATABASE_SECOND_HOST": "second-base.default.svc.cluster.local",
"DATABASE_SECOND_PASSWORD": "SECOND_base_PASS",
"DATABASE_SECOND_PORT": "3306",
"DATABASE_SECOND_SCHEMA": "mysql",
"DATABASE_SECOND_USERNAME": "api_user_second",
"RABBIT_MQ_HOST": "rabbitmq.default.svc.cluster.local",
"RABBIT_MQ_PASSWORD": "rabbitmq_pass",
"RABBIT_MQ_USER": "rabbitmq_user",
"RABBIT_MQ_PORT": "5672",
}), []string{
"amqp://rabbitmq_user:[email protected]:5672",
"http://api_username:[email protected]:80",
"mysql://api_user_second:[email protected]:3306",
"pgsql://api_user_main:[email protected]:6432/api",
"pgsql://api_user_main:[email protected]:5432/api",
})
}
func TestFilter(t *testing.T) {
os.Clearenv()
t.Setenv("SCHEME_DETECTOR_EXCLUDE", ".*PASS")
testDetect(t, FromMap(map[string]string{
"DATABASE_HOST": "127.0.0.1",
"DATABASE_USER": "user",
"DATABASE_PASS": "password",
"DATABASE_PORT": "5432",
"DATABASE_NAME": "test",
}), []string{"pgsql://[email protected]:5432/test"})
}