Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set DescribeConfigRequest Version field #1576

Merged
merged 2 commits into from
Jan 16, 2020
Merged
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
9 changes: 8 additions & 1 deletion admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ func dependsOnSpecificNode(resource ConfigResource) bool {
}

func (ca *clusterAdmin) DescribeConfig(resource ConfigResource) ([]ConfigEntry, error) {

var entries []ConfigEntry
var resources []*ConfigResource
resources = append(resources, &resource)
Expand All @@ -461,6 +460,14 @@ func (ca *clusterAdmin) DescribeConfig(resource ConfigResource) ([]ConfigEntry,
Resources: resources,
}

if ca.conf.Version.IsAtLeast(V1_1_0_0) {
request.Version = 1
}

if ca.conf.Version.IsAtLeast(V2_0_0_0) {
request.Version = 2
}

var (
b *Broker
err error
Expand Down
73 changes: 49 additions & 24 deletions admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package sarama

import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"testing"
)
Expand Down Expand Up @@ -492,35 +488,64 @@ func TestClusterAdminDescribeConfig(t *testing.T) {
"DescribeConfigsRequest": NewMockDescribeConfigsResponse(t),
})

config := NewConfig()
config.Version = V1_0_0_0
admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
if err != nil {
t.Fatal(err)
}
var tests = []struct {
saramaVersion KafkaVersion
requestVersion int16
includeSynonyms bool
}{
{V1_0_0_0, 0, false},
{V1_1_0_0, 1, true},
{V1_1_1_0, 1, true},
{V2_0_0_0, 2, true},
}
for _, tt := range tests {
config := NewConfig()
config.Version = tt.saramaVersion
admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
if err != nil {
t.Fatal(err)
}
defer func() {
_ = admin.Close()
}()

resource := ConfigResource{
Name: "r1",
Type: TopicResource,
ConfigNames: []string{"my_topic"},
}

resource := ConfigResource{Name: "r1", Type: TopicResource, ConfigNames: []string{"my_topic"}}
entries, err := admin.DescribeConfig(resource)
if err != nil {
t.Fatal(err)
}
entries, err := admin.DescribeConfig(resource)
if err != nil {
t.Fatal(err)
}

if len(entries) <= 0 {
t.Fatal(errors.New("no resource present"))
}
history := seedBroker.History()
describeReq, ok := history[len(history)-1].Request.(*DescribeConfigsRequest)
if !ok {
t.Fatal("failed to find DescribeConfigsRequest in mockBroker history")
}

err = admin.Close()
if err != nil {
t.Fatal(err)
if describeReq.Version != tt.requestVersion {
t.Fatalf(
"requestVersion %v did not match expected %v",
describeReq.Version, tt.requestVersion)
}

if len(entries) <= 0 {
t.Fatal(errors.New("no resource present"))
}
if tt.includeSynonyms {
if len(entries[0].Synonyms) == 0 {
t.Fatal("expected synonyms to have been included")
}
}
}
}

// TestClusterAdminDescribeBrokerConfig ensures that a describe broker config
// is sent to the broker in the resource struct, _not_ the controller
func TestClusterAdminDescribeBrokerConfig(t *testing.T) {
Logger = log.New(os.Stdout, fmt.Sprintf("[%s] ", t.Name()), log.LstdFlags)
defer func() { Logger = log.New(ioutil.Discard, "[Sarama] ", log.LstdFlags) }()

controllerBroker := NewMockBroker(t, 1)
defer controllerBroker.Close()
configBroker := NewMockBroker(t, 2)
Expand Down
59 changes: 41 additions & 18 deletions mockresponses.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,11 @@ func NewMockDescribeConfigsResponse(t TestReporter) *MockDescribeConfigsResponse

func (mr *MockDescribeConfigsResponse) For(reqBody versionedDecoder) encoder {
req := reqBody.(*DescribeConfigsRequest)
res := &DescribeConfigsResponse{}
res := &DescribeConfigsResponse{
Version: req.Version,
}

includeSynonyms := (req.Version > 0)

for _, r := range req.Resources {
var configEntries []*ConfigEntry
Expand Down Expand Up @@ -763,23 +767,42 @@ func (mr *MockDescribeConfigsResponse) For(reqBody versionedDecoder) encoder {
Configs: configEntries,
})
case TopicResource:
configEntries = append(configEntries,
&ConfigEntry{Name: "max.message.bytes",
Value: "1000000",
ReadOnly: false,
Default: true,
Sensitive: false,
}, &ConfigEntry{Name: "retention.ms",
Value: "5000",
ReadOnly: false,
Default: false,
Sensitive: false,
}, &ConfigEntry{Name: "password",
Value: "12345",
ReadOnly: false,
Default: false,
Sensitive: true,
})
maxMessageBytes := &ConfigEntry{Name: "max.message.bytes",
Value: "1000000",
ReadOnly: false,
Default: true,
Sensitive: false,
}
if includeSynonyms {
maxMessageBytes.Synonyms = []*ConfigSynonym{
{
ConfigName: "max.message.bytes",
ConfigValue: "500000",
},
}
}
retentionMs := &ConfigEntry{Name: "retention.ms",
Value: "5000",
ReadOnly: false,
Default: false,
Sensitive: false,
}
if includeSynonyms {
retentionMs.Synonyms = []*ConfigSynonym{
{
ConfigName: "log.retention.ms",
ConfigValue: "2500",
},
}
}
password := &ConfigEntry{Name: "password",
Value: "12345",
ReadOnly: false,
Default: false,
Sensitive: true,
}
configEntries = append(
configEntries, maxMessageBytes, retentionMs, password)
res.Resources = append(res.Resources, &ResourceResponse{
Name: r.Name,
Configs: configEntries,
Expand Down