Skip to content

Commit

Permalink
[FAB-1958] Add MSP test template
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1958

This is part of an ongoing change series which has the individual
components automatically generate a template during tests of the
configuration that is requried in order to intialize said component.

This one adds a template for MSP which will hopefully allow us to
shortly rip out all of the LoadFake...MSP calls.

Change-Id: I91f455f70f6286aa2a8eccb32221d3fcf9199415
Signed-off-by: Jason Yellick <[email protected]>
  • Loading branch information
Jason Yellick committed Feb 5, 2017
1 parent d1e939f commit d6d2250
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 32 deletions.
49 changes: 36 additions & 13 deletions common/configtx/test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,63 @@ import (

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/protos/peer"
logging "github.com/op/go-logging"
)

var logger = logging.MustGetLogger("common/configtx/test")

const (
// AcceptAllPolicyKey is the key of the AcceptAllPolicy.
AcceptAllPolicyKey = "AcceptAllPolicy"
)

const (
OrdererTemplateName = "orderer.template"
MSPTemplateName = "msp.template"
)

var ordererTemplate configtx.Template
var mspTemplate configtx.Template

var genesisFactory genesis.Factory

// XXX This is a hacky singleton, which should go away, but is an artifact of using the existing utils implementation
type MSPTemplate struct{}

func (msp MSPTemplate) Items(chainID string) ([]*cb.SignedConfigurationItem, error) {
return []*cb.SignedConfigurationItem{utils.EncodeMSP(chainID)}, nil
}

