Skip to content
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

chore: fix endpoints-grpc system test #1794

Merged
merged 1 commit into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions endpoints/getting-started-grpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,8 @@
"yargs": "^15.0.0"
},
"devDependencies": {
"delay": "^4.2.0",
"mocha": "^7.0.0"
},
"cloud-repo-tools": {
"requiresKeyFile": true,
"requiresProjectId": true,
"test": {
"app": {
"requiredEnvVars": [
"ENDPOINTS_API_KEY",
"ENDPOINTS_GCE_HOST",
"ENDPOINTS_GKE_HOST",
"ENDPOINTS_SERVICE_NAME"
]
},
"build": {
"requiredEnvVars": [
"ENDPOINTS_API_KEY",
"ENDPOINTS_GCE_HOST",
"ENDPOINTS_GKE_HOST",
"ENDPOINTS_SERVICE_NAME"
]
}
}
"chai": "^4.2.0",
"mocha": "^7.1.2",
"wait-port": "^0.2.7"
}
}
132 changes: 13 additions & 119 deletions endpoints/getting-started-grpc/system-test/endpoints.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Google LLC
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,127 +14,21 @@

'use strict';

const waitPort = require('wait-port');
const {expect} = require('chai');

const childProcess = require('child_process');
const path = require('path');
const assert = require('assert');
const fs = require(`fs`);
const jwt = require('jsonwebtoken');
const delay = require('delay');

const clientCmd = `node client.js`;
const serverCmd = `node server.js`;

const cwd = path.join(__dirname, `..`);

const API_KEY = process.env.ENDPOINTS_API_KEY;
const GOOGLE_KEYFILE = JSON.parse(
fs.readFileSync(process.env.GOOGLE_APPLICATION_CREDENTIALS, 'utf8')
);
const SERVICE_NAME = process.env.ENDPOINTS_SERVICE_NAME;
const GCE_HOST = process.env.ENDPOINTS_GCE_HOST;
const GKE_HOST = process.env.ENDPOINTS_GKE_HOST;

before(() => {
assert.ok(API_KEY, 'Must set ENDPOINTS_API_KEY environment variable!');
assert.ok(GCE_HOST, 'Must set ENDPOINTS_GCE_HOST environment variable!');
assert.ok(GKE_HOST, 'Must set ENDPOINTS_GKE_HOST environment variable!');
assert.ok(
SERVICE_NAME,
'Must set ENDPOINTS_SERVICE_NAME environment variable!'
);
assert.ok(
GOOGLE_KEYFILE,
'GOOGLE_APPLICATION_CREDENTIALS environment variable must point to a service account keyfile!'
);
assert.ok(
GOOGLE_KEYFILE.client_email,
'Service account keyfile must contain a "client_email" field!'
);
assert.ok(
GOOGLE_KEYFILE.private_key,
'Service account keyfile must contain a "private_key" field!'
);
});

// Generate JWT based on GOOGLE_APPLICATION_CREDENTIALS and ENDPOINTS_SERVICE_NAME
const JWT_AUTH_TOKEN = jwt.sign(
{
aud: SERVICE_NAME,
iss: GOOGLE_KEYFILE.client_email,
iat: parseInt(Date.now() / 1000),
exp: parseInt(Date.now() / 1000) + 20 * 60, // 20 minutes
email: GOOGLE_KEYFILE.client_email,
sub: GOOGLE_KEYFILE.client_email,
},
GOOGLE_KEYFILE.private_key,
{algorithm: 'RS256'}
);

// API key
it(`should request a greeting from a remote Compute Engine instance using an API key`, () => {
const output = childProcess.execSync(
`${clientCmd} -h ${GCE_HOST} -k ${API_KEY}`,
cwd
);
assert.ok(new RegExp('Hello world').test(output));
});

it(`should request a greeting from a remote Container Engine cluster using an API key`, () => {
const output = childProcess.execSync(
`${clientCmd} -h ${GKE_HOST} -k ${API_KEY}`,
cwd
);
assert.ok(new RegExp('Hello world').test(output));
});

it('should request and handle a greeting locally using an API key', async () => {
const PORT = 50051;
const server = childProcess.exec(`${serverCmd} -p ${PORT}`, {cwd: cwd});

await delay(1000);
const clientOutput = childProcess.execSync(
`${clientCmd} -h localhost:${PORT} -k ${API_KEY}`,
cwd
);
assert.ok(new RegExp('Hello world').test(clientOutput));
server.kill();
});

// Authtoken
it(`should request a greeting from a remote Compute Engine instance using a JWT Auth Token`, () => {
const output = childProcess.execSync(
`${clientCmd} -h ${GCE_HOST} -j ${JWT_AUTH_TOKEN}`,
cwd
);
assert.ok(new RegExp('Hello world').test(output));
});

it(`should request a greeting from a remote Container Engine cluster using a JWT Auth Token`, () => {
const output = childProcess.execSync(
`${clientCmd} -h ${GKE_HOST} -j ${JWT_AUTH_TOKEN}`,
cwd
);
assert.ok(new RegExp('Hello world').test(output));
});

it(`should request and handle a greeting locally using a JWT Auth Token`, async () => {
const PORT = 50051;
const server = childProcess.exec(`${serverCmd} -p ${PORT}`, {cwd: cwd});
const appPath = path.join(__dirname, '../server.js');

await delay(1000);
const clientOutput = childProcess.execSync(
`${clientCmd} -h localhost:${PORT} -j ${JWT_AUTH_TOKEN}`,
cwd
);
assert.ok(new RegExp('Hello world').test(clientOutput));
server.kill();
});
const PORT = process.env.PORT || 8080;

// Misc
it('should require either an API key or a JWT Auth Token', async () => {
const {stderr} = await childProcess.exec(`${clientCmd} -h ${GCE_HOST}`, {
cwd: cwd,
shell: true,
describe('server listening', () => {
it('should be listening', async () => {
const child = childProcess.exec(`node ${appPath} -p ${PORT}`);
const isOpen = await waitPort({port: PORT});
expect(isOpen).to.be.true;
process.kill(child.pid, 'SIGTERM');
});
assert.ok(stderr.includes('One of API_KEY or JWT_AUTH_TOKEN must be set'));
});
});