forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
end_to_end.go
229 lines (188 loc) · 7.82 KB
/
end_to_end.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package e2e
import (
"strconv"
"testing"
"time"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
"github.com/hyperledger/fabric-sdk-go/test/metadata"
"github.com/stretchr/testify/require"
"github.com/hyperledger/fabric-sdk-go/test/integration"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
mspclient "github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
packager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/gopackager"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
)
const (
channelID = "mychannel"
orgName = "Org1"
orgAdmin = "Admin"
ordererOrgName = "OrdererOrg"
)
var (
ccID = "example_cc_e2e" + metadata.TestRunID
)
// Run enables testing an end-to-end scenario against the supplied SDK options
func Run(t *testing.T, configOpt core.ConfigProvider, sdkOpts ...fabsdk.Option) {
setupAndRun(t, true, configOpt, e2eTest, sdkOpts...)
}
// RunWithoutSetup will execute the same way as Run but without creating a new channel and registering a new CC
func RunWithoutSetup(t *testing.T, configOpt core.ConfigProvider, sdkOpts ...fabsdk.Option) {
setupAndRun(t, false, configOpt, e2eTest, sdkOpts...)
}
type testSDKFunc func(t *testing.T, sdk *fabsdk.FabricSDK)
// setupAndRun enables testing an end-to-end scenario against the supplied SDK options
// 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)
func setupAndRun(t *testing.T, createChannel bool, configOpt core.ConfigProvider, test testSDKFunc, sdkOpts ...fabsdk.Option) {
if integration.IsLocal() {
//If it is a local test then add entity mapping to config backend to parse URLs
configOpt = integration.AddLocalEntityMapping(configOpt)
}
sdk, err := fabsdk.New(configOpt, sdkOpts...)
if err != nil {
t.Fatalf("Failed to create new SDK: %s", err)
}
defer sdk.Close()
// Delete all private keys from the crypto suite store
// and users from the user store at the end
integration.CleanupUserData(t, sdk)
defer integration.CleanupUserData(t, sdk)
if createChannel {
createChannelAndCC(t, sdk)
}
test(t, sdk)
}
func e2eTest(t *testing.T, sdk *fabsdk.FabricSDK) {
//prepare channel client context using client context
clientChannelContext := sdk.ChannelContext(channelID, fabsdk.WithUser("User1"), fabsdk.WithOrg(orgName))
// Channel client is used to query and execute transactions (Org1 is default org)
client, err := channel.New(clientChannelContext)
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}
existingValue := queryCC(t, client)
ccEvent := moveFunds(t, client)
// Verify move funds transaction result on the same peer where the event came from.
verifyFundsIsMoved(t, client, existingValue, ccEvent)
}
func createChannelAndCC(t *testing.T, sdk *fabsdk.FabricSDK) {
//clientContext allows creation of transactions using the supplied identity as the credential.
clientContext := sdk.Context(fabsdk.WithUser(orgAdmin), fabsdk.WithOrg(ordererOrgName))
// Resource management client is responsible for managing channels (create/update channel)
// Supply user that has privileges to create channel (in this case orderer admin)
resMgmtClient, err := resmgmt.New(clientContext)
if err != nil {
t.Fatalf("Failed to create channel management client: %s", err)
}
// Create channel
createChannel(t, sdk, resMgmtClient)
//prepare context
adminContext := sdk.Context(fabsdk.WithUser(orgAdmin), fabsdk.WithOrg(orgName))
// Org resource management client
orgResMgmt, err := resmgmt.New(adminContext)
if err != nil {
t.Fatalf("Failed to create new resource management client: %s", err)
}
// Org peers join channel
if err = orgResMgmt.JoinChannel(channelID, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint("orderer.example.com")); err != nil {
t.Fatalf("Org peers failed to JoinChannel: %s", err)
}
// Create chaincode package for example cc
createCC(t, orgResMgmt)
}
func moveFunds(t *testing.T, client *channel.Client) *fab.CCEvent {
eventID := "test([a-zA-Z]+)"
// Register chaincode event (pass in channel which receives event details when the event is complete)
reg, notifier, err := client.RegisterChaincodeEvent(ccID, eventID)
if err != nil {
t.Fatalf("Failed to register cc event: %s", err)
}
defer client.UnregisterChaincodeEvent(reg)
// Move funds
executeCC(t, client)
var ccEvent *fab.CCEvent
select {
case ccEvent = <-notifier:
t.Logf("Received CC event: %#v\n", ccEvent)
case <-time.After(time.Second * 20):
t.Fatalf("Did NOT receive CC event for eventId(%s)\n", eventID)
}
return ccEvent
}
func verifyFundsIsMoved(t *testing.T, client *channel.Client, value []byte, ccEvent *fab.CCEvent) {
newValue := queryCC(t, client, ccEvent.SourceURL)
valueInt, err := strconv.Atoi(string(value))
if err != nil {
t.Fatal(err.Error())
}
valueAfterInvokeInt, err := strconv.Atoi(string(newValue))
if err != nil {
t.Fatal(err.Error())
}
if valueInt+1 != valueAfterInvokeInt {
t.Fatalf("Execute failed. Before: %s, after: %s", value, newValue)
}
}
func executeCC(t *testing.T, client *channel.Client) {
_, err := client.Execute(channel.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCDefaultTxArgs()},
channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to move funds: %s", err)
}
}
func queryCC(t *testing.T, client *channel.Client, targetEndpoints ...string) []byte {
response, err := client.Query(channel.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCDefaultQueryArgs()},
channel.WithRetry(retry.DefaultChannelOpts),
channel.WithTargetEndpoints(targetEndpoints...),
)
if err != nil {
t.Fatalf("Failed to query funds: %s", err)
}
return response.Payload
}
func createCC(t *testing.T, orgResMgmt *resmgmt.Client) {
ccPkg, err := packager.NewCCPackage("github.com/example_cc", integration.GetDeployPath())
if err != nil {
t.Fatal(err)
}
// Install example cc to org peers
installCCReq := resmgmt.InstallCCRequest{Name: ccID, Path: "github.com/example_cc", Version: "0", Package: ccPkg}
_, err = orgResMgmt.InstallCC(installCCReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts))
if err != nil {
t.Fatal(err)
}
// Set up chaincode policy
ccPolicy := cauthdsl.SignedByAnyMember([]string{"Org1MSP"})
// Org resource manager will instantiate 'example_cc' on channel
resp, err := orgResMgmt.InstantiateCC(
channelID,
resmgmt.InstantiateCCRequest{Name: ccID, Path: "github.com/example_cc", Version: "0", Args: integration.ExampleCCInitArgs(), Policy: ccPolicy},
resmgmt.WithRetry(retry.DefaultResMgmtOpts),
)
require.Nil(t, err, "error should be nil")
require.NotEmpty(t, resp, "transaction response should be populated")
}
func createChannel(t *testing.T, sdk *fabsdk.FabricSDK, resMgmtClient *resmgmt.Client) {
mspClient, err := mspclient.New(sdk.Context(), mspclient.WithOrg(orgName))
if err != nil {
t.Fatal(err)
}
adminIdentity, err := mspClient.GetSigningIdentity(orgAdmin)
if err != nil {
t.Fatal(err)
}
req := resmgmt.SaveChannelRequest{ChannelID: channelID,
ChannelConfigPath: integration.GetChannelConfigPath(channelID + ".tx"),
SigningIdentities: []msp.SigningIdentity{adminIdentity}}
txID, err := resMgmtClient.SaveChannel(req, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint("orderer.example.com"))
require.Nil(t, err, "error should be nil")
require.NotEmpty(t, txID, "transaction ID should be populated")
}