Skip to content

Commit 1ce0143

Browse files
committed
Add support for testing with alternate credentials
1 parent 08172ba commit 1ce0143

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

packages/grpc-js-xds/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import * as typed_struct_lb from './lb-policy-registry/typed-struct';
3232
import * as pick_first_lb from './lb-policy-registry/pick-first';
3333

3434
export { XdsServer } from './server';
35+
export { XdsServerCredentials } from './xds-credentials';
3536

3637
/**
3738
* Register the "xds:" name scheme with the @grpc/grpc-js library.

packages/grpc-js-xds/test/backend.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class Backend {
5050
private server: Server | null = null;
5151
private receivedCallCount = 0;
5252
private callListeners: (() => void)[] = [];
53-
constructor(private port: number, private useXdsServer: boolean, private serverOptions?: ServerOptions) {
53+
constructor(private port: number, private useXdsServer: boolean, private creds?: ServerCredentials | undefined, private serverOptions?: ServerOptions) {
5454
}
5555
Echo(call: ServerUnaryCall<EchoRequest__Output, EchoResponse>, callback: sendUnaryData<EchoResponse>) {
5656
// call.request.params is currently ignored
@@ -90,9 +90,8 @@ export class Backend {
9090
}
9191
const server = this.server;
9292
server.addService(loadedProtos.grpc.testing.EchoTestService.service, this as unknown as UntypedServiceImplementation);
93-
const insecureCredentials = ServerCredentials.createInsecure();
94-
const xdsCredentials = new XdsServerCredentials(insecureCredentials);
95-
server.bindAsync(`[::1]:${this.port}`, xdsCredentials, (error, port) => {
93+
const credentials = this.creds ?? ServerCredentials.createInsecure();
94+
server.bindAsync(`[::1]:${this.port}`, credentials, (error, port) => {
9695
if (!error) {
9796
this.port = port;
9897
}
@@ -148,7 +147,7 @@ export class Backend {
148147
}
149148
}
150149

151-
export async function createBackends(count: number, useXdsServer?: boolean, serverOptions?: ServerOptions): Promise<Backend[]> {
150+
export async function createBackends(count: number, useXdsServer?: boolean, creds?: ServerCredentials | undefined, serverOptions?: ServerOptions): Promise<Backend[]> {
152151
const ports = await findFreePorts(count);
153-
return ports.map(port => new Backend(port, useXdsServer ?? true, serverOptions));
152+
return ports.map(port => new Backend(port, useXdsServer ?? true, creds, serverOptions));
154153
}

packages/grpc-js-xds/test/client.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*/
1717

18-
import { ChannelOptions, credentials, loadPackageDefinition, ServiceError } from "@grpc/grpc-js";
18+
import { ChannelCredentials, ChannelOptions, credentials, loadPackageDefinition, ServiceError } from "@grpc/grpc-js";
1919
import { loadSync } from "@grpc/proto-loader";
2020
import { ProtoGrpcType } from "./generated/echo";
2121
import { EchoTestServiceClient } from "./generated/grpc/testing/EchoTestService";
@@ -44,14 +44,14 @@ export class XdsTestClient {
4444
private client: EchoTestServiceClient;
4545
private callInterval: NodeJS.Timer;
4646

47-
constructor(target: string, bootstrapInfo: string, options?: ChannelOptions) {
48-
this.client = new loadedProtos.grpc.testing.EchoTestService(target, credentials.createInsecure(), {...options, [BOOTSTRAP_CONFIG_KEY]: bootstrapInfo});
47+
constructor(target: string, bootstrapInfo: string, creds?: ChannelCredentials | undefined, options?: ChannelOptions) {
48+
this.client = new loadedProtos.grpc.testing.EchoTestService(target, creds ?? credentials.createInsecure(), {...options, [BOOTSTRAP_CONFIG_KEY]: bootstrapInfo});
4949
this.callInterval = setInterval(() => {}, 0);
5050
clearInterval(this.callInterval);
5151
}
5252

53-
static createFromServer(targetName: string, xdsServer: ControlPlaneServer, options?: ChannelOptions) {
54-
return new XdsTestClient(`xds:///${targetName}`, xdsServer.getBootstrapInfoString(), options);
53+
static createFromServer(targetName: string, xdsServer: ControlPlaneServer, creds?: ChannelCredentials | undefined, options?: ChannelOptions) {
54+
return new XdsTestClient(`xds:///${targetName}`, xdsServer.getBootstrapInfoString(), creds, options);
5555
}
5656

5757
startCalls(interval: number) {

packages/grpc-js-xds/test/test-core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('core xDS functionality', () => {
8484
const serverRoute = new FakeServerRoute(backend.getPort(), 'serverRoute');
8585
xdsServer.setRdsResource(serverRoute.getRouteConfiguration());
8686
xdsServer.setLdsResource(serverRoute.getListener());
87-
client = XdsTestClient.createFromServer('listener1', xdsServer, {
87+
client = XdsTestClient.createFromServer('listener1', xdsServer, undefined, {
8888
'grpc.client_idle_timeout_ms': 1000,
8989
});
9090
client.sendOneCall(error => {
@@ -102,7 +102,7 @@ describe('core xDS functionality', () => {
102102
});
103103
it('should handle connections aging out', function(done) {
104104
this.timeout(5000);
105-
createBackends(1, true, {'grpc.max_connection_age_ms': 1000}).then(([backend]) => {
105+
createBackends(1, true, undefined, {'grpc.max_connection_age_ms': 1000}).then(([backend]) => {
106106
const serverRoute = new FakeServerRoute(backend.getPort(), 'serverRoute');
107107
xdsServer.setRdsResource(serverRoute.getRouteConfiguration());
108108
xdsServer.setLdsResource(serverRoute.getListener());

0 commit comments

Comments
 (0)