github.com/elfadel/cilium@v1.6.12/pkg/datapath/loader/hash_test.go (about) 1 // Copyright 2019 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build !privileged_tests 16 17 package loader 18 19 import ( 20 "github.com/cilium/cilium/pkg/datapath" 21 "github.com/cilium/cilium/pkg/datapath/linux" 22 "github.com/cilium/cilium/pkg/testutils" 23 24 . "gopkg.in/check.v1" 25 ) 26 27 var ( 28 dummyNodeCfg = datapath.LocalNodeConfiguration{} 29 dummyDevCfg = testutils.NewTestEndpoint() 30 dummyEPCfg = testutils.NewTestEndpoint() 31 ) 32 33 // TesthashDatapath is done in this package just for easy access to dummy 34 // configuration objects. 35 func (s *LoaderTestSuite) TesthashDatapath(c *C) { 36 dp := linux.NewDatapath(linux.DatapathConfiguration{}, nil) 37 h := newDatapathHash() 38 baseHash := h.String() 39 40 // Ensure we get different hashes when config is added 41 h = hashDatapath(dp, &dummyNodeCfg, &dummyDevCfg, &dummyEPCfg) 42 dummyHash := h.String() 43 c.Assert(dummyHash, Not(Equals), baseHash) 44 45 // Ensure we get the same base hash when config is removed via Reset() 46 h.Reset() 47 c.Assert(h.String(), Equals, baseHash) 48 c.Assert(h.String(), Not(Equals), dummyHash) 49 50 // Ensure that with a copy of the endpoint config we get the same hash 51 newEPCfg := dummyEPCfg 52 h = hashDatapath(dp, &dummyNodeCfg, &dummyDevCfg, &newEPCfg) 53 c.Assert(h.String(), Not(Equals), baseHash) 54 c.Assert(h.String(), Equals, dummyHash) 55 56 // Even with different endpoint IDs, we get the same hash 57 // 58 // This is the key to avoiding recompilation per endpoint; static 59 // data substitution is performed via pkg/elf instead. 60 newEPCfg.Id++ 61 h = hashDatapath(dp, &dummyNodeCfg, &dummyDevCfg, &newEPCfg) 62 c.Assert(h.String(), Not(Equals), baseHash) 63 c.Assert(h.String(), Equals, dummyHash) 64 65 // But when we configure the endpoint differently, it's different 66 newEPCfg = testutils.NewTestEndpoint() 67 newEPCfg.Opts.SetBool("foo", true) 68 h = hashDatapath(dp, &dummyNodeCfg, &dummyDevCfg, &newEPCfg) 69 c.Assert(h.String(), Not(Equals), baseHash) 70 c.Assert(h.String(), Not(Equals), dummyHash) 71 }