-
Notifications
You must be signed in to change notification settings - Fork 51
/
test_utils.go
112 lines (85 loc) · 2.26 KB
/
test_utils.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
// +build !windows
package nfqdatapath
import (
"encoding/json"
"fmt"
"github.com/golang/mock/gomock"
"go.aporeto.io/trireme-lib/collector"
"go.aporeto.io/trireme-lib/controller/pkg/pucontext"
"go.aporeto.io/trireme-lib/controller/pkg/secrets"
)
type myMatcher struct {
x interface{}
}
func (m *myMatcher) Matches(x interface{}) bool {
f1 := m.x.(*collector.FlowRecord)
f2 := x.(*collector.FlowRecord)
return f1.Destination.IP == f2.Destination.IP &&
f1.Source.IP == f2.Source.IP &&
f1.Destination.Port == f2.Destination.Port &&
f1.Action == f2.Action &&
f1.Count == f2.Count &&
f1.DropReason == f2.DropReason
}
func (m *myMatcher) String() string {
out, err := json.Marshal(m.x)
if err != nil {
panic(err)
}
return fmt.Sprintf("is equal to %v", string(out))
}
type myGotFormatter struct{}
func (g *myGotFormatter) Got(got interface{}) string {
out, err := json.Marshal(got)
if err != nil {
panic(err)
}
return string(out)
}
// MyMatcher returns gomock matcher
func MyMatcher(x interface{}) gomock.Matcher {
return gomock.GotFormatterAdapter(&myGotFormatter{}, &myMatcher{x: x})
}
type packetEventMatcher struct {
x interface{}
}
func (p *packetEventMatcher) Matches(x interface{}) bool {
f1 := p.x.(*collector.PacketReport)
f2 := x.(*collector.PacketReport)
return f1.DestinationIP == f2.DestinationIP
}
func (p *packetEventMatcher) String() string {
return fmt.Sprintf("is equal to %v", p.x)
}
// PacketEventMatcher return gomock matcher
func PacketEventMatcher(x interface{}) gomock.Matcher {
return &packetEventMatcher{x: x}
}
type myCounterMatcher struct {
x interface{}
}
func (m *myCounterMatcher) Matches(x interface{}) bool {
f1 := m.x.(*collector.CounterReport)
f2 := x.(*collector.CounterReport)
if f2.Namespace != "/ns1" {
return true
}
return f1.ContextID == f2.ContextID && f1.Counters[pucontext.ErrNonPUTraffic].Value == 0
}
func (m *myCounterMatcher) String() string {
return fmt.Sprintf("is equal to %v", m.x)
}
// MyCounterMatcher custom matcher for counter record
func MyCounterMatcher(x interface{}) gomock.Matcher {
return &myCounterMatcher{x: x}
}
type fakeSecrets struct {
id string
*secrets.NullPKI
}
func (f *fakeSecrets) setID(id string) {
f.id = id
}
func (f *fakeSecrets) getID() string {
return f.id
}