github.com/rigado/snapd@v2.42.5-go-mod+incompatible/overlord/configstate/configcore/services_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017 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 "fmt" 24 "os" 25 "path/filepath" 26 27 . "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/dirs" 30 "github.com/snapcore/snapd/overlord/configstate/configcore" 31 "github.com/snapcore/snapd/release" 32 "github.com/snapcore/snapd/snap" 33 "github.com/snapcore/snapd/testutil" 34 ) 35 36 type servicesSuite struct { 37 configcoreSuite 38 testutil.BaseTest 39 } 40 41 var _ = Suite(&servicesSuite{}) 42 43 func (s *servicesSuite) SetUpTest(c *C) { 44 s.BaseTest.SetUpTest(c) 45 s.configcoreSuite.SetUpTest(c) 46 dirs.SetRootDir(c.MkDir()) 47 c.Assert(os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "etc"), 0755), IsNil) 48 s.systemctlArgs = nil 49 s.BaseTest.AddCleanup(snap.MockSanitizePlugsSlots(func(snapInfo *snap.Info) {})) 50 } 51 52 func (s *servicesSuite) TearDownTest(c *C) { 53 dirs.SetRootDir("/") 54 s.BaseTest.TearDownTest(c) 55 } 56 57 func (s *servicesSuite) TestConfigureServiceInvalidValue(c *C) { 58 err := configcore.SwitchDisableService("ssh.service", "xxx") 59 c.Check(err, ErrorMatches, `option "ssh.service" has invalid value "xxx"`) 60 } 61 62 func (s *servicesSuite) TestConfigureServiceNotDisabled(c *C) { 63 err := configcore.SwitchDisableService("sshd.service", "false") 64 c.Assert(err, IsNil) 65 c.Check(s.systemctlArgs, DeepEquals, [][]string{ 66 {"--root", dirs.GlobalRootDir, "unmask", "sshd.service"}, 67 {"--root", dirs.GlobalRootDir, "enable", "sshd.service"}, 68 {"start", "sshd.service"}, 69 }) 70 } 71 72 func (s *servicesSuite) TestConfigureServiceDisabled(c *C) { 73 err := configcore.SwitchDisableService("sshd.service", "true") 74 c.Assert(err, IsNil) 75 c.Check(s.systemctlArgs, DeepEquals, [][]string{ 76 {"--root", dirs.GlobalRootDir, "disable", "sshd.service"}, 77 {"--root", dirs.GlobalRootDir, "mask", "sshd.service"}, 78 {"stop", "sshd.service"}, 79 {"show", "--property=ActiveState", "sshd.service"}, 80 }) 81 } 82 83 func (s *servicesSuite) TestConfigureServiceDisabledIntegration(c *C) { 84 restore := release.MockOnClassic(false) 85 defer restore() 86 87 err := os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "/etc/ssh"), 0755) 88 c.Assert(err, IsNil) 89 90 for _, service := range []struct { 91 cfgName string 92 systemdName string 93 }{ 94 {"ssh", "ssh.service"}, 95 {"rsyslog", "rsyslog.service"}, 96 } { 97 s.systemctlArgs = nil 98 99 err := configcore.Run(&mockConf{ 100 state: s.state, 101 conf: map[string]interface{}{ 102 fmt.Sprintf("service.%s.disable", service.cfgName): true, 103 }, 104 }) 105 c.Assert(err, IsNil) 106 srv := service.systemdName 107 if service.cfgName == "ssh" { 108 // SSH is special cased 109 sshCanary := filepath.Join(dirs.GlobalRootDir, "/etc/ssh/sshd_not_to_be_run") 110 _, err := os.Stat(sshCanary) 111 c.Assert(err, IsNil) 112 c.Check(s.systemctlArgs, DeepEquals, [][]string{ 113 {"stop", srv}, 114 {"show", "--property=ActiveState", srv}, 115 }) 116 } else { 117 c.Check(s.systemctlArgs, DeepEquals, [][]string{ 118 {"--root", dirs.GlobalRootDir, "disable", srv}, 119 {"--root", dirs.GlobalRootDir, "mask", srv}, 120 {"stop", srv}, 121 {"show", "--property=ActiveState", srv}, 122 }) 123 } 124 } 125 } 126 127 func (s *servicesSuite) TestConfigureServiceEnableIntegration(c *C) { 128 restore := release.MockOnClassic(false) 129 defer restore() 130 131 err := os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "/etc/ssh"), 0755) 132 c.Assert(err, IsNil) 133 134 for _, service := range []struct { 135 cfgName string 136 systemdName string 137 }{ 138 {"ssh", "ssh.service"}, 139 {"rsyslog", "rsyslog.service"}, 140 } { 141 s.systemctlArgs = nil 142 err := configcore.Run(&mockConf{ 143 state: s.state, 144 conf: map[string]interface{}{ 145 fmt.Sprintf("service.%s.disable", service.cfgName): false, 146 }, 147 }) 148 149 c.Assert(err, IsNil) 150 srv := service.systemdName 151 if service.cfgName == "ssh" { 152 // SSH is special cased 153 c.Check(s.systemctlArgs, DeepEquals, [][]string{ 154 {"--root", dirs.GlobalRootDir, "unmask", "sshd.service"}, 155 {"--root", dirs.GlobalRootDir, "unmask", "ssh.service"}, 156 {"start", srv}, 157 }) 158 sshCanary := filepath.Join(dirs.GlobalRootDir, "/etc/ssh/sshd_not_to_be_run") 159 _, err := os.Stat(sshCanary) 160 c.Assert(err, ErrorMatches, ".* no such file or directory") 161 } else { 162 c.Check(s.systemctlArgs, DeepEquals, [][]string{ 163 {"--root", dirs.GlobalRootDir, "unmask", srv}, 164 {"--root", dirs.GlobalRootDir, "enable", srv}, 165 {"start", srv}, 166 }) 167 } 168 } 169 } 170 171 func (s *servicesSuite) TestConfigureServiceUnsupportedService(c *C) { 172 restore := release.MockOnClassic(false) 173 defer restore() 174 175 err := configcore.Run(&mockConf{ 176 state: s.state, 177 conf: map[string]interface{}{ 178 "service.snapd.disable": true, 179 }, 180 }) 181 c.Assert(err, IsNil) 182 183 // ensure nothing gets enabled/disabled when an unsupported 184 // service is set for disable 185 c.Check(s.systemctlArgs, IsNil) 186 }