github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/sysctl/sysctl_linux_privileged_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 linux,privileged_tests
    16  
    17  package sysctl
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "gopkg.in/check.v1"
    23  )
    24  
    25  // Hook up gocheck into the "go test" runner.
    26  func Test(t *testing.T) {
    27  	TestingT(t)
    28  }
    29  
    30  type SysctlLinuxPrivilegedTestSuite struct{}
    31  
    32  var _ = Suite(&SysctlLinuxPrivilegedTestSuite{})
    33  
    34  func (s *SysctlLinuxPrivilegedTestSuite) TestWriteSysctl(c *C) {
    35  	testCases := []struct {
    36  		name        string
    37  		value       string
    38  		expectedErr bool
    39  	}{
    40  		{
    41  			name:        "net.ipv4.ip_forward",
    42  			value:       "1",
    43  			expectedErr: false,
    44  		},
    45  		{
    46  			name:        "net.ipv4.conf.all.forwarding",
    47  			value:       "1",
    48  			expectedErr: false,
    49  		},
    50  		{
    51  			name:        "net.ipv6.conf.all.forwarding",
    52  			value:       "1",
    53  			expectedErr: false,
    54  		},
    55  		{
    56  			name:        "foo.bar",
    57  			value:       "1",
    58  			expectedErr: true,
    59  		},
    60  	}
    61  
    62  	for _, tc := range testCases {
    63  		err := writeSysctl(tc.name, tc.value)
    64  		if tc.expectedErr {
    65  			c.Assert(err, NotNil)
    66  		} else {
    67  			c.Assert(err, IsNil)
    68  		}
    69  	}
    70  }
    71  
    72  func (s *SysctlLinuxPrivilegedTestSuite) TestDisableEnable(c *C) {
    73  	testCases := []struct {
    74  		name        string
    75  		expectedErr bool
    76  	}{
    77  		{
    78  			name:        "net.ipv4.ip_forward",
    79  			expectedErr: false,
    80  		},
    81  		{
    82  			name:        "net.ipv4.conf.all.forwarding",
    83  			expectedErr: false,
    84  		},
    85  		{
    86  			name:        "net.ipv6.conf.all.forwarding",
    87  			expectedErr: false,
    88  		},
    89  		{
    90  			name:        "foo.bar",
    91  			expectedErr: true,
    92  		},
    93  	}
    94  
    95  	for _, tc := range testCases {
    96  		err := Enable(tc.name)
    97  		if tc.expectedErr {
    98  			c.Assert(err, NotNil)
    99  		} else {
   100  			c.Assert(err, IsNil)
   101  		}
   102  		err = Disable(tc.name)
   103  		if tc.expectedErr {
   104  			c.Assert(err, NotNil)
   105  		} else {
   106  			c.Assert(err, IsNil)
   107  		}
   108  	}
   109  }