diff --git a/pkg/state/consumer_group_consumers.go b/pkg/state/consumer_group_consumers.go index 12b6108..317565a 100644 --- a/pkg/state/consumer_group_consumers.go +++ b/pkg/state/consumer_group_consumers.go @@ -151,12 +151,21 @@ func getConsumerGroupConsumer(txn *memdb.Txn, consumerGroupID string, IDs ...str for _, id := range IDs { for _, index := range indexes { - res, err := txn.First(consumerGroupConsumerTableName, index, id) + res, err := txn.Get(consumerGroupConsumerTableName, index, id) if err != nil { return nil, err } - if res != nil { - consumer := res.(*ConsumerGroupConsumer) + + for { + resultValue := res.Next() + if resultValue == nil { + break + } + consumer, ok := resultValue.(*ConsumerGroupConsumer) + if !ok { + break + } + if *consumer.ConsumerGroup.ID == consumerGroupID { return consumer, nil } diff --git a/tests/integration/sync_test.go b/tests/integration/sync_test.go index 3269e2b..17960f1 100644 --- a/tests/integration/sync_test.go +++ b/tests/integration/sync_test.go @@ -5686,3 +5686,42 @@ func Test_Sync_PluginAutoFields(t *testing.T) { }) }) } + +// test scope: +// - 3.x +func Test_Sync_MoreThanOneConsumerGroupForOneConsumer(t *testing.T) { + client, err := getTestClient() + require.NoError(t, err) + + expectedState := utils.KongRawState{ + ConsumerGroups: []*kong.ConsumerGroupObject{ + { + ConsumerGroup: &kong.ConsumerGroup{ + Name: kong.String("group1"), + }, + Consumers: []*kong.Consumer{ + { + Username: kong.String("my-test-consumer"), + }, + }, + }, + { + ConsumerGroup: &kong.ConsumerGroup{ + Name: kong.String("group2"), + }, + Consumers: []*kong.Consumer{ + { + Username: kong.String("my-test-consumer"), + }, + }, + }, + }, + Consumers: []*kong.Consumer{ + { + Username: kong.String("my-test-consumer"), + }, + }, + } + require.NoError(t, sync("testdata/sync/xxx-more-than-one-consumer-group-with-a-consumer/kong.yaml")) + testKongState(t, client, false, expectedState, nil) +} diff --git a/tests/integration/test_utils.go b/tests/integration/test_utils.go index 8cddbad..b128789 100644 --- a/tests/integration/test_utils.go +++ b/tests/integration/test_utils.go @@ -228,6 +228,7 @@ func testKongState(t *testing.T, client *kong.Client, isKonnect bool, cmpopts.IgnoreFields(kong.Vault{}, "ID", "CreatedAt", "UpdatedAt"), cmpopts.IgnoreFields(kong.Certificate{}, "ID", "CreatedAt"), cmpopts.IgnoreFields(kong.SNI{}, "ID", "CreatedAt"), + cmpopts.IgnoreFields(kong.Consumer{}, "CreatedAt", "ID"), cmpopts.IgnoreFields(kong.ConsumerGroup{}, "CreatedAt", "ID"), cmpopts.IgnoreFields(kong.ConsumerGroupPlugin{}, "CreatedAt", "ID"), cmpopts.IgnoreFields(kong.KeyAuth{}, "ID", "CreatedAt"), diff --git a/tests/integration/testdata/sync/xxx-more-than-one-consumer-group-with-a-consumer/kong.yaml b/tests/integration/testdata/sync/xxx-more-than-one-consumer-group-with-a-consumer/kong.yaml new file mode 100644 index 0000000..e5be41f --- /dev/null +++ b/tests/integration/testdata/sync/xxx-more-than-one-consumer-group-with-a-consumer/kong.yaml @@ -0,0 +1,9 @@ +_format_version: "3.0" +consumer_groups: +- name: group1 +- name: group2 +consumers: +- username: my-test-consumer + groups: + - name: group1 + - name: group2