github.com/cilium/cilium@v1.16.2/pkg/datapath/loader/hash_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package loader 5 6 import ( 7 "errors" 8 "io" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 13 datapath "github.com/cilium/cilium/pkg/datapath/types" 14 "github.com/cilium/cilium/pkg/option" 15 "github.com/cilium/cilium/pkg/testutils" 16 ) 17 18 var ( 19 dummyNodeCfg = datapath.LocalNodeConfiguration{} 20 ) 21 22 // TestHashDatapath is done in this package just for easy access to dummy 23 // configuration objects. 24 func TestHashDatapath(t *testing.T) { 25 // Error from ConfigWriter is forwarded. 26 _, err := hashDatapath(fakeConfigWriter{}, nil) 27 require.Error(t, err) 28 29 // Ensure we get different hashes when config is changed 30 a, err := hashDatapath(fakeConfigWriter("a"), &dummyNodeCfg) 31 require.NoError(t, err) 32 33 b, err := hashDatapath(fakeConfigWriter("b"), &dummyNodeCfg) 34 require.NoError(t, err) 35 require.NotEqual(t, a, b) 36 37 // Ensure we get the same base hash when config is the same. 38 b, err = hashDatapath(fakeConfigWriter("a"), &dummyNodeCfg) 39 require.NoError(t, err) 40 require.Equal(t, a, b) 41 } 42 43 func TestHashEndpoint(t *testing.T) { 44 var base datapathHash 45 ep := testutils.NewTestEndpoint() 46 cfg := configWriterForTest(t) 47 48 // Error from ConfigWriter is forwarded. 49 _, err := base.hashEndpoint(fakeConfigWriter{}, nil, nil) 50 require.Error(t, err) 51 52 // Hashing the endpoint gives a hash distinct from the base. 53 a, err := base.hashEndpoint(cfg, &localNodeConfig, &ep) 54 require.NoError(t, err) 55 require.NotEqual(t, base.String(), a) 56 57 // When we configure the endpoint differently, it's different 58 ep.Opts.SetBool("foo", true) 59 b, err := base.hashEndpoint(cfg, &localNodeConfig, &ep) 60 require.NoError(t, err) 61 require.NotEqual(t, a, b) 62 } 63 64 func TestHashTemplate(t *testing.T) { 65 var base datapathHash 66 ep := testutils.NewTestEndpoint() 67 cfg := configWriterForTest(t) 68 69 // Error from ConfigWriter is forwarded. 70 _, err := base.hashTemplate(fakeConfigWriter{}, nil, nil) 71 require.Error(t, err) 72 73 // Hashing the endpoint gives a hash distinct from the base. 74 a, err := base.hashTemplate(cfg, &localNodeConfig, &ep) 75 require.NoError(t, err) 76 require.NotEqual(t, base.String(), a) 77 78 // Even with different endpoint IDs, we get the same hash 79 // 80 // This is the key to avoiding recompilation per endpoint; static 81 // data substitution is performed via pkg/elf instead. 82 ep.Id++ 83 b, err := base.hashTemplate(cfg, &localNodeConfig, &ep) 84 require.NoError(t, err) 85 require.Equal(t, a, b) 86 } 87 88 type fakeConfigWriter []byte 89 90 func (fc fakeConfigWriter) WriteNodeConfig(w io.Writer, cfg *datapath.LocalNodeConfiguration) error { 91 if cfg == nil { 92 return errors.New("LocalNodeConfiguration is nil") 93 } 94 _, err := w.Write(fc) 95 return err 96 } 97 98 func (fc fakeConfigWriter) WriteNetdevConfig(w io.Writer, opts *option.IntOptions) error { 99 return errors.New("not implemented") 100 } 101 102 func (fc fakeConfigWriter) WriteTemplateConfig(w io.Writer, _ *datapath.LocalNodeConfiguration, cfg datapath.EndpointConfiguration) error { 103 return errors.New("not implemented") 104 } 105 106 func (fc fakeConfigWriter) WriteEndpointConfig(w io.Writer, _ *datapath.LocalNodeConfiguration, cfg datapath.EndpointConfiguration) error { 107 return errors.New("not implemented") 108 }