diff --git a/tests/integration/apply_test.go b/tests/integration/apply_test.go new file mode 100644 index 000000000..6d06d8c9a --- /dev/null +++ b/tests/integration/apply_test.go @@ -0,0 +1,62 @@ +//go:build integration + +package integration + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_Apply_3x(t *testing.T) { + // setup stage + + tests := []struct { + name string + firstFile string + secondFile string + expectedState string + }{ + { + name: "applies multiple of the same entity", + firstFile: "testdata/apply/001-same-type/service-01.yaml", + secondFile: "testdata/apply/001-same-type/service-02.yaml", + expectedState: "testdata/apply/001-same-type/expected-state.yaml", + }, + { + name: "applies different entity types", + firstFile: "testdata/apply/002-different-types/service-01.yaml", + secondFile: "testdata/apply/002-different-types/plugin-01.yaml", + expectedState: "testdata/apply/002-different-types/expected-state.yaml", + }, + { + name: "accepts consumer foreign keys", + firstFile: "testdata/apply/003-foreign-keys-consumers/consumer-01.yaml", + secondFile: "testdata/apply/003-foreign-keys-consumers/plugin-01.yaml", + expectedState: "testdata/apply/003-foreign-keys-consumers/expected-state.yaml", + }, + { + name: "accepts consumer group foreign keys", + firstFile: "testdata/apply/004-foreign-keys-consumer-groups/consumer-group-01.yaml", + secondFile: "testdata/apply/004-foreign-keys-consumer-groups/consumer-01.yaml", + expectedState: "testdata/apply/004-foreign-keys-consumer-groups/expected-state.yaml", + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + runWhen(t, "kong", ">=3.0.0") + setup(t) + apply(tc.firstFile) + apply(tc.secondFile) + + out, _ := dump() + + expected, err := readFile(tc.expectedState) + if err != nil { + t.Fatalf("failed to read expected state: %v", err) + } + + assert.Equal(t, expected, out) + }) + } +} diff --git a/tests/integration/test_utils.go b/tests/integration/test_utils.go index 41a23b40e..7cedc5bce 100644 --- a/tests/integration/test_utils.go +++ b/tests/integration/test_utils.go @@ -266,6 +266,16 @@ func setup(t *testing.T) { }) } +func apply(kongFile string, opts ...string) error { + deckCmd := cmd.NewRootCmd() + args := []string{"gateway", "apply", kongFile} + if len(opts) > 0 { + args = append(args, opts...) + } + deckCmd.SetArgs(args) + return deckCmd.ExecuteContext(context.Background()) +} + func sync(kongFile string, opts ...string) error { deckCmd := cmd.NewRootCmd() args := []string{"sync", "-s", kongFile} @@ -300,7 +310,7 @@ func diff(kongFile string, opts ...string) (string, error) { func dump(opts ...string) (string, error) { deckCmd := cmd.NewRootCmd() - args := []string{"dump"} + args := []string{"gateway", "dump"} if len(opts) > 0 { args = append(args, opts...) } diff --git a/tests/integration/testdata/apply/001-same-type/expected-state.yaml b/tests/integration/testdata/apply/001-same-type/expected-state.yaml new file mode 100644 index 000000000..ad11045d2 --- /dev/null +++ b/tests/integration/testdata/apply/001-same-type/expected-state.yaml @@ -0,0 +1,22 @@ +_format_version: "3.0" +services: +- connect_timeout: 60000 + enabled: true + host: httpbin.org + name: mock1 + path: /anything + port: 80 + protocol: http + read_timeout: 60000 + retries: 5 + write_timeout: 60000 +- connect_timeout: 60000 + enabled: true + host: httpbin.org + name: mock2 + path: /anything + port: 80 + protocol: http + read_timeout: 60000 + retries: 5 + write_timeout: 60000 diff --git a/tests/integration/testdata/apply/001-same-type/service-01.yaml b/tests/integration/testdata/apply/001-same-type/service-01.yaml new file mode 100644 index 000000000..e1adfd2e8 --- /dev/null +++ b/tests/integration/testdata/apply/001-same-type/service-01.yaml @@ -0,0 +1,12 @@ +_format_version: "3.0" +services: + - connect_timeout: 60000 + enabled: true + host: httpbin.org + name: mock1 + path: /anything + port: 80 + protocol: http + read_timeout: 60000 + retries: 5 + write_timeout: 60000 diff --git a/tests/integration/testdata/apply/001-same-type/service-02.yaml b/tests/integration/testdata/apply/001-same-type/service-02.yaml new file mode 100644 index 000000000..e90105139 --- /dev/null +++ b/tests/integration/testdata/apply/001-same-type/service-02.yaml @@ -0,0 +1,12 @@ +_format_version: "3.0" +services: + - connect_timeout: 60000 + enabled: true + host: httpbin.org + name: mock2 + path: /anything + port: 80 + protocol: http + read_timeout: 60000 + retries: 5 + write_timeout: 60000 diff --git a/tests/integration/testdata/apply/002-different-types/expected-state.yaml b/tests/integration/testdata/apply/002-different-types/expected-state.yaml new file mode 100644 index 000000000..e1542af59 --- /dev/null +++ b/tests/integration/testdata/apply/002-different-types/expected-state.yaml @@ -0,0 +1,27 @@ +_format_version: "3.0" +plugins: +- config: + body: null + content_type: null + echo: false + message: null + status_code: 200 + trigger: null + enabled: true + name: request-termination + protocols: + - grpc + - grpcs + - http + - https +services: +- connect_timeout: 60000 + enabled: true + host: httpbin.org + name: mock1 + path: /anything + port: 80 + protocol: http + read_timeout: 60000 + retries: 5 + write_timeout: 60000 diff --git a/tests/integration/testdata/apply/002-different-types/plugin-01.yaml b/tests/integration/testdata/apply/002-different-types/plugin-01.yaml new file mode 100644 index 000000000..4fa00c681 --- /dev/null +++ b/tests/integration/testdata/apply/002-different-types/plugin-01.yaml @@ -0,0 +1,16 @@ +_format_version: "3.0" +plugins: +- name: request-termination + config: + body: null + content_type: null + echo: false + message: null + status_code: 200 + trigger: null + enabled: true + protocols: + - grpc + - grpcs + - http + - https diff --git a/tests/integration/testdata/apply/002-different-types/service-01.yaml b/tests/integration/testdata/apply/002-different-types/service-01.yaml new file mode 100644 index 000000000..e1adfd2e8 --- /dev/null +++ b/tests/integration/testdata/apply/002-different-types/service-01.yaml @@ -0,0 +1,12 @@ +_format_version: "3.0" +services: + - connect_timeout: 60000 + enabled: true + host: httpbin.org + name: mock1 + path: /anything + port: 80 + protocol: http + read_timeout: 60000 + retries: 5 + write_timeout: 60000 diff --git a/tests/integration/testdata/apply/003-foreign-keys-consumers/consumer-01.yaml b/tests/integration/testdata/apply/003-foreign-keys-consumers/consumer-01.yaml new file mode 100644 index 000000000..72f1982d2 --- /dev/null +++ b/tests/integration/testdata/apply/003-foreign-keys-consumers/consumer-01.yaml @@ -0,0 +1,3 @@ +_format_version: "3.0" +consumers: + - username: alice diff --git a/tests/integration/testdata/apply/003-foreign-keys-consumers/expected-state.yaml b/tests/integration/testdata/apply/003-foreign-keys-consumers/expected-state.yaml new file mode 100644 index 000000000..e1d58c23c --- /dev/null +++ b/tests/integration/testdata/apply/003-foreign-keys-consumers/expected-state.yaml @@ -0,0 +1,16 @@ +_format_version: "3.0" +consumers: +- plugins: + - config: + body: null + content_type: null + echo: false + message: null + status_code: 404 + trigger: null + enabled: true + name: request-termination + protocols: + - http + - https + username: alice diff --git a/tests/integration/testdata/apply/003-foreign-keys-consumers/plugin-01.yaml b/tests/integration/testdata/apply/003-foreign-keys-consumers/plugin-01.yaml new file mode 100644 index 000000000..3159b3c63 --- /dev/null +++ b/tests/integration/testdata/apply/003-foreign-keys-consumers/plugin-01.yaml @@ -0,0 +1,15 @@ +_format_version: "3.0" +plugins: + - name: request-termination + enabled: true + consumer: alice + config: + status_code: 404 + body: null + content_type: null + echo: false + message: null + trigger: null + protocols: + - http + - https diff --git a/tests/integration/testdata/apply/004-foreign-keys-consumer-groups/consumer-01.yaml b/tests/integration/testdata/apply/004-foreign-keys-consumer-groups/consumer-01.yaml new file mode 100644 index 000000000..3a34eec0b --- /dev/null +++ b/tests/integration/testdata/apply/004-foreign-keys-consumer-groups/consumer-01.yaml @@ -0,0 +1,5 @@ +_format_version: "3.0" +consumers: + - username: alice + groups: + - name: gold diff --git a/tests/integration/testdata/apply/004-foreign-keys-consumer-groups/consumer-group-01.yaml b/tests/integration/testdata/apply/004-foreign-keys-consumer-groups/consumer-group-01.yaml new file mode 100644 index 000000000..45ad17e1c --- /dev/null +++ b/tests/integration/testdata/apply/004-foreign-keys-consumer-groups/consumer-group-01.yaml @@ -0,0 +1,3 @@ +_format_version: "3.0" +consumer_groups: + - name: gold diff --git a/tests/integration/testdata/apply/004-foreign-keys-consumer-groups/expected-state.yaml b/tests/integration/testdata/apply/004-foreign-keys-consumer-groups/expected-state.yaml new file mode 100644 index 000000000..45f5d4e5b --- /dev/null +++ b/tests/integration/testdata/apply/004-foreign-keys-consumer-groups/expected-state.yaml @@ -0,0 +1,7 @@ +_format_version: "3.0" +consumer_groups: +- name: gold +consumers: +- groups: + - name: gold + username: alice