@@ -14,13 +14,15 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package configtx
17
+ package configtx_test
18
18
19
19
import (
20
20
"fmt"
21
21
"testing"
22
22
23
+ . "github.com/hyperledger/fabric/common/configtx"
23
24
"github.com/hyperledger/fabric/common/policies"
25
+ mockconfigtx "github.com/hyperledger/fabric/orderer/mocks/configtx"
24
26
cb "github.com/hyperledger/fabric/protos/common"
25
27
26
28
"errors"
@@ -88,9 +90,9 @@ func makeSignedConfigurationItem(id, modificationPolicy string, lastModified uin
88
90
89
91
// TestOmittedHandler tests that startup fails if not all configuration types have an associated handler
90
92
func TestOmittedHandler (t * testing.T ) {
91
- _ , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
93
+ _ , err := NewManagerImpl (& cb.ConfigurationEnvelope {
92
94
Items : []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), defaultChain )},
93
- }, & mockPolicyManager {& mockPolicy {}}, map [cb.ConfigurationItem_ConfigurationType ]Handler {})
95
+ }, & mockconfigtx. Initializer { PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : map [cb.ConfigurationItem_ConfigurationType ]Handler {} })
94
96
95
97
if err == nil {
96
98
t .Fatal ("Should have failed to construct manager because handlers were missing" )
@@ -99,9 +101,9 @@ func TestOmittedHandler(t *testing.T) {
99
101
100
102
// TestWrongChainID tests that a configuration update for a different chain ID fails
101
103
func TestWrongChainID (t * testing.T ) {
102
- cm , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
104
+ cm , err := NewManagerImpl (& cb.ConfigurationEnvelope {
103
105
Items : []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), defaultChain )},
104
- }, & mockPolicyManager {& mockPolicy {}}, defaultHandlers ())
106
+ }, & mockconfigtx. Initializer { PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : defaultHandlers ()} )
105
107
106
108
if err != nil {
107
109
t .Fatalf ("Error constructing configuration manager: %s" , err )
@@ -124,9 +126,9 @@ func TestWrongChainID(t *testing.T) {
124
126
125
127
// TestOldConfigReplay tests that resubmitting a config for a sequence number which is not newer is ignored
126
128
func TestOldConfigReplay (t * testing.T ) {
127
- cm , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
129
+ cm , err := NewManagerImpl (& cb.ConfigurationEnvelope {
128
130
Items : []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), defaultChain )},
129
- }, & mockPolicyManager {& mockPolicy {}}, defaultHandlers ())
131
+ }, & mockconfigtx. Initializer { PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : defaultHandlers ()} )
130
132
131
133
if err != nil {
132
134
t .Fatalf ("Error constructing configuration manager: %s" , err )
@@ -151,9 +153,9 @@ func TestOldConfigReplay(t *testing.T) {
151
153
func TestInvalidInitialConfigByStructure (t * testing.T ) {
152
154
entries := []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), defaultChain )}
153
155
entries [0 ].ConfigurationItem = []byte ("Corrupted" )
154
- _ , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
156
+ _ , err := NewManagerImpl (& cb.ConfigurationEnvelope {
155
157
Items : entries ,
156
- }, & mockPolicyManager {& mockPolicy {}}, defaultHandlers ())
158
+ }, & mockconfigtx. Initializer { PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : defaultHandlers ()} )
157
159
158
160
if err == nil {
159
161
t .Fatal ("Should have failed to construct configuration by policy" )
@@ -162,9 +164,9 @@ func TestInvalidInitialConfigByStructure(t *testing.T) {
162
164
163
165
// TestValidConfigChange tests the happy path of updating a configuration value with no defaultModificationPolicy
164
166
func TestValidConfigChange (t * testing.T ) {
165
- cm , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
167
+ cm , err := NewManagerImpl (& cb.ConfigurationEnvelope {
166
168
Items : []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), defaultChain )},
167
- }, & mockPolicyManager {& mockPolicy {}}, defaultHandlers ())
169
+ }, & mockconfigtx. Initializer { PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : defaultHandlers ()} )
168
170
169
171
if err != nil {
170
172
t .Fatalf ("Error constructing configuration manager: %s" , err )
@@ -188,9 +190,9 @@ func TestValidConfigChange(t *testing.T) {
188
190
// TestConfigChangeRegressedSequence tests to make sure that a new config cannot roll back one of the
189
191
// config values while advancing another
190
192
func TestConfigChangeRegressedSequence (t * testing.T ) {
191
- cm , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
193
+ cm , err := NewManagerImpl (& cb.ConfigurationEnvelope {
192
194
Items : []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 1 , []byte ("foo" ), defaultChain )},
193
- }, & mockPolicyManager {& mockPolicy {}}, defaultHandlers ())
195
+ }, & mockconfigtx. Initializer { PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : defaultHandlers ()} )
194
196
195
197
if err != nil {
196
198
t .Fatalf ("Error constructing configuration manager: %s" , err )
@@ -217,9 +219,9 @@ func TestConfigChangeRegressedSequence(t *testing.T) {
217
219
// TestConfigChangeOldSequence tests to make sure that a new config cannot roll back one of the
218
220
// config values while advancing another
219
221
func TestConfigChangeOldSequence (t * testing.T ) {
220
- cm , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
222
+ cm , err := NewManagerImpl (& cb.ConfigurationEnvelope {
221
223
Items : []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 1 , []byte ("foo" ), defaultChain )},
222
- }, & mockPolicyManager {& mockPolicy {}}, defaultHandlers ())
224
+ }, & mockconfigtx. Initializer { PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : defaultHandlers ()} )
223
225
224
226
if err != nil {
225
227
t .Fatalf ("Error constructing configuration manager: %s" , err )
@@ -246,12 +248,12 @@ func TestConfigChangeOldSequence(t *testing.T) {
246
248
// TestConfigImplicitDelete tests to make sure that a new config does not implicitly delete config items
247
249
// by omitting them in the new config
248
250
func TestConfigImplicitDelete (t * testing.T ) {
249
- cm , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
251
+ cm , err := NewManagerImpl (& cb.ConfigurationEnvelope {
250
252
Items : []* cb.SignedConfigurationItem {
251
253
makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), defaultChain ),
252
254
makeSignedConfigurationItem ("bar" , "bar" , 0 , []byte ("bar" ), defaultChain ),
253
255
},
254
- }, & mockPolicyManager {& mockPolicy {}}, defaultHandlers ())
256
+ }, & mockconfigtx. Initializer { PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : defaultHandlers ()} )
255
257
256
258
if err != nil {
257
259
t .Fatalf ("Error constructing configuration manager: %s" , err )
@@ -276,9 +278,9 @@ func TestConfigImplicitDelete(t *testing.T) {
276
278
277
279
// TestEmptyConfigUpdate tests to make sure that an empty config is rejected as an update
278
280
func TestEmptyConfigUpdate (t * testing.T ) {
279
- cm , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
281
+ cm , err := NewManagerImpl (& cb.ConfigurationEnvelope {
280
282
Items : []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), defaultChain )},
281
- }, & mockPolicyManager {& mockPolicy {}}, defaultHandlers ())
283
+ }, & mockconfigtx. Initializer { PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : defaultHandlers ()} )
282
284
283
285
if err != nil {
284
286
t .Fatalf ("Error constructing configuration manager: %s" , err )
@@ -301,12 +303,12 @@ func TestEmptyConfigUpdate(t *testing.T) {
301
303
// is substituted into an otherwise valid new config, that the new config is rejected for attempting a modification without
302
304
// increasing the config item's LastModified
303
305
func TestSilentConfigModification (t * testing.T ) {
304
- cm , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
306
+ cm , err := NewManagerImpl (& cb.ConfigurationEnvelope {
305
307
Items : []* cb.SignedConfigurationItem {
306
308
makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), defaultChain ),
307
309
makeSignedConfigurationItem ("bar" , "bar" , 0 , []byte ("bar" ), defaultChain ),
308
310
},
309
- }, & mockPolicyManager {& mockPolicy {}}, defaultHandlers ())
311
+ }, & mockconfigtx. Initializer { PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : defaultHandlers ()} )
310
312
311
313
if err != nil {
312
314
t .Fatalf ("Error constructing configuration manager: %s" , err )
@@ -333,10 +335,9 @@ func TestSilentConfigModification(t *testing.T) {
333
335
// TestInvalidInitialConfigByPolicy tests to make sure that if an existing policies does not validate the config that
334
336
// even construction fails
335
337
func TestInvalidInitialConfigByPolicy (t * testing.T ) {
336
- _ , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
338
+ _ , err := NewManagerImpl (& cb.ConfigurationEnvelope {
337
339
Items : []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), defaultChain )},
338
- }, & mockPolicyManager {& mockPolicy {policyResult : fmt .Errorf ("err" )}}, defaultHandlers ())
339
- // mockPolicyManager will return non-validating defualt policy
340
+ }, & mockconfigtx.Initializer {PolicyManagerVal : & mockPolicyManager {& mockPolicy {policyResult : fmt .Errorf ("err" )}}, HandlersVal : defaultHandlers ()})
340
341
341
342
if err == nil {
342
343
t .Fatal ("Should have failed to construct configuration by policy" )
@@ -347,9 +348,9 @@ func TestInvalidInitialConfigByPolicy(t *testing.T) {
347
348
// it is rejected in a config update
348
349
func TestConfigChangeViolatesPolicy (t * testing.T ) {
349
350
mpm := & mockPolicyManager {}
350
- cm , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
351
+ cm , err := NewManagerImpl (& cb.ConfigurationEnvelope {
351
352
Items : []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), defaultChain )},
352
- }, mpm , defaultHandlers ())
353
+ }, & mockconfigtx. Initializer { PolicyManagerVal : mpm , HandlersVal : defaultHandlers ()} )
353
354
354
355
if err != nil {
355
356
t .Fatalf ("Error constructing configuration manager: %s" , err )
@@ -376,9 +377,9 @@ func TestConfigChangeViolatesPolicy(t *testing.T) {
376
377
// as the policy may have changed, certs revoked, etc. since the config was adopted.
377
378
func TestUnchangedConfigViolatesPolicy (t * testing.T ) {
378
379
mpm := & mockPolicyManager {}
379
- cm , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
380
+ cm , err := NewManagerImpl (& cb.ConfigurationEnvelope {
380
381
Items : []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), defaultChain )},
381
- }, mpm , defaultHandlers ())
382
+ }, & mockconfigtx. Initializer { PolicyManagerVal : mpm , HandlersVal : defaultHandlers ()} )
382
383
383
384
if err != nil {
384
385
t .Fatalf ("Error constructing configuration manager: %s" , err )
@@ -420,15 +421,16 @@ func (fh failHandler) ProposeConfig(item *cb.ConfigurationItem) error {
420
421
// that if the handler does not accept the config, it is rejected
421
422
func TestInvalidProposal (t * testing.T ) {
422
423
handlers := defaultHandlers ()
423
- cm , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
424
+ initializer := & mockconfigtx.Initializer {PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : handlers }
425
+ cm , err := NewManagerImpl (& cb.ConfigurationEnvelope {
424
426
Items : []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), defaultChain )},
425
- }, & mockPolicyManager { & mockPolicy {}}, handlers )
427
+ }, initializer )
426
428
427
429
if err != nil {
428
430
t .Fatalf ("Error constructing configuration manager: %s" , err )
429
431
}
430
432
431
- cm .( * configurationManager ). handlers [cb .ConfigurationItem_ConfigurationType (0 )] = failHandler {}
433
+ initializer . Handlers () [cb .ConfigurationItem_ConfigurationType (0 )] = failHandler {}
432
434
433
435
newConfig := & cb.ConfigurationEnvelope {
434
436
Items : []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 1 , []byte ("foo" ), defaultChain )},
@@ -451,9 +453,9 @@ func TestMissingHeader(t *testing.T) {
451
453
configItem := makeConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), defaultChain )
452
454
configItem .Header = nil
453
455
data , _ := proto .Marshal (configItem )
454
- _ , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
456
+ _ , err := NewManagerImpl (& cb.ConfigurationEnvelope {
455
457
Items : []* cb.SignedConfigurationItem {& cb.SignedConfigurationItem {ConfigurationItem : data }},
456
- }, & mockPolicyManager {& mockPolicy {}}, handlers )
458
+ }, & mockconfigtx. Initializer { PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : handlers } )
457
459
458
460
if err == nil {
459
461
t .Error ("Should have errored creating the configuration manager because of the missing header" )
@@ -463,9 +465,9 @@ func TestMissingHeader(t *testing.T) {
463
465
// TestMissingChainID checks that a configuration item with a missing chainID causes the config to be rejected
464
466
func TestMissingChainID (t * testing.T ) {
465
467
handlers := defaultHandlers ()
466
- _ , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
468
+ _ , err := NewManagerImpl (& cb.ConfigurationEnvelope {
467
469
Items : []* cb.SignedConfigurationItem {makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), "" )},
468
- }, & mockPolicyManager {& mockPolicy {}}, handlers )
470
+ }, & mockconfigtx. Initializer { PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : handlers } )
469
471
470
472
if err == nil {
471
473
t .Error ("Should have errored creating the configuration manager because of the missing header" )
@@ -475,12 +477,12 @@ func TestMissingChainID(t *testing.T) {
475
477
// TestMismatchedChainID checks that a configuration envelope with items with mixed chainIDs causes the config to be rejected
476
478
func TestMismatchedChainID (t * testing.T ) {
477
479
handlers := defaultHandlers ()
478
- _ , err := NewConfigurationManager (& cb.ConfigurationEnvelope {
480
+ _ , err := NewManagerImpl (& cb.ConfigurationEnvelope {
479
481
Items : []* cb.SignedConfigurationItem {
480
482
makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), "chain1" ),
481
483
makeSignedConfigurationItem ("foo" , "foo" , 0 , []byte ("foo" ), "chain2" ),
482
484
},
483
- }, & mockPolicyManager {& mockPolicy {}}, handlers )
485
+ }, & mockconfigtx. Initializer { PolicyManagerVal : & mockPolicyManager {& mockPolicy {}}, HandlersVal : handlers } )
484
486
485
487
if err == nil {
486
488
t .Error ("Should have errored creating the configuration manager because of the missing header" )
0 commit comments