gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/shutdown/shutdown_test.go (about) 1 // Copyright 2017 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "os" 9 "os/exec" 10 "syscall" 11 "testing" 12 13 "golang.org/x/sys/unix" 14 ) 15 16 var tests = []struct { 17 args []string 18 retCode int 19 }{ 20 // Too many args is an error. 21 {[]string{"halt", "police"}, 1}, 22 {[]string{"halt"}, 2}, 23 {[]string{"-h"}, 2}, 24 // No args means halt. 25 {[]string{}, 2}, 26 {[]string{"reboot"}, 3}, 27 {[]string{"-r"}, 3}, 28 {[]string{"suspend"}, 4}, 29 {[]string{"-s"}, 4}, 30 } 31 32 func TestShutdown(t *testing.T) { 33 for i, tt := range tests { 34 var retCode int 35 c := exec.Command(os.Args[0], append([]string{"-test.run=TestHelperProcess", "--"}, tt.args...)...) 36 c.Env = []string{"GO_WANT_HELPER_PROCESS=1"} 37 _, err := c.Output() 38 if err != nil { 39 exitErr, ok := err.(*exec.ExitError) 40 if !ok { 41 t.Errorf("%d. Error running shutdown: %v", i, err) 42 continue 43 } 44 retCode = exitErr.Sys().(syscall.WaitStatus).ExitStatus() 45 } 46 if retCode != tt.retCode { 47 t.Errorf("%d. Want: %d; Got: %d", i, tt.retCode, retCode) 48 } 49 } 50 } 51 52 func TestHelperProcess(t *testing.T) { 53 if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { 54 t.Logf("just a helper") 55 return 56 } 57 58 reboot = func(i int) error { 59 xval := 1 60 switch uint32(i) { 61 case unix.LINUX_REBOOT_CMD_POWER_OFF: 62 xval = 2 63 case unix.LINUX_REBOOT_CMD_RESTART: 64 xval = 3 65 case unix.LINUX_REBOOT_CMD_SW_SUSPEND: 66 xval = 4 67 } 68 69 t.Logf("Exit with %#x", i) 70 os.Exit(xval) 71 return nil 72 } 73 74 os.Args = append([]string{"shutdown"}, os.Args[3:]...) 75 main() 76 }