14325: Configurable SSH target port for cloud VMs.
[arvados.git] / lib / dispatchcloud / ssh_executor / executor_test.go
index 619e47383fbfe3efe628c8f4768ed2a32087413e..526840b137c776529ec23ba2fc0710b1e6581ef4 100644 (file)
@@ -8,6 +8,7 @@ import (
        "bytes"
        "io"
        "io/ioutil"
+       "net"
        "sync"
        "testing"
        "time"
@@ -32,6 +33,25 @@ func (*testTarget) VerifyHostKey(ssh.PublicKey, *ssh.Client) error {
        return nil
 }
 
+// Address returns the wrapped SSHService's host, with the port
+// stripped. This ensures the executor won't work until
+// SetTargetPort() is called -- see (*testTarget)Port().
+func (tt *testTarget) Address() string {
+       h, _, err := net.SplitHostPort(tt.SSHService.Address())
+       if err != nil {
+               panic(err)
+       }
+       return h
+}
+
+func (tt *testTarget) Port() string {
+       _, p, err := net.SplitHostPort(tt.SSHService.Address())
+       if err != nil {
+               panic(err)
+       }
+       return p
+}
+
 type ExecutorSuite struct{}
 
 func (s *ExecutorSuite) TestExecute(c *check.C) {
@@ -77,6 +97,22 @@ func (s *ExecutorSuite) TestExecute(c *check.C) {
                exr := New(srv)
                exr.SetSigners(clientpriv)
 
+               // Use the default target port (ssh). Execute will
+               // return a connection error or an authentication
+               // error, depending on whether the test host is
+               // running an SSH server.
+               _, _, err = exr.Execute(nil, command, nil)
+               c.Check(err, check.ErrorMatches, `.*(unable to authenticate|connection refused).*`)
+
+               // Use a bogus target port. Execute will return a
+               // connection error.
+               exr.SetTargetPort("0")
+               _, _, err = exr.Execute(nil, command, nil)
+               c.Check(err, check.ErrorMatches, `.*connection refused.*`)
+
+               // Use the test server's listening port.
+               exr.SetTargetPort(srv.Port())
+
                done := make(chan bool)
                go func() {
                        stdout, stderr, err := exr.Execute(map[string]string{"TESTVAR": "test value"}, command, bytes.NewBufferString(stdinData))