github.com/fafucoder/cilium@v1.6.11/pkg/modules/modules_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 modules
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"github.com/cilium/cilium/pkg/checker"
    24  
    25  	. "gopkg.in/check.v1"
    26  )
    27  
    28  const (
    29  	modulesContent = `ebtable_nat 16384 1 - Live 0x0000000000000000
    30  ebtable_broute 16384 1 - Live 0x0000000000000000
    31  bridge 172032 1 ebtable_broute, Live 0x0000000000000000
    32  ip6table_nat 16384 1 - Live 0x0000000000000000
    33  nf_nat_ipv6 16384 1 ip6table_nat, Live 0x0000000000000000
    34  ip6table_mangle 16384 1 - Live 0x0000000000000000
    35  ip6table_raw 16384 1 - Live 0x0000000000000000
    36  ip6table_security 16384 1 - Live 0x0000000000000000
    37  iptable_nat 16384 1 - Live 0x0000000000000000
    38  nf_nat_ipv4 16384 1 iptable_nat, Live 0x0000000000000000
    39  iptable_mangle 16384 1 - Live 0x0000000000000000
    40  iptable_raw 16384 1 - Live 0x0000000000000000
    41  iptable_security 16384 1 - Live 0x0000000000000000
    42  ebtable_filter 16384 1 - Live 0x0000000000000000
    43  ebtables 36864 3 ebtable_nat,ebtable_broute,ebtable_filter, Live 0x0000000000000000
    44  ip6table_filter 16384 1 - Live 0x0000000000000000
    45  ip6_tables 28672 5 ip6table_nat,ip6table_mangle,ip6table_raw,ip6table_security,ip6table_filter, Live 0x0000000000000000
    46  iptable_filter 16384 1 - Live 0x0000000000000000
    47  ip_tables 28672 5 iptable_nat,iptable_mangle,iptable_raw,iptable_security,iptable_filter, Live 0x0000000000000000
    48  x_tables 40960 23 xt_multiport,xt_nat,xt_addrtype,xt_mark,xt_comment,xt_CHECKSUM,ipt_MASQUERADE,xt_tcpudp,ip6t_rpfilter,ip6t_REJECT,ipt_REJECT,xt_conntrack,ip6table_mangle,ip6table_raw,ip6table_security,iptable_mangle,iptable_raw,iptable_security,ebtables,ip6table_filter,ip6_tables,iptable_filter,ip_tables, Live 0x0000000000000000`
    49  )
    50  
    51  // Hook up gocheck into the "go test" runner.
    52  func Test(t *testing.T) {
    53  	TestingT(t)
    54  }
    55  
    56  type ModulesTestSuite struct{}
    57  
    58  var _ = Suite(&ModulesTestSuite{})
    59  
    60  func (s *ModulesTestSuite) TestInit(c *C) {
    61  	manager := &ModulesManager{}
    62  	c.Assert(manager.modulesList, IsNil)
    63  	err := manager.Init()
    64  	c.Assert(err, IsNil)
    65  	c.Assert(manager.modulesList, NotNil)
    66  }
    67  
    68  func (s *ModulesTestSuite) TestFindModules(c *C) {
    69  	manager := &ModulesManager{
    70  		modulesList: []string{
    71  			"ip6_tables",
    72  			"ip6table_mangle",
    73  			"ip6table_filter",
    74  			"ip6table_security",
    75  			"ip6table_raw",
    76  			"ip6table_nat",
    77  		},
    78  	}
    79  	testCases := []struct {
    80  		modulesToFind []string
    81  		isSubset      bool
    82  		expectedDiff  []string
    83  	}{
    84  		{
    85  			modulesToFind: []string{
    86  				"ip6_tables",
    87  				"ip6table_mangle",
    88  				"ip6table_filter",
    89  				"ip6table_security",
    90  				"ip6table_raw",
    91  				"ip6table_nat",
    92  			},
    93  			isSubset:     true,
    94  			expectedDiff: nil,
    95  		},
    96  		{
    97  			modulesToFind: []string{
    98  				"ip6_tables",
    99  				"ip6table_mangle",
   100  				"ip6table_raw",
   101  			},
   102  			isSubset:     true,
   103  			expectedDiff: nil,
   104  		},
   105  		{
   106  			modulesToFind: []string{
   107  				"ip6_tables",
   108  				"ip6table_mangle",
   109  				"ip6table_raw",
   110  				"foo_module",
   111  			},
   112  			isSubset:     false,
   113  			expectedDiff: []string{"foo_module"},
   114  		},
   115  		{
   116  			modulesToFind: []string{
   117  				"foo_module",
   118  				"bar_module",
   119  			},
   120  			isSubset:     false,
   121  			expectedDiff: []string{"foo_module", "bar_module"},
   122  		},
   123  	}
   124  	for _, tc := range testCases {
   125  		found, diff := manager.FindModules(tc.modulesToFind...)
   126  		c.Assert(found, Equals, tc.isSubset)
   127  		c.Assert(diff, checker.DeepEquals, tc.expectedDiff)
   128  	}
   129  }
   130  
   131  func (s *ModulesTestSuite) TestParseModuleFile(c *C) {
   132  	expectedLength := 20
   133  	expectedModules := []string{
   134  		"ebtable_nat",
   135  		"ebtable_broute",
   136  		"bridge",
   137  		"ip6table_nat",
   138  		"nf_nat_ipv6",
   139  		"ip6table_mangle",
   140  		"ip6table_raw",
   141  		"ip6table_security",
   142  		"iptable_nat",
   143  		"nf_nat_ipv4",
   144  		"iptable_mangle",
   145  		"iptable_raw",
   146  		"iptable_security",
   147  		"ebtable_filter",
   148  		"ebtables",
   149  		"ip6table_filter",
   150  		"ip6_tables",
   151  		"iptable_filter",
   152  		"ip_tables",
   153  		"x_tables",
   154  	}
   155  
   156  	r := bytes.NewBuffer([]byte(modulesContent))
   157  	moduleInfos, err := parseModulesFile(r)
   158  	c.Assert(err, IsNil)
   159  	c.Assert(moduleInfos, HasLen, expectedLength)
   160  	c.Assert(moduleInfos, checker.DeepEquals, expectedModules)
   161  }
   162  
   163  func (s *ModulesTestSuite) TestListModules(c *C) {
   164  	_, err := listModules()
   165  	c.Assert(err, IsNil)
   166  }