Example #1
0
func (client *SSHClient) RunCommandAndWait(cmd *SSHCommand) ([]byte, error) {
	var (
		session *ssh.Session
		err     error
		baData  []byte
	)

	if session, err = client.newSession(); err != nil {
		return nil, err
	}
	defer session.Close()

	baData, err = session.Output(cmd.Path)
	return baData, err
}
			It("uses the shell locator to find the default shell path", func() {
				Expect(shellLocator.ShellPathCallCount()).To(Equal(1))

				cmd := runner.StartArgsForCall(0)
				Expect(cmd.Path).To(Equal("/bin/sh"))
			})
		})

		Context("when stdin is provided by the client", func() {
			BeforeEach(func() {
				session.Stdin = strings.NewReader("Hello")
			})

			It("can use the session to execute a command that reads it", func() {
				result, err := session.Output("cat")
				Expect(err).NotTo(HaveOccurred())
				Expect(string(result)).To(Equal("Hello"))
			})
		})

		Context("when the command exits with a non-zero value", func() {
			It("it preserve the exit code", func() {
				err := session.Run("exit 3")
				Expect(err).To(HaveOccurred())

				exitErr, ok := err.(*ssh.ExitError)
				Expect(ok).To(BeTrue())
				Expect(exitErr.ExitStatus()).To(Equal(3))
			})
		})