@@ -2368,15 +2368,21 @@ var failedConnectionsBuildModes = map[ebpftest.BuildMode]struct{}{
2368
2368
ebpftest .RuntimeCompiled : {},
2369
2369
}
2370
2370
2371
- func (s * TracerSuite ) TestTCPFailureConnectionTimeout () {
2372
- t := s .T ()
2371
+ func checkSkipFailureConnectionsTests (t * testing.T ) {
2373
2372
if _ , ok := failedConnectionsBuildModes [ebpftest .GetBuildMode ()]; ! ok {
2374
2373
t .Skip ("Skipping test on unsupported build mode: " , ebpftest .GetBuildMode ())
2375
2374
}
2375
+
2376
+ }
2377
+ func (s * TracerSuite ) TestTCPFailureConnectionTimeout () {
2378
+ t := s .T ()
2379
+
2380
+ checkSkipFailureConnectionsTests (t )
2376
2381
// TODO: remove this check when we fix this test on kernels < 4.19
2377
2382
if kv < kernel .VersionCode (4 , 19 , 0 ) {
2378
2383
t .Skip ("Skipping test on kernels < 4.19" )
2379
2384
}
2385
+
2380
2386
setupDropTrafficRule (t )
2381
2387
cfg := testConfig ()
2382
2388
cfg .TCPFailedConnectionsEnabled = true
@@ -2427,109 +2433,6 @@ func (s *TracerSuite) TestTCPFailureConnectionTimeout() {
2427
2433
}, 3 * time .Second , 1000 * time .Millisecond , "Failed connection not recorded properly" )
2428
2434
}
2429
2435
2430
- func (s * TracerSuite ) TestTCPFailureConnectionRefused () {
2431
- t := s .T ()
2432
- if _ , ok := failedConnectionsBuildModes [ebpftest .GetBuildMode ()]; ! ok {
2433
- t .Skip ("Skipping test on unsupported build mode: " , ebpftest .GetBuildMode ())
2434
- }
2435
- cfg := testConfig ()
2436
- cfg .TCPFailedConnectionsEnabled = true
2437
- tr := setupTracer (t , cfg )
2438
-
2439
- // try to connect to a port where no server is accepting connections
2440
- srvAddr := "127.0.0.1:9998"
2441
- conn , err := net .Dial ("tcp" , srvAddr )
2442
- if err == nil {
2443
- conn .Close () // If the connection unexpectedly succeeds, close it immediately.
2444
- require .Fail (t , "expected connection to be refused, but it succeeded" )
2445
- }
2446
- require .Error (t , err , "expected connection refused error but got none" )
2447
-
2448
- // Check if the connection was recorded as refused
2449
- require .Eventually (t , func () bool {
2450
- conns := getConnections (t , tr )
2451
- // Check for the refusal record
2452
- return findFailedConnectionByRemoteAddr (srvAddr , conns , 111 )
2453
- }, 3 * time .Second , 100 * time .Millisecond , "Failed connection not recorded properly" )
2454
- }
2455
-
2456
- func (s * TracerSuite ) TestTCPFailureConnectionReset () {
2457
- t := s .T ()
2458
- if _ , ok := failedConnectionsBuildModes [ebpftest .GetBuildMode ()]; ! ok {
2459
- t .Skip ("Skipping test on unsupported build mode: " , ebpftest .GetBuildMode ())
2460
- }
2461
- cfg := testConfig ()
2462
- cfg .TCPFailedConnectionsEnabled = true
2463
- tr := setupTracer (t , cfg )
2464
-
2465
- srv := NewTCPServer (func (c net.Conn ) {
2466
- if tcpConn , ok := c .(* net.TCPConn ); ok {
2467
- tcpConn .SetLinger (0 )
2468
- buf := make ([]byte , 10 )
2469
- _ , _ = c .Read (buf )
2470
- time .Sleep (10 * time .Millisecond )
2471
- }
2472
- c .Close ()
2473
- })
2474
-
2475
- require .NoError (t , srv .Run (), "error running server" )
2476
- t .Cleanup (srv .Shutdown )
2477
-
2478
- serverAddr := srv .ln .Addr ()
2479
- c , err := net .Dial ("tcp" , serverAddr .String ())
2480
- require .NoError (t , err , "could not connect to server: " , err )
2481
-
2482
- // Write to the server and expect a reset
2483
- _ , writeErr := c .Write ([]byte ("ping" ))
2484
- if writeErr != nil {
2485
- t .Log ("Write error:" , writeErr )
2486
- }
2487
-
2488
- // Read from server to ensure that the server has a chance to reset the connection
2489
- _ , readErr := c .Read (make ([]byte , 4 ))
2490
- require .Error (t , readErr , "expected connection reset error but got none" )
2491
-
2492
- // Check if the connection was recorded as reset
2493
- require .Eventually (t , func () bool {
2494
- conns := getConnections (t , tr )
2495
- // 104 is the errno for ECONNRESET
2496
- return findFailedConnection (t , c .LocalAddr ().String (), serverAddr .String (), conns , 104 )
2497
- }, 3 * time .Second , 100 * time .Millisecond , "Failed connection not recorded properly" )
2498
-
2499
- require .NoError (t , c .Close (), "error closing client connection" )
2500
- }
2501
-
2502
- // findFailedConnection is a utility function to find a failed connection based on specific TCP error codes
2503
- func findFailedConnection (t * testing.T , local , remote string , conns * network.Connections , errorCode uint32 ) bool {
2504
- // Extract the address and port from the net.Addr types
2505
- localAddrPort , err := netip .ParseAddrPort (local )
2506
- if err != nil {
2507
- t .Logf ("Failed to parse local address: %v" , err )
2508
- return false
2509
- }
2510
- remoteAddrPort , err := netip .ParseAddrPort (remote )
2511
- if err != nil {
2512
- t .Logf ("Failed to parse remote address: %v" , err )
2513
- return false
2514
- }
2515
-
2516
- failureFilter := func (cs network.ConnectionStats ) bool {
2517
- localMatch := netip .AddrPortFrom (cs .Source .Addr , cs .SPort ) == localAddrPort
2518
- remoteMatch := netip .AddrPortFrom (cs .Dest .Addr , cs .DPort ) == remoteAddrPort
2519
- return localMatch && remoteMatch && cs .TCPFailures [errorCode ] > 0
2520
- }
2521
-
2522
- return network .FirstConnection (conns , failureFilter ) != nil
2523
- }
2524
-
2525
- // for some failed connections we don't know the local addr/port so we need to search by remote addr only
2526
- func findFailedConnectionByRemoteAddr (remoteAddr string , conns * network.Connections , errorCode uint32 ) bool {
2527
- failureFilter := func (cs network.ConnectionStats ) bool {
2528
- return netip .MustParseAddrPort (remoteAddr ) == netip .AddrPortFrom (cs .Dest .Addr , cs .DPort ) && cs .TCPFailures [errorCode ] > 0
2529
- }
2530
- return network .FirstConnection (conns , failureFilter ) != nil
2531
- }
2532
-
2533
2436
func setupDropTrafficRule (tb testing.TB ) (ns string ) {
2534
2437
state := testutil .IptablesSave (tb )
2535
2438
tb .Cleanup (func () {
0 commit comments