Newer
Older
Garet Halliday
committed
"strconv"
Garet Halliday
committed
"testing"
"gfx.cafe/gfx/pggat/lib/auth/credentials"
"gfx.cafe/gfx/pggat/lib/gat"
"gfx.cafe/gfx/pggat/lib/gat/gatcaddyfile"
pool_handler "gfx.cafe/gfx/pggat/lib/gat/handlers/pool"
"gfx.cafe/gfx/pggat/lib/gat/handlers/rewrite_password"
"gfx.cafe/gfx/pggat/lib/gat/matchers"
"gfx.cafe/gfx/pggat/lib/gat/pool"
"gfx.cafe/gfx/pggat/lib/gat/pool/recipe"
"gfx.cafe/gfx/pggat/lib/gat/poolers/session"
"gfx.cafe/gfx/pggat/lib/gat/poolers/transaction"
"gfx.cafe/gfx/pggat/test"
"gfx.cafe/gfx/pggat/test/tests"
type dialer struct {
Address string
Username string
Password string
Database string
}
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
var nextPort int
func randAddress() string {
nextPort++
return "/tmp/.s.PGGAT." + strconv.Itoa(nextPort)
}
func resolveNetwork(address string) string {
if strings.HasPrefix(address, "/") {
return "unix"
} else {
return "tcp"
}
}
func randPassword() (string, error) {
var b [20]byte
_, err := rand.Read(b[:])
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b[:]), nil
}
func createServer(parent dialer, poolers map[string]caddy.Module) (server gat.ServerConfig, dialers map[string]dialer, err error) {
address := randAddress()
server.Listen = []gat.ListenerConfig{
{
Address: address,
},
}
var password string
password, err = randPassword()
if err != nil {
return
}
server.Routes = append(
server.Routes,
gat.RouteConfig{
Handle: gatcaddyfile.JSONModuleObject(
&rewrite_password.Module{
Password: password,
},
gatcaddyfile.Handler,
"handler",
nil,
),
},
)
for name, pooler := range poolers {
p := pool_handler.Module{
Config: pool_handler.Config{
Pooler: gatcaddyfile.JSONModuleObject(
pooler,
gatcaddyfile.Pooler,
"pooler",
nil,
),
ServerAddress: parent.Address,
ServerUsername: parent.Username,
ServerPassword: parent.Password,
ServerDatabase: parent.Database,
},
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
server.Routes = append(server.Routes, gat.RouteConfig{
Match: gatcaddyfile.JSONModuleObject(
&matchers.Database{
Database: name,
},
gatcaddyfile.Matcher,
"matcher",
nil,
),
Handle: gatcaddyfile.JSONModuleObject(
&p,
gatcaddyfile.Handler,
"handler",
nil,
),
})
if dialers == nil {
dialers = make(map[string]dialer)
}
dialers[name] = dialer{
Address: address,
Username: "pooler",
Password: password,
Database: name,
}
}
return
}
func daisyChain(config *gat.Config, control dialer, n int) (dialer, error) {
for i := 0; i < n; i++ {
pooler = &transaction.Module{
ManagementConfig: poolConfig,
}
poolConfig.ServerResetQuery = "DISCARD ALL"
pooler = &session.Module{
ManagementConfig: poolConfig,
}
server, dialers, err := createServer(control, map[string]caddy.Module{
"pool": pooler,
})
control = dialers["pool"]
config.Servers = append(config.Servers, server)
return control, nil
}
func TestTester(t *testing.T) {
Network: "tcp",
Address: "localhost:5432",
Username: "postgres",
Credentials: credentials.Cleartext{
Garet Halliday
committed
Username: "postgres",
config := gat.Config{}
parent, err := daisyChain(&config, dialer{
Address: "localhost:5432",
Username: "postgres",
Password: "password",
Database: "postgres",
}, 16)
server, dialers, err := createServer(parent, map[string]caddy.Module{
"transaction": &transaction.Module{},
"session": &session.Module{
ManagementConfig: pool.ManagementConfig{
ServerResetQuery: "discard all",
},
},
})
config.Servers = append(config.Servers, server)
transactionDialer := recipe.Dialer{
Network: resolveNetwork(dialers["transaction"].Address),
Address: dialers["transaction"].Address,
Username: dialers["transaction"].Username,
Credentials: credentials.FromString(
dialers["transaction"].Username,
dialers["transaction"].Password,
),
Database: "transaction",
sessionDialer := recipe.Dialer{
Network: resolveNetwork(dialers["transaction"].Address),
Address: dialers["session"].Address,
Username: dialers["session"].Username,
Credentials: credentials.FromString(
dialers["session"].Username,
dialers["session"].Password,
),
Database: "session",
caddyConfig := caddy.Config{
AppsRaw: caddy.ModuleMap{
"pggat": caddyconfig.JSON(config, nil),
},
}
tester := test.NewTester(test.Config{
"control": control,
"transaction": transactionDialer,
"session": sessionDialer,
tests.EQP0,
tests.EQP1,
tests.EQP2,
tests.EQP3,
tests.EQP4,
tests.EQP5,
tests.EQP6,
tests.EQP7,
tests.CopyIn0,
tests.CopyIn1,
Garet Halliday
committed
tests.DiscardAll,