forked from unai-ss/Kafka-Kconnector-Prometheus-Grafana
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_utils.go
More file actions
346 lines (292 loc) · 9.1 KB
/
Copy pathtest_utils.go
File metadata and controls
346 lines (292 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// TestUtils provides shared utilities for testing across all test files
type TestUtils struct {
t *testing.T
}
// NewTestUtils creates a new test utilities instance
func NewTestUtils(t *testing.T) *TestUtils {
return &TestUtils{t: t}
}
// HTTPServerConfig defines configuration for mock HTTP servers
type HTTPServerConfig struct {
Path string
Method string
ResponseCode int
ResponseBody string
Headers map[string]string
}
// CreateMockHTTPServer creates a mock HTTP server with specified responses
func (tu *TestUtils) CreateMockHTTPServer(configs []HTTPServerConfig) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, config := range configs {
if r.URL.Path == config.Path && (config.Method == "" || r.Method == config.Method) {
// Set headers
for key, value := range config.Headers {
w.Header().Set(key, value)
}
w.WriteHeader(config.ResponseCode)
if config.ResponseBody != "" {
w.Write([]byte(config.ResponseBody))
}
return
}
}
// Default 404 for unmatched requests
w.WriteHeader(http.StatusNotFound)
}))
}
// CreateKafkaConnectMockServer creates a standard Kafka Connect API mock server
func (tu *TestUtils) CreateKafkaConnectMockServer(connectors []string, statusCode int) *httptest.Server {
connectorsJSON := `["` + strings.Join(connectors, `","`) + `"]`
if len(connectors) == 0 {
connectorsJSON = "[]"
}
configs := []HTTPServerConfig{
{
Path: "/connectors",
Method: "GET",
ResponseCode: http.StatusOK,
ResponseBody: connectorsJSON,
Headers: map[string]string{"Content-Type": "application/json"},
},
}
// Add DELETE handlers for each connector
for _, connector := range connectors {
configs = append(configs, HTTPServerConfig{
Path: "/connectors/" + connector,
Method: "DELETE",
ResponseCode: statusCode,
})
}
return tu.CreateMockHTTPServer(configs)
}
// TempDirStructure defines a temporary directory structure for testing
type TempDirStructure struct {
Dirs []string
Files map[string]string // filename -> content
}
// CreateTempDirStructure creates a temporary directory with specified structure
func (tu *TestUtils) CreateTempDirStructure(structure TempDirStructure) string {
tempDir := tu.t.TempDir()
// Create directories
for _, dir := range structure.Dirs {
dirPath := filepath.Join(tempDir, dir)
err := os.MkdirAll(dirPath, 0755)
if err != nil {
tu.t.Fatalf("Failed to create directory %s: %v", dirPath, err)
}
}
// Create files
for filename, content := range structure.Files {
filePath := filepath.Join(tempDir, filename)
// Ensure parent directory exists
parentDir := filepath.Dir(filePath)
if parentDir != tempDir {
err := os.MkdirAll(parentDir, 0755)
if err != nil {
tu.t.Fatalf("Failed to create parent directory %s: %v", parentDir, err)
}
}
err := os.WriteFile(filePath, []byte(content), 0644)
if err != nil {
tu.t.Fatalf("Failed to create file %s: %v", filePath, err)
}
}
return tempDir
}
// CreateConfigTestEnv creates a standard config test environment
func (tu *TestUtils) CreateConfigTestEnv(configs map[string]string) string {
structure := TempDirStructure{
Dirs: []string{"case_configs", "volumes", "logs"},
Files: make(map[string]string),
}
// Add config files
for name, content := range configs {
structure.Files["case_configs/"+name] = content
}
// Add standard files
structure.Files[".env"] = "MONGO_KAFKA_CONNECT_VERSION=1.13.0\n"
structure.Files["docker/docker-compose.yaml"] = "version: '3'\nservices:\n test: {}\n"
return tu.CreateTempDirStructure(structure)
}
// DockerCommand represents a Docker command for testing
type DockerCommand struct {
Args []string
ExpectError bool
Timeout time.Duration
}
// ValidateDockerCommand validates Docker command structure without executing
func (tu *TestUtils) ValidateDockerCommand(cmd DockerCommand) {
if len(cmd.Args) == 0 {
tu.t.Error("Docker command should not be empty")
return
}
if cmd.Args[0] != "docker" {
tu.t.Errorf("Expected first argument to be 'docker', got '%s'", cmd.Args[0])
}
// Validate common patterns
if len(cmd.Args) >= 3 && cmd.Args[1] == "exec" {
if len(cmd.Args) < 4 {
tu.t.Error("Docker exec command should have at least 4 arguments")
}
}
}
// TopicFilterConfig defines configuration for topic filtering tests
type TopicFilterConfig struct {
RawTopics []string
ExcludedTopics []string
ExpectedTopics []string
}
// ValidateTopicFiltering tests topic filtering logic
func (tu *TestUtils) ValidateTopicFiltering(config TopicFilterConfig) {
var filteredTopics []string
for _, topic := range config.RawTopics {
topic = strings.TrimSpace(topic)
if len(topic) == 0 {
continue
}
excluded := false
for _, excludedTopic := range config.ExcludedTopics {
if topic == excludedTopic {
excluded = true
break
}
}
if !excluded {
filteredTopics = append(filteredTopics, topic)
}
}
if len(filteredTopics) != len(config.ExpectedTopics) {
tu.t.Errorf("Expected %d topics, got %d", len(config.ExpectedTopics), len(filteredTopics))
return
}
for i, expected := range config.ExpectedTopics {
if filteredTopics[i] != expected {
tu.t.Errorf("Topic %d: expected '%s', got '%s'", i, expected, filteredTopics[i])
}
}
}
// StringCleaningTest represents a test case for string cleaning functions
type StringCleaningTest struct {
Name string
Input string
Expected string
}
// ValidateStringCleaning tests string cleaning operations
func (tu *TestUtils) ValidateStringCleaning(tests []StringCleaningTest, cleanFunc func(string) string) {
for _, test := range tests {
tu.t.Run(test.Name, func(t *testing.T) {
result := cleanFunc(test.Input)
if result != test.Expected {
t.Errorf("Expected '%s', got '%s'", test.Expected, result)
}
})
}
}
// PortAvailability checks if a port is available for testing
func (tu *TestUtils) PortAvailability(port string) bool {
// This is a simplified check - in real testing you might want more robust checking
return len(port) > 0 && port != "0"
}
// ErrorTestCase represents a test case for error handling
type ErrorTestCase struct {
Name string
Setup func() error
ExpectError bool
ErrorCheck func(error) bool // Optional: custom error validation
}
// RunErrorTests runs a series of error handling tests
func (tu *TestUtils) RunErrorTests(cases []ErrorTestCase) {
for _, testCase := range cases {
tu.t.Run(testCase.Name, func(t *testing.T) {
err := testCase.Setup()
if testCase.ExpectError && err == nil {
t.Error("Expected error but got none")
}
if !testCase.ExpectError && err != nil {
t.Errorf("Unexpected error: %v", err)
}
if testCase.ErrorCheck != nil && err != nil {
if !testCase.ErrorCheck(err) {
t.Errorf("Error validation failed for: %v", err)
}
}
})
}
}
// FileExists checks if a file exists
func (tu *TestUtils) FileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// ReadFileContent reads file content and handles errors
func (tu *TestUtils) ReadFileContent(path string) string {
content, err := os.ReadFile(path)
if err != nil {
tu.t.Fatalf("Failed to read file %s: %v", path, err)
}
return string(content)
}
// CreateJSONFile creates a JSON file with specified content
func (tu *TestUtils) CreateJSONFile(dir, filename, content string) string {
filePath := filepath.Join(dir, filename)
err := os.WriteFile(filePath, []byte(content), 0644)
if err != nil {
tu.t.Fatalf("Failed to create JSON file %s: %v", filePath, err)
}
return filePath
}
// StandardKafkaBootstrapServers returns the standard Kafka bootstrap servers used in tests
func (tu *TestUtils) StandardKafkaBootstrapServers() string {
return "kafka2:19092,kafka3:19093,kafka1:19091"
}
// ValidateBootstrapServers validates that all expected Kafka brokers are present
func (tu *TestUtils) ValidateBootstrapServers(bootstrapServers string) {
expectedServers := []string{"kafka1:19091", "kafka2:19092", "kafka3:19093"}
for _, server := range expectedServers {
if !strings.Contains(bootstrapServers, server) {
tu.t.Errorf("Bootstrap servers missing expected server: %s", server)
}
}
}
// BenchmarkHelper provides utilities for benchmark tests
type BenchmarkHelper struct {
b *testing.B
}
// NewBenchmarkHelper creates a new benchmark helper
func NewBenchmarkHelper(b *testing.B) *BenchmarkHelper {
return &BenchmarkHelper{b: b}
}
// RunWithReset runs a function with timer reset for benchmarks
func (bh *BenchmarkHelper) RunWithReset(fn func()) {
bh.b.ResetTimer()
fn()
}
// CleanupHelper provides utilities for test cleanup
type CleanupHelper struct {
t *testing.T
items []func()
}
// NewCleanupHelper creates a new cleanup helper
func NewCleanupHelper(t *testing.T) *CleanupHelper {
return &CleanupHelper{t: t}
}
// Add adds a cleanup function
func (ch *CleanupHelper) Add(fn func()) {
ch.items = append(ch.items, fn)
}
// Cleanup runs all cleanup functions
func (ch *CleanupHelper) Cleanup() {
for _, fn := range ch.items {
fn()
}
}