github.com/cilium/cilium@v1.16.2/pkg/maps/srv6map/sid_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package srv6map
     5  
     6  import (
     7  	"context"
     8  	"net/netip"
     9  	"testing"
    10  
    11  	"github.com/cilium/hive"
    12  	"github.com/cilium/hive/cell"
    13  	"github.com/cilium/hive/hivetest"
    14  	"github.com/stretchr/testify/require"
    15  
    16  	"github.com/cilium/cilium/pkg/bpf"
    17  	"github.com/cilium/cilium/pkg/datapath/linux/config/defines"
    18  	"github.com/cilium/cilium/pkg/option"
    19  	"github.com/cilium/cilium/pkg/testutils"
    20  )
    21  
    22  func TestSIDMapsHive(t *testing.T) {
    23  	testutils.PrivilegedTest(t)
    24  
    25  	type in struct {
    26  		cell.In
    27  
    28  		Map              *SIDMap
    29  		NodeExtraDefines []defines.Map `group:"header-node-defines"`
    30  	}
    31  
    32  	hive := hive.New(
    33  		cell.Provide(
    34  			newSIDMap,
    35  			func() *option.DaemonConfig {
    36  				return &option.DaemonConfig{
    37  					EnableSRv6: true,
    38  				}
    39  			},
    40  		),
    41  		cell.Invoke(func(in in) {
    42  			// Test DI works
    43  			require.NotNil(t, in.Map)
    44  
    45  			merged := defines.Map{}
    46  			for _, def := range in.NodeExtraDefines {
    47  				require.NoError(t, merged.Merge(def))
    48  			}
    49  
    50  			require.Contains(t, merged, "SRV6_SID_MAP")
    51  			require.Contains(t, merged, "SRV6_SID_MAP_SIZE")
    52  
    53  			// Setup cleanup
    54  			t.Cleanup(func() {
    55  				in.Map.Unpin()
    56  			})
    57  		}),
    58  	)
    59  
    60  	logger := hivetest.Logger(t)
    61  
    62  	require.NoError(t, hive.Start(logger, context.TODO()))
    63  
    64  	// Test map creation
    65  	require.FileExists(t, bpf.MapPath(sidMapName))
    66  
    67  	// Test map iteration
    68  	k := &SIDKey{
    69  		SID: netip.MustParseAddr("fd00::1").As16(),
    70  	}
    71  
    72  	v := &SIDValue{
    73  		VRFID: 1,
    74  	}
    75  
    76  	m, err := OpenSIDMap()
    77  	require.NoError(t, err)
    78  
    79  	require.NoError(t, m.Map.Update(k, v))
    80  
    81  	var (
    82  		keys []*SIDKey
    83  		vals []*SIDValue
    84  	)
    85  	require.NoError(t, m.IterateWithCallback(func(k *SIDKey, v *SIDValue) {
    86  		keys = append(keys, k)
    87  		vals = append(vals, v)
    88  	}))
    89  
    90  	require.Contains(t, keys, k)
    91  	require.Contains(t, vals, v)
    92  
    93  	require.NoError(t, hive.Stop(logger, context.TODO()))
    94  
    95  	// Map should be pinned even after stopping the hive
    96  	require.FileExists(t, bpf.MapPath(sidMapName))
    97  }