/
connector_test.go
167 lines (152 loc) · 3.7 KB
/
connector_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
162
163
164
165
166
167
package connector
import (
"context"
"database/sql"
"fmt"
"github.com/lib/pq"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"net"
"net/url"
"strings"
"testing"
)
type PostgresContainer struct {
testcontainers.Container
Host string
Port string
}
func NewPostgresContainer(ctx context.Context) (*PostgresContainer, error) {
req := testcontainers.ContainerRequest{
Image: "postgres:13.7",
ExposedPorts: []string{"5432/tcp"},
Env: map[string]string{"POSTGRES_PASSWORD": "pa$$w0rd"},
WaitingFor: wait.ForListeningPort("5432/tcp"),
}
container, err := testcontainers.GenericContainer(
ctx,
testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
return nil, err
}
mappedPort, err := container.MappedPort(ctx, "5432")
if err != nil {
return nil, err
}
hostIP, err := container.Host(ctx)
if err != nil {
return nil, err
}
postgresContainer := PostgresContainer{
Container: container,
Host: hostIP,
Port: mappedPort.Port()}
return &postgresContainer, nil
}
func (c *PostgresContainer) Shutdown() {
_ = c.Terminate(context.Background())
}
type TestCredentialsProvider struct {
host string
port string
passwords []string
count int
}
func (p *TestCredentialsProvider) GetDataSourceName() (string, error) {
withPassword := func(password string) string {
rawUrl := fmt.Sprintf("postgresql://%s:%s/postgres", p.host, p.port)
dsn, _ := url.Parse(rawUrl)
query := dsn.Query()
query.Add("user", "postgres")
query.Add("password", password)
query.Add("sslmode", "disable")
dsn.RawQuery = query.Encode()
return dsn.String()
}
password := p.passwords[p.count]
p.count++
return withPassword(password), nil
}
func (p *TestCredentialsProvider) Retries() int {
return len(p.passwords)
}
func TestConnector(t *testing.T) {
container, err := NewPostgresContainer(context.Background())
if err != nil {
t.Fatal(err)
}
defer container.Shutdown()
type TestCase struct {
provider CredentialsProvider
expectedErr error
}
testCases := []TestCase{
{
provider: &TestCredentialsProvider{
host: container.Host,
port: container.Port,
passwords: []string{"pa$$w0rd"},
},
expectedErr: nil,
},
{
provider: &TestCredentialsProvider{
host: container.Host,
port: container.Port,
passwords: []string{"wrong-password"},
},
expectedErr: &pq.Error{
Code: "28P01",
Message: "password authentication failed for user \"postgres\"",
},
},
{
provider: &TestCredentialsProvider{
host: container.Host,
port: container.Port,
passwords: []string{"wrong-password-0", "wrong-password-1"},
},
expectedErr: &pq.Error{
Code: "28P01",
Message: "password authentication failed for user \"postgres\"",
},
},
{
provider: &TestCredentialsProvider{
host: "badhost",
port: container.Port,
passwords: []string{"pa$$word"},
},
expectedErr: &net.OpError{
Op: "dial",
Net: "tcp",
Err: &net.DNSError{
Err: "Temporary failure in name resolution",
Name: "badhost",
IsNotFound: true,
},
},
},
{
provider: &TestCredentialsProvider{
host: container.Host,
port: container.Port,
passwords: []string{"wrong-password-0", "wrong-password-1", "pa$$w0rd"},
},
expectedErr: nil,
},
}
for _, c := range testCases {
connector := NewPgSecretsConnector(c.provider)
db := sql.OpenDB(connector)
_, err = db.Exec("SELECT 1")
if err != c.expectedErr {
if !strings.Contains(err.Error(), c.expectedErr.Error()) {
t.Fatal("unexpected error:", err)
}
}
}
}