github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/orderer/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/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(mockLocalConfig.General.TLS, mockLocalConfig.Kafka.Retry, mockLocalConfig.Kafka.Version, differentPartition) 44 producer, _ := sarama.NewSyncProducer([]string{mockBroker.Addr()}, mockBrokerConfig2) 45 defer func() { producer.Close() }() 46 47 for i := 0; i < 10; i++ { 48 assignedPartition, _, err := producer.SendMessage(&sarama.ProducerMessage{Topic: mockChannel2.topic()}) 49 assert.NoError(t, err, "Failed to send message:", err) 50 assert.Equal(t, differentPartition, assignedPartition, "Message wasn't posted to the right partition - expected %d, got %v", differentPartition, assignedPartition) 51 } 52 }) 53 54 producer, _ := sarama.NewSyncProducer([]string{mockBroker.Addr()}, mockBrokerConfig) 55 defer func() { producer.Close() }() 56 57 testCases := []struct { 58 name string 59 size int 60 err error 61 }{ 62 {"TypicalDeploy", 4 * 1024, nil}, 63 {"TooBig", int(sarama.MaxRequestSize + 1), sarama.ErrMessageSizeTooLarge}, 64 } 65 66 for _, tc := range testCases { 67 t.Run("ProducerMessageMaxBytes"+tc.name, func(t *testing.T) { 68 _, _, err := producer.SendMessage(&sarama.ProducerMessage{ 69 Topic: mockChannel1.topic(), 70 Value: sarama.ByteEncoder(make([]byte, tc.size)), 71 }) 72 assert.Equal(t, tc.err, err) 73 }) 74 } 75 } 76 77 func TestBrokerConfigTLSConfigEnabled(t *testing.T) { 78 publicKey, privateKey, _ := util.GenerateMockPublicPrivateKeyPairPEM(false) 79 caPublicKey, _, _ := util.GenerateMockPublicPrivateKeyPairPEM(true) 80 81 t.Run("Enabled", func(t *testing.T) { 82 testBrokerConfig := newBrokerConfig(localconfig.TLS{ 83 Enabled: true, 84 PrivateKey: privateKey, 85 Certificate: publicKey, 86 RootCAs: []string{caPublicKey}, 87 }, mockLocalConfig.Kafka.Retry, mockLocalConfig.Kafka.Version, defaultPartition) 88 89 assert.True(t, testBrokerConfig.Net.TLS.Enable) 90 assert.NotNil(t, testBrokerConfig.Net.TLS.Config) 91 assert.Len(t, testBrokerConfig.Net.TLS.Config.Certificates, 1) 92 assert.Len(t, testBrokerConfig.Net.TLS.Config.RootCAs.Subjects(), 1) 93 assert.Equal(t, uint16(0), testBrokerConfig.Net.TLS.Config.MaxVersion) 94 assert.Equal(t, uint16(tls.VersionTLS12), testBrokerConfig.Net.TLS.Config.MinVersion) 95 }) 96 97 t.Run("Disabled", func(t *testing.T) { 98 testBrokerConfig := newBrokerConfig(localconfig.TLS{ 99 Enabled: false, 100 PrivateKey: privateKey, 101 Certificate: publicKey, 102 RootCAs: []string{caPublicKey}, 103 }, mockLocalConfig.Kafka.Retry, mockLocalConfig.Kafka.Version, defaultPartition) 104 105 assert.False(t, testBrokerConfig.Net.TLS.Enable) 106 assert.Zero(t, testBrokerConfig.Net.TLS.Config) 107 }) 108 } 109 110 func TestBrokerConfigTLSConfigBadCert(t *testing.T) { 111 publicKey, privateKey, _ := util.GenerateMockPublicPrivateKeyPairPEM(false) 112 caPublicKey, _, _ := util.GenerateMockPublicPrivateKeyPairPEM(true) 113 114 t.Run("BadPrivateKey", func(t *testing.T) { 115 assert.Panics(t, func() { 116 newBrokerConfig(localconfig.TLS{ 117 Enabled: true, 118 PrivateKey: privateKey, 119 Certificate: "TRASH", 120 RootCAs: []string{caPublicKey}, 121 }, mockLocalConfig.Kafka.Retry, mockLocalConfig.Kafka.Version, defaultPartition) 122 }) 123 }) 124 t.Run("BadPublicKey", func(t *testing.T) { 125 assert.Panics(t, func() { 126 newBrokerConfig(localconfig.TLS{ 127 Enabled: true, 128 PrivateKey: "TRASH", 129 Certificate: publicKey, 130 RootCAs: []string{caPublicKey}, 131 }, mockLocalConfig.Kafka.Retry, mockLocalConfig.Kafka.Version, defaultPartition) 132 }) 133 }) 134 t.Run("BadRootCAs", func(t *testing.T) { 135 assert.Panics(t, func() { 136 newBrokerConfig(localconfig.TLS{ 137 Enabled: true, 138 PrivateKey: privateKey, 139 Certificate: publicKey, 140 RootCAs: []string{"TRASH"}, 141 }, mockLocalConfig.Kafka.Retry, mockLocalConfig.Kafka.Version, defaultPartition) 142 }) 143 }) 144 }