Skip to content

Commit

Permalink
[FAB-9602] Config overrides inplace of custom backends
Browse files Browse the repository at this point in the history
- removed custom backend fallback in mockConfigBackend.
instead, config override feature is being used in all unit-test
and integration-tests.

Change-Id: If088dbe4723eec748da47dc1367d90b83939f2a6
Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Apr 28, 2018
1 parent bdf8ca3 commit fc7ae84
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 88 deletions.
10 changes: 6 additions & 4 deletions pkg/client/msp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (f *textFixture) setup() *fabsdk.FabricSDK {
customBackend := getCustomBackend(backend...)

configProvider := func() ([]core.ConfigBackend, error) {
return []core.ConfigBackend{customBackend}, nil
return customBackend, nil
}

// Instantiate the SDK
Expand Down Expand Up @@ -233,12 +233,12 @@ func randomUsername() string {
return "user" + strconv.Itoa(rand.Intn(500000))
}

func getCustomBackend(backend ...core.ConfigBackend) *mocks.MockConfigBackend {
func getCustomBackend(currentBackends ...core.ConfigBackend) []core.ConfigBackend {
backendMap := make(map[string]interface{})

//Custom URLs for ca configs
networkConfig := fab.NetworkConfig{}
configLookup := lookup.New(backend...)
configLookup := lookup.New(currentBackends...)
configLookup.UnmarshalKey("certificateAuthorities", &networkConfig.CertificateAuthorities)

ca1Config := networkConfig.CertificateAuthorities["ca.org1.example.com"]
Expand All @@ -250,5 +250,7 @@ func getCustomBackend(backend ...core.ConfigBackend) *mocks.MockConfigBackend {
networkConfig.CertificateAuthorities["ca.org2.example.com"] = ca2Config
backendMap["certificateAuthorities"] = networkConfig.CertificateAuthorities

return &mocks.MockConfigBackend{KeyValueMap: backendMap, CustomBackend: backend}
backends := append([]core.ConfigBackend{}, &mocks.MockConfigBackend{KeyValueMap: backendMap})
return append(backends, currentBackends...)

}
15 changes: 0 additions & 15 deletions pkg/core/mocks/mockconfigbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,14 @@ SPDX-License-Identifier: Apache-2.0

package mocks

import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
)

//MockConfigBackend mocks config backend for unit tests
type MockConfigBackend struct {
//KeyValueMap map to override CustomBackend key-values.
KeyValueMap map[string]interface{}
//CustomBackend config backend
CustomBackend []core.ConfigBackend
}

//Lookup returns or unmarshals value for given key
func (b *MockConfigBackend) Lookup(key string) (interface{}, bool) {
v, ok := b.KeyValueMap[key]
//if not found in custom map then try with backend
if !ok && b.CustomBackend != nil {
for _, backend := range b.CustomBackend {
val, ok := backend.Lookup(key)
if ok {
return val, true
}
}
}
return v, ok
}
3 changes: 1 addition & 2 deletions pkg/fab/endpointconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ func (c *EndpointConfig) NetworkConfig() (*fab.NetworkConfig, error) {
return c.networkConfig, nil
}

// NetworkPeers returns the network peers configuration
//returns network peers from all the peers from all the
// NetworkPeers returns the network peers configuration, all the peers from all the orgs in config.
func (c *EndpointConfig) NetworkPeers() ([]fab.NetworkPeer, error) {
netConfig, err := c.NetworkConfig()
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions pkg/fabsdk/fabsdk_chconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestNewDefaultTwoValidSDK(t *testing.T) {
t.Fatalf("failed to get configbackend for test: %v", err)
}
configProvider := func() ([]core.ConfigBackend, error) {
return []core.ConfigBackend{customBackend}, nil
return customBackend, nil
}

sdk2, err := New(configProvider)
Expand Down Expand Up @@ -147,7 +147,7 @@ func checkClientOrg(configBackend core.ConfigBackend, t *testing.T, orgName stri
}
}

func getCustomBackend() (*mockCore.MockConfigBackend, error) {
func getCustomBackend() ([]core.ConfigBackend, error) {
backend, err := config.FromFile(sdkConfigFile)()
if err != nil {
return nil, err
Expand All @@ -167,5 +167,6 @@ func getCustomBackend() (*mockCore.MockConfigBackend, error) {
backendMap := make(map[string]interface{})
backendMap["client"] = clientConfig

return &mockCore.MockConfigBackend{KeyValueMap: backendMap, CustomBackend: backend}, nil
backends := append([]core.ConfigBackend{}, &mockCore.MockConfigBackend{KeyValueMap: backendMap})
return append(backends, backend...), nil
}
105 changes: 61 additions & 44 deletions pkg/msp/caclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"

"github.com/golang/mock/gomock"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
fabApi "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/pkg/common/providers/test/mockcontext"
Expand All @@ -33,7 +34,7 @@ import (
func TestEnrollAndReenroll(t *testing.T) {

f := textFixture{}
f.setup(nil)
f.setup()
defer f.close()

orgMSPID := mspIDByOrgName(t, f.endpointConfig, org1)
Expand Down Expand Up @@ -97,20 +98,20 @@ func reenrollWithAppropriateUser(f textFixture, t *testing.T, enrolledUserData *
func TestWrongURL(t *testing.T) {

f := textFixture{}
f.setup(nil)
f.setup()
defer f.close()

configBackend, err := getInvalidURLBackend()
if err != nil {
panic(fmt.Sprintf("Failed to get config backend: %v", err))
}

wrongURLIdentityConfig, err := ConfigFromBackend(configBackend)
wrongURLIdentityConfig, err := ConfigFromBackend(configBackend...)
if err != nil {
panic(fmt.Sprintf("Failed to read config: %v", err))
}

wrongURLEndpointConfig, err := fab.ConfigFromBackend(configBackend)
wrongURLEndpointConfig, err := fab.ConfigFromBackend(configBackend...)
if err != nil {
panic(fmt.Sprintf("Failed to read config: %v", err))
}
Expand Down Expand Up @@ -145,15 +146,15 @@ func TestWrongURL(t *testing.T) {
func TestNoConfiguredCAs(t *testing.T) {

f := textFixture{}
f.setup(nil)
f.setup()
defer f.close()

configBackend, err := getNoCAConfigBackend()
if err != nil {
panic(fmt.Sprintf("Failed to get config backend: %v", err))
}

wrongURLEndpointConfig, err := fab.ConfigFromBackend(configBackend)
wrongURLEndpointConfig, err := fab.ConfigFromBackend(configBackend...)
if err != nil {
panic(fmt.Sprintf("Failed to read config: %v", err))
}
Expand All @@ -179,7 +180,7 @@ func TestRegister(t *testing.T) {
time.Sleep(2 * time.Second)

f := textFixture{}
f.setup(nil)
f.setup()
defer f.close()

// Register with nil request
Expand Down Expand Up @@ -216,7 +217,7 @@ func TestEmbeddedRegistar(t *testing.T) {
}

f := textFixture{}
f.setup(embeddedRegistrarBackend)
f.setup(embeddedRegistrarBackend...)
defer f.close()

// Register with valid request
Expand All @@ -241,7 +242,7 @@ func TestRegisterNoRegistrar(t *testing.T) {
}

f := textFixture{}
f.setup(noRegistrarBackend)
f.setup(noRegistrarBackend...)
defer f.close()

// Register with nil request
Expand Down Expand Up @@ -271,7 +272,7 @@ func TestRegisterNoRegistrar(t *testing.T) {
func TestRevoke(t *testing.T) {

f := textFixture{}
f.setup(nil)
f.setup()
defer f.close()

// Revoke with nil request
Expand All @@ -295,7 +296,7 @@ func TestRevoke(t *testing.T) {
func TestCAConfigError(t *testing.T) {

f := textFixture{}
f.setup(nil)
f.setup()
defer f.close()

mockCtrl := gomock.NewController(t)
Expand All @@ -319,7 +320,7 @@ func TestCAConfigError(t *testing.T) {
func TestCAServerCertPathsError(t *testing.T) {

f := textFixture{}
f.setup(nil)
f.setup()
defer f.close()

mockCtrl := gomock.NewController(t)
Expand All @@ -345,7 +346,7 @@ func TestCAServerCertPathsError(t *testing.T) {
func TestCAClientCertPathError(t *testing.T) {

f := textFixture{}
f.setup(nil)
f.setup()
defer f.close()

mockCtrl := gomock.NewController(t)
Expand All @@ -372,7 +373,7 @@ func TestCAClientCertPathError(t *testing.T) {
func TestCAClientKeyPathError(t *testing.T) {

f := textFixture{}
f.setup(nil)
f.setup()
defer f.close()

mockCtrl := gomock.NewController(t)
Expand Down Expand Up @@ -408,25 +409,27 @@ func TestInterfaces(t *testing.T) {
}
}

func getCustomBackend(configPath string) (*mocks.MockConfigBackend, error) {
func getCustomBackend(configPath string) ([]core.ConfigBackend, error) {

backend, err := config.FromFile(configPath)()
configBackends, err := config.FromFile(configPath)()
if err != nil {
return nil, err
}
backendMap := make(map[string]interface{})
backendMap["client"], _ = backend[0].Lookup("client")
backendMap["certificateAuthorities"], _ = backend[0].Lookup("certificateAuthorities")
backendMap["entityMatchers"], _ = backend[0].Lookup("entityMatchers")
backendMap["peers"], _ = backend[0].Lookup("peers")
backendMap["organizations"], _ = backend[0].Lookup("organizations")
backendMap["orderers"], _ = backend[0].Lookup("orderers")
backendMap["channels"], _ = backend[0].Lookup("channels")

return &mocks.MockConfigBackend{KeyValueMap: backendMap, CustomBackend: backend}, nil
backendMap["client"], _ = configBackends[0].Lookup("client")
backendMap["certificateAuthorities"], _ = configBackends[0].Lookup("certificateAuthorities")
backendMap["entityMatchers"], _ = configBackends[0].Lookup("entityMatchers")
backendMap["peers"], _ = configBackends[0].Lookup("peers")
backendMap["organizations"], _ = configBackends[0].Lookup("organizations")
backendMap["orderers"], _ = configBackends[0].Lookup("orderers")
backendMap["channels"], _ = configBackends[0].Lookup("channels")

backends := append([]core.ConfigBackend{}, &mocks.MockConfigBackend{KeyValueMap: backendMap})
backends = append(backends, configBackends...)
return backends, nil
}

func getInvalidURLBackend() (*mocks.MockConfigBackend, error) {
func getInvalidURLBackend() ([]core.ConfigBackend, error) {

mockConfigBackend, err := getCustomBackend(configPath)
if err != nil {
Expand All @@ -436,7 +439,7 @@ func getInvalidURLBackend() (*mocks.MockConfigBackend, error) {
//Create an invalid channel
networkConfig := fabApi.NetworkConfig{}
//get valid certificate authorities
err = lookup.New(mockConfigBackend).UnmarshalKey("certificateAuthorities", &networkConfig.CertificateAuthorities)
err = lookup.New(mockConfigBackend...).UnmarshalKey("certificateAuthorities", &networkConfig.CertificateAuthorities)
if err != nil {
return nil, err
}
Expand All @@ -451,12 +454,15 @@ func getInvalidURLBackend() (*mocks.MockConfigBackend, error) {
networkConfig.CertificateAuthorities["ca.org2.example.com"] = ca2Config

//Override backend with this new CertificateAuthorities config
mockConfigBackend.KeyValueMap["certificateAuthorities"] = networkConfig.CertificateAuthorities
backendMap := make(map[string]interface{})
backendMap["certificateAuthorities"] = networkConfig.CertificateAuthorities
backends := append([]core.ConfigBackend{}, &mocks.MockConfigBackend{KeyValueMap: backendMap})
backends = append(backends, mockConfigBackend...)

return mockConfigBackend, nil
return backends, nil
}

func getNoRegistrarBackend() (*mocks.MockConfigBackend, error) {
func getNoRegistrarBackend() ([]core.ConfigBackend, error) {

mockConfigBackend, err := getCustomBackend(configPath)
if err != nil {
Expand All @@ -466,7 +472,7 @@ func getNoRegistrarBackend() (*mocks.MockConfigBackend, error) {
//Create an invalid channel
networkConfig := fabApi.NetworkConfig{}
//get valid certificate authorities
err = lookup.New(mockConfigBackend).UnmarshalKey("certificateAuthorities", &networkConfig.CertificateAuthorities)
err = lookup.New(mockConfigBackend...).UnmarshalKey("certificateAuthorities", &networkConfig.CertificateAuthorities)
if err != nil {
return nil, err
}
Expand All @@ -481,12 +487,15 @@ func getNoRegistrarBackend() (*mocks.MockConfigBackend, error) {
networkConfig.CertificateAuthorities["ca.org2.example.com"] = ca2Config

//Override backend with this new CertificateAuthorities config
mockConfigBackend.KeyValueMap["certificateAuthorities"] = networkConfig.CertificateAuthorities
backendMap := make(map[string]interface{})
backendMap["certificateAuthorities"] = networkConfig.CertificateAuthorities
backends := append([]core.ConfigBackend{}, &mocks.MockConfigBackend{KeyValueMap: backendMap})
backends = append(backends, mockConfigBackend...)

return mockConfigBackend, nil
return backends, nil
}

func getNoCAConfigBackend() (*mocks.MockConfigBackend, error) {
func getNoCAConfigBackend() ([]core.ConfigBackend, error) {

mockConfigBackend, err := getCustomBackend(configPath)
if err != nil {
Expand All @@ -496,7 +505,7 @@ func getNoCAConfigBackend() (*mocks.MockConfigBackend, error) {
//Create an empty network config
networkConfig := fabApi.NetworkConfig{}
//get valid certificate authorities
err = lookup.New(mockConfigBackend).UnmarshalKey("organizations", &networkConfig.Organizations)
err = lookup.New(mockConfigBackend...).UnmarshalKey("organizations", &networkConfig.Organizations)
if err != nil {
return nil, err
}
Expand All @@ -506,15 +515,19 @@ func getNoCAConfigBackend() (*mocks.MockConfigBackend, error) {
org1.CertificateAuthorities = []string{}
networkConfig.Organizations["org1"] = org1

backendMap := make(map[string]interface{})
//Override backend with organization config having empty CertificateAuthorities
mockConfigBackend.KeyValueMap["organizations"] = networkConfig.Organizations
backendMap["organizations"] = networkConfig.Organizations
//Override backend with this nil empty CertificateAuthorities config
mockConfigBackend.KeyValueMap["certificateAuthorities"] = networkConfig.CertificateAuthorities
backendMap["certificateAuthorities"] = networkConfig.CertificateAuthorities

return mockConfigBackend, nil
backends := append([]core.ConfigBackend{}, &mocks.MockConfigBackend{KeyValueMap: backendMap})
backends = append(backends, mockConfigBackend...)

return backends, nil
}

func getEmbeddedRegistrarConfigBackend() (*mocks.MockConfigBackend, error) {
func getEmbeddedRegistrarConfigBackend() ([]core.ConfigBackend, error) {

mockConfigBackend, err := getCustomBackend(configPath)
if err != nil {
Expand All @@ -526,11 +539,11 @@ func getEmbeddedRegistrarConfigBackend() (*mocks.MockConfigBackend, error) {
//Create an empty network config
networkConfig := fabApi.NetworkConfig{}
//get valid certificate authorities
err = lookup.New(mockConfigBackend).UnmarshalKey("organizations", &networkConfig.Organizations)
err = lookup.New(mockConfigBackend...).UnmarshalKey("organizations", &networkConfig.Organizations)
if err != nil {
return nil, err
}
err = lookup.New(mockConfigBackend).UnmarshalKey("certificateAuthorities", &networkConfig.CertificateAuthorities)
err = lookup.New(mockConfigBackend...).UnmarshalKey("certificateAuthorities", &networkConfig.CertificateAuthorities)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -572,10 +585,14 @@ XdsmTcdRvJ3TS/6HCA==
networkConfig.CertificateAuthorities["ca.org1.example.com"] = ca1Config
networkConfig.CertificateAuthorities["ca.org2.example.com"] = ca2Config

backendMap := make(map[string]interface{})
//Override backend with updated organization config
mockConfigBackend.KeyValueMap["organizations"] = networkConfig.Organizations
backendMap["organizations"] = networkConfig.Organizations
//Override backend with updated certificate authorities config
mockConfigBackend.KeyValueMap["certificateAuthorities"] = networkConfig.CertificateAuthorities
backendMap["certificateAuthorities"] = networkConfig.CertificateAuthorities

backends := append([]core.ConfigBackend{}, &mocks.MockConfigBackend{KeyValueMap: backendMap})
backends = append(backends, mockConfigBackend...)

return mockConfigBackend, nil
return backends, nil
}
Loading

0 comments on commit fc7ae84

Please sign in to comment.