Skip to content

Commit

Permalink
test: add apisix e2e environment (api7#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Feb 5, 2024
2 parents 50a016a + 49f4c70 commit 5599a58
Show file tree
Hide file tree
Showing 14 changed files with 252 additions and 711 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: E2E
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
apisix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

# Setup backend environment
- name: Setup Apache APISIX
working-directory: ./apps/cli-e2e/assets/apisix
run: |
docker compose up -d
sleep 10
# Build and test ADC CLI
- uses: actions/setup-node@v4
with:
node-version: 'lts/*'
- uses: pnpm/action-setup@v2
with:
version: latest
- name: Build ADC
run: |
pnpm install
npx nx build cli
node --experimental-sea-config apps/cli/node-sea.json
cp $(command -v node) adc
npx postject adc NODE_SEA_BLOB sea-prep.blob --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2
# Run ADC tests
- name: Run E2E tests
run: npx nx run cli-e2e:e2e --test-file apps/cli-e2e/src/apisix
9 changes: 9 additions & 0 deletions apps/cli-e2e/assets/apisix/apisix-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
deployment:
admin:
allow_admin:
- 0.0.0.0/0
etcd:
host:
- "http://etcd:2379"
prefix: "/apisix"
timeout: 30
19 changes: 19 additions & 0 deletions apps/cli-e2e/assets/apisix/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3'
services:
etcd:
image: bitnami/etcd:3.5.11
environment:
ALLOW_NONE_AUTHENTICATION: 'yes'
ETCD_ADVERTISE_CLIENT_URLS: 'http://etcd:2379'
ETCD_LISTEN_CLIENT_URLS: 'http://0.0.0.0:2379'
ports:
- '2379:2379/tcp'
apisix:
image: apache/apisix:3.8.0-debian
depends_on:
- etcd
volumes:
- ./apisix-config.yaml:/usr/local/apisix/conf/config.yaml:ro
ports:
- '9080:9080/tcp'
- '9180:9180/tcp'
35 changes: 35 additions & 0 deletions apps/cli-e2e/src/apisix/ping.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { execSync } from 'node:child_process';

describe('Ping command', () => {
it('Ensure ping command is exist', async () => {
const out = execSync(`./adc help ping`).toString('utf8');
expect(out).toContain('Usage: adc ping [options]');
});

it('Use ping command (with correct token)', () => {
const out = execSync(
'./adc ping --token edd1c9f034335f136f87ad84b625c8f1',
).toString('utf8');
expect(out).toContain('Connected to backend successfully!');
});

it('Use ping command (with incorrect token)', () => {
try {
execSync('./adc ping --token incorrent-token');
} catch (err) {
expect(err.output.toString('utf8')).toContain(
'Failed to ping backend, AxiosError: Request failed with status code 401',
);
}
});

it('Use ping command (with incorrect server address)', () => {
try {
execSync('./adc ping --token token --server http://127.0.0.1:9999');
} catch (err) {
expect(err.output.toString('utf8')).toContain(
'Failed to ping backend, Error: connect ECONNREFUSED 127.0.0.1:9999',
);
}
});
});
4 changes: 4 additions & 0 deletions apps/cli/node-sea.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "dist/apps/cli/main.js",
"output": "sea-prep.blob"
}
12 changes: 0 additions & 12 deletions apps/cli/src/app/app.module.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/cli/src/app/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const setupCommands = (): Command => {
'HTTP address of backend. This value can also be set using the environment variable ADC_SERVER environment variable',
)
.env('ADC_SERVER')
.default('http://localhost:9080'),
.default('http://localhost:9180'),
)
.addOption(
program
Expand Down
3 changes: 2 additions & 1 deletion apps/cli/src/app/command/ping.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const PingCommand = new Command('ping')
console.log(chalk.green('Connected to backend successfully!'));
} catch (err) {
console.log(chalk.red(`Failed to ping backend, ${err}`));
if (opts.debug) throw err;
if (opts.debug) console.log(err);
process.exit(1);
}
});
9 changes: 0 additions & 9 deletions apps/cli/src/app/command/test.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ export const TestCommand = new Command('dev')

const cfg = await backend.dump();

Object.keys(cfg).forEach((key) => {
//if (cfg[key].length === 0) delete cfg[key];
});

//console.log(cfg);

unlinkSync('./t.yaml');
writeFileSync('./t.yaml', stringify(cfg), {});

const fileContent =
parse(readFileSync('./t1.yaml', { encoding: 'utf-8' })) ?? {};

Expand Down
10 changes: 4 additions & 6 deletions apps/cli/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { NestFactory } from '@nestjs/core';
import { setupCommands } from './app/command';

import { AppModule } from './app/app.module';
// hide nodejs sea warning
process.removeAllListeners('warning');

async function bootstrap() {
const app = await NestFactory.createApplicationContext(AppModule, {
logger: false,
});
await app.close();
await setupCommands().parseAsync(process.argv);
}

bootstrap();
3 changes: 3 additions & 0 deletions apps/cli/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = composePlugins(
(config) => {
// Update the webpack config as needed here.
// e.g. `config.plugins.push(new MyPlugin())`
config.externals = {
'*': false
}
return config;
}
);
3 changes: 0 additions & 3 deletions libs/backend-apisix/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ export class BackendAPISIX implements ADCSDK.Backend {
total: number;
}>(resourceName);

if (resourceName === 'consumer_groups') console.log(resp.data.list);

return resp.data.list
.map((item) => {
// the special measure for compatibility with PluginMetadata structure
Expand Down Expand Up @@ -142,7 +140,6 @@ export class BackendAPISIX implements ADCSDK.Backend {
}

private async deleteResources(resourceName: string, resourceId: string) {
console.log('deleteResources', resourceName, resourceId);
const resp = await this.client.delete(`${resourceName}/${resourceId}`, {
validateStatus: () => true,
});
Expand Down
11 changes: 1 addition & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
"scripts": {},
"private": true,
"devDependencies": {
"@nestjs/schematics": "^10.0.1",
"@nestjs/testing": "^10.0.2",
"@nx/esbuild": "17.2.8",
"@nx/eslint": "17.2.8",
"@nx/eslint-plugin": "17.2.8",
"@nx/jest": "17.2.8",
"@nx/js": "17.2.8",
"@nx/nest": "^17.2.8",
"@nx/node": "^17.2.8",
"@nx/webpack": "17.2.8",
"@nx/workspace": "17.2.8",
Expand Down Expand Up @@ -41,20 +38,14 @@
"url-loader": "^4.1.1"
},
"dependencies": {
"@aws-crypto/crc32": "^5.2.0",
"@nestjs/common": "^10.0.2",
"@nestjs/core": "^10.0.2",
"@nestjs/platform-express": "^10.0.2",
"axios": "^1.6.5",
"chalk": "^4",
"commander": "^11.1.0",
"deep-object-diff": "^1.1.9",
"effect": "^2.2.1",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"nest-commander": "^3.12.5",
"nestjs-console": "^9.0.0",
"p-queue": "^6",
"p-queue": "^8",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0",
"tslib": "^2.3.0",
Expand Down
Loading

0 comments on commit 5599a58

Please sign in to comment.