github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/clients/pkg/promtail/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"testing"
     7  
     8  	"github.com/go-kit/log"
     9  	dskitflagext "github.com/grafana/dskit/flagext"
    10  	"github.com/prometheus/common/model"
    11  	"github.com/stretchr/testify/require"
    12  	"gopkg.in/yaml.v2"
    13  
    14  	"github.com/grafana/loki/clients/pkg/promtail/client"
    15  
    16  	"github.com/grafana/loki/pkg/util/flagext"
    17  )
    18  
    19  const testFile = `
    20  clients:
    21    - external_labels:
    22        cluster: dev1
    23      url: https://1:shh@example.com/loki/api/v1/push
    24    - external_labels:
    25        cluster: prod1
    26      url: https://1:shh@example.com/loki/api/v1/push
    27  scrape_configs:
    28    - job_name: kubernetes-pods-name
    29      kubernetes_sd_configs:
    30        - role: pod
    31    - job_name: system
    32      static_configs:
    33      - targets:
    34        - localhost
    35        labels:
    36          job: varlogs
    37  limits_config:
    38    readline_rate: 100
    39    readline_burst: 200
    40  options:
    41    stream_lag_labels: foo
    42  `
    43  
    44  func Test_Load(t *testing.T) {
    45  	var dst Config
    46  	err := yaml.Unmarshal([]byte(testFile), &dst)
    47  	require.Nil(t, err)
    48  }
    49  
    50  func Test_RateLimitLoad(t *testing.T) {
    51  	var dst Config
    52  	err := yaml.Unmarshal([]byte(testFile), &dst)
    53  	require.Nil(t, err)
    54  	config := dst.LimitsConfig
    55  	require.Equal(t, float64(100), config.ReadlineRate)
    56  	require.Equal(t, 200, config.ReadlineBurst)
    57  }
    58  
    59  func TestConfig_Setup(t *testing.T) {
    60  	for i, tt := range []struct {
    61  		in       Config
    62  		expected Config
    63  	}{
    64  		{
    65  			Config{
    66  				ClientConfig: client.Config{
    67  					ExternalLabels: flagext.LabelSet{LabelSet: model.LabelSet{"foo": "bar"}},
    68  				},
    69  				ClientConfigs: []client.Config{
    70  					{
    71  						ExternalLabels: flagext.LabelSet{LabelSet: model.LabelSet{"client1": "1"}},
    72  					},
    73  					{
    74  						ExternalLabels: flagext.LabelSet{LabelSet: model.LabelSet{"client2": "2"}},
    75  					},
    76  				},
    77  				Options: Options{
    78  					StreamLagLabels: []string{},
    79  				},
    80  			},
    81  			Config{
    82  				ClientConfig: client.Config{
    83  					ExternalLabels: flagext.LabelSet{LabelSet: model.LabelSet{"foo": "bar"}},
    84  				},
    85  				ClientConfigs: []client.Config{
    86  					{
    87  						ExternalLabels: flagext.LabelSet{LabelSet: model.LabelSet{"client1": "1", "foo": "bar"}},
    88  					},
    89  					{
    90  						ExternalLabels: flagext.LabelSet{LabelSet: model.LabelSet{"client2": "2", "foo": "bar"}},
    91  					},
    92  				},
    93  				Options: Options{
    94  					StreamLagLabels: []string{},
    95  				},
    96  			},
    97  		},
    98  		{
    99  			Config{
   100  				ClientConfig: client.Config{
   101  					ExternalLabels: flagext.LabelSet{LabelSet: model.LabelSet{"foo": "bar"}},
   102  					URL:            dskitflagext.URLValue{URL: mustURL("http://foo")},
   103  				},
   104  				ClientConfigs: []client.Config{
   105  					{
   106  						ExternalLabels: flagext.LabelSet{LabelSet: model.LabelSet{"client1": "1"}},
   107  					},
   108  					{
   109  						ExternalLabels: flagext.LabelSet{LabelSet: model.LabelSet{"client2": "2"}},
   110  					},
   111  				},
   112  				Options: Options{
   113  					StreamLagLabels: []string{},
   114  				},
   115  			},
   116  			Config{
   117  				ClientConfig: client.Config{
   118  					ExternalLabels: flagext.LabelSet{LabelSet: model.LabelSet{"foo": "bar"}},
   119  					URL:            dskitflagext.URLValue{URL: mustURL("http://foo")},
   120  				},
   121  				ClientConfigs: []client.Config{
   122  					{
   123  						ExternalLabels: flagext.LabelSet{LabelSet: model.LabelSet{"client1": "1", "foo": "bar"}},
   124  					},
   125  					{
   126  						ExternalLabels: flagext.LabelSet{LabelSet: model.LabelSet{"client2": "2", "foo": "bar"}},
   127  					},
   128  					{
   129  						ExternalLabels: flagext.LabelSet{LabelSet: model.LabelSet{"foo": "bar"}},
   130  						URL:            dskitflagext.URLValue{URL: mustURL("http://foo")},
   131  					},
   132  				},
   133  				Options: Options{
   134  					StreamLagLabels: []string{},
   135  				},
   136  			},
   137  		},
   138  	} {
   139  		tt := tt
   140  		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
   141  			tt.in.Setup(log.NewNopLogger())
   142  			require.Equal(t, tt.expected, tt.in)
   143  		})
   144  	}
   145  }
   146  
   147  func mustURL(u string) *url.URL {
   148  	res, err := url.Parse(u)
   149  	if err != nil {
   150  		panic(err)
   151  	}
   152  	return res
   153  }