Skip to content

Commit 388802e

Browse files
committed
[FAB-16745] Remove SDK from integration tests
- Remove the use of the SDK to setup fabric - Use the script.sh in the cli container for the channel, and cc install etc. - Use Java ProcessBuilder to then issue peer exec/peer chaincode invoke calls in the cli container - This change is remove dependency on the SDK and focus on the tests that do exist - Will move to Cucumber in due course Change-Id: I0a6529a0bf3e324e432622f8ada35a2d2ec18829 Signed-off-by: Matthew B. White <[email protected]>
1 parent b58f11d commit 388802e

File tree

17 files changed

+762
-796
lines changed

17 files changed

+762
-796
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ nbdist/
3737

3838
local-config.yaml
3939
gradle.properties
40-
.vscode/
40+
.vscode/
41+
42+
# The local repos for the chaincode
43+
44+
fabric-chaincode-example-sacc/repository
45+
fabric-chaincode-example-sbe/repository

fabric-chaincode-example-sbe/src/main/java/org/hyperledger/fabric/example/EndorsementCC.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class EndorsementCC extends ChaincodeBase {
3232
functions.put("getval", EndorsementCC.class.getMethod("getVal", ChaincodeStub.class));
3333
functions.put("cc2cc", EndorsementCC.class.getMethod("invokeCC", ChaincodeStub.class));
3434
} catch (NoSuchMethodException e) {
35+
e.printStackTrace();
3536
_logger.error(e);
3637
}
3738
}
@@ -44,6 +45,7 @@ public Response init(ChaincodeStub stub) {
4445
_logger.info("Init done");
4546
return newSuccessResponse();
4647
} catch (Throwable e) {
48+
e.printStackTrace();
4749
return newErrorResponse(e);
4850
}
4951
}
@@ -59,6 +61,7 @@ public Response invoke(ChaincodeStub stub) {
5961
}
6062
return newErrorResponse("Unknown function " + funcName);
6163
} catch (Throwable e) {
64+
e.printStackTrace();
6265
return newErrorResponse(e);
6366
}
6467
}
@@ -189,13 +192,16 @@ public Response setVal(ChaincodeStub stub) {
189192

190193
if ("pub".equals(parameters.get(0))) {
191194
stub.putStringState("pub", parameters.get(1));
195+
_logger.info("Put state "+parameters.get(1));
192196
} else if ("priv".equals(parameters.get(0))) {
193197
stub.putPrivateData("col", "priv", parameters.get(1));
198+
_logger.info("Put Private "+parameters.get(1));
194199
} else {
195200
return newErrorResponse("Unknown key specified");
196201
}
197202
return newSuccessResponse(new byte[]{});
198203
} catch (Throwable e) {
204+
e.printStackTrace();
199205
return newErrorResponse(e);
200206
}
201207
}
@@ -209,13 +215,16 @@ public Response getVal(ChaincodeStub stub) {
209215
}
210216

211217
if ("pub".equals(parameters.get(0))) {
218+
_logger.info(stub.getState("pub"));
212219
return newSuccessResponse(stub.getState("pub"));
213220
} else if ("priv".equals(parameters.get(0))) {
221+
_logger.info("get privateData" +stub.getPrivateData("col", "priv"));
214222
return newSuccessResponse(stub.getPrivateData("col", "priv"));
215223
} else {
216224
return newErrorResponse("Unknown key specified");
217225
}
218226
} catch (Throwable e) {
227+
e.printStackTrace();
219228
return newErrorResponse(e);
220229
}
221230
}

fabric-chaincode-integration-test/build.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,22 @@ dependencies {
33
testCompile 'org.testcontainers:testcontainers:1.7.1'
44
testCompile 'org.hyperledger.fabric-sdk-java:fabric-sdk-java:1.4.1'
55
compile project(':fabric-chaincode-shim')
6+
implementation group: 'org.json', name: 'json', version: '20180813'
67
}
78

89
build.dependsOn project(':fabric-chaincode-docker').buildImage
910

11+
test {
12+
// Always run tests, even when nothing changed.
13+
dependsOn 'cleanTest'
14+
15+
// Show test results.
16+
testLogging {
17+
events "passed", "skipped", "failed"
18+
showExceptions true
19+
showCauses true
20+
showStandardStreams true
21+
exceptionFormat "full"
22+
23+
}
24+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package org.hyperleder.fabric.shim.integration;
8+
9+
import java.io.BufferedReader;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.io.InputStreamReader;
13+
import java.io.PrintStream;
14+
import java.util.ArrayList;
15+
import java.util.HashMap;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.concurrent.CompletableFuture;
19+
import java.util.concurrent.ExecutionException;
20+
21+
public class Command {
22+
23+
protected List<String> cmd;
24+
protected Map<String, String> env;
25+
26+
Command(List<String> cmd) {
27+
this.cmd = cmd;
28+
this.env = new HashMap<>();
29+
30+
}
31+
32+
class Result {
33+
ArrayList<String> stdout;
34+
ArrayList<String> stderr;
35+
int exitcode;
36+
}
37+
38+
/**
39+
* Run but don't suppress the output being printed directly
40+
*/
41+
public Result run() {
42+
return this.run(false);
43+
}
44+
45+
/**
46+
* Run the command, and process the output to arrays for later parsing and checking
47+
*
48+
* @param quiet true if the output should NOT be printed directly to System.out/System.err
49+
*/
50+
public Result run(boolean quiet) {
51+
52+
ProcessBuilder processBuilder = new ProcessBuilder(cmd);
53+
processBuilder.environment().putAll(env);
54+
final Result result = new Result();
55+
56+
System.out.println("Running:" + this.toString());
57+
try {
58+
Process process = processBuilder.start();
59+
60+
CompletableFuture<ArrayList<String>> soutFut = readOutStream(process.getInputStream(),quiet?null:System.out);
61+
CompletableFuture<ArrayList<String>> serrFut = readOutStream(process.getErrorStream(),quiet?null:System.err);
62+
63+
CompletableFuture<Result> resultFut = soutFut.thenCombine(serrFut, (stdout, stderr) -> {
64+
// print to current stderr the stderr of process and return the stdout
65+
result.stderr = stderr;
66+
result.stdout = stdout;
67+
return result;
68+
});
69+
70+
result.exitcode = process.waitFor();
71+
// get stdout once ready, blocking
72+
resultFut.get();
73+
74+
} catch (IOException | InterruptedException | ExecutionException e) {
75+
e.printStackTrace();
76+
result.exitcode = -1;
77+
}
78+
79+
return result;
80+
}
81+
82+
/**
83+
* Collect the information from the executed process and add them to a result object
84+
*
85+
* @param is
86+
* @param stream
87+
* @return Completable Future with the array list of the stdout/sstderr
88+
*/
89+
CompletableFuture<ArrayList<String>> readOutStream(InputStream is, PrintStream stream) {
90+
return CompletableFuture.supplyAsync(() -> {
91+
try (InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr);) {
92+
ArrayList<String> res = new ArrayList<String>();
93+
String inputLine;
94+
while ((inputLine = br.readLine()) != null) {
95+
if (stream!=null) stream.println(inputLine);
96+
res.add(inputLine);
97+
}
98+
return res;
99+
} catch (Throwable e) {
100+
throw new RuntimeException("problem with executing program", e);
101+
}
102+
});
103+
}
104+
105+
public String toString() {
106+
return "[" + String.join(" ", cmd) + "]";
107+
}
108+
109+
static public class Builder<T extends Command> implements Cloneable {
110+
public Builder<T> duplicate() {
111+
try {
112+
return (Builder<T>) this.clone();
113+
} catch (CloneNotSupportedException e) {
114+
e.printStackTrace();
115+
return null;
116+
}
117+
}
118+
}
119+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
package org.hyperleder.fabric.shim.integration;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/** Represents the 'peer' cli command
11+
*
12+
*
13+
*
14+
*/
15+
public class Docker extends Command {
16+
17+
public static DockerBuilder newBuilder(){
18+
return new DockerBuilder();
19+
}
20+
21+
static public class DockerBuilder implements Cloneable {
22+
boolean exec;
23+
String container;
24+
String script;
25+
26+
public DockerBuilder duplicate() {
27+
try {
28+
return (DockerBuilder) this.clone();
29+
} catch (CloneNotSupportedException e) {
30+
e.printStackTrace();
31+
return null;
32+
}
33+
}
34+
35+
public DockerBuilder script(String script){
36+
this.script = script;
37+
return this;
38+
}
39+
40+
public DockerBuilder container(String container){
41+
this.container = container;
42+
return this;
43+
}
44+
45+
public DockerBuilder exec(){
46+
this.exec = true;
47+
return this;
48+
}
49+
public Docker build(){
50+
51+
ArrayList<String> list = new ArrayList<>();
52+
list.add("docker");
53+
if(exec){
54+
list.add("exec");
55+
56+
}
57+
58+
if (container == null || container.isEmpty()){
59+
throw new RuntimeException("container should be set");
60+
}
61+
list.add(container);
62+
63+
if (script == null || script.isEmpty()){
64+
throw new RuntimeException("script should be set");
65+
}
66+
list.add(script);
67+
68+
69+
return new Docker(list);
70+
}
71+
}
72+
73+
Docker(List<String> cmd) {
74+
super(cmd);
75+
}
76+
77+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
package org.hyperleder.fabric.shim.integration;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/** Represents the 'peer' cli command
11+
*
12+
*
13+
*
14+
*/
15+
public class DockerCompose extends Command {
16+
17+
public static DockerComposeBuilder newBuilder(){
18+
return new DockerComposeBuilder();
19+
}
20+
21+
static public class DockerComposeBuilder extends Command.Builder<DockerCompose>{
22+
String composeFile;
23+
24+
boolean up = true;
25+
boolean detach = false;
26+
27+
public DockerComposeBuilder file(String composeFile){
28+
this.composeFile = composeFile;
29+
return this;
30+
}
31+
32+
public DockerComposeBuilder duplicate() {
33+
return (DockerComposeBuilder) super.duplicate();
34+
}
35+
36+
public DockerComposeBuilder up(){
37+
this.up = true;
38+
return this;
39+
}
40+
41+
public DockerComposeBuilder detach(){
42+
this.detach = true;
43+
return this;
44+
}
45+
46+
public DockerComposeBuilder down(){
47+
this.up = false;
48+
return this;
49+
}
50+
51+
public DockerCompose build(){
52+
53+
ArrayList<String> list = new ArrayList<>();
54+
list.add("docker-compose");
55+
if (composeFile!=null && !composeFile.isEmpty()) {
56+
list.add("-f");
57+
list.add(composeFile);
58+
}
59+
list.add(up?"up":"down");
60+
if (detach){
61+
list.add("-d");
62+
}
63+
64+
65+
return new DockerCompose(list);
66+
}
67+
}
68+
69+
DockerCompose(List<String> cmd) {
70+
super(cmd);
71+
super.env.put("COMPOSE_PROJECT_NAME","first-network");
72+
}
73+
74+
}

0 commit comments

Comments
 (0)