Files
akvorado/common/kafka/config_test.go
Vincent Bernat 756e4a8fbd */kafka: switch to franz-go
The concurrency of this library is easier to handle than Sarama.
Notably, it is more compatible with the new model of "almost share
nothing" we use for the inlet and the outlet. The lock for workers in
outlet is removed. We can now use sync.Pool to allocate slice of bytes
in inlet.

It may also be more performant.

In the future, we may want to commit only when pushing data to
ClickHouse. However, this does not seem easy when there is a rebalance.
In case of rebalance, we need to do something when a partition is
revoked to avoid duplicating data. For example, we could flush the
current batch to ClickHouse. Have a look at the
`example/mark_offsets/main.go` file in franz-go repository for a
possible approach. In the meantime, we rely on autocommit.

Another contender could be https://github.com/segmentio/kafka-go. Also
see https://github.com/twmb/franz-go/pull/1064.
2025-07-27 21:44:28 +02:00

261 lines
6.1 KiB
Go

// SPDX-FileCopyrightText: 2022 Free Mobile
// SPDX-License-Identifier: AGPL-3.0-only
package kafka
import (
"testing"
"akvorado/common/helpers"
"akvorado/common/reporter"
"github.com/gin-gonic/gin"
)
func TestDefaultConfiguration(t *testing.T) {
if err := helpers.Validate.Struct(DefaultConfiguration()); err != nil {
t.Fatalf("validate.Struct() error:\n%+v", err)
}
}
func TestKafkaNewConfig(t *testing.T) {
// It is a bit a pain to test the result, just check we don't have an error
cases := []struct {
description string
config Configuration
}{
{
description: "No TLS",
config: DefaultConfiguration(),
}, {
description: "SASL plain",
config: Configuration{
TLS: helpers.TLSConfiguration{
Enable: true,
},
SASL: SASLConfiguration{
Username: "hello",
Password: "password",
Mechanism: SASLPlain,
},
},
}, {
description: "SASL SCRAM SHA256",
config: Configuration{
TLS: helpers.TLSConfiguration{
Enable: true,
},
SASL: SASLConfiguration{
Username: "hello",
Password: "password",
Mechanism: SASLScramSHA256,
},
},
}, {
description: "SASL SCRAM SHA512",
config: Configuration{
TLS: helpers.TLSConfiguration{
Enable: true,
},
SASL: SASLConfiguration{
Username: "hello",
Password: "password",
Mechanism: SASLScramSHA512,
},
},
}, {
description: "SASL OAuth2",
config: Configuration{
TLS: helpers.TLSConfiguration{
Enable: true,
},
SASL: SASLConfiguration{
Username: "hello",
Password: "password",
Mechanism: SASLOauth,
OAuthTokenURL: "http://example.com/token",
},
},
},
}
for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
r := reporter.NewMock(t)
_, err := NewConfig(r, tc.config)
if err != nil {
t.Fatalf("NewConfig() error:\n%+v", err)
}
})
}
}
func TestTLSConfiguration(t *testing.T) {
helpers.TestConfigurationDecode(t, helpers.ConfigurationDecodeCases{
{
Description: "no TLS",
Initial: func() interface{} { return DefaultConfiguration() },
Configuration: func() interface{} { return nil },
Expected: DefaultConfiguration(),
}, {
Description: "TLS without auth",
Initial: func() interface{} { return DefaultConfiguration() },
Configuration: func() interface{} {
return gin.H{
"tls": gin.H{
"enable": true,
},
}
},
Expected: Configuration{
Topic: "flows",
Brokers: []string{"127.0.0.1:9092"},
TLS: helpers.TLSConfiguration{
Enable: true,
Verify: true,
},
},
}, {
Description: "TLS SASL plain, skip cert verification (old style)",
Initial: func() interface{} { return DefaultConfiguration() },
Configuration: func() interface{} {
return gin.H{
"tls": gin.H{
"enable": true,
"verify": false,
"sasl-username": "hello",
"sasl-password": "bye",
"sasl-mechanism": "plain",
},
}
},
Expected: Configuration{
Topic: "flows",
Brokers: []string{"127.0.0.1:9092"},
TLS: helpers.TLSConfiguration{
Enable: true,
Verify: false,
},
SASL: SASLConfiguration{
Username: "hello",
Password: "bye",
Mechanism: SASLPlain,
},
},
}, {
Description: "TLS SASL plain, skip cert verification",
Initial: func() interface{} { return DefaultConfiguration() },
Configuration: func() interface{} {
return gin.H{
"sasl": gin.H{
"username": "hello",
"password": "bye",
"mechanism": "plain",
},
}
},
Expected: Configuration{
Topic: "flows",
Brokers: []string{"127.0.0.1:9092"},
TLS: helpers.TLSConfiguration{
Enable: false,
Verify: true,
},
SASL: SASLConfiguration{
Username: "hello",
Password: "bye",
Mechanism: SASLPlain,
},
},
}, {
Description: "TLS SASL SCRAM 256",
Initial: func() interface{} { return DefaultConfiguration() },
Configuration: func() interface{} {
return gin.H{
"tls": gin.H{
"enable": true,
},
"sasl": gin.H{
"username": "hello",
"password": "bye",
"mechanism": "scram-sha256",
},
}
},
Expected: Configuration{
Topic: "flows",
Brokers: []string{"127.0.0.1:9092"},
TLS: helpers.TLSConfiguration{
Enable: true,
// Value from DefaultConfig is true
Verify: true,
},
SASL: SASLConfiguration{
Username: "hello",
Password: "bye",
Mechanism: SASLScramSHA256,
},
},
}, {
Description: "TLS SASL OAuth",
Initial: func() interface{} { return DefaultConfiguration() },
Configuration: func() interface{} {
return gin.H{
"tls": gin.H{
"enable": true,
},
"sasl": gin.H{
"username": "hello",
"password": "bye",
"mechanism": "oauth",
"oauth-token-url": "http://example.com/token",
"oauth-scopes": "one,two",
},
}
},
Expected: Configuration{
Topic: "flows",
Brokers: []string{"127.0.0.1:9092"},
TLS: helpers.TLSConfiguration{
Enable: true,
// Value from DefaultConfig is true
Verify: true,
},
SASL: SASLConfiguration{
Username: "hello",
Password: "bye",
Mechanism: SASLOauth,
OAuthTokenURL: "http://example.com/token",
OAuthScopes: []string{"one", "two"},
},
},
}, {
Description: "OAuth requires a token URL",
Initial: func() interface{} { return DefaultConfiguration() },
Configuration: func() interface{} {
return gin.H{
"sasl": gin.H{
"username": "hello",
"password": "bye",
"mechanism": "oauth",
},
}
},
Error: true,
}, {
Description: "OAuth token URL only with OAuth",
Initial: func() interface{} { return DefaultConfiguration() },
Configuration: func() interface{} {
return gin.H{
"sasl": gin.H{
"username": "hello",
"password": "bye",
"mechanism": "plain",
"oauth-token-url": "http://example.com/token",
},
}
},
Error: true,
},
})
}