-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from gojek/listConsumerGroups
Add support for ListConsumerGroups for a topic closes #7
- Loading branch information
Showing
11 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,3 +99,6 @@ workflows: | |
- release: | ||
requires: | ||
- test | ||
filters: | ||
branches: | ||
only: master |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/gojek/kat/cmd/list" | ||
"github.com/gojek/kat/logger" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var consumerGroupCmd = &cobra.Command{ | ||
Use: "consumergroup", | ||
Short: "Admin commands on consumergroups", | ||
} | ||
|
||
func init() { | ||
consumerGroupCmd.PersistentFlags().StringP("broker-list", "b", "", "Comma separated list of broker ips") | ||
if err := consumerGroupCmd.MarkPersistentFlagRequired("broker-list"); err != nil { | ||
logger.Fatal(err) | ||
} | ||
|
||
consumerGroupCmd.PersistentFlags().StringP("topic", "t", "", "Specify topic") | ||
if err := consumerGroupCmd.MarkPersistentFlagRequired("topic"); err != nil { | ||
logger.Fatal(err) | ||
} | ||
|
||
consumerGroupCmd.AddCommand(list.ListConsumerGroupsCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package list | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
|
||
"github.com/gojek/kat/cmd/base" | ||
"github.com/gojek/kat/logger" | ||
"github.com/gojek/kat/pkg/client" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type consumerGroupAdmin struct { | ||
saramaClient client.ConsumerLister | ||
} | ||
|
||
var ListConsumerGroupsCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "Lists the consumer groups", | ||
Run: func(command *cobra.Command, args []string) { | ||
cobraUtil := base.NewCobraUtil(command) | ||
|
||
addr := strings.Split(cobraUtil.GetStringArg("broker-list"), ",") | ||
|
||
cgl := consumerGroupAdmin{ | ||
saramaClient: client.NewSaramaClient(addr), | ||
} | ||
err := cgl.ListGroups(cobraUtil.GetStringArg("topic")) | ||
if err != nil { | ||
logger.Fatalf("Error while listing consumer groups for %v topic", err) | ||
} | ||
}, | ||
} | ||
|
||
func (c *consumerGroupAdmin) ListGroups(topic string) error { | ||
consumerGroupsMap, err := c.saramaClient.ListConsumerGroups() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
consumerGroupList := make([]string, len(consumerGroupsMap)) | ||
for group := range consumerGroupsMap { | ||
consumerGroupList = append(consumerGroupList, group) | ||
} | ||
|
||
sort.Slice(consumerGroupList, func(i int, j int) bool { | ||
return consumerGroupList[i] < consumerGroupList[j] | ||
}) | ||
|
||
var consumerGroups []string | ||
|
||
for consumerGroupID := range consumerGroupsMap { | ||
consumerGroups = append(consumerGroups, consumerGroupID) | ||
} | ||
|
||
_, err = c.saramaClient.GetConsumerGroupsForTopic(consumerGroups, topic) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package list | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type mockConsumerListener struct{ mock.Mock } | ||
|
||
func (m *mockConsumerListener) ListConsumerGroups() (map[string]string, error) { | ||
args := m.Called() | ||
return args.Get(0).(map[string]string), args.Error(1) | ||
} | ||
|
||
func (m *mockConsumerListener) GetConsumerGroupsForTopic(consumerGroups []string, topic string) (chan string, error) { | ||
args := m.Called(consumerGroups, topic) | ||
return args.Get(0).(chan string), args.Error(1) | ||
} | ||
|
||
func TestListGroupsReturnsSuccess(t *testing.T) { | ||
mockConsumer := new(mockConsumerListener) | ||
admin := consumerGroupAdmin{mockConsumer} | ||
mockChannel := make(chan string, 0) | ||
|
||
consumerGroupsMap := map[string]string{"consumer1": "", "consumer2": ""} | ||
mockConsumer.On("ListConsumerGroups").Return(consumerGroupsMap, nil) | ||
|
||
mockConsumer.On("GetConsumerGroupsForTopic", []string{"consumer1", "consumer2"}, "").Return(mockChannel, nil) | ||
|
||
err := admin.ListGroups("") | ||
|
||
require.NoError(t, err) | ||
mockConsumer.AssertExpectations(t) | ||
} | ||
|
||
func TestListGroupsReturnsFailureIfListConsumerGroupsFails(t *testing.T) { | ||
mockConsumer := new(mockConsumerListener) | ||
admin := consumerGroupAdmin{mockConsumer} | ||
multipleConsumers := map[string]string{"consumer1": "", "consumer2": ""} | ||
mockConsumer.On("ListConsumerGroups").Return(multipleConsumers, errors.New("list consumer groups failed")) | ||
|
||
err := admin.ListGroups("") | ||
|
||
require.Error(t, err) | ||
assert.Equal(t, "list consumer groups failed", err.Error()) | ||
mockConsumer.AssertExpectations(t) | ||
} | ||
|
||
func TestListGroupsReturnsFailureIfGetConsumerGroupsFails(t *testing.T) { | ||
mockConsumer := new(mockConsumerListener) | ||
admin := consumerGroupAdmin{mockConsumer} | ||
mockChannel := make(chan string, 0) | ||
|
||
consumerGroupsMap := map[string]string{"consumer1": "", "consumer2": ""} | ||
mockConsumer.On("ListConsumerGroups").Return(consumerGroupsMap, nil) | ||
|
||
mockConsumer.On("GetConsumerGroupsForTopic", []string{"consumer1", "consumer2"}, "").Return(mockChannel, errors.New("get consumer groups failed")) | ||
|
||
err := admin.ListGroups("") | ||
require.Error(t, err) | ||
assert.Equal(t, "get consumer groups failed", err.Error()) | ||
mockConsumer.AssertExpectations(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package client | ||
|
||
type ConsumerLister interface { | ||
ListConsumerGroups() (map[string]string, error) | ||
GetConsumerGroupsForTopic([]string, string) (chan string, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters