github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/handlers/library/config_test.go (about)

     1  /*
     2   Copyright hechain All Rights Reserved.
     3  
     4   SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package library
     8  
     9  import (
    10  	"bytes"
    11  	"os"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/spf13/viper"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestLoadConfig(t *testing.T) {
    20  	yaml := `---
    21  peer:
    22    handlers:
    23      authFilters:
    24        - name: DefaultAuth
    25          library: /path/to/default.so
    26        - name: ExpirationCheck
    27          library: /path/to/expiration.so
    28      decorators:
    29        - name: DefaultDecorator
    30          library: /path/to/decorators.so
    31      endorsers:
    32        escc:
    33          name: DefaultEndorsement
    34          library: /path/to/escc.so
    35      validators:
    36        vscc:
    37          name: DefaultValidation
    38          library: /path/to/vscc.so
    39  `
    40  
    41  	viper.SetConfigType("yaml")
    42  	err := viper.ReadConfig(bytes.NewReader([]byte(yaml)))
    43  	require.NoError(t, err)
    44  	defer viper.Reset()
    45  
    46  	actual, err := LoadConfig()
    47  	require.NoError(t, err)
    48  	require.NotNil(t, actual)
    49  	expect := Config{
    50  		AuthFilters: []*HandlerConfig{
    51  			{Name: "DefaultAuth", Library: "/path/to/default.so"},
    52  			{Name: "ExpirationCheck", Library: "/path/to/expiration.so"},
    53  		},
    54  		Decorators: []*HandlerConfig{
    55  			{Name: "DefaultDecorator", Library: "/path/to/decorators.so"},
    56  		},
    57  		Endorsers: PluginMapping{
    58  			"escc": &HandlerConfig{Name: "DefaultEndorsement", Library: "/path/to/escc.so"},
    59  		},
    60  		Validators: PluginMapping{
    61  			"vscc": &HandlerConfig{Name: "DefaultValidation", Library: "/path/to/vscc.so"},
    62  		},
    63  	}
    64  	require.EqualValues(t, expect, actual)
    65  }
    66  
    67  func TestLoadConfigEnvVarOverride(t *testing.T) {
    68  	yaml := `---
    69  peer:
    70    handlers:
    71      authFilters:
    72      decorators:
    73      endorsers:
    74        escc:
    75          name: DefaultEndorsement
    76          library:
    77      validators:
    78        vscc:
    79          name: DefaultValidation
    80          library:
    81  `
    82  
    83  	os.Setenv("LIBTEST_PEER_HANDLERS_ENDORSERS_ESCC_LIBRARY", "/path/to/foo")
    84  	defer os.Unsetenv("LIBTEST_PEER_HANDLERS_ENDORSERS_ESCC_LIBRARY")
    85  
    86  	defer viper.Reset()
    87  	viper.SetConfigType("yaml")
    88  	viper.SetEnvPrefix("LIBTEST")
    89  	viper.AutomaticEnv()
    90  	replacer := strings.NewReplacer(".", "_")
    91  	viper.SetEnvKeyReplacer(replacer)
    92  
    93  	err := viper.ReadConfig(bytes.NewReader([]byte(yaml)))
    94  	require.NoError(t, err)
    95  
    96  	actual, err := LoadConfig()
    97  	require.NoError(t, err)
    98  	require.NotNil(t, actual)
    99  
   100  	expect := Config{
   101  		AuthFilters: nil,
   102  		Decorators:  nil,
   103  		Endorsers:   PluginMapping{"escc": &HandlerConfig{Name: "DefaultEndorsement", Library: "/path/to/foo"}},
   104  		Validators:  PluginMapping{"vscc": &HandlerConfig{Name: "DefaultValidation"}},
   105  	}
   106  
   107  	require.EqualValues(t, expect, actual)
   108  }