github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/overlord/hookstate/ctlcmd/reboot_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2021 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 ctlcmd_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/dirs"
    26  	"github.com/snapcore/snapd/overlord/devicestate"
    27  	"github.com/snapcore/snapd/overlord/hookstate"
    28  	"github.com/snapcore/snapd/overlord/hookstate/ctlcmd"
    29  	"github.com/snapcore/snapd/overlord/hookstate/hooktest"
    30  	"github.com/snapcore/snapd/overlord/state"
    31  	"github.com/snapcore/snapd/snap"
    32  	"github.com/snapcore/snapd/testutil"
    33  )
    34  
    35  type rebootSuite struct {
    36  	testutil.BaseTest
    37  	state       *state.State
    38  	mockContext *hookstate.Context
    39  	mockHandler *hooktest.MockHandler
    40  	hookTask    *state.Task
    41  	restartTask *state.Task
    42  }
    43  
    44  var _ = Suite(&rebootSuite{})
    45  
    46  func (s *rebootSuite) SetUpTest(c *C) {
    47  	s.BaseTest.SetUpTest(c)
    48  	dirs.SetRootDir(c.MkDir())
    49  
    50  	s.mockHandler = hooktest.NewMockHandler()
    51  
    52  	s.state = state.New(nil)
    53  	s.state.Lock()
    54  	defer s.state.Unlock()
    55  	task := s.state.NewTask("test-task", "my test task")
    56  	setup := &hookstate.HookSetup{Snap: "test-snap", Revision: snap.R(42), Hook: "install-device"}
    57  
    58  	ctx, err := hookstate.NewContext(task, s.state, setup, s.mockHandler, "")
    59  	c.Assert(err, IsNil)
    60  	s.mockContext = ctx
    61  
    62  	s.hookTask = task
    63  	s.restartTask = s.state.NewTask("restart-task", "task managing restart")
    64  }
    65  
    66  func (s *rebootSuite) TestBadHook(c *C) {
    67  	s.state.Lock()
    68  	task := s.state.NewTask("test-task", "my test task")
    69  	setup := &hookstate.HookSetup{Snap: "test-snap", Revision: snap.R(42), Hook: "configure"}
    70  	s.state.Unlock()
    71  
    72  	ctx, err := hookstate.NewContext(task, s.state, setup, s.mockHandler, "")
    73  	c.Assert(err, IsNil)
    74  
    75  	_, _, err = ctlcmd.Run(ctx, []string{"reboot", "--halt"}, 0)
    76  	c.Assert(err, ErrorMatches, `cannot use reboot command outside of gadget install-device hook`)
    77  }
    78  
    79  func (s *rebootSuite) TestBadArgs(c *C) {
    80  	type tableT struct {
    81  		args []string
    82  		err  string
    83  	}
    84  	table := []tableT{
    85  		{
    86  			[]string{"reboot"},
    87  			"either --halt or --poweroff must be specified",
    88  		}, {
    89  			[]string{"reboot", "--halt", "--poweroff"},
    90  			"cannot specify both --halt and --poweroff",
    91  		}, {
    92  			[]string{"reboot", "--foo"},
    93  			"unknown flag `foo'",
    94  		},
    95  	}
    96  
    97  	for i, t := range table {
    98  		_, _, err := ctlcmd.Run(s.mockContext, t.args, 0)
    99  		c.Check(err, ErrorMatches, t.err, Commentf("%d", i))
   100  	}
   101  }
   102  
   103  func (s *rebootSuite) TestRegularRunHalt(c *C) {
   104  	s.state.Lock()
   105  	s.hookTask.Set("restart-task", s.restartTask.ID())
   106  	chg := s.state.NewChange("install-device", "install-device")
   107  	chg.AddTask(s.restartTask)
   108  	s.state.Unlock()
   109  
   110  	_, _, err := ctlcmd.Run(s.mockContext, []string{"reboot", "--halt"}, 0)
   111  	c.Assert(err, IsNil)
   112  
   113  	s.state.Lock()
   114  	defer s.state.Unlock()
   115  	var rebootOpts devicestate.RebootOptions
   116  	err = s.restartTask.Get("reboot", &rebootOpts)
   117  	c.Assert(err, IsNil)
   118  
   119  	c.Check(rebootOpts.Op, Equals, devicestate.RebootHaltOp)
   120  }
   121  
   122  func (s *rebootSuite) TestRegularRunPoweroff(c *C) {
   123  	s.state.Lock()
   124  	s.hookTask.Set("restart-task", s.restartTask.ID())
   125  	chg := s.state.NewChange("install-device", "install-device")
   126  	chg.AddTask(s.restartTask)
   127  	s.state.Unlock()
   128  
   129  	_, _, err := ctlcmd.Run(s.mockContext, []string{"reboot", "--poweroff"}, 0)
   130  	c.Assert(err, IsNil)
   131  
   132  	s.state.Lock()
   133  	defer s.state.Unlock()
   134  	var rebootOpts devicestate.RebootOptions
   135  	err = s.restartTask.Get("reboot", &rebootOpts)
   136  	c.Assert(err, IsNil)
   137  
   138  	c.Check(rebootOpts.Op, Equals, devicestate.RebootPoweroffOp)
   139  
   140  }