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
175 changes: 175 additions & 0 deletions tests/integration/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,178 @@ func Test_Apply_KonnectWorkspace(t *testing.T) {
})
}
}

// test scope:
// - enterprise >=3.14.0
func Test_Apply_Plugin_Conditional(t *testing.T) {
runWhen(t, "enterprise", ">=3.14.0")
setup(t)
ctx := context.Background()

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

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

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

expectedStatePostSync := utils.KongRawState{
Plugins: []*kong.Plugin{
{
ID: kong.String("efead952-0a1d-43ec-9794-0ac6abdc7f55"),
Name: kong.String("key-auth"),
Config: kong.Configuration{
"anonymous": nil,
"hide_credentials": bool(true),
"identity_realms": []any{map[string]any{"id": nil, "region": nil, "scope": string("cp")}},
"key_in_body": bool(false),
"key_in_header": bool(true),
"key_in_query": bool(true),
"key_names": []any{string("apikey")},
"realm": nil,
"run_on_preflight": bool(true),
},
Enabled: kong.Bool(true),
Protocols: []*string{
kong.String("grpc"),
kong.String("grpcs"),
kong.String("http"),
kong.String("https"),
kong.String("ws"),
kong.String("wss"),
},
Condition: kong.String("route.name != \"health\""),
},
},
}

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

// apply command to change condition
updatedFile := "testdata/apply/012-plugin-conditional/updated.yaml"
require.NoError(t, apply(ctx, updatedFile))

expectedStatePostApply := utils.KongRawState{
Plugins: []*kong.Plugin{
{
ID: kong.String("efead952-0a1d-43ec-9794-0ac6abdc7f55"),
Name: kong.String("key-auth"),
Config: kong.Configuration{
"anonymous": nil,
"hide_credentials": bool(true),
"identity_realms": []any{map[string]any{"id": nil, "region": nil, "scope": string("cp")}},
"key_in_body": bool(false),
"key_in_header": bool(true),
"key_in_query": bool(true),
"key_names": []any{string("apikey")},
"realm": nil,
"run_on_preflight": bool(true),
},
Enabled: kong.Bool(true),
Protocols: []*string{
kong.String("grpc"),
kong.String("grpcs"),
kong.String("http"),
kong.String("https"),
kong.String("ws"),
kong.String("wss"),
},
Condition: kong.String("service.name != \"healthcheck-service\""),
},
},
}

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

// test scope:
// - konnect
func Test_Apply_Plugin_Conditional_Konnect(t *testing.T) {
runDualTestWithSkipDefaults(t, "Test_Apply_Plugin_Conditional_Konnect", testApplyPluginConditionalKonnectImpl)
}

func testApplyPluginConditionalKonnectImpl(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-conditional.yaml"
require.NoError(t, sync(ctx, kongFile))

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

expectedStatePostSync := utils.KongRawState{
Plugins: []*kong.Plugin{
{
ID: kong.String("efead952-0a1d-43ec-9794-0ac6abdc7f55"),
Name: kong.String("key-auth"),
Config: kong.Configuration{
"anonymous": nil,
"hide_credentials": bool(true),
"identity_realms": []any{map[string]any{"id": nil, "region": nil, "scope": string("cp")}},
"key_in_body": bool(false),
"key_in_header": bool(true),
"key_in_query": bool(true),
"key_names": []any{string("apikey")},
"realm": nil,
"run_on_preflight": bool(true),
},
Enabled: kong.Bool(true),
Protocols: []*string{
kong.String("grpc"),
kong.String("grpcs"),
kong.String("http"),
kong.String("https"),
kong.String("ws"),
kong.String("wss"),
},
Condition: kong.String("route.name != \"health\""),
},
},
}

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

// apply command to change condition
updatedFile := "testdata/apply/012-plugin-conditional/updated.yaml"
require.NoError(t, apply(ctx, updatedFile))

expectedStatePostApply := utils.KongRawState{
Plugins: []*kong.Plugin{
{
ID: kong.String("efead952-0a1d-43ec-9794-0ac6abdc7f55"),
Name: kong.String("key-auth"),
Config: kong.Configuration{
"anonymous": nil,
"hide_credentials": bool(true),
"identity_realms": []any{map[string]any{"id": nil, "region": nil, "scope": string("cp")}},
"key_in_body": bool(false),
"key_in_header": bool(true),
"key_in_query": bool(true),
"key_names": []any{string("apikey")},
"realm": nil,
"run_on_preflight": bool(true),
},
Enabled: kong.Bool(true),
Protocols: []*string{
kong.String("grpc"),
kong.String("grpcs"),
kong.String("http"),
kong.String("https"),
kong.String("ws"),
kong.String("wss"),
},
Condition: kong.String("service.name != \"healthcheck-service\""),
},
},
}

testKongState(t, client, true, false, expectedStatePostApply, nil)
}
34 changes: 34 additions & 0 deletions tests/integration/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1352,3 +1352,37 @@ func Test_Dump_KonnectWorkspace_AllWorkspaces(t *testing.T) {
assert.Contains(t, string(defaultOutput), "_workspace: default",
"default.yaml should contain _workspace: default")
}

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

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

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

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

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

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

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

expectedFile := "testdata/dump/012-plugin-conditional/expected-konnect.yaml"
expected, err := readFile(expectedFile)
require.NoError(t, err)
assert.Equal(t, expected, output)
}
103 changes: 103 additions & 0 deletions tests/integration/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11882,3 +11882,106 @@ func testSyncPluginsNestedForeignKeysExternalEntitiesKonnectImpl(t *testing.T) {
})
}
}

