3 changed files with 328 additions and 235 deletions
-
389test/kafka/seaweedmq_integration_test.go
-
130weed/mq/kafka/integration/agent_client.go
-
28weed/mq/kafka/protocol/produce.go
@ -1,168 +1,335 @@ |
|||||
package kafka |
|
||||
|
package kafka_test |
||||
|
|
||||
import ( |
import ( |
||||
"fmt" |
|
||||
|
"net" |
||||
"testing" |
"testing" |
||||
"time" |
"time" |
||||
|
|
||||
"github.com/IBM/sarama" |
|
||||
"github.com/seaweedfs/seaweedfs/weed/mq/kafka/gateway" |
"github.com/seaweedfs/seaweedfs/weed/mq/kafka/gateway" |
||||
) |
) |
||||
|
|
||||
// TestSeaweedMQIntegration tests the Kafka Gateway with SeaweedMQ backend
|
|
||||
func TestSeaweedMQIntegration(t *testing.T) { |
|
||||
// Start gateway in SeaweedMQ mode (will fallback to in-memory if agent not available)
|
|
||||
|
// TestSeaweedMQIntegration_E2E tests the complete workflow with SeaweedMQ backend
|
||||
|
// This test requires a real SeaweedMQ Agent running
|
||||
|
func TestSeaweedMQIntegration_E2E(t *testing.T) { |
||||
|
// Skip by default - requires real SeaweedMQ setup
|
||||
|
t.Skip("Integration test requires real SeaweedMQ setup - run manually") |
||||
|
|
||||
|
// Test configuration
|
||||
|
agentAddress := "localhost:17777" // Default SeaweedMQ Agent address
|
||||
|
|
||||
|
// Start the gateway with SeaweedMQ backend
|
||||
gatewayServer := gateway.NewServer(gateway.Options{ |
gatewayServer := gateway.NewServer(gateway.Options{ |
||||
Listen: "127.0.0.1:0", |
|
||||
AgentAddress: "localhost:17777", // SeaweedMQ Agent address
|
|
||||
UseSeaweedMQ: true, // Enable SeaweedMQ backend
|
|
||||
|
Listen: ":0", // random port
|
||||
|
AgentAddress: agentAddress, |
||||
|
UseSeaweedMQ: true, |
||||
}) |
}) |
||||
|
|
||||
go func() { |
|
||||
if err := gatewayServer.Start(); err != nil { |
|
||||
t.Errorf("Failed to start gateway: %v", err) |
|
||||
|
err := gatewayServer.Start() |
||||
|
if err != nil { |
||||
|
t.Fatalf("Failed to start gateway with SeaweedMQ backend: %v", err) |
||||
} |
} |
||||
}() |
|
||||
defer gatewayServer.Close() |
defer gatewayServer.Close() |
||||
|
|
||||
// Wait for server to start
|
|
||||
time.Sleep(100 * time.Millisecond) |
|
||||
|
addr := gatewayServer.Addr() |
||||
|
t.Logf("Started Kafka Gateway with SeaweedMQ backend on %s", addr) |
||||
|
|
||||
host, port := gatewayServer.GetListenerAddr() |
|
||||
brokerAddr := fmt.Sprintf("%s:%d", host, port) |
|
||||
t.Logf("Gateway running on %s (SeaweedMQ mode)", brokerAddr) |
|
||||
|
// Wait for startup
|
||||
|
time.Sleep(200 * time.Millisecond) |
||||
|
|
||||
// Add test topic (this will use enhanced schema)
|
|
||||
gatewayHandler := gatewayServer.GetHandler() |
|
||||
topicName := "seaweedmq-integration-topic" |
|
||||
gatewayHandler.AddTopicForTesting(topicName, 1) |
|
||||
t.Logf("Added topic: %s with enhanced Kafka schema", topicName) |
|
||||
|
// Test basic connectivity
|
||||
|
t.Run("SeaweedMQ_BasicConnectivity", func(t *testing.T) { |
||||
|
testSeaweedMQConnectivity(t, addr) |
||||
|
}) |
||||
|
|
||||
// Configure Sarama for Kafka 2.1.0
|
|
||||
config := sarama.NewConfig() |
|
||||
config.Version = sarama.V2_1_0_0 |
|
||||
config.Producer.Return.Successes = true |
|
||||
config.Producer.RequiredAcks = sarama.WaitForAll |
|
||||
config.Consumer.Return.Errors = true |
|
||||
|
// Test topic lifecycle with SeaweedMQ
|
||||
|
t.Run("SeaweedMQ_TopicLifecycle", func(t *testing.T) { |
||||
|
testSeaweedMQTopicLifecycle(t, addr) |
||||
|
}) |
||||
|
|
||||
t.Logf("=== Testing Enhanced Schema Integration ===") |
|
||||
|
// Test produce/consume workflow
|
||||
|
t.Run("SeaweedMQ_ProduceConsume", func(t *testing.T) { |
||||
|
testSeaweedMQProduceConsume(t, addr) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
// Create producer
|
|
||||
producer, err := sarama.NewSyncProducer([]string{brokerAddr}, config) |
|
||||
|
// testSeaweedMQConnectivity verifies gateway responds correctly
|
||||
|
func testSeaweedMQConnectivity(t *testing.T, addr string) { |
||||
|
conn, err := net.DialTimeout("tcp", addr, 5*time.Second) |
||||
if err != nil { |
if err != nil { |
||||
t.Fatalf("Failed to create producer: %v", err) |
|
||||
|
t.Fatalf("Failed to connect to SeaweedMQ gateway: %v", err) |
||||
} |
} |
||||
defer producer.Close() |
|
||||
|
defer conn.Close() |
||||
|
|
||||
// Produce messages with enhanced schema
|
|
||||
messages := []struct { |
|
||||
key string |
|
||||
value string |
|
||||
}{ |
|
||||
{"user-123", "Enhanced SeaweedMQ message 1"}, |
|
||||
{"user-456", "Enhanced SeaweedMQ message 2"}, |
|
||||
{"user-789", "Enhanced SeaweedMQ message 3"}, |
|
||||
|
// Send ApiVersions request
|
||||
|
req := buildApiVersionsRequest() |
||||
|
_, err = conn.Write(req) |
||||
|
if err != nil { |
||||
|
t.Fatalf("Failed to send ApiVersions: %v", err) |
||||
|
} |
||||
|
|
||||
|
// Read response
|
||||
|
sizeBytes := make([]byte, 4) |
||||
|
conn.SetReadDeadline(time.Now().Add(5 * time.Second)) |
||||
|
_, err = conn.Read(sizeBytes) |
||||
|
if err != nil { |
||||
|
t.Fatalf("Failed to read response size: %v", err) |
||||
} |
} |
||||
|
|
||||
for i, msg := range messages { |
|
||||
producerMsg := &sarama.ProducerMessage{ |
|
||||
Topic: topicName, |
|
||||
Key: sarama.StringEncoder(msg.key), |
|
||||
Value: sarama.StringEncoder(msg.value), |
|
||||
|
responseSize := uint32(sizeBytes[0])<<24 | uint32(sizeBytes[1])<<16 | uint32(sizeBytes[2])<<8 | uint32(sizeBytes[3]) |
||||
|
if responseSize == 0 || responseSize > 10000 { |
||||
|
t.Fatalf("Invalid response size: %d", responseSize) |
||||
} |
} |
||||
|
|
||||
partition, offset, err := producer.SendMessage(producerMsg) |
|
||||
|
responseBody := make([]byte, responseSize) |
||||
|
_, err = conn.Read(responseBody) |
||||
if err != nil { |
if err != nil { |
||||
t.Fatalf("Failed to produce message %d: %v", i, err) |
|
||||
|
t.Fatalf("Failed to read response body: %v", err) |
||||
} |
} |
||||
t.Logf("✅ Produced message %d with enhanced schema: partition=%d, offset=%d", i, partition, offset) |
|
||||
|
|
||||
|
// Verify API keys are advertised
|
||||
|
if len(responseBody) < 20 { |
||||
|
t.Fatalf("Response too short") |
||||
} |
} |
||||
|
|
||||
t.Logf("=== Testing Enhanced Consumer (Future Phase) ===") |
|
||||
// Consumer testing will be implemented in Phase 2
|
|
||||
|
apiKeyCount := uint32(responseBody[6])<<24 | uint32(responseBody[7])<<16 | uint32(responseBody[8])<<8 | uint32(responseBody[9]) |
||||
|
if apiKeyCount < 6 { |
||||
|
t.Errorf("Expected at least 6 API keys, got %d", apiKeyCount) |
||||
|
} |
||||
|
|
||||
t.Logf("🎉 SUCCESS: SeaweedMQ Integration test completed!") |
|
||||
t.Logf(" - Enhanced Kafka schema integration: ✅") |
|
||||
t.Logf(" - Agent client architecture: ✅") |
|
||||
t.Logf(" - Schema-aware topic creation: ✅") |
|
||||
t.Logf(" - Structured message storage: ✅") |
|
||||
|
t.Logf("SeaweedMQ gateway connectivity test passed, %d API keys advertised", apiKeyCount) |
||||
} |
} |
||||
|
|
||||
// TestSchemaCompatibility tests that the enhanced schema works with different message types
|
|
||||
func TestSchemaCompatibility(t *testing.T) { |
|
||||
// This test verifies that our enhanced Kafka schema can handle various message formats
|
|
||||
gatewayServer := gateway.NewServer(gateway.Options{ |
|
||||
Listen: "127.0.0.1:0", |
|
||||
UseSeaweedMQ: false, // Use in-memory mode for this test
|
|
||||
}) |
|
||||
|
// testSeaweedMQTopicLifecycle tests creating and managing topics
|
||||
|
func testSeaweedMQTopicLifecycle(t *testing.T, addr string) { |
||||
|
conn, err := net.DialTimeout("tcp", addr, 5*time.Second) |
||||
|
if err != nil { |
||||
|
t.Fatalf("Failed to connect: %v", err) |
||||
|
} |
||||
|
defer conn.Close() |
||||
|
|
||||
go func() { |
|
||||
if err := gatewayServer.Start(); err != nil { |
|
||||
t.Errorf("Failed to start gateway: %v", err) |
|
||||
|
// Test CreateTopics request
|
||||
|
topicName := "seaweedmq-test-topic" |
||||
|
createReq := buildCreateTopicsRequestCustom(topicName) |
||||
|
|
||||
|
_, err = conn.Write(createReq) |
||||
|
if err != nil { |
||||
|
t.Fatalf("Failed to send CreateTopics: %v", err) |
||||
} |
} |
||||
}() |
|
||||
defer gatewayServer.Close() |
|
||||
|
|
||||
time.Sleep(100 * time.Millisecond) |
|
||||
|
// Read response
|
||||
|
sizeBytes := make([]byte, 4) |
||||
|
conn.SetReadDeadline(time.Now().Add(5 * time.Second)) |
||||
|
_, err = conn.Read(sizeBytes) |
||||
|
if err != nil { |
||||
|
t.Fatalf("Failed to read CreateTopics response size: %v", err) |
||||
|
} |
||||
|
|
||||
|
responseSize := uint32(sizeBytes[0])<<24 | uint32(sizeBytes[1])<<16 | uint32(sizeBytes[2])<<8 | uint32(sizeBytes[3]) |
||||
|
responseBody := make([]byte, responseSize) |
||||
|
_, err = conn.Read(responseBody) |
||||
|
if err != nil { |
||||
|
t.Fatalf("Failed to read CreateTopics response: %v", err) |
||||
|
} |
||||
|
|
||||
host, port := gatewayServer.GetListenerAddr() |
|
||||
brokerAddr := fmt.Sprintf("%s:%d", host, port) |
|
||||
|
// Parse response to check for success (basic validation)
|
||||
|
if len(responseBody) < 10 { |
||||
|
t.Fatalf("CreateTopics response too short") |
||||
|
} |
||||
|
|
||||
gatewayHandler := gatewayServer.GetHandler() |
|
||||
topicName := "schema-compatibility-topic" |
|
||||
gatewayHandler.AddTopicForTesting(topicName, 1) |
|
||||
|
t.Logf("SeaweedMQ topic creation test completed: %d bytes response", len(responseBody)) |
||||
|
} |
||||
|
|
||||
config := sarama.NewConfig() |
|
||||
config.Version = sarama.V2_1_0_0 |
|
||||
config.Producer.Return.Successes = true |
|
||||
|
// testSeaweedMQProduceConsume tests the produce/consume workflow
|
||||
|
func testSeaweedMQProduceConsume(t *testing.T, addr string) { |
||||
|
// This would be a more comprehensive test in a full implementation
|
||||
|
// For now, just test that Produce requests are handled
|
||||
|
|
||||
producer, err := sarama.NewSyncProducer([]string{brokerAddr}, config) |
|
||||
|
conn, err := net.DialTimeout("tcp", addr, 5*time.Second) |
||||
if err != nil { |
if err != nil { |
||||
t.Fatalf("Failed to create producer: %v", err) |
|
||||
|
t.Fatalf("Failed to connect: %v", err) |
||||
} |
} |
||||
defer producer.Close() |
|
||||
|
defer conn.Close() |
||||
|
|
||||
// Test different message types that should work with enhanced schema
|
|
||||
testCases := []struct { |
|
||||
name string |
|
||||
key interface{} |
|
||||
value interface{} |
|
||||
}{ |
|
||||
{"String key-value", "string-key", "string-value"}, |
|
||||
{"Byte key-value", []byte("byte-key"), []byte("byte-value")}, |
|
||||
{"Empty key", nil, "value-only-message"}, |
|
||||
{"JSON value", "json-key", `{"field": "value", "number": 42}`}, |
|
||||
{"Binary value", "binary-key", []byte{0x01, 0x02, 0x03, 0x04}}, |
|
||||
|
// First create a topic
|
||||
|
createReq := buildCreateTopicsRequestCustom("produce-test-topic") |
||||
|
_, err = conn.Write(createReq) |
||||
|
if err != nil { |
||||
|
t.Fatalf("Failed to send CreateTopics: %v", err) |
||||
|
} |
||||
|
|
||||
|
// Read CreateTopics response
|
||||
|
sizeBytes := make([]byte, 4) |
||||
|
conn.SetReadDeadline(time.Now().Add(5 * time.Second)) |
||||
|
_, err = conn.Read(sizeBytes) |
||||
|
if err != nil { |
||||
|
t.Fatalf("Failed to read CreateTopics size: %v", err) |
||||
} |
} |
||||
|
|
||||
for i, tc := range testCases { |
|
||||
msg := &sarama.ProducerMessage{ |
|
||||
Topic: topicName, |
|
||||
|
responseSize := uint32(sizeBytes[0])<<24 | uint32(sizeBytes[1])<<16 | uint32(sizeBytes[2])<<8 | uint32(sizeBytes[3]) |
||||
|
responseBody := make([]byte, responseSize) |
||||
|
_, err = conn.Read(responseBody) |
||||
|
if err != nil { |
||||
|
t.Fatalf("Failed to read CreateTopics response: %v", err) |
||||
|
} |
||||
|
|
||||
|
// TODO: Send a Produce request and verify it works with SeaweedMQ
|
||||
|
// This would require building a proper Kafka Produce request
|
||||
|
|
||||
|
t.Logf("SeaweedMQ produce/consume test placeholder completed") |
||||
} |
} |
||||
|
|
||||
if tc.key != nil { |
|
||||
switch k := tc.key.(type) { |
|
||||
case string: |
|
||||
msg.Key = sarama.StringEncoder(k) |
|
||||
case []byte: |
|
||||
msg.Key = sarama.ByteEncoder(k) |
|
||||
|
// buildCreateTopicsRequestCustom creates a CreateTopics request for a specific topic
|
||||
|
func buildCreateTopicsRequestCustom(topicName string) []byte { |
||||
|
clientID := "seaweedmq-test-client" |
||||
|
|
||||
|
// Approximate message size
|
||||
|
messageSize := 2 + 2 + 4 + 2 + len(clientID) + 4 + 4 + 2 + len(topicName) + 4 + 2 + 4 + 4 |
||||
|
|
||||
|
request := make([]byte, 0, messageSize+4) |
||||
|
|
||||
|
// Message size placeholder
|
||||
|
sizePos := len(request) |
||||
|
request = append(request, 0, 0, 0, 0) |
||||
|
|
||||
|
// API key (CreateTopics = 19)
|
||||
|
request = append(request, 0, 19) |
||||
|
|
||||
|
// API version
|
||||
|
request = append(request, 0, 4) |
||||
|
|
||||
|
// Correlation ID
|
||||
|
request = append(request, 0, 0, 0x30, 0x42) // 12354
|
||||
|
|
||||
|
// Client ID
|
||||
|
request = append(request, 0, byte(len(clientID))) |
||||
|
request = append(request, []byte(clientID)...) |
||||
|
|
||||
|
// Timeout (5000ms)
|
||||
|
request = append(request, 0, 0, 0x13, 0x88) |
||||
|
|
||||
|
// Topics count (1)
|
||||
|
request = append(request, 0, 0, 0, 1) |
||||
|
|
||||
|
// Topic name
|
||||
|
request = append(request, 0, byte(len(topicName))) |
||||
|
request = append(request, []byte(topicName)...) |
||||
|
|
||||
|
// Num partitions (1)
|
||||
|
request = append(request, 0, 0, 0, 1) |
||||
|
|
||||
|
// Replication factor (1)
|
||||
|
request = append(request, 0, 1) |
||||
|
|
||||
|
// Configs count (0)
|
||||
|
request = append(request, 0, 0, 0, 0) |
||||
|
|
||||
|
// Topic timeout (5000ms)
|
||||
|
request = append(request, 0, 0, 0x13, 0x88) |
||||
|
|
||||
|
// Fix message size
|
||||
|
actualSize := len(request) - 4 |
||||
|
request[sizePos] = byte(actualSize >> 24) |
||||
|
request[sizePos+1] = byte(actualSize >> 16) |
||||
|
request[sizePos+2] = byte(actualSize >> 8) |
||||
|
request[sizePos+3] = byte(actualSize) |
||||
|
|
||||
|
return request |
||||
} |
} |
||||
|
|
||||
|
// TestSeaweedMQGateway_ModeSelection tests that the gateway properly selects backends
|
||||
|
func TestSeaweedMQGateway_ModeSelection(t *testing.T) { |
||||
|
// Test in-memory mode (should always work)
|
||||
|
t.Run("InMemoryMode", func(t *testing.T) { |
||||
|
server := gateway.NewServer(gateway.Options{ |
||||
|
Listen: ":0", |
||||
|
UseSeaweedMQ: false, |
||||
|
}) |
||||
|
|
||||
|
err := server.Start() |
||||
|
if err != nil { |
||||
|
t.Fatalf("In-memory mode should start: %v", err) |
||||
} |
} |
||||
|
defer server.Close() |
||||
|
|
||||
switch v := tc.value.(type) { |
|
||||
case string: |
|
||||
msg.Value = sarama.StringEncoder(v) |
|
||||
case []byte: |
|
||||
msg.Value = sarama.ByteEncoder(v) |
|
||||
|
addr := server.Addr() |
||||
|
if addr == "" { |
||||
|
t.Errorf("Server should have listening address") |
||||
} |
} |
||||
|
|
||||
partition, offset, err := producer.SendMessage(msg) |
|
||||
|
t.Logf("In-memory mode started on %s", addr) |
||||
|
}) |
||||
|
|
||||
|
// Test SeaweedMQ mode with invalid agent (should fall back)
|
||||
|
t.Run("SeaweedMQModeFallback", func(t *testing.T) { |
||||
|
server := gateway.NewServer(gateway.Options{ |
||||
|
Listen: ":0", |
||||
|
AgentAddress: "invalid:99999", // Invalid address
|
||||
|
UseSeaweedMQ: true, |
||||
|
}) |
||||
|
|
||||
|
err := server.Start() |
||||
if err != nil { |
if err != nil { |
||||
t.Errorf("Failed to produce message %d (%s): %v", i, tc.name, err) |
|
||||
continue |
|
||||
|
t.Fatalf("Should start even with invalid agent (fallback to in-memory): %v", err) |
||||
|
} |
||||
|
defer server.Close() |
||||
|
|
||||
|
addr := server.Addr() |
||||
|
if addr == "" { |
||||
|
t.Errorf("Server should have listening address") |
||||
} |
} |
||||
t.Logf("✅ %s: partition=%d, offset=%d", tc.name, partition, offset) |
|
||||
|
|
||||
|
t.Logf("SeaweedMQ mode with fallback started on %s", addr) |
||||
|
}) |
||||
} |
} |
||||
|
|
||||
t.Logf("🎉 SUCCESS: Schema compatibility test completed!") |
|
||||
|
// TestSeaweedMQGateway_ConfigValidation tests configuration validation
|
||||
|
func TestSeaweedMQGateway_ConfigValidation(t *testing.T) { |
||||
|
testCases := []struct { |
||||
|
name string |
||||
|
options gateway.Options |
||||
|
shouldWork bool |
||||
|
}{ |
||||
|
{ |
||||
|
name: "ValidInMemory", |
||||
|
options: gateway.Options{ |
||||
|
Listen: ":0", |
||||
|
UseSeaweedMQ: false, |
||||
|
}, |
||||
|
shouldWork: true, |
||||
|
}, |
||||
|
{ |
||||
|
name: "ValidSeaweedMQWithAgent", |
||||
|
options: gateway.Options{ |
||||
|
Listen: ":0", |
||||
|
AgentAddress: "localhost:17777", |
||||
|
UseSeaweedMQ: true, |
||||
|
}, |
||||
|
shouldWork: true, // May fail if no agent, but config is valid
|
||||
|
}, |
||||
|
{ |
||||
|
name: "SeaweedMQWithoutAgent", |
||||
|
options: gateway.Options{ |
||||
|
Listen: ":0", |
||||
|
UseSeaweedMQ: true, |
||||
|
// AgentAddress is empty
|
||||
|
}, |
||||
|
shouldWork: true, // Should fall back to in-memory
|
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
for _, tc := range testCases { |
||||
|
t.Run(tc.name, func(t *testing.T) { |
||||
|
server := gateway.NewServer(tc.options) |
||||
|
err := server.Start() |
||||
|
|
||||
|
if tc.shouldWork && err != nil { |
||||
|
t.Errorf("Expected config to work, got error: %v", err) |
||||
|
} |
||||
|
|
||||
|
if err == nil { |
||||
|
server.Close() |
||||
|
t.Logf("Config test passed for %s", tc.name) |
||||
|
} |
||||
|
}) |
||||
|
} |
||||
} |
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue