Skip to content

Commit

Permalink
Refactor: unify table engine tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
alex1704 committed Sep 19, 2023
1 parent 7d2ff65 commit 530edf6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 123 deletions.
126 changes: 74 additions & 52 deletions exporter/clickhouseexporter/exporter_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,70 +137,75 @@ func TestLogsClusterConfigOff(t *testing.T) {
}

func TestLogsTableEngineConfig(t *testing.T) {
teName := "CustomEngine"
te := TableEngine{Name: teName}
expectedTEValue := fmt.Sprintf("%s()", teName)
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
testTableEngineConfig(t, func(t *testing.T, dsn string, test tableEngineTestConfig, fns ...func(*Config)) {
exporter := newTestLogsExporter(t, dsn, fns...)
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
test.sanityCheck(t, exporter.cfg.TableEngine)
})
}

func TestLogsTableEngineConfigWithParams(t *testing.T) {
teName := "CustomEngine"
teParams := "'/x/y/z', 'some_param', another_param, last_param"
te := TableEngine{Name: teName, Params: teParams}
expectedTEValue := fmt.Sprintf("%s(%s)", teName, teParams)
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
exporter := newTestLogsExporter(t, dsn, fns...)
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
})
}
func testTableEngineConfig(t *testing.T, completion tableEngineTestCompletion) {
tests := []tableEngineTestConfig {
{
name: "no params",
teName: "CustomEngine",
teParams: "",
expectedTableName: "CustomEngine",
shouldSucceed: true,
},
{
name: "with params",
teName: "CustomEngine",
teParams: "'/x/y/z', 'some_param', another_param, last_param",
expectedTableName: "CustomEngine",
shouldSucceed: true,
},
{
name: "with empty name",
teName: "",
teParams: "",
expectedTableName: defaultTableEngineName,
shouldSucceed: true,
},
{
name: "fail",
teName: "CustomEngine",
teParams: "",
expectedTableName: defaultTableEngineName,
shouldSucceed: false,
},
}

func TestLogsEmptyTableEngineConfig(t *testing.T) {
expectedTEValue := fmt.Sprintf("%s()", defaultTableEngineName)
te := TableEngine{Name: ""}
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
exporter := newTestLogsExporter(t, dsn, fns...)
require.Empty(t, exporter.cfg.TableEngine.Name)
})
}
for _, tt := range tests {
te := TableEngine{Name: tt.teName, Params: tt.teParams}
expectedTEValue := fmt.Sprintf("%s(%s)", tt.expectedTableName, tt.teParams)

func TestLogsTableEngineConfigFail(t *testing.T) {
teName := "CustomEngine"
te := TableEngine{Name: teName}
expectedTEValue := fmt.Sprintf("%s()", defaultTableEngineName)
testTableEngineConfig(t, te, expectedTEValue, false, func(t *testing.T, dsn string, fns ...func(*Config)) {
exporter := newTestLogsExporter(t, dsn, fns...)
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
})
}
t.Run("test table engine config " + tt.name, func(t *testing.T) {
initClickhouseTestServer(t, func(query string, values []driver.Value) error {
firstLine := getQueryFirstLine(query)
if !strings.HasPrefix(strings.ToLower(firstLine), "create table") {
return nil
}

func testTableEngineConfig(t *testing.T, tableEngine TableEngine, expectedTableEngineValue string, shouldSucceed bool, completion exporterValuesProvider) {
initClickhouseTestServer(t, func(query string, values []driver.Value) error {
firstLine := getQueryFirstLine(query)
if !strings.HasPrefix(strings.ToLower(firstLine), "create table") {
return nil
}
check := checkTableEngineQueryDefinition(query, expectedTEValue)
if tt.shouldSucceed {
require.NoError(t, check)
} else {
require.Error(t, check)
}

check := checkTableEngineQueryDefinition(query, expectedTableEngineValue)
if shouldSucceed {
require.NoError(t, check)
} else {
require.Error(t, check)
}
return nil
})

return nil
})
var configMods []func(*Config)
if te.Name != "" {
configMods = append(configMods, func(cfg *Config) {
cfg.TableEngine = te
})
}

var configMods []func(*Config)
if tableEngine.Name != "" {
configMods = append(configMods, func(cfg *Config) {
cfg.TableEngine = tableEngine
completion(t, defaultEndpoint, tt, configMods...)
})
}

completion(t, defaultEndpoint, configMods...)
}

func testClusterConfigOn(t *testing.T, completion exporterValuesProvider) {
Expand Down Expand Up @@ -323,11 +328,28 @@ func initClickhouseTestServer(t *testing.T, recorder recorder) {

type recorder func(query string, values []driver.Value) error
type exporterValuesProvider func(t *testing.T, dsn string, fns ...func(*Config))
type tableEngineTestCompletion func(t *testing.T, dsn string, test tableEngineTestConfig, fns ...func(*Config))

type testClickhouseDriver struct {
recorder recorder
}

type tableEngineTestConfig struct {
name string
teName string
teParams string
expectedTableName string
shouldSucceed bool
}

func (teTest tableEngineTestConfig) sanityCheck(t *testing.T, te TableEngine) {
if teTest.teName == "" {
require.Empty(t, te.Name)
} else {
require.NotEmpty(t, te.Name)
}
}

func (t *testClickhouseDriver) Open(_ string) (driver.Conn, error) {
return &testClickhouseDriverConn{
recorder: t.recorder,
Expand Down
37 changes: 2 additions & 35 deletions exporter/clickhouseexporter/exporter_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,42 +125,9 @@ func TestMetricsClusterConfigOff(t *testing.T) {
}

func TestMetricsTableEngineConfig(t *testing.T) {
teName := "CustomEngine"
te := TableEngine{Name: teName}
expectedTEValue := fmt.Sprintf("%s()", teName)
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
testTableEngineConfig(t, func(t *testing.T, dsn string, test tableEngineTestConfig, fns ...func(*Config)) {
exporter := newTestMetricsExporter(t, dsn, fns...)
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
})
}

func TestMetricsTableEngineConfigWithParams(t *testing.T) {
teName := "CustomEngine"
teParams := "'/x/y/z', 'some_param', another_param, last_param"
te := TableEngine{Name: teName, Params: teParams}
expectedTEValue := fmt.Sprintf("%s(%s)", teName, teParams)
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
exporter := newTestMetricsExporter(t, dsn, fns...)
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
})
}

func TestMetricsEmptyTableEngineConfig(t *testing.T) {
expectedTEValue := fmt.Sprintf("%s()", defaultTableEngineName)
te := TableEngine{Name: ""}
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
exporter := newTestMetricsExporter(t, dsn, fns...)
require.Empty(t, exporter.cfg.TableEngine.Name)
})
}

func TestMetricsTableEngineConfigFail(t *testing.T) {
teName := "CustomEngine"
te := TableEngine{Name: teName}
expectedTEValue := fmt.Sprintf("%s()", defaultTableEngineName)
testTableEngineConfig(t, te, expectedTEValue, false, func(t *testing.T, dsn string, fns ...func(*Config)) {
exporter := newTestMetricsExporter(t, dsn, fns...)
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
test.sanityCheck(t, exporter.cfg.TableEngine)
})
}

Expand Down
38 changes: 2 additions & 36 deletions exporter/clickhouseexporter/exporter_traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"testing"
"time"
"fmt"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
Expand Down Expand Up @@ -64,42 +63,9 @@ func TestTracesClusterConfigOff(t *testing.T) {
}

func TestTracesTableEngineConfig(t *testing.T) {
teName := "CustomEngine"
te := TableEngine{Name: teName}
expectedTEValue := fmt.Sprintf("%s()", teName)
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
testTableEngineConfig(t, func(t *testing.T, dsn string, test tableEngineTestConfig, fns ...func(*Config)) {
exporter := newTestTracesExporter(t, dsn, fns...)
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
})
}

func TestTracesTableEngineConfigWithParams(t *testing.T) {
teName := "CustomEngine"
teParams := "'/x/y/z', 'some_param', another_param, last_param"
te := TableEngine{Name: teName, Params: teParams}
expectedTEValue := fmt.Sprintf("%s(%s)", teName, teParams)
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
exporter := newTestTracesExporter(t, dsn, fns...)
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
})
}

func TestTracesEmptyTableEngineConfig(t *testing.T) {
expectedTEValue := fmt.Sprintf("%s()", defaultTableEngineName)
te := TableEngine{Name: ""}
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
exporter := newTestTracesExporter(t, dsn, fns...)
require.Empty(t, exporter.cfg.TableEngine.Name)
})
}

func TestTracesTableEngineConfigFail(t *testing.T) {
teName := "CustomEngine"
te := TableEngine{Name: teName}
expectedTEValue := fmt.Sprintf("%s()", defaultTableEngineName)
testTableEngineConfig(t, te, expectedTEValue, false, func(t *testing.T, dsn string, fns ...func(*Config)) {
exporter := newTestTracesExporter(t, dsn, fns...)
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
test.sanityCheck(t, exporter.cfg.TableEngine)
})
}

Expand Down

0 comments on commit 530edf6

Please sign in to comment.