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