config_update_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api/types/swarm"
  11. "github.com/docker/docker/errdefs"
  12. "gotest.tools/v3/assert"
  13. is "gotest.tools/v3/assert/cmp"
  14. )
  15. func TestConfigUpdateUnsupported(t *testing.T) {
  16. client := &Client{
  17. version: "1.29",
  18. client: &http.Client{},
  19. }
  20. err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
  21. assert.Check(t, is.Error(err, `"config update" requires API version 1.30, but the Docker daemon API version is 1.29`))
  22. }
  23. func TestConfigUpdateError(t *testing.T) {
  24. client := &Client{
  25. version: "1.30",
  26. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  27. }
  28. err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
  29. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  30. }
  31. func TestConfigUpdate(t *testing.T) {
  32. expectedURL := "/v1.30/configs/config_id/update"
  33. client := &Client{
  34. version: "1.30",
  35. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  36. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  37. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  38. }
  39. if req.Method != http.MethodPost {
  40. return nil, fmt.Errorf("expected POST method, got %s", req.Method)
  41. }
  42. return &http.Response{
  43. StatusCode: http.StatusOK,
  44. Body: io.NopCloser(bytes.NewReader([]byte("body"))),
  45. }, nil
  46. }),
  47. }
  48. err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. }