|
| 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 |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + api "github.com/hyperledger/fabric/common/configvalues" |
| 24 | + cb "github.com/hyperledger/fabric/protos/common" |
| 25 | + "github.com/hyperledger/fabric/protos/utils" |
| 26 | + |
| 27 | + "github.com/golang/protobuf/proto" |
| 28 | + logging "github.com/op/go-logging" |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | +) |
| 31 | + |
| 32 | +func init() { |
| 33 | + logging.SetLevel(logging.DEBUG, "") |
| 34 | +} |
| 35 | + |
| 36 | +type mockValues struct { |
| 37 | + ProtoMsgMap map[string]proto.Message |
| 38 | + ValidateReturn error |
| 39 | +} |
| 40 | + |
| 41 | +func (v *mockValues) ProtoMsg(key string) (proto.Message, bool) { |
| 42 | + msg, ok := v.ProtoMsgMap[key] |
| 43 | + return msg, ok |
| 44 | +} |
| 45 | + |
| 46 | +func (v *mockValues) Validate() error { |
| 47 | + return v.ValidateReturn |
| 48 | +} |
| 49 | + |
| 50 | +func (v *mockValues) Commit() {} |
| 51 | + |
| 52 | +func newMockValues() *mockValues { |
| 53 | + return &mockValues{ |
| 54 | + ProtoMsgMap: make(map[string]proto.Message), |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +type mockHandler struct { |
| 59 | + AllocateReturn *mockValues |
| 60 | + NewGroupMap map[string]api.ValueProposer |
| 61 | + NewGroupError error |
| 62 | +} |
| 63 | + |
| 64 | +func (h *mockHandler) Allocate() Values { |
| 65 | + return h.AllocateReturn |
| 66 | +} |
| 67 | + |
| 68 | +func (h *mockHandler) NewGroup(name string) (api.ValueProposer, error) { |
| 69 | + group, ok := h.NewGroupMap[name] |
| 70 | + if !ok { |
| 71 | + return nil, fmt.Errorf("Missing group implies error") |
| 72 | + } |
| 73 | + return group, nil |
| 74 | +} |
| 75 | + |
| 76 | +func newMockHandler() *mockHandler { |
| 77 | + return &mockHandler{ |
| 78 | + AllocateReturn: newMockValues(), |
| 79 | + NewGroupMap: make(map[string]api.ValueProposer), |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestDoubleBegin(t *testing.T) { |
| 84 | + p := NewProposer(&mockHandler{AllocateReturn: &mockValues{}}) |
| 85 | + p.BeginValueProposals(nil) |
| 86 | + assert.Panics(t, func() { p.BeginValueProposals(nil) }, "Two begins back to back should have caused a panic") |
| 87 | +} |
| 88 | + |
| 89 | +func TestCommitWithoutBegin(t *testing.T) { |
| 90 | + p := NewProposer(&mockHandler{AllocateReturn: &mockValues{}}) |
| 91 | + assert.Panics(t, func() { p.CommitProposals() }, "Commit without begin should have caused a panic") |
| 92 | +} |
| 93 | + |
| 94 | +func TestRollback(t *testing.T) { |
| 95 | + p := NewProposer(&mockHandler{AllocateReturn: &mockValues{}}) |
| 96 | + p.pending = &config{} |
| 97 | + p.RollbackProposals() |
| 98 | + assert.Nil(t, p.pending, "Should have cleared pending config on rollback") |
| 99 | +} |
| 100 | + |
| 101 | +func TestGoodKeys(t *testing.T) { |
| 102 | + mh := newMockHandler() |
| 103 | + mh.AllocateReturn.ProtoMsgMap["Envelope"] = &cb.Envelope{} |
| 104 | + mh.AllocateReturn.ProtoMsgMap["Payload"] = &cb.Payload{} |
| 105 | + |
| 106 | + p := NewProposer(mh) |
| 107 | + _, err := p.BeginValueProposals(nil) |
| 108 | + assert.NoError(t, err) |
| 109 | + |
| 110 | + env := &cb.Envelope{Payload: []byte("SOME DATA")} |
| 111 | + pay := &cb.Payload{Data: []byte("SOME OTHER DATA")} |
| 112 | + |
| 113 | + assert.NoError(t, p.ProposeValue("Envelope", &cb.ConfigValue{Value: utils.MarshalOrPanic(env)})) |
| 114 | + assert.NoError(t, p.ProposeValue("Payload", &cb.ConfigValue{Value: utils.MarshalOrPanic(pay)})) |
| 115 | + |
| 116 | + assert.Equal(t, mh.AllocateReturn.ProtoMsgMap["Envelope"], env) |
| 117 | + assert.Equal(t, mh.AllocateReturn.ProtoMsgMap["Payload"], pay) |
| 118 | +} |
| 119 | + |
| 120 | +func TestBadMarshaling(t *testing.T) { |
| 121 | + mh := newMockHandler() |
| 122 | + mh.AllocateReturn.ProtoMsgMap["Envelope"] = &cb.Envelope{} |
| 123 | + |
| 124 | + p := NewProposer(mh) |
| 125 | + _, err := p.BeginValueProposals(nil) |
| 126 | + assert.NoError(t, err) |
| 127 | + |
| 128 | + assert.Error(t, p.ProposeValue("Envelope", &cb.ConfigValue{Value: []byte("GARBAGE")}), "Should have errored unmarshaling") |
| 129 | +} |
| 130 | + |
| 131 | +func TestBadMissingMessage(t *testing.T) { |
| 132 | + mh := newMockHandler() |
| 133 | + mh.AllocateReturn.ProtoMsgMap["Payload"] = &cb.Payload{} |
| 134 | + |
| 135 | + p := NewProposer(mh) |
| 136 | + _, err := p.BeginValueProposals(nil) |
| 137 | + assert.NoError(t, err) |
| 138 | + |
| 139 | + assert.Error(t, p.ProposeValue("Envelope", &cb.ConfigValue{Value: utils.MarshalOrPanic(&cb.Envelope{})}), "Should have errored on unexpected message") |
| 140 | +} |
| 141 | + |
| 142 | +func TestGroups(t *testing.T) { |
| 143 | + mh := newMockHandler() |
| 144 | + mh.NewGroupMap["foo"] = nil |
| 145 | + mh.NewGroupMap["bar"] = nil |
| 146 | + |
| 147 | + p := NewProposer(mh) |
| 148 | + _, err := p.BeginValueProposals([]string{"foo", "bar"}) |
| 149 | + assert.NoError(t, err, "Both groups were present") |
| 150 | + p.CommitProposals() |
| 151 | + |
| 152 | + mh.NewGroupMap = make(map[string]api.ValueProposer) |
| 153 | + _, err = p.BeginValueProposals([]string{"foo", "bar"}) |
| 154 | + assert.NoError(t, err, "Should not have tried to recreate the groups") |
| 155 | + p.CommitProposals() |
| 156 | + |
| 157 | + _, err = p.BeginValueProposals([]string{"foo", "other"}) |
| 158 | + assert.Error(t, err, "Should not have errored when trying to create 'other'") |
| 159 | + |
| 160 | + _, err = p.BeginValueProposals([]string{"foo"}) |
| 161 | + assert.NoError(t, err, "Should be able to begin again without rolling back because of error") |
| 162 | +} |
0 commit comments