github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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/golang/protobuf/proto"
    24  	configtxorderer "github.com/hyperledger/fabric/common/configvalues/channel/orderer"
    25  	cb "github.com/hyperledger/fabric/protos/common"
    26  	ab "github.com/hyperledger/fabric/protos/orderer"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func verifyItemsResult(t *testing.T, template Template, count int) {
    32  	newChainID := "foo"
    33  
    34  	configEnv, err := template.Envelope(newChainID)
    35  	if err != nil {
    36  		t.Fatalf("Should not have errored: %s", err)
    37  	}
    38  
    39  	configNext, err := UnmarshalConfigUpdate(configEnv.ConfigUpdate)
    40  	if err != nil {
    41  		t.Fatalf("Should not have errored: %s", err)
    42  	}
    43  
    44  	assert.Equal(t, len(configNext.WriteSet.Values), count, "Not the right number of config values")
    45  
    46  	for i := 0; i < len(configNext.WriteSet.Values); i++ {
    47  		_, ok := configNext.WriteSet.Values[fmt.Sprintf("%d", i)]
    48  		assert.True(t, ok, "Expected %d but did not find it", i)
    49  	}
    50  }
    51  
    52  func simpleGroup(index int) *cb.ConfigGroup {
    53  	group := cb.NewConfigGroup()
    54  	group.Values[fmt.Sprintf("%d", index)] = &cb.ConfigValue{}
    55  	return group
    56  }
    57  
    58  func TestSimpleTemplate(t *testing.T) {
    59  	simple := NewSimpleTemplate(
    60  		simpleGroup(0),
    61  		simpleGroup(1),
    62  	)
    63  	verifyItemsResult(t, simple, 2)
    64  }
    65  
    66  func TestCompositeTemplate(t *testing.T) {
    67  	composite := NewCompositeTemplate(
    68  		NewSimpleTemplate(
    69  			simpleGroup(0),
    70  			simpleGroup(1),
    71  		),
    72  		NewSimpleTemplate(
    73  			simpleGroup(2),
    74  		),
    75  	)
    76  
    77  	verifyItemsResult(t, composite, 3)
    78  }
    79  
    80  func TestNewChainTemplate(t *testing.T) {
    81  	simple := NewSimpleTemplate(
    82  		simpleGroup(0),
    83  		simpleGroup(1),
    84  	)
    85  
    86  	creationPolicy := "Test"
    87  	nct := NewChainCreationTemplate(creationPolicy, simple)
    88  
    89  	newChainID := "foo"
    90  	configEnv, err := nct.Envelope(newChainID)
    91  	if err != nil {
    92  		t.Fatalf("Error creation a chain creation config")
    93  	}
    94  
    95  	configNext, err := UnmarshalConfigUpdate(configEnv.ConfigUpdate)
    96  	if err != nil {
    97  		t.Fatalf("Should not have errored: %s", err)
    98  	}
    99  
   100  	assert.Equal(t, len(configNext.WriteSet.Values), 2, "Not the right number of config values")
   101  
   102  	for i := 0; i < 2; i++ {
   103  		_, ok := configNext.WriteSet.Values[fmt.Sprintf("%d", i)]
   104  		assert.True(t, ok, "Expected to find %d but did not", i)
   105  	}
   106  
   107  	configValue, ok := configNext.WriteSet.Groups[configtxorderer.GroupKey].Values[CreationPolicyKey]
   108  	assert.True(t, ok, "Did not find creation policy")
   109  
   110  	creationPolicyMessage := new(ab.CreationPolicy)
   111  	if err := proto.Unmarshal(configValue.Value, creationPolicyMessage); err != nil {
   112  		t.Fatal("Should not have errored:", err)
   113  	}
   114  	assert.Equal(t, creationPolicy, creationPolicyMessage.Policy, "Policy names don't match")
   115  }