Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ require (
github.com/fatih/color v1.19.0
github.com/google/go-cmp v0.7.0
github.com/kong/go-apiops v0.4.6
github.com/kong/go-database-reconciler v1.42.5
github.com/kong/go-kong v0.79.0
github.com/kong/go-database-reconciler v1.43.0
github.com/kong/go-kong v0.80.0
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kong/go-apiops v0.4.6 h1:sQkTievX92AT5PZNZcUFbm7inysBVeZIIagXN8MWnP8=
github.com/kong/go-apiops v0.4.6/go.mod h1:Xt99d90LallLVwYJAGaufiNbBdsK0KKboe7gR4Ryths=
github.com/kong/go-database-reconciler v1.42.5 h1:jmtUys4cOnq2Qzt1WBHMFrOjhwnfVqB0+F4LjQAVjuA=
github.com/kong/go-database-reconciler v1.42.5/go.mod h1:s+1bVmpIUBVpz0mPbrjb7LGBVF1KB/8s9/yAUSvzOnY=
github.com/kong/go-kong v0.79.0 h1:upDuGUNS2UbQMFR3g1wlcXw7VmpJp5jc33XHGpFfVTc=
github.com/kong/go-kong v0.79.0/go.mod h1:77mEn0XQ7maUS7HvF7qiqbNJEftcSAf0y7mKN1sA72U=
github.com/kong/go-database-reconciler v1.43.0 h1:rsSI06Q+XjMtP15HSVTjPFkgtnpL42L825JXHihev10=
github.com/kong/go-database-reconciler v1.43.0/go.mod h1:o09zCc/YNNgIq7VFBZSo8VWiuR5qyU+exZ365MKaZ1Y=
github.com/kong/go-kong v0.80.0 h1:2gnfi6xh9F4Bam99QFcsNGzR+i7xlHyeLZOq6bNO8Rs=
github.com/kong/go-kong v0.80.0/go.mod h1:77mEn0XQ7maUS7HvF7qiqbNJEftcSAf0y7mKN1sA72U=
github.com/kong/go-slugify v1.0.0 h1:vCFAyf2sdoSlBtLcrmDWUFn0ohlpKiKvQfXZkO5vSKY=
github.com/kong/go-slugify v1.0.0/go.mod h1:dbR2h3J2QKXQ1k0aww6cN7o4cIcwlWflr6RKRdcoaiw=
github.com/kong/kubernetes-configuration v1.5.3 h1:4Hh2ZAT5zjHHqx07VVMF5lIUZ7c2RAOUpIINpOsduZs=
Expand Down
169 changes: 169 additions & 0 deletions tests/integration/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1290,3 +1290,172 @@ func testApplyCustomPluginDefinitionsKonnectImpl(t *testing.T) {
})
}
}

