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