diff --git a/common/configtx/inspector/inspector_test.go b/common/configtx/inspector/inspector_test.go index 7a91def8fd8..2fbf9cd8ad3 100644 --- a/common/configtx/inspector/inspector_test.go +++ b/common/configtx/inspector/inspector_test.go @@ -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) diff --git a/common/configtx/test/helper.go b/common/configtx/test/helper.go index 36e2b4af4c8..c9de35d5cbc 100644 --- a/common/configtx/test/helper.go +++ b/common/configtx/test/helper.go @@ -48,19 +48,34 @@ 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) } @@ -68,39 +83,35 @@ func readTemplate(name string) configtx.Template { 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 } diff --git a/core/common/validation/config_test.go b/core/common/validation/config_test.go index dcbe8f1ac09..a4805c916a8 100644 --- a/core/common/validation/config_test.go +++ b/core/common/validation/config_test.go @@ -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) diff --git a/msp/template_test.go b/msp/template_test.go index 723f4ef8e4f..aba06c6cb73 100644 --- a/msp/template_test.go +++ b/msp/template_test.go @@ -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) } diff --git a/orderer/tools/configtemplate/main_test.go b/orderer/common/bootstrap/provisional/template_test.go similarity index 74% rename from orderer/tools/configtemplate/main_test.go rename to orderer/common/bootstrap/provisional/template_test.go index b7ec65e54ab..aab2c9259ad 100644 --- a/orderer/tools/configtemplate/main_test.go +++ b/orderer/common/bootstrap/provisional/template_test.go @@ -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...) } diff --git a/orderer/tools/configtemplate/main.go b/orderer/tools/configtemplate/main.go deleted file mode 100644 index c6f020b175a..00000000000 --- a/orderer/tools/configtemplate/main.go +++ /dev/null @@ -1,53 +0,0 @@ -/* -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 main - -import ( - "flag" - - 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" - - logging "github.com/op/go-logging" -) - -var logger = logging.MustGetLogger("orderer/tools/baseconfig") - -const defaultOutputFile = "orderer.template" - -func writeTemplate(outputFile string) { - conf := config.Load() - flogging.InitFromSpec(conf.General.LogLevel) - - logger.Debugf("Initializing generator") - generator := provisional.New(conf) - - logger.Debugf("Producing template items") - templateItems := generator.TemplateItems() - - configtxtest.WriteTemplate(outputFile, templateItems...) -} - -func main() { - var outputFile string - flag.StringVar(&outputFile, "outputFile", defaultOutputFile, "The file to write the configuration templatee to") - flag.Parse() - - writeTemplate(outputFile) -} diff --git a/peer/channel/create.go b/peer/channel/create.go index 075911cea2f..4947cf07406 100644 --- a/peer/channel/create.go +++ b/peer/channel/create.go @@ -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) diff --git a/peer/sharedconfig/template_test.go b/peer/sharedconfig/template_test.go index 92c15cf3a81..929f7ba7e52 100644 --- a/peer/sharedconfig/template_test.go +++ b/peer/sharedconfig/template_test.go @@ -23,8 +23,5 @@ import ( ) func TestTemplate(t *testing.T) { - configtxtest.WriteTemplate( - "../../common/configtx/test/"+configtxtest.PeerTemplateName, - DefaultAnchorPeers(), - ) + configtxtest.WriteTemplate(configtxtest.PeerTemplateName, DefaultAnchorPeers()) }