gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/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  	cmd := exec.Command("sleep", "10")
    36  	if err := cmd.Start(); err != nil {
    37  		t.Fatalf("Failed to start test process: %v", err)
    38  	}
    39  
    40  	// from the orignal. hokey .1 second wait for the process to start. Racy.
    41  	time.Sleep(100 * time.Millisecond)
    42  
    43  	if _, _, err := run(testutil.Command(t, "-9", fmt.Sprintf("%d", cmd.Process.Pid))); err != nil {
    44  		t.Errorf("Could not spawn first kill: %v", err)
    45  	}
    46  
    47  	if err := cmd.Wait(); err == nil {
    48  		t.Errorf("Test process succeeded, but expected to fail")
    49  	}
    50  
    51  	// now this is a little weird. We're going to try to kill it again.
    52  	// Arguably, this should be done in another test, but finding a process
    53  	// you just "know" does not exist is tricky. What PID do you use?
    54  	// So we just kill the one we just killed; it should get an error.
    55  	// If not, something's wrong.
    56  	if _, _, err := run(testutil.Command(t, "-9", fmt.Sprintf("%d", cmd.Process.Pid))); err == nil {
    57  		t.Fatalf("Second kill: got nil, want error")
    58  	}
    59  }
    60  
    61  func TestBadInvocations(t *testing.T) {
    62  	var (
    63  		tab = []struct {
    64  			a   []string
    65  			err string
    66  		}{
    67  			{a: []string{"-1w34"}, err: "1w34 is not a valid signal\n"},
    68  			{a: []string{"-s"}, err: eUsage + "\n"},
    69  			{a: []string{"-s", "a"}, err: "a is not a valid signal\n"},
    70  			{a: []string{"a"}, err: "Some processes could not be killed: [a: arguments must be process or job IDS]\n"},
    71  			{a: []string{"--signal"}, err: eUsage + "\n"},
    72  			{a: []string{"--signal", "a"}, err: "a is not a valid signal\n"},
    73  			{a: []string{"-1", "a"}, err: "Some processes could not be killed: [a: arguments must be process or job IDS]\n"},
    74  		}
    75  	)
    76  
    77  	tmpDir, err := ioutil.TempDir("", "KillTest")
    78  	if err != nil {
    79  		t.Fatal("TempDir failed: ", err)
    80  	}
    81  	defer os.RemoveAll(tmpDir)
    82  
    83  	for _, v := range tab {
    84  		_, e, err := run(testutil.Command(t, v.a...))
    85  		if e != v.err {
    86  			t.Errorf("Kill for '%v' failed: got '%s', want '%s'", v.a, e, v.err)
    87  		}
    88  		if err == nil {
    89  			t.Errorf("Kill for '%v' failed: got nil, want err", v.a)
    90  		}
    91  	}
    92  }
    93  
    94  func TestMain(m *testing.M) {
    95  	testutil.Run(m, main)
    96  }