2022-09-21 15:50:48 +00:00
|
|
|
//go:build detectors && integration
|
|
|
|
// +build detectors,integration
|
|
|
|
|
|
|
|
package jdbc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-11-07 01:17:37 +00:00
|
|
|
"fmt"
|
2024-02-28 16:51:37 +00:00
|
|
|
"log"
|
2022-09-21 15:50:48 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-02-28 16:51:37 +00:00
|
|
|
"github.com/brianvoe/gofakeit/v6"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
"github.com/testcontainers/testcontainers-go/modules/postgres"
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
2022-09-21 15:50:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPostgres(t *testing.T) {
|
2023-07-19 20:57:57 +00:00
|
|
|
type result struct {
|
2024-02-28 16:51:37 +00:00
|
|
|
ParseErr bool
|
|
|
|
PingOk bool
|
|
|
|
PingDeterminate bool
|
2023-07-19 20:57:57 +00:00
|
|
|
}
|
2024-02-28 16:51:37 +00:00
|
|
|
|
|
|
|
user := gofakeit.Username()
|
|
|
|
pass := gofakeit.Password(true, true, true, false, false, 32)
|
|
|
|
dbName := gofakeit.Word()
|
|
|
|
|
|
|
|
t.Log("user: ", user)
|
|
|
|
t.Log("dbName: ", dbName)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
postgresContainer, err := postgres.RunContainer(ctx,
|
|
|
|
testcontainers.WithImage("postgres:13-alpine"),
|
|
|
|
postgres.WithDatabase(dbName),
|
|
|
|
postgres.WithUsername(user),
|
|
|
|
postgres.WithPassword(pass),
|
|
|
|
testcontainers.WithWaitStrategy(
|
|
|
|
wait.ForLog("database system is ready to accept connections").
|
|
|
|
WithOccurrence(2).
|
|
|
|
WithStartupTimeout(5*time.Second)),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
host, err := postgresContainer.Host(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
port, err := postgresContainer.MappedPort(ctx, "5432")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to start container: %s", err)
|
|
|
|
}
|
|
|
|
defer postgresContainer.Terminate(ctx)
|
|
|
|
|
2022-09-21 15:50:48 +00:00
|
|
|
tests := []struct {
|
2024-02-28 16:51:37 +00:00
|
|
|
name string
|
2023-07-19 20:57:57 +00:00
|
|
|
input string
|
|
|
|
want result
|
2022-09-21 15:50:48 +00:00
|
|
|
}{
|
|
|
|
{
|
2024-02-28 16:51:37 +00:00
|
|
|
name: "invalid password",
|
|
|
|
input: fmt.Sprintf("//%s:%s/foo?sslmode=disable&password=foo", host, port.Port()),
|
|
|
|
want: result{PingOk: false, PingDeterminate: true},
|
2023-07-19 20:57:57 +00:00
|
|
|
},
|
2023-11-07 01:17:37 +00:00
|
|
|
{
|
2024-02-28 16:51:37 +00:00
|
|
|
name: "valid password - no user name",
|
|
|
|
input: fmt.Sprintf("//%s:%s/foo?sslmode=disable&password=%s", host, port.Port(), pass),
|
|
|
|
want: result{PingOk: false, PingDeterminate: true},
|
2023-11-07 01:17:37 +00:00
|
|
|
},
|
2023-07-19 20:57:57 +00:00
|
|
|
{
|
2024-02-28 16:51:37 +00:00
|
|
|
name: "exact username and password, wrong db",
|
|
|
|
input: fmt.Sprintf("//%s:%s@%s:%s/foo?sslmode=disable", user, pass, host, port.Port()),
|
|
|
|
want: result{PingOk: true, PingDeterminate: true},
|
2022-09-21 15:50:48 +00:00
|
|
|
},
|
2023-11-07 01:17:37 +00:00
|
|
|
{
|
2024-02-28 16:51:37 +00:00
|
|
|
name: "exact username and password, no db",
|
|
|
|
input: fmt.Sprintf("//%s:%s@%s:%s?sslmode=disable", user, pass, host, port.Port()),
|
|
|
|
want: result{PingOk: true, PingDeterminate: true},
|
2023-11-07 01:17:37 +00:00
|
|
|
},
|
2022-09-21 15:50:48 +00:00
|
|
|
{
|
2024-02-28 16:51:37 +00:00
|
|
|
name: "invalid user name",
|
|
|
|
input: fmt.Sprintf("//%s:%s/foo?sslmode=disable&user=foo&password=%s", host, port.Port(), pass),
|
|
|
|
want: result{PingOk: false, PingDeterminate: true},
|
2022-09-21 15:50:48 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-28 16:51:37 +00:00
|
|
|
name: "invalid hostname",
|
|
|
|
input: fmt.Sprintf("//badhost:%s/foo?sslmode=disable&user=foo&password=%s", port.Port(), pass),
|
|
|
|
want: result{PingOk: false, PingDeterminate: false},
|
2022-09-21 15:50:48 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-28 16:51:37 +00:00
|
|
|
name: "no username, password",
|
|
|
|
input: fmt.Sprintf("//%s:%s/foo?password=%s", host, port.Port(), pass),
|
|
|
|
want: result{PingOk: false, PingDeterminate: false},
|
2022-09-21 15:50:48 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-28 16:51:37 +00:00
|
|
|
name: "db, password - no username",
|
|
|
|
input: fmt.Sprintf("//%s:%s/foo?sslmode=disable&password=%s", host, port.Port(), pass),
|
|
|
|
want: result{PingOk: false, PingDeterminate: true},
|
2022-09-21 15:50:48 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-28 16:51:37 +00:00
|
|
|
name: "invalid format",
|
|
|
|
input: "invalid",
|
|
|
|
want: result{ParseErr: true},
|
2022-09-21 15:50:48 +00:00
|
|
|
},
|
|
|
|
{
|
2024-02-28 16:51:37 +00:00
|
|
|
name: "normal connect with generated username and password",
|
|
|
|
input: fmt.Sprintf("//%s:%s@%s:%s/%s?sslmode=disable", user, pass, host, port.Port(), dbName),
|
|
|
|
want: result{PingOk: true, PingDeterminate: true},
|
2022-09-21 15:50:48 +00:00
|
|
|
},
|
|
|
|
}
|
2024-02-28 16:51:37 +00:00
|
|
|
|
2022-09-21 15:50:48 +00:00
|
|
|
for _, tt := range tests {
|
2024-02-28 16:51:37 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-09-21 15:50:48 +00:00
|
|
|
j, err := parsePostgres(tt.input)
|
2023-07-19 20:57:57 +00:00
|
|
|
if err != nil {
|
2024-02-28 16:51:37 +00:00
|
|
|
got := result{ParseErr: true}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(tt.want, got); diff != "" {
|
|
|
|
t.Errorf("%s: (-want +got)\n%s", tt.name, diff)
|
|
|
|
t.Errorf("error is: %v", err)
|
|
|
|
}
|
2022-09-21 15:50:48 +00:00
|
|
|
return
|
|
|
|
}
|
2023-07-19 20:57:57 +00:00
|
|
|
|
2024-02-28 16:51:37 +00:00
|
|
|
pr := j.ping(ctx)
|
2022-09-21 15:50:48 +00:00
|
|
|
|
2024-02-28 16:51:37 +00:00
|
|
|
got := result{PingOk: pr.err == nil, PingDeterminate: pr.determinate}
|
2022-09-21 15:50:48 +00:00
|
|
|
|
2024-02-28 16:51:37 +00:00
|
|
|
if diff := cmp.Diff(tt.want, got); diff != "" {
|
|
|
|
t.Errorf("%s: (-want +got)\n%s", tt.name, diff)
|
|
|
|
t.Errorf("error is: %v", pr.err)
|
|
|
|
}
|
|
|
|
})
|
2022-09-21 15:50:48 +00:00
|
|
|
}
|
|
|
|
}
|