1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150 |
- package sarama
- import (
- "errors"
- "strings"
- "testing"
- )
- func TestClusterAdmin(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminInvalidController(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- _, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err == nil {
- t.Fatal(errors.New("controller not set still cluster admin was created"))
- }
- if err != ErrControllerNotAvailable {
- t.Fatal(err)
- }
- }
- func TestClusterAdminCreateTopic(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "CreateTopicsRequest": NewMockCreateTopicsResponse(t),
- })
- config := NewConfig()
- config.Version = V0_10_2_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.CreateTopic("my_topic", &TopicDetail{NumPartitions: 1, ReplicationFactor: 1}, false)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminCreateTopicWithInvalidTopicDetail(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "CreateTopicsRequest": NewMockCreateTopicsResponse(t),
- })
- config := NewConfig()
- config.Version = V0_10_2_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.CreateTopic("my_topic", nil, false)
- if err.Error() != "you must specify topic details" {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminCreateTopicWithoutAuthorization(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "CreateTopicsRequest": NewMockCreateTopicsResponse(t),
- })
- config := NewConfig()
- config.Version = V0_11_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.CreateTopic("_internal_topic", &TopicDetail{NumPartitions: 1, ReplicationFactor: 1}, false)
- want := "insufficient permissions to create topic with reserved prefix"
- if !strings.HasSuffix(err.Error(), want) {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminListTopics(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
- SetLeader("my_topic", 0, seedBroker.BrokerID()),
- "DescribeConfigsRequest": NewMockDescribeConfigsResponse(t),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- entries, err := admin.ListTopics()
- if err != nil {
- t.Fatal(err)
- }
- if len(entries) <= 0 {
- t.Fatal(errors.New("no resource present"))
- }
- topic, found := entries["my_topic"]
- if !found {
- t.Fatal(errors.New("topic not found in response"))
- }
- _, found = topic.ConfigEntries["max.message.bytes"]
- if found {
- t.Fatal(errors.New("default topic config entry incorrectly found in response"))
- }
- value := topic.ConfigEntries["retention.ms"]
- if value == nil || *value != "5000" {
- t.Fatal(errors.New("non-default topic config entry not found in response"))
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- if topic.ReplicaAssignment == nil || topic.ReplicaAssignment[0][0] != 1 {
- t.Fatal(errors.New("replica assignment not found in response"))
- }
- }
- func TestClusterAdminDeleteTopic(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "DeleteTopicsRequest": NewMockDeleteTopicsResponse(t),
- })
- config := NewConfig()
- config.Version = V0_10_2_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.DeleteTopic("my_topic")
- if err != nil {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminDeleteEmptyTopic(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "DeleteTopicsRequest": NewMockDeleteTopicsResponse(t),
- })
- config := NewConfig()
- config.Version = V0_10_2_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.DeleteTopic("")
- if err != ErrInvalidTopic {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminCreatePartitions(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "CreatePartitionsRequest": NewMockCreatePartitionsResponse(t),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.CreatePartitions("my_topic", 3, nil, false)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminCreatePartitionsWithDiffVersion(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "CreatePartitionsRequest": NewMockCreatePartitionsResponse(t),
- })
- config := NewConfig()
- config.Version = V0_10_2_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.CreatePartitions("my_topic", 3, nil, false)
- if err != ErrUnsupportedVersion {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminCreatePartitionsWithoutAuthorization(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "CreatePartitionsRequest": NewMockCreatePartitionsResponse(t),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.CreatePartitions("_internal_topic", 3, nil, false)
- want := "insufficient permissions to create partition on topic with reserved prefix"
- if !strings.HasSuffix(err.Error(), want) {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminDeleteRecords(t *testing.T) {
- topicName := "my_topic"
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
- SetLeader(topicName, 1, 1).
- SetLeader(topicName, 2, 1).
- SetLeader(topicName, 3, 1),
- "DeleteRecordsRequest": NewMockDeleteRecordsResponse(t),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- partitionOffsetFake := make(map[int32]int64)
- partitionOffsetFake[4] = 1000
- errFake := admin.DeleteRecords(topicName, partitionOffsetFake)
- if errFake == nil {
- t.Fatal(err)
- }
- partitionOffset := make(map[int32]int64)
- partitionOffset[1] = 1000
- partitionOffset[2] = 1000
- partitionOffset[3] = 1000
- err = admin.DeleteRecords(topicName, partitionOffset)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminDeleteRecordsWithInCorrectBroker(t *testing.T) {
- topicName := "my_topic"
- seedBroker := NewMockBroker(t, 1)
- secondBroker := NewMockBroker(t, 2)
- defer seedBroker.Close()
- defer secondBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
- SetBroker(secondBroker.Addr(), secondBroker.brokerID).
- SetLeader(topicName, 1, 1).
- SetLeader(topicName, 2, 1).
- SetLeader(topicName, 3, 2),
- "DeleteRecordsRequest": NewMockDeleteRecordsResponse(t),
- })
- secondBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
- SetBroker(secondBroker.Addr(), secondBroker.brokerID).
- SetLeader(topicName, 1, 1).
- SetLeader(topicName, 2, 1).
- SetLeader(topicName, 3, 2),
- "DeleteRecordsRequest": NewMockDeleteRecordsResponse(t),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- partitionOffset := make(map[int32]int64)
- partitionOffset[1] = 1000
- partitionOffset[2] = 1000
- partitionOffset[3] = 1000
- err = admin.DeleteRecords(topicName, partitionOffset)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminDeleteRecordsWithDiffVersion(t *testing.T) {
- topicName := "my_topic"
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
- SetLeader(topicName, 1, 1).
- SetLeader(topicName, 2, 1).
- SetLeader(topicName, 3, 1),
- "DeleteRecordsRequest": NewMockDeleteRecordsResponse(t),
- })
- config := NewConfig()
- config.Version = V0_10_2_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- partitionOffset := make(map[int32]int64)
- partitionOffset[1] = 1000
- partitionOffset[2] = 1000
- partitionOffset[3] = 1000
- err = admin.DeleteRecords(topicName, partitionOffset)
- if !strings.HasPrefix(err.Error(), "kafka server: failed to delete records") {
- t.Fatal(err)
- }
- deleteRecordsError, ok := err.(ErrDeleteRecords)
- if !ok {
- t.Fatal(err)
- }
- for _, err := range *deleteRecordsError.Errors {
- if err != ErrUnsupportedVersion {
- t.Fatal(err)
- }
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminDescribeConfig(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "DescribeConfigsRequest": NewMockDescribeConfigsResponse(t),
- })
- 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"},
- }
- entries, err := admin.DescribeConfig(resource)
- if err != nil {
- t.Fatal(err)
- }
- history := seedBroker.History()
- describeReq, ok := history[len(history)-1].Request.(*DescribeConfigsRequest)
- if !ok {
- t.Fatal("failed to find DescribeConfigsRequest in mockBroker history")
- }
- 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) {
- controllerBroker := NewMockBroker(t, 1)
- defer controllerBroker.Close()
- configBroker := NewMockBroker(t, 2)
- defer configBroker.Close()
- controllerBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(controllerBroker.BrokerID()).
- SetBroker(controllerBroker.Addr(), controllerBroker.BrokerID()).
- SetBroker(configBroker.Addr(), configBroker.BrokerID()),
- })
- configBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(controllerBroker.BrokerID()).
- SetBroker(controllerBroker.Addr(), controllerBroker.BrokerID()).
- SetBroker(configBroker.Addr(), configBroker.BrokerID()),
- "DescribeConfigsRequest": NewMockDescribeConfigsResponse(t),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin(
- []string{
- controllerBroker.Addr(),
- configBroker.Addr(),
- }, config)
- if err != nil {
- t.Fatal(err)
- }
- for _, resourceType := range []ConfigResourceType{BrokerResource, BrokerLoggerResource} {
- resource := ConfigResource{Name: "2", Type: resourceType}
- entries, err := admin.DescribeConfig(resource)
- if err != nil {
- t.Fatal(err)
- }
- if len(entries) <= 0 {
- t.Fatal(errors.New("no resource present"))
- }
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminAlterConfig(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "AlterConfigsRequest": NewMockAlterConfigsResponse(t),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- var value string
- entries := make(map[string]*string)
- value = "60000"
- entries["retention.ms"] = &value
- err = admin.AlterConfig(TopicResource, "my_topic", entries, false)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminAlterBrokerConfig(t *testing.T) {
- controllerBroker := NewMockBroker(t, 1)
- defer controllerBroker.Close()
- configBroker := NewMockBroker(t, 2)
- defer configBroker.Close()
- controllerBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(controllerBroker.BrokerID()).
- SetBroker(controllerBroker.Addr(), controllerBroker.BrokerID()).
- SetBroker(configBroker.Addr(), configBroker.BrokerID()),
- })
- configBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(controllerBroker.BrokerID()).
- SetBroker(controllerBroker.Addr(), controllerBroker.BrokerID()).
- SetBroker(configBroker.Addr(), configBroker.BrokerID()),
- "AlterConfigsRequest": NewMockAlterConfigsResponse(t),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin(
- []string{
- controllerBroker.Addr(),
- configBroker.Addr(),
- }, config)
- if err != nil {
- t.Fatal(err)
- }
- var value string
- entries := make(map[string]*string)
- value = "3"
- entries["min.insync.replicas"] = &value
- for _, resourceType := range []ConfigResourceType{BrokerResource, BrokerLoggerResource} {
- resource := ConfigResource{Name: "2", Type: resourceType}
- err = admin.AlterConfig(
- resource.Type,
- resource.Name,
- entries,
- false)
- if err != nil {
- t.Fatal(err)
- }
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminCreateAcl(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "CreateAclsRequest": NewMockCreateAclsResponse(t),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- r := Resource{ResourceType: AclResourceTopic, ResourceName: "my_topic"}
- a := Acl{Host: "localhost", Operation: AclOperationAlter, PermissionType: AclPermissionAny}
- err = admin.CreateACL(r, a)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminListAcls(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "DescribeAclsRequest": NewMockListAclsResponse(t),
- "CreateAclsRequest": NewMockCreateAclsResponse(t),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- r := Resource{ResourceType: AclResourceTopic, ResourceName: "my_topic"}
- a := Acl{Host: "localhost", Operation: AclOperationAlter, PermissionType: AclPermissionAny}
- err = admin.CreateACL(r, a)
- if err != nil {
- t.Fatal(err)
- }
- resourceName := "my_topic"
- filter := AclFilter{
- ResourceType: AclResourceTopic,
- Operation: AclOperationRead,
- ResourceName: &resourceName,
- }
- rAcls, err := admin.ListAcls(filter)
- if err != nil {
- t.Fatal(err)
- }
- if len(rAcls) <= 0 {
- t.Fatal("no acls present")
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestClusterAdminDeleteAcl(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "DeleteAclsRequest": NewMockDeleteAclsResponse(t),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- resourceName := "my_topic"
- filter := AclFilter{
- ResourceType: AclResourceTopic,
- Operation: AclOperationAlter,
- ResourceName: &resourceName,
- }
- _, err = admin.DeleteACL(filter, false)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestDescribeTopic(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetLeader("my_topic", 0, seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- topics, err := admin.DescribeTopics([]string{"my_topic"})
- if err != nil {
- t.Fatal(err)
- }
- if len(topics) != 1 {
- t.Fatalf("Expected 1 result, got %v", len(topics))
- }
- if topics[0].Name != "my_topic" {
- t.Fatalf("Incorrect topic name: %v", topics[0].Name)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestDescribeTopicWithVersion0_11(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetLeader("my_topic", 0, seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- })
- config := NewConfig()
- config.Version = V0_11_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- topics, err := admin.DescribeTopics([]string{"my_topic"})
- if err != nil {
- t.Fatal(err)
- }
- if len(topics) != 1 {
- t.Fatalf("Expected 1 result, got %v", len(topics))
- }
- if topics[0].Name != "my_topic" {
- t.Fatalf("Incorrect topic name: %v", topics[0].Name)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestDescribeConsumerGroup(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- expectedGroupID := "my-group"
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "DescribeGroupsRequest": NewMockDescribeGroupsResponse(t).AddGroupDescription(expectedGroupID, &GroupDescription{
- GroupId: expectedGroupID,
- }),
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "FindCoordinatorRequest": NewMockFindCoordinatorResponse(t).SetCoordinator(CoordinatorGroup, expectedGroupID, seedBroker),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- result, err := admin.DescribeConsumerGroups([]string{expectedGroupID})
- if err != nil {
- t.Fatal(err)
- }
- if len(result) != 1 {
- t.Fatalf("Expected 1 result, got %v", len(result))
- }
- if result[0].GroupId != expectedGroupID {
- t.Fatalf("Expected groupID %v, got %v", expectedGroupID, result[0].GroupId)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestListConsumerGroups(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "ListGroupsRequest": NewMockListGroupsResponse(t).
- AddGroup("my-group", "consumer"),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- groups, err := admin.ListConsumerGroups()
- if err != nil {
- t.Fatal(err)
- }
- if len(groups) != 1 {
- t.Fatalf("Expected %v results, got %v", 1, len(groups))
- }
- protocolType, ok := groups["my-group"]
- if !ok {
- t.Fatal("Expected group to be returned, but it did not")
- }
- if protocolType != "consumer" {
- t.Fatalf("Expected protocolType %v, got %v", "consumer", protocolType)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestListConsumerGroupsMultiBroker(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- secondBroker := NewMockBroker(t, 2)
- defer secondBroker.Close()
- firstGroup := "first"
- secondGroup := "second"
- nonExistingGroup := "non-existing-group"
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
- SetBroker(secondBroker.Addr(), secondBroker.BrokerID()),
- "ListGroupsRequest": NewMockListGroupsResponse(t).
- AddGroup(firstGroup, "consumer"),
- })
- secondBroker.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
- SetBroker(secondBroker.Addr(), secondBroker.BrokerID()),
- "ListGroupsRequest": NewMockListGroupsResponse(t).
- AddGroup(secondGroup, "consumer"),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- groups, err := admin.ListConsumerGroups()
- if err != nil {
- t.Fatal(err)
- }
- if len(groups) != 2 {
- t.Fatalf("Expected %v results, got %v", 1, len(groups))
- }
- if _, found := groups[firstGroup]; !found {
- t.Fatalf("Expected group %v to be present in result set, but it isn't", firstGroup)
- }
- if _, found := groups[secondGroup]; !found {
- t.Fatalf("Expected group %v to be present in result set, but it isn't", secondGroup)
- }
- if _, found := groups[nonExistingGroup]; found {
- t.Fatalf("Expected group %v to not exist, but it exists", nonExistingGroup)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestListConsumerGroupOffsets(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- group := "my-group"
- topic := "my-topic"
- partition := int32(0)
- expectedOffset := int64(0)
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- "OffsetFetchRequest": NewMockOffsetFetchResponse(t).SetOffset(group, "my-topic", partition, expectedOffset, "", ErrNoError).SetError(ErrNoError),
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "FindCoordinatorRequest": NewMockFindCoordinatorResponse(t).SetCoordinator(CoordinatorGroup, group, seedBroker),
- })
- config := NewConfig()
- config.Version = V1_0_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- response, err := admin.ListConsumerGroupOffsets(group, map[string][]int32{
- topic: {0},
- })
- if err != nil {
- t.Fatalf("ListConsumerGroupOffsets failed with error %v", err)
- }
- block := response.GetBlock(topic, partition)
- if block == nil {
- t.Fatalf("Expected block for topic %v and partition %v to exist, but it doesn't", topic, partition)
- }
- if block.Offset != expectedOffset {
- t.Fatalf("Expected offset %v, got %v", expectedOffset, block.Offset)
- }
- err = admin.Close()
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestDeleteConsumerGroup(t *testing.T) {
- seedBroker := NewMockBroker(t, 1)
- defer seedBroker.Close()
- group := "my-group"
- seedBroker.SetHandlerByMap(map[string]MockResponse{
- // "OffsetFetchRequest": NewMockOffsetFetchResponse(t).SetOffset(group, "my-topic", partition, expectedOffset, "", ErrNoError),
- "DeleteGroupsRequest": NewMockDeleteGroupsRequest(t).SetDeletedGroups([]string{group}),
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker.BrokerID()).
- SetBroker(seedBroker.Addr(), seedBroker.BrokerID()),
- "FindCoordinatorRequest": NewMockFindCoordinatorResponse(t).SetCoordinator(CoordinatorGroup, group, seedBroker),
- })
- config := NewConfig()
- config.Version = V1_1_0_0
- admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- err = admin.DeleteConsumerGroup(group)
- if err != nil {
- t.Fatalf("DeleteConsumerGroup failed with error %v", err)
- }
- }
- // TestRefreshMetaDataWithDifferentController ensures that the cached
- // controller can be forcibly updated from Metadata by the admin client
- func TestRefreshMetaDataWithDifferentController(t *testing.T) {
- seedBroker1 := NewMockBroker(t, 1)
- seedBroker2 := NewMockBroker(t, 2)
- defer seedBroker1.Close()
- defer seedBroker2.Close()
- seedBroker1.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker1.BrokerID()).
- SetBroker(seedBroker1.Addr(), seedBroker1.BrokerID()).
- SetBroker(seedBroker2.Addr(), seedBroker2.BrokerID()),
- })
- config := NewConfig()
- config.Version = V1_1_0_0
- client, err := NewClient([]string{seedBroker1.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- ca := clusterAdmin{client: client, conf: config}
- if b, _ := ca.Controller(); seedBroker1.BrokerID() != b.ID() {
- t.Fatalf("expected cached controller to be %d rather than %d",
- seedBroker1.BrokerID(), b.ID())
- }
- seedBroker1.SetHandlerByMap(map[string]MockResponse{
- "MetadataRequest": NewMockMetadataResponse(t).
- SetController(seedBroker2.BrokerID()).
- SetBroker(seedBroker1.Addr(), seedBroker1.BrokerID()).
- SetBroker(seedBroker2.Addr(), seedBroker2.BrokerID()),
- })
- if b, _ := ca.refreshController(); seedBroker2.BrokerID() != b.ID() {
- t.Fatalf("expected refreshed controller to be %d rather than %d",
- seedBroker2.BrokerID(), b.ID())
- }
- if b, _ := ca.Controller(); seedBroker2.BrokerID() != b.ID() {
- t.Fatalf("expected cached controller to be %d rather than %d",
- seedBroker2.BrokerID(), b.ID())
- }
- }
|