gopkg.in/hugelgupf/u-root.v9@v9.0.0-20180831063832-3f6f1057f09b/cmds/kill/kill_test.go (about) 1 // Copyright 2016 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 "bytes" 9 "fmt" 10 "io/ioutil" 11 "os" 12 "os/exec" 13 "testing" 14 "time" 15 16 "github.com/u-root/u-root/pkg/testutil" 17 ) 18 19 // Run the command, with the optional args, and return a string 20 // for stdout, stderr, and an error. 21 func run(c *exec.Cmd) (string, string, error) { 22 var o, e bytes.Buffer 23 c.Stdout, c.Stderr = &o, &e 24 err := c.Run() 25 return o.String(), e.String(), err 26 } 27 28 func TestKillProcess(t *testing.T) { 29 tmpDir, err := ioutil.TempDir("", "KillTest") 30 if err != nil { 31 t.Fatal("TempDir failed: ", err) 32 } 33 defer os.RemoveAll(tmpDir) 34 35 // Re-exec the test binary itself to emulate "sleep 1". 36 cmd := exec.Command("/bin/sleep", "10") 37 if err := cmd.Start(); err != nil { 38 t.Fatalf("Failed to start test process: %v", err) 39 } 40 41 // from the orignal. hokey .1 second wait for the process to start. Racy. 42 time.Sleep(100 * time.Millisecond) 43 44 if _, _, err := run(testutil.Command(t, "-9", fmt.Sprintf("%d", cmd.Process.Pid))); err != nil { 45 t.Errorf("Could not spawn first kill: %v", err) 46 } 47 48 if err := cmd.Wait(); err == nil { 49 t.Errorf("Test process succeeded, but expected to fail") 50 } 51 52 // now this is a little weird. We're going to try to kill it again. 53 // Arguably, this should be done in another test, but finding a process 54 // you just "know" does not exist is tricky. What PID do you use? 55 // So we just kill the one we just killed; it should get an error. 56 // If not, something's wrong. 57 if _, _, err := run(testutil.Command(t, "-9", fmt.Sprintf("%d", cmd.Process.Pid))); err == nil { 58 t.Fatalf("Second kill: got nil, want error") 59 } 60 } 61 62 func TestBadInvocations(t *testing.T) { 63 var ( 64 tab = []struct { 65 a []string 66 err string 67 }{ 68 {a: []string{"-1w34"}, err: "1w34 is not a valid signal\n"}, 69 {a: []string{"-s"}, err: eUsage + "\n"}, 70 {a: []string{"-s", "a"}, err: "a is not a valid signal\n"}, 71 {a: []string{"a"}, err: "Some processes could not be killed: [a: arguments must be process or job IDS]\n"}, 72 {a: []string{"--signal"}, err: eUsage + "\n"}, 73 {a: []string{"--signal", "a"}, err: "a is not a valid signal\n"}, 74 {a: []string{"-1", "a"}, err: "Some processes could not be killed: [a: arguments must be process or job IDS]\n"}, 75 } 76 ) 77 78 tmpDir, err := ioutil.TempDir("", "KillTest") 79 if err != nil { 80 t.Fatal("TempDir failed: ", err) 81 } 82 defer os.RemoveAll(tmpDir) 83 84 for _, v := range tab { 85 _, e, err := run(testutil.Command(t, v.a...)) 86 if e != v.err { 87 t.Errorf("Kill for '%v' failed: got '%s', want '%s'", v.a, e, v.err) 88 } 89 if err == nil { 90 t.Errorf("Kill for '%v' failed: got nil, want err", v.a) 91 } 92 } 93 } 94 95 func TestMain(m *testing.M) { 96 testutil.Run(m, main) 97 }