Path: blob/main/component/loki/source/cloudflare/internal/cloudflaretarget/util_test.go
4097 views
package cloudflaretarget12// This code is copied from Promtail. The cloudflaretarget package is used to3// configure and run a target that can read from the Cloudflare Logpull API and4// forward entries to other loki components.56import (7"context"8"errors"9"sync"10"time"1112"github.com/grafana/cloudflare-go"13"github.com/stretchr/testify/mock"14)1516var ErrorLogpullReceived = errors.New("error logpull received")1718type fakeCloudflareClient struct {19mut sync.RWMutex20mock.Mock21}2223func (f *fakeCloudflareClient) CallCount() int {24var actualCalls int25f.mut.RLock()26for _, call := range f.Calls {27if call.Method == "LogpullReceived" {28actualCalls++29}30}31f.mut.RUnlock()32return actualCalls33}3435type fakeLogIterator struct {36logs []string37current string3839err error40}4142func (f *fakeLogIterator) Next() bool {43if len(f.logs) == 0 {44return false45}46f.current = f.logs[0]47if f.current == `error` {48f.err = errors.New("error")49return false50}51f.logs = f.logs[1:]52return true53}54func (f *fakeLogIterator) Err() error { return f.err }55func (f *fakeLogIterator) Line() []byte { return []byte(f.current) }56func (f *fakeLogIterator) Fields() (map[string]string, error) { return nil, nil }57func (f *fakeLogIterator) Close() error {58if f.err == ErrorLogpullReceived {59f.err = nil60}61return nil62}6364func newFakeCloudflareClient() *fakeCloudflareClient {65return &fakeCloudflareClient{}66}6768func (f *fakeCloudflareClient) LogpullReceived(ctx context.Context, start, end time.Time) (cloudflare.LogpullReceivedIterator, error) {69f.mut.Lock()70defer f.mut.Unlock()7172r := f.Called(ctx, start, end)73if r.Get(0) != nil {74it := r.Get(0).(cloudflare.LogpullReceivedIterator)75if it.Err() == ErrorLogpullReceived {76return it, it.Err()77}78return it, nil79}80return nil, r.Error(1)81}828384