Skip to content

Commit

Permalink
fix: plugin shown as global despite a consumer-group present (#134)
Browse files Browse the repository at this point in the history
* fix: plugin shown as global despite a consumer-group present

Prior to this fix, deck was reporting the plugins as global
even if a consumer-group was attached to them. This was only
a output problem, not a association problem. The fix
checks if a consumer-group exists or not before reporting
any plugin as global.

Fixes: Kong/deck#1005

* chore: moved to table-driven tests for plugin console function
  • Loading branch information
Prashansa-K authored Aug 27, 2024
1 parent 6d1bb06 commit 64dd801
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/state/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ func (p1 *Plugin) Identifier() string {
func (p1 *Plugin) Console() string {
res := *p1.Name + " "

if p1.Service == nil && p1.Route == nil && p1.Consumer == nil {
if p1.Service == nil && p1.Route == nil && p1.Consumer == nil && p1.ConsumerGroup == nil {
return res + "(global)"
}
associations := []string{}
Expand Down
63 changes: 63 additions & 0 deletions pkg/state/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,66 @@ func TestDeepEqualWithSorting(t *testing.T) {
t.Errorf("expected maps to be equal, but they are not")
}
}

func TestPluginConsole(t *testing.T) {
tests := []struct {
plugin kong.Plugin
name string
expected string
}{
{
name: "plugin default case",
plugin: kong.Plugin{},
expected: "foo-plugin (global)",
},
{
name: "plugin associated with service",
plugin: kong.Plugin{
Service: &kong.Service{ID: kong.String("bar")},
},
expected: "foo-plugin for service bar",
},
{
name: "plugin associated with route",
plugin: kong.Plugin{
Route: &kong.Route{ID: kong.String("baz")},
},
expected: "foo-plugin for route baz",
},
{
name: "plugin associated with consumer",
plugin: kong.Plugin{
Consumer: &kong.Consumer{ID: kong.String("demo")},
},
expected: "foo-plugin for consumer demo",
},
{
name: "plugin associated with consumer group",
plugin: kong.Plugin{
ConsumerGroup: &kong.ConsumerGroup{ID: kong.String("demo-group")},
},
expected: "foo-plugin for consumer-group demo-group",
},
{
name: "plugin associated with >1 entities",
plugin: kong.Plugin{
Service: &kong.Service{ID: kong.String("bar")},
Route: &kong.Route{ID: kong.String("baz")},
Consumer: &kong.Consumer{ID: kong.String("demo")},
ConsumerGroup: &kong.ConsumerGroup{ID: kong.String("demo-group")},
},
expected: "foo-plugin for service bar and route baz and consumer demo and consumer-group demo-group",
},
}
for _, tt := range tests {
var p1 Plugin
p1.Plugin = tt.plugin
p1.ID = kong.String("foo")
p1.Name = kong.String("foo-plugin")

t.Run(tt.name, func(t *testing.T) {
actual := p1.Console()
assert.Equal(t, tt.expected, actual)
})
}
}

0 comments on commit 64dd801

Please sign in to comment.