-
Notifications
You must be signed in to change notification settings - Fork 14
feat(apisix): add server-side configuration validator #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d81071b
feat(apisix,apisix-standalone): add validate command support
jarvis9443 a07e2cf
fix: add version gating for validate e2e tests and 404 handling
jarvis9443 f8d5c67
fix: add id to global_rules, update CI matrix with 3.15/3.16/dev, bum…
jarvis9443 ed238ce
fix: relax standalone validate version gate to 3.16.0
jarvis9443 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 216 additions & 0 deletions
216
libs/backend-apisix-standalone/e2e/validate.e2e-spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| import { DifferV3 } from '@api7/adc-differ'; | ||
| import * as ADCSDK from '@api7/adc-sdk'; | ||
| import { lastValueFrom } from 'rxjs'; | ||
| import { gte } from 'semver'; | ||
|
|
||
| import { BackendAPISIXStandalone } from '../src'; | ||
| import { | ||
| defaultBackendOptions, | ||
| server1, | ||
| token1, | ||
| } from './support/constants'; | ||
| import { conditionalDescribe, semverCondition } from './support/utils'; | ||
|
|
||
| const configToEvents = (config: ADCSDK.Configuration): Array<ADCSDK.Event> => { | ||
| return DifferV3.diff( | ||
| config as ADCSDK.InternalConfiguration, | ||
| {} as ADCSDK.InternalConfiguration, | ||
| ); | ||
| }; | ||
|
|
||
| conditionalDescribe(semverCondition(gte, '3.16.0'))('Validate', () => { | ||
| let backend: BackendAPISIXStandalone; | ||
|
|
||
| beforeAll(() => { | ||
| backend = new BackendAPISIXStandalone({ | ||
| server: server1, | ||
| token: token1, | ||
| cacheKey: 'validate-test', | ||
| ...defaultBackendOptions, | ||
| }); | ||
| }); | ||
|
|
||
| it('should succeed with empty configuration', async () => { | ||
| const result = await lastValueFrom(backend.validate([])); | ||
| expect(result.success).toBe(true); | ||
| expect(result.errors).toEqual([]); | ||
| }); | ||
|
|
||
| it('should succeed with valid service and route', async () => { | ||
| const config: ADCSDK.Configuration = { | ||
| services: [ | ||
| { | ||
| name: 'validate-test-svc', | ||
| upstream: { | ||
| scheme: 'http', | ||
| nodes: [{ host: 'httpbin.org', port: 80, weight: 100 }], | ||
| }, | ||
| routes: [ | ||
| { | ||
| name: 'validate-test-route', | ||
| uris: ['/validate-test'], | ||
| methods: ['GET'], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const result = await lastValueFrom( | ||
| backend.validate(configToEvents(config)), | ||
| ); | ||
| expect(result.success).toBe(true); | ||
| expect(result.errors).toEqual([]); | ||
| }); | ||
|
|
||
| it('should succeed with valid consumer', async () => { | ||
| const config: ADCSDK.Configuration = { | ||
| consumers: [ | ||
| { | ||
| username: 'validate-test-consumer', | ||
| plugins: { | ||
| 'key-auth': { key: 'test-key-123' }, | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const result = await lastValueFrom( | ||
| backend.validate(configToEvents(config)), | ||
| ); | ||
| expect(result.success).toBe(true); | ||
| expect(result.errors).toEqual([]); | ||
| }); | ||
|
|
||
| it('should fail with invalid plugin configuration', async () => { | ||
| const config: ADCSDK.Configuration = { | ||
| services: [ | ||
| { | ||
| name: 'validate-bad-plugin-svc', | ||
| upstream: { | ||
| scheme: 'http', | ||
| nodes: [{ host: 'httpbin.org', port: 80, weight: 100 }], | ||
| }, | ||
| routes: [ | ||
| { | ||
| name: 'validate-bad-plugin-route', | ||
| uris: ['/bad-plugin'], | ||
| plugins: { | ||
| 'limit-count': { | ||
| // missing required fields: count, time_window | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const result = await lastValueFrom( | ||
| backend.validate(configToEvents(config)), | ||
| ); | ||
| expect(result.success).toBe(false); | ||
| expect(result.errors.length).toBeGreaterThan(0); | ||
| expect(result.errors[0].resource_type).toBe('routes'); | ||
| }); | ||
|
|
||
| it('should fail with invalid route (bad uri type)', async () => { | ||
| const config: ADCSDK.Configuration = { | ||
| services: [ | ||
| { | ||
| name: 'validate-bad-route-svc', | ||
| upstream: { | ||
| scheme: 'http', | ||
| nodes: [{ host: 'httpbin.org', port: 80, weight: 100 }], | ||
| }, | ||
| routes: [ | ||
| { | ||
| name: 'validate-bad-route', | ||
| uris: [123 as unknown as string], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const result = await lastValueFrom( | ||
| backend.validate(configToEvents(config)), | ||
| ); | ||
| expect(result.success).toBe(false); | ||
| expect(result.errors.length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it('should collect multiple errors', async () => { | ||
| const config: ADCSDK.Configuration = { | ||
| services: [ | ||
| { | ||
| name: 'validate-multi-err-svc', | ||
| upstream: { | ||
| scheme: 'http', | ||
| nodes: [{ host: 'httpbin.org', port: 80, weight: 100 }], | ||
| }, | ||
| routes: [ | ||
| { | ||
| name: 'validate-multi-err-route1', | ||
| uris: ['/multi-err-1'], | ||
| plugins: { | ||
| 'limit-count': {}, | ||
| }, | ||
| }, | ||
| { | ||
| name: 'validate-multi-err-route2', | ||
| uris: ['/multi-err-2'], | ||
| plugins: { | ||
| 'limit-count': {}, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const result = await lastValueFrom( | ||
| backend.validate(configToEvents(config)), | ||
| ); | ||
| expect(result.success).toBe(false); | ||
| expect(result.errors.length).toBeGreaterThanOrEqual(2); | ||
| }); | ||
|
|
||
| it('should succeed with mixed resource types', async () => { | ||
| const config: ADCSDK.Configuration = { | ||
| services: [ | ||
| { | ||
| name: 'validate-mixed-svc', | ||
| upstream: { | ||
| scheme: 'https', | ||
| nodes: [{ host: 'httpbin.org', port: 443, weight: 100 }], | ||
| }, | ||
| routes: [ | ||
| { | ||
| name: 'validate-mixed-route', | ||
| uris: ['/mixed-test'], | ||
| methods: ['GET', 'POST'], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| consumers: [ | ||
| { | ||
| username: 'validate-mixed-consumer', | ||
| plugins: { | ||
| 'key-auth': { key: 'mixed-key-456' }, | ||
| }, | ||
| }, | ||
| ], | ||
| global_rules: { | ||
| prometheus: { prefer_name: false }, | ||
| } as ADCSDK.Configuration['global_rules'], | ||
| }; | ||
|
|
||
| const result = await lastValueFrom( | ||
| backend.validate(configToEvents(config)), | ||
| ); | ||
| expect(result.success).toBe(true); | ||
| expect(result.errors).toEqual([]); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.