convergence_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. moby "github.com/docker/docker/api/types"
  21. "github.com/docker/docker/api/types/filters"
  22. "github.com/golang/mock/gomock"
  23. "gotest.tools/assert"
  24. "github.com/docker/compose/v2/pkg/api"
  25. "github.com/docker/compose/v2/pkg/mocks"
  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 := composeService{
  68. dockerCli: cli,
  69. }
  70. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  71. s.Links = []string{"db"}
  72. c := testContainer("db", dbContainerName, false)
  73. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
  74. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  75. assert.NilError(t, err)
  76. assert.Equal(t, len(links), 3)
  77. assert.Equal(t, links[0], "testProject-db-1:db")
  78. assert.Equal(t, links[1], "testProject-db-1:db-1")
  79. assert.Equal(t, links[2], "testProject-db-1:testProject-db-1")
  80. })
  81. t.Run("service links", func(t *testing.T) {
  82. mockCtrl := gomock.NewController(t)
  83. defer mockCtrl.Finish()
  84. apiClient := mocks.NewMockAPIClient(mockCtrl)
  85. cli := mocks.NewMockCli(mockCtrl)
  86. tested := composeService{
  87. dockerCli: cli,
  88. }
  89. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  90. s.Links = []string{"db:db"}
  91. c := testContainer("db", dbContainerName, false)
  92. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
  93. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  94. assert.NilError(t, err)
  95. assert.Equal(t, len(links), 3)
  96. assert.Equal(t, links[0], "testProject-db-1:db")
  97. assert.Equal(t, links[1], "testProject-db-1:db-1")
  98. assert.Equal(t, links[2], "testProject-db-1:testProject-db-1")
  99. })
  100. t.Run("service links name", func(t *testing.T) {
  101. mockCtrl := gomock.NewController(t)
  102. defer mockCtrl.Finish()
  103. apiClient := mocks.NewMockAPIClient(mockCtrl)
  104. cli := mocks.NewMockCli(mockCtrl)
  105. tested := composeService{
  106. dockerCli: cli,
  107. }
  108. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  109. s.Links = []string{"db:dbname"}
  110. c := testContainer("db", dbContainerName, false)
  111. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
  112. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  113. assert.NilError(t, err)
  114. assert.Equal(t, len(links), 3)
  115. assert.Equal(t, links[0], "testProject-db-1:dbname")
  116. assert.Equal(t, links[1], "testProject-db-1:db-1")
  117. assert.Equal(t, links[2], "testProject-db-1:testProject-db-1")
  118. })
  119. t.Run("service links external links", func(t *testing.T) {
  120. mockCtrl := gomock.NewController(t)
  121. defer mockCtrl.Finish()
  122. apiClient := mocks.NewMockAPIClient(mockCtrl)
  123. cli := mocks.NewMockCli(mockCtrl)
  124. tested := composeService{
  125. dockerCli: cli,
  126. }
  127. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  128. s.Links = []string{"db:dbname"}
  129. s.ExternalLinks = []string{"db1:db2"}
  130. c := testContainer("db", dbContainerName, false)
  131. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
  132. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  133. assert.NilError(t, err)
  134. assert.Equal(t, len(links), 4)
  135. assert.Equal(t, links[0], "testProject-db-1:dbname")
  136. assert.Equal(t, links[1], "testProject-db-1:db-1")
  137. assert.Equal(t, links[2], "testProject-db-1:testProject-db-1")
  138. // ExternalLink
  139. assert.Equal(t, links[3], "db1:db2")
  140. })
  141. t.Run("service links itself oneoff", func(t *testing.T) {
  142. mockCtrl := gomock.NewController(t)
  143. defer mockCtrl.Finish()
  144. apiClient := mocks.NewMockAPIClient(mockCtrl)
  145. cli := mocks.NewMockCli(mockCtrl)
  146. tested := composeService{
  147. dockerCli: cli,
  148. }
  149. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  150. s.Links = []string{}
  151. s.ExternalLinks = []string{}
  152. s.Labels = s.Labels.Add(api.OneoffLabel, "True")
  153. c := testContainer("web", webContainerName, true)
  154. containerListOptionsOneOff := moby.ContainerListOptions{
  155. Filters: filters.NewArgs(
  156. projectFilter(testProject),
  157. serviceFilter("web"),
  158. oneOffFilter(false),
  159. ),
  160. All: true,
  161. }
  162. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptionsOneOff).Return([]moby.Container{c}, nil)
  163. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  164. assert.NilError(t, err)
  165. assert.Equal(t, len(links), 3)
  166. assert.Equal(t, links[0], "testProject-web-1:web")
  167. assert.Equal(t, links[1], "testProject-web-1:web-1")
  168. assert.Equal(t, links[2], "testProject-web-1:testProject-web-1")
  169. })
  170. }
  171. func TestWaitDependencies(t *testing.T) {
  172. mockCtrl := gomock.NewController(t)
  173. defer mockCtrl.Finish()
  174. apiClient := mocks.NewMockAPIClient(mockCtrl)
  175. cli := mocks.NewMockCli(mockCtrl)
  176. tested := composeService{
  177. dockerCli: cli,
  178. }
  179. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  180. t.Run("should skip dependencies with scale 0", func(t *testing.T) {
  181. dbService := types.ServiceConfig{Name: "db", Scale: 0}
  182. redisService := types.ServiceConfig{Name: "redis", Scale: 0}
  183. project := types.Project{Name: strings.ToLower(testProject), Services: []types.ServiceConfig{dbService, redisService}}
  184. dependencies := types.DependsOnConfig{
  185. "db": {Condition: ServiceConditionRunningOrHealthy},
  186. "redis": {Condition: ServiceConditionRunningOrHealthy},
  187. }
  188. assert.NilError(t, tested.waitDependencies(context.Background(), &project, dependencies))
  189. })
  190. t.Run("should skip dependencies with condition service_started", func(t *testing.T) {
  191. dbService := types.ServiceConfig{Name: "db", Scale: 1}
  192. redisService := types.ServiceConfig{Name: "redis", Scale: 1}
  193. project := types.Project{Name: strings.ToLower(testProject), Services: []types.ServiceConfig{dbService, redisService}}
  194. dependencies := types.DependsOnConfig{
  195. "db": {Condition: types.ServiceConditionStarted},
  196. "redis": {Condition: types.ServiceConditionStarted},
  197. }
  198. assert.NilError(t, tested.waitDependencies(context.Background(), &project, dependencies))
  199. })
  200. }