github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/orderer/common/server/main_test.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package server 8 9 import ( 10 "io/ioutil" 11 "log" 12 "net" 13 "net/http" 14 "os" 15 "strconv" 16 "strings" 17 "testing" 18 "time" 19 20 "github.com/Shopify/sarama" 21 "github.com/hyperledger/fabric/bccsp/factory" 22 "github.com/hyperledger/fabric/common/flogging" 23 "github.com/hyperledger/fabric/common/localmsp" 24 coreconfig "github.com/hyperledger/fabric/core/config" 25 config "github.com/hyperledger/fabric/orderer/common/localconfig" 26 logging "github.com/op/go-logging" 27 // logging "github.com/op/go-logging" 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestInitializeLoggingLevel(t *testing.T) { 32 initializeLoggingLevel( 33 &config.TopLevel{ 34 General: config.General{LogLevel: "debug"}, 35 Kafka: config.Kafka{Verbose: true}, 36 }, 37 ) 38 assert.Equal(t, flogging.GetModuleLevel("orderer/main"), "DEBUG") 39 assert.NotNil(t, sarama.Logger) 40 } 41 42 func TestInitializeProfilingService(t *testing.T) { 43 // get a free random port 44 listenAddr := func() string { 45 l, _ := net.Listen("tcp", "localhost:0") 46 l.Close() 47 return l.Addr().String() 48 }() 49 initializeProfilingService( 50 &config.TopLevel{ 51 General: config.General{ 52 LogLevel: "debug", 53 Profile: config.Profile{ 54 Enabled: true, 55 Address: listenAddr, 56 }}, 57 Kafka: config.Kafka{Verbose: true}, 58 }, 59 ) 60 time.Sleep(500 * time.Millisecond) 61 if _, err := http.Get("http://" + listenAddr + "/" + "/debug/"); err != nil { 62 t.Logf("Expected pprof to be up (will retry again in 3 seconds): %s", err) 63 time.Sleep(3 * time.Second) 64 if _, err := http.Get("http://" + listenAddr + "/" + "/debug/"); err != nil { 65 t.Fatalf("Expected pprof to be up: %s", err) 66 } 67 } 68 } 69 70 func TestInitializeSecureServerConfig(t *testing.T) { 71 initializeSecureServerConfig( 72 &config.TopLevel{ 73 General: config.General{ 74 TLS: config.TLS{ 75 Enabled: true, 76 ClientAuthEnabled: true, 77 Certificate: "main.go", 78 PrivateKey: "main.go", 79 RootCAs: []string{"main.go"}, 80 ClientRootCAs: []string{"main.go"}, 81 }, 82 }, 83 }) 84 85 goodFile := "main.go" 86 badFile := "does_not_exist" 87 88 logger.SetBackend(logging.AddModuleLevel(newPanicOnCriticalBackend())) 89 defer func() { 90 logger = logging.MustGetLogger("orderer/main") 91 }() 92 93 testCases := []struct { 94 name string 95 certificate string 96 privateKey string 97 rootCA string 98 clientCertificate string 99 }{ 100 {"BadCertificate", badFile, goodFile, goodFile, goodFile}, 101 {"BadPrivateKey", goodFile, badFile, goodFile, goodFile}, 102 {"BadRootCA", goodFile, goodFile, badFile, goodFile}, 103 {"BadClientCertificate", goodFile, goodFile, goodFile, badFile}, 104 } 105 for _, tc := range testCases { 106 t.Run(tc.name, func(t *testing.T) { 107 assert.Panics(t, func() { 108 initializeSecureServerConfig( 109 &config.TopLevel{ 110 General: config.General{ 111 TLS: config.TLS{ 112 Enabled: true, 113 ClientAuthEnabled: true, 114 Certificate: tc.certificate, 115 PrivateKey: tc.privateKey, 116 RootCAs: []string{tc.rootCA}, 117 ClientRootCAs: []string{tc.clientCertificate}, 118 }, 119 }, 120 }) 121 }, 122 ) 123 }) 124 } 125 } 126 127 func TestInitializeBootstrapChannel(t *testing.T) { 128 testCases := []struct { 129 genesisMethod string 130 ledgerType string 131 panics bool 132 }{ 133 {"provisional", "ram", false}, 134 {"provisional", "file", false}, 135 {"provisional", "json", false}, 136 {"invalid", "ram", true}, 137 {"file", "ram", true}, 138 } 139 140 for _, tc := range testCases { 141 142 t.Run(tc.genesisMethod+"/"+tc.ledgerType, func(t *testing.T) { 143 144 fileLedgerLocation, _ := ioutil.TempDir("", "test-ledger") 145 ledgerFactory, _ := createLedgerFactory( 146 &config.TopLevel{ 147 General: config.General{LedgerType: tc.ledgerType}, 148 FileLedger: config.FileLedger{ 149 Location: fileLedgerLocation, 150 }, 151 }, 152 ) 153 154 bootstrapConfig := &config.TopLevel{ 155 General: config.General{ 156 GenesisMethod: tc.genesisMethod, 157 GenesisProfile: "SampleSingleMSPSolo", 158 GenesisFile: "genesisblock", 159 }, 160 } 161 162 if tc.panics { 163 assert.Panics(t, func() { 164 initializeBootstrapChannel(bootstrapConfig, ledgerFactory) 165 }) 166 } else { 167 initializeBootstrapChannel(bootstrapConfig, ledgerFactory) 168 } 169 170 }) 171 } 172 } 173 174 func TestInitializeLocalMsp(t *testing.T) { 175 t.Run("Happy", func(t *testing.T) { 176 assert.NotPanics(t, func() { 177 localMSPDir, _ := coreconfig.GetDevMspDir() 178 initializeLocalMsp( 179 &config.TopLevel{ 180 General: config.General{ 181 LocalMSPDir: localMSPDir, 182 LocalMSPID: "DEFAULT", 183 BCCSP: &factory.FactoryOpts{ 184 ProviderName: "SW", 185 SwOpts: &factory.SwOpts{ 186 HashFamily: "SHA2", 187 SecLevel: 256, 188 Ephemeral: true, 189 }, 190 }, 191 }, 192 }) 193 }) 194 }) 195 t.Run("Error", func(t *testing.T) { 196 logger.SetBackend(logging.AddModuleLevel(newPanicOnCriticalBackend())) 197 defer func() { 198 logger = logging.MustGetLogger("orderer/main") 199 }() 200 assert.Panics(t, func() { 201 initializeLocalMsp( 202 &config.TopLevel{ 203 General: config.General{ 204 LocalMSPDir: "", 205 LocalMSPID: "", 206 }, 207 }) 208 }) 209 }) 210 } 211 212 func TestInitializeMultiChainManager(t *testing.T) { 213 localMSPDir, _ := coreconfig.GetDevMspDir() 214 conf := &config.TopLevel{ 215 General: config.General{ 216 LedgerType: "ram", 217 GenesisMethod: "provisional", 218 GenesisProfile: "SampleSingleMSPSolo", 219 LocalMSPDir: localMSPDir, 220 LocalMSPID: "DEFAULT", 221 BCCSP: &factory.FactoryOpts{ 222 ProviderName: "SW", 223 SwOpts: &factory.SwOpts{ 224 HashFamily: "SHA2", 225 SecLevel: 256, 226 Ephemeral: true, 227 }, 228 }, 229 }, 230 } 231 assert.NotPanics(t, func() { 232 initializeLocalMsp(conf) 233 initializeMultiChainManager(conf, localmsp.NewSigner()) 234 }) 235 } 236 237 func TestInitializeGrpcServer(t *testing.T) { 238 // get a free random port 239 listenAddr := func() string { 240 l, _ := net.Listen("tcp", "localhost:0") 241 l.Close() 242 return l.Addr().String() 243 }() 244 host := strings.Split(listenAddr, ":")[0] 245 port, _ := strconv.ParseUint(strings.Split(listenAddr, ":")[1], 10, 16) 246 assert.NotPanics(t, func() { 247 grpcServer := initializeGrpcServer( 248 &config.TopLevel{ 249 General: config.General{ 250 ListenAddress: host, 251 ListenPort: uint16(port), 252 TLS: config.TLS{ 253 Enabled: false, 254 ClientAuthEnabled: false, 255 }, 256 }, 257 }) 258 grpcServer.Listener().Close() 259 }) 260 } 261 262 // var originalLogger *Logger 263 264 func newPanicOnCriticalBackend() *panicOnCriticalBackend { 265 return &panicOnCriticalBackend{ 266 backend: logging.AddModuleLevel(logging.NewLogBackend(os.Stderr, "", log.LstdFlags)), 267 } 268 } 269 270 type panicOnCriticalBackend struct { 271 backend logging.Backend 272 } 273 274 func (b *panicOnCriticalBackend) Log(level logging.Level, calldepth int, record *logging.Record) error { 275 err := b.backend.Log(level, calldepth, record) 276 if level == logging.CRITICAL { 277 panic(record.Formatted(calldepth)) 278 } 279 return err 280 }