Skip to content

Commit

Permalink
Always sort the connections when comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
JoukoVirtanen committed Nov 14, 2024
1 parent e99a414 commit 4cc55bf
Showing 1 changed file with 11 additions and 28 deletions.
39 changes: 11 additions & 28 deletions integration-tests/pkg/mock_sensor/expect_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,26 @@ func (s *MockSensor) checkIfConnectionsMatchExpected(t *testing.T, connections [
return false
}

// getConnectionsAndCompare gets the connections for a container, sorts them if order is set to true, and compares with a set of expected
// getConnectionsAndCompare gets the connections for a container, sorts them, 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 {
func (s *MockSensor) getConnectionsAndCompare(t *testing.T, containerID string, assertMismatch bool, expected ...types.NetworkInfo) bool {
connections := s.Connections(containerID)
if order {
types.SortConnections(connections)
}
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)
}
// ExpectSameElementsConnections 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 the expected and observed connections must match, but the order
// does not matter.
func (s *MockSensor) ExpectSameElementsConnections(t *testing.T, containerID string, timeout time.Duration, expected ...types.NetworkInfo) bool {
types.SortConnections(expected)

success := s.getConnectionsAndCompare(t, containerID, order, false, expected...)
success := s.getConnectionsAndCompare(t, containerID, false, expected...)
if success {
return true
}
Expand All @@ -130,31 +125,19 @@ func (s *MockSensor) CompareConnections(t *testing.T, containerID string, timeou
for {
select {
case <-timer:
return s.getConnectionsAndCompare(t, containerID, order, true, expected...)
return s.getConnectionsAndCompare(t, containerID, true, expected...)
case conn := <-s.LiveConnections():
if conn.GetContainerId() != containerID {
continue
}
success := s.getConnectionsAndCompare(t, containerID, order, false, expected...)
success := s.getConnectionsAndCompare(t, containerID, 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
Expand Down

0 comments on commit 4cc55bf

Please sign in to comment.