Skip to content

Commit

Permalink
Add functional test for admin client quotas
Browse files Browse the repository at this point in the history
  • Loading branch information
agriffaut committed Dec 30, 2021
1 parent a1807bf commit 581f074
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions functional_admin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//go:build functional
// +build functional

package sarama

import (
"testing"
)

func TestFuncAdminQuotas(t *testing.T) {
checkKafkaVersion(t, "2.6.0.0")
setupFunctionalTest(t)
defer teardownFunctionalTest(t)

kafkaVersion, err := ParseKafkaVersion(FunctionalTestEnv.KafkaVersion)
if err != nil {
t.Fatal(err)
}

config := NewTestConfig()
config.Version = kafkaVersion
adminClient, err := NewClusterAdmin(FunctionalTestEnv.KafkaBrokerAddrs, config)
if err != nil {
t.Fatal(err)
}

// Check that we can read the quotas, and that they are empty
quotas, err := adminClient.DescribeClientQuotas(nil, false)
if err != nil {
t.Fatal(err)
}
if len(quotas) != 0 {
t.Errorf("Expected quotas to be empty at start, found: %v", quotas)
}

// Put a quota on default user
// /config/users/<default>
component := QuotaEntityComponent{
EntityType: QuotaEntityUser,
MatchType: QuotaMatchDefault,
}
op := ClientQuotasOp{
Key: "producer_byte_rate",
Value: 1024,
}
err = adminClient.AlterClientQuotas([]QuotaEntityComponent{component}, op, false)
if err != nil {
t.Fatal(err)
}

// Check that we now have a quota entry
quotas, err = adminClient.DescribeClientQuotas(nil, false)
if err != nil {
t.Fatal(err)
}
if len(quotas) == 0 {
t.Error("Expected not empty quotas")
}
if len(quotas) > 1 {
t.Errorf("Expected one quota entry, found: %v", quotas)
}

// Put a quota on specific client-id for a specific user
// /config/users/<user>/clients/<client-id>
userEntityComponent := QuotaEntityComponent{
EntityType: QuotaEntityUser,
MatchType: QuotaMatchExact,
Name: "sarama",
}
clientEntityComponent := QuotaEntityComponent{
EntityType: QuotaEntityClientID,
MatchType: QuotaMatchExact,
Name: "sarama-consumer",
}
op = ClientQuotasOp{
Key: "consumer_byte_rate",
Value: 2048,
}

err = adminClient.AlterClientQuotas([]QuotaEntityComponent{userEntityComponent, clientEntityComponent}, op, false)
if err != nil {
t.Fatal(err)
}

// Check that we can query a specific quota entry
userFilterComponent := QuotaFilterComponent{
EntityType: QuotaEntityUser,
MatchType: QuotaMatchExact,
Match: "sarama",
}
clientFilterComponent := QuotaFilterComponent{
EntityType: QuotaEntityClientID,
MatchType: QuotaMatchExact,
Match: "sarama-consumer",
}
quotas, err = adminClient.DescribeClientQuotas([]QuotaFilterComponent{userFilterComponent, clientFilterComponent}, true)
if err != nil {
t.Fatal(err)
}
if len(quotas) == 0 {
t.Error("Expected not empty quotas")
}
if len(quotas) > 1 {
t.Errorf("Expected one quota entry, found: %v", quotas)
}
if quotas[0].Values[op.Key] != op.Value {
t.Errorf("Expected specific quota value to be %f, found: %v", op.Value, quotas[0].Values[op.Key])
}
}

0 comments on commit 581f074

Please sign in to comment.