github.com/thanos-io/thanos@v0.32.5/pkg/httpconfig/config_test.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package httpconfig
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/efficientgo/core/testutil"
    10  )
    11  
    12  func TestBuildConfig(t *testing.T) {
    13  	for _, tc := range []struct {
    14  		desc      string
    15  		addresses []string
    16  		err       bool
    17  		expected  []Config
    18  	}{
    19  		{
    20  			desc:      "single addr without path",
    21  			addresses: []string{"localhost:9093"},
    22  			expected: []Config{{
    23  				EndpointsConfig: EndpointsConfig{
    24  					StaticAddresses: []string{"localhost:9093"},
    25  					Scheme:          "http",
    26  				},
    27  			}},
    28  		},
    29  		{
    30  			desc:      "1st addr without path, 2nd with",
    31  			addresses: []string{"localhost:9093", "localhost:9094/prefix"},
    32  			expected: []Config{
    33  				{
    34  					EndpointsConfig: EndpointsConfig{
    35  						StaticAddresses: []string{"localhost:9093"},
    36  						Scheme:          "http",
    37  					},
    38  				},
    39  				{
    40  					EndpointsConfig: EndpointsConfig{
    41  						StaticAddresses: []string{"localhost:9094"},
    42  						Scheme:          "http",
    43  						PathPrefix:      "/prefix",
    44  					},
    45  				},
    46  			},
    47  		},
    48  		{
    49  			desc:      "single addr with path and http scheme",
    50  			addresses: []string{"http://localhost:9093"},
    51  			expected: []Config{{
    52  				EndpointsConfig: EndpointsConfig{
    53  					StaticAddresses: []string{"localhost:9093"},
    54  					Scheme:          "http",
    55  				},
    56  			}},
    57  		},
    58  		{
    59  			desc:      "single addr with path and https scheme",
    60  			addresses: []string{"https://localhost:9093"},
    61  			expected: []Config{{
    62  				EndpointsConfig: EndpointsConfig{
    63  					StaticAddresses: []string{"localhost:9093"},
    64  					Scheme:          "https",
    65  				},
    66  			}},
    67  		},
    68  		{
    69  			desc:      "not supported scheme",
    70  			addresses: []string{"ttp://localhost:9093"},
    71  			err:       true,
    72  		},
    73  		{
    74  			desc:      "invalid addr",
    75  			addresses: []string{"this is not a valid addr"},
    76  			err:       true,
    77  		},
    78  		{
    79  			desc:      "empty addr",
    80  			addresses: []string{""},
    81  			err:       true,
    82  		},
    83  	} {
    84  		t.Run(tc.desc, func(t *testing.T) {
    85  			cfg, err := BuildConfig(tc.addresses)
    86  			if tc.err {
    87  				testutil.NotOk(t, err)
    88  				return
    89  			}
    90  
    91  			testutil.Equals(t, tc.expected, cfg)
    92  		})
    93  	}
    94  }