Skip to content

Commit abacfb7

Browse files
TestRepeatedNetworkFlow checks that number of active connections is in expected range (#2642)
1 parent 1e3b4f9 commit abacfb7

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

integration-tests/integration_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func TestMissingProcScrape(t *testing.T) {
4343
}
4444
}
4545

46+
// TODO (ROX-31753): Add tests with procfs scrape only and tests with syscalls only.
47+
// The reason for this is that currently there might be situations in which we have
48+
// race conditions between procfs scrape and syscalls. It would be good to confirm
49+
// that each method is working well independently.
4650
func TestRepeatedNetworkFlow(t *testing.T) {
4751
// Perform 11 curl commands with a 2 second sleep between each curl command.
4852
// The scrapeInterval is increased to 4 seconds to reduce the chance that jiter will effect the results.
@@ -58,7 +62,8 @@ func TestRepeatedNetworkFlow(t *testing.T) {
5862
NumIter: 11,
5963
SleepBetweenCurlTime: 2,
6064
SleepBetweenIterations: 1,
61-
ExpectedActive: 1,
65+
ExpectedMinActive: 1,
66+
ExpectedMaxActive: 1,
6267
ExpectedInactive: 1,
6368
}
6469
suite.Run(t, repeatedNetworkFlowTestSuite)
@@ -75,8 +80,16 @@ func TestRepeatedNetworkFlowWithZeroAfterglowPeriod(t *testing.T) {
7580
NumIter: 3,
7681
SleepBetweenCurlTime: 3,
7782
SleepBetweenIterations: 1,
78-
ExpectedActive: 0,
79-
ExpectedInactive: 3,
83+
// It is possible that at the scrap interval the connection will be active.
84+
// If the connections is active at t=0 and that coinsides with a scrape interval.
85+
// The next connection will be at t=3 and the next scrape is at t=2.
86+
// The next connection after that will be at t=6. That also coisides with a
87+
// scrape interval. That is a second opportunity for the connection to be active
88+
// during the scrape inteval. Therefore it is possible that between 0 and 2 active
89+
// connections will be seen by the test.
90+
ExpectedMinActive: 0,
91+
ExpectedMaxActive: 2,
92+
ExpectedInactive: 3,
8093
}
8194
suite.Run(t, repeatedNetworkFlowTestSuite)
8295
}
@@ -91,8 +104,10 @@ func TestRepeatedNetworkFlowThreeCurlsNoAfterglow(t *testing.T) {
91104
NumIter: 3,
92105
SleepBetweenCurlTime: 6,
93106
SleepBetweenIterations: 1,
94-
ExpectedActive: 0,
95-
ExpectedInactive: 3,
107+
// Similar analysis as for the above test.
108+
ExpectedMinActive: 0,
109+
ExpectedMaxActive: 2,
110+
ExpectedInactive: 3,
96111
}
97112
suite.Run(t, repeatedNetworkFlowTestSuite)
98113
}

integration-tests/suites/repeated_network_flow.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ type RepeatedNetworkFlowTestSuite struct {
3131
NumIter int
3232
SleepBetweenCurlTime int
3333
SleepBetweenIterations int
34-
ExpectedActive int // number of active connections expected
35-
ExpectedInactive int // number of inactive connections expected
34+
// Due to connections having finite lifetimes it is possible to catch the connection
35+
// during the short time that it is active, thus we cannot in all cases be certain
36+
// of the number of active connections that are found, but we can be certain that the
37+
// number of active connections will be in a certain range.
38+
ExpectedMinActive int // minimum number of active connections expected
39+
ExpectedMaxActive int // maximum number of active connections expected
40+
ExpectedInactive int // number of inactive connections expected
3641
}
3742

3843
// Launches collector
@@ -111,7 +116,7 @@ func (s *RepeatedNetworkFlowTestSuite) TearDownSuite() {
111116
}
112117

113118
func (s *RepeatedNetworkFlowTestSuite) TestRepeatedNetworkFlow() {
114-
networkConnections := s.Sensor().ExpectConnectionsN(s.T(), s.ServerContainer, 10*time.Second, s.ExpectedActive+s.ExpectedInactive)
119+
networkConnections := s.Sensor().Connections(s.ServerContainer)
115120

116121
observedActive := 0
117122
observedInactive := 0
@@ -124,7 +129,9 @@ func (s *RepeatedNetworkFlowTestSuite) TestRepeatedNetworkFlow() {
124129
}
125130
}
126131

127-
assert.Equal(s.T(), s.ExpectedActive, observedActive, "Unexpected number of active connections reported")
132+
//assert.Equal(s.T(), s.ExpectedActive, observedActive, "Unexpected number of active connections reported")
133+
assert.True(s.T(), s.ExpectedMinActive <= observedActive, "Too few active connections reported")
134+
assert.True(s.T(), s.ExpectedMaxActive >= observedActive, "Too many active connections reported")
128135
assert.Equal(s.T(), s.ExpectedInactive, observedInactive, "Unexpected number of inactive connections reported")
129136

130137
// Server side checks

0 commit comments

Comments
 (0)