|
| 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 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx" |
| 24 | + mockpolicies "github.com/hyperledger/fabric/common/mocks/policies" |
| 25 | + cb "github.com/hyperledger/fabric/protos/common" |
| 26 | + |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | +) |
| 29 | + |
| 30 | +func TestPolicyForItem(t *testing.T) { |
| 31 | + // Policies are set to different error values to differentiate them in equal assertion |
| 32 | + rootPolicy := &mockpolicies.Policy{Err: fmt.Errorf("rootPolicy")} |
| 33 | + fooPolicy := &mockpolicies.Policy{Err: fmt.Errorf("fooPolicy")} |
| 34 | + |
| 35 | + cm := &configManager{ |
| 36 | + Resources: &mockconfigtx.Resources{ |
| 37 | + PolicyManagerVal: &mockpolicies.Manager{ |
| 38 | + BasePathVal: "root", |
| 39 | + Policy: rootPolicy, |
| 40 | + SubManagersMap: map[string]*mockpolicies.Manager{ |
| 41 | + "foo": &mockpolicies.Manager{ |
| 42 | + Policy: fooPolicy, |
| 43 | + BasePathVal: "foo", |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + policy, ok := cm.policyForItem(comparable{ |
| 51 | + path: []string{"root"}, |
| 52 | + ConfigValue: &cb.ConfigValue{ |
| 53 | + ModPolicy: "rootPolicy", |
| 54 | + }, |
| 55 | + }) |
| 56 | + assert.True(t, ok) |
| 57 | + assert.Equal(t, policy, rootPolicy, "Should have found relative policy off the root manager") |
| 58 | + |
| 59 | + policy, ok = cm.policyForItem(comparable{ |
| 60 | + path: []string{"root", "wrong"}, |
| 61 | + ConfigValue: &cb.ConfigValue{ |
| 62 | + ModPolicy: "rootPolicy", |
| 63 | + }, |
| 64 | + }) |
| 65 | + assert.False(t, ok, "Should not have found rootPolicy off a non-existant manager") |
| 66 | + |
| 67 | + policy, ok = cm.policyForItem(comparable{ |
| 68 | + path: []string{"root", "foo"}, |
| 69 | + ConfigValue: &cb.ConfigValue{ |
| 70 | + ModPolicy: "foo", |
| 71 | + }, |
| 72 | + }) |
| 73 | + assert.True(t, ok) |
| 74 | + assert.Equal(t, policy, fooPolicy, "Should not have found relative foo policy the foo manager") |
| 75 | + |
| 76 | + policy, ok = cm.policyForItem(comparable{ |
| 77 | + path: []string{"root", "foo"}, |
| 78 | + ConfigValue: &cb.ConfigValue{ |
| 79 | + ModPolicy: "/rootPolicy", |
| 80 | + }, |
| 81 | + }) |
| 82 | + assert.True(t, ok) |
| 83 | + assert.Equal(t, policy, rootPolicy, "Should not have found absolute root policy from the foo path position") |
| 84 | +} |
0 commit comments