forked from linkerd/linkerd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtap.go
89 lines (79 loc) · 2.06 KB
/
tap.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
package testutil
import (
"fmt"
"strings"
"time"
)
// TapEvent represents a tap event
type TapEvent struct {
Method string
Authority string
Path string
HTTPStatus string
GrpcStatus string
TLS string
LineCount int
}
// Tap executes a tap command and converts the command's streaming output into tap
// events using each line's "id" field
func Tap(target string, h *TestHelper, arg ...string) ([]*TapEvent, error) {
cmd := append([]string{"viz", "tap", target}, arg...)
outputStream, err := h.LinkerdRunStream(cmd...)
if err != nil {
return nil, err
}
defer outputStream.Stop()
outputLines, err := outputStream.ReadUntil(10, 1*time.Minute)
if err != nil {
return nil, err
}
tapEventByID := make(map[string]*TapEvent)
for _, line := range outputLines {
fields := toFieldMap(line)
obj, ok := tapEventByID[fields["id"]]
if !ok {
obj = &TapEvent{}
tapEventByID[fields["id"]] = obj
}
obj.LineCount++
obj.TLS = fields["tls"]
switch fields["type"] {
case "req":
obj.Method = fields[":method"]
obj.Authority = fields[":authority"]
obj.Path = fields[":path"]
case "rsp":
obj.HTTPStatus = fields[":status"]
case "end":
obj.GrpcStatus = fields["grpc-status"]
}
}
output := make([]*TapEvent, 0)
for _, obj := range tapEventByID {
if obj.LineCount == 3 { // filter out incomplete events
output = append(output, obj)
}
}
return output, nil
}
func toFieldMap(line string) map[string]string {
fields := strings.Fields(line)
fieldMap := map[string]string{"type": fields[0]}
for _, field := range fields[1:] {
parts := strings.SplitN(field, "=", 2)
fieldMap[parts[0]] = parts[1]
}
return fieldMap
}
// ValidateExpected compares the received tap event with the expected tap event
func ValidateExpected(events []*TapEvent, expectedEvent TapEvent) error {
if len(events) == 0 {
return fmt.Errorf("Expected tap events, got nothing")
}
for _, event := range events {
if *event != expectedEvent {
return fmt.Errorf("Unexpected tap event [%+v]; expected=[%+v]", *event, expectedEvent)
}
}
return nil
}