File: reboot_test.go

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (191 lines) | stat: -rw-r--r-- 5,304 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2022 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package boot_test

import (
	"fmt"
	"path/filepath"
	"time"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/boot"
	"github.com/snapcore/snapd/bootloader"
	"github.com/snapcore/snapd/bootloader/bootloadertest"
	"github.com/snapcore/snapd/testutil"
)

type rebootSuite struct {
	baseBootenvSuite
}

var _ = Suite(&rebootSuite{})

func (s *rebootSuite) SetUpTest(c *C) {
	s.baseBootenvSuite.SetUpTest(c)
	s.AddCleanup(boot.EnableTestingRebootFunction())
}

func (s *rebootSuite) TestRebootActionString(c *C) {
	c.Assert(fmt.Sprint(boot.RebootReboot), Equals, "system reboot")
	c.Assert(fmt.Sprint(boot.RebootHalt), Equals, "system halt")
	c.Assert(fmt.Sprint(boot.RebootPoweroff), Equals, "system poweroff")
}

func (s *rebootSuite) TestRebootHelper(c *C) {
	bl := bootloadertest.Mock("test", "")
	bootloader.Force(bl)
	s.AddCleanup(func() { bootloader.Force(nil) })

	cmd := testutil.MockCommand(c, "shutdown", "")
	defer cmd.Restore()

	tests := []struct {
		delay    time.Duration
		delayArg string
	}{
		{-1, "+0"},
		{0, "+0"},
		{time.Minute, "+1"},
		{10 * time.Minute, "+10"},
		{30 * time.Second, "+0"},
	}

	args := []struct {
		a   boot.RebootAction
		arg string
		msg string
	}{
		{boot.RebootReboot, "-r", "reboot scheduled to update the system"},
		{boot.RebootHalt, "--halt", "system halt scheduled"},
		{boot.RebootPoweroff, "--poweroff", "system poweroff scheduled"},
	}

	for _, arg := range args {
		for _, t := range tests {
			err := boot.Reboot(arg.a, t.delay, nil)
			c.Assert(err, IsNil)
			c.Check(cmd.Calls(), DeepEquals, [][]string{
				{"shutdown", arg.arg, t.delayArg, arg.msg},
			})

			cmd.ForgetCalls()
		}
	}
}

func (s *rebootSuite) TestRebootWithBootloaderError(c *C) {
	rbl := bootloadertest.Mock("rebootargs", "")
	bootloader.Force(rbl)
	s.AddCleanup(func() { bootloader.Force(nil) })

	r := boot.MockBootloaderFind(func(rootdir string, opts *bootloader.Options) (bootloader.Bootloader, error) {
		c.Check(rootdir, Equals, "")
		c.Check(opts, IsNil)
		return nil, fmt.Errorf("oh no")
	})
	defer r()

	cmd := testutil.MockCommand(c, "shutdown", "")
	defer cmd.Restore()

	err := boot.Reboot(0, 0, &boot.RebootInfo{
		BootloaderOptions: nil,
	})
	c.Assert(err, ErrorMatches, `cannot resolve bootloader: oh no`)
	c.Check(cmd.Calls(), HasLen, 0)
}

func (s *rebootSuite) TestRebootWithBootloader(c *C) {
	rbl := bootloadertest.Mock("rebootargs", "")
	bootloader.Force(rbl)
	s.AddCleanup(func() { bootloader.Force(nil) })

	// still get the file-path so we can ensure that the file
	// has not been written
	dir := c.MkDir()
	rebArgsPath := filepath.Join(dir, "reboot-param")
	restoreRebootArgs := boot.MockRebootArgsPath(rebArgsPath)
	defer restoreRebootArgs()

	cmd := testutil.MockCommand(c, "shutdown", "")
	defer cmd.Restore()

	err := boot.Reboot(0, 0, &boot.RebootInfo{
		BootloaderOptions: &bootloader.Options{
			Role: bootloader.RoleRunMode,
		},
	})
	c.Assert(err, IsNil)

	// ensure the arguments file is absent
	c.Assert(rebArgsPath, testutil.FileAbsent)
	c.Check(cmd.Calls(), DeepEquals, [][]string{
		{"shutdown", "-r", "+0", "reboot scheduled to update the system"},
	})
}

func (s *rebootSuite) TestRebootWithRebootBootloader(c *C) {
	rbl := bootloadertest.Mock("rebootargs", "").WithRebootBootloader()
	bootloader.Force(rbl)
	s.AddCleanup(func() { bootloader.Force(nil) })
	rbl.RebootArgs = "0 tryboot"
	dir := c.MkDir()
	rebArgsPath := filepath.Join(dir, "reboot-param")
	restoreRebootArgs := boot.MockRebootArgsPath(rebArgsPath)
	defer restoreRebootArgs()

	cmd := testutil.MockCommand(c, "shutdown", "")
	defer cmd.Restore()

	err := boot.Reboot(0, 0, &boot.RebootInfo{
		BootloaderOptions: &bootloader.Options{
			Role: bootloader.RoleRunMode,
		},
	})
	c.Assert(err, IsNil)
	c.Assert(rebArgsPath, testutil.FileEquals, "0 tryboot\n")
	c.Check(cmd.Calls(), DeepEquals, [][]string{
		{"shutdown", "-r", "+0", "reboot scheduled to update the system"},
	})
}

func (s *rebootSuite) TestRebootWithRebootBootloaderNoArguments(c *C) {
	rbl := bootloadertest.Mock("rebootargs", "").WithRebootBootloader()
	bootloader.Force(rbl)
	s.AddCleanup(func() { bootloader.Force(nil) })
	rbl.RebootArgs = ""
	dir := c.MkDir()
	rebArgsPath := filepath.Join(dir, "reboot-param")
	restoreRebootArgs := boot.MockRebootArgsPath(rebArgsPath)
	defer restoreRebootArgs()

	cmd := testutil.MockCommand(c, "shutdown", "")
	defer cmd.Restore()

	err := boot.Reboot(0, 0, nil)
	c.Assert(err, IsNil)

	// ensure the arguments file is absent
	c.Assert(rebArgsPath, testutil.FileAbsent)
	c.Check(cmd.Calls(), DeepEquals, [][]string{
		{"shutdown", "-r", "+0", "reboot scheduled to update the system"},
	})
}