convergence_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "strings"
  18. "testing"
  19. "github.com/compose-spec/compose-go/types"
  20. "github.com/docker/compose/v2/pkg/api"
  21. "github.com/docker/compose/v2/pkg/mocks"
  22. moby "github.com/docker/docker/api/types"
  23. "github.com/docker/docker/api/types/filters"
  24. "github.com/golang/mock/gomock"
  25. "gotest.tools/assert"
  26. )
  27. func TestContainerName(t *testing.T) {
  28. s := types.ServiceConfig{
  29. Name: "testservicename",
  30. ContainerName: "testcontainername",
  31. Scale: 1,
  32. Deploy: &types.DeployConfig{},
  33. }
  34. ret, err := getScale(s)
  35. assert.NilError(t, err)
  36. assert.Equal(t, ret, s.Scale)
  37. var zero uint64 // = 0
  38. s.Deploy.Replicas = &zero
  39. ret, err = getScale(s)
  40. assert.NilError(t, err)
  41. assert.Equal(t, ret, int(*s.Deploy.Replicas))
  42. var two uint64 = 2
  43. s.Deploy.Replicas = &two
  44. _, err = getScale(s)
  45. assert.Error(t, err, fmt.Sprintf(doubledContainerNameWarning, s.Name, s.ContainerName))
  46. }
  47. func TestServiceLinks(t *testing.T) {
  48. const dbContainerName = "/" + testProject + "-db-1"
  49. const webContainerName = "/" + testProject + "-web-1"
  50. s := types.ServiceConfig{
  51. Name: "web",
  52. Scale: 1,
  53. }
  54. containerListOptions := moby.ContainerListOptions{
  55. Filters: filters.NewArgs(
  56. projectFilter(testProject),
  57. serviceFilter("db"),
  58. oneOffFilter(false),
  59. ),
  60. All: true,
  61. }
  62. t.Run("service links default", func(t *testing.T) {
  63. mockCtrl := gomock.NewController(t)
  64. defer mockCtrl.Finish()
  65. apiClient := mocks.NewMockAPIClient(mockCtrl)
  66. cli := mocks.NewMockCli(mockCtrl)
  67. tested.dockerCli = cli
  68. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  69. s.Links = []string{"db"}
  70. c := testContainer("db", dbContainerName, false)
  71. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
  72. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  73. assert.NilError(t, err)
  74. assert.Equal(t, len(links), 3)
  75. assert.Equal(t, links[0], "testProject-db-1:db")
  76. assert.Equal(t, links[1], "testProject-db-1:db-1")
  77. assert.Equal(t, links[2], "testProject-db-1:testProject-db-1")
  78. })
  79. t.Run("service links", func(t *testing.T) {
  80. mockCtrl := gomock.NewController(t)
  81. defer mockCtrl.Finish()
  82. apiClient := mocks.NewMockAPIClient(mockCtrl)
  83. cli := mocks.NewMockCli(mockCtrl)
  84. tested.dockerCli = cli
  85. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  86. s.Links = []string{"db:db"}
  87. c := testContainer("db", dbContainerName, false)
  88. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
  89. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  90. assert.NilError(t, err)
  91. assert.Equal(t, len(links), 3)
  92. assert.Equal(t, links[0], "testProject-db-1:db")
  93. assert.Equal(t, links[1], "testProject-db-1:db-1")
  94. assert.Equal(t, links[2], "testProject-db-1:testProject-db-1")
  95. })
  96. t.Run("service links name", func(t *testing.T) {
  97. mockCtrl := gomock.NewController(t)
  98. defer mockCtrl.Finish()
  99. apiClient := mocks.NewMockAPIClient(mockCtrl)
  100. cli := mocks.NewMockCli(mockCtrl)
  101. tested.dockerCli = cli
  102. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  103. s.Links = []string{"db:dbname"}
  104. c := testContainer("db", dbContainerName, false)
  105. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
  106. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  107. assert.NilError(t, err)
  108. assert.Equal(t, len(links), 3)
  109. assert.Equal(t, links[0], "testProject-db-1:dbname")
  110. assert.Equal(t, links[1], "testProject-db-1:db-1")
  111. assert.Equal(t, links[2], "testProject-db-1:testProject-db-1")
  112. })
  113. t.Run("service links external links", func(t *testing.T) {
  114. mockCtrl := gomock.NewController(t)
  115. defer mockCtrl.Finish()
  116. apiClient := mocks.NewMockAPIClient(mockCtrl)
  117. cli := mocks.NewMockCli(mockCtrl)
  118. tested.dockerCli = cli
  119. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  120. s.Links = []string{"db:dbname"}
  121. s.ExternalLinks = []string{"db1:db2"}
  122. c := testContainer("db", dbContainerName, false)
  123. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
  124. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  125. assert.NilError(t, err)
  126. assert.Equal(t, len(links), 4)
  127. assert.Equal(t, links[0], "testProject-db-1:dbname")
  128. assert.Equal(t, links[1], "testProject-db-1:db-1")
  129. assert.Equal(t, links[2], "testProject-db-1:testProject-db-1")
  130. // ExternalLink
  131. assert.Equal(t, links[3], "db1:db2")
  132. })
  133. t.Run("service links itself oneoff", func(t *testing.T) {
  134. mockCtrl := gomock.NewController(t)
  135. defer mockCtrl.Finish()
  136. apiClient := mocks.NewMockAPIClient(mockCtrl)
  137. cli := mocks.NewMockCli(mockCtrl)
  138. tested.dockerCli = cli
  139. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  140. s.Links = []string{}
  141. s.ExternalLinks = []string{}
  142. s.Labels = s.Labels.Add(api.OneoffLabel, "True")
  143. c := testContainer("web", webContainerName, true)
  144. containerListOptionsOneOff := moby.ContainerListOptions{
  145. Filters: filters.NewArgs(
  146. projectFilter(testProject),
  147. serviceFilter("web"),
  148. oneOffFilter(false),
  149. ),
  150. All: true,
  151. }
  152. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptionsOneOff).Return([]moby.Container{c}, nil)
  153. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  154. assert.NilError(t, err)
  155. assert.Equal(t, len(links), 3)
  156. assert.Equal(t, links[0], "testProject-web-1:web")
  157. assert.Equal(t, links[1], "testProject-web-1:web-1")
  158. assert.Equal(t, links[2], "testProject-web-1:testProject-web-1")
  159. })
  160. }
  161. func TestWaitDependencies(t *testing.T) {
  162. mockCtrl := gomock.NewController(t)
  163. defer mockCtrl.Finish()
  164. apiClient := mocks.NewMockAPIClient(mockCtrl)
  165. cli := mocks.NewMockCli(mockCtrl)
  166. tested.dockerCli = cli
  167. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  168. t.Run("should skip dependencies with scale 0", func(t *testing.T) {
  169. dbService := types.ServiceConfig{Name: "db", Scale: 0}
  170. redisService := types.ServiceConfig{Name: "redis", Scale: 0}
  171. project := types.Project{Name: strings.ToLower(testProject), Services: []types.ServiceConfig{dbService, redisService}}
  172. dependencies := types.DependsOnConfig{
  173. "db": {Condition: ServiceConditionRunningOrHealthy},
  174. "redis": {Condition: ServiceConditionRunningOrHealthy},
  175. }
  176. assert.NilError(t, tested.waitDependencies(context.Background(), &project, dependencies))
  177. })
  178. t.Run("should skip dependencies with condition service_started", func(t *testing.T) {
  179. dbService := types.ServiceConfig{Name: "db", Scale: 1}
  180. redisService := types.ServiceConfig{Name: "redis", Scale: 1}
  181. project := types.Project{Name: strings.ToLower(testProject), Services: []types.ServiceConfig{dbService, redisService}}
  182. dependencies := types.DependsOnConfig{
  183. "db": {Condition: types.ServiceConditionStarted},
  184. "redis": {Condition: types.ServiceConditionStarted},
  185. }
  186. assert.NilError(t, tested.waitDependencies(context.Background(), &project, dependencies))
  187. })
  188. }