Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/loki/source/cloudflare/internal/cloudflaretarget/util_test.go
4097 views
1
package cloudflaretarget
2
3
// This code is copied from Promtail. The cloudflaretarget package is used to
4
// configure and run a target that can read from the Cloudflare Logpull API and
5
// forward entries to other loki components.
6
7
import (
8
"context"
9
"errors"
10
"sync"
11
"time"
12
13
"github.com/grafana/cloudflare-go"
14
"github.com/stretchr/testify/mock"
15
)
16
17
var ErrorLogpullReceived = errors.New("error logpull received")
18
19
type fakeCloudflareClient struct {
20
mut sync.RWMutex
21
mock.Mock
22
}
23
24
func (f *fakeCloudflareClient) CallCount() int {
25
var actualCalls int
26
f.mut.RLock()
27
for _, call := range f.Calls {
28
if call.Method == "LogpullReceived" {
29
actualCalls++
30
}
31
}
32
f.mut.RUnlock()
33
return actualCalls
34
}
35
36
type fakeLogIterator struct {
37
logs []string
38
current string
39
40
err error
41
}
42
43
func (f *fakeLogIterator) Next() bool {
44
if len(f.logs) == 0 {
45
return false
46
}
47
f.current = f.logs[0]
48
if f.current == `error` {
49
f.err = errors.New("error")
50
return false
51
}
52
f.logs = f.logs[1:]
53
return true
54
}
55
func (f *fakeLogIterator) Err() error { return f.err }
56
func (f *fakeLogIterator) Line() []byte { return []byte(f.current) }
57
func (f *fakeLogIterator) Fields() (map[string]string, error) { return nil, nil }
58
func (f *fakeLogIterator) Close() error {
59
if f.err == ErrorLogpullReceived {
60
f.err = nil
61
}
62
return nil
63
}
64
65
func newFakeCloudflareClient() *fakeCloudflareClient {
66
return &fakeCloudflareClient{}
67
}
68
69
func (f *fakeCloudflareClient) LogpullReceived(ctx context.Context, start, end time.Time) (cloudflare.LogpullReceivedIterator, error) {
70
f.mut.Lock()
71
defer f.mut.Unlock()
72
73
r := f.Called(ctx, start, end)
74
if r.Get(0) != nil {
75
it := r.Get(0).(cloudflare.LogpullReceivedIterator)
76
if it.Err() == ErrorLogpullReceived {
77
return it, it.Err()
78
}
79
return it, nil
80
}
81
return nil, r.Error(1)
82
}
83
84