config_update_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package client
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "testing"
  9. "github.com/docker/docker/api/types/swarm"
  10. "github.com/stretchr/testify/assert"
  11. "golang.org/x/net/context"
  12. )
  13. func TestConfigUpdateUnsupported(t *testing.T) {
  14. client := &Client{
  15. version: "1.29",
  16. client: &http.Client{},
  17. }
  18. err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
  19. assert.EqualError(t, err, `"config update" requires API version 1.30, but the Docker daemon API version is 1.29`)
  20. }
  21. func TestConfigUpdateError(t *testing.T) {
  22. client := &Client{
  23. version: "1.30",
  24. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  25. }
  26. err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
  27. if err == nil || err.Error() != "Error response from daemon: Server error" {
  28. t.Fatalf("expected a Server Error, got %v", err)
  29. }
  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 != "POST" {
  40. return nil, fmt.Errorf("expected POST method, got %s", req.Method)
  41. }
  42. return &http.Response{
  43. StatusCode: http.StatusOK,
  44. Body: ioutil.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. }