config_inspect_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/swarm"
  12. "github.com/docker/docker/errdefs"
  13. "github.com/pkg/errors"
  14. "gotest.tools/v3/assert"
  15. is "gotest.tools/v3/assert/cmp"
  16. )
  17. func TestConfigInspectNotFound(t *testing.T) {
  18. client := &Client{
  19. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  20. }
  21. _, _, err := client.ConfigInspectWithRaw(context.Background(), "unknown")
  22. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  23. }
  24. func TestConfigInspectWithEmptyID(t *testing.T) {
  25. client := &Client{
  26. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  27. return nil, errors.New("should not make request")
  28. }),
  29. }
  30. _, _, err := client.ConfigInspectWithRaw(context.Background(), "")
  31. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  32. }
  33. func TestConfigInspectUnsupported(t *testing.T) {
  34. client := &Client{
  35. version: "1.29",
  36. client: &http.Client{},
  37. }
  38. _, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
  39. assert.Check(t, is.Error(err, `"config inspect" requires API version 1.30, but the Docker daemon API version is 1.29`))
  40. }
  41. func TestConfigInspectError(t *testing.T) {
  42. client := &Client{
  43. version: "1.30",
  44. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  45. }
  46. _, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
  47. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  48. }
  49. func TestConfigInspectConfigNotFound(t *testing.T) {
  50. client := &Client{
  51. version: "1.30",
  52. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  53. }
  54. _, _, err := client.ConfigInspectWithRaw(context.Background(), "unknown")
  55. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  56. }
  57. func TestConfigInspect(t *testing.T) {
  58. expectedURL := "/v1.30/configs/config_id"
  59. client := &Client{
  60. version: "1.30",
  61. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  62. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  63. return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
  64. }
  65. content, err := json.Marshal(swarm.Config{
  66. ID: "config_id",
  67. })
  68. if err != nil {
  69. return nil, err
  70. }
  71. return &http.Response{
  72. StatusCode: http.StatusOK,
  73. Body: io.NopCloser(bytes.NewReader(content)),
  74. }, nil
  75. }),
  76. }
  77. configInspect, _, err := client.ConfigInspectWithRaw(context.Background(), "config_id")
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. if configInspect.ID != "config_id" {
  82. t.Fatalf("expected `config_id`, got %s", configInspect.ID)
  83. }
  84. }