forked from smartcontractkit/wasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloki_mock.go
75 lines (62 loc) · 1.94 KB
/
loki_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package wasp
import (
"encoding/json"
"fmt"
"time"
"github.com/grafana/loki/pkg/promtail/api"
lokiClient "github.com/grafana/loki/pkg/promtail/client"
"github.com/prometheus/common/model"
)
type PromtailSendResult struct {
Labels model.LabelSet
Time time.Time
Entry string
}
type MockPromtailClient struct {
Results []PromtailSendResult
OnHandleEntry api.EntryHandlerFunc
}
// ExtendedLokiClient an extended Loki/Promtail client used for testing last results in batch
type ExtendedLokiClient interface {
lokiClient.Client
api.EntryHandler
HandleStruct(ls model.LabelSet, t time.Time, st interface{}) error
LastHandleResult() PromtailSendResult
AllHandleResults() []PromtailSendResult
}
func (m *LokiClient) LastHandleResult() PromtailSendResult {
panic("implement me")
}
func (m *LokiClient) AllHandleResults() []PromtailSendResult {
panic("implement me")
}
func NewMockPromtailClient() ExtendedLokiClient {
mc := &MockPromtailClient{
Results: make([]PromtailSendResult, 0),
}
mc.OnHandleEntry = func(labels model.LabelSet, time time.Time, entry string) error {
mc.Results = append(mc.Results, PromtailSendResult{Labels: labels, Time: time, Entry: entry})
return nil
}
return mc
}
func (c *MockPromtailClient) HandleStruct(ls model.LabelSet, t time.Time, st interface{}) error {
d, err := json.Marshal(st)
if err != nil {
return fmt.Errorf("failed to marshal struct in response: %v", st)
}
return c.OnHandleEntry.Handle(ls, t, string(d))
}
// Stop implements client.Client
func (c *MockPromtailClient) Stop() {}
// Handle implements client.Client
func (c *MockPromtailClient) Handle(labels model.LabelSet, time time.Time, entry string) error {
return c.OnHandleEntry.Handle(labels, time, entry)
}
func (c *MockPromtailClient) LastHandleResult() PromtailSendResult {
time.Sleep(2 * time.Second)
return c.Results[len(c.Results)-1]
}
func (c *MockPromtailClient) AllHandleResults() []PromtailSendResult {
return c.Results
}