From dcd3654b4ebb1be62427f867dd0d9c36bc7ebd04 Mon Sep 17 00:00:00 2001 From: Fernando Cainelli Date: Wed, 17 Jul 2024 14:56:11 -0300 Subject: [PATCH] test: add unknown field test cases Signed-off-by: Fernando Cainelli --- internal/pkg/parser/testcase_test.go | 6 +++++- internal/pkg/parser/testdata/invalid_test.yml | 16 ++++++++++++++++ internal/pkg/parser/testdata/invalid_vs.yml | 18 ++++++++++++++++++ internal/pkg/parser/virtualservice_test.go | 5 +++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 internal/pkg/parser/testdata/invalid_test.yml create mode 100644 internal/pkg/parser/testdata/invalid_vs.yml diff --git a/internal/pkg/parser/testcase_test.go b/internal/pkg/parser/testcase_test.go index db7f0aae..21119d53 100644 --- a/internal/pkg/parser/testcase_test.go +++ b/internal/pkg/parser/testcase_test.go @@ -7,6 +7,11 @@ import ( "github.com/stretchr/testify/require" ) +func TestUnknownField(t *testing.T) { + _, err := ParseTestCases([]string{"testdata/invalid_test.yml"}, true) + require.ErrorContains(t, err, "json: unknown field") +} + func TestParseTestCases(t *testing.T) { expectedTestCases := []*TestCase{ {Description: "happy path users"}, @@ -27,7 +32,6 @@ func TestParseTestCases(t *testing.T) { if !testPass { t.Errorf("could not find expected description:'%v'", expected.Description) } - } } diff --git a/internal/pkg/parser/testdata/invalid_test.yml b/internal/pkg/parser/testdata/invalid_test.yml new file mode 100644 index 00000000..5a0e62bb --- /dev/null +++ b/internal/pkg/parser/testdata/invalid_test.yml @@ -0,0 +1,16 @@ +testCases: + - description: a test + unknownField: true # not a valid field + wantMatch: true + request: + authority: + - www.example.com + method: + - GET + uri: + - /users + route: + - destination: + host: users.users.svc.cluster.local + port: + number: 80 diff --git a/internal/pkg/parser/testdata/invalid_vs.yml b/internal/pkg/parser/testdata/invalid_vs.yml new file mode 100644 index 00000000..b4090abc --- /dev/null +++ b/internal/pkg/parser/testdata/invalid_vs.yml @@ -0,0 +1,18 @@ +apiVersion: networking.istio.io/v1 +kind: VirtualService +metadata: + name: example + namespace: example +spec: + unknownField: true # not a valid field + hosts: + - www.example.com + http: + - match: + - uri: + regex: /users(/.*)? + route: + - destination: + host: users.users.svc.cluster.local + port: + number: 80 diff --git a/internal/pkg/parser/virtualservice_test.go b/internal/pkg/parser/virtualservice_test.go index cf0933b1..cd757df8 100644 --- a/internal/pkg/parser/virtualservice_test.go +++ b/internal/pkg/parser/virtualservice_test.go @@ -42,3 +42,8 @@ func TestParseMultipleVirtualServices(t *testing.T) { } } } + +func TestVirtualServiceUnknownFields(t *testing.T) { + _, err := ParseVirtualServices([]string{}, true) + require.ErrorContains(t, err, "json: unknown field") +}