github.com/pvitto98/fabric@v2.1.1+incompatible/common/configtx/configmap_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package configtx
     8  
     9  import (
    10  	"testing"
    11  
    12  	cb "github.com/hyperledger/fabric-protos-go/common"
    13  	"github.com/hyperledger/fabric/protoutil"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestBadKey(t *testing.T) {
    18  	assert.Error(t, addToMap(comparable{key: "[Label]", path: []string{}}, make(map[string]comparable)),
    19  		"Should have errored on key with illegal characters")
    20  }
    21  
    22  func TestConfigMapMultiGroup(t *testing.T) {
    23  	config := protoutil.NewConfigGroup()
    24  	config.Groups["0"] = protoutil.NewConfigGroup()
    25  	config.Groups["0"].Groups["1"] = protoutil.NewConfigGroup()
    26  	config.Groups["0"].Groups["1"].Groups["2.1"] = protoutil.NewConfigGroup()
    27  	config.Groups["0"].Groups["1"].Groups["2.1"].Values["Value"] = &cb.ConfigValue{}
    28  	config.Groups["0"].Groups["1"].Groups["2.2"] = protoutil.NewConfigGroup()
    29  	config.Groups["0"].Groups["1"].Groups["2.2"].Values["Value"] = &cb.ConfigValue{}
    30  
    31  	confMap, err := mapConfig(config, "Channel")
    32  	assert.NoError(t, err)
    33  	assert.Equal(t, []string{"Channel", "0", "1", "2.1"}, confMap["[Value]  /Channel/0/1/2.1/Value"].path)
    34  	assert.Equal(t, []string{"Channel", "0", "1", "2.2"}, confMap["[Value]  /Channel/0/1/2.2/Value"].path)
    35  }
    36  
    37  func TestConfigMap(t *testing.T) {
    38  	config := protoutil.NewConfigGroup()
    39  	config.Groups["0DeepGroup"] = protoutil.NewConfigGroup()
    40  	config.Values["0DeepValue1"] = &cb.ConfigValue{}
    41  	config.Values["0DeepValue2"] = &cb.ConfigValue{}
    42  	config.Groups["0DeepGroup"].Policies["1DeepPolicy"] = &cb.ConfigPolicy{}
    43  	config.Groups["0DeepGroup"].Groups["1DeepGroup"] = protoutil.NewConfigGroup()
    44  	config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"] = &cb.ConfigValue{}
    45  
    46  	confMap, err := mapConfig(config, "Channel")
    47  	assert.NoError(t, err, "Should not have errored building map")
    48  
    49  	assert.Len(t, confMap, 7, "There should be 7 entries in the config map")
    50  
    51  	assert.Equal(t, comparable{key: "Channel", path: []string{}, ConfigGroup: config},
    52  		confMap["[Group]  /Channel"])
    53  	assert.Equal(t, comparable{key: "0DeepGroup", path: []string{"Channel"}, ConfigGroup: config.Groups["0DeepGroup"]},
    54  		confMap["[Group]  /Channel/0DeepGroup"])
    55  	assert.Equal(t, comparable{key: "0DeepValue1", path: []string{"Channel"}, ConfigValue: config.Values["0DeepValue1"]},
    56  		confMap["[Value]  /Channel/0DeepValue1"])
    57  	assert.Equal(t, comparable{key: "0DeepValue2", path: []string{"Channel"}, ConfigValue: config.Values["0DeepValue2"]},
    58  		confMap["[Value]  /Channel/0DeepValue2"])
    59  	assert.Equal(t, comparable{key: "1DeepPolicy", path: []string{"Channel", "0DeepGroup"}, ConfigPolicy: config.Groups["0DeepGroup"].Policies["1DeepPolicy"]},
    60  		confMap["[Policy] /Channel/0DeepGroup/1DeepPolicy"])
    61  	assert.Equal(t, comparable{key: "1DeepGroup", path: []string{"Channel", "0DeepGroup"}, ConfigGroup: config.Groups["0DeepGroup"].Groups["1DeepGroup"]},
    62  		confMap["[Group]  /Channel/0DeepGroup/1DeepGroup"])
    63  	assert.Equal(t, comparable{key: "2DeepValue", path: []string{"Channel", "0DeepGroup", "1DeepGroup"}, ConfigValue: config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"]},
    64  		confMap["[Value]  /Channel/0DeepGroup/1DeepGroup/2DeepValue"])
    65  }
    66  
    67  func TestMapConfigBack(t *testing.T) {
    68  	config := protoutil.NewConfigGroup()
    69  	config.Groups["0DeepGroup"] = protoutil.NewConfigGroup()
    70  	config.Values["0DeepValue1"] = &cb.ConfigValue{}
    71  	config.Values["0DeepValue2"] = &cb.ConfigValue{}
    72  	config.Groups["0DeepGroup"].Policies["1DeepPolicy"] = &cb.ConfigPolicy{}
    73  	config.Groups["0DeepGroup"].Groups["1DeepGroup"] = protoutil.NewConfigGroup()
    74  	config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"] = &cb.ConfigValue{}
    75  
    76  	confMap, err := mapConfig(config, "Channel")
    77  	assert.NoError(t, err, "Should not have errored building map")
    78  
    79  	newConfig, err := configMapToConfig(confMap, "Channel")
    80  	assert.NoError(t, err, "Should not have errored building config")
    81  
    82  	assert.Equal(t, config, newConfig, "Should have transformed config map back from confMap")
    83  
    84  	newConfig.Values["Value"] = &cb.ConfigValue{}
    85  	assert.NotEqual(t, config, newConfig, "Mutating the new config should not mutate the existing config")
    86  }
    87  
    88  func TestHackInmapConfigBack(t *testing.T) {
    89  	config := protoutil.NewConfigGroup()
    90  	config.Values["ChannelValue1"] = &cb.ConfigValue{}
    91  	config.Values["ChannelValue2"] = &cb.ConfigValue{}
    92  	config.Groups["Orderer"] = protoutil.NewConfigGroup()
    93  	config.Groups["Orderer"].Values["Capabilities"] = &cb.ConfigValue{}
    94  	config.Groups["Orderer"].Policies["OrdererPolicy"] = &cb.ConfigPolicy{}
    95  	config.Groups["Orderer"].Groups["OrdererOrg"] = protoutil.NewConfigGroup()
    96  	config.Groups["Orderer"].Groups["OrdererOrg"].Values["OrdererOrgValue"] = &cb.ConfigValue{}
    97  	config.Groups["Application"] = protoutil.NewConfigGroup()
    98  	config.Groups["Application"].Policies["ApplicationPolicy"] = &cb.ConfigPolicy{}
    99  	config.Groups["Application"].Policies["ApplicationValue"] = &cb.ConfigPolicy{}
   100  
   101  	confMap, err := mapConfig(config, "Channel")
   102  	assert.NoError(t, err, "Should not have errored building map")
   103  
   104  	newConfig, err := configMapToConfig(confMap, "Channel")
   105  	assert.NoError(t, err, "Should not have errored building config")
   106  
   107  	var checkModPolicy func(cg *cb.ConfigGroup)
   108  	checkModPolicy = func(cg *cb.ConfigGroup) {
   109  		assert.NotEmpty(t, cg.ModPolicy, "empty group mod_policy")
   110  
   111  		for key, value := range cg.Values {
   112  			assert.NotEmpty(t, value.ModPolicy, "empty value mod_policy %s", key)
   113  		}
   114  
   115  		for key, policy := range cg.Policies {
   116  			assert.NotEmpty(t, policy.ModPolicy, "empty policy mod_policy %s", key)
   117  		}
   118  
   119  		for _, group := range cg.Groups {
   120  			checkModPolicy(group)
   121  		}
   122  	}
   123  
   124  	checkModPolicy(newConfig)
   125  }