Skip to content

Commit

Permalink
Merge pull request #120 from msaf1980/e2e_storage_usage
Browse files Browse the repository at this point in the history
e2e tests images cleanup
  • Loading branch information
msaf1980 authored Nov 22, 2022
2 parents aebdd8c + 5999391 commit 101d36c
Show file tree
Hide file tree
Showing 12 changed files with 447 additions and 158 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ jobs:
strategy:
matrix:
go:
- ^1.15
- ^1.16
- ^1.17
- ^1.18
- ^1.19
- ^1
steps:

Expand Down Expand Up @@ -131,4 +127,4 @@ jobs:
- name: Integration tests
run: |
make e2e-test
./e2e-test -config tests
./e2e-test -config tests -abort -rmi
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Last releases are stable and ready for production use
Docker images are available on [packages](https://github.com/lomik/carbon-clickhouse/pkgs/container/carbon-clickhouse) page.

## Build

Required golang 1.18+

```sh
# build binary
git clone https://github.com/lomik/carbon-clickhouse.git
Expand Down
14 changes: 7 additions & 7 deletions cmd/e2e-test/carbon_clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type CarbonClickhouse struct {
cmd *exec.Cmd `toml:"-"`
}

func (c *CarbonClickhouse) Start(clickhouseAddr string) error {
func (c *CarbonClickhouse) Start(clickhouseURL string) error {
if c.cmd != nil {
return fmt.Errorf("carbon-clickhouse already started")
}
Expand Down Expand Up @@ -52,13 +52,13 @@ func (c *CarbonClickhouse) Start(clickhouseAddr string) error {
return err
}
param := struct {
CLICKHOUSE_ADDR string
CCH_STORE_DIR string
CCH_ADDR string
CLICKHOUSE_URL string
CCH_STORE_DIR string
CCH_ADDR string
}{
CLICKHOUSE_ADDR: clickhouseAddr,
CCH_STORE_DIR: c.storeDir,
CCH_ADDR: c.address,
CLICKHOUSE_URL: clickhouseURL,
CCH_STORE_DIR: c.storeDir,
CCH_ADDR: c.address,
}

c.configFile = path.Join(c.storeDir, "carbon-clickhouse.conf")
Expand Down
181 changes: 153 additions & 28 deletions cmd/e2e-test/clickhouse.go
Original file line number Diff line number Diff line change
@@ -1,99 +1,224 @@
package main

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"

"github.com/msaf1980/go-stringutils"
)

var ClickhouseContainerName = "clickhouse-server-gch-test"
var ClickhouseOldImage = "yandex/clickhouse-server"
var ClickhouseDefaultImage = "clickhouse/clickhouse-server"

type Clickhouse struct {
Version string `toml:"version"`
Dir string `toml:"dir"`

Docker string `toml:"docker"`
DockerImage string `toml:"image"`

address string `toml:"-"`
container string `toml:"-"`
TZ string `toml:"tz"` // override timezone

httpAddress string `toml:"-"`
url string `toml:"-"`
container string `toml:"-"`
}

func (c *Clickhouse) Start() (error, string) {
if len(c.Version) == 0 {
return fmt.Errorf("version not set"), ""
func (c *Clickhouse) CheckConfig(rootDir string) error {
if c.Version == "" {
c.Version = "latest"
}
if len(c.Dir) == 0 {
return fmt.Errorf("dir not set"), ""
return ErrNoSetDir
}
if len(c.Docker) == 0 {
c.Docker = "docker"
if !strings.HasPrefix(c.Dir, "/") {
c.Dir = rootDir + "/" + c.Dir
}
if len(c.DockerImage) == 0 {
c.DockerImage = "yandex/clickhouse-server"

if c.DockerImage == "" {
if c.Version == "latest" {
c.DockerImage = ClickhouseDefaultImage
} else {
splitV := strings.Split(c.Version, ".")
majorV, err := strconv.Atoi(splitV[0])
if err != nil {
c.DockerImage = ClickhouseDefaultImage
} else if majorV >= 21 {
c.DockerImage = ClickhouseDefaultImage
} else {
c.DockerImage = ClickhouseOldImage
}
}
}
return nil
}

func (c *Clickhouse) Key() string {
return c.DockerImage + ":" + c.Version + " " + c.Dir + " TZ " + c.TZ
}

func (c *Clickhouse) Start() (string, error) {
var err error
c.address, err = getFreeTCPPort("")
c.httpAddress, err = getFreeTCPPort("")
if err != nil {
return err, ""
return "", err
}
c.url = "http://" + c.httpAddress

c.container = ClickhouseContainerName

// tz, _ := localTZLocationName()

chStart := []string{"run", "-d",
"--name", c.container,
"--ulimit", "nofile=262144:262144",
"-p", c.address + ":8123",
//"-e", "TZ=UTC",
"-p", c.httpAddress + ":8123",
// "-e", "TZ=" + tz, // workaround for TZ=":/etc/localtime"
"-v", c.Dir + "/config.xml:/etc/clickhouse-server/config.xml",
"-v", c.Dir + "/users.xml:/etc/clickhouse-server/users.xml",
"-v", c.Dir + "/rollup.xml:/etc/clickhouse-server/config.d/rollup.xml",
"-v", c.Dir + "/init.sql:/docker-entrypoint-initdb.d/init.sql",
c.DockerImage + ":" + c.Version,
}
if c.TZ != "" {
chStart = append(chStart, "-e", "TZ="+c.TZ)
}

cmd := exec.Command(c.Docker, chStart...)
chStart = append(chStart, c.DockerImage+":"+c.Version)

cmd := exec.Command(DockerBinary, chStart...)
out, err := cmd.CombinedOutput()

return err, string(out)
return string(out), err
}

func (c *Clickhouse) Stop(delete bool) (error, string) {
func (c *Clickhouse) Stop(delete bool) (string, error) {
if len(c.container) == 0 {
return nil, ""
return "", nil
}

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

cmd := exec.Command(c.Docker, chStop...)
cmd := exec.Command(DockerBinary, chStop...)
out, err := cmd.CombinedOutput()

if err == nil && delete {
return c.Delete()
}
return err, string(out)
return string(out), err
}

func (c *Clickhouse) Delete() (error, string) {
func (c *Clickhouse) Delete() (string, error) {
if len(c.container) == 0 {
return nil, ""
return "", nil
}

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

cmd := exec.Command(c.Docker, chDel...)
cmd := exec.Command(DockerBinary, chDel...)
out, err := cmd.CombinedOutput()

if err == nil {
c.container = ""
}

return err, string(out)
return string(out), err
}

func (c *Clickhouse) Address() string {
return c.address
func (c *Clickhouse) URL() string {
return c.url
}

func (c *Clickhouse) Container() string {
return c.container
}

func (c *Clickhouse) Exec(sql string) (bool, string) {
return containerExec(c.container, []string{"sh", "-c", "clickhouse-client -q '" + sql + "'"})
}

func (c *Clickhouse) Query(sql string) (string, error) {
reader := strings.NewReader(sql)
request, err := http.NewRequest("POST", c.URL(), reader)
if err != nil {
return "", err
}

httpClient := http.Client{
Timeout: time.Minute,
}
resp, err := httpClient.Do(request)
if err != nil {
return "", err
}
msg, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", errors.New(resp.Status + ": " + string(bytes.TrimRight(msg, "\n")))
}
return string(msg), nil
}

func (c *Clickhouse) Alive() bool {
if len(c.container) == 0 {
return false
}
req, err := http.DefaultClient.Get(c.url)
if err != nil {
return false
}
return req.StatusCode == http.StatusOK
}

func (c *Clickhouse) CopyLog(destDir string, tail uint64) error {
if len(c.container) == 0 {
return nil
}
dest := destDir + "/clickhouse-server.log"

chArgs := []string{"cp", c.container + ":/var/log/clickhouse-server/clickhouse-server.log", dest}

cmd := exec.Command(DockerBinary, chArgs...)
out, err := cmd.CombinedOutput()
if err != nil {
return errors.New(err.Error() + ": " + string(bytes.TrimRight(out, "\n")))
}

if tail > 0 {
out, _ := exec.Command("tail", "-"+strconv.FormatUint(tail, 10), dest).Output()
fmt.Fprintf(os.Stderr, "CLICKHOUSE-SERVER.LOG %s", stringutils.UnsafeString(out))
}

return nil
}

func (c *Clickhouse) CopyErrLog(destDir string, tail uint64) error {
if len(c.container) == 0 {
return nil
}
dest := destDir + "/clickhouse-server.err.log"

chArgs := []string{"cp", c.container + ":/var/log/clickhouse-server/clickhouse-server.err.log", dest}

cmd := exec.Command(DockerBinary, chArgs...)
out, err := cmd.CombinedOutput()
if err != nil {
return errors.New(err.Error() + ": " + string(bytes.TrimRight(out, "\n")))
}

if tail > 0 {
out, _ := exec.Command("tail", "-"+strconv.FormatUint(tail, 10), dest).Output()
fmt.Fprintf(os.Stderr, "CLICKHOUSE-SERVER.ERR %s", stringutils.UnsafeString(out))
}

return nil
}
67 changes: 62 additions & 5 deletions cmd/e2e-test/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,77 @@ import (
"strings"
)

func containerExist(dockerBinary, name string) (bool, string) {
if len(dockerBinary) == 0 {
dockerBinary = "docker"
var DockerBinary string

func imageDelete(image, version string) (bool, string) {
if len(DockerBinary) == 0 {
panic("docker not set")
}

chArgs := []string{"rmi", image + ":" + version}

cmd := exec.Command(DockerBinary, chArgs...)
out, err := cmd.CombinedOutput()
s := strings.Trim(string(out), "\n")

if err == nil {
return true, s
}

return false, err.Error() + ": " + s
}

func containerExist(name string) (bool, string) {
if len(DockerBinary) == 0 {
panic("docker not set")
}

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

cmd := exec.Command(dockerBinary, chInspect...)
cmd := exec.Command(DockerBinary, chInspect...)
out, err := cmd.CombinedOutput()
s := strings.Trim(string(out), "\n")

if err == nil {
return true, s
}

return false, err.Error() + ": " + s
}

func containerRemove(name string) (bool, string) {
if len(DockerBinary) == 0 {
panic("docker not set")
}

chInspect := []string{"rm", "-f", name}

cmd := exec.Command(DockerBinary, chInspect...)
out, err := cmd.CombinedOutput()
s := strings.Trim(string(out), "\n")

if err == nil {
return true, s
}

return false, err.Error() + ": " + s
}

func containerExec(name string, args []string) (bool, string) {
if len(DockerBinary) == 0 {
panic("docker not set")
}

dCmd := []string{"exec", name}
dCmd = append(dCmd, args...)

cmd := exec.Command(DockerBinary, dCmd...)
out, err := cmd.CombinedOutput()
s := strings.Trim(string(out), "\n")

if err == nil {
return true, s
}

return false, s
return false, err.Error() + ": " + s
}
Loading

0 comments on commit 101d36c

Please sign in to comment.