Skip to content

Commit d6d2250

Browse files
author
Jason Yellick
committed
[FAB-1958] Add MSP test template
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]>
1 parent d1e939f commit d6d2250

File tree

8 files changed

+140
-32
lines changed

8 files changed

+140
-32
lines changed

common/configtx/test/helper.go

+36-13
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,63 @@ import (
2828

2929
"github.com/golang/protobuf/proto"
3030
"github.com/hyperledger/fabric/protos/peer"
31+
logging "github.com/op/go-logging"
3132
)
3233

34+
var logger = logging.MustGetLogger("common/configtx/test")
35+
3336
const (
3437
// AcceptAllPolicyKey is the key of the AcceptAllPolicy.
3538
AcceptAllPolicyKey = "AcceptAllPolicy"
3639
)
3740

41+
const (
42+
OrdererTemplateName = "orderer.template"
43+
MSPTemplateName = "msp.template"
44+
)
45+
3846
var ordererTemplate configtx.Template
47+
var mspTemplate configtx.Template
3948

4049
var genesisFactory genesis.Factory
4150

42-
// XXX This is a hacky singleton, which should go away, but is an artifact of using the existing utils implementation
43-
type MSPTemplate struct{}
44-
45-
func (msp MSPTemplate) Items(chainID string) ([]*cb.SignedConfigurationItem, error) {
46-
return []*cb.SignedConfigurationItem{utils.EncodeMSP(chainID)}, nil
47-
}
48-
49-
func init() {
50-
51+
func readTemplate(name string) configtx.Template {
5152
gopath := os.Getenv("GOPATH")
52-
data, err := ioutil.ReadFile(gopath + "/src/github.com/hyperledger/fabric/common/configtx/test/orderer.template")
53+
data, err := ioutil.ReadFile(gopath + "/src/github.com/hyperledger/fabric/common/configtx/test/" + name)
5354
if err != nil {
5455
peerConfig := os.Getenv("PEER_CFG_PATH")
55-
data, err = ioutil.ReadFile(peerConfig + "/common/configtx/test/orderer.template")
56+
data, err = ioutil.ReadFile(peerConfig + "/common/configtx/test/" + name)
5657
if err != nil {
5758
panic(err)
5859
}
5960
}
61+
6062
templateProto := &cb.ConfigurationTemplate{}
6163
err = proto.Unmarshal(data, templateProto)
6264
if err != nil {
6365
panic(err)
6466
}
6567

66-
ordererTemplate = configtx.NewSimpleTemplate(templateProto.Items...)
68+
return configtx.NewSimpleTemplate(templateProto.Items...)
69+
}
70+
71+
func init() {
72+
ordererTemplate = readTemplate(OrdererTemplateName)
73+
mspTemplate = readTemplate(MSPTemplateName)
6774
anchorPeers := []*peer.AnchorPeer{{Host: "fakehost", Port: 2000, Cert: []byte{}}}
6875
gossTemplate := configtx.NewSimpleTemplate(peersharedconfig.TemplateAnchorPeers(anchorPeers))
69-
genesisFactory = genesis.NewFactoryImpl(configtx.NewCompositeTemplate(MSPTemplate{}, ordererTemplate, gossTemplate))
76+
genesisFactory = genesis.NewFactoryImpl(configtx.NewCompositeTemplate(mspTemplate, ordererTemplate, gossTemplate))
77+
}
78+
79+
// WriteTemplate takes an output file and set of config items and writes them to that file as a marshaled ConfigurationTemplate
80+
func WriteTemplate(outputFile string, items ...*cb.ConfigurationItem) {
81+
logger.Debugf("Encoding configuration template")
82+
outputData := utils.MarshalOrPanic(&cb.ConfigurationTemplate{
83+
Items: items,
84+
})
85+
86+
logger.Debugf("Writing configuration to disk")
87+
ioutil.WriteFile(outputFile, outputData, 0644)
7088
}
7189

7290
func MakeGenesisBlock(chainID string) (*cb.Block, error) {
@@ -77,3 +95,8 @@ func MakeGenesisBlock(chainID string) (*cb.Block, error) {
7795
func GetOrdererTemplate() configtx.Template {
7896
return ordererTemplate
7997
}
98+
99+
// GetMSPerTemplate returns the test MSP template
100+
func GetMSPTemplate() configtx.Template {
101+
return mspTemplate
102+
}

common/configtx/test/msp.template

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
�"DEFAUL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w
39+
WhcNMTcxMTExMTcwNzAwWjBjMQswCQYDVQQGEwJVUzEXMBUGA1UECBMOTm9ydGgg
40+
Q2Fyb2xpbmExEDAOBgNVBAcTB1JhbGVpZ2gxGzAZBgNVBAoTEkh5cGVybGVkZ2Vy
41+
IEZhYnJpYzEMMAoGA1UECxMDQ09QMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE
42+
HBuKsAO43hs4JGpFfiGMkB/xsILTsOvmN2WmwpsPHZNL6w8HWe3xCPQtdG/XJJvZ
43+
+C756KEsUBM3yw5PTfku8qOBpzCBpDAOBgNVHQ8BAf8EBAMCBaAwHQYDVR0lBBYw
44+
FAYIKwYBBQUHAwEGCCsGAQUFBwMCMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFOFC
45+
dcUZ4es3ltiCgAVDoyLfVpPIMB8GA1UdIwQYMBaAFBdnQj2qnoI/xMUdn1vDmdG1
46+
nEgQMCUGA1UdEQQeMByCCm15aG9zdC5jb22CDnd3dy5teWhvc3QuY29tMAoGCCqG
47+
SM49BAMCA0gAMEUCIDf9Hbl4xn3z4EwNKmilM9lX2Fq4jWpAaRVB97OmVEeyAiEA
48+
25aDPQHGGq2AvhKT0wvt08cX1GTGCIbfmuLpMwKQj38=
49+
-----END CERTIFICATE-----
50+
�
51+
PEER�-----BEGIN EC PRIVATE KEY-----
52+
MHcCAQEEIAsWwFunEzqz1Rh6nvD4MiPkKCtmoxzh3jTquG5MSbeLoAoGCCqGSM49
53+
AwEHoUQDQgAEHBuKsAO43hs4JGpFfiGMkB/xsILTsOvmN2WmwpsPHZNL6w8HWe3x
54+
CPQtdG/XJJvZ+C756KEsUBM3yw5PTfku8g==
55+
-----END EC PRIVATE KEY-----

msp/mgmt/mspconfigmgr_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/hyperledger/fabric/msp"
2525
. "github.com/hyperledger/fabric/msp/mgmt"
2626
"github.com/hyperledger/fabric/protos/common"
27-
"github.com/hyperledger/fabric/protos/msp/utils"
2827
"github.com/stretchr/testify/assert"
2928
)
3029

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

38-
ci := &common.ConfigurationItem{Key: msputils.MSPKey, Value: confBytes}
37+
ci := &common.ConfigurationItem{Key: "DEFAULT", Value: confBytes}
3938

4039
// test success:
4140

@@ -62,6 +61,6 @@ func TestMSPConfigManager(t *testing.T) {
6261
mspCH.BeginConfig()
6362
err = mspCH.ProposeConfig(ci)
6463
assert.NoError(t, err)
65-
err = mspCH.ProposeConfig(&common.ConfigurationItem{Key: msputils.MSPKey, Value: []byte("BARF!")})
64+
err = mspCH.ProposeConfig(&common.ConfigurationItem{Type: common.ConfigurationItem_MSP, Key: "DEFAULT", Value: []byte("BARF!")})
6665
assert.Error(t, err)
6766
}

msp/template_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package msp_test
18+
19+
import (
20+
"testing"
21+
22+
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
23+
. "github.com/hyperledger/fabric/msp"
24+
"github.com/hyperledger/fabric/protos/common"
25+
26+
"github.com/golang/protobuf/proto"
27+
"github.com/stretchr/testify/assert"
28+
)
29+
30+
func TestTemplate(t *testing.T) {
31+
conf, err := GetLocalMspConfig("sampleconfig/")
32+
assert.NoError(t, err)
33+
34+
confBytes, err := proto.Marshal(conf)
35+
assert.NoError(t, err)
36+
37+
// XXX We should really get the MSP name by inspecting it, but, we know it is DEFAULT so hardcoding for now
38+
ci := &common.ConfigurationItem{Type: common.ConfigurationItem_MSP, Key: "DEFAULT", Value: confBytes}
39+
40+
configtxtest.WriteTemplate("../common/configtx/test/"+configtxtest.MSPTemplateName, ci)
41+
}

orderer/tools/configtemplate/main.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ package main
1818

1919
import (
2020
"flag"
21-
"io/ioutil"
2221

22+
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
2323
"github.com/hyperledger/fabric/common/flogging"
2424
"github.com/hyperledger/fabric/orderer/common/bootstrap/provisional"
2525
"github.com/hyperledger/fabric/orderer/localconfig"
26-
cb "github.com/hyperledger/fabric/protos/common"
27-
"github.com/hyperledger/fabric/protos/utils"
2826

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

46-
logger.Debugf("Encoding configuration template")
47-
outputData := utils.MarshalOrPanic(&cb.ConfigurationTemplate{
48-
Items: templateItems,
49-
})
50-
51-
logger.Debugf("Writing configuration to disk")
52-
ioutil.WriteFile(outputFile, outputData, 0644)
44+
configtxtest.WriteTemplate(outputFile, templateItems...)
5345
}
5446

5547
func main() {

orderer/tools/configtemplate/main_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ package main
1818

1919
import (
2020
"testing"
21+
22+
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
2123
)
2224

2325
func TestUpdateTemplate(t *testing.T) {
24-
writeTemplate("../../../common/configtx/test/orderer.template")
26+
writeTemplate("../../../common/configtx/test/" + configtxtest.OrdererTemplateName)
2527
}

protos/msp/testutils/testutils.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/golang/protobuf/proto"
2323
"github.com/hyperledger/fabric/protos/common"
2424
"github.com/hyperledger/fabric/protos/msp"
25-
"github.com/hyperledger/fabric/protos/msp/utils"
2625
"github.com/hyperledger/fabric/protos/utils"
2726
)
2827

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

36-
ci := &common.ConfigurationItem{Type: common.ConfigurationItem_MSP, Key: msputils.MSPKey, Value: confBytes}
35+
ci := &common.ConfigurationItem{Type: common.ConfigurationItem_MSP, Key: "DEFAULT", Value: confBytes}
3736
ciBytes, err := proto.Marshal(ci)
3837
if err != nil {
3938
return nil, fmt.Errorf("proto.Marshal failed for a configuration item, err %s", err)

protos/msp/utils/utils.go

-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ import (
2222
"github.com/hyperledger/fabric/protos/msp"
2323
)
2424

25-
// FIXME: is this the right way to place this const?
26-
const MSPKey = "MSP"
27-
2825
func GetMSPManagerConfigFromBlock(b *common.Block) ([]*msp.MSPConfig, error) {
2926
// TODO: should we check that b is a configuration
3027
// block or should we just assume our caller will

0 commit comments

Comments
 (0)