11package golang
22
33import (
4+ "fmt"
5+ "os"
6+ "strings"
47 "testing"
58
69 "archive/tar"
710 "bytes"
811 "compress/gzip"
912 "time"
1013
14+ "github.com/spf13/viper"
15+
16+ "github.com/hyperledger/fabric/core/config"
1117 pb "github.com/hyperledger/fabric/protos/peer"
1218)
1319
20+ func testerr (err error , succ bool ) error {
21+ if succ && err != nil {
22+ return fmt .Errorf ("Expected success but got %s" , err )
23+ } else if ! succ && err == nil {
24+ return fmt .Errorf ("Expected failer but succeeded" )
25+ }
26+ return nil
27+ }
28+
1429func writeBytesToPackage (name string , payload []byte , mode int64 , tw * tar.Writer ) error {
1530 //Make headers identical by using zero time
1631 var zeroTime time.Time
@@ -20,7 +35,7 @@ func writeBytesToPackage(name string, payload []byte, mode int64, tw *tar.Writer
2035 return nil
2136}
2237
23- func generateFakeCDS (path , file string , mode int64 ) (* pb.ChaincodeDeploymentSpec , error ) {
38+ func generateFakeCDS (ccname , path , file string , mode int64 ) (* pb.ChaincodeDeploymentSpec , error ) {
2439 codePackage := bytes .NewBuffer (nil )
2540 gw := gzip .NewWriter (codePackage )
2641 tw := tar .NewWriter (gw )
@@ -37,7 +52,7 @@ func generateFakeCDS(path, file string, mode int64) (*pb.ChaincodeDeploymentSpec
3752 cds := & pb.ChaincodeDeploymentSpec {
3853 ChaincodeSpec : & pb.ChaincodeSpec {
3954 ChaincodeId : & pb.ChaincodeID {
40- Name : "Bad Code" ,
55+ Name : ccname ,
4156 Path : path ,
4257 },
4358 },
@@ -48,21 +63,23 @@ func generateFakeCDS(path, file string, mode int64) (*pb.ChaincodeDeploymentSpec
4863}
4964
5065type spec struct {
66+ CCName string
5167 Path , File string
5268 Mode int64
5369 SuccessExpected bool
70+ RealGen bool
5471}
5572
5673func TestValidateCDS (t * testing.T ) {
5774 platform := & Platform {}
5875
5976 specs := make ([]spec , 0 )
60- specs = append (specs , spec {Path : "path/to/nowhere" , File : "/bin/warez" , Mode : 0100400 , SuccessExpected : false })
61- specs = append (specs , spec {Path : "path/to/somewhere" , File : "/src/path/to/somewhere/main.go" , Mode : 0100400 , SuccessExpected : true })
62- specs = append (specs , spec {Path : "path/to/somewhere" , File : "/src/path/to/somewhere/warez" , Mode : 0100555 , SuccessExpected : false })
77+ specs = append (specs , spec {CCName : "NoCode" , Path : "path/to/nowhere" , File : "/bin/warez" , Mode : 0100400 , SuccessExpected : false })
78+ specs = append (specs , spec {CCName : "NoCode" , Path : "path/to/somewhere" , File : "/src/path/to/somewhere/main.go" , Mode : 0100400 , SuccessExpected : true })
79+ specs = append (specs , spec {CCName : "NoCode" , Path : "path/to/somewhere" , File : "/src/path/to/somewhere/warez" , Mode : 0100555 , SuccessExpected : false })
6380
6481 for _ , s := range specs {
65- cds , err := generateFakeCDS (s .Path , s .File , s .Mode )
82+ cds , err := generateFakeCDS (s .CCName , s . Path , s .File , s .Mode )
6683
6784 err = platform .ValidateDeploymentSpec (cds )
6885 if s .SuccessExpected == true && err != nil {
@@ -123,3 +140,101 @@ func Test_decodeUrl(t *testing.T) {
123140 t .Logf ("Error to decodeUrl successfully with invalid path: %s" , cs .ChaincodeId .Path )
124141 }
125142}
143+
144+ func TestValidChaincodeSpec (t * testing.T ) {
145+ platform := & Platform {}
146+
147+ var tests = []struct {
148+ spec * pb.ChaincodeSpec
149+ succ bool
150+ }{
151+ {spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "http://github.com/hyperledger/fabric/examples/chaincode/go/map" }}, succ : true },
152+ {spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "https://github.com/hyperledger/fabric/examples/chaincode/go/map" }}, succ : true },
153+ {spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "github.com/hyperledger/fabric/examples/chaincode/go/map" }}, succ : true },
154+ {spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "github.com/hyperledger/fabric/bad/chaincode/go/map" }}, succ : false },
155+ }
156+
157+ for _ , tst := range tests {
158+ err := platform .ValidateSpec (tst .spec )
159+ if err = testerr (err , tst .succ ); err != nil {
160+ t .Errorf ("Error to validating chaincode spec: %s, %s" , tst .spec .ChaincodeId .Path , err )
161+ }
162+ }
163+ }
164+
165+ func TestGetDeploymentPayload (t * testing.T ) {
166+ platform := & Platform {}
167+
168+ var tests = []struct {
169+ spec * pb.ChaincodeSpec
170+ succ bool
171+ }{
172+ {spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "github.com/hyperledger/fabric/examples/chaincode/go/map" }}, succ : true },
173+ {spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "github.com/hyperledger/fabric/examples/bad/go/map" }}, succ : false },
174+ }
175+
176+ for _ , tst := range tests {
177+ _ , err := platform .GetDeploymentPayload (tst .spec )
178+ if err = testerr (err , tst .succ ); err != nil {
179+ t .Errorf ("Error to validating chaincode spec: %s, %s" , tst .spec .ChaincodeId .Path , err )
180+ }
181+ }
182+ }
183+
184+ //TestGenerateDockerBuild goes through the functions needed to do docker build
185+ func TestGenerateDockerBuild (t * testing.T ) {
186+ platform := & Platform {}
187+
188+ specs := make ([]spec , 0 )
189+ specs = append (specs , spec {CCName : "NoCode" , Path : "path/to/nowhere" , File : "/bin/warez" , Mode : 0100400 , SuccessExpected : false })
190+ specs = append (specs , spec {CCName : "invalidhttp" , Path : "https://not/a/valid/path" , File : "/src/github.com/hyperledger/fabric/examples/chaincode/go/map/map.go" , Mode : 0100400 , SuccessExpected : false , RealGen : true })
191+ specs = append (specs , spec {CCName : "map" , Path : "github.com/hyperledger/fabric/examples/chaincode/go/map" , File : "/src/github.com/hyperledger/fabric/examples/chaincode/go/map/map.go" , Mode : 0100400 , SuccessExpected : true , RealGen : true })
192+ specs = append (specs , spec {CCName : "mapBadPath" , Path : "github.com/hyperledger/fabric/examples/chaincode/go/map" , File : "/src/github.com/hyperledger/fabric/examples/bad/path/to/map.go" , Mode : 0100400 , SuccessExpected : false })
193+ specs = append (specs , spec {CCName : "mapBadMode" , Path : "github.com/hyperledger/fabric/examples/chaincode/go/map" , File : "/src/github.com/hyperledger/fabric/examples/chaincode/go/map/map.go" , Mode : 0100555 , SuccessExpected : false })
194+
195+ var err error
196+ for _ , tst := range specs {
197+ inputbuf := bytes .NewBuffer (nil )
198+ tw := tar .NewWriter (inputbuf )
199+
200+ var cds * pb.ChaincodeDeploymentSpec
201+ if tst .RealGen {
202+ cds = & pb.ChaincodeDeploymentSpec {
203+ ChaincodeSpec : & pb.ChaincodeSpec {
204+ ChaincodeId : & pb.ChaincodeID {
205+ Name : tst .CCName ,
206+ Path : tst .Path ,
207+ Version : "0" ,
208+ },
209+ },
210+ }
211+ cds .CodePackage , err = platform .GetDeploymentPayload (cds .ChaincodeSpec )
212+ if err = testerr (err , tst .SuccessExpected ); err != nil {
213+ t .Errorf ("test failed in GetDeploymentPayload: %s, %s" , cds .ChaincodeSpec .ChaincodeId .Path , err )
214+ }
215+ } else {
216+ cds , err = generateFakeCDS (tst .CCName , tst .Path , tst .File , tst .Mode )
217+ }
218+
219+ if _ , err = platform .GenerateDockerfile (cds ); err != nil {
220+ t .Errorf ("could not generate docker file for a valid spec: %s, %s" , cds .ChaincodeSpec .ChaincodeId .Path , err )
221+ }
222+ err = platform .GenerateDockerBuild (cds , tw )
223+ if err = testerr (err , tst .SuccessExpected ); err != nil {
224+ t .Errorf ("Error to validating chaincode spec: %s, %s" , cds .ChaincodeSpec .ChaincodeId .Path , err )
225+ }
226+ }
227+ }
228+
229+ func TestMain (m * testing.M ) {
230+ viper .SetConfigName ("core" )
231+ viper .SetEnvPrefix ("CORE" )
232+ config .AddDevConfigPath (nil )
233+ viper .SetEnvKeyReplacer (strings .NewReplacer ("." , "_" ))
234+ viper .AutomaticEnv ()
235+ if err := viper .ReadInConfig (); err != nil {
236+ fmt .Printf ("could not read config %s\n " , err )
237+ os .Exit (- 1 )
238+ }
239+ os .Exit (m .Run ())
240+ }
0 commit comments