Skip to content

Commit

Permalink
[FAB-8847] Use Reader in SaveChannel
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
troyronda committed Mar 13, 2018
1 parent 45ed5e9 commit 1b231db
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pkg/client/resmgmt/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/pkg/errors"
)

// WithTargetURLs allows overriding of the target peers for the request.
// WithTargets allows overriding of the target peers for the request.
func WithTargets(targets ...fab.Peer) RequestOption {
return func(ctx context.Client, opts *requestOptions) error {
opts.Targets = targets
Expand Down
17 changes: 8 additions & 9 deletions pkg/client/resmgmt/resmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SPDX-License-Identifier: Apache-2.0
package resmgmt

import (
"io"
"io/ioutil"
"math/rand"
"time"
Expand Down Expand Up @@ -83,12 +84,10 @@ type requestOptions struct {

//SaveChannelRequest used to save channel request
type SaveChannelRequest struct {
// Channel Name (ID)
ChannelID string
// Path to channel configuration file
ChannelConfig string
// Users that sign channel configuration
SigningIdentities []msp.Identity
ChannelID string
ChannelConfig io.Reader // ChannelConfig data source
SigningIdentities []msp.Identity // Users that sign channel configuration
// TODO: support pre-signed signature blocks
}

//RequestOption func for each Opts argument
Expand Down Expand Up @@ -597,11 +596,11 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
return err
}

if req.ChannelID == "" || req.ChannelConfig == "" {
if req.ChannelID == "" || req.ChannelConfig == nil {
return errors.New("must provide channel ID and channel config")
}

logger.Debugf("***** Saving channel: %s *****\n", req.ChannelID)
logger.Debugf("saving channel: %s", req.ChannelID)

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

configTx, err := ioutil.ReadFile(req.ChannelConfig)
configTx, err := ioutil.ReadAll(req.ChannelConfig)
if err != nil {
return errors.WithMessage(err, "reading channel config file failed")
}
Expand Down
62 changes: 51 additions & 11 deletions pkg/client/resmgmt/resmgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"net"
"net/http"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -1343,32 +1344,44 @@ func TestSaveChannelSuccess(t *testing.T) {
t.Fatalf("Should have failed for empty channel request")
}

r, err := os.Open(channelConfig)
assert.Nil(t, err, "opening channel config file failed")
defer r.Close()

// Test empty channel name
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "", ChannelConfig: channelConfig})
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "", ChannelConfig: r})
if err == nil {
t.Fatalf("Should have failed for empty channel id")
}

// Test empty channel config
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: ""})
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel"})
if err == nil {
t.Fatalf("Should have failed for empty channel config")
}

// Test extract configuration error
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: "./testdata/extractcherr.tx"})
r1, err := os.Open("./testdata/extractcherr.tx")
assert.Nil(t, err, "opening channel config file failed")
defer r1.Close()

err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r1})
if err == nil {
t.Fatalf("Should have failed to extract configuration")
}

// Test sign channel error
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: "./testdata/signcherr.tx"})
r2, err := os.Open("./testdata/signcherr.tx")
assert.Nil(t, err, "opening channel config file failed")
defer r2.Close()

err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r2})
if err == nil {
t.Fatalf("Should have failed to sign configuration")
}

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

// Test create channel failure
err = cc.SaveChannel(SaveChannelRequest{ChannelID: "Invalid", ChannelConfig: channelConfig})
r, err := os.Open(channelConfig)
assert.Nil(t, err, "opening channel config file failed")
defer r.Close()

err = cc.SaveChannel(SaveChannelRequest{ChannelID: "Invalid", ChannelConfig: r})
if err == nil {
t.Fatal("Should have failed with create channel error")
}
Expand Down Expand Up @@ -1425,23 +1442,38 @@ func TestSaveChannelWithOpts(t *testing.T) {
cc := setupResMgmtClient(ctx, nil, t)

// Valid request (same for all options)
req := SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: channelConfig}
r1, err := os.Open(channelConfig)
assert.Nil(t, err, "opening channel config file failed")
defer r1.Close()

req := SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r1}

// Test empty option (default order is random orderer from config)
opts := WithOrdererURL("")
err := cc.SaveChannel(req, opts)
err = cc.SaveChannel(req, opts)
if err != nil {
t.Fatal(err)
}

// Test valid orderer ID
r2, err := os.Open(channelConfig)
assert.Nil(t, err, "opening channel config file failed")
defer r2.Close()

req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r2}

opts = WithOrdererURL("orderer.example.com")
err = cc.SaveChannel(req, opts)
if err != nil {
t.Fatal(err)
}

// Test invalid orderer ID
r3, err := os.Open(channelConfig)
assert.Nil(t, err, "opening channel config file failed")
defer r3.Close()

req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r3}

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

// empty list of signing identities (defaults to context user)
req := SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: channelConfig, SigningIdentities: []msp.Identity{}}
err := cc.SaveChannel(req, WithOrdererURL(""))
r1, err := os.Open(channelConfig)
assert.Nil(t, err, "opening channel config file failed")
defer r1.Close()

req := SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r1, SigningIdentities: []msp.Identity{}}
err = cc.SaveChannel(req, WithOrdererURL(""))
if err != nil {
t.Fatalf("Failed to save channel with default signing identity: %s", err)
}

// multiple signing identities
r2, err := os.Open(channelConfig)
assert.Nil(t, err, "opening channel config file failed")
defer r2.Close()

