github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/common/configtx/configmap_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 "testing" 21 22 cb "github.com/hyperledger/fabric/protos/common" 23 24 logging "github.com/op/go-logging" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func init() { 29 logging.SetLevel(logging.DEBUG, "") 30 } 31 32 func TestBadKey(t *testing.T) { 33 assert.Error(t, addToMap(comparable{key: "[Label]", path: []string{}}, make(map[string]comparable)), 34 "Should have errored on key with illegal characters") 35 } 36 37 func TestConfigMapMultiGroup(t *testing.T) { 38 config := cb.NewConfigGroup() 39 config.Groups["0"] = cb.NewConfigGroup() 40 config.Groups["0"].Groups["1"] = cb.NewConfigGroup() 41 config.Groups["0"].Groups["1"].Groups["2.1"] = cb.NewConfigGroup() 42 config.Groups["0"].Groups["1"].Groups["2.1"].Values["Value"] = &cb.ConfigValue{} 43 config.Groups["0"].Groups["1"].Groups["2.2"] = cb.NewConfigGroup() 44 config.Groups["0"].Groups["1"].Groups["2.2"].Values["Value"] = &cb.ConfigValue{} 45 46 confMap, err := MapConfig(config) 47 assert.NoError(t, err) 48 assert.Equal(t, []string{"Channel", "0", "1", "2.1"}, confMap["[Values] /Channel/0/1/2.1/Value"].path) 49 assert.Equal(t, []string{"Channel", "0", "1", "2.2"}, confMap["[Values] /Channel/0/1/2.2/Value"].path) 50 } 51 52 func TestConfigMap(t *testing.T) { 53 config := cb.NewConfigGroup() 54 config.Groups["0DeepGroup"] = cb.NewConfigGroup() 55 config.Values["0DeepValue1"] = &cb.ConfigValue{} 56 config.Values["0DeepValue2"] = &cb.ConfigValue{} 57 config.Groups["0DeepGroup"].Policies["1DeepPolicy"] = &cb.ConfigPolicy{} 58 config.Groups["0DeepGroup"].Groups["1DeepGroup"] = cb.NewConfigGroup() 59 config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"] = &cb.ConfigValue{} 60 61 confMap, err := MapConfig(config) 62 assert.NoError(t, err, "Should not have errored building map") 63 64 assert.Len(t, confMap, 7, "There should be 7 entries in the config map") 65 66 assert.Equal(t, comparable{key: "Channel", path: []string{}, ConfigGroup: config}, 67 confMap["[Groups] /Channel"]) 68 assert.Equal(t, comparable{key: "0DeepGroup", path: []string{"Channel"}, ConfigGroup: config.Groups["0DeepGroup"]}, 69 confMap["[Groups] /Channel/0DeepGroup"]) 70 assert.Equal(t, comparable{key: "0DeepValue1", path: []string{"Channel"}, ConfigValue: config.Values["0DeepValue1"]}, 71 confMap["[Values] /Channel/0DeepValue1"]) 72 assert.Equal(t, comparable{key: "0DeepValue2", path: []string{"Channel"}, ConfigValue: config.Values["0DeepValue2"]}, 73 confMap["[Values] /Channel/0DeepValue2"]) 74 assert.Equal(t, comparable{key: "1DeepPolicy", path: []string{"Channel", "0DeepGroup"}, ConfigPolicy: config.Groups["0DeepGroup"].Policies["1DeepPolicy"]}, 75 confMap["[Policy] /Channel/0DeepGroup/1DeepPolicy"]) 76 assert.Equal(t, comparable{key: "1DeepGroup", path: []string{"Channel", "0DeepGroup"}, ConfigGroup: config.Groups["0DeepGroup"].Groups["1DeepGroup"]}, 77 confMap["[Groups] /Channel/0DeepGroup/1DeepGroup"]) 78 assert.Equal(t, comparable{key: "2DeepValue", path: []string{"Channel", "0DeepGroup", "1DeepGroup"}, ConfigValue: config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"]}, 79 confMap["[Values] /Channel/0DeepGroup/1DeepGroup/2DeepValue"]) 80 } 81 82 func TestMapConfigBack(t *testing.T) { 83 config := cb.NewConfigGroup() 84 config.Groups["0DeepGroup"] = cb.NewConfigGroup() 85 config.Values["0DeepValue1"] = &cb.ConfigValue{} 86 config.Values["0DeepValue2"] = &cb.ConfigValue{} 87 config.Groups["0DeepGroup"].Policies["1DeepPolicy"] = &cb.ConfigPolicy{} 88 config.Groups["0DeepGroup"].Groups["1DeepGroup"] = cb.NewConfigGroup() 89 config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"] = &cb.ConfigValue{} 90 91 confMap, err := MapConfig(config) 92 assert.NoError(t, err, "Should not have errored building map") 93 94 newConfig, err := configMapToConfig(confMap) 95 assert.NoError(t, err, "Should not have errored building config") 96 97 assert.Equal(t, config, newConfig, "Should have transformed config map back from confMap") 98 99 newConfig.Values["Value"] = &cb.ConfigValue{} 100 assert.NotEqual(t, config, newConfig, "Mutating the new config should not mutate the existing config") 101 }