Path: blob/main/component/common/loki/client/multi_test.go
4096 views
package client12// This code is copied from Promtail. The client package is used to configure3// and run the clients that can send log entries to a Loki instance.45import (6"net/url"7"reflect"8"testing"9"time"1011"github.com/grafana/agent/component/common/loki/client/fake"1213"github.com/go-kit/log"14"github.com/grafana/dskit/backoff"15"github.com/grafana/dskit/flagext"16"github.com/prometheus/client_golang/prometheus"17"github.com/prometheus/common/model"18"github.com/stretchr/testify/require"1920"github.com/grafana/agent/component/common/loki"2122"github.com/grafana/loki/pkg/logproto"23lokiflag "github.com/grafana/loki/pkg/util/flagext"24util_log "github.com/grafana/loki/pkg/util/log"25)2627var (28nilMetrics = NewMetrics(nil, nil)29metrics = NewMetrics(prometheus.DefaultRegisterer, nil)30)3132func TestNewMulti(t *testing.T) {33_, err := NewMulti(nilMetrics, nil, util_log.Logger, 0, []Config{}...)34if err == nil {35t.Fatal("expected err but got nil")36}37host1, _ := url.Parse("http://localhost:3100")38host2, _ := url.Parse("https://grafana.com")39cc1 := Config{40BatchSize: 20,41BatchWait: 1 * time.Second,42URL: flagext.URLValue{URL: host1},43ExternalLabels: lokiflag.LabelSet{LabelSet: model.LabelSet{"order": "yaml"}},44}45cc2 := Config{46BatchSize: 10,47BatchWait: 1 * time.Second,48URL: flagext.URLValue{URL: host2},49ExternalLabels: lokiflag.LabelSet{LabelSet: model.LabelSet{"hi": "there"}},50}5152clients, err := NewMulti(metrics, nil, util_log.Logger, 0, cc1, cc2)53if err != nil {54t.Fatalf("expected err: nil got:%v", err)55}56multi := clients.(*MultiClient)57if len(multi.clients) != 2 {58t.Fatalf("expected client: 2 got:%d", len(multi.clients))59}60actualCfg1 := clients.(*MultiClient).clients[0].(*client).cfg61// Yaml should overridden the command line so 'order: yaml' should be expected62expectedCfg1 := Config{63BatchSize: 20,64BatchWait: 1 * time.Second,65URL: flagext.URLValue{URL: host1},66ExternalLabels: lokiflag.LabelSet{LabelSet: model.LabelSet{"order": "yaml"}},67}6869if !reflect.DeepEqual(actualCfg1, expectedCfg1) {70t.Fatalf("expected cfg: %v got:%v", expectedCfg1, actualCfg1)71}72}7374func TestNewMulti_BlockDuplicates(t *testing.T) {75_, err := NewMulti(nilMetrics, nil, util_log.Logger, 0, []Config{}...)76if err == nil {77t.Fatal("expected err but got nil")78}79host1, _ := url.Parse("http://localhost:3100")80cc1 := Config{81BatchSize: 20,82BatchWait: 1 * time.Second,83URL: flagext.URLValue{URL: host1},84ExternalLabels: lokiflag.LabelSet{LabelSet: model.LabelSet{"order": "yaml"}},85}86cc1Copy := cc18788_, err = NewMulti(metrics, nil, util_log.Logger, 0, cc1, cc1Copy)89require.Error(t, err, "expected NewMulti to reject duplicate client configs")9091cc1Copy.Name = "copy"92clients, err := NewMulti(metrics, nil, util_log.Logger, 0, cc1, cc1Copy)93require.NoError(t, err, "expected NewMulti to reject duplicate client configs")9495multi := clients.(*MultiClient)96if len(multi.clients) != 2 {97t.Fatalf("expected client: 2 got:%d", len(multi.clients))98}99actualCfg1 := clients.(*MultiClient).clients[0].(*client).cfg100// Yaml should overridden the command line so 'order: yaml' should be expected101expectedCfg1 := Config{102BatchSize: 20,103BatchWait: 1 * time.Second,104URL: flagext.URLValue{URL: host1},105ExternalLabels: lokiflag.LabelSet{LabelSet: model.LabelSet{"order": "yaml"}},106}107108if !reflect.DeepEqual(actualCfg1, expectedCfg1) {109t.Fatalf("expected cfg: %v got:%v", expectedCfg1, actualCfg1)110}111}112113func TestMultiClient_Stop(t *testing.T) {114var stopped int115116stopping := func() {117stopped++118}119fc := fake.NewClient(stopping)120clients := []Client{fc, fc, fc, fc}121m := &MultiClient{122clients: clients,123entries: make(chan loki.Entry),124}125m.start()126m.Stop()127128if stopped != len(clients) {129t.Fatal("missing stop call")130}131}132133func TestMultiClient_Handle(t *testing.T) {134f := fake.NewClient(func() {})135clients := []Client{f, f, f, f, f, f}136m := &MultiClient{137clients: clients,138entries: make(chan loki.Entry),139}140m.start()141142m.Chan() <- loki.Entry{Labels: model.LabelSet{"foo": "bar"}, Entry: logproto.Entry{Line: "foo"}}143144m.Stop()145146if len(f.Received()) != len(clients) {147t.Fatal("missing handle call")148}149}150151func TestMultiClient_Handle_Race(t *testing.T) {152u := flagext.URLValue{}153require.NoError(t, u.Set("http://localhost"))154c1, err := New(nilMetrics, Config{URL: u, BackoffConfig: backoff.Config{MaxRetries: 1}, Timeout: time.Microsecond}, nil, 0, log.NewNopLogger())155require.NoError(t, err)156c2, err := New(nilMetrics, Config{URL: u, BackoffConfig: backoff.Config{MaxRetries: 1}, Timeout: time.Microsecond}, nil, 0, log.NewNopLogger())157require.NoError(t, err)158clients := []Client{c1, c2}159m := &MultiClient{160clients: clients,161entries: make(chan loki.Entry),162}163m.start()164165m.Chan() <- loki.Entry{Labels: model.LabelSet{"foo": "bar", ReservedLabelTenantID: "1"}, Entry: logproto.Entry{Line: "foo"}}166167m.Stop()168}169170171