Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-2197] Factor out configmap construction
https://jira.hyperledger.org/browse/FAB-2197 The configtx processing currently performs validation while applying configuration. In order to split this process, the config map construction will need to be used in two different code paths, so this CR factors it out. Change-Id: Ib230c3f363d490fb89b83fcd64bf2c9ec4fc67a4 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
- Loading branch information
Jason Yellick
committed
Feb 14, 2017
1 parent
c16f5b3
commit 75327ff
Showing
5 changed files
with
201 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
Copyright IBM Corp. 2017 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package configtx | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
cb "github.com/hyperledger/fabric/protos/common" | ||
) | ||
|
||
const ( | ||
RootGroupKey = "Channel" | ||
|
||
GroupPrefix = "[Groups] " | ||
ValuePrefix = "[Values] " | ||
PolicyPrefix = "[Policy] " // The plurarility doesn't match, but, it makes the logs much easier being the same lenght as "Groups" and "Values" | ||
|
||
PathSeparator = "/" | ||
) | ||
|
||
// mapConfig is the only method in this file intended to be called outside this file | ||
// it takes a ConfigGroup and generates a map of fqPath to comparables (or error on invalid keys) | ||
func mapConfig(channelGroup *cb.ConfigGroup) (map[string]comparable, error) { | ||
result := make(map[string]comparable) | ||
err := recurseConfig(result, []string{RootGroupKey}, channelGroup) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return result, nil | ||
} | ||
|
||
func addToMap(cg comparable, result map[string]comparable) error { | ||
var fqPath string | ||
|
||
switch { | ||
case cg.ConfigGroup != nil: | ||
fqPath = GroupPrefix | ||
case cg.ConfigValue != nil: | ||
fqPath = ValuePrefix | ||
case cg.ConfigPolicy != nil: | ||
fqPath = PolicyPrefix | ||
} | ||
|
||
// TODO rename validateChainID to validateConfigID | ||
if err := validateChainID(cg.key); err != nil { | ||
return fmt.Errorf("Illegal characters in key: %s", fqPath) | ||
} | ||
|
||
if len(cg.path) == 0 { | ||
fqPath += PathSeparator + cg.key | ||
} else { | ||
fqPath += PathSeparator + strings.Join(cg.path, PathSeparator) + PathSeparator + cg.key | ||
} | ||
|
||
logger.Debugf("Adding to config map: %s", fqPath) | ||
|
||
result[fqPath] = cg | ||
|
||
return nil | ||
} | ||
|
||
func recurseConfig(result map[string]comparable, path []string, group *cb.ConfigGroup) error { | ||
if err := addToMap(comparable{key: path[len(path)-1], path: path[:len(path)-1], ConfigGroup: group}, result); err != nil { | ||
return err | ||
} | ||
|
||
for key, group := range group.Groups { | ||
nextPath := append(path, key) | ||
if err := recurseConfig(result, nextPath, group); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for key, value := range group.Values { | ||
if err := addToMap(comparable{key: key, path: path, ConfigValue: value}, result); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for key, policy := range group.Policies { | ||
if err := addToMap(comparable{key: key, path: path, ConfigPolicy: policy}, result); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright IBM Corp. 2017 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package configtx | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
|
||
cb "github.com/hyperledger/fabric/protos/common" | ||
) | ||
|
||
func TestBadKey(t *testing.T) { | ||
assert.Error(t, addToMap(comparable{key: "[Label]", path: []string{}}, make(map[string]comparable)), | ||
"Should have errored on key with illegal characters") | ||
} | ||
|
||
func TestConfigMap(t *testing.T) { | ||
config := cb.NewConfigGroup() | ||
config.Groups["0DeepGroup"] = cb.NewConfigGroup() | ||
config.Values["0DeepValue1"] = &cb.ConfigValue{} | ||
config.Values["0DeepValue2"] = &cb.ConfigValue{} | ||
config.Groups["0DeepGroup"].Policies["1DeepPolicy"] = &cb.ConfigPolicy{} | ||
config.Groups["0DeepGroup"].Groups["1DeepGroup"] = cb.NewConfigGroup() | ||
config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"] = &cb.ConfigValue{} | ||
|
||
confMap, err := mapConfig(config) | ||
assert.NoError(t, err, "Should not have errored building map") | ||
|
||
assert.Len(t, confMap, 7, "There should be 7 entries in the config map") | ||
|
||
assert.Equal(t, comparable{key: "Channel", path: []string{}, ConfigGroup: config}, | ||
confMap["[Groups] /Channel"]) | ||
assert.Equal(t, comparable{key: "0DeepGroup", path: []string{"Channel"}, ConfigGroup: config.Groups["0DeepGroup"]}, | ||
confMap["[Groups] /Channel/0DeepGroup"]) | ||
assert.Equal(t, comparable{key: "0DeepValue1", path: []string{"Channel"}, ConfigValue: config.Values["0DeepValue1"]}, | ||
confMap["[Values] /Channel/0DeepValue1"]) | ||
assert.Equal(t, comparable{key: "0DeepValue2", path: []string{"Channel"}, ConfigValue: config.Values["0DeepValue2"]}, | ||
confMap["[Values] /Channel/0DeepValue2"]) | ||
assert.Equal(t, comparable{key: "1DeepPolicy", path: []string{"Channel", "0DeepGroup"}, ConfigPolicy: config.Groups["0DeepGroup"].Policies["1DeepPolicy"]}, | ||
confMap["[Policy] /Channel/0DeepGroup/1DeepPolicy"]) | ||
assert.Equal(t, comparable{key: "1DeepGroup", path: []string{"Channel", "0DeepGroup"}, ConfigGroup: config.Groups["0DeepGroup"].Groups["1DeepGroup"]}, | ||
confMap["[Groups] /Channel/0DeepGroup/1DeepGroup"]) | ||
assert.Equal(t, comparable{key: "2DeepValue", path: []string{"Channel", "0DeepGroup", "1DeepGroup"}, ConfigValue: config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"]}, | ||
confMap["[Values] /Channel/0DeepGroup/1DeepGroup/2DeepValue"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters