|
| 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 config_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "testing" |
| 22 | + |
| 23 | + . "github.com/hyperledger/fabric/common/config" |
| 24 | + "github.com/hyperledger/fabric/common/config/msp" |
| 25 | + "github.com/hyperledger/fabric/common/configtx" |
| 26 | + "github.com/hyperledger/fabric/common/configtx/tool/localconfig" |
| 27 | + "github.com/hyperledger/fabric/common/configtx/tool/provisional" |
| 28 | + cb "github.com/hyperledger/fabric/protos/common" |
| 29 | + |
| 30 | + "github.com/stretchr/testify/assert" |
| 31 | +) |
| 32 | + |
| 33 | +func init() { |
| 34 | + // We are in fabric/common/config/ but need to execute from fabric/ |
| 35 | + err := os.Chdir("../..") |
| 36 | + if err != nil { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +const tx = "foo" |
| 42 | + |
| 43 | +func driveConfig(t *testing.T, configGroup *cb.ConfigGroup, proposer ValueProposer) { |
| 44 | + var groups []string |
| 45 | + for key := range configGroup.Groups { |
| 46 | + groups = append(groups, key) |
| 47 | + } |
| 48 | + |
| 49 | + vd, vp, err := proposer.BeginValueProposals(tx, groups) |
| 50 | + assert.NoError(t, err, "BeginValueProposal failed") |
| 51 | + |
| 52 | + for i, groupName := range groups { |
| 53 | + driveConfig(t, configGroup.Groups[groupName], vp[i]) |
| 54 | + } |
| 55 | + |
| 56 | + for key, value := range configGroup.Values { |
| 57 | + _, err := vd.Deserialize(key, value.Value) |
| 58 | + assert.NoError(t, err, "Value failed to deserialize") |
| 59 | + } |
| 60 | + |
| 61 | + err = proposer.PreCommit(tx) |
| 62 | + assert.NoError(t, err, "PreCommit failed") |
| 63 | + |
| 64 | + proposer.CommitProposals(tx) |
| 65 | +} |
| 66 | + |
| 67 | +func commonTest(t *testing.T, profile string) { |
| 68 | + conf := localconfig.Load(profile) |
| 69 | + configUpdateEnv, err := provisional.New(conf).ChannelTemplate().Envelope("foo") |
| 70 | + assert.NoError(t, err, "Generating config failed") |
| 71 | + |
| 72 | + configUpdate := configtx.UnmarshalConfigUpdateOrPanic(configUpdateEnv.ConfigUpdate) |
| 73 | + root := NewRoot(msp.NewMSPConfigHandler()) |
| 74 | + preChannel := cb.NewConfigGroup() |
| 75 | + preChannel.Groups[ChannelGroupKey] = configUpdate.WriteSet |
| 76 | + driveConfig(t, preChannel, root) |
| 77 | +} |
| 78 | + |
| 79 | +func TestSimpleConfig(t *testing.T) { |
| 80 | + commonTest(t, localconfig.SampleInsecureProfile) |
| 81 | +} |
| 82 | + |
| 83 | +func TestOneMSPConfig(t *testing.T) { |
| 84 | + commonTest(t, localconfig.SampleSingleMSPSoloProfile) |
| 85 | +} |
0 commit comments