service_update_test.go 1.9 KB

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