service_update_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/api/types/swarm"
  12. "github.com/docker/docker/errdefs"
  13. )
  14. func TestServiceUpdateError(t *testing.T) {
  15. client := &Client{
  16. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  17. }
  18. _, err := client.ServiceUpdate(context.Background(), "service_id", swarm.Version{}, swarm.ServiceSpec{}, types.ServiceUpdateOptions{})
  19. if err == nil || err.Error() != "Error response from daemon: Server error" {
  20. t.Fatalf("expected a Server Error, got %v", err)
  21. }
  22. if !errdefs.IsSystem(err) {
  23. t.Fatalf("expected a Server Error, got %T", err)
  24. }
  25. }
  26. func TestServiceUpdate(t *testing.T) {
  27. expectedURL := "/services/service_id/update"
  28. updateCases := []struct {
  29. swarmVersion swarm.Version
  30. expectedVersion string
  31. }{
  32. {
  33. expectedVersion: "0",
  34. },
  35. {
  36. swarmVersion: swarm.Version{
  37. Index: 0,
  38. },
  39. expectedVersion: "0",
  40. },
  41. {
  42. swarmVersion: swarm.Version{
  43. Index: 10,
  44. },
  45. expectedVersion: "10",
  46. },
  47. }
  48. for _, updateCase := range updateCases {
  49. client := &Client{
  50. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  51. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  52. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  53. }
  54. if req.Method != "POST" {
  55. return nil, fmt.Errorf("expected POST method, got %s", req.Method)
  56. }
  57. version := req.URL.Query().Get("version")
  58. if version != updateCase.expectedVersion {
  59. return nil, fmt.Errorf("version not set in URL query properly, expected '%s', got %s", updateCase.expectedVersion, version)
  60. }
  61. return &http.Response{
  62. StatusCode: http.StatusOK,
  63. Body: ioutil.NopCloser(bytes.NewReader([]byte("{}"))),
  64. }, nil
  65. }),
  66. }
  67. _, err := client.ServiceUpdate(context.Background(), "service_id", updateCase.swarmVersion, swarm.ServiceSpec{}, types.ServiceUpdateOptions{})
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. }
  72. }