diff --git a/tests/integration/sync_test.go b/tests/integration/sync_test.go index 5a143ead7..64efe81e3 100644 --- a/tests/integration/sync_test.go +++ b/tests/integration/sync_test.go @@ -1,4 +1,4 @@ -//go:build integration +//gos:build integration package integration @@ -4814,3 +4814,41 @@ func Test_Sync_DoNotUpdateCreatedAt(t *testing.T) { // plugins do not have an updated_at field // consumers do not have an updated_at field } + +// Test_Sync_LookupConsumerTags tests that existing behavior when referencing +// consumers from plugins is preserved: +// - if a referenced consumer is not present in the state file, the sync fails +// - if a referenced consumer is present in the state file, the sync succeeds +// +// This test also tests that the new behavior is implemented correctly: +// - if a referenced consumer is not present in the state file, but is present +// in Kong when using the new lookup selector tags, the sync succeeds +// - if a referenced consumer is not present in the state file and neither in +// Kong when using the new lookup selector tags, the sync fails +func Test_Sync_LookupConsumerTags(t *testing.T) { + runWhen(t, "enterprise", ">=3.0.0") + setup(t) + + // test that reference to non-existing consumer fails. + pluginsNoLookupStateFile := "testdata/sync/028-lookup-tags/plugins_no_lookup.yaml" + err := sync(pluginsNoLookupStateFile) + require.Error(t, err) + require.EqualError(t, err, "building state: consumer foo for plugin rate-limiting-advanced: entity not found") + + // test that reference to existing local consumer succeeds. + pluginsAndConsumersStateFile := "testdata/sync/028-lookup-tags/plugins_and_consumers.yaml" + require.NoError(t, sync(pluginsAndConsumersStateFile)) + reset(t) + + // test that reference to existing global consumer succeeds via lookup tags. + globalConsumersStateFile := "testdata/sync/028-lookup-tags/global_consumers.yaml" + require.NoError(t, sync(globalConsumersStateFile)) + // sync plugins with lookup reference to global consumers. + pluginsLookupStateFile := "testdata/sync/028-lookup-tags/plugins_lookup.yaml" + require.NoError(t, sync(pluginsLookupStateFile)) + reset(t) + + // test that reference to non-existing global consumer fails via lookup tags. + require.Error(t, sync(pluginsLookupStateFile)) + require.EqualError(t, err, "building state: consumer foo for plugin rate-limiting-advanced: entity not found") +} diff --git a/tests/integration/testdata/sync/028-lookup-tags/global_consumers.yaml b/tests/integration/testdata/sync/028-lookup-tags/global_consumers.yaml new file mode 100644 index 000000000..b5ef686fc --- /dev/null +++ b/tests/integration/testdata/sync/028-lookup-tags/global_consumers.yaml @@ -0,0 +1,6 @@ +_format_version: "3.0" +_info: + select_tags: + - global-consumers +consumers: +- username: foo \ No newline at end of file diff --git a/tests/integration/testdata/sync/028-lookup-tags/plugins_and_consumers.yaml b/tests/integration/testdata/sync/028-lookup-tags/plugins_and_consumers.yaml new file mode 100644 index 000000000..c352b3d61 --- /dev/null +++ b/tests/integration/testdata/sync/028-lookup-tags/plugins_and_consumers.yaml @@ -0,0 +1,13 @@ +_format_version: "3.0" +plugins: +- name: rate-limiting-advanced + config: + limit: + - 10 + window_size: + - 60 + namespace: foo + sync_rate: -1 + consumer: foo +consumers: +- username: foo \ No newline at end of file diff --git a/tests/integration/testdata/sync/028-lookup-tags/plugins_lookup.yaml b/tests/integration/testdata/sync/028-lookup-tags/plugins_lookup.yaml new file mode 100644 index 000000000..c31b4c55f --- /dev/null +++ b/tests/integration/testdata/sync/028-lookup-tags/plugins_lookup.yaml @@ -0,0 +1,17 @@ +_format_version: "3.0" +_info: + select_tags: + - managed-by-deck + lookup_tags: + consumers: + - global-consumers +plugins: +- name: rate-limiting-advanced + config: + limit: + - 10 + window_size: + - 60 + namespace: foo + sync_rate: -1 + consumer: foo \ No newline at end of file diff --git a/tests/integration/testdata/sync/028-lookup-tags/plugins_no_lookup.yaml b/tests/integration/testdata/sync/028-lookup-tags/plugins_no_lookup.yaml new file mode 100644 index 000000000..f0560594d --- /dev/null +++ b/tests/integration/testdata/sync/028-lookup-tags/plugins_no_lookup.yaml @@ -0,0 +1,14 @@ +_format_version: "3.0" +_info: + select_tags: + - managed-by-deck +plugins: +- name: rate-limiting-advanced + config: + limit: + - 10 + window_size: + - 60 + namespace: foo + sync_rate: -1 + consumer: foo \ No newline at end of file