info_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types/system"
  12. "github.com/docker/docker/errdefs"
  13. "gotest.tools/v3/assert"
  14. is "gotest.tools/v3/assert/cmp"
  15. )
  16. func TestInfoServerError(t *testing.T) {
  17. client := &Client{
  18. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  19. }
  20. _, err := client.Info(context.Background())
  21. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  22. }
  23. func TestInfoInvalidResponseJSONError(t *testing.T) {
  24. client := &Client{
  25. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  26. return &http.Response{
  27. StatusCode: http.StatusOK,
  28. Body: io.NopCloser(bytes.NewReader([]byte("invalid json"))),
  29. }, nil
  30. }),
  31. }
  32. _, err := client.Info(context.Background())
  33. if err == nil || !strings.Contains(err.Error(), "invalid character") {
  34. t.Fatalf("expected a 'invalid character' error, got %v", err)
  35. }
  36. }
  37. func TestInfo(t *testing.T) {
  38. expectedURL := "/info"
  39. client := &Client{
  40. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  41. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  42. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  43. }
  44. info := &system.Info{
  45. ID: "daemonID",
  46. Containers: 3,
  47. }
  48. b, err := json.Marshal(info)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return &http.Response{
  53. StatusCode: http.StatusOK,
  54. Body: io.NopCloser(bytes.NewReader(b)),
  55. }, nil
  56. }),
  57. }
  58. info, err := client.Info(context.Background())
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. if info.ID != "daemonID" {
  63. t.Fatalf("expected daemonID, got %s", info.ID)
  64. }
  65. if info.Containers != 3 {
  66. t.Fatalf("expected 3 containers, got %d", info.Containers)
  67. }
  68. }