github.com/lzy4123/fabric@v2.1.1+incompatible/orderer/consensus/kafka/config_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package kafka 8 9 import ( 10 "crypto/tls" 11 "testing" 12 13 "github.com/Shopify/sarama" 14 localconfig "github.com/hyperledger/fabric/orderer/common/localconfig" 15 "github.com/hyperledger/fabric/orderer/mocks/util" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestBrokerConfig(t *testing.T) { 20 mockChannel1 := newChannel(channelNameForTest(t), defaultPartition) 21 // Use a partition ID that is not the 'default' (defaultPartition) 22 var differentPartition int32 = defaultPartition + 1 23 mockChannel2 := newChannel(channelNameForTest(t), differentPartition) 24 25 mockBroker := sarama.NewMockBroker(t, 0) 26 defer func() { mockBroker.Close() }() 27 28 mockBroker.SetHandlerByMap(map[string]sarama.MockResponse{ 29 "MetadataRequest": sarama.NewMockMetadataResponse(t). 30 SetBroker(mockBroker.Addr(), mockBroker.BrokerID()). 31 SetLeader(mockChannel1.topic(), mockChannel1.partition(), mockBroker.BrokerID()). 32 SetLeader(mockChannel2.topic(), mockChannel2.partition(), mockBroker.BrokerID()), 33 "ProduceRequest": sarama.NewMockProduceResponse(t), 34 }) 35 36 t.Run("New", func(t *testing.T) { 37 producer, err := sarama.NewSyncProducer([]string{mockBroker.Addr()}, mockBrokerConfig) 38 assert.NoError(t, err, "Failed to create producer with given config:", err) 39 producer.Close() 40 }) 41 42 t.Run("Partitioner", func(t *testing.T) { 43 mockBrokerConfig2 := newBrokerConfig( 44 mockLocalConfig.General.TLS, 45 mockLocalConfig.Kafka.SASLPlain, 46 mockLocalConfig.Kafka.Retry, 47 mockLocalConfig.Kafka.Version, 48 differentPartition) 49 producer, _ := sarama.NewSyncProducer([]string{mockBroker.Addr()}, mockBrokerConfig2) 50 defer func() { producer.Close() }() 51 52 for i := 0; i < 10; i++ { 53 assignedPartition, _, err := producer.SendMessage(&sarama.ProducerMessage{Topic: mockChannel2.topic()}) 54 assert.NoError(t, err, "Failed to send message:", err) 55 assert.Equal(t, differentPartition, assignedPartition, "Message wasn't posted to the right partition - expected %d, got %v", differentPartition, assignedPartition) 56 } 57 }) 58 59 producer, _ := sarama.NewSyncProducer([]string{mockBroker.Addr()}, mockBrokerConfig) 60 defer func() { producer.Close() }() 61 62 testCases := []struct { 63 name string 64 size int 65 err error 66 }{ 67 {"TypicalDeploy", 4 * 1024, nil}, 68 {"TooBig", int(sarama.MaxRequestSize + 1), sarama.ErrMessageSizeTooLarge}, 69 } 70 71 for _, tc := range testCases { 72 t.Run("ProducerMessageMaxBytes"+tc.name, func(t *testing.T) { 73 _, _, err := producer.SendMessage(&sarama.ProducerMessage{ 74 Topic: mockChannel1.topic(), 75 Value: sarama.ByteEncoder(make([]byte, tc.size)), 76 }) 77 assert.Equal(t, tc.err, err) 78 }) 79 } 80 } 81 82 func TestBrokerConfigTLSConfigEnabled(t *testing.T) { 83 publicKey, privateKey, _ := util.GenerateMockPublicPrivateKeyPairPEM(false) 84 caPublicKey, _, _ := util.GenerateMockPublicPrivateKeyPairPEM(true) 85 86 t.Run("Enabled", func(t *testing.T) { 87 testBrokerConfig := newBrokerConfig(localconfig.TLS{ 88 Enabled: true, 89 PrivateKey: privateKey, 90 Certificate: publicKey, 91 RootCAs: []string{caPublicKey}, 92 }, 93 mockLocalConfig.Kafka.SASLPlain, 94 mockLocalConfig.Kafka.Retry, 95 mockLocalConfig.Kafka.Version, 96 defaultPartition) 97 98 assert.True(t, testBrokerConfig.Net.TLS.Enable) 99 assert.NotNil(t, testBrokerConfig.Net.TLS.Config) 100 assert.Len(t, testBrokerConfig.Net.TLS.Config.Certificates, 1) 101 assert.Len(t, testBrokerConfig.Net.TLS.Config.RootCAs.Subjects(), 1) 102 assert.Equal(t, uint16(0), testBrokerConfig.Net.TLS.Config.MaxVersion) 103 assert.Equal(t, uint16(tls.VersionTLS12), testBrokerConfig.Net.TLS.Config.MinVersion) 104 }) 105 106 t.Run("Disabled", func(t *testing.T) { 107 testBrokerConfig := newBrokerConfig(localconfig.TLS{ 108 Enabled: false, 109 PrivateKey: privateKey, 110 Certificate: publicKey, 111 RootCAs: []string{caPublicKey}, 112 }, 113 mockLocalConfig.Kafka.SASLPlain, 114 mockLocalConfig.Kafka.Retry, 115 mockLocalConfig.Kafka.Version, 116 defaultPartition) 117 118 assert.False(t, testBrokerConfig.Net.TLS.Enable) 119 assert.Zero(t, testBrokerConfig.Net.TLS.Config) 120 }) 121 } 122 123 func TestBrokerConfigTLSConfigBadCert(t *testing.T) { 124 publicKey, privateKey, _ := util.GenerateMockPublicPrivateKeyPairPEM(false) 125 caPublicKey, _, _ := util.GenerateMockPublicPrivateKeyPairPEM(true) 126 127 t.Run("BadPrivateKey", func(t *testing.T) { 128 assert.Panics(t, func() { 129 newBrokerConfig(localconfig.TLS{ 130 Enabled: true, 131 PrivateKey: privateKey, 132 Certificate: "TRASH", 133 RootCAs: []string{caPublicKey}, 134 }, 135 mockLocalConfig.Kafka.SASLPlain, 136 mockLocalConfig.Kafka.Retry, 137 mockLocalConfig.Kafka.Version, 138 defaultPartition) 139 }) 140 }) 141 t.Run("BadPublicKey", func(t *testing.T) { 142 assert.Panics(t, func() { 143 newBrokerConfig(localconfig.TLS{ 144 Enabled: true, 145 PrivateKey: "TRASH", 146 Certificate: publicKey, 147 RootCAs: []string{caPublicKey}, 148 }, 149 mockLocalConfig.Kafka.SASLPlain, 150 mockLocalConfig.Kafka.Retry, 151 mockLocalConfig.Kafka.Version, 152 defaultPartition) 153 }) 154 }) 155 t.Run("BadRootCAs", func(t *testing.T) { 156 assert.Panics(t, func() { 157 newBrokerConfig(localconfig.TLS{ 158 Enabled: true, 159 PrivateKey: privateKey, 160 Certificate: publicKey, 161 RootCAs: []string{"TRASH"}, 162 }, 163 mockLocalConfig.Kafka.SASLPlain, 164 mockLocalConfig.Kafka.Retry, 165 mockLocalConfig.Kafka.Version, 166 defaultPartition) 167 }) 168 }) 169 }