// test scope:
// - enterprise >=3.14.0
func Test_Sync_Plugin_Conditional(t *testing.T) {
runWhen(t, "enterprise", ">=3.14.0")
setup(t)
ctx := context.Background()

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

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

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

expectedStatePostSync := utils.KongRawState{
Plugins: []*kong.Plugin{
{
ID: kong.String("efead952-0a1d-43ec-9794-0ac6abdc7f55"),
Name: kong.String("key-auth"),
Config: kong.Configuration{
"anonymous": nil,
"hide_credentials": bool(true),
"identity_realms": []any{map[string]any{"id": nil, "region": nil, "scope": string("cp")}},
"key_in_body": bool(false),
"key_in_header": bool(true),
"key_in_query": bool(true),
"key_names": []any{string("apikey")},
"realm": nil,
"run_on_preflight": bool(true),
},
Enabled: kong.Bool(true),
Protocols: []*string{
kong.String("grpc"),
kong.String("grpcs"),
kong.String("http"),
kong.String("https"),
kong.String("ws"),
kong.String("wss"),
},
Condition: kong.String("route.name != \"health\""),
},
},
}

testKongState(t, client, false, false, expectedStatePostSync, nil)
}

// test scope:
// - konnect
func Test_Sync_Plugin_Conditional_Konnect(t *testing.T) {
runDualTestWithSkipDefaults(t, "Test_Sync_Plugin_Conditional_Konnect", testSyncPluginConditionalKonnectImpl)
}

func testSyncPluginConditionalKonnectImpl(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-conditional.yaml"
require.NoError(t, sync(ctx, kongFile))

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

expectedStatePostSync := utils.KongRawState{
Plugins: []*kong.Plugin{
{
ID: kong.String("efead952-0a1d-43ec-9794-0ac6abdc7f55"),
Name: kong.String("key-auth"),
Config: kong.Configuration{
"anonymous": nil,
"hide_credentials": bool(true),
"identity_realms": []any{map[string]any{"id": nil, "region": nil, "scope": string("cp")}},
"key_in_body": bool(false),
"key_in_header": bool(true),
"key_in_query": bool(true),
"key_names": []any{string("apikey")},
"realm": nil,
"run_on_preflight": bool(true),
},
Enabled: kong.Bool(true),
Protocols: []*string{
kong.String("grpc"),
kong.String("grpcs"),
kong.String("http"),
kong.String("https"),
kong.String("ws"),
kong.String("wss"),
},
Condition: kong.String("route.name != \"health\""),
},
},
}

testKongState(t, client, true, false, expectedStatePostSync, nil)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
_format_version: "3.0"
plugins:
- id: efead952-0a1d-43ec-9794-0ac6abdc7f55
name: key-auth
condition: service.name != "healthcheck-service"
config:
hide_credentials: true
key_names: [apikey]
enabled: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
_format_version: "3.0"
_konnect:
control_plane_name: default
plugins:
- condition: route.name != "health"
config:
anonymous: null
hide_credentials: true
identity_realms:
- id: null
region: null
scope: cp
key_in_body: false
key_in_header: true
key_in_query: true
key_names:
- apikey
realm: null
run_on_preflight: true
enabled: true
id: efead952-0a1d-43ec-9794-0ac6abdc7f55
name: key-auth
protocols:
- grpc
- grpcs
- http
- https
- ws
- wss
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
_format_version: "3.0"
plugins:
- condition: route.name != "health"
config:
anonymous: null
hide_credentials: true
identity_realms:
- id: null
region: null
scope: cp
key_in_body: false
key_in_header: true
key_in_query: true
key_names:
- apikey
realm: null
run_on_preflight: true
enabled: true
id: efead952-0a1d-43ec-9794-0ac6abdc7f55
name: key-auth
protocols:
- grpc
- grpcs
- http
- https
- ws
- wss
Loading
Loading