github.com/rigado/snapd@v2.42.5-go-mod+incompatible/overlord/configstate/configcore/network_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package configcore_test
    21  
    22  import (
    23  	"os"
    24  	"path/filepath"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	"github.com/snapcore/snapd/dirs"
    29  	"github.com/snapcore/snapd/osutil"
    30  	"github.com/snapcore/snapd/overlord/configstate/configcore"
    31  	"github.com/snapcore/snapd/release"
    32  	"github.com/snapcore/snapd/testutil"
    33  )
    34  
    35  type networkSuite struct {
    36  	configcoreSuite
    37  
    38  	mockNetworkSysctlPath string
    39  	restores              []func()
    40  	mockSysctl            *testutil.MockCmd
    41  }
    42  
    43  var _ = Suite(&networkSuite{})
    44  
    45  func (s *networkSuite) SetUpTest(c *C) {
    46  	s.configcoreSuite.SetUpTest(c)
    47  	dirs.SetRootDir(c.MkDir())
    48  	s.restores = append(s.restores, release.MockOnClassic(false))
    49  
    50  	s.mockSysctl = testutil.MockCommand(c, "sysctl", "")
    51  	s.restores = append(s.restores, func() { s.mockSysctl.Restore() })
    52  
    53  	s.mockNetworkSysctlPath = filepath.Join(dirs.GlobalRootDir, "/etc/sysctl.d/10-snapd-network.conf")
    54  	c.Assert(os.MkdirAll(filepath.Dir(s.mockNetworkSysctlPath), 0755), IsNil)
    55  }
    56  
    57  func (s *networkSuite) TearDownTest(c *C) {
    58  	dirs.SetRootDir("/")
    59  	for _, f := range s.restores {
    60  		f()
    61  	}
    62  }
    63  
    64  func (s *networkSuite) TestConfigureNetworkIntegrationIPv6(c *C) {
    65  	// disable ipv6
    66  	err := configcore.Run(&mockConf{
    67  		state: s.state,
    68  		conf: map[string]interface{}{
    69  			"network.disable-ipv6": true,
    70  		},
    71  	})
    72  	c.Assert(err, IsNil)
    73  
    74  	c.Check(s.mockNetworkSysctlPath, testutil.FileEquals, "net.ipv6.conf.all.disable_ipv6=1\n")
    75  	c.Check(s.mockSysctl.Calls(), DeepEquals, [][]string{
    76  		{"sysctl", "-w", "net.ipv6.conf.all.disable_ipv6=1"},
    77  	})
    78  	s.mockSysctl.ForgetCalls()
    79  
    80  	// enable it again
    81  	err = configcore.Run(&mockConf{
    82  		state: s.state,
    83  		conf: map[string]interface{}{
    84  			"network.disable-ipv6": false,
    85  		},
    86  	})
    87  	c.Assert(err, IsNil)
    88  
    89  	c.Check(osutil.FileExists(s.mockNetworkSysctlPath), Equals, false)
    90  	c.Check(s.mockSysctl.Calls(), DeepEquals, [][]string{
    91  		{"sysctl", "-w", "net.ipv6.conf.all.disable_ipv6=0"},
    92  	})
    93  	s.mockSysctl.ForgetCalls()
    94  
    95  	// enable it yet again, this does not trigger another syscall
    96  	err = configcore.Run(&mockConf{
    97  		state: s.state,
    98  		conf: map[string]interface{}{
    99  			"network.disable-ipv6": false,
   100  		},
   101  	})
   102  	c.Assert(err, IsNil)
   103  	c.Check(s.mockSysctl.Calls(), HasLen, 0)
   104  }
   105  
   106  func (s *networkSuite) TestConfigureNetworkIntegrationNoSetting(c *C) {
   107  	err := configcore.Run(&mockConf{
   108  		state: s.state,
   109  		conf:  map[string]interface{}{},
   110  	})
   111  	c.Assert(err, IsNil)
   112  
   113  	// the file is not there and was not there before, nothing changed
   114  	// and no sysctl call is generated
   115  	c.Check(osutil.FileExists(s.mockNetworkSysctlPath), Equals, false)
   116  	c.Check(s.mockSysctl.Calls(), HasLen, 0)
   117  }