From 4cc55bf3eba9f2ebfb97e4a5b9fe15d19e653061 Mon Sep 17 00:00:00 2001 From: JoukoVirtanen Date: Wed, 13 Nov 2024 19:05:20 -0800 Subject: [PATCH] Always sort the connections when comparing --- .../pkg/mock_sensor/expect_conn.go | 39 ++++++------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/integration-tests/pkg/mock_sensor/expect_conn.go b/integration-tests/pkg/mock_sensor/expect_conn.go index c9ad8be520..95fd111344 100644 --- a/integration-tests/pkg/mock_sensor/expect_conn.go +++ b/integration-tests/pkg/mock_sensor/expect_conn.go @@ -96,14 +96,12 @@ 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") @@ -111,16 +109,13 @@ func (s *MockSensor) getConnectionsAndCompare(t *testing.T, containerID string, 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 } @@ -130,12 +125,12 @@ 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 } @@ -143,18 +138,6 @@ func (s *MockSensor) CompareConnections(t *testing.T, containerID string, timeou } } -// 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