github.com/rigado/snapd@v2.42.5-go-mod+incompatible/overlord/configstate/configcore/powerbtn_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/testutil" 33 ) 34 35 type powerbtnSuite struct { 36 configcoreSuite 37 38 mockPowerBtnCfg string 39 } 40 41 var _ = Suite(&powerbtnSuite{}) 42 43 func (s *powerbtnSuite) SetUpTest(c *C) { 44 s.configcoreSuite.SetUpTest(c) 45 dirs.SetRootDir(c.MkDir()) 46 c.Assert(os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "etc"), 0755), IsNil) 47 48 s.mockPowerBtnCfg = filepath.Join(dirs.GlobalRootDir, "/etc/systemd/logind.conf.d/00-snap-core.conf") 49 } 50 51 func (s *powerbtnSuite) TearDownTest(c *C) { 52 dirs.SetRootDir("/") 53 } 54 55 func (s *powerbtnSuite) TestConfigurePowerButtonInvalid(c *C) { 56 err := configcore.SwitchHandlePowerKey("invalid-action") 57 c.Check(err, ErrorMatches, `invalid action "invalid-action" supplied for system.power-key-action option`) 58 } 59 60 func (s *powerbtnSuite) TestConfigurePowerIntegration(c *C) { 61 restore := release.MockOnClassic(false) 62 defer restore() 63 64 for _, action := range []string{"ignore", "poweroff", "reboot", "halt", "kexec", "suspend", "hibernate", "hybrid-sleep", "lock"} { 65 66 err := configcore.Run(&mockConf{ 67 state: s.state, 68 conf: map[string]interface{}{ 69 "system.power-key-action": action, 70 }, 71 }) 72 c.Assert(err, IsNil) 73 74 // ensure nothing gets enabled/disabled when an unsupported 75 // service is set for disable 76 c.Check(s.mockPowerBtnCfg, testutil.FileEquals, fmt.Sprintf("[Login]\nHandlePowerKey=%s\n", action)) 77 } 78 79 }