-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathTestConfig.java
452 lines (358 loc) · 18.9 KB
/
TestConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* Copyright 2016, 2017 IBM, DTCC, Fujitsu Australia Software Technology, IBM - All Rights Reserved.
*
* 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.
*/
package org.hyperledger.fabric.sdk.testutils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hyperledger.fabric.sdk.helper.Utils;
import org.hyperledger.fabric.sdkintegration.SampleOrg;
/**
* Config allows for a global config of the toolkit. Central location for all
* toolkit configuration defaults. Has a local config file that can override any
* property defaults. Config file can be relocated via a system property
* "org.hyperledger.fabric.sdk.configuration". Any property can be overridden
* with environment variable and then overridden
* with a java system property. Property hierarchy goes System property
* overrides environment variable which overrides config file for default values specified here.
*/
/**
* Test Configuration
*/
public class TestConfig {
private static final Log logger = LogFactory.getLog(TestConfig.class);
private static final String DEFAULT_CONFIG = "src/test/java/org/hyperledger/fabric/sdk/testutils.properties";
private static final String ORG_HYPERLEDGER_FABRIC_SDK_CONFIGURATION = "org.hyperledger.fabric.sdktest.configuration";
private static final String ORG_HYPERLEDGER_FABRIC_SDK_TEST_FABRIC_HOST = "ORG_HYPERLEDGER_FABRIC_SDK_TEST_FABRIC_HOST";
private static final String LOCALHOST = //Change test to reference another host .. easier config for my testing on Windows !
// System.getenv(ORG_HYPERLEDGER_FABRIC_SDK_TEST_FABRIC_HOST) == null ? "localhost" : System.getenv(ORG_HYPERLEDGER_FABRIC_SDK_TEST_FABRIC_HOST);
"49.4.14.76";
private static final String PROPBASE = "org.hyperledger.fabric.sdktest.";
private static final String INVOKEWAITTIME = PROPBASE + "InvokeWaitTime";
private static final String DEPLOYWAITTIME = PROPBASE + "DeployWaitTime";
private static final String PROPOSALWAITTIME = PROPBASE + "ProposalWaitTime";
private static final String INTEGRATIONTESTS_ORG = PROPBASE + "integrationTests.org.";
private static final Pattern orgPat = Pattern.compile("^" + Pattern.quote(INTEGRATIONTESTS_ORG) + "([^\\.]+)\\.mspid$");
private static final String INTEGRATIONTESTSTLS = PROPBASE + "integrationtests.tls";
// location switching between fabric cryptogen and configtxgen artifacts for v1.0 and v1.1 in src/test/fixture/sdkintegration/e2e-2Orgs
public static final String FAB_CONFIG_GEN_VERS =
"v1.0";
// Objects.equals(System.getenv("ORG_HYPERLEDGER_FABRIC_SDKTEST_VERSION"), "1.0.0") ? "v1.0" : "v1.1";
private static TestConfig config;
private static final Properties sdkProperties = new Properties();
private final boolean runningTLS;
private final boolean runningFabricCATLS;
public boolean isRunningFabricTLS() {
return runningFabricTLS;
}
private final boolean runningFabricTLS;
private static final HashMap<String, SampleOrg> sampleOrgs = new HashMap<>();
private TestConfig() {
File loadFile;
FileInputStream configProps;
try {
loadFile = new File(System.getProperty(ORG_HYPERLEDGER_FABRIC_SDK_CONFIGURATION, DEFAULT_CONFIG))
.getAbsoluteFile();
logger.debug(String.format("Loading configuration from %s and it is present: %b", loadFile.toString(),
loadFile.exists()));
configProps = new FileInputStream(loadFile);
sdkProperties.load(configProps);
} catch (IOException e) { // if not there no worries just use defaults
// logger.warn(String.format("Failed to load any test configuration from: %s. Using toolkit defaults",
// DEFAULT_CONFIG));
} finally {
// Default values
defaultProperty(INVOKEWAITTIME, "120");
defaultProperty(DEPLOYWAITTIME, "120000");
defaultProperty(PROPOSALWAITTIME, "120000");
//////
defaultProperty(INTEGRATIONTESTS_ORG + "peerOrg1.mspid", "ea24fef7f9427f8086859fad278c7748e316b24cMSP");
defaultProperty(INTEGRATIONTESTS_ORG + "peerOrg1.domname", "org1.example.com");
defaultProperty(INTEGRATIONTESTS_ORG + "peerOrg1.ca_location", "http://" + LOCALHOST + ":30605");
defaultProperty(INTEGRATIONTESTS_ORG + "peerOrg1.caName", "ca0");
defaultProperty(INTEGRATIONTESTS_ORG + "peerOrg1.peer_locations", "peer0.org1.example.com@grpcs://" +
LOCALHOST + ":30605, peer1.org1.example.com@grpcs://" + LOCALHOST + ":30606");
defaultProperty(INTEGRATIONTESTS_ORG + "peerOrg1.orderer_locations", "orderer.example.com@grpcs://" +
LOCALHOST + ":30805");
defaultProperty(INTEGRATIONTESTS_ORG + "peerOrg1.eventhub_locations", "peer0.org1.example.com@grpcs://" +
LOCALHOST + ":30705,peer1.org1.example.com@grpcs://" + LOCALHOST + ":30706");
defaultProperty(INTEGRATIONTESTS_ORG + "peerOrg2.mspid", "Org2MSP");
defaultProperty(INTEGRATIONTESTS_ORG + "peerOrg2.domname", "org2.example.com");
defaultProperty(INTEGRATIONTESTS_ORG + "peerOrg2.ca_location", "http://" + LOCALHOST + ":8054");
defaultProperty(INTEGRATIONTESTS_ORG + "peerOrg2.peer_locations", "peer0.org2.example.com@grpcs://" +
LOCALHOST + ":30610,peer1.org2.example.com@grpcs://" + LOCALHOST + ":30611");
defaultProperty(INTEGRATIONTESTS_ORG + "peerOrg2.orderer_locations", "orderer.example.com@grpcs://" +
LOCALHOST + ":30805");
defaultProperty(INTEGRATIONTESTS_ORG + "peerOrg2.eventhub_locations", "peer0.org2.example.com@grpcs://" +
LOCALHOST + ":30710, peer1.org2.example.com@grpcs://" + LOCALHOST + ":30711");
defaultProperty(INTEGRATIONTESTSTLS, null);
runningTLS = null != sdkProperties.getProperty(INTEGRATIONTESTSTLS, null);
runningFabricCATLS = runningTLS;
runningFabricTLS = runningTLS;
for (Map.Entry<Object, Object> x : sdkProperties.entrySet()) {
final String key = x.getKey() + "";
final String val = x.getValue() + "";
if (key.startsWith(INTEGRATIONTESTS_ORG)) {
Matcher match = orgPat.matcher(key);
if (match.matches() && match.groupCount() == 1) {
String orgName = match.group(1).trim();
sampleOrgs.put(orgName, new SampleOrg(orgName, val.trim()));
}
}
}
for (Map.Entry<String, SampleOrg> org : sampleOrgs.entrySet()) {
final SampleOrg sampleOrg = org.getValue();
final String orgName = org.getKey();
String peerNames = sdkProperties.getProperty(INTEGRATIONTESTS_ORG + orgName + ".peer_locations");
String[] ps = peerNames.split("[ \t]*,[ \t]*");
for (String peer : ps) {
String[] nl = peer.split("[ \t]*@[ \t]*");
if(nl[0].equals("peer0.org1.example.com")){
nl[0]="peer-ea24fef7f9427f8086859fad278c7748e316b24c-0.peer" +
"-ea24fef7f9427f8086859fad278c7748e316b24c.default.svc.cluster.local";
}
if(nl[0].equals("peer1.org1.example.com")){
nl[0]="peer-ea24fef7f9427f8086859fad278c7748e316b24c-1.peer" +
"-ea24fef7f9427f8086859fad278c7748e316b24c.default.svc.cluster.local";
}
if(nl[0].equals("peer0.org2.example.com")){
nl[0]="peer-0fd932b876c28c30cbe967409e0114a984e4aa5b-0.peer" +
"-0fd932b876c28c30cbe967409e0114a984e4aa5b.default.svc.cluster.local";
}
if(nl[0].equals("peer1.org2.example.com")){
nl[0]="peer-0fd932b876c28c30cbe967409e0114a984e4aa5b-1.peer" +
"-0fd932b876c28c30cbe967409e0114a984e4aa5b.default.svc.cluster.local";
}
sampleOrg.addPeerLocation(nl[0], grpcTLSify(nl[1]));
}
final String domainName = sdkProperties.getProperty(INTEGRATIONTESTS_ORG + orgName + ".domname");
sampleOrg.setDomainName(domainName);
String ordererNames = sdkProperties.getProperty(INTEGRATIONTESTS_ORG + orgName + ".orderer_locations");
ps = ordererNames.split("[ \t]*,[ \t]*");
for (String peer : ps) {
String[] nl = peer.split("[ \t]*@[ \t]*");
if(nl[0].equals("orderer.example.com")){
nl[0]="orderer-8c7e908584a3d49065390cda14c0b35334c6862d-0.orderer" +
"-8c7e908584a3d49065390cda14c0b35334c6862d.default.svc.cluster.local";
}
sampleOrg.addOrdererLocation(nl[0], grpcTLSify(nl[1]));
}
String eventHubNames = sdkProperties.getProperty(INTEGRATIONTESTS_ORG + orgName + ".eventhub_locations");
ps = eventHubNames.split("[ \t]*,[ \t]*");
for (String peer : ps) {
String[] nl = peer.split("[ \t]*@[ \t]*");
if(nl[0].equals("peer0.org1.example.com")){
nl[0]="peer-ea24fef7f9427f8086859fad278c7748e316b24c-0.peer" +
"-ea24fef7f9427f8086859fad278c7748e316b24c.default.svc.cluster.local";
}
if(nl[0].equals("peer1.org1.example.com")){
nl[0]="peer-ea24fef7f9427f8086859fad278c7748e316b24c-1.peer" +
"-ea24fef7f9427f8086859fad278c7748e316b24c.default.svc.cluster.local";
}
if(nl[0].equals("peer0.org2.example.com")){
nl[0]="peer-0fd932b876c28c30cbe967409e0114a984e4aa5b-0.peer" +
"-0fd932b876c28c30cbe967409e0114a984e4aa5b.default.svc.cluster.local";
}
if(nl[0].equals("peer1.org2.example.com")){
nl[0]="peer-0fd932b876c28c30cbe967409e0114a984e4aa5b-1.peer" +
"-0fd932b876c28c30cbe967409e0114a984e4aa5b.default.svc.cluster.local";
}
sampleOrg.addEventHubLocation(nl[0], grpcTLSify(nl[1]));
}
sampleOrg.setCALocation(httpTLSify(sdkProperties.getProperty((INTEGRATIONTESTS_ORG + org.getKey() + ".ca_location"))));
sampleOrg.setCAName(sdkProperties.getProperty((INTEGRATIONTESTS_ORG + org.getKey() + ".caName")));
if (runningFabricCATLS) {
String cert = "src/test/fixture/sdkintegration/e2e-2Orgs/FAB_CONFIG_GEN_VERS/crypto-config/peerOrganizations/DNAME/ca/ca.DNAME-cert.pem"
.replaceAll("DNAME", domainName).replaceAll("FAB_CONFIG_GEN_VERS", FAB_CONFIG_GEN_VERS);
File cf = new File(cert);
if (!cf.exists() || !cf.isFile()) {
throw new RuntimeException("TEST is missing cert file " + cf.getAbsolutePath());
}
Properties properties = new Properties();
properties.setProperty("pemFile", cf.getAbsolutePath());
properties.setProperty("allowAllHostNames", "true"); //testing environment only NOT FOR PRODUCTION!
sampleOrg.setCAProperties(properties);
}
}
}
}
private String grpcTLSify(String location) {
location = location.trim();
Exception e = Utils.checkGrpcUrl(location);
if (e != null) {
throw new RuntimeException(String.format("Bad TEST parameters for grpc url %s", location), e);
}
return runningFabricTLS ?
location.replaceFirst("^grpc://", "grpcs://") : location;
}
private String httpTLSify(String location) {
location = location.trim();
return runningFabricCATLS ?
location.replaceFirst("^http://", "https://") : location;
}
/**
* getConfig return back singleton for SDK configuration.
*
* @return Global configuration
*/
public static TestConfig getConfig() {
if (null == config) {
config = new TestConfig();
}
return config;
}
/**
* getProperty return back property for the given value.
*
* @param property
* @return String value for the property
*/
private String getProperty(String property) {
String ret = sdkProperties.getProperty(property);
if (null == ret) {
logger.warn(String.format("No configuration value found for '%s'", property));
}
return ret;
}
private static void defaultProperty(String key, String value) {
String ret = System.getProperty(key);
if (ret != null) {
sdkProperties.put(key, ret);
} else {
String envKey = key.toUpperCase().replaceAll("\\.", "_");
ret = System.getenv(envKey);
if (null != ret) {
sdkProperties.put(key, ret);
} else {
if (null == sdkProperties.getProperty(key) && value != null) {
sdkProperties.put(key, value);
}
}
}
}
public int getTransactionWaitTime() {
return Integer.parseInt(getProperty(INVOKEWAITTIME));
}
public int getDeployWaitTime() {
return Integer.parseInt(getProperty(DEPLOYWAITTIME));
}
public long getProposalWaitTime() {
return Integer.parseInt(getProperty(PROPOSALWAITTIME));
}
public Collection<SampleOrg> getIntegrationTestsSampleOrgs() {
return Collections.unmodifiableCollection(sampleOrgs.values());
}
public SampleOrg getIntegrationTestsSampleOrg(String name) {
return sampleOrgs.get(name);
}
public Properties getPeerProperties(String name) {
return getEndPointProperties("peer", name);
}
public Properties getOrdererProperties(String name) {
return getEndPointProperties("orderer", name);
}
private Properties getEndPointProperties(final String type, final String name) {
final String domainName = getDomainName(name);
File cert = Paths.get(getTestChannelPath(), "crypto-config/ordererOrganizations".replace("orderer", type), domainName, type + "s",
name, "tls/ca.crt").toFile();
if (!cert.exists()) {
throw new RuntimeException(String.format("Missing cert file for: %s. Could not find at location: %s", name,
cert.getAbsolutePath()));
}
Properties ret = new Properties();
ret.setProperty("pemFile", cert.getAbsolutePath());
// ret.setProperty("trustServerCertificate", "true"); //testing environment only NOT FOR PRODUCTION!
ret.setProperty("hostnameOverride", name);
ret.setProperty("sslProvider", "openSSL");
ret.setProperty("negotiationType", "TLS");
return ret;
}
public Properties getEventHubProperties(String name) {
return getEndPointProperties("peer", name); //uses same as named peer
}
public String getTestChannelPath() {
return "src/test/fixture/sdkintegration/e2e-2Orgs/" + FAB_CONFIG_GEN_VERS;
}
public boolean isRunningAgainstFabric10() {
return "IntegrationSuiteV1.java".equals(System.getProperty("org.hyperledger.fabric.sdktest.ITSuite"));
}
/**
* url location of configtxlator
*
* @return
*/
public String getFabricConfigTxLaterLocation() {
return "http://" + LOCALHOST + ":7059";
}
/**
* Returns the appropriate Network Config YAML file based on whether TLS is currently
* enabled or not
*
* @return The appropriate Network Config YAML file
*/
public File getTestNetworkConfigFileYAML() {
String fname = runningTLS ? "network-config-tls.yaml" : "network-config.yaml";
String pname = "src/test/fixture/sdkintegration/network_configs/";
File ret = new File(pname, fname);
if (!"localhost".equals(LOCALHOST)) {
// change on the fly ...
File temp = null;
try {
//create a temp file
temp = File.createTempFile(fname, "-FixedUp.yaml");
if (temp.exists()) { //For testing start fresh
temp.delete();
}
byte[] data = Files.readAllBytes(Paths.get(ret.getAbsolutePath()));
String sourceText = new String(data, StandardCharsets.UTF_8);
sourceText = sourceText.replaceAll("https://localhost", "https://" + LOCALHOST);
sourceText = sourceText.replaceAll("http://localhost", "http://" + LOCALHOST);
sourceText = sourceText.replaceAll("grpcs://localhost", "grpcs://" + LOCALHOST);
sourceText = sourceText.replaceAll("grpc://localhost", "grpc://" + LOCALHOST);
Files.write(Paths.get(temp.getAbsolutePath()), sourceText.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.CREATE_NEW, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
if (!Objects.equals("true", System.getenv(ORG_HYPERLEDGER_FABRIC_SDK_TEST_FABRIC_HOST + "_KEEP"))) {
temp.deleteOnExit();
} else {
System.err.println("produced new network-config.yaml file at:" + temp.getAbsolutePath());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
ret = temp;
}
return ret;
}
private String getDomainName(final String name) {
int dot = name.indexOf(".");
if (-1 == dot) {
return null;
} else {
return name.substring(dot + 1);
}
}
}