Execute executes a command on a remote host viá SSH
(test TestCase)
| 50 | |
| 51 | // Execute executes a command on a remote host viá SSH |
| 52 | func (e SSHExecutor) Execute(test TestCase) TestResult { |
| 53 | if test.Command.InheritEnv { |
| 54 | panic("Inherit env is not supported viá SSH") |
| 55 | } |
| 56 | |
| 57 | // initialize auth methods with pass auth method as the default |
| 58 | authMethods := []ssh.AuthMethod{ |
| 59 | ssh.Password(e.Password), |
| 60 | } |
| 61 | |
| 62 | // add public key auth if identity file is given |
| 63 | if e.IdentityFile != "" { |
| 64 | signer := e.createSigner() |
| 65 | authMethods = append(authMethods, ssh.PublicKeys(signer)) |
| 66 | } |
| 67 | |
| 68 | // create ssh config |
| 69 | sshConf := &ssh.ClientConfig{ |
| 70 | User: e.User, |
| 71 | Auth: authMethods, |
| 72 | HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { |
| 73 | return nil |
| 74 | }, |
| 75 | } |
| 76 | |
| 77 | // create ssh connection |
| 78 | conn, err := ssh.Dial("tcp", e.Host, sshConf) |
| 79 | if err != nil { |
| 80 | log.Fatal(err) |
| 81 | } |
| 82 | |
| 83 | // start session |
| 84 | session, err := conn.NewSession() |
| 85 | defer session.Close() |
| 86 | if err != nil { |
| 87 | log.Fatal(err) |
| 88 | } |
| 89 | |
| 90 | var stdoutBuffer bytes.Buffer |
| 91 | var stderrBuffer bytes.Buffer |
| 92 | session.Stdout = &stdoutBuffer |
| 93 | session.Stderr = &stderrBuffer |
| 94 | |
| 95 | for k, v := range test.Command.Env { |
| 96 | err := session.Setenv(k, v) |
| 97 | if err != nil { |
| 98 | test.Result = CommandResult{ |
| 99 | Error: fmt.Errorf("Failed setting env variables, maybe ssh server is configured to only accept LC_ prefixed env variables. Error: %s", err), |
| 100 | } |
| 101 | return TestResult{ |
| 102 | TestCase: test, |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | dirCmd := "" |
| 108 | if test.Command.Dir != "" { |
| 109 | dirCmd = fmt.Sprintf("cd %s; ", test.Command.Dir) |
nothing calls this directly
no test coverage detected