Skip to content

Commit 8bca980

Browse files
committed
e2e tests: update clickhouse versions (21.3, 22.8, latest)
1 parent aebdd8c commit 8bca980

File tree

11 files changed

+444
-154
lines changed

11 files changed

+444
-154
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,4 @@ jobs:
131131
- name: Integration tests
132132
run: |
133133
make e2e-test
134-
./e2e-test -config tests
134+
./e2e-test -config tests -abort -rmi

cmd/e2e-test/carbon_clickhouse.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type CarbonClickhouse struct {
2121
cmd *exec.Cmd `toml:"-"`
2222
}
2323

24-
func (c *CarbonClickhouse) Start(clickhouseAddr string) error {
24+
func (c *CarbonClickhouse) Start(clickhouseURL string) error {
2525
if c.cmd != nil {
2626
return fmt.Errorf("carbon-clickhouse already started")
2727
}
@@ -52,13 +52,13 @@ func (c *CarbonClickhouse) Start(clickhouseAddr string) error {
5252
return err
5353
}
5454
param := struct {
55-
CLICKHOUSE_ADDR string
56-
CCH_STORE_DIR string
57-
CCH_ADDR string
55+
CLICKHOUSE_URL string
56+
CCH_STORE_DIR string
57+
CCH_ADDR string
5858
}{
59-
CLICKHOUSE_ADDR: clickhouseAddr,
60-
CCH_STORE_DIR: c.storeDir,
61-
CCH_ADDR: c.address,
59+
CLICKHOUSE_URL: clickhouseURL,
60+
CCH_STORE_DIR: c.storeDir,
61+
CCH_ADDR: c.address,
6262
}
6363

6464
c.configFile = path.Join(c.storeDir, "carbon-clickhouse.conf")

cmd/e2e-test/clickhouse.go

Lines changed: 153 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,224 @@
11
package main
22

33
import (
4+
"bytes"
5+
"errors"
46
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
"os"
510
"os/exec"
11+
"strconv"
12+
"strings"
13+
"time"
14+
15+
"github.com/msaf1980/go-stringutils"
616
)
717

818
var ClickhouseContainerName = "clickhouse-server-gch-test"
19+
var ClickhouseOldImage = "yandex/clickhouse-server"
20+
var ClickhouseDefaultImage = "clickhouse/clickhouse-server"
921

1022
type Clickhouse struct {
1123
Version string `toml:"version"`
1224
Dir string `toml:"dir"`
1325

14-
Docker string `toml:"docker"`
1526
DockerImage string `toml:"image"`
1627

17-
address string `toml:"-"`
18-
container string `toml:"-"`
28+
TZ string `toml:"tz"` // override timezone
29+
30+
httpAddress string `toml:"-"`
31+
url string `toml:"-"`
32+
container string `toml:"-"`
1933
}
2034

21-
func (c *Clickhouse) Start() (error, string) {
22-
if len(c.Version) == 0 {
23-
return fmt.Errorf("version not set"), ""
35+
func (c *Clickhouse) CheckConfig(rootDir string) error {
36+
if c.Version == "" {
37+
c.Version = "latest"
2438
}
2539
if len(c.Dir) == 0 {
26-
return fmt.Errorf("dir not set"), ""
40+
return ErrNoSetDir
2741
}
28-
if len(c.Docker) == 0 {
29-
c.Docker = "docker"
42+
if !strings.HasPrefix(c.Dir, "/") {
43+
c.Dir = rootDir + "/" + c.Dir
3044
}
31-
if len(c.DockerImage) == 0 {
32-
c.DockerImage = "yandex/clickhouse-server"
45+
46+
if c.DockerImage == "" {
47+
if c.Version == "latest" {
48+
c.DockerImage = ClickhouseDefaultImage
49+
} else {
50+
splitV := strings.Split(c.Version, ".")
51+
majorV, err := strconv.Atoi(splitV[0])
52+
if err != nil {
53+
c.DockerImage = ClickhouseDefaultImage
54+
} else if majorV >= 21 {
55+
c.DockerImage = ClickhouseDefaultImage
56+
} else {
57+
c.DockerImage = ClickhouseOldImage
58+
}
59+
}
3360
}
61+
return nil
62+
}
63+
64+
func (c *Clickhouse) Key() string {
65+
return c.DockerImage + ":" + c.Version + " " + c.Dir + " TZ " + c.TZ
66+
}
67+
68+
func (c *Clickhouse) Start() (string, error) {
3469
var err error
35-
c.address, err = getFreeTCPPort("")
70+
c.httpAddress, err = getFreeTCPPort("")
3671
if err != nil {
37-
return err, ""
72+
return "", err
3873
}
74+
c.url = "http://" + c.httpAddress
3975

4076
c.container = ClickhouseContainerName
4177

78+
// tz, _ := localTZLocationName()
79+
4280
chStart := []string{"run", "-d",
4381
"--name", c.container,
4482
"--ulimit", "nofile=262144:262144",
45-
"-p", c.address + ":8123",
46-
//"-e", "TZ=UTC",
83+
"-p", c.httpAddress + ":8123",
84+
// "-e", "TZ=" + tz, // workaround for TZ=":/etc/localtime"
4785
"-v", c.Dir + "/config.xml:/etc/clickhouse-server/config.xml",
4886
"-v", c.Dir + "/users.xml:/etc/clickhouse-server/users.xml",
4987
"-v", c.Dir + "/rollup.xml:/etc/clickhouse-server/config.d/rollup.xml",
5088
"-v", c.Dir + "/init.sql:/docker-entrypoint-initdb.d/init.sql",
51-
c.DockerImage + ":" + c.Version,
89+
}
90+
if c.TZ != "" {
91+
chStart = append(chStart, "-e", "TZ="+c.TZ)
5292
}
5393

54-
cmd := exec.Command(c.Docker, chStart...)
94+
chStart = append(chStart, c.DockerImage+":"+c.Version)
95+
96+
cmd := exec.Command(DockerBinary, chStart...)
5597
out, err := cmd.CombinedOutput()
5698

57-
return err, string(out)
99+
return string(out), err
58100
}
59101

60-
func (c *Clickhouse) Stop(delete bool) (error, string) {
102+
func (c *Clickhouse) Stop(delete bool) (string, error) {
61103
if len(c.container) == 0 {
62-
return nil, ""
104+
return "", nil
63105
}
64106

65107
chStop := []string{"stop", c.container}
66108

67-
cmd := exec.Command(c.Docker, chStop...)
109+
cmd := exec.Command(DockerBinary, chStop...)
68110
out, err := cmd.CombinedOutput()
69111

70112
if err == nil && delete {
71113
return c.Delete()
72114
}
73-
return err, string(out)
115+
return string(out), err
74116
}
75117

76-
func (c *Clickhouse) Delete() (error, string) {
118+
func (c *Clickhouse) Delete() (string, error) {
77119
if len(c.container) == 0 {
78-
return nil, ""
120+
return "", nil
79121
}
80122

81123
chDel := []string{"rm", c.container}
82124

83-
cmd := exec.Command(c.Docker, chDel...)
125+
cmd := exec.Command(DockerBinary, chDel...)
84126
out, err := cmd.CombinedOutput()
85127

86128
if err == nil {
87129
c.container = ""
88130
}
89131

90-
return err, string(out)
132+
return string(out), err
91133
}
92134

93-
func (c *Clickhouse) Address() string {
94-
return c.address
135+
func (c *Clickhouse) URL() string {
136+
return c.url
95137
}
96138

97139
func (c *Clickhouse) Container() string {
98140
return c.container
99141
}
142+
143+
func (c *Clickhouse) Exec(sql string) (bool, string) {
144+
return containerExec(c.container, []string{"sh", "-c", "clickhouse-client -q '" + sql + "'"})
145+
}
146+
147+
func (c *Clickhouse) Query(sql string) (string, error) {
148+
reader := strings.NewReader(sql)
149+
request, err := http.NewRequest("POST", c.URL(), reader)
150+
if err != nil {
151+
return "", err
152+
}
153+
154+
httpClient := http.Client{
155+
Timeout: time.Minute,
156+
}
157+
resp, err := httpClient.Do(request)
158+
if err != nil {
159+
return "", err
160+
}
161+
msg, err := ioutil.ReadAll(resp.Body)
162+
if err != nil {
163+
return "", err
164+
}
165+
if resp.StatusCode != http.StatusOK {
166+
return "", errors.New(resp.Status + ": " + string(bytes.TrimRight(msg, "\n")))
167+
}
168+
return string(msg), nil
169+
}
170+
171+
func (c *Clickhouse) Alive() bool {
172+
if len(c.container) == 0 {
173+
return false
174+
}
175+
req, err := http.DefaultClient.Get(c.url)
176+
if err != nil {
177+
return false
178+
}
179+
return req.StatusCode == http.StatusOK
180+
}
181+
182+
func (c *Clickhouse) CopyLog(destDir string, tail uint64) error {
183+
if len(c.container) == 0 {
184+
return nil
185+
}
186+
dest := destDir + "/clickhouse-server.log"
187+
188+
chArgs := []string{"cp", c.container + ":/var/log/clickhouse-server/clickhouse-server.log", dest}
189+
190+
cmd := exec.Command(DockerBinary, chArgs...)
191+
out, err := cmd.CombinedOutput()
192+
if err != nil {
193+
return errors.New(err.Error() + ": " + string(bytes.TrimRight(out, "\n")))
194+
}
195+
196+
if tail > 0 {
197+
out, _ := exec.Command("tail", "-"+strconv.FormatUint(tail, 10), dest).Output()
198+
fmt.Fprintf(os.Stderr, "CLICKHOUSE-SERVER.LOG %s", stringutils.UnsafeString(out))
199+
}
200+
201+
return nil
202+
}
203+
204+
func (c *Clickhouse) CopyErrLog(destDir string, tail uint64) error {
205+
if len(c.container) == 0 {
206+
return nil
207+
}
208+
dest := destDir + "/clickhouse-server.err.log"
209+
210+
chArgs := []string{"cp", c.container + ":/var/log/clickhouse-server/clickhouse-server.err.log", dest}
211+
212+
cmd := exec.Command(DockerBinary, chArgs...)
213+
out, err := cmd.CombinedOutput()
214+
if err != nil {
215+
return errors.New(err.Error() + ": " + string(bytes.TrimRight(out, "\n")))
216+
}
217+
218+
if tail > 0 {
219+
out, _ := exec.Command("tail", "-"+strconv.FormatUint(tail, 10), dest).Output()
220+
fmt.Fprintf(os.Stderr, "CLICKHOUSE-SERVER.ERR %s", stringutils.UnsafeString(out))
221+
}
222+
223+
return nil
224+
}

cmd/e2e-test/container.go

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,77 @@ import (
55
"strings"
66
)
77

8-
func containerExist(dockerBinary, name string) (bool, string) {
9-
if len(dockerBinary) == 0 {
10-
dockerBinary = "docker"
8+
var DockerBinary string
9+
10+
func imageDelete(image, version string) (bool, string) {
11+
if len(DockerBinary) == 0 {
12+
panic("docker not set")
13+
}
14+
15+
chArgs := []string{"rmi", image + ":" + version}
16+
17+
cmd := exec.Command(DockerBinary, chArgs...)
18+
out, err := cmd.CombinedOutput()
19+
s := strings.Trim(string(out), "\n")
20+
21+
if err == nil {
22+
return true, s
23+
}
24+
25+
return false, err.Error() + ": " + s
26+
}
27+
28+
func containerExist(name string) (bool, string) {
29+
if len(DockerBinary) == 0 {
30+
panic("docker not set")
1131
}
1232

1333
chInspect := []string{"inspect", "--format", "'{{.Name}}'", name}
1434

15-
cmd := exec.Command(dockerBinary, chInspect...)
35+
cmd := exec.Command(DockerBinary, chInspect...)
36+
out, err := cmd.CombinedOutput()
37+
s := strings.Trim(string(out), "\n")
38+
39+
if err == nil {
40+
return true, s
41+
}
42+
43+
return false, err.Error() + ": " + s
44+
}
45+
46+
func containerRemove(name string) (bool, string) {
47+
if len(DockerBinary) == 0 {
48+
panic("docker not set")
49+
}
50+
51+
chInspect := []string{"rm", "-f", name}
52+
53+
cmd := exec.Command(DockerBinary, chInspect...)
54+
out, err := cmd.CombinedOutput()
55+
s := strings.Trim(string(out), "\n")
56+
57+
if err == nil {
58+
return true, s
59+
}
60+
61+
return false, err.Error() + ": " + s
62+
}
63+
64+
func containerExec(name string, args []string) (bool, string) {
65+
if len(DockerBinary) == 0 {
66+
panic("docker not set")
67+
}
68+
69+
dCmd := []string{"exec", name}
70+
dCmd = append(dCmd, args...)
71+
72+
cmd := exec.Command(DockerBinary, dCmd...)
1673
out, err := cmd.CombinedOutput()
1774
s := strings.Trim(string(out), "\n")
1875

1976
if err == nil {
2077
return true, s
2178
}
2279

23-
return false, s
80+
return false, err.Error() + ": " + s
2481
}

0 commit comments

Comments
 (0)