Skip to content

Commit 4ae322b

Browse files
committed
[FAB-4323] Improve code coverage of orderer filter
Add UT for orderer common filter and configfilter. Change-Id: Iefdd2a702a3ce397dcda4466e5d754c1d40d95de Signed-off-by: Jay Guo <[email protected]>
1 parent 9ca37ee commit 4ae322b

File tree

2 files changed

+89
-17
lines changed

2 files changed

+89
-17
lines changed

orderer/common/configtxfilter/filter_test.go

+83-17
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package configtxfilter
1818

1919
import (
2020
"fmt"
21-
"reflect"
2221
"testing"
2322

2423
mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx"
@@ -27,16 +26,60 @@ import (
2726
"github.com/hyperledger/fabric/protos/utils"
2827

2928
"github.com/golang/protobuf/proto"
29+
"github.com/stretchr/testify/assert"
3030
)
3131

32-
func TestForwardNonConfig(t *testing.T) {
32+
func TestForwardOpaquePayload(t *testing.T) {
3333
cf := NewFilter(&mockconfigtx.Manager{})
3434
result, _ := cf.Apply(&cb.Envelope{
3535
Payload: []byte("Opaque"),
3636
})
37-
if result != filter.Forward {
38-
t.Fatal("Should have forwarded opaque message")
39-
}
37+
assert.EqualValues(t, filter.Forward, result, "Should have forwarded opaque message")
38+
}
39+
40+
func TestForwardNilHeader(t *testing.T) {
41+
cf := NewFilter(&mockconfigtx.Manager{})
42+
result, _ := cf.Apply(&cb.Envelope{
43+
Payload: utils.MarshalOrPanic(&cb.Payload{
44+
Header: nil,
45+
}),
46+
})
47+
assert.EqualValues(t, filter.Forward, result, "Should have forwarded message with nil header")
48+
}
49+
50+
func TestForwardBadHeader(t *testing.T) {
51+
cf := NewFilter(&mockconfigtx.Manager{})
52+
result, _ := cf.Apply(&cb.Envelope{
53+
Payload: utils.MarshalOrPanic(&cb.Payload{
54+
Header: &cb.Header{ChannelHeader: []byte("Hello, world!")},
55+
}),
56+
})
57+
assert.EqualValues(t, filter.Forward, result, "Should have forwarded message with bad header")
58+
}
59+
60+
func TestForwardNonConfig(t *testing.T) {
61+
cf := NewFilter(&mockconfigtx.Manager{})
62+
result, _ := cf.Apply(&cb.Envelope{
63+
Payload: utils.MarshalOrPanic(&cb.Payload{
64+
Header: &cb.Header{ChannelHeader: []byte{}},
65+
}),
66+
})
67+
assert.EqualValues(t, filter.Forward, result, "Should have forwarded message with non-config message")
68+
}
69+
70+
func TestRejectMalformedData(t *testing.T) {
71+
cf := NewFilter(&mockconfigtx.Manager{})
72+
result, _ := cf.Apply(&cb.Envelope{
73+
Payload: utils.MarshalOrPanic(&cb.Payload{
74+
Header: &cb.Header{
75+
ChannelHeader: utils.MarshalOrPanic(&cb.ChannelHeader{
76+
Type: int32(cb.HeaderType_CONFIG),
77+
}),
78+
},
79+
Data: []byte("Hello, world!"),
80+
}),
81+
})
82+
assert.EqualValues(t, filter.Reject, result, "Should have rejected message with malformed payload data")
4083
}
4184

4285
func TestAcceptGoodConfig(t *testing.T) {
@@ -65,19 +108,43 @@ func TestAcceptGoodConfig(t *testing.T) {
65108
Payload: configBytes,
66109
}
67110
result, committer := cf.Apply(configEnvelope)
68-
if result != filter.Accept {
69-
t.Fatal("Should have indicated a good config message causes a reconfig")
70-
}
71-
72-
if !committer.Isolated() {
73-
t.Fatal("Config transactions should be isolated to their own block")
74-
}
111+
assert.EqualValues(t, filter.Accept, result, "Should have indicated a good config message causes a reconfig")
112+
assert.True(t, committer.Isolated(), "Config transactions should be isolated to their own block")
75113

76114
committer.Commit()
115+
assert.Equal(t, mcm.AppliedConfigUpdateEnvelope, configEnv, "Should have applied new config on commit got %+v and %+v", mcm.AppliedConfigUpdateEnvelope, configEnv.LastUpdate)
116+
}
77117

78-
if !reflect.DeepEqual(mcm.AppliedConfigUpdateEnvelope, configEnv) {
79-
t.Fatalf("Should have applied new config on commit got %+v and %+v", mcm.AppliedConfigUpdateEnvelope, configEnv.LastUpdate)
118+
func TestPanicApplyingValidatedConfig(t *testing.T) {
119+
mcm := &mockconfigtx.Manager{ApplyVal: fmt.Errorf("Error applying config tx")}
120+
cf := NewFilter(mcm)
121+
configGroup := cb.NewConfigGroup()
122+
configGroup.Values["Foo"] = &cb.ConfigValue{}
123+
configUpdateEnv := &cb.ConfigUpdateEnvelope{
124+
ConfigUpdate: utils.MarshalOrPanic(configGroup),
125+
}
126+
configEnv := &cb.ConfigEnvelope{
127+
LastUpdate: &cb.Envelope{
128+
Payload: utils.MarshalOrPanic(&cb.Payload{
129+
Header: &cb.Header{
130+
ChannelHeader: utils.MarshalOrPanic(&cb.ChannelHeader{
131+
Type: int32(cb.HeaderType_CONFIG_UPDATE),
132+
}),
133+
},
134+
Data: utils.MarshalOrPanic(configUpdateEnv),
135+
}),
136+
},
80137
}
138+
configEnvBytes := utils.MarshalOrPanic(configEnv)
139+
configBytes := utils.MarshalOrPanic(&cb.Payload{Header: &cb.Header{ChannelHeader: utils.MarshalOrPanic(&cb.ChannelHeader{Type: int32(cb.HeaderType_CONFIG)})}, Data: configEnvBytes})
140+
configEnvelope := &cb.Envelope{
141+
Payload: configBytes,
142+
}
143+
result, committer := cf.Apply(configEnvelope)
144+
145+
assert.EqualValues(t, filter.Accept, result, "Should have indicated a good config message causes a reconfig")
146+
assert.True(t, committer.Isolated(), "Config transactions should be isolated to their own block")
147+
assert.Panics(t, func() { committer.Commit() }, "Should panic upon error applying a validated config tx")
81148
}
82149

83150
func TestRejectBadConfig(t *testing.T) {
@@ -87,7 +154,6 @@ func TestRejectBadConfig(t *testing.T) {
87154
result, _ := cf.Apply(&cb.Envelope{
88155
Payload: configBytes,
89156
})
90-
if result != filter.Reject {
91-
t.Fatal("Should have rejected bad config message")
92-
}
157+
158+
assert.EqualValues(t, filter.Reject, result, "Should have rejected bad config message")
93159
}

orderer/common/filter/filter_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121

2222
cb "github.com/hyperledger/fabric/protos/common"
23+
"github.com/stretchr/testify/assert"
2324
)
2425

2526
var RejectRule = Rule(rejectRule{})
@@ -38,6 +39,11 @@ func (r forwardRule) Apply(message *cb.Envelope) (Action, Committer) {
3839
return Forward, nil
3940
}
4041

42+
func TestNoopCommitter(t *testing.T) {
43+
var nc noopCommitter
44+
assert.False(t, nc.Isolated(), "Should return false")
45+
}
46+
4147
func TestEmptyRejectRule(t *testing.T) {
4248
result, _ := EmptyRejectRule.Apply(&cb.Envelope{})
4349
if result != Reject {

0 commit comments

Comments
 (0)