-
Notifications
You must be signed in to change notification settings - Fork 23
/
repeated_network_flow.go
144 lines (114 loc) · 4.6 KB
/
repeated_network_flow.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
package suites
import (
"fmt"
"strconv"
"time"
"github.com/stackrox/collector/integration-tests/pkg/collector"
"github.com/stackrox/collector/integration-tests/pkg/config"
"github.com/stretchr/testify/assert"
)
type RepeatedNetworkFlowTestSuite struct {
//The goal with these integration tests is to make sure we report the correct number of
//networking events. Sometimes if a connection is made multiple times within a short time
//called an "afterglow" period, we only want to report the connection once.
IntegrationTestSuiteBase
ClientContainer string
ClientIP string
ServerContainer string
ServerIP string
ServerPort string
EnableAfterglow bool
AfterglowPeriod int
ScrapeInterval int
NumMetaIter int
NumIter int
SleepBetweenCurlTime int
SleepBetweenIterations int
ExpectedActive int // number of active connections expected
ExpectedInactive int // number of inactive connections expected
}
// Launches collector
// Launches gRPC server in insecure mode
// Launches nginx container
func (s *RepeatedNetworkFlowTestSuite) SetupSuite() {
s.RegisterCleanup("nginx", "nginx-curl")
s.StartContainerStats()
collectorOptions := collector.StartupOptions{
Config: map[string]any{
// turnOffScrape will be true, but the scrapeInterval
// also controls the reporting interval for network events
"scrapeInterval": s.ScrapeInterval,
},
Env: map[string]string{
"ROX_AFTERGLOW_PERIOD": strconv.Itoa(s.AfterglowPeriod),
"ROX_ENABLE_AFTERGLOW": strconv.FormatBool(s.EnableAfterglow),
},
}
s.StartCollector(false, &collectorOptions)
image_store := config.Images()
scheduled_curls_image := image_store.QaImageByKey("qa-schedule-curls")
images := []string{
image_store.ImageByKey("nginx"),
scheduled_curls_image,
}
for _, image := range images {
err := s.executor.PullImage(image)
s.Require().NoError(err)
}
time.Sleep(10 * time.Second)
// invokes default nginx
containerID, err := s.launchContainer("nginx", image_store.ImageByKey("nginx"))
s.Require().NoError(err)
s.ServerContainer = containerID[0:12]
// invokes another container
containerID, err = s.launchContainer("nginx-curl", scheduled_curls_image, "sleep", "300")
s.Require().NoError(err)
s.ClientContainer = containerID[0:12]
s.ServerIP, err = s.getIPAddress("nginx")
s.Require().NoError(err)
s.ServerPort, err = s.getPort("nginx")
s.Require().NoError(err)
time.Sleep(30 * time.Second)
serverAddress := fmt.Sprintf("%s:%s", s.ServerIP, s.ServerPort)
numMetaIter := strconv.Itoa(s.NumMetaIter)
numIter := strconv.Itoa(s.NumIter)
sleepBetweenCurlTime := strconv.Itoa(s.SleepBetweenCurlTime)
sleepBetweenIterations := strconv.Itoa(s.SleepBetweenIterations)
_, err = s.execContainer("nginx-curl", []string{"/usr/bin/schedule-curls.sh", numMetaIter, numIter, sleepBetweenCurlTime, sleepBetweenIterations, serverAddress})
s.ClientIP, err = s.getIPAddress("nginx-curl")
s.Require().NoError(err)
totalTime := (s.SleepBetweenCurlTime*s.NumIter+s.SleepBetweenIterations)*s.NumMetaIter + s.AfterglowPeriod + 10
time.Sleep(time.Duration(totalTime) * time.Second)
}
func (s *RepeatedNetworkFlowTestSuite) TearDownSuite() {
s.StopCollector()
s.cleanupContainers("nginx", "nginx-curl")
s.WritePerfResults()
}
func (s *RepeatedNetworkFlowTestSuite) TestRepeatedNetworkFlow() {
networkInfos := s.Sensor().ExpectConnectionsN(s.T(), s.ServerContainer, 10*time.Second, s.ExpectedActive+s.ExpectedInactive)
observedActive := 0
observedInactive := 0
for _, info := range networkInfos {
if info.IsActive() {
observedActive++
} else {
observedInactive++
}
}
assert.Equal(s.T(), s.ExpectedActive, observedActive, "Unexpected number of active connections reported")
assert.Equal(s.T(), s.ExpectedInactive, observedInactive, "Unexpected number of inactive connections reported")
// Server side checks
actualServerEndpoint := networkInfos[0].LocalAddress
actualClientEndpoint := networkInfos[0].RemoteAddress
// From server perspective, network connection info only has local port and remote IP
assert.Equal(s.T(), fmt.Sprintf(":%s", s.ServerPort), actualServerEndpoint)
assert.Equal(s.T(), s.ClientIP, actualClientEndpoint)
// client side checks
// NetworkSignalHandler does not currently report endpoints.
// See the comment above for the server container endpoint test for more info.
assert.Equal(s.T(), 0, len(s.Sensor().Endpoints(s.ClientContainer)))
networkInfos = s.Sensor().Connections(s.ClientContainer)
actualClientEndpoint = networkInfos[0].LocalAddress
actualServerEndpoint = networkInfos[0].RemoteAddress
}