github.com/cilium/cilium@v1.16.2/pkg/datapath/loader/util_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package loader
     5  
     6  import (
     7  	"fmt"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/spf13/afero"
    12  
    13  	"github.com/cilium/cilium/pkg/cidr"
    14  	"github.com/cilium/cilium/pkg/datapath/linux/sysctl"
    15  	datapath "github.com/cilium/cilium/pkg/datapath/types"
    16  	"github.com/cilium/cilium/pkg/maps/callsmap"
    17  	"github.com/cilium/cilium/pkg/maps/policymap"
    18  	"github.com/cilium/cilium/pkg/option"
    19  )
    20  
    21  var (
    22  	localNodeConfig = datapath.LocalNodeConfiguration{
    23  		NodeIPv4:           templateIPv4[:],
    24  		CiliumInternalIPv4: templateIPv4[:],
    25  		AllocCIDRIPv4:      cidr.MustParseCIDR("10.147.0.0/16"),
    26  		LoopbackIPv4:       templateIPv4[:],
    27  		HostEndpointID:     1,
    28  		EnableIPv4:         true,
    29  	}
    30  )
    31  
    32  func setupCompilationDirectories(tb testing.TB) {
    33  	option.Config.DryMode = true
    34  	option.Config.BpfDir = bpfDir
    35  	option.Config.StateDir = bpfDir
    36  	testIncludes = []string{
    37  		// Unit tests rely on using bpf/ep_config.h instead of
    38  		// the real per endpoint config. Otherwise you get compilation
    39  		// errors due to redefined macros and such. *sigh*
    40  		fmt.Sprintf("-I%s", bpfDir),
    41  		fmt.Sprintf("-I%s", filepath.Join(bpfDir, "include")),
    42  	}
    43  
    44  	oldElfMapPrefixes := elfMapPrefixes
    45  	elfMapPrefixes = []string{
    46  		fmt.Sprintf("test_%s", policymap.MapName),
    47  		fmt.Sprintf("test_%s", callsmap.MapName),
    48  	}
    49  
    50  	tb.Cleanup(func() {
    51  		option.Config.DryMode = false
    52  		option.Config.BpfDir = ""
    53  		option.Config.StateDir = ""
    54  		testIncludes = nil
    55  		elfMapPrefixes = oldElfMapPrefixes
    56  	})
    57  }
    58  
    59  func newTestLoader(tb testing.TB) *loader {
    60  	setupCompilationDirectories(tb)
    61  
    62  	l := newLoader(Params{
    63  		Config: DefaultConfig,
    64  		Sysctl: sysctl.NewDirectSysctl(afero.NewOsFs(), "/proc"),
    65  	})
    66  	l.nodeConfig.Store(&localNodeConfig)
    67  	cw := configWriterForTest(tb)
    68  	l.templateCache = newObjectCache(cw, tb.TempDir())
    69  	return l
    70  }