From 530edf6c8aa34c79d5693dfa184e4f111eb1ab59 Mon Sep 17 00:00:00 2001 From: Oleksandr Kostenko Date: Tue, 19 Sep 2023 16:36:18 +0300 Subject: [PATCH] Refactor: unify table engine tests. --- .../clickhouseexporter/exporter_logs_test.go | 126 ++++++++++-------- .../exporter_metrics_test.go | 37 +---- .../exporter_traces_test.go | 38 +----- 3 files changed, 78 insertions(+), 123 deletions(-) diff --git a/exporter/clickhouseexporter/exporter_logs_test.go b/exporter/clickhouseexporter/exporter_logs_test.go index 515db706cc26..df895433e356 100644 --- a/exporter/clickhouseexporter/exporter_logs_test.go +++ b/exporter/clickhouseexporter/exporter_logs_test.go @@ -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) { @@ -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, diff --git a/exporter/clickhouseexporter/exporter_metrics_test.go b/exporter/clickhouseexporter/exporter_metrics_test.go index 9d4a123e1d55..548c5617732b 100644 --- a/exporter/clickhouseexporter/exporter_metrics_test.go +++ b/exporter/clickhouseexporter/exporter_metrics_test.go @@ -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) }) } diff --git a/exporter/clickhouseexporter/exporter_traces_test.go b/exporter/clickhouseexporter/exporter_traces_test.go index c063a6ae9c5c..0c5fe091b5f7 100644 --- a/exporter/clickhouseexporter/exporter_traces_test.go +++ b/exporter/clickhouseexporter/exporter_traces_test.go @@ -9,7 +9,6 @@ import ( "strings" "testing" "time" - "fmt" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/pdata/pcommon" @@ -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) }) }