github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/storage/bucket/http/config_test.go (about)

     1  package http
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/grafana/dskit/flagext"
     8  	"github.com/stretchr/testify/require"
     9  	yaml "gopkg.in/yaml.v2"
    10  )
    11  
    12  // defaultConfig should match the default flag values defined in RegisterFlagsWithPrefix.
    13  var defaultConfig = Config{
    14  	IdleConnTimeout:       90 * time.Second,
    15  	ResponseHeaderTimeout: 2 * time.Minute,
    16  	InsecureSkipVerify:    false,
    17  	TLSHandshakeTimeout:   10 * time.Second,
    18  	ExpectContinueTimeout: 1 * time.Second,
    19  	MaxIdleConns:          100,
    20  	MaxIdleConnsPerHost:   100,
    21  	MaxConnsPerHost:       0,
    22  }
    23  
    24  func TestConfig(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	tests := map[string]struct {
    28  		config         string
    29  		expectedConfig Config
    30  		expectedErr    error
    31  	}{
    32  		"default config": {
    33  			config:         "",
    34  			expectedConfig: defaultConfig,
    35  			expectedErr:    nil,
    36  		},
    37  		"custom config": {
    38  			config: `
    39  idle_conn_timeout: 2s
    40  response_header_timeout: 3s
    41  insecure_skip_verify: true
    42  tls_handshake_timeout: 4s
    43  expect_continue_timeout: 5s
    44  max_idle_connections: 6
    45  max_idle_connections_per_host: 7
    46  max_connections_per_host: 8
    47  `,
    48  			expectedConfig: Config{
    49  				IdleConnTimeout:       2 * time.Second,
    50  				ResponseHeaderTimeout: 3 * time.Second,
    51  				InsecureSkipVerify:    true,
    52  				TLSHandshakeTimeout:   4 * time.Second,
    53  				ExpectContinueTimeout: 5 * time.Second,
    54  				MaxIdleConns:          6,
    55  				MaxIdleConnsPerHost:   7,
    56  				MaxConnsPerHost:       8,
    57  			},
    58  			expectedErr: nil,
    59  		},
    60  		"invalid type": {
    61  			config:         `max_idle_connections: foo`,
    62  			expectedConfig: defaultConfig,
    63  			expectedErr:    &yaml.TypeError{Errors: []string{"line 1: cannot unmarshal !!str `foo` into int"}},
    64  		},
    65  	}
    66  
    67  	for testName, testData := range tests {
    68  		testData := testData
    69  
    70  		t.Run(testName, func(t *testing.T) {
    71  			cfg := Config{}
    72  			flagext.DefaultValues(&cfg)
    73  
    74  			err := yaml.Unmarshal([]byte(testData.config), &cfg)
    75  			require.Equal(t, testData.expectedErr, err)
    76  			require.Equal(t, testData.expectedConfig, cfg)
    77  		})
    78  	}
    79  }