Skip to content

Commit

Permalink
[FAB-1961] Clean up templating in peer/msp/orderer
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1961

Now that peer, msp, and orderer all generate templates to be used in the
configtxtest helper, this CR goes through an factors out some common
logic and makes things uniform.

Change-Id: I06d6a4e6993353d8d0682b45ed3993a2a35cc8a1
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Feb 5, 2017
1 parent 6a7c188 commit 6500a2f
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 88 deletions.
2 changes: 1 addition & 1 deletion common/configtx/inspector/inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func TestFromTemplate(t *testing.T) {
ordererTemplate := configtxtest.GetOrdererTemplate()
ordererTemplate := configtxtest.OrdererTemplate()
signedItems, err := ordererTemplate.Items("SampleChainID")
if err != nil {
t.Fatalf("Error creating signed items: %s", err)
Expand Down
61 changes: 36 additions & 25 deletions common/configtx/test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,59 +48,70 @@ var peerTemplate configtx.Template

var genesisFactory genesis.Factory

func readTemplate(name string) configtx.Template {
gopath := os.Getenv("GOPATH")
data, err := ioutil.ReadFile(gopath + "/src/github.com/hyperledger/fabric/common/configtx/test/" + name)
func init() {
ordererTemplate = readTemplate(OrdererTemplateName)
mspTemplate = readTemplate(MSPTemplateName)
peerTemplate = readTemplate(PeerTemplateName)
genesisFactory = genesis.NewFactoryImpl(configtx.NewCompositeTemplate(mspTemplate, ordererTemplate, peerTemplate))
}

func resolveName(name string) (string, []byte) {
path := os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/common/configtx/test/" + name
data, err := ioutil.ReadFile(path)
if err == nil {
return path, data
}

path = os.Getenv("PEER_CFG_PATH") + "/common/configtx/test/" + name
data, err = ioutil.ReadFile(path)
if err != nil {
peerConfig := os.Getenv("PEER_CFG_PATH")
data, err = ioutil.ReadFile(peerConfig + "/common/configtx/test/" + name)
if err != nil {
panic(err)
}
panic(err)
}

return path, data
}

func readTemplate(name string) configtx.Template {
_, data := resolveName(name)

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

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

func init() {
ordererTemplate = readTemplate(OrdererTemplateName)
mspTemplate = readTemplate(MSPTemplateName)
peerTemplate = readTemplate(PeerTemplateName)
genesisFactory = genesis.NewFactoryImpl(configtx.NewCompositeTemplate(mspTemplate, ordererTemplate, peerTemplate))
}

// 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) {
func WriteTemplate(name string, items ...*cb.ConfigurationItem) {
path, _ := resolveName(name)

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

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

// MakeGenesisBlock creates a genesis block using the test templates for the given chainID
func MakeGenesisBlock(chainID string) (*cb.Block, error) {
return genesisFactory.Block(chainID)
}

// GetOrderererTemplate returns the test orderer template
func GetOrdererTemplate() configtx.Template {
// OrderererTemplate returns the test orderer template
func OrdererTemplate() configtx.Template {
return ordererTemplate
}

// GetMSPerTemplate returns the test MSP template
func GetMSPTemplate() configtx.Template {
// MSPerTemplate returns the test MSP template
func MSPTemplate() configtx.Template {
return mspTemplate
}

// GetMSPerTemplate returns the test peer template
func GetPeerTemplate() configtx.Template {
// MSPerTemplate returns the test peer template
func PeerTemplate() configtx.Template {
return peerTemplate
}
2 changes: 1 addition & 1 deletion core/common/validation/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

func TestValidateConfigTx(t *testing.T) {
chainID := util.GetTestChainID()
oTemplate := test.GetOrdererTemplate()
oTemplate := test.OrdererTemplate()
mspcfg := configtx.NewSimpleTemplate(utils.EncodeMSPUnsigned(chainID))
chainCfg := configtx.NewSimpleTemplate(chainconfig.DefaultHashingAlgorithm())
chCrtTemp := configtx.NewCompositeTemplate(oTemplate, mspcfg, chainCfg)
Expand Down
2 changes: 1 addition & 1 deletion msp/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ func TestTemplate(t *testing.T) {
// 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)
configtxtest.WriteTemplate(configtxtest.MSPTemplateName, ci)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package provisional

import (
"testing"

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

func TestUpdateTemplate(t *testing.T) {
writeTemplate("../../../common/configtx/test/" + configtxtest.OrdererTemplateName)
conf := config.Load()

generator := New(conf)

templateItems := generator.TemplateItems()

configtxtest.WriteTemplate(configtxtest.OrdererTemplateName, templateItems...)
}
53 changes: 0 additions & 53 deletions orderer/tools/configtemplate/main.go

This file was deleted.

2 changes: 1 addition & 1 deletion peer/channel/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func sendCreateChainTransaction(cf *ChannelCmdFactory) error {
return err
}
//TODO this is a temporary hack until `orderer.template` is supplied from the CLI
oTemplate := configtxtest.GetOrdererTemplate()
oTemplate := configtxtest.OrdererTemplate()
mspTemplate := configtx.NewSimpleTemplate(utils.EncodeMSPUnsigned(chainID))
gossTemplate := configtx.NewSimpleTemplate(sharedconfig.TemplateAnchorPeers(anchorPeers))
chCrtTemp := configtx.NewCompositeTemplate(oTemplate, mspTemplate, gossTemplate)
Expand Down
5 changes: 1 addition & 4 deletions peer/sharedconfig/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,5 @@ import (
)

func TestTemplate(t *testing.T) {
configtxtest.WriteTemplate(
"../../common/configtx/test/"+configtxtest.PeerTemplateName,
DefaultAnchorPeers(),
)
configtxtest.WriteTemplate(configtxtest.PeerTemplateName, DefaultAnchorPeers())
}

0 comments on commit 6500a2f

Please sign in to comment.