@@ -27,7 +27,6 @@ import (
27
27
28
28
cb "github.com/hyperledger/fabric/protos/common"
29
29
ab "github.com/hyperledger/fabric/protos/orderer"
30
- "github.com/hyperledger/fabric/protos/utils"
31
30
32
31
logging "github.com/op/go-logging"
33
32
)
@@ -36,6 +35,14 @@ func init() {
36
35
logging .SetLevel (logging .DEBUG , "" )
37
36
}
38
37
38
+ func invalidMessage (key string ) * cb.ConfigurationItem {
39
+ return & cb.ConfigurationItem {
40
+ Type : cb .ConfigurationItem_Orderer ,
41
+ Key : key ,
42
+ Value : []byte ("Garbage Data" ),
43
+ }
44
+ }
45
+
39
46
func doesFuncCrash (crasher func (), test string ) bool {
40
47
// Adapted from https://talks.golang.org/2014/testing.slide#23 to test os.Exit() functionality
41
48
if os .Getenv ("BE_CRASHER" ) == "1" {
@@ -85,22 +92,10 @@ func TestRollback(t *testing.T) {
85
92
86
93
func TestConsensusType (t * testing.T ) {
87
94
endType := "foo"
88
- invalidMessage :=
89
- & cb.ConfigurationItem {
90
- Type : cb .ConfigurationItem_Orderer ,
91
- Key : ConsensusTypeKey ,
92
- Value : []byte ("Garbage Data" ),
93
- }
94
- validMessage := & cb.ConfigurationItem {
95
- Type : cb .ConfigurationItem_Orderer ,
96
- Key : ConsensusTypeKey ,
97
- Value : utils .MarshalOrPanic (& ab.ConsensusType {Type : endType }),
98
- }
99
- otherValidMessage := & cb.ConfigurationItem {
100
- Type : cb .ConfigurationItem_Orderer ,
101
- Key : ConsensusTypeKey ,
102
- Value : utils .MarshalOrPanic (& ab.ConsensusType {Type : "bar" }),
103
- }
95
+ invalidMessage := invalidMessage (ConsensusTypeKey )
96
+ validMessage := TemplateConsensusType (endType )
97
+ otherValidMessage := TemplateConsensusType ("bar" )
98
+
104
99
m := NewManagerImpl ()
105
100
m .BeginConfig ()
106
101
@@ -143,11 +138,9 @@ func TestBatchSize(t *testing.T) {
143
138
t .Run ("ValidConfiguration" , func (t * testing.T ) {
144
139
m := NewManagerImpl ()
145
140
m .BeginConfig ()
146
- err := m .ProposeConfig (& cb.ConfigurationItem {
147
- Type : cb .ConfigurationItem_Orderer ,
148
- Key : BatchSizeKey ,
149
- Value : utils .MarshalOrPanic (& ab.BatchSize {MaxMessageCount : validMaxMessageCount , AbsoluteMaxBytes : validAbsoluteMaxBytes , PreferredMaxBytes : validPreferredMaxBytes }),
150
- })
141
+ err := m .ProposeConfig (
142
+ TemplateBatchSize (& ab.BatchSize {MaxMessageCount : validMaxMessageCount , AbsoluteMaxBytes : validAbsoluteMaxBytes , PreferredMaxBytes : validPreferredMaxBytes }),
143
+ )
151
144
assert .Nil (t , err , "Error applying valid config: %s" , err )
152
145
m .CommitConfig ()
153
146
if m .BatchSize ().MaxMessageCount != validMaxMessageCount {
@@ -164,74 +157,43 @@ func TestBatchSize(t *testing.T) {
164
157
t .Run ("UnserializableConfiguration" , func (t * testing.T ) {
165
158
m := NewManagerImpl ()
166
159
m .BeginConfig ()
167
- err := m .ProposeConfig (& cb.ConfigurationItem {
168
- Type : cb .ConfigurationItem_Orderer ,
169
- Key : BatchSizeKey ,
170
- Value : []byte ("Garbage Data" ),
171
- })
160
+ err := m .ProposeConfig (invalidMessage (BatchSizeKey ))
172
161
assert .NotNil (t , err , "Should have failed on invalid message" )
173
162
m .CommitConfig ()
174
163
})
175
164
176
165
t .Run ("ZeroMaxMessageCount" , func (t * testing.T ) {
177
166
m := NewManagerImpl ()
178
167
m .BeginConfig ()
179
- err := m .ProposeConfig (& cb.ConfigurationItem {
180
- Type : cb .ConfigurationItem_Orderer ,
181
- Key : BatchSizeKey ,
182
- Value : utils .MarshalOrPanic (& ab.BatchSize {MaxMessageCount : 0 , AbsoluteMaxBytes : validAbsoluteMaxBytes , PreferredMaxBytes : validPreferredMaxBytes }),
183
- })
168
+ err := m .ProposeConfig (TemplateBatchSize (& ab.BatchSize {MaxMessageCount : 0 , AbsoluteMaxBytes : validAbsoluteMaxBytes , PreferredMaxBytes : validPreferredMaxBytes }))
184
169
assert .NotNil (t , err , "Should have rejected batch size max message count of 0" )
185
170
m .CommitConfig ()
186
171
})
187
172
188
173
t .Run ("ZeroAbsoluteMaxBytes" , func (t * testing.T ) {
189
174
m := NewManagerImpl ()
190
175
m .BeginConfig ()
191
- err := m .ProposeConfig (& cb.ConfigurationItem {
192
- Type : cb .ConfigurationItem_Orderer ,
193
- Key : BatchSizeKey ,
194
- Value : utils .MarshalOrPanic (& ab.BatchSize {MaxMessageCount : validMaxMessageCount , AbsoluteMaxBytes : 0 , PreferredMaxBytes : validPreferredMaxBytes }),
195
- })
176
+ err := m .ProposeConfig (TemplateBatchSize (& ab.BatchSize {MaxMessageCount : validMaxMessageCount , AbsoluteMaxBytes : 0 , PreferredMaxBytes : validPreferredMaxBytes }))
196
177
assert .NotNil (t , err , "Should have rejected batch size absolute max message bytes of 0" )
197
178
m .CommitConfig ()
198
179
})
199
180
200
181
t .Run ("TooLargePreferredMaxBytes" , func (t * testing.T ) {
201
182
m := NewManagerImpl ()
202
183
m .BeginConfig ()
203
- err := m .ProposeConfig (& cb.ConfigurationItem {
204
- Type : cb .ConfigurationItem_Orderer ,
205
- Key : BatchSizeKey ,
206
- Value : utils .MarshalOrPanic (& ab.BatchSize {MaxMessageCount : validMaxMessageCount , AbsoluteMaxBytes : validAbsoluteMaxBytes , PreferredMaxBytes : validAbsoluteMaxBytes + 1 }),
207
- })
184
+ err := m .ProposeConfig (TemplateBatchSize (& ab.BatchSize {MaxMessageCount : validMaxMessageCount , AbsoluteMaxBytes : validAbsoluteMaxBytes , PreferredMaxBytes : validAbsoluteMaxBytes + 1 }))
208
185
assert .NotNil (t , err , "Should have rejected batch size preferred max message bytes greater than absolute max message bytes" )
209
186
m .CommitConfig ()
210
187
})
211
188
}
212
189
213
190
func TestBatchTimeout (t * testing.T ) {
214
191
endBatchTimeout , _ := time .ParseDuration ("1s" )
215
- invalidMessage := & cb.ConfigurationItem {
216
- Type : cb .ConfigurationItem_Orderer ,
217
- Key : BatchTimeoutKey ,
218
- Value : []byte ("Garbage Data" ),
219
- }
220
- negativeBatchTimeout := & cb.ConfigurationItem {
221
- Type : cb .ConfigurationItem_Orderer ,
222
- Key : BatchTimeoutKey ,
223
- Value : utils .MarshalOrPanic (& ab.BatchTimeout {Timeout : "-1s" }),
224
- }
225
- zeroBatchTimeout := & cb.ConfigurationItem {
226
- Type : cb .ConfigurationItem_Orderer ,
227
- Key : BatchTimeoutKey ,
228
- Value : utils .MarshalOrPanic (& ab.BatchTimeout {Timeout : "0s" }),
229
- }
230
- validMessage := & cb.ConfigurationItem {
231
- Type : cb .ConfigurationItem_Orderer ,
232
- Key : BatchTimeoutKey ,
233
- Value : utils .MarshalOrPanic (& ab.BatchTimeout {Timeout : endBatchTimeout .String ()}),
234
- }
192
+ invalidMessage := invalidMessage (BatchTimeoutKey )
193
+ negativeBatchTimeout := TemplateBatchTimeout ("-1s" )
194
+ zeroBatchTimeout := TemplateBatchTimeout ("0s" )
195
+ validMessage := TemplateBatchTimeout (endBatchTimeout .String ())
196
+
235
197
m := NewManagerImpl ()
236
198
m .BeginConfig ()
237
199
@@ -265,34 +227,15 @@ func TestBatchTimeout(t *testing.T) {
265
227
func TestKafkaBrokers (t * testing.T ) {
266
228
endList := []string {"127.0.0.1:9092" , "foo.bar:9092" }
267
229
268
- invalidMessage := & cb.ConfigurationItem {
269
- Type : cb .ConfigurationItem_Orderer ,
270
- Key : KafkaBrokersKey ,
271
- Value : []byte ("Garbage Data" ),
272
- }
273
-
274
- zeroBrokers := & cb.ConfigurationItem {
275
- Type : cb .ConfigurationItem_Orderer ,
276
- Key : KafkaBrokersKey ,
277
- Value : utils .MarshalOrPanic (& ab.KafkaBrokers {}),
278
- }
279
-
230
+ invalidMessage := invalidMessage (KafkaBrokersKey )
231
+ zeroBrokers := TemplateKafkaBrokers ([]string {})
280
232
badList := []string {"127.0.0.1" , "foo.bar" , "127.0.0.1:-1" , "localhost:65536" , "foo.bar.:9092" , ".127.0.0.1:9092" , "-foo.bar:9092" }
281
233
badMessages := []* cb.ConfigurationItem {}
282
234
for _ , badAddress := range badList {
283
- msg := & cb.ConfigurationItem {
284
- Type : cb .ConfigurationItem_Orderer ,
285
- Key : KafkaBrokersKey ,
286
- Value : utils .MarshalOrPanic (& ab.KafkaBrokers {Brokers : []string {badAddress }}),
287
- }
288
- badMessages = append (badMessages , msg )
235
+ badMessages = append (badMessages , TemplateKafkaBrokers ([]string {badAddress }))
289
236
}
290
237
291
- validMessage := & cb.ConfigurationItem {
292
- Type : cb .ConfigurationItem_Orderer ,
293
- Key : KafkaBrokersKey ,
294
- Value : utils .MarshalOrPanic (& ab.KafkaBrokers {Brokers : endList }),
295
- }
238
+ validMessage := TemplateKafkaBrokers (endList )
296
239
297
240
m := NewManagerImpl ()
298
241
m .BeginConfig ()
@@ -330,20 +273,11 @@ func TestKafkaBrokers(t *testing.T) {
330
273
}
331
274
}
332
275
333
- func TestIngressPolicyNames (t * testing.T ) {
334
- endPolicy := []string {"foo" }
335
- invalidMessage :=
336
- & cb.ConfigurationItem {
337
- Type : cb .ConfigurationItem_Orderer ,
338
- Key : IngressPolicyNamesKey ,
339
- Value : []byte ("Garbage Data" ),
340
- }
341
- validMessage := & cb.ConfigurationItem {
342
- Type : cb .ConfigurationItem_Orderer ,
343
- Key : IngressPolicyNamesKey ,
344
- Value : utils .MarshalOrPanic (& ab.IngressPolicyNames {Names : endPolicy }),
345
- }
346
- m := NewManagerImpl ()
276
+ func testPolicyNames (m * ManagerImpl , key string , initializer func (val []string ) * cb.ConfigurationItem , retriever func () []string , t * testing.T ) {
277
+ endPolicy := []string {"foo" , "bar" }
278
+ invalidMessage := invalidMessage (key )
279
+ validMessage := initializer (endPolicy )
280
+
347
281
m .BeginConfig ()
348
282
349
283
err := m .ProposeConfig (validMessage )
@@ -366,48 +300,22 @@ func TestIngressPolicyNames(t *testing.T) {
366
300
367
301
m .CommitConfig ()
368
302
369
- if nowPolicy := m . IngressPolicyNames (); ! reflect .DeepEqual (nowPolicy , endPolicy ) {
370
- t .Fatalf ("IngressPolicyNames should have ended as %s but was %s" , endPolicy , nowPolicy )
303
+ if nowPolicy := retriever (); ! reflect .DeepEqual (nowPolicy , endPolicy ) {
304
+ t .Fatalf ("%s should have ended as %s but was %s" , key , endPolicy , nowPolicy )
371
305
}
372
306
}
373
307
374
- func TestEgressPolicyNames (t * testing.T ) {
375
- endPolicy := []string {"foo" }
376
- invalidMessage :=
377
- & cb.ConfigurationItem {
378
- Type : cb .ConfigurationItem_Orderer ,
379
- Key : EgressPolicyNamesKey ,
380
- Value : []byte ("Garbage Data" ),
381
- }
382
- validMessage := & cb.ConfigurationItem {
383
- Type : cb .ConfigurationItem_Orderer ,
384
- Key : EgressPolicyNamesKey ,
385
- Value : utils .MarshalOrPanic (& ab.EgressPolicyNames {Names : endPolicy }),
386
- }
308
+ func TestIngressPolicyNames (t * testing.T ) {
387
309
m := NewManagerImpl ()
388
- m .BeginConfig ()
389
-
390
- err := m .ProposeConfig (validMessage )
391
- if err != nil {
392
- t .Fatalf ("Error applying valid config: %s" , err )
393
- }
394
-
395
- m .CommitConfig ()
396
- m .BeginConfig ()
397
-
398
- err = m .ProposeConfig (invalidMessage )
399
- if err == nil {
400
- t .Fatalf ("Should have failed on invalid message" )
401
- }
402
-
403
- err = m .ProposeConfig (validMessage )
404
- if err != nil {
405
- t .Fatalf ("Error re-applying valid config: %s" , err )
406
- }
310
+ testPolicyNames (m , IngressPolicyNamesKey , TemplateIngressPolicyNames , m .IngressPolicyNames , t )
311
+ }
407
312
408
- m .CommitConfig ()
313
+ func TestEgressPolicyNames (t * testing.T ) {
314
+ m := NewManagerImpl ()
315
+ testPolicyNames (m , EgressPolicyNamesKey , TemplateEgressPolicyNames , m .EgressPolicyNames , t )
316
+ }
409
317
410
- if nowPolicy := m . EgressPolicyNames (); ! reflect . DeepEqual ( nowPolicy , endPolicy ) {
411
- t . Fatalf ( "EgressPolicyNames should have ended as %s but was %s" , endPolicy , nowPolicy )
412
- }
318
+ func TestChainCreationPolicyNames ( t * testing. T ) {
319
+ m := NewManagerImpl ( )
320
+ testPolicyNames ( m , ChainCreationPolicyNamesKey , TemplateChainCreationPolicyNames , m . ChainCreationPolicyNames , t )
413
321
}
0 commit comments