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

Commit 1b231db

Browse files
committed
[FAB-8847] Use Reader in SaveChannel
This change replaces the filename in SaveChannel with an io.Reader for greater flexability. Change-Id: Icdd76ff0cea9e42ed958e31bb540bc99e00abd42 Signed-off-by: Troy Ronda <troy@troyronda.com>
1 parent 45ed5e9 commit 1b231db

File tree

11 files changed

+109
-44
lines changed

11 files changed

+109
-44
lines changed

pkg/client/resmgmt/opts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/pkg/errors"
1616
)
1717

18-
// WithTargetURLs allows overriding of the target peers for the request.
18+
// WithTargets allows overriding of the target peers for the request.
1919
func WithTargets(targets ...fab.Peer) RequestOption {
2020
return func(ctx context.Client, opts *requestOptions) error {
2121
opts.Targets = targets

pkg/client/resmgmt/resmgmt.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ SPDX-License-Identifier: Apache-2.0
88
package resmgmt
99

1010
import (
11+
"io"
1112
"io/ioutil"
1213
"math/rand"
1314
"time"
@@ -83,12 +84,10 @@ type requestOptions struct {
8384

8485
//SaveChannelRequest used to save channel request
8586
type SaveChannelRequest struct {
86-
// Channel Name (ID)
87-
ChannelID string
88-
// Path to channel configuration file
89-
ChannelConfig string
90-
// Users that sign channel configuration
91-
SigningIdentities []msp.Identity
87+
ChannelID string
88+
ChannelConfig io.Reader // ChannelConfig data source
89+
SigningIdentities []msp.Identity // Users that sign channel configuration
90+
// TODO: support pre-signed signature blocks
9291
}
9392

9493
//RequestOption func for each Opts argument
@@ -597,11 +596,11 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
597596
return err
598597
}
599598

600-
if req.ChannelID == "" || req.ChannelConfig == "" {
599+
if req.ChannelID == "" || req.ChannelConfig == nil {
601600
return errors.New("must provide channel ID and channel config")
602601
}
603602

604-
logger.Debugf("***** Saving channel: %s *****\n", req.ChannelID)
603+
logger.Debugf("saving channel: %s", req.ChannelID)
605604

606605
// Signing user has to belong to one of configured channel organisations
607606
// In case that order org is one of channel orgs we can use context user
@@ -619,7 +618,7 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
619618
return errors.New("must provide signing user")
620619
}
621620

622-
configTx, err := ioutil.ReadFile(req.ChannelConfig)
621+
configTx, err := ioutil.ReadAll(req.ChannelConfig)
623622
if err != nil {
624623
return errors.WithMessage(err, "reading channel config file failed")
625624
}

pkg/client/resmgmt/resmgmt_test.go

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"net"
1212
"net/http"
13+
"os"
1314
"strings"
1415
"testing"
1516
"time"
@@ -1343,32 +1344,44 @@ func TestSaveChannelSuccess(t *testing.T) {
13431344
t.Fatalf("Should have failed for empty channel request")
13441345
}
13451346

1347+
r, err := os.Open(channelConfig)
1348+
assert.Nil(t, err, "opening channel config file failed")
1349+
defer r.Close()
1350+
13461351
// Test empty channel name
1347-
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "", ChannelConfig: channelConfig})
1352+
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "", ChannelConfig: r})
13481353
if err == nil {
13491354
t.Fatalf("Should have failed for empty channel id")
13501355
}
13511356

13521357
// Test empty channel config
1353-
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: ""})
1358+
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel"})
13541359
if err == nil {
13551360
t.Fatalf("Should have failed for empty channel config")
13561361
}
13571362

13581363
// Test extract configuration error
1359-
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: "./testdata/extractcherr.tx"})
1364+
r1, err := os.Open("./testdata/extractcherr.tx")
1365+
assert.Nil(t, err, "opening channel config file failed")
1366+
defer r1.Close()
1367+
1368+
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r1})
13601369
if err == nil {
13611370
t.Fatalf("Should have failed to extract configuration")
13621371
}
13631372

13641373
// Test sign channel error
1365-
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: "./testdata/signcherr.tx"})
1374+
r2, err := os.Open("./testdata/signcherr.tx")
1375+
assert.Nil(t, err, "opening channel config file failed")
1376+
defer r2.Close()
1377+
1378+
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r2})
13661379
if err == nil {
13671380
t.Fatalf("Should have failed to sign configuration")
13681381
}
13691382

13701383
// Test valid Save Channel request (success)
1371-
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: channelConfig}, WithOrdererURL("example.com"))
1384+
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r}, WithOrdererURL("example.com"))
13721385
if err != nil {
13731386
t.Fatal(err)
13741387
}
@@ -1395,7 +1408,11 @@ func TestSaveChannelFailure(t *testing.T) {
13951408
}
13961409

