-
Notifications
You must be signed in to change notification settings - Fork 18
/
clickhouse_dockertest.go
83 lines (75 loc) · 1.86 KB
/
clickhouse_dockertest.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
package Ch
import (
"database/sql"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ory/dockertest/v3"
"github.com/kokizzu/gotro/D"
)
type ChDockerTest struct {
User string
Password string
Database string
Image string
Port string
pool *D.DockerTest
}
// ImageVersion https://hub.docker.com/r/clickhouse/clickhouse-server
func (in *ChDockerTest) ImageVersion(pool *D.DockerTest, version string) *dockertest.RunOptions {
in.pool = pool
in.SetDefaults(version)
return &dockertest.RunOptions{
Repository: `clickhouse/clickhouse-server`,
Name: `dockertest-clickhouse-` + pool.Uniq,
Tag: in.Image,
NetworkID: pool.Network.ID,
Env: []string{
`CLICKHOUSE_USER=` + in.User,
`CLICKHOUSE_PASSWORD=` + in.Password,
`CLICKHOUSE_DB=` + in.Database,
`CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1`,
},
}
}
func (in *ChDockerTest) ImageLatest(pool *D.DockerTest) *dockertest.RunOptions {
return in.ImageVersion(pool, `latest`)
}
func (in *ChDockerTest) SetDefaults(img string) {
if in.Image == `` {
in.Image = img
}
if in.User == `` {
in.User = `chuser`
}
if in.Password == `` {
in.Password = `chpass`
}
if in.Database == `` {
in.Database = `chdb`
}
}
func (in *ChDockerTest) ConnectCheck(res *dockertest.Resource) (conn *sql.DB, err error) {
in.Port = res.GetPort("9000/tcp")
hostPort := in.pool.HostPort(in.Port)
conn = clickhouse.OpenDB(&clickhouse.Options{
Addr: []string{hostPort},
Auth: clickhouse.Auth{
Database: in.Database,
Username: in.User,
Password: in.Password,
},
Settings: clickhouse.Settings{
`max_execution_time`: 60,
},
DialTimeout: 5 * time.Second,
Compression: &clickhouse.Compression{
Method: clickhouse.CompressionLZ4,
},
Debug: true,
})
conn.SetConnMaxLifetime(time.Hour)
conn.SetMaxIdleConns(5)
conn.SetMaxOpenConns(10)
err = conn.Ping()
return conn, err
}