github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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  	c.Assert(os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "etc"), 0755), IsNil)
    46  
    47  	s.mockPowerBtnCfg = filepath.Join(dirs.GlobalRootDir, "/etc/systemd/logind.conf.d/00-snap-core.conf")
    48  }
    49  
    50  func (s *powerbtnSuite) TestConfigurePowerButtonInvalid(c *C) {
    51  	err := configcore.SwitchHandlePowerKey("invalid-action", nil)
    52  	c.Check(err, ErrorMatches, `invalid action "invalid-action" supplied for system.power-key-action option`)
    53  }
    54  
    55  func (s *powerbtnSuite) TestConfigurePowerIntegration(c *C) {
    56  	restore := release.MockOnClassic(false)
    57  	defer restore()
    58  
    59  	for _, action := range []string{"ignore", "poweroff", "reboot", "halt", "kexec", "suspend", "hibernate", "hybrid-sleep", "lock"} {
    60  
    61  		err := configcore.Run(&mockConf{
    62  			state: s.state,
    63  			conf: map[string]interface{}{
    64  				"system.power-key-action": action,
    65  			},
    66  		})
    67  		c.Assert(err, IsNil)
    68  
    69  		// ensure nothing gets enabled/disabled when an unsupported
    70  		// service is set for disable
    71  		c.Check(s.mockPowerBtnCfg, testutil.FileEquals, fmt.Sprintf("[Login]\nHandlePowerKey=%s\n", action))
    72  	}
    73  
    74  }
    75  
    76  func (s *powerbtnSuite) TestFilesystemOnlyApply(c *C) {
    77  	conf := configcore.PlainCoreConfig(map[string]interface{}{
    78  		"system.power-key-action": "reboot",
    79  	})
    80  	tmpDir := c.MkDir()
    81  	c.Assert(configcore.FilesystemOnlyApply(tmpDir, conf, nil), IsNil)
    82  
    83  	powerBtnCfg := filepath.Join(tmpDir, "/etc/systemd/logind.conf.d/00-snap-core.conf")
    84  	c.Check(powerBtnCfg, testutil.FileEquals, "[Login]\nHandlePowerKey=reboot\n")
    85  }