func (s *S) TestSSHToContainerCmdRun(c *gocheck.C) { var closeClientConn func() server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/docker/ssh/af3332d" && r.Method == "GET" && r.Header.Get("Authorization") == "bearer abc123" { conn, _, err := w.(http.Hijacker).Hijack() c.Assert(err, gocheck.IsNil) conn.Write([]byte("hello my friend\n")) conn.Write([]byte("glad to see you here\n")) closeClientConn() } else { http.Error(w, "not found", http.StatusNotFound) } })) defer server.Close() closeClientConn = server.CloseClientConnections target := "http://" + server.Listener.Addr().String() targetRecover := ttesting.SetTargetFile(c, []byte(target)) defer ttesting.RollbackFile(targetRecover) tokenRecover := ttesting.SetTokenFile(c, []byte("abc123")) defer ttesting.RollbackFile(tokenRecover) var stdout, stderr, stdin bytes.Buffer context := cmd.Context{ Args: []string{"af3332d"}, Stdout: &stdout, Stderr: &stderr, Stdin: &stdin, } var command sshToContainerCmd err := command.Run(&context, nil) c.Assert(err, gocheck.IsNil) c.Assert(stdout.String(), gocheck.Equals, "hello my friend\nglad to see you here\n") }
func (s *S) TestSSHToContainerCmdLongNoNewLine(c *gocheck.C) { var closeClientConn func() expected := fmt.Sprintf("%0200s", "x") server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { conn, _, err := w.(http.Hijacker).Hijack() c.Assert(err, gocheck.IsNil) conn.Write([]byte(expected)) closeClientConn() })) defer server.Close() closeClientConn = server.CloseClientConnections target := "http://" + server.Listener.Addr().String() targetRecover := ttesting.SetTargetFile(c, []byte(target)) defer ttesting.RollbackFile(targetRecover) var stdout, stderr, stdin bytes.Buffer context := cmd.Context{ Args: []string{"af3332d"}, Stdout: &stdout, Stderr: &stderr, Stdin: &stdin, } var command sshToContainerCmd err := command.Run(&context, nil) c.Assert(err, gocheck.IsNil) c.Assert(stdout.String(), gocheck.Equals, expected) }
func (s *S) TearDownSuite(c *gocheck.C) { coll := collection() defer coll.Close() err := coll.Database.DropDatabase() c.Assert(err, gocheck.IsNil) tTesting.RollbackFile(s.targetRecover) s.storage.Apps().Database.DropDatabase() s.storage.Close() app.Provisioner = s.oldProvisioner }
func (s *S) TestSSHToContainerCmdConnectionRefused(c *gocheck.C) { server := httptest.NewServer(nil) addr := server.Listener.Addr().String() server.Close() targetRecover := ttesting.SetTargetFile(c, []byte("http://"+addr)) defer ttesting.RollbackFile(targetRecover) tokenRecover := ttesting.SetTokenFile(c, []byte("abc123")) defer ttesting.RollbackFile(tokenRecover) var buf bytes.Buffer context := cmd.Context{ Args: []string{"af3332d"}, Stdout: &buf, Stderr: &buf, Stdin: &buf, } var command sshToContainerCmd err := command.Run(&context, nil) c.Assert(err, gocheck.NotNil) opErr, ok := err.(*net.OpError) c.Assert(ok, gocheck.Equals, true) c.Assert(opErr.Net, gocheck.Equals, "tcp") c.Assert(opErr.Op, gocheck.Equals, "dial") c.Assert(opErr.Addr.String(), gocheck.Equals, addr) }
func (s *S) TestSSHToContainerCmdNoToken(c *gocheck.C) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.Error(w, "You must provide a valid Authorization header", http.StatusUnauthorized) })) defer server.Close() target := "http://" + server.Listener.Addr().String() targetRecover := ttesting.SetTargetFile(c, []byte(target)) defer ttesting.RollbackFile(targetRecover) var buf bytes.Buffer context := cmd.Context{ Args: []string{"af3332d"}, Stdout: &buf, Stderr: &buf, Stdin: &buf, } var command sshToContainerCmd err := command.Run(&context, nil) c.Assert(err, gocheck.FitsTypeOf, &errors.HTTP{}) httpErr := err.(*errors.HTTP) c.Assert(httpErr.Code, gocheck.Equals, 401) c.Assert(httpErr.Message, gocheck.Equals, "HTTP/1.1 401 Unauthorized") }
func (s *S) TearDownSuite(c *gocheck.C) { tTesting.RollbackFile(s.recover) }