github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/configtx/template_test.go (about)

     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 configtx
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric/common/config"
    24  	cb "github.com/hyperledger/fabric/protos/common"
    25  
    26  	"github.com/golang/protobuf/proto"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func verifyItemsResult(t *testing.T, template Template, count int) {
    31  	newChainID := "foo"
    32  
    33  	configEnv, err := template.Envelope(newChainID)
    34  	if err != nil {
    35  		t.Fatalf("Should not have errored: %s", err)
    36  	}
    37  
    38  	configNext, err := UnmarshalConfigUpdate(configEnv.ConfigUpdate)
    39  	if err != nil {
    40  		t.Fatalf("Should not have errored: %s", err)
    41  	}
    42  
    43  	assert.Equal(t, len(configNext.WriteSet.Values), count, "Not the right number of config values")
    44  
    45  	for i := 0; i < len(configNext.WriteSet.Values); i++ {
    46  		_, ok := configNext.WriteSet.Values[fmt.Sprintf("%d", i)]
    47  		assert.True(t, ok, "Expected %d but did not find it", i)
    48  	}
    49  }
    50  
    51  func simpleGroup(index int) *cb.ConfigGroup {
    52  	group := cb.NewConfigGroup()
    53  	group.Values[fmt.Sprintf("%d", index)] = &cb.ConfigValue{}
    54  	return group
    55  }
    56  
    57  func TestSimpleTemplate(t *testing.T) {
    58  	simple := NewSimpleTemplate(
    59  		simpleGroup(0),
    60  		simpleGroup(1),
    61  	)
    62  	verifyItemsResult(t, simple, 2)
    63  }
    64  
    65  func TestCompositeTemplate(t *testing.T) {
    66  	composite := NewCompositeTemplate(
    67  		NewSimpleTemplate(
    68  			simpleGroup(0),
    69  			simpleGroup(1),
    70  		),
    71  		NewSimpleTemplate(
    72  			simpleGroup(2),
    73  		),
    74  	)
    75  
    76  	verifyItemsResult(t, composite, 3)
    77  }
    78  
    79  func TestModPolicySettingTemplate(t *testing.T) {
    80  	subGroup := "group"
    81  	input := cb.NewConfigGroup()
    82  	input.Groups[subGroup] = cb.NewConfigGroup()
    83  
    84  	policyName := "policy"
    85  	valueName := "value"
    86  	for _, group := range []*cb.ConfigGroup{input, input.Groups[subGroup]} {
    87  		group.Values[valueName] = &cb.ConfigValue{}
    88  		group.Policies[policyName] = &cb.ConfigPolicy{}
    89  	}
    90  
    91  	modPolicyName := "foo"
    92  	mpst := NewModPolicySettingTemplate(modPolicyName, NewSimpleTemplate(input))
    93  	output, err := mpst.Envelope("bar")
    94  	assert.NoError(t, err, "Creating envelope")
    95  
    96  	configUpdate := UnmarshalConfigUpdateOrPanic(output.ConfigUpdate)
    97  
    98  	assert.Equal(t, modPolicyName, configUpdate.WriteSet.ModPolicy)
    99  	assert.Equal(t, modPolicyName, configUpdate.WriteSet.Values[valueName].ModPolicy)
   100  	assert.Equal(t, modPolicyName, configUpdate.WriteSet.Policies[policyName].ModPolicy)
   101  	assert.Equal(t, modPolicyName, configUpdate.WriteSet.Groups[subGroup].ModPolicy)
   102  	assert.Equal(t, modPolicyName, configUpdate.WriteSet.Groups[subGroup].Values[valueName].ModPolicy)
   103  	assert.Equal(t, modPolicyName, configUpdate.WriteSet.Groups[subGroup].Policies[policyName].ModPolicy)
   104  }
   105  
   106  func TestNewChainTemplate(t *testing.T) {
   107  	consortiumName := "Test"
   108  	orgs := []string{"org1", "org2", "org3"}
   109  	nct := NewChainCreationTemplate(consortiumName, orgs)
   110  
   111  	newChainID := "foo"
   112  	configEnv, err := nct.Envelope(newChainID)
   113  	if err != nil {
   114  		t.Fatalf("Error creation a chain creation config")
   115  	}
   116  
   117  	configUpdate, err := UnmarshalConfigUpdate(configEnv.ConfigUpdate)
   118  	if err != nil {
   119  		t.Fatalf("Should not have errored: %s", err)
   120  	}
   121  
   122  	consortiumProto := &cb.Consortium{}
   123  	err = proto.Unmarshal(configUpdate.WriteSet.Values[config.ConsortiumKey].Value, consortiumProto)
   124  	assert.NoError(t, err)
   125  	assert.Equal(t, consortiumName, consortiumProto.Name, "Should have set correct consortium name")
   126  
   127  	assert.Equal(t, configUpdate.WriteSet.Groups[config.ApplicationGroupKey].Version, uint64(1))
   128  
   129  	assert.Len(t, configUpdate.WriteSet.Groups[config.ApplicationGroupKey].Groups, len(orgs))
   130  
   131  	for _, org := range orgs {
   132  		_, ok := configUpdate.WriteSet.Groups[config.ApplicationGroupKey].Groups[org]
   133  		assert.True(t, ok, "Expected to find %s but did not", org)
   134  	}
   135  }