container_top_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "reflect"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types/container"
  12. "golang.org/x/net/context"
  13. )
  14. func TestContainerTopError(t *testing.T) {
  15. client := &Client{
  16. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  17. }
  18. _, err := client.ContainerTop(context.Background(), "nothing", []string{})
  19. if err == nil || err.Error() != "Error response from daemon: Server error" {
  20. t.Fatalf("expected a Server Error, got %v", err)
  21. }
  22. }
  23. func TestContainerTop(t *testing.T) {
  24. expectedURL := "/containers/container_id/top"
  25. expectedProcesses := [][]string{
  26. {"p1", "p2"},
  27. {"p3"},
  28. }
  29. expectedTitles := []string{"title1", "title2"}
  30. client := &Client{
  31. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  32. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  33. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  34. }
  35. query := req.URL.Query()
  36. args := query.Get("ps_args")
  37. if args != "arg1 arg2" {
  38. return nil, fmt.Errorf("args not set in URL query properly. Expected 'arg1 arg2', got %v", args)
  39. }
  40. b, err := json.Marshal(container.ContainerTopOKBody{
  41. Processes: [][]string{
  42. {"p1", "p2"},
  43. {"p3"},
  44. },
  45. Titles: []string{"title1", "title2"},
  46. })
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &http.Response{
  51. StatusCode: http.StatusOK,
  52. Body: ioutil.NopCloser(bytes.NewReader(b)),
  53. }, nil
  54. }),
  55. }
  56. processList, err := client.ContainerTop(context.Background(), "container_id", []string{"arg1", "arg2"})
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. if !reflect.DeepEqual(expectedProcesses, processList.Processes) {
  61. t.Fatalf("Processes: expected %v, got %v", expectedProcesses, processList.Processes)
  62. }
  63. if !reflect.DeepEqual(expectedTitles, processList.Titles) {
  64. t.Fatalf("Titles: expected %v, got %v", expectedTitles, processList.Titles)
  65. }
  66. }