func Test_Apply_Plugin_Expressions(t *testing.T) {
runWhen(t, "enterprise", ">=3.16.0")
setup(t)
ctx := context.Background()

client, err := getTestClient()
require.NoError(t, err)

kongFile := "testdata/sync/003-create-a-plugin/kong-expressions.yaml"
require.NoError(t, sync(ctx, kongFile))

require.NoError(t, sync(ctx, kongFile))

ignoreFields := []cmp.Option{
cmpopts.IgnoreFields(kong.Plugin{}, "Config"),
cmpopts.IgnoreFields(kong.Plugin{}, "Protocols"),
}

expectedStatePostSync := utils.KongRawState{
Plugins: []*kong.Plugin{
{
ID: kong.String("6f5c0bb0-8e5c-4d3e-9d5b-1c1f2f0a9a11"),
Name: kong.String("rate-limiting-advanced"),
Enabled: kong.Bool(true),
Expressions: kong.PluginExpressions{
"custom_key": nil,
"limit": []any{"5*10"},
},
},
{
ID: kong.String("8f2c4a6e-1b3d-4e5f-9a7b-0c1d2e3f4a5b"),
Name: kong.String("rate-limiting"),
Enabled: kong.Bool(true),
Expressions: kong.PluginExpressions{
"custom_key": nil,
"second": nil,
"minute": `consumer.username == "vip" ? 1000 : 10`,
"hour": nil,
"day": `consumer.username == "vip" ? 10000 : 1000`,
"month": nil,
"year": nil,
},
},
},
}

testKongState(t, client, false, false, expectedStatePostSync, ignoreFields)

updatedFile := "testdata/apply/015-plugin-expressions/updated.yaml"
require.NoError(t, apply(ctx, updatedFile))

expectedStatePostApply := utils.KongRawState{
Plugins: []*kong.Plugin{
{
ID: kong.String("6f5c0bb0-8e5c-4d3e-9d5b-1c1f2f0a9a11"),
Name: kong.String("rate-limiting-advanced"),
Enabled: kong.Bool(true),
Expressions: kong.PluginExpressions{
"custom_key": nil,
"limit": []any{"7*10"},
},
},
{
ID: kong.String("8f2c4a6e-1b3d-4e5f-9a7b-0c1d2e3f4a5b"),
Name: kong.String("rate-limiting"),
Enabled: kong.Bool(true),
Expressions: kong.PluginExpressions{
"custom_key": nil,
"second": nil,
"minute": `consumer.username == "vip" ? 20 : 5`,
"hour": nil,
"day": `consumer.username == "vip" ? 20000 : 5000`,
"month": nil,
"year": nil,
},
},
},
}

testKongState(t, client, false, false, expectedStatePostApply, ignoreFields)
}

func Test_Apply_Plugin_Expressions_Konnect(t *testing.T) {
runDualTestWithSkipDefaults(t, "Test_Apply_Plugin_Expressions_Konnect", testApplyPluginExpressionsKonnectImpl)
}

func testApplyPluginExpressionsKonnectImpl(t *testing.T) {
setDefaultKonnectControlPlane(t)
runWhenKonnect(t)
setup(t)
ctx := context.Background()

client, err := getTestClient()
require.NoError(t, err)

kongFile := "testdata/sync/003-create-a-plugin/kong-expressions.yaml"
require.NoError(t, sync(ctx, kongFile))

require.NoError(t, sync(ctx, kongFile))

ignoreFields := []cmp.Option{
cmpopts.IgnoreFields(kong.Plugin{}, "Config"),
cmpopts.IgnoreFields(kong.Plugin{}, "Protocols"),
}

expectedStatePostSync := utils.KongRawState{
Plugins: []*kong.Plugin{
{
ID: kong.String("6f5c0bb0-8e5c-4d3e-9d5b-1c1f2f0a9a11"),
Name: kong.String("rate-limiting-advanced"),
Enabled: kong.Bool(true),
Expressions: kong.PluginExpressions{
"custom_key": nil,
"limit": []any{"5*10"},
},
},
{
ID: kong.String("8f2c4a6e-1b3d-4e5f-9a7b-0c1d2e3f4a5b"),
Name: kong.String("rate-limiting"),
Enabled: kong.Bool(true),
Expressions: kong.PluginExpressions{
"custom_key": nil,
"second": nil,
"minute": `consumer.username == "vip" ? 1000 : 10`,
"hour": nil,
"day": `consumer.username == "vip" ? 10000 : 1000`,
"month": nil,
"year": nil,
},
},
},
}

testKongState(t, client, true, false, expectedStatePostSync, ignoreFields)

updatedFile := "testdata/apply/015-plugin-expressions/updated.yaml"
require.NoError(t, apply(ctx, updatedFile))

expectedStatePostApply := utils.KongRawState{
Plugins: []*kong.Plugin{
{
ID: kong.String("6f5c0bb0-8e5c-4d3e-9d5b-1c1f2f0a9a11"),
Name: kong.String("rate-limiting-advanced"),
Enabled: kong.Bool(true),
Expressions: kong.PluginExpressions{
"custom_key": nil,
"limit": []any{"7*10"},
},
},
{
ID: kong.String("8f2c4a6e-1b3d-4e5f-9a7b-0c1d2e3f4a5b"),
Name: kong.String("rate-limiting"),
Enabled: kong.Bool(true),
Expressions: kong.PluginExpressions{
"custom_key": nil,
"second": nil,
"minute": `consumer.username == "vip" ? 20 : 5`,
"hour": nil,
"day": `consumer.username == "vip" ? 20000 : 5000`,
"month": nil,
"year": nil,
},
},
},
}

testKongState(t, client, true, false, expectedStatePostApply, ignoreFields)
}
58 changes: 58 additions & 0 deletions tests/integration/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"github.com/acarl005/stripansi"
"github.com/kong/deck/sanitize"
"github.com/kong/go-database-reconciler/pkg/file"
"github.com/kong/go-kong/kong"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/yaml"
)