secondCtx := fcmocks.NewMockContext(fcmocks.NewMockUser("second"))
req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: channelConfig, SigningIdentities: []msp.Identity{cc.ctx, secondCtx}}
req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r2, SigningIdentities: []msp.Identity{cc.ctx, secondCtx}}
err = cc.SaveChannel(req, WithOrdererURL(""))
if err != nil {
t.Fatalf("Failed to save channel with multiple signing identities: %s", err)
Expand Down
20 changes: 13 additions & 7 deletions test/integration/base_test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (

// BaseSetupImpl implementation of BaseTestSetup
type BaseSetupImpl struct {
Identity msp.Identity
Targets []string
ConfigFile string
OrgID string
ChannelID string
ChannelConfig string
Identity msp.Identity
Targets []string
ConfigFile string
OrgID string
ChannelID string
ChannelConfigFile string
}

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

r, err := os.Open(setup.ChannelConfigFile)
if err != nil {
return errors.Wrapf(err, "opening channel config file failed")
}
defer r.Close()

// Create channel for tests
req := resmgmt.SaveChannelRequest{ChannelID: setup.ChannelID, ChannelConfig: setup.ChannelConfig, SigningIdentities: []msp.Identity{adminIdentity}}
req := resmgmt.SaveChannelRequest{ChannelID: setup.ChannelID, ChannelConfig: r, SigningIdentities: []msp.Identity{adminIdentity}}
if err = InitializeChannel(sdk, setup.OrgID, req, targets); err != nil {
return errors.WithMessage(err, "failed to initialize channel")
}
Expand Down
9 changes: 8 additions & 1 deletion test/integration/e2e/end_to_end.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package e2e

import (
"os"
"path"
"strconv"
"testing"
Expand Down Expand Up @@ -67,8 +68,14 @@ func Run(t *testing.T, configOpt core.ConfigProvider, sdkOpts ...fabsdk.Option)
t.Fatal(err)
}

ccReader, err := os.Open(path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"))
if err != nil {
t.Fatal(err)
}
defer ccReader.Close()

req := resmgmt.SaveChannelRequest{ChannelID: channelID,
ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"),
ChannelConfig: ccReader,
SigningIdentities: []msp.Identity{adminIdentity}}
if err = resMgmtClient.SaveChannel(req); err != nil {
t.Fatal(err)
Expand Down
10 changes: 8 additions & 2 deletions test/integration/fab/channel_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package fab

import (
"os"
"path"
"strconv"
"testing"
Expand Down Expand Up @@ -50,8 +51,13 @@ func initializeLedgerTests(t *testing.T) (*fabsdk.FabricSDK, []string) {
t.Fatalf("creating peers failed: %v", err)
}

channelConfig := path.Join("../../../", metadata.ChannelConfigPath, channelConfigFile)
req := resmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: channelConfig, SigningIdentities: []msp.Identity{adminIdentity}}
ccReader, err := os.Open(path.Join("../../../", metadata.ChannelConfigPath, channelConfigFile))
if err != nil {
t.Fatal(err)
}
defer ccReader.Close()

req := resmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: ccReader, SigningIdentities: []msp.Identity{adminIdentity}}
err = integration.InitializeChannel(sdk, orgName, req, targets)
if err != nil {
t.Fatalf("failed to ensure channel has been initialized: %s", err)
Expand Down
6 changes: 3 additions & 3 deletions test/integration/fab/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func setup() {
testSetup := integration.BaseSetupImpl{
ConfigFile: "../" + integration.ConfigTestFile,

ChannelID: "mychannel",
OrgID: org1Name,
ChannelConfig: path.Join("../../", metadata.ChannelConfigPath, "mychannel.tx"),
ChannelID: "mychannel",
OrgID: org1Name,
ChannelConfigFile: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"),
}

sdk, err := fabsdk.New(config.FromFile(testSetup.ConfigFile))
Expand Down
4 changes: 2 additions & 2 deletions test/integration/msp/user_data_mgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// TestWithCustomStores demonstrates the usage of custom key and cert stores
// to manage user private keys and certificates.
func TestWithCustomStores(t *testing.T) {
config, err := configImpl.FromFile(sdkConfigFile)()
config, err := configImpl.FromFile("../" + integration.ConfigTestFile)()
if err != nil {
t.Fatalf("Unexpected error from config: %v", err)
}
Expand All @@ -45,7 +45,7 @@ func TestWithCustomStores(t *testing.T) {

//
// NOTE: BCCSP SW implementation currently doesn't allow
// writting private keys out. The file store used internally
// writing private keys out. The file store used internally
// by BCCSP has access to provate parts that are not available
// outside of BCCSP at the moment. Fot this reason, our
// example custom kay store will just hold the keys in memory.
Expand Down
9 changes: 8 additions & 1 deletion test/integration/orgs/multiple_orgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package orgs

import (
"math"
"os"
"path"
"strconv"
"strings"
Expand Down Expand Up @@ -96,8 +97,14 @@ func testWithOrg1(t *testing.T, sdk *fabsdk.FabricSDK) int {
}

// Create channel (or update if it already exists)
ccReader, err := os.Open(path.Join("../../../", metadata.ChannelConfigPath, "orgchannel.tx"))
if err != nil {
t.Fatal(err)
}
defer ccReader.Close()

req := resmgmt.SaveChannelRequest{ChannelID: "orgchannel",
ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "orgchannel.tx"),
ChannelConfig: ccReader,
SigningIdentities: []msp.Identity{org1AdminUser, org2AdminUser}}
if err = chMgmtClient.SaveChannel(req); err != nil {
t.Fatal(err)
Expand Down
8 changes: 4 additions & 4 deletions test/integration/sdk/channel_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ func TestChannelConfig(t *testing.T) {
func TestChannelConfigWithOrderer(t *testing.T) {

testSetup := integration.BaseSetupImpl{
ConfigFile: "../" + integration.ConfigTestFile,
ChannelID: "mychannel",
OrgID: org1Name,
ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"),
ConfigFile: "../" + integration.ConfigTestFile,
ChannelID: "mychannel",
OrgID: org1Name,
ChannelConfigFile: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"),
}

confProvider := config.FromFile(testSetup.ConfigFile)
Expand Down
6 changes: 3 additions & 3 deletions test/integration/sdk/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func setup() {
testSetup := integration.BaseSetupImpl{
ConfigFile: "../" + integration.ConfigTestFile,

ChannelID: "mychannel",
OrgID: org1Name,
ChannelConfig: path.Join("../../", metadata.ChannelConfigPath, "mychannel.tx"),
ChannelID: "mychannel",
OrgID: org1Name,
ChannelConfigFile: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"),
}

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

0 comments on commit 1b231db

Please sign in to comment.