13971410
// Test create channel failure
1398-
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "Invalid", ChannelConfig: channelConfig})
1411+
r, err := os.Open(channelConfig)
1412+
assert.Nil(t, err, "opening channel config file failed")
1413+
defer r.Close()
1414+
1415+
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "Invalid", ChannelConfig: r})
13991416
if err == nil {
14001417
t.Fatal("Should have failed with create channel error")
14011418
}
@@ -1425,23 +1442,38 @@ func TestSaveChannelWithOpts(t *testing.T) {
14251442
cc := setupResMgmtClient(ctx, nil, t)
14261443

14271444
// Valid request (same for all options)
1428-
req := SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: channelConfig}
1445+
r1, err := os.Open(channelConfig)
1446+
assert.Nil(t, err, "opening channel config file failed")
1447+
defer r1.Close()
1448+
1449+
req := SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r1}
14291450

14301451
// Test empty option (default order is random orderer from config)
14311452
opts := WithOrdererURL("")
1432-
err := cc.SaveChannel(req, opts)
1453+
err = cc.SaveChannel(req, opts)
14331454
if err != nil {
14341455
t.Fatal(err)
14351456
}
14361457

14371458
// Test valid orderer ID
1459+
r2, err := os.Open(channelConfig)
1460+
assert.Nil(t, err, "opening channel config file failed")
1461+
defer r2.Close()
1462+
1463+
req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r2}
1464+
14381465
opts = WithOrdererURL("orderer.example.com")
14391466
err = cc.SaveChannel(req, opts)
14401467
if err != nil {
14411468
t.Fatal(err)
14421469
}
14431470

14441471
// Test invalid orderer ID
1472+
r3, err := os.Open(channelConfig)
1473+
assert.Nil(t, err, "opening channel config file failed")
1474+
defer r3.Close()
1475+
1476+
req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r3}
14451477

14461478
mockConfig = &fcmocks.MockConfig{}
14471479
ctx.SetConfig(mockConfig)
@@ -1487,15 +1519,23 @@ func TestSaveChannelWithMultipleSigningIdenities(t *testing.T) {
14871519
cc := setupResMgmtClient(ctx, nil, t)
14881520

14891521
// empty list of signing identities (defaults to context user)
1490-
req := SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: channelConfig, SigningIdentities: []msp.Identity{}}
1491-
err := cc.SaveChannel(req, WithOrdererURL(""))
1522+
r1, err := os.Open(channelConfig)
1523+
assert.Nil(t, err, "opening channel config file failed")
1524+
defer r1.Close()
1525+
1526+
req := SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r1, SigningIdentities: []msp.Identity{}}
1527+
err = cc.SaveChannel(req, WithOrdererURL(""))
14921528
if err != nil {
14931529
t.Fatalf("Failed to save channel with default signing identity: %s", err)
14941530
}
14951531

