-
Notifications
You must be signed in to change notification settings - Fork 25
/
p4prom_test.go
242 lines (217 loc) · 7.66 KB
/
p4prom_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"context"
"fmt"
"os"
"regexp"
"runtime"
"sort"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/perforce/p4prometheus/config"
metrics "github.com/rcowham/go-libp4dlog/metrics"
"github.com/sirupsen/logrus"
)
var (
eol = regexp.MustCompile("\r\n|\n")
logger = &logrus.Logger{Out: os.Stderr,
Formatter: &logrus.TextFormatter{TimestampFormat: "15:04:05.000", FullTimestamp: true},
// Level: logrus.DebugLevel}
Level: logrus.InfoLevel}
)
func getResult(output chan string) []string {
lines := []string{}
for line := range output {
lines = append(lines, line)
}
return lines
}
func funcName() string {
fpcs := make([]uintptr, 1)
// Skip 2 levels to get the caller
n := runtime.Callers(2, fpcs)
if n == 0 {
return ""
}
caller := runtime.FuncForPC(fpcs[0] - 1)
if caller == nil {
return ""
}
return caller.Name()
}
// Assuming there are several outputs - this returns the latest one unless historical
func getOutput(testchan chan string, historical bool) []string {
result := make([]string, 0)
lastoutput := ""
if historical {
for output := range testchan {
for _, line := range eol.Split(output, -1) {
if len(line) > 0 && !strings.HasPrefix(line, "#") {
result = append(result, line)
}
}
}
} else {
for output := range testchan {
lastoutput = output
}
for _, line := range eol.Split(lastoutput, -1) {
if len(line) > 0 && !strings.HasPrefix(line, "#") {
result = append(result, line)
}
}
}
sort.Strings(result)
return result
}
func hasPrefix(prefixes []string, line string) bool {
for _, p := range prefixes {
if strings.HasPrefix(line, p) {
return true
}
}
return false
}
func compareOutput(t *testing.T, expected, actual []string) {
nExpected := make([]string, 0)
nActual := make([]string, 0)
// Ignore these elements as the contents varies per test run
ignorePrefixes := []string{"p4_prom_cmds_pending", "p4_prom_cpu_user", "p4_prom_cpu_system",
"p4_cmd_cpu_"}
for _, line := range expected {
if !hasPrefix(ignorePrefixes, line) {
nExpected = append(nExpected, line)
}
}
for _, line := range actual {
if !hasPrefix(ignorePrefixes, line) {
nActual = append(nActual, line)
}
}
sort.Strings(nActual)
sort.Strings(nExpected)
assert.Equal(t, nExpected, nActual)
}
func basicTest(t *testing.T, cfg *config.Config, input string, historical bool) []string {
logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: "15:04:05.000", FullTimestamp: true})
logger.SetReportCaller(true)
logger.Debugf("Function: %s", funcName())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
linesChan := make(chan string, 100)
mconfig := &metrics.Config{
ServerID: "myserverid",
UpdateInterval: 10 * time.Millisecond,
OutputCmdsByUser: cfg.OutputCmdsByUser,
OutputCmdsByUserRegex: cfg.OutputCmdsByUserRegex,
OutputCmdsByIP: cfg.OutputCmdsByIP}
p4m := metrics.NewP4DMetricsLogParser(mconfig, logger, historical)
_, metricsChan := p4m.ProcessEvents(ctx, linesChan, false)
var wg sync.WaitGroup
for _, l := range eol.Split(input, -1) {
linesChan <- l
}
output := []string{}
go func() {
defer wg.Done()
time.Sleep(20 * time.Millisecond)
logger.Debugf("Waiting for metrics")
output = getOutput(metricsChan, historical)
}()
wg.Add(1)
time.Sleep(50 * time.Millisecond)
close(linesChan)
logger.Debugf("Waiting for finish")
wg.Wait()
logger.Debugf("Finished")
return output
}
func TestP4PromBasic(t *testing.T) {
cfg := &config.Config{
ServerID: "myserverid",
UpdateInterval: 10 * time.Millisecond,
OutputCmdsByUser: false,
OutputCmdsByUserRegex: "", // No match so don't output
OutputCmdsByIP: false,
}
input := `
Perforce server info:
2015/09/02 15:23:09 pid 1616 robert@robert-test 127.0.0.1 [p4/2016.2/LINUX26X86_64/1598668] 'user-sync //...'
Perforce server info:
2015/09/02 15:23:09 pid 1616 compute end .031s
Perforce server info:
2015/09/02 15:23:09 pid 1616 completed .031s
`
cmdTime, _ := time.Parse(p4timeformat, "2015/09/02 15:23:09")
historical := false
output := basicTest(t, cfg, input, historical)
baseExpected := eol.Split(`p4_cmd_counter{serverid="myserverid",cmd="user-sync"} 1
p4_cmd_cumulative_seconds{serverid="myserverid",cmd="user-sync"} 0.031
p4_cmd_program_counter{serverid="myserverid",program="p4/2016.2/LINUX26X86_64/1598668"} 1
p4_cmd_program_cumulative_seconds{serverid="myserverid",program="p4/2016.2/LINUX26X86_64/1598668"} 0.031
p4_cmd_running{serverid="myserverid"} 1
p4_prom_cmds_pending{serverid="myserverid"} 0
p4_prom_cmds_processed{serverid="myserverid"} 1
p4_prom_log_lines_read{serverid="myserverid"} 8
p4_sync_bytes_added{serverid="myserverid"} 0
p4_sync_bytes_updated{serverid="myserverid"} 0
p4_sync_files_added{serverid="myserverid"} 0
p4_sync_files_deleted{serverid="myserverid"} 0
p4_sync_files_updated{serverid="myserverid"} 0`, -1)
compareOutput(t, baseExpected, output)
historical = true
output = basicTest(t, cfg, input, historical)
// Cross check appropriate time is being produced for historical runs
assert.Contains(t, output[0], fmt.Sprintf("%d", cmdTime.Unix()))
baseExpectedHistorical := eol.Split(`p4_cmd_counter;serverid=myserverid;cmd=user-sync 1 1441207389
p4_cmd_cumulative_seconds;serverid=myserverid;cmd=user-sync 0.031 1441207389
p4_cmd_program_counter;serverid=myserverid;program=p4/2016.2/LINUX26X86_64/1598668 1 1441207389
p4_cmd_program_cumulative_seconds;serverid=myserverid;program=p4/2016.2/LINUX26X86_64/1598668 0.031 1441207389
p4_cmd_running;serverid=myserverid 1 1441207389
p4_prom_cmds_pending;serverid=myserverid 0 1441207389
p4_prom_cmds_processed;serverid=myserverid 1 1441207389
p4_prom_log_lines_read;serverid=myserverid 8 1441207389
p4_sync_bytes_added;serverid=myserverid 0 1441207389
p4_sync_bytes_updated;serverid=myserverid 0 1441207389
p4_sync_files_added;serverid=myserverid 0 1441207389
p4_sync_files_deleted;serverid=myserverid 0 1441207389
p4_sync_files_updated;serverid=myserverid 0 1441207389`, -1)
compareOutput(t, baseExpectedHistorical, output)
// Now change config and expect some extra metrics to be output
cfg = &config.Config{
ServerID: "myserverid",
UpdateInterval: 10 * time.Millisecond,
OutputCmdsByUser: true,
OutputCmdsByUserRegex: ".*", // all users
OutputCmdsByIP: true,
}
expected := eol.Split(`p4_cmd_ip_counter{serverid="myserverid",ip="127.0.0.1"} 1
p4_cmd_ip_cumulative_seconds{serverid="myserverid",ip="127.0.0.1"} 0.031
p4_cmd_user_counter{serverid="myserverid",user="robert"} 1
p4_cmd_user_cumulative_seconds{serverid="myserverid",user="robert"} 0.031
p4_cmd_user_detail_counter{serverid="myserverid",user="robert",cmd="user-sync"} 1
p4_cmd_user_detail_cumulative_seconds{serverid="myserverid",user="robert",cmd="user-sync"} 0.031`, -1)
for _, l := range baseExpected {
expected = append(expected, l)
}
sort.Strings(expected)
historical = false
output = basicTest(t, cfg, input, historical)
compareOutput(t, expected, output)
expected = eol.Split(`p4_cmd_ip_counter;serverid=myserverid;ip=127.0.0.1 1 1441207389
p4_cmd_ip_cumulative_seconds;serverid=myserverid;ip=127.0.0.1 0.031 1441207389
p4_cmd_user_counter;serverid=myserverid;user=robert 1 1441207389
p4_cmd_user_cumulative_seconds;serverid=myserverid;user=robert 0.031 1441207389
p4_cmd_user_detail_counter;serverid=myserverid;user=robert;cmd=user-sync 1 1441207389
p4_cmd_user_detail_cumulative_seconds;serverid=myserverid;user=robert;cmd=user-sync 0.031 1441207389`, -1)
for _, l := range baseExpectedHistorical {
expected = append(expected, l)
}
sort.Strings(expected)
historical = true
output = basicTest(t, cfg, input, historical)
compareOutput(t, expected, output)
}