pangolin_exporter
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
14package config
15
16import (
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.
25func Test_dsn_String(t *testing.T) {
26type fields struct {
27scheme string
28username string
29password string
30host string
31path string
32query url.Values
33}
34tests := []struct {
35name string
36fields fields
37want string
38}{
39{
40name: "Without Password",
41fields: fields{
42scheme: "postgresql",
43username: "test",
44host: "localhost:5432",
45query: url.Values{},
46},
47want: "postgresql://test@localhost:5432?",
48},
49{
50name: "With Password",
51fields: fields{
52scheme: "postgresql",
53username: "test",
54password: "supersecret",
55host: "localhost:5432",
56query: url.Values{},
57},
58want: "postgresql://test:******@localhost:5432?",
59},
60{
61name: "With Password and Query String",
62fields: fields{
63scheme: "postgresql",
64username: "test",
65password: "supersecret",
66host: "localhost:5432",
67query: url.Values{
68"ssldisable": []string{"true"},
69},
70},
71want: "postgresql://test:******@localhost:5432?ssldisable=true",
72},
73{
74name: "With Password, Path, and Query String",
75fields: fields{
76scheme: "postgresql",
77username: "test",
78password: "supersecret",
79host: "localhost:5432",
80path: "/somevalue",
81query: url.Values{
82"ssldisable": []string{"true"},
83},
84},
85want: "postgresql://test:******@localhost:5432/somevalue?ssldisable=true",
86},
87}
88for _, tt := range tests {
89t.Run(tt.name, func(t *testing.T) {
90d := DSN{
91scheme: tt.fields.scheme,
92username: tt.fields.username,
93password: tt.fields.password,
94host: tt.fields.host,
95path: tt.fields.path,
96query: tt.fields.query,
97}
98if got := d.String(); got != tt.want {
99t.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.
108func Test_dsnFromString(t *testing.T) {
109
110tests := []struct {
111name string
112input string
113want DSN
114wantErr bool
115}{
116{
117name: "Key value with password",
118input: "host=host.example.com user=postgres port=5432 password=s3cr3t",
119want: DSN{
120scheme: "postgresql",
121host: "host.example.com:5432",
122username: "postgres",
123password: "s3cr3t",
124query: url.Values{},
125},
126wantErr: false,
127},
128{
129name: "Key value with quoted password and space",
130input: "host=host.example.com user=postgres port=5432 password=\"s3cr 3t\"",
131want: DSN{
132scheme: "postgresql",
133host: "host.example.com:5432",
134username: "postgres",
135password: "s3cr 3t",
136query: url.Values{},
137},
138wantErr: false,
139},
140{
141name: "Key value with different order",
142input: "password=abcde host=host.example.com user=postgres port=5432",
143want: DSN{
144scheme: "postgresql",
145host: "host.example.com:5432",
146username: "postgres",
147password: "abcde",
148query: url.Values{},
149},
150wantErr: false,
151},
152{
153name: "Key value with different order, quoted password, duplicate password",
154input: "password=abcde host=host.example.com user=postgres port=5432 password=\"s3cr 3t\"",
155want: DSN{
156scheme: "postgresql",
157host: "host.example.com:5432",
158username: "postgres",
159password: "s3cr 3t",
160query: url.Values{},
161},
162wantErr: false,
163},
164{
165name: "URL with user in query string",
166input: "postgresql://host.example.com:5432/tsdb?user=postgres",
167want: DSN{
168scheme: "postgresql",
169host: "host.example.com:5432",
170path: "/tsdb",
171query: url.Values{},
172username: "postgres",
173},
174wantErr: false,
175},
176{
177name: "URL with user and password",
178input: "postgresql://user:s3cret@host.example.com:5432/tsdb?user=postgres",
179want: DSN{
180scheme: "postgresql",
181host: "host.example.com:5432",
182path: "/tsdb",
183query: url.Values{},
184username: "user",
185password: "s3cret",
186},
187wantErr: false,
188},
189{
190name: "Alternative URL prefix",
191input: "postgres://user:s3cret@host.example.com:5432/tsdb?user=postgres",
192want: DSN{
193scheme: "postgres",
194host: "host.example.com:5432",
195path: "/tsdb",
196query: url.Values{},
197username: "user",
198password: "s3cret",
199},
200wantErr: false,
201},
202{
203name: "URL with user and password in query string",
204input: "postgresql://host.example.com:5432/tsdb?user=postgres&password=s3cr3t",
205want: DSN{
206scheme: "postgresql",
207host: "host.example.com:5432",
208path: "/tsdb",
209query: url.Values{},
210username: "postgres",
211password: "s3cr3t",
212},
213wantErr: false,
214},
215}
216for _, tt := range tests {
217t.Run(tt.name, func(t *testing.T) {
218got, err := dsnFromString(tt.input)
219if (err != nil) != tt.wantErr {
220t.Errorf("dsnFromString() error = %v, wantErr %v", err, tt.wantErr)
221return
222}
223if !reflect.DeepEqual(got, tt.want) {
224t.Errorf("dsnFromString() = %+v, want %+v", got, tt.want)
225}
226})
227}
228}
229