github.com/kaituanwang/hyperledger@v2.0.1+incompatible/internal/configtxgen/genesisconfig/config_test.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package genesisconfig 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric-protos-go/orderer/etcdraft" 13 "github.com/hyperledger/fabric/core/config/configtest" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestLoadProfile(t *testing.T) { 18 cleanup := configtest.SetDevFabricConfigPath(t) 19 defer cleanup() 20 21 pNames := []string{ 22 SampleDevModeKafkaProfile, 23 SampleDevModeSoloProfile, 24 SampleSingleMSPChannelProfile, 25 SampleSingleMSPKafkaProfile, 26 SampleSingleMSPSoloProfile, 27 } 28 for _, pName := range pNames { 29 t.Run(pName, func(t *testing.T) { 30 p := Load(pName) 31 assert.NotNil(t, p, "profile should not be nil") 32 }) 33 } 34 } 35 36 func TestLoadProfileWithPath(t *testing.T) { 37 devConfigDir := configtest.GetDevConfigDir() 38 39 pNames := []string{ 40 SampleDevModeKafkaProfile, 41 SampleDevModeSoloProfile, 42 SampleSingleMSPChannelProfile, 43 SampleSingleMSPKafkaProfile, 44 SampleSingleMSPSoloProfile, 45 } 46 for _, pName := range pNames { 47 t.Run(pName, func(t *testing.T) { 48 p := Load(pName, devConfigDir) 49 assert.NotNil(t, p, "profile should not be nil") 50 }) 51 } 52 } 53 54 func TestLoadTopLevel(t *testing.T) { 55 cleanup := configtest.SetDevFabricConfigPath(t) 56 defer cleanup() 57 58 topLevel := LoadTopLevel() 59 assert.NotNil(t, topLevel.Application, "application should not be nil") 60 assert.NotNil(t, topLevel.Capabilities, "capabilities should not be nil") 61 assert.NotNil(t, topLevel.Orderer, "orderer should not be nil") 62 assert.NotNil(t, topLevel.Organizations, "organizations should not be nil") 63 assert.NotNil(t, topLevel.Profiles, "profiles should not be nil") 64 } 65 66 func TestLoadTopLevelWithPath(t *testing.T) { 67 devConfigDir := configtest.GetDevConfigDir() 68 69 topLevel := LoadTopLevel(devConfigDir) 70 assert.NotNil(t, topLevel.Application, "application should not be nil") 71 assert.NotNil(t, topLevel.Capabilities, "capabilities should not be nil") 72 assert.NotNil(t, topLevel.Orderer, "orderer should not be nil") 73 assert.NotNil(t, topLevel.Organizations, "organizations should not be nil") 74 assert.NotNil(t, topLevel.Profiles, "profiles should not be nil") 75 } 76 77 func TestConsensusSpecificInit(t *testing.T) { 78 cleanup := configtest.SetDevFabricConfigPath(t) 79 defer cleanup() 80 81 devConfigDir := configtest.GetDevConfigDir() 82 83 t.Run("nil orderer type", func(t *testing.T) { 84 profile := &Profile{ 85 Orderer: &Orderer{ 86 OrdererType: "", 87 }, 88 } 89 profile.completeInitialization(devConfigDir) 90 91 assert.Equal(t, profile.Orderer.OrdererType, genesisDefaults.Orderer.OrdererType) 92 }) 93 94 t.Run("unknown orderer type", func(t *testing.T) { 95 profile := &Profile{ 96 Orderer: &Orderer{ 97 OrdererType: "unknown", 98 }, 99 } 100 101 assert.Panics(t, func() { 102 profile.completeInitialization(devConfigDir) 103 }) 104 }) 105 106 t.Run("solo", func(t *testing.T) { 107 profile := &Profile{ 108 Orderer: &Orderer{ 109 OrdererType: "solo", 110 }, 111 } 112 profile.completeInitialization(devConfigDir) 113 assert.Nil(t, profile.Orderer.Kafka.Brokers, "Kafka config settings should not be set") 114 }) 115 116 t.Run("kafka", func(t *testing.T) { 117 profile := &Profile{ 118 Orderer: &Orderer{ 119 OrdererType: "kafka", 120 }, 121 } 122 profile.completeInitialization(devConfigDir) 123 assert.NotNil(t, profile.Orderer.Kafka.Brokers, "Kafka config settings should be set") 124 }) 125 126 t.Run("raft", func(t *testing.T) { 127 makeProfile := func(consenters []*etcdraft.Consenter, options *etcdraft.Options) *Profile { 128 return &Profile{ 129 Orderer: &Orderer{ 130 OrdererType: "etcdraft", 131 EtcdRaft: &etcdraft.ConfigMetadata{ 132 Consenters: consenters, 133 Options: options, 134 }, 135 }, 136 } 137 } 138 t.Run("EtcdRaft section not specified in profile", func(t *testing.T) { 139 profile := &Profile{ 140 Orderer: &Orderer{ 141 OrdererType: "etcdraft", 142 }, 143 } 144 145 assert.Panics(t, func() { 146 profile.completeInitialization(devConfigDir) 147 }) 148 }) 149 150 t.Run("nil consenter set", func(t *testing.T) { // should panic 151 profile := makeProfile(nil, nil) 152 153 assert.Panics(t, func() { 154 profile.completeInitialization(devConfigDir) 155 }) 156 }) 157 158 t.Run("single consenter", func(t *testing.T) { 159 consenters := []*etcdraft.Consenter{ 160 { 161 Host: "node-1.example.com", 162 Port: 7050, 163 ClientTlsCert: []byte("path/to/client/cert"), 164 ServerTlsCert: []byte("path/to/server/cert"), 165 }, 166 } 167 168 t.Run("invalid consenters specification", func(t *testing.T) { 169 failingConsenterSpecifications := []*etcdraft.Consenter{ 170 { // missing Host 171 Port: 7050, 172 ClientTlsCert: []byte("path/to/client/cert"), 173 ServerTlsCert: []byte("path/to/server/cert"), 174 }, 175 { // missing Port 176 Host: "node-1.example.com", 177 ClientTlsCert: []byte("path/to/client/cert"), 178 ServerTlsCert: []byte("path/to/server/cert"), 179 }, 180 { // missing ClientTlsCert 181 Host: "node-1.example.com", 182 Port: 7050, 183 ServerTlsCert: []byte("path/to/server/cert"), 184 }, 185 { // missing ServerTlsCert 186 Host: "node-1.example.com", 187 Port: 7050, 188 ClientTlsCert: []byte("path/to/client/cert"), 189 }, 190 } 191 192 for _, consenter := range failingConsenterSpecifications { 193 profile := makeProfile([]*etcdraft.Consenter{consenter}, nil) 194 195 assert.Panics(t, func() { 196 profile.completeInitialization(devConfigDir) 197 }) 198 } 199 }) 200 201 t.Run("nil Options", func(t *testing.T) { 202 profile := makeProfile(consenters, nil) 203 profile.completeInitialization(devConfigDir) 204 205 // need not be tested in subsequent tests 206 assert.NotNil(t, profile.Orderer.EtcdRaft, "EtcdRaft config settings should be set") 207 assert.Equal(t, profile.Orderer.EtcdRaft.Consenters[0].ClientTlsCert, consenters[0].ClientTlsCert, 208 "Client TLS cert path should be correctly set") 209 210 // specific assertion for this test context 211 assert.Equal(t, profile.Orderer.EtcdRaft.Options, genesisDefaults.Orderer.EtcdRaft.Options, 212 "Options should be set to the default value") 213 }) 214 215 t.Run("heartbeat tick specified in Options", func(t *testing.T) { 216 heartbeatTick := uint32(2) 217 options := &etcdraft.Options{ // partially set so that we can check that the other members are set to defaults 218 HeartbeatTick: heartbeatTick, 219 } 220 profile := makeProfile(consenters, options) 221 profile.completeInitialization(devConfigDir) 222 223 // specific assertions for this test context 224 assert.Equal(t, profile.Orderer.EtcdRaft.Options.HeartbeatTick, heartbeatTick, 225 "HeartbeatTick should be set to the specified value") 226 assert.Equal(t, profile.Orderer.EtcdRaft.Options.ElectionTick, genesisDefaults.Orderer.EtcdRaft.Options.ElectionTick, 227 "ElectionTick should be set to the default value") 228 }) 229 230 t.Run("election tick specified in Options", func(t *testing.T) { 231 electionTick := uint32(20) 232 options := &etcdraft.Options{ // partially set so that we can check that the other members are set to defaults 233 ElectionTick: electionTick, 234 } 235 profile := makeProfile(consenters, options) 236 profile.completeInitialization(devConfigDir) 237 238 // specific assertions for this test context 239 assert.Equal(t, profile.Orderer.EtcdRaft.Options.ElectionTick, electionTick, 240 "ElectionTick should be set to the specified value") 241 assert.Equal(t, profile.Orderer.EtcdRaft.Options.HeartbeatTick, genesisDefaults.Orderer.EtcdRaft.Options.HeartbeatTick, 242 "HeartbeatTick should be set to the default value") 243 }) 244 245 t.Run("panic on invalid Heartbeat and Election tick", func(t *testing.T) { 246 options := &etcdraft.Options{ 247 HeartbeatTick: 2, 248 ElectionTick: 1, 249 } 250 profile := makeProfile(consenters, options) 251 252 assert.Panics(t, func() { 253 profile.completeInitialization(devConfigDir) 254 }) 255 }) 256 257 t.Run("panic on invalid TickInterval", func(t *testing.T) { 258 options := &etcdraft.Options{ 259 TickInterval: "500", 260 } 261 profile := makeProfile(consenters, options) 262 263 assert.Panics(t, func() { 264 profile.completeInitialization(devConfigDir) 265 }) 266 }) 267 }) 268 }) 269 }