Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 779a8c1

Browse files
committed
[FABG-708] No orderer e2e: verify from same peer
This change updates the no orderer test to verify from the same peer that delivered the CC event. The tests are also refactored to allow reuse of code since the original e2e test was already doing this. Change-Id: I32e5cd309b57639a023d79f8d9743a3be2bc3c2b Signed-off-by: Troy Ronda <troy@troyronda.com>
1 parent cacc1a7 commit 779a8c1

File tree

2 files changed

+62
-102
lines changed

2 files changed

+62
-102
lines changed

test/integration/e2e/end_to_end.go

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,21 @@ var (
4444

4545
// Run enables testing an end-to-end scenario against the supplied SDK options
4646
func 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
5151
func 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

116100
func 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)

test/integration/e2e/no_orderer_config_test.go

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ SPDX-License-Identifier: Apache-2.0
77
package e2e
88

99
import (
10-
"strconv"
1110
"testing"
12-
"time"
1311

1412
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
1513
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
@@ -21,88 +19,44 @@ import (
2119

2220
// runWithNoOrdererConfig enables chclient scenarios using config and client options provided
2321
func runWithNoOrdererConfig(t *testing.T, configOpt core.ConfigProvider, sdkOpts ...fabsdk.Option) {
22+
setupAndRun(t, false, configOpt, noOrdererE2ETest, sdkOpts...)
23+
}
2424

25-
if integration.IsLocal() {
26-
//If it is a local test then add entity mapping to config backend to parse URLs
27-
configOpt = integration.AddLocalEntityMapping(configOpt)
28-
}
29-
30-
sdk, err := fabsdk.New(configOpt, sdkOpts...)
31-
if err != nil {
32-
t.Fatalf("Failed to create new SDK: %s", err)
33-
}
34-
defer sdk.Close()
35-
36-
// Delete all private keys from the crypto suite store
37-
// and users from the user store at the end
38-
integration.CleanupUserData(t, sdk)
39-
defer integration.CleanupUserData(t, sdk)
40-
41-
// ************ Test setup complete ************** //
42-
43-
//TODO : discovery filter should be fixed
44-
discoveryFilter := &mockDiscoveryFilter{called: false}
45-
25+
func noOrdererE2ETest(t *testing.T, sdk *fabsdk.FabricSDK) {
4626
//prepare channel client context using client context
4727
clientChannelContext := sdk.ChannelContext(channelID, fabsdk.WithUser("User1"), fabsdk.WithOrg(orgName))
4828

4929
// Channel client is used to query and execute transactions (Org1 is default org)
5030
client, err := channel.New(clientChannelContext)
51-
5231
if err != nil {
5332
t.Fatalf("Failed to create new channel client: %s", err)
5433
}
5534

35+
value := queryCCUsingTargetFilter(t, client)
36+
37+
// Move and verify funds
38+
ccEvent := moveFunds(t, client)
39+
verifyFundsIsMoved(t, client, value, ccEvent)
40+
}
41+
42+
func queryCCUsingTargetFilter(t *testing.T, client *channel.Client) []byte {
43+
//TODO : discovery filter should be fixed
44+
discoveryFilter := &mockDiscoveryFilter{called: false}
45+
5646
response, err := client.Query(
5747
channel.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()},
5848
channel.WithTargetFilter(discoveryFilter),
5949
channel.WithRetry(retry.DefaultChannelOpts))
6050
if err != nil {
6151
t.Fatalf("Failed to query funds: %s", err)
6252
}
63-
value := response.Payload
6453

6554
//Test if discovery filter is being called
6655
if !discoveryFilter.called {
6756
t.Fatal("discoveryFilter not called")
6857
}
6958

70-
eventID := "test([a-zA-Z]+)"
71-
72-
// Register chaincode event (pass in channel which receives event details when the event is complete)
73-
reg, notifier, err := client.RegisterChaincodeEvent(ccID, eventID)
74-
if err != nil {
75-
t.Fatalf("Failed to register cc event: %s", err)
76-
}
77-
defer client.UnregisterChaincodeEvent(reg)
78-
79-
// Move funds
80-
moveFunds(response, client, t, notifier, eventID, value)
81-
}
82-
83-
func moveFunds(response channel.Response, client *channel.Client, t *testing.T, notifier <-chan *fab.CCEvent, eventID string, value []byte) {
84-
response, err := client.Execute(channel.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCTxArgs()},
85-
channel.WithRetry(retry.DefaultChannelOpts))
86-
if err != nil {
87-
t.Fatalf("Failed to move funds: %s", err)
88-
}
89-
select {
90-
case ccEvent := <-notifier:
91-
t.Logf("Received CC event: %#v\n", ccEvent)
92-
case <-time.After(time.Second * 20):
93-
t.Fatalf("Did NOT receive CC event for eventId(%s)\n", eventID)
94-
}
95-
// Verify move funds transaction result
96-
response, err = client.Query(channel.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()},
97-
channel.WithRetry(retry.DefaultChannelOpts))
98-
if err != nil {
99-
t.Fatalf("Failed to query funds after transaction: %s", err)
100-
}
101-
valueInt, _ := strconv.Atoi(string(value))
102-
valueAfterInvokeInt, _ := strconv.Atoi(string(response.Payload))
103-
if valueInt+1 != valueAfterInvokeInt {
104-
t.Fatalf("Execute failed. Before: %s, after: %s", value, response.Payload)
105-
}
59+
return response.Payload
10660
}
10761

10862
type mockDiscoveryFilter struct {

0 commit comments

Comments
 (0)