mirror of https://github.com/grpc/grpc-node.git
Add support for testing with alternate credentials
This commit is contained in:
parent
08172ba9d9
commit
1ce0143220
|
@ -32,6 +32,7 @@ import * as typed_struct_lb from './lb-policy-registry/typed-struct';
|
|||
import * as pick_first_lb from './lb-policy-registry/pick-first';
|
||||
|
||||
export { XdsServer } from './server';
|
||||
export { XdsServerCredentials } from './xds-credentials';
|
||||
|
||||
/**
|
||||
* Register the "xds:" name scheme with the @grpc/grpc-js library.
|
||||
|
|
|
@ -50,7 +50,7 @@ export class Backend {
|
|||
private server: Server | null = null;
|
||||
private receivedCallCount = 0;
|
||||
private callListeners: (() => void)[] = [];
|
||||
constructor(private port: number, private useXdsServer: boolean, private serverOptions?: ServerOptions) {
|
||||
constructor(private port: number, private useXdsServer: boolean, private creds?: ServerCredentials | undefined, private serverOptions?: ServerOptions) {
|
||||
}
|
||||
Echo(call: ServerUnaryCall<EchoRequest__Output, EchoResponse>, callback: sendUnaryData<EchoResponse>) {
|
||||
// call.request.params is currently ignored
|
||||
|
@ -90,9 +90,8 @@ export class Backend {
|
|||
}
|
||||
const server = this.server;
|
||||
server.addService(loadedProtos.grpc.testing.EchoTestService.service, this as unknown as UntypedServiceImplementation);
|
||||
const insecureCredentials = ServerCredentials.createInsecure();
|
||||
const xdsCredentials = new XdsServerCredentials(insecureCredentials);
|
||||
server.bindAsync(`[::1]:${this.port}`, xdsCredentials, (error, port) => {
|
||||
const credentials = this.creds ?? ServerCredentials.createInsecure();
|
||||
server.bindAsync(`[::1]:${this.port}`, credentials, (error, port) => {
|
||||
if (!error) {
|
||||
this.port = port;
|
||||
}
|
||||
|
@ -148,7 +147,7 @@ export class Backend {
|
|||
}
|
||||
}
|
||||
|
||||
export async function createBackends(count: number, useXdsServer?: boolean, serverOptions?: ServerOptions): Promise<Backend[]> {
|
||||
export async function createBackends(count: number, useXdsServer?: boolean, creds?: ServerCredentials | undefined, serverOptions?: ServerOptions): Promise<Backend[]> {
|
||||
const ports = await findFreePorts(count);
|
||||
return ports.map(port => new Backend(port, useXdsServer ?? true, serverOptions));
|
||||
return ports.map(port => new Backend(port, useXdsServer ?? true, creds, serverOptions));
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
import { ChannelOptions, credentials, loadPackageDefinition, ServiceError } from "@grpc/grpc-js";
|
||||
import { ChannelCredentials, ChannelOptions, credentials, loadPackageDefinition, ServiceError } from "@grpc/grpc-js";
|
||||
import { loadSync } from "@grpc/proto-loader";
|
||||
import { ProtoGrpcType } from "./generated/echo";
|
||||
import { EchoTestServiceClient } from "./generated/grpc/testing/EchoTestService";
|
||||
|
@ -44,14 +44,14 @@ export class XdsTestClient {
|
|||
private client: EchoTestServiceClient;
|
||||
private callInterval: NodeJS.Timer;
|
||||
|
||||
constructor(target: string, bootstrapInfo: string, options?: ChannelOptions) {
|
||||
this.client = new loadedProtos.grpc.testing.EchoTestService(target, credentials.createInsecure(), {...options, [BOOTSTRAP_CONFIG_KEY]: bootstrapInfo});
|
||||
constructor(target: string, bootstrapInfo: string, creds?: ChannelCredentials | undefined, options?: ChannelOptions) {
|
||||
this.client = new loadedProtos.grpc.testing.EchoTestService(target, creds ?? credentials.createInsecure(), {...options, [BOOTSTRAP_CONFIG_KEY]: bootstrapInfo});
|
||||
this.callInterval = setInterval(() => {}, 0);
|
||||
clearInterval(this.callInterval);
|
||||
}
|
||||
|
||||
static createFromServer(targetName: string, xdsServer: ControlPlaneServer, options?: ChannelOptions) {
|
||||
return new XdsTestClient(`xds:///${targetName}`, xdsServer.getBootstrapInfoString(), options);
|
||||
static createFromServer(targetName: string, xdsServer: ControlPlaneServer, creds?: ChannelCredentials | undefined, options?: ChannelOptions) {
|
||||
return new XdsTestClient(`xds:///${targetName}`, xdsServer.getBootstrapInfoString(), creds, options);
|
||||
}
|
||||
|
||||
startCalls(interval: number) {
|
||||
|
|
|
@ -84,7 +84,7 @@ describe('core xDS functionality', () => {
|
|||
const serverRoute = new FakeServerRoute(backend.getPort(), 'serverRoute');
|
||||
xdsServer.setRdsResource(serverRoute.getRouteConfiguration());
|
||||
xdsServer.setLdsResource(serverRoute.getListener());
|
||||
client = XdsTestClient.createFromServer('listener1', xdsServer, {
|
||||
client = XdsTestClient.createFromServer('listener1', xdsServer, undefined, {
|
||||
'grpc.client_idle_timeout_ms': 1000,
|
||||
});
|
||||
client.sendOneCall(error => {
|
||||
|
@ -102,7 +102,7 @@ describe('core xDS functionality', () => {
|
|||
});
|
||||
it('should handle connections aging out', function(done) {
|
||||
this.timeout(5000);
|
||||
createBackends(1, true, {'grpc.max_connection_age_ms': 1000}).then(([backend]) => {
|
||||
createBackends(1, true, undefined, {'grpc.max_connection_age_ms': 1000}).then(([backend]) => {
|
||||
const serverRoute = new FakeServerRoute(backend.getPort(), 'serverRoute');
|
||||
xdsServer.setRdsResource(serverRoute.getRouteConfiguration());
|
||||
xdsServer.setLdsResource(serverRoute.getListener());
|
||||
|
|
Loading…
Reference in New Issue