-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ROX-26726: Collector integration tests should record all connections #1902
base: master
Are you sure you want to change the base?
Changes from all commits
82c1b44
585d648
eca4ddb
03c775e
d1a1847
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ loop: | |
case <-timer: | ||
// we know they don't match at this point, but by using | ||
// ElementsMatch we get much better logging about the differences | ||
return assert.ElementsMatch(t, expected, s.Connections(containerID), "timed out waiting for networks") | ||
return assert.ElementsMatch(t, expected, s.Connections(containerID), "timed out waiting for network connections") | ||
case network := <-s.LiveConnections(): | ||
if network.GetContainerId() != containerID { | ||
continue loop | ||
|
@@ -72,14 +72,90 @@ loop: | |
if conn.GetContainerId() != containerID { | ||
continue loop | ||
} | ||
|
||
if len(s.Connections(containerID)) == n { | ||
return s.Connections(containerID) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// checkIfConnectionsMatchExpected compares a list of expected and observed connection match exactly. | ||
func (s *MockSensor) checkIfConnectionsMatchExpected(t *testing.T, connections []types.NetworkInfo, expected []types.NetworkInfo) bool { | ||
if len(connections) > len(expected) { | ||
return assert.ElementsMatch(t, expected, connections, "networking connections do not match") | ||
} | ||
|
||
if len(connections) == len(expected) { | ||
types.SortConnections(connections) | ||
for i := range expected { | ||
if !expected[i].Equal(connections[i]) { | ||
return assert.ElementsMatch(t, expected, connections, "networking connections do not match") | ||
} | ||
} | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// getConnectionsAndCompare gets the connections for a container, sorts them if order is set to true, and compares with a set of expected | ||
// connections. If asssertMismatch is true and the set of observed connections does not match the set of expected connections an assert is | ||
// triggered. If assertMismatch is not set then just return if the observed and expected connections match. | ||
func (s *MockSensor) getConnectionsAndCompare(t *testing.T, containerID string, order bool, assertMismatch bool, expected ...types.NetworkInfo) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, why do we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On a separate note about |
||
connections := s.Connections(containerID) | ||
if order { | ||
types.SortConnections(connections) | ||
} | ||
success := s.checkIfConnectionsMatchExpected(t, connections, expected) | ||
if assertMismatch && !success { | ||
return assert.ElementsMatch(t, expected, connections, "networking connections do not match") | ||
} | ||
return success | ||
} | ||
|
||
// CompareConnections compares a list of expected connections to the observed connections. This comparison is done at the beginning, when a new | ||
// connection arrives, and after a timeout period. The number of connections must match and it can be specified if the order of the connections | ||
// must match or not. The difference between this function and ExpectConnections is that ExpectConnections tolerates extra observed connections | ||
// that are not expected. | ||
func (s *MockSensor) CompareConnections(t *testing.T, containerID string, timeout time.Duration, order bool, expected ...types.NetworkInfo) bool { | ||
if order { | ||
types.SortConnections(expected) | ||
} | ||
|
||
success := s.getConnectionsAndCompare(t, containerID, order, false, expected...) | ||
if success { | ||
return true | ||
} | ||
|
||
timer := time.After(timeout) | ||
|
||
for { | ||
select { | ||
case <-timer: | ||
return s.getConnectionsAndCompare(t, containerID, order, true, expected...) | ||
case conn := <-s.LiveConnections(): | ||
if conn.GetContainerId() != containerID { | ||
continue | ||
} | ||
success := s.getConnectionsAndCompare(t, containerID, order, false, expected...) | ||
if success { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
|
||
// ExpectExactConnections requires that within a timeout period the networking connections involving containerID match a list of expected | ||
// netwoking connections. | ||
func (s *MockSensor) ExpectExactConnections(t *testing.T, containerID string, timeout time.Duration, expected ...types.NetworkInfo) bool { | ||
return s.CompareConnections(t, containerID, timeout, false, expected...) | ||
} | ||
|
||
// ExpectSameElementsConnections requires that within a timeout period the networking connections involving containerID match a list of expected | ||
// netwoking connections, but the order of those connections do not have to match. | ||
func (s *MockSensor) ExpectSameElementsConnections(t *testing.T, containerID string, timeout time.Duration, expected ...types.NetworkInfo) bool { | ||
return s.CompareConnections(t, containerID, timeout, true, expected...) | ||
} | ||
|
||
// ExpectEndpoints waits up to the timeout for the gRPC server to receive | ||
// the list of expected Endpoints. It will first check to see if the endpoints | ||
// have been received already, and then monitor the live feed of endpoints | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this whole method the same as just calling
assert.ElementsMatch(t, expected, connections, "networking connections do not match")
? From the testify docs:https://pkg.go.dev/github.com/stretchr/testify/assert#ElementsMatch