Skip to content

Commit

Permalink
chore: kafka config coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fracasula committed Jan 22, 2024
1 parent 09baea4 commit 4a26115
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
4 changes: 2 additions & 2 deletions kafkaclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ func TestWriteErrors(t *testing.T) {
require.True(t, IsProducerErrTemporary(err))
}

func TestConfluentAzureCloud(t *testing.T) {
func TestConfluentCloud(t *testing.T) {
kafkaHost := os.Getenv("TEST_KAFKA_CONFLUENT_CLOUD_HOST")
confluentCloudKey := os.Getenv("TEST_KAFKA_CONFLUENT_CLOUD_KEY")
confluentCloudSecret := os.Getenv("TEST_KAFKA_CONFLUENT_CLOUD_SECRET")
Expand Down Expand Up @@ -642,7 +642,7 @@ func TestConfluentAzureCloud(t *testing.T) {

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// the topic needs to be created beforehand via the ConfluentCloud admin panel
err = p.Publish(ctx, Message{Key: []byte("key-01"), Value: []byte("value-01"), Topic: t.Name()})
err = p.Publish(ctx, Message{Key: []byte("key-01"), Value: []byte("value-01"), Topic: "TestConfluentAzureCloud"})
cancel()
require.NoError(t, err)

Expand Down
118 changes: 118 additions & 0 deletions kafkaclient/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package client

import (
"testing"

"github.com/segmentio/kafka-go/sasl/plain"
"github.com/stretchr/testify/require"
)

func TestScramHashGeneratorString(t *testing.T) {
t.Run("ok", func(t *testing.T) {
tests := []struct {
generator ScramHashGenerator
expected string
}{
{ScramPlainText, "plain"},
{ScramSHA256, "sha256"},
{ScramSHA512, "sha512"},
}

for _, test := range tests {
require.Equal(t, test.expected, test.generator.String())
}
})
t.Run("panic", func(t *testing.T) {
require.Panics(t, func() {
_ = ScramHashGenerator(123).String()
})
})
}

func TestScramHashGeneratorFromString(t *testing.T) {
t.Run("ok", func(t *testing.T) {
tests := []struct {
generator string
expected ScramHashGenerator
}{
{"plain", ScramPlainText},
{"sha256", ScramSHA256},
{"sha512", ScramSHA512},
}

for _, test := range tests {
generator, err := ScramHashGeneratorFromString(test.generator)
require.NoError(t, err)
require.Equal(t, test.expected, generator)
}
})
t.Run("error", func(t *testing.T) {
_, err := ScramHashGeneratorFromString("foo")
require.Error(t, err)
})
}

func TestTLS(t *testing.T) {
t.Run("empty configuration", func(t *testing.T) {
conf, err := (&TLS{}).build()
require.Nil(t, conf)
require.ErrorContains(t, err, "invalid TLS configuration, either provide certificates or skip validation")
})
t.Run("can run with system cert pool", func(t *testing.T) {
conf, err := (&TLS{WithSystemCertPool: true}).build()
require.NoError(t, err)
require.NotNil(t, conf)
})
t.Run("it fails with invalid ca cert", func(t *testing.T) {
conf, err := (&TLS{CACertificate: []byte("foo")}).build()
require.Nil(t, conf)
require.ErrorContains(t, err, "could not append certs from PEM")
})
t.Run("it fails with invalid cert and key", func(t *testing.T) {
conf, err := (&TLS{Cert: []byte("foo"), Key: []byte("bar"), WithSystemCertPool: true}).build()
require.Nil(t, conf)
require.ErrorContains(t, err, "could not get TLS certificate")
})
}

func TestSASL(t *testing.T) {
t.Run("plain text", func(t *testing.T) {
mechanism, err := (&SASL{
ScramHashGen: ScramPlainText,
Username: "foo",
Password: "bar",
}).build()
require.NoError(t, err)
require.NotNil(t, mechanism)
require.IsType(t, plain.Mechanism{}, mechanism)
})
t.Run("sha256", func(t *testing.T) {
mechanism, err := (&SASL{
ScramHashGen: ScramSHA256,
Username: "foo",
Password: "bar",
}).build()
require.NoError(t, err)
require.NotNil(t, mechanism)
require.Equal(t, "SCRAM-SHA-256", mechanism.Name())
})
t.Run("sha512", func(t *testing.T) {
mechanism, err := (&SASL{
ScramHashGen: ScramSHA512,
Username: "foo",
Password: "bar",
}).build()
require.NoError(t, err)
require.NotNil(t, mechanism)
require.Equal(t, "SCRAM-SHA-512", mechanism.Name())
})
t.Run("error", func(t *testing.T) {
mechanism, err := (&SASL{
ScramHashGen: ScramHashGenerator(123),
Username: "foo",
Password: "bar",
}).build()
require.Nil(t, mechanism)
require.Error(t, err)
})
}

0 comments on commit 4a26115

Please sign in to comment.