github.com/elfadel/cilium@v1.6.12/pkg/datapath/linux/config_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 linux
    18  
    19  import (
    20  	"bytes"
    21  	"errors"
    22  	"io"
    23  	"strings"
    24  
    25  	"github.com/cilium/cilium/pkg/bpf"
    26  	"github.com/cilium/cilium/pkg/datapath"
    27  	"github.com/cilium/cilium/pkg/datapath/loader"
    28  	"github.com/cilium/cilium/pkg/node"
    29  	"github.com/cilium/cilium/pkg/testutils"
    30  
    31  	. "gopkg.in/check.v1"
    32  )
    33  
    34  type DatapathSuite struct{}
    35  
    36  var (
    37  	_ = Suite(&DatapathSuite{})
    38  
    39  	dummyNodeCfg  = datapath.LocalNodeConfiguration{}
    40  	dummyDevCfg   = testutils.NewTestEndpoint()
    41  	dummyEPCfg    = testutils.NewTestEndpoint()
    42  	ipv4DummyAddr = []byte{192, 0, 2, 3}
    43  )
    44  
    45  func (s *DatapathSuite) SetUpTest(c *C) {
    46  	err := bpf.ConfigureResourceLimits()
    47  	c.Assert(err, IsNil)
    48  	node.InitDefaultPrefix("")
    49  	node.SetInternalIPv4(ipv4DummyAddr)
    50  	node.SetIPv4Loopback(ipv4DummyAddr)
    51  }
    52  
    53  func (s *DatapathSuite) TearDownTest(c *C) {
    54  	node.SetInternalIPv4(nil)
    55  	node.SetIPv4Loopback(nil)
    56  }
    57  
    58  type badWriter struct{}
    59  
    60  func (b *badWriter) Write(p []byte) (int, error) {
    61  	return 0, errors.New("bad write :(")
    62  }
    63  
    64  type writeFn func(io.Writer, datapath.Datapath) error
    65  
    66  func writeConfig(c *C, header string, write writeFn) {
    67  	tests := []struct {
    68  		description string
    69  		output      io.Writer
    70  		expResult   Checker
    71  	}{
    72  		{
    73  			description: "successful write to an in-memory buffer",
    74  			output:      &bytes.Buffer{},
    75  			expResult:   IsNil,
    76  		},
    77  		{
    78  			description: "write to a failing writer",
    79  			output:      &badWriter{},
    80  			expResult:   NotNil,
    81  		},
    82  	}
    83  	for _, test := range tests {
    84  		c.Logf("  Testing %s configuration: %s", header, test.description)
    85  		dp := NewDatapath(DatapathConfiguration{}, nil)
    86  		c.Assert(write(test.output, dp), test.expResult)
    87  	}
    88  }
    89  
    90  func (s *DatapathSuite) TestWriteNodeConfig(c *C) {
    91  	writeConfig(c, "node", func(w io.Writer, dp datapath.Datapath) error {
    92  		return dp.WriteNodeConfig(w, &dummyNodeCfg)
    93  	})
    94  }
    95  
    96  func (s *DatapathSuite) TestWriteNetdevConfig(c *C) {
    97  	writeConfig(c, "netdev", func(w io.Writer, dp datapath.Datapath) error {
    98  		return dp.WriteNetdevConfig(w, &dummyDevCfg)
    99  	})
   100  }
   101  
   102  func (s *DatapathSuite) TestWriteEndpointConfig(c *C) {
   103  	writeConfig(c, "endpoint", func(w io.Writer, dp datapath.Datapath) error {
   104  		return dp.WriteEndpointConfig(w, &dummyEPCfg)
   105  	})
   106  }
   107  
   108  func (s *DatapathSuite) TestWriteStaticData(c *C) {
   109  	dp := NewDatapath(DatapathConfiguration{}, nil).(*linuxDatapath)
   110  	ep := &dummyEPCfg
   111  
   112  	varSub, stringSub := loader.ELFSubstitutions(ep)
   113  
   114  	var buf bytes.Buffer
   115  	dp.writeStaticData(&buf, ep)
   116  	b := buf.Bytes()
   117  	for k := range varSub {
   118  		for _, suffix := range []string{"_1", "_2", "_3", "_4"} {
   119  			// Variables with these suffixes are implemented via
   120  			// multiple 32-bit values. The header define doesn't
   121  			// include these numbers though, so strip them.
   122  			if strings.HasSuffix(k, suffix) {
   123  				k = strings.TrimSuffix(k, suffix)
   124  				break
   125  			}
   126  		}
   127  		c.Assert(bytes.Contains(b, []byte(k)), Equals, true)
   128  	}
   129  	for _, v := range stringSub {
   130  		c.Logf("Ensuring config has %s", v)
   131  		if strings.HasPrefix(v, "1/0x") {
   132  			// Skip tail call map name replacement
   133  			continue
   134  		}
   135  		c.Assert(bytes.Contains(b, []byte(v)), Equals, true)
   136  	}
   137  }