func Test_Dump_SelectTags_30(t *testing.T) {
Expand Down Expand Up @@ -1753,3 +1755,59 @@ func Test_Dump_CustomPluginDefinitions(t *testing.T) {
})
}
}

func Test_Dump_Plugin_Expressions(t *testing.T) {
runWhen(t, "enterprise", ">=3.16.0")
setup(t)

ctx := context.Background()
kongFile := "testdata/sync/003-create-a-plugin/kong-expressions.yaml"
require.NoError(t, sync(ctx, kongFile))

output, err := dump("-o", "-", "--with-id")
require.NoError(t, err)

expectedFile := "testdata/dump/016-plugin-expressions/expected.yaml"
expected, err := readFile(expectedFile)
require.NoError(t, err)
assert.Equal(t, expected, output)
}

func Test_Dump_Plugin_Expressions_Konnect(t *testing.T) {
runWhenKonnect(t)
setup(t)

ctx := context.Background()
kongFile := "testdata/sync/003-create-a-plugin/kong-expressions.yaml"
require.NoError(t, sync(ctx, kongFile))

output, err := dump("-o", "-", "--with-id")
require.NoError(t, err)

// the dumped file round-trips the plugins and their expressions
var dumped struct {
Plugins []file.FPlugin `json:"plugins"`
}
require.NoError(t, yaml.Unmarshal([]byte(output), &dumped))
byName := make(map[string]file.FPlugin, len(dumped.Plugins))
for _, plugin := range dumped.Plugins {
byName[*plugin.Name] = plugin
}
require.Len(t, byName, 2)

assert.Equal(t, kong.PluginExpressions{
"custom_key": nil,
"limit": []any{"5*10"},
}, byName["rate-limiting-advanced"].Expressions)

// unset scalar fields survive the round-trip as null
assert.Equal(t, kong.PluginExpressions{
"custom_key": nil,
"second": nil,
"minute": `consumer.username == "vip" ? 1000 : 10`,
"hour": nil,
"day": `consumer.username == "vip" ? 10000 : 1000`,
"month": nil,
"year": nil,
}, byName["rate-limiting"].Expressions)
}
125 changes: 125 additions & 0 deletions tests/integration/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13882,3 +13882,128 @@ func Test_Sync_EnvVar_Masking(t *testing.T) {
require.NotContains(t, output, "webhookSecretV2", "actual webhook secret should not be exposed")
})
}

