config_inspect_test.go 2.8 KB

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