service_create_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types"
  12. registrytypes "github.com/docker/docker/api/types/registry"
  13. "github.com/docker/docker/api/types/swarm"
  14. "github.com/opencontainers/image-spec/specs-go/v1"
  15. "golang.org/x/net/context"
  16. )
  17. func TestServiceCreateError(t *testing.T) {
  18. client := &Client{
  19. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  20. }
  21. _, err := client.ServiceCreate(context.Background(), swarm.ServiceSpec{}, types.ServiceCreateOptions{})
  22. if err == nil || err.Error() != "Error response from daemon: Server error" {
  23. t.Fatalf("expected a Server Error, got %v", err)
  24. }
  25. }
  26. func TestServiceCreate(t *testing.T) {
  27. expectedURL := "/services/create"
  28. client := &Client{
  29. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  30. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  31. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  32. }
  33. if req.Method != "POST" {
  34. return nil, fmt.Errorf("expected POST method, got %s", req.Method)
  35. }
  36. b, err := json.Marshal(types.ServiceCreateResponse{
  37. ID: "service_id",
  38. })
  39. if err != nil {
  40. return nil, err
  41. }
  42. return &http.Response{
  43. StatusCode: http.StatusOK,
  44. Body: ioutil.NopCloser(bytes.NewReader(b)),
  45. }, nil
  46. }),
  47. }
  48. r, err := client.ServiceCreate(context.Background(), swarm.ServiceSpec{}, types.ServiceCreateOptions{})
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if r.ID != "service_id" {
  53. t.Fatalf("expected `service_id`, got %s", r.ID)
  54. }
  55. }
  56. func TestServiceCreateCompatiblePlatforms(t *testing.T) {
  57. var (
  58. platforms []v1.Platform
  59. distributionInspectBody io.ReadCloser
  60. distributionInspect registrytypes.DistributionInspect
  61. )
  62. client := &Client{
  63. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  64. if strings.HasPrefix(req.URL.Path, "/services/create") {
  65. // check if the /distribution endpoint returned correct output
  66. err := json.NewDecoder(distributionInspectBody).Decode(&distributionInspect)
  67. if err != nil {
  68. return nil, err
  69. }
  70. if len(distributionInspect.Platforms) == 0 || distributionInspect.Platforms[0].Architecture != platforms[0].Architecture || distributionInspect.Platforms[0].OS != platforms[0].OS {
  71. return nil, fmt.Errorf("received incorrect platform information from registry")
  72. }
  73. b, err := json.Marshal(types.ServiceCreateResponse{
  74. ID: "service_" + platforms[0].Architecture,
  75. })
  76. if err != nil {
  77. return nil, err
  78. }
  79. return &http.Response{
  80. StatusCode: http.StatusOK,
  81. Body: ioutil.NopCloser(bytes.NewReader(b)),
  82. }, nil
  83. } else if strings.HasPrefix(req.URL.Path, "/distribution/") {
  84. platforms = []v1.Platform{
  85. {
  86. Architecture: "amd64",
  87. OS: "linux",
  88. },
  89. }
  90. b, err := json.Marshal(registrytypes.DistributionInspect{
  91. Descriptor: v1.Descriptor{},
  92. Platforms: platforms,
  93. })
  94. if err != nil {
  95. return nil, err
  96. }
  97. distributionInspectBody = ioutil.NopCloser(bytes.NewReader(b))
  98. return &http.Response{
  99. StatusCode: http.StatusOK,
  100. Body: ioutil.NopCloser(bytes.NewReader(b)),
  101. }, nil
  102. } else {
  103. return nil, fmt.Errorf("unexpected URL '%s'", req.URL.Path)
  104. }
  105. }),
  106. }
  107. r, err := client.ServiceCreate(context.Background(), swarm.ServiceSpec{}, types.ServiceCreateOptions{QueryRegistry: true})
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. if r.ID != "service_amd64" {
  112. t.Fatalf("expected `service_amd64`, got %s", r.ID)
  113. }
  114. }