Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
403 changes: 382 additions & 21 deletions downstreamadapter/sink/kafka/sink_test.go

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions downstreamadapter/sink/topicmanager/topic_manager_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 0 additions & 78 deletions pkg/sink/kafka/oauth2_token_provider_test.go

This file was deleted.

File renamed without changes.
File renamed without changes.
59 changes: 59 additions & 0 deletions pkg/sink/kafka/sarama_oauth2_token_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@
package kafka

import (
<<<<<<< HEAD

Check failure on line 17 in pkg/sink/kafka/sarama_oauth2_token_provider_test.go

View workflow job for this annotation

GitHub Actions / Build Classic CDC

missing import path
"encoding/json"
"io"
"net/http"
"net/http/httptest"
=======
"context"
>>>>>>> f796ba3e6 (kafka: add more unit test (#5969))
"net/url"
"testing"

"github.com/pingcap/ticdc/pkg/errors"
"github.com/pingcap/ticdc/pkg/security"
"github.com/stretchr/testify/require"
<<<<<<< HEAD
"golang.org/x/oauth2"
)

Expand Down Expand Up @@ -160,6 +165,60 @@
require.Empty(t, retrieveErr.ErrorURI)
require.Empty(t, retrieveErr.Body)
require.NotContains(t, err.Error(), "client-secret-sentinel")
=======
)

func TestNewTokenProvider(t *testing.T) {
t.Parallel()

for _, test := range []struct {
name string
options *options
expectedErr string
}{
{
name: "valid",
options: &options{
SASL: &security.SASL{
OAuth2: security.OAuth2{
ClientID: "client-id",
ClientSecret: "client-secret",
TokenURL: "http://localhost:8080/oauth2/token",
Scopes: []string{"scope1", "scope2"},
GrantType: "client_credentials",
},
},
},
},
{
name: "invalid token URL",
options: &options{
SASL: &security.SASL{
OAuth2: security.OAuth2{
ClientID: "client-id",
ClientSecret: "client-secret",
TokenURL: "http://test.com/Segment%%2815197306101420000%29",
Scopes: []string{"scope1", "scope2"},
GrantType: "client_credentials",
},
},
},
expectedErr: "invalid URL escape",
},
} {
ts := test
t.Run(ts.name, func(t *testing.T) {
t.Parallel()
_, err := newTokenProvider(context.TODO(), ts.options)
if ts.expectedErr == "" {
require.NoError(t, err)
} else {
require.ErrorIs(t, err, errors.ErrKafkaInvalidConfig)
var escapeErr url.EscapeError
require.ErrorAs(t, err, &escapeErr)
require.Contains(t, err.Error(), ts.expectedErr)
}
>>>>>>> f796ba3e6 (kafka: add more unit test (#5969))
})
}
}
73 changes: 73 additions & 0 deletions pkg/sink/kafka/scram_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2026 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package kafka

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/xdg/scram"
)

func TestSCRAMClientHandshake(t *testing.T) {
t.Parallel()

tests := []struct {
name string
hashGenerator scram.HashGeneratorFcn
}{
{name: "SHA-256", hashGenerator: sha256HashGenerator},
{name: "SHA-512", hashGenerator: sha512HashGenerator},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

const (
username = "user"
password = "password"
)
client := &xdgSCRAMClient{HashGeneratorFcn: test.hashGenerator}
require.NoError(t, client.Begin(username, password, ""))

credentialClient, err := test.hashGenerator.NewClient(username, password, "")
require.NoError(t, err)
credentials := credentialClient.GetStoredCredentials(scram.KeyFactors{
Salt: "salt",
Iters: 4096,
})
server, err := test.hashGenerator.NewServer(
func(string) (scram.StoredCredentials, error) { return credentials, nil })
require.NoError(t, err)
serverConversation := server.NewConversation()

clientMessage, err := client.Step("")
require.NoError(t, err)
serverMessage, err := serverConversation.Step(clientMessage)
require.NoError(t, err)
clientMessage, err = client.Step(serverMessage)
require.NoError(t, err)
serverMessage, err = serverConversation.Step(clientMessage)
require.NoError(t, err)
clientMessage, err = client.Step(serverMessage)
require.NoError(t, err)

require.Empty(t, clientMessage)
require.True(t, client.Done())
require.True(t, serverConversation.Done())
require.True(t, serverConversation.Valid())
})
}
}
24 changes: 24 additions & 0 deletions scripts/generate-mock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ if [ ! -f "$MOCKGEN" ]; then
exit 1
fi

