github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/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/testutil" 32 ) 33 34 type networkSuite struct { 35 configcoreSuite 36 37 mockNetworkSysctlPath string 38 mockSysctl *testutil.MockCmd 39 } 40 41 var _ = Suite(&networkSuite{}) 42 43 func (s *networkSuite) SetUpTest(c *C) { 44 s.configcoreSuite.SetUpTest(c) 45 46 s.mockSysctl = testutil.MockCommand(c, "sysctl", "") 47 s.AddCleanup(s.mockSysctl.Restore) 48 49 s.mockNetworkSysctlPath = filepath.Join(dirs.GlobalRootDir, "/etc/sysctl.d/10-snapd-network.conf") 50 c.Assert(os.MkdirAll(filepath.Dir(s.mockNetworkSysctlPath), 0755), IsNil) 51 } 52 53 func (s *networkSuite) TestConfigureNetworkIntegrationIPv6(c *C) { 54 // disable ipv6 55 err := configcore.Run(coreDev, &mockConf{ 56 state: s.state, 57 conf: map[string]interface{}{ 58 "network.disable-ipv6": true, 59 }, 60 }) 61 c.Assert(err, IsNil) 62 63 c.Check(s.mockNetworkSysctlPath, testutil.FileEquals, "net.ipv6.conf.all.disable_ipv6=1\n") 64 c.Check(s.mockSysctl.Calls(), DeepEquals, [][]string{ 65 {"sysctl", "-w", "net.ipv6.conf.all.disable_ipv6=1"}, 66 }) 67 s.mockSysctl.ForgetCalls() 68 69 // enable it again 70 err = configcore.Run(coreDev, &mockConf{ 71 state: s.state, 72 conf: map[string]interface{}{ 73 "network.disable-ipv6": false, 74 }, 75 }) 76 c.Assert(err, IsNil) 77 78 c.Check(osutil.FileExists(s.mockNetworkSysctlPath), Equals, false) 79 c.Check(s.mockSysctl.Calls(), DeepEquals, [][]string{ 80 {"sysctl", "-w", "net.ipv6.conf.all.disable_ipv6=0"}, 81 }) 82 s.mockSysctl.ForgetCalls() 83 84 // enable it yet again, this does not trigger another syscall 85 err = configcore.Run(coreDev, &mockConf{ 86 state: s.state, 87 conf: map[string]interface{}{ 88 "network.disable-ipv6": false, 89 }, 90 }) 91 c.Assert(err, IsNil) 92 c.Check(s.mockSysctl.Calls(), HasLen, 0) 93 } 94 95 func (s *networkSuite) TestConfigureNetworkIntegrationNoSetting(c *C) { 96 err := configcore.Run(coreDev, &mockConf{ 97 state: s.state, 98 conf: map[string]interface{}{}, 99 }) 100 c.Assert(err, IsNil) 101 102 // the file is not there and was not there before, nothing changed 103 // and no sysctl call is generated 104 c.Check(osutil.FileExists(s.mockNetworkSysctlPath), Equals, false) 105 c.Check(s.mockSysctl.Calls(), HasLen, 0) 106 } 107 108 func (s *networkSuite) TestFilesystemOnlyApply(c *C) { 109 conf := configcore.PlainCoreConfig(map[string]interface{}{ 110 "network.disable-ipv6": true, 111 }) 112 113 tmpDir := c.MkDir() 114 c.Assert(os.MkdirAll(filepath.Join(tmpDir, "/etc/sysctl.d/"), 0755), IsNil) 115 c.Assert(configcore.FilesystemOnlyApply(coreDev, tmpDir, conf), IsNil) 116 117 networkSysctlPath := filepath.Join(tmpDir, "/etc/sysctl.d/10-snapd-network.conf") 118 c.Check(networkSysctlPath, testutil.FileEquals, "net.ipv6.conf.all.disable_ipv6=1\n") 119 120 // sysctl was not executed 121 c.Check(s.mockSysctl.Calls(), HasLen, 0) 122 } 123 124 func (s *networkSuite) TestFilesystemOnlyApplyValidationFails(c *C) { 125 conf := configcore.PlainCoreConfig(map[string]interface{}{ 126 "network.disable-ipv6": "0", 127 }) 128 129 tmpDir := c.MkDir() 130 c.Assert(configcore.FilesystemOnlyApply(coreDev, tmpDir, conf), ErrorMatches, `network.disable-ipv6 can only be set to 'true' or 'false'`) 131 }