Skip to content

Commit

Permalink
test: added tls to e2e-tests
Browse files Browse the repository at this point in the history
- accessed clickhouse using TLS params
- fixed rollup
  • Loading branch information
lordvidex committed Aug 3, 2023
1 parent 963a947 commit 0e7f22a
Show file tree
Hide file tree
Showing 20 changed files with 1,193 additions and 156 deletions.
46 changes: 32 additions & 14 deletions cmd/e2e-test/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"

"github.com/msaf1980/go-stringutils"
)
Expand All @@ -20,16 +19,19 @@ var ClickhouseOldImage = "yandex/clickhouse-server"
var ClickhouseDefaultImage = "clickhouse/clickhouse-server"

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

DockerImage string `toml:"image"`

TZ string `toml:"tz"` // override timezone

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

func (c *Clickhouse) CheckConfig(rootDir string) error {
Expand Down Expand Up @@ -71,6 +73,7 @@ func (c *Clickhouse) Start() (string, error) {
if err != nil {
return "", err
}
port := strings.Split(c.httpAddress, ":")[1]
c.url = "http://" + c.httpAddress

c.container = ClickhouseContainerName
Expand All @@ -80,14 +83,28 @@ func (c *Clickhouse) Start() (string, error) {
chStart := []string{"run", "-d",
"--name", c.container,
"--ulimit", "nofile=262144:262144",
"-p", c.httpAddress + ":8123",
"-p", port + ":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",
"--network", DockerNetwork,
}
if c.TLSEnabled {
c.httpsAddress, err = getFreeTCPPort("")
if err != nil {
return "", err
}
port = strings.Split(c.httpsAddress, ":")[1]
c.tlsurl = "https://" + c.httpsAddress
chStart = append(chStart,
"-v", c.Dir+"/server.crt:/etc/clickhouse-server/server.crt",
"-v", c.Dir+"/server.key:/etc/clickhouse-server/server.key",
"-v", c.Dir+"/rootCA.crt:/etc/clickhouse-server/rootCA.crt",
"-p", port+":8443",
)
}
if c.TZ != "" {
chStart = append(chStart, "-e", "TZ="+c.TZ)
}
Expand Down Expand Up @@ -137,6 +154,10 @@ func (c *Clickhouse) URL() string {
return c.url
}

func (c *Clickhouse) TLSURL() string {
return c.tlsurl
}

func (c *Clickhouse) Container() string {
return c.container
}
Expand All @@ -152,14 +173,11 @@ func (c *Clickhouse) Query(sql string) (string, error) {
return "", err
}

httpClient := http.Client{
Timeout: time.Minute,
}
resp, err := httpClient.Do(request)
resp, err := http.DefaultClient.Do(request)
if err != nil {
return "", err
}
msg, err := io.ReadAll(resp.Body)
msg, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
Expand All @@ -173,7 +191,7 @@ func (c *Clickhouse) Alive() bool {
if len(c.container) == 0 {
return false
}
req, err := http.DefaultClient.Get(c.url)
req, err := http.DefaultClient.Get(c.URL())
if err != nil {
return false
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/e2e-test/e2etesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
"strings"
"time"

"go.uber.org/zap"

"github.com/lomik/graphite-clickhouse/helper/client"
"github.com/lomik/graphite-clickhouse/helper/datetime"
"go.uber.org/zap"

"github.com/pelletier/go-toml"
)
Expand Down Expand Up @@ -152,6 +153,10 @@ type TestSchema struct {
// input map[string][]Point `toml:"-"`
}

func (schema *TestSchema) HasTLSSettings() bool {
return strings.Contains(schema.dir, "tls")
}

func getFreeTCPPort(name string) (string, error) {
if len(name) == 0 {
name = "127.0.0.1:0"
Expand Down Expand Up @@ -202,7 +207,7 @@ func sendPlain(network, address string, metrics []InputMetric) error {

func verifyGraphiteClickhouse(test *TestSchema, gch *GraphiteClickhouse, clickhouse *Clickhouse, testDir, clickhouseDir string, verbose, breakOnError bool, logger *zap.Logger) (testSuccess bool, verifyCount, verifyFailed int) {
testSuccess = true
err := gch.Start(testDir, clickhouse.URL(), test.Proxy.URL())
err := gch.Start(testDir, clickhouse.URL(), test.Proxy.URL(), clickhouse.TLSURL())
if err != nil {
logger.Error("starting graphite-clickhouse",
zap.String("config", test.name),
Expand Down
31 changes: 21 additions & 10 deletions cmd/e2e-test/graphite-clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (
"syscall"
"text/template"

"github.com/lomik/graphite-clickhouse/helper/client"
"github.com/msaf1980/go-stringutils"

"github.com/lomik/graphite-clickhouse/helper/client"
)

type GraphiteClickhouse struct {
Binary string `toml:"binary"`
ConfigTpl string `toml:"template"`
TestDir string `toml:"-"`

TZ string `toml:"tz"` // override timezone

Expand All @@ -28,7 +30,7 @@ type GraphiteClickhouse struct {
cmd *exec.Cmd `toml:"-"`
}

func (c *GraphiteClickhouse) Start(testDir, clickhouseURL, chProxyURL string) error {
func (c *GraphiteClickhouse) Start(testDir, chURL, chProxyURL, chTLSURL string) error {
if c.cmd != nil {
return errors.New("carbon-clickhouse already started")
}
Expand All @@ -52,22 +54,31 @@ func (c *GraphiteClickhouse) Start(testDir, clickhouseURL, chProxyURL string) er
return err
}

c.TestDir, err = filepath.Abs(testDir)
if err != nil {
return err
}

name := filepath.Base(c.ConfigTpl)
tmpl, err := template.New(name).ParseFiles(path.Join(testDir, c.ConfigTpl))
if err != nil {
c.Cleanup()
return err
}
param := struct {
CLICKHOUSE_URL string
PROXY_URL string
GCH_ADDR string
GCH_DIR string
CLICKHOUSE_URL string
CLICKHOUSE_TLS_URL string
PROXY_URL string
GCH_ADDR string
GCH_DIR string
TEST_DIR string
}{
CLICKHOUSE_URL: clickhouseURL,
PROXY_URL: chProxyURL,
GCH_ADDR: c.address,
GCH_DIR: c.storeDir,
CLICKHOUSE_URL: chURL,
CLICKHOUSE_TLS_URL: chTLSURL,
PROXY_URL: chProxyURL,
GCH_ADDR: c.address,
GCH_DIR: c.storeDir,
TEST_DIR: c.TestDir,
}

c.configFile = path.Join(c.storeDir, "graphite-clickhouse.conf")
Expand Down
Loading

0 comments on commit 0e7f22a

Please sign in to comment.