forked from postmanlabs/observability-cli
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_test.go
153 lines (145 loc) · 3.41 KB
/
stats_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
package trace
import (
"testing"
. "github.com/akitasoftware/akita-libs/client_telemetry"
"github.com/akitasoftware/go-utils/optionals"
"github.com/stretchr/testify/assert"
)
func TestCountByPort(t *testing.T) {
// Make enough ports to exceed the limit.
limitPlus1Inputs := make([]PacketCounts, maxKeys+1)
limitPlus1Expected := make(map[int]*PacketCounts, maxKeys)
for i := range limitPlus1Inputs {
limitPlus1Inputs[i] = PacketCounts{
Interface: "*",
SrcPort: i,
DstPort: i,
TCPPackets: 1,
}
if i < maxKeys {
limitPlus1Expected[i] = &PacketCounts{
Interface: "*",
SrcPort: i,
TCPPackets: 2,
}
}
}
tests := []struct {
name string
input []PacketCounts
limit int
expected map[int]*PacketCounts
expectedOverflow optionals.Optional[PacketCounts]
}{
{
name: "init",
input: []PacketCounts{{
Interface: "lo0",
SrcPort: 1,
DstPort: 2,
TCPPackets: 3,
}},
limit: 100,
expected: map[int]*PacketCounts{
1: {
Interface: "*",
SrcPort: 1,
TCPPackets: 3,
},
2: {
Interface: "*",
SrcPort: 2,
TCPPackets: 3,
},
},
},
{
name: "limit + 1",
input: limitPlus1Inputs,
expected: limitPlus1Expected,
expectedOverflow: optionals.Some(PacketCounts{
Interface: "*",
TCPPackets: 2,
}),
},
}
for _, tc := range tests {
c := NewPacketCounter()
for _, counts := range tc.input {
c.Update(counts)
}
// Set a summary above the limit to ensure we get all the ports.
assert.Equal(t, tc.expected, c.byPort.RawMap(), "["+tc.name+"] raw map")
assert.Equal(t, tc.expectedOverflow, c.byPort.GetOverflow(), "["+tc.name+"] overflow")
}
}
func TestTopNTCP(t *testing.T) {
tests := []struct {
name string
take int
from map[int]*PacketCounts
expected map[int]*PacketCounts
}{
{
name: "empty",
take: 5,
from: map[int]*PacketCounts{},
expected: map[int]*PacketCounts{},
},
{
name: "take nothing",
take: 0,
from: map[int]*PacketCounts{80: &PacketCounts{}},
expected: map[int]*PacketCounts{},
},
{
name: "take one from many",
take: 1,
from: map[int]*PacketCounts{
1: &PacketCounts{TCPPackets: 1},
2: &PacketCounts{TCPPackets: 2},
3: &PacketCounts{TCPPackets: 3},
},
expected: map[int]*PacketCounts{
3: &PacketCounts{TCPPackets: 3},
},
},
{
name: "take two from many",
take: 2,
from: map[int]*PacketCounts{
6: &PacketCounts{TCPPackets: 1},
5: &PacketCounts{TCPPackets: 2},
4: &PacketCounts{TCPPackets: 3},
},
expected: map[int]*PacketCounts{
5: &PacketCounts{TCPPackets: 2},
4: &PacketCounts{TCPPackets: 3},
},
},
{
name: "take two from one",
take: 2,
from: map[int]*PacketCounts{80: &PacketCounts{}},
expected: map[int]*PacketCounts{80: &PacketCounts{}},
},
{
name: "take one from many, all equal",
take: 1,
from: map[int]*PacketCounts{
1: &PacketCounts{TCPPackets: 1},
2: &PacketCounts{TCPPackets: 1},
3: &PacketCounts{TCPPackets: 1},
},
expected: map[int]*PacketCounts{1: &PacketCounts{TCPPackets: 1}},
},
}
for _, tc := range tests {
bc := &BoundedPacketCounter[int]{
limit: 100,
m: tc.from,
}
actual, _ := bc.TopN(tc.take, func(c *PacketCounts) int { return c.TCPPackets })
assert.Equal(t, tc.expected, actual, tc.name)
}
}