pangolin_exporter

Форк
0
228 строк · 5.8 Кб
1
// Copyright 2022 The Prometheus Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13

14
package config
15

16
import (
17
	"net/url"
18
	"reflect"
19
	"testing"
20
)
21

22
// Test_dsn_String is designed to test different dsn combinations for their string representation.
23
// dsn.String() is designed to be safe to print, redacting any password information and these test
24
// cases are intended to cover known cases.
25
func Test_dsn_String(t *testing.T) {
26
	type fields struct {
27
		scheme   string
28
		username string
29
		password string
30
		host     string
31
		path     string
32
		query    url.Values
33
	}
34
	tests := []struct {
35
		name   string
36
		fields fields
37
		want   string
38
	}{
39
		{
40
			name: "Without Password",
41
			fields: fields{
42
				scheme:   "postgresql",
43
				username: "test",
44
				host:     "localhost:5432",
45
				query:    url.Values{},
46
			},
47
			want: "postgresql://test@localhost:5432?",
48
		},
49
		{
50
			name: "With Password",
51
			fields: fields{
52
				scheme:   "postgresql",
53
				username: "test",
54
				password: "supersecret",
55
				host:     "localhost:5432",
56
				query:    url.Values{},
57
			},
58
			want: "postgresql://test:******@localhost:5432?",
59
		},
60
		{
61
			name: "With Password and Query String",
62
			fields: fields{
63
				scheme:   "postgresql",
64
				username: "test",
65
				password: "supersecret",
66
				host:     "localhost:5432",
67
				query: url.Values{
68
					"ssldisable": []string{"true"},
69
				},
70
			},
71
			want: "postgresql://test:******@localhost:5432?ssldisable=true",
72
		},
73
		{
74
			name: "With Password, Path, and Query String",
75
			fields: fields{
76
				scheme:   "postgresql",
77
				username: "test",
78
				password: "supersecret",
79
				host:     "localhost:5432",
80
				path:     "/somevalue",
81
				query: url.Values{
82
					"ssldisable": []string{"true"},
83
				},
84
			},
85
			want: "postgresql://test:******@localhost:5432/somevalue?ssldisable=true",
86
		},
87
	}
88
	for _, tt := range tests {
89
		t.Run(tt.name, func(t *testing.T) {
90
			d := DSN{
91
				scheme:   tt.fields.scheme,
92
				username: tt.fields.username,
93
				password: tt.fields.password,
94
				host:     tt.fields.host,
95
				path:     tt.fields.path,
96
				query:    tt.fields.query,
97
			}
98
			if got := d.String(); got != tt.want {
99
				t.Errorf("dsn.String() = %v, want %v", got, tt.want)
100
			}
101
		})
102
	}
103
}
104

105
// Test_dsnFromString tests the dsnFromString function with known variations
106
// of connection string inputs to ensure that it properly parses the input into
107
// a dsn.
108
func Test_dsnFromString(t *testing.T) {
109

110
	tests := []struct {
111
		name    string
112
		input   string
113
		want    DSN
114
		wantErr bool
115
	}{
116
		{
117
			name:  "Key value with password",
118
			input: "host=host.example.com user=postgres port=5432 password=s3cr3t",
119
			want: DSN{
120
				scheme:   "postgresql",
121
				host:     "host.example.com:5432",
122
				username: "postgres",
123
				password: "s3cr3t",
124
				query:    url.Values{},
125
			},
126
			wantErr: false,
127
		},
128
		{
129
			name:  "Key value with quoted password and space",
130
			input: "host=host.example.com user=postgres port=5432 password=\"s3cr 3t\"",
131
			want: DSN{
132
				scheme:   "postgresql",
133
				host:     "host.example.com:5432",
134
				username: "postgres",
135
				password: "s3cr 3t",
136
				query:    url.Values{},
137
			},
138
			wantErr: false,
139
		},
140
		{
141
			name:  "Key value with different order",
142
			input: "password=abcde host=host.example.com user=postgres port=5432",
143
			want: DSN{
144
				scheme:   "postgresql",
145
				host:     "host.example.com:5432",
146
				username: "postgres",
147
				password: "abcde",
148
				query:    url.Values{},
149
			},
150
			wantErr: false,
151
		},
152
		{
153
			name:  "Key value with different order, quoted password, duplicate password",
154
			input: "password=abcde host=host.example.com user=postgres port=5432 password=\"s3cr 3t\"",
155
			want: DSN{
156
				scheme:   "postgresql",
157
				host:     "host.example.com:5432",
158
				username: "postgres",
159
				password: "s3cr 3t",
160
				query:    url.Values{},
161
			},
162
			wantErr: false,
163
		},
164
		{
165
			name:  "URL with user in query string",
166
			input: "postgresql://host.example.com:5432/tsdb?user=postgres",
167
			want: DSN{
168
				scheme:   "postgresql",
169
				host:     "host.example.com:5432",
170
				path:     "/tsdb",
171
				query:    url.Values{},
172
				username: "postgres",
173
			},
174
			wantErr: false,
175
		},
176
		{
177
			name:  "URL with user and password",
178
			input: "postgresql://user:s3cret@host.example.com:5432/tsdb?user=postgres",
179
			want: DSN{
180
				scheme:   "postgresql",
181
				host:     "host.example.com:5432",
182
				path:     "/tsdb",
183
				query:    url.Values{},
184
				username: "user",
185
				password: "s3cret",
186
			},
187
			wantErr: false,
188
		},
189
		{
190
			name:  "Alternative URL prefix",
191
			input: "postgres://user:s3cret@host.example.com:5432/tsdb?user=postgres",
192
			want: DSN{
193
				scheme:   "postgres",
194
				host:     "host.example.com:5432",
195
				path:     "/tsdb",
196
				query:    url.Values{},
197
				username: "user",
198
				password: "s3cret",
199
			},
200
			wantErr: false,
201
		},
202
		{
203
			name:  "URL with user and password in query string",
204
			input: "postgresql://host.example.com:5432/tsdb?user=postgres&password=s3cr3t",
205
			want: DSN{
206
				scheme:   "postgresql",
207
				host:     "host.example.com:5432",
208
				path:     "/tsdb",
209
				query:    url.Values{},
210
				username: "postgres",
211
				password: "s3cr3t",
212
			},
213
			wantErr: false,
214
		},
215
	}
216
	for _, tt := range tests {
217
		t.Run(tt.name, func(t *testing.T) {
218
			got, err := dsnFromString(tt.input)
219
			if (err != nil) != tt.wantErr {
220
				t.Errorf("dsnFromString() error = %v, wantErr %v", err, tt.wantErr)
221
				return
222
			}
223
			if !reflect.DeepEqual(got, tt.want) {
224
				t.Errorf("dsnFromString() = %+v, want %+v", got, tt.want)
225
			}
226
		})
227
	}
228
}
229

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.