Skip to content

Commit aef60a0

Browse files
authored
Merge pull request #92 from msaf1980/integration_tests
Add integration tests
2 parents 61c3933 + 4872bcb commit aef60a0

File tree

15 files changed

+1204
-4
lines changed

15 files changed

+1204
-4
lines changed

.github/workflows/tests.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,26 @@ jobs:
7575
run: make DEVEL=1 packagecloud-autobuilds
7676
env:
7777
PACKAGECLOUD_TOKEN: ${{ secrets.PACKAGECLOUD_TOKEN }}
78+
79+
integration_tests:
80+
name: Integration tests
81+
runs-on: ubuntu-latest
82+
steps:
83+
84+
- name: Set up Go
85+
uses: actions/setup-go@v2
86+
with:
87+
go-version: ${{ matrix.go }}
88+
89+
- name: Check out code
90+
uses: actions/checkout@v2
91+
92+
- name: Set up Go
93+
uses: actions/setup-go@v2
94+
with:
95+
go-version: ^1
96+
97+
- name: Integration tests
98+
run: |
99+
make e2e-test
100+
./e2e-test -config tests

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ clean:
2424
rm -f *deb *rpm
2525
rm -f sha256sum md5sum
2626

27-
$(NAME): $(wildcard **/*.go)
27+
$(NAME): $(shell find . -name '*.go')
2828
$(GO) build $(MODULE)
2929

30+
e2e-test: $(NAME)
31+
$(GO) build $(MODULE)/cmd/e2e-test
32+
3033
test:
3134
$(GO) test -race ./...
3235

cmd/e2e-test/carbon_clickhouse.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"os/exec"
8+
"path"
9+
"path/filepath"
10+
"syscall"
11+
"text/template"
12+
)
13+
14+
type CarbonClickhouse struct {
15+
Binary string `toml:"binary"`
16+
ConfigTpl string `toml:"template"`
17+
18+
storeDir string `toml:"-"`
19+
configFile string `toml:"-"`
20+
address string `toml:"-"`
21+
cmd *exec.Cmd `toml:"-"`
22+
}
23+
24+
func (c *CarbonClickhouse) Start(clickhouseAddr string) error {
25+
if c.cmd != nil {
26+
return fmt.Errorf("carbon-clickhouse already started")
27+
}
28+
29+
if len(c.Binary) == 0 {
30+
c.Binary = "./carbon-clickhouse"
31+
}
32+
if len(c.ConfigTpl) == 0 {
33+
return fmt.Errorf("carbon-clickhouse config template not set")
34+
}
35+
36+
var err error
37+
c.storeDir, err = ioutil.TempDir("", "carbon-clickhouse")
38+
if err != nil {
39+
return err
40+
}
41+
42+
c.address, err = getFreeTCPPort("")
43+
if err != nil {
44+
c.Cleanup()
45+
return err
46+
}
47+
48+
name := filepath.Base(c.ConfigTpl)
49+
tmpl, err := template.New(name).ParseFiles(c.ConfigTpl)
50+
if err != nil {
51+
c.Cleanup()
52+
return err
53+
}
54+
param := struct {
55+
CLICKHOUSE_ADDR string
56+
CCH_STORE_DIR string
57+
CCH_ADDR string
58+
}{
59+
CLICKHOUSE_ADDR: clickhouseAddr,
60+
CCH_STORE_DIR: c.storeDir,
61+
CCH_ADDR: c.address,
62+
}
63+
64+
c.configFile = path.Join(c.storeDir, "carbon-clickhouse.conf")
65+
f, err := os.OpenFile(c.configFile, os.O_WRONLY|os.O_CREATE, 0644)
66+
if err != nil {
67+
c.Cleanup()
68+
return err
69+
}
70+
err = tmpl.ExecuteTemplate(f, name, param)
71+
if err != nil {
72+
c.Cleanup()
73+
return err
74+
}
75+
76+
c.cmd = exec.Command(c.Binary, "-config", c.configFile)
77+
c.cmd.Stdout = os.Stdout
78+
c.cmd.Stderr = os.Stderr
79+
//c.cmd.Env = append(c.cmd.Env, "TZ=UTC")
80+
err = c.cmd.Start()
81+
if err != nil {
82+
c.Cleanup()
83+
return err
84+
}
85+
86+
return nil
87+
}
88+
89+
func (c *CarbonClickhouse) Stop() error {
90+
if c.cmd == nil {
91+
return nil
92+
}
93+
var err error
94+
if err = c.cmd.Process.Kill(); err == nil {
95+
if err = c.cmd.Wait(); err != nil {
96+
if exitErr, ok := err.(*exec.ExitError); ok {
97+
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
98+
ec := status.ExitStatus()
99+
if ec == 0 || ec == -1 {
100+
return nil
101+
}
102+
}
103+
}
104+
}
105+
}
106+
return err
107+
}
108+
109+
func (c *CarbonClickhouse) Cleanup() {
110+
if len(c.storeDir) > 0 {
111+
os.RemoveAll(c.storeDir)
112+
c.storeDir = ""
113+
c.cmd = nil
114+
}
115+
}
116+
117+
func (c *CarbonClickhouse) Address() string {
118+
return c.address
119+
}

cmd/e2e-test/clickhouse.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
)
7+
8+
type Clickhouse struct {
9+
Version string `toml:"version"`
10+
Dir string `toml:"dir"`
11+
12+
Docker string `toml:"docker"`
13+
DockerImage string `toml:"image"`
14+
15+
address string `toml:"-"`
16+
container string `toml:"-"`
17+
}
18+
19+
func (c *Clickhouse) Start() (error, string) {
20+
if len(c.Version) == 0 {
21+
return fmt.Errorf("version not set"), ""
22+
}
23+
if len(c.Dir) == 0 {
24+
return fmt.Errorf("dir not set"), ""
25+
}
26+
if len(c.Docker) == 0 {
27+
c.Docker = "docker"
28+
}
29+
if len(c.DockerImage) == 0 {
30+
c.DockerImage = "yandex/clickhouse-server"
31+
}
32+
var err error
33+
c.address, err = getFreeTCPPort("")
34+
if err != nil {
35+
return err, ""
36+
}
37+
38+
c.container = "carbon-clickhouse-clickhouse-server-test"
39+
40+
chStart := []string{"run", "-d",
41+
"--name", c.container,
42+
"--ulimit", "nofile=262144:262144",
43+
"-p", c.address + ":8123",
44+
//"-e", "TZ=UTC",
45+
"-v", c.Dir + "/config.xml:/etc/clickhouse-server/config.xml",
46+
"-v", c.Dir + "/users.xml:/etc/clickhouse-server/users.xml",
47+
"-v", c.Dir + "/rollup.xml:/etc/clickhouse-server/config.d/rollup.xml",
48+
"-v", c.Dir + "/init.sql:/docker-entrypoint-initdb.d/init.sql",
49+
c.DockerImage + ":" + c.Version,
50+
}
51+
52+
cmd := exec.Command(c.Docker, chStart...)
53+
out, err := cmd.CombinedOutput()
54+
55+
return err, string(out)
56+
}
57+
58+
func (c *Clickhouse) Stop(delete bool) (error, string) {
59+
if len(c.container) == 0 {
60+
return nil, ""
61+
}
62+
63+
chStop := []string{"stop", c.container}
64+
65+
cmd := exec.Command(c.Docker, chStop...)
66+
out, err := cmd.CombinedOutput()
67+
68+
if err == nil && delete {
69+
return c.Delete()
70+
}
71+
return err, string(out)
72+
}
73+
74+
func (c *Clickhouse) Delete() (error, string) {
75+
if len(c.container) == 0 {
76+
return nil, ""
77+
}
78+
79+
chDel := []string{"rm", c.container}
80+
81+
cmd := exec.Command(c.Docker, chDel...)
82+
out, err := cmd.CombinedOutput()
83+
84+
if err == nil {
85+
c.container = ""
86+
}
87+
88+
return err, string(out)
89+
}
90+
91+
func (c *Clickhouse) Address() string {
92+
return c.address
93+
}
94+
95+
func (c *Clickhouse) Container() string {
96+
return c.container
97+
}

0 commit comments

Comments
 (0)