func init() {

func readTemplate(name string) configtx.Template {
gopath := os.Getenv("GOPATH")
data, err := ioutil.ReadFile(gopath + "/src/github.com/hyperledger/fabric/common/configtx/test/orderer.template")
data, err := ioutil.ReadFile(gopath + "/src/github.com/hyperledger/fabric/common/configtx/test/" + name)
if err != nil {
peerConfig := os.Getenv("PEER_CFG_PATH")
data, err = ioutil.ReadFile(peerConfig + "/common/configtx/test/orderer.template")
data, err = ioutil.ReadFile(peerConfig + "/common/configtx/test/" + name)
if err != nil {
panic(err)
}
}

templateProto := &cb.ConfigurationTemplate{}
err = proto.Unmarshal(data, templateProto)
if err != nil {
panic(err)
}

ordererTemplate = configtx.NewSimpleTemplate(templateProto.Items...)
return configtx.NewSimpleTemplate(templateProto.Items...)
}

func init() {
ordererTemplate = readTemplate(OrdererTemplateName)
mspTemplate = readTemplate(MSPTemplateName)
anchorPeers := []*peer.AnchorPeer{{Host: "fakehost", Port: 2000, Cert: []byte{}}}
gossTemplate := configtx.NewSimpleTemplate(peersharedconfig.TemplateAnchorPeers(anchorPeers))
genesisFactory = genesis.NewFactoryImpl(configtx.NewCompositeTemplate(MSPTemplate{}, ordererTemplate, gossTemplate))
genesisFactory = genesis.NewFactoryImpl(configtx.NewCompositeTemplate(mspTemplate, ordererTemplate, gossTemplate))
}

// WriteTemplate takes an output file and set of config items and writes them to that file as a marshaled ConfigurationTemplate
func WriteTemplate(outputFile string, items ...*cb.ConfigurationItem) {
logger.Debugf("Encoding configuration template")
outputData := utils.MarshalOrPanic(&cb.ConfigurationTemplate{
Items: items,
})

logger.Debugf("Writing configuration to disk")
ioutil.WriteFile(outputFile, outputData, 0644)
}

func MakeGenesisBlock(chainID string) (*cb.Block, error) {
Expand All @@ -77,3 +95,8 @@ func MakeGenesisBlock(chainID string) (*cb.Block, error) {
func GetOrdererTemplate() configtx.Template {
return ordererTemplate
}

// GetMSPerTemplate returns the test MSP template
func GetMSPTemplate() configtx.Template {
return mspTemplate
}
55 changes: 55 additions & 0 deletions common/configtx/test/msp.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

�"DEFAULT*��
DEFAULT�-----BEGIN CERTIFICATE-----
MIICYjCCAgmgAwIBAgIUB3CTDOU47sUC5K4kn/Caqnh114YwCgYIKoZIzj0EAwIw
fzELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNh
biBGcmFuY2lzY28xHzAdBgNVBAoTFkludGVybmV0IFdpZGdldHMsIEluYy4xDDAK
BgNVBA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AYIKwYBBQUHAwEGCCsGAQUFBwMCMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFOFC
dcUZ4es3ltiCgAVDoyLfVpPIMB8GA1UdIwQYMBaAFBdnQj2qnoI/xMUdn1vDmdG1
nEgQMCUGA1UdEQQeMByCCm15aG9zdC5jb22CDnd3dy5teWhvc3QuY29tMAoGCCqG
SM49BAMCA0gAMEUCIDf9Hbl4xn3z4EwNKmilM9lX2Fq4jWpAaRVB97OmVEeyAiEA
25aDPQHGGq2AvhKT0wvt08cX1GTGCIbfmuLpMwKQj38=
-----END CERTIFICATE-----
�
PEER�-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIAsWwFunEzqz1Rh6nvD4MiPkKCtmoxzh3jTquG5MSbeLoAoGCCqGSM49
AwEHoUQDQgAEHBuKsAO43hs4JGpFfiGMkB/xsILTsOvmN2WmwpsPHZNL6w8HWe3x
CPQtdG/XJJvZ+C756KEsUBM3yw5PTfku8g==
-----END EC PRIVATE KEY-----
5 changes: 2 additions & 3 deletions msp/mgmt/mspconfigmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/hyperledger/fabric/msp"
. "github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/msp/utils"
"github.com/stretchr/testify/assert"
)

Expand All @@ -35,7 +34,7 @@ func TestMSPConfigManager(t *testing.T) {
confBytes, err := proto.Marshal(conf)
assert.NoError(t, err)

ci := &common.ConfigurationItem{Key: msputils.MSPKey, Value: confBytes}
ci := &common.ConfigurationItem{Key: "DEFAULT", Value: confBytes}

// test success:

Expand All @@ -62,6 +61,6 @@ func TestMSPConfigManager(t *testing.T) {
mspCH.BeginConfig()
err = mspCH.ProposeConfig(ci)
assert.NoError(t, err)
err = mspCH.ProposeConfig(&common.ConfigurationItem{Key: msputils.MSPKey, Value: []byte("BARF!")})
err = mspCH.ProposeConfig(&common.ConfigurationItem{Type: common.ConfigurationItem_MSP, Key: "DEFAULT", Value: []byte("BARF!")})
assert.Error(t, err)
}
41 changes: 41 additions & 0 deletions msp/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package msp_test

import (
"testing"

configtxtest "github.com/hyperledger/fabric/common/configtx/test"
. "github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protos/common"

"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
)

func TestTemplate(t *testing.T) {
conf, err := GetLocalMspConfig("sampleconfig/")
assert.NoError(t, err)

confBytes, err := proto.Marshal(conf)
assert.NoError(t, err)

// XXX We should really get the MSP name by inspecting it, but, we know it is DEFAULT so hardcoding for now
ci := &common.ConfigurationItem{Type: common.ConfigurationItem_MSP, Key: "DEFAULT", Value: confBytes}

configtxtest.WriteTemplate("../common/configtx/test/"+configtxtest.MSPTemplateName, ci)
}
12 changes: 2 additions & 10 deletions orderer/tools/configtemplate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ package main

import (
"flag"
"io/ioutil"

configtxtest "github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/orderer/common/bootstrap/provisional"
"github.com/hyperledger/fabric/orderer/localconfig"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"

logging "github.com/op/go-logging"
)
Expand All @@ -43,13 +41,7 @@ func writeTemplate(outputFile string) {
logger.Debugf("Producing template items")
templateItems := generator.TemplateItems()

logger.Debugf("Encoding configuration template")
outputData := utils.MarshalOrPanic(&cb.ConfigurationTemplate{
Items: templateItems,
})

logger.Debugf("Writing configuration to disk")
ioutil.WriteFile(outputFile, outputData, 0644)
configtxtest.WriteTemplate(outputFile, templateItems...)
}

func main() {
Expand Down
4 changes: 3 additions & 1 deletion orderer/tools/configtemplate/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package main

import (
"testing"

configtxtest "github.com/hyperledger/fabric/common/configtx/test"
)

func TestUpdateTemplate(t *testing.T) {
writeTemplate("../../../common/configtx/test/orderer.template")
writeTemplate("../../../common/configtx/test/" + configtxtest.OrdererTemplateName)
}
3 changes: 1 addition & 2 deletions protos/msp/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/msp"
"github.com/hyperledger/fabric/protos/msp/utils"
"github.com/hyperledger/fabric/protos/utils"
)

Expand All @@ -33,7 +32,7 @@ func GetTestBlockFromMspConfig(conf *msp.MSPConfig) (*common.Block, error) {
return nil, fmt.Errorf("proto.Marshal failed for a configuration item payload, err %s", err)
}

ci := &common.ConfigurationItem{Type: common.ConfigurationItem_MSP, Key: msputils.MSPKey, Value: confBytes}
ci := &common.ConfigurationItem{Type: common.ConfigurationItem_MSP, Key: "DEFAULT", Value: confBytes}
ciBytes, err := proto.Marshal(ci)
if err != nil {
return nil, fmt.Errorf("proto.Marshal failed for a configuration item, err %s", err)
Expand Down
3 changes: 0 additions & 3 deletions protos/msp/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (
"github.com/hyperledger/fabric/protos/msp"
)

// FIXME: is this the right way to place this const?
const MSPKey = "MSP"

func GetMSPManagerConfigFromBlock(b *common.Block) ([]*msp.MSPConfig, error) {
// TODO: should we check that b is a configuration
// block or should we just assume our caller will
Expand Down

0 comments on commit d6d2250

Please sign in to comment.