test(backend): add Kafka to the shared integration bootstrap
This commit is contained in:
1 parent
65d66b6f7d
commit
1aa7a9feaf
11 files changed
+485
-32
No files matched your search
@@ -0,0 +1,124 @@
|
||||
//go:build integration
|
||||
|
||||
package integration_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/twmb/franz-go/pkg/kerr"
|
||||
"github.com/twmb/franz-go/pkg/kgo"
|
||||
)
|
||||
|
||||
const (
|
||||
kafkaOrderingPartitions = 3
|
||||
kafkaRecordsPerKey = 5
|
||||
kafkaRecordType = "integration.technical-check"
|
||||
)
|
||||
|
||||
// TestKafkaAgainstContainer checks the test harness and broker settings with
|
||||
// technical records. It does not exercise an outbox relay or product events.
|
||||
func TestKafkaAgainstContainer(t *testing.T) {
|
||||
env := bootstrap(t, kafkaService)
|
||||
ctx := t.Context()
|
||||
|
||||
t.Run("keyed records keep headers and partition order", func(t *testing.T) {
|
||||
topic := env.createKafkaTopic(ctx, t, kafkaOrderingPartitions)
|
||||
producer := env.newKafkaClient(t)
|
||||
keys := []string{"aggregate-a", "aggregate-b", "aggregate-c"}
|
||||
sent := make([]*kgo.Record, 0, len(keys)*kafkaRecordsPerKey)
|
||||
for sequence := range kafkaRecordsPerKey {
|
||||
for _, key := range keys {
|
||||
sent = append(sent, technicalRecord(topic, key, sequence))
|
||||
}
|
||||
}
|
||||
produceKafkaRecords(ctx, t, producer, sent...)
|
||||
|
||||
received := env.consumeKafkaRecords(ctx, t, topic, len(sent))
|
||||
if len(received) != len(sent) {
|
||||
t.Fatalf("received %d records, want %d", len(received), len(sent))
|
||||
}
|
||||
byKey := make(map[string][]*kgo.Record, len(keys))
|
||||
for _, record := range received {
|
||||
byKey[string(record.Key)] = append(byKey[string(record.Key)], record)
|
||||
}
|
||||
for _, key := range keys {
|
||||
assertKeyedSequence(t, key, byKey[key])
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("missing topic is not created on demand", func(t *testing.T) {
|
||||
topic := newKafkaTopicName()
|
||||
// The client asks the broker to create unknown topics; the broker
|
||||
// setting must refuse, so a missing topic surfaces as an error.
|
||||
producer := env.newKafkaClient(t,
|
||||
kgo.AllowAutoTopicCreation(),
|
||||
kgo.MetadataMinAge(readinessInterval),
|
||||
)
|
||||
produceCtx, cancel := context.WithTimeout(ctx, kafkaOperationTimeout)
|
||||
defer cancel()
|
||||
err := producer.ProduceSync(produceCtx, technicalRecord(topic, "aggregate-a", 0)).FirstErr()
|
||||
if !errors.Is(err, kerr.UnknownTopicOrPartition) {
|
||||
t.Fatalf("produce to missing topic: %v, want %v", err, kerr.UnknownTopicOrPartition)
|
||||
}
|
||||
if kafkaTopicExists(ctx, t, env.KafkaAdmin, topic) {
|
||||
t.Fatalf("broker created missing topic %s", topic)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("topic cleanup runs when the test finishes", func(t *testing.T) {
|
||||
var topic string
|
||||
t.Run("owner", func(t *testing.T) {
|
||||
topic = env.createKafkaTopic(ctx, t, 1)
|
||||
produceKafkaRecords(ctx, t, env.newKafkaClient(t), technicalRecord(topic, "aggregate-a", 0))
|
||||
if !kafkaTopicExists(ctx, t, env.KafkaAdmin, topic) {
|
||||
t.Fatalf("created topic %s is missing", topic)
|
||||
}
|
||||
})
|
||||
if topic == "" {
|
||||
t.Fatal("owner test did not create a topic")
|
||||
}
|
||||
waitForKafkaTopicDeletion(ctx, t, env.KafkaAdmin, topic)
|
||||
})
|
||||
}
|
||||
|
||||
func technicalRecord(topic, key string, sequence int) *kgo.Record {
|
||||
return &kgo.Record{
|
||||
Topic: topic,
|
||||
Key: []byte(key),
|
||||
Value: []byte(`{"sequence":` + strconv.Itoa(sequence) + `}`),
|
||||
Headers: []kgo.RecordHeader{
|
||||
{Key: "event-type", Value: []byte(kafkaRecordType)},
|
||||
{Key: "sequence", Value: []byte(strconv.Itoa(sequence))},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// assertKeyedSequence checks that records with one key share a partition and
|
||||
// arrive in production order with their payload and headers intact.
|
||||
func assertKeyedSequence(t *testing.T, key string, records []*kgo.Record) {
|
||||
t.Helper()
|
||||
if len(records) != kafkaRecordsPerKey {
|
||||
t.Fatalf("key %s: received %d records, want %d", key, len(records), kafkaRecordsPerKey)
|
||||
}
|
||||
for sequence, record := range records {
|
||||
want := technicalRecord(record.Topic, key, sequence)
|
||||
if record.Partition != records[0].Partition {
|
||||
t.Errorf("key %s: record %d in partition %d, first in %d", key, sequence, record.Partition, records[0].Partition)
|
||||
}
|
||||
if sequence > 0 && record.Offset <= records[sequence-1].Offset {
|
||||
t.Errorf("key %s: offset %d does not follow %d", key, record.Offset, records[sequence-1].Offset)
|
||||
}
|
||||
if string(record.Value) != string(want.Value) {
|
||||
t.Errorf("key %s: record %d value %q, want %q", key, sequence, record.Value, want.Value)
|
||||
}
|
||||
if !slices.EqualFunc(record.Headers, want.Headers, func(got, want kgo.RecordHeader) bool {
|
||||
return got.Key == want.Key && string(got.Value) == string(want.Value)
|
||||
}) {
|
||||
t.Errorf("key %s: record %d headers %v, want %v", key, sequence, record.Headers, want.Headers)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user