<<<<<<< HEAD
pids=()

wait_for_batch() {
Expand Down Expand Up @@ -68,3 +69,26 @@ run_mockgen -source pkg/redo/writer/writer.go -destination pkg/redo/writer/write
run_mockgen -source downstreamadapter/sink/sink.go -destination downstreamadapter/sink/mock/sink_mock.go -package mock

wait_for_batch
=======
"$MOCKGEN" -source coordinator/changefeed/changefeed_db_backend.go -destination coordinator/changefeed/mock/changefeed_db_backend.go
"$MOCKGEN" -source pkg/etcd/etcd.go -destination pkg/etcd/etcd_mock.go -package etcd
"$MOCKGEN" -source pkg/etcd/client.go -destination pkg/etcd/client_mock.go -package etcd
"$MOCKGEN" -source pkg/api/v2/tso.go -destination pkg/api/v2/mock/tso_mock.go -package mock
"$MOCKGEN" -source pkg/api/v2/unsafe.go -destination pkg/api/v2/mock/unsafe_mock.go -package mock
"$MOCKGEN" -source pkg/api/v2/status.go -destination pkg/api/v2/mock/status_mock.go -package mock
"$MOCKGEN" -source pkg/api/v2/capture.go -destination pkg/api/v2/mock/capture_mock.go -package mock
"$MOCKGEN" -source pkg/api/v2/processor.go -destination pkg/api/v2/mock/processor_mock.go -package mock
"$MOCKGEN" -source pkg/api/v2/changefeed.go -destination pkg/api/v2/mock/changefeed_mock.go -package mock
"$MOCKGEN" -source pkg/api/v2/api_client.go -destination pkg/api/v2/mock/api_client_mock.go -package mock
"$MOCKGEN" -source pkg/sink/codec/simple/marshaller.go -destination pkg/sink/codec/simple/mock/marshaller.go
"$MOCKGEN" -source pkg/sink/kafka/admin_client.go -destination pkg/sink/kafka/admin_client_mock.go -package kafka
"$MOCKGEN" -source pkg/sink/kafka/factory.go -destination pkg/sink/kafka/factory_mock.go -package kafka
"$MOCKGEN" -source pkg/sink/kafka/admin.go -destination pkg/sink/kafka/sarama_admin_mock.go -package kafka
"$MOCKGEN" -source pkg/sink/kafka/sarama_sync_producer.go -destination pkg/sink/kafka/sarama_sync_producer_mock.go -package kafka
"$MOCKGEN" -source downstreamadapter/sink/topicmanager/topic_manager.go -destination downstreamadapter/sink/topicmanager/topic_manager_mock.go -package topicmanager
"$MOCKGEN" -source pkg/keyspace/keyspace_manager.go -destination pkg/keyspace/keyspace_manager_mock.go -package keyspace
"$MOCKGEN" -source pkg/txnutil/gc/gc_manager.go -destination pkg/txnutil/gc/gc_manager_mock.go -package gc
"$MOCKGEN" -source pkg/txnutil/gc/gc_client.go -destination pkg/txnutil/gc/gc_client_mock.go -package gc
"$MOCKGEN" -source pkg/redo/writer/writer.go -destination pkg/redo/writer/writer_mock.go -package writer
"$MOCKGEN" -source downstreamadapter/sink/sink.go -destination downstreamadapter/sink/mock/sink_mock.go -package mock
>>>>>>> f796ba3e6 (kafka: add more unit test (#5969))
Loading