|
| 1 | +package kafka |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/confluentinc/confluent-kafka-go/v2/kafka" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "go.opentelemetry.io/otel/metric/noop" |
| 13 | +) |
| 14 | + |
| 15 | +// FIXME(chrisgacsal): move discardHandler to 'testutils' pkg after import cycle is resolved. |
| 16 | +// discardHandler is a slog.Handler implementation which does not emit log messages |
| 17 | +// See: https://go-review.googlesource.com/c/go/+/548335/5/src/log/slog/example_discard_test.go#14 |
| 18 | +type discardHandler struct { |
| 19 | + slog.JSONHandler |
| 20 | +} |
| 21 | + |
| 22 | +func (d *discardHandler) Enabled(context.Context, slog.Level) bool { return false } |
| 23 | + |
| 24 | +func NewDiscardLogger(t testing.TB) *slog.Logger { |
| 25 | + t.Helper() |
| 26 | + |
| 27 | + return slog.New(&discardHandler{}) |
| 28 | +} |
| 29 | + |
| 30 | +var _ AdminClient = (*mockTopicProvisioner)(nil) |
| 31 | + |
| 32 | +type mockTopicProvisioner struct { |
| 33 | + added []string |
| 34 | + removed []string |
| 35 | +} |
| 36 | + |
| 37 | +func (m *mockTopicProvisioner) CreateTopics(_ context.Context, topics []kafka.TopicSpecification, _ ...kafka.CreateTopicsAdminOption) ([]kafka.TopicResult, error) { |
| 38 | + result := make([]kafka.TopicResult, 0, len(topics)) |
| 39 | + |
| 40 | + for _, topic := range topics { |
| 41 | + m.added = append(m.added, topic.Topic) |
| 42 | + |
| 43 | + result = append(result, kafka.TopicResult{ |
| 44 | + Topic: topic.Topic, |
| 45 | + Error: kafka.NewError(kafka.ErrNoError, "", false), |
| 46 | + }) |
| 47 | + } |
| 48 | + |
| 49 | + return result, nil |
| 50 | +} |
| 51 | + |
| 52 | +func (m *mockTopicProvisioner) DeleteTopics(_ context.Context, topics []string, _ ...kafka.DeleteTopicsAdminOption) ([]kafka.TopicResult, error) { |
| 53 | + result := make([]kafka.TopicResult, 0, len(topics)) |
| 54 | + |
| 55 | + for _, topic := range topics { |
| 56 | + m.removed = append(m.removed, topic) |
| 57 | + |
| 58 | + result = append(result, kafka.TopicResult{ |
| 59 | + Topic: topic, |
| 60 | + Error: kafka.NewError(kafka.ErrNoError, "", false), |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + return result, nil |
| 65 | +} |
| 66 | + |
| 67 | +func (m *mockTopicProvisioner) reset() { |
| 68 | + m.added, m.removed = []string{}, []string{} |
| 69 | +} |
| 70 | + |
| 71 | +func TestTopicProvisioner(t *testing.T) { |
| 72 | + tests := []struct { |
| 73 | + Name string |
| 74 | + |
| 75 | + AddTopics []TopicConfig |
| 76 | + RemoveTopics []string |
| 77 | + |
| 78 | + ExpectedError error |
| 79 | + ExpectedAddedTopics []string |
| 80 | + ExpectedRemovedTopics []string |
| 81 | + }{ |
| 82 | + { |
| 83 | + Name: "Add topics", |
| 84 | + AddTopics: []TopicConfig{ |
| 85 | + { |
| 86 | + Name: "topic-1", |
| 87 | + Partitions: 1, |
| 88 | + }, |
| 89 | + |
| 90 | + { |
| 91 | + Name: "topic-2", |
| 92 | + Partitions: 1, |
| 93 | + }, |
| 94 | + }, |
| 95 | + ExpectedError: nil, |
| 96 | + ExpectedAddedTopics: []string{"topic-1", "topic-2"}, |
| 97 | + ExpectedRemovedTopics: []string{}, |
| 98 | + }, |
| 99 | + { |
| 100 | + Name: "Remove topics", |
| 101 | + RemoveTopics: []string{"topic-1", "topic-2"}, |
| 102 | + ExpectedError: nil, |
| 103 | + ExpectedAddedTopics: []string{}, |
| 104 | + ExpectedRemovedTopics: []string{"topic-1", "topic-2"}, |
| 105 | + }, |
| 106 | + { |
| 107 | + Name: "Remove protected topics", |
| 108 | + RemoveTopics: []string{"protected-topic-1", "protected-topic-2"}, |
| 109 | + ExpectedError: nil, |
| 110 | + ExpectedAddedTopics: []string{}, |
| 111 | + ExpectedRemovedTopics: []string{}, |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + adminClient := &mockTopicProvisioner{} |
| 116 | + meter := noop.NewMeterProvider().Meter("test") |
| 117 | + logger := NewDiscardLogger(t) |
| 118 | + |
| 119 | + provisioner, err := NewTopicProvisioner(TopicProvisionerConfig{ |
| 120 | + AdminClient: adminClient, |
| 121 | + Logger: logger, |
| 122 | + Meter: meter, |
| 123 | + CacheSize: 200, |
| 124 | + CacheTTL: 5 * time.Second, |
| 125 | + ProtectedTopics: []string{ |
| 126 | + "protected-topic-1", |
| 127 | + "protected-topic-2", |
| 128 | + }, |
| 129 | + }) |
| 130 | + require.NoError(t, err, "initializing new topic provisioner should not fail") |
| 131 | + |
| 132 | + for _, test := range tests { |
| 133 | + t.Run(test.Name, func(t *testing.T) { |
| 134 | + adminClient.reset() |
| 135 | + |
| 136 | + ctx := context.TODO() |
| 137 | + |
| 138 | + err = provisioner.Provision(ctx, test.AddTopics...) |
| 139 | + assert.NoError(t, err, "provisioning topics must not fail") |
| 140 | + |
| 141 | + assert.ElementsMatch(t, test.ExpectedAddedTopics, adminClient.added, "provisioned topics must match") |
| 142 | + |
| 143 | + err = provisioner.DeProvision(ctx, test.RemoveTopics...) |
| 144 | + assert.NoError(t, err, "de-provisioning topics must not fail") |
| 145 | + |
| 146 | + assert.ElementsMatch(t, test.ExpectedRemovedTopics, adminClient.removed, "de-provisioned topics must match") |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
0 commit comments