-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
145 lines (115 loc) · 3.22 KB
/
main_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
package kohaku
import (
"context"
"log"
"os"
"testing"
ch "github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/clickhouse"
)
const (
clickhouseUser = "default"
clickhousePassword = "default"
clickhouseDB = "default"
)
var (
conn *driver.Conn
addr []string
server *Server
config = &Config{
TLSFullchainFile: "cert/server/server.pem",
TLSPrivkeyFile: "cert/server/server.key",
TLSVerifyCacertPath: "cert/client/ca.pem",
HTTPS: true,
ListenAddr: "0.0.0.0",
ListenPort: 15890,
}
)
// https://github.com/rilldata/rill/blob/65e7cb07060deab31ef1fef32e345d64d9611cb4/runtime/queries/metricsview_toplist_test.go#L25
func TestMain(m *testing.M) {
ctx := context.Background()
clickHouseContainer, err := clickhouse.RunContainer(ctx,
testcontainers.WithImage("clickhouse/clickhouse-server:latest"),
clickhouse.WithUsername(clickhouseUser),
clickhouse.WithPassword(clickhousePassword),
clickhouse.WithDatabase(clickhouseDB),
)
if err != nil {
log.Fatalf("failed to start container: %s", err)
}
defer func() {
if err := clickHouseContainer.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}
}()
// state, err := clickHouseContainer.State(ctx)
// if err != nil {
// log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
// }
host, err := clickHouseContainer.Host(ctx)
if err != nil {
log.Fatalf("failed to get container host: %s", err)
}
port, err := clickHouseContainer.MappedPort(ctx, "9000")
if err != nil {
log.Fatalf("failed to get container port: %s", err)
}
addr = []string{host + ":" + port.Port()}
conn, err := ch.Open(&ch.Options{
Addr: addr,
Auth: ch.Auth{
Username: clickhouseUser,
Password: clickhousePassword,
Database: clickhouseDB,
},
})
if err != nil {
log.Fatalf("failed to open ClickHouse connection: %s", err)
}
err = conn.Ping(ctx)
if err != nil {
log.Fatalf("failed to ping ClickHouse: %s", err)
}
// ここでテストを実行する
code := m.Run()
// 処理が終わったら、deferによってコンテナが自動的に停止して削除される
os.Exit(code)
}
func open() driver.Conn {
conn, err := ch.Open(&ch.Options{
Addr: addr,
Auth: ch.Auth{
Username: clickhouseUser,
Password: clickhousePassword,
Database: clickhouseDB,
},
})
if err != nil {
log.Fatalf("failed to open ClickHouse connection: %s", err)
}
return conn
}
func TestDummy(t *testing.T) {
ctx := context.Background()
var err error
conn := open()
err = conn.Exec(ctx, "CREATE TABLE IF NOT EXISTS test (id UInt32, name String) ENGINE = Memory")
assert.NoError(t, err)
err = conn.Exec(ctx, "INSERT INTO test (id, name) VALUES (1, 'Alice')")
assert.NoError(t, err)
rows, err := conn.Query(ctx, "SELECT * FROM test")
assert.NoError(t, err)
assert.Len(t, rows.Columns(), 2)
log.Println("ダミーのテストが実行されました。")
}
func newTestServer(c *Config, conn driver.Conn) *Server {
s := &Server{
config: c,
conn: conn,
}
s.setupEchoServer()
return s
}