14961532
// multiple signing identities
1533+
r2, err := os.Open(channelConfig)
1534+
assert.Nil(t, err, "opening channel config file failed")
1535+
defer r2.Close()
1536+
14971537
secondCtx := fcmocks.NewMockContext(fcmocks.NewMockUser("second"))
1498-
req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: channelConfig, SigningIdentities: []msp.Identity{cc.ctx, secondCtx}}
1538+
req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r2, SigningIdentities: []msp.Identity{cc.ctx, secondCtx}}
14991539
err = cc.SaveChannel(req, WithOrdererURL(""))
15001540
if err != nil {
15011541
t.Fatalf("Failed to save channel with multiple signing identities: %s", err)

test/integration/base_test_setup.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import (
2222

2323
// BaseSetupImpl implementation of BaseTestSetup
2424
type BaseSetupImpl struct {
25-
Identity msp.Identity
26-
Targets []string
27-
ConfigFile string
28-
OrgID string
29-
ChannelID string
30-
ChannelConfig string
25+
Identity msp.Identity
26+
Targets []string
27+
ConfigFile string
28+
OrgID string
29+
ChannelID string
30+
ChannelConfigFile string
3131
}
3232

3333
// Initial B values for ExampleCC
@@ -80,8 +80,14 @@ func (setup *BaseSetupImpl) Initialize(sdk *fabsdk.FabricSDK) error {
8080
}
8181
setup.Targets = targets
8282

83+
r, err := os.Open(setup.ChannelConfigFile)
84+
if err != nil {
85+
return errors.Wrapf(err, "opening channel config file failed")
86+
}
87+
defer r.Close()
88+
8389
// Create channel for tests
84-
req := resmgmt.SaveChannelRequest{ChannelID: setup.ChannelID, ChannelConfig: setup.ChannelConfig, SigningIdentities: []msp.Identity{adminIdentity}}
90+
req := resmgmt.SaveChannelRequest{ChannelID: setup.ChannelID, ChannelConfig: r, SigningIdentities: []msp.Identity{adminIdentity}}
8591
if err = InitializeChannel(sdk, setup.OrgID, req, targets); err != nil {
8692
return errors.WithMessage(err, "failed to initialize channel")
8793
}

test/integration/e2e/end_to_end.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
77
package e2e
88

99
import (
10+
"os"
1011
"path"
1112
"strconv"
1213
"testing"
@@ -67,8 +68,14 @@ func Run(t *testing.T, configOpt core.ConfigProvider, sdkOpts ...fabsdk.Option)
6768
t.Fatal(err)
6869
}
6970

71+
ccReader, err := os.Open(path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"))
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
defer ccReader.Close()
76+
7077
req := resmgmt.SaveChannelRequest{ChannelID: channelID,
71-
ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"),
78+
ChannelConfig: ccReader,
7279
SigningIdentities: []msp.Identity{adminIdentity}}
7380
if err = resMgmtClient.SaveChannel(req); err != nil {
7481
t.Fatal(err)

test/integration/fab/channel_ledger_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
77
package fab
88

99
import (
10+
"os"
1011
"path"
1112
"strconv"
1213
"testing"
@@ -50,8 +51,13 @@ func initializeLedgerTests(t *testing.T) (*fabsdk.FabricSDK, []string) {
5051
t.Fatalf("creating peers failed: %v", err)
5152
}
5253

53-
channelConfig := path.Join("../../../", metadata.ChannelConfigPath, channelConfigFile)
54-
req := resmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: channelConfig, SigningIdentities: []msp.Identity{adminIdentity}}
54+
ccReader, err := os.Open(path.Join("../../../", metadata.ChannelConfigPath, channelConfigFile))
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
defer ccReader.Close()
59+
60+
req := resmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: ccReader, SigningIdentities: []msp.Identity{adminIdentity}}
5561
err = integration.InitializeChannel(sdk, orgName, req, targets)
5662
if err != nil {
5763
t.Fatalf("failed to ensure channel has been initialized: %s", err)

test/integration/fab/main_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ func setup() {
3636
testSetup := integration.BaseSetupImpl{
3737
ConfigFile: "../" + integration.ConfigTestFile,
3838

39-
ChannelID: "mychannel",
40-
OrgID: org1Name,
41-
ChannelConfig: path.Join("../../", metadata.ChannelConfigPath, "mychannel.tx"),
39+
ChannelID: "mychannel",
40+
OrgID: org1Name,
41+
ChannelConfigFile: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"),
4242
}
4343

4444
sdk, err := fabsdk.New(config.FromFile(testSetup.ConfigFile))

test/integration/msp/user_data_mgmt_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
// TestWithCustomStores demonstrates the usage of custom key and cert stores
2828
// to manage user private keys and certificates.
2929
func TestWithCustomStores(t *testing.T) {
30-
config, err := configImpl.FromFile(sdkConfigFile)()
30+
config, err := configImpl.FromFile("../" + integration.ConfigTestFile)()
3131
if err != nil {
3232
t.Fatalf("Unexpected error from config: %v", err)
3333
}
@@ -45,7 +45,7 @@ func TestWithCustomStores(t *testing.T) {
4545

4646
//
4747
// NOTE: BCCSP SW implementation currently doesn't allow
48-
// writting private keys out. The file store used internally
48+
// writing private keys out. The file store used internally
4949
// by BCCSP has access to provate parts that are not available
5050
// outside of BCCSP at the moment. Fot this reason, our
5151
// example custom kay store will just hold the keys in memory.

test/integration/orgs/multiple_orgs_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package orgs
88

99
import (
1010
"math"
11+
"os"
1112
"path"
1213
"strconv"
1314
"strings"
@@ -96,8 +97,14 @@ func testWithOrg1(t *testing.T, sdk *fabsdk.FabricSDK) int {
9697
}
9798

9899
// Create channel (or update if it already exists)
100+
ccReader, err := os.Open(path.Join("../../../", metadata.ChannelConfigPath, "orgchannel.tx"))
101+
if err != nil {
102+
t.Fatal(err)
103+
}
104+
defer ccReader.Close()
105+
99106
req := resmgmt.SaveChannelRequest{ChannelID: "orgchannel",
100-
ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "orgchannel.tx"),
107+
ChannelConfig: ccReader,
101108
SigningIdentities: []msp.Identity{org1AdminUser, org2AdminUser}}
102109
if err = chMgmtClient.SaveChannel(req); err != nil {
103110
t.Fatal(err)

test/integration/sdk/channel_config_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ func TestChannelConfig(t *testing.T) {
8989
func TestChannelConfigWithOrderer(t *testing.T) {
9090

9191
testSetup := integration.BaseSetupImpl{
92-
ConfigFile: "../" + integration.ConfigTestFile,
93-
ChannelID: "mychannel",
94-
OrgID: org1Name,
95-
ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"),
92+
ConfigFile: "../" + integration.ConfigTestFile,
93+
ChannelID: "mychannel",
94+
OrgID: org1Name,
95+
ChannelConfigFile: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"),
9696
}
9797

9898
confProvider := config.FromFile(testSetup.ConfigFile)

0 commit comments

Comments
 (0)