github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/plugin/builtin/shell/subtree_test.go (about)

     1  // +build !windows
     2  
     3  package shell
     4  
     5  import (
     6  	"bytes"
     7  	"fmt"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/evergreen-ci/evergreen/command"
    12  	"github.com/evergreen-ci/evergreen/plugin/plugintest"
    13  	. "github.com/smartystreets/goconvey/convey"
    14  )
    15  
    16  func TestSubtreeCleanup(t *testing.T) {
    17  	Convey("With a tracked long-running shell command", t, func() {
    18  		id := "testID"
    19  		buf := &bytes.Buffer{}
    20  		env := os.Environ()
    21  		env = append(env, "EVR_TASK_ID=bogus")
    22  		env = append(env, "EVR_AGENT_PID=12345")
    23  		env = append(env, fmt.Sprintf("EVR_TASK_ID=%v", id))
    24  		env = append(env, fmt.Sprintf("EVR_AGENT_PID=%v", os.Getpid()))
    25  		localCmd := &command.LocalCommand{
    26  			CmdString:   "while true; do sleep 1; done; echo 'finish'",
    27  			Stdout:      buf,
    28  			Stderr:      buf,
    29  			ScriptMode:  true,
    30  			Environment: env,
    31  		}
    32  		So(localCmd.Start(), ShouldBeNil)
    33  		trackProcess(id, localCmd.Cmd.Process.Pid, &plugintest.MockLogger{})
    34  
    35  		Convey("running KillSpawnedProcs should kill the process before it finishes", func() {
    36  			So(KillSpawnedProcs(id, &plugintest.MockLogger{}), ShouldBeNil)
    37  			So(localCmd.Cmd.Wait(), ShouldNotBeNil)
    38  			So(buf.String(), ShouldNotContainSubstring, "finish")
    39  		})
    40  	})
    41  }