Skip to content

Commit

Permalink
refactor: refactored tests and code to add all konnect validate usecases
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashansa-K committed Oct 10, 2024
1 parent b05d593 commit dff8245
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 32 deletions.
14 changes: 9 additions & 5 deletions cmd/gateway_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ func executeValidate(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
var kongClient *kong.Client
if validateOnline {
if validateCmdRBACResourcesOnly && mode == modeKonnect {
return fmt.Errorf("rbac validation not yet supported in konnect mode")
}

kongClient, err = getKongClient(ctx, targetContent, mode)
if err != nil {
return err
Expand Down Expand Up @@ -144,10 +140,18 @@ func executeValidate(cmd *cobra.Command, _ []string) error {
return err
}

if validateKonnectCompatibility {
if validateKonnectCompatibility || mode == modeKonnect {
if errs := validate.KonnectCompatibility(targetContent); len(errs) != 0 {
return validate.ErrorsWrapper{Errors: errs}
}

if validateCmdRBACResourcesOnly {
return fmt.Errorf("[rbac] not yet supported by konnect")
}

if validateWorkspace != "" {
return fmt.Errorf("[workspaces] not supported by Konnect - use control planes instead")
}
}

if validateOnline {
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/testdata/validate/konnect_1_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
_format_version: "1.1"
_konnect:
control_plane_name: default
services:
- connect_timeout: 60000
id: 58076db2-28b6-423b-ba39-a797193017f7
host: mockbin.org
name: svc1
port: 80
protocol: http
read_timeout: 60000
retries: 5
routes:
- name: r1
id: 87b6a97e-f3f7-4c47-857a-7464cb9e202b
https_redirect_status_code: 301
paths:
- /r1
16 changes: 16 additions & 0 deletions tests/integration/testdata/validate/konnect_invalid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
_format_version: "3.0"
services:
- connect_timeout: 60000
id: 58076db2-28b6-423b-ba39-a797193017f7
host: mockbin.org
name: svc1
port: 80
protocol: http
read_timeout: 60000
retries: 5
routes:
- name: r1
id: 87b6a97e-f3f7-4c47-857a-7464cb9e202b
https_redirect_status_code: 301
paths:
- /r1
17 changes: 17 additions & 0 deletions tests/integration/testdata/validate/konnect_no_version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
_konnect:
control_plane_name: default
services:
- connect_timeout: 60000
id: 58076db2-28b6-423b-ba39-a797193017f7
host: mockbin.org
name: svc1
port: 80
protocol: http
read_timeout: 60000
retries: 5
routes:
- name: r1
id: 87b6a97e-f3f7-4c47-857a-7464cb9e202b
https_redirect_status_code: 301
paths:
- /r1
2 changes: 2 additions & 0 deletions tests/integration/testdata/validate/rbac-resources.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
_format_version: "3.0"
_konnect:
control_plane_name: default
rbac_roles:
- comment: Full access to Dev Portal related endpoints in the workspace
endpoint_permissions:
Expand Down
69 changes: 42 additions & 27 deletions tests/integration/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,54 @@ func Test_Validate_Konnect(t *testing.T) {
stateFile string
additionalArgs []string
errorExpected bool
errorString string
}{
{
name: "validate with konnect",
stateFile: "testdata/validate/konnect.yaml",
additionalArgs: []string{},
errorExpected: false,
},
{
name: "validate with --konnect-compatibility",
stateFile: "testdata/validate/konnect.yaml",
additionalArgs: []string{"--konnect-compatibility"},
errorExpected: false,
},
{
name: "validate with 1.1 version file",
stateFile: "testdata/validate/konnect_1_1.yaml",
additionalArgs: []string{},
errorExpected: true,
errorString: "[version] decK file version must be '3.0' or greater",
},
{
name: "validate with no version in deck file",
stateFile: "testdata/validate/konnect_no_version.yaml",
additionalArgs: []string{},
errorExpected: true,
errorString: "[version] unable to determine decK file version",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
validateOpts := []string{
tc.stateFile,
}
validateOpts = append(validateOpts, tc.additionalArgs...)

err := validate(true, validateOpts...)
assert.NoError(t, err)
})
}
}

func Test_Validate_Konnect_RBAC(t *testing.T) {
setup(t)
runWhen(t, "konnect", "")

tests := []struct {
name string
stateFile string
additionalArgs []string
errorExpected bool
}{
{
name: "validate with --rbac-resources-only",
stateFile: "testdata/validate/rbac-resources.yaml",
additionalArgs: []string{"--rbac-resources-only"},
errorExpected: true,
errorString: "[rbac] not yet supported by konnect",
},
{
name: "validate with workspaces set",
stateFile: "testdata/validate/konnect.yaml",
additionalArgs: []string{"--workspace=default"},
errorExpected: true,
errorString: "[workspaces] not supported by Konnect - use control planes instead",
},
{
name: "validate with no konnect config in file",
stateFile: "testdata/validate/konnect_invalid.yaml",
additionalArgs: []string{},
errorExpected: true,
errorString: "[konnect] section not specified - ensure details are set via cli flags",
},
}

Expand All @@ -68,8 +76,15 @@ func Test_Validate_Konnect_RBAC(t *testing.T) {
validateOpts = append(validateOpts, tc.additionalArgs...)

err := validate(true, validateOpts...)
assert.Error(t, err)
assert.Contains(t, err.Error(), "rbac validation not yet supported in konnect mode")

if tc.errorExpected {
assert.Error(t, err)
if tc.errorString != "" {
assert.Contains(t, err.Error(), tc.errorString)
}
} else {
assert.NoError(t, err)
}
})
}
}

0 comments on commit dff8245

Please sign in to comment.