-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_test.go
171 lines (146 loc) · 3.79 KB
/
log_test.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"context"
"errors"
"net/http"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hansmi/paperhooks/pkg/client"
"github.com/hansmi/prometheus-paperless-exporter/internal/testutil"
)
type fakeLogClient struct {
names []string
entries map[string][]client.LogEntry
listErr error
getErr error
}
func (c *fakeLogClient) addEntries(name string, e []client.LogEntry) {
if c.entries == nil {
c.entries = map[string][]client.LogEntry{}
}
c.entries[name] = append(c.entries[name], e...)
}
func (c *fakeLogClient) ListLogs(context.Context) ([]string, *client.Response, error) {
return c.names, nil, c.listErr
}
func (c *fakeLogClient) GetLog(_ context.Context, name string) ([]client.LogEntry, *client.Response, error) {
if c.getErr != nil {
return nil, nil, c.getErr
}
entries, ok := c.entries[name]
if !ok {
return nil, nil, &client.RequestError{StatusCode: http.StatusNotFound}
}
return entries, nil, nil
}
func TestLog(t *testing.T) {
errTest := errors.New("test error")
for _, tc := range []struct {
name string
cl fakeLogClient
wantErr error
}{
{name: "empty"},
{
name: "listing fails",
cl: fakeLogClient{
listErr: errTest,
},
wantErr: errTest,
},
{
name: "get fails",
cl: fakeLogClient{
names: []string{"foo", "bar"},
getErr: errTest,
},
wantErr: errTest,
},
{
name: "empty logs",
cl: fakeLogClient{
names: []string{"first", "second", "error404"},
entries: map[string][]client.LogEntry{
"first": nil,
"second": nil,
},
},
},
{
name: "entries",
cl: fakeLogClient{
names: []string{"first", "second"},
entries: map[string][]client.LogEntry{
"first": []client.LogEntry{
{
Time: time.Date(2020, time.March, 1, 0, 0, 0, 0, time.UTC),
Message: "aaa",
},
{
Time: time.Date(2020, time.March, 2, 0, 0, 0, 0, time.UTC),
Message: "bbb",
},
},
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
c := newLogCollector(&tc.cl)
err := c.collect(context.Background(), testutil.DiscardMetrics(t))
if diff := cmp.Diff(tc.wantErr, err, cmpopts.EquateErrors()); diff != "" {
t.Errorf("Error diff (-want +got):\n%s", diff)
}
})
}
}
func TestLogCollect(t *testing.T) {
cl := fakeLogClient{
entries: map[string][]client.LogEntry{},
}
c := newMultiCollector(newLogCollector(&cl))
testutil.CollectAndCompare(t, c, "")
cl.names = append(cl.names, "server", "db", "not found")
testutil.CollectAndCompare(t, c, "")
cl.addEntries("server", []client.LogEntry{
{
Time: time.Date(2020, time.March, 2, 0, 0, 0, 0, time.UTC),
Module: "storage",
},
{
Time: time.Date(2020, time.April, 1, 0, 0, 0, 0, time.UTC),
Module: "storage",
Level: "another",
},
})
testutil.CollectAndCompare(t, c, `
# HELP paperless_log_entries_total Best-effort count of log entries.
# TYPE paperless_log_entries_total counter
paperless_log_entries_total{level="",module="storage",name="server"} 1
paperless_log_entries_total{level="another",module="storage",name="server"} 1
`)
cl.addEntries("server", []client.LogEntry{
{
Time: time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC),
Module: "storage",
},
})
for range [3]int{} {
cl.addEntries("db", []client.LogEntry{
{
Time: time.Date(2021, time.January, 1, 2, 3, 0, 0, time.UTC),
},
})
testutil.CollectAndCompare(t, c, `
# HELP paperless_log_entries_total Best-effort count of log entries.
# TYPE paperless_log_entries_total counter
paperless_log_entries_total{level="",module="",name="db"} 1
paperless_log_entries_total{level="",module="storage",name="server"} 2
paperless_log_entries_total{level="another",module="storage",name="server"} 1
`)
// Reset logs
cl.entries = nil
}
}