func Test_Sync_Plugin_Expressions(t *testing.T) {
runWhen(t, "enterprise", ">=3.16.0")
setup(t)
ctx := context.Background()

client, err := getTestClient()
require.NoError(t, err)

kongFile := "testdata/sync/003-create-a-plugin/kong-expressions.yaml"
require.NoError(t, sync(ctx, kongFile))

// resync with no error
require.NoError(t, sync(ctx, kongFile))

ignoreFields := []cmp.Option{
cmpopts.IgnoreFields(kong.Plugin{}, "Config"),
cmpopts.IgnoreFields(kong.Plugin{}, "Protocols"),
}

expectedStatePostSync := utils.KongRawState{
Plugins: []*kong.Plugin{
{
ID: kong.String("6f5c0bb0-8e5c-4d3e-9d5b-1c1f2f0a9a11"),
Name: kong.String("rate-limiting-advanced"),
Enabled: kong.Bool(true),
Expressions: kong.PluginExpressions{
"custom_key": nil,
"limit": []any{"5*10"},
},
},
{
ID: kong.String("8f2c4a6e-1b3d-4e5f-9a7b-0c1d2e3f4a5b"),
Name: kong.String("rate-limiting"),
Enabled: kong.Bool(true),
Expressions: kong.PluginExpressions{
"custom_key": nil,
"second": nil,
"minute": `consumer.username == "vip" ? 1000 : 10`,
"hour": nil,
"day": `consumer.username == "vip" ? 10000 : 1000`,
"month": nil,
"year": nil,
},
},
},
}

testKongState(t, client, false, false, expectedStatePostSync, ignoreFields)

// static config values must survive alongside the expressions
plugins, err := client.Plugins.ListAll(ctx)
require.NoError(t, err)
byName := pluginsByName(plugins)
require.Contains(t, byName, "rate-limiting")
for field, want := range map[string]any{"minute": float64(10), "day": float64(1000)} {
assert.Equal(t, want, byName["rate-limiting"].Config[field],
"rate-limiting config %q", field)
}
}

func Test_Sync_Plugin_Expressions_Konnect(t *testing.T) {
runDualTestWithSkipDefaults(t, "Test_Sync_Plugin_Expressions_Konnect", testSyncPluginExpressionsKonnectImpl)
}

func testSyncPluginExpressionsKonnectImpl(t *testing.T) {
setDefaultKonnectControlPlane(t)
runWhenKonnect(t)
setup(t)
ctx := context.Background()

client, err := getTestClient()
require.NoError(t, err)

kongFile := "testdata/sync/003-create-a-plugin/kong-expressions.yaml"
require.NoError(t, sync(ctx, kongFile))

// resync with no error
require.NoError(t, sync(ctx, kongFile))

ignoreFields := []cmp.Option{
cmpopts.IgnoreFields(kong.Plugin{}, "Config"),
cmpopts.IgnoreFields(kong.Plugin{}, "Protocols"),
}

expectedStatePostSync := utils.KongRawState{
Plugins: []*kong.Plugin{
{
ID: kong.String("6f5c0bb0-8e5c-4d3e-9d5b-1c1f2f0a9a11"),
Name: kong.String("rate-limiting-advanced"),
Enabled: kong.Bool(true),
Expressions: kong.PluginExpressions{
"custom_key": nil,
"limit": []any{"5*10"},
},
},
{
ID: kong.String("8f2c4a6e-1b3d-4e5f-9a7b-0c1d2e3f4a5b"),
Name: kong.String("rate-limiting"),
Enabled: kong.Bool(true),
Expressions: kong.PluginExpressions{
"custom_key": nil,
"second": nil,
"minute": `consumer.username == "vip" ? 1000 : 10`,
"hour": nil,
"day": `consumer.username == "vip" ? 10000 : 1000`,
"month": nil,
"year": nil,
},
},
},
}

testKongState(t, client, true, false, expectedStatePostSync, ignoreFields)

// static config values must survive alongside the expressions
plugins, err := client.Plugins.ListAll(ctx)
require.NoError(t, err)
byName := pluginsByName(plugins)
require.Contains(t, byName, "rate-limiting")
for field, want := range map[string]any{"minute": float64(10), "day": float64(1000)} {
assert.Equal(t, want, byName["rate-limiting"].Config[field],
"rate-limiting config %q", field)
}
}
Loading
Loading