From 24b109daa930f7bdce781ec937a53603ced4685c Mon Sep 17 00:00:00 2001 From: Gabriele Gerbino Date: Mon, 22 Jan 2024 14:23:58 +0100 Subject: [PATCH] fix: consumer-group scope doesn't work with multiple nesting --- pkg/file/writer.go | 4 +-- tests/integration/sync_test.go | 36 +++++++++++++++++++ .../029-plugin-scoped-to-cg-route/kong.yaml | 34 ++++++++++-------- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/pkg/file/writer.go b/pkg/file/writer.go index 2fd66da..a0c25d1 100644 --- a/pkg/file/writer.go +++ b/pkg/file/writer.go @@ -319,7 +319,7 @@ func fetchService(id string, kongState *state.KongState, config WriteConfig) (*F route := &FRoute{Route: r.Route} for _, p := range plugins { p := p - if p.Service != nil || p.Consumer != nil { + if p.Service != nil || p.Consumer != nil || p.ConsumerGroup != nil { continue } p.Route = nil @@ -588,7 +588,7 @@ func populateConsumers(kongState *state.KongState, file *Content, } for _, p := range plugins { p := p - if p.Service != nil || p.Route != nil { + if p.Service != nil || p.Route != nil || p.ConsumerGroup != nil { continue } utils.ZeroOutID(p, p.Name, config.WithID) diff --git a/tests/integration/sync_test.go b/tests/integration/sync_test.go index e007ebd..669c4c8 100644 --- a/tests/integration/sync_test.go +++ b/tests/integration/sync_test.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "net/http" + "os" "testing" "time" @@ -4822,6 +4823,22 @@ func Test_Sync_PluginScopedToConsumerGroupAndRoute(t *testing.T) { Username: kong.String("foo"), }, }, + Services: []*kong.Service{ + { + ID: kong.String("1b9d6d8e-9f0f-4a1a-8d5c-9d2a6b2b7f3c"), + Host: kong.String("example.com"), + Name: kong.String("s1"), + ConnectTimeout: kong.Int(60000), + Port: kong.Int(80), + Path: nil, + Protocol: kong.String("http"), + ReadTimeout: kong.Int(60000), + Retries: kong.Int(5), + WriteTimeout: kong.Int(60000), + Tags: nil, + Enabled: kong.Bool(true), + }, + }, Routes: []*kong.Route{ { Name: kong.String("r1"), @@ -4835,6 +4852,9 @@ func Test_Sync_PluginScopedToConsumerGroupAndRoute(t *testing.T) { HTTPSRedirectStatusCode: kong.Int(426), RequestBuffering: kong.Bool(true), ResponseBuffering: kong.Bool(true), + Service: &kong.Service{ + ID: kong.String("1b9d6d8e-9f0f-4a1a-8d5c-9d2a6b2b7f3c"), + }, }, }, Plugins: []*kong.Plugin{ @@ -4895,4 +4915,20 @@ func Test_Sync_PluginScopedToConsumerGroupAndRoute(t *testing.T) { } require.NoError(t, sync("testdata/sync/029-plugin-scoped-to-cg-route/kong.yaml")) testKongState(t, client, false, expectedState, nil) + + // create a temporary file to dump the state. + cwd, err := os.Getwd() + require.NoError(t, err) + file, err := os.CreateTemp(cwd, "dump.*.yaml") + assert.NoError(t, err) + + // dump the state. + _, err = dump("-o", file.Name(), "--yes") + assert.NoError(t, err) + + // verify that the dumped state can be sync'd back and that + // the end result is the same. + require.NoError(t, sync(file.Name())) + testKongState(t, client, false, expectedState, nil) + } diff --git a/tests/integration/testdata/sync/029-plugin-scoped-to-cg-route/kong.yaml b/tests/integration/testdata/sync/029-plugin-scoped-to-cg-route/kong.yaml index 6dcb5ad..2e3a783 100644 --- a/tests/integration/testdata/sync/029-plugin-scoped-to-cg-route/kong.yaml +++ b/tests/integration/testdata/sync/029-plugin-scoped-to-cg-route/kong.yaml @@ -60,18 +60,22 @@ plugins: - http - https route: r1 -routes: -- hosts: - - 10.* - https_redirect_status_code: 426 - id: a9730e9e-df7e-4042-8bc7-e8b99af70171 - name: r1 - path_handling: v0 - preserve_host: false - protocols: - - http - - https - regex_priority: 0 - request_buffering: true - response_buffering: true - strip_path: true +services: +- host: example.com + id: 1b9d6d8e-9f0f-4a1a-8d5c-9d2a6b2b7f3c + name: s1 + routes: + - hosts: + - 10.* + https_redirect_status_code: 426 + id: a9730e9e-df7e-4042-8bc7-e8b99af70171 + name: r1 + path_handling: v0 + preserve_host: false + protocols: + - http + - https + regex_priority: 0 + request_buffering: true + response_buffering: true + strip_path: true