forked from cortexproject/cortex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_test.go
63 lines (55 loc) · 1.35 KB
/
db_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
package db
import (
"net/url"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSetPassword(t *testing.T) {
testCases := []struct {
testName string
url string
passwordStr string
isError bool
expected string
}{
{
testName: "Test1",
url: "scheme://[email protected]",
passwordStr: "\n\tpassword\n\n\t",
isError: false,
expected: "scheme://user:[email protected]",
},
{
testName: "Test2",
url: "scheme://host.com",
passwordStr: "\n\tpassword\n\n\t",
isError: true,
expected: "--database.password-file requires username in --database.uri",
},
}
for _, tc := range testCases {
passwordFile, err := os.CreateTemp("", "passwordFile")
if err != nil {
t.Fatalf("error while creating the password file: %v", err)
}
defer os.Remove(passwordFile.Name())
defer passwordFile.Close()
_, err = passwordFile.WriteString(tc.passwordStr)
if err != nil {
t.Fatalf("error while writing to the password file: %v", err)
}
t.Run(tc.testName, func(t *testing.T) {
u, _ := url.Parse(tc.url)
uNew, err := setPassword(u, passwordFile.Name())
if tc.isError {
assert.Error(t, err)
assert.Equal(t, tc.expected, err.Error())
} else {
assert.NoError(t, err)
uExp, _ := url.Parse(tc.expected)
assert.Equal(t, uNew, uExp)
}
})
}
}