github.com/cilium/cilium@v1.16.2/pkg/datapath/loader/cache_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package loader 5 6 import ( 7 "context" 8 "runtime" 9 "sync" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 15 fakeTypes "github.com/cilium/cilium/pkg/datapath/fake/types" 16 "github.com/cilium/cilium/pkg/datapath/linux/config" 17 "github.com/cilium/cilium/pkg/datapath/types" 18 fakeNodeMap "github.com/cilium/cilium/pkg/maps/nodemap/fake" 19 "github.com/cilium/cilium/pkg/testutils" 20 ) 21 22 func TestObjectCache(t *testing.T) { 23 tmpDir := t.TempDir() 24 25 setupCompilationDirectories(t) 26 27 ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) 28 defer cancel() 29 30 cache := newObjectCache(configWriterForTest(t), tmpDir) 31 realEP := testutils.NewTestEndpoint() 32 33 dir := getDirs(t) 34 35 // First run should compile and generate the object. 36 first, hash, err := cache.fetchOrCompile(ctx, &localNodeConfig, &realEP, dir, nil) 37 require.NoError(t, err) 38 require.NotEmpty(t, hash) 39 40 // Same EP should not be compiled twice. 41 second, hash2, err := cache.fetchOrCompile(ctx, &localNodeConfig, &realEP, dir, nil) 42 require.NoError(t, err) 43 require.Equal(t, hash, hash2) 44 require.False(t, second == first) 45 46 // Changing the ID should not generate a new object. 47 realEP.Id++ 48 third, hash3, err := cache.fetchOrCompile(ctx, &localNodeConfig, &realEP, dir, nil) 49 require.NoError(t, err) 50 require.Equal(t, hash, hash3) 51 require.False(t, third == first) 52 53 // Changing a setting on the EP should generate a new object. 54 realEP.Opts.SetBool("foo", true) 55 fourth, hash4, err := cache.fetchOrCompile(ctx, &localNodeConfig, &realEP, dir, nil) 56 require.NoError(t, err) 57 require.NotEqual(t, hash, hash4) 58 require.False(t, fourth == first) 59 } 60 61 func TestObjectCacheParallel(t *testing.T) { 62 tmpDir := t.TempDir() 63 64 setupCompilationDirectories(t) 65 66 ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) 67 defer cancel() 68 69 cache := newObjectCache(configWriterForTest(t), tmpDir) 70 ep := testutils.NewTestEndpoint() 71 72 var wg sync.WaitGroup 73 for i := 0; i < runtime.GOMAXPROCS(0); i++ { 74 wg.Add(1) 75 go func() { 76 defer wg.Done() 77 _, _, err := cache.fetchOrCompile(ctx, &localNodeConfig, &ep, getDirs(t), nil) 78 assert.NoError(t, err) 79 }() 80 } 81 82 wg.Wait() 83 } 84 85 func configWriterForTest(t testing.TB) types.ConfigWriter { 86 t.Helper() 87 88 cfg, err := config.NewHeaderfileWriter(config.WriterParams{ 89 NodeMap: fakeNodeMap.NewFakeNodeMapV2(), 90 NodeAddressing: fakeTypes.NewNodeAddressing(), 91 Sysctl: nil, 92 }) 93 if err != nil { 94 t.Fatalf("failed to create header file writer: %v", err) 95 } 96 return cfg 97 }