gitee.com/mysnapcore/mysnapd@v0.1.0/boot/reboot_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2022 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 boot_test 21 22 import ( 23 "fmt" 24 "os" 25 "path/filepath" 26 "time" 27 28 . "gopkg.in/check.v1" 29 30 "gitee.com/mysnapcore/mysnapd/boot" 31 "gitee.com/mysnapcore/mysnapd/bootloader" 32 "gitee.com/mysnapcore/mysnapd/bootloader/bootloadertest" 33 "gitee.com/mysnapcore/mysnapd/testutil" 34 ) 35 36 type rebootSuite struct { 37 baseBootenvSuite 38 } 39 40 var _ = Suite(&rebootSuite{}) 41 42 func (s *rebootSuite) SetUpTest(c *C) { 43 s.baseBootenvSuite.SetUpTest(c) 44 s.AddCleanup(boot.EnableTestingRebootFunction()) 45 } 46 47 func (s *rebootSuite) TestRebootActionString(c *C) { 48 c.Assert(fmt.Sprint(boot.RebootReboot), Equals, "system reboot") 49 c.Assert(fmt.Sprint(boot.RebootHalt), Equals, "system halt") 50 c.Assert(fmt.Sprint(boot.RebootPoweroff), Equals, "system poweroff") 51 } 52 53 func (s *rebootSuite) TestRebootHelper(c *C) { 54 cmd := testutil.MockCommand(c, "shutdown", "") 55 defer cmd.Restore() 56 57 tests := []struct { 58 delay time.Duration 59 delayArg string 60 }{ 61 {-1, "+0"}, 62 {0, "+0"}, 63 {time.Minute, "+1"}, 64 {10 * time.Minute, "+10"}, 65 {30 * time.Second, "+0"}, 66 } 67 68 args := []struct { 69 a boot.RebootAction 70 arg string 71 msg string 72 }{ 73 {boot.RebootReboot, "-r", "reboot scheduled to update the system"}, 74 {boot.RebootHalt, "--halt", "system halt scheduled"}, 75 {boot.RebootPoweroff, "--poweroff", "system poweroff scheduled"}, 76 } 77 78 for _, arg := range args { 79 for _, t := range tests { 80 err := boot.Reboot(arg.a, t.delay, nil) 81 c.Assert(err, IsNil) 82 c.Check(cmd.Calls(), DeepEquals, [][]string{ 83 {"shutdown", arg.arg, t.delayArg, arg.msg}, 84 }) 85 86 cmd.ForgetCalls() 87 } 88 } 89 } 90 91 func (s *rebootSuite) TestRebootWithArguments(c *C) { 92 rbl := bootloadertest.Mock("rebootargs", "").WithRebootBootloader() 93 bootloader.Force(rbl) 94 s.AddCleanup(func() { bootloader.Force(nil) }) 95 rbl.RebootArgs = "0 tryboot" 96 dir := c.MkDir() 97 rebArgsPath := filepath.Join(dir, "reboot-param") 98 restoreRebootArgs := boot.MockRebootArgsPath(rebArgsPath) 99 defer restoreRebootArgs() 100 101 cmd := testutil.MockCommand(c, "shutdown", "") 102 defer cmd.Restore() 103 104 err := boot.Reboot(0, 0, &boot.RebootInfo{RebootRequired: true, RebootBootloader: rbl}) 105 c.Assert(err, IsNil) 106 c.Assert(rebArgsPath, testutil.FileEquals, "0 tryboot\n") 107 c.Check(cmd.Calls(), DeepEquals, [][]string{ 108 {"shutdown", "-r", "+0", "reboot scheduled to update the system"}, 109 }) 110 } 111 112 func (s *rebootSuite) TestRebootNoArguments(c *C) { 113 rbl := bootloadertest.Mock("rebootargs", "").WithRebootBootloader() 114 bootloader.Force(rbl) 115 s.AddCleanup(func() { bootloader.Force(nil) }) 116 rbl.RebootArgs = "" 117 dir := c.MkDir() 118 rebArgsPath := filepath.Join(dir, "reboot-param") 119 restoreRebootArgs := boot.MockRebootArgsPath(rebArgsPath) 120 defer restoreRebootArgs() 121 122 cmd := testutil.MockCommand(c, "shutdown", "") 123 defer cmd.Restore() 124 125 err := boot.Reboot(0, 0, nil) 126 c.Assert(err, IsNil) 127 128 _, err = os.Stat(rebArgsPath) 129 c.Check(os.IsNotExist(err), Equals, true) 130 131 c.Check(cmd.Calls(), DeepEquals, [][]string{ 132 {"shutdown", "-r", "+0", "reboot scheduled to update the system"}, 133 }) 134 }