Skip to content

Commit 2a64806

Browse files
author
Oleksandr Kostenko
committed
Implement tests for correct query generation when Config.ClusterName option is on/off.
1 parent cd5d854 commit 2a64806

File tree

4 files changed

+69
-79
lines changed

4 files changed

+69
-79
lines changed

exporter/clickhouseexporter/config_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ import (
2121
)
2222

2323
const defaultEndpoint = "clickhouse://127.0.0.1:9000"
24-
const replicationEndpoint = "tcp://127.0.0.1:19000"
25-
const replicationEndpoint2 = "tcp://127.0.0.1:19001"
26-
const replicationCluster = "cluster_1S_2R"
24+
const defaultCluster = "cluster_1S_2R"
2725

2826
func TestTableEngineConfigParsing(t *testing.T) {
2927
t.Parallel()

exporter/clickhouseexporter/exporter_logs_test.go

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,47 @@ func TestExporter_pushLogsData(t *testing.T) {
122122
})
123123
}
124124

125-
func TestLogsTableCreationOnCluster(t *testing.T) {
126-
dbName := "test_db_" + time.Now().Format("20060102150405")
125+
func TestLogsClusterConfigOn(t *testing.T) {
126+
testClusterConfigOn(t, func(t *testing.T, dsn string, fns ...func(*Config)) {
127+
exporter := newTestLogsExporter(t, dsn, fns...)
128+
require.NotEmpty(t, exporter.cfg.ClusterClause())
129+
})
130+
}
131+
132+
func TestLogsClusterConfigOff(t *testing.T) {
133+
testClusterConfigOff(t, func(t *testing.T, dsn string, fns ...func(*Config)) {
134+
exporter := newTestLogsExporter(t, dsn, fns...)
135+
require.Empty(t, exporter.cfg.ClusterClause())
136+
})
137+
}
138+
139+
func testClusterConfigOn(t *testing.T, completion exporterValuesProvider) {
140+
initClickhouseTestServer(t, func(query string, values []driver.Value) error {
141+
require.NoError(t, checkClusterQueryStatememt(query, defaultCluster))
142+
return nil
143+
})
144+
127145
var configMods []func(*Config)
128146
configMods = append(configMods, func(cfg *Config) {
129-
cfg.ClusterName = replicationCluster
130-
cfg.Database = dbName
131-
cfg.TableEngine = TableEngine{Name: "ReplicatedMergeTree", Params: ""}
147+
cfg.ClusterName = defaultCluster
148+
cfg.Database = "test_db_" + time.Now().Format("20060102150405")
132149
})
133150

134-
t.Run("Check database and table creation on cluster", func(t *testing.T) {
135-
exporter := newTestLogsExporter(t, replicationEndpoint, configMods...)
136-
require.NotEmpty(t, exporter.cfg.ClusterClause())
151+
completion(t, defaultEndpoint, configMods...)
152+
}
153+
154+
func testClusterConfigOff(t *testing.T, completion exporterValuesProvider) {
155+
initClickhouseTestServer(t, func(query string, values []driver.Value) error {
156+
require.Error(t, checkClusterQueryStatememt(query, defaultCluster))
157+
return nil
158+
})
137159

138-
samplesCount := 5
139-
mustPushLogsData(t, exporter, simpleLogs(samplesCount))
140-
checkReplicatedTableCount(t, dbName, getTableNames(dbName, exporter.client), samplesCount)
160+
var configMods []func(*Config)
161+
configMods = append(configMods, func(cfg *Config) {
162+
cfg.Database = "test_db_" + time.Now().Format("20060102150405")
141163
})
164+
165+
completion(t, defaultEndpoint, configMods...)
142166
}
143167

144168
func newTestLogsExporter(t *testing.T, dsn string, fns ...func(*Config)) *logsExporter {
@@ -161,45 +185,22 @@ func withTestExporterConfig(fns ...func(*Config)) func(string) *Config {
161185
}
162186
}
163187

164-
// Opens Clickhouse client to `replicationEndpoint2` to check if table data was replicated.
165-
func checkReplicatedTableCount(t *testing.T, dbName string, tableNames []string, expectedCount int) {
166-
// wait for replication
167-
require.NotEmpty(t, tableNames)
168-
time.Sleep(1 * time.Second)
169-
170-
config := withTestExporterConfig()(replicationEndpoint2)
171-
client, err := newClickhouseClient(config)
172-
require.NoError(t, err)
173-
defer client.Close()
174-
175-
println("replication in db", dbName, "; url:", replicationEndpoint2)
176-
for _, tableName := range tableNames {
177-
println("check count in replicated table", tableName)
178-
query := fmt.Sprintf("SELECT count(*) FROM %s.%s", dbName, tableName)
179-
row := client.QueryRowContext(context.TODO(), query)
180-
var count int
181-
row.Scan(&count)
182-
require.Equal(t, expectedCount, count)
183-
}
184-
}
185-
186-
// Get table names from database.
187-
func getTableNames(dbName string, client *sql.DB) []string {
188-
rows, err := client.QueryContext(context.TODO(), "show tables from " + dbName)
189-
defer rows.Close()
190-
191-
if err != nil {
192-
return []string{}
193-
}
194-
195-
var tableNames []string
196-
for rows.Next() {
197-
var tableName string
198-
rows.Scan(&tableName)
199-
tableNames = append(tableNames, tableName)
188+
func checkClusterQueryStatememt(query string, clusterName string) error {
189+
trimmed := strings.Trim(query, "\n")
190+
line := strings.Split(trimmed, "\n")[0]
191+
line = strings.Trim(line, " (")
192+
lowercasedLine := strings.ToLower(line)
193+
suffix := fmt.Sprintf("ON CLUSTER %s", clusterName)
194+
prefixes := []string{"create database", "create table", "create materialized view"}
195+
for _, prefix := range prefixes {
196+
if strings.HasPrefix(lowercasedLine, prefix) {
197+
if strings.HasSuffix(line, suffix) {
198+
return nil
199+
}
200+
}
200201
}
201202

202-
return tableNames
203+
return errors.New(fmt.Sprintf("Does not contain cluster clause: %s", line))
203204
}
204205

205206
func simpleLogs(count int) plog.Logs {
@@ -233,6 +234,7 @@ func initClickhouseTestServer(t *testing.T, recorder recorder) {
233234
}
234235

235236
type recorder func(query string, values []driver.Value) error
237+
type exporterValuesProvider func(t *testing.T, dsn string, fns ...func(*Config))
236238

237239
type testClickhouseDriver struct {
238240
recorder recorder

exporter/clickhouseexporter/exporter_metrics_test.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,17 @@ func Benchmark_pushMetricsData(b *testing.B) {
110110
}
111111
}
112112

113-
func TestMetricsTablesCreationOnCluster(t *testing.T) {
114-
var configMods []func(*Config)
115-
dbName := "test_db_" + time.Now().Format("20060102150405")
116-
configMods = append(configMods, func(cfg *Config) {
117-
cfg.ClusterName = replicationCluster
118-
cfg.Database = dbName
119-
cfg.TableEngine = TableEngine{Name: "ReplicatedMergeTree", Params: ""}
120-
})
121-
122-
t.Run("Check database and table creation on cluster", func(t *testing.T) {
123-
exporter := newTestMetricsExporter(t, replicationEndpoint, configMods...)
113+
func TestMetricsClusterConfigOn(t *testing.T) {
114+
testClusterConfigOn(t, func(t *testing.T, dsn string, fns ...func(*Config)) {
115+
exporter := newTestMetricsExporter(t, dsn, fns...)
124116
require.NotEmpty(t, exporter.cfg.ClusterClause())
117+
})
118+
}
125119

126-
samplesCount := 5
127-
metrics := simpleMetrics(samplesCount)
128-
mustPushMetricsData(t, exporter, metrics)
129-
checkReplicatedTableCount(t, dbName, getTableNames(dbName, exporter.client), samplesCount * 3)
120+
func TestMetricsClusterConfigOff(t *testing.T) {
121+
testClusterConfigOff(t, func(t *testing.T, dsn string, fns ...func(*Config)) {
122+
exporter := newTestMetricsExporter(t, dsn, fns...)
123+
require.Empty(t, exporter.cfg.ClusterClause())
130124
})
131125
}
132126

exporter/clickhouseexporter/exporter_traces_test.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,17 @@ func TestExporter_pushTracesData(t *testing.T) {
4848
})
4949
}
5050

51-
func TestTracesTablesCreationOnCluster(t *testing.T) {
52-
var configMods []func(*Config)
53-
dbName := "test_db_" + time.Now().Format("20060102150405")
54-
configMods = append(configMods, func(cfg *Config) {
55-
cfg.ClusterName = replicationCluster
56-
cfg.Database = dbName
57-
cfg.TableEngine = TableEngine{Name: "ReplicatedMergeTree", Params: ""}
58-
})
59-
t.Run("Check database and table creation on cluster", func(t *testing.T) {
60-
exporter := newTestTracesExporter(t, replicationEndpoint, configMods...)
51+
func TestTracesClusterConfigOn(t *testing.T) {
52+
testClusterConfigOn(t, func(t *testing.T, dsn string, fns ...func(*Config)) {
53+
exporter := newTestTracesExporter(t, dsn, fns...)
6154
require.NotEmpty(t, exporter.cfg.ClusterClause())
55+
})
56+
}
6257

63-
samplesCount := 5
64-
mustPushTracesData(t, exporter, simpleTraces(samplesCount))
65-
checkReplicatedTableCount(t, dbName, []string{exporter.cfg.TracesTableName}, samplesCount)
58+
func TestTracesClusterConfigOff(t *testing.T) {
59+
testClusterConfigOff(t, func(t *testing.T, dsn string, fns ...func(*Config)) {
60+
exporter := newTestTracesExporter(t, dsn, fns...)
61+
require.Empty(t, exporter.cfg.ClusterClause())
6662
})
6763
}
6864

0 commit comments

Comments
 (0)