Skip to content

Commit 4e9dd2b

Browse files
rennmanMr. Angry
authored and
Mr. Angry
committed
Utility to assist in writing Node SDK Unit tests
Adding a utility file that will simplify the process of writing and adding Node SDK unit tests. Additionally, this file allows the user to run tests against different environments without modifying the unit test files, by instead modifying a set of environment variables. Change-Id: I7a2a2d714024584ab91417cbb8f83b5e365ab182 Signed-off-by: Allen E Bailey <[email protected]>
1 parent c5a9b14 commit 4e9dd2b

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

sdk/node/test/unit/test-util.js

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Copyright 2016 IBM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
var hfc = require('hfc');
18+
var util = require('util');
19+
var fs = require('fs');
20+
21+
//Set defaults, if not set
22+
var deployMode = process.env.SDK_DEPLOY_MODE
23+
? process.env.SDK_DEPLOY_MODE
24+
: "net" ;
25+
var keyStore = process.env.SDK_KEYSTORE
26+
? process.env.SDK_KEYSTORE
27+
: "/tmp/keyValStore" ;
28+
var caCert = process.env.SDK_CA_CERT_FILE
29+
? process.env.SDK_CA_CERT_FILE
30+
: "tlsca.cert" ;
31+
var caCertHost = process.env.SDK_CA_CERT_HOST
32+
? process.env.SDK_CA_CERT_HOST
33+
: "" ;
34+
var caAddr = process.env.SDK_MEMBERSRVC_ADDRESS
35+
? process.env.SDK_MEMBERSRVC_ADDRESS
36+
: "localhost:7054" ;
37+
var peerAddr0 = process.env.SDK_PEER_ADDRESS
38+
? process.env.SDK_PEER_ADDRESS
39+
: "localhost:7051" ;
40+
var tlsOn = ( process.env.SDK_TLS == null )
41+
? Boolean(false)
42+
: Boolean(parseInt(process.env.SDK_TLS));
43+
var deployWait = process.env.SDK_DEPLOYWAIT
44+
? process.env.SDK_DEPLOYWAIT
45+
: 20;
46+
var invokeWait = process.env.SDK_INVOKEWAIT
47+
? process.env.SDK_INVOKEWAIT
48+
: 5;
49+
var ciphers = process.env.GRPC_SSL_CIPHER_SUITES
50+
51+
console.log("deployMode :"+deployMode );
52+
console.log("keyStore :"+keyStore );
53+
console.log("caCert :"+caCert );
54+
console.log("caAddr :"+caAddr );
55+
console.log("peerAddr0 :"+peerAddr0 );
56+
console.log("tlsOn :"+tlsOn );
57+
console.log("deployWait :"+deployWait );
58+
console.log("invokeWait :"+invokeWait );
59+
console.log("ciphers :"+ciphers );
60+
61+
function getTestChain(name) {
62+
name = name || "testChain";
63+
var chain = hfc.newChain(name);
64+
65+
chain.setKeyValStore(hfc.newFileKeyValStore(keyStore));
66+
if (tlsOn) {
67+
if (fs.existsSync(caCert)) {
68+
var pem = fs.readFileSync(caCert);
69+
if (caCertHost) { var grpcOpts={ pem:pem, hostnameOverride:'tlsca' } }
70+
else { var grpcOpts={ pem:pem } };
71+
72+
console.log("Setting membersrvc address to grpcs://" + caAddr);
73+
console.log("Setting peer address to grpcs://" + peerAddr0);
74+
console.log("Setting cert to " + caCert);
75+
76+
chain.setMemberServicesUrl("grpcs://" + caAddr, { pem:pem, hostnameOverride:'tlsca' } );
77+
chain.addPeer("grpcs://" + peerAddr0, { pem:pem, hostnameOverride:'tlsca' } );
78+
} else {
79+
console.log("TLS was requested but " + caCert + " not found.")
80+
process.exit(1)
81+
}
82+
} else {
83+
console.log("Setting membersrvc address to grpc://" + caAddr);
84+
console.log("Setting peer address to grpc://" + peerAddr0);
85+
chain.setMemberServicesUrl("grpc://" + caAddr);
86+
chain.addPeer("grpc://" + peerAddr0);
87+
}
88+
//
89+
// Set the chaincode deployment mode to either developent mode (user runs chaincode)
90+
// or network mode (code package built and sent to the peer).
91+
console.log("$SDK_DEPLOY_MODE: " + deployMode);
92+
if (deployMode === 'dev') {
93+
chain.setDevMode(true);
94+
} else {
95+
chain.setDevMode(false);
96+
}
97+
chain.setDeployWaitTime(parseInt(deployWait));
98+
chain.setInvokeWaitTime(parseInt(invokeWait));
99+
return chain;
100+
}
101+
102+
exports.getTestChain = getTestChain;
103+
exports.deployMode = deployMode
104+
exports.keyStore = keyStore
105+
exports.caCert = caCert
106+
exports.caAddr = caAddr
107+
exports.peerAddr0 = peerAddr0
108+
exports.tlsOn = tlsOn
109+
exports.deployWait = deployWait
110+
exports.invokeWait = invokeWait
111+
exports.ciphers = ciphers

0 commit comments

Comments
 (0)