From 47d44c5f3bbaf4579900a38cb16af5ffa384d355 Mon Sep 17 00:00:00 2001 From: bzp2010 Date: Sun, 10 Nov 2024 21:24:11 +0800 Subject: [PATCH] test(apisix): e2e --- libs/backend-apisix/e2e/misc.e2e-spec.ts | 85 ++++++++++++++++++++++++ libs/backend-apisix/e2e/support/utils.ts | 10 +++ 2 files changed, 95 insertions(+) create mode 100644 libs/backend-apisix/e2e/misc.e2e-spec.ts diff --git a/libs/backend-apisix/e2e/misc.e2e-spec.ts b/libs/backend-apisix/e2e/misc.e2e-spec.ts new file mode 100644 index 0000000..ee5a27b --- /dev/null +++ b/libs/backend-apisix/e2e/misc.e2e-spec.ts @@ -0,0 +1,85 @@ +import * as ADCSDK from '@api7/adc-sdk'; + +import { BackendAPISIX } from '../src'; +import { + createEvent, + deleteEvent, + dumpConfiguration, + overrideEventResourceId, + syncEvents, +} from './support/utils'; + +describe('Miscellaneous', () => { + let backend: BackendAPISIX; + + beforeAll(() => { + backend = new BackendAPISIX({ + server: process.env.SERVER, + token: process.env.TOKEN, + }); + }); + + describe('Sync resources with custom id', () => { + const routeName = 'Test Route'; + const serviceName = 'Test Service'; + const route = { + id: 'custom-route', + name: routeName, + uris: ['/test'], + }; + const service = { + id: 'custom-service', + name: serviceName, + upstream: { + scheme: 'https', + nodes: [ + { + host: 'httpbin.org', + port: 443, + weight: 100, + }, + ], + }, + routes: [route], + } as ADCSDK.Service; + + it('Create services', async () => + syncEvents(backend, [ + overrideEventResourceId( + createEvent(ADCSDK.ResourceType.SERVICE, serviceName, service), + 'custom-service', + ), + overrideEventResourceId( + createEvent(ADCSDK.ResourceType.ROUTE, routeName, route, serviceName), + 'custom-route', + 'custom-service', + ), + ])); + + it('Dump', async () => { + const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration; + expect(result.services).toHaveLength(1); + expect(result.services[0]).toMatchObject(service); + expect(result.services[0]).toMatchObject(service); + expect(result.services[0].routes[0]).toMatchObject(route); + }); + + it('Delete service', async () => + syncEvents(backend, [ + overrideEventResourceId( + deleteEvent(ADCSDK.ResourceType.ROUTE, routeName, serviceName), + 'custom-route', + 'custom-service', + ), + overrideEventResourceId( + deleteEvent(ADCSDK.ResourceType.SERVICE, serviceName), + 'custom-service', + ), + ])); + + it('Dump again', async () => { + const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration; + expect(result.services).toHaveLength(0); + }); + }); +}); diff --git a/libs/backend-apisix/e2e/support/utils.ts b/libs/backend-apisix/e2e/support/utils.ts index 24dc14b..5d4d3fb 100644 --- a/libs/backend-apisix/e2e/support/utils.ts +++ b/libs/backend-apisix/e2e/support/utils.ts @@ -91,6 +91,16 @@ export const deleteEvent = ( : undefined, }); +export const overrideEventResourceId = ( + event: ADCSDK.Event, + resourceId: string, + parentId?: string, +) => { + event.resourceId = resourceId; + if (parentId) event.parentId = parentId; + return event; +}; + type cond = boolean | (() => boolean); export const conditionalDescribe = (cond: cond) =>