|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 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 | +package java_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "archive/tar" |
| 21 | + "bytes" |
| 22 | + "compress/gzip" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/hyperledger/fabric/core/chaincode/platforms/java" |
| 26 | + pb "github.com/hyperledger/fabric/protos/peer" |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | +) |
| 29 | + |
| 30 | +var chaincodePath = "testdata/SimpleSample" |
| 31 | +var spec = &pb.ChaincodeSpec{ |
| 32 | + Type: pb.ChaincodeSpec_JAVA, |
| 33 | + ChaincodeId: &pb.ChaincodeID{ |
| 34 | + Name: "ssample", |
| 35 | + Path: chaincodePath}, |
| 36 | + Input: &pb.ChaincodeInput{ |
| 37 | + Args: [][]byte{[]byte("f")}}} |
| 38 | + |
| 39 | +var expected = ` |
| 40 | +ADD codepackage.tgz /root/chaincode |
| 41 | +RUN cd /root/chaincode/src && gradle -b build.gradle clean && gradle -b build.gradle build |
| 42 | +RUN cp /root/chaincode/src/build/chaincode.jar /root |
| 43 | +RUN cp /root/chaincode/src/build/libs/* /root/libs` |
| 44 | + |
| 45 | +func TestValidateSpec(t *testing.T) { |
| 46 | + platform := java.Platform{} |
| 47 | + |
| 48 | + err := platform.ValidateSpec(spec) |
| 49 | + assert.NoError(t, err) |
| 50 | +} |
| 51 | + |
| 52 | +func TestValidateDeploymentSpec(t *testing.T) { |
| 53 | + platform := java.Platform{} |
| 54 | + err := platform.ValidateSpec(spec) |
| 55 | + assert.NoError(t, err) |
| 56 | +} |
| 57 | + |
| 58 | +func TestGetDeploymentPayload(t *testing.T) { |
| 59 | + platform := java.Platform{} |
| 60 | + spec.ChaincodeId.Path = "pathdoesnotexist" |
| 61 | + |
| 62 | + _, err := platform.GetDeploymentPayload(spec) |
| 63 | + assert.Contains(t, err.Error(), "code does not exist") |
| 64 | + |
| 65 | + spec.ChaincodeId.Path = chaincodePath |
| 66 | + payload, err := platform.GetDeploymentPayload(spec) |
| 67 | + assert.NoError(t, err) |
| 68 | + assert.NotZero(t, len(payload)) |
| 69 | +} |
| 70 | + |
| 71 | +func TestGenerateDockerfile(t *testing.T) { |
| 72 | + platform := java.Platform{} |
| 73 | + cds := &pb.ChaincodeDeploymentSpec{ |
| 74 | + CodePackage: []byte{}} |
| 75 | + |
| 76 | + _, err := platform.GenerateDockerfile(cds) |
| 77 | + assert.Error(t, err) |
| 78 | + |
| 79 | + spec.ChaincodeId.Path = chaincodePath |
| 80 | + payload, err := platform.GetDeploymentPayload(spec) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("failed to get Java CC payload: %s", err) |
| 83 | + } |
| 84 | + cds.CodePackage = payload |
| 85 | + |
| 86 | + dockerfile, err := platform.GenerateDockerfile(cds) |
| 87 | + assert.NoError(t, err) |
| 88 | + assert.Equal(t, expected, dockerfile) |
| 89 | +} |
| 90 | + |
| 91 | +func TestGenerateDockerBuild(t *testing.T) { |
| 92 | + platform := java.Platform{} |
| 93 | + cds := &pb.ChaincodeDeploymentSpec{ |
| 94 | + CodePackage: []byte{}} |
| 95 | + tw := tar.NewWriter(gzip.NewWriter(bytes.NewBuffer(nil))) |
| 96 | + |
| 97 | + err := platform.GenerateDockerBuild(cds, tw) |
| 98 | + assert.NoError(t, err) |
| 99 | +} |
0 commit comments