github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/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  	"github.com/stretchr/testify/assert"
    23  
    24  	cb "github.com/hyperledger/fabric/protos/common"
    25  )
    26  
    27  func TestBadKey(t *testing.T) {
    28  	assert.Error(t, addToMap(comparable{key: "[Label]", path: []string{}}, make(map[string]comparable)),
    29  		"Should have errored on key with illegal characters")
    30  }
    31  
    32  func TestConfigMap(t *testing.T) {
    33  	config := cb.NewConfigGroup()
    34  	config.Groups["0DeepGroup"] = cb.NewConfigGroup()
    35  	config.Values["0DeepValue1"] = &cb.ConfigValue{}
    36  	config.Values["0DeepValue2"] = &cb.ConfigValue{}
    37  	config.Groups["0DeepGroup"].Policies["1DeepPolicy"] = &cb.ConfigPolicy{}
    38  	config.Groups["0DeepGroup"].Groups["1DeepGroup"] = cb.NewConfigGroup()
    39  	config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"] = &cb.ConfigValue{}
    40  
    41  	confMap, err := MapConfig(config)
    42  	assert.NoError(t, err, "Should not have errored building map")
    43  
    44  	assert.Len(t, confMap, 7, "There should be 7 entries in the config map")
    45  
    46  	assert.Equal(t, comparable{key: "Channel", path: []string{}, ConfigGroup: config},
    47  		confMap["[Groups] /Channel"])
    48  	assert.Equal(t, comparable{key: "0DeepGroup", path: []string{"Channel"}, ConfigGroup: config.Groups["0DeepGroup"]},
    49  		confMap["[Groups] /Channel/0DeepGroup"])
    50  	assert.Equal(t, comparable{key: "0DeepValue1", path: []string{"Channel"}, ConfigValue: config.Values["0DeepValue1"]},
    51  		confMap["[Values] /Channel/0DeepValue1"])
    52  	assert.Equal(t, comparable{key: "0DeepValue2", path: []string{"Channel"}, ConfigValue: config.Values["0DeepValue2"]},
    53  		confMap["[Values] /Channel/0DeepValue2"])
    54  	assert.Equal(t, comparable{key: "1DeepPolicy", path: []string{"Channel", "0DeepGroup"}, ConfigPolicy: config.Groups["0DeepGroup"].Policies["1DeepPolicy"]},
    55  		confMap["[Policy] /Channel/0DeepGroup/1DeepPolicy"])
    56  	assert.Equal(t, comparable{key: "1DeepGroup", path: []string{"Channel", "0DeepGroup"}, ConfigGroup: config.Groups["0DeepGroup"].Groups["1DeepGroup"]},
    57  		confMap["[Groups] /Channel/0DeepGroup/1DeepGroup"])
    58  	assert.Equal(t, comparable{key: "2DeepValue", path: []string{"Channel", "0DeepGroup", "1DeepGroup"}, ConfigValue: config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"]},
    59  		confMap["[Values] /Channel/0DeepGroup/1DeepGroup/2DeepValue"])
    60  }
    61  
    62  func TestMapConfigBack(t *testing.T) {
    63  	config := cb.NewConfigGroup()
    64  	config.Groups["0DeepGroup"] = cb.NewConfigGroup()
    65  	config.Values["0DeepValue1"] = &cb.ConfigValue{}
    66  	config.Values["0DeepValue2"] = &cb.ConfigValue{}
    67  	config.Groups["0DeepGroup"].Policies["1DeepPolicy"] = &cb.ConfigPolicy{}
    68  	config.Groups["0DeepGroup"].Groups["1DeepGroup"] = cb.NewConfigGroup()
    69  	config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"] = &cb.ConfigValue{}
    70  
    71  	confMap, err := MapConfig(config)
    72  	assert.NoError(t, err, "Should not have errored building map")
    73  
    74  	newConfig, err := configMapToConfig(confMap)
    75  	assert.NoError(t, err, "Should not have errored building config")
    76  
    77  	assert.Equal(t, config, newConfig, "Should have transformed config map back from confMap")
    78  }