github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/command/remote_command_test.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	. "github.com/smartystreets/goconvey/convey"
     9  )
    10  
    11  func TestRemoteCommand(t *testing.T) {
    12  	t.Skip("skipping because local testing ssh configuration is not implemented")
    13  
    14  	Convey("With a remote command", t, func() {
    15  
    16  		Convey("failure and success should be detected", func() {
    17  			failCmd := &RemoteCommand{
    18  				CmdString:      "false",
    19  				Stdout:         ioutil.Discard,
    20  				Stderr:         ioutil.Discard,
    21  				RemoteHostName: TestRemote,
    22  				User:           TestRemoteUser,
    23  				Options:        []string{"-i", TestRemoteKey},
    24  			}
    25  			So(failCmd.Run(), ShouldNotBeNil)
    26  
    27  			trueCmd := &RemoteCommand{
    28  				CmdString:      "true",
    29  				Stdout:         ioutil.Discard,
    30  				Stderr:         ioutil.Discard,
    31  				RemoteHostName: TestRemote,
    32  				User:           TestRemoteUser,
    33  				Options:        []string{"-i", TestRemoteKey},
    34  			}
    35  			So(trueCmd.Run(), ShouldBeNil)
    36  		})
    37  
    38  		Convey("output should be passed appropriately to the stdout and stderr"+
    39  			" writers", func() {
    40  
    41  			stdout := &CacheLastWritten{}
    42  			stderr := &CacheLastWritten{}
    43  
    44  			command := &RemoteCommand{
    45  				CmdString:      "echo 'hi stdout'; echo 'hi stderr'>&2",
    46  				Stdout:         stdout,
    47  				Stderr:         stderr,
    48  				RemoteHostName: TestRemote,
    49  				User:           TestRemoteUser,
    50  				Options:        []string{"-i", TestRemoteKey},
    51  			}
    52  
    53  			// run the command, make sure the output was given to stdout
    54  			So(command.Run(), ShouldBeNil)
    55  			So(stdout.LastWritten, ShouldResemble, []byte("hi stdout\n"))
    56  			So(stderr.LastWritten, ShouldResemble, []byte("hi stderr\n"))
    57  
    58  		})
    59  
    60  		Convey("if the background option is set to true, the ssh connection"+
    61  			" should not wait for the command to finish", func() {
    62  
    63  			// this command would sleep for 30 years if it were waited for
    64  			sleepCmd := "sleep 1000000000"
    65  			command := &RemoteCommand{
    66  				CmdString:      sleepCmd,
    67  				Stdout:         ioutil.Discard,
    68  				Stderr:         ioutil.Discard,
    69  				RemoteHostName: TestRemote,
    70  				User:           TestRemoteUser,
    71  				Options:        []string{"-i", TestRemoteKey},
    72  				Background:     true,
    73  			}
    74  
    75  			// run the command, it should finish rather than sleeping forever
    76  			So(command.Run(), ShouldBeNil)
    77  
    78  			// clean up the sleeping process
    79  			cleanupCmd := &RemoteCommand{
    80  				CmdString:      fmt.Sprintf("pkill -xf '%v'", sleepCmd),
    81  				Stdout:         ioutil.Discard,
    82  				Stderr:         ioutil.Discard,
    83  				RemoteHostName: TestRemote,
    84  				User:           TestRemoteUser,
    85  				Options:        []string{"-i", TestRemoteKey},
    86  			}
    87  			So(cleanupCmd.Run(), ShouldBeNil)
    88  
    89  		})
    90  
    91  	})
    92  
    93  }