github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/common/config/orderer_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 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  	"testing"
    21  
    22  	ab "github.com/hyperledger/fabric/protos/orderer"
    23  
    24  	logging "github.com/op/go-logging"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func init() {
    29  	logging.SetLevel(logging.DEBUG, "")
    30  }
    31  
    32  func TestConsensusType(t *testing.T) {
    33  	oc := &OrdererConfig{ordererGroup: &OrdererGroup{}, protos: &OrdererProtos{ConsensusType: &ab.ConsensusType{Type: "foo"}}}
    34  	assert.NoError(t, oc.validateConsensusType(), "Should have validly set new consensus type")
    35  
    36  	oc = &OrdererConfig{
    37  		ordererGroup: &OrdererGroup{OrdererConfig: &OrdererConfig{protos: &OrdererProtos{ConsensusType: &ab.ConsensusType{Type: "foo"}}}},
    38  		protos:       &OrdererProtos{ConsensusType: &ab.ConsensusType{Type: "foo"}},
    39  	}
    40  	assert.NoError(t, oc.validateConsensusType(), "Should have kept consensus type")
    41  
    42  	oc = &OrdererConfig{
    43  		ordererGroup: &OrdererGroup{OrdererConfig: &OrdererConfig{protos: &OrdererProtos{ConsensusType: &ab.ConsensusType{Type: "bar"}}}},
    44  		protos:       &OrdererProtos{ConsensusType: &ab.ConsensusType{Type: "foo"}},
    45  	}
    46  	assert.Error(t, oc.validateConsensusType(), "Should have failed to change consensus type")
    47  }
    48  
    49  func TestBatchSize(t *testing.T) {
    50  
    51  	validMaxMessageCount := uint32(10)
    52  	validAbsoluteMaxBytes := uint32(1000)
    53  	validPreferredMaxBytes := uint32(500)
    54  
    55  	oc := &OrdererConfig{protos: &OrdererProtos{BatchSize: &ab.BatchSize{MaxMessageCount: validMaxMessageCount, AbsoluteMaxBytes: validAbsoluteMaxBytes, PreferredMaxBytes: validPreferredMaxBytes}}}
    56  	assert.NoError(t, oc.validateBatchSize(), "BatchSize was valid")
    57  
    58  	oc = &OrdererConfig{protos: &OrdererProtos{BatchSize: &ab.BatchSize{MaxMessageCount: 0, AbsoluteMaxBytes: validAbsoluteMaxBytes, PreferredMaxBytes: validPreferredMaxBytes}}}
    59  	assert.Error(t, oc.validateBatchSize(), "MaxMessageCount was zero")
    60  
    61  	oc = &OrdererConfig{protos: &OrdererProtos{BatchSize: &ab.BatchSize{MaxMessageCount: validMaxMessageCount, AbsoluteMaxBytes: 0, PreferredMaxBytes: validPreferredMaxBytes}}}
    62  	assert.Error(t, oc.validateBatchSize(), "AbsoluteMaxBytes was zero")
    63  
    64  	oc = &OrdererConfig{protos: &OrdererProtos{BatchSize: &ab.BatchSize{MaxMessageCount: validMaxMessageCount, AbsoluteMaxBytes: validAbsoluteMaxBytes, PreferredMaxBytes: validAbsoluteMaxBytes + 1}}}
    65  	assert.Error(t, oc.validateBatchSize(), "PreferredMaxBytes larger to AbsoluteMaxBytes")
    66  }
    67  
    68  func TestBatchTimeout(t *testing.T) {
    69  	oc := &OrdererConfig{protos: &OrdererProtos{BatchTimeout: &ab.BatchTimeout{Timeout: "1s"}}}
    70  	assert.NoError(t, oc.validateBatchTimeout(), "Valid batch timeout")
    71  
    72  	oc = &OrdererConfig{protos: &OrdererProtos{BatchTimeout: &ab.BatchTimeout{Timeout: "-1s"}}}
    73  	assert.Error(t, oc.validateBatchTimeout(), "Negative batch timeout")
    74  
    75  	oc = &OrdererConfig{protos: &OrdererProtos{BatchTimeout: &ab.BatchTimeout{Timeout: "0s"}}}
    76  	assert.Error(t, oc.validateBatchTimeout(), "Zero batch timeout")
    77  }
    78  
    79  func TestKafkaBrokers(t *testing.T) {
    80  	oc := &OrdererConfig{protos: &OrdererProtos{KafkaBrokers: &ab.KafkaBrokers{Brokers: []string{"127.0.0.1:9092", "foo.bar:9092"}}}}
    81  	assert.NoError(t, oc.validateKafkaBrokers(), "Valid kafka brokers")
    82  
    83  	oc = &OrdererConfig{protos: &OrdererProtos{KafkaBrokers: &ab.KafkaBrokers{Brokers: []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"}}}}
    84  	assert.Error(t, oc.validateKafkaBrokers(), "Invalid kafka brokers")
    85  }