@@ -44,19 +44,21 @@ var (
4444
4545// Run enables testing an end-to-end scenario against the supplied SDK options
4646func Run (t * testing.T , configOpt core.ConfigProvider , sdkOpts ... fabsdk.Option ) {
47- setupAndRun (t , true , configOpt , sdkOpts ... )
47+ setupAndRun (t , true , configOpt , e2eTest , sdkOpts ... )
4848}
4949
5050// RunWithoutSetup will execute the same way as Run but without creating a new channel and registering a new CC
5151func RunWithoutSetup (t * testing.T , configOpt core.ConfigProvider , sdkOpts ... fabsdk.Option ) {
52- setupAndRun (t , false , configOpt , sdkOpts ... )
52+ setupAndRun (t , false , configOpt , e2eTest , sdkOpts ... )
5353}
5454
55+ type testSDKFunc func (t * testing.T , sdk * fabsdk.FabricSDK )
56+
5557// setupAndRun enables testing an end-to-end scenario against the supplied SDK options
56- // the doSetup flag will be used to either create a channel and the example CC or not(ie run the tests with existing ch and CC)
57- func setupAndRun (t * testing.T , doSetup bool , configOpt core.ConfigProvider , sdkOpts ... fabsdk.Option ) {
58+ // the createChannel flag will be used to either create a channel and the example CC or not(ie run the tests with existing ch and CC)
59+ func setupAndRun (t * testing.T , createChannel bool , configOpt core.ConfigProvider , test testSDKFunc , sdkOpts ... fabsdk.Option ) {
5860
59- if integration .IsLocal () && doSetup {
61+ if integration .IsLocal () {
6062 //If it is a local test then add entity mapping to config backend to parse URLs
6163 configOpt = integration .AddLocalEntityMapping (configOpt )
6264 }
@@ -72,12 +74,14 @@ func setupAndRun(t *testing.T, doSetup bool, configOpt core.ConfigProvider, sdkO
7274 integration .CleanupUserData (t , sdk )
7375 defer integration .CleanupUserData (t , sdk )
7476
75- if doSetup {
77+ if createChannel {
7678 createChannelAndCC (t , sdk )
7779 }
7880
79- // ************ Test setup complete ************** //
81+ test (t , sdk )
82+ }
8083
84+ func e2eTest (t * testing.T , sdk * fabsdk.FabricSDK ) {
8185 //prepare channel client context using client context
8286 clientChannelContext := sdk .ChannelContext (channelID , fabsdk .WithUser ("User1" ), fabsdk .WithOrg (orgName ))
8387 // Channel client is used to query and execute transactions (Org1 is default org)
@@ -86,31 +90,11 @@ func setupAndRun(t *testing.T, doSetup bool, configOpt core.ConfigProvider, sdkO
8690 t .Fatalf ("Failed to create new channel client: %s" , err )
8791 }
8892
89- value := queryCC (client , t )
90-
91- eventID := "test([a-zA-Z]+)"
92-
93- // Register chaincode event (pass in channel which receives event details when the event is complete)
94- reg , notifier , err := client .RegisterChaincodeEvent (ccID , eventID )
95- if err != nil {
96- t .Fatalf ("Failed to register cc event: %s" , err )
97- }
98- defer client .UnregisterChaincodeEvent (reg )
99-
100- // Move funds
101- executeCC (client , t )
102-
103- var ccEvent * fab.CCEvent
104- select {
105- case ccEvent = <- notifier :
106- t .Logf ("Received CC event: %#v\n " , ccEvent )
107- case <- time .After (time .Second * 20 ):
108- t .Fatalf ("Did NOT receive CC event for eventId(%s)\n " , eventID )
109- }
93+ existingValue := queryCC (t , client )
94+ ccEvent := moveFunds (t , client )
11095
11196 // Verify move funds transaction result on the same peer where the event came from.
112- verifyFundsIsMoved (client , t , value , ccEvent )
113-
97+ verifyFundsIsMoved (t , client , existingValue , ccEvent )
11498}
11599
116100func createChannelAndCC (t * testing.T , sdk * fabsdk.FabricSDK ) {
@@ -125,10 +109,7 @@ func createChannelAndCC(t *testing.T, sdk *fabsdk.FabricSDK) {
125109 }
126110
127111 // Create channel
128-
129- // Org admin user is signing user for creating channel
130-
131- createChannel (sdk , t , resMgmtClient )
112+ createChannel (t , sdk , resMgmtClient )
132113
133114 //prepare context
134115 adminContext := sdk .Context (fabsdk .WithUser (orgAdmin ), fabsdk .WithOrg (orgName ))
@@ -148,15 +129,40 @@ func createChannelAndCC(t *testing.T, sdk *fabsdk.FabricSDK) {
148129 createCC (t , orgResMgmt )
149130}
150131
151- func verifyFundsIsMoved (client * channel.Client , t * testing.T , value []byte , ccevent * fab.CCEvent ) {
132+ func moveFunds (t * testing.T , client * channel.Client ) * fab.CCEvent {
133+
134+ eventID := "test([a-zA-Z]+)"
135+
136+ // Register chaincode event (pass in channel which receives event details when the event is complete)
137+ reg , notifier , err := client .RegisterChaincodeEvent (ccID , eventID )
138+ if err != nil {
139+ t .Fatalf ("Failed to register cc event: %s" , err )
140+ }
141+ defer client .UnregisterChaincodeEvent (reg )
142+
143+ // Move funds
144+ executeCC (t , client )
145+
146+ var ccEvent * fab.CCEvent
147+ select {
148+ case ccEvent = <- notifier :
149+ t .Logf ("Received CC event: %#v\n " , ccEvent )
150+ case <- time .After (time .Second * 20 ):
151+ t .Fatalf ("Did NOT receive CC event for eventId(%s)\n " , eventID )
152+ }
153+
154+ return ccEvent
155+ }
156+
157+ func verifyFundsIsMoved (t * testing.T , client * channel.Client , value []byte , ccEvent * fab.CCEvent ) {
152158
153159 //Fix for issue prev in release test, where 'ccEvent.SourceURL' has event URL
154160 if ! integration .IsLocal () {
155- portIndex := strings .Index (ccevent .SourceURL , ":" )
156- ccevent .SourceURL = ccevent .SourceURL [0 :portIndex ]
161+ portIndex := strings .Index (ccEvent .SourceURL , ":" )
162+ ccEvent .SourceURL = ccEvent .SourceURL [0 :portIndex ]
157163 }
158164
159- newValue := queryCC (client , t , ccevent .SourceURL )
165+ newValue := queryCC (t , client , ccEvent .SourceURL )
160166 valueInt , err := strconv .Atoi (string (value ))
161167 if err != nil {
162168 t .Fatal (err .Error ())
@@ -170,15 +176,15 @@ func verifyFundsIsMoved(client *channel.Client, t *testing.T, value []byte, ccev
170176 }
171177}
172178
173- func executeCC (client * channel. Client , t * testing. T ) {
179+ func executeCC (t * testing. T , client * channel. Client ) {
174180 _ , err := client .Execute (channel.Request {ChaincodeID : ccID , Fcn : "invoke" , Args : integration .ExampleCCTxArgs ()},
175181 channel .WithRetry (retry .DefaultChannelOpts ))
176182 if err != nil {
177183 t .Fatalf ("Failed to move funds: %s" , err )
178184 }
179185}
180186
181- func queryCC (client * channel. Client , t * testing. T , targetEndpoints ... string ) []byte {
187+ func queryCC (t * testing. T , client * channel. Client , targetEndpoints ... string ) []byte {
182188 response , err := client .Query (channel.Request {ChaincodeID : ccID , Fcn : "invoke" , Args : integration .ExampleCCQueryArgs ()},
183189 channel .WithRetry (retry .DefaultChannelOpts ),
184190 channel .WithTargetEndpoints (targetEndpoints ... ),
@@ -212,7 +218,7 @@ func createCC(t *testing.T, orgResMgmt *resmgmt.Client) {
212218 require .NotEmpty (t , resp , "transaction response should be populated" )
213219}
214220
215- func createChannel (sdk * fabsdk. FabricSDK , t * testing. T , resMgmtClient * resmgmt.Client ) {
221+ func createChannel (t * testing. T , sdk * fabsdk. FabricSDK , resMgmtClient * resmgmt.Client ) {
216222 mspClient , err := mspclient .New (sdk .Context (), mspclient .WithOrg (orgName ))
217223 if err != nil {
218224 t .Fatal (err )
0 commit comments