github.com/cilium/cilium@v1.16.2/pkg/datapath/garp/garp_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  //go:build linux
     5  
     6  package garp
     7  
     8  import (
     9  	"errors"
    10  	"net/netip"
    11  	"testing"
    12  
    13  	"github.com/cilium/hive/cell"
    14  	"github.com/cilium/hive/hivetest"
    15  	"github.com/mdlayher/arp"
    16  	"github.com/stretchr/testify/require"
    17  
    18  	"github.com/cilium/cilium/pkg/hive"
    19  	"github.com/cilium/cilium/pkg/testutils"
    20  )
    21  
    22  func TestGARPCell(t *testing.T) {
    23  	testutils.PrivilegedTest(t)
    24  
    25  	testIfaceName := "lo"
    26  	testGARPCell := func(garpSender Sender) error {
    27  		s, _ := garpSender.(*sender)
    28  		require.NotNil(t, s)
    29  		require.Equal(t, testIfaceName, s.iface.Name)
    30  
    31  		t.Logf("iface: %+v", s.iface)
    32  
    33  		// Here we just want to make sure that the Send method works,
    34  		// not that the gratuitous arp actually appears as expected. To
    35  		// do this, we can try to Send on loopback, and just check to
    36  		// see if we get the correct error from the underlying arp
    37  		// package.
    38  		err := garpSender.Send(netip.MustParseAddr("1.2.3.4"))
    39  		if err != nil && errors.Is(err, arp.ErrInvalidHardwareAddr) {
    40  			// We got the error we expected.
    41  			return nil
    42  		}
    43  
    44  		t.Fatal(err)
    45  		return nil
    46  	}
    47  
    48  	h := hive.New(cell.Module(
    49  		"test-garp-cell",
    50  		"TestGARPCell",
    51  
    52  		cell.Config(Config{}),
    53  		cell.Provide(newGARPSender),
    54  		cell.Invoke(testGARPCell),
    55  	))
    56  	hive.AddConfigOverride(h, func(cfg *Config) { cfg.L2PodAnnouncementsInterface = testIfaceName })
    57  
    58  	if err := h.Populate(hivetest.Logger(t)); err != nil {
    59  		t.Fatalf("Failed to populate: %s", err)
    60  	}
    61  }