use merged gRPC object in api tests

This commit is contained in:
Kelvin Jin 2017-11-07 15:00:23 -08:00
parent 00ff2c909e
commit 9d9404615e
14 changed files with 245 additions and 52 deletions

View File

@ -1,11 +1,6 @@
module.exports = {
packageJson: {},
core: {
packageJson: {}
}
};
const _ = require('lodash');
function assignExportsFromGlobal(globalField, exportsField) {
function getImplementation(globalField) {
if (global[globalField] !== 'js' && global[globalField] !== 'native') {
throw new Error([
`Invalid value for global.${globalField}: ${global.globalField}.`,
@ -13,30 +8,28 @@ function assignExportsFromGlobal(globalField, exportsField) {
].join(' '));
}
const impl = global[globalField];
// (1) set global field.
module.exports[exportsField] = require(`../packages/grpc-${impl}`);
// (2) make package's package.json file accessible thru packageJson path.
module.exports.packageJson[exportsField] = require(`../packages/grpc-${impl}/package.json`);
// (3) make package's underlying core dependency accessible thru core path.
module.exports.core[exportsField] = require(`../packages/grpc-${impl}-core`);
// (4) make (3) x (2) accessible thru core.packageJson path.
module.exports.core.packageJson[exportsField] = require(`../packages/grpc-${impl}-core/package.json`);
return {
surface: require(`../packages/grpc-${impl}`),
pjson: require(`../packages/grpc-${impl}/package.json`),
core: require(`../packages/grpc-${impl}-core`),
corePjson: require(`../packages/grpc-${impl}-core/package.json`)
};
}
// Set 'server' and 'client' fields on this module's exports.
// These don't refer to the portions of the gRPC interface that are
// relevant to an application behaving as a server or a client respectively.
// Instead, they refer to the entire gRPC module as it's visible to the
// application.
// In other words, a test that simulates a gRPC client should treat
// require('any-grpc').client as the value of require('grpc'), and would simply
// not be expected to use server components.
assignExportsFromGlobal('_server_implementation', 'server');
assignExportsFromGlobal('_client_implementation', 'client');
// Increase clarity when there's no distinction between client/server
if (module.exports.client === module.exports.server) {
module.exports.all = module.exports.client;
module.exports.core.all = module.exports.core.client;
module.exports.packageJson.all = module.exports.packageJson.client;
module.exports.core.packageJson.all = module.exports.core.packageJson.client;
}
const clientImpl = getImplementation('_client_implementation');
const serverImpl = getImplementation('_server_implementation');
// We export a "merged" gRPC API by merging client and server specified
// APIs together. Any function that is unspecific to client/server defaults
// to client-side implementation.
// This object also has a test-only field from which details about the
// modules may be read.
module.exports = Object.assign({
'$implementationInfo': {
client: clientImpl,
server: serverImpl
}
}, clientImpl.surface, _.pick(serverImpl.surface, [
'Server',
'ServerCredentials'
]));

View File

@ -20,7 +20,7 @@
var assert = require('assert');
var grpc = require('../any_grpc').all;
var grpc = require('../any_grpc');
var math = grpc.load(
__dirname + '/../../packages/grpc-native-core/deps/grpc/src/proto/math/math.proto').math;

View File

@ -0,0 +1,165 @@
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
'use strict';
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var grpc = require('../any_grpc');
var key_data, pem_data, ca_data;
before(function() {
var key_path = path.join(__dirname, '../data/server1.key');
var pem_path = path.join(__dirname, '../data/server1.pem');
var ca_path = path.join(__dirname, '../data/ca.pem');
key_data = fs.readFileSync(key_path);
pem_data = fs.readFileSync(pem_path);
ca_data = fs.readFileSync(ca_path);
});
describe('channel credentials', function() {
describe('#createSsl', function() {
it('works with no arguments', function() {
var creds;
assert.doesNotThrow(function() {
creds = grpc.credentials.createSsl();
});
assert.notEqual(creds, null);
});
it('works with just one Buffer argument', function() {
var creds;
assert.doesNotThrow(function() {
creds = grpc.credentials.createSsl(ca_data);
});
assert.notEqual(creds, null);
});
it('works with 3 Buffer arguments', function() {
var creds;
assert.doesNotThrow(function() {
creds = grpc.credentials.createSsl(ca_data, key_data, pem_data);
});
assert.notEqual(creds, null);
});
it('works if the first argument is null', function() {
var creds;
assert.doesNotThrow(function() {
creds = grpc.credentials.createSsl(null, key_data, pem_data);
});
assert.notEqual(creds, null);
});
it('fails if the first argument is a non-Buffer value', function() {
assert.throws(function() {
grpc.credentials.createSsl('test');
}, TypeError);
});
it('fails if the second argument is a non-Buffer value', function() {
assert.throws(function() {
grpc.credentials.createSsl(null, 'test', pem_data);
}, TypeError);
});
it('fails if the third argument is a non-Buffer value', function() {
assert.throws(function() {
grpc.credentials.createSsl(null, key_data, 'test');
}, TypeError);
});
it('fails if only 1 of the last 2 arguments is provided', function() {
assert.throws(function() {
grpc.credentials.createSsl(null, key_data);
});
assert.throws(function() {
grpc.credentials.createSsl(null, null, pem_data);
});
});
});
});
describe('server credentials', function() {
describe('#createSsl', function() {
it('accepts a buffer and array as the first 2 arguments', function() {
var creds;
assert.doesNotThrow(function() {
creds = grpc.ServerCredentials.createSsl(ca_data, []);
});
assert.notEqual(creds, null);
});
it('accepts a boolean as the third argument', function() {
var creds;
assert.doesNotThrow(function() {
creds = grpc.ServerCredentials.createSsl(ca_data, [], true);
});
assert.notEqual(creds, null);
});
it('accepts an object with two buffers in the second argument', function() {
var creds;
assert.doesNotThrow(function() {
creds = grpc.ServerCredentials.createSsl(null,
[{private_key: key_data,
cert_chain: pem_data}]);
});
assert.notEqual(creds, null);
});
it('accepts multiple objects in the second argument', function() {
var creds;
assert.doesNotThrow(function() {
creds = grpc.ServerCredentials.createSsl(null,
[{private_key: key_data,
cert_chain: pem_data},
{private_key: key_data,
cert_chain: pem_data}]);
});
assert.notEqual(creds, null);
});
it('fails if the second argument is not an Array', function() {
assert.throws(function() {
grpc.ServerCredentials.createSsl(ca_data, 'test');
}, TypeError);
});
it('fails if the first argument is a non-Buffer value', function() {
assert.throws(function() {
grpc.ServerCredentials.createSsl('test', []);
}, TypeError);
});
it('fails if the third argument is a non-boolean value', function() {
assert.throws(function() {
grpc.ServerCredentials.createSsl(ca_data, [], 'test');
}, TypeError);
});
it('fails if the array elements are not objects', function() {
assert.throws(function() {
grpc.ServerCredentials.createSsl(ca_data, 'test');
}, TypeError);
});
it('fails if the object does not have a Buffer private_key', function() {
assert.throws(function() {
grpc.ServerCredentials.createSsl(null,
[{private_key: 'test',
cert_chain: pem_data}]);
}, TypeError);
});
it('fails if the object does not have a Buffer cert_chain', function() {
assert.throws(function() {
grpc.ServerCredentials.createSsl(null,
[{private_key: key_data,
cert_chain: 'test'}]);
}, TypeError);
});
});
});

View File

@ -22,7 +22,7 @@ var assert = require('assert');
var fs = require('fs');
var path = require('path');
var grpc = require('../any_grpc').all;
var grpc = require('../any_grpc');
/**
* This is used for testing functions with multiple asynchronous calls that

View File

@ -0,0 +1,40 @@
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
'use strict';
var assert = require('assert');
var grpc = require('../any_grpc');
describe('File loader', function() {
it('Should load a proto file by default', function() {
assert.doesNotThrow(function() {
grpc.load(__dirname + '/test_service.proto');
});
});
it('Should load a proto file with the proto format', function() {
assert.doesNotThrow(function() {
grpc.load(__dirname + '/test_service.proto', 'proto');
});
});
it('Should load a json file with the json format', function() {
assert.doesNotThrow(function() {
grpc.load(__dirname + '/test_service.json', 'json');
});
});
});

View File

@ -18,8 +18,8 @@
'use strict';
var interop_server = require('./interop_server.js');
var interop_client = require('./interop_client.js');
var interop_server = require('../interop/interop_server.js');
var interop_client = require('../interop/interop_client.js');
var server;

View File

@ -16,7 +16,7 @@
// limitations under the License.
//
'use strict';
var grpc = require('../../any_grpc.js').all;
var grpc = require('../../any_grpc.js');
var math_math_pb = require('../math/math_pb.js');
function serialize_DivArgs(arg) {

View File

@ -18,7 +18,7 @@
'use strict';
var grpc = require('../../any_grpc.js').all;
var grpc = require('../../any_grpc.js');
var grpcMath = require('./math_grpc_pb');
var math = require('./math_pb');

View File

@ -20,7 +20,7 @@
var assert = require('assert');
var grpc = require('../any_grpc').all;
var grpc = require('../any_grpc');
var math = require('./math/math_pb');
var MathClient = require('./math/math_grpc_pb').MathClient;

View File

@ -18,7 +18,7 @@
'use strict';
var Metadata = require('../any_grpc').all.Metadata;
var Metadata = require('../any_grpc').Metadata;
var assert = require('assert');

View File

@ -21,7 +21,7 @@
var assert = require('assert');
var _ = require('lodash');
var grpc = require('../any_grpc').all;
var grpc = require('../any_grpc');
var MathClient = grpc.load(
__dirname + '/../../packages/grpc-native-core/deps/grpc/src/proto/math/math.proto').math.Math;
@ -485,12 +485,10 @@ describe('Echo metadata', function() {
call.end();
});
it('shows the correct user-agent string', function(done) {
var version = require('../any_grpc').core.packageJson.all.version;
var version = require('../any_grpc')['$implementationInfo'].server.corePjson.version;
var call = client.unary({}, metadata,
function(err, data) { assert.ifError(err); });
call.on('metadata', function(metadata) {
console.log(version);
console.log(metadata.get('user-agent')[0]);
assert(_.startsWith(metadata.get('user-agent')[0],
'grpc-node/' + version));
done();

View File

@ -52,14 +52,11 @@ gulp.task('internal.test.test', 'Run API-level tests', () => {
.on('error', reject);
});
const apiTestGlob = `${apiTestDir}/*.js`;
const interopTestGlob = `${testDir}/interop/interop_sanity_test.js`;
const runTestsArgPairs = [
[apiTestGlob, 'native_native'],
// [apiTestGlob, 'js_js'],
[interopTestGlob, 'native_native'],
// [interopTestGlob, 'native_js'],
// [interopTestGlob, 'js_native'],
// [interopTestGlob, 'js_js']
// [apiTestGlob, 'native_js'],
// [apiTestGlob, 'js_native'],
// [apiTestGlob, 'js_js']
];
return runTestsArgPairs.reduce((previousPromise, argPair) => {
return previousPromise.then(runTestsWithFixture.bind(null, argPair[0], argPair[1]));

View File

@ -20,7 +20,7 @@
var fs = require('fs');
var path = require('path');
var grpc = require('../any_grpc').client;
var grpc = require('../any_grpc')['$implementationInfo'].client.surface;
var testProto = grpc.load({
root: __dirname + '/../../packages/grpc-native-core/deps/grpc',
file: 'src/proto/grpc/testing/test.proto'}).grpc.testing;

View File

@ -22,7 +22,7 @@ var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var AsyncDelayQueue = require('./async_delay_queue');
var grpc = require('../any_grpc').server;
var grpc = require('../any_grpc')['$implementationInfo'].server.surface;
var testProto = grpc.load({
root: __dirname + '/../../packages/grpc-native-core/deps/grpc',
file: 'src/proto/grpc/testing/test.proto'}).grpc.testing;