func prepareCommand(session *ssh.Session, cmd *command) error { for _, env := range cmd.Env { variable := strings.Split(env, "=") if len(variable) != 2 { continue } if err := session.Setenv(variable[0], variable[1]); err != nil { return err } } if cmd.Stdout == nil { session.Stdout = &cmd.stdout } else { session.Stdout = cmd.Stdout } if cmd.Stdin != nil { session.Stdin = cmd.Stdin } if cmd.Stderr == nil { session.Stderr = &cmd.stderr } else { session.Stderr = cmd.Stderr } return nil }
//NewSSHClientSession creates a new ssh session instance func NewSSHClientSession(s *ssh.Session, in io.Reader) *SSHClientSession { out := new(bytes.Buffer) err := new(bytes.Buffer) s.Stdin = in s.Stdout = out s.Stderr = err return &SSHClientSession{s, in, out, err} }
BeforeEach(func() { err := session.Run("true") Expect(err).NotTo(HaveOccurred()) }) 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)