github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/localconfig/config_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  	"fmt"
    21  	"os"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/hyperledger/fabric/common/viperutil"
    26  
    27  	"github.com/spf13/viper"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestGoodConfig(t *testing.T) {
    32  	config := Load()
    33  	if config == nil {
    34  		t.Fatalf("Could not load config")
    35  	}
    36  	t.Logf("%+v", config)
    37  }
    38  
    39  func TestBadConfig(t *testing.T) {
    40  	config := viper.New()
    41  	config.SetConfigName("orderer")
    42  	config.AddConfigPath("../")
    43  
    44  	err := config.ReadInConfig()
    45  	if err != nil {
    46  		t.Fatalf("Error reading %s plugin config: %s", Prefix, err)
    47  	}
    48  
    49  	var uconf struct{}
    50  
    51  	err = viperutil.EnhancedExactUnmarshal(config, &uconf)
    52  	if err == nil {
    53  		t.Fatalf("Should have failed to unmarshal")
    54  	}
    55  }
    56  
    57  // TestEnvInnerVar verifies that with the Unmarshal function that
    58  // the environmental overrides still work on internal vars.  This was
    59  // a bug in the original viper implementation that is worked around in
    60  // the Load codepath for now
    61  func TestEnvInnerVar(t *testing.T) {
    62  	envVar1 := "ORDERER_GENERAL_LISTENPORT"
    63  	envVal1 := uint16(80)
    64  	envVar2 := "ORDERER_KAFKA_RETRY_PERIOD"
    65  	envVal2 := "42s"
    66  	os.Setenv(envVar1, fmt.Sprintf("%d", envVal1))
    67  	os.Setenv(envVar2, envVal2)
    68  	defer os.Unsetenv(envVar1)
    69  	defer os.Unsetenv(envVar2)
    70  	config := Load()
    71  
    72  	if config == nil {
    73  		t.Fatalf("Could not load config")
    74  	}
    75  
    76  	if config.General.ListenPort != envVal1 {
    77  		t.Fatalf("Environmental override of inner config test 1 did not work")
    78  	}
    79  	v2, _ := time.ParseDuration(envVal2)
    80  	if config.Kafka.Retry.Period != v2 {
    81  		t.Fatalf("Environmental override of inner config test 2 did not work")
    82  	}
    83  }
    84  
    85  func TestKafkaTLSConfig(t *testing.T) {
    86  	testCases := []struct {
    87  		name        string
    88  		tls         TLS
    89  		shouldPanic bool
    90  	}{
    91  		{"Disabled", TLS{Enabled: false}, false},
    92  		{"EnabledNoPrivateKey", TLS{Enabled: true, Certificate: "public.key"}, true},
    93  		{"EnabledNoPublicKey", TLS{Enabled: true, PrivateKey: "private.key"}, true},
    94  		{"EnabledNoTrustedRoots", TLS{Enabled: true, PrivateKey: "private.key", Certificate: "public.key"}, true},
    95  	}
    96  	for _, tc := range testCases {
    97  		t.Run(tc.name, func(t *testing.T) {
    98  			uconf := &TopLevel{Kafka: Kafka{TLS: tc.tls}}
    99  			if tc.shouldPanic {
   100  				assert.Panics(t, func() { uconf.completeInitialization() }, "should panic")
   101  			} else {
   102  				assert.NotPanics(t, func() { uconf.completeInitialization() }, "should not panic")
   103  			}
   104  		})
   105